Skip to main content
Tweeted twitter.com/StackGameDev/status/821183245333254144
added 631 characters in body
Source Link
Connor W
  • 183
  • 1
  • 1
  • 9

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!

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?

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!

Source Link
Connor W
  • 183
  • 1
  • 1
  • 9

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?