• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
doyleman77

SDL_CreateTextureFromSurface issue

9 posts in this topic

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?

0

Share this post


Link to post
Share on other sites

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...
1

Share this post


Link to post
Share on other sites

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
0

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

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
2

Share this post


Link to post
Share on other sites

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. 

0

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

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!

0

Share this post


Link to post
Share on other sites
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.
1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0