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

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

build: fix the WTFPL site URL in all code comments.

  • 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#   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::Run()
113{
114    while (!Ticker::Finished())
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}
138
139SdlApp::~SdlApp()
140{
141#if defined USE_SDL
142    SDL_Quit();
143#endif
144    delete data;
145}
146
147} /* namespace lol */
148
Note: See TracBrowser for help on using the repository browser.