fixed some portability issues
[picodrive.git] / platform / win32 / GenaDrive / Loop.cpp
CommitLineData
cc68a136 1#include "app.h"\r
2#include "FileMenu.h"\r
3\r
4// sram\r
5struct PicoSRAM\r
6{\r
7 unsigned char *data; // actual data\r
8 unsigned int start; // start address in 68k address space\r
9 unsigned int end;\r
10 unsigned char resize; // 1=SRAM size changed and needs to be reallocated on PicoReset\r
11 unsigned char reg_back; // copy of Pico.m.sram_reg to set after reset\r
12 unsigned char changed;\r
13 unsigned char pad;\r
14};\r
15\r
16extern "C" PicoSRAM SRam;\r
17extern char *romname;\r
18int fastForward=0;\r
19int frameStep=0;\r
20\r
21char LoopQuit=0;\r
22static FILE *DebugFile=NULL;\r
23int LoopMode=0;\r
24static void UpdateSound();\r
25\r
26int LoopInit()\r
27{\r
28 int ret=0;\r
29\r
30 // bits LSb->MSb:\r
31 // enable_ym2612&dac, enable_sn76496, enable_z80, stereo_sound;\r
32 // alt_renderer, 6button_gamepad, accurate_timing, accurate_sprites\r
33 PicoOpt=0x1f;\r
34 PsndRate=44100;\r
35 //PsndLen=PsndRate/60; // calculated later by pico itself\r
36\r
37 // Init Direct3D:\r
38 ret=DirectInit(); if (ret) return 1;\r
39 InputInit();\r
40\r
41 // Init DirectSound:\r
42 //DSoundInit();\r
43\r
44 ret=EmuInit(); if (ret) return 1;\r
45 FileMenu.init();\r
46\r
47 LoopMode=8;\r
48 PicoWriteSound = UpdateSound;\r
49\r
50 return 0;\r
51}\r
52\r
53void preLoopInit()\r
54{\r
55 romname[strlen(romname)-3] = 0;\r
56 strcat(romname, "srm");\r
57 int sram_size = SRam.end-SRam.start+1;\r
58 if(SRam.reg_back & 4) sram_size=0x2000;\r
59 FILE *f = fopen(romname, "rb");\r
60 if(f && SRam.data)\r
61 fread(SRam.data, 1, sram_size, f);\r
62 if(f) fclose(f);\r
63}\r
64\r
65extern "C" char *debugString();\r
66\r
67void LoopExit()\r
68{\r
69 dprintf(debugString());\r
70\r
71 romname[strlen(romname)-3] = 0;\r
72 strcat(romname, "srm");\r
73 int sram_size = SRam.end-SRam.start+1;\r
74 if(SRam.reg_back & 4) sram_size=0x2000;\r
75 for(; sram_size > 0; sram_size--)\r
76 if(SRam.data[sram_size-1]) break;\r
77 if(sram_size) {\r
78 FILE *f = fopen(romname, "wb");\r
79 if(f) {\r
80 fwrite(SRam.data, 1, sram_size, f);\r
81 fclose(f);\r
82 }\r
83 }\r
84\r
85 FileMenu.exit();\r
86 EmuExit();\r
87 DSoundExit(); PsndLen=0;\r
88 InputExit();\r
89 DirectExit();\r
90\r
91 if (DebugFile) fclose(DebugFile);\r
92 DebugFile=NULL;\r
93}\r
94\r
95// ----------------------------------------------------------------\r
96\r
97int emu_frame = 0;\r
98\r
99static int DoGame()\r
100{\r
101 if(fastForward) { PicoSkipFrame+=1; PicoSkipFrame&=7; }\r
102 else PicoSkipFrame=0;\r
103\r
104 if(frameStep==1) return 0;\r
105 else if(frameStep==3) frameStep=1;\r
106\r
107 EmuFrame();\r
108 emu_frame++;\r
109\r
110 if (Inp.held[7]==1) LoopMode=2; // Right thumb = Toggle Menu\r
111\r
112 return 0;\r
113}\r
114// ----------------------------------------------------------------\r
115\r
116static int MenuUpdate()\r
117{\r
118 int delta=0;\r
119\r
120 if (Inp.repeat[0]) delta-=0x100;\r
121 if (Inp.repeat[1]) delta+=0x100;\r
122\r
123 if (Inp.button[14]>30) delta-=Inp.button[14]-30;\r
124 if (Inp.button[15]>30) delta+=Inp.button[15]-30;\r
125\r
126 if (delta) FileMenu.scroll(delta);\r
127\r
128 if (Inp.held[8]==1 || Inp.held[10]==1 || Inp.held[4]==1) // A, X or Start\r
129 {\r
130 //RomFree();\r
131 //FileMenu.getFilePath(RomName);\r
132 //RomLoad();\r
133 //LoopMode=8; // Go to game\r
134 }\r
135\r
136 if (Inp.held[7]==1) LoopMode=8; // Right thumb = Toggle Menu\r
137\r
138 return 0;\r
139}\r
140\r
141static int MenuRender()\r
142{\r
143 WCHAR text[80]={0};\r
144 wsprintfW(text,L"%.40S v%x.%.3x",AppName,PicoVer>>12,PicoVer&0xfff);\r
145 FontSetColour(0x60c0ff);\r
146 FontText(text,64,48);\r
147\r
148 FileMenu.render();\r
149\r
150 return 0;\r
151}\r
152\r
153// ----------------------------------------------------------------\r
154\r
155static int ModeUpdate()\r
156{\r
157 if (Inp.held[14] && Inp.held[15] && Inp.held[12]==1) LoopQuit=1; // L+R+black to quit:\r
158 if (Inp.button[4]>30 && Inp.button[5]>30) LoopQuit=1; // Start and back to quit\r
159\r
160 if (LoopMode==8) { DoGame(); return 0; }\r
161\r
162 if (DSoundNext) memset(DSoundNext,0,PsndLen<<2);\r
163\r
164 if (LoopMode==2) { FileMenu.scan(); LoopMode++; return 0; }\r
165 if (LoopMode==3) { MenuUpdate(); return 0; }\r
166 if (LoopMode==4) { LightCalUpdate(); return 0; }\r
167\r
168 LoopMode=2; // Unknown mode, go to rom menu\r
169 return 0;\r
170}\r
171\r
172\r
173static int ModeRender()\r
174{\r
175 DirectScreen();\r
176 if (LoopMode==3) MenuRender();\r
177 if (LoopMode==4) LightCalRender();\r
178\r
179 return 0;\r
180}\r
181\r
182static void UpdateSound()\r
183{\r
184 if(fastForward) return;\r
185 while (DSoundUpdate()) { Sleep(1); }\r
186 while (DSoundUpdate()==0) { }\r
187}\r
188\r
189int LoopCode()\r
190{\r
191\r
192 // Main loop:\r
193 while (!LoopQuit)\r
194 {\r
195 InputUpdate();\r
196\r
197 DirectClear(0);\r
198 ModeUpdate();\r
199 ModeRender();\r
200 DirectPresent();\r
201// UpdateSound();\r
202 }\r
203\r
204 return 0;\r
205}\r
206\r
207// -------------------------------------------------------------------------------------\r
208\r
209extern "C" int dprintf(char *format, ...)\r
210{\r
211 char *name=NULL;\r
212 va_list val=NULL;\r
213\r
214#ifdef _XBOX\r
215 name="d:\\zout.txt";\r
216#else\r
217 name="zout.txt";\r
218#endif\r
219\r
220 if (DebugFile==NULL) DebugFile=fopen(name,"wt");\r
221 if (DebugFile==NULL) return 1;\r
222\r
223 fprintf(DebugFile, "%05i: ", emu_frame);\r
224 va_start(val,format);\r
225 vfprintf(DebugFile,format,val);\r
226 fprintf(DebugFile, "\n");\r
227 fflush(DebugFile);\r
228\r
229 va_end(val);\r
230 return 0;\r
231}\r
232\r
233extern "C" int dprintf2(char *format, ...)\r
234{\r
235 char str[512];\r
236 va_list val=NULL;\r
237\r
238 va_start(val,format);\r
239 vsprintf(str,format,val);\r
240 va_end(val);\r
241 OutputDebugString(str);\r
242\r
243 return 0;\r
244}\r