From: Holger Schemel Date: Fri, 18 Sep 1998 14:48:43 +0000 (+0200) Subject: rnd-19980918 X-Git-Tag: 1.2.0^2~81 X-Git-Url: https://git.artsoft.org/?a=commitdiff_plain;h=c4baa69cc8d5e8398425e67498b49f4b77f8e477;p=rocksndiamonds.git rnd-19980918 --- diff --git a/src/Makefile b/src/Makefile index cd1c80e9..3bba1da0 100644 --- a/src/Makefile +++ b/src/Makefile @@ -97,7 +97,7 @@ clean: $(RM) $(OBJS) BACKUP_DIR = /home/aeglos/BACKUP/rnd -BACKUP_DATE = `date +%y%m%d` +BACKUP_DATE = `date +%Y%m%d` BACKUP_FILE = $(BACKUP_DIR)/rnd-$(BACKUP_DATE).tgz backup: diff --git a/src/cartoons.c b/src/cartoons.c index b1b31921..b8efebf3 100644 --- a/src/cartoons.c +++ b/src/cartoons.c @@ -181,7 +181,7 @@ void HandleAnimation(int mode) if (anim_restart) { - if (!DelayReached(&animstart_delay,animstart_delay_value)) + if (!DelayReached(&animstart_delay, animstart_delay_value)) return; toon_nr = SimpleRND(NUM_TOONS); @@ -232,7 +232,7 @@ BOOL AnimateToon(int toon_nr, BOOL restart) JUMPER_FPS, JUMPER_STEPSIZE, ANIM_NORMAL, - ANIMDIR_LEFT, + ANIMDIR_RIGHT, ANIMPOS_DOWN }, { @@ -461,7 +461,7 @@ BOOL AnimateToon(int toon_nr, BOOL restart) pos_y >= FULL_SYSIZE + anim->stepsize) return(TRUE); - if (!DelayReached(&anim_delay,anim_delay_value)) + if (!DelayReached(&anim_delay, anim_delay_value)) { if (game_status==HELPSCREEN && !restart) DrawAnim(anim_pixmap,anim_clip_gc, diff --git a/src/editor.c b/src/editor.c index 17677e8d..fef23e2b 100644 --- a/src/editor.c +++ b/src/editor.c @@ -19,6 +19,8 @@ #include "buttons.h" #include "files.h" +#define CHOICE_DELAY_VALUE 100 + static int level_xpos,level_ypos; static BOOL edit_mode; static BOOL name_typing; @@ -676,7 +678,7 @@ void LevelEd(int mx, int my, int button) if (((choice == ED_BUTTON_EUP && element_shift>0) || (choice == ED_BUTTON_EDOWN && element_shift=0) { - if (!DelayReached(&choice_delay,10)) + if (!DelayReached(&choice_delay, CHOICE_DELAY_VALUE)) break; if (lev_fieldx<2*SCR_FIELDX-2) break; @@ -763,7 +765,7 @@ void LevelEd(int mx, int my, int button) case ED_BUTTON_RIGHT: if (level_xpos<=lev_fieldx-2*SCR_FIELDX) { - if (!DelayReached(&choice_delay,10)) + if (!DelayReached(&choice_delay, CHOICE_DELAY_VALUE)) break; if (lev_fieldx<2*SCR_FIELDX-2) break; @@ -780,7 +782,7 @@ void LevelEd(int mx, int my, int button) case ED_BUTTON_UP: if (level_ypos>=0) { - if (!DelayReached(&choice_delay,10)) + if (!DelayReached(&choice_delay, CHOICE_DELAY_VALUE)) break; if (lev_fieldy<2*SCR_FIELDY-2) break; @@ -797,7 +799,7 @@ void LevelEd(int mx, int my, int button) case ED_BUTTON_DOWN: if (level_ypos<=lev_fieldy-2*SCR_FIELDY) { - if (!DelayReached(&choice_delay,10)) + if (!DelayReached(&choice_delay, CHOICE_DELAY_VALUE)) break; if (lev_fieldy<2*SCR_FIELDY-2) break; @@ -864,7 +866,8 @@ void LevelEd(int mx, int my, int button) int choice = CheckCountButtons(mx,my,button); int step = (button==1 ? 1 : button==2 ? 5 : button==3 ? 10 : 0); - if (choice>=0 && choice<36 && DelayReached(&choice_delay,10)) + if (choice >= 0 && choice < 36 && + DelayReached(&choice_delay, CHOICE_DELAY_VALUE)) { if (!(choice % 2)) step = -step; diff --git a/src/events.c b/src/events.c index 73d681d7..02a13795 100644 --- a/src/events.c +++ b/src/events.c @@ -516,9 +516,9 @@ void HandleKey(KeySym key, int key_status) case XK_8: case XK_9: if (key == XK_0) - GameSpeed = 50; + GameSpeed = 500; else - GameSpeed = key - XK_0; + GameSpeed = (key - XK_0) * 10; printf("GameSpeed == %d\n", GameSpeed); break; diff --git a/src/game.c b/src/game.c index 0561c22c..384d7431 100644 --- a/src/game.c +++ b/src/game.c @@ -2041,7 +2041,7 @@ void AmoebeWaechst(int x, int y) { MovDelay[x][y] = 7; - if (DelayReached(&sound_delay,sound_delay_value)) + if (DelayReached(&sound_delay, sound_delay_value)) { PlaySoundLevel(x,y,SND_AMOEBE); sound_delay_value = 30; @@ -2597,8 +2597,8 @@ void GameActions() if (PlayerMovPos) ScrollFigure(0); - while(!DelayReached(&action_delay, action_delay_value)) - Delay(5); + /* main game synchronization point */ + WaitUntilDelayReached(&action_delay, action_delay_value); if (tape.pausing || (tape.playing && !TapePlayDelay())) return; @@ -2727,7 +2727,7 @@ void GameActions() } } - if (TimeLeft>0 && TimeFrames>=(100/GameSpeed) && !tape.pausing) + if (TimeLeft>0 && TimeFrames>=(1000/GameSpeed) && !tape.pausing) { TimeFrames = 0; TimeLeft--; diff --git a/src/main.c b/src/main.c index fd9804cc..aa4d14bd 100644 --- a/src/main.c +++ b/src/main.c @@ -64,6 +64,7 @@ int fading_on = FALSE; int autorecord_on = FALSE; int joystick_nr = 0; int quick_doors = FALSE; +int cpu_friendly = TRUE; BOOL redraw[MAX_BUF_XSIZE][MAX_BUF_YSIZE]; int redraw_x1 = 0, redraw_y1 = 0; diff --git a/src/main.h b/src/main.h index 3d8267d5..d34fbb96 100644 --- a/src/main.h +++ b/src/main.h @@ -302,6 +302,7 @@ extern int fading_on; extern int autorecord_on; extern int joystick_nr; extern int quick_doors; +extern int cpu_friendly; extern BOOL redraw[MAX_BUF_XSIZE][MAX_BUF_YSIZE]; extern int redraw_x1, redraw_y1; diff --git a/src/misc.c b/src/misc.c index 28d04a6d..944458f5 100644 --- a/src/misc.c +++ b/src/misc.c @@ -24,19 +24,45 @@ #include #include -void microsleep(unsigned long usec) +static unsigned long mainCounter(int mode) { - if (usec < 5000) + static struct timeval base_time = { 0, 0 }; + struct timeval current_time; + unsigned long counter_ms; + + gettimeofday(¤t_time, NULL); + + if (mode == INIT_COUNTER || current_time.tv_sec < base_time.tv_sec) + base_time = current_time; + + counter_ms = (current_time.tv_sec - base_time.tv_sec) * 1000 + + (current_time.tv_usec - base_time.tv_usec) / 1000; + + return counter_ms; /* return milliseconds since last init */ +} + +void InitCounter() /* set counter back to zero */ +{ + mainCounter(INIT_COUNTER); +} + +unsigned long Counter() /* get milliseconds since last call of InitCounter() */ +{ + return(mainCounter(READ_COUNTER)); +} + +static void sleep_milliseconds(unsigned long milliseconds_delay) +{ + if (milliseconds_delay < 5 || !cpu_friendly) { /* we want to wait less than 5 ms -- if we assume that we have a kernel timer resolution of 10 ms, we would wait far to long; therefore it's better to do a short interval of busy waiting to get our sleeping time more accurate */ - long base_counter = Counter(), actual_counter = Counter(); - long delay = usec/1000; + unsigned long base_counter = Counter(), actual_counter = Counter(); - while (actual_counter < base_counter+delay && + while (actual_counter < base_counter + milliseconds_delay && actual_counter >= base_counter) actual_counter = Counter(); } @@ -44,79 +70,52 @@ void microsleep(unsigned long usec) { struct timeval delay; - delay.tv_sec = usec / 1000000; - delay.tv_usec = usec % 1000000; + delay.tv_sec = milliseconds_delay / 1000; + delay.tv_usec = 1000 * (milliseconds_delay % 1000); - if (select(0,NULL,NULL,NULL,&delay) != 0) - fprintf(stderr,"%s: in function microsleep: select failed!\n", + if (select(0, NULL, NULL, NULL, &delay) != 0) + fprintf(stderr,"%s: in function sleep_milliseconds: select() failed!\n", progname); } } -long mainCounter(int mode) +void Delay(unsigned long delay) /* Sleep specified number of milliseconds */ { - static struct timeval base_time = { 0, 0 }; - struct timeval current_time; - long counter_ms; - - gettimeofday(¤t_time,NULL); - if (mode == INIT_COUNTER || current_time.tv_sec < base_time.tv_sec) - base_time = current_time; - - counter_ms = (current_time.tv_sec - base_time.tv_sec)*1000 - + (current_time.tv_usec - base_time.tv_usec)/1000; - - return counter_ms; /* return milliseconds since last init */ -} - -void InitCounter() /* set counter back to zero */ -{ - mainCounter(INIT_COUNTER); -} - -long Counter() /* get milliseconds since last call of InitCounter() */ -{ - return(mainCounter(READ_COUNTER)); + sleep_milliseconds(delay); } -void WaitCounter(long value) /* wait for counter to reach value */ +BOOL FrameReached(unsigned long *frame_counter_var, unsigned long frame_delay) { - long wait; + unsigned long actual_frame_counter = FrameCounter; - while((wait=value-Counter())>0) - microsleep(wait * 1000); -} + if (actual_frame_counter < *frame_counter_var+frame_delay && + actual_frame_counter >= *frame_counter_var) + return(FALSE); -void Delay(long value) /* Delay 'value' milliseconds */ -{ - microsleep(value * 1000); + *frame_counter_var = actual_frame_counter; + return(TRUE); } -BOOL DelayReached(long *counter_var, int delay) +BOOL DelayReached(unsigned long *counter_var, unsigned long delay) { - long actual_counter = Counter(); + unsigned long actual_counter = Counter(); - if (actual_counter >= *counter_var+delay || actual_counter < *counter_var) - { - *counter_var = actual_counter; - return(TRUE); - } - else + if (actual_counter < *counter_var + delay && + actual_counter >= *counter_var) return(FALSE); + + *counter_var = actual_counter; + return(TRUE); } -BOOL FrameReached(long *frame_counter_var, int frame_delay) +void WaitUntilDelayReached(unsigned long *counter_var, unsigned long delay) { - long actual_frame_counter = FrameCounter; + unsigned long actual_counter = Counter(); - if (actual_frame_counter >= *frame_counter_var+frame_delay || - actual_frame_counter < *frame_counter_var) - { - *frame_counter_var = actual_frame_counter; - return(TRUE); - } - else - return(FALSE); + if (actual_counter < *counter_var + delay && actual_counter >= *counter_var) + sleep_milliseconds(*counter_var + delay - actual_counter); + + *counter_var = actual_counter; } char *int2str(int ct, int nr) diff --git a/src/misc.h b/src/misc.h index 9641265f..a6a8fb01 100644 --- a/src/misc.h +++ b/src/misc.h @@ -22,14 +22,12 @@ #define NEW_RANDOMIZE -1 -void microsleep(unsigned long); -long mainCounter(int); void InitCounter(void); -long Counter(void); -void WaitCounter(long); -void Delay(long); -BOOL DelayReached(long *, int); -BOOL FrameReached(long *, int); +unsigned long Counter(void); +void Delay(unsigned long); +BOOL FrameReached(unsigned long *, unsigned long); +BOOL DelayReached(unsigned long *, unsigned long); +void WaitUntilDelayReached(unsigned long *, unsigned long); char *int2str(int, int); unsigned int SimpleRND(unsigned int); unsigned int RND(unsigned int); diff --git a/src/screens.c b/src/screens.c index 907878bf..9cc9235e 100644 --- a/src/screens.c +++ b/src/screens.c @@ -146,7 +146,7 @@ void HandleMainMenu(int mx, int my, int dx, int dy, int button) if (new_level_nr>leveldir[leveldir_nr].levels-1) new_level_nr = leveldir[leveldir_nr].levels-1; - if (old_level_nr==new_level_nr || !DelayReached(&level_delay,15)) + if (old_level_nr==new_level_nr || !DelayReached(&level_delay,150)) goto out; level_nr = new_level_nr; diff --git a/src/tools.c b/src/tools.c index efcba4db..08c6dc68 100644 --- a/src/tools.c +++ b/src/tools.c @@ -1538,8 +1538,7 @@ unsigned int MoveDoor(unsigned int door_state) for(x=start; x<=DXSIZE; x+=stepsize) { - while(!DelayReached(&door_delay, door_delay_value/10)) - Delay(1); + WaitUntilDelayReached(&door_delay, door_delay_value); if (door_state & DOOR_ACTION_1) {