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

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

Move video initialisation stuff to a separate source file.

  • Property svn:keywords set to Id
File size: 6.9 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 <math.h>
19
20#include "video.h"
21
22/* Storage for one texture  */
23GLuint texture[1];
24
25/* Storage for 3 vertex buffers */
26GLuint buflist[3];
27
28// Load Bitmaps And Convert To Textures
29void 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
49void MakeVBOs(void)
50{
51    glGenBuffers(3, buflist);
52}
53
54void PutMap(int const *themap)
55{
56    // Put map
57    float uvs[8 * 20 * 15];
58
59    for (int y = 0; y < 15; y++)
60        for (int x = 0; x < 20; x++)
61        {
62            int tile = themap[20 * y + x];
63            float ty = .0625f * (tile / 16);
64            float tx = .0625f * (tile % 16);
65            uvs[8 * (20 * y + x) + 0] = tx;
66            uvs[8 * (20 * y + x) + 1] = ty;
67            uvs[8 * (20 * y + x) + 2] = tx + .0625f;
68            uvs[8 * (20 * y + x) + 3] = ty;
69            uvs[8 * (20 * y + x) + 4] = tx + .0625f;
70            uvs[8 * (20 * y + x) + 5] = ty + .0625f;
71            uvs[8 * (20 * y + x) + 6] = tx;
72            uvs[8 * (20 * y + x) + 7] = ty + .0625f;
73        }
74    glBindBuffer(GL_ARRAY_BUFFER, buflist[1]);
75    glBufferData(GL_ARRAY_BUFFER,
76                 8 * 20 * 15 * sizeof(float), uvs, GL_STATIC_DRAW);
77
78    float vertices[8 * 20 * 15];
79    for (int y = 0; y < 15; y++)
80        for (int x = 0; x < 20; x++)
81        {
82            vertices[8 * (20 * y + x) + 0] = x * 32;
83            vertices[8 * (20 * y + x) + 1] = y * 32;
84            vertices[8 * (20 * y + x) + 2] = x * 32 + 32;
85            vertices[8 * (20 * y + x) + 3] = y * 32;
86            vertices[8 * (20 * y + x) + 4] = x * 32 + 32;
87            vertices[8 * (20 * y + x) + 5] = y * 32 + 32;
88            vertices[8 * (20 * y + x) + 6] = x * 32;
89            vertices[8 * (20 * y + x) + 7] = y * 32 + 32;
90        }
91    glBindBuffer(GL_ARRAY_BUFFER, buflist[0]);
92    glBufferData(GL_ARRAY_BUFFER,
93                 8 * 20 * 15 * sizeof(float), vertices, GL_STATIC_DRAW);
94
95    int indices[4 * 20 * 15];
96    for (int n = 0; n < 4 * 20 * 15; n++)
97        indices[n] = n;
98    glBindBuffer(GL_ARRAY_BUFFER, buflist[2]);
99    glBufferData(GL_ARRAY_BUFFER,
100                 4 * 20 * 15 * sizeof(int), indices, GL_STATIC_DRAW);
101
102    glEnableClientState(GL_VERTEX_ARRAY);
103    glEnableClientState(GL_TEXTURE_COORD_ARRAY);
104    glEnableClientState(GL_INDEX_ARRAY);
105
106    glBindTexture(GL_TEXTURE_2D, texture[0]);
107
108    glBindBuffer(GL_ARRAY_BUFFER, buflist[0]);
109    glVertexPointer(2, GL_FLOAT, 0, NULL);
110    glBindBuffer(GL_ARRAY_BUFFER, buflist[1]);
111    glTexCoordPointer(2, GL_FLOAT, 0, NULL);
112    glBindBuffer(GL_ARRAY_BUFFER, buflist[2]);
113    glIndexPointer(GL_INT, 0, NULL);
114
115    glDrawArrays(GL_QUADS, 0, 4 * 20 * 15);
116
117    glDisableClientState(GL_VERTEX_ARRAY);
118    glDisableClientState(GL_TEXTURE_COORD_ARRAY);
119    glDisableClientState(GL_INDEX_ARRAY);
120}
121
122/* The main drawing function. */
123void DrawScene()
124{
125    int ground[20 * 15] =
126    {
12718, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18,
12818,  1,  2,  2,  2, 34,  2,  2,  2,  2,  2,  2,  3, 34,  4, 18, 18, 18, 18, 18,
12918, 17, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 17, 18, 20,  4, 18, 18, 18, 18,
13018, 19, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 17, 18, 17, 19, 18, 18, 18, 18,
13118, 17, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 17, 18, 17, 17, 18, 18, 18, 18,
13218, 17, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 19, 18, 20, 36, 18, 18, 18, 18,
13318, 33,  2,  2,  2,  2,  2,  2,  2,  2, 34,  2, 35,  2, 36, 18, 18, 18, 18, 18,
13418, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18,
13516, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
13616, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
13716, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
13816, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
13916, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
14016, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
14116, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
142    };
143
144    int l1objects[20 * 15] =
145    {
146 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
147 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
148 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
149 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
150 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0, 49,  0,  0,  0,  0,  0,  0,  0,  0,
151 0,  0,  0,  0, 49, 49,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
152 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
153 0,  0,  0,  0, 49, 49, 49, 49, 49, 49,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
154 0,  0,  0,  0,  0, 49, 49, 49, 49,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
155 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0, 48,  0, 32, 49,  0,  0,  0,
156 0,  0,  0, 49, 49, 32,  0, 50,  0,  0,  0, 48,  0, 64,  0, 49, 49,  0,  0,  0,
157 0,  0,  0,  0,  0,  0,  0,  0,  0, 32,  0, 64,  0,  0,  0,  0,  0,  0,  0,  0,
158 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0, 32,  0,  0,  0,  0,  0,
159 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
160 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
161    };
162
163    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
164    glLoadIdentity();
165
166    PutMap(ground);
167    //glTranslatef(10.0f * sinf(0.16f * frames), 10.0f * cosf(0.16f * frames), 0.0f);
168    PutMap(l1objects);
169}
170
171int main(int argc, char **argv)
172{
173    Video *video = new Video("Deus Hax", 640, 480);
174
175    int done;
176
177    /* Loop, drawing and checking events */
178    LoadGLTextures();
179    MakeVBOs();
180
181    done = 0;
182    while (!done)
183    {
184        DrawScene();
185
186        video->Refresh(33.33333f);
187
188        /* This could go in a separate function */
189        SDL_Event event;
190        while (SDL_PollEvent(&event))
191        {
192            if (event.type == SDL_QUIT)
193                done = 1;
194            if (event.type == SDL_KEYDOWN)
195            {
196                if (event.key.keysym.sym == SDLK_RETURN)
197                    video->FullScreen();
198                else if (event.key.keysym.sym == SDLK_ESCAPE)
199                    done = 1;
200            }
201        }
202    }
203
204    delete video;
205
206    return EXIT_SUCCESS;
207}
208
Note: See TracBrowser for help on using the repository browser.