1 | // |
---|
2 | // Lol Engine |
---|
3 | // |
---|
4 | // Copyright: (c) 2010-2011 Sam Hocevar <sam@hocevar.net> |
---|
5 | // This program is free software; you can redistribute it and/or |
---|
6 | // modify it under the terms of the Do What The Fuck You Want To |
---|
7 | // Public License, Version 2, as published by Sam Hocevar. See |
---|
8 | // http://sam.zoy.org/projects/COPYING.WTFPL for more details. |
---|
9 | // |
---|
10 | |
---|
11 | #if defined HAVE_CONFIG_H |
---|
12 | # include "config.h" |
---|
13 | #endif |
---|
14 | |
---|
15 | #include <cstdio> |
---|
16 | |
---|
17 | #include "core.h" |
---|
18 | |
---|
19 | /* |
---|
20 | * Tiler implementation class |
---|
21 | */ |
---|
22 | |
---|
23 | static class TilerData |
---|
24 | { |
---|
25 | friend class Tiler; |
---|
26 | |
---|
27 | public: |
---|
28 | TilerData() |
---|
29 | #if !FINAL_RELEASE |
---|
30 | : lasterror(-1) |
---|
31 | #endif |
---|
32 | { } |
---|
33 | |
---|
34 | private: |
---|
35 | Dict tilesets; |
---|
36 | #if !FINAL_RELEASE |
---|
37 | int lasterror; |
---|
38 | #endif |
---|
39 | } |
---|
40 | tilerdata; |
---|
41 | |
---|
42 | static TilerData * const data = &tilerdata; |
---|
43 | |
---|
44 | /* |
---|
45 | * Public Tiler class |
---|
46 | */ |
---|
47 | |
---|
48 | int Tiler::Register(char const *path, int2 size, int2 count, float dilate) |
---|
49 | { |
---|
50 | int id = data->tilesets.MakeSlot(path); |
---|
51 | |
---|
52 | if (!data->tilesets.GetEntity(id)) |
---|
53 | { |
---|
54 | TileSet *tileset = new TileSet(path, size, count, dilate); |
---|
55 | data->tilesets.SetEntity(id, tileset); |
---|
56 | #if !FINAL_RELEASE |
---|
57 | if (id == data->lasterror) |
---|
58 | data->lasterror = -1; |
---|
59 | #endif |
---|
60 | } |
---|
61 | |
---|
62 | return id + 1; /* ID 0 is for the empty tileset */ |
---|
63 | } |
---|
64 | |
---|
65 | void Tiler::Deregister(int id) |
---|
66 | { |
---|
67 | data->tilesets.RemoveSlot(id - 1); /* ID 0 is for the empty tileset */ |
---|
68 | } |
---|
69 | |
---|
70 | int2 Tiler::GetSize(int id) |
---|
71 | { |
---|
72 | TileSet *tileset = (TileSet *)data->tilesets.GetEntity(id - 1); |
---|
73 | #if !FINAL_RELEASE |
---|
74 | if (!tileset) |
---|
75 | { |
---|
76 | fprintf(stderr, "ERROR: getting size for null tiler #%i\n", id); |
---|
77 | return 0; |
---|
78 | } |
---|
79 | #endif |
---|
80 | return tileset->GetSize(); |
---|
81 | } |
---|
82 | |
---|
83 | int2 Tiler::GetCount(int id) |
---|
84 | { |
---|
85 | TileSet *tileset = (TileSet *)data->tilesets.GetEntity(id - 1); |
---|
86 | #if !FINAL_RELEASE |
---|
87 | if (!tileset) |
---|
88 | { |
---|
89 | fprintf(stderr, "ERROR: getting count for null tiler #%i\n", id); |
---|
90 | return 0; |
---|
91 | } |
---|
92 | #endif |
---|
93 | return tileset->GetCount(); |
---|
94 | } |
---|
95 | |
---|
96 | void 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 | TileSet *tileset = (TileSet *)data->tilesets.GetEntity(id); |
---|
101 | #if !FINAL_RELEASE |
---|
102 | if (!tileset) |
---|
103 | { |
---|
104 | if (id != data->lasterror) |
---|
105 | fprintf(stderr, "ERROR: blitting to null tiler #%i\n", id); |
---|
106 | data->lasterror = id; |
---|
107 | return; |
---|
108 | } |
---|
109 | #endif |
---|
110 | tileset->BlitTile(code & 0xffff, x, y, z, o); |
---|
111 | } |
---|
112 | |
---|