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

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