source: trunk/src/scene.cpp @ 133

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

Fix z-sorting of tiles. For now.

  • Property svn:keywords set to Id
File size: 1.5 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 <stdlib.h>
11
12#include "scene.h"
13#include "tiler.h"
14
15struct Tile
16{
17    uint32_t prio, code;
18    int x, y;
19};
20
21/*
22 * Scene implementation class
23 */
24
25class SceneData
26{
27    friend class Scene;
28
29private:
30    static int Compare(void const *p1, void const *p2)
31    {
32        Tile const *t1 = (Tile const *)p1;
33        Tile const *t2 = (Tile const *)p2;
34
35        return t2->prio - t1->prio;
36    }
37
38    Tile *tiles;
39    int ntiles;
40};
41
42/*
43 * Public Scene class
44 */
45
46Scene::Scene()
47{
48    data = new SceneData();
49    data->tiles = 0;
50    data->ntiles = 0;
51}
52
53Scene::~Scene()
54{
55    delete data;
56}
57
58void Scene::AddTile(uint32_t code, int x, int y, int z, int o)
59{
60    if ((data->ntiles % 1024) == 0)
61        data->tiles = (Tile *)realloc(data->tiles,
62                                      (data->ntiles + 1024) * sizeof(Tile));
63    data->tiles[data->ntiles].prio = y - 2 * 32 * z + (o ? 0 : 32);
64    data->tiles[data->ntiles].code = code;
65    data->tiles[data->ntiles].x = x;
66    data->tiles[data->ntiles].y = y;
67    data->ntiles++;
68}
69
70void Scene::Render() // XXX: rename to Blit()
71{
72    qsort(data->tiles, data->ntiles, sizeof(Tile), SceneData::Compare);
73
74    for (int i = 0; i < data->ntiles; i++)
75        Tiler::Render(data->tiles[i].code, data->tiles[i].x, data->tiles[i].y);
76
77    free(data->tiles);
78    data->tiles = 0;
79    data->ntiles = 0;
80}
81
Note: See TracBrowser for help on using the repository browser.