Changeset 14
- Timestamp:
- Jun 27, 2010, 9:57:26 PM (13 years ago)
- Location:
- trunk
- Files:
-
- 1 added
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/test-map.cpp
r13 r14 7 7 #if defined __APPLE__ && defined __MACH__ 8 8 # include <OpenGL/gl.h> 9 # include <OpenGL/glu.h>10 9 #else 10 # define GL_GLEXT_PROTOTYPES 11 11 # include <GL/gl.h> 12 # include <GL/gl u.h>12 # include <GL/glext.h> 13 13 #endif 14 14 … … 16 16 #include <SDL_image.h> 17 17 18 float xrot, yrot, zrot; 18 #include <math.h> 19 20 int frames; 19 21 20 22 /* Storage for one texture */ 21 23 GLuint texture[1]; 24 25 /* Storage for 3 vertex buffers */ 26 GLuint buflist[3]; 22 27 23 28 // Load Bitmaps And Convert To Textures … … 35 40 glBindTexture(GL_TEXTURE_2D, texture[0]); 36 41 37 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);38 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);39 40 42 glTexImage2D(GL_TEXTURE_2D, 0, 4, image1->w, image1->h, 0, 41 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); 42 47 }; 43 48 49 void MakeVBOs(void) 50 { 51 glGenBuffers(3, buflist); 52 53 float vertices[8 * 20 * 15]; 54 for (int y = 0; y < 15; y++) 55 for (int x = 0; x < 20; x++) 56 { 57 vertices[8 * (20 * y + x) + 0] = x * 32; 58 vertices[8 * (20 * y + x) + 1] = y * 32; 59 vertices[8 * (20 * y + x) + 2] = x * 32 + 32; 60 vertices[8 * (20 * y + x) + 3] = y * 32; 61 vertices[8 * (20 * y + x) + 4] = x * 32 + 32; 62 vertices[8 * (20 * y + x) + 5] = y * 32 + 32; 63 vertices[8 * (20 * y + x) + 6] = x * 32; 64 vertices[8 * (20 * y + x) + 7] = y * 32 + 32; 65 } 66 glBindBuffer(GL_ARRAY_BUFFER, buflist[0]); 67 glBufferData(GL_ARRAY_BUFFER, 68 8 * 20 * 15 * sizeof(float), vertices, GL_STATIC_DRAW); 69 70 int indices[4 * 20 * 15]; 71 for (int n = 0; n < 4 * 20 * 15; n++) 72 indices[n] = n; 73 glBindBuffer(GL_ARRAY_BUFFER, buflist[2]); 74 glBufferData(GL_ARRAY_BUFFER, 75 4 * 20 * 15 * sizeof(int), indices, GL_STATIC_DRAW); 76 } 77 44 78 void InitGL(int Width, int Height) 45 79 { 80 // Resize method 46 81 glViewport(0, 0, Width, Height); 47 LoadGLTextures();48 glEnable(GL_TEXTURE_2D);49 glClearColor(0.0f, 0.0f, 1.0f, 0.0f);50 glClearDepth(1.0);51 glDepthFunc(GL_LESS);52 glEnable(GL_DEPTH_TEST);53 glShadeModel(GL_SMOOTH);54 55 82 glMatrixMode(GL_PROJECTION); 56 83 glLoadIdentity(); 57 58 gluPerspective(45.0f, (GLfloat)Width / (GLfloat)Height, 0.1f, 100.0f); 59 84 glOrtho(0, Width, Height, 0, -1, 10); 60 85 glMatrixMode(GL_MODELVIEW); 86 glLoadIdentity(); 87 88 // Init method 89 glEnable(GL_TEXTURE_2D); 90 LoadGLTextures(); 91 MakeVBOs(); 92 glShadeModel(GL_SMOOTH); 93 glClearColor(0.0f, 0.0f, 0.0f, 0.0f); 94 glClearDepth(1.0); 95 glEnable(GL_DEPTH_TEST); 96 glDepthFunc(GL_LEQUAL); 97 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); 98 99 glEnable(GL_BLEND); 100 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); 101 } 102 103 void PutMap(int const *themap) 104 { 105 // Put map 106 float uvs[8 * 20 * 15]; 107 108 for (int y = 0; y < 15; y++) 109 for (int x = 0; x < 20; x++) 110 { 111 int tile = themap[20 * y + x]; 112 float ty = .0625f * (tile / 16); 113 float tx = .0625f * (tile % 16); 114 uvs[8 * (20 * y + x) + 0] = tx; 115 uvs[8 * (20 * y + x) + 1] = ty; 116 uvs[8 * (20 * y + x) + 2] = tx + .0625f; 117 uvs[8 * (20 * y + x) + 3] = ty; 118 uvs[8 * (20 * y + x) + 4] = tx + .0625f; 119 uvs[8 * (20 * y + x) + 5] = ty + .0625f; 120 uvs[8 * (20 * y + x) + 6] = tx; 121 uvs[8 * (20 * y + x) + 7] = ty + .0625f; 122 } 123 glBindBuffer(GL_ARRAY_BUFFER, buflist[1]); 124 glBufferData(GL_ARRAY_BUFFER, 125 8 * 20 * 15 * sizeof(float), uvs, 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); 61 145 } 62 146 63 147 /* The main drawing function. */ 64 void DrawGLScene() 65 { 66 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 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); 67 189 glLoadIdentity(); 68 190 69 glTranslatef(0.0f, 0.0f, -5.0f); 70 71 glRotatef(xrot, 1.0f, 0.0f, 0.0f); 72 glRotatef(yrot, 0.0f, 1.0f, 0.0f); 73 glRotatef(zrot, 0.0f, 0.0f, 1.0f); 74 75 glBindTexture(GL_TEXTURE_2D, texture[0]); 76 77 glBegin(GL_QUADS); 78 79 glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f); 80 glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f); 81 glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f); 82 glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f); 83 84 glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f); 85 glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f); 86 glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f); 87 glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f); 88 89 glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f); 90 glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, 1.0f, 1.0f); 91 glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, 1.0f, 1.0f); 92 glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f); 93 94 glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, -1.0f, -1.0f); 95 glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, -1.0f, -1.0f); 96 glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f); 97 glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f); 98 99 glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f); 100 glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f); 101 glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f); 102 glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f); 103 104 glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f); 105 glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f); 106 glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f); 107 glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f); 108 109 glEnd(); 110 111 xrot += 0.2f; 112 yrot += 0.2f; 113 zrot += 0.2f; 191 PutMap(ground); 192 //glTranslatef(10.0f * sinf(0.16f * frames), 10.0f * cosf(0.16f * frames), 0.0f); 193 PutMap(l1objects); 114 194 115 195 SDL_GL_SwapBuffers(); … … 121 201 122 202 /* Initialize SDL for video output */ 123 if ( SDL_Init(SDL_INIT_VIDEO) < 0)203 if (SDL_Init(SDL_INIT_VIDEO) < 0) 124 204 { 125 205 fprintf(stderr, "Unable to initialize SDL: %s\n", SDL_GetError()); … … 128 208 129 209 /* Create a 640x480 OpenGL screen */ 130 if ( SDL_SetVideoMode(640, 480, 0, SDL_OPENGL) == NULL)210 if (SDL_SetVideoMode(640, 480, 0, SDL_OPENGL) == NULL) 131 211 { 132 212 fprintf(stderr, "Unable to create OpenGL screen: %s\n", SDL_GetError()); … … 141 221 InitGL(640, 480); 142 222 done = 0; 143 while ( !done ) 223 frames = 0; 224 Uint32 ticks = SDL_GetTicks(); 225 Uint32 start = ticks; 226 while (!done) 144 227 { 145 DrawGLScene(); 228 DrawScene(); 229 frames++; 146 230 147 231 /* This could go in a separate function */ 148 232 SDL_Event event; 149 while ( SDL_PollEvent(&event))150 { 151 if ( event.type == SDL_QUIT)233 while (SDL_PollEvent(&event)) 234 { 235 if (event.type == SDL_QUIT) 152 236 done = 1; 153 if ( event.type == SDL_KEYDOWN)154 if ( event.key.keysym.sym == SDLK_ESCAPE)237 if (event.type == SDL_KEYDOWN) 238 if (event.key.keysym.sym == SDLK_ESCAPE) 155 239 done = 1; 156 240 } 241 242 while (SDL_GetTicks() < ticks + 33) 243 SDL_Delay(1); 244 ticks = SDL_GetTicks(); 157 245 } 246 printf("%i fps\n", frames * 1000 / (SDL_GetTicks() - start)); 158 247 SDL_Quit(); 159 return 1; 160 } 161 248 249 return EXIT_SUCCESS; 250 } 251 -
trunk/test-map.py
r12 r14 116 116 117 117 themap = [ 118 [ 1 7, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17],119 [ 1 7, 0, 1, 1, 1, 33, 1, 1, 1, 1, 1, 1, 2, 33, 3, 17, 17, 17, 17, 17],120 [ 1 7, 16, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 16, 17, 19, 3, 17, 17, 17, 17],121 [ 1 7, 18, 17, 48, 49, 50, 48, 49, 50, 48, 49, 17, 16, 17, 16, 18, 17, 17, 17, 17],122 [ 1 7, 16, 17, 48, 49, 50, 48, 49, 50, 48, 49, 17, 16, 17, 16, 16, 17, 17, 17, 17],123 [ 1 7, 16, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 18, 17, 19, 35, 17, 17, 17, 17],124 [ 1 7, 32, 1, 1, 1, 1, 1, 1, 1, 1, 33, 1, 34, 1, 35, 17, 17, 17, 17, 17],125 [ 1 7, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17],126 [ 5 1, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51],127 [ 5 1, 51, 52, 52, 52, 51, 52, 52, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51],128 [ 5 1, 51, 52, 52, 52, 51, 51, 52, 51, 52, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51],129 [ 5 1, 51, 52, 52, 52, 51, 51, 51, 51, 51, 51, 52, 52, 51, 51, 51, 51, 51, 51, 51],130 [ 5 1, 51, 52, 52, 52, 51, 51, 51, 51, 51, 51, 52, 52, 51, 51, 51, 51, 51, 51, 51],131 [ 5 1, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51],132 [ 5 1, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51],118 [ 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18 ], 119 [ 18, 1, 2, 2, 2, 34, 2, 2, 2, 2, 2, 2, 3, 34, 4, 18, 18, 18, 18, 18 ], 120 [ 18, 17, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 17, 18, 20, 4, 18, 18, 18, 18 ], 121 [ 18, 19, 18, 49, 50, 51, 49, 50, 51, 49, 50, 18, 17, 18, 17, 19, 18, 18, 18, 18 ], 122 [ 18, 17, 18, 49, 50, 51, 49, 50, 51, 49, 50, 18, 17, 18, 17, 17, 18, 18, 18, 18 ], 123 [ 18, 17, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 19, 18, 20, 36, 18, 18, 18, 18 ], 124 [ 18, 33, 2, 2, 2, 2, 2, 2, 2, 2, 34, 2, 35, 2, 36, 18, 18, 18, 18, 18 ], 125 [ 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18 ], 126 [ 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52 ], 127 [ 52, 52, 53, 53, 53, 52, 53, 53, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52 ], 128 [ 52, 52, 53, 53, 53, 52, 52, 53, 52, 53, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52 ], 129 [ 52, 52, 53, 53, 53, 52, 52, 52, 52, 52, 52, 53, 53, 52, 52, 52, 52, 52, 52, 52 ], 130 [ 52, 52, 53, 53, 53, 52, 52, 52, 52, 52, 52, 53, 53, 52, 52, 52, 52, 52, 52, 52 ], 131 [ 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52 ], 132 [ 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52 ], 133 133 ] 134 134 glPushMatrix()
Note: See TracChangeset
for help on using the changeset viewer.