Reputation: 87
I'm not sure where I'm going wrong when rendering a sprite. Of what I have been taught in lectures, to render a PNG you must firstly have a surface that you load the file to, then you must have a texture and create texture from surface, you then free the space and then it should output it? Where am I going wrong here? Could it be file path? If so I have tried putting the full directory in and everything?
Here is my code:
case 2:
{
//game code..............
SDL_SetRenderDrawColor(renderer, 255, 210, 0, 0);
SDL_RenderClear(renderer);
SDL_Texture* sprite;
SDL_Texture* blockt;
SDL_Texture* points;
SDL_Surface* blockS;
SDL_Surface* windowS;
SDL_Surface* temp;
blockS = IMG_Load("Barriers.png");
blockt = SDL_CreateTextureFromSurface(renderer, blockS);
SDL_FreeSurface(blockS);
SDL_RenderPresent(renderer);
}
break;
Try to ignore the temp and stuff I was just trying out different things I have seen and left some code in there. Basically just need to know why it isn't working. I have SDL_INIT_EVERYTHING at the top as well as the IMG_Init(SDL_INIT_EVERYTHING) and I have included the SDL_image.h header.
Upvotes: 0
Views: 1206
Reputation: 13269
You have to actually draw the texture using SDL_RenderCopy
(before SDL_RenderPresent
).
Note that, once you have the texture, freeing the original surface is irrelevant (although you do want to free it at some point). What you do with it won't change that texture; they are separate entities. All you need to do is render the texture.
Also, go take a look at the wiki (here's the rendering category page). It should give you a better understanding of what you can do.
Upvotes: 2