beginner problem
Benjamin Hiltpolt

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)
2{
3 prebuffer = create_bitmap(maxview_x+tilesize,maxview_y+tilesize);
4 data = load_datafile("tiles.dat");
5 int p;
6 
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);
10
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);
25}

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

gnolam

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.

Derezo

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

class Map
{
  public:
    Map()
      {
        prebuffer = create_bitmap(maxview_x+tilesize,maxview_y+tilesize);
        data = load_datafile("tiles.dat");
      }
    ~Map()
      {
        destroy_bitmap(prebuffer);
        unload_datafile(data);
      }
}

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

LordHolNapul

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.
ciao

Benjamin Hiltpolt

thank you all for good and fast help!

Thread #572899. Printed from Allegro.cc