SDL_Image not loading multiple images.

Hello there, I'm trying to make a simple game using SDL2 libraries. Currently I was able to to load an image but when I try to multiple them, it doesn't work. The code which I'm using:

```

#include <stdio.h>
#include "structs.h"
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <time.h>


typedef struct
{

int x, y;

}BODVAR;

typedef struct{

Player player;
// Bodvars
BODVAR bodvars[100];

SDL_Texture *bodvar;
SDL_Renderer *renderer;

}GameState;


void LoadGame(GameState *game){

SDL_Surface *bodvarSurface = NULL;
// Load image and create a rendering texture from them
bodvarSurface = IMG_Load("Bodvar.png");
if (bodvarSurface == NULL)
{
printf("Can not find Bodvar.png!\n");
SDL_Quit();
exit(1);
}

game->bodvar = SDL_CreateTextureFromSurface(game->renderer, bodvarSurface);
SDL_FreeSurface(bodvarSurface);

game->player.x = 320-40;
game->player.y = 240-40;

// init the stars
for (int i = 0; i < 100; ++i)
{
game->bodvars[i].x = random()%640;
game->bodvars[i].y = random()%480;
}
}

int processEvents(SDL_Window *window, GameState *game){


SDL_Event event;

int done = 0;
while(SDL_PollEvent(&event)){
switch(event.type){
case SDL_WINDOWEVENT_CLOSE:
if(window) {
SDL_DestroyWindow(window);
window = NULL;
done = 1;
}
break;
case SDL_KEYDOWN:
switch(event.key.keysym.sym) {
case SDLK_ESCAPE:
done = 1;
break;
}
break;
case SDL_QUIT:
//quit out of the game
done = 1;
}

}

// uses to do the same thing with case SDLK_RIGHT,LEFT, DOWN or UP
const Uint8 *state = SDL_GetKeyboardState(NULL);
if(state[SDL_SCANCODE_LEFT]){

game->player.x -=10;
}
if(state[SDL_SCANCODE_RIGHT]){

game->player.x +=10;
}
if(state[SDL_SCANCODE_UP]){

game->player.y -=10;
}
if(state[SDL_SCANCODE_DOWN]){

game->player.y +=10;
}


return done;

}

void doRender(SDL_Renderer *renderer, GameState *game){

// set drawing to blue
SDL_SetRenderDrawColor(renderer, 0, 0, 255, 255);
// Clear the screen to blue
SDL_RenderClear(renderer);

// set the drawing color to white
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);

// x, y -- x is horizontal, y is vertical | the other numbers are the width and the height
SDL_Rect rect = {game->player.x, game->player.y, 200, 200};
SDL_RenderFillRect(renderer, &rect);

// draw bodvar
for (int i = 0; i < 100; ++i)
{

SDL_Rect bodvarRect = {game->bodvars[i].x, game->bodvars[i].y, 200, 400};
SDL_RenderCopy(renderer, game->bodvar, NULL, &bodvarRect);

}
// we are done drawing, "present" or show to the screen what we have drawn
SDL_RenderPresent(renderer);

}

int main(int argc, const char *argv[]){

GameState gameState;
SDL_Window *window = NULL; // declare a window
SDL_Renderer *renderer = NULL; // declare a renderer

SDL_Init(SDL_INIT_VIDEO); // initialize SDL2

gameState.renderer = renderer;
LoadGame(&gameState);

srandom((int)time(NULL));

// Create an application windows with the following settings:
window = SDL_CreateWindow("Game Window", // window title
SDL_WINDOWPOS_UNDEFINED, // initial x position
SDL_WINDOWPOS_UNDEFINED, // initial y position
640, // width in pixels
480, // height in pixels
0 // flags
);

renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);

int done = 0;

// Event loop
while(!done){

// Checks for events
done = processEvents(window, &gameState);

doRender(renderer, &gameState);

// wait a few seconds before quitting
// also resposible about the speed of animation
//SDL_Delay(10);

}


// Shutdown and unload all memory
SDL_DestroyTexture(gameState.bodvar);

// Close and destroy the window
SDL_DestroyWindow(window);
SDL_DestroyRenderer(renderer);

// Clean up
SDL_Quit();

return 0;

}

```

When I was not using the for loop it was fine but when I added it to multiple the image, it didn't work. Any suggestions would be appreciated! Thanks!

EDIT: I'm using Linux to build this game.

Parents Reply Children
No Data