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

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