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

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