1
votes

I am new to SDL programming. And in one of the tutorials, http://twinklebeardev.blogspot.in/2012/07/lesson-2-dont-put-everything-in-main.html it is mentioned that the SDL Window,and SDL Renderer should not be declared as global?

What is the technical underpinning behind it ?

3

3 Answers

0
votes

There is no reason as far as c standard and sdl library are concerned, to not use SDL Window and SDL Renderer as global variables.

0
votes

A think is more a recommendation than a mandatory order. If they are declared locally, you will structure your code better and is easier figure out where these are being used. If you declare them as global you can quickly forget what parts of your code are using it and some small change in a part of it can break everything without giving you any clue about it.

The rule of thumb is never declare any globals except if extremely necessary.

0
votes

There is nothing technically preventing the use of globals, it's more a matter of taste and choice of language.

The author of the tutorial is speaking from the point of view of using SDL from the c++ language. In C++ there is a strong emphasis on object-oriented code structure, including encapsulation and abstraction (i.e. hiding the details of an implementation behind a class structure).

The author goes into more details regarding global objects and classes in tutorial 7, which might be useful to get a better understanding of their design process.