I'm was following this tutorial from Pikuma on how to setup a basic C/SDL2 gameloop, and it was all working really well. I was able to finish the tutorial and my program was running fine.
The problem apeared when I tried to split the code into multiple files(in the tutorial he was doing everything in the main function, besides some constants declaration)
I am still able to create the window and delete it, and I can still call my render function. BUT the render function doesn´t reconize my renderer. It was suposed to draw a simple red line in a gray background, but it is not working anymore ever since I split my functions into different files.
here it is how my files and code is arranged:
main.c
#include <stdio.h>
#include <SDL2/SDL.h>
#include "./constants.h"
#include "./init_and_destroy.h"
#include "./render.h"
//globals
int game_is_running = FALSE;
SDL_Window* my_window = NULL;
SDL_Renderer* my_renderer = NULL;
int last_frame_time = 0;
void setup() {
}
void process_input() {
SDL_Event event;
SDL_PollEvent(&event); //reference to event
switch (event.type) {
case SDL_QUIT:
game_is_running = FALSE;
break;
case SDL_KEYDOWN:
if(event.key.keysym.sym == SDLK_ESCAPE)
game_is_running = FALSE;
break;
}
}
void update() {
//---------------------------------FPS AND DELTA_TIME LOGIC---------------------------------------
//caping frame rate
int time_to_wait = FRAME_TARGET_TIME - (SDL_GetTicks() - last_frame_time);
if (time_to_wait > 0 && time_to_wait <= FRAME_TARGET_TIME) {
SDL_Delay(time_to_wait);
}
//delta time in seconds
float delta_time = (SDL_GetTicks() - last_frame_time) / 1000.0f;
last_frame_time = SDL_GetTicks();
//------------------------------------------------------------------------------------------------
//UPDATE LOGIC GOES HERE
}
int main() {
game_is_running = initialize_window(my_window, my_renderer);
setup();
while(game_is_running) {
process_input();
update();
render(my_renderer);
}
destroy_window(my_window, my_renderer);
return 0;
}
init_and_destroy.c
#include <stdio.h>
#include <SDL2/SDL.h>
#include "./constants.h"
#include "./init_and_destroy.h"
int initialize_window(SDL_Window* window, SDL_Renderer* renderer) {
if (SDL_Init(SDL_INIT_EVERYTHING) != 0) {
fprintf(stderr, "Error initializing SDL.\n");
return FALSE;
}
window = SDL_CreateWindow(
"My Window",
SDL_WINDOWPOS_CENTERED, //x position
SDL_WINDOWPOS_CENTERED, //y position
WINDOW_WIDTH,
WINDOW_HEIGHT,
0
);
if(!window) {
fprintf(stderr, "Error creating SDL Window.\n");
return FALSE;
}
renderer = SDL_CreateRenderer(window, -1, 0);
if(!renderer) {
fprintf(stderr, "Error creating SDL Renderer.\n");
return FALSE;
}
return TRUE;
}
void destroy_window(SDL_Window* window, SDL_Renderer* renderer) {
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
}
render.c
#include <stdio.h>
#include <SDL2/SDL.h>
#include "./constants.h"
#include "./render.h"
void render(SDL_Renderer* renderer) {
SDL_SetRenderDrawColor(renderer, 50, 50, 50, 255);
SDL_RenderClear(renderer);
//draw a red line
SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
for(int i = 0; i < WINDOW_WIDTH; i++) {
SDL_RenderDrawPoint(renderer, i, i);
}
SDL_RenderPresent(renderer); //swap buffer
}
and my header files:
render.h
#ifndef RENDER
#define RENDER
void render(SDL_Renderer* renderer);
#endif
init_and_destroy.h
#ifndef INIT_AND_DESTROY
#define INIT_AND_DESTROY
int initialize_window(SDL_Window* window, SDL_Renderer* renderer);
void destroy_window(SDL_Window* window, SDL_Renderer* renderer);
#endif
I tried just declaring the render function without passing any parameters, and now I'm trying passing my SDL_Renderer as a parameter, and still nothing work, all I can see is a black window.
I see someone posted the answer already.
Just one quick tip: remember that we should loop the return of SDL_PollEvent
.
void process_input() {
SDL_Event event;
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_QUIT:
game_is_running = FALSE;
break;
case SDL_KEYDOWN:
if (event.key.keysym.sym == SDLK_ESCAPE)
game_is_running = FALSE;
break;
}
}
}
We add this loop in one of the later lectures of the course.