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