1 // Run module
2 #include "burner.h"
3 #include "pg.h"
4 //#include "app.h"
5 //#include "kailleraclient.h"
6 
7 
8 //stolen from psp sdl
9 #include <time.h>
10 #include <sys/time.h>
11 
12 static struct timeval start;
13 
PspStartTicks(void)14 void PspStartTicks(void)
15 {
16 	gettimeofday(&start, NULL);
17 }
18 
PspGetTicks(void)19 unsigned int PspGetTicks(void)
20 {
21 	struct timeval now;
22 	unsigned int ticks;
23 
24 	gettimeofday(&now, NULL);
25 	ticks=(now.tv_sec-start.tv_sec)*1000+(now.tv_usec-start.tv_usec)/1000;
26 	return(ticks);
27 }
28 
PspDelay(unsigned int ms)29 void PspDelay(unsigned int ms)
30 {
31 	const unsigned int max_delay = 0xffffffffUL / 1000;
32 	if(ms > max_delay)
33 		ms = max_delay;
34 	sceKernelDelayThreadCB(ms * 1000);
35 }
36 
37 //end of stolen. :)
38 
39 
40 bool bAltPause = 0;
41 
42 int bAlwaysDrawFrames = 0;
43 
44 static bool bShowFPS = false;
45 
46 //int kNetGame = 0;							// Non-zero if Kaillera is being used
47 
48 int counter;								// General purpose variable used when debugging
49 
50 static unsigned int nNormalLast = 0;		// Last value of timeGetTime()
51 static int nNormalFrac = 0;					// Extra fraction we did
52 
53 static bool bAppDoStep = 0;
54 static bool bAppDoFast = 0;
55 static int nFastSpeed = 6;
56 
GetInput(bool bCopy)57 static int GetInput(bool bCopy)
58 {
59 	static int i = 0;
60 	InputMake(bCopy); 						// get input
61 
62 	// Update Input dialog ever 3 frames
63 	if (i == 0) {
64 		//InpdUpdate();
65 	}
66 
67 	i++;
68 
69 	if (i >= 3) {
70 		i = 0;
71 	}
72 
73 	// Update Input Set dialog
74 	//InpsUpdate();
75 	return 0;
76 }
77 
DisplayFPS()78 static void DisplayFPS()
79 {
80 	static time_t fpstimer;
81 	static unsigned int nPreviousFrames;
82 
83 	char fpsstring[8];
84 	time_t temptime = clock();
85 	float fps = static_cast<float>(nFramesRendered - nPreviousFrames) * CLOCKS_PER_SEC / (temptime - fpstimer);
86 	sprintf(fpsstring, "%2.1f", fps);
87 	//VidSNewShortMsg(fpsstring, 0xDFDFFF, 480, 0);
88 	pgPrint(1,1,0xFFFFFF,fpsstring,0);
89 
90 	fpstimer = temptime;
91 	nPreviousFrames = nFramesRendered;
92 }
93 
94 // define this function somewhere above RunMessageLoop()
ToggleLayer(unsigned char thisLayer)95 void ToggleLayer(unsigned char thisLayer)
96 {
97 	nBurnLayer ^= thisLayer;				// xor with thisLayer
98 	VidRedraw();
99 	VidPaint(0);
100 }
101 
102 
103 
104 // With or without sound, run one frame.
105 // If bDraw is true, it's the last frame before we are up to date, and so we should draw the screen
RunFrame(int bDraw,int bPause)106 static int RunFrame(int bDraw, int bPause)
107 {
108 	static int bPrevPause = 0;
109 	static int bPrevDraw = 0;
110 
111 	if (bPrevDraw && !bPause) {
112 		VidPaint(0);							// paint the screen (no need to validate)
113 		DisplayFPS();
114 	}
115 
116 	if (!bDrvOkay) {
117 		return 1;
118 	}
119 
120 	if (bPause)
121 	{
122 		GetInput(false);						// Update burner inputs, but not game inputs
123 		if (bPause != bPrevPause)
124 		{
125 			VidPaint(2);                        // Redraw the screen (to ensure mode indicators are updated)
126 		}
127 	}
128 	else
129 	{
130 		nFramesEmulated++;
131 		nCurrentFrame++;
132 
133 	/*	{
134 		if (nReplayStatus == 2) {
135 			GetInput(false);				// Update burner inputs, but not game inputs
136 			if (ReplayInput()) {			// Read input from file
137 				bAltPause = 1;
138 				bRunPause = 1;
139 				MenuEnableItems();
140 				InputSetCooperativeLevel(false, false);
141 			}
142 		}
143 		else
144 	*/	{
145 			GetInput(true);					// Update inputs
146 		}
147 	}
148 /*	if (nReplayStatus == 1) {
149 		RecordInput();						// Write input to file
150 	}
151 */	if (bDraw) {
152 		nFramesRendered++;
153 		if (VidFrame()) {					// Do one frame
154 			AudBlankSound();
155 
156 		}
157 	}
158 	else {								// frame skipping
159 		pBurnDraw = NULL;					// Make sure no image is drawn
160 		BurnDrvFrame();
161 	}
162 	bPrevPause = bPause;
163 	bPrevDraw = bDraw;
164 
165 	return 0;
166 }
167 
168 
169 // Callback used when DSound needs more sound
RunGetNextSound(int bDraw)170 static int RunGetNextSound(int bDraw)
171 {
172 	if (nAudNextSound == NULL) {
173 		return 1;
174 	}
175 
176 	if (bRunPause) {
177 		if (bAppDoStep) {
178 			RunFrame(bDraw, 0);
179 			memset(nAudNextSound, 0, nAudSegLen << 2);	// Write silence into the buffer
180 		} else {
181 			RunFrame(bDraw, 1);
182 		}
183 
184 		bAppDoStep = 0;									// done one step
185 		return 0;
186 	}
187 
188 	if (bAppDoFast) {									// do more frames
189 		for (int i = 0; i < nFastSpeed; i++) {
190 			RunFrame(0, 0);
191 		}
192 	}
193 
194 	// Render frame with sound
195 	pBurnSoundOut = nAudNextSound;
196 	RunFrame(bDraw, 0);
197 /*
198 	if (WaveLog != NULL && pBurnSoundOut != NULL) {		// log to the file
199 		fwrite(pBurnSoundOut, 1, nBurnSoundLen << 2, WaveLog);
200 		pBurnSoundOut = NULL;
201 	}
202 */
203 	if (bAppDoStep) {
204 		memset(nAudNextSound, 0, nAudSegLen << 2);		// Write silence into the buffer
205 	}
206 	bAppDoStep = 0;										// done one step
207 
208 	return 0;
209 }
210 
RunIdle()211 int RunIdle()
212 {
213 	int nTime, nCount;
214 
215 	if (bAudPlaying) {
216 		// Run with sound
217 		AudSoundCheck();
218 		return 0;
219 	}
220 
221 	// Run without sound
222 	nTime = PspGetTicks() - nNormalLast;
223 	nCount = (nTime * nAppVirtualFps - nNormalFrac) / 100000;
224 	if (nCount <= 0) {						// No need to do anything for a bit
225 		PspDelay(2);
226 
227 		return 0;
228 	}
229 
230 	nNormalFrac += nCount * 100000;
231 	nNormalLast += nNormalFrac / nAppVirtualFps;
232 	nNormalFrac %= nAppVirtualFps;
233 
234 	if (bAppDoFast){						// Temporarily increase virtual fps
235 		nCount *= nFastSpeed;
236 	}
237 	if (nCount > 100) {						// Limit frame skipping
238 		nCount = 100;
239 	}
240 	if (bRunPause) {
241 		if (bAppDoStep) {					// Step one frame
242 			nCount = 10;
243 		} else {
244 			RunFrame(1, 1);					// Paused
245 			return 0;
246 		}
247 	}
248 	bAppDoStep = 0;
249 
250 	for (int i = nCount / 10; i > 0; i--) {	// Mid-frames
251 		RunFrame(!bAlwaysDrawFrames, 0);
252 	}
253 	RunFrame(1, 0);							// End-frame
254 	// temp added for SDLFBA
255 	//VidPaint(0);
256 	return 0;
257 }
258 
RunReset()259 int RunReset()
260 {
261 	// Reset the speed throttling code
262 	nNormalLast = 0; nNormalFrac = 0;
263 	if (!bAudPlaying) {
264 		// run without sound
265 		nNormalLast = PspGetTicks();
266 	}
267 	return 0;
268 }
269 
RunInit()270 static int RunInit()
271 {
272 	// Try to run with sound
273 	AudSetCallback(RunGetNextSound);
274 	AudSoundPlay();
275 
276 	RunReset();
277 
278 	return 0;
279 }
280 
RunExit()281 static int RunExit()
282 {
283 	nNormalLast = 0;
284 	// Stop sound if it was playing
285 	AudSoundStop();
286 	return 0;
287 }
288 
289 // The main message loop
RunMessageLoop()290 int RunMessageLoop()
291 {
292 	int bRestartVideo;
293 	int finished= 0;
294 	do {
295 		bRestartVideo = 0;
296 
297 		//MediaInit();
298 
299 		if (!bVidOkay) {
300 
301 			// Reinit the video plugin
302 			VidInit();
303 			if (!bVidOkay && nVidFullscreen) {
304 
305 				nVidFullscreen = 0;
306 
307 		//		MediaExit(bRestartVideo);
308 		//		MediaInit();
309 				VidInit();
310 			}
311 			if (!nVidFullscreen) {
312 				//ScrnSize();
313 			}
314 
315 		/*	if (!bVidOkay && (bDrvOkay || bVidUsePlaceholder)) {
316 				// Maske sure the error will be visible
317 				SplashDestroy(1);
318 
319 				AppError("VidInit Failed", 0);
320 			}
321 */
322 /*			if (bVidOkay && (bRunPause || !bDrvOkay)) {
323 				VidRedraw();
324 			}
325 */		}
326 
327 		RunInit();
328 
329 	//	ShowWindow(hScrnWnd, nAppShowCmd);												// Show the screen window
330 	//	nAppShowCmd = SW_NORMAL;
331 
332 	//	SetForegroundWindow(hScrnWnd);
333 
334 		//GameInpCheckLeftAlt();
335 		GameInpCheckMouse();															// Hide the cursor
336 		while (!finished) {
337 				RunIdle();
338 		}
339 		RunExit();
340 	} while (bRestartVideo);
341 
342 	return 0;
343 }
344 
345