terom@25: terom@25: #include "Graphics.hh" terom@41: #include "Physics.hh" terom@41: #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@35: win(GRAPHICS_WINDOW_TITLE, GRAPHICS_RESOLUTION_WIDTH, GRAPHICS_RESOLUTION_HEIGHT), terom@25: keyboard(win.get_ic()->get_keyboard()) { terom@41: terom@41: Vector tmp; terom@41: CL_Color color; terom@41: CL_PixelBuffer terr(MAP_WIDTH, MAP_HEIGHT, 3*MAP_WIDTH, CL_PixelFormat::rgb888); terom@25: terom@41: terom@41: for (tmp.x = 0; tmp.x < MAP_WIDTH; tmp.x++) { terom@41: for (tmp.y = 0; tmp.y < MAP_HEIGHT; tmp.y++) { terom@41: if (state.getType(tmp) == EMPTY) { terom@41: color = CL_Color(80, 35, 0); terom@41: } else if (state.getType(tmp) == DIRT) { terom@41: color = CL_Color(144, 82, 23); terom@41: } else if (state.getType(tmp) == ROCK) { terom@41: color = CL_Color(132, 136, 135); terom@41: } else { terom@41: // Fale terom@41: } terom@41: terr.draw_pixel(tmp.x, tmp.y, color); terom@41: } terom@41: } terom@41: terrain = CL_Surface(terr); terom@41: terom@41: Engine::log(DEBUG, "Graphics") << "Taalla ollaan."; terom@41: 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@35: 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@35: input_move |= INPUT_MOVE_UP; terom@25: terom@25: if (keyboard.get_keycode(CL_KEY_DOWN)) terom@35: input_move |= INPUT_MOVE_DOWN; terom@25: terom@25: if (keyboard.get_keycode(CL_KEY_LEFT)) terom@35: input_move |= INPUT_MOVE_LEFT; terom@25: terom@25: if (keyboard.get_keycode(CL_KEY_RIGHT)) terom@35: input_move |= INPUT_MOVE_RIGHT; terom@25: terom@25: // apply movement if applicable terom@35: if (input_move) terom@35: 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@35: const float factorX = GRAPHICS_RESOLUTION_WIDTH / MAP_WIDTH; terom@35: const float factorY = GRAPHICS_RESOLUTION_HEIGHT / MAP_HEIGHT; terom@35: terom@41: // draw terrain terom@41: terrain.draw(0,0, gc); terom@41: 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: terom@25: // draw square terom@25: gc->fill_rect( terom@25: CL_Rect( terom@35: p->getPosition().x * factorX - 5, p->getPosition().y * factorY - 5, terom@35: p->getPosition().x * factorX + 5, p->getPosition().y * factorY + 5 terom@25: ), CL_Color::black 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: }