2 // Some simple emulation classes to get PTC code running on SDL
\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
10 #define __SDLPTC_H__
\r
16 #endif // #ifdef __PORT__
\r
21 #define randomize() srand(time(NULL))
\r
22 #define random(max) (rand() % (max))
\r
23 #endif // #ifndef __PORT__
\r
28 Error(const char * msg) { strcpy(message, msg); }
\r
29 void report(void) { printf("Error: %s\n", message); }
\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
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
62 Uint32 maskR, maskG, maskB;
\r
68 Surface(int w, int h, const Format &format)
\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
74 throw Error(SDL_GetError());
\r
87 SDL_FreeSurface(surface);
\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
95 SDL_Color colors[256];
\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
104 virtual void * lock(void)
\r
106 if (SDL_MUSTLOCK(surface))
\r
107 while (SDL_LockSurface(surface) < 0)
\r
110 return (Uint8 *)surface->pixels;
\r
113 virtual void unlock(void)
\r
115 if (SDL_MUSTLOCK(surface))
\r
116 SDL_UnlockSurface(surface);
\r
119 virtual void copy(Surface &dst, const Area &srcarea, const Area &dstarea)
\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
133 virtual void copy(Surface &dst)
\r
135 SDL_Rect srcrect, dstrect;
\r
138 srcrect.w = surface->w;
\r
139 srcrect.h = surface->h;
\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
148 virtual void update(void)
\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
159 SDL_Surface * surface;
\r
163 SDL_Rect updates[1]; /* Definitely increase this.. */
\r
167 class Console: public Surface
\r
170 Console(): Surface(), fullscreen(0), nJoystick(0) {}
\r
171 ~Console() { SDL_Quit(); }
\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
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
183 void option(char * option)
\r
185 if (!stricmp(option, "fullscreen output"))
\r
187 else if (!stricmp(option, "windowed output"))
\r
189 /* Added/changed by SDLEMU (http://sdlemu.ngemu.com) */
\r
190 else if (!stricmp(option, "joystick enabled"))
\r
192 else if (!stricmp(option, "joystick disabled"))
\r
196 /* The following was changed/added by SDLEMU (http://sdlemu.ngemu.com) */
\r
198 void open(const char * title, int width, int height, const Format &format)
\r
200 Uint32 flags = SDL_SWSURFACE;
\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
205 const SDL_VideoInfo * info = SDL_GetVideoInfo(); // Let us get proper info about the platform.
\r
209 fprintf(stderr, "SDL is unable to get the video query : %s\n", SDL_GetError());
\r
213 if (info->hw_available)
\r
214 flags = SDL_HWSURFACE | SDL_HWPALETTE | SDL_DOUBLEBUF;
\r
217 flags |= SDL_HWACCEL;
\r
220 flags |= SDL_FULLSCREEN;
\r
222 surface = SDL_SetVideoMode(width, height, 16, flags);
\r
224 if (surface == NULL)
\r
225 throw Error(SDL_GetError());
\r
227 SDL_WM_SetCaption(title, title);
\r
235 while (SDL_PollEvent(&event))
\r
237 /* Real key events trigger this function */
\r
238 if (event.type == SDL_KEYDOWN)
\r
241 /* So do quit events -- let the app know about it */
\r
242 if (event.type == SDL_QUIT)
\r
248 int JoyEnabled(void) { return (nJoystick == 1 ? 1 : 0); }
\r
251 SDL_Joystick * joystick;
\r
258 #endif // #ifndef __SDLPTC_H__
\r