SDL_CreateTextureFromSurface issue

Started by
8 comments, last by exOfde 10 years, 2 months ago

I noticed that CreateTextureFromSurface wants a renderer and a surface passed in as arguments.

I was trying to seperate sprites from the game window, which holds the renderer; and am not quite sure how to do it.

At the moment, I have a Window class which holds the window and renderer, and a Sprite, which has image loading capabilities. An image should be able to load it's own image without knowing about the window; or that's the idea I was chasing after.

Do I really have to pass a Window down by reference into Sprite, so that Sprite can get() a renderer from the Window?

Advertisement

An image should be able to load it's own image without knowing about the window; or that's the idea I was chasing after.

Do I really have to pass a Window down by reference into Sprite, so that Sprite can get() a renderer from the Window?


If I recall correctly, SDL_Surface can accept a SDL_Format (or SDL_SurfaceFormat?) of the screen to create a new SDL_Surface optimized for rendering to the screen. There should be a way you can access the screen's surface format and pass that along to the sprite. Otherwise, you might want to consider factoring out the image loading out of the sprite and into a ImageLoading class that accepts the responsibility...

Hi.

I agree with fastcall22, that you should factor out loading functionality from your Image class to an ImageLoader class, there is no point for each Image to have its own loading functionality.

Even if you do that or not, just pass the renderer as a parameter to the loading method, that way it doesn't have to know about the WIndow class.


// Signature of loading method
const bool load_image(SDL_Renderer* renderer, std::string const& name);
 
// Usage
Window window;
 
// Initialization and stuff...
 
// Load an image
if (!load_image(window.get_renderer(), "imageName.jpg"))
{
  std::cerr << "Could not load image :(" << std::endl;
}

The image loading was a function that was defined separate from any class; but I suppose the point still stands I should definitely separate and move it.

At the moment, I have:

an App class that uses a Window Object

A Window class that has the Renderer and the Window structs from SDL2

And a Sprite Class that just contains a Texture, coordinates, details, etc about a sprite.

The App class has the update(), draw(), and input(); the draw calls all sprites draws() to be hopefully put onto the Window, and then the Window's draw() does the Clear() and RenderPresent().

If I'm reading what you're saying, Alan, it seems like you want my image loading built into the Window class? It looks like that. I mean, I could, but Image Loading isn't part of the window, per say. Maybe I'm thinking about this the wrong way.

Hi!

Maybe you are confused by my code, i declared load_imge to return a bool, which mas a typo, it should return a Image object and the loader class should be separate from any other class.

An example:

Window.h


class Window
{
  public:
  //...
    SDL_Renderer* get_renderer() { return _renderer; }
 
  private:
  //...
    SDL_Renderer* _renderer;
};

ImageLoader.h


class ImageLoader
{
  public:
    Image const& load_image(SDL_Renderer*, std::string const&);
};

main.cpp


{
  Window window;
  ImageLoader imageLoader;
  Image image;
 
  //...
 
  image = imageLoader.load_image(window.get_renderer(), "foo.png");
  if (image == nullptr)
  {
    std::cerr << "Could not load image" << std::endl;
  }
 
  //...
}

Or something like that, sorry for any confusion i might have caused.

Question out of curiosity: Why do you want seperate Your Image from the Window?

And should you follow AlanSmithee's idea of seperating the Loading function, i suggest

you do it for the renderer too. that could makes the use of textures really interesting.

Question out of curiosity: Why do you want seperate Your Image from the Window?

And should you follow AlanSmithee's idea of seperating the Loading function, i suggest

you do it for the renderer too. that could makes the use of textures really interesting.

Separate in the sense that I want to organize data into appropriate objects. Sprites have images, not windows, etc. But a window displays the screen, which is what a sprite in SDL2 needs to have passed in in order to be loaded and placed into a texture.

I think I understand what fastcall and adamsmithee were talking about; but what do you mean separate the renderer from the window? That seems a bit more abstract than it should be; as a SDL window, at least this one, 'renders' content - and thus should hold the renderer struct. That was my intent, anyway.

Proper separation for me would be a window not needing to know anything about image loading, but images i suppose do need to know about what window they'll eventually be displayed on? Hrm.

I'm not quite good in explaining on english so look at my code what i mean with seperate the Renderer:

Window class: https://code.google.com/p/game-utility-library/source/browse/include/Window.h

Renderer class: https://code.google.com/p/game-utility-library/source/browse/include/Renderer.h

Oh wow. I've not had a lot of experience reading code from others... usually it's done more... cleanly? than mine? or maybe up to date. I usually have a very rudementary setup as far as my C++ goes. I could definately follow along with what was going on.

Am I right in guessing that, then, your Renderer class holds all textures via textureDictionary? and scaling a texture is also done via renderer?

Either way; very clean and documented code. I think I see what you mean now!

Yes right scaling is done by the renderer because is just an wrapper around sdl and sdl do this job for you.
And yes renderer holds all textures, its an nice feature i think.
Thanks, it awesome to read that you could follow my code and additional understand what I meant.

This topic is closed to new replies.

Advertisement