/usr/include/flatzebra/GameEngine.h is in libflatzebra-dev 0.1.5-4build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 | /* $Id: GameEngine.h,v 1.13 2010/05/10 04:23:38 sarrazip Exp $
GameEngine.h - Abstract class representing an SDL Game Engine.
flatzebra - Generic 2D Game Engine library
Copyright (C) 1999-2010 Pierre Sarrazin <http://sarrazip.com/>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
02111-1307, USA.
*/
#ifndef _H_GameEngine
#define _H_GameEngine
#include <flatzebra/Couple.h>
#include <flatzebra/RCouple.h>
#include <flatzebra/Sprite.h>
#include <flatzebra/RSprite.h>
#include <flatzebra/PixmapArray.h>
#include <flatzebra/PixmapLoadError.h>
/* Include directive instead of forward declaration, because of the
exception specifications that mention PixmapLoadError.
This is necessary for g++ 2.96, but it was not for g++ 2.95.2.
@sarrazip 20010324
*/
#include <SDL_types.h>
#include <SDL_keysym.h>
#include <string>
namespace flatzebra {
class GameEngine
/* Abstract class representing a Generic Game Engine.
*/
{
public:
GameEngine(Couple screenSizeInPixels,
const std::string &wmCaption,
bool fullScreen,
bool processActiveEvent)
throw(std::string);
/* Initializes SDL and an adequate video mode.
The x and y fields of 'screenSizeInPixels' specify the size of
the screen in pixels.
'wmCaption' must be the caption to be used by the window manager
as the window and icon titles.
If 'fullScreen' is true, an attempt is made to use the
full-screen mode.
Calls setVideoMode() and upon failure, throws the error message
as an exception.
If 'processActiveEvent' is true, SDL's SDL_ACTIVEEVENT is
processed, and the virtual method processActivation() is called
when the app loses and regains focus.
The run() method should be called after successfully constructing
this object.
*/
virtual ~GameEngine();
/* Calls SDL_Quit().
*/
std::string setVideoMode(Couple screenSizeInPixels, bool fullScreen);
/* Changes the current video mode to the given resolution and screen mode.
Returns an empty string upon success, or a non-empty error message
otherwise.
*/
bool inFullScreenMode() const;
/* Indicates if the currently selected video mode is in full screen
instead of a window.
Reflects the state selected by the most recent successful call
to setVideoMode(), or the state selected by the constructor if
setVideoMode() has never been called.
*/
virtual void processKey(SDLKey keysym, bool pressed) = 0;
/* Called upon a key event.
'keysym' is an SDL key symbol, e.g., SDLK_ESCAPE.
'pressed' indicates the new state of the key.
*/
virtual void processActivation(bool appActive);
/* Called when the app is activated or deactivated (iconified).
'appActive' is true if the app has just become active,
or false if it has just become inactive.
This method does nothing; an override of this method in a derived
class does not have to call this method.
After this method has been called, the tick() method will not be
called until the app has been reactivated. However, the SDL
screen is flipped after this call, so the effect of drawing
commands made by this method will appear.
Never called if processActiveEvent argument of constructor is false.
*/
virtual bool tick() = 0;
/* Called at the beginning of every animation period.
Must return true to ask to be called again at the next period.
Must return false to ask not to be called anymore.
*/
void run(int millisecondsPerFrame = 50);
/* Main loop.
To be called after the object has been successfully constructed.
Runs until the user asks to quit the program.
'millisecondsPerFrame' must be the number of milliseconds
between the start of each frame of animation. 50 ms means
20 frames per second, which is about the minimum speed needed
to fool the human eye into seeing smooth motion.
*/
int getScreenWidthInPixels() const;
int getScreenHeightInPixels() const;
/* Mumble.
*/
protected:
Couple theScreenSizeInPixels;
/* After initialization, this variable should not be modified
by derived class methods.
*/
SDL_Surface *theSDLScreen;
SDL_Surface *fixedWidthFontPixmap;
int theDepth; // the SDL "depth" used in the video mode
bool usingFullScreen;
bool processActiveEvent; // if true, SDL_ACTIVEEVENT is processed by run()
// Wu's line algorithm:
unsigned char gamma_table[256];
protected:
void loadPixmap(const char **xpmData,
PixmapArray &pa,
size_t index) const throw(PixmapLoadError);
void loadPixmap(const char **xpmData,
SDL_Surface *&pixmap,
Couple &pixmapSize) const throw(PixmapLoadError);
/* Loads a pixmap from the specified file or data and stores the results
in the other parameters.
Throws an exception to signal errors.
Upon success, the caller is responsible for freeing the surface
by calling SDL_FreeSurface().
*/
void copyPixmap(SDL_Surface *src, Couple dest,
SDL_Surface *surface = NULL) const;
/* Copies 'src' at the specified destination in the drawing pixmap.
If 'surface' is null, the visible screen is used.
*/
void copySpritePixmap(const Sprite &s,
size_t pixmapNo,
Couple posInSurface,
SDL_Surface *surface = NULL);
void copySpritePixmap(const RSprite &s,
size_t pixmapNo,
RCouple posInSurface,
SDL_Surface *surface = NULL);
/* Copies sprite s's pixmap number 'pixmapNo' into the surface
at the specified position.
Uses the sprite's size but not its position.
If 'surface' is null, the visible screen is used.
*/
void writeString(const char *s, Couple pos,
SDL_Surface *surface = NULL);
void writeString(const std::string &s, Couple pos,
SDL_Surface *surface = NULL);
void writeStringCentered(const char *s, Couple pos,
SDL_Surface *surface = NULL);
void writeStringCentered(const std::string &s, Couple pos,
SDL_Surface *surface = NULL);
void writeStringXCentered(const char *s, int y,
SDL_Surface *surface = NULL);
void writeStringXCentered(const std::string &s, int y,
SDL_Surface *surface = NULL);
void writeStringXCentered(const char *s, Couple pos,
SDL_Surface *surface = NULL);
void writeStringXCentered(const std::string &s, Couple pos,
SDL_Surface *surface = NULL);
void writeStringRightJustified(const char *s, Couple pos,
SDL_Surface *surface = NULL);
void writeStringRightJustified(const std::string &s, Couple pos,
SDL_Surface *surface = NULL);
/* Writes the given zero-terminated Latin-1 (ISO-8859-1) string
at the given position.
Uses a fixed width font whose dimensions are given by
getFontDimensions().
If 'surface' is null, the visible screen is used.
*/
void drawPixel(int x, int y, Uint32 color);
/* Sets the color of the pixel at the given position.
The upper-left corner of the game screen is at (0, 0).
*/
void drawLine(int x1, int y1, int x2, int y2, Uint32 color);
/* Draws a line with the given color that goes from point (x1, y1)
to point (x2, y2).
*/
void fillRect(int x, int y, int width, int height, Uint32 color);
/* Fills a rectangle with the given color at an upper-left corner
given by (x, y) and with dimensions given by width and height.
*/
Couple getFontDimensions() const;
/* Returns the width and height (in the x and y fields) of the fixed
font used by writeString().
*/
bool waitForReactivation();
/* Sleeps while waiting for SDL events until a reactivation event
or a quit event is received.
Returns true to request the continuation of the program.
Returns false to request quitting the program.
Returns false if a wait error occurs.
Never called if processActiveEvent argument of constructor is false.
*/
private:
void putpixel(SDL_Surface *surface, int x, int y, Uint32 pixel);
void initWuLineAlgorithm();
void wu_line(SDL_Surface *surface,
Uint32 x0, Uint32 y0, Uint32 x1, Uint32 y1,
Uint32 fgc, Uint32 bgc);
/* Forbidden operations:
*/
GameEngine(const GameEngine &x);
GameEngine &operator = (const GameEngine &x);
};
inline
bool
GameEngine::inFullScreenMode() const
{
return usingFullScreen;
}
inline
int
GameEngine::getScreenWidthInPixels() const
{
return theScreenSizeInPixels.x;
}
inline
int
GameEngine::getScreenHeightInPixels() const
{
return theScreenSizeInPixels.y;
}
inline
void
GameEngine::copyPixmap(SDL_Surface *src, Couple dest,
SDL_Surface *surface) const
{
if (surface == NULL)
surface = theSDLScreen;
SDL_Rect dstrect = { Sint16(dest.x), Sint16(dest.y), 0, 0 };
SDL_BlitSurface(src, NULL, surface, &dstrect);
}
inline
void
GameEngine::copySpritePixmap(const Sprite &s, size_t pixmapNo,
Couple posInSurface, SDL_Surface *surface)
{
if (surface == NULL)
surface = theSDLScreen;
SDL_Surface *image = s.getPixmap(pixmapNo);
SDL_Rect dstrect = { Sint16(posInSurface.x), Sint16(posInSurface.y), 0, 0 };
SDL_BlitSurface(image, NULL, surface, &dstrect);
/* We suppose that the image has a color key that indicates
which pixel is of the transparent color.
See SDL doc re: SDL_SetColorKey().
*/
}
inline
void
GameEngine::copySpritePixmap(const RSprite &s, size_t pixmapNo,
RCouple posInSurface, SDL_Surface *surface)
{
if (surface == NULL)
surface = theSDLScreen;
SDL_Surface *image = s.getPixmap(pixmapNo);
Couple p = posInSurface.round();
SDL_Rect dstrect = { Sint16(p.x), Sint16(p.y), 0, 0 };
SDL_BlitSurface(image, NULL, surface, &dstrect);
/* We suppose that the image has a color key that indicates
which pixel is of the transparent color.
See SDL doc re: SDL_SetColorKey().
*/
}
inline
void
GameEngine::writeString(const std::string &s, Couple pos,
SDL_Surface *surface)
{
writeString(s.c_str(), pos, surface);
}
inline
void
GameEngine::writeStringCentered(const std::string &s, Couple pos,
SDL_Surface *surface)
{
writeStringCentered(s.c_str(), pos, surface);
}
inline
void
GameEngine::writeStringXCentered(const std::string &s, Couple pos,
SDL_Surface *surface)
{
writeStringXCentered(s.c_str(), pos, surface);
}
inline
void
GameEngine::writeStringXCentered(const char *s, int y,
SDL_Surface *surface)
{
writeStringXCentered(s,
Couple(getScreenWidthInPixels() / 2, y),
surface);
}
inline
void
GameEngine::writeStringXCentered(const std::string &s, int y,
SDL_Surface *surface)
{
writeStringXCentered(s.c_str(), y, surface);
}
inline
void
GameEngine::writeStringRightJustified(const std::string &s, Couple pos,
SDL_Surface *surface)
{
writeStringRightJustified(s.c_str(), pos, surface);
}
inline
void
GameEngine::drawPixel(int x, int y, Uint32 color)
{
putpixel(theSDLScreen, x, y, color);
}
inline
void
GameEngine::drawLine(int x1, int y1, int x2, int y2, Uint32 color)
{
wu_line(theSDLScreen, Uint32(x1), Uint32(y1), Uint32(x2), Uint32(y2), color, 0);
}
inline
void
GameEngine::fillRect(int x, int y, int width, int height, Uint32 color)
{
SDL_Rect rect = { x, y, width, height };
(void) SDL_FillRect(theSDLScreen, &rect, color);
}
inline
Couple
GameEngine::getFontDimensions() const
{
return Couple(7, 13);
}
} // namespace flatzebra
#endif /* _H_GameEngine */
|