-
Notifications
You must be signed in to change notification settings - Fork 0
/
Graphics.d
executable file
·126 lines (92 loc) · 2.34 KB
/
Graphics.d
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
module Graphics;
private import derelict.sdl.sdl;
private import Misc;
private import Vector;
private SDL_Surface* _screen = null;
class GraphicsSystem
{
public:
this(bit fullscreen, bit hwcursor)
{
_fullscreen = fullscreen;
_hwcursor = hwcursor;
Uint32 flags = SDL_DOUBLEBUF;
if(_fullscreen) {
flags |= SDL_FULLSCREEN;
}
_screen = SDL_SetVideoMode(640, 480, 32, flags);
if(!_hwcursor) {
SDL_ShowCursor(SDL_DISABLE);
}
if(!_screen) {
throw new CouldNotSetVideoModeException;
}
}
SDL_Surface* screen()
{
return _screen;
}
void updateScreen()
{
SDL_Flip(_screen);
}
void paintRect(Rect rect, int r, int g, int b)
{
SDL_Rect sdlrect;
sdlrect.x = cast(short) rect.left();
sdlrect.y = cast(short) rect.top();
sdlrect.w = cast(short) rect.diagonale().x();
sdlrect.h = cast(short) rect.diagonale().y();
Uint32 color = SDL_MapRGB(_screen.format, r, g, b);
SDL_FillRect(_screen, &sdlrect, color);
}
void paintRectAlpha(Rect rect, int r, int g, int b, int a)
{
SDL_Rect sdlrect;
sdlrect.x = cast(short) rect.left();
sdlrect.y = cast(short) rect.top();
sdlrect.w = cast(short) rect.diagonale().x();
sdlrect.h = cast(short) rect.diagonale().y();
Uint32 color = SDL_MapRGBA(_screen.format, r, g, b, a);
SDL_FillRect(_screen, &sdlrect, color);
}
bit hwcursor() {
return _hwcursor;
}
bit fullscreen() {
return _fullscreen;
}
private:
SDL_Surface* _screen;
bit _fullscreen;
bit _hwcursor;
}
private GraphicsSystem myInstance = null;
/+ Start graphics +/
void start(bit fullscreen, bit hwcursor)
{
if(myInstance) {
delete myInstance;
}
myInstance = new GraphicsSystem(fullscreen, hwcursor);
}
/+ Stop graphics +/
void stop()
{
assert(myInstance);
delete myInstance;
}
/+ Return the current GraphicsSystem instance +/
GraphicsSystem inst()
{
assert(myInstance);
return myInstance;
}
class CouldNotSetVideoModeException : Exception
{
this()
{
char[] msg = swritef("Could not set video mode");
super(msg);
}
}