diff options
author | Eric Anholt <eric@anholt.net> | 2009-07-21 20:38:53 -0700 |
---|---|---|
committer | Eric Anholt <eric@anholt.net> | 2009-07-21 20:54:37 -0700 |
commit | 64575ce8f515b6c2f997ebe509f858cb4d384c50 (patch) | |
tree | d109a640194ece0fd439a34246a127cfe497d895 | |
parent | b2ab26e1d95bda557517a73d67c9cc0aa2514e5e (diff) |
Use SDL instead of GLUT.
Cribbed thoroughly from idr's stuff.
-rw-r--r-- | Makefile.am | 2 | ||||
-rw-r--r-- | configure.ac | 4 | ||||
-rw-r--r-- | glass.c | 143 | ||||
-rw-r--r-- | glass.h | 3 |
4 files changed, 118 insertions, 34 deletions
diff --git a/Makefile.am b/Makefile.am index 0180cfc..67483dd 100644 --- a/Makefile.am +++ b/Makefile.am @@ -1,7 +1,7 @@ AUTOMAKE_OPTIONS = foreign ACLOCAL_AMFLAGS = -I m4 -LDADD = $(PNG_LIBS) $(GL_LIBS) -lglut +LDADD = $(PNG_LIBS) $(GL_LIBS) CFLAGS = $(PNG_CFLAGS) $(GL_CFLAGS) $(WARN_CFLAGS) bin_PROGRAMS = glass diff --git a/configure.ac b/configure.ac index 7c17e32..56f84ed 100644 --- a/configure.ac +++ b/configure.ac @@ -46,11 +46,11 @@ WARN_CFLAGS="" if test "x$GCC" = "xyes"; then WARN_CFLAGS="-Wall -Wpointer-arith -Wstrict-prototypes \ -Wmissing-prototypes -Wmissing-declarations \ - -Wnested-externs -fno-strict-aliasing" + -Wnested-externs -fno-strict-aliasing -Wno-switch-enum" fi AC_SUBST([WARN_CFLAGS]) -PKG_CHECK_MODULES(GL, [gl]) +PKG_CHECK_MODULES(GL, [gl sdl]) GL_LIBS="$GL_LIBS -lGLEW" dnl libpng check, taken from cairo. Yuck. @@ -1,5 +1,6 @@ /* * Copyright © 2009 Eric Anholt + * Copyright © 2009 Ian D. Romanick * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), @@ -36,14 +37,10 @@ #include <sys/stat.h> #include <getopt.h> -#define GL_GLEXT_PROTOTYPES -#include <GL/glew.h> -#include <GL/glut.h> -#include <GL/gl.h> -#include <GL/glext.h> - #include "glass.h" +#include <SDL.h> + #define DEFAULT_WIDTH 600 #define DEFAULT_HEIGHT 700 @@ -88,6 +85,8 @@ static time_t start_tv_sec = 0; static float start_time, cur_time, last_fps_time = 0; static int no_multi_draw_arrays; static int frames = 0, last_fps_frames = 0; +static SDL_Surface *sdl_surf = NULL; +static GLboolean done = GL_FALSE; static double get_time_in_secs(void) @@ -238,7 +237,7 @@ draw(void) do_ring_drawelements(); } - glutSwapBuffers(); + SDL_GL_SwapBuffers(); frames++; } @@ -428,11 +427,14 @@ reshape(int width, int height) win_width = width; win_height = height; + sdl_surf = SDL_SetVideoMode(width, height, 0, + SDL_OPENGL | SDL_RESIZABLE); + if (sdl_surf == NULL) + errx(1, "video mode set fail\n"); + glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(80, win_width / (float)win_height, 0.2, 40); - - glutPostRedisplay(); } static GLint @@ -666,6 +668,10 @@ init(void) GLuint vbo; int sv; + reshape(DEFAULT_WIDTH, DEFAULT_HEIGHT); + + glewInit(); + if (!GLEW_VERSION_2_0) errx(1, "Requires GL 2.0\n"); if (!GLEW_ARB_vertex_array_object) @@ -710,8 +716,6 @@ init(void) glUnmapBuffer(GL_ARRAY_BUFFER_ARB); - reshape(win_width, win_height); - free(verts); load_program(); @@ -722,13 +726,30 @@ init(void) heightmap_tex = load_texture_rgb("heightmap.png"); } +/** + * Push an event each time the timer callback fires. + */ +static uint32_t +timer_callback(uint32_t interval, void *not_used) +{ + SDL_Event e; + + (void) not_used; + + e.type = SDL_USEREVENT; + e.user.code = 0; + e.user.data1 = NULL; + e.user.data2 = NULL; + SDL_PushEvent(&e); + + return interval; +} + static void idle(void) { cur_time = get_time_in_secs() - start_time; - glutPostRedisplay(); - if (cur_time > last_fps_time + 5) { printf("%d frames in %.2f secs: %.1f fps\n", frames - last_fps_frames, @@ -741,14 +762,19 @@ idle(void) } static void -key(unsigned char k, int x, int y) +key(SDLKey sym) { - switch (k) { + switch (sym) { case 'c': load_program(); break; - case 27: /* escape */ - exit(0); + case 'f': + SDL_WM_ToggleFullScreen(sdl_surf); + break; + case SDLK_ESCAPE: + done = GL_TRUE; + break; + default: break; } } @@ -759,22 +785,73 @@ static void usage(char *program) exit(1); } +static void +sdl_event(SDL_Event *event) +{ + static int shift_status = 0; + + switch (event->type) { + case SDL_VIDEORESIZE: + reshape(event->resize.w, event->resize.h); + break; + + case SDL_QUIT: + done = GL_TRUE; + break; + + case SDL_KEYDOWN: + if ((event->key.keysym.sym == SDLK_RSHIFT) || + (event->key.keysym.sym == SDLK_LSHIFT)) { + shift_status++; + } else { + key(event->key.keysym.sym); + } + break; + + case SDL_KEYUP: + if ((event->key.keysym.sym == SDLK_RSHIFT) + || (event->key.keysym.sym == SDLK_LSHIFT)) { + shift_status--; + } + + /* By clamping the shift status value to 0 we + * prevent some bugs when the program is + * started with one or both of the shift keys + * held down. + */ + if (shift_status < 0) { + shift_status = 0; + } + break; + + + case SDL_MOUSEBUTTONDOWN: + break; + case SDL_MOUSEBUTTONUP: + break; + case SDL_MOUSEMOTION: + break; + case SDL_VIDEOEXPOSE: + break; + default: + if (event->type != SDL_USEREVENT) { + printf("event = 0x%04x\n", event->type); + } + break; + } +} + int main(int argc, char **argv) { int i; start_time = get_time_in_secs(); - glutInit(&argc, argv); - glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE); - glutInitWindowPosition(0, 0); - glutInitWindowSize(DEFAULT_WIDTH, DEFAULT_HEIGHT); - glutCreateWindow("glass"); + if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) < 0) + errx(1, "sdl init fail"); + atexit(SDL_Quit); - glutDisplayFunc(draw); - glutReshapeFunc(reshape); - glutKeyboardFunc(key); - glutIdleFunc(idle); + (void)SDL_AddTimer(10, timer_callback, NULL); for (i = 1; i < argc; i++) { if (strcmp(argv[i], "-nomultidraw") == 0) @@ -783,10 +860,20 @@ int main(int argc, char **argv) usage(argv[0]); } - glewInit(); init(); - glutMainLoop(); + while (!done) { + SDL_Event event; + + SDL_WaitEvent(&event); + do { + sdl_event(&event); + + } while (SDL_PollEvent(&event)); + + idle(); + draw(); + } return 0; } @@ -27,9 +27,6 @@ #define GL_GLEXT_PROTOTYPES #include <GL/glew.h> -#include <GL/glut.h> -#include <GL/gl.h> -#include <GL/glext.h> /* matrix.c */ void mult_m4_p4(float *result, const float *mat4, const float *point4); |