You have implemented your own rectangle collision check routine when you really didn't have to. SDL provides the function SDL_IntersectRect()
which tests two SDL_Rect
s for intersection and also returns the intersection rect of A and B.
Now lets talk about coding practices a little bit.
It looks like you are an adept of the single return single return statement. In the initialize_game()
function, for instance, it has lead to a lot of juggling and flag checking to ensure the single return. In that case, it only made the code more complex. You should have just done it like so:
if(SDL_Init(SDL_INIT_VIDEO) < 0) {
return false;
}
... stuff ...
if(globalWindow == NULL) {
return false;
}
Prefer to return early and avoid deep nesting. It makes logic easier to follow. The single return technique is hardly applicable to modern C++. The RAII idiom covers dynamic resource management inside functions.
Other minor things:
You have implemented your own rectangle collision check routine when you really didn't have to. SDL provides the function SDL_IntersectRect()
which tests two SDL_Rect
s for intersection and also returns the intersection rect of A and B.
Now lets talk about coding practices a little bit.
It looks like you are an adept of the single return statement. In the initialize_game()
function, for instance, it has lead to a lot of juggling and flag checking to ensure the single return. In that case, it only made the code more complex. You should have just done it like so:
if(SDL_Init(SDL_INIT_VIDEO) < 0) {
return false;
}
... stuff ...
if(globalWindow == NULL) {
return false;
}
Prefer to return early and avoid deep nesting. It makes logic easier to follow. The single return technique is hardly applicable to modern C++. The RAII idiom covers dynamic resource management inside functions.
Other minor things:
You have implemented your own rectangle collision check routine when you really didn't have to. SDL provides the function SDL_IntersectRect()
which tests two SDL_Rect
s for intersection and also returns the intersection rect of A and B.
Now lets talk about coding practices a little bit.
It looks like you are an adept of the single return statement. In the initialize_game()
function, for instance, it has lead to a lot of juggling and flag checking to ensure the single return. In that case, it only made the code more complex. You should have just done it like so:
if(SDL_Init(SDL_INIT_VIDEO) < 0) {
return false;
}
... stuff ...
if(globalWindow == NULL) {
return false;
}
Prefer to return early and avoid deep nesting. It makes logic easier to follow. The single return technique is hardly applicable to modern C++. The RAII idiom covers dynamic resource management inside functions.
Other minor things:
You have implemented your own rectangle collision check routine when you really didn't have to. SDL provides the function SDL_IntersectRect()
which tests two SDL_Rect
s for intersection and also returns the intersection rect of A and B.
Now lets talk about coding practices a little bit.
It looks like you are an adept of the single return statement. In the initialize_game()
function, for instance, it has lead to a lot of juggling and flag checking to ensure the single return. In that case, it only made the code more complex. You should have just done it like so:
if(SDL_Init(SDL_INIT_VIDEO) < 0) {
return false;
}
... stuff ...
if(globalWindow == NULL) {
return false;
}
Prefer to return early and avoid deep nesting. It makes logic easier to follow. The single return technique is hardly applicable to modern C++. The RAII idiom covers dynamic resource management inside functions.
Other minor things: