source: trunk/src/platform/sdl/sdlapp.cpp @ 2222

Last change on this file since 2222 was 2222, checked in by sam, 10 years ago

core: you can now while(app.MustTick()) { Tick(); } instead of app.Run().

  • Property svn:keywords set to Id
File size: 2.9 KB
Line 
1//
2// Lol Engine
3//
4// Copyright: (c) 2010-2011 Sam Hocevar <sam@hocevar.net>
5//   This program is free software; you can redistribute it and/or
6//   modify it under the terms of the Do What The Fuck You Want To
7//   Public License, Version 2, as published by Sam Hocevar. See
8//   http://www.wtfpl.net/ for more details.
9//
10
11#if defined HAVE_CONFIG_H
12#   include "config.h"
13#endif
14
15#if defined USE_SDL
16#   if defined HAVE_SDL_SDL_H
17#      include <SDL/SDL.h>
18#   else
19#      include <SDL.h>
20#   endif
21#   if defined USE_D3D9
22#       include <d3d9.h>
23#       include <SDL_syswm.h>
24#   endif
25#endif
26
27#include "core.h"
28#include "lolgl.h"
29#include "platform/sdl/sdlapp.h"
30#include "platform/sdl/sdlinput.h"
31#if defined USE_XINPUT
32#   include "platform/d3d9/d3d9input.h"
33#endif
34
35#if defined USE_SDL && defined USE_D3D9
36HWND g_hwnd = NULL;
37extern IDirect3DDevice9 *g_d3ddevice;
38#endif
39
40namespace lol
41{
42
43/*
44 * SDL App implementation class
45 */
46
47class SdlAppData
48{
49    friend class SdlApp;
50
51private:
52    int unused;
53};
54
55/*
56 * Public SdlApp class
57 */
58
59SdlApp::SdlApp(char const *title, ivec2 res, float fps) :
60    data(new SdlAppData())
61{
62#if defined USE_SDL
63    /* Initialise SDL */
64    if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0)
65    {
66        Log::Error("cannot initialise SDL: %s\n", SDL_GetError());
67        exit(EXIT_FAILURE);
68    }
69
70#   if defined USE_D3D9
71    SDL_Surface *video = SDL_SetVideoMode(res.x, res.y, 16, 0);
72    SDL_SysWMinfo wminfo;
73    SDL_VERSION(&wminfo.version);
74    SDL_GetWMInfo(&wminfo);
75    g_hwnd = wminfo.window;
76#   else
77    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
78    SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
79    SDL_Surface *video = SDL_SetVideoMode(res.x, res.y, 0, SDL_OPENGL);
80#   endif
81    if (!video)
82    {
83        Log::Error("cannot create rendering window: %s\n", SDL_GetError());
84        SDL_Quit();
85        exit(EXIT_FAILURE);
86    }
87
88    SDL_WM_SetCaption(title, NULL);
89
90    /* Initialise everything */
91    Ticker::Setup(fps);
92    Video::Setup(ivec2(video->w, video->h));
93    Audio::Setup(2);
94
95    /* Autoreleased objects */
96#   if defined USE_XINPUT
97    /* Prefer D3d9 for joysticks on Windows, because the X360 pads are not
98     * advertised with the proper number of axes. */
99    new D3d9Input();
100#   endif
101    new SdlInput();
102#endif
103}
104
105void SdlApp::ShowPointer(bool show)
106{
107#if defined USE_SDL
108    SDL_ShowCursor(show ? 1 : 0);
109#endif
110}
111
112void SdlApp::Tick()
113{
114#if defined USE_SDL && defined USE_D3D9
115    HRESULT hr;
116    hr = g_d3ddevice->BeginScene();
117    if (FAILED(hr))
118        Abort();
119#endif
120    /* Tick the renderer, show the frame and clamp to desired framerate. */
121    Ticker::TickDraw();
122#if defined USE_SDL
123#   if defined USE_D3D9
124    hr = g_d3ddevice->EndScene();
125    if (FAILED(hr))
126        Abort();
127    hr = g_d3ddevice->Present(NULL, NULL, NULL, NULL);
128    if (FAILED(hr))
129        Abort();
130#   else
131    SDL_GL_SwapBuffers();
132#   endif
133#endif
134}
135
136SdlApp::~SdlApp()
137{
138#if defined USE_SDL
139    SDL_Quit();
140#endif
141    delete data;
142}
143
144} /* namespace lol */
145
Note: See TracBrowser for help on using the repository browser.