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

Last change on this file since 2300 was 2300, checked in by sam, 8 years ago

build: add SDL detection code in new file lol-sdl.m4. The SDL2 version is
disabled until the engine code itself is ported to SDL2.

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