after initializing SDL_Event, the background image wont render. but commenting out the parts related to event renders everything as expected, what am i doing wrong?
55 int i = 1;
56 SDL_Event event;
57 while(i == 1) {
58 while (SDL_PollEvent(&event)) {
59 switch (event.type) {
60 case SDL_QUIT:
61 i = 2;
62 default:
63 continue;
64 }
65 }
66 f_r.x -= 10;
67 if (f_r.x < 10) {f_r.x = 1200;}
68 bg_r.x -=2;
69 if (bg_r.x < -1200) {bg_r.x = 0;}
70 SDL_RenderClear(renderer);
71 SDL_RenderCopy(renderer, bg_tex, NULL, &bg_r); // <- this does not render
72 SDL_RenderCopy(renderer, fire_tex, NULL, &f_r); // <- this works
73 SDL_RenderCopy(renderer, tex, NULL, &player); // ,<- this works
74 SDL_RenderPresent(renderer);
75 SDL_Delay(16);
76
77 }
I would check that the bg_tex texture is valid and also that the bg_r contains correct values.
The x field is being set in the while loop but check that the y, w and h fields are valid too. I have sometimes made the mistake by not setting the w and h fields correctly, which has ended up having a rect of (0, 0) in size, which, together with that rect, won’t render anything.
You should still leave out the continue statement, since it’s not needed, and replace it with break or leave out the default case entirely. It would only be useful if you had some additional code after the switch statement in the while loop that you wanted to skip. As your code currently stands, there’s nothing after the switch block, so it jumps back up to the top of that while loop anyway.
As to the rest of it, if simply adding one variable (the SDL_Event) breaks your code then you might have stack corruption. Are you surebg_tex and bg_r are valid with the SDL_Event?
Also, why are you calling SDL_Delay()? If you want vertical sync, create your renderer with the SDL_RENDERER_PRESENTVSYNC flag, since that will make it hook into the OS’s hooks for syncing up with the display. SDL_Delay() guarantees only that it will delay for at least that much time; it could delay for a lot longer.