1 /***********************************************************************
2  * ��������Ĵ������ (�����ƥ��¸)
3  *
4  *	�ܺ٤ϡ� wait.h ����
5  ************************************************************************/
6 
7 #include <stdio.h>
8 #include <SDL.h>
9 
10 #include "quasi88.h"
11 #include "initval.h"
12 #include "wait.h"
13 
14 
15 
16 /*---------------------------------------------------------------------------*/
17 static	int	wait_do_sleep;			/* idle���� sleep ����       */
18 
19 static	int	wait_counter = 0;		/* Ϣ³������֥����С�������*/
20 static	int	wait_count_max = 10;		/* ����ʾ�Ϣ³�����С�������
21 						   ��ö,����Ĵ������������ */
22 
23 /* �������Ȥ˻��Ѥ�����֤�����ɽ���ϡ� usñ�̤Ȥ��롣 (ms�������٤��㤤�Τ�)
24 
25    SDL �λ�������ؿ� SDL_GetTicks() �� ms ñ�̤ǡ� unsigned long �����֤���
26    ����� 1000�ܤ��� (us���Ѵ�����) ���Ѥ���ȡ�71ʬ�Ƿ夢�դ줷�Ƥ��ޤ��Τǡ�
27    ����ɽ���� long long ���ˤ��褦��
28 
29    �ʤ��� SDL_GetTicks �� 49���ܤ���ä�(wrap)���ޤ��Τǡ�����ɽ���⤳�νִ֤�
30    �������ʤ�Τˤʤ� (�������Ȼ��֤��Ѥˤʤ�) �������ˤ��ʤ����Ȥˤ��롣 */
31 
32 #ifdef SDL_HAS_64BIT_TYPE
33 typedef	Sint64		T_WAIT_TICK;
34 #else
35 typedef	long		T_WAIT_TICK;
36 #endif
37 
38 static	T_WAIT_TICK	next_time;		/* ���ե졼��λ��� */
39 static	T_WAIT_TICK	delta_time;		/* 1 �ե졼��λ��� */
40 
41 
42 
43 /* ---- ���߻����������� (usecñ��) ---- */
44 
45 #define	GET_TICK()	((T_WAIT_TICK)SDL_GetTicks() * 1000)
46 
47 
48 
49 
50 
51 /****************************************************************************
52  * ��������Ĵ�������ν��������λ
53  *****************************************************************************/
wait_vsync_init(void)54 int	wait_vsync_init(void)
55 {
56     if (! SDL_WasInit(SDL_INIT_TIMER)) {
57 	if (SDL_InitSubSystem(SDL_INIT_TIMER) != 0) {
58 	    if (verbose_wait) printf("Error Wait (SDL)\n");
59 	    return FALSE;
60 	}
61     }
62 
63     return TRUE;
64 }
65 
wait_vsync_exit(void)66 void	wait_vsync_exit(void)
67 {
68 }
69 
70 
71 
72 /****************************************************************************
73  * ��������Ĵ������������
74  *****************************************************************************/
wait_vsync_setup(long vsync_cycle_us,int do_sleep)75 void	wait_vsync_setup(long vsync_cycle_us, int do_sleep)
76 {
77     wait_counter = 0;
78 
79 
80     delta_time = (T_WAIT_TICK) vsync_cycle_us;		/* 1�ե졼����� */
81     next_time  = GET_TICK() + delta_time;		/* ���ե졼����� */
82 
83     wait_do_sleep = do_sleep;				/* Sleep ̵ͭ */
84 }
85 
86 
87 
88 /****************************************************************************
89  * �������Ƚ���
90  *****************************************************************************/
wait_vsync_update(void)91 int	wait_vsync_update(void)
92 {
93     int slept   = FALSE;
94     int on_time = FALSE;
95     T_WAIT_TICK diff_ms;
96 
97 
98     diff_ms = (next_time - GET_TICK()) / 1000;
99 
100     if (diff_ms > 0) {			/* �٤�Ƥʤ�(���֤�;�äƤ���)�ʤ� */
101 					/* diff_ms �ߥ��á��������Ȥ���     */
102 
103 	if (wait_do_sleep) {		/* ���֤����ޤ� sleep ������ */
104 
105 #if 1	    /* ��ˡ (1) */
106 	    SDL_Delay((Uint32) diff_ms);	/* diff_ms �ߥ��á��ǥ��쥤 */
107 	    slept = TRUE;
108 
109 #else	    /* ��ˡ (2) */
110 	    if (diff_ms < 10) {			/* 10ms̤���ʤ�ӥ�����������*/
111 		while (GET_TICK() <= next_time)
112 		    ;
113 	    } else {				/* 10ms�ʾ�ʤ�ǥ��쥤      */
114 		SDL_Delay((Uint32) diff_ms);
115 		slept = TRUE;
116 	    }
117 #endif
118 
119 	} else {			/* ���֤����ޤ�Tick��ƻ뤹���� */
120 
121 	    while (GET_TICK() <= next_time)
122 		;				/* �ӥ����������� */
123 	}
124 
125 	on_time = TRUE;
126     }
127 
128     if (slept == FALSE) {	/* ���٤� SDL_Delay ���ʤ��ä���� */
129 	SDL_Delay(1);			/* for AUDIO thread ?? */
130     }
131 
132 
133     /* ���ե졼�������� */
134     next_time += delta_time;
135 
136 
137     if (on_time) {			/* ������˽����Ǥ��� */
138 	wait_counter = 0;
139     } else {				/* ������˽����Ǥ��Ƥ��ʤ� */
140 	wait_counter ++;
141 	if (wait_counter >= wait_count_max) {	/* �٤줬�Ҥɤ����� */
142 	    wait_vsync_setup((long) delta_time,	/* �������Ȥ�����   */
143 			     wait_do_sleep);
144 	}
145     }
146 
147 #if 0
148     {
149 	static int x = 0, y = 0;
150 	if (++x == 55) {
151 	    y++;
152 	    x = 0;
153 	    printf("wait %d\n", y);
154 	    fflush(stdout);
155 	}
156     }
157 #endif
158 
159     if (on_time) return WAIT_JUST;
160     else         return WAIT_OVER;
161 }
162