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.
*/
/**
* @file
* Video-testprogram, standalone
*/
#include "global.hpp"
#include "font/sdl_ttf.hpp"
#include "video.hpp"
#include "display.hpp"
#include "floating_label.hpp"
#include "image.hpp"
#include "log.hpp"
#include "preferences.hpp"
#include "sdl/utils.hpp"
#include "sdl/rect.hpp"
#include "sdl/window.hpp"
#include "video.hpp"
#include "display.hpp"
#include <vector>
#include <map>
#include <algorithm>
#include <cassert>
#include <vector>
static lg::log_domain log_display("display");
#define LOG_DP LOG_STREAM(info, log_display)
@ -42,18 +32,7 @@ static lg::log_domain log_display("display");
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 {
surface frameBuffer = nullptr;
bool fake_interactive = false;
}
@ -62,8 +41,8 @@ namespace video2 {
std::list<events::sdl_handler*> draw_layers;
draw_layering::draw_layering(const bool auto_join) :
sdl_handler(auto_join)
draw_layering::draw_layering(const bool auto_join)
: sdl_handler(auto_join)
{
draw_layers.push_back(this);
}
@ -82,8 +61,8 @@ void trigger_full_redraw() {
event.window.data1 = (*frameBuffer).h;
event.window.data2 = (*frameBuffer).w;
for(std::list<events::sdl_handler*>::iterator it = draw_layers.begin(); it != draw_layers.end(); ++it) {
(*it)->handle_window_event(event);
for(const auto& layer : draw_layers) {
layer->handle_window_event(event);
}
SDL_Event drawEvent;
@ -99,19 +78,61 @@ void trigger_full_redraw() {
SDL_FlushEvent(DRAW_ALL_EVENT);
SDL_PushEvent(&drawEvent);
}
} // video2
CVideo::CVideo(FAKE_TYPES type)
: window()
, mode_changed_(false)
, fake_screen_(false)
, help_string_(0)
, updatesLocked_(0)
, flip_locked_(0)
{
assert(!singleton_);
singleton_ = this;
initSDL();
switch(type) {
case NO_FAKE:
break;
case FAKE:
make_fake();
break;
case FAKE_TEST:
make_test_fake();
break;
}
}
void CVideo::initSDL()
{
const int res = SDL_InitSubSystem(SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE);
if(res < 0) {
ERR_DP << "Could not initialize SDL_video: " << SDL_GetError() << std::endl;
throw CVideo::error();
}
}
CVideo::~CVideo()
{
LOG_DP << "calling SDL_Quit()\n";
SDL_Quit();
assert(singleton_);
singleton_ = nullptr;
LOG_DP << "called SDL_Quit()\n";
}
bool CVideo::non_interactive()
{
if (fake_interactive)
return false;
return window == nullptr;
return fake_interactive ? false : (window == nullptr);
}
SDL_Rect screen_area()
{
return sdl::create_rect(0, 0, frameBuffer->w, frameBuffer->h);
return {0, 0, frameBuffer->w, frameBuffer->h};
}
void CVideo::video_event_handler::handle_window_event(const SDL_Event &event)
@ -142,51 +163,6 @@ void CVideo::video_event_handler::handle_window_event(const SDL_Event &event)
}
}
CVideo::CVideo(FAKE_TYPES type) :
window(),
mode_changed_(false),
fake_screen_(false),
help_string_(0),
updatesLocked_(0),
flip_locked_(0)
{
assert(!singleton_);
singleton_ = this;
initSDL();
switch(type)
{
case NO_FAKE:
break;
case FAKE:
make_fake();
break;
case FAKE_TEST:
make_test_fake();
break;
}
}
void CVideo::initSDL()
{
const int res = SDL_InitSubSystem(SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE);
if(res < 0) {
ERR_DP << "Could not initialize SDL_video: " << SDL_GetError() << std::endl;
throw CVideo::error();
}
}
CVideo::~CVideo()
{
LOG_DP << "calling SDL_Quit()\n";
SDL_Quit();
assert(singleton_);
singleton_ = nullptr;
LOG_DP << "called SDL_Quit()\n";
}
void CVideo::blit_surface(int x, int y, surface surf, SDL_Rect* srcrect, SDL_Rect* clip_rect)
{
surface& target(getSurface());
@ -196,7 +172,6 @@ void CVideo::blit_surface(int x, int y, surface surf, SDL_Rect* srcrect, SDL_Rec
sdl_blit(surf,srcrect,target,&dst);
}
void CVideo::make_fake()
{
fake_screen_ = true;
@ -204,35 +179,32 @@ void CVideo::make_fake()
image::set_pixel_format(frameBuffer->format);
}
void CVideo::make_test_fake(const unsigned width,
const unsigned height, const unsigned bpp)
void CVideo::make_test_fake(const unsigned width, const unsigned height, const unsigned bpp)
{
frameBuffer = SDL_CreateRGBSurface(SDL_SWSURFACE,
width, height, bpp, 0xFF0000, 0xFF00, 0xFF, 0);
frameBuffer = SDL_CreateRGBSurface(SDL_SWSURFACE, width, height, bpp, 0xFF0000, 0xFF00, 0xFF, 0);
image::set_pixel_format(frameBuffer->format);
fake_interactive = true;
}
void CVideo::update_framebuffer()
{
if (!window)
if(!window) {
return;
}
surface fb = SDL_GetWindowSurface(*window);
if (!frameBuffer)
if(!frameBuffer) {
frameBuffer = fb;
else
} else {
frameBuffer.assign(fb);
}
}
/**
* Creates a new window instance.
*/
bool CVideo::init_window()
void CVideo::init_window()
{
// Position
const int x = preferences::fullscreen() ? SDL_WINDOWPOS_UNDEFINED : SDL_WINDOWPOS_CENTERED;
@ -245,7 +217,8 @@ bool CVideo::init_window()
// Video flags
int video_flags = 0;
video_flags = get_flags(video_flags);
// Add any more default flags here
video_flags |= SDL_WINDOW_RESIZABLE;
if(preferences::fullscreen()) {
video_flags |= SDL_WINDOW_FULLSCREEN_DESKTOP;
@ -269,8 +242,6 @@ bool CVideo::init_window()
if(frameBuffer) {
image::set_pixel_format(frameBuffer->format);
}
return true;
}
void CVideo::setMode(int x, int y, const MODE_EVENT mode)
@ -326,25 +297,30 @@ int CVideo::gety() const
void CVideo::delay(unsigned int milliseconds)
{
if (!game_config::no_delay)
if(!game_config::no_delay) {
SDL_Delay(milliseconds);
}
}
void CVideo::flip()
{
if(fake_screen_ || flip_locked_ > 0)
if(fake_screen_ || flip_locked_ > 0) {
return;
if (window)
}
if(window) {
window->render();
}
}
void CVideo::lock_updates(bool value)
{
if(value == true)
if(value == true) {
++updatesLocked_;
else
} else {
--updatesLocked_;
}
}
bool CVideo::update_locked() const
{
@ -420,7 +396,6 @@ bool CVideo::isFullScreen() const {
return (window->get_flags() & SDL_WINDOW_FULLSCREEN_DESKTOP) != 0;
}
int CVideo::set_help_string(const std::string& str)
{
font::remove_floating_label(help_string_);
@ -465,7 +440,6 @@ void CVideo::clear_all_help_strings()
clear_help_string(help_string_);
}
void CVideo::set_fullscreen(bool ison)
{

View file

@ -56,7 +56,7 @@ public:
/**
* 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 );