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
--- 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);