Added build option to build "stratego" without graphics
[progcomp2012.git] / judge / manager / graphics.h
1 //#define BUILD_GRAPHICS
2 #ifdef BUILD_GRAPHICS
3
4 #ifndef GRAPHICS_H
5 #define GRAPHICS_H
6
7
8 #include <SDL/SDL.h>
9 #include <SDL/SDL_opengl.h>
10
11 typedef SDL_Surface Screen;
12 typedef SDL_Rect Rectangle;
13 typedef short unsigned int SUint;
14 typedef unsigned char Uint8;
15
16 #include <vector>
17 #include <list>
18
19
20
21
22
23
24
25 class Texture;
26 class Font;
27
28
29
30 class Graphics
31 {
32         public:
33
34         class Colour
35         {
36                 public:
37                         Colour(float red=0, float green=0, float blue=0, float alpha=0) : r(red), g(green), b(blue), a(alpha) {}
38                         Colour(const Colour & cpy) : r(cpy.r), g(cpy.g), b(cpy.b), a(cpy.a) {}
39         
40                         Colour & operator=(const Colour & s) {r = s.r; g = s.g; b = s.b; a = s.a; return *this;}
41                         bool operator==(const Colour & s) const {return (r == s.r && g == s.g && b == s.b && a == s.a);}
42                         bool operator!=(const Colour & s) const {return !operator==(s);}
43                         float r;
44                         float g;
45                         float b;
46                         float a;
47         
48         };
49                 static int ScreenWidth() {return screenWidth;}
50                 static int ScreenHeight() {return screenHeight;}
51
52                 static void Initialise(const char * caption, int width=640, int height=480);
53                 static void Destroy();
54
55                 static SDL_Surface * LoadTextureBMP(const char * file);
56                 static void SaveTextureBMP(SDL_Surface * tex, const char * file);
57                 
58                 static void ClearScreen();
59                 static void UpdateScreen();
60                 static void DrawGrid(int gridWidth, int gridHeight, Colour colour);
61                 static Uint8 MakeColour(int R, int G, int B, int Alpha = 0);
62                 static Colour ConvertColour(Uint8 colour);
63
64                 static void DrawTexture(SDL_Surface * src, int destX, int destY, int srcX=0, int srcY=0, int width=-1, int height=-1);
65                 static void DrawTexture(SDL_Surface * src, int destX, int destY, double rotate, double scale);
66                 static void DrawPixel(int x, int y, Colour colour);
67
68                 static Colour GetPixel(int x, int y);
69                 static void DrawLine(int x1, int y1, int x2, int y2, Colour colour, double scale=1.0);
70                 static void DrawLineDashed(int x1, int y1, int x2, int y2, Colour colour, double scale=1.0);
71                 static void DrawRectangle(int x1, int y1, int x2, int y2, Colour colour, double scale=1.0);
72
73                 static void GetColourData(SDL_Surface * src, std::vector<SUint> * R, std::vector<SUint> * G, std::vector<SUint> * B, std::vector<SUint> * A = NULL);
74                 static void GetColourData(SDL_Surface * src, std::vector<std::vector<SUint> > * R, std::vector<std::vector<SUint> > * G,  std::vector<std::vector<SUint> > * B,  std::vector<std::vector<SUint> > * A = NULL);
75
76                 static void DrawColourData(int destX, int destY, std::vector<SUint> * R, std::vector<SUint> * G, std::vector<SUint> * B, std::vector<SUint> * A = NULL) {DrawColourData(screen, destX, destY, R, G, B, A);}
77
78                 static void DrawColourData(int destX, int destY, std::vector<std::vector<SUint> > * R, std::vector<std::vector<SUint> > * G, std::vector<std::vector<SUint> > * B, std::vector<std::vector<SUint> > * A = NULL) {DrawColourData(screen, destX, destY, R, G, B, A);}
79
80                 static SDL_Surface * TextureFromColours(std::vector<SUint> * R, std::vector<SUint> * G, std::vector<SUint> * B, std::vector<SUint> * A = NULL);
81                 static SDL_Surface * TextureFromColours(std::vector<std::vector<SUint> > * R, std::vector<std::vector<SUint> > * G, std::vector<std::vector<SUint> > * B, std::vector<std::vector<SUint> > * A = NULL);
82                 
83                 static void Wait(int n);
84
85                 template <class T>
86                 class TextureManager
87                 {
88                         public:
89                                 TextureManager() {}
90                                 virtual ~TextureManager() {}
91
92                                 virtual Texture & operator[](const T & at) = 0;
93                 };
94
95                 static bool Initialised() {return initialised;}
96
97         protected:
98                 static void DrawColourData(SDL_Surface * dest, int destX, int destY, std::vector<SUint> * R, std::vector<SUint> * G, std::vector<SUint> * B, std::vector<SUint> * A = NULL);
99                 static void DrawColourData(SDL_Surface * dest, int destX, int destY, std::vector<std::vector<SUint> > * R, std::vector<std::vector<SUint> > * G, std::vector<std::vector<SUint> > * B, std::vector<std::vector<SUint> > * A = NULL);
100                 static void DrawTexture(SDL_Surface * dest, SDL_Surface * src, int srcX, int srcY, int destX, int destY, int width, int height);
101                 static void DrawPixel(SDL_Surface * dest, int x, int y, Colour colour);
102                 static Colour GetPixel(SDL_Surface * dest, int x, int y);
103                 static void DrawLine(SDL_Surface * dest, int x1, int y1, int x2, int y2, Colour colour);
104
105         private:
106                 Graphics() {}
107                 ~Graphics() {}  
108                 static std::list<SDL_Surface*> allTextures;
109                 static Screen * screen;
110
111                 static int screenWidth;
112                 static int screenHeight;
113                 static bool initialised;
114                 
115
116 };
117 typedef Graphics::Colour Colour;
118
119 class Texture
120 {
121         public:
122                 Texture(const char * fileName, bool newDrawCentred = true);
123                 virtual ~Texture();
124
125                 void Draw(int x, int y, double angle=0, double scale=1);
126                 void DrawColour(int x, int y, double angle, double scale, Colour colour);
127                 
128                 int width() const {return surface->w;}
129                 int height() const {return surface->h;}
130
131         protected:
132                 SDL_Surface * surface;
133                 GLuint texture;
134
135         private:
136                 bool drawCentred;
137                 
138 };      
139
140 class Font : private Texture
141 {
142         public:
143                 Font(const char * fileName, int newWidth, int newHeight);
144                 virtual ~Font();
145
146                 void DrawTextColour(const char * string, int x, int y, double angle, double scale, Colour colour);
147                 void DrawText(const char * string, int x, int y, double angle=0, double scale=1);       
148         private:
149                 int width;
150                 int height;
151 };
152
153
154 #endif //GRAPHICS_H
155
156 #endif //BUILD_GRAPHICS
157
158 //EOF

UCC git Repository :: git.ucc.asn.au