source: trunk/src/video.cpp @ 81

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

Minor Win32 compilation fixes.

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