Split SDL_Surface wrapper class and related helper struts into their own file

This (should) allow sdl/utils.hpp to be modified without a huge rebuild, since a number
of widely used headers included that file simply for the surface class type.
This commit is contained in:
Charles Dang 2016-12-10 22:35:46 +11:00
parent 09e73dbc98
commit b97a20b059
39 changed files with 311 additions and 244 deletions

View file

@ -365,6 +365,7 @@ set_target_properties(wesnoth-lua
set(wesnoth-sdl_SRC
sdl/exception.cpp
sdl/rect.cpp
sdl/surface.cpp
sdl/window.cpp
sdl/utils.cpp
xBRZ/xbrz.cpp

View file

@ -157,6 +157,7 @@ libcampaignd = env.Library("campaignd", libcampaignd_sources, OBJPREFIX = "campa
libwesnoth_sdl_sources = Split("""
sdl/exception.cpp
sdl/rect.cpp
sdl/surface.cpp
sdl/utils.cpp
sdl/window.cpp
tracer.cpp

View file

@ -27,7 +27,7 @@
#include "font/standard_colors.hpp"
#include "scripting/plugins/context.hpp"
#include "scripting/plugins/manager.hpp"
#include "sdl/utils.hpp"
#include "sdl/surface.hpp"
#include "sdl/rect.hpp"
#include "utils/functional.hpp"

View file

@ -15,7 +15,7 @@
#pragma once
#include "color.hpp"
#include "sdl/utils.hpp"
#include "sdl/surface.hpp"
#include <string>
namespace font {

View file

@ -26,7 +26,7 @@
#include "gettext.hpp"
#include "font/marked-up_text.hpp"
#include "font/standard_colors.hpp"
#include "sdl/utils.hpp"
#include "sdl/surface.hpp"
#include "serialization/string_utils.hpp"
#include "serialization/unicode.hpp"
#include "video.hpp"

View file

@ -28,7 +28,7 @@
#include "tooltips.hpp"
#include "sdl/rect.hpp"
#include "sdl/utils.hpp"
#include "sdl/surface.hpp"
#include "serialization/unicode.hpp"
#include <SDL_ttf.h>

View file

@ -17,6 +17,7 @@
#include "font/font_options.hpp"
#include "color.hpp"
#include "sdl/surface.hpp"
#include "sdl/utils.hpp"
#include "serialization/unicode_types.hpp"

View file

@ -13,7 +13,7 @@
*/
#include "font/text_cache.hpp"
#include "sdl/utils.hpp"
#include "sdl/surface.hpp"
#include <algorithm>

View file

@ -16,7 +16,7 @@
#include "font/sdl_ttf.hpp"
#include "sdl/utils.hpp"
#include "sdl/surface.hpp"
#include "log.hpp"

View file

@ -51,7 +51,7 @@
#include "preferences_display.hpp"
#include "savegame.hpp" // for clean_saves, etc
#include "scripting/application_lua_kernel.hpp"
#include "sdl/utils.hpp" // for surface
#include "sdl/surface.hpp" // for surface
#include "serialization/compression.hpp" // for format::NONE
#include "serialization/string_utils.hpp" // for split
#include "game_initialization/singleplayer.hpp" // for sp_create_mode

View file

@ -22,7 +22,7 @@
#define GUI_AUXILIARY_CANVAS_HPP_INCLUDED
#include "formula/callable.hpp"
#include "sdl/utils.hpp"
#include "sdl/surface.hpp"
class config;
class variant;

View file

@ -55,7 +55,7 @@
#include "preferences.hpp"
#include "preferences_display.hpp"
#include "sdl/rect.hpp"
#include "sdl/utils.hpp"
#include "sdl/surface.hpp"
#include "tstring.hpp"
#include "formula/variant.hpp"
#include "video.hpp"

View file

@ -32,7 +32,7 @@
#include "help/help_impl.hpp" // for hidden_symbol, toplevel, etc
#include "key.hpp" // for CKey
#include "log.hpp" // for LOG_STREAM, log_domain
#include "sdl/utils.hpp" // for surface
#include "sdl/surface.hpp" // for surface
#include "show_dialog.hpp" // for dialog_frame, etc
#include "terrain/terrain.hpp" // for terrain_type
#include "units/unit.hpp" // for unit

View file

@ -31,7 +31,7 @@
#include "font/standard_colors.hpp" // for NORMAL_COLOR
#include "units/race.hpp" // for unit_race, etc
#include "resources.hpp" // for tod_manager, config_manager
#include "sdl/utils.hpp" // for surface
#include "sdl/surface.hpp" // for surface
#include "serialization/string_utils.hpp" // for split, quoted_split, etc
#include "serialization/unicode_cast.hpp" // for unicode_cast
#include "serialization/unicode_types.hpp" // for char_t, etc

View file

@ -19,7 +19,7 @@
#include <string> // for string
#include <utility> // for pair
#include "font/standard_colors.hpp" // for NORMAL_COLOR
#include "sdl/utils.hpp" // for surface
#include "sdl/surface.hpp" // for surface
#include "widgets/scrollarea.hpp" // for scrollarea
class CVideo;
class config;

View file

@ -22,7 +22,7 @@
#include "gettext.hpp"
#include "serialization/unicode.hpp"
#include "sdl/utils.hpp"
#include "sdl/surface.hpp"
#include <boost/algorithm/string/join.hpp>
#include <boost/algorithm/string.hpp>

View file

@ -19,6 +19,7 @@
#include "color_range.hpp"
#include "lua_jailbreak_exception.hpp"
#include "sdl/surface.hpp"
#include "sdl/utils.hpp"
#include <queue>

View file

@ -15,7 +15,8 @@
#include "joystick.hpp"
#include "preferences.hpp"
#include "log.hpp"
#include "sdl/utils.hpp"
#include "sdl/surface.hpp"
#include "util.hpp"
#include <boost/math/constants/constants.hpp>
using namespace boost::math::constants;

View file

@ -24,7 +24,7 @@
#include "map/map.hpp"
#include "resources.hpp"
#include "color.hpp"
#include "sdl/utils.hpp"
#include "sdl/surface.hpp"
#include "team.hpp"
#include "terrain/type_data.hpp"
#include "wml_exception.hpp"

View file

@ -83,7 +83,7 @@
#include "scripting/lua_unit_type.hpp"
#include "scripting/push_check.hpp"
#include "color.hpp" // for surface
#include "sdl/utils.hpp" // for surface
#include "sdl/surface.hpp" // for surface
#include "side_filter.hpp" // for side_filter
#include "sound.hpp" // for commit_music_changes, etc
#include "soundsource.hpp"

View file

@ -14,7 +14,7 @@
#include "gui/core/point.hpp"
#include "sdl/rect.hpp"
#include "sdl/utils.hpp"
#include "sdl/surface.hpp"
namespace sdl
{

111
src/sdl/surface.cpp Normal file
View file

@ -0,0 +1,111 @@
/*
Copyright (C) 2003 - 2016 the Battle for Wesnoth Project http://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/
#include "sdl/surface.hpp"
#include "sdl/rect.hpp"
#include "video.hpp"
surface_restorer::surface_restorer()
: target_(nullptr)
, rect_(sdl::empty_rect)
, surface_(nullptr)
{
}
surface_restorer::surface_restorer(CVideo* target, const SDL_Rect& rect)
: target_(target)
, rect_(rect)
, surface_(nullptr)
{
update();
}
surface_restorer::~surface_restorer()
{
restore();
}
void surface_restorer::restore(SDL_Rect const &dst) const
{
if(surface_.null()) {
return;
}
SDL_Rect dst2 = sdl::intersect_rects(dst, rect_);
if(dst2.w == 0 || dst2.h == 0) {
return;
}
SDL_Rect src = dst2;
src.x -= rect_.x;
src.y -= rect_.y;
sdl_blit(surface_, &src, target_->getSurface(), &dst2);
}
void surface_restorer::restore() const
{
if(surface_.null()) {
return;
}
SDL_Rect dst = rect_;
sdl_blit(surface_, nullptr, target_->getSurface(), &dst);
}
void surface_restorer::update()
{
if(rect_.w <= 0 || rect_.h <= 0) {
surface_.assign(nullptr);
} else {
surface_.assign(::get_surface_portion(target_->getSurface(),rect_));
}
}
void surface_restorer::cancel()
{
surface_.assign(nullptr);
}
surface_lock::surface_lock(surface &surf) : surface_(surf), locked_(false)
{
if(SDL_MUSTLOCK(surface_)) {
locked_ = SDL_LockSurface(surface_) == 0;
}
}
surface_lock::~surface_lock()
{
if(locked_) {
SDL_UnlockSurface(surface_);
}
}
const_surface_lock::const_surface_lock(const surface &surf) : surface_(surf), locked_(false)
{
if(SDL_MUSTLOCK(surface_)) {
locked_ = SDL_LockSurface(surface_) == 0;
}
}
const_surface_lock::~const_surface_lock()
{
if(locked_) {
SDL_UnlockSurface(surface_);
}
}
bool operator<(const surface& a, const surface& b)
{
return a.get() < b.get();
}

163
src/sdl/surface.hpp Normal file
View file

@ -0,0 +1,163 @@
/*
Copyright (C) 2003 - 2016 the Battle for Wesnoth Project http://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/
#ifndef SDL_SURFACE_HEADER_INCLUDED
#define SDL_SURFACE_HEADER_INCLUDED
#include <SDL.h>
class CVideo;
class surface
{
public:
surface() : surface_(nullptr)
{}
surface(SDL_Surface* surf) : surface_(surf)
{}
surface(const surface& s) : surface_(s.get())
{
add_surface_ref(surface_);
}
~surface()
{
free_surface();
}
void assign(SDL_Surface* surf)
{
assign_surface_internal(surf);
}
void assign(const surface& s)
{
assign_surface_internal(s.get());
}
surface& operator=(const surface& s)
{
assign(s);
return *this;
}
operator SDL_Surface*() const { return surface_; }
SDL_Surface* get() const { return surface_; }
SDL_Surface* operator->() const { return surface_; }
bool null() const { return surface_ == nullptr; }
private:
static void add_surface_ref(SDL_Surface* surf)
{
if(surf) {
++surf->refcount;
}
}
void assign_surface_internal(SDL_Surface* surf)
{
add_surface_ref(surf); // Needs to be done before assignment to avoid corruption on "a = a;"
free_surface();
surface_ = surf;
}
void free_surface()
{
if(surface_) {
SDL_FreeSurface(surface_);
}
}
SDL_Surface* surface_;
};
bool operator<(const surface& a, const surface& b);
struct surface_restorer
{
surface_restorer();
surface_restorer(class CVideo* target, const SDL_Rect& rect);
~surface_restorer();
void restore() const;
void restore(SDL_Rect const &dst) const;
void update();
void cancel();
const SDL_Rect& area() const { return rect_; }
private:
class CVideo* target_;
SDL_Rect rect_;
surface surface_;
};
/**
* Helper class for pinning SDL surfaces into memory.
* @note This class should be used only with neutral surfaces, so that
* the pointer returned by #pixels is meaningful.
*/
struct surface_lock
{
surface_lock(surface &surf);
~surface_lock();
Uint32* pixels() { return reinterpret_cast<Uint32*>(surface_->pixels); }
private:
surface& surface_;
bool locked_;
};
struct const_surface_lock
{
const_surface_lock(const surface &surf);
~const_surface_lock();
const Uint32* pixels() const { return reinterpret_cast<const Uint32*>(surface_->pixels); }
private:
const surface& surface_;
bool locked_;
};
struct clip_rect_setter
{
// if r is nullptr, clip to the full size of the surface.
clip_rect_setter(const surface &surf, const SDL_Rect* r, bool operate = true) : surface_(surf), rect_(), operate_(operate)
{
if(operate_){
SDL_GetClipRect(surface_, &rect_);
SDL_SetClipRect(surface_, r);
}
}
~clip_rect_setter()
{
if(operate_) {
SDL_SetClipRect(surface_, &rect_);
}
}
private:
surface surface_;
SDL_Rect rect_;
const bool operate_;
};
#endif

View file

@ -37,40 +37,11 @@
#include <boost/math/constants/constants.hpp>
surface_lock::surface_lock(surface &surf) : surface_(surf), locked_(false)
{
if (SDL_MUSTLOCK(surface_))
locked_ = SDL_LockSurface(surface_) == 0;
}
surface_lock::~surface_lock()
{
if (locked_)
SDL_UnlockSurface(surface_);
}
const_surface_lock::const_surface_lock(const surface &surf) : surface_(surf), locked_(false)
{
if (SDL_MUSTLOCK(surface_))
locked_ = SDL_LockSurface(surface_) == 0;
}
const_surface_lock::~const_surface_lock()
{
if (locked_)
SDL_UnlockSurface(surface_);
}
SDL_Keycode sdl_keysym_from_name(const std::string& keyname)
{
return SDL_GetKeyFromName(keyname.c_str());
}
bool operator<(const surface& a, const surface& b)
{
return a.get() < b.get();
}
bool is_neutral(const surface& surf)
{
return (surf->format->BytesPerPixel == 4 &&
@ -2462,55 +2433,6 @@ SDL_Rect get_non_transparent_portion(const surface &surf)
return res;
}
surface_restorer::surface_restorer() : target_(nullptr), rect_(sdl::empty_rect), surface_(nullptr)
{
}
surface_restorer::surface_restorer(CVideo* target, const SDL_Rect& rect)
: target_(target), rect_(rect), surface_(nullptr)
{
update();
}
surface_restorer::~surface_restorer()
{
restore();
}
void surface_restorer::restore(SDL_Rect const &dst) const
{
if (surface_.null())
return;
SDL_Rect dst2 = sdl::intersect_rects(dst, rect_);
if (dst2.w == 0 || dst2.h == 0)
return;
SDL_Rect src = dst2;
src.x -= rect_.x;
src.y -= rect_.y;
sdl_blit(surface_, &src, target_->getSurface(), &dst2);
}
void surface_restorer::restore() const
{
if (surface_.null())
return;
SDL_Rect dst = rect_;
sdl_blit(surface_, nullptr, target_->getSurface(), &dst);
}
void surface_restorer::update()
{
if(rect_.w <= 0 || rect_.h <= 0)
surface_.assign(nullptr);
else
surface_.assign(::get_surface_portion(target_->getSurface(),rect_));
}
void surface_restorer::cancel()
{
surface_.assign(nullptr);
}
void draw_centered_on_background(surface surf, const SDL_Rect& rect, const color_t& color, surface target)
{
clip_rect_setter clip_setter(target, &rect);

View file

@ -19,87 +19,17 @@
#include "color_range.hpp"
#include "color.hpp"
#include "sdl/surface.hpp"
#include "util.hpp"
#include <SDL.h>
#include <cstdlib>
#include <iosfwd>
#include <map>
#include <string>
SDL_Keycode sdl_keysym_from_name(const std::string& keyname);
class surface
{
public:
surface() : surface_(nullptr)
{}
surface(SDL_Surface* surf) : surface_(surf)
{}
surface(const surface& s) : surface_(s.get())
{
add_surface_ref(surface_);
}
~surface()
{
free_surface();
}
void assign(SDL_Surface* surf)
{
assign_surface_internal(surf);
}
void assign(const surface& s)
{
assign_surface_internal(s.get());
}
surface& operator=(const surface& s)
{
assign(s);
return *this;
}
operator SDL_Surface*() const { return surface_; }
SDL_Surface* get() const { return surface_; }
SDL_Surface* operator->() const { return surface_; }
bool null() const { return surface_ == nullptr; }
private:
static void add_surface_ref(SDL_Surface* surf)
{
if(surf) {
++surf->refcount;
}
}
void assign_surface_internal(SDL_Surface* surf)
{
add_surface_ref(surf); // Needs to be done before assignment to avoid corruption on "a = a;"
free_surface();
surface_ = surf;
}
void free_surface()
{
if(surface_) {
SDL_FreeSurface(surface_);
}
}
SDL_Surface* surface_;
};
bool operator<(const surface& a, const surface& b);
inline void sdl_blit(const surface& src, SDL_Rect* src_rect, surface& dst, SDL_Rect* dst_rect){
SDL_BlitSurface(src, src_rect, dst, dst_rect);
}
@ -409,33 +339,6 @@ void blit_surface(const surface& src,
SDL_Rect get_non_transparent_portion(const surface &surf);
/**
* Helper class for pinning SDL surfaces into memory.
* @note This class should be used only with neutral surfaces, so that
* the pointer returned by #pixels is meaningful.
*/
struct surface_lock
{
surface_lock(surface &surf);
~surface_lock();
Uint32* pixels() { return reinterpret_cast<Uint32*>(surface_->pixels); }
private:
surface& surface_;
bool locked_;
};
struct const_surface_lock
{
const_surface_lock(const surface &surf);
~const_surface_lock();
const Uint32* pixels() const { return reinterpret_cast<const Uint32*>(surface_->pixels); }
private:
const surface& surface_;
bool locked_;
};
/**
* Helper methods for setting/getting a single pixel in an image.
* Lifted from http://sdl.beuc.net/sdl.wiki/Pixel_Access
@ -448,47 +351,6 @@ private:
void put_pixel(const surface& surf, surface_lock& surf_lock, int x, int y, Uint32 pixel);
Uint32 get_pixel(const surface& surf, const const_surface_lock& surf_lock, int x, int y);
struct surface_restorer
{
surface_restorer();
surface_restorer(class CVideo* target, const SDL_Rect& rect);
~surface_restorer();
void restore() const;
void restore(SDL_Rect const &dst) const;
void update();
void cancel();
const SDL_Rect& area() const { return rect_; }
private:
class CVideo* target_;
SDL_Rect rect_;
surface surface_;
};
struct clip_rect_setter
{
// if r is nullptr, clip to the full size of the surface.
clip_rect_setter(const surface &surf, const SDL_Rect* r, bool operate = true) : surface_(surf), rect_(), operate_(operate)
{
if(operate_){
SDL_GetClipRect(surface_, &rect_);
SDL_SetClipRect(surface_, r);
}
}
~clip_rect_setter() {
if (operate_)
SDL_SetClipRect(surface_, &rect_);
}
private:
surface surface_;
SDL_Rect rect_;
const bool operate_;
};
// blit the image on the center of the rectangle
// and a add a colored background
void draw_centered_on_background(surface surf, const SDL_Rect& rect,

View file

@ -12,7 +12,7 @@
See the COPYING file for more details.
*/
#include "sdl/utils.hpp"
#include "sdl/surface.hpp"
#include "sdl/window.hpp"
#include "sdl/exception.hpp"

View file

@ -29,6 +29,7 @@
#include "game_events/pump.hpp"
#include "image.hpp"
#include "serialization/string_utils.hpp"
#include "sdl/utils.hpp"
#include "variable.hpp"
namespace storyscreen {

View file

@ -24,7 +24,7 @@
#include <utility>
#include <vector>
#include "sdl/utils.hpp"
#include "sdl/surface.hpp"
class config;
class vconfig;

View file

@ -15,7 +15,7 @@
#ifndef TESTS_TEST_SDL_UTILS_HPP_INCLUDED
#define TESTS_TEST_SDL_UTILS_HPP_INCLUDED
#include "sdl/utils.hpp"
#include "sdl/surface.hpp"
#include "utils/functional.hpp"

View file

@ -15,7 +15,7 @@
#ifndef EXPLODER_CUTTER_HPP_INCLUDED
#define EXPLODER_CUTTER_HPP_INCLUDED
#include "sdl/utils.hpp"
#include "sdl/surface.hpp"
#include "config.hpp"
#include "exploder_utils.hpp"

View file

@ -15,7 +15,7 @@
#ifndef EXPLODER_UTILS_HPP_INCLUDED
#define EXPLODER_UTILS_HPP_INCLUDED
#include "sdl/utils.hpp"
#include "sdl/surface.hpp"
#include <string>
struct exploder_failure

View file

@ -16,7 +16,7 @@
#define TOOLTIPS_HPP_INCLUDED
#include <string>
#include "sdl/utils.hpp"
#include "sdl/surface.hpp"
class CVideo;
struct SDL_Rect;

View file

@ -22,7 +22,7 @@
#include "map/map.hpp"
#include "map/location.hpp"
#include "color.hpp"
#include "sdl/utils.hpp"
#include "sdl/surface.hpp"
#include "team.hpp"
#include "units/unit.hpp"
#include "units/animation.hpp"

View file

@ -20,7 +20,7 @@
#ifndef WESMAGE_FILTER_HPP_INCLUDED
#define WESMAGE_FILTER_HPP_INCLUDED
#include "sdl/utils.hpp"
#include "sdl/surface.hpp"
#include <string>
#include <vector>

View file

@ -15,6 +15,7 @@
#ifndef WIDGET_MENU_HPP_INCLUDED
#define WIDGET_MENU_HPP_INCLUDED
#include <map>
#include <set>
#include "scrollarea.hpp"

View file

@ -21,6 +21,7 @@
#include "font/sdl_ttf.hpp"
#include "image.hpp"
#include "sdl/utils.hpp"
#include "video.hpp"
namespace gui {

View file

@ -21,7 +21,7 @@
#include <vector>
#include <SDL.h>
#include "sdl/utils.hpp"
#include "sdl/surface.hpp"
#include "scrollarea.hpp"
namespace gui {

View file

@ -16,6 +16,7 @@
#include <SDL.h>
#include "color.hpp"
#include "widget.hpp"
#include <vector>

View file

@ -16,7 +16,7 @@
#define WIDGET_HPP_INCLUDED
#include "events.hpp"
#include "sdl/utils.hpp"
#include "sdl/surface.hpp"
class CVideo;