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