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