src/Graphics.cc
changeset 394 82def222fe7d
parent 393 5dd4d782cf3a
child 408 e6cfc44266af
equal deleted inserted replaced
393:5dd4d782cf3a 394:82def222fe7d
    15             resolution.y
    15             resolution.y
    16     );
    16     );
    17 }
    17 }
    18 
    18 
    19 Graphics::Graphics (Engine &engine, GameState &state, PixelCoordinate resolution, bool fullscreen) :
    19 Graphics::Graphics (Engine &engine, GameState &state, PixelCoordinate resolution, bool fullscreen) :
    20     CL_DisplayWindow(GRAPHICS_WINDOW_TITLE, resolution.x, resolution.y, fullscreen),
    20     CL_DisplayWindow(GRAPHICS_WINDOW_TITLE, resolution.x, resolution.y, fullscreen, true),
    21     engine(engine), 
    21     engine(engine), 
    22     state(state), 
    22     state(state), 
    23     resolution(resolution),
    23     resolution(resolution),
       
    24     fullscreen_resolution(0, 0), window_resolution(0, 0),
    24     update_timer(GRAPHICS_UPDATE_INTERVAL_MS),
    25     update_timer(GRAPHICS_UPDATE_INTERVAL_MS),
    25     input(get_ic()->get_keyboard()),
    26     input(get_ic()->get_keyboard()),
    26     simple_font("Font2", engine.getResourceManager()),
    27     simple_font("Font2", engine.getResourceManager()),
    27     message_view(getMessageViewArea(resolution))
    28     message_view(getMessageViewArea(resolution))
    28 {
    29 {
    29 
    30 
    30     // connect timer signal
    31     // connect timer signal
    31     slots.connect(update_timer.sig_tick(), this, &Graphics::on_update);
    32     slots.connect(update_timer.sig_tick(), this, &Graphics::on_update);
       
    33     slots.connect(this->sig_resize(), this, &Graphics::on_window_resize);
    32 
    34 
    33     // enable
    35     // enable
    34     update_timer.start();
    36     update_timer.start();
    35 
    37 
    36     // push something to message_view
    38     // push something to message_view
    37     message_view.add_message(CL_Color::white, "Hello World"); 
    39     message_view.add_message(CL_Color::white, "Hello World"); 
    38 
    40 
    39     // GameState events....
    41     // GameState events....
    40     state.setEventHandler(this);
    42     state.setEventHandler(this);
       
    43 
       
    44     // set correct resolution
       
    45     if (fullscreen) {
       
    46         fullscreen_resolution = resolution;
       
    47 
       
    48     } else {
       
    49         CL_DisplayMode best_mode = getBestMode();
       
    50 
       
    51         fullscreen_resolution = PixelCoordinate(best_mode.get_resolution().width, best_mode.get_resolution().height);
       
    52         window_resolution = resolution;
       
    53     }
       
    54 
    41 }
    55 }
    42 
    56 
    43 const std::vector<CL_DisplayMode> & Graphics::getDisplayModes (void) {
    57 const std::vector<CL_DisplayMode> & Graphics::getDisplayModes (void) {
    44     return CL_DisplayMode::get_display_modes();
    58     return CL_DisplayMode::get_display_modes();
    45 }
    59 }
    80     // apply input if there was any
    94     // apply input if there was any
    81     if (input_mask)
    95     if (input_mask)
    82         player->handleInput(input_mask, input_dt);
    96         player->handleInput(input_mask, input_dt);
    83 }
    97 }
    84     
    98     
       
    99 void Graphics::toggle_fullscreen (void) {
       
   100     if (is_fullscreen()) {
       
   101         // remember current fullscreen resolution
       
   102         fullscreen_resolution = resolution;
       
   103         
       
   104         // enter windowed mode
       
   105         set_windowed();
       
   106 
       
   107 /*        
       
   108         // do we have a window-mode resolution stored?
       
   109         if (window_resolution.x && window_resolution.y)
       
   110             set_size(window_resolution.x, window_resolution.y);
       
   111 */
       
   112     } else {
       
   113         // remember current window resolution
       
   114         window_resolution = resolution;
       
   115 
       
   116         // enter fullscreen mode
       
   117         set_fullscreen(fullscreen_resolution.x, fullscreen_resolution.y);
       
   118         
       
   119         // update resolution
       
   120         resolution = fullscreen_resolution;
       
   121         
       
   122         // log
       
   123         message_view.add_message(CL_Color::yellow, CL_String::format("[ Enter fullscreen mode with %1 x %2 resolution ]", 
       
   124                 (int) resolution.x, (int) resolution.y));
       
   125     }
       
   126 }
       
   127 
    85 void Graphics::handle_input (GuiInput flags) {
   128 void Graphics::handle_input (GuiInput flags) {
    86     // update flags
   129     // update flags
    87     this->flags = flags;
   130     this->flags = flags;
    88 
   131 
    89     // quit?
   132     // quit?
    99         
   142         
   100         message_view.add_message(CL_Color::green, "...");
   143         message_view.add_message(CL_Color::green, "...");
   101     }
   144     }
   102     
   145     
   103     // toggle fullscreen?
   146     // toggle fullscreen?
   104     if (flags & GUI_INPUT_TOGGLE_FULLSCREEN) {
   147     if (flags & GUI_INPUT_TOGGLE_FULLSCREEN)
   105         if (is_fullscreen())
   148         toggle_fullscreen();
   106             set_windowed();
       
   107         else
       
   108             set_fullscreen();
       
   109     }
       
   110 }
   149 }
   111 
   150 
   112 static PixelDimension value_between (PixelDimension low, PixelDimension value, PixelDimension high) {
   151 static PixelDimension value_between (PixelDimension low, PixelDimension value, PixelDimension high) {
   113     if (high < low)
   152     if (high < low)
   114         return (high + low) / 2;
   153         return (high + low) / 2;
   287         get_gc()
   326         get_gc()
   288     );
   327     );
   289 
   328 
   290 }
   329 }
   291 
   330 
       
   331 void Graphics::on_window_resize (int new_x, int new_y) {
       
   332     // ignore resize in fullscreen mode
       
   333     if (is_fullscreen())
       
   334         return;
       
   335     
       
   336     // update resolution
       
   337     resolution = PixelCoordinate(new_x, new_y);
       
   338     
       
   339     // resize components
       
   340     message_view.on_resize(getMessageViewArea(resolution));
       
   341     
       
   342     // log message
       
   343     message_view.add_message(CL_Color::yellow, CL_String::format("[ Resized window to %1 x %2 ]", new_x, new_y));
       
   344 }
   292     
   345     
   293 void Graphics::on_player_joined (Player *p) {
   346 void Graphics::on_player_joined (Player *p) {
   294     message_view.add_message(CL_Color::white, " *** Player joined");
   347     message_view.add_message(CL_Color::white, " *** Player joined");
   295 }
   348 }
   296     
   349