author | nireco |
Fri, 28 Nov 2008 18:11:23 +0000 | |
changeset 125 | 8e15c7db2333 |
parent 116 | 0d36aade845e |
child 138 | cc326b64ae20 |
permissions | -rw-r--r-- |
25 | 1 |
|
2 |
#include "Graphics.hh" |
|
86 | 3 |
#include "Physics.hh" |
4 |
#include "GameState.hh" |
|
108 | 5 |
#include <cmath> |
25 | 6 |
|
7 |
Graphics::Graphics (Engine &engine, GameState &state) : |
|
8 |
engine(engine), |
|
9 |
state(state), |
|
10 |
update_timer(GRAPHICS_UPDATE_INTERVAL_MS), |
|
60 | 11 |
win(GRAPHICS_WINDOW_TITLE, GRAPHICS_RESOLUTION_WIDTH, GRAPHICS_RESOLUTION_HEIGHT), |
25 | 12 |
keyboard(win.get_ic()->get_keyboard()) { |
86 | 13 |
|
14 |
Vector tmp; |
|
15 |
CL_Color color; |
|
90 | 16 |
CL_PixelBuffer terr(MAP_WIDTH, MAP_HEIGHT, 4*MAP_WIDTH, CL_PixelFormat::rgba8888); |
87 | 17 |
|
18 |
||
86 | 19 |
for (tmp.x = 0; tmp.x < MAP_WIDTH; tmp.x++) { |
20 |
for (tmp.y = 0; tmp.y < MAP_HEIGHT; tmp.y++) { |
|
21 |
if (state.getType(tmp) == EMPTY) { |
|
90 | 22 |
color = CL_Color(86, 41, 0); |
86 | 23 |
} else if (state.getType(tmp) == DIRT) { |
88 | 24 |
color = CL_Color(144, 82, 23); |
86 | 25 |
} else if (state.getType(tmp) == ROCK) { |
88 | 26 |
color = CL_Color(132, 136, 135); |
86 | 27 |
} else { |
28 |
// Fale |
|
29 |
} |
|
30 |
terr.draw_pixel(tmp.x, tmp.y, color); |
|
90 | 31 |
} |
86 | 32 |
} |
87 | 33 |
terrain = CL_Surface(terr); |
34 |
||
25 | 35 |
// connect timer signal |
36 |
slots.connect(update_timer.sig_timer(), this, &Graphics::on_update); |
|
37 |
||
38 |
// enable |
|
39 |
update_timer.enable(); |
|
40 |
} |
|
41 |
||
42 |
void Graphics::check_input (void) { |
|
43 |
LocalPlayer *player; |
|
58 | 44 |
PlayerInput_Move input_move = 0; |
25 | 45 |
|
46 |
// stop on escape |
|
47 |
if (keyboard.get_keycode(CL_KEY_ESCAPE)) { |
|
108 | 48 |
engine.stop(); |
25 | 49 |
|
108 | 50 |
return; |
25 | 51 |
} |
52 |
||
53 |
// ignore if we don't have a local player |
|
54 |
if ((player = state.getLocalPlayer()) == NULL) |
|
55 |
return; |
|
56 |
||
96
4a801210096c
fix movement physics+network code to some degree, jumping is now buggy?
terom
parents:
94
diff
changeset
|
57 |
// handle movement |
25 | 58 |
if (keyboard.get_keycode(CL_KEY_LEFT)) |
108 | 59 |
input_move |= INPUT_MOVE_LEFT; |
25 | 60 |
|
61 |
if (keyboard.get_keycode(CL_KEY_RIGHT)) |
|
108 | 62 |
input_move |= INPUT_MOVE_RIGHT; |
63 |
||
64 |
if (keyboard.get_keycode(CL_KEY_UP)) |
|
65 |
input_move |= INPUT_MOVE_UP; |
|
66 |
||
67 |
if (keyboard.get_keycode(CL_KEY_DOWN)) |
|
68 |
input_move |= INPUT_MOVE_DOWN; |
|
94 | 69 |
|
96
4a801210096c
fix movement physics+network code to some degree, jumping is now buggy?
terom
parents:
94
diff
changeset
|
70 |
if (keyboard.get_keycode(CL_KEY_RSHIFT)) |
108 | 71 |
input_move |= INPUT_MOVE_JUMP; |
96
4a801210096c
fix movement physics+network code to some degree, jumping is now buggy?
terom
parents:
94
diff
changeset
|
72 |
|
108 | 73 |
if (keyboard.get_keycode(CL_KEY_I)) |
74 |
player->debugInfo(); |
|
116 | 75 |
|
76 |
if (keyboard.get_keycode(CL_KEY_M)) |
|
77 |
input_move |= INPUT_MOVE_DIG; |
|
78 |
||
25 | 79 |
// apply movement if applicable |
50
9e1a6506f5a1
some rough-handed code modifications towards a newer, better, working Physics
terom
parents:
25
diff
changeset
|
80 |
if (input_move) |
9e1a6506f5a1
some rough-handed code modifications towards a newer, better, working Physics
terom
parents:
25
diff
changeset
|
81 |
player->handleMove(input_move); |
25 | 82 |
} |
83 |
||
84 |
void Graphics::do_redraw (void) { |
|
85 |
CL_GraphicContext *gc = win.get_gc(); |
|
86 |
||
87 |
// white background |
|
88 |
gc->clear(CL_Color::white); |
|
89 |
||
60 | 90 |
const float factorX = GRAPHICS_RESOLUTION_WIDTH / MAP_WIDTH; |
91 |
const float factorY = GRAPHICS_RESOLUTION_HEIGHT / MAP_HEIGHT; |
|
92 |
||
86 | 93 |
// draw terrain |
94 |
terrain.draw(0,0, gc); |
|
95 |
||
125 | 96 |
// Demonstrates digging, but is very slow |
97 |
/* Vector tmp(0, 0); |
|
98 |
CL_Color color; |
|
99 |
CL_PixelBuffer pix(1, 1, 4, CL_PixelFormat::rgba8888); |
|
100 |
CL_Surface surf(pix); |
|
101 |
for (tmp.x = 380; tmp.x < 430; tmp.x++) { |
|
102 |
for (tmp.y = 560; tmp.y < 600; tmp.y++) { |
|
103 |
if (state.getType(tmp) == EMPTY) { |
|
104 |
color = CL_Color(86, 41, 0); |
|
105 |
} else if (state.getType(tmp) == DIRT) { |
|
106 |
color = CL_Color(144, 82, 23); |
|
107 |
} else if (state.getType(tmp) == ROCK) { |
|
108 |
color = CL_Color(132, 136, 135); |
|
109 |
} else { |
|
110 |
// Fale |
|
111 |
} |
|
112 |
surf.set_color(color); |
|
113 |
surf.draw(tmp.x, tmp.y, gc); |
|
114 |
} |
|
115 |
}*/ |
|
116 |
||
25 | 117 |
// draw players |
118 |
for (std::list<Player*>::iterator it = state.player_list.begin(); it != state.player_list.end(); it++) { |
|
119 |
Player *p = *it; |
|
120 |
||
108 | 121 |
Vector loc = p->getPosition(); |
122 |
bool right = p->getFacing(); |
|
123 |
float aim = p->getAim(); |
|
124 |
std::vector<Vector> &shape = p->getShape(); |
|
91 | 125 |
// draw quad |
126 |
gc->fill_quad( |
|
108 | 127 |
CL_Quad( |
128 |
(loc.x+shape[0].x) * factorX, (loc.y+shape[0].y) * factorY, |
|
129 |
(loc.x+shape[1].x) * factorX, (loc.y+shape[1].y) * factorY, |
|
130 |
(loc.x+shape[2].x) * factorX, (loc.y+shape[2].y) * factorY, |
|
131 |
(loc.x+shape[3].x) * factorX, (loc.y+shape[3].y) * factorY |
|
132 |
), CL_Color::green |
|
133 |
); |
|
134 |
||
135 |
// Draw crosshair |
|
136 |
if (right) { |
|
137 |
gc->draw_line(loc.x, loc.y, loc.x + std::cos(aim)*5, loc.y - std::sin(aim)*5, CL_Color::black); |
|
138 |
} else { |
|
139 |
gc->draw_line(loc.x, loc.y, loc.x - std::cos(aim)*5, loc.y - std::sin(aim)*5, CL_Color::black); |
|
140 |
} |
|
25 | 141 |
} |
142 |
||
143 |
// flip window buffer, LIEK NAO |
|
144 |
win.flip(0); |
|
145 |
} |
|
146 |
||
147 |
void Graphics::on_update (void) { |
|
148 |
// check keyboard input |
|
149 |
check_input(); |
|
150 |
||
151 |
// redraw display |
|
152 |
do_redraw(); |
|
153 |
} |