-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsdlgraphics.cpp
More file actions
102 lines (91 loc) · 1.64 KB
/
sdlgraphics.cpp
File metadata and controls
102 lines (91 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include "sdlgraphics.h"
#include "debug.h"
SDLGraphics::SDLGraphics()
{
quit = false;
started = false;
mouse_x = 0;
mouse_y = 0;
}
bool SDLGraphics::start()
{
Log << "Initialising SDL... ";
if(SDL_Init(SDL_INIT_VIDEO) != 0)
{
Log << "failed\n";
return false;
}
SDL_WM_SetCaption("MX",NULL);
Log << "ok\n";
started = true;
return true;
}
int SDLGraphics::get_mouse_x()
{
return mouse_x;
}
int SDLGraphics::get_mouse_y()
{
return mouse_y;
}
bool SDLGraphics::set_video_mode(int width, int height, int bpp, bool fullscreen, bool hw)
{
Log << "Setting video mode... ";
SDL_Surface *screen_surface = SDL_SetVideoMode(width,height,bpp,SDL_DOUBLEBUF | (hw?SDL_HWSURFACE:SDL_SWSURFACE) | (fullscreen?SDL_FULLSCREEN:0));
if(screen_surface == NULL)
{
Log << "failed: unable to set video mode " << width << "x" << height << "x" << bpp << ", " << (fullscreen?"fullscreen":"windowed") << (hw?", hardware-accelerated":"") << "\n";
return false;
}
else
{
screen = new SDLSurface(screen_surface,false);
Log << "ok\n";
return true;
}
}
SDLSurface *SDLGraphics::get_screen()
{
return screen;
}
void SDLGraphics::flip()
{
SDL_Flip(screen->get_surface());
}
void SDLGraphics::game_loop(GameLoop *game)
{
SDL_Event event;
while(!quit)
{
while(SDL_PollEvent(&event))
{
if(event.type == SDL_QUIT)
{
quit = true;
}
else if(event.type == SDL_MOUSEMOTION)
{
mouse_x = event.motion.x;
mouse_y = event.motion.y;
}
}
game->step();
flip();
}
}
void SDLGraphics::stop()
{
quit = true;
}
void SDLGraphics::close()
{
SAFE_DELETE(screen);
SDL_Quit();
}
SDLGraphics::~SDLGraphics()
{
if(started)
{
close();
}
}