source: trunk/src/scene.cpp @ 147

Last change on this file since 147 was 147, checked in by sam, 10 years ago

Rename "asset" to "entity". It's way cooler.

  • Property svn:keywords set to Id
File size: 2.4 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 <cstdlib>
11#include <cmath>
12
13#ifdef WIN32
14#   define WIN32_LEAN_AND_MEAN
15#   include <windows.h>
16#endif
17#if defined __APPLE__ && defined __MACH__
18#   include <OpenGL/gl.h>
19#else
20#   define GL_GLEXT_PROTOTYPES
21#   include <GL/gl.h>
22#endif
23
24#include "scene.h"
25#include "tiler.h"
26
27struct Tile
28{
29    uint32_t prio, code;
30    int x, y, z, o;
31};
32
33/*
34 * Scene implementation class
35 */
36
37class SceneData
38{
39    friend class Scene;
40
41private:
42    static int Compare(void const *p1, void const *p2)
43    {
44        Tile const *t1 = (Tile const *)p1;
45        Tile const *t2 = (Tile const *)p2;
46
47        return t2->prio - t1->prio;
48    }
49
50    Tile *tiles;
51    int ntiles;
52};
53
54/*
55 * Public Scene class
56 */
57
58Scene::Scene()
59{
60    data = new SceneData();
61    data->tiles = 0;
62    data->ntiles = 0;
63}
64
65Scene::~Scene()
66{
67    delete data;
68}
69
70void Scene::AddTile(uint32_t code, int x, int y, int z, int o)
71{
72    if ((data->ntiles % 1024) == 0)
73        data->tiles = (Tile *)realloc(data->tiles,
74                                      (data->ntiles + 1024) * sizeof(Tile));
75    data->tiles[data->ntiles].prio = -y - 2 * 32 * z + (o ? 0 : 32);
76    data->tiles[data->ntiles].code = code;
77    data->tiles[data->ntiles].x = x;
78    data->tiles[data->ntiles].y = y;
79    data->tiles[data->ntiles].z = z;
80    data->tiles[data->ntiles].o = o;
81    data->ntiles++;
82}
83
84void Scene::Render() // XXX: rename to Blit()
85{
86#if 0
87    // Randomise, then sort.
88    for (int i = 0; i < data->ntiles; i++)
89    {
90        Tile tmp = data->tiles[i];
91        int j = rand() % data->ntiles;
92        data->tiles[i] = data->tiles[j];
93        data->tiles[j] = tmp;
94    }
95#endif
96    qsort(data->tiles, data->ntiles, sizeof(Tile), SceneData::Compare);
97
98    // XXX: debug stuff
99    glPushMatrix();
100    static float f = 0.0f;
101    f += 0.05f;
102    glTranslatef(320.0f, 240.0f, 0.0f);
103    glRotatef(45.0f, 1.0f, 0.0f, 0.0f);
104#if 0
105    glRotatef(3.0f * sinf(f), 1.0f, 0.0f, 0.0f);
106    glRotatef(8.0f * cosf(f), 0.0f, 0.0f, 1.0f);
107#endif
108    glTranslatef(-320.0f, -240.0f, 0.0f);
109
110    for (int i = 0; i < data->ntiles; i++)
111        Tiler::BlitTile(data->tiles[i].code, data->tiles[i].x,
112                        data->tiles[i].y, data->tiles[i].z, data->tiles[i].o);
113
114    glPopMatrix();
115    // XXX: end of debug stuff
116
117    free(data->tiles);
118    data->tiles = 0;
119    data->ntiles = 0;
120}
121
Note: See TracBrowser for help on using the repository browser.