diff options
author | Matt Turner <mattst88@gmail.com> | 2010-01-21 15:08:48 -0500 |
---|---|---|
committer | Matt Turner <mattst88@gmail.com> | 2011-04-04 17:14:35 -0400 |
commit | bf6f824b24b18197eabb7737f8bac8cc8a4baa8c (patch) | |
tree | e0fa7056baac4a53a48daaf47c0316557c6c1a41 | |
parent | 8be6ba5efd36c67b0486b313603bf3085c5f328a (diff) |
Switch to glpong's HandleEvent's function
Signed-off-by: Matt Turner <mattst88@gmail.com>
-rw-r--r-- | glpong3d.c | 68 | ||||
-rw-r--r-- | glpong3d.h | 22 |
2 files changed, 62 insertions, 28 deletions
@@ -11,7 +11,7 @@ #include "glpong3d.h" static void GLPong_Init(void); -static void GLPong_HandleEvents(void); +static int GLPong_HandleEvents(void); static void GLPong_Draw(void); static void GLPong_CleanUp(void); static void GLPong_Move(void); @@ -21,6 +21,9 @@ GLuint box; int main(int argc, char * argv[]) { clock_t start; unsigned int frames = 0; + int done = 0; + int action; + if (argc) { if (argv) {} } @@ -29,8 +32,14 @@ int main(int argc, char * argv[]) { start = SDL_GetTicks(); - while ( !GLPong.done ) { - GLPong_HandleEvents(); + while (!done) { + action = GLPong_HandleEvents(); + switch (action) { + case GLPONG_EXIT: /* exit */ + done = 1; + break; + } + GLPong_Move(); GLPong_Draw(); frames++; @@ -52,11 +61,6 @@ void GLPong_Init(void) { int i; int count; - GLPong.done = 0; - - GLPong.w = 800; - GLPong.h = 600; - GLPong.Ball.w = 0.5f; GLPong.Ball.h = 0.5f; GLPong.Ball.x = -(GLPong.Ball.w / 2); @@ -121,12 +125,12 @@ void GLPong_Init(void) { SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 8); SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);; - GLPong.Surface = SDL_SetVideoMode(GLPong.w, GLPong.h, 32, SDL_OPENGL); - glViewport(0, 0, GLPong.w, GLPong.h); + GLPong.Surface = SDL_SetVideoMode(GLPONG_WIDTH, GLPONG_HEIGHT, 32, SDL_OPENGL); + glViewport(0, 0, GLPONG_WIDTH, GLPONG_HEIGHT); glMatrixMode(GL_PROJECTION); glLoadIdentity(); - gluPerspective(45.0f, GLPong.w / GLPong.h, 0.1f, 50.0f); + gluPerspective(45.0f, GLPONG_WIDTH / GLPONG_HEIGHT, 0.1f, 50.0f); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); @@ -138,8 +142,6 @@ void GLPong_Init(void) { glDepthFunc(GL_LEQUAL); glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); - GLPong.Event.type = SDL_NOEVENT; - printf("OpenGL Vendor: %s\n", glGetString(GL_VENDOR)); printf("OpenGL Renderer: %s\n", glGetString(GL_RENDERER)); printf("OpenGL Version: %s\n", glGetString(GL_VERSION)); @@ -172,25 +174,42 @@ void GLPong_Init(void) { glPointSize(10.0f); } -void GLPong_HandleEvents(void) { +static int +GLPong_HandleEvents(void) { + SDL_Event Event; Uint8 * key; int x = 0, y = 0; - if (SDL_PollEvent(&GLPong.Event) != 0) { - if (GLPong.Event.type == SDL_QUIT) { - GLPong.done = 1; - return; + + if (SDL_PollEvent(&Event) != 0) { + if (Event.type == SDL_QUIT) { + return GLPONG_EXIT; } - if (GLPong.Event.type == SDL_KEYDOWN) { - if (GLPong.Event.key.keysym.sym == SDLK_ESCAPE) { - GLPong.done = 1; - return; + if (Event.type == SDL_KEYDOWN) { + if (Event.key.keysym.sym == SDLK_ESCAPE) { + return GLPONG_EXIT; + } else if (Event.key.keysym.sym == SDLK_p) { + for (;;) { + SDL_Delay(10); + if (SDL_PollEvent(&Event) != 0) { + if (Event.type == SDL_QUIT) { + return GLPONG_EXIT; + } else if (Event.type == SDL_KEYDOWN){ + if (Event.key.keysym.sym == SDLK_ESCAPE) { + return GLPONG_EXIT; + } else if (Event.key.keysym.sym == SDLK_p) { + break; + } + } + } + } } } } + key = SDL_GetKeyState(NULL); SDL_GetMouseState(&x, &y); - GLPong.Mouse.x = ((float) x / GLPong.w) * 3.3f - 1.65f; + GLPong.Mouse.x = ((float) x / GLPONG_WIDTH) * 3.3f - 1.65f; if (GLPong.Mouse.x <= -1.5 + (GLPong.Front.w / 2)) { GLPong.Mouse.x = -1.5 + (GLPong.Front.w / 2); @@ -198,7 +217,7 @@ void GLPong_HandleEvents(void) { GLPong.Mouse.x = 1.5 - (GLPong.Front.w / 2); } - GLPong.Mouse.y = ((float) y / GLPong.h) * 2.5f - 1.25f; + GLPong.Mouse.y = ((float) y / GLPONG_HEIGHT) * 2.5f - 1.25f; if (GLPong.Mouse.y <= -1.0 + (GLPong.Front.h / 2)) { GLPong.Mouse.y = -1.0 + (GLPong.Front.h / 2); @@ -208,6 +227,7 @@ void GLPong_HandleEvents(void) { GLPong.Front.x = GLPong.Mouse.x - GLPong.Front.w / 2; GLPong.Front.y = -(GLPong.Mouse.y + GLPong.Front.h / 2); /*printf("SDL x,y: %d,%d; OpenGL x,y: %f,%f\n", x, y, GLPong.Mouse.x, GLPong.Mouse.y);*/ + return GLPONG_NOACTION; } void GLPong_Move(void) { @@ -4,6 +4,24 @@ #include "SDL.h" #include "SDL_opengl.h" +#define GLPONG_WIDTH 800.0f +#define GLPONG_HEIGHT 600.0f + +#define GLPONG_NOACTION 0 +#define GLPONG_EXIT 1 + +#if SDL_BYTEORDER == SDL_BIG_ENDIAN +#define RMASK 0xff000000 +#define GMASK 0x00ff0000 +#define BMASK 0x0000ff00 +#define AMASK 0x000000ff +#else +#define RMASK 0x000000ff +#define GMASK 0x0000ff00 +#define BMASK 0x00ff0000 +#define AMASK 0xff000000 +#endif + GLuint background; GLuint paddle_texture; @@ -32,17 +50,13 @@ typedef struct { } GLC_t; struct { - SDL_Event Event; SDL_Surface * Surface; Ball_t Ball; Paddle_t Front; Paddle_t Back; Mouse_t Mouse; GLC_t GLC; - GLfloat w; - GLfloat h; GLfloat fps; - Sint8 done; } GLPong; void GLPong_Collide(Ball_t * ball, const Paddle_t * paddle); |