Add texture creation code for the window.

Since textures are attached to a renderer the window (and thus its
renderer) often is the best place to create the texture.
This commit is contained in:
Mark de Wever 2014-03-20 21:49:42 +01:00
parent e2ce7ec219
commit ba447c16e6
2 changed files with 36 additions and 0 deletions

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