CVideo: general cleanup

This commit is contained in:
Charles Dang 2016-12-07 01:44:08 +11:00
parent b33fc6a74f
commit ec8ff1ac91
2 changed files with 96 additions and 122 deletions

View file

@ -12,29 +12,19 @@
See the COPYING file for more details. See the COPYING file for more details.
*/ */
/**
* @file
* Video-testprogram, standalone
*/
#include "global.hpp" #include "global.hpp"
#include "font/sdl_ttf.hpp" #include "video.hpp"
#include "display.hpp"
#include "floating_label.hpp" #include "floating_label.hpp"
#include "image.hpp" #include "image.hpp"
#include "log.hpp" #include "log.hpp"
#include "preferences.hpp" #include "preferences.hpp"
#include "sdl/utils.hpp"
#include "sdl/rect.hpp"
#include "sdl/window.hpp" #include "sdl/window.hpp"
#include "video.hpp"
#include "display.hpp"
#include <vector>
#include <map>
#include <algorithm>
#include <cassert> #include <cassert>
#include <vector>
static lg::log_domain log_display("display"); static lg::log_domain log_display("display");
#define LOG_DP LOG_STREAM(info, log_display) #define LOG_DP LOG_STREAM(info, log_display)
@ -42,28 +32,17 @@ static lg::log_domain log_display("display");
CVideo* CVideo::singleton_ = nullptr; CVideo* CVideo::singleton_ = nullptr;
static unsigned int get_flags(unsigned int flags)
{
/* The wanted flags for the render need to be evaluated for SDL2. */
flags |= SDL_WINDOW_RESIZABLE;
return flags;
}
namespace { namespace {
surface frameBuffer = nullptr;
surface frameBuffer = nullptr; bool fake_interactive = false;
bool fake_interactive = false;
} }
namespace video2 { namespace video2 {
std::list<events::sdl_handler *> draw_layers; std::list<events::sdl_handler*> draw_layers;
draw_layering::draw_layering(const bool auto_join) : draw_layering::draw_layering(const bool auto_join)
sdl_handler(auto_join) : sdl_handler(auto_join)
{ {
draw_layers.push_back(this); draw_layers.push_back(this);
} }
@ -82,8 +61,8 @@ void trigger_full_redraw() {
event.window.data1 = (*frameBuffer).h; event.window.data1 = (*frameBuffer).h;
event.window.data2 = (*frameBuffer).w; event.window.data2 = (*frameBuffer).w;
for(std::list<events::sdl_handler*>::iterator it = draw_layers.begin(); it != draw_layers.end(); ++it) { for(const auto& layer : draw_layers) {
(*it)->handle_window_event(event); layer->handle_window_event(event);
} }
SDL_Event drawEvent; SDL_Event drawEvent;
@ -99,63 +78,23 @@ void trigger_full_redraw() {
SDL_FlushEvent(DRAW_ALL_EVENT); SDL_FlushEvent(DRAW_ALL_EVENT);
SDL_PushEvent(&drawEvent); SDL_PushEvent(&drawEvent);
} }
}
} // video2
bool CVideo::non_interactive() CVideo::CVideo(FAKE_TYPES type)
{ : window()
if (fake_interactive) , mode_changed_(false)
return false; , fake_screen_(false)
return window == nullptr; , help_string_(0)
} , updatesLocked_(0)
, flip_locked_(0)
SDL_Rect screen_area()
{
return sdl::create_rect(0, 0, frameBuffer->w, frameBuffer->h);
}
void CVideo::video_event_handler::handle_window_event(const SDL_Event &event)
{
if (event.type == SDL_WINDOWEVENT) {
switch (event.window.event) {
case SDL_WINDOWEVENT_RESIZED:
case SDL_WINDOWEVENT_RESTORED:
case SDL_WINDOWEVENT_SHOWN:
case SDL_WINDOWEVENT_EXPOSED:
//if (display::get_singleton())
//display::get_singleton()->redraw_everything();
SDL_Event drawEvent;
SDL_UserEvent data;
data.type = DRAW_ALL_EVENT;
data.code = 0;
data.data1 = nullptr;
data.data2 = nullptr;
drawEvent.type = DRAW_ALL_EVENT;
drawEvent.user = data;
SDL_FlushEvent(DRAW_ALL_EVENT);
SDL_PushEvent(&drawEvent);
break;
}
}
}
CVideo::CVideo(FAKE_TYPES type) :
window(),
mode_changed_(false),
fake_screen_(false),
help_string_(0),
updatesLocked_(0),
flip_locked_(0)
{ {
assert(!singleton_); assert(!singleton_);
singleton_ = this; singleton_ = this;
initSDL(); initSDL();
switch(type)
{ switch(type) {
case NO_FAKE: case NO_FAKE:
break; break;
case FAKE: case FAKE:
@ -177,7 +116,6 @@ void CVideo::initSDL()
} }
} }
CVideo::~CVideo() CVideo::~CVideo()
{ {
LOG_DP << "calling SDL_Quit()\n"; LOG_DP << "calling SDL_Quit()\n";
@ -187,6 +125,44 @@ CVideo::~CVideo()
LOG_DP << "called SDL_Quit()\n"; LOG_DP << "called SDL_Quit()\n";
} }
bool CVideo::non_interactive()
{
return fake_interactive ? false : (window == nullptr);
}
SDL_Rect screen_area()
{
return {0, 0, frameBuffer->w, frameBuffer->h};
}
void CVideo::video_event_handler::handle_window_event(const SDL_Event &event)
{
if(event.type == SDL_WINDOWEVENT) {
switch (event.window.event) {
case SDL_WINDOWEVENT_RESIZED:
case SDL_WINDOWEVENT_RESTORED:
case SDL_WINDOWEVENT_SHOWN:
case SDL_WINDOWEVENT_EXPOSED:
//if(display::get_singleton())
//display::get_singleton()->redraw_everything();
SDL_Event drawEvent;
SDL_UserEvent data;
data.type = DRAW_ALL_EVENT;
data.code = 0;
data.data1 = nullptr;
data.data2 = nullptr;
drawEvent.type = DRAW_ALL_EVENT;
drawEvent.user = data;
SDL_FlushEvent(DRAW_ALL_EVENT);
SDL_PushEvent(&drawEvent);
break;
}
}
}
void CVideo::blit_surface(int x, int y, surface surf, SDL_Rect* srcrect, SDL_Rect* clip_rect) void CVideo::blit_surface(int x, int y, surface surf, SDL_Rect* srcrect, SDL_Rect* clip_rect)
{ {
surface& target(getSurface()); surface& target(getSurface());
@ -196,43 +172,39 @@ void CVideo::blit_surface(int x, int y, surface surf, SDL_Rect* srcrect, SDL_Rec
sdl_blit(surf,srcrect,target,&dst); sdl_blit(surf,srcrect,target,&dst);
} }
void CVideo::make_fake() void CVideo::make_fake()
{ {
fake_screen_ = true; fake_screen_ = true;
frameBuffer = SDL_CreateRGBSurface(SDL_SWSURFACE,16,16,24,0xFF0000,0xFF00,0xFF,0); frameBuffer = SDL_CreateRGBSurface(SDL_SWSURFACE, 16, 16, 24, 0xFF0000, 0xFF00, 0xFF, 0);
image::set_pixel_format(frameBuffer->format); image::set_pixel_format(frameBuffer->format);
} }
void CVideo::make_test_fake(const unsigned width, void CVideo::make_test_fake(const unsigned width, const unsigned height, const unsigned bpp)
const unsigned height, const unsigned bpp)
{ {
frameBuffer = SDL_CreateRGBSurface(SDL_SWSURFACE, frameBuffer = SDL_CreateRGBSurface(SDL_SWSURFACE, width, height, bpp, 0xFF0000, 0xFF00, 0xFF, 0);
width, height, bpp, 0xFF0000, 0xFF00, 0xFF, 0);
image::set_pixel_format(frameBuffer->format); image::set_pixel_format(frameBuffer->format);
fake_interactive = true; fake_interactive = true;
} }
void CVideo::update_framebuffer() void CVideo::update_framebuffer()
{ {
if (!window) if(!window) {
return; return;
}
surface fb = SDL_GetWindowSurface(*window); surface fb = SDL_GetWindowSurface(*window);
if (!frameBuffer) if(!frameBuffer) {
frameBuffer = fb; frameBuffer = fb;
else } else {
frameBuffer.assign(fb); frameBuffer.assign(fb);
}
} }
/** /**
* Creates a new window instance. * Creates a new window instance.
*/ */
bool CVideo::init_window() void CVideo::init_window()
{ {
// Position // Position
const int x = preferences::fullscreen() ? SDL_WINDOWPOS_UNDEFINED : SDL_WINDOWPOS_CENTERED; const int x = preferences::fullscreen() ? SDL_WINDOWPOS_UNDEFINED : SDL_WINDOWPOS_CENTERED;
@ -245,11 +217,12 @@ bool CVideo::init_window()
// Video flags // Video flags
int video_flags = 0; int video_flags = 0;
video_flags = get_flags(video_flags); // Add any more default flags here
video_flags |= SDL_WINDOW_RESIZABLE;
if (preferences::fullscreen()) { if(preferences::fullscreen()) {
video_flags |= SDL_WINDOW_FULLSCREEN_DESKTOP; video_flags |= SDL_WINDOW_FULLSCREEN_DESKTOP;
} else if (preferences::maximized()) { } else if(preferences::maximized()) {
video_flags |= SDL_WINDOW_MAXIMIZED; video_flags |= SDL_WINDOW_MAXIMIZED;
} }
@ -269,14 +242,12 @@ bool CVideo::init_window()
if(frameBuffer) { if(frameBuffer) {
image::set_pixel_format(frameBuffer->format); image::set_pixel_format(frameBuffer->format);
} }
return true;
} }
void CVideo::setMode(int x, int y, const MODE_EVENT mode) void CVideo::setMode(int x, int y, const MODE_EVENT mode)
{ {
assert(window); assert(window);
if (fake_screen_) return; if(fake_screen_) return;
mode_changed_ = true; mode_changed_ = true;
switch(mode) { switch(mode) {
@ -326,24 +297,29 @@ int CVideo::gety() const
void CVideo::delay(unsigned int milliseconds) void CVideo::delay(unsigned int milliseconds)
{ {
if (!game_config::no_delay) if(!game_config::no_delay) {
SDL_Delay(milliseconds); SDL_Delay(milliseconds);
}
} }
void CVideo::flip() void CVideo::flip()
{ {
if(fake_screen_ || flip_locked_ > 0) if(fake_screen_ || flip_locked_ > 0) {
return; return;
if (window) }
if(window) {
window->render(); window->render();
}
} }
void CVideo::lock_updates(bool value) void CVideo::lock_updates(bool value)
{ {
if(value == true) if(value == true) {
++updatesLocked_; ++updatesLocked_;
else } else {
--updatesLocked_; --updatesLocked_;
}
} }
bool CVideo::update_locked() const bool CVideo::update_locked() const
@ -368,16 +344,16 @@ sdl::window *CVideo::get_window()
return window.get(); return window.get();
} }
std::vector<std::pair<int, int> > CVideo::get_available_resolutions(const bool include_current) std::vector<std::pair<int, int>> CVideo::get_available_resolutions(const bool include_current)
{ {
std::vector<std::pair<int, int> > result; std::vector<std::pair<int, int>> result;
if(!window) { if(!window) {
return result; return result;
} }
const int modes = SDL_GetNumDisplayModes(window->get_display_index()); const int modes = SDL_GetNumDisplayModes(window->get_display_index());
if (modes <= 0) { if(modes <= 0) {
std::cerr << "No modes supported\n"; std::cerr << "No modes supported\n";
return result; return result;
} }
@ -387,12 +363,12 @@ std::vector<std::pair<int, int> > CVideo::get_available_resolutions(const bool i
SDL_DisplayMode mode; SDL_DisplayMode mode;
for (int i = 0; i < modes; ++i) { for (int i = 0; i < modes; ++i) {
if(SDL_GetDisplayMode(0, i, &mode) == 0) { if(SDL_GetDisplayMode(0, i, &mode) == 0) {
if (mode.w >= min_res.first && mode.h >= min_res.second) if(mode.w >= min_res.first && mode.h >= min_res.second)
result.push_back(std::make_pair(mode.w, mode.h)); result.push_back(std::make_pair(mode.w, mode.h));
} }
} }
if (std::find(result.begin(), result.end(), min_res) == result.end()) { if(std::find(result.begin(), result.end(), min_res) == result.end()) {
result.push_back(min_res); result.push_back(min_res);
} }
@ -420,7 +396,6 @@ bool CVideo::isFullScreen() const {
return (window->get_flags() & SDL_WINDOW_FULLSCREEN_DESKTOP) != 0; return (window->get_flags() & SDL_WINDOW_FULLSCREEN_DESKTOP) != 0;
} }
int CVideo::set_help_string(const std::string& str) int CVideo::set_help_string(const std::string& str)
{ {
font::remove_floating_label(help_string_); font::remove_floating_label(help_string_);
@ -465,17 +440,16 @@ void CVideo::clear_all_help_strings()
clear_help_string(help_string_); clear_help_string(help_string_);
} }
void CVideo::set_fullscreen(bool ison) void CVideo::set_fullscreen(bool ison)
{ {
if (window && isFullScreen() != ison) { if(window && isFullScreen() != ison) {
const std::pair<int,int>& res = preferences::resolution(); const std::pair<int,int>& res = preferences::resolution();
MODE_EVENT mode; MODE_EVENT mode;
if (ison) { if(ison) {
mode = TO_FULLSCREEN; mode = TO_FULLSCREEN;
} else { } else {
mode = preferences::maximized() ? TO_MAXIMIZED_WINDOW : TO_WINDOWED; mode = preferences::maximized() ? TO_MAXIMIZED_WINDOW : TO_WINDOWED;
@ -483,7 +457,7 @@ void CVideo::set_fullscreen(bool ison)
setMode(res.first, res.second, mode); setMode(res.first, res.second, mode);
if (display::get_singleton()) { if(display::get_singleton()) {
display::get_singleton()->redraw_everything(); display::get_singleton()->redraw_everything();
} }
} }
@ -499,8 +473,8 @@ void CVideo::set_resolution(const std::pair<int,int>& resolution)
void CVideo::set_resolution(const unsigned width, const unsigned height) void CVideo::set_resolution(const unsigned width, const unsigned height)
{ {
if (static_cast<unsigned int> (current_resolution().first) == width && if(static_cast<unsigned int>(current_resolution().first) == width &&
static_cast<unsigned int> (current_resolution().second) == height) { static_cast<unsigned int>(current_resolution().second) == height) {
return; return;
} }
@ -517,7 +491,7 @@ void CVideo::set_resolution(const unsigned width, const unsigned height)
} }
void CVideo::lock_flips(bool lock) { void CVideo::lock_flips(bool lock) {
if (lock) { if(lock) {
++flip_locked_; ++flip_locked_;
} else { } else {
--flip_locked_; --flip_locked_;

View file

@ -56,7 +56,7 @@ public:
/** /**
* Initializes a new window, taking into account any preiously saved states. * Initializes a new window, taking into account any preiously saved states.
*/ */
bool init_window(); void init_window();
void setMode( int x, int y, const MODE_EVENT mode ); void setMode( int x, int y, const MODE_EVENT mode );