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

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

win32: add an Xinput class for Xbox360 controllers, because they're not
properly detected using the generic SDL driver.

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