- Online Community Forums » Programming Questions » beginner problem

Credits go to Derezo, gnolam, and LordHolNapul for helping out!
This thread is locked; no one can reply to it. rss feed Print
beginner problem
Benjamin Hiltpolt
Member #6,733
December 2005

Hi all!

I decided to write my first game. I have less programming experience so i think i did some beginners mistakes.

the code looks like that:

1void Map::draw(BITMAP *buffer)
3 prebuffer = create_bitmap(maxview_x+tilesize,maxview_y+tilesize);
4 data = load_datafile("tiles.dat");
5 int p;
7 //Point (x,y) where to start draw
8 Point startpoint (viewpoint.getX() - maxview_x - tilesize,viewp.getY() - maxview_y - tilesize);
9 Point endpointp (viewpoint.getX() + maxview_x + tilesize,viewp.getY() + maxview_y + tilesize);
11 for (int x = startpoint.getX(); x < endpoint.getX();x+=tilesize)
12 {
13 for (int y = startpoint.getY(); y < endpoint.getY();y+=tilesize)
14 {
15 //check if tile is still on map
16 if (!Point(x,y).onMap()) p = 0;
17 else {
18 p=this.getTile(x,y);
19 }
20 draw_sprite(prebuffer,(BITMAP*)data[p].dat,x+lborder,y+uborder);
21 }
22 }
23 draw_sprite
24 (buffer, prebuffer,0,0);

Hope the code is clear to understand.

Now if I start the game it draws everything allright, but the problem is that the space requirement is increasing permanently.
So after a while my computer crashes because of a lack of free memory space.
My game runs at 70 fps, but when i ran it with 10 fps i got the same problems.
Or do you suggest I did something else wrong?

Where do I have to free my memory or what shall i do?
thx for all help comments and stuff :)
mfg mastasucka

Member #2,030
March 2002

1) Every call to a load_ or create_ function must be matched with a corresponding destroy_ call when you're done with the resource you loaded or created.

2) You're loading all your graphics and creating your buffers within the drawing loop itself. Never do this. Load them once, at the beginning of your program, and then unload them at the end.

Move to the Democratic People's Republic of Vivendi Universal (formerly known as Sweden) - officially democracy- and privacy-free since 2008-06-18!

Member #1,666
April 2001

It's best to use constructors for something like this. :)
For example:

class Map
        prebuffer = create_bitmap(maxview_x+tilesize,maxview_y+tilesize);
        data = load_datafile("tiles.dat");

[edit: my blocky coding style looks scary on these forums :P]

"He who controls the stuffing controls the Universe"

Member #3,619
June 2003

You should study the "Dynamic Allocation in the Heap" to understand what you are doing. Search a C++ manual that speak about "new" and "delete", or "alloc"/"malloc" and "free"....

When you have understood this, proceed to read the allegro manual using the .CHM help file.

Best regards.

Benjamin Hiltpolt
Member #6,733
December 2005

thank you all for good and fast help!

Go to: