Skip to main content
Code Review

Return to Answer

replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

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_Rects 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:

  • NULL is not C++! NULL is a C library macro. Modern C++ should use nullptr.

  • Did you code everything up in the same file? If so, it is probably time to move each class to it's own module.

  • Give std::cout a try. You might end up liking it ;).

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_Rects 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:

  • NULL is not C++! NULL is a C library macro. Modern C++ should use nullptr.

  • Did you code everything up in the same file? If so, it is probably time to move each class to it's own module.

  • Give std::cout a try. You might end up liking it ;).

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_Rects 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:

  • NULL is not C++! NULL is a C library macro. Modern C++ should use nullptr.

  • Did you code everything up in the same file? If so, it is probably time to move each class to it's own module.

  • Give std::cout a try. You might end up liking it ;).

Source Link
glampert
  • 17.3k
  • 4
  • 31
  • 89

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_Rects 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:

  • NULL is not C++! NULL is a C library macro. Modern C++ should use nullptr.

  • Did you code everything up in the same file? If so, it is probably time to move each class to it's own module.

  • Give std::cout a try. You might end up liking it ;).

lang-cpp

AltStyle によって変換されたページ (->オリジナル) /