Browse Source

Create the SdlApp class to avoid code duplication. Ensure USE_SDL is

tested wherever relevant.
legacy
Sam Hocevar sam 13 years ago
parent
commit
e41dd3a949
15 changed files with 211 additions and 16 deletions
  1. +1
    -1
      configure.ac
  2. +1
    -1
      src/Makefile.am
  3. +6
    -1
      src/audio.cpp
  4. +13
    -7
      src/input.cpp
  5. +5
    -0
      src/lolgl.h
  6. +13
    -2
      src/sample.cpp
  7. +92
    -0
      src/sdlapp.cpp
  8. +36
    -0
      src/sdlapp.h
  9. +20
    -4
      src/tileset.cpp
  10. +2
    -0
      win32/deushax.vcxproj
  11. +6
    -0
      win32/deushax.vcxproj.filters
  12. +2
    -0
      win32/editor.vcxproj
  13. +6
    -0
      win32/editor.vcxproj.filters
  14. +2
    -0
      win32/monsterz.vcxproj
  15. +6
    -0
      win32/monsterz.vcxproj.filters

+ 1
- 1
configure.ac View File

@@ -125,7 +125,7 @@ CPPFLAGS="${save_CPPFLAGS}"
if test "${ac_cv_my_have_sdl}" = "no"; then if test "${ac_cv_my_have_sdl}" = "no"; then
AC_MSG_ERROR([[One of SDL, SDL_Image or SDL_Mixer not found]]) AC_MSG_ERROR([[One of SDL, SDL_Image or SDL_Mixer not found]])
else 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 fi
AM_CONDITIONAL(USE_SDL, test "${ac_cv_my_have_sdl}" = "yes") AM_CONDITIONAL(USE_SDL, test "${ac_cv_my_have_sdl}" = "yes")




+ 1
- 1
src/Makefile.am View File

@@ -11,7 +11,7 @@ liblol_a_SOURCES = \
text.cpp text.h emitter.cpp emitter.h numeric.h hash.cpp hash.h \ text.cpp text.h emitter.cpp emitter.h numeric.h hash.cpp hash.h \
worldentity.cpp worldentity.h shader.cpp shader.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 \ debugfps.cpp debugfps.h debugsphere.cpp debugsphere.h \
debugrecord.cpp debugrecord.h debugstats.cpp debugstats.h debugrecord.cpp debugrecord.h debugstats.cpp debugstats.h


+ 6
- 1
src/audio.cpp View File

@@ -14,7 +14,10 @@


#include <cmath> #include <cmath>


#include <SDL_mixer.h>
#if defined USE_SDL
# include <SDL.h>
# include <SDL_mixer.h>
#endif


#include "core.h" #include "core.h"


@@ -24,6 +27,8 @@


void Audio::Setup(int channels) void Audio::Setup(int channels)
{ {
#if defined USE_SDL
Mix_OpenAudio(22050, AUDIO_S16, channels, 1024); Mix_OpenAudio(22050, AUDIO_S16, channels, 1024);
#endif
} }



+ 13
- 7
src/input.cpp View File

@@ -12,12 +12,14 @@
# include "config.h" # include "config.h"
#endif #endif


#include <SDL.h>

#include <cstdio> #include <cstdio>
#include <cstdlib> #include <cstdlib>
#include <cmath> #include <cmath>


#if defined USE_SDL
# include <SDL.h>
#endif

#include "core.h" #include "core.h"


/* /*
@@ -56,18 +58,22 @@ static InputData * const data = &inputdata;
vec2 Input::GetAxis(int axis) vec2 Input::GetAxis(int axis)
{ {
float invsqrt2 = sqrtf(0.5f); float invsqrt2 = sqrtf(0.5f);
vec2 f;
vec2 ret;


#if defined USE_SDL
/* Simulate a joystick using the keyboard. This SDL call is free. */ /* Simulate a joystick using the keyboard. This SDL call is free. */
Uint8 *keystate = SDL_GetKeyState(NULL); Uint8 *keystate = SDL_GetKeyState(NULL);
int left = keystate[SDLK_d] - (keystate[SDLK_a] | keystate[SDLK_q]); int left = keystate[SDLK_d] - (keystate[SDLK_a] | keystate[SDLK_q]);
int up = (keystate[SDLK_w] | keystate[SDLK_z]) - keystate[SDLK_s] ; 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) if (left && up)
f = f * invsqrt2;
ret = ret * invsqrt2;
#else
ret = 0;
#endif


return f;
return ret;
} }


vec2i Input::GetMousePos() vec2i Input::GetMousePos()


+ 5
- 0
src/lolgl.h View File

@@ -18,6 +18,11 @@


#define GL_GLEXT_PROTOTYPES #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 */ /* Only define one GL platform */
#if defined HAVE_GL_1X #if defined HAVE_GL_1X
# undef HAVE_GLES_1X # undef HAVE_GLES_1X


+ 13
- 2
src/sample.cpp View File

@@ -13,10 +13,13 @@
#endif #endif


#include <cstdlib> #include <cstdlib>
#include <cstdio>
#include <cmath> #include <cmath>


#include <SDL.h>
#include <SDL_mixer.h>
#if defined USE_SDL
# include <SDL.h>
# include <SDL_mixer.h>
#endif


#include "core.h" #include "core.h"


@@ -30,7 +33,9 @@ class SampleData


private: private:
char *name, *path; char *name, *path;
#if defined USE_SDL
Mix_Chunk *chunk; Mix_Chunk *chunk;
#endif
}; };


/* /*
@@ -44,6 +49,7 @@ Sample::Sample(char const *path)
data->path = data->name + 9; data->path = data->name + 9;
sprintf(data->name, "<sample> %s", path); sprintf(data->name, "<sample> %s", path);


#if defined USE_SDL
data->chunk = Mix_LoadWAV(path); data->chunk = Mix_LoadWAV(path);
if (!data->chunk) if (!data->chunk)
{ {
@@ -53,11 +59,14 @@ Sample::Sample(char const *path)
SDL_Quit(); SDL_Quit();
exit(1); exit(1);
} }
#endif
} }


Sample::~Sample() Sample::~Sample()
{ {
#if defined USE_SDL
Mix_FreeChunk(data->chunk); Mix_FreeChunk(data->chunk);
#endif
free(data->name); free(data->name);
delete data; delete data;
} }
@@ -74,6 +83,8 @@ char const *Sample::GetName()


void Sample::Play() void Sample::Play()
{ {
#if defined USE_SDL
Mix_PlayChannel(-1, data->chunk, 0); Mix_PlayChannel(-1, data->chunk, 0);
#endif
} }



+ 92
- 0
src/sdlapp.cpp View File

@@ -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);
}


+ 36
- 0
src/sdlapp.h View File

@@ -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__


+ 20
- 4
src/tileset.cpp View File

@@ -13,6 +13,7 @@
#endif #endif


#include <cstdlib> #include <cstdlib>
#include <cstdio>
#include <cmath> #include <cmath>


#ifdef WIN32 #ifdef WIN32
@@ -20,8 +21,10 @@
# include <windows.h> # include <windows.h>
#endif #endif


#include <SDL.h>
#include <SDL_image.h>
#if defined USE_SDL
# include <SDL.h>
# include <SDL_image.h>
#endif


#include "core.h" #include "core.h"
#include "lolgl.h" #include "lolgl.h"
@@ -40,7 +43,9 @@ private:
vec2i size, count; vec2i size, count;
float dilate, tx, ty; float dilate, tx, ty;


#if defined USE_SDL
SDL_Surface *img; SDL_Surface *img;
#endif
GLuint texture; GLuint texture;
}; };


@@ -56,9 +61,12 @@ TileSet::TileSet(char const *path, vec2i size, vec2i count, float dilate)
sprintf(data->name, "<tileset> %s", path); sprintf(data->name, "<tileset> %s", path);


data->tiles = NULL; data->tiles = NULL;
#if defined USE_SDL
data->img = NULL; data->img = NULL;
#endif
data->texture = 0; data->texture = 0;


#if defined USE_SDL
for (char const *name = path; *name; name++) for (char const *name = path; *name; name++)
if ((data->img = IMG_Load(name))) if ((data->img = IMG_Load(name)))
break; break;
@@ -86,10 +94,12 @@ TileSet::TileSet(char const *path, vec2i size, vec2i count, float dilate)
data->size = size; 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->tx = (float)data->size.x / PotUp(data->img->w);
data->ty = (float)data->size.y / PotUp(data->img->h); 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; drawgroup = DRAWGROUP_BEFORE;
} }
@@ -105,6 +115,7 @@ void TileSet::TickDraw(float deltams)
{ {
Entity::TickDraw(deltams); Entity::TickDraw(deltams);


#if defined USE_SDL
if (IsDestroying()) if (IsDestroying())
{ {
if (data->img) if (data->img)
@@ -145,6 +156,7 @@ void TileSet::TickDraw(float deltams)
SDL_FreeSurface(data->img); SDL_FreeSurface(data->img);
data->img = NULL; data->img = NULL;
} }
#endif
} }


char const *TileSet::GetName() char const *TileSet::GetName()
@@ -164,8 +176,10 @@ vec2i TileSet::GetCount() const


void TileSet::Bind() void TileSet::Bind()
{ {
#if defined USE_SDL
if (!data->img) if (!data->img)
glBindTexture(GL_TEXTURE_2D, data->texture); glBindTexture(GL_TEXTURE_2D, data->texture);
#endif
} }


void TileSet::BlitTile(uint32_t id, int x, int y, int z, int o, 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 dy = o ? 0 : data->size.y;
int dz = o ? data->size.y : 0; int dz = o ? data->size.y : 0;


#if defined USE_SDL
if (!data->img) if (!data->img)
{ {
float tmp[10]; 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; *texture++ = ty + data->ty;
} }
else else
#endif
{ {
memset(vertex, 0, 3 * sizeof(float)); memset(vertex, 0, 3 * sizeof(float));
memset(texture, 0, 2 * sizeof(float)); memset(texture, 0, 2 * sizeof(float));


+ 2
- 0
win32/deushax.vcxproj View File

@@ -36,6 +36,7 @@
<ClInclude Include="..\src\sample.h" /> <ClInclude Include="..\src\sample.h" />
<ClInclude Include="..\src\sampler.h" /> <ClInclude Include="..\src\sampler.h" />
<ClInclude Include="..\src\scene.h" /> <ClInclude Include="..\src\scene.h" />
<ClInclude Include="..\src\sdlapp.h" />
<ClInclude Include="..\src\sdlinput.h" /> <ClInclude Include="..\src\sdlinput.h" />
<ClInclude Include="..\src\shader.h" /> <ClInclude Include="..\src\shader.h" />
<ClInclude Include="..\src\text.h" /> <ClInclude Include="..\src\text.h" />
@@ -70,6 +71,7 @@
<ClCompile Include="..\src\sample.cpp" /> <ClCompile Include="..\src\sample.cpp" />
<ClCompile Include="..\src\sampler.cpp" /> <ClCompile Include="..\src\sampler.cpp" />
<ClCompile Include="..\src\scene.cpp" /> <ClCompile Include="..\src\scene.cpp" />
<ClCompile Include="..\src\sdlapp.cpp" />
<ClCompile Include="..\src\sdlinput.cpp" /> <ClCompile Include="..\src\sdlinput.cpp" />
<ClCompile Include="..\src\shader.cpp" /> <ClCompile Include="..\src\shader.cpp" />
<ClCompile Include="..\src\text.cpp" /> <ClCompile Include="..\src\text.cpp" />


+ 6
- 0
win32/deushax.vcxproj.filters View File

@@ -66,6 +66,9 @@
<ClInclude Include="..\src\scene.h"> <ClInclude Include="..\src\scene.h">
<Filter>lolengine</Filter> <Filter>lolengine</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="..\src\sdlapp.h">
<Filter>lolengine</Filter>
</ClInclude>
<ClInclude Include="..\src\sdlinput.h"> <ClInclude Include="..\src\sdlinput.h">
<Filter>lolengine</Filter> <Filter>lolengine</Filter>
</ClInclude> </ClInclude>
@@ -157,6 +160,9 @@
<ClCompile Include="..\src\scene.cpp"> <ClCompile Include="..\src\scene.cpp">
<Filter>lolengine</Filter> <Filter>lolengine</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="..\src\sdlapp.cpp">
<Filter>lolengine</Filter>
</ClCompile>
<ClCompile Include="..\src\sdlinput.cpp"> <ClCompile Include="..\src\sdlinput.cpp">
<Filter>lolengine</Filter> <Filter>lolengine</Filter>
</ClCompile> </ClCompile>


+ 2
- 0
win32/editor.vcxproj View File

@@ -36,6 +36,7 @@
<ClInclude Include="..\src\sample.h" /> <ClInclude Include="..\src\sample.h" />
<ClInclude Include="..\src\sampler.h" /> <ClInclude Include="..\src\sampler.h" />
<ClInclude Include="..\src\scene.h" /> <ClInclude Include="..\src\scene.h" />
<ClInclude Include="..\src\sdlapp.h" />
<ClInclude Include="..\src\sdlinput.h" /> <ClInclude Include="..\src\sdlinput.h" />
<ClInclude Include="..\src\shader.h" /> <ClInclude Include="..\src\shader.h" />
<ClInclude Include="..\src\text.h" /> <ClInclude Include="..\src\text.h" />
@@ -70,6 +71,7 @@
<ClCompile Include="..\src\sample.cpp" /> <ClCompile Include="..\src\sample.cpp" />
<ClCompile Include="..\src\sampler.cpp" /> <ClCompile Include="..\src\sampler.cpp" />
<ClCompile Include="..\src\scene.cpp" /> <ClCompile Include="..\src\scene.cpp" />
<ClCompile Include="..\src\sdlapp.cpp" />
<ClCompile Include="..\src\sdlinput.cpp" /> <ClCompile Include="..\src\sdlinput.cpp" />
<ClCompile Include="..\src\shader.cpp" /> <ClCompile Include="..\src\shader.cpp" />
<ClCompile Include="..\src\text.cpp" /> <ClCompile Include="..\src\text.cpp" />


+ 6
- 0
win32/editor.vcxproj.filters View File

@@ -66,6 +66,9 @@
<ClInclude Include="..\src\scene.h"> <ClInclude Include="..\src\scene.h">
<Filter>lolengine</Filter> <Filter>lolengine</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="..\src\sdlapp.h">
<Filter>lolengine</Filter>
</ClInclude>
<ClInclude Include="..\src\sdlinput.h"> <ClInclude Include="..\src\sdlinput.h">
<Filter>lolengine</Filter> <Filter>lolengine</Filter>
</ClInclude> </ClInclude>
@@ -157,6 +160,9 @@
<ClCompile Include="..\src\scene.cpp"> <ClCompile Include="..\src\scene.cpp">
<Filter>lolengine</Filter> <Filter>lolengine</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="..\src\sdlapp.cpp">
<Filter>lolengine</Filter>
</ClCompile>
<ClCompile Include="..\src\sdlinput.cpp"> <ClCompile Include="..\src\sdlinput.cpp">
<Filter>lolengine</Filter> <Filter>lolengine</Filter>
</ClCompile> </ClCompile>


+ 2
- 0
win32/monsterz.vcxproj View File

@@ -41,6 +41,7 @@
<ClInclude Include="..\src\sample.h" /> <ClInclude Include="..\src\sample.h" />
<ClInclude Include="..\src\sampler.h" /> <ClInclude Include="..\src\sampler.h" />
<ClInclude Include="..\src\scene.h" /> <ClInclude Include="..\src\scene.h" />
<ClInclude Include="..\src\sdlapp.h" />
<ClInclude Include="..\src\sdlinput.h" /> <ClInclude Include="..\src\sdlinput.h" />
<ClInclude Include="..\src\shader.h" /> <ClInclude Include="..\src\shader.h" />
<ClInclude Include="..\src\text.h" /> <ClInclude Include="..\src\text.h" />
@@ -79,6 +80,7 @@
<ClCompile Include="..\src\sample.cpp" /> <ClCompile Include="..\src\sample.cpp" />
<ClCompile Include="..\src\sampler.cpp" /> <ClCompile Include="..\src\sampler.cpp" />
<ClCompile Include="..\src\scene.cpp" /> <ClCompile Include="..\src\scene.cpp" />
<ClCompile Include="..\src\sdlapp.cpp" />
<ClCompile Include="..\src\sdlinput.cpp" /> <ClCompile Include="..\src\sdlinput.cpp" />
<ClCompile Include="..\src\shader.cpp" /> <ClCompile Include="..\src\shader.cpp" />
<ClCompile Include="..\src\text.cpp" /> <ClCompile Include="..\src\text.cpp" />


+ 6
- 0
win32/monsterz.vcxproj.filters View File

@@ -66,6 +66,9 @@
<ClInclude Include="..\src\scene.h"> <ClInclude Include="..\src\scene.h">
<Filter>lolengine</Filter> <Filter>lolengine</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="..\src\sdlapp.h">
<Filter>lolengine</Filter>
</ClInclude>
<ClInclude Include="..\src\sdlinput.h"> <ClInclude Include="..\src\sdlinput.h">
<Filter>lolengine</Filter> <Filter>lolengine</Filter>
</ClInclude> </ClInclude>
@@ -162,6 +165,9 @@
<ClCompile Include="..\src\scene.cpp"> <ClCompile Include="..\src\scene.cpp">
<Filter>lolengine</Filter> <Filter>lolengine</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="..\src\sdlapp.cpp">
<Filter>lolengine</Filter>
</ClCompile>
<ClCompile Include="..\src\sdlinput.cpp"> <ClCompile Include="..\src\sdlinput.cpp">
<Filter>lolengine</Filter> <Filter>lolengine</Filter>
</ClCompile> </ClCompile>


Loading…
Cancel
Save