source: trunk/src/tiler.cpp @ 150

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

Replace mouse scrolling with a joystick simulated by the e/s/d/f keys.

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