Thursday, May 18, 2017

c++ - SDL Function for Loading PNGs


I'm learning SDL 2.0 at the moment and I've seen this method for loading BMPs:



SDL_Texture* LoadImage(std::string file)
{
SDL_Surface *loadedImage = nullptr;
SDL_Texture *texture = nullptr;
loadedImage = SDL_LoadBMP(file.c_str());

if (loadedImage != nullptr)
{
texture = SDL_CreateTextureFromSurface(renderer, loadedImage);
SDL_FreeSurface(loadedImage);

}
else
std::cout << SDL_GetError() << std::endl;
return texture;
}

How could I switch it up to load PNG files?


EDIT: Woops, forgot to add my method of loading PNGs.


SDL_Texture* grass_image = nullptr;
grass_image = IMG_LoadTexture(renderer, "res/grass.bmp");

SDL_Rect grass_rect;
grass_rect.x = 0;
grass_rect.y = 0;
grass_rect.w = SCREEN_WIDTH;
grass_rect.h = SCREEN_HEIGHT;

if (grass_image == NULL)
std::cout << "Couldn't load grass_image" << std::endl;

while (!quit && mainEvent -> type != SDL_Quit)

{
SDL_RenderCopy(renderer, grass_image, NULL, &grass_rect);
}

I just don't want to have to do that for EVERY image. So how could I put that into a function?


Thanks!




No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...