1 | // Test stuff |
---|
2 | |
---|
3 | #ifdef WIN32 |
---|
4 | # define WIN32_LEAN_AND_MEAN |
---|
5 | # include <windows.h> |
---|
6 | #endif |
---|
7 | #if defined __APPLE__ && defined __MACH__ |
---|
8 | # include <OpenGL/gl.h> |
---|
9 | #else |
---|
10 | # define GL_GLEXT_PROTOTYPES |
---|
11 | # include <GL/gl.h> |
---|
12 | # include <GL/glext.h> |
---|
13 | #endif |
---|
14 | |
---|
15 | #include <SDL.h> |
---|
16 | #include <SDL_image.h> |
---|
17 | |
---|
18 | #include <math.h> |
---|
19 | |
---|
20 | int frames; |
---|
21 | |
---|
22 | /* Storage for one texture */ |
---|
23 | GLuint texture[1]; |
---|
24 | |
---|
25 | /* Storage for 3 vertex buffers */ |
---|
26 | GLuint buflist[3]; |
---|
27 | |
---|
28 | // Load Bitmaps And Convert To Textures |
---|
29 | void LoadGLTextures(void) |
---|
30 | { |
---|
31 | SDL_Surface *image1 = IMG_Load("art/test/groundtest.png"); |
---|
32 | |
---|
33 | if (!image1) |
---|
34 | { |
---|
35 | SDL_Quit(); |
---|
36 | exit(1); |
---|
37 | } |
---|
38 | |
---|
39 | glGenTextures(1, &texture[0]); |
---|
40 | glBindTexture(GL_TEXTURE_2D, texture[0]); |
---|
41 | |
---|
42 | glTexImage2D(GL_TEXTURE_2D, 0, 4, image1->w, image1->h, 0, |
---|
43 | GL_RGBA, GL_UNSIGNED_BYTE, image1->pixels); |
---|
44 | |
---|
45 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
---|
46 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
---|
47 | }; |
---|
48 | |
---|
49 | void MakeVBOs(void) |
---|
50 | { |
---|
51 | glGenBuffers(3, buflist); |
---|
52 | } |
---|
53 | |
---|
54 | void InitGL(int Width, int Height) |
---|
55 | { |
---|
56 | // Resize method |
---|
57 | glViewport(0, 0, Width, Height); |
---|
58 | glMatrixMode(GL_PROJECTION); |
---|
59 | glLoadIdentity(); |
---|
60 | glOrtho(0, Width, Height, 0, -1, 10); |
---|
61 | glMatrixMode(GL_MODELVIEW); |
---|
62 | glLoadIdentity(); |
---|
63 | |
---|
64 | // Init method |
---|
65 | glEnable(GL_TEXTURE_2D); |
---|
66 | LoadGLTextures(); |
---|
67 | MakeVBOs(); |
---|
68 | glShadeModel(GL_SMOOTH); |
---|
69 | glClearColor(0.0f, 0.0f, 0.0f, 0.0f); |
---|
70 | glClearDepth(1.0); |
---|
71 | glEnable(GL_DEPTH_TEST); |
---|
72 | glDepthFunc(GL_LEQUAL); |
---|
73 | glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); |
---|
74 | |
---|
75 | glEnable(GL_BLEND); |
---|
76 | glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); |
---|
77 | } |
---|
78 | |
---|
79 | void PutMap(int const *themap) |
---|
80 | { |
---|
81 | // Put map |
---|
82 | float uvs[8 * 20 * 15]; |
---|
83 | |
---|
84 | for (int y = 0; y < 15; y++) |
---|
85 | for (int x = 0; x < 20; x++) |
---|
86 | { |
---|
87 | int tile = themap[20 * y + x]; |
---|
88 | float ty = .0625f * (tile / 16); |
---|
89 | float tx = .0625f * (tile % 16); |
---|
90 | uvs[8 * (20 * y + x) + 0] = tx; |
---|
91 | uvs[8 * (20 * y + x) + 1] = ty; |
---|
92 | uvs[8 * (20 * y + x) + 2] = tx + .0625f; |
---|
93 | uvs[8 * (20 * y + x) + 3] = ty; |
---|
94 | uvs[8 * (20 * y + x) + 4] = tx + .0625f; |
---|
95 | uvs[8 * (20 * y + x) + 5] = ty + .0625f; |
---|
96 | uvs[8 * (20 * y + x) + 6] = tx; |
---|
97 | uvs[8 * (20 * y + x) + 7] = ty + .0625f; |
---|
98 | } |
---|
99 | glBindBuffer(GL_ARRAY_BUFFER, buflist[1]); |
---|
100 | glBufferData(GL_ARRAY_BUFFER, |
---|
101 | 8 * 20 * 15 * sizeof(float), uvs, GL_STATIC_DRAW); |
---|
102 | |
---|
103 | float vertices[8 * 20 * 15]; |
---|
104 | for (int y = 0; y < 15; y++) |
---|
105 | for (int x = 0; x < 20; x++) |
---|
106 | { |
---|
107 | vertices[8 * (20 * y + x) + 0] = x * 32; |
---|
108 | vertices[8 * (20 * y + x) + 1] = y * 32; |
---|
109 | vertices[8 * (20 * y + x) + 2] = x * 32 + 32; |
---|
110 | vertices[8 * (20 * y + x) + 3] = y * 32; |
---|
111 | vertices[8 * (20 * y + x) + 4] = x * 32 + 32; |
---|
112 | vertices[8 * (20 * y + x) + 5] = y * 32 + 32; |
---|
113 | vertices[8 * (20 * y + x) + 6] = x * 32; |
---|
114 | vertices[8 * (20 * y + x) + 7] = y * 32 + 32; |
---|
115 | } |
---|
116 | glBindBuffer(GL_ARRAY_BUFFER, buflist[0]); |
---|
117 | glBufferData(GL_ARRAY_BUFFER, |
---|
118 | 8 * 20 * 15 * sizeof(float), vertices, GL_STATIC_DRAW); |
---|
119 | |
---|
120 | int indices[4 * 20 * 15]; |
---|
121 | for (int n = 0; n < 4 * 20 * 15; n++) |
---|
122 | indices[n] = n; |
---|
123 | glBindBuffer(GL_ARRAY_BUFFER, buflist[2]); |
---|
124 | glBufferData(GL_ARRAY_BUFFER, |
---|
125 | 4 * 20 * 15 * sizeof(int), indices, GL_STATIC_DRAW); |
---|
126 | |
---|
127 | glEnableClientState(GL_VERTEX_ARRAY); |
---|
128 | glEnableClientState(GL_TEXTURE_COORD_ARRAY); |
---|
129 | glEnableClientState(GL_INDEX_ARRAY); |
---|
130 | |
---|
131 | glBindTexture(GL_TEXTURE_2D, texture[0]); |
---|
132 | |
---|
133 | glBindBuffer(GL_ARRAY_BUFFER, buflist[0]); |
---|
134 | glVertexPointer(2, GL_FLOAT, 0, NULL); |
---|
135 | glBindBuffer(GL_ARRAY_BUFFER, buflist[1]); |
---|
136 | glTexCoordPointer(2, GL_FLOAT, 0, NULL); |
---|
137 | glBindBuffer(GL_ARRAY_BUFFER, buflist[2]); |
---|
138 | glIndexPointer(GL_INT, 0, NULL); |
---|
139 | |
---|
140 | glDrawArrays(GL_QUADS, 0, 4 * 20 * 15); |
---|
141 | |
---|
142 | glDisableClientState(GL_VERTEX_ARRAY); |
---|
143 | glDisableClientState(GL_TEXTURE_COORD_ARRAY); |
---|
144 | glDisableClientState(GL_INDEX_ARRAY); |
---|
145 | } |
---|
146 | |
---|
147 | /* The main drawing function. */ |
---|
148 | void DrawScene() |
---|
149 | { |
---|
150 | int ground[20 * 15] = |
---|
151 | { |
---|
152 | 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, |
---|
153 | 18, 1, 2, 2, 2, 34, 2, 2, 2, 2, 2, 2, 3, 34, 4, 18, 18, 18, 18, 18, |
---|
154 | 18, 17, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 17, 18, 20, 4, 18, 18, 18, 18, |
---|
155 | 18, 19, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 17, 18, 17, 19, 18, 18, 18, 18, |
---|
156 | 18, 17, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 17, 18, 17, 17, 18, 18, 18, 18, |
---|
157 | 18, 17, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 19, 18, 20, 36, 18, 18, 18, 18, |
---|
158 | 18, 33, 2, 2, 2, 2, 2, 2, 2, 2, 34, 2, 35, 2, 36, 18, 18, 18, 18, 18, |
---|
159 | 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, |
---|
160 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, |
---|
161 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, |
---|
162 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, |
---|
163 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, |
---|
164 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, |
---|
165 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, |
---|
166 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, |
---|
167 | }; |
---|
168 | |
---|
169 | int l1objects[20 * 15] = |
---|
170 | { |
---|
171 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
---|
172 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
---|
173 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
---|
174 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
---|
175 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 49, 0, 0, 0, 0, 0, 0, 0, 0, |
---|
176 | 0, 0, 0, 0, 49, 49, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
---|
177 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
---|
178 | 0, 0, 0, 0, 49, 49, 49, 49, 49, 49, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
---|
179 | 0, 0, 0, 0, 0, 49, 49, 49, 49, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
---|
180 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 48, 0, 32, 49, 0, 0, 0, |
---|
181 | 0, 0, 0, 49, 49, 32, 0, 50, 0, 0, 0, 48, 0, 64, 0, 49, 49, 0, 0, 0, |
---|
182 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 32, 0, 64, 0, 0, 0, 0, 0, 0, 0, 0, |
---|
183 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 32, 0, 0, 0, 0, 0, |
---|
184 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
---|
185 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
---|
186 | }; |
---|
187 | |
---|
188 | glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); |
---|
189 | glLoadIdentity(); |
---|
190 | |
---|
191 | PutMap(ground); |
---|
192 | //glTranslatef(10.0f * sinf(0.16f * frames), 10.0f * cosf(0.16f * frames), 0.0f); |
---|
193 | PutMap(l1objects); |
---|
194 | |
---|
195 | SDL_GL_SwapBuffers(); |
---|
196 | } |
---|
197 | |
---|
198 | int main(int argc, char **argv) |
---|
199 | { |
---|
200 | SDL_Surface *video; |
---|
201 | int done; |
---|
202 | |
---|
203 | /* Initialize SDL for video output */ |
---|
204 | if (SDL_Init(SDL_INIT_VIDEO) < 0) |
---|
205 | { |
---|
206 | fprintf(stderr, "Unable to initialize SDL: %s\n", SDL_GetError()); |
---|
207 | exit(1); |
---|
208 | } |
---|
209 | |
---|
210 | /* Create a 640x480 OpenGL screen */ |
---|
211 | video = SDL_SetVideoMode(640, 480, 0, SDL_OPENGL); |
---|
212 | if (!video) |
---|
213 | { |
---|
214 | fprintf(stderr, "Unable to create OpenGL screen: %s\n", SDL_GetError()); |
---|
215 | SDL_Quit(); |
---|
216 | exit(2); |
---|
217 | } |
---|
218 | |
---|
219 | /* Set the title bar in environments that support it */ |
---|
220 | SDL_WM_SetCaption("Deus Hax", NULL); |
---|
221 | |
---|
222 | /* Loop, drawing and checking events */ |
---|
223 | InitGL(640, 480); |
---|
224 | done = 0; |
---|
225 | frames = 0; |
---|
226 | Uint32 ticks = SDL_GetTicks(); |
---|
227 | Uint32 start = ticks; |
---|
228 | while (!done) |
---|
229 | { |
---|
230 | DrawScene(); |
---|
231 | frames++; |
---|
232 | |
---|
233 | /* This could go in a separate function */ |
---|
234 | SDL_Event event; |
---|
235 | while (SDL_PollEvent(&event)) |
---|
236 | { |
---|
237 | if (event.type == SDL_QUIT) |
---|
238 | done = 1; |
---|
239 | if (event.type == SDL_KEYDOWN) |
---|
240 | { |
---|
241 | if (event.key.keysym.sym == SDLK_RETURN) |
---|
242 | SDL_WM_ToggleFullScreen(video); |
---|
243 | else if (event.key.keysym.sym == SDLK_ESCAPE) |
---|
244 | done = 1; |
---|
245 | } |
---|
246 | } |
---|
247 | |
---|
248 | while (SDL_GetTicks() < ticks + 33) |
---|
249 | SDL_Delay(1); |
---|
250 | ticks = SDL_GetTicks(); |
---|
251 | } |
---|
252 | printf("%i fps\n", frames * 1000 / (SDL_GetTicks() - start)); |
---|
253 | SDL_Quit(); |
---|
254 | |
---|
255 | return EXIT_SUCCESS; |
---|
256 | } |
---|
257 | |
---|