terom@410: terom@410: #include "Display.hh" terom@412: #include "../Error.hh" terom@410: terom@410: namespace graphics terom@410: { terom@410: terom@411: Display::Display (const DisplayConfig &config) : terom@411: CL_DisplayWindow(GRAPHICS_WINDOW_TITLE, config.resolution.width, config.resolution.height, config.fullscreen, true), terom@411: config(config), terom@411: fullscreen_resolution(0, 0), terom@411: update_timer(GRAPHICS_UPDATE_INTERVAL_MS), terom@413: current_view(NULL) terom@411: { terom@411: // connect timer signal terom@411: slots.connect(update_timer.sig_tick(), this, &Display::on_update); terom@411: slots.connect(this->sig_resize(), this, &Display::on_window_resize); terom@411: terom@413: // enable timer terom@413: update_timer.start(); terom@413: terom@411: // set correct fullscreen resolution terom@411: if (config.fullscreen) { terom@411: fullscreen_resolution = config.resolution; terom@411: terom@411: } else { terom@411: CL_DisplayMode best_mode = getBestMode(); terom@411: terom@412: fullscreen_resolution = PixelDimensions(best_mode.get_resolution().width, best_mode.get_resolution().height); terom@411: } terom@411: } terom@411: terom@411: const std::vector & Display::getDisplayModes (void) { terom@410: return CL_DisplayMode::get_display_modes(); terom@410: } terom@410: terom@411: const CL_DisplayMode Display::getBestMode (void) { terom@412: const std::vector &modes = getDisplayModes(); terom@410: terom@410: const CL_DisplayMode *best_mode = NULL; terom@410: terom@410: for (std::vector::const_iterator it = modes.begin(); it != modes.end(); it++) terom@410: if (best_mode == NULL || ( terom@410: it->get_resolution().width * it->get_resolution().height > terom@410: best_mode->get_resolution().width * best_mode->get_resolution().height terom@410: )) terom@410: best_mode = &*it; terom@410: terom@410: if (best_mode == NULL) terom@410: throw Error("No available video modes!"); terom@410: terom@410: return *best_mode; terom@410: } terom@410: terom@413: void Display::setView (View *view) { terom@413: this->current_view = view; terom@413: terom@413: // resize to fill display terom@413: if (view) terom@413: view->resize(PixelArea(0, 0, get_width(), get_height())); terom@413: } terom@413: terom@414: void Display::toggleFullscreen (void) { terom@411: if (is_fullscreen()) { terom@411: // enter windowed mode terom@411: set_windowed(); terom@411: terom@411: } else { terom@411: // enter fullscreen mode terom@411: set_fullscreen(fullscreen_resolution.width, fullscreen_resolution.height); terom@411: } terom@410: } terom@410: terom@411: void Display::on_update (TimeMS dt) { terom@411: (void) dt; terom@411: terom@411: // do we have something to draw? terom@413: if (current_view) { terom@411: // draw it terom@413: current_view->draw(*this); terom@411: terom@411: // flip buffers, sync terom@411: flip(1); terom@411: } terom@411: } terom@411: terom@411: void Display::on_window_resize (int new_x, int new_y) { terom@412: (void) new_x; terom@412: (void) new_y; terom@412: terom@411: // ignore resize in fullscreen mode terom@411: if (is_fullscreen()) terom@411: return; terom@413: terom@413: // resize view terom@413: if (current_view) terom@413: current_view->resize(PixelArea(0, 0, new_x, new_y)); terom@411: } terom@411: terom@411: terom@411: } terom@411: