I have an array of textures that I want to render in the render loop. When I'm going through the array, I don't get any of the textures actually displayed on the window.
Here's how I'm initializing the textures I wanna use:
SDL_Texture* tracks[5];
SDL_Rect trackRects[5];
for (int i = 1; i < 6; i++) {
char buf[20];
snprintf(buf, 20, ".\\assets\\track%d.png", i);
tracks[i-1] = IMG_LoadTexture(appRenderer, buf);
trackRects[i-1].x = 15;
trackRects[i-1].y = 28*(i-1) + 150;
}
To render all of these, I'm trying to do this:
for (int i = 0; i < 5; i++) {
res = SDL_RenderCopy(appRenderer, tracks[i], NULL, &trackRects[i]);
if (res < 0) printError();
}
This does not return any error, and it compiles, but when I start my program it displays none of the images.
Full code:
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <stdio.h>
int running = 1;
SDL_Window* appWindow = NULL;
SDL_Surface* appSurface = NULL;
SDL_Renderer* appRenderer = NULL;
int res;
int main(int argc, char* argv[]) {
int videoerror = SDL_Init(SDL_INIT_VIDEO);
if (videoerror < 0) printError();
SDL_Window* appWindow = SDL_CreateWindow("name", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 1000, 800, SDL_WINDOW_SHOWN);
int imgerror = IMG_Init(IMG_INIT_PNG);
if (imgerror < 0) printError();
SDL_Renderer* appRenderer = SDL_CreateRenderer(appWindow, -1, SDL_RENDERER_SOFTWARE);
SDL_Texture* tracks[5];
SDL_Rect trackRects[5];
for (int i = 1; i < 6; i++) {
char buf[20];
snprintf(buf, 20, ".\\assets\\track%d.png", i);
tracks[i-1] = IMG_LoadTexture(appRenderer, buf);
trackRects[i-1].x = 15;
trackRects[i-1].y = 28*(i-1) + 150;
}
while (running == 1) {
SDL_Event event;
while (SDL_PollEvent(&event) != 0) {
if (event.type == SDL_QUIT) {
running = 0;
}
}
SDL_RenderClear(appRenderer);
for (int i = 0; i < 5; i++) {
res = SDL_RenderCopy(appRenderer, tracks[i], NULL, &trackRects[i]);
if (res < 0) printError();
}
SDL_RenderPresent(appRenderer);
SDL_Delay(100);
}
SDL_DestroyWindow(appWindow);
appWindow = NULL;
IMG_Quit();
SDL_Quit();
return 0;
}
Prefaced by my top comments ...
I've added the setup of
.wand.hand added a [crude] placement:In the code above, I've used
cppconditionals to denote old vs. new code:Note: this can be cleaned up by running the file through
unifdef -kI used random images on my system. Here is the output: