fixed: broken fs0, sram saves
[fceu.git] / drivers / cli / sdl-video.c
1 /* FCE Ultra - NES/Famicom Emulator
2  *
3  * Copyright notice for this file:
4  *  Copyright (C) 2002 Ben Parnell
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include <stdio.h>
22 #include <string.h>
23 #include <stdlib.h>
24
25 #include "sdl.h"
26 #include "../common/vidblit.h"
27
28 #define _sline srendline
29 #define _eline erendline
30
31 SDL_Surface *screen;
32
33 static int tlines;
34 static int inited=0;
35
36 static int exs,eys,eefx;
37 #define NWIDTH  (256-((eoptions&EO_CLIPSIDES)?16:0))
38 #define NOFFSET (32-8+(eoptions&EO_CLIPSIDES?8:0))
39
40 static void CleanSurface(void)
41 {
42  uint32 x;
43
44  x=screen->pitch*screen->h;
45
46  if(SDL_MUSTLOCK(screen))
47   SDL_LockSurface(screen);
48
49  memset((uint8*)screen->pixels, 0x80, x);
50
51  if(SDL_MUSTLOCK(screen))
52   SDL_UnlockSurface(screen);
53
54  SDL_UpdateRect(screen, 0, 0, 0, 0);
55 }
56
57 static int paletterefresh;
58
59 void KillVideo(void)
60 {
61  if(inited&1)
62  {
63   SDL_QuitSubSystem(SDL_INIT_VIDEO);
64  }
65  inited=0;
66 }
67
68 int InitVideo(void)
69 {
70  const SDL_VideoInfo *vinf;
71  int flags=0;
72
73  #ifdef BROKEN
74  if(_fullscreen && _fshack)
75   setenv("SDL_VIDEODRIVER",_fshack,1);
76  else
77  {
78   if(!_fshacksave)
79    unsetenv("SDL_VIDEODRIVER");
80   else
81    setenv("SDL_VIDEODRIVER",_fshacksave,1);
82  }
83  #endif
84  if(SDL_InitSubSystem(SDL_INIT_VIDEO)==-1)
85  {
86   puts(SDL_GetError());
87   return(0);
88  }
89  inited|=1;
90
91  SDL_ShowCursor(0);
92  tlines=_eline-_sline+1;
93
94  vinf=SDL_GetVideoInfo();
95
96  if(vinf->hw_available)
97   flags|=SDL_HWSURFACE;
98
99  if(_fullscreen)
100   flags|=SDL_FULLSCREEN;
101  flags|=SDL_HWPALETTE;
102
103  if(_fullscreen)
104  {
105   exs=_xscalefs;
106   eys=_yscalefs;
107   eefx=_efxfs;
108   if(_xres<NWIDTH*exs || _yres<tlines*eys)
109   {
110    puts("xscale and/or yscale out of bounds.");
111    KillVideo();
112    return(0);
113   }
114   screen = SDL_SetVideoMode(_xres, _yres, 8, flags);
115  }
116  else
117  {
118   exs=_xscale;
119   eys=_yscale;
120   eefx=_efx;
121   screen = SDL_SetVideoMode(NWIDTH*exs, tlines*eys, 8, flags);
122  }
123  if(!screen)
124  {
125   puts(SDL_GetError());
126   KillVideo();
127   return(0);
128  }
129  inited=1;
130  CleanSurface();
131
132  SDL_WM_SetCaption("FCE Ultra","FCE Ultra");
133  paletterefresh=1;
134  return 1;
135 }
136
137 void ToggleFS(void)
138 {
139  KillVideo();
140  _fullscreen=!_fullscreen;
141
142  if(!InitVideo())
143  {
144   _fullscreen=!_fullscreen;
145   if(!InitVideo())
146   {
147    puts("Gah, bailing out.");
148    exit(1);
149   }
150  }
151 }
152 static SDL_Color psdl[256];
153
154 void FCEUD_SetPalette(uint8 index, uint8 r, uint8 g, uint8 b)
155 {
156
157  psdl[index].r=r;
158  psdl[index].g=g;
159  psdl[index].b=b;
160
161  paletterefresh=1;
162 }
163
164 void FCEUD_GetPalette(uint8 index, uint8 *r, uint8 *g, uint8 *b)
165 {
166  *r=psdl[index].r;
167  *g=psdl[index].g;
168  *b=psdl[index].b;
169 }
170
171 static void RedoPalette(void)
172 {
173  SDL_SetPalette(screen,SDL_PHYSPAL,psdl,0,256);
174 }
175
176 void LockConsole(){}
177 void UnlockConsole(){}
178 void BlitScreen(uint8 *XBuf)
179 {
180  uint8 *dest;
181  int xo=0,yo=0;
182
183  if(paletterefresh)
184  {
185   RedoPalette();
186   paletterefresh=0;
187  }
188
189  XBuf+=_sline*320;
190
191  if(SDL_MUSTLOCK(screen))
192   SDL_LockSurface(screen);
193
194  dest=screen->pixels;
195
196  if(_fullscreen)
197  {
198   xo=(((screen->w-NWIDTH*exs))>>1);
199   dest+=xo;
200   if(screen->h>(tlines*eys))
201   {
202    yo=((screen->h-tlines*eys)>>1);
203    dest+=yo*screen->pitch;
204   }
205  }
206
207  Blit8To8(XBuf+NOFFSET,dest, NWIDTH, tlines, screen->pitch,exs,eys,eefx);
208
209  if(SDL_MUSTLOCK(screen))
210   SDL_UnlockSurface(screen);
211
212  SDL_UpdateRect(screen, xo, yo, NWIDTH*exs, tlines*eys);
213 }
214
215 uint32 PtoV(uint16 x, uint16 y)
216 {
217  if(_fullscreen)
218  {
219
220  }
221  else
222  {
223   if(eoptions&EO_CLIPSIDES)
224    x+=8;
225   y+=srendline;
226  }
227  return(x|(y<<16));
228 }