diff options
author | Jinghua Luo <sunmoon1997@gmail.com> | 2007-02-04 14:20:54 +0800 |
---|---|---|
committer | Jinghua Luo <sunmoon1997@gmail.com> | 2007-02-04 14:20:54 +0800 |
commit | 9062f3acc189bd0babfcec6f3a10d35dbf194a6f (patch) | |
tree | fdb072204e89494b1fcc3a0dccc733a2cbc145d0 | |
parent | 2dc542d03c53eb73d1a3cfba28fb19d49d8a0f92 (diff) |
test: add opengl test.
-rw-r--r-- | src/.gitignore | 1 | ||||
-rw-r--r-- | src/Makefile.am | 8 | ||||
-rw-r--r-- | src/sdl-opengl-test.c | 115 |
3 files changed, 123 insertions, 1 deletions
diff --git a/src/.gitignore b/src/.gitignore index 8735953..65f9622 100644 --- a/src/.gitignore +++ b/src/.gitignore @@ -13,3 +13,4 @@ hash-test cache-test font-test sdl-test +sdl-opengl-test diff --git a/src/Makefile.am b/src/Makefile.am index e58db11..e0a452e 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -52,9 +52,12 @@ libsdl_freetype_opengl_la_CFLAGS = $(GL_CFLAGS) $(AM_CFLAGS) if HAVE_SDL sdl_test = sdl-test +if HAVE_OPENGL +sdl_opengl_test = sdl-opengl-test +endif endif -noinst_PROGRAMS = hash-test cache-test font-test $(sdl_test) +noinst_PROGRAMS = hash-test cache-test font-test $(sdl_test) $(sdl_opengl_test) hash_test_SOURCES = hash-test.c hash_test_LDADD = libsdl-freetype.la @@ -67,6 +70,9 @@ font_test_LDADD = libsdl-ft.la sdl_test_SOURCES = sdl-test.c sdl_test_LDADD = libsdl-ft.la +sdl_opengl_test_SOURCES = sdl-opengl-test.c +sdl_opengl_test_LDADD = libsdl-freetype-opengl.la libsdl-ft.la + lib_LTLIBRARIES = \ libsdl-freetype.la \ $(libsdl_ft) \ diff --git a/src/sdl-opengl-test.c b/src/sdl-opengl-test.c new file mode 100644 index 0000000..b0d77c8 --- /dev/null +++ b/src/sdl-opengl-test.c @@ -0,0 +1,115 @@ +#include <SDL.h> +#include <GL/gl.h> + +#include <sdl-freetype.h> +#include <sdl-freetype-opengl.h> +#include <sdl-ft.h> + +#include <stdio.h> + +#define TEXT "sdl freetype OpenGL render" + +int +main(int argc, char *argv[]) +{ + SDL_Surface * screen; + Uint8 video_bpp; + Uint32 videoflags; + int done; + SDL_Event event; + float alpha, step; + sdl_freetype_font_t * font; + sdl_freetype_glyph_render_t * render; + sdl_freetype_text_extents_t extents; + + /* Initialize SDL */ + if (SDL_Init(SDL_INIT_VIDEO) < 0) { + fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError()); + goto errquit0; + } + + videoflags = SDL_HWSURFACE; + videoflags |= SDL_OPENGL; + video_bpp = 32; + + /* Set 640x480 video mode */ + screen = SDL_SetVideoMode(640, 480, video_bpp, videoflags); + if (!screen) { + fprintf(stderr, "Couldn't set 640x480x%d video mode: %s\n", + video_bpp, SDL_GetError()); + goto errquit0; + } + + font = sdl_ft_create_font ("sans", 20, + SDL_FT_WEIGHT_NORMAL, SDL_FT_SLANT_NORMAL, 96); + if (!font) + goto errquit0; + + render = sdl_freetype_opengl_render_create (); + if (!render) + goto errquit1; + sdl_freetype_opengl_render_set_unit_scale (render, 1.0 / 320, 1.0 / 240); + sdl_freetype_font_set_render (font, render); + + glEnable (GL_TEXTURE_2D); + glShadeModel (GL_SMOOTH); + glClearColor (0.8f, 0.8f, 0.8f, 0.0f); + glClearDepth (1.0f); + glEnable (GL_DEPTH_TEST); + glDepthFunc (GL_LEQUAL); + glHint (GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); + + alpha = 1.0f; + step = -0.01; + /* Wait for a keystroke */ + done = 0; + while (!done) { + /* Check for events */ + while (SDL_PollEvent(&event)) { + switch (event.type) { + case SDL_MOUSEBUTTONDOWN: + break; + case SDL_KEYDOWN: + case SDL_QUIT: + done = 1; + break; + default: + break; + } + } + + glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + glLoadIdentity (); + + glEnable (GL_BLEND); + glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + + glScalef (alpha + 1.0, alpha + 1.0, 1.0); + glRotatef (alpha * 360, 1.0, 1.0, 0); + sdl_freetype_font_utf8_extents (font, &extents, TEXT, -1); + glTranslatef (-extents.width / (320.0 * 2), -extents.height / (240.0 * 2), 0); + sdl_freetype_font_show_utf8 (font, NULL, 255, 0, 0, alpha * 255, + 0, 0, TEXT, -1); + + SDL_GL_SwapBuffers(); + + alpha += step; + if (alpha < 0.0f) { + alpha = 0.0f; + step = -step; + } else if (alpha > 1.0f) { + alpha = 1.0f; + step = -step; + } + + SDL_Delay(20); + } + + errquit1: + sdl_freetype_font_destroy (font); + sdl_freetype_fini (); + + errquit0: + SDL_Quit(); + return 0; +} |