source: trunk/src/tiler.cpp @ 138

Last change on this file since 138 was 138, checked in by sam, 11 years ago

Start to handle differences between vertical and horizontal tiles.

  • Property svn:keywords set to Id
File size: 2.0 KB
Line 
1//
2// Deus Hax (working title)
3// Copyright (c) 2010 Sam Hocevar <sam@hocevar.net>
4//
5
6#if defined HAVE_CONFIG_H
7#   include "config.h"
8#endif
9
10#include <cstring>
11#include <cstdio>
12#include <cstdlib>
13
14#include "tiler.h"
15#include "tileset.h"
16
17#if defined WIN32
18#   define strcasecmp _stricmp
19#endif
20
21/*
22 * Tiler implementation class
23 */
24
25static class TilerData
26{
27    friend class Tiler;
28
29public:
30    TilerData() :
31        tilesets(0),
32        ntilesets(0)
33    {
34        /* Nothing to do */
35    }
36
37    ~TilerData()
38    {
39        if (ntilesets)
40            fprintf(stderr, "ERROR: still %i tilesets in tiler\n", ntilesets);
41        free(tilesets);
42    }
43
44private:
45    TileSet **tilesets;
46    int ntilesets;
47}
48tilerdata;
49
50static TilerData * const data = &tilerdata;
51
52/*
53 * Public Tiler class
54 */
55
56int Tiler::Register(char const *path)
57{
58    int id, empty = -1;
59
60    /* If the tileset is already registered, remember its ID. Look for an
61     * empty slot at the same time. */
62    for (id = 0; id < data->ntilesets; id++)
63    {
64        TileSet *t = data->tilesets[id];
65        if (!t)
66            empty = id;
67        else if (!strcasecmp(path, t->GetName()))
68            break;
69    }
70
71    /* If this is a new tileset, create a new one. */
72    if (id == data->ntilesets)
73    {
74        if (empty == -1)
75        {
76            empty = data->ntilesets++;
77            data->tilesets = (TileSet **)realloc(data->tilesets,
78                                         data->ntilesets * sizeof(TileSet *));
79        }
80
81        data->tilesets[empty] = new TileSet(path);
82        id = empty;
83    }
84
85    data->tilesets[id]->Ref();
86    return id + 1; /* ID 0 is for the empty tileset */
87}
88
89void Tiler::Deregister(int id)
90{
91    --id; /* ID 0 is for the empty tileset */
92
93    if (data->tilesets[id]->Unref() == 0)
94        data->tilesets[id] = NULL;
95}
96
97void Tiler::BlitTile(uint32_t code, int x, int y, int z, int o)
98{
99    int id = (code >> 16) - 1; /* ID 0 is for the empty tileset */
100
101    data->tilesets[id]->BlitTile(code & 0xffff, x, y, z, o);
102}
103
Note: See TracBrowser for help on using the repository browser.