Changeset 680


Ignore:
Timestamp:
Feb 21, 2011, 6:11:36 PM (8 years ago)
Author:
sam
Message:

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

Location:
trunk
Files:
15 edited
2 copied

Legend:

Unmodified
Added
Removed
  • trunk/configure.ac

    r674 r680  
    126126  AC_MSG_ERROR([[One of SDL, SDL_Image or SDL_Mixer not found]])
    127127else
    128   AC_DEFINE(USE_SDL, 1, Define to 1 to use SDL_image)
     128  AC_DEFINE(USE_SDL, 1, Define to 1 to use SDL)
    129129fi
    130130AM_CONDITIONAL(USE_SDL, test "${ac_cv_my_have_sdl}" = "yes")
  • trunk/deushax/deushax.cpp

    r622 r680  
    88#endif
    99
    10 #include <cstdio>
    11 #include <cmath>
    1210#if defined _WIN32
    1311#   include <direct.h>
    1412#endif
    1513
    16 #include <SDL.h>
     14#include "core.h"
    1715
    18 #include "core.h"
    1916#include "game.h"
     17#include "sdlapp.h"
    2018#include "sdlinput.h"
    2119#include "debugfps.h"
     
    2624
    2725#if defined _WIN32
    28 #   undef main
     26#   undef main /* FIXME: still needed? */
    2927#endif
    30 
    31 static float const FPS = 30.0f;
    3228
    3329int main(int argc, char **argv)
    3430{
    35     /* Initialise SDL */
    36     if (SDL_Init(SDL_INIT_VIDEO) < 0)
    37     {
    38         fprintf(stderr, "Cannot initialise SDL: %s\n", SDL_GetError());
    39         return EXIT_FAILURE;
    40     }
     31    SdlApp app("Map Test (SDL)", vec2i(640, 480), 30.0f);
    4132
    42     SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
    43     SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
    44     SDL_Surface *video = SDL_SetVideoMode(640, 480, 0, SDL_OPENGL);
    45     if (!video)
    46     {
    47         fprintf(stderr, "Cannot create OpenGL screen: %s\n", SDL_GetError());
    48         SDL_Quit();
    49         return EXIT_FAILURE;
    50     }
    51 
    52     SDL_WM_SetCaption("Map Test (SDL)", NULL);
    53     SDL_ShowCursor(0);
    54     //SDL_WM_GrabInput(SDL_GRAB_ON);
    55 
    56     Ticker::Setup(FPS);
    57 
    58     /* Initialise OpenGL */
    59     Video::Setup(video->w, video->h);
    60 
    61     /* Create a game */
    6233#if defined _WIN32
    6334    _chdir(".."); /* Temporary Win32 hack */
     
    7445    new DebugStats("stats.txt");
    7546
    76     while (!Ticker::Finished())
    77     {
    78         /* Tick the game */
    79         Ticker::TickGame();
    80 
    81         /* Tick the renderer, show the frame and clamp to desired framerate. */
    82         Ticker::TickDraw();
    83         SDL_GL_SwapBuffers();
    84         Ticker::ClampFps();
    85     }
    86 
    87     SDL_Quit();
     47    app.Run();
    8848
    8949    return EXIT_SUCCESS;
  • trunk/monsterz/monsterz.cpp

    r622 r680  
    1313#endif
    1414
    15 #include <cstdio>
    16 #include <cmath>
    1715#if defined _WIN32
    1816#   include <direct.h>
    1917#endif
    2018
    21 #include <SDL.h>
     19#include "core.h"
    2220
    23 #include "core.h"
     21#include "sdlapp.h"
    2422#include "sdlinput.h"
    2523#include "interface.h"
     
    2927
    3028#if defined _WIN32
    31 #   undef main
     29#   undef main /* FIXME: still needed? */
    3230#endif
    33 
    34 static float const FPS = 60.0f;
    3531
    3632int main(int argc, char **argv)
    3733{
    38     /* Initialise SDL */
    39     if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0)
    40     {
    41         fprintf(stderr, "Cannot initialise SDL: %s\n", SDL_GetError());
    42         return EXIT_FAILURE;
    43     }
     34    SdlApp app("Monsterz", vec2i(640, 480), 60.0f);
    4435
    45     SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
    46     SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
    47     SDL_Surface *video = SDL_SetVideoMode(640, 480, 0, SDL_OPENGL);
    48     if (!video)
    49     {
    50         fprintf(stderr, "Cannot create OpenGL screen: %s\n", SDL_GetError());
    51         SDL_Quit();
    52         return EXIT_FAILURE;
    53     }
    54 
    55     SDL_WM_SetCaption("Monsterz", NULL);
    56     SDL_ShowCursor(0);
    57 
    58     /* Initialise everything */
    59     Ticker::Setup(FPS);
    60     Video::Setup(video->w, video->h);
    61     Audio::Setup(2);
    62 
    63     /* Create a game */
    6436#if defined _WIN32
    6537    _chdir(".."); /* Temporary Win32 hack */
     
    7244    //new DebugRecord("monsterz.ogm", FPS);
    7345
    74     while (!Ticker::Finished())
    75     {
    76         /* Tick the game */
    77         Ticker::TickGame();
    78 
    79         /* Tick the renderer, show the frame and clamp to desired framerate. */
    80         Ticker::TickDraw();
    81         SDL_GL_SwapBuffers();
    82         Ticker::ClampFps();
    83     }
    84 
    85     SDL_Quit();
     46    app.Run();
    8647
    8748    return EXIT_SUCCESS;
  • trunk/src/Makefile.am

    r673 r680  
    1212    worldentity.cpp worldentity.h shader.cpp shader.h \
    1313    \
    14     sdlinput.cpp sdlinput.h \
     14    sdlapp.cpp sdlapp.h sdlinput.cpp sdlinput.h \
    1515    \
    1616    debugfps.cpp debugfps.h debugsphere.cpp debugsphere.h \
  • trunk/src/audio.cpp

    r254 r680  
    1515#include <cmath>
    1616
    17 #include <SDL_mixer.h>
     17#if defined USE_SDL
     18#   include <SDL.h>
     19#   include <SDL_mixer.h>
     20#endif
    1821
    1922#include "core.h"
     
    2528void Audio::Setup(int channels)
    2629{
     30#if defined USE_SDL
    2731    Mix_OpenAudio(22050, AUDIO_S16, channels, 1024);
     32#endif
    2833}
    2934
  • trunk/src/input.cpp

    r664 r680  
    1313#endif
    1414
    15 #include <SDL.h>
    16 
    1715#include <cstdio>
    1816#include <cstdlib>
    1917#include <cmath>
     18
     19#if defined USE_SDL
     20#   include <SDL.h>
     21#endif
    2022
    2123#include "core.h"
     
    5759{
    5860    float invsqrt2 = sqrtf(0.5f);
    59     vec2 f;
     61    vec2 ret;
    6062
     63#if defined USE_SDL
    6164    /* Simulate a joystick using the keyboard. This SDL call is free. */
    6265    Uint8 *keystate = SDL_GetKeyState(NULL);
    6366    int left = keystate[SDLK_d] - (keystate[SDLK_a] | keystate[SDLK_q]);
    6467    int up = (keystate[SDLK_w] | keystate[SDLK_z]) - keystate[SDLK_s] ;
    65     f.x += left;
    66     f.y += up;
     68    ret.x += left;
     69    ret.y += up;
    6770    if (left && up)
    68         f = f * invsqrt2;
     71        ret = ret * invsqrt2;
     72#else
     73    ret = 0;
     74#endif
    6975
    70     return f;
     76    return ret;
    7177}
    7278
  • trunk/src/lolgl.h

    r674 r680  
    1818
    1919#define GL_GLEXT_PROTOTYPES
     20
     21/* Defines for exotic platforms (until they get their config.h) */
     22#ifdef ANDROID_NDK
     23#   define HAVE_GLES_1X
     24#endif
    2025
    2126/* Only define one GL platform */
  • trunk/src/sample.cpp

    r658 r680  
    1414
    1515#include <cstdlib>
     16#include <cstdio>
    1617#include <cmath>
    1718
    18 #include <SDL.h>
    19 #include <SDL_mixer.h>
     19#if defined USE_SDL
     20#   include <SDL.h>
     21#   include <SDL_mixer.h>
     22#endif
    2023
    2124#include "core.h"
     
    3134private:
    3235    char *name, *path;
     36#if defined USE_SDL
    3337    Mix_Chunk *chunk;
     38#endif
    3439};
    3540
     
    4550    sprintf(data->name, "<sample> %s", path);
    4651
     52#if defined USE_SDL
    4753    data->chunk = Mix_LoadWAV(path);
    4854    if (!data->chunk)
     
    5460        exit(1);
    5561    }
     62#endif
    5663}
    5764
    5865Sample::~Sample()
    5966{
     67#if defined USE_SDL
    6068    Mix_FreeChunk(data->chunk);
     69#endif
    6170    free(data->name);
    6271    delete data;
     
    7584void Sample::Play()
    7685{
     86#if defined USE_SDL
    7787    Mix_PlayChannel(-1, data->chunk, 0);
     88#endif
    7889}
    7990
  • trunk/src/sdlapp.cpp

    r675 r680  
    11//
    2 // Monsterz
     2// Lol Engine
    33//
    4 // Copyright: (c) 2005-2011 Sam Hocevar <sam@hocevar.net>
     4// Copyright: (c) 2010-2011 Sam Hocevar <sam@hocevar.net>
    55//   This program is free software; you can redistribute it and/or
    66//   modify it under the terms of the Do What The Fuck You Want To
     
    1313#endif
    1414
    15 #include <cstdio>
    16 #include <cmath>
    17 #if defined _WIN32
    18 #   include <direct.h>
     15#if defined USE_SDL
     16#   include <SDL.h>
    1917#endif
    2018
    21 #include <SDL.h>
     19#include "core.h"
     20#include "sdlapp.h"
    2221
    23 #include "core.h"
    24 #include "sdlinput.h"
    25 #include "interface.h"
     22/*
     23 * SDL App implementation class
     24 */
    2625
    27 #include "debugfps.h"
    28 #include "debugrecord.h"
     26class SdlAppData
     27{
     28    friend class SdlApp;
    2929
    30 #if defined _WIN32
    31 #   undef main
    32 #endif
     30private:
     31    int unused;
     32};
    3333
    34 static float const FPS = 60.0f;
     34/*
     35 * Public SdlApp class
     36 */
    3537
    36 int main(int argc, char **argv)
     38SdlApp::SdlApp(char const *title, vec2i res, float fps) :
     39    data(new SdlAppData())
    3740{
     41#if defined USE_SDL
    3842    /* Initialise SDL */
    3943    if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0)
    4044    {
    4145        fprintf(stderr, "Cannot initialise SDL: %s\n", SDL_GetError());
    42         return EXIT_FAILURE;
     46        exit(EXIT_FAILURE);
    4347    }
    4448
    4549    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
    4650    SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
    47     SDL_Surface *video = SDL_SetVideoMode(640, 480, 0, SDL_OPENGL);
     51    SDL_Surface *video = SDL_SetVideoMode(res.x, res.y, 0, SDL_OPENGL);
    4852    if (!video)
    4953    {
    5054        fprintf(stderr, "Cannot create OpenGL screen: %s\n", SDL_GetError());
    5155        SDL_Quit();
    52         return EXIT_FAILURE;
     56        exit(EXIT_FAILURE);
    5357    }
    5458
    55     SDL_WM_SetCaption("Monsterz", NULL);
     59    SDL_WM_SetCaption(title, NULL);
    5660    SDL_ShowCursor(0);
    5761
    5862    /* Initialise everything */
    59     Ticker::Setup(FPS);
     63    Ticker::Setup(fps);
    6064    Video::Setup(video->w, video->h);
    6165    Audio::Setup(2);
     66#endif
     67}
    6268
    63     /* Create a game */
    64 #if defined _WIN32
    65     _chdir(".."); /* Temporary Win32 hack */
    66 #endif
    67 
    68     /* Register an input driver and some debug stuff */
    69     new SdlInput();
    70     new Interface();
    71     new DebugFps(20, 20);
    72     //new DebugRecord("monsterz.ogm", FPS);
    73 
     69void SdlApp::Run()
     70{
    7471    while (!Ticker::Finished())
    7572    {
     
    7976        /* Tick the renderer, show the frame and clamp to desired framerate. */
    8077        Ticker::TickDraw();
     78#if defined USE_SDL
    8179        SDL_GL_SwapBuffers();
     80#endif
    8281        Ticker::ClampFps();
    8382    }
    84 
    85     SDL_Quit();
    86 
    87     return EXIT_SUCCESS;
    8883}
    8984
     85SdlApp::~SdlApp()
     86{
     87#if defined USE_SDL
     88    SDL_Quit();
     89#endif
     90    free(data);
     91}
     92
  • trunk/src/sdlapp.h

    r675 r680  
    99//
    1010
    11 #if defined HAVE_CONFIG_H
    12 #   include "config.h"
    13 #endif
     11//
     12// The SdlApp class
     13// ----------------
     14//
    1415
    15 #include <cmath>
     16#if !defined __DH_SDLAPP_H__
     17#define __DH_SDLAPP_H__
    1618
    17 #include <SDL_mixer.h>
     19#include "matrix.h"
    1820
    19 #include "core.h"
     21class SdlAppData;
    2022
    21 /*
    22  * Public Audio class
    23  */
     23class SdlApp
     24{
     25public:
     26    SdlApp(char const *title, vec2i res, float fps);
     27    virtual ~SdlApp();
    2428
    25 void Audio::Setup(int channels)
    26 {
    27     Mix_OpenAudio(22050, AUDIO_S16, channels, 1024);
    28 }
     29    void Run();
    2930
     31private:
     32    SdlAppData *data;
     33};
     34
     35#endif // __DH_SDLAPP_H__
     36
  • trunk/src/tileset.cpp

    r673 r680  
    1414
    1515#include <cstdlib>
     16#include <cstdio>
    1617#include <cmath>
    1718
     
    2122#endif
    2223
    23 #include <SDL.h>
    24 #include <SDL_image.h>
     24#if defined USE_SDL
     25#   include <SDL.h>
     26#   include <SDL_image.h>
     27#endif
    2528
    2629#include "core.h"
     
    4144    float dilate, tx, ty;
    4245
     46#if defined USE_SDL
    4347    SDL_Surface *img;
     48#endif
    4449    GLuint texture;
    4550};
     
    5762
    5863    data->tiles = NULL;
     64#if defined USE_SDL
    5965    data->img = NULL;
     66#endif
    6067    data->texture = 0;
    6168
     69#if defined USE_SDL
    6270    for (char const *name = path; *name; name++)
    6371        if ((data->img = IMG_Load(name)))
     
    8795    }
    8896
     97    data->tx = (float)data->size.x / PotUp(data->img->w);
     98    data->ty = (float)data->size.y / PotUp(data->img->h);
     99#endif
     100
    89101    data->dilate = dilate;
    90102    data->ntiles = data->count.i * data->count.j;
    91     data->tx = (float)data->size.x / PotUp(data->img->w);
    92     data->ty = (float)data->size.y / PotUp(data->img->h);
    93103
    94104    drawgroup = DRAWGROUP_BEFORE;
     
    106116    Entity::TickDraw(deltams);
    107117
     118#if defined USE_SDL
    108119    if (IsDestroying())
    109120    {
     
    146157        data->img = NULL;
    147158    }
     159#endif
    148160}
    149161
     
    165177void TileSet::Bind()
    166178{
     179#if defined USE_SDL
    167180    if (!data->img)
    168181        glBindTexture(GL_TEXTURE_2D, data->texture);
     182#endif
    169183}
    170184
     
    180194    int dz = o ? data->size.y : 0;
    181195
     196#if defined USE_SDL
    182197    if (!data->img)
    183198    {
     
    221236    }
    222237    else
     238#endif
    223239    {
    224240        memset(vertex, 0, 3 * sizeof(float));
  • trunk/win32/deushax.vcxproj

    r673 r680  
    3737    <ClInclude Include="..\src\sampler.h" />
    3838    <ClInclude Include="..\src\scene.h" />
     39    <ClInclude Include="..\src\sdlapp.h" />
    3940    <ClInclude Include="..\src\sdlinput.h" />
    4041    <ClInclude Include="..\src\shader.h" />
     
    7172    <ClCompile Include="..\src\sampler.cpp" />
    7273    <ClCompile Include="..\src\scene.cpp" />
     74    <ClCompile Include="..\src\sdlapp.cpp" />
    7375    <ClCompile Include="..\src\sdlinput.cpp" />
    7476    <ClCompile Include="..\src\shader.cpp" />
  • trunk/win32/deushax.vcxproj.filters

    r673 r680  
    6767      <Filter>lolengine</Filter>
    6868    </ClInclude>
     69    <ClInclude Include="..\src\sdlapp.h">
     70      <Filter>lolengine</Filter>
     71    </ClInclude>
    6972    <ClInclude Include="..\src\sdlinput.h">
    7073      <Filter>lolengine</Filter>
     
    156159    </ClCompile>
    157160    <ClCompile Include="..\src\scene.cpp">
     161      <Filter>lolengine</Filter>
     162    </ClCompile>
     163    <ClCompile Include="..\src\sdlapp.cpp">
    158164      <Filter>lolengine</Filter>
    159165    </ClCompile>
  • trunk/win32/editor.vcxproj

    r673 r680  
    3737    <ClInclude Include="..\src\sampler.h" />
    3838    <ClInclude Include="..\src\scene.h" />
     39    <ClInclude Include="..\src\sdlapp.h" />
    3940    <ClInclude Include="..\src\sdlinput.h" />
    4041    <ClInclude Include="..\src\shader.h" />
     
    7172    <ClCompile Include="..\src\sampler.cpp" />
    7273    <ClCompile Include="..\src\scene.cpp" />
     74    <ClCompile Include="..\src\sdlapp.cpp" />
    7375    <ClCompile Include="..\src\sdlinput.cpp" />
    7476    <ClCompile Include="..\src\shader.cpp" />
  • trunk/win32/editor.vcxproj.filters

    r673 r680  
    6767      <Filter>lolengine</Filter>
    6868    </ClInclude>
     69    <ClInclude Include="..\src\sdlapp.h">
     70      <Filter>lolengine</Filter>
     71    </ClInclude>
    6972    <ClInclude Include="..\src\sdlinput.h">
    7073      <Filter>lolengine</Filter>
     
    158161      <Filter>lolengine</Filter>
    159162    </ClCompile>
     163    <ClCompile Include="..\src\sdlapp.cpp">
     164      <Filter>lolengine</Filter>
     165    </ClCompile>
    160166    <ClCompile Include="..\src\sdlinput.cpp">
    161167      <Filter>lolengine</Filter>
  • trunk/win32/monsterz.vcxproj

    r673 r680  
    4242    <ClInclude Include="..\src\sampler.h" />
    4343    <ClInclude Include="..\src\scene.h" />
     44    <ClInclude Include="..\src\sdlapp.h" />
    4445    <ClInclude Include="..\src\sdlinput.h" />
    4546    <ClInclude Include="..\src\shader.h" />
     
    8081    <ClCompile Include="..\src\sampler.cpp" />
    8182    <ClCompile Include="..\src\scene.cpp" />
     83    <ClCompile Include="..\src\sdlapp.cpp" />
    8284    <ClCompile Include="..\src\sdlinput.cpp" />
    8385    <ClCompile Include="..\src\shader.cpp" />
  • trunk/win32/monsterz.vcxproj.filters

    r673 r680  
    6767      <Filter>lolengine</Filter>
    6868    </ClInclude>
     69    <ClInclude Include="..\src\sdlapp.h">
     70      <Filter>lolengine</Filter>
     71    </ClInclude>
    6972    <ClInclude Include="..\src\sdlinput.h">
    7073      <Filter>lolengine</Filter>
     
    161164    </ClCompile>
    162165    <ClCompile Include="..\src\scene.cpp">
     166      <Filter>lolengine</Filter>
     167    </ClCompile>
     168    <ClCompile Include="..\src\sdlapp.cpp">
    163169      <Filter>lolengine</Filter>
    164170    </ClCompile>
Note: See TracChangeset for help on using the changeset viewer.