author | nireco |
Mon, 08 Dec 2008 21:54:04 +0000 | |
changeset 316 | d909a7e36f8d |
parent 315 | fe9da2d5355e |
child 318 | dca3c836edb2 |
permissions | -rw-r--r-- |
25 | 1 |
|
2 |
#include "Graphics.hh" |
|
86 | 3 |
#include "GameState.hh" |
108 | 4 |
#include <cmath> |
25 | 5 |
|
6 |
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
|
7 |
CL_DisplayWindow(GRAPHICS_WINDOW_TITLE, GRAPHICS_RESOLUTION_WIDTH, GRAPHICS_RESOLUTION_HEIGHT), |
25 | 8 |
engine(engine), |
9 |
state(state), |
|
282
e0e4dfc3e528
compiles cleanly with -Wall -Wextra -Wconversion, not tested, but that shouldn't break anything :)
terom
parents:
275
diff
changeset
|
10 |
resolution(GRAPHICS_RESOLUTION_WIDTH, GRAPHICS_RESOLUTION_HEIGHT), |
25 | 11 |
update_timer(GRAPHICS_UPDATE_INTERVAL_MS), |
235 | 12 |
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
|
13 |
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
|
14 |
{ |
86 | 15 |
|
25 | 16 |
// connect timer signal |
205 | 17 |
slots.connect(update_timer.sig_tick(), this, &Graphics::on_update); |
25 | 18 |
|
19 |
// enable |
|
205 | 20 |
update_timer.start(); |
25 | 21 |
} |
22 |
||
311
440763821484
make Input have its own timer, and add key-repeat handling, and fix some warnings
terom
parents:
282
diff
changeset
|
23 |
void Graphics::check_input (void) { |
25 | 24 |
LocalPlayer *player; |
311
440763821484
make Input have its own timer, and add key-repeat handling, and fix some warnings
terom
parents:
282
diff
changeset
|
25 |
PlayerInput input_mask; |
440763821484
make Input have its own timer, and add key-repeat handling, and fix some warnings
terom
parents:
282
diff
changeset
|
26 |
TimeMS input_dt; |
25 | 27 |
|
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
|
28 |
// update gui flags |
235 | 29 |
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
|
30 |
|
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 |
// 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
|
32 |
if (flags & GUI_INPUT_QUIT) { |
108 | 33 |
engine.stop(); |
25 | 34 |
|
108 | 35 |
return; |
25 | 36 |
} |
37 |
||
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
|
38 |
// stop here if we don't have a local player |
25 | 39 |
if ((player = state.getLocalPlayer()) == NULL) |
40 |
return; |
|
41 |
||
221 | 42 |
// 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
|
43 |
if (flags & GUI_INPUT_DEBUG_PLAYER) |
221 | 44 |
player->printDebugInfo(); |
180 | 45 |
|
230 | 46 |
// 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
|
47 |
input.readPlayerInput(input_mask, input_dt); |
221 | 48 |
|
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
|
49 |
// apply input if there was any |
221 | 50 |
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
|
51 |
player->handleInput(input_mask, input_dt); |
25 | 52 |
} |
53 |
||
266
ad72d0a0cc02
increase the map size and control the camera to always have the screen full of terrain
terom
parents:
248
diff
changeset
|
54 |
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
|
55 |
if (high < low) |
d909a7e36f8d
value_between now puts small map in the center, or at least should
nireco
parents:
315
diff
changeset
|
56 |
return (high + low) / 2; |
d909a7e36f8d
value_between now puts small map in the center, or at least should
nireco
parents:
315
diff
changeset
|
57 |
|
d909a7e36f8d
value_between now puts small map in the center, or at least should
nireco
parents:
315
diff
changeset
|
58 |
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
|
59 |
return low; |
ad72d0a0cc02
increase the map size and control the camera to always have the screen full of terrain
terom
parents:
248
diff
changeset
|
60 |
|
ad72d0a0cc02
increase the map size and control the camera to always have the screen full of terrain
terom
parents:
248
diff
changeset
|
61 |
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
|
62 |
return high; |
ad72d0a0cc02
increase the map size and control the camera to always have the screen full of terrain
terom
parents:
248
diff
changeset
|
63 |
|
ad72d0a0cc02
increase the map size and control the camera to always have the screen full of terrain
terom
parents:
248
diff
changeset
|
64 |
else |
ad72d0a0cc02
increase the map size and control the camera to always have the screen full of terrain
terom
parents:
248
diff
changeset
|
65 |
return value; |
ad72d0a0cc02
increase the map size and control the camera to always have the screen full of terrain
terom
parents:
248
diff
changeset
|
66 |
} |
ad72d0a0cc02
increase the map size and control the camera to always have the screen full of terrain
terom
parents:
248
diff
changeset
|
67 |
|
25 | 68 |
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
|
69 |
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
|
70 |
LocalPlayer *player; |
ad72d0a0cc02
increase the map size and control the camera to always have the screen full of terrain
terom
parents:
248
diff
changeset
|
71 |
|
ad72d0a0cc02
increase the map size and control the camera to always have the screen full of terrain
terom
parents:
248
diff
changeset
|
72 |
// calculate camera |
ad72d0a0cc02
increase the map size and control the camera to always have the screen full of terrain
terom
parents:
248
diff
changeset
|
73 |
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
|
74 |
|
ad72d0a0cc02
increase the map size and control the camera to always have the screen full of terrain
terom
parents:
248
diff
changeset
|
75 |
// ...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
|
76 |
if ((player = state.getLocalPlayer()) != NULL) { |
ad72d0a0cc02
increase the map size and control the camera to always have the screen full of terrain
terom
parents:
248
diff
changeset
|
77 |
PixelCoordinate target = player->getCoordinate() - resolution / 2; |
ad72d0a0cc02
increase the map size and control the camera to always have the screen full of terrain
terom
parents:
248
diff
changeset
|
78 |
PixelCoordinate max = state.world.getDimensions() - resolution; |
ad72d0a0cc02
increase the map size and control the camera to always have the screen full of terrain
terom
parents:
248
diff
changeset
|
79 |
|
ad72d0a0cc02
increase the map size and control the camera to always have the screen full of terrain
terom
parents:
248
diff
changeset
|
80 |
// 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
|
81 |
camera = PixelCoordinate( |
ad72d0a0cc02
increase the map size and control the camera to always have the screen full of terrain
terom
parents:
248
diff
changeset
|
82 |
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
|
83 |
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
|
84 |
); |
ad72d0a0cc02
increase the map size and control the camera to always have the screen full of terrain
terom
parents:
248
diff
changeset
|
85 |
} |
25 | 86 |
|
312 | 87 |
// Black background |
248 | 88 |
gc->clear(CL_Color::black); |
25 | 89 |
|
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
|
90 |
// 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
|
91 |
state.draw(this, camera, flags & GUI_INPUT_DISPLAY_WEAPON); |
180 | 92 |
|
312 | 93 |
// Draw box for player information |
94 |
if (player != NULL) { |
|
315 | 95 |
draw_player_info(gc, (Player*)player); |
312 | 96 |
} |
97 |
||
184 | 98 |
// 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
|
99 |
flip(1); |
25 | 100 |
} |
101 |
||
205 | 102 |
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
|
103 |
(void) tick_length; |
440763821484
make Input have its own timer, and add key-repeat handling, and fix some warnings
terom
parents:
282
diff
changeset
|
104 |
|
25 | 105 |
// 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
|
106 |
check_input(); |
25 | 107 |
|
108 |
// redraw display |
|
109 |
do_redraw(); |
|
110 |
} |
|
315 | 111 |
|
112 |
void Graphics::draw_player_info(CL_GraphicContext *gc, Player *p) { |
|
113 |
int box_top = GRAPHICS_RESOLUTION_HEIGHT - 100; |
|
114 |
int box_left = 0; |
|
115 |
int box_right = GRAPHICS_RESOLUTION_WIDTH; |
|
116 |
int box_bottom = GRAPHICS_RESOLUTION_HEIGHT; |
|
117 |
int life_bar_length = 3; // *100 |
|
118 |
||
119 |
// draw status info at bottom of display |
|
120 |
gc->fill_rect( |
|
121 |
CL_Rect( |
|
122 |
box_left, |
|
123 |
box_top, |
|
124 |
box_right, |
|
125 |
box_bottom |
|
126 |
), |
|
127 |
CL_Gradient( |
|
128 |
CL_Color(0, 0, 0, 100), |
|
129 |
CL_Color(50, 50, 50, 150), |
|
130 |
CL_Color(50, 50, 50, 150), |
|
131 |
CL_Color(100, 100, 100, 200) |
|
132 |
) |
|
133 |
); |
|
134 |
||
135 |
gc->draw_rect( |
|
136 |
CL_Rect( |
|
137 |
box_left + 9, |
|
138 |
box_top + 9, |
|
139 |
box_left + 11 + 100 * life_bar_length, |
|
140 |
box_top + 31 |
|
141 |
), |
|
142 |
CL_Color(150, 150, 150) |
|
143 |
); |
|
144 |
||
145 |
gc->fill_rect( |
|
146 |
CL_Rect( |
|
147 |
box_left + 10, |
|
148 |
box_top + 10, |
|
149 |
box_left + 10 + (int) (p->getHealthPercent() * life_bar_length), |
|
150 |
box_top + 30 |
|
151 |
), |
|
152 |
CL_Gradient( |
|
153 |
CL_Color(200, 0, 0), |
|
154 |
CL_Color(200 - (int)(p->getHealthPercent() * 2), (int)(p->getHealthPercent() * 2), 0), |
|
155 |
CL_Color(200, 0, 0), |
|
156 |
CL_Color(200 - (int)(p->getHealthPercent() * 2), (int)(p->getHealthPercent() * 2), 0) |
|
157 |
) |
|
158 |
); |
|
159 |
} |
|
160 |