Jump to content

  • Log In with Google      Sign In   
  • Create Account

SDL_CreateTextureFromSurface issue


Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.


  • You cannot reply to this topic
9 replies to this topic

#1   Members   

367
Like
0Likes
Like

Posted 26 January 2014 - 08:46 PM

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?



#2   Moderators   

9748
Like
1Likes
Like

Posted 27 January 2014 - 12:23 AM

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...
zlib: eJzVVLsSAiEQ6/1qCwoK i7PxA/2S2zMOZljYB1TO ZG7OhUtiduH9egZQCJH9 KcJyo4Wq9t0/RXkKmjx+ cgU4FIMWHhKCU+o/Nx2R LEPgQWLtnfcErbiEl0u4 0UrMghhZewgYcptoEF42 YMj+Z1kg+bVvqxhyo17h nUf+h4b2W4bR4XO01TJ7 qFNzA7jjbxyL71Avh6Tv odnFk4hnxxAf4w6496Kd OgH7/RxC

#3   Members   

1987
Like
0Likes
Like

Posted 27 January 2014 - 01:54 AM

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;
}

Edited by AlanSmithee, 27 January 2014 - 07:13 AM.


#4   Members   

367
Like
0Likes
Like

Posted 27 January 2014 - 07:24 AM

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.



#5   Members   

1987
Like
2Likes
Like

Posted 27 January 2014 - 07:43 AM

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.


Edited by AlanSmithee, 27 January 2014 - 07:47 AM.


#6   Members   

836
Like
0Likes
Like

Posted 27 January 2014 - 12:41 PM

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. 



#7   Members   

367
Like
0Likes
Like

Posted 27 January 2014 - 02:15 PM

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.



#8   Members   

836
Like
1Likes
Like

Posted 27 January 2014 - 05:11 PM

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



#9   Members   

367
Like
0Likes
Like

Posted 27 January 2014 - 06:00 PM

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!



#10   Members   

836
Like
1Likes
Like

Posted 28 January 2014 - 12:30 AM

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.




Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.