Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add the initial SDL_Texture wrapper code.
- Loading branch information
Showing
4 changed files
with
165 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -358,6 +358,7 @@ endif() | |
|
||
set(wesnoth-sdl_SRC | ||
sdl/alpha.cpp | ||
sdl/texture.cpp | ||
sdl/window.cpp | ||
) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |