author | ekku |
Thu, 20 Nov 2008 23:54:41 +0000 | |
changeset 90 | c1a072928790 |
parent 88 | 7431cd0cf900 |
child 91 | 0a6d675099dc |
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 |
||
88 | 34 |
Engine::log(DEBUG, "Graphics") << "Taalla ollaan."; |
86 | 35 |
|
25 | 36 |
// connect timer signal |
37 |
slots.connect(update_timer.sig_timer(), this, &Graphics::on_update); |
|
38 |
||
39 |
// enable |
|
40 |
update_timer.enable(); |
|
41 |
} |
|
42 |
||
43 |
void Graphics::check_input (void) { |
|
44 |
LocalPlayer *player; |
|
58 | 45 |
PlayerInput_Move input_move = 0; |
25 | 46 |
|
47 |
// stop on escape |
|
48 |
if (keyboard.get_keycode(CL_KEY_ESCAPE)) { |
|
49 |
engine.stop(); |
|
50 |
||
51 |
return; |
|
52 |
} |
|
53 |
||
54 |
// ignore if we don't have a local player |
|
55 |
if ((player = state.getLocalPlayer()) == NULL) |
|
56 |
return; |
|
57 |
||
58 |
// handle up/down/left/right |
|
59 |
if (keyboard.get_keycode(CL_KEY_UP)) |
|
54 | 60 |
input_move |= INPUT_MOVE_UP; |
25 | 61 |
|
62 |
if (keyboard.get_keycode(CL_KEY_DOWN)) |
|
54 | 63 |
input_move |= INPUT_MOVE_DOWN; |
25 | 64 |
|
65 |
if (keyboard.get_keycode(CL_KEY_LEFT)) |
|
54 | 66 |
input_move |= INPUT_MOVE_LEFT; |
25 | 67 |
|
68 |
if (keyboard.get_keycode(CL_KEY_RIGHT)) |
|
54 | 69 |
input_move |= INPUT_MOVE_RIGHT; |
25 | 70 |
|
71 |
// apply movement if applicable |
|
50
9e1a6506f5a1
some rough-handed code modifications towards a newer, better, working Physics
terom
parents:
25
diff
changeset
|
72 |
if (input_move) |
9e1a6506f5a1
some rough-handed code modifications towards a newer, better, working Physics
terom
parents:
25
diff
changeset
|
73 |
player->handleMove(input_move); |
25 | 74 |
} |
75 |
||
76 |
void Graphics::do_redraw (void) { |
|
77 |
CL_GraphicContext *gc = win.get_gc(); |
|
78 |
||
79 |
// white background |
|
80 |
gc->clear(CL_Color::white); |
|
81 |
||
60 | 82 |
const float factorX = GRAPHICS_RESOLUTION_WIDTH / MAP_WIDTH; |
83 |
const float factorY = GRAPHICS_RESOLUTION_HEIGHT / MAP_HEIGHT; |
|
84 |
||
86 | 85 |
// draw terrain |
86 |
terrain.draw(0,0, gc); |
|
87 |
||
25 | 88 |
// draw players |
89 |
for (std::list<Player*>::iterator it = state.player_list.begin(); it != state.player_list.end(); it++) { |
|
90 |
Player *p = *it; |
|
91 |
||
92 |
// draw square |
|
93 |
gc->fill_rect( |
|
94 |
CL_Rect( |
|
60 | 95 |
p->getPosition().x * factorX - 5, p->getPosition().y * factorY - 5, |
96 |
p->getPosition().x * factorX + 5, p->getPosition().y * factorY + 5 |
|
25 | 97 |
), CL_Color::black |
98 |
); |
|
99 |
} |
|
100 |
||
101 |
// flip window buffer, LIEK NAO |
|
102 |
win.flip(0); |
|
103 |
} |
|
104 |
||
105 |
void Graphics::on_update (void) { |
|
106 |
// check keyboard input |
|
107 |
check_input(); |
|
108 |
||
109 |
// redraw display |
|
110 |
do_redraw(); |
|
111 |
} |