-
Notifications
You must be signed in to change notification settings - Fork 0
/
Game.d
executable file
·151 lines (121 loc) · 4.26 KB
/
Game.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
module Game;
private import Scene;
private import derelict.sdl.sdl;
private import IntroScene;
private import MainMenu;
private import Vector;
private import Graphics;
private import ImageResource;
private import SofuResource;
private import Image;
class Game
{
public:
this() {
pushScene(new MainMenu(this));
pushScene(new IntroScene(this));
_mouseCursor = loadImage("images/cursor.png");
_mousePosition = new Vector2d;
}
void run()
{
Uint32 frametime=0, time1=0, time2=SDL_GetTicks();
while(currentScene()) {
Scene scene = currentScene();
SDL_Event event;
while(SDL_PollEvent(&event)) {
switch(event.type) {
case SDL_MOUSEMOTION: {
Vector2d mousePosition =
new Vector2d(event.motion.x, event.motion.y);
_mousePosition = mousePosition;
Vector2d mouseRel =
new Vector2d(event.motion.xrel, event.motion.yrel);
scene.mouseMotion(mousePosition, mouseRel);
break;
}
case SDL_MOUSEBUTTONDOWN: {
Vector2d mousePosition =
new Vector2d(event.motion.x, event.motion.y);
switch(event.button.button) {
case SDL_BUTTON_LEFT:
scene.lmbDown(mousePosition);
break;
case SDL_BUTTON_RIGHT:
scene.rmbDown(mousePosition);
break;
default:
break;
}
break;
}
case SDL_MOUSEBUTTONUP: {
Vector2d mousePosition =
new Vector2d(event.motion.x, event.motion.y);
switch(event.button.button) {
case SDL_BUTTON_LEFT:
scene.lmbUp(mousePosition);
break;
case SDL_BUTTON_RIGHT:
scene.rmbUp(mousePosition);
break;
default:
break;
}
break;
}
case SDL_KEYDOWN: {
scene.keyDown(event.key);
break;
}
case SDL_KEYUP: {
scene.keyUp(event.key);
break;
}
case SDL_QUIT: {
scene.finish();
break;
}
default: {
break;
}
}
}
time1 = time2;
time2 = SDL_GetTicks();
frametime = time2 - time1;
scene.advance(cast(double) frametime);
SDL_Rect screenrect;
screenrect.x = 0; screenrect.y = 0;
screenrect.w = 640; screenrect.h = 480;
SDL_FillRect(Graphics.inst().screen(), &screenrect,
SDL_MapRGB(Graphics.inst().screen().format, 0, 0, 0));
scene.draw();
if(!Graphics.inst().hwcursor()) {
_mouseCursor.draw(_mousePosition - new Vector2d(16, 16));
}
Graphics.inst().updateScreen();
if(currentScene().finished()) {
_popScene();
}
}
}
Scene currentScene() {
if(_scenes.length) {
return _scenes[length-1];
}
else {
return null;
}
}
void pushScene(Scene scene) {
_scenes ~= scene;
}
private:
void _popScene() {
_scenes.length = _scenes.length - 1;
}
Scene[] _scenes;
Image _mouseCursor;
Vector2d _mousePosition;
}