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