graphics.h 5.11 KB
Newer Older
Sam Moore's avatar
Sam Moore committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#ifndef GRAPHICS_H
#define GRAPHICS_H

#include <SDL/SDL.h>
#include <SDL/SDL_opengl.h>


#include <vector>
#include <list>


typedef SDL_Surface Screen;
typedef SDL_Rect Rectangle;


typedef short unsigned int SUint;

class Texture;
class Font;



class Graphics
{
	public:

	class Colour
	{
		public:
			Colour(float red=0, float green=0, float blue=0, float alpha=0) : r(red), g(green), b(blue), a(alpha) {}
			Colour(const Colour & cpy) : r(cpy.r), g(cpy.g), b(cpy.b), a(cpy.a) {}
	
			Colour & operator=(const Colour & s) {r = s.r; g = s.g; b = s.b; a = s.a; return *this;}
			bool operator==(const Colour & s) const {return (r == s.r && g == s.g && b == s.b && a == s.a);}
			bool operator!=(const Colour & s) const {return !operator==(s);}
			float r;
			float g;
			float b;
			float a;
	
	};
		static int ScreenWidth() {return screenWidth;}
		static int ScreenHeight() {return screenHeight;}

		static void Initialise(const char * caption, int width=640, int height=480);
		static void Destroy();

		static SDL_Surface * LoadTextureBMP(const char * file);
		static void SaveTextureBMP(SDL_Surface * tex, const char * file);
		
		static void ClearScreen();
		static void UpdateScreen();
		static void DrawGrid(int gridWidth, int gridHeight, Colour colour);
		static Uint8 MakeColour(int R, int G, int B, int Alpha = 0);
		static Colour ConvertColour(Uint8 colour);

		static void DrawTexture(SDL_Surface * src, int destX, int destY, int srcX=0, int srcY=0, int width=-1, int height=-1);
		static void DrawTexture(SDL_Surface * src, int destX, int destY, double rotate, double scale);
		static void DrawPixel(int x, int y, Colour colour);

		static Colour GetPixel(int x, int y);
		static void DrawLine(int x1, int y1, int x2, int y2, Colour colour, double scale=1.0);
		static void DrawLineDashed(int x1, int y1, int x2, int y2, Colour colour, double scale=1.0);
		static void DrawRectangle(int x1, int y1, int x2, int y2, Colour colour, double scale=1.0);

		static void GetColourData(SDL_Surface * src, std::vector<SUint> * R, std::vector<SUint> * G, std::vector<SUint> * B, std::vector<SUint> * A = NULL);
		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);

		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);}

		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);}

		static SDL_Surface * TextureFromColours(std::vector<SUint> * R, std::vector<SUint> * G, std::vector<SUint> * B, std::vector<SUint> * A = NULL);
		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);

		static void Wait(int n) {SDL_Delay(n);}

		template <class T>
		class TextureManager
		{
			public:
				TextureManager() {}
				virtual ~TextureManager() {}

				virtual Texture & operator[](const T & at) = 0;
		};

		static bool Initialised() {return initialised;}

	protected:
		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);
		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);
		static void DrawTexture(SDL_Surface * dest, SDL_Surface * src, int srcX, int srcY, int destX, int destY, int width, int height);
		static void DrawPixel(SDL_Surface * dest, int x, int y, Colour colour);
		static Colour GetPixel(SDL_Surface * dest, int x, int y);
		static void DrawLine(SDL_Surface * dest, int x1, int y1, int x2, int y2, Colour colour);

	private:
		Graphics() {}
		~Graphics() {}	
		static std::list<SDL_Surface*> allTextures;
		static Screen * screen;

		static int screenWidth;
		static int screenHeight;
		static bool initialised;
		

};
typedef Graphics::Colour Colour;

class Texture
{
	public:
		Texture(const char * fileName, bool newDrawCentred = true);
		virtual ~Texture();

		void Draw(int x, int y, double angle=0, double scale=1);
		void DrawColour(int x, int y, double angle, double scale, Colour colour);
		
		int width() const {return surface->w;}
		int height() const {return surface->h;}
	protected:
		SDL_Surface * surface;
		GLuint texture;

	private:
		bool drawCentred;
		
};	

class Font : private Texture
{
	public:
		Font(const char * fileName, int newWidth, int newHeight);
		virtual ~Font();

		void DrawTextColour(const char * string, int x, int y, double angle, double scale, Colour colour);
		void DrawText(const char * string, int x, int y, double angle=0, double scale=1);	
	private:
		int width;
		int height;
};


#endif //GRAPHICS_H

//EOF