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
21 Error(const char * msg) { strcpy(message, msg); }
22 void report(void) { printf("Error: %s\n", message); }
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; }
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; }
55 Uint32 maskR, maskG, maskB;
61 Surface(int w, int h, const Format &format)
63 surface = SDL_AllocSurface(SDL_SWSURFACE, w, h, format.BPP(),
64 format.MaskR(), format.MaskG(), format.MaskB(), 0);
67 throw Error(SDL_GetError());
80 SDL_FreeSurface(surface);
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)
88 SDL_Color colors[256];
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);
97 virtual void * lock(void)
99 if (SDL_MUSTLOCK(surface))
100 while (SDL_LockSurface(surface) < 0)
103 return (Uint8 *)surface->pixels;
106 virtual void unlock(void)
108 if (SDL_MUSTLOCK(surface))
109 SDL_UnlockSurface(surface);
112 virtual void copy(Surface &dst, const Area &srcarea, const Area &dstarea)
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;
126 virtual void copy(Surface &dst)
128 SDL_Rect srcrect, dstrect;
131 srcrect.w = surface->w;
132 srcrect.h = surface->h;
135 dstrect.w = surface->w;
136 dstrect.h = surface->h;
137 SDL_LowerBlit(surface, &srcrect, dst.surface, &dstrect);
138 dst.updates[dst.nupdates++] = dstrect;
141 virtual void update(void)
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 */
152 SDL_Surface * surface;
156 SDL_Rect updates[1]; /* Definitely increase this.. */
160 class Console: public Surface
163 Console(): Surface(), fullscreen(0), nJoystick(0) {}
164 ~Console() { SDL_Quit(); }
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 :) */
171 SDL_JoystickClose(joystick);
172 SDL_FreeSurface(surface);
173 SDL_QuitSubSystem(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_AUDIO | SDL_INIT_TIMER);
176 void option(char * option)
178 if (!stricmp(option, "fullscreen output"))
180 else if (!stricmp(option, "windowed output"))
182 /* Added/changed by SDLEMU (http://sdlemu.ngemu.com) */
183 else if (!stricmp(option, "joystick enabled"))
185 else if (!stricmp(option, "joystick disabled"))
189 /* The following was changed/added by SDLEMU (http://sdlemu.ngemu.com) */
191 void open(const char * title, int width, int height, const Format &format)
193 Uint32 flags = SDL_SWSURFACE;
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());
199 const SDL_VideoInfo * info = SDL_GetVideoInfo(); // Let us get proper info about the platform.
203 fprintf(stderr, "SDL is unable to get the video query : %s\n", SDL_GetError());
207 if (info->hw_available)
208 flags = SDL_HWSURFACE | SDL_HWPALETTE | SDL_DOUBLEBUF;
211 flags |= SDL_HWACCEL;
214 flags |= SDL_FULLSCREEN;
216 surface = SDL_SetVideoMode(width, height, 16, flags);
219 throw Error(SDL_GetError());
221 SDL_WM_SetCaption(title, title);
229 while (SDL_PollEvent(&event))
231 /* Real key events trigger this function */
232 if (event.type == SDL_KEYDOWN)
235 /* So do quit events -- let the app know about it */
236 if (event.type == SDL_QUIT)
242 int JoyEnabled(void) { return (nJoystick == 1 ? 1 : 0); }
245 SDL_Joystick * joystick;
252 #endif // __SDLPTC_H__