]> Shamusworld >> Repos - virtualjaguar/blob - src/include/SDLptc.h
c7f366b49d4e37c6285bdaae189e688d81294d19
[virtualjaguar] / src / include / SDLptc.h
1 //
2 // Some simple emulation classes to get PTC code running on SDL
3 //
4 // by cal16
5 // GCC/SDL port by Niels Wagenaar (Linux/WIN32) and Caz (BeOS)
6 // Cleanups in some classes and code in general by James L. Hammons
7 //
8
9 #ifndef __SDLPTC_H__
10 #define __SDLPTC_H__
11
12 #include "SDL.h"
13 #include <string.h>
14 #include <stdlib.h>
15 #include "types.h"
16
17
18 class Error
19 {
20         public:
21                 Error(const char * msg) { strcpy(message, msg); }
22                 void report(void)               { printf("Error: %s\n", message); }
23
24         private:
25                 char message[1024];
26 };
27
28 class Area
29 {
30         public:
31                 Area(int ll, int tt, int rr, int bb): l(ll), t(tt), r(rr), b(bb) {}
32                 int left(void) const    { return l; }
33                 int right(void) const   { return r; }
34                 int top(void) const             { return t; }
35                 int bottom(void) const  { return b; }
36                 int width(void) const   { return r - l; }
37                 int height(void) const  { return b - t; }
38
39         private:
40                 int l, t, r, b;
41 };
42
43         
44 class Format
45 {
46         public:
47                 Format(int pBpp, int r=0, int g=0, int b=0): bpp(pBpp), maskR(r), maskG(g), maskB(b) {}
48                 Uint8 BPP(void) const           { return bpp; }
49                 Uint32 MaskR(void) const        { return maskR; }
50                 Uint32 MaskG(void) const        { return maskG; }
51                 Uint32 MaskB(void) const        { return maskB; }
52
53         private:
54                 Uint8 bpp;
55                 Uint32 maskR, maskG, maskB;
56 };
57
58 class Surface
59 {
60         public:
61             Surface(int w, int h, const Format &format)
62             {
63                         surface = SDL_AllocSurface(SDL_SWSURFACE, w, h, format.BPP(),
64                                         format.MaskR(), format.MaskG(), format.MaskB(), 0);
65                         if (surface == NULL)
66                         {
67                                 throw Error(SDL_GetError());
68                         }
69                         nupdates = 0;
70                         is_console = 0;
71                 }
72                 Surface(void)
73                 {
74                         nupdates = 0;
75                         is_console = 1;
76                 }
77                 ~Surface()
78                 {
79                         if (!is_console)
80                                 SDL_FreeSurface(surface);
81                 }
82
83                 virtual int width(void)         { return surface->w; }
84                 virtual int height(void)        { return surface->h; }
85                 virtual int pitch(void)         { return surface->pitch; }
86                 virtual void palette(int32 * pcolors)
87                 {
88                         SDL_Color colors[256];
89
90                         for(int i=0; i<256; ++i)
91                                 colors[i].r = (pcolors[i] >> 16) & 0xFF,
92                                 colors[i].g = (pcolors[i] >> 8) & 0xFF,
93                                 colors[i].b = (pcolors[i] >> 0) & 0xFF;
94                         SDL_SetColors(surface, colors, 0, 256);
95                 }
96
97                 virtual void * lock(void)
98                 {
99                         if (SDL_MUSTLOCK(surface))
100                                 while (SDL_LockSurface(surface) < 0)
101                                         SDL_Delay(10);
102
103                         return (Uint8 *)surface->pixels;
104                 }
105
106                 virtual void unlock(void)
107                 {
108                         if (SDL_MUSTLOCK(surface))
109                                 SDL_UnlockSurface(surface);
110                 }
111         
112                 virtual void copy(Surface &dst, const Area &srcarea, const Area &dstarea)
113                 {
114                         SDL_Rect srcrect, dstrect;
115                         srcrect.x = srcarea.left();
116                         srcrect.y = srcarea.top();
117                         srcrect.w = srcarea.width();
118                         srcrect.h = srcarea.height();
119                         dstrect.x = dstarea.left();
120                         dstrect.y = dstarea.top();
121                         dstrect.w = dstarea.width();
122                         dstrect.h = dstarea.height();
123                         SDL_BlitSurface(surface, &srcrect, dst.surface, &dstrect);
124                         dst.updates[dst.nupdates++] = dstrect;
125                 }
126                 virtual void copy(Surface &dst)
127                 {
128                         SDL_Rect srcrect, dstrect;
129                         srcrect.x = 0;
130                         srcrect.y = 0;
131                         srcrect.w = surface->w;
132                         srcrect.h = surface->h;
133                         dstrect.x = 0;
134                         dstrect.y = 0;
135                         dstrect.w = surface->w;
136                         dstrect.h = surface->h;
137                         SDL_LowerBlit(surface, &srcrect, dst.surface, &dstrect);
138                         dst.updates[dst.nupdates++] = dstrect;
139                 }
140
141                 virtual void update(void)
142                 {
143                         /* Added/Changed by SDLEMU (http://sdlemu.ngemu.com) */
144                         /* SDL_Flip is infact the same as SDL_Blitsurface but
145                                 if SDL_DOUBLEBUF | SDL_HWSURFACE is available, it 
146                                 makes use of it. If not is uses SDL_Blitsurface */
147                     SDL_Flip(surface);      
148                         nupdates = 0;
149                 }
150
151         public:
152                 SDL_Surface * surface;
153
154         protected:
155                 int nupdates;
156                 SDL_Rect updates[1];            /* Definitely increase this.. */
157                 int is_console;
158 };
159
160 class Console: public Surface
161 {
162         public:
163                 Console(): Surface(), fullscreen(0), nJoystick(0) {}
164                 ~Console()                              { SDL_Quit(); }
165
166                 /* Added/changed by SDLEMU (http://sdlemu.ngemu.com) */
167                 /* We need to close several items in SDL because of memory leaks
168                         and core dumps and stuff :) */
169                 void close(void)
170                 {
171                         SDL_JoystickClose(joystick);
172                         SDL_FreeSurface(surface);
173                         SDL_QuitSubSystem(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_AUDIO | SDL_INIT_TIMER);
174                         SDL_Quit();
175                 }
176                 void option(char * option)
177                 {
178                         if (!stricmp(option, "fullscreen output"))
179                                 fullscreen = 1;
180                         else if (!stricmp(option, "windowed output"))
181                                 fullscreen = 0;
182                         /* Added/changed by SDLEMU (http://sdlemu.ngemu.com) */
183                         else if (!stricmp(option, "joystick enabled"))
184                                 nJoystick = 1;
185                         else if (!stricmp(option, "joystick disabled"))
186                                 nJoystick = 0;
187                 }
188         
189                 /* The following was changed/added by SDLEMU (http://sdlemu.ngemu.com) */
190         
191                 void open(const char * title, int width, int height, const Format &format)
192                 {
193                         Uint32 flags = SDL_SWSURFACE;
194
195 //Testing                       if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_AUDIO | SDL_INIT_TIMER) < 0)
196                         if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_AUDIO | SDL_INIT_TIMER | SDL_INIT_NOPARACHUTE) < 0)
197                                 throw Error(SDL_GetError());
198
199                     const SDL_VideoInfo * info = SDL_GetVideoInfo(); // Let us get proper info about the platform.
200
201                         if (!info)
202                         {
203                                 fprintf(stderr, "SDL is unable to get the video query : %s\n", SDL_GetError());
204                                 exit(1);
205                         }
206
207                         if (info->hw_available)
208                                 flags = SDL_HWSURFACE | SDL_HWPALETTE | SDL_DOUBLEBUF;
209                 
210                         if (info->blit_hw)
211                                 flags |= SDL_HWACCEL;
212
213                         if (fullscreen)
214                                 flags |= SDL_FULLSCREEN;
215
216                         surface = SDL_SetVideoMode(width, height, 16, flags);
217
218                         if (surface == NULL)
219                                 throw Error(SDL_GetError());
220                 
221                 SDL_WM_SetCaption(title, title);
222             }
223
224                 int key(void)
225                 {
226                         SDL_Event event;
227                         int keyevent = 0;
228
229                         while (SDL_PollEvent(&event))
230                         {
231                                 /* Real key events trigger this function */
232                                 if (event.type == SDL_KEYDOWN)
233                                         keyevent = 1;
234
235                                 /* So do quit events -- let the app know about it */
236                                 if (event.type == SDL_QUIT)
237                                         keyevent = 1;
238                         }
239                         return keyevent;
240                 }
241
242                 int JoyEnabled(void)    { return (nJoystick == 1 ? 1 : 0); }
243
244         public:
245                 SDL_Joystick * joystick;
246
247         private:
248                 int fullscreen;
249                 int nJoystick;
250 };
251
252 #endif  // __SDLPTC_H__