1 /*************************************************************************** 2 sgw.hpp - Header file for SDL Wrapper classes 3 ------------------- 4 begin : Wed Oct 1 14:06:28 BST 2003 5 copyright : (C) 2003 by Paul Robson 6 email : autismuk@autismuk.freeserve.co.uk 7 ***************************************************************************/ 8 9 /*************************************************************************** 10 * * 11 * This program is free software; you can redistribute it and/or modify * 12 * it under the terms of the GNU General Public License as published by * 13 * the Free Software Foundation; either version 2 of the License, or * 14 * (at your option) any later version. * 15 * * 16 ***************************************************************************/ 17 18 using namespace std; 19 20 #include <iostream> 21 #include <string> 22 23 namespace SDLWrapper // It's all nicely in a namespace SDLWrapper 24 { 25 26 #define DEFAULT_SCX (1024) // Default Screen Size and Depth 27 #define DEFAULT_SCY (768) 28 #define DEFAULT_SCBPP (0) 29 #define MAXSOUND (16) // Maximum number of sounds 30 31 #define SDWASSERT(x) if (!(x)) SDWERROR() // ASSERT and ERROR macros 32 #define SDWERROR() FatalError(__LINE__,__FILE__) 33 34 int Main(int argc,char *argv[]); // Main program must be defined. 35 char *Name(char *Name); // Return Window Caption Name must be defined 36 37 void SetSpeed(int n); 38 void FatalError(int Line,string File); // Prototyping 39 40 int GameClock(void); // No point in wrapping this, it just goes to SDL_GetTicks() 41 int SystemClock(void); // This one is unaffected by the game speed. 42 43 int ReadStick(int &A,int &B,int &dx,int &dy); // Read a joystick 44 int MouseClick(int &x,int &y); // Read a mouse select - and - click 45 int ExitKey(void); 46 int GetKey(void); 47 48 class Rect 49 { 50 public: Rect()51 Rect() {} // Constructors Rect(int x1,int y1,int x2,int y2)52 Rect(int x1,int y1,int x2,int y2) { Left = x1;Top = y1;Right = x2;Bottom = y2; } 53 int Left,Top,Right,Bottom; // The rectangle 54 }; 55 56 class Surface // A basic Surface 57 { 58 public: 59 Surface(int x = 0,int y = 0,int Trans = 0,int UseDisplay = 0,char *File = NULL); 60 ~Surface(); 61 62 void SetColour(int r,int g,int b); SetColour()63 void SetColour() { SetColour(-1,-1,-1); } SetColour(int Col)64 void SetColour(int Col) { Colour = Col; } GetColour(void)65 unsigned int GetColour(void) { return Colour; } Width(void)66 int Width(void) { return xSize; } Height(void)67 int Height(void) { return ySize; } SetOrigin(int x=0,int y=0)68 void SetOrigin(int x = 0,int y = 0) { xOrigin = x; yOrigin = y; } SetScale(int x=256,int y=256)69 void SetScale(int x = 256,int y = 256) { xScale = x; yScale = y; } 70 71 void Plot(int x1,int y1); 72 void FillRect(int x1=0,int y1=0,int x2=0,int y2=0); FillRect(Rect & r)73 void FillRect(Rect &r) { FillRect(r.Left,r.Top,r.Right,r.Bottom); } 74 void FrameRect(int x1=0,int y1=0,int x2=0,int y2=0); FrameRect(Rect & r)75 void FrameRect(Rect &r) { FrameRect(r.Left,r.Top,r.Right,r.Bottom); } 76 void FillEllipse(int x1=0,int y1=0,int x2=0,int y2=0); FillEllipse(Rect & r)77 void FillEllipse(Rect &r) { FillEllipse(r.Left,r.Top,r.Right,r.Bottom); } 78 void FrameEllipse(int x1=0,int y1=0,int x2=0,int y2=0); FrameEllipse(Rect & r)79 void FrameEllipse(Rect &r) { FrameEllipse(r.Left,r.Top,r.Right,r.Bottom); } 80 void Line(int x1=0,int y1=0,int x2=0,int y2=0); 81 82 void Copy(Surface &Target,Rect &SrcRect,int x = 0,int y = 0); 83 void Copy(Rect &SrcRect,int x = 0,int y = 0); 84 void Copy(Surface &Target,int x = 0,int y = 0); 85 void Copy(int x = 0,int y = 0); 86 87 void HorizontalMirror(int x1 = 0,int y1 = 0,int x2 = 0,int y2 = 0); 88 void VerticalMirror(int x1 = 0,int y1 = 0,int x2 = 0,int y2 = 0); 89 90 void Char(int x1=0,int y1=0,int x2=0,int y2=0,char c = ' '); Char(Rect & r,char c)91 void Char(Rect &r,char c) { Char(r.Left,r.Top,r.Right,r.Bottom,c); } 92 void String(int x1=0,int y1=0,int x2=0,int y2=0,char *s = ""); String(Rect & r,char * s)93 void String(Rect &r,char *s) { String(r.Left,r.Top,r.Right,r.Bottom,s); } 94 95 void Flip(void); 96 97 98 protected: 99 void SortAndValidate(int &x1,int &y1,int &x2,int &y2); 100 void PointProcess(int &x1,int &y1); 101 102 private: 103 void *sSurface; // Surface = actually SDL_Surface but I don't wanna include SDL 104 int xSize,ySize; // Surface size (physical) 105 unsigned int Colour; // Drawing colour 106 int IsTransparent; // Set if transparent 107 unsigned int TransColour; // Transparency drawing colour 108 int IsDisplay; // Set if is the physical display object 109 int xOrigin,yOrigin; // Mobile origin and scaling 110 int xScale,yScale; 111 } ; 112 113 class TransparentSurface : public Surface // A surface but with transparency 114 { 115 public: TransparentSurface(int x=0,int y=0)116 TransparentSurface(int x = 0,int y = 0) : Surface(x,y,1,0,NULL) { } 117 }; 118 119 class BitmapSurface : public Surface // A surface with a bitmap on it, one solid, one transparent 120 { 121 public: BitmapSurface(char * File)122 BitmapSurface(char *File) : Surface(0,0,0,0,File) {} 123 }; 124 125 class TransparentBitmapSurface : public Surface 126 { 127 public: TransparentBitmapSurface(char * File)128 TransparentBitmapSurface(char *File) : Surface(0,0,1,0,File) {} 129 }; 130 131 class DisplaySurface : public Surface // The actual physical display 132 { 133 public: DisplaySurface(int x=0,int y=0)134 DisplaySurface(int x = 0,int y = 0) : Surface(0,0,0,1,NULL) { } 135 }; 136 137 class Timer // A simple timer 138 { 139 public: 140 Timer(int TimeOut = 0); 141 void ResetTimer(int t = 0); 142 unsigned int Elapsed(void); 143 int TimedOut(void); 144 void WaitTimer(void); 145 146 private: 147 int StartClock; 148 int EndClock; 149 int EventTime; 150 }; 151 152 class AudioObject // An audio object 153 { 154 public: AudioObject()155 AudioObject() { Data = NULL; Position = Length = 0;Attach(); SoundOn = 0;LoopSound = 0; } ~AudioObject()156 ~AudioObject() { Detach();if (Data != NULL) free(Data); } 157 void CopyStream(void *Stream,int Reqd); Play(void)158 void Play(void) { Position = 0;SoundOn = 1; } PlayLoop(void)159 void PlayLoop(void) { Position = 0;SoundOn = 1;LoopSound = 1; } Stop(void)160 void Stop(void) { SoundOn = 0; LoopSound = 0; } Size(void)161 int Size(void) { return Length/2; } 162 void Write(int Pos,int Dat); 163 protected: 164 void Attach(void); 165 void Detach(void); 166 void *Data; 167 int Position; 168 int Length; 169 int SoundOn; 170 int LoopSound; 171 }; 172 173 class AudioWave : public AudioObject 174 { 175 public: AudioWave(char * File)176 AudioWave(char *File) : AudioObject() { Load(File); } 177 protected: 178 void Load(char *File); 179 }; 180 181 class AudioBeep : public AudioObject 182 { 183 public: AudioBeep(int p,int l)184 AudioBeep(int p,int l) : AudioObject() { CreateBeep(p,l); } 185 protected: 186 void CreateWave(void *Data,int Size,int sPitch); 187 void CreateBeep(int sPitch,int sLength); 188 }; 189 190 class AudioNoise : public AudioBeep 191 { 192 public: AudioNoise(int l)193 AudioNoise(int l) : AudioBeep(0,l) { } 194 }; 195 196 }; 197