source: trunk/src/tiler.cpp @ 787

Last change on this file since 787 was 787, checked in by sam, 9 years ago

tiler: Tiler::GetSize can now return a different value for different tiles.

  • Property svn:keywords set to Id
File size: 2.7 KB
Line 
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 "core.h"
16
17namespace lol
18{
19
20/*
21 * Tiler implementation class
22 */
23
24static class TilerData
25{
26    friend class Tiler;
27
28public:
29    TilerData()
30#if !LOL_RELEASE
31      : lasterror(-1)
32#endif
33    { }
34
35private:
36    Dict tilesets;
37#if !LOL_RELEASE
38    int lasterror;
39#endif
40}
41tilerdata;
42
43static TilerData * const data = &tilerdata;
44
45/*
46 * Public Tiler class
47 */
48
49int Tiler::Register(char const *path, vec2i size, vec2i count, float dilate)
50{
51    int id = data->tilesets.MakeSlot(path);
52
53    if (!data->tilesets.GetEntity(id))
54    {
55        TileSet *tileset = new TileSet(path, size, count, dilate);
56        data->tilesets.SetEntity(id, tileset);
57#if !LOL_RELEASE
58        if (id == data->lasterror)
59            data->lasterror = -1;
60#endif
61    }
62
63    return id + 1; /* ID 0 is for the empty tileset */
64}
65
66void Tiler::Deregister(int id)
67{
68    data->tilesets.RemoveSlot(id - 1); /* ID 0 is for the empty tileset */
69}
70
71vec2i Tiler::GetCount(int id)
72{
73    TileSet *tileset = (TileSet *)data->tilesets.GetEntity(id - 1);
74#if !LOL_RELEASE
75    if (!tileset)
76    {
77        Log::Error("getting count for null tiler #%i\n", id);
78        return 0;
79    }
80#endif
81    return tileset->GetCount();
82}
83
84vec2i Tiler::GetSize(int id, int tileid)
85{
86    TileSet *tileset = (TileSet *)data->tilesets.GetEntity(id - 1);
87#if !LOL_RELEASE
88    if (!tileset)
89    {
90        Log::Error("getting size for null tiler #%i\n", id);
91        return 0;
92    }
93#endif
94    return tileset->GetSize(tileid);
95}
96
97void Tiler::Bind(uint32_t code)
98{
99    int id = (code >> 16) - 1; /* ID 0 is for the empty tileset */
100
101    TileSet *tileset = (TileSet *)data->tilesets.GetEntity(id);
102#if !LOL_RELEASE
103    if (!tileset)
104    {
105        if (id != data->lasterror)
106            Log::Error("binding null tiler #%i\n", id);
107        data->lasterror = id;
108        return;
109    }
110#endif
111    tileset->Bind();
112}
113
114void Tiler::BlitTile(uint32_t code, int x, int y, int z, int o,
115                     float *vertex, float *texture)
116{
117    int id = (code >> 16) - 1; /* ID 0 is for the empty tileset */
118
119    TileSet *tileset = (TileSet *)data->tilesets.GetEntity(id);
120#if !LOL_RELEASE
121    if (!tileset)
122    {
123        if (id != data->lasterror)
124            Log::Error("blitting to null tiler #%i\n", id);
125        data->lasterror = id;
126        return;
127    }
128#endif
129    tileset->BlitTile(code & 0xffff, x, y, z, o, vertex, texture);
130}
131
132} /* namespace lol */
133
Note: See TracBrowser for help on using the repository browser.