start using libpicofe, move some files
[fceu.git] / svga.c
1 /* FCE Ultra - NES/Famicom Emulator
2  *
3  * Copyright notice for this file:
4  *  Copyright (C) 1998 BERO
5  *  Copyright (C) 2002 Ben Parnell
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21
22 /*                      SVGA High Level Routines
23                           FCE / FCE Ultra
24 */
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <math.h>
28 #include <string.h>
29
30 #include <stdarg.h>
31
32
33 #ifndef M_PI
34 #define M_PI 3.14159265358979323846
35 #endif
36
37 #include "types.h"
38 #include "svga.h"
39 #include "fce.h"
40 #include "general.h"
41 #include "video.h"
42 #include "sound.h"
43 #include "version.h"
44 #include "nsf.h"
45 #include "palette.h"
46 #include "fds.h"
47 #include "netplay.h"
48 #include "state.h"
49 #include "cart.h"
50 #include "input.h"
51
52 #include "vsuni.h"
53
54 FCEUS FSettings;
55
56 static int howlong;
57 static char errmsg[65];
58
59 void FCEU_PrintError(char *format, ...)
60 {
61  char temp[2048];
62
63  va_list ap;
64
65  va_start(ap,format);
66  vsprintf(temp,format,ap);
67  FCEUD_PrintError(temp);
68
69  va_end(ap);
70 }
71
72 void FCEU_DispMessage(char *format, ...)
73 {
74  va_list ap;
75
76  va_start(ap,format);
77  vsprintf(errmsg,format,ap);
78  va_end(ap);
79
80  howlong=180;
81  if (errmsg[0] != '|')
82   printf("%s\n", errmsg);
83 }
84
85 void FCEU_CancelDispMessage(void)
86 {
87  howlong=0;
88 }
89
90 void FCEUI_SetRenderedLines(int ntscf, int ntscl, int palf, int pall)
91 {
92  FSettings.UsrFirstSLine[0]=ntscf;
93  FSettings.UsrLastSLine[0]=ntscl;
94  FSettings.UsrFirstSLine[1]=palf;
95  FSettings.UsrLastSLine[1]=pall;
96  if(PAL)
97  {
98   FSettings.FirstSLine=FSettings.UsrFirstSLine[1];
99   FSettings.LastSLine=FSettings.UsrLastSLine[1];
100  }
101  else
102  {
103   FSettings.FirstSLine=FSettings.UsrFirstSLine[0];
104   FSettings.LastSLine=FSettings.UsrLastSLine[0];
105  }
106
107 }
108
109 void FCEUI_SetVidSystem(int a)
110 {
111  FSettings.PAL=a?1:0;
112  FCEU_ResetVidSys();
113  FCEU_ResetPalette();
114 }
115
116 int FCEUI_GetCurrentVidSystem(int *slstart, int *slend)
117 {
118  if(slstart)
119   *slstart=FSettings.FirstSLine;
120  if(slend)
121   *slend=FSettings.LastSLine;
122  return(PAL);
123 }
124
125 #ifdef NETWORK
126 void FCEUI_SetNetworkPlay(int type)
127 {
128  FSettings.NetworkPlay=type;
129 }
130 #endif
131
132 void FCEUI_SetGameGenie(int a)
133 {
134  FSettings.GameGenie=a?1:0;
135 }
136
137 #ifndef NETWORK
138 #define netplay 0
139 #endif
140
141 uint8 Exit=0;
142
143 uint8 DIPS=0;
144
145 uint8 CommandQueue=0;
146
147
148 void FCEUI_SetSnapName(int a)
149 {
150  FSettings.SnapName=a;
151 }
152
153 void FCEUI_SaveExtraDataUnderBase(int a)
154 {
155  FSettings.SUnderBase=a;
156 }
157
158
159 void FCEUI_SelectState(int w)
160 {
161  // if(netplay!=2 && FCEUGameInfo.type!=GIT_NSF)
162  //  CommandQueue=42+w;
163 }
164
165 void FCEUI_SaveState(void)
166 {
167  // if(netplay!=2 && FCEUGameInfo.type!=GIT_NSF)
168  //  CommandQueue=40;
169
170  //CheckStates();
171  SaveState();
172 }
173
174 void FCEUI_LoadState(void)
175 {
176  // if(netplay!=2 && FCEUGameInfo.type!=GIT_NSF)
177  //  CommandQueue=41;
178  //CheckStates();
179  LoadState();
180 }
181
182 int32 FCEUI_GetDesiredFPS(void)
183 {
184   if(PAL)
185    return(838977920); // ~50.007
186   else
187    return(1008307711);  // ~60.1
188 }
189
190 static int dosnapsave=0;
191 void FCEUI_SaveSnapshot(void)
192 {
193  dosnapsave=1;
194 }
195
196 /* I like the sounds of breaking necks. */
197 static void ReallySnap(void)
198 {
199  int x=SaveSnapshot();
200  if(!x)
201   FCEU_DispMessage("Error saving screen snapshot.");
202  else
203   FCEU_DispMessage("Screen snapshot %d saved.",x-1);
204 }
205
206 void DriverInterface(int w, void *d)
207 {
208  switch(w)
209  {
210   case DES_RESET:if(netplay!=2) CommandQueue=30;break;
211   case DES_POWER:if(netplay!=2) CommandQueue=31;break;
212
213   case DES_VSUNIDIPSET:CommandQueue=10+(int)d;break;
214   case DES_VSUNITOGGLEDIPVIEW:CommandQueue=10;break;
215   case DES_VSUNICOIN:CommandQueue=19;break;
216 #if 0
217   case DES_NTSCDEC:
218                   if(ntsccol && FCEUGameInfo.type!=GIT_VSUNI && !PAL && FCEUGameInfo.type!=GIT_NSF)
219                   {
220                    char which;
221                    if(controlselect)
222                    {
223                     if(controllength)
224                     {
225                      which=controlselect==1?ntschue:ntsctint;
226                      which--;
227                      if(which<0) which=0;
228                          if(controlselect==1)
229                           ntschue=which;
230                          else ntsctint=which;
231                      CalculatePalette();
232                     }
233                    controllength=360;
234                     }
235                    }
236                   break;
237   case DES_NTSCINC:
238                    if(ntsccol && FCEUGameInfo.type!=GIT_VSUNI && !PAL && FCEUGameInfo.type!=GIT_NSF)
239                      if(controlselect)
240                      {
241                       if(controllength)
242                       {
243                        switch(controlselect)
244                        {
245                         case 1:ntschue++;
246                                if(ntschue>128) ntschue=128;
247                                CalculatePalette();
248                                break;
249                         case 2:ntsctint++;
250                                if(ntsctint>128) ntsctint=128;
251                                CalculatePalette();
252                                break;
253                        }
254                       }
255                       controllength=360;
256                      }
257                     break;
258 #endif
259   }
260 }
261
262
263 #include "drawing.h"
264 #ifdef FRAMESKIP
265 void FCEU_PutImageDummy(void)
266 {
267  if(howlong) howlong--; /* DrawMessage() */
268  #ifdef FPS
269  {
270   extern uint64 frcount;
271   frcount++;
272  }
273  #endif
274
275 }
276 #endif
277
278 void FCEU_PutImage(void)
279 {
280         if(FCEUGameInfo.type==GIT_NSF)
281         {
282          DrawNSF(XBuf);
283          /* Save snapshot after NSF screen is drawn.  Why would we want to
284             do it before?
285          */
286          if(dosnapsave)
287          {
288           ReallySnap();
289           dosnapsave=0;
290          }
291         }
292         else
293         {
294          /* Save snapshot before overlay stuff is written. */
295          if(dosnapsave)
296          {
297           ReallySnap();
298           dosnapsave=0;
299          }
300          if(FCEUGameInfo.type==GIT_VSUNI)
301                  FCEU_VSUniDraw(XBuf);
302
303          //FCEU_DrawSaveStates(XBuf);
304          //FCEU_DrawMovies(XBuf);
305          //FCEU_DrawNTSCControlBars(XBuf);
306          //FCEU_DrawRecordingStatus(XBuf);
307
308          //if(controllength) {controllength--;DrawBars();}
309         }
310         DrawMessage();
311         #ifdef FPS
312         {
313         extern uint64 frcount;
314         frcount++;
315         }
316         #endif
317         DrawInput(XBuf+8);
318 }
319
320 #if 0
321 void DoCommand(uint8 c)
322 {
323  switch(c)
324  {
325   case 1:FDSControl(FDS_SELECT);break;
326   case 2:FDSControl(FDS_IDISK);break;
327   case 3:FDSControl(FDS_EJECT);break;
328
329   case 10:DIPS^=2;break;
330   case 11:vsdip^=1;DIPS|=2;break;
331   case 12:vsdip^=2;DIPS|=2;break;
332   case 13:vsdip^=4;DIPS|=2;break;
333   case 14:vsdip^=8;DIPS|=2;break;
334   case 15:vsdip^=0x10;DIPS|=2;break;
335   case 16:vsdip^=0x20;DIPS|=2;break;
336   case 17:vsdip^=0x40;DIPS|=2;break;
337   case 18:vsdip^=0x80;DIPS|=2;break;
338   case 19:coinon=6;break;
339   case 30:ResetNES();break;
340   case 31:PowerNES();break;
341   case 40:CheckStates();StateShow=0;SaveState();break;
342   case 41:CheckStates();StateShow=0;LoadState();break;
343   case 42: case 43: case 44: case 45: case 46: case 47: case 48: case 49:
344   case 50: case 51:StateShow=180;CurrentState=c-42;CheckStates();break;
345  }
346 }
347 #endif
348