source: trunk/src/sdlvideo.cpp @ 105

Last change on this file since 105 was 105, checked in by sam, 11 years ago

Create a helper class for easy scene setup. Highly reduces the number
of includes in a few source files.

  • Property svn:keywords set to Id
File size: 1.8 KB
Line 
1//
2// Deus Hax (working title)
3// Copyright (c) 2010 Sam Hocevar <sam@hocevar.net>
4//
5
6#if defined HAVE_CONFIG_H
7#   include "config.h"
8#endif
9
10#include <SDL.h>
11
12#include "sdlvideo.h"
13#include "video.h"
14
15/*
16 * SDL Video implementation class
17 */
18
19class SdlVideoData
20{
21    friend class SdlVideo;
22
23private:
24    SDL_Surface *video;
25    Uint32 start, ticks;
26    int frames;
27};
28
29/*
30 * Public SdlVideo class
31 */
32
33SdlVideo::SdlVideo(char const *title, int width, int height)
34{
35    data = new SdlVideoData();
36
37    /* Initialise SDL */
38    if (SDL_Init(SDL_INIT_VIDEO) < 0)
39    {
40        fprintf(stderr, "Cannot initialise SDL: %s\n", SDL_GetError());
41        exit(EXIT_FAILURE);
42    }
43
44    data->video = SDL_SetVideoMode(width, height, 0, SDL_OPENGL);
45    if (!data->video)
46    {
47        fprintf(stderr, "Cannot create OpenGL screen: %s\n", SDL_GetError());
48        SDL_Quit();
49        exit(EXIT_FAILURE);
50    }
51
52    SDL_WM_SetCaption(title, NULL);
53    SDL_ShowCursor(0);
54    SDL_WM_GrabInput(SDL_GRAB_ON);
55
56    Video::Setup(data->video->w, data->video->h);
57
58    /* Initialise timer */
59    data->start = data->ticks = SDL_GetTicks();
60    data->frames = 0;
61}
62
63int SdlVideo::GetWidth() const
64{
65    return data->video->w;
66}
67
68int SdlVideo::GetHeight() const
69{
70    return data->video->h;
71}
72
73void SdlVideo::PreRender()
74{
75    Video::Clear();
76}
77
78void SdlVideo::PostRender(float milliseconds)
79{
80    if (milliseconds > 0.0f)
81        while (SDL_GetTicks() < data->ticks + (milliseconds - 0.5f))
82            SDL_Delay(1);
83    data->ticks = SDL_GetTicks();
84    data->frames++;
85
86    SDL_GL_SwapBuffers();
87}
88
89void SdlVideo::FullScreen()
90{
91    SDL_WM_ToggleFullScreen(data->video);
92}
93
94SdlVideo::~SdlVideo()
95{
96    Uint32 total = SDL_GetTicks() - data->start;
97    printf("%f fps\n", 1000.0f * data->frames / total);
98    SDL_Quit();
99    delete data;
100}
101
Note: See TracBrowser for help on using the repository browser.