Iâm just going to chime in as a FRP sort of guy, I agree there are good
cases for globals when it comes to constants, but be weary when globals are
modified during the lifetime of the app. If you have some sort of state
attached to kiss_sdl that is completely localized, you may want something
like:
struct {
// Things that would normally be global go in here
SDL_Renderer *renderer;
} KISS_STATE;
KISS_STATE *kiss_init(...);
From here, you have two options. One is you have a single swappable global
state, ie:
kiss_use_state(KISS_STATE *);
KISS_STATE *kiss_get_state(void);
KISS_STATE *kiss_copy_state();
// For any function that needs to use a global var, use kiss_get_state().
If you need to modify it, copy it, modify the copy, then kiss_use_state on
the copy.
This will break all the examples, but itâs only a few lines to change. You
could have kiss_init still rendering an SDL_Renderer * and create a global
KISS_STATE in the background, for compatibility. Youâd have a separate
KISS_STATE *kiss_init_state()
method that would eventually be the
migration path
Or you pass the KISS_STATE* into every function that needs access to the
global state. This is a much more painful refactor, but it has some
advantages with immutability. I could go on about the merits of this
approach, but the bottom line is that it is a very painful refactor, and
will break every example and every project using it.
But again, this is just opinion. The idea of your keeping it simple means
that what you currently have, along with globals, is probably the simplest,
so donât take what I wrote as heavy criticism or a need to refactor, just
thoughts.
On the brighter side, good job, this is a neat little ui kit to get a
simple app off the ground!On Mon, Jul 4, 2016 at 4:59 PM, actsl wrote:
It is ok to modify, it is made for that.
There is by itself nothing wrong in global variables, it is important how
they are used. Getting rid of global variables is the easiest way to avoid
all the problems they may cause, but it is just a no-brainer to solve the
problems. In kiss_sdl, global variables are supposed to be used as
constants only, and changed only during the initiation. When one really
does not like them, then it is ok to get rid of them, but i didnât do it
because i didnât want to make the code more complex. One more argument to
the functions, or one more member to the structures, all matters. Or if you
have some other reason why you want to get rid of them, have several sets
of settings or whatever, thatâs perfectly ok.
kiss_sdl - Simple universal GUI widget toolkit for SDL2
GitHub - actsl/kiss_sdl: Simple generic GUI widget toolkit for SDL2
SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org