terom@25: terom@25: #include "Graphics.hh" saiam@86: #include "Physics.hh" saiam@86: #include "GameState.hh" terom@25: terom@25: Graphics::Graphics (Engine &engine, GameState &state) : terom@25: engine(engine), terom@25: state(state), terom@25: update_timer(GRAPHICS_UPDATE_INTERVAL_MS), terom@60: win(GRAPHICS_WINDOW_TITLE, GRAPHICS_RESOLUTION_WIDTH, GRAPHICS_RESOLUTION_HEIGHT), terom@25: keyboard(win.get_ic()->get_keyboard()) { saiam@86: saiam@86: Vector tmp; saiam@86: CL_Color color; ekku@90: CL_PixelBuffer terr(MAP_WIDTH, MAP_HEIGHT, 4*MAP_WIDTH, CL_PixelFormat::rgba8888); saiam@87: saiam@87: saiam@86: for (tmp.x = 0; tmp.x < MAP_WIDTH; tmp.x++) { saiam@86: for (tmp.y = 0; tmp.y < MAP_HEIGHT; tmp.y++) { saiam@86: if (state.getType(tmp) == EMPTY) { ekku@90: color = CL_Color(86, 41, 0); saiam@86: } else if (state.getType(tmp) == DIRT) { ekku@88: color = CL_Color(144, 82, 23); saiam@86: } else if (state.getType(tmp) == ROCK) { ekku@88: color = CL_Color(132, 136, 135); saiam@86: } else { saiam@86: // Fale saiam@86: } saiam@86: terr.draw_pixel(tmp.x, tmp.y, color); ekku@90: } saiam@86: } saiam@87: terrain = CL_Surface(terr); saiam@87: terom@25: // connect timer signal terom@25: slots.connect(update_timer.sig_timer(), this, &Graphics::on_update); terom@25: terom@25: // enable terom@25: update_timer.enable(); terom@25: } terom@25: terom@25: void Graphics::check_input (void) { terom@25: LocalPlayer *player; terom@58: PlayerInput_Move input_move = 0; terom@25: terom@25: // stop on escape terom@25: if (keyboard.get_keycode(CL_KEY_ESCAPE)) { terom@25: engine.stop(); terom@25: terom@25: return; terom@25: } terom@25: terom@25: // ignore if we don't have a local player terom@25: if ((player = state.getLocalPlayer()) == NULL) terom@25: return; terom@25: terom@25: // handle up/down/left/right terom@25: if (keyboard.get_keycode(CL_KEY_UP)) terom@54: input_move |= INPUT_MOVE_UP; terom@25: terom@25: if (keyboard.get_keycode(CL_KEY_DOWN)) terom@54: input_move |= INPUT_MOVE_DOWN; terom@25: terom@25: if (keyboard.get_keycode(CL_KEY_LEFT)) terom@54: input_move |= INPUT_MOVE_LEFT; terom@25: terom@25: if (keyboard.get_keycode(CL_KEY_RIGHT)) terom@54: input_move |= INPUT_MOVE_RIGHT; ekku@94: ekku@94: if (keyboard.get_keycode(CL_KEY_I)) ekku@94: player->debugInfo(); terom@25: terom@25: // apply movement if applicable terom@50: if (input_move) terom@50: player->handleMove(input_move); terom@25: } terom@25: terom@25: void Graphics::do_redraw (void) { terom@25: CL_GraphicContext *gc = win.get_gc(); terom@25: terom@25: // white background terom@25: gc->clear(CL_Color::white); terom@25: terom@60: const float factorX = GRAPHICS_RESOLUTION_WIDTH / MAP_WIDTH; terom@60: const float factorY = GRAPHICS_RESOLUTION_HEIGHT / MAP_HEIGHT; terom@60: saiam@86: // draw terrain saiam@86: terrain.draw(0,0, gc); saiam@86: terom@25: // draw players terom@25: for (std::list::iterator it = state.player_list.begin(); it != state.player_list.end(); it++) { terom@25: Player *p = *it; terom@25: ekku@91: Vector loc = p->getPosition(); ekku@91: std::vector &shape = p->getShape(); ekku@91: // draw quad ekku@91: gc->fill_quad( ekku@91: CL_Quad( ekku@94: (loc.x+shape[0].x) * factorX, (loc.y+shape[0].y) * factorY, ekku@94: (loc.x+shape[1].x) * factorX, (loc.y+shape[1].y) * factorY, ekku@94: (loc.x+shape[2].x) * factorX, (loc.y+shape[2].y) * factorY, ekku@94: (loc.x+shape[3].x) * factorX, (loc.y+shape[3].y) * factorY ekku@91: ), CL_Color::green terom@25: ); terom@25: } terom@25: terom@25: // flip window buffer, LIEK NAO terom@25: win.flip(0); terom@25: } terom@25: terom@25: void Graphics::on_update (void) { terom@25: // check keyboard input terom@25: check_input(); terom@25: terom@25: // redraw display terom@25: do_redraw(); terom@25: }