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