25
|
1 |
|
|
2 |
#include "Graphics.hh"
|
41
|
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),
|
35
|
10 |
win(GRAPHICS_WINDOW_TITLE, GRAPHICS_RESOLUTION_WIDTH, GRAPHICS_RESOLUTION_HEIGHT),
|
25
|
11 |
keyboard(win.get_ic()->get_keyboard()) {
|
41
|
12 |
|
|
13 |
Vector tmp;
|
|
14 |
CL_Color color;
|
|
15 |
CL_PixelBuffer terr(MAP_WIDTH, MAP_HEIGHT, 3*MAP_WIDTH, CL_PixelFormat::rgb888);
|
25
|
16 |
|
41
|
17 |
|
|
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) {
|
|
21 |
color = CL_Color(80, 35, 0);
|
|
22 |
} else if (state.getType(tmp) == DIRT) {
|
|
23 |
color = CL_Color(144, 82, 23);
|
|
24 |
} else if (state.getType(tmp) == ROCK) {
|
|
25 |
color = CL_Color(132, 136, 135);
|
|
26 |
} else {
|
|
27 |
// Fale
|
|
28 |
}
|
|
29 |
terr.draw_pixel(tmp.x, tmp.y, color);
|
|
30 |
}
|
|
31 |
}
|
|
32 |
terrain = CL_Surface(terr);
|
|
33 |
|
|
34 |
Engine::log(DEBUG, "Graphics") << "Taalla ollaan.";
|
|
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;
|
35
|
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))
|
35
|
60 |
input_move |= INPUT_MOVE_UP;
|
25
|
61 |
|
|
62 |
if (keyboard.get_keycode(CL_KEY_DOWN))
|
35
|
63 |
input_move |= INPUT_MOVE_DOWN;
|
25
|
64 |
|
|
65 |
if (keyboard.get_keycode(CL_KEY_LEFT))
|
35
|
66 |
input_move |= INPUT_MOVE_LEFT;
|
25
|
67 |
|
|
68 |
if (keyboard.get_keycode(CL_KEY_RIGHT))
|
35
|
69 |
input_move |= INPUT_MOVE_RIGHT;
|
25
|
70 |
|
|
71 |
// apply movement if applicable
|
35
|
72 |
if (input_move)
|
|
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 |
|
35
|
82 |
const float factorX = GRAPHICS_RESOLUTION_WIDTH / MAP_WIDTH;
|
|
83 |
const float factorY = GRAPHICS_RESOLUTION_HEIGHT / MAP_HEIGHT;
|
|
84 |
|
41
|
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(
|
35
|
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 |
}
|