terom@25: terom@25: #include "Graphics.hh" saiam@86: #include "GameState.hh" saiam@108: #include terom@25: terom@25: Graphics::Graphics (Engine &engine, GameState &state) : terom@233: CL_DisplayWindow(GRAPHICS_WINDOW_TITLE, GRAPHICS_RESOLUTION_WIDTH, GRAPHICS_RESOLUTION_HEIGHT), terom@25: engine(engine), terom@25: state(state), terom@282: resolution(GRAPHICS_RESOLUTION_WIDTH, GRAPHICS_RESOLUTION_HEIGHT), terom@25: update_timer(GRAPHICS_UPDATE_INTERVAL_MS), terom@235: input(get_ic()->get_keyboard()), terom@233: simple_font("Font2", engine.getResourceManager()) terom@233: { saiam@86: terom@25: // connect timer signal terom@205: slots.connect(update_timer.sig_tick(), this, &Graphics::on_update); terom@25: terom@25: // enable terom@205: update_timer.start(); terom@25: } terom@25: saiam@275: void Graphics::check_input (TimeMS dt) { terom@25: LocalPlayer *player; terom@221: PlayerInput input_mask = 0; terom@25: terom@233: // update gui flags terom@235: this->flags = input.readGuiInput(); terom@233: terom@233: // quit? terom@233: if (flags & GUI_INPUT_QUIT) { saiam@108: engine.stop(); terom@25: saiam@108: return; terom@25: } terom@25: terom@233: // stop here if we don't have a local player terom@25: if ((player = state.getLocalPlayer()) == NULL) terom@25: return; terom@25: terom@221: // dump debug info on stderr terom@233: if (flags & GUI_INPUT_DEBUG_PLAYER) terom@221: player->printDebugInfo(); ekku@180: terom@230: // build input_mask terom@235: input_mask = input.readPlayerInput(); terom@221: terom@233: // apply input if there was any terom@221: if (input_mask) saiam@275: player->handleInput(input_mask, dt); terom@25: } terom@25: terom@266: static PixelDimension value_between (PixelDimension low, PixelDimension value, PixelDimension high) { terom@266: if (value < low) terom@266: return low; terom@266: terom@266: else if (value > high) terom@266: return high; terom@266: terom@266: else terom@266: return value; terom@266: } terom@266: terom@25: void Graphics::do_redraw (void) { terom@233: CL_GraphicContext *gc = get_gc(); terom@266: LocalPlayer *player; terom@266: terom@266: // calculate camera terom@266: PixelCoordinate camera(0, 0); terom@266: terom@266: // ...to track our local player terom@266: if ((player = state.getLocalPlayer()) != NULL) { terom@266: PixelCoordinate target = player->getCoordinate() - resolution / 2; terom@266: PixelCoordinate max = state.world.getDimensions() - resolution; terom@266: terom@266: // keep the terrain in view terom@266: camera = PixelCoordinate( terom@266: value_between(0, target.x, max.x), terom@266: value_between(0, target.y, max.y) terom@266: ); terom@266: } terom@25: saiam@162: // White background nireco@248: gc->clear(CL_Color::black); terom@25: terom@233: // Draw the game terom@266: state.draw(this, camera, flags & GUI_INPUT_DISPLAY_WEAPON); ekku@180: terom@184: // Flip window buffer, sync terom@233: flip(1); terom@25: } terom@25: terom@205: void Graphics::on_update (TimeMS tick_length) { terom@25: // check keyboard input saiam@275: check_input(tick_length); terom@25: terom@25: // redraw display terom@25: do_redraw(); terom@25: }