source: trunk/src/video.cpp @ 18

Last change on this file since 18 was 18, checked in by sam, 12 years ago

Move video initialisation stuff to a separate source file.

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