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