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

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

gpu: fix a great lot of Direct3D problems, spotted using PIX.

  • Property svn:keywords set to Id
File size: 2.7 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
28#if defined USE_SDL && defined USE_D3D9
29HWND g_hwnd = NULL;
30extern IDirect3DDevice9 *g_d3ddevice;
31#endif
32
33namespace lol
34{
35
36/*
37 * SDL App implementation class
38 */
39
40class SdlAppData
41{
42    friend class SdlApp;
43
44private:
45    int unused;
46};
47
48/*
49 * Public SdlApp class
50 */
51
52SdlApp::SdlApp(char const *title, ivec2 res, float fps) :
53    data(new SdlAppData())
54{
55#if defined USE_SDL
56    /* Initialise SDL */
57    if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0)
58    {
59        Log::Error("cannot initialise SDL: %s\n", SDL_GetError());
60        exit(EXIT_FAILURE);
61    }
62
63#   if defined USE_D3D9
64    SDL_Surface *video = SDL_SetVideoMode(res.x, res.y, 16, 0);
65    SDL_SysWMinfo wminfo;
66    SDL_VERSION(&wminfo.version);
67    SDL_GetWMInfo(&wminfo);
68    g_hwnd = wminfo.window;
69#   else
70    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
71    SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
72    SDL_Surface *video = SDL_SetVideoMode(res.x, res.y, 0, SDL_OPENGL);
73#   endif
74    if (!video)
75    {
76        Log::Error("cannot create rendering window: %s\n", SDL_GetError());
77        SDL_Quit();
78        exit(EXIT_FAILURE);
79    }
80
81    SDL_WM_SetCaption(title, NULL);
82
83    /* Initialise everything */
84    Ticker::Setup(fps);
85    Video::Setup(ivec2(video->w, video->h));
86    Audio::Setup(2);
87
88    /* Autoreleased objects */
89    new SdlInput();
90#endif
91}
92
93void SdlApp::ShowPointer(bool show)
94{
95#if defined USE_SDL
96    SDL_ShowCursor(show ? 1 : 0);
97#endif
98}
99
100void SdlApp::Run()
101{
102    while (!Ticker::Finished())
103    {
104#if defined USE_SDL && defined USE_D3D9
105        HRESULT hr;
106        hr = g_d3ddevice->BeginScene();
107        if (FAILED(hr))
108            Abort();
109#endif
110        /* Tick the renderer, show the frame and clamp to desired framerate. */
111        Ticker::TickDraw();
112#if defined USE_SDL
113#   if defined USE_D3D9
114        hr = g_d3ddevice->EndScene();
115        if (FAILED(hr))
116            Abort();
117        hr = g_d3ddevice->Present(NULL, NULL, NULL, NULL);
118        if (FAILED(hr))
119            Abort();
120#   else
121        SDL_GL_SwapBuffers();
122#   endif
123#endif
124    }
125}
126
127SdlApp::~SdlApp()
128{
129#if defined USE_SDL
130    SDL_Quit();
131#endif
132    delete data;
133}
134
135} /* namespace lol */
136
Note: See TracBrowser for help on using the repository browser.