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

Last change on this file since 2810 was 2810, checked in by benlitz, 6 years ago

Completely reworked input system. Unified device interface, button, axis and cursor support, controller and binding system, mouse capture. Tutorial 07_input provided. Require to compile lolcore with LOL_INPUT_V2 to be activated (also needed app-side before including core.h)

  • Property svn:keywords set to Id
File size: 3.5 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#   ifdef LOL_INPUT_V2
70        const SDL_VideoInfo* vidinfo = SDL_GetVideoInfo();
71        int screen_w = vidinfo->current_w;
72        int screen_h = vidinfo->current_h;
73#   endif
74
75#   if defined USE_D3D9
76    SDL_Surface *video = SDL_SetVideoMode(res.x, res.y, 16, 0);
77    SDL_SysWMinfo wminfo;
78    SDL_VERSION(&wminfo.version);
79    SDL_GetWMInfo(&wminfo);
80    g_hwnd = wminfo.window;
81#   elif SDL_VERSION_ATLEAST(2,0,0)
82    TODO
83#   else
84    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
85    SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
86#   ifdef LOL_INPUT_V2
87        // TODO: when implementing fullscreen, be sure to overwrite screen_w and screen_h with the value of vidinfo after the call to SDL_SetVideoMode
88#   endif
89    SDL_Surface *video = SDL_SetVideoMode(res.x, res.y, 0, SDL_OPENGL);
90#   endif
91    if (!video)
92    {
93        Log::Error("cannot create rendering window: %s\n", SDL_GetError());
94        SDL_Quit();
95        exit(EXIT_FAILURE);
96    }
97
98    SDL_WM_SetCaption(title, nullptr);
99
100    /* Initialise everything */
101    Ticker::Setup(fps);
102    Video::Setup(ivec2(video->w, video->h));
103    Audio::Setup(2);
104
105    /* Autoreleased objects */
106#   if defined USE_XINPUT
107    /* Prefer D3d9 for joysticks on Windows, because the X360 pads are not
108     * advertised with the proper number of axes. */
109    new D3d9Input();
110#   endif
111
112#   ifdef LOL_INPUT_V2
113    new SdlInput(video->w, video->h, screen_w, screen_h);
114#   else
115        new SdlInput();
116#   endif
117#endif
118}
119
120void SdlApp::ShowPointer(bool show)
121{
122#if defined USE_SDL
123    SDL_ShowCursor(show ? 1 : 0);
124#endif
125}
126
127void SdlApp::Tick()
128{
129#if defined USE_SDL && defined USE_D3D9
130    IDirect3DDevice9 *d3d_dev = (IDirect3DDevice9 *)g_renderer->GetDevice();
131    HRESULT hr;
132    hr = d3d_dev->BeginScene();
133    if (FAILED(hr))
134        Abort();
135#endif
136
137    /* Tick the renderer, show the frame and clamp to desired framerate. */
138    Ticker::TickDraw();
139
140#if defined USE_SDL && defined USE_D3D9
141    hr = d3d_dev->EndScene();
142    if (FAILED(hr))
143        Abort();
144    hr = d3d_dev->Present(nullptr, nullptr, nullptr, nullptr);
145    if (FAILED(hr))
146        Abort();
147#elif defined USE_SDL
148    SDL_GL_SwapBuffers();
149#endif
150}
151
152SdlApp::~SdlApp()
153{
154#if defined USE_SDL
155    SDL_Quit();
156#endif
157    delete data;
158}
159
160} /* namespace lol */
161
Note: See TracBrowser for help on using the repository browser.