0

I'm wondering how to handle more than one image using IMG_Load(); Do I always have to create a surface for every image? Or I have to create some loop that will create texture only using this one surface?

SDL_Surface* surface = IMG_Load("resources/hello.png");
SDL_Texture* tex = SDL_CreateTextureFromSurface(rend, surface);

Here is the code:

#include <stdio.h>
#include <SDL.h>
#include <SDL_timer.h>
#include <SDL_image.h>

int main(void)
{
    if (SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER) != 0)
    {
        printf("error initializing SDL: %s\n", SDL_GetError());
        return 1;
    }

    SDL_Window* win = SDL_CreateWindow("Hello, SDL2!",
                                       SDL_WINDOWPOS_CENTERED,
                                       SDL_WINDOWPOS_CENTERED,
                                       640, 480, 0);
    if (!win)
    {
        printf("error creating window: %s\n", SDL_GetError());
        SDL_Quit();
        return 1;
    }

    Uint32 render_flags = SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC;
    SDL_Renderer* rend = SDL_CreateRenderer(win, -1, render_flags);
    if (!rend)
    {
      printf("error creating renderer: %s\n", SDL_GetError());
      SDL_DestroyWindow(win);
      SDL_Quit();
      return 1;
    }

    SDL_Surface* surface = IMG_Load("resources/hello.png");
    if (!surface)
    {
        printf("error creating surface\n");
        SDL_DestroyRenderer(rend);
        SDL_DestroyWindow(win);
        SDL_Quit();
        return 1;
    }
    SDL_Texture* tex = SDL_CreateTextureFromSurface(rend, surface);
    SDL_FreeSurface(surface);
    if (!tex)
    {
        printf("error creating texture: %s\n", SDL_GetError());
        SDL_DestroyRenderer(rend);
        SDL_DestroyWindow(win);
        SDL_Quit();
        return 1;
    }

    SDL_RenderClear(rend);

    SDL_RenderCopy(rend, tex, NULL, NULL);
    SDL_RenderPresent(rend);

    SDL_Delay(5000);
    
    SDL_DestroyTexture(tex);
    SDL_DestroyRenderer(rend);
    SDL_DestroyWindow(win);
    SDL_Quit();
    return 0;
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
krystof
  • 1
  • 3
  • 1
    It seems pretty obvious that IMG_Load creates a new surface each time you call it, doesn't it? – user253751 Nov 17 '20 at 16:34
  • @user253751 Ah yes I can see it now. So if I want to load 10 images I always have to create a surface and load an image into it and create texture from the surface 10 times? – krystof Nov 17 '20 at 17:14
  • 1
    wouldn't that give you 10 textures that all had the same image? – user253751 Nov 17 '20 at 18:10
  • @user253751 I mean 10 different images. – krystof Nov 17 '20 at 19:05
  • 1
    well obviously if you load an image (which creates a surface) and create 10 different textures from that surface, you'll have the same image in 10 different textures. If you want to load 10 different images then you have to load 10 different images, that means calling IMG_Load 10 times – user253751 Nov 17 '20 at 19:26
  • you can't load 10 images without loading 10 images, IMG_Load loads 1 image, so you have to call it 10 times – user253751 Nov 19 '20 at 21:05

1 Answers1

0

What I usually do when I have lots of images to load is create a vector or array of type SDL_Texture* that is a member of a class, and set it up with a for loop, loading in the images and appending them

genpfault
  • 51,148
  • 11
  • 85
  • 139
404 TNF
  • 19
  • 6
  • I was thinking the same thing(loop through the array), Thanks! I will have to do it without Classes, I use C. – krystof Nov 19 '20 at 20:45