tested wherever relevant.legacy
| @@ -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") | |||
| @@ -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 | |||
| @@ -14,7 +14,10 @@ | |||
| #include <cmath> | |||
| #include <SDL_mixer.h> | |||
| #if defined USE_SDL | |||
| # include <SDL.h> | |||
| # include <SDL_mixer.h> | |||
| #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 | |||
| } | |||
| @@ -12,12 +12,14 @@ | |||
| # include "config.h" | |||
| #endif | |||
| #include <SDL.h> | |||
| #include <cstdio> | |||
| #include <cstdlib> | |||
| #include <cmath> | |||
| #if defined USE_SDL | |||
| # include <SDL.h> | |||
| #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() | |||
| @@ -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 | |||
| @@ -13,10 +13,13 @@ | |||
| #endif | |||
| #include <cstdlib> | |||
| #include <cstdio> | |||
| #include <cmath> | |||
| #include <SDL.h> | |||
| #include <SDL_mixer.h> | |||
| #if defined USE_SDL | |||
| # include <SDL.h> | |||
| # include <SDL_mixer.h> | |||
| #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, "<sample> %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 | |||
| } | |||
| @@ -0,0 +1,92 @@ | |||
| // | |||
| // Lol Engine | |||
| // | |||
| // Copyright: (c) 2010-2011 Sam Hocevar <sam@hocevar.net> | |||
| // 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 <SDL.h> | |||
| #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); | |||
| } | |||
| @@ -0,0 +1,36 @@ | |||
| // | |||
| // Lol Engine | |||
| // | |||
| // Copyright: (c) 2010-2011 Sam Hocevar <sam@hocevar.net> | |||
| // 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__ | |||
| @@ -13,6 +13,7 @@ | |||
| #endif | |||
| #include <cstdlib> | |||
| #include <cstdio> | |||
| #include <cmath> | |||
| #ifdef WIN32 | |||
| @@ -20,8 +21,10 @@ | |||
| # include <windows.h> | |||
| #endif | |||
| #include <SDL.h> | |||
| #include <SDL_image.h> | |||
| #if defined USE_SDL | |||
| # include <SDL.h> | |||
| # include <SDL_image.h> | |||
| #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, "<tileset> %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)); | |||
| @@ -36,6 +36,7 @@ | |||
| <ClInclude Include="..\src\sample.h" /> | |||
| <ClInclude Include="..\src\sampler.h" /> | |||
| <ClInclude Include="..\src\scene.h" /> | |||
| <ClInclude Include="..\src\sdlapp.h" /> | |||
| <ClInclude Include="..\src\sdlinput.h" /> | |||
| <ClInclude Include="..\src\shader.h" /> | |||
| <ClInclude Include="..\src\text.h" /> | |||
| @@ -70,6 +71,7 @@ | |||
| <ClCompile Include="..\src\sample.cpp" /> | |||
| <ClCompile Include="..\src\sampler.cpp" /> | |||
| <ClCompile Include="..\src\scene.cpp" /> | |||
| <ClCompile Include="..\src\sdlapp.cpp" /> | |||
| <ClCompile Include="..\src\sdlinput.cpp" /> | |||
| <ClCompile Include="..\src\shader.cpp" /> | |||
| <ClCompile Include="..\src\text.cpp" /> | |||
| @@ -66,6 +66,9 @@ | |||
| <ClInclude Include="..\src\scene.h"> | |||
| <Filter>lolengine</Filter> | |||
| </ClInclude> | |||
| <ClInclude Include="..\src\sdlapp.h"> | |||
| <Filter>lolengine</Filter> | |||
| </ClInclude> | |||
| <ClInclude Include="..\src\sdlinput.h"> | |||
| <Filter>lolengine</Filter> | |||
| </ClInclude> | |||
| @@ -157,6 +160,9 @@ | |||
| <ClCompile Include="..\src\scene.cpp"> | |||
| <Filter>lolengine</Filter> | |||
| </ClCompile> | |||
| <ClCompile Include="..\src\sdlapp.cpp"> | |||
| <Filter>lolengine</Filter> | |||
| </ClCompile> | |||
| <ClCompile Include="..\src\sdlinput.cpp"> | |||
| <Filter>lolengine</Filter> | |||
| </ClCompile> | |||
| @@ -36,6 +36,7 @@ | |||
| <ClInclude Include="..\src\sample.h" /> | |||
| <ClInclude Include="..\src\sampler.h" /> | |||
| <ClInclude Include="..\src\scene.h" /> | |||
| <ClInclude Include="..\src\sdlapp.h" /> | |||
| <ClInclude Include="..\src\sdlinput.h" /> | |||
| <ClInclude Include="..\src\shader.h" /> | |||
| <ClInclude Include="..\src\text.h" /> | |||
| @@ -70,6 +71,7 @@ | |||
| <ClCompile Include="..\src\sample.cpp" /> | |||
| <ClCompile Include="..\src\sampler.cpp" /> | |||
| <ClCompile Include="..\src\scene.cpp" /> | |||
| <ClCompile Include="..\src\sdlapp.cpp" /> | |||
| <ClCompile Include="..\src\sdlinput.cpp" /> | |||
| <ClCompile Include="..\src\shader.cpp" /> | |||
| <ClCompile Include="..\src\text.cpp" /> | |||
| @@ -66,6 +66,9 @@ | |||
| <ClInclude Include="..\src\scene.h"> | |||
| <Filter>lolengine</Filter> | |||
| </ClInclude> | |||
| <ClInclude Include="..\src\sdlapp.h"> | |||
| <Filter>lolengine</Filter> | |||
| </ClInclude> | |||
| <ClInclude Include="..\src\sdlinput.h"> | |||
| <Filter>lolengine</Filter> | |||
| </ClInclude> | |||
| @@ -157,6 +160,9 @@ | |||
| <ClCompile Include="..\src\scene.cpp"> | |||
| <Filter>lolengine</Filter> | |||
| </ClCompile> | |||
| <ClCompile Include="..\src\sdlapp.cpp"> | |||
| <Filter>lolengine</Filter> | |||
| </ClCompile> | |||
| <ClCompile Include="..\src\sdlinput.cpp"> | |||
| <Filter>lolengine</Filter> | |||
| </ClCompile> | |||
| @@ -41,6 +41,7 @@ | |||
| <ClInclude Include="..\src\sample.h" /> | |||
| <ClInclude Include="..\src\sampler.h" /> | |||
| <ClInclude Include="..\src\scene.h" /> | |||
| <ClInclude Include="..\src\sdlapp.h" /> | |||
| <ClInclude Include="..\src\sdlinput.h" /> | |||
| <ClInclude Include="..\src\shader.h" /> | |||
| <ClInclude Include="..\src\text.h" /> | |||
| @@ -79,6 +80,7 @@ | |||
| <ClCompile Include="..\src\sample.cpp" /> | |||
| <ClCompile Include="..\src\sampler.cpp" /> | |||
| <ClCompile Include="..\src\scene.cpp" /> | |||
| <ClCompile Include="..\src\sdlapp.cpp" /> | |||
| <ClCompile Include="..\src\sdlinput.cpp" /> | |||
| <ClCompile Include="..\src\shader.cpp" /> | |||
| <ClCompile Include="..\src\text.cpp" /> | |||
| @@ -66,6 +66,9 @@ | |||
| <ClInclude Include="..\src\scene.h"> | |||
| <Filter>lolengine</Filter> | |||
| </ClInclude> | |||
| <ClInclude Include="..\src\sdlapp.h"> | |||
| <Filter>lolengine</Filter> | |||
| </ClInclude> | |||
| <ClInclude Include="..\src\sdlinput.h"> | |||
| <Filter>lolengine</Filter> | |||
| </ClInclude> | |||
| @@ -162,6 +165,9 @@ | |||
| <ClCompile Include="..\src\scene.cpp"> | |||
| <Filter>lolengine</Filter> | |||
| </ClCompile> | |||
| <ClCompile Include="..\src\sdlapp.cpp"> | |||
| <Filter>lolengine</Filter> | |||
| </ClCompile> | |||
| <ClCompile Include="..\src\sdlinput.cpp"> | |||
| <Filter>lolengine</Filter> | |||
| </ClCompile> | |||