Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added SDL_Texture wrapper class and some utility functions
- Loading branch information
Showing
11 changed files
with
269 additions
and
27 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
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 |
---|---|---|
@@ -1,6 +1,7 @@ | ||
sdl/exception.cpp | ||
sdl/rect.cpp | ||
sdl/surface.cpp | ||
sdl/texture.cpp | ||
sdl/utils.cpp | ||
sdl/window.cpp | ||
tracer.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
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
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,132 @@ | ||
/* | ||
Copyright (C) 2017 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" | ||
|
||
#include "log.hpp" | ||
#include "sdl/surface.hpp" | ||
#include "video.hpp" | ||
|
||
#include <cassert> | ||
#include <iostream> | ||
|
||
static lg::log_domain log_sdl("SDL"); | ||
#define ERR_SDL LOG_STREAM(err, log_sdl) | ||
|
||
// The default pixel format to create textures with. | ||
static int default_texture_format = SDL_PIXELFORMAT_ARGB8888; | ||
|
||
texture::texture() | ||
: texture_(nullptr) | ||
{ | ||
} | ||
|
||
texture::texture(SDL_Texture* txt) | ||
: texture_(txt) | ||
{ | ||
finalize(); | ||
} | ||
|
||
texture::texture(const surface& surf) | ||
: texture_(nullptr) | ||
{ | ||
SDL_Renderer* renderer = CVideo::get_singleton().get_renderer(); | ||
if(!renderer) { | ||
return; | ||
} | ||
|
||
texture_ = SDL_CreateTextureFromSurface(renderer, surf); | ||
if(!texture_) { | ||
ERR_SDL << "When creating texture from surface: " << SDL_GetError() << std::endl; | ||
} | ||
} | ||
|
||
texture::texture(int w, int h, SDL_TextureAccess access) | ||
: texture_(nullptr) | ||
{ | ||
reset(w, h, access); | ||
} | ||
|
||
texture::~texture() | ||
{ | ||
destroy_texture(); | ||
} | ||
|
||
void texture::finalize() | ||
{ | ||
SDL_SetTextureBlendMode(texture_, SDL_BLENDMODE_BLEND); | ||
} | ||
|
||
void texture::reset(int w, int h, SDL_TextureAccess access) | ||
{ | ||
// No-op if texture is null. | ||
destroy_texture(); | ||
|
||
SDL_Renderer* renderer = CVideo::get_singleton().get_renderer(); | ||
if(!renderer) { | ||
return; | ||
} | ||
|
||
texture_ = SDL_CreateTexture(renderer, default_texture_format, access, w, h); | ||
if(!texture_) { | ||
ERR_SDL << "When creating texture: " << SDL_GetError() << std::endl; | ||
} | ||
|
||
finalize(); | ||
} | ||
|
||
void texture::destroy_texture() | ||
{ | ||
if(texture_) { | ||
SDL_DestroyTexture(texture_); | ||
} | ||
} | ||
|
||
#if 0 | ||
texture& texture::operator=(texture&& t) | ||
{ | ||
destroy_texture(); | ||
|
||
texture_ = t.texture_; | ||
t.texture_ = nullptr; | ||
|
||
return *this; | ||
} | ||
#endif | ||
|
||
texture::info::info(const texture& t) | ||
: format(0) | ||
, access(0) | ||
, w(0) | ||
, h(0) | ||
{ | ||
SDL_QueryTexture(t, &format, &access, &w, &h); | ||
} | ||
|
||
render_target_setter::render_target_setter(texture& t) | ||
: renderer_(CVideo::get_singleton().get_renderer()) | ||
{ | ||
if(renderer_) { | ||
// Validate we can render to this texture. | ||
assert(t.get_info().access == SDL_TEXTUREACCESS_TARGET); | ||
|
||
SDL_SetRenderTarget(renderer_, t); | ||
} | ||
} | ||
|
||
render_target_setter::~render_target_setter() | ||
{ | ||
if(renderer_) { | ||
SDL_SetRenderTarget(renderer_, nullptr); | ||
} | ||
} |
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,96 @@ | ||
/* | ||
Copyright (C) 2017 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. | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <SDL_render.h> | ||
|
||
class surface; | ||
|
||
/** | ||
* Wrapper class to encapsulate creation and management of an SDL_Texture. | ||
* Supports free creation and creation from a surface. | ||
*/ | ||
class texture | ||
{ | ||
public: | ||
/** Default ctor. Texture will be a nullptr. */ | ||
texture(); | ||
|
||
/** Assigns the given texture to this one. */ | ||
explicit texture(SDL_Texture* txt); | ||
|
||
/** Construct a texture from a surface. */ | ||
explicit texture(const surface& surf); | ||
|
||
/** Construct a texture of the specified size and access type. */ | ||
texture(int w, int h, SDL_TextureAccess access); | ||
|
||
~texture(); | ||
|
||
/** Small wrapper that queries metadata about the provided texture. */ | ||
struct info | ||
{ | ||
explicit info(const texture& t); | ||
|
||
Uint32 format; | ||
int access; | ||
int w; | ||
int h; | ||
}; | ||
|
||
/** Queries metadata about the texture, such as its dimensions. */ | ||
const info get_info() const | ||
{ | ||
return info(*this); | ||
} | ||
|
||
/** Destroys the managed texture and creates a new one. */ | ||
void reset(int w, int h, SDL_TextureAccess access); | ||
|
||
#if 0 | ||
/** Move assignment. Frees the managed texture from the passed object. */ | ||
texture& operator=(texture&& t); | ||
#endif | ||
|
||
operator SDL_Texture*() const | ||
{ | ||
return texture_; | ||
} | ||
|
||
bool null() const | ||
{ | ||
return texture_ == nullptr; | ||
} | ||
|
||
private: | ||
void finalize(); | ||
|
||
void destroy_texture(); | ||
|
||
SDL_Texture* texture_; | ||
}; | ||
|
||
/** | ||
* Small RAII helper class to temporarily set the renderer target to a texture. | ||
*/ | ||
class render_target_setter | ||
{ | ||
public: | ||
explicit render_target_setter(texture& t); | ||
|
||
~render_target_setter(); | ||
|
||
private: | ||
SDL_Renderer* renderer_; | ||
}; |
Oops, something went wrong.