#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;
if (((choice == ED_BUTTON_EUP && element_shift>0) ||
(choice == ED_BUTTON_EDOWN &&
element_shift<elements_in_list-MAX_ELEM_X*MAX_ELEM_Y)) &&
- DelayReached(&choice_delay,10))
+ DelayReached(&choice_delay, CHOICE_DELAY_VALUE))
{
int i, step;
case ED_BUTTON_LEFT:
if (level_xpos>=0)
{
- if (!DelayReached(&choice_delay,10))
+ if (!DelayReached(&choice_delay, CHOICE_DELAY_VALUE))
break;
if (lev_fieldx<2*SCR_FIELDX-2)
break;
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;
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;
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;
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;
#include <sys/param.h>
#include <sys/types.h>
-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();
}
{
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)