]> Shamusworld >> Repos - virtualjaguar/blob - src/Jagem.cpp
Adding 1.0.1/2 uncompressed tarballs to tags for historical purposes.
[virtualjaguar] / src / Jagem.cpp
1 ////////////////////////////////////////////////////////////////////////////////\r
2 //\r
3 ////////////////////////////////////////////////////////////////////////////////\r
4 //\r
5 //\r
6 //\r
7 //\r
8 //\r
9 //\r
10 //\r
11 ////////////////////////////////////////////////////////////////////////////////\r
12 #ifndef __PORT__\r
13 #include "stdafx.h"\r
14 #include <mmsystem.h>\r
15 #include <direct.h>\r
16 #endif  // #ifndef __PORT__\r
17 #include <time.h>\r
18 \r
19 /* Added by SDLEMU (http://sdlemu.ngemu.com) */\r
20 /* Added for GCC UNIX compatibility          */\r
21 #ifdef __GCCUNIX__\r
22 #include <unistd.h>\r
23 #endif  // #ifdef __GCCUNIX__\r
24 \r
25 #include <SDL.h>\r
26 #include "SDLptc.h"\r
27 #include "jaguar.h"\r
28 \r
29 /* Enable this (uncomment) for speed control */\r
30 //#define SPEED_CONTROL\r
31 \r
32 Surface * surface;\r
33 Format format(16, 0x007C00, 0x00003E0, 0x0000001F);\r
34 uint8 finished = 0;\r
35 Console console;\r
36 \r
37 int fullscreen = 0;\r
38 char * jaguar_boot_dir;\r
39 \r
40 void main_screen_switch(void)\r
41 {\r
42         fullscreen = !fullscreen;\r
43         if (fullscreen)\r
44                 console.option("fullscreen output");\r
45         else\r
46                 console.option("windowed output");\r
47         console.close();\r
48         console.open("WIP VERSION", tom_width, tom_height, format);\r
49 }\r
50 \r
51 #ifdef __PORT__\r
52 \r
53 /* Added/changed by SDLEMU http://sdlemu.ngemu.com */\r
54 \r
55 int main(int argc, char * argv[])\r
56 {\r
57 \r
58         uint32           startTime, totalFrames, endTime, w, h;\r
59         int32        *vs;\r
60         unsigned int nNormalLast = 0;\r
61         int                      nNormalFrac = 0; \r
62         int                      i           = 0;\r
63     int          nTime       = 0;\r
64     int          nCount      = 0;\r
65     int          nFrameskip  = 1; /* Frameskip */\r
66     int          nFrame      = 0; /* No. of Frame */\r
67     int          nJoyport    = 0; /* Joystick port */\r
68 \r
69         printf("Virtual Jaguar/SDL v1.0.1 (GCC/SDL Port)\n");\r
70         printf("Based upon Virtual Jaguar core v1.0.0 by Potato emulation.\n");\r
71         printf("Written by Niels Wagenaar (Linux/WIN32) and Caz (BeOS)\n");\r
72         printf("Portions massaged by James L. Hammons (WIN32)\n");\r
73         printf("Contact : http://sdlemu.ngemu.com/ | sdlemu@ngemu.com\n");\r
74 \r
75         if (argc <= 1)\r
76         {\r
77             printf("Usage : \n\n");\r
78                 printf("jag_em <romfile> [switches]  ]\n");\r
79                 printf("                  -fullscreen     : Enable fullscreen mode           \n");\r
80                 printf("                  -window         : Enable windowed   mode (default) \n");\r
81                 printf("                  -frameskip 1-10 : Enable frameskip 1 (default) - 10\n");\r
82                 printf("                  -joystick       : Enable joystick/gamepad          \n");\r
83                 printf("                  -joyport   0-3  : Select desired joystick port     \n");\r
84                 return true;\r
85         }\r
86 \r
87         jaguar_boot_dir=(char*)malloc(1024);\r
88         getcwd(jaguar_boot_dir,1024);\r
89 \r
90         log_init("jag_em.log");\r
91 \r
92         memory_init();\r
93         version_init();\r
94         version_display(log_get());\r
95         jaguar_init(argv[1]);\r
96         jaguar_reset();\r
97         \r
98     /* Setting up the backbuffer */\r
99         int16 *backbuffer=(int16*)malloc(845*525*sizeof(int16));\r
100         memset(backbuffer,0xaa,tom_getVideoModeWidth()*tom_getVideoModeHeight()*sizeof(int16));\r
101 \r
102         /* Setting up the primary SDL display */\r
103         Format format(16,0x007c00,0x00003e0,0x0000001f);\r
104         surface=new Surface(tom_getVideoModeWidth(),tom_getVideoModeHeight(), format);\r
105     console.option("windowed output");\r
106 \r
107     /* Checking the switches ;) */\r
108         for(i = 0; (i < argc || argv[i] != NULL); i++)\r
109         {\r
110            if(!strcmp(argv[i], "-fullscreen")) \r
111                console.option("fullscreen output");\r
112 \r
113            if(!strcmp(argv[i], "-window")) \r
114                console.option("windowed output");\r
115 \r
116            if(!strcmp(argv[i], "-joystick")) \r
117                console.option("joystick enabled");\r
118 \r
119            if(!strcmp(argv[i], "-joyport"))\r
120            {\r
121                 nJoyport = atoi(argv[++i]) + 1;\r
122                         if(nJoyport > 3) nJoyport = 3;\r
123            }\r
124 \r
125            if(!strcmp(argv[i], "-frameskip"))\r
126            {\r
127                 nFrameskip = atoi(argv[++i]) + 1;\r
128                         if(nFrameskip > 10) nFrameskip = 10;\r
129                         #ifdef SPEED_CONTROL\r
130                            nFrameskip = 0;\r
131             #endif\r
132            }\r
133 \r
134 \r
135                \r
136     }\r
137     \r
138 \r
139         /* Initialize Joystick support under SDL */\r
140         if ( console.JoyEnabled() == 1 ) {\r
141         \r
142         if(SDL_NumJoysticks() <= 0)\r
143         {\r
144         console.option("joystick disabled");\r
145                 printf("No joystick(s) or joypad(s) detected on your system. Using keyboards....\n");\r
146         }\r
147         else {\r
148           if((console.joystick = SDL_JoystickOpen(nJoyport)) == 0)\r
149           {\r
150         console.option("joystick disabled");\r
151                 printf("Unable to open a Joystick on port : %d\n", nJoyport);\r
152           }\r
153           else\r
154            printf("Using: %s\n", SDL_JoystickName(nJoyport));\r
155         }\r
156         }\r
157 \r
158         /* Open the display and start emulating some l337 Atari Jaguar games :P */\r
159         console.open("Virtual Jaguar",tom_getVideoModeWidth(),tom_getVideoModeHeight(),format);\r
160 \r
161         \r
162         totalFrames=0;\r
163         startTime=clock();\r
164         nNormalLast=0;// Last value of timeGetTime()\r
165         nNormalFrac=0; // Extra fraction we did\r
166 \r
167         nNormalLast=SDL_GetTicks();//timeGetTime();\r
168         while (!finished)\r
169         {\r
170 #ifdef SPEED_CONTROL\r
171                 nTime=SDL_GetTicks()-nNormalLast;                                         // calcule le temps écoulé depuis le dernier affichage\r
172                                                                                                                           // nTime est en mili-secondes.\r
173                 // détermine le nombre de trames à passer + 1\r
174                 nCount=(nTime*600 - nNormalFrac) /10000;        \r
175 \r
176                 // si le nombre de trames à passer + 1 est nul ou négatif,\r
177                 // ne rien faire pendant 2 ms\r
178                 if (nCount<=0) \r
179                 { \r
180                         //Sleep(2); \r
181                         //SDL_Delay(1);\r
182                 } // No need to do anything for a bit\r
183                 else\r
184                 {\r
185                         nNormalFrac+=nCount*10000;                              // \r
186                         nNormalLast+=nNormalFrac/600;                           // add the duration of nNormalFrac frames\r
187                         nNormalFrac%=600;                                                       // \r
188 \r
189                         // Pas plus de 9 (10-1) trames non affichées \r
190                         if (nCount>10) \r
191                           nCount=10; \r
192                         for (i=0;i<nCount-1;i++) \r
193                                 jaguar_exec(backbuffer,0);\r
194 #endif\r
195             /* Setting up new backbuffer with new pixels and data */\r
196                         jaguar_exec(backbuffer,1);\r
197                         totalFrames++;\r
198 \r
199                         /* Simple frameskip */\r
200                         if ( nFrame == nFrameskip ) {\r
201                w=tom_width;\r
202                    h=tom_height;\r
203                            vs = (int32 *)surface->lock();\r
204                            memcpy(vs,backbuffer,w*h*2);\r
205                            surface->unlock();\r
206                            surface->copy(console);\r
207                            console.update();\r
208                            nFrame = 0;\r
209                         }\r
210             else\r
211                nFrame++;\r
212                         joystick_exec();\r
213                         \r
214 #ifdef SPEED_CONTROL\r
215                 }\r
216 #endif\r
217         }\r
218         int elapsedTime=clock()-startTime;\r
219         int fps=(1000*totalFrames)/elapsedTime;\r
220         printf("statistics: %i fps\n",fps);\r
221         \r
222         if ( console.JoyEnabled() == 1 ) {}\r
223  \r
224         jaguar_done();\r
225         version_done();\r
226         memory_done();\r
227         log_done();     \r
228         console.close(); // Close SDL items as last!\r
229     return 0;\r
230 }\r
231 \r
232 #else\r
233 \r
234 int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)\r
235 {\r
236         uint32           startTime, endTime, totalFrames;\r
237         unsigned int nNormalLast=0;\r
238         int                      nNormalFrac=0; \r
239         int                      nTime=0,nCount=0; int i=0;\r
240 \r
241         jaguar_boot_dir=(char*)malloc(1024);\r
242         _getcwd(jaguar_boot_dir,1024);\r
243 \r
244         log_init("jagem.log");\r
245         // 15 bits RGB555\r
246         SDL_Event       app_input_event;\r
247 \r
248         memory_init();\r
249         version_init();\r
250         version_display(log_get());\r
251         jaguar_init();\r
252         jaguar_reset();\r
253         \r
254 \r
255         int16 * backbuffer = (int16 *)malloc(845 * 525 * sizeof(int16));\r
256         memset(backbuffer, 0xAA, tom_getVideoModeWidth() * tom_getVideoModeHeight() * sizeof(int16));\r
257         surface=new Surface(tom_getVideoModeWidth(),tom_getVideoModeHeight(),format);\r
258         console.option("DirectX");\r
259         console.option("windowed output");\r
260 //      console.option("fullscreen output");\r
261         console.option("center window");\r
262         console.open("WIP VERSION",tom_getVideoModeWidth(),tom_getVideoModeHeight(),format);\r
263         uint32 surfacePitch=(surface->pitch()>>1);\r
264 \r
265         totalFrames=0;\r
266         startTime=clock();\r
267         nNormalLast=0;// Last value of timeGetTime()\r
268         nNormalFrac=0; // Extra fraction we did\r
269         nNormalLast=timeGetTime();\r
270         while (!finished)\r
271         {\r
272                 while ( SDL_PollEvent(&app_input_event) )\r
273                 {\r
274                         if ( app_input_event.type == SDL_QUIT )\r
275                         {\r
276                                 finished = 1;\r
277                         }\r
278                 }\r
279                 joystick_exec();\r
280 #define SPEED_CONTROL\r
281 #ifdef SPEED_CONTROL\r
282                 nTime=timeGetTime()-nNormalLast;                                          // calcule le temps écoulé depuis le dernier affichage\r
283                                                                                                                           // nTime est en mili-secondes.\r
284                 // détermine le nombre de trames à passer + 1\r
285                 nCount=(nTime*600 - nNormalFrac) /10000;        \r
286 \r
287                 // si le nombre de trames à passer + 1 est nul ou négatif,\r
288                 // ne rien faire pendant 2 ms\r
289                 if (nCount<=0) \r
290                 { \r
291                         Sleep(2); \r
292                 } // No need to do anything for a bit\r
293                 else\r
294                 {\r
295                         nNormalFrac+=nCount*10000;                              // \r
296                         nNormalLast+=nNormalFrac/600;                           // add the duration of nNormalFrac frames\r
297                         nNormalFrac%=600;                                                       // \r
298 \r
299                         // Pas plus de 9 (10-1) trames non affichées \r
300                         if (nCount>10) \r
301                           nCount=10; \r
302                         for (i=0;i<nCount-1;i++) \r
303                                 jaguar_exec(backbuffer,0);\r
304 #endif\r
305                         jaguar_exec(backbuffer,1);\r
306                         totalFrames++;\r
307 \r
308                         int32 *vs = (int32 *)surface->lock();\r
309                         uint32 w,h;\r
310                         w=tom_width;\r
311                         h=tom_height;\r
312 \r
313                         memcpy(vs,backbuffer,w*h*2);\r
314                         surface->unlock();\r
315                         surface->copy(console);\r
316 \r
317                         console.update();\r
318 #ifdef SPEED_CONTROL\r
319                 }\r
320 #endif\r
321         }\r
322         int elapsedTime=clock()-startTime;\r
323         int fps=(1000*totalFrames)/elapsedTime;\r
324         fprintf(log_get(),"statistics: %i fps\n",fps);\r
325         \r
326         console.close();\r
327 \r
328         jaguar_done();\r
329         version_done();\r
330         memory_done();\r
331         log_done();     \r
332     return 0;\r
333     \r
334 }\r
335 \r
336 #endif\r
337 \r