creating a level editor

Hi. I'm trying to create a level editor for a game I'm building. The program runs fine when it just displays the grid and potential objects, but I can't figure out how to implement storing objects onto the map surface and/ or displaying the map during creation. I know it is because I'm passing around too much information (namely a 560 * 560 array) and thus it crashes at runtime. How can I avoid having to reload this information every animation frame? The code shown compiles and runs fine; the bad functions are commented out. I was thinking of looping between the animation loop and a storing loop (for every click) and using writeimagefile and putimage.

P.S. Any tips/ advice on my programming would be much appreciated.

Main Program:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
int main()
{
    int mx, my;
    string shape = KB_H;
    int color = BLACK;
//    int map[S+OS][S+OS];
//    int size = 1;

    initwindow(S + OS, S + OS, "Map editor by Derek Gann", 0, 0, true);
//    create_map(map);
    
    while(true)
    {
	mx = mousex();
	my = mousey();
	
	clearviewport();
	setfillstyle(SOLID_FILL, RED);
	bar(130, 130, 430, 430);

	process_kbhit(shape, color);
	track_mouse(shape, color, mx, my);
//	process_click(map);
//	animate_map(map);
	draw_grid();
	swapbuffers();
	delay(1000/FPS);
    }

    return EXIT_SUCCESS;
}



Troubling functions:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
void process_click(int color_array[][S+OS])
{
    if(ismouseclick(WM_LBUTTONDOWN))
    {
	create_map(color_array);
    }
}
//-------------------------------------------------
void create_map(int color_array[][S+OS])
{
    int i, j;
    for(i = 0; i < S + OS; i++)
    {
	for(j = 0; i < S + OS; j++)
	{
	    color_array[i][j] = getpixel(i, j);
	}
    }
}
//-------------------------------------------------
void animate_map(const int color_array[][S+OS])
{
    int i, j;
    for(i = 0; i < S + OS; i++)
    {
	for(j = 0; i < S + OS; j++)
	{
	    putpixel(i, j, color_array[i][j]);
	}
    }
}


Thanks!
Last edited on
Don't copy the whole array to the function. When you pass an argument to a function like you are doing a copy of that object is created on the stack. For large objects like this you should be passing by reference:
 
void animate_map(const int &color_array[][S+OS]) //Note the '&' 

Using the reference operator is basically a short form for pointers. Any changes to the parameter you pass will occur to the object you pass in. There is no local copy:
1
2
3
4
5
6
7
8
9
10
void Foo(int &Bar)
{
Bar+=3;
}
int main()
{
int I = 0;
Foo(I); //Now I will be 3.
return 0;
}
I just tried this and I still get a runtime crash with clean compilation. How is it any better to pass an array of pointers equal to the size of the array?
I figured it out. I created a void pointer called map and passed it around. then used getimage and putimage functions (from bgi library) in the create_map and animate_map functions respectively. i.e.
1
2
3
4
5
6
7
8
9
10
11
12
void create_map(void* map)
{
    getimage(0, 0, S + OS, S + OS, map);
}
//-------------------------------------------------
void animate_map(void* map)
{
    if(map == NULL)
    {}
    else
    {putimage(0, 0, map, COPY_PUT);}
}


runs quite nicely

p.s. if you'd like to check out the code or the game, give me a holler
Last edited on
Topic archived. No new replies allowed.