speeding up a while loop full of ifs and class function calls

My main WHILE loop is too slow, and I'm too ignorant to know where to go from here to speed things up. I think nested if statements would speed things up, but I don't know if that's bad practice or not. I thought perhaps storing some nInt values to the avatar class may speed things up to avoid the if statements involved in paintSpray(stuff,nInt) and others, but now I can figure how I'd store the nInt values without the same number of if statements. Perhaps the getStuff() class function calls are slower than if I made Stuff public and just did .stuff

Any recommendations or direction would be appreciated. Here's the section of code:

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
230
231
232
233
234
235
while ( !key[KEY_ESC] && aAvatar[0].getScore() < nWinScore && aAvatar[1].getScore() < nWinScore )
	{
		//readkey();

		//It seems like aAvatar[0] has trouble going downleft and downright while painting.
		if ( key[KEY_UP] )
		{
			moveAvatar(maTile, aAvatar[0], 0, -1);
			aAvatar[0].setDir('N');
		}
		if ( key[KEY_DOWN] )
		{
			moveAvatar(maTile, aAvatar[0], 0, 1);
			aAvatar[0].setDir('S');
		}
		if ( key[KEY_RIGHT] )
		{
			moveAvatar(maTile, aAvatar[0], 1, 0);
			aAvatar[0].setDir('E');
		}
		if ( key[KEY_LEFT] )
		{
			moveAvatar(maTile, aAvatar[0], -1, 0);
			aAvatar[0].setDir('W');
		}

				if ( key[KEY_W] )
		{
			moveAvatar(maTile, aAvatar[1], 0, -1);
			aAvatar[1].setDir('N');
		}
		if ( key[KEY_S] )
		{
			moveAvatar(maTile, aAvatar[1], 0, 1);
			aAvatar[1].setDir('S');
		}
		if ( key[KEY_D] )
		{
			moveAvatar(maTile, aAvatar[1], 1, 0);
			aAvatar[1].setDir('E');
		}
		if ( key[KEY_A] )
		{
			moveAvatar(maTile, aAvatar[1], -1, 0);
			aAvatar[1].setDir('W');
		}



        //SPRAY//this could be SET instead of looked up EVERY LOOP to increase game speed
		if ( key[KEY_INSERT] )
		{
			if ( aAvatar[0].getScore() < nSpray2Score )
			{
				aAvatar[0].paintSpray(maTile,1);
			}
			else if ( aAvatar[0].getScore() >= nSpray2Score &&  aAvatar[0].getScore() < nSpray3Score )
			{
				aAvatar[0].paintSpray(maTile,2);
			}
			else if ( aAvatar[0].getScore() >= nSpray3Score &&  aAvatar[0].getScore() < nSpray4Score )
			{
				aAvatar[0].paintSpray(maTile,3);
			}
			else if ( aAvatar[0].getScore() >= nSpray4Score &&  aAvatar[0].getScore() < nSpray5Score )
			{
				aAvatar[0].paintSpray(maTile,4);
			}
			else if ( aAvatar[0].getScore() >= nSpray5Score &&  aAvatar[0].getScore() < nSpray6Score )
			{
				aAvatar[0].paintSpray(maTile,5);
			}
			else if ( aAvatar[0].getScore() >= nSpray6Score &&  aAvatar[0].getScore() < nSpray7Score )
			{
				aAvatar[0].paintSpray(maTile,6);
			}
			else if ( aAvatar[0].getScore() >= nSpray7Score &&  aAvatar[0].getScore() < nSpray8Score )
			{
				aAvatar[0].paintSpray(maTile,7);
			}
			else if ( aAvatar[0].getScore() >= nSpray8Score &&  aAvatar[0].getScore() < nSpray9Score )
			{
				aAvatar[0].paintSpray(maTile,8);
			}
			else if ( aAvatar[0].getScore() >= nSpray9Score &&  aAvatar[0].getScore() < nSpray10Score )
			{
				aAvatar[0].paintSpray(maTile,9);
			}
			else if ( aAvatar[0].getScore() >= nSpray10Score &&  aAvatar[0].getScore() < nStreamScore )
			{
				aAvatar[0].paintSpray(maTile,10);
			}
			else if ( aAvatar[0].getScore() >= nStreamScore )
			{
				aAvatar[0].paintStream(maTile);
			}
		}
		//SPLASH//this could be SET instead of looked up EVERY LOOP to increase game speed
		if ( key[KEY_DEL] )
		{
			if ( aAvatar[0].getScore() < nSplash1Score )
			{
			}
			else if ( aAvatar[0].getScore() >= nSplash1Score &&  aAvatar[0].getScore() < nSplash2Score )
			{
				aAvatar[0].paintSplash(maTile,1);
			}
			else if ( aAvatar[0].getScore() >= nSplash2Score &&  aAvatar[0].getScore() < nSplash3Score )
			{
				aAvatar[0].paintSplash(maTile,2);
			}
			else if ( aAvatar[0].getScore() >= nSplash3Score &&  aAvatar[0].getScore() < nSplash4Score )
			{
				aAvatar[0].paintSplash(maTile,3);
			}
			else if ( aAvatar[0].getScore() >= nSplash4Score )
			{
				aAvatar[0].paintSplash(maTile,4);
			}
		}
		//BOMB
		if ( key[KEY_ALTGR] && aAvatar[0].getScore() >= nBombScore && aAvatar[0].getBombs() > 0 )
		{
			aAvatar[0].paintBomb(maTile);			
		}

		//SPRAY//this could be SET instead of looked up EVERY LOOP to increase game speed
		if ( key[KEY_H] )
		{

(...)

		}
		//SPLASH//this could be SET instead of looked up EVERY LOOP to increase game speed
		if ( key[KEY_G] )
		{

(...)

		}
		//BOMB
		if ( key[KEY_F]  && aAvatar[1].getScore() >= nBombScore && aAvatar[1].getBombs() > 0)
		{
			aAvatar[1].paintBomb(maTile);
		}


		
		++nRunLoops;
		nOldGameLevel = nGameLevel;
		nGameLevel = nRunLoops/nLoopsPerGameLevel+1;
		if ( nGameLevel > nOldGameLevel )
		{
			for (int i=0; i<2; i++)
			{
				aAvatar[i].addBombs(1);
			}
		}

		if ( rand()%10 <= nGameLevel )
		{
			for ( int i=0; i<nGameLevel; i++ )
			{
				int maTileX = rand()%20;
				int maTileY = rand()%20;
				maTile[maTileX][maTileY].setColor(nTileOffColor);
				acquire_screen();
				rectfill( buffer, nTileFieldBufferW+maTileX*nTileW, nTileFieldBufferH+maTileY*nTileH, nTileFieldBufferW+maTileX*nTileW+nTileW-1, nTileFieldBufferH+maTileY*nTileH+nTileH-1, nTileOffColor);
				release_screen();
				draw_sprite( screen, buffer, 0, 0);
			}
		}
		
		charOut(stringToChar("nRunLoops: "), 20, 510, nWhite);
		charOut(intToChar(nRunLoops), 120, 510, nWhite, 60);
		charOut(stringToChar("nGameLevel: "), 20, 500, nWhite);
		charOut(intToChar(nGameLevel), 120, 500, nWhite, 60);
        


		charOut(stringToChar("Red Team"), 20, 20, aAvatar[0].getColor());
		charOut(stringToChar("Score:"), 20, 30, aAvatar[0].getColor());
		charOut(intToChar(aAvatar[0].getScore()), 100, 30, aAvatar[0].getColor(), 40);

		charOut(stringToChar("'Ins' to SPRAY PAINT!"), 20, 50, aAvatar[0].getColor());
		if ( aAvatar[0].getScore() >= nSpray2Score ) { charOut(stringToChar("SPRAY increased! x2"), 20, 60, aAvatar[0].getColor()); }
		if ( aAvatar[0].getScore() >= nSpray3Score ) { charOut(stringToChar("SPRAY increased! x3"), 20, 60, aAvatar[0].getColor(), 170); }

(...)

		if ( aAvatar[0].getScore() >= nSpray9Score ) { charOut(stringToChar("SPRAY increased! x9"), 20, 60, aAvatar[0].getColor(), 170); }
		if ( aAvatar[0].getScore() >= nSpray10Score ) { charOut(stringToChar("SPRAY is a STREAM! x20"), 20, 60, aAvatar[0].getColor(), 170); }

		if ( aAvatar[0].getScore() >= nSplash1Score ) { charOut(stringToChar("'Del' to SPLASH PAINT!"), 20, 80, aAvatar[0].getColor()); }
		if ( aAvatar[0].getScore() >= nSplash2Score ) { charOut(stringToChar("SPLASH increased! x2"), 20, 90, aAvatar[0].getColor()); }
		if ( aAvatar[0].getScore() >= nSplash3Score ) { charOut(stringToChar("SPLASH increased! x3"), 20, 90, aAvatar[0].getColor(), 170); }
		if ( aAvatar[0].getScore() >= nSplash4Score ) { charOut(stringToChar("SPLASH increased! x4"), 20, 90, aAvatar[0].getColor(), 170); }

		if ( aAvatar[0].getScore() >= nBombScore )
		{
			charOut(stringToChar("'ALTGR' to throw PAINT BOMB!"), 20, 110, aAvatar[0].getColor());
			charOut(stringToChar("Bombs: "), 20, 120, aAvatar[0].getColor());
			charOut(intToChar(aAvatar[0].getBombs()), 120, 120, aAvatar[0].getColor(), 20);
		}



				charOut(stringToChar("Blue Team"), 620, 20, aAvatar[1].getColor());
		charOut(stringToChar("Score:"), 620, 30, aAvatar[1].getColor());
		charOut(intToChar(aAvatar[1].getScore()), 700, 30, aAvatar[1].getColor(), 40);

		charOut(stringToChar("'H' to SPRAY PAINT!"), 620, 50, aAvatar[1].getColor());
		if ( aAvatar[1].getScore() >= nSpray2Score ) { charOut(stringToChar("SPRAY increased! x2"), 620, 60, aAvatar[1].getColor()); }

		(...)

		if ( aAvatar[1].getScore() >= nSpray9Score ) { charOut(stringToChar("SPRAY increased! x9"), 620, 60, aAvatar[1].getColor(), 170); }
		if ( aAvatar[1].getScore() >= nSpray10Score ) { charOut(stringToChar("SPRAY is a STREAM! x20"), 620, 60, aAvatar[1].getColor(), 170); }

		if ( aAvatar[1].getScore() >= nSplash1Score ) { charOut(stringToChar("'G' to SPLASH PAINT!"), 620, 80, aAvatar[1].getColor()); }

(...)

		if ( aAvatar[1].getScore() >= nSplash4Score ) { charOut(stringToChar("SPLASH increased! x4"), 620, 90, aAvatar[1].getColor(), 170); }

		if ( aAvatar[1].getScore() >= nBombScore )
		{
			charOut(stringToChar("'F' to throw PAINT BOMB!"), 620, 110, aAvatar[1].getColor());
			charOut(stringToChar("Bombs: "), 620, 120, aAvatar[1].getColor());
			charOut(intToChar(aAvatar[1].getBombs()), 720, 120, aAvatar[1].getColor(), 20);
		}

		rest(nDelay);

	}


Thanks!
Nothing in the above function looks like it should be slow. Perhaps remove some of the text prints?

Your rectfill in the loop looks like it probably could take the majority of the time, although I don't know
what some of the other draw functions do.

I'd profile the functions acquire_screen(), rectfill(), release_screen(), drawSprite(), paintSplash(),
paintSpray(), and moveAvatar(). Those functions probably do a lot more work than your function
above.
Topic archived. No new replies allowed.