| author | terom |
| Mon, 24 Nov 2008 22:09:29 +0000 | |
| changeset 105 | 91e3f3806b31 |
| parent 96 | 4a801210096c |
| child 108 | 1b93045a5b0a |
| 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 |
||
|
96
4a801210096c
fix movement physics+network code to some degree, jumping is now buggy?
terom
parents:
94
diff
changeset
|
56 |
// handle movement |
| 25 | 57 |
if (keyboard.get_keycode(CL_KEY_LEFT)) |
| 54 | 58 |
input_move |= INPUT_MOVE_LEFT; |
| 25 | 59 |
|
60 |
if (keyboard.get_keycode(CL_KEY_RIGHT)) |
|
| 54 | 61 |
input_move |= INPUT_MOVE_RIGHT; |
| 94 | 62 |
|
|
96
4a801210096c
fix movement physics+network code to some degree, jumping is now buggy?
terom
parents:
94
diff
changeset
|
63 |
if (keyboard.get_keycode(CL_KEY_RSHIFT)) |
|
4a801210096c
fix movement physics+network code to some degree, jumping is now buggy?
terom
parents:
94
diff
changeset
|
64 |
input_move |= INPUT_MOVE_JUMP; |
|
4a801210096c
fix movement physics+network code to some degree, jumping is now buggy?
terom
parents:
94
diff
changeset
|
65 |
|
| 94 | 66 |
if (keyboard.get_keycode(CL_KEY_I)) |
67 |
player->debugInfo(); |
|
| 25 | 68 |
|
69 |
// apply movement if applicable |
|
|
50
9e1a6506f5a1
some rough-handed code modifications towards a newer, better, working Physics
terom
parents:
25
diff
changeset
|
70 |
if (input_move) |
|
9e1a6506f5a1
some rough-handed code modifications towards a newer, better, working Physics
terom
parents:
25
diff
changeset
|
71 |
player->handleMove(input_move); |
| 25 | 72 |
} |
73 |
||
74 |
void Graphics::do_redraw (void) {
|
|
75 |
CL_GraphicContext *gc = win.get_gc(); |
|
76 |
||
77 |
// white background |
|
78 |
gc->clear(CL_Color::white); |
|
79 |
||
| 60 | 80 |
const float factorX = GRAPHICS_RESOLUTION_WIDTH / MAP_WIDTH; |
81 |
const float factorY = GRAPHICS_RESOLUTION_HEIGHT / MAP_HEIGHT; |
|
82 |
||
| 86 | 83 |
// draw terrain |
84 |
terrain.draw(0,0, gc); |
|
85 |
||
| 25 | 86 |
// draw players |
87 |
for (std::list<Player*>::iterator it = state.player_list.begin(); it != state.player_list.end(); it++) {
|
|
88 |
Player *p = *it; |
|
89 |
||
| 91 | 90 |
Vector loc = p->getPosition(); |
91 |
std::vector<Vector> &shape = p->getShape(); |
|
92 |
// draw quad |
|
93 |
gc->fill_quad( |
|
94 |
CL_Quad( |
|
| 94 | 95 |
(loc.x+shape[0].x) * factorX, (loc.y+shape[0].y) * factorY, |
96 |
(loc.x+shape[1].x) * factorX, (loc.y+shape[1].y) * factorY, |
|
97 |
(loc.x+shape[2].x) * factorX, (loc.y+shape[2].y) * factorY, |
|
98 |
(loc.x+shape[3].x) * factorX, (loc.y+shape[3].y) * factorY |
|
| 91 | 99 |
), CL_Color::green |
| 25 | 100 |
); |
101 |
} |
|
102 |
||
103 |
// flip window buffer, LIEK NAO |
|
104 |
win.flip(0); |
|
105 |
} |
|
106 |
||
107 |
void Graphics::on_update (void) {
|
|
108 |
// check keyboard input |
|
109 |
check_input(); |
|
110 |
||
111 |
// redraw display |
|
112 |
do_redraw(); |
|
113 |
} |