source: trunk/src/test-map.cpp @ 20

Last change on this file since 20 was 20, checked in by sam, 13 years ago

Add mediocre support for .tmx tiled maps.

  • Property svn:keywords set to Id
File size: 7.8 KB
Line 
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 <stdio.h>
19#include <math.h>
20
21#include "video.h"
22
23/* Storage for one texture  */
24GLuint texture[1];
25
26/* Storage for 3 vertex buffers */
27GLuint buflist[3];
28
29/* Storage for map layers */
30int *layers[128];
31int width = 32, height = 32;
32int nlayers = 0;
33
34// Load Bitmaps And Convert To Textures
35void LoadGLTextures(void)
36{       
37    SDL_Surface *image1 = IMG_Load("art/test/groundtest.png");
38
39    if (!image1)
40    {
41        SDL_Quit();
42        exit(1);
43    }
44
45    glGenTextures(1, &texture[0]);
46    glBindTexture(GL_TEXTURE_2D, texture[0]);
47
48    glTexImage2D(GL_TEXTURE_2D, 0, 4, image1->w, image1->h, 0,
49                 GL_RGBA, GL_UNSIGNED_BYTE, image1->pixels);
50
51    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
52    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
53};
54
55void MakeVBOs(void)
56{
57    glGenBuffers(3, buflist);
58}
59
60void LoadMap(void)
61{
62    FILE *fp = popen("grep '^   ' maps/testmap.tmx | while read i; do echo -n \"$i\" | perl -MMIME::Base64 -ne 'print decode_base64($_)' | gunzip; done", "r");
63    while (fp && !feof(fp))
64    {
65        layers[nlayers] = (int *)malloc(width * height * sizeof(int));
66        fread(layers[nlayers], sizeof(int), width * height, fp);
67        if (feof(fp))
68        {
69            free(layers[nlayers]);
70            layers[nlayers] = 0;
71            fclose(fp);
72            break;
73        }
74        for (int n = 0; n < width * height; n++)
75        {
76            unsigned int i = layers[nlayers][n];
77            //i = (i >> 24) | ((i >> 8) & 0xff00) | ((i << 8) & 0xff0000) | (i << 24);
78            layers[nlayers][n] = i ? i - 1 : 0;
79        }
80        nlayers++;
81    }
82}
83
84void PutMap(int const *themap, int width, int height)
85{
86    // Put map
87    float uvs[8 * width * height];
88
89    for (int y = 0; y < height; y++)
90        for (int x = 0; x < width; x++)
91        {
92            int tile = themap[width * y + x];
93            float ty = .0625f * (tile / 16);
94            float tx = .0625f * (tile % 16);
95            uvs[8 * (width * y + x) + 0] = tx;
96            uvs[8 * (width * y + x) + 1] = ty;
97            uvs[8 * (width * y + x) + 2] = tx + .0625f;
98            uvs[8 * (width * y + x) + 3] = ty;
99            uvs[8 * (width * y + x) + 4] = tx + .0625f;
100            uvs[8 * (width * y + x) + 5] = ty + .0625f;
101            uvs[8 * (width * y + x) + 6] = tx;
102            uvs[8 * (width * y + x) + 7] = ty + .0625f;
103        }
104    glBindBuffer(GL_ARRAY_BUFFER, buflist[1]);
105    glBufferData(GL_ARRAY_BUFFER,
106                 8 * width * height * sizeof(float), uvs, GL_STATIC_DRAW);
107
108    float vertices[8 * width * height];
109    for (int y = 0; y < height; y++)
110        for (int x = 0; x < width; x++)
111        {
112            vertices[8 * (width * y + x) + 0] = x * 32;
113            vertices[8 * (width * y + x) + 1] = y * 32;
114            vertices[8 * (width * y + x) + 2] = x * 32 + 32;
115            vertices[8 * (width * y + x) + 3] = y * 32;
116            vertices[8 * (width * y + x) + 4] = x * 32 + 32;
117            vertices[8 * (width * y + x) + 5] = y * 32 + 32;
118            vertices[8 * (width * y + x) + 6] = x * 32;
119            vertices[8 * (width * y + x) + 7] = y * 32 + 32;
120        }
121    glBindBuffer(GL_ARRAY_BUFFER, buflist[0]);
122    glBufferData(GL_ARRAY_BUFFER,
123                 8 * width * height * sizeof(float), vertices, GL_STATIC_DRAW);
124
125    int indices[4 * width * height];
126    for (int n = 0; n < 4 * width * height; n++)
127        indices[n] = n;
128    glBindBuffer(GL_ARRAY_BUFFER, buflist[2]);
129    glBufferData(GL_ARRAY_BUFFER,
130                 4 * width * height * sizeof(int), indices, GL_STATIC_DRAW);
131
132    glEnableClientState(GL_VERTEX_ARRAY);
133    glEnableClientState(GL_TEXTURE_COORD_ARRAY);
134    glEnableClientState(GL_INDEX_ARRAY);
135
136    glBindTexture(GL_TEXTURE_2D, texture[0]);
137
138    glBindBuffer(GL_ARRAY_BUFFER, buflist[0]);
139    glVertexPointer(2, GL_FLOAT, 0, NULL);
140    glBindBuffer(GL_ARRAY_BUFFER, buflist[1]);
141    glTexCoordPointer(2, GL_FLOAT, 0, NULL);
142    glBindBuffer(GL_ARRAY_BUFFER, buflist[2]);
143    glIndexPointer(GL_INT, 0, NULL);
144
145    glDrawArrays(GL_QUADS, 0, 4 * width * height);
146
147    glDisableClientState(GL_VERTEX_ARRAY);
148    glDisableClientState(GL_TEXTURE_COORD_ARRAY);
149    glDisableClientState(GL_INDEX_ARRAY);
150}
151
152/* The main drawing function. */
153void DrawScene()
154{
155/*
156    int ground[20 * 15] =
157    {
15818, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18,
15918,  1,  2,  2,  2, 34,  2,  2,  2,  2,  2,  2,  3, 34,  4, 18, 18, 18, 18, 18,
16018, 17, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 17, 18, 20,  4, 18, 18, 18, 18,
16118, 19, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 17, 18, 17, 19, 18, 18, 18, 18,
16218, 17, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 17, 18, 17, 17, 18, 18, 18, 18,
16318, 17, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 19, 18, 20, 36, 18, 18, 18, 18,
16418, 33,  2,  2,  2,  2,  2,  2,  2,  2, 34,  2, 35,  2, 36, 18, 18, 18, 18, 18,
16518, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18,
16616, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
16716, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
16816, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
16916, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
17016, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
17116, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
17216, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
173    };
174
175    int l1objects[20 * 15] =
176    {
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,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
179 0,  0,  0,  0,  0,  0,  0,  0,  0,  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,  0,  0,  0,  0,  0,  0,  0,
181 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0, 49,  0,  0,  0,  0,  0,  0,  0,  0,
182 0,  0,  0,  0, 49, 49,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
183 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
184 0,  0,  0,  0, 49, 49, 49, 49, 49, 49,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
185 0,  0,  0,  0,  0, 49, 49, 49, 49,  0,  0,  0,  0, 25,  9,  9,  9,  8,  0,  0,
186 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0, 54, 40, 39, 39, 41,  0,  0,
187 0,  0,  0, 49, 49, 32,  0, 50,  0,  0,  0, 48,  0, 24, 23, 54, 40, 55,  0,  0,
188 0,  0,  0,  0,  0,  0,  0,  0,  0, 32,  0, 64,  0, 24, 23, 24, 23,  7,  0,  0,
189 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  6, 38, 24, 23,  7,  0,  0,
190 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  6, 38, 22,  0,  0,
191 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
192    };
193*/
194
195    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
196    glLoadIdentity();
197
198    for (int i = 0; i < nlayers; i++)
199        PutMap(layers[i], width, height);
200}
201
202int main(int argc, char **argv)
203{
204    Video *video = new Video("Deus Hax", 640, 480);
205
206    int done;
207
208    /* Loop, drawing and checking events */
209    LoadGLTextures();
210    MakeVBOs();
211    LoadMap();
212
213    done = 0;
214    while (!done)
215    {
216        DrawScene();
217
218        video->Refresh(33.33333f);
219
220        /* This could go in a separate function */
221        SDL_Event event;
222        while (SDL_PollEvent(&event))
223        {
224            if (event.type == SDL_QUIT)
225                done = 1;
226            if (event.type == SDL_KEYDOWN)
227            {
228                if (event.key.keysym.sym == SDLK_RETURN)
229                    video->FullScreen();
230                else if (event.key.keysym.sym == SDLK_ESCAPE)
231                    done = 1;
232            }
233        }
234    }
235
236    delete video;
237
238    return EXIT_SUCCESS;
239}
240
Note: See TracBrowser for help on using the repository browser.