Skip to content

Commit

Permalink
Add the initial SDL_Texture wrapper code.
Browse files Browse the repository at this point in the history
  • Loading branch information
mordante committed Mar 20, 2014
1 parent 093f19f commit e2ce7ec
Show file tree
Hide file tree
Showing 4 changed files with 165 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Expand Up @@ -358,6 +358,7 @@ endif()

set(wesnoth-sdl_SRC
sdl/alpha.cpp
sdl/texture.cpp
sdl/window.cpp
)

Expand Down
1 change: 1 addition & 0 deletions src/SConscript
Expand Up @@ -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
""")
Expand Down
78 changes: 78 additions & 0 deletions src/sdl/texture.cpp
@@ -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 changes: 85 additions & 0 deletions src/sdl/texture.hpp
@@ -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

0 comments on commit e2ce7ec

Please sign in to comment.