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 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
|
Description: Fix the 100% CPU usage of alex4.
Fix the 100% CPU usage of alex4. We can't do this properly (the game
is written with a DOS mindset), but we can do two things easily:
- Put a rest() in every busy loop.
- Limit the frame rate to 50Hz (the speed of the game logic).
.
For future reference, the counters used by the game:
game_count = increments at 50Hz
cycle_count = increments at 50Hz, but is reset whenever the game feels like it
frame_count = number of frames rendered, reset every second
logic_count = number of game loops processed, reset every second
Forwarded: no
Author: Peter De Wachter <pdewacht@gmail.com>
Last-Update: 2009-05-07
--- a/src/main.c
+++ b/src/main.c
@@ -280,7 +280,7 @@
if (keypressed()) kp = 1;
if (is_fire(&ctrl) || is_jump(&ctrl)) kp = 1;
if (got_sound && dp != NULL) al_poll_duh(dp);
- while(!cycle_count);
+ rest(20);
}
}
@@ -322,7 +322,7 @@
cycle_count = 0;
if (got_sound && duh_player != NULL) al_poll_duh(duh_player);
i ++;
- while(!cycle_count) yield_timeslice();
+ while(!cycle_count) rest(1);
}
}
@@ -1127,6 +1127,10 @@
cycle_count = 0;
while(mode != 3) {
+ // Let other processes play
+ while(cycle_count == 0)
+ rest(1);
+
// do logic
while(cycle_count > 0) {
logic_count ++;
@@ -1152,9 +1156,6 @@
cycle_count --;
}
- // let other processes play
- yield_timeslice();
-
// draw stuff
draw_frame(swap_screen, 1);
draw_sprite(swap_screen, go, x, 35);
@@ -1174,6 +1175,10 @@
cycle_count = 0;
while(mode != 3) {
+ // let other processes play
+ while(cycle_count == 0)
+ rest(1);
+
// do logic
while(cycle_count > 0) {
logic_count ++;
@@ -1194,9 +1199,6 @@
cycle_count --;
}
- // let other processes play
- yield_timeslice();
-
// draw stuff
draw_frame(swap_screen, 1);
draw_sprite(swap_screen, go, x, 35);
@@ -1237,6 +1239,10 @@
cycle_count = 0;
while(!done) {
+ // let other processes play
+ while(cycle_count == 0)
+ rest(1);
+
// do logic
while(cycle_count > 0) {
logic_count ++;
@@ -1256,9 +1262,6 @@
cycle_count --;
}
- // let other processes play
- yield_timeslice();
-
// draw stuff
draw_custom_ending(swap_screen);
blit_to_screen(swap_screen);
@@ -1334,6 +1337,10 @@
clear_keybuf();
cycle_count = 0;
while(mode != 3) {
+ // let other processes play
+ while(cycle_count == 0)
+ rest(1);
+
// do logic
while(cycle_count > 0) {
logic_count ++;
@@ -1372,9 +1379,6 @@
cycle_count --;
}
- // let other processes play
- yield_timeslice();
-
// draw stuff
blit(swap2, swap_screen, 0, 0, 0, 0, 160, 120);
draw_status_bar(swap_screen, 110);
@@ -1488,6 +1492,10 @@
cycle_count = 0;
while(!done) {
+ // let other processes play
+ while(cycle_count == 0)
+ rest(1);
+
// do logic
while(cycle_count > 0) {
logic_count ++;
@@ -2343,7 +2351,7 @@
if (is_fire(&ctrl) || is_jump(&ctrl)) done = 1;
if (keypressed()) done = 1;
if (key[KEY_ESC]) done = -1;
- yield_timeslice();
+ rest(20);
}
if (done == -1) {
@@ -2369,6 +2377,9 @@
game_status = GS_OK;
cycle_count = 0;
while(game_status == GS_OK) {
+ // let other processes play
+ while(cycle_count == 0)
+ rest(1);
// do logic
while(cycle_count > 0) {
@@ -2492,9 +2503,6 @@
cycle_count --;
}
- // let other processes play
- yield_timeslice();
-
// draw
frame_count ++;
draw_frame(swap_screen, 1);
@@ -2658,7 +2666,7 @@
}
- while(!cycle_count);
+ while(!cycle_count) rest(1);
}
}
@@ -2693,6 +2701,9 @@
clear_keybuf();
cycle_count = 0;
while(status == GS_OK) {
+ // let other processes play
+ while(cycle_count == 0)
+ rest(1);
// do logic
while(cycle_count > 0) {
@@ -2774,9 +2785,6 @@
cycle_count --;
}
- // let other processes play
- yield_timeslice();
-
// draw
frame_count ++;
draw_title(swap_screen, tick);
@@ -3021,7 +3029,8 @@
blit_to_screen(swap_screen);
fade_in_pal(100);
cycle_count = 0;
- while(!key[KEY_ESC] && cycle_count < 200);
+ while(!key[KEY_ESC] && cycle_count < 200)
+ rest(50);
fade_out_pal(100);
clear(screen);
}
--- a/src/script.c
+++ b/src/script.c
@@ -102,8 +102,8 @@
cycle_count = 0;
poll_music();
count ++;
- while(!cycle_count);
- yield_timeslice();
+ while(!cycle_count)
+ rest(20);
}
if (key[KEY_ESC]) script_done = -1;
}
@@ -498,6 +498,10 @@
cycle_count = 0;
while(loops && !script_done) {
+ // let other processes play
+ while(cycle_count == 0)
+ rest(1);
+
while(cycle_count > 0 && loops && !script_done) {
logic_count ++;
@@ -520,9 +524,6 @@
cycle_count --;
}
- // let other processes play
- yield_timeslice();
-
// blit buffer to swap buffer
blit(buffer, swap_buffer, 0, 0, 0, 0, 160, 120);
--- a/src/shooter.c
+++ b/src/shooter.c
@@ -1131,7 +1131,10 @@
game_count = 0;
s_activate_sign(0, -1);
while(playing || s_sign.alive) {
-
+ // let other processes play
+ while(cycle_count == 0)
+ rest(1);
+
// do logic
while(cycle_count > 0) {
logic_count ++;
@@ -1266,10 +1269,6 @@
cycle_count --;
}
-
- // let other processes play
- yield_timeslice();
-
// draw
frame_count ++;
s_draw_frame(s_buffer);
|