author | ekku |
Sun, 23 Nov 2008 18:19:53 +0000 | |
changeset 94 | 08bebac3d0c2 |
parent 91 | 0a6d675099dc |
child 96 | 4a801210096c |
permissions | -rw-r--r-- |
25 | 1 |
|
2 |
#include "Graphics.hh" |
|
86 | 3 |
#include "Physics.hh" |
4 |
#include "GameState.hh" |
|
25 | 5 |
|
6 |
Graphics::Graphics (Engine &engine, GameState &state) : |
|
7 |
engine(engine), |
|
8 |
state(state), |
|
9 |
update_timer(GRAPHICS_UPDATE_INTERVAL_MS), |
|
60 | 10 |
win(GRAPHICS_WINDOW_TITLE, GRAPHICS_RESOLUTION_WIDTH, GRAPHICS_RESOLUTION_HEIGHT), |
25 | 11 |
keyboard(win.get_ic()->get_keyboard()) { |
86 | 12 |
|
13 |
Vector tmp; |
|
14 |
CL_Color color; |
|
90 | 15 |
CL_PixelBuffer terr(MAP_WIDTH, MAP_HEIGHT, 4*MAP_WIDTH, CL_PixelFormat::rgba8888); |
87 | 16 |
|
17 |
||
86 | 18 |
for (tmp.x = 0; tmp.x < MAP_WIDTH; tmp.x++) { |
19 |
for (tmp.y = 0; tmp.y < MAP_HEIGHT; tmp.y++) { |
|
20 |
if (state.getType(tmp) == EMPTY) { |
|
90 | 21 |
color = CL_Color(86, 41, 0); |
86 | 22 |
} else if (state.getType(tmp) == DIRT) { |
88 | 23 |
color = CL_Color(144, 82, 23); |
86 | 24 |
} else if (state.getType(tmp) == ROCK) { |
88 | 25 |
color = CL_Color(132, 136, 135); |
86 | 26 |
} else { |
27 |
// Fale |
|
28 |
} |
|
29 |
terr.draw_pixel(tmp.x, tmp.y, color); |
|
90 | 30 |
} |
86 | 31 |
} |
87 | 32 |
terrain = CL_Surface(terr); |
33 |
||
25 | 34 |
// connect timer signal |
35 |
slots.connect(update_timer.sig_timer(), this, &Graphics::on_update); |
|
36 |
||
37 |
// enable |
|
38 |
update_timer.enable(); |
|
39 |
} |
|
40 |
||
41 |
void Graphics::check_input (void) { |
|
42 |
LocalPlayer *player; |
|
58 | 43 |
PlayerInput_Move input_move = 0; |
25 | 44 |
|
45 |
// stop on escape |
|
46 |
if (keyboard.get_keycode(CL_KEY_ESCAPE)) { |
|
47 |
engine.stop(); |
|
48 |
||
49 |
return; |
|
50 |
} |
|
51 |
||
52 |
// ignore if we don't have a local player |
|
53 |
if ((player = state.getLocalPlayer()) == NULL) |
|
54 |
return; |
|
55 |
||
56 |
// handle up/down/left/right |
|
57 |
if (keyboard.get_keycode(CL_KEY_UP)) |
|
54 | 58 |
input_move |= INPUT_MOVE_UP; |
25 | 59 |
|
60 |
if (keyboard.get_keycode(CL_KEY_DOWN)) |
|
54 | 61 |
input_move |= INPUT_MOVE_DOWN; |
25 | 62 |
|
63 |
if (keyboard.get_keycode(CL_KEY_LEFT)) |
|
54 | 64 |
input_move |= INPUT_MOVE_LEFT; |
25 | 65 |
|
66 |
if (keyboard.get_keycode(CL_KEY_RIGHT)) |
|
54 | 67 |
input_move |= INPUT_MOVE_RIGHT; |
94 | 68 |
|
69 |
if (keyboard.get_keycode(CL_KEY_I)) |
|
70 |
player->debugInfo(); |
|
25 | 71 |
|
72 |
// apply movement if applicable |
|
50
9e1a6506f5a1
some rough-handed code modifications towards a newer, better, working Physics
terom
parents:
25
diff
changeset
|
73 |
if (input_move) |
9e1a6506f5a1
some rough-handed code modifications towards a newer, better, working Physics
terom
parents:
25
diff
changeset
|
74 |
player->handleMove(input_move); |
25 | 75 |
} |
76 |
||
77 |
void Graphics::do_redraw (void) { |
|
78 |
CL_GraphicContext *gc = win.get_gc(); |
|
79 |
||
80 |
// white background |
|
81 |
gc->clear(CL_Color::white); |
|
82 |
||
60 | 83 |
const float factorX = GRAPHICS_RESOLUTION_WIDTH / MAP_WIDTH; |
84 |
const float factorY = GRAPHICS_RESOLUTION_HEIGHT / MAP_HEIGHT; |
|
85 |
||
86 | 86 |
// draw terrain |
87 |
terrain.draw(0,0, gc); |
|
88 |
||
25 | 89 |
// draw players |
90 |
for (std::list<Player*>::iterator it = state.player_list.begin(); it != state.player_list.end(); it++) { |
|
91 |
Player *p = *it; |
|
92 |
||
91 | 93 |
Vector loc = p->getPosition(); |
94 |
std::vector<Vector> &shape = p->getShape(); |
|
95 |
// draw quad |
|
96 |
gc->fill_quad( |
|
97 |
CL_Quad( |
|
94 | 98 |
(loc.x+shape[0].x) * factorX, (loc.y+shape[0].y) * factorY, |
99 |
(loc.x+shape[1].x) * factorX, (loc.y+shape[1].y) * factorY, |
|
100 |
(loc.x+shape[2].x) * factorX, (loc.y+shape[2].y) * factorY, |
|
101 |
(loc.x+shape[3].x) * factorX, (loc.y+shape[3].y) * factorY |
|
91 | 102 |
), CL_Color::green |
25 | 103 |
); |
104 |
} |
|
105 |
||
106 |
// flip window buffer, LIEK NAO |
|
107 |
win.flip(0); |
|
108 |
} |
|
109 |
||
110 |
void Graphics::on_update (void) { |
|
111 |
// check keyboard input |
|
112 |
check_input(); |
|
113 |
||
114 |
// redraw display |
|
115 |
do_redraw(); |
|
116 |
} |