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

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

gpu: get rid of the global D3D device pointer and move D3D initialisation
code from the Video to the Renderer class.

  • 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 = nullptr;
37#endif
38
39namespace lol
40{
41
42/*
43 * SDL App implementation class
44 */
45
46class SdlAppData
47{
48    friend class SdlApp;
49
50private:
51    int unused;
52};
53
54/*
55 * Public SdlApp class
56 */
57
58SdlApp::SdlApp(char const *title, ivec2 res, float fps) :
59    data(new SdlAppData())
60{
61#if defined USE_SDL
62    /* Initialise SDL */
63    if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0)
64    {
65        Log::Error("cannot initialise SDL: %s\n", SDL_GetError());
66        exit(EXIT_FAILURE);
67    }
68
69#   if defined USE_D3D9
70    SDL_Surface *video = SDL_SetVideoMode(res.x, res.y, 16, 0);
71    SDL_SysWMinfo wminfo;
72    SDL_VERSION(&wminfo.version);
73    SDL_GetWMInfo(&wminfo);
74    g_hwnd = wminfo.window;
75#   elif SDL_VERSION_ATLEAST(2,0,0)
76    TODO
77#   else
78    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
79    SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
80    SDL_Surface *video = SDL_SetVideoMode(res.x, res.y, 0, SDL_OPENGL);
81#   endif
82    if (!video)
83    {
84        Log::Error("cannot create rendering window: %s\n", SDL_GetError());
85        SDL_Quit();
86        exit(EXIT_FAILURE);
87    }
88
89    SDL_WM_SetCaption(title, nullptr);
90
91    /* Initialise everything */
92    Ticker::Setup(fps);
93    Video::Setup(ivec2(video->w, video->h));
94    Audio::Setup(2);
95
96    /* Autoreleased objects */
97#   if defined USE_XINPUT
98    /* Prefer D3d9 for joysticks on Windows, because the X360 pads are not
99     * advertised with the proper number of axes. */
100    new D3d9Input();
101#   endif
102
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    IDirect3DDevice9 *d3d_dev = (IDirect3DDevice9 *)g_renderer->GetDevice();
118    HRESULT hr;
119    hr = d3d_dev->BeginScene();
120    if (FAILED(hr))
121        Abort();
122#endif
123
124    /* Tick the renderer, show the frame and clamp to desired framerate. */
125    Ticker::TickDraw();
126
127#if defined USE_SDL && defined USE_D3D9
128    hr = d3d_dev->EndScene();
129    if (FAILED(hr))
130        Abort();
131    hr = d3d_dev->Present(nullptr, nullptr, nullptr, nullptr);
132    if (FAILED(hr))
133        Abort();
134#elif defined USE_SDL
135    SDL_GL_SwapBuffers();
136#endif
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.