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