SDL and renderers

Oke.
I'm starting using SDL, and i need an optimized img loading function.
Currently in the new SDL2 there are Renderers, and i'm using the corrisppetivly library SDL_IMAGE to load other types other than BMP.
Now i came across a problem
The only function that convert's an SDL_Surface to a Texture is
SDL_CreateTextureFromSurface(SDL_Renderer*, SDL_Surface*)
I'm also using OOP, so in my Sprite class seems that i need to create a renderer.
I don't find this very appealing, since it's a waste of computational power and resource(yeah, i know, we are in the era of "4gb ram? You are such a pleasant".

Now, there is actually a better way to CreateATextureFromASurface?
Or is the Renderer thing actually, how do i say that "way of the programmer correct"?
1
2
3
4
5
6
7
8
9
10
void Sprite::LoadTexture(std::string path)
{
    SDL_Surface *surface;
    SDL_Renderer *renderer;
    surface = IMG_LOAD(path.c_str());

    texture = SDL_CreateTextureFromSurface(renderer, surface);
    SDL_FreeSurface(surface);
}


That not seem optimized.

And is fine to have a renderer, for each Entity?


EDIT.
I'm actually dumber than i thought:
 
void Sprite::LoadTexture(std::string path, SDL_Renderer *ren)


but still, it normal to have a renderer for each entity?
Last edited on
You can use the same renderer to create all your textures.
Thank you, i came across another problem:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//.cpp
SDL_Texture Sprite::GetT()
{
    return texture;
}

//GameClass
void Gmae::Start()
{
    while(running == true)
    {
        Control();
        SDL_RenderClear(renderer);
        SDL_RenderCopy(renderer, Player.GetT(), NULL, NULL);
        SDL_RenderPresent(renderer);
    }
}


And this is the error
1
2
invalid use of incomplete type 'SDL_Texture {aka struct SDL_Texture}'
forward declaration of 'SDL_Texture {aka struct SDL_Texture}'


I kinda know why it says is an error
it should be
SDL_Texture *Sprite::GetT()
since i'm returning a pointer. But what is that error...
I guess SDL.h contains a forward declaration of SDL_Texture without a struct definition. A forward declaration of SDL_Texture would look something like this:

 
struct SDL_Texture;

This tells the compiler that there is a struct named SDL_Texture but it doesn't say anything about what data it contains, so we don't know the size of SDL_Texture so we can't create SDL_Texture objects. All that the forward declaration allows you to do is to have pointers (and references) to the type so that is why you get an error if you leave out the *.

SDL_Texture is of course defined somewhere inside the SDL library but I don't know if it's exposed to the user of the library or not.
Topic archived. No new replies allowed.