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

Last change on this file since 2301 was 2301, checked in by sam, 7 years ago

build: fix a few macro logic issues in SDL.h inclusion.

  • 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_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#   elif SDL_VERSION_ATLEAST(2,0,0)
77    TODO
78#   else
79    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
80    SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
81    SDL_Surface *video = SDL_SetVideoMode(res.x, res.y, 0, SDL_OPENGL);
82#   endif
83    if (!video)
84    {
85        Log::Error("cannot create rendering window: %s\n", SDL_GetError());
86        SDL_Quit();
87        exit(EXIT_FAILURE);
88    }
89
90    SDL_WM_SetCaption(title, NULL);
91
92    /* Initialise everything */
93    Ticker::Setup(fps);
94    Video::Setup(ivec2(video->w, video->h));
95    Audio::Setup(2);
96
97    /* Autoreleased objects */
98#   if defined USE_XINPUT
99    /* Prefer D3d9 for joysticks on Windows, because the X360 pads are not
100     * advertised with the proper number of axes. */
101    new D3d9Input();
102#   endif
103    new SdlInput();
104#endif
105}
106
107void SdlApp::ShowPointer(bool show)
108{
109#if defined USE_SDL
110    SDL_ShowCursor(show ? 1 : 0);
111#endif
112}
113
114void SdlApp::Tick()
115{
116#if defined USE_SDL && defined USE_D3D9
117    HRESULT hr;
118    hr = g_d3ddevice->BeginScene();
119    if (FAILED(hr))
120        Abort();
121#endif
122    /* Tick the renderer, show the frame and clamp to desired framerate. */
123    Ticker::TickDraw();
124#if defined USE_SDL
125#   if defined USE_D3D9
126    hr = g_d3ddevice->EndScene();
127    if (FAILED(hr))
128        Abort();
129    hr = g_d3ddevice->Present(NULL, NULL, NULL, NULL);
130    if (FAILED(hr))
131        Abort();
132#   else
133    SDL_GL_SwapBuffers();
134#   endif
135#endif
136}
137
138SdlApp::~SdlApp()
139{
140#if defined USE_SDL
141    SDL_Quit();
142#endif
143    delete data;
144}
145
146} /* namespace lol */
147
Note: See TracBrowser for help on using the repository browser.