author | nireco |
Mon, 08 Dec 2008 22:57:01 +0000 | |
changeset 325 | a19c78786d7f |
parent 318 | dca3c836edb2 |
child 326 | b6bdc29bcf6e |
permissions | -rw-r--r-- |
25 | 1 |
|
2 |
#include "Graphics.hh" |
|
86 | 3 |
#include "GameState.hh" |
108 | 4 |
#include <cmath> |
325
a19c78786d7f
kills, deaths and weapon name is displayed in right place
nireco
parents:
318
diff
changeset
|
5 |
#include <sstream> |
25 | 6 |
|
7 |
Graphics::Graphics (Engine &engine, GameState &state) : |
|
233
ff4ecea83cf5
start using CL_ResourceManager, change most draw methods to take a Graphics*, implment even better input handling, and draw weapon names
terom
parents:
230
diff
changeset
|
8 |
CL_DisplayWindow(GRAPHICS_WINDOW_TITLE, GRAPHICS_RESOLUTION_WIDTH, GRAPHICS_RESOLUTION_HEIGHT), |
25 | 9 |
engine(engine), |
10 |
state(state), |
|
282
e0e4dfc3e528
compiles cleanly with -Wall -Wextra -Wconversion, not tested, but that shouldn't break anything :)
terom
parents:
275
diff
changeset
|
11 |
resolution(GRAPHICS_RESOLUTION_WIDTH, GRAPHICS_RESOLUTION_HEIGHT), |
25 | 12 |
update_timer(GRAPHICS_UPDATE_INTERVAL_MS), |
235 | 13 |
input(get_ic()->get_keyboard()), |
233
ff4ecea83cf5
start using CL_ResourceManager, change most draw methods to take a Graphics*, implment even better input handling, and draw weapon names
terom
parents:
230
diff
changeset
|
14 |
simple_font("Font2", engine.getResourceManager()) |
ff4ecea83cf5
start using CL_ResourceManager, change most draw methods to take a Graphics*, implment even better input handling, and draw weapon names
terom
parents:
230
diff
changeset
|
15 |
{ |
86 | 16 |
|
25 | 17 |
// connect timer signal |
205 | 18 |
slots.connect(update_timer.sig_tick(), this, &Graphics::on_update); |
25 | 19 |
|
20 |
// enable |
|
205 | 21 |
update_timer.start(); |
25 | 22 |
} |
23 |
||
311
440763821484
make Input have its own timer, and add key-repeat handling, and fix some warnings
terom
parents:
282
diff
changeset
|
24 |
void Graphics::check_input (void) { |
25 | 25 |
LocalPlayer *player; |
311
440763821484
make Input have its own timer, and add key-repeat handling, and fix some warnings
terom
parents:
282
diff
changeset
|
26 |
PlayerInput input_mask; |
440763821484
make Input have its own timer, and add key-repeat handling, and fix some warnings
terom
parents:
282
diff
changeset
|
27 |
TimeMS input_dt; |
25 | 28 |
|
233
ff4ecea83cf5
start using CL_ResourceManager, change most draw methods to take a Graphics*, implment even better input handling, and draw weapon names
terom
parents:
230
diff
changeset
|
29 |
// update gui flags |
235 | 30 |
this->flags = input.readGuiInput(); |
233
ff4ecea83cf5
start using CL_ResourceManager, change most draw methods to take a Graphics*, implment even better input handling, and draw weapon names
terom
parents:
230
diff
changeset
|
31 |
|
ff4ecea83cf5
start using CL_ResourceManager, change most draw methods to take a Graphics*, implment even better input handling, and draw weapon names
terom
parents:
230
diff
changeset
|
32 |
// quit? |
ff4ecea83cf5
start using CL_ResourceManager, change most draw methods to take a Graphics*, implment even better input handling, and draw weapon names
terom
parents:
230
diff
changeset
|
33 |
if (flags & GUI_INPUT_QUIT) { |
108 | 34 |
engine.stop(); |
25 | 35 |
|
108 | 36 |
return; |
25 | 37 |
} |
38 |
||
233
ff4ecea83cf5
start using CL_ResourceManager, change most draw methods to take a Graphics*, implment even better input handling, and draw weapon names
terom
parents:
230
diff
changeset
|
39 |
// stop here if we don't have a local player |
25 | 40 |
if ((player = state.getLocalPlayer()) == NULL) |
41 |
return; |
|
42 |
||
221 | 43 |
// dump debug info on stderr |
233
ff4ecea83cf5
start using CL_ResourceManager, change most draw methods to take a Graphics*, implment even better input handling, and draw weapon names
terom
parents:
230
diff
changeset
|
44 |
if (flags & GUI_INPUT_DEBUG_PLAYER) |
221 | 45 |
player->printDebugInfo(); |
180 | 46 |
|
230 | 47 |
// build input_mask |
311
440763821484
make Input have its own timer, and add key-repeat handling, and fix some warnings
terom
parents:
282
diff
changeset
|
48 |
input.readPlayerInput(input_mask, input_dt); |
221 | 49 |
|
233
ff4ecea83cf5
start using CL_ResourceManager, change most draw methods to take a Graphics*, implment even better input handling, and draw weapon names
terom
parents:
230
diff
changeset
|
50 |
// apply input if there was any |
221 | 51 |
if (input_mask) |
311
440763821484
make Input have its own timer, and add key-repeat handling, and fix some warnings
terom
parents:
282
diff
changeset
|
52 |
player->handleInput(input_mask, input_dt); |
25 | 53 |
} |
54 |
||
266
ad72d0a0cc02
increase the map size and control the camera to always have the screen full of terrain
terom
parents:
248
diff
changeset
|
55 |
static PixelDimension value_between (PixelDimension low, PixelDimension value, PixelDimension high) { |
316
d909a7e36f8d
value_between now puts small map in the center, or at least should
nireco
parents:
315
diff
changeset
|
56 |
if (high < low) |
d909a7e36f8d
value_between now puts small map in the center, or at least should
nireco
parents:
315
diff
changeset
|
57 |
return (high + low) / 2; |
d909a7e36f8d
value_between now puts small map in the center, or at least should
nireco
parents:
315
diff
changeset
|
58 |
|
d909a7e36f8d
value_between now puts small map in the center, or at least should
nireco
parents:
315
diff
changeset
|
59 |
else if (value < low) |
266
ad72d0a0cc02
increase the map size and control the camera to always have the screen full of terrain
terom
parents:
248
diff
changeset
|
60 |
return low; |
ad72d0a0cc02
increase the map size and control the camera to always have the screen full of terrain
terom
parents:
248
diff
changeset
|
61 |
|
ad72d0a0cc02
increase the map size and control the camera to always have the screen full of terrain
terom
parents:
248
diff
changeset
|
62 |
else if (value > high) |
ad72d0a0cc02
increase the map size and control the camera to always have the screen full of terrain
terom
parents:
248
diff
changeset
|
63 |
return high; |
ad72d0a0cc02
increase the map size and control the camera to always have the screen full of terrain
terom
parents:
248
diff
changeset
|
64 |
|
ad72d0a0cc02
increase the map size and control the camera to always have the screen full of terrain
terom
parents:
248
diff
changeset
|
65 |
else |
ad72d0a0cc02
increase the map size and control the camera to always have the screen full of terrain
terom
parents:
248
diff
changeset
|
66 |
return value; |
ad72d0a0cc02
increase the map size and control the camera to always have the screen full of terrain
terom
parents:
248
diff
changeset
|
67 |
} |
ad72d0a0cc02
increase the map size and control the camera to always have the screen full of terrain
terom
parents:
248
diff
changeset
|
68 |
|
25 | 69 |
void Graphics::do_redraw (void) { |
233
ff4ecea83cf5
start using CL_ResourceManager, change most draw methods to take a Graphics*, implment even better input handling, and draw weapon names
terom
parents:
230
diff
changeset
|
70 |
CL_GraphicContext *gc = get_gc(); |
266
ad72d0a0cc02
increase the map size and control the camera to always have the screen full of terrain
terom
parents:
248
diff
changeset
|
71 |
LocalPlayer *player; |
ad72d0a0cc02
increase the map size and control the camera to always have the screen full of terrain
terom
parents:
248
diff
changeset
|
72 |
|
ad72d0a0cc02
increase the map size and control the camera to always have the screen full of terrain
terom
parents:
248
diff
changeset
|
73 |
// calculate camera |
ad72d0a0cc02
increase the map size and control the camera to always have the screen full of terrain
terom
parents:
248
diff
changeset
|
74 |
PixelCoordinate camera(0, 0); |
ad72d0a0cc02
increase the map size and control the camera to always have the screen full of terrain
terom
parents:
248
diff
changeset
|
75 |
|
ad72d0a0cc02
increase the map size and control the camera to always have the screen full of terrain
terom
parents:
248
diff
changeset
|
76 |
// ...to track our local player |
ad72d0a0cc02
increase the map size and control the camera to always have the screen full of terrain
terom
parents:
248
diff
changeset
|
77 |
if ((player = state.getLocalPlayer()) != NULL) { |
318 | 78 |
PixelCoordinate target = player->getCoordinate() - PixelCoordinate(resolution.x / 2, (resolution.y - 100) / 2); |
79 |
PixelCoordinate max = state.world.getDimensions() - resolution + PixelCoordinate(0, 100); |
|
266
ad72d0a0cc02
increase the map size and control the camera to always have the screen full of terrain
terom
parents:
248
diff
changeset
|
80 |
|
ad72d0a0cc02
increase the map size and control the camera to always have the screen full of terrain
terom
parents:
248
diff
changeset
|
81 |
// keep the terrain in view |
ad72d0a0cc02
increase the map size and control the camera to always have the screen full of terrain
terom
parents:
248
diff
changeset
|
82 |
camera = PixelCoordinate( |
ad72d0a0cc02
increase the map size and control the camera to always have the screen full of terrain
terom
parents:
248
diff
changeset
|
83 |
value_between(0, target.x, max.x), |
ad72d0a0cc02
increase the map size and control the camera to always have the screen full of terrain
terom
parents:
248
diff
changeset
|
84 |
value_between(0, target.y, max.y) |
ad72d0a0cc02
increase the map size and control the camera to always have the screen full of terrain
terom
parents:
248
diff
changeset
|
85 |
); |
ad72d0a0cc02
increase the map size and control the camera to always have the screen full of terrain
terom
parents:
248
diff
changeset
|
86 |
} |
25 | 87 |
|
312 | 88 |
// Black background |
248 | 89 |
gc->clear(CL_Color::black); |
25 | 90 |
|
233
ff4ecea83cf5
start using CL_ResourceManager, change most draw methods to take a Graphics*, implment even better input handling, and draw weapon names
terom
parents:
230
diff
changeset
|
91 |
// Draw the game |
266
ad72d0a0cc02
increase the map size and control the camera to always have the screen full of terrain
terom
parents:
248
diff
changeset
|
92 |
state.draw(this, camera, flags & GUI_INPUT_DISPLAY_WEAPON); |
180 | 93 |
|
312 | 94 |
// Draw box for player information |
95 |
if (player != NULL) { |
|
315 | 96 |
draw_player_info(gc, (Player*)player); |
312 | 97 |
} |
98 |
||
184 | 99 |
// Flip window buffer, sync |
233
ff4ecea83cf5
start using CL_ResourceManager, change most draw methods to take a Graphics*, implment even better input handling, and draw weapon names
terom
parents:
230
diff
changeset
|
100 |
flip(1); |
25 | 101 |
} |
102 |
||
205 | 103 |
void Graphics::on_update (TimeMS tick_length) { |
311
440763821484
make Input have its own timer, and add key-repeat handling, and fix some warnings
terom
parents:
282
diff
changeset
|
104 |
(void) tick_length; |
440763821484
make Input have its own timer, and add key-repeat handling, and fix some warnings
terom
parents:
282
diff
changeset
|
105 |
|
25 | 106 |
// check keyboard input |
311
440763821484
make Input have its own timer, and add key-repeat handling, and fix some warnings
terom
parents:
282
diff
changeset
|
107 |
check_input(); |
25 | 108 |
|
109 |
// redraw display |
|
110 |
do_redraw(); |
|
111 |
} |
|
315 | 112 |
|
113 |
void Graphics::draw_player_info(CL_GraphicContext *gc, Player *p) { |
|
114 |
int box_top = GRAPHICS_RESOLUTION_HEIGHT - 100; |
|
115 |
int box_left = 0; |
|
116 |
int box_right = GRAPHICS_RESOLUTION_WIDTH; |
|
117 |
int box_bottom = GRAPHICS_RESOLUTION_HEIGHT; |
|
318 | 118 |
int bar_length = 3; // *100 |
315 | 119 |
|
120 |
// draw status info at bottom of display |
|
121 |
gc->fill_rect( |
|
122 |
CL_Rect( |
|
123 |
box_left, |
|
124 |
box_top, |
|
125 |
box_right, |
|
126 |
box_bottom |
|
127 |
), |
|
128 |
CL_Gradient( |
|
325
a19c78786d7f
kills, deaths and weapon name is displayed in right place
nireco
parents:
318
diff
changeset
|
129 |
CL_Color(0, 0, 0), |
a19c78786d7f
kills, deaths and weapon name is displayed in right place
nireco
parents:
318
diff
changeset
|
130 |
CL_Color(50, 50, 50), |
315 | 131 |
CL_Color(50, 50, 50, 150), |
132 |
CL_Color(100, 100, 100, 200) |
|
133 |
) |
|
134 |
); |
|
318 | 135 |
|
136 |
// Lifebar |
|
315 | 137 |
gc->draw_rect( |
138 |
CL_Rect( |
|
139 |
box_left + 9, |
|
140 |
box_top + 9, |
|
318 | 141 |
box_left + 11 + 100 * bar_length, |
315 | 142 |
box_top + 31 |
143 |
), |
|
144 |
CL_Color(150, 150, 150) |
|
145 |
); |
|
146 |
||
147 |
gc->fill_rect( |
|
148 |
CL_Rect( |
|
149 |
box_left + 10, |
|
150 |
box_top + 10, |
|
318 | 151 |
box_left + 10 + (int) (p->getHealthPercent() * bar_length), |
315 | 152 |
box_top + 30 |
153 |
), |
|
154 |
CL_Gradient( |
|
155 |
CL_Color(200, 0, 0), |
|
156 |
CL_Color(200 - (int)(p->getHealthPercent() * 2), (int)(p->getHealthPercent() * 2), 0), |
|
157 |
CL_Color(200, 0, 0), |
|
158 |
CL_Color(200 - (int)(p->getHealthPercent() * 2), (int)(p->getHealthPercent() * 2), 0) |
|
159 |
) |
|
160 |
); |
|
325
a19c78786d7f
kills, deaths and weapon name is displayed in right place
nireco
parents:
318
diff
changeset
|
161 |
std::stringstream sskills; |
a19c78786d7f
kills, deaths and weapon name is displayed in right place
nireco
parents:
318
diff
changeset
|
162 |
sskills << "Kills: " << p->getKills(); |
a19c78786d7f
kills, deaths and weapon name is displayed in right place
nireco
parents:
318
diff
changeset
|
163 |
getSimpleFont().draw( |
a19c78786d7f
kills, deaths and weapon name is displayed in right place
nireco
parents:
318
diff
changeset
|
164 |
box_left + 20 + 100 * bar_length, |
a19c78786d7f
kills, deaths and weapon name is displayed in right place
nireco
parents:
318
diff
changeset
|
165 |
box_top + 10, |
a19c78786d7f
kills, deaths and weapon name is displayed in right place
nireco
parents:
318
diff
changeset
|
166 |
sskills.str(), |
a19c78786d7f
kills, deaths and weapon name is displayed in right place
nireco
parents:
318
diff
changeset
|
167 |
get_gc() |
a19c78786d7f
kills, deaths and weapon name is displayed in right place
nireco
parents:
318
diff
changeset
|
168 |
); |
a19c78786d7f
kills, deaths and weapon name is displayed in right place
nireco
parents:
318
diff
changeset
|
169 |
|
a19c78786d7f
kills, deaths and weapon name is displayed in right place
nireco
parents:
318
diff
changeset
|
170 |
std::stringstream ssdeaths; |
a19c78786d7f
kills, deaths and weapon name is displayed in right place
nireco
parents:
318
diff
changeset
|
171 |
ssdeaths << "Deaths: " << p->getDeaths(); |
a19c78786d7f
kills, deaths and weapon name is displayed in right place
nireco
parents:
318
diff
changeset
|
172 |
getSimpleFont().draw( |
a19c78786d7f
kills, deaths and weapon name is displayed in right place
nireco
parents:
318
diff
changeset
|
173 |
box_left + 20 + 100 * bar_length, |
a19c78786d7f
kills, deaths and weapon name is displayed in right place
nireco
parents:
318
diff
changeset
|
174 |
box_top + 40, |
a19c78786d7f
kills, deaths and weapon name is displayed in right place
nireco
parents:
318
diff
changeset
|
175 |
ssdeaths.str(), |
a19c78786d7f
kills, deaths and weapon name is displayed in right place
nireco
parents:
318
diff
changeset
|
176 |
get_gc() |
a19c78786d7f
kills, deaths and weapon name is displayed in right place
nireco
parents:
318
diff
changeset
|
177 |
); |
a19c78786d7f
kills, deaths and weapon name is displayed in right place
nireco
parents:
318
diff
changeset
|
178 |
|
318 | 179 |
|
180 |
// Ammobar |
|
181 |
gc->draw_rect( |
|
182 |
CL_Rect( |
|
183 |
box_left + 9, |
|
325
a19c78786d7f
kills, deaths and weapon name is displayed in right place
nireco
parents:
318
diff
changeset
|
184 |
box_top + 69, |
318 | 185 |
box_left + 11 + 100 * bar_length, |
325
a19c78786d7f
kills, deaths and weapon name is displayed in right place
nireco
parents:
318
diff
changeset
|
186 |
box_top + 91 |
318 | 187 |
), |
188 |
CL_Color(150, 150, 150) |
|
189 |
); |
|
190 |
||
191 |
gc->fill_rect( |
|
192 |
CL_Rect( |
|
193 |
box_left + 10, |
|
325
a19c78786d7f
kills, deaths and weapon name is displayed in right place
nireco
parents:
318
diff
changeset
|
194 |
box_top + 70, |
318 | 195 |
box_left + 10 + (int) (p->getCurrentWeapon()->getReloadTimer() * 100 / p->getCurrentWeapon()->getReloadTime() * bar_length), |
325
a19c78786d7f
kills, deaths and weapon name is displayed in right place
nireco
parents:
318
diff
changeset
|
196 |
box_top + 90 |
318 | 197 |
), |
198 |
CL_Gradient( |
|
199 |
CL_Color(100, 100, 0), |
|
200 |
CL_Color(100, 100, 0), |
|
201 |
CL_Color(100, 100, 0), |
|
202 |
CL_Color(100, 100, 100) |
|
203 |
) |
|
204 |
); |
|
325
a19c78786d7f
kills, deaths and weapon name is displayed in right place
nireco
parents:
318
diff
changeset
|
205 |
|
a19c78786d7f
kills, deaths and weapon name is displayed in right place
nireco
parents:
318
diff
changeset
|
206 |
getSimpleFont().draw( |
a19c78786d7f
kills, deaths and weapon name is displayed in right place
nireco
parents:
318
diff
changeset
|
207 |
box_left + 20 + 100 * bar_length, |
a19c78786d7f
kills, deaths and weapon name is displayed in right place
nireco
parents:
318
diff
changeset
|
208 |
box_top + 70, |
a19c78786d7f
kills, deaths and weapon name is displayed in right place
nireco
parents:
318
diff
changeset
|
209 |
p->getCurrentWeapon()->getName(), |
a19c78786d7f
kills, deaths and weapon name is displayed in right place
nireco
parents:
318
diff
changeset
|
210 |
get_gc() |
a19c78786d7f
kills, deaths and weapon name is displayed in right place
nireco
parents:
318
diff
changeset
|
211 |
); |
315 | 212 |
} |
213 |