source: trunk/src/sdlvideo.cpp @ 100

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

Slightly improve the documentation in a few files, add the missing
joystick class, and put a header in each source file.

  • Property svn:keywords set to Id
File size: 2.5 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#ifdef WIN32
11#   define WIN32_LEAN_AND_MEAN
12#   include <windows.h>
13#endif
14#if defined __APPLE__ && defined __MACH__
15#   include <OpenGL/gl.h>
16#else
17#   define GL_GLEXT_PROTOTYPES
18#   include <GL/gl.h>
19#endif
20
21#include <SDL.h>
22
23#include "sdlvideo.h"
24
25/*
26 * SDL Video implementation class
27 */
28
29class SdlVideoData
30{
31    friend class SdlVideo;
32
33private:
34    SDL_Surface *video;
35    Uint32 start, ticks;
36    int frames;
37};
38
39/*
40 * Public SdlVideo class
41 */
42
43SdlVideo::SdlVideo(char const *title, int width, int height)
44{
45    data = new SdlVideoData();
46
47    /* Initialise SDL */
48    if (SDL_Init(SDL_INIT_VIDEO) < 0)
49    {
50        fprintf(stderr, "Cannot initialise SDL: %s\n", SDL_GetError());
51        exit(EXIT_FAILURE);
52    }
53
54    data->video = SDL_SetVideoMode(width, height, 0, SDL_OPENGL);
55    if (!data->video)
56    {
57        fprintf(stderr, "Cannot create OpenGL screen: %s\n", SDL_GetError());
58        SDL_Quit();
59        exit(EXIT_FAILURE);
60    }
61
62    SDL_WM_SetCaption(title, NULL);
63    SDL_ShowCursor(0);
64    SDL_WM_GrabInput(SDL_GRAB_ON);
65
66    /* Initialise OpenGL */
67    glViewport(0, 0, data->video->w, data->video->h);
68    glMatrixMode(GL_PROJECTION);
69    glLoadIdentity();
70    glOrtho(0, data->video->w, data->video->h, 0, -1, 10);
71    glMatrixMode(GL_MODELVIEW);
72    glLoadIdentity();
73
74    glEnable(GL_TEXTURE_2D);
75    glShadeModel(GL_SMOOTH);
76    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
77    glClearDepth(1.0);
78    glEnable(GL_DEPTH_TEST);
79    glDepthFunc(GL_LEQUAL);
80    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
81
82    glEnable(GL_BLEND);
83    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
84
85    /* Initialise timer */
86    data->start = data->ticks = SDL_GetTicks();
87    data->frames = 0;
88}
89
90int SdlVideo::GetWidth() const
91{
92    return data->video->w;
93}
94
95int SdlVideo::GetHeight() const
96{
97    return data->video->h;
98}
99
100void SdlVideo::PreRender()
101{
102    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
103    glLoadIdentity();
104}
105
106void SdlVideo::PostRender(float milliseconds)
107{
108    if (milliseconds > 0.0f)
109        while (SDL_GetTicks() < data->ticks + (milliseconds - 0.5f))
110            SDL_Delay(1);
111    data->ticks = SDL_GetTicks();
112    data->frames++;
113
114    SDL_GL_SwapBuffers();
115}
116
117void SdlVideo::FullScreen()
118{
119    SDL_WM_ToggleFullScreen(data->video);
120}
121
122SdlVideo::~SdlVideo()
123{
124    Uint32 total = SDL_GetTicks() - data->start;
125    printf("%f fps\n", 1000.0f * data->frames / total);
126    SDL_Quit();
127    delete data;
128}
129
Note: See TracBrowser for help on using the repository browser.