|
|
@@ -6,20 +6,25 @@ |
|
|
|
#endif |
|
|
|
#if defined __APPLE__ && defined __MACH__ |
|
|
|
# include <OpenGL/gl.h> |
|
|
|
# include <OpenGL/glu.h> |
|
|
|
#else |
|
|
|
# define GL_GLEXT_PROTOTYPES |
|
|
|
# include <GL/gl.h> |
|
|
|
# include <GL/glu.h> |
|
|
|
# include <GL/glext.h> |
|
|
|
#endif |
|
|
|
|
|
|
|
#include <SDL.h> |
|
|
|
#include <SDL_image.h> |
|
|
|
|
|
|
|
float xrot, yrot, zrot; |
|
|
|
#include <math.h> |
|
|
|
|
|
|
|
int frames; |
|
|
|
|
|
|
|
/* Storage for one texture */ |
|
|
|
GLuint texture[1]; |
|
|
|
|
|
|
|
/* Storage for 3 vertex buffers */ |
|
|
|
GLuint buflist[3]; |
|
|
|
|
|
|
|
// Load Bitmaps And Convert To Textures |
|
|
|
void LoadGLTextures(void) |
|
|
|
{ |
|
|
@@ -34,83 +39,158 @@ void LoadGLTextures(void) |
|
|
|
glGenTextures(1, &texture[0]); |
|
|
|
glBindTexture(GL_TEXTURE_2D, texture[0]); |
|
|
|
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
|
|
|
|
|
|
|
glTexImage2D(GL_TEXTURE_2D, 0, 4, image1->w, image1->h, 0, |
|
|
|
GL_RGBA, GL_UNSIGNED_BYTE, image1->pixels); |
|
|
|
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
|
|
|
}; |
|
|
|
|
|
|
|
void MakeVBOs(void) |
|
|
|
{ |
|
|
|
glGenBuffers(3, buflist); |
|
|
|
|
|
|
|
float vertices[8 * 20 * 15]; |
|
|
|
for (int y = 0; y < 15; y++) |
|
|
|
for (int x = 0; x < 20; x++) |
|
|
|
{ |
|
|
|
vertices[8 * (20 * y + x) + 0] = x * 32; |
|
|
|
vertices[8 * (20 * y + x) + 1] = y * 32; |
|
|
|
vertices[8 * (20 * y + x) + 2] = x * 32 + 32; |
|
|
|
vertices[8 * (20 * y + x) + 3] = y * 32; |
|
|
|
vertices[8 * (20 * y + x) + 4] = x * 32 + 32; |
|
|
|
vertices[8 * (20 * y + x) + 5] = y * 32 + 32; |
|
|
|
vertices[8 * (20 * y + x) + 6] = x * 32; |
|
|
|
vertices[8 * (20 * y + x) + 7] = y * 32 + 32; |
|
|
|
} |
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, buflist[0]); |
|
|
|
glBufferData(GL_ARRAY_BUFFER, |
|
|
|
8 * 20 * 15 * sizeof(float), vertices, GL_STATIC_DRAW); |
|
|
|
|
|
|
|
int indices[4 * 20 * 15]; |
|
|
|
for (int n = 0; n < 4 * 20 * 15; n++) |
|
|
|
indices[n] = n; |
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, buflist[2]); |
|
|
|
glBufferData(GL_ARRAY_BUFFER, |
|
|
|
4 * 20 * 15 * sizeof(int), indices, GL_STATIC_DRAW); |
|
|
|
} |
|
|
|
|
|
|
|
void InitGL(int Width, int Height) |
|
|
|
{ |
|
|
|
// Resize method |
|
|
|
glViewport(0, 0, Width, Height); |
|
|
|
LoadGLTextures(); |
|
|
|
glEnable(GL_TEXTURE_2D); |
|
|
|
glClearColor(0.0f, 0.0f, 1.0f, 0.0f); |
|
|
|
glClearDepth(1.0); |
|
|
|
glDepthFunc(GL_LESS); |
|
|
|
glEnable(GL_DEPTH_TEST); |
|
|
|
glShadeModel(GL_SMOOTH); |
|
|
|
|
|
|
|
glMatrixMode(GL_PROJECTION); |
|
|
|
glLoadIdentity(); |
|
|
|
glOrtho(0, Width, Height, 0, -1, 10); |
|
|
|
glMatrixMode(GL_MODELVIEW); |
|
|
|
glLoadIdentity(); |
|
|
|
|
|
|
|
gluPerspective(45.0f, (GLfloat)Width / (GLfloat)Height, 0.1f, 100.0f); |
|
|
|
// Init method |
|
|
|
glEnable(GL_TEXTURE_2D); |
|
|
|
LoadGLTextures(); |
|
|
|
MakeVBOs(); |
|
|
|
glShadeModel(GL_SMOOTH); |
|
|
|
glClearColor(0.0f, 0.0f, 0.0f, 0.0f); |
|
|
|
glClearDepth(1.0); |
|
|
|
glEnable(GL_DEPTH_TEST); |
|
|
|
glDepthFunc(GL_LEQUAL); |
|
|
|
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); |
|
|
|
|
|
|
|
glMatrixMode(GL_MODELVIEW); |
|
|
|
glEnable(GL_BLEND); |
|
|
|
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); |
|
|
|
} |
|
|
|
|
|
|
|
/* The main drawing function. */ |
|
|
|
void DrawGLScene() |
|
|
|
void PutMap(int const *themap) |
|
|
|
{ |
|
|
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
|
|
|
glLoadIdentity(); |
|
|
|
|
|
|
|
glTranslatef(0.0f, 0.0f, -5.0f); |
|
|
|
|
|
|
|
glRotatef(xrot, 1.0f, 0.0f, 0.0f); |
|
|
|
glRotatef(yrot, 0.0f, 1.0f, 0.0f); |
|
|
|
glRotatef(zrot, 0.0f, 0.0f, 1.0f); |
|
|
|
// Put map |
|
|
|
float uvs[8 * 20 * 15]; |
|
|
|
|
|
|
|
for (int y = 0; y < 15; y++) |
|
|
|
for (int x = 0; x < 20; x++) |
|
|
|
{ |
|
|
|
int tile = themap[20 * y + x]; |
|
|
|
float ty = .0625f * (tile / 16); |
|
|
|
float tx = .0625f * (tile % 16); |
|
|
|
uvs[8 * (20 * y + x) + 0] = tx; |
|
|
|
uvs[8 * (20 * y + x) + 1] = ty; |
|
|
|
uvs[8 * (20 * y + x) + 2] = tx + .0625f; |
|
|
|
uvs[8 * (20 * y + x) + 3] = ty; |
|
|
|
uvs[8 * (20 * y + x) + 4] = tx + .0625f; |
|
|
|
uvs[8 * (20 * y + x) + 5] = ty + .0625f; |
|
|
|
uvs[8 * (20 * y + x) + 6] = tx; |
|
|
|
uvs[8 * (20 * y + x) + 7] = ty + .0625f; |
|
|
|
} |
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, buflist[1]); |
|
|
|
glBufferData(GL_ARRAY_BUFFER, |
|
|
|
8 * 20 * 15 * sizeof(float), uvs, GL_STATIC_DRAW); |
|
|
|
|
|
|
|
glEnableClientState(GL_VERTEX_ARRAY); |
|
|
|
glEnableClientState(GL_TEXTURE_COORD_ARRAY); |
|
|
|
glEnableClientState(GL_INDEX_ARRAY); |
|
|
|
|
|
|
|
glBindTexture(GL_TEXTURE_2D, texture[0]); |
|
|
|
|
|
|
|
glBegin(GL_QUADS); |
|
|
|
|
|
|
|
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f); |
|
|
|
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f); |
|
|
|
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f); |
|
|
|
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f); |
|
|
|
|
|
|
|
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f); |
|
|
|
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f); |
|
|
|
glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f); |
|
|
|
glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f); |
|
|
|
|
|
|
|
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f); |
|
|
|
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, 1.0f, 1.0f); |
|
|
|
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, 1.0f, 1.0f); |
|
|
|
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f); |
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, buflist[0]); |
|
|
|
glVertexPointer(2, GL_FLOAT, 0, NULL); |
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, buflist[1]); |
|
|
|
glTexCoordPointer(2, GL_FLOAT, 0, NULL); |
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, buflist[2]); |
|
|
|
glIndexPointer(GL_INT, 0, NULL); |
|
|
|
|
|
|
|
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, -1.0f, -1.0f); |
|
|
|
glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, -1.0f, -1.0f); |
|
|
|
glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f); |
|
|
|
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f); |
|
|
|
glDrawArrays(GL_QUADS, 0, 4 * 20 * 15); |
|
|
|
|
|
|
|
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f); |
|
|
|
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f); |
|
|
|
glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f); |
|
|
|
glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f); |
|
|
|
|
|
|
|
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f); |
|
|
|
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f); |
|
|
|
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f); |
|
|
|
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f); |
|
|
|
glDisableClientState(GL_VERTEX_ARRAY); |
|
|
|
glDisableClientState(GL_TEXTURE_COORD_ARRAY); |
|
|
|
glDisableClientState(GL_INDEX_ARRAY); |
|
|
|
} |
|
|
|
|
|
|
|
glEnd(); |
|
|
|
/* The main drawing function. */ |
|
|
|
void DrawScene() |
|
|
|
{ |
|
|
|
int ground[20 * 15] = |
|
|
|
{ |
|
|
|
18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, |
|
|
|
18, 1, 2, 2, 2, 34, 2, 2, 2, 2, 2, 2, 3, 34, 4, 18, 18, 18, 18, 18, |
|
|
|
18, 17, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 17, 18, 20, 4, 18, 18, 18, 18, |
|
|
|
18, 19, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 17, 18, 17, 19, 18, 18, 18, 18, |
|
|
|
18, 17, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 17, 18, 17, 17, 18, 18, 18, 18, |
|
|
|
18, 17, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 19, 18, 20, 36, 18, 18, 18, 18, |
|
|
|
18, 33, 2, 2, 2, 2, 2, 2, 2, 2, 34, 2, 35, 2, 36, 18, 18, 18, 18, 18, |
|
|
|
18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, |
|
|
|
16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, |
|
|
|
16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, |
|
|
|
16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, |
|
|
|
16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, |
|
|
|
16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, |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0, 0, 0, 0, 32, 0, 0, 0, 0, 0, |
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
|
|
|
}; |
|
|
|
|
|
|
|
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); |
|
|
|
glLoadIdentity(); |
|
|
|
|
|
|
|
xrot += 0.2f; |
|
|
|
yrot += 0.2f; |
|
|
|
zrot += 0.2f; |
|
|
|
PutMap(ground); |
|
|
|
//glTranslatef(10.0f * sinf(0.16f * frames), 10.0f * cosf(0.16f * frames), 0.0f); |
|
|
|
PutMap(l1objects); |
|
|
|
|
|
|
|
SDL_GL_SwapBuffers(); |
|
|
|
} |
|
|
@@ -120,14 +200,14 @@ int main(int argc, char **argv) |
|
|
|
int done; |
|
|
|
|
|
|
|
/* Initialize SDL for video output */ |
|
|
|
if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) |
|
|
|
if (SDL_Init(SDL_INIT_VIDEO) < 0) |
|
|
|
{ |
|
|
|
fprintf(stderr, "Unable to initialize SDL: %s\n", SDL_GetError()); |
|
|
|
exit(1); |
|
|
|
} |
|
|
|
|
|
|
|
/* Create a 640x480 OpenGL screen */ |
|
|
|
if ( SDL_SetVideoMode(640, 480, 0, SDL_OPENGL) == NULL ) |
|
|
|
if (SDL_SetVideoMode(640, 480, 0, SDL_OPENGL) == NULL) |
|
|
|
{ |
|
|
|
fprintf(stderr, "Unable to create OpenGL screen: %s\n", SDL_GetError()); |
|
|
|
SDL_Quit(); |
|
|
@@ -140,22 +220,32 @@ int main(int argc, char **argv) |
|
|
|
/* Loop, drawing and checking events */ |
|
|
|
InitGL(640, 480); |
|
|
|
done = 0; |
|
|
|
while ( !done ) |
|
|
|
frames = 0; |
|
|
|
Uint32 ticks = SDL_GetTicks(); |
|
|
|
Uint32 start = ticks; |
|
|
|
while (!done) |
|
|
|
{ |
|
|
|
DrawGLScene(); |
|
|
|
DrawScene(); |
|
|
|
frames++; |
|
|
|
|
|
|
|
/* This could go in a separate function */ |
|
|
|
SDL_Event event; |
|
|
|
while ( SDL_PollEvent(&event) ) |
|
|
|
while (SDL_PollEvent(&event)) |
|
|
|
{ |
|
|
|
if ( event.type == SDL_QUIT ) |
|
|
|
if (event.type == SDL_QUIT) |
|
|
|
done = 1; |
|
|
|
if ( event.type == SDL_KEYDOWN ) |
|
|
|
if ( event.key.keysym.sym == SDLK_ESCAPE ) |
|
|
|
if (event.type == SDL_KEYDOWN) |
|
|
|
if (event.key.keysym.sym == SDLK_ESCAPE) |
|
|
|
done = 1; |
|
|
|
} |
|
|
|
|
|
|
|
while (SDL_GetTicks() < ticks + 33) |
|
|
|
SDL_Delay(1); |
|
|
|
ticks = SDL_GetTicks(); |
|
|
|
} |
|
|
|
printf("%i fps\n", frames * 1000 / (SDL_GetTicks() - start)); |
|
|
|
SDL_Quit(); |
|
|
|
return 1; |
|
|
|
|
|
|
|
return EXIT_SUCCESS; |
|
|
|
} |
|
|
|
|