Merge branch 'SDL_Texture'

Add a wrapper for the SDL_Texture. This contains the basics more
improvements will be added later.
This commit is contained in:
Mark de Wever 2014-03-20 22:25:32 +01:00
commit 69b9b01dc4
6 changed files with 201 additions and 0 deletions

View file

@ -358,6 +358,7 @@ endif()
set(wesnoth-sdl_SRC
sdl/alpha.cpp
sdl/texture.cpp
sdl/window.cpp
)

View file

@ -150,6 +150,7 @@ libcampaignd = env.Library("campaignd", libcampaignd_sources, OBJPREFIX = "campa
libwesnoth_sdl_sources = Split("""
sdl_utils.cpp
sdl/alpha.cpp
sdl/texture.cpp
sdl/window.cpp
tracer.cpp
""")

78
src/sdl/texture.cpp Normal file
View file

@ -0,0 +1,78 @@
/*
Copyright (C) 2014 by Mark de Wever <koraq@xs4all.nl>
Part of 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/texture.hpp"
#if SDL_VERSION_ATLEAST(2, 0, 0)
#include "log.hpp"
#include <cassert>
static lg::log_domain log_display("display");
#define ERR_DP LOG_STREAM(err, log_display)
namespace sdl
{
ttexture::ttexture(SDL_Renderer& renderer,
const Uint32 format,
const int access,
const int w,
const int h)
: reference_count_(new unsigned(1))
, texture_(SDL_CreateTexture(&renderer, format, access, w, h))
{
if(!texture_) {
ERR_DP << "Failed to create a SDL_Texture object with error »"
<< SDL_GetError() << "«.\n";
}
}
ttexture::~ttexture()
{
assert(reference_count_);
--(*reference_count_);
if(*reference_count_ == 0) {
if(texture_) {
SDL_DestroyTexture(texture_);
}
delete reference_count_;
}
}
ttexture::ttexture(const ttexture& texture)
: reference_count_(texture.reference_count_), texture_(texture.texture_)
{
assert(reference_count_);
++(*reference_count_);
/* In the unlikely case the reference count wraps, we die. */
assert(*reference_count_ != 0);
}
ttexture& ttexture::operator=(const ttexture& texture)
{
if(&texture != this) {
this->~ttexture();
new (this) ttexture(texture);
}
return *this;
}
} // namespace sdl
#endif

85
src/sdl/texture.hpp Normal file
View file

@ -0,0 +1,85 @@
/*
Copyright (C) 2014 by Mark de Wever <koraq@xs4all.nl>
Part of 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_TEXTURE_HPP_INCLUDED
#define SDL_TEXTURE_HPP_INCLUDED
/**
* @file
* Contains a wrapper class for the @ref SDL_Texture class.
*/
#include <SDL_version.h>
#if SDL_VERSION_ATLEAST(2, 0, 0)
#include <SDL_render.h>
namespace sdl
{
/**
* The reference counted wrapper class for the @ref SDL_Texture class.
*/
class ttexture
{
public:
/***** ***** ***** Constructor and destructor. ***** ***** *****/
/**
* Constructor.
*
* The function calls @ref SDL_CreateTexture.
*
* @param renderer Used as renderer for @ref SDL_CreateTexture.
* @param format Used as format for @ref SDL_CreateTexture.
* @param access Used as access for @ref SDL_CreateTexture.
* @param w Used as w for @ref SDL_CreateTexture.
* @param h Used as x for @ref SDL_CreateTexture.
*/
ttexture(SDL_Renderer& renderer,
const Uint32 format,
const int access,
const int w,
const int h);
~ttexture();
ttexture(const ttexture& texture);
ttexture& operator=(const ttexture& texture);
/***** ***** ***** Members. ***** ***** *****/
private:
/**
* The reference count of the texture.
*
* Since allocating the reference counter can throw an exception it is the
* first member of the class.
*/
unsigned* reference_count_;
/** The SDL_Texture we manage. */
SDL_Texture* texture_;
};
} // namespace sdl
#endif
#endif

View file

@ -18,6 +18,7 @@
#include "exceptions.hpp"
#include "log.hpp"
#include "sdl/texture.hpp"
#include <SDL_render.h>
@ -35,6 +36,7 @@ twindow::twindow(const std::string& title,
const Uint32 window_flags,
const Uint32 render_flags)
: window_(SDL_CreateWindow(title.c_str(), x, y, w, h, window_flags))
, pixel_format_(SDL_PIXELFORMAT_UNKNOWN)
{
if(!window_) {
ERR_DP << "Failed to create a SDL_Window object with error »"
@ -49,6 +51,22 @@ twindow::twindow(const std::string& title,
throw game::error("");
}
SDL_RendererInfo info;
if(SDL_GetRendererInfo(*this, &info) != 0) {
ERR_DP << "Failed to retrieve the information of the renderer, error »"
<< SDL_GetError() << "«.\n";
throw game::error("");
}
if(info.num_texture_formats == 0) {
ERR_DP << "The renderer has no texture information available.\n";
throw game::error("");
}
pixel_format_ = info.texture_formats[0];
}
twindow::~twindow()
@ -83,6 +101,11 @@ void twindow::set_icon(const surface& icon)
SDL_SetWindowIcon(window_, icon);
}
ttexture twindow::create_texture(const int access, const int w, const int h)
{
return ttexture(*SDL_GetRenderer(window_), pixel_format_, access, w, h);
}
twindow::operator SDL_Window*()
{
return window_;

View file

@ -37,6 +37,8 @@ struct SDL_Renderer;
namespace sdl
{
class ttexture;
/**
* The wrapper class for the @ref SDL_Window class.
*
@ -119,6 +121,14 @@ public:
*/
void set_icon(const surface& icon);
/**
* Creates a texture for the renderer of this object.
*
* @param access Used as access for @ref SDL_CreateTexture.
* @param w Used as w for @ref SDL_CreateTexture.
* @param h Used as x for @ref SDL_CreateTexture.
*/
ttexture create_texture(const int access, const int w, const int h);
/***** ***** ***** Conversion operators. ***** ***** *****/
@ -140,6 +150,9 @@ private:
/** The @ref SDL_Window we own. */
SDL_Window* window_;
/** The preferred pixel format for the renderer. */
Uint32 pixel_format_;
};
} // namespace sdl