source: trunk/src/tiler.cpp @ 664

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

Get rid of float3, float4, int3 etc. in favour of GLSL types.

  • 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 <cstdio>
16
17#include "core.h"
18
19/*
20 * Tiler implementation class
21 */
22
23static class TilerData
24{
25    friend class Tiler;
26
27public:
28    TilerData()
29#if !LOL_RELEASE
30      : lasterror(-1)
31#endif
32    { }
33
34private:
35    Dict tilesets;
36#if !LOL_RELEASE
37    int lasterror;
38#endif
39}
40tilerdata;
41
42static TilerData * const data = &tilerdata;
43
44/*
45 * Public Tiler class
46 */
47
48int Tiler::Register(char const *path, vec2i size, vec2i 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 !LOL_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
65void Tiler::Deregister(int id)
66{
67    data->tilesets.RemoveSlot(id - 1); /* ID 0 is for the empty tileset */
68}
69
70vec2i Tiler::GetSize(int id)
71{
72    TileSet *tileset = (TileSet *)data->tilesets.GetEntity(id - 1);
73#if !LOL_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
83vec2i Tiler::GetCount(int id)
84{
85    TileSet *tileset = (TileSet *)data->tilesets.GetEntity(id - 1);
86#if !LOL_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
96void Tiler::Bind(uint32_t code)
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 !LOL_RELEASE
102    if (!tileset)
103    {
104        if (id != data->lasterror)
105            fprintf(stderr, "ERROR: binding null tiler #%i\n", id);
106        data->lasterror = id;
107        return;
108    }
109#endif
110    tileset->Bind();
111}
112
113void Tiler::BlitTile(uint32_t code, int x, int y, int z, int o,
114                     float *vertex, float *texture)
115{
116    int id = (code >> 16) - 1; /* ID 0 is for the empty tileset */
117
118    TileSet *tileset = (TileSet *)data->tilesets.GetEntity(id);
119#if !LOL_RELEASE
120    if (!tileset)
121    {
122        if (id != data->lasterror)
123            fprintf(stderr, "ERROR: blitting to null tiler #%i\n", id);
124        data->lasterror = id;
125        return;
126    }
127#endif
128    tileset->BlitTile(code & 0xffff, x, y, z, o, vertex, texture);
129}
130
Note: See TracBrowser for help on using the repository browser.