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

Last change on this file since 2273 was 2273, checked in by sam, 9 years ago

build: use our own main() wrapper in addition to SDL's, and only
in that case. Currently only works with GCC.

  • Property svn:keywords set to Id
File size: 3.9 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/* This instructs our headers to let SDL override the "main"
16 * symbol using its macros. */
17#define LOL_DONT_DIVERT_MAIN 1
18
19#if defined USE_SDL
20#   if defined HAVE_SDL_SDL_H
21#      include <SDL/SDL.h>
22#   else
23#      include <SDL.h>
24#   endif
25#   if defined USE_D3D9
26#       include <d3d9.h>
27#       include <SDL_syswm.h>
28#   endif
29#endif
30
31#include "core.h"
32#include "lolgl.h"
33#include "platform/sdl/sdlapp.h"
34#include "platform/sdl/sdlinput.h"
35#if defined USE_XINPUT
36#   include "platform/d3d9/d3d9input.h"
37#endif
38
39#if defined USE_SDL && defined USE_D3D9
40HWND g_hwnd = NULL;
41extern IDirect3DDevice9 *g_d3ddevice;
42#endif
43
44#if defined main
45#   if defined _MSC_VER
46#       pragma comment(linker, "/alternatename:_lol_sdl_main=_lol_sdl_main_msvc")
47#       define WRAPPER lol_sdl_main_msvc
48#   else
49int lol_sdl_main() __attribute__((weak));
50int lol_sdl_main(int argc, char **argv) __attribute__((weak));
51int lol_sdl_main(int argc, char **argv, char **envp) __attribute__((weak));
52#       define WRAPPER lol_sdl_main
53#   endif
54
55/* One of these wrappers will be overridden by the user's version */
56int WRAPPER() { return 0; }
57int WRAPPER(int argc, char **argv) { return 0; }
58int WRAPPER(int argc, char **argv, char **envp) { return 0; }
59
60int main(int argc, char *argv[])
61{
62printf("LOL OK\n");
63    int ret = 0;
64    ret += lol_sdl_main();
65    ret += lol_sdl_main(argc, argv);
66    ret += lol_sdl_main(argc, argv, NULL);
67    return ret;
68}
69#endif
70
71namespace lol
72{
73
74/*
75 * SDL App implementation class
76 */
77
78class SdlAppData
79{
80    friend class SdlApp;
81
82private:
83    int unused;
84};
85
86/*
87 * Public SdlApp class
88 */
89
90SdlApp::SdlApp(char const *title, ivec2 res, float fps) :
91    data(new SdlAppData())
92{
93#if defined USE_SDL
94    /* Initialise SDL */
95    if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0)
96    {
97        Log::Error("cannot initialise SDL: %s\n", SDL_GetError());
98        exit(EXIT_FAILURE);
99    }
100
101#   if defined USE_D3D9
102    SDL_Surface *video = SDL_SetVideoMode(res.x, res.y, 16, 0);
103    SDL_SysWMinfo wminfo;
104    SDL_VERSION(&wminfo.version);
105    SDL_GetWMInfo(&wminfo);
106    g_hwnd = wminfo.window;
107#   else
108    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
109    SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
110    SDL_Surface *video = SDL_SetVideoMode(res.x, res.y, 0, SDL_OPENGL);
111#   endif
112    if (!video)
113    {
114        Log::Error("cannot create rendering window: %s\n", SDL_GetError());
115        SDL_Quit();
116        exit(EXIT_FAILURE);
117    }
118
119    SDL_WM_SetCaption(title, NULL);
120
121    /* Initialise everything */
122    Ticker::Setup(fps);
123    Video::Setup(ivec2(video->w, video->h));
124    Audio::Setup(2);
125
126    /* Autoreleased objects */
127#   if defined USE_XINPUT
128    /* Prefer D3d9 for joysticks on Windows, because the X360 pads are not
129     * advertised with the proper number of axes. */
130    new D3d9Input();
131#   endif
132    new SdlInput();
133#endif
134}
135
136void SdlApp::ShowPointer(bool show)
137{
138#if defined USE_SDL
139    SDL_ShowCursor(show ? 1 : 0);
140#endif
141}
142
143void SdlApp::Tick()
144{
145#if defined USE_SDL && defined USE_D3D9
146    HRESULT hr;
147    hr = g_d3ddevice->BeginScene();
148    if (FAILED(hr))
149        Abort();
150#endif
151    /* Tick the renderer, show the frame and clamp to desired framerate. */
152    Ticker::TickDraw();
153#if defined USE_SDL
154#   if defined USE_D3D9
155    hr = g_d3ddevice->EndScene();
156    if (FAILED(hr))
157        Abort();
158    hr = g_d3ddevice->Present(NULL, NULL, NULL, NULL);
159    if (FAILED(hr))
160        Abort();
161#   else
162    SDL_GL_SwapBuffers();
163#   endif
164#endif
165}
166
167SdlApp::~SdlApp()
168{
169#if defined USE_SDL
170    SDL_Quit();
171#endif
172    delete data;
173}
174
175} /* namespace lol */
176
Note: See TracBrowser for help on using the repository browser.