I am writing a SDL program in C and I am using raycastlib. I set a global variable in a header file. I change it in main() and then call another function. This function uses the old value of the variable.
I have tried giving the new variable as an argument to the function, but that function called another function and it broke again. I don't want to add this as an argument to literally all functions in the header (yes this function uses almost all functions).
void draw()
{
RCL_RayConstraints c;
c.maxHits = 32;
c.maxSteps = 32;
RCL_renderComplex(camera,floorHeightAt,ceilingHeightAt,textureA
t,c);
}
This is the function as it is. The code is based on the example from raycastlib. It just tries to procedurelly generate the world (levelFloor in the code).
If this is not possible I will need to set the variable from the start (that is okay, the map doesn't change) but I will need to find a way to pass an array from a generating function to a variable without the main function to start the function.
The header file:
#define MAZE_WIDTH 25
#define MAZE_HEIGHT 25
int levelFloor[MAZE_WIDTH * MAZE_HEIGHT]; /* THIS VARIABLE IS THE PROBLEM */
static void shuffle(int *array, size_t n)
{
/* stuff */
/* returns nothing (but changes levelFloor) */
}
The part of main loop that uses these:
/* stuff */
#define STARTX (MAZE_WIDTH - (MAZE_WIDTH % 4))/2
#define STARTY (MAZE_HEIGHT - (MAZE_HEIGHT % 4))/2
MAZE_Recurse(STARTX,STARTY,levelFloor);
#undef STARTX
#undef STARTY
/* checked if it is changed here. it is. */
and
draw(levelFloor) /* I also tried changing the function and passing levelFloor as an argument which worked but didn't effect the other called function */
the draw() function:
void draw(int levelFloor[]) /* originally I didn't pass levelFloor as an argument */
{
RCL_RayConstraints c;
c.maxHits = 32;
c.maxSteps = 32;
RCL_renderComplex(camera,floorHeightAt,ceilingHeightAt,textureA
t,c);
}
RCL_RenderComplex comes from raycastlib.
To abstract the code is like this:
/*main.c*/
#include "xar.h"
#include "bar.h"
void foo()
{
bar();
}
int main()
{
xar();
foo();
}
/*bar.h*/
void bar()
{
stuff();
}
/*xar.h*/
int var[];
void xar()
{
var = 1;
return;
}
Thanks.
EDIT: I think I am going to rewrite the program.
The main issue seems to be that you declare variables/arrays in headers. That is bad practice, since it means that the variable will only be available to a certain translation unit. That is:
The work-around is to declare the array as extern int array [n];
in the header file foo.h and in the file foo.c only, define the variable int array[n] = { ... };
.
However that using "globals" with extern
a poor design as well, since it exposes a variable to multiple files. This is called "tight coupling" or sometimes "spaghetti programming", where a poor design forces unrelated parts of the program to depend on each other. Change the variable in one file and poof: some unrelated file also using that variable suddenly stops working. Meaning you'd have to track down all uses of the variable to solve the bug. Also these are generally not safe for multi-threading.
A better solution is to share data between unrelated files through so-called "setter/getter" functions. Then it becomes clear who the data belongs to.