From e41dd3a949221bd1776428e1c318c629acf68038 Mon Sep 17 00:00:00 2001 From: Sam Hocevar Date: Mon, 21 Feb 2011 17:11:36 +0000 Subject: [PATCH] Create the SdlApp class to avoid code duplication. Ensure USE_SDL is tested wherever relevant. --- configure.ac | 2 +- src/Makefile.am | 2 +- src/audio.cpp | 7 ++- src/input.cpp | 20 +++++--- src/lolgl.h | 5 ++ src/sample.cpp | 15 +++++- src/sdlapp.cpp | 92 ++++++++++++++++++++++++++++++++++ src/sdlapp.h | 36 +++++++++++++ src/tileset.cpp | 24 +++++++-- win32/deushax.vcxproj | 2 + win32/deushax.vcxproj.filters | 6 +++ win32/editor.vcxproj | 2 + win32/editor.vcxproj.filters | 6 +++ win32/monsterz.vcxproj | 2 + win32/monsterz.vcxproj.filters | 6 +++ 15 files changed, 211 insertions(+), 16 deletions(-) create mode 100644 src/sdlapp.cpp create mode 100644 src/sdlapp.h diff --git a/configure.ac b/configure.ac index 7af17d00..9697eebe 100644 --- a/configure.ac +++ b/configure.ac @@ -125,7 +125,7 @@ CPPFLAGS="${save_CPPFLAGS}" if test "${ac_cv_my_have_sdl}" = "no"; then AC_MSG_ERROR([[One of SDL, SDL_Image or SDL_Mixer not found]]) else - AC_DEFINE(USE_SDL, 1, Define to 1 to use SDL_image) + AC_DEFINE(USE_SDL, 1, Define to 1 to use SDL) fi AM_CONDITIONAL(USE_SDL, test "${ac_cv_my_have_sdl}" = "yes") diff --git a/src/Makefile.am b/src/Makefile.am index 0239393f..eac6f83c 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -11,7 +11,7 @@ liblol_a_SOURCES = \ text.cpp text.h emitter.cpp emitter.h numeric.h hash.cpp hash.h \ worldentity.cpp worldentity.h shader.cpp shader.h \ \ - sdlinput.cpp sdlinput.h \ + sdlapp.cpp sdlapp.h sdlinput.cpp sdlinput.h \ \ debugfps.cpp debugfps.h debugsphere.cpp debugsphere.h \ debugrecord.cpp debugrecord.h debugstats.cpp debugstats.h diff --git a/src/audio.cpp b/src/audio.cpp index cc344707..8e801ff1 100644 --- a/src/audio.cpp +++ b/src/audio.cpp @@ -14,7 +14,10 @@ #include -#include +#if defined USE_SDL +# include +# include +#endif #include "core.h" @@ -24,6 +27,8 @@ void Audio::Setup(int channels) { +#if defined USE_SDL Mix_OpenAudio(22050, AUDIO_S16, channels, 1024); +#endif } diff --git a/src/input.cpp b/src/input.cpp index 22581a3e..f9574ee1 100644 --- a/src/input.cpp +++ b/src/input.cpp @@ -12,12 +12,14 @@ # include "config.h" #endif -#include - #include #include #include +#if defined USE_SDL +# include +#endif + #include "core.h" /* @@ -56,18 +58,22 @@ static InputData * const data = &inputdata; vec2 Input::GetAxis(int axis) { float invsqrt2 = sqrtf(0.5f); - vec2 f; + vec2 ret; +#if defined USE_SDL /* Simulate a joystick using the keyboard. This SDL call is free. */ Uint8 *keystate = SDL_GetKeyState(NULL); int left = keystate[SDLK_d] - (keystate[SDLK_a] | keystate[SDLK_q]); int up = (keystate[SDLK_w] | keystate[SDLK_z]) - keystate[SDLK_s] ; - f.x += left; - f.y += up; + ret.x += left; + ret.y += up; if (left && up) - f = f * invsqrt2; + ret = ret * invsqrt2; +#else + ret = 0; +#endif - return f; + return ret; } vec2i Input::GetMousePos() diff --git a/src/lolgl.h b/src/lolgl.h index ef651ca0..8ab5f0fa 100644 --- a/src/lolgl.h +++ b/src/lolgl.h @@ -18,6 +18,11 @@ #define GL_GLEXT_PROTOTYPES +/* Defines for exotic platforms (until they get their config.h) */ +#ifdef ANDROID_NDK +# define HAVE_GLES_1X +#endif + /* Only define one GL platform */ #if defined HAVE_GL_1X # undef HAVE_GLES_1X diff --git a/src/sample.cpp b/src/sample.cpp index d60297dd..6d43b94a 100644 --- a/src/sample.cpp +++ b/src/sample.cpp @@ -13,10 +13,13 @@ #endif #include +#include #include -#include -#include +#if defined USE_SDL +# include +# include +#endif #include "core.h" @@ -30,7 +33,9 @@ class SampleData private: char *name, *path; +#if defined USE_SDL Mix_Chunk *chunk; +#endif }; /* @@ -44,6 +49,7 @@ Sample::Sample(char const *path) data->path = data->name + 9; sprintf(data->name, " %s", path); +#if defined USE_SDL data->chunk = Mix_LoadWAV(path); if (!data->chunk) { @@ -53,11 +59,14 @@ Sample::Sample(char const *path) SDL_Quit(); exit(1); } +#endif } Sample::~Sample() { +#if defined USE_SDL Mix_FreeChunk(data->chunk); +#endif free(data->name); delete data; } @@ -74,6 +83,8 @@ char const *Sample::GetName() void Sample::Play() { +#if defined USE_SDL Mix_PlayChannel(-1, data->chunk, 0); +#endif } diff --git a/src/sdlapp.cpp b/src/sdlapp.cpp new file mode 100644 index 00000000..2c66fb18 --- /dev/null +++ b/src/sdlapp.cpp @@ -0,0 +1,92 @@ +// +// Lol Engine +// +// Copyright: (c) 2010-2011 Sam Hocevar +// This program is free software; you can redistribute it and/or +// modify it under the terms of the Do What The Fuck You Want To +// Public License, Version 2, as published by Sam Hocevar. See +// http://sam.zoy.org/projects/COPYING.WTFPL for more details. +// + +#if defined HAVE_CONFIG_H +# include "config.h" +#endif + +#if defined USE_SDL +# include +#endif + +#include "core.h" +#include "sdlapp.h" + +/* + * SDL App implementation class + */ + +class SdlAppData +{ + friend class SdlApp; + +private: + int unused; +}; + +/* + * Public SdlApp class + */ + +SdlApp::SdlApp(char const *title, vec2i res, float fps) : + data(new SdlAppData()) +{ +#if defined USE_SDL + /* Initialise SDL */ + if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0) + { + fprintf(stderr, "Cannot initialise SDL: %s\n", SDL_GetError()); + exit(EXIT_FAILURE); + } + + SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); + SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16); + SDL_Surface *video = SDL_SetVideoMode(res.x, res.y, 0, SDL_OPENGL); + if (!video) + { + fprintf(stderr, "Cannot create OpenGL screen: %s\n", SDL_GetError()); + SDL_Quit(); + exit(EXIT_FAILURE); + } + + SDL_WM_SetCaption(title, NULL); + SDL_ShowCursor(0); + + /* Initialise everything */ + Ticker::Setup(fps); + Video::Setup(video->w, video->h); + Audio::Setup(2); +#endif +} + +void SdlApp::Run() +{ + while (!Ticker::Finished()) + { + /* Tick the game */ + Ticker::TickGame(); + + /* Tick the renderer, show the frame and clamp to desired framerate. */ + Ticker::TickDraw(); +#if defined USE_SDL + SDL_GL_SwapBuffers(); +#endif + Ticker::ClampFps(); + } +} + +SdlApp::~SdlApp() +{ +#if defined USE_SDL + SDL_Quit(); +#endif + free(data); +} + diff --git a/src/sdlapp.h b/src/sdlapp.h new file mode 100644 index 00000000..120550fd --- /dev/null +++ b/src/sdlapp.h @@ -0,0 +1,36 @@ +// +// Lol Engine +// +// Copyright: (c) 2010-2011 Sam Hocevar +// This program is free software; you can redistribute it and/or +// modify it under the terms of the Do What The Fuck You Want To +// Public License, Version 2, as published by Sam Hocevar. See +// http://sam.zoy.org/projects/COPYING.WTFPL for more details. +// + +// +// The SdlApp class +// ---------------- +// + +#if !defined __DH_SDLAPP_H__ +#define __DH_SDLAPP_H__ + +#include "matrix.h" + +class SdlAppData; + +class SdlApp +{ +public: + SdlApp(char const *title, vec2i res, float fps); + virtual ~SdlApp(); + + void Run(); + +private: + SdlAppData *data; +}; + +#endif // __DH_SDLAPP_H__ + diff --git a/src/tileset.cpp b/src/tileset.cpp index de579a36..d755bda2 100644 --- a/src/tileset.cpp +++ b/src/tileset.cpp @@ -13,6 +13,7 @@ #endif #include +#include #include #ifdef WIN32 @@ -20,8 +21,10 @@ # include #endif -#include -#include +#if defined USE_SDL +# include +# include +#endif #include "core.h" #include "lolgl.h" @@ -40,7 +43,9 @@ private: vec2i size, count; float dilate, tx, ty; +#if defined USE_SDL SDL_Surface *img; +#endif GLuint texture; }; @@ -56,9 +61,12 @@ TileSet::TileSet(char const *path, vec2i size, vec2i count, float dilate) sprintf(data->name, " %s", path); data->tiles = NULL; +#if defined USE_SDL data->img = NULL; +#endif data->texture = 0; +#if defined USE_SDL for (char const *name = path; *name; name++) if ((data->img = IMG_Load(name))) break; @@ -86,10 +94,12 @@ TileSet::TileSet(char const *path, vec2i size, vec2i count, float dilate) data->size = size; } - data->dilate = dilate; - data->ntiles = data->count.i * data->count.j; data->tx = (float)data->size.x / PotUp(data->img->w); data->ty = (float)data->size.y / PotUp(data->img->h); +#endif + + data->dilate = dilate; + data->ntiles = data->count.i * data->count.j; drawgroup = DRAWGROUP_BEFORE; } @@ -105,6 +115,7 @@ void TileSet::TickDraw(float deltams) { Entity::TickDraw(deltams); +#if defined USE_SDL if (IsDestroying()) { if (data->img) @@ -145,6 +156,7 @@ void TileSet::TickDraw(float deltams) SDL_FreeSurface(data->img); data->img = NULL; } +#endif } char const *TileSet::GetName() @@ -164,8 +176,10 @@ vec2i TileSet::GetCount() const void TileSet::Bind() { +#if defined USE_SDL if (!data->img) glBindTexture(GL_TEXTURE_2D, data->texture); +#endif } void TileSet::BlitTile(uint32_t id, int x, int y, int z, int o, @@ -179,6 +193,7 @@ void TileSet::BlitTile(uint32_t id, int x, int y, int z, int o, int dy = o ? 0 : data->size.y; int dz = o ? data->size.y : 0; +#if defined USE_SDL if (!data->img) { float tmp[10]; @@ -220,6 +235,7 @@ void TileSet::BlitTile(uint32_t id, int x, int y, int z, int o, *texture++ = ty + data->ty; } else +#endif { memset(vertex, 0, 3 * sizeof(float)); memset(texture, 0, 2 * sizeof(float)); diff --git a/win32/deushax.vcxproj b/win32/deushax.vcxproj index fac23f40..fe1ccc55 100644 --- a/win32/deushax.vcxproj +++ b/win32/deushax.vcxproj @@ -36,6 +36,7 @@ + @@ -70,6 +71,7 @@ + diff --git a/win32/deushax.vcxproj.filters b/win32/deushax.vcxproj.filters index 4cfc9306..155a856b 100644 --- a/win32/deushax.vcxproj.filters +++ b/win32/deushax.vcxproj.filters @@ -66,6 +66,9 @@ lolengine + + lolengine + lolengine @@ -157,6 +160,9 @@ lolengine + + lolengine + lolengine diff --git a/win32/editor.vcxproj b/win32/editor.vcxproj index 211f6f32..799f4368 100644 --- a/win32/editor.vcxproj +++ b/win32/editor.vcxproj @@ -36,6 +36,7 @@ + @@ -70,6 +71,7 @@ + diff --git a/win32/editor.vcxproj.filters b/win32/editor.vcxproj.filters index bbcfa161..f0da3856 100644 --- a/win32/editor.vcxproj.filters +++ b/win32/editor.vcxproj.filters @@ -66,6 +66,9 @@ lolengine + + lolengine + lolengine @@ -157,6 +160,9 @@ lolengine + + lolengine + lolengine diff --git a/win32/monsterz.vcxproj b/win32/monsterz.vcxproj index a842a73b..4155a973 100644 --- a/win32/monsterz.vcxproj +++ b/win32/monsterz.vcxproj @@ -41,6 +41,7 @@ + @@ -79,6 +80,7 @@ + diff --git a/win32/monsterz.vcxproj.filters b/win32/monsterz.vcxproj.filters index 2390c597..bf7de2c2 100644 --- a/win32/monsterz.vcxproj.filters +++ b/win32/monsterz.vcxproj.filters @@ -66,6 +66,9 @@ lolengine + + lolengine + lolengine @@ -162,6 +165,9 @@ lolengine + + lolengine + lolengine