Allegro 5 Sand Game Slow

I've been making one of those sand games that are everywhere. I'm a beginner so the code is probably bad. The game works perfectly except whenever there is a lot of stuff on the screen. It slows down from drawing all of the tiny rectangles for each thing. I've tried making a bitmap for each one, but that seems to make it slower. Also, I deleted the update code and a lot of the beginning stuff some other stuff just so it was shorter to post.

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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
const int width = 640;
const int height = 480;
const int mapWidth = 320;
const int mapHeight = 240;
const int sandSize = 2;
int randNum;

char Map [mapHeight] [mapWidth];

int main() {
	while (!done) {
		ALLEGRO_EVENT ev;
		al_wait_for_event(event_queue, &ev);

		if (ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE)
			done = true;
		else if (ev.type == ALLEGRO_EVENT_TIMER) {
				redraw = true;

					frames++;
				if (al_current_time() - gameTime >= 1) {
					gameTime = al_current_time();
					gameFPS = frames;
					frames = 0;
				}

				UpdateSandNum(sandNum);

				if (checkDir == 1) {
					checkDir *= -1;
					for (int x = 0; x < mapWidth; x++)
					UpdateSand(x);
				}
				else if (checkDir == -1) {
					checkDir *= -1;
					for (int x = mapWidth; x >= 0; x--)
					UpdateSand(x);
				}
		}
		else if (ev.type == ALLEGRO_EVENT_MOUSE_BUTTON_DOWN) {
			if (ev.mouse.button == 1)
				mouseHold [0] = true;
			else if (ev.mouse.button == 2)
				mouseHold [1] = true;
			else if (ev.mouse.button == 3)
				ClearScreen();
		}
		else if (ev.type == ALLEGRO_EVENT_MOUSE_BUTTON_UP) {
			if (ev.mouse.button == 1)
				mouseHold [0] = false;
			else if (ev.mouse.button == 2)
				mouseHold [1] = false;
		}
		else if (ev.type == ALLEGRO_EVENT_MOUSE_AXES) {
			if (ev.mouse.x >= 0 && ev.mouse.x <= width && ev.mouse.y >= 0 && ev.mouse.y <= height) {
				mouseX = ev.mouse.x;
				mouseY = ev.mouse.y;
			}
			if (mouseZ != ev.mouse.z) {
				if (mouseZ < ev.mouse.z)
					select++;
				else
					select--;

				if (select >= maxSelect)
					select = 0;
				if (select < 0)
					select = maxSelect - 1;
				mouseZ = ev.mouse.z;
			}
		}
		else if (ev.type == ALLEGRO_EVENT_KEY_DOWN) {
			switch (ev.keyboard.keycode) {
			case ALLEGRO_KEY_ESCAPE:
				done = true;
				break;
			case ALLEGRO_KEY_R:
				rain = !rain;
				break;
			case ALLEGRO_KEY_UP:
				radius++;
				if (radius >= 20)
					radius = 20;
				break;
			case ALLEGRO_KEY_DOWN:
				radius--;
				if (radius <= 1)
					radius = 1;
				break;
			case ALLEGRO_KEY_M:
				dispMouse = !dispMouse;
				if (dispMouse)
					al_show_mouse_cursor(display);
				else if (!dispMouse)
					al_hide_mouse_cursor(display);
				break;
			case ALLEGRO_KEY_H:
				al_show_native_message_box(display, "Help", "Controls", "Up / Down: Change drawing radius,   Left mouse button: Create material,   Right mouse button: Erase,   Mouse wheel: Scroll through materials,   M: Toggle mouse, R: Toggle rain", NULL, NULL);
				break;
			}
		}

		if (mouseHold [0]) {
			for (int i = -radius; i <= radius; i++) {
				for (int j = -radius; j <= radius; j++) {
					distance = sqrt(pow(float(mouseX / sandSize - mouseX / sandSize + i), 2) + pow(float(mouseY / sandSize - mouseY / sandSize + j), 2));

					if (distance <= radius) {
						if (select != 3 && select != 7 && select != 10) {
							if (rand() % 30 == 0) {
								NewSand(mouseX + i, mouseY + j, select, false);
							}
						}
						else
							NewSand(mouseX + i, mouseY + j, select, false);
					}
				}
			}
		}
		else if (mouseHold [1]) {
			for (int i = -radius; i <= radius; i++) {
				for (int j = -radius; j <= radius; j++) {
					distance = sqrt(pow(float(mouseX / sandSize - mouseX / sandSize + i), 2) + pow(float(mouseY / sandSize - mouseY / sandSize + j), 2));
					if (distance <= radius)
						DeleteSand(mouseX + i, mouseY + j);
				}
			}
		}

		if (rain)
			MakeRain();

		if (redraw && al_is_event_queue_empty(event_queue)) {
			redraw = false;

			DrawSand();
			DrawSelect(select);
			//al_draw_textf(font18, al_map_rgb(255, 255, 255), 200, 0, 0, "Particles: %i", sandNum);
			al_draw_textf(font18, al_map_rgb(255, 255, 255), 580, 0, 0, "H: Help", gameFPS);
			if (!dispMouse)
				al_draw_circle(mouseX, mouseY, radius, al_map_rgb(255, 255, 255), 1);
			al_flip_display();
			al_clear_to_color(al_map_rgb(0,0,0));
		}
	}

	al_destroy_display(display);
	al_destroy_event_queue(event_queue);
	al_destroy_timer(timer);
	return 0;
}

void NewSand(int mouseX, int mouseY, int select, bool explode) {
	if (!explode) {
		if (Map [mouseY / sandSize] [mouseX / sandSize] == ' ' || Map [mouseY / sandSize] [mouseX / sandSize] == 'f') {
			if (select == 0)
				Map [mouseY / sandSize] [mouseX / sandSize] = 's';
			else if (select == 1)
				Map [mouseY / sandSize] [mouseX / sandSize] = 't';
			else if (select == 2)
				Map [mouseY / sandSize] [mouseX / sandSize] = 'w';
			else if (select == 3)
				Map [mouseY / sandSize] [mouseX / sandSize] = 'a';
			else if (select == 4)
				Map [mouseY / sandSize] [mouseX / sandSize] = 'c';
			else if (select == 5)
				Map [mouseY / sandSize] [mouseX / sandSize] = 'e';
			else if (select == 6)
				Map [mouseY / sandSize] [mouseX / sandSize] = 'f';
			else if (select == 7)
				Map [mouseY / sandSize] [mouseX / sandSize] = 'p';
			else if (select == 8)
				Map [mouseY / sandSize] [mouseX / sandSize] = 'l';
			else if (select == 9)
				Map [mouseY / sandSize] [mouseX / sandSize] = 'i';
			else if (select == 10)
				Map [mouseY / sandSize] [mouseX / sandSize] = 'x';
			else if (select == 11)
				Map [mouseY / sandSize] [mouseX / sandSize] = 'm';
			else if (select == 12)
				Map [mouseY / sandSize] [mouseX / sandSize] = 'q';
		}
	}
	else if (explode) {
		if (Map [mouseY / sandSize] [mouseX / sandSize] != 'a') {
			if (mouseY / sandSize >= 1 && mouseY / sandSize <= mapHeight - 1 && mouseX / sandSize >= 1 && mouseX / sandSize <= mapWidth - 1) {
				Map [mouseY / sandSize] [mouseX / sandSize] = 'f';
			}
		}
	}
}
void DrawSand() {
	for (int y = 0; y < mapHeight; y++) {
		for (int x = 0; x < mapWidth; x++) {
			if (Map [y] [x] == 's')
				al_draw_filled_rectangle(x * sandSize, y * sandSize, x * sandSize + sandSize, y * sandSize + sandSize, al_map_rgb(255, 191, 0));
			else if (Map [y] [x] == 't')
				al_draw_filled_rectangle(x * sandSize, y * sandSize, x * sandSize + sandSize, y * sandSize + sandSize, al_map_rgb(142, 134, 134));
			else if (Map [y] [x] == 'w')
				al_draw_filled_rectangle(x * sandSize, y * sandSize, x * sandSize + sandSize, y * sandSize + sandSize, al_map_rgb(20, 24, 255));
			else if (Map [y] [x] == 'a')
				al_draw_filled_rectangle(x * sandSize, y * sandSize, x * sandSize + sandSize, y * sandSize + sandSize, al_map_rgb(150, 37, 0));
			else if (Map [y] [x] == 'c')
				al_draw_filled_rectangle(x * sandSize, y * sandSize, x * sandSize + sandSize, y * sandSize + sandSize, al_map_rgb(21, 255, 0));
			else if (Map [y] [x] == 'e')
				al_draw_filled_rectangle(x * sandSize, y * sandSize, x * sandSize + sandSize, y * sandSize + sandSize, al_map_rgb(107, 203, 255));
			else if (Map [y] [x] == 'f')
				al_draw_filled_rectangle(x * sandSize, y * sandSize, x * sandSize + sandSize, y * sandSize + sandSize, al_map_rgb(255, 0, 0));
			else if (Map [y] [x] == 'p')
				al_draw_filled_rectangle(x * sandSize, y * sandSize, x * sandSize + sandSize, y * sandSize + sandSize, al_map_rgb(0, 255, 21));
			else if (Map [y] [x] == 'n')
				al_draw_filled_rectangle(x * sandSize, y * sandSize, x * sandSize + sandSize, y * sandSize + sandSize, al_map_rgb(0, 186, 21));
			else if (Map [y] [x] == 'l')
				al_draw_filled_rectangle(x * sandSize, y * sandSize, x * sandSize + sandSize, y * sandSize + sandSize, al_map_rgb(255, 67, 0));
			else if (Map [y] [x] == 'i')
				al_draw_filled_rectangle(x * sandSize, y * sandSize, x * sandSize + sandSize, y * sandSize + sandSize, al_map_rgb(181, 251, 255));
			else if (Map [y] [x] == 'x')
				al_draw_filled_rectangle(x * sandSize, y * sandSize, x * sandSize + sandSize, y * sandSize + sandSize, al_map_rgb(255, 211, 135));
			else if (Map [y] [x] == 'z')
				al_draw_filled_rectangle(x * sandSize, y * sandSize, x * sandSize + sandSize, y * sandSize + sandSize, al_map_rgb(226, 187, 120));
			else if (Map [y] [x] == 'm')
				al_draw_filled_rectangle(x * sandSize, y * sandSize, x * sandSize + sandSize, y * sandSize + sandSize, al_map_rgb(22, 22, 22));
			else if (Map [y] [x] == 'q')
				al_draw_filled_rectangle(x * sandSize, y * sandSize, x * sandSize + sandSize, y * sandSize + sandSize, al_map_rgb(163, 192, 192));
			else if (Map [y] [x] == 'r')
				al_draw_filled_rectangle(x * sandSize, y * sandSize, x * sandSize + sandSize, y * sandSize + sandSize, al_map_rgb(73, 255, 63));
		}
	}
}
I have never worked with Allegro but I will try and give some advice.

Calling al_draw_filled_rectangle many times will definitely create some lag but even with thousands of rectangles most modern computers should be able to handle it pretty well, I would comment out your DrawSand() function temporarily and make sure that is the main reason for any slow down effects. Are you using an old graphics card?

You shouldn't be cycling through so many if statements every loop. Seeing as the only thing between each 'letter' that seems to change is the color you should be storing each letter/color in some sort of data structure (for example a map) and just loop over the map until it find the right letter. Make some sort of data structure to hold the relationship between the select number and letter too for your NewSand function.
http://www.cplusplus.com/reference/map/map/map/

To avoid calling al_draw_filled_rectangle for every object you could just put every vertex/color into 1 big array/vector then send the entire thing each frame using. https://www.allegro.cc/manual/5/al_draw_prim That can be hard to understand at first but google some examples about it and it shouldn't be too bad.

That should get you started at least.
Last edited on
Topic archived. No new replies allowed.