Skip to content

Commit

Permalink
Add texture creation code for the window.
Browse files Browse the repository at this point in the history
Since textures are attached to a renderer the window (and thus its
renderer) often is the best place to create the texture.
  • Loading branch information
mordante committed Mar 20, 2014
1 parent e2ce7ec commit ba447c1
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/sdl/window.cpp
Expand Up @@ -18,6 +18,7 @@

#include "exceptions.hpp"
#include "log.hpp"
#include "sdl/texture.hpp"

#include <SDL_render.h>

Expand All @@ -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 »"
Expand All @@ -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()
Expand Down Expand Up @@ -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_;
Expand Down
13 changes: 13 additions & 0 deletions src/sdl/window.hpp
Expand Up @@ -37,6 +37,8 @@ struct SDL_Renderer;
namespace sdl
{

class ttexture;

/**
* The wrapper class for the @ref SDL_Window class.
*
Expand Down Expand Up @@ -119,6 +121,14 @@ class twindow : private boost::noncopyable
*/
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. ***** ***** *****/

Expand All @@ -140,6 +150,9 @@ class twindow : private boost::noncopyable

/** The @ref SDL_Window we own. */
SDL_Window* window_;

/** The preferred pixel format for the renderer. */
Uint32 pixel_format_;
};

} // namespace sdl
Expand Down

0 comments on commit ba447c1

Please sign in to comment.