source: trunk/src/video.cpp @ 21

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

Add a sprite following the mouse.

  • 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#   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    SDL_ShowCursor(0);
57    SDL_WM_GrabInput(SDL_GRAB_ON);
58
59    /* Initialise OpenGL */
60    glViewport(0, 0, data->video->w, data->video->h);
61    glMatrixMode(GL_PROJECTION);
62    glLoadIdentity();
63    glOrtho(0, data->video->w, data->video->h, 0, -1, 10);
64    glMatrixMode(GL_MODELVIEW);
65    glLoadIdentity();
66
67    glEnable(GL_TEXTURE_2D);
68    glShadeModel(GL_SMOOTH);
69    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
70    glClearDepth(1.0);
71    glEnable(GL_DEPTH_TEST);
72    glDepthFunc(GL_LEQUAL);
73    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
74
75    glEnable(GL_BLEND);
76    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
77
78    /* Initialise timer */
79    data->start = data->ticks = SDL_GetTicks();
80    data->frames = 0;
81}
82
83int Video::GetWidth() const
84{
85    return data->video->w;
86}
87
88int Video::GetHeight() const
89{
90    return data->video->h;
91}
92
93void Video::Refresh(float milliseconds)
94{
95    if (milliseconds > 0.0f)
96        while (SDL_GetTicks() < data->ticks + (milliseconds - 0.5f))
97            SDL_Delay(1);
98    data->ticks = SDL_GetTicks();
99    data->frames++;
100
101    SDL_GL_SwapBuffers();
102}
103
104void Video::FullScreen()
105{
106    SDL_WM_ToggleFullScreen(data->video);
107}
108
109Video::~Video()
110{
111    Uint32 total = SDL_GetTicks() - data->start;
112    printf("%f fps\n", 1000.0f * data->frames / total);
113    SDL_Quit();
114    delete data;
115}
116
Note: See TracBrowser for help on using the repository browser.