author | saiam |
Mon, 08 Dec 2008 00:36:24 +0000 | |
changeset 275 | fa44b905bc2e |
parent 266 | ad72d0a0cc02 |
child 282 | e0e4dfc3e528 |
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), |
266
ad72d0a0cc02
increase the map size and control the camera to always have the screen full of terrain
terom
parents:
248
diff
changeset
|
8 |
resolution(GRAPHICS_RESOLUTION_WIDTH, GRAPHICS_RESOLUTION_HEIGHT), |
25 | 9 |
engine(engine), |
10 |
state(state), |
|
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 |
||
275
fa44b905bc2e
Tried to take input tick into account in updatePosition but it still doesn't seem to work
saiam
parents:
266
diff
changeset
|
23 |
void Graphics::check_input (TimeMS dt) { |
25 | 24 |
LocalPlayer *player; |
221 | 25 |
PlayerInput input_mask = 0; |
25 | 26 |
|
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
|
27 |
// update gui flags |
235 | 28 |
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
|
29 |
|
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 |
// 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
|
31 |
if (flags & GUI_INPUT_QUIT) { |
108 | 32 |
engine.stop(); |
25 | 33 |
|
108 | 34 |
return; |
25 | 35 |
} |
36 |
||
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
|
37 |
// stop here if we don't have a local player |
25 | 38 |
if ((player = state.getLocalPlayer()) == NULL) |
39 |
return; |
|
40 |
||
221 | 41 |
// 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
|
42 |
if (flags & GUI_INPUT_DEBUG_PLAYER) |
221 | 43 |
player->printDebugInfo(); |
180 | 44 |
|
230 | 45 |
// build input_mask |
235 | 46 |
input_mask = input.readPlayerInput(); |
221 | 47 |
|
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
|
48 |
// apply input if there was any |
221 | 49 |
if (input_mask) |
275
fa44b905bc2e
Tried to take input tick into account in updatePosition but it still doesn't seem to work
saiam
parents:
266
diff
changeset
|
50 |
player->handleInput(input_mask, dt); |
25 | 51 |
} |
52 |
||
266
ad72d0a0cc02
increase the map size and control the camera to always have the screen full of terrain
terom
parents:
248
diff
changeset
|
53 |
static PixelDimension value_between (PixelDimension low, PixelDimension value, PixelDimension high) { |
ad72d0a0cc02
increase the map size and control the camera to always have the screen full of terrain
terom
parents:
248
diff
changeset
|
54 |
if (value < low) |
ad72d0a0cc02
increase the map size and control the camera to always have the screen full of terrain
terom
parents:
248
diff
changeset
|
55 |
return low; |
ad72d0a0cc02
increase the map size and control the camera to always have the screen full of terrain
terom
parents:
248
diff
changeset
|
56 |
|
ad72d0a0cc02
increase the map size and control the camera to always have the screen full of terrain
terom
parents:
248
diff
changeset
|
57 |
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
|
58 |
return high; |
ad72d0a0cc02
increase the map size and control the camera to always have the screen full of terrain
terom
parents:
248
diff
changeset
|
59 |
|
ad72d0a0cc02
increase the map size and control the camera to always have the screen full of terrain
terom
parents:
248
diff
changeset
|
60 |
else |
ad72d0a0cc02
increase the map size and control the camera to always have the screen full of terrain
terom
parents:
248
diff
changeset
|
61 |
return value; |
ad72d0a0cc02
increase the map size and control the camera to always have the screen full of terrain
terom
parents:
248
diff
changeset
|
62 |
} |
ad72d0a0cc02
increase the map size and control the camera to always have the screen full of terrain
terom
parents:
248
diff
changeset
|
63 |
|
25 | 64 |
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
|
65 |
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
|
66 |
LocalPlayer *player; |
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 |
// calculate camera |
ad72d0a0cc02
increase the map size and control the camera to always have the screen full of terrain
terom
parents:
248
diff
changeset
|
69 |
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
|
70 |
|
ad72d0a0cc02
increase the map size and control the camera to always have the screen full of terrain
terom
parents:
248
diff
changeset
|
71 |
// ...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
|
72 |
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
|
73 |
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
|
74 |
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
|
75 |
|
ad72d0a0cc02
increase the map size and control the camera to always have the screen full of terrain
terom
parents:
248
diff
changeset
|
76 |
// 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
|
77 |
camera = PixelCoordinate( |
ad72d0a0cc02
increase the map size and control the camera to always have the screen full of terrain
terom
parents:
248
diff
changeset
|
78 |
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
|
79 |
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
|
80 |
); |
ad72d0a0cc02
increase the map size and control the camera to always have the screen full of terrain
terom
parents:
248
diff
changeset
|
81 |
} |
25 | 82 |
|
162
f760591b7481
Removed unnecessary variables (factoFoo) from do_redraw. Renamed
saiam
parents:
161
diff
changeset
|
83 |
// White background |
248 | 84 |
gc->clear(CL_Color::black); |
25 | 85 |
|
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
|
86 |
// 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
|
87 |
state.draw(this, camera, flags & GUI_INPUT_DISPLAY_WEAPON); |
180 | 88 |
|
184 | 89 |
// 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
|
90 |
flip(1); |
25 | 91 |
} |
92 |
||
205 | 93 |
void Graphics::on_update (TimeMS tick_length) { |
25 | 94 |
// check keyboard input |
275
fa44b905bc2e
Tried to take input tick into account in updatePosition but it still doesn't seem to work
saiam
parents:
266
diff
changeset
|
95 |
check_input(tick_length); |
25 | 96 |
|
97 |
// redraw display |
|
98 |
do_redraw(); |
|
99 |
} |