Memory issue

I've recently been playing around with Allegro and I managed to make a very simple 2d game. It's a grid game which loads up a 5 by 5 grid of images. The placement of the images is determined by their coordinates. The user uses the direction keys on the keyboard to reset the coordinates and then recreate the grid around the center square.

It actually works exactly the way I was hoping, although when I opened up task manager to see how much memory it was consuming it appeared to be growing rapidly upon a direction button being pressed. If I had to guess I'd say it was because I'm piling images on top of each other. Is there a way I can sort of clear the old grid before my program fills in the new one? Or is the problem more serious? Any help is appreciated, cheers.

Here's the code below:

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <allegro.h>
#include <sstream>
using namespace std;
BITMAP* buffer;
int x=0,y=0;
string grass[] = {"1:1","2:2","0"};
bool in_array(string haystack[],string needle)
{
     int i=0;
     while(true)
     {
           if(haystack[i] == "0")
                  return false;
           if(haystack[i] == needle)
                  return true;     
           i++;
     }
}
string intToStr(int i)
{
       string s;
       stringstream ss;
       ss << i;
       s = ss.str();
       return s;
}
void setupGame()
{
    buffer = create_bitmap(200,200);
    int tX = x - 2;
    int tY = y + 2;
    clear_to_color(buffer,makecol(0,0,0));
    for(int i = 0; i <= 5; i++)
    {
        for(int t = 0; t <= 5; t++)
        {
                string coords = intToStr(tX) + ":" + intToStr(tY);
                if(i == 2 && t == 2)
                     rectfill(buffer,t*40,i*40,(t+1)*40,(i+1)*40,makecol(255,0,0));
                else if(in_array(grass,coords) == true)
                     rectfill(buffer,t*40,i*40,(t+1)*40,(i+1)*40,makecol(0,0,200));
                else
                     rectfill(buffer,t*40,i*40,(t+1)*40,(i+1)*40,makecol(0,255,0));
                tX++;
                if(t == 5)
                      tX -= 6;
        }
        tY--;
    }
    draw_sprite(screen,buffer,0,0);
}
void moveChar()
{
     clear_keybuf();
     if(key[KEY_UP])
     {
            y++;        
     }
     if(key[KEY_DOWN])
     {
            y--;        
     }
     if(key[KEY_LEFT])
     {
            x--;        
     }
     if(key[KEY_RIGHT])
     {
            x++;        
     }
}
int main()
{
    allegro_init();
    install_keyboard();
    set_color_depth(16);
    set_gfx_mode(GFX_AUTODETECT_WINDOWED,200,200,0,0);
    setupGame();
    while(!key[KEY_ESC])
    {
          readkey();
          moveChar();
          setupGame();        
    }
    return 0;
}
END_OF_MAIN();
I've just added destroy_bitmap(buffer) on line 28. It seems to have sorted the problem, but will this cause any further problems? Thanks
Topic archived. No new replies allowed.