Browse Source

Initialisierung des GIT-Repositorys

seba 12 years ago
commit
9de0171fc5
34 changed files with 2009 additions and 0 deletions
  1. 2
    0
      .gitignore
  2. 22
    0
      Makefile
  3. 243
    0
      emath.cpp
  4. 74
    0
      emath.h
  5. 46
    0
      emath_opengl.cpp
  6. 11
    0
      emath_opengl.h
  7. 30
    0
      glcolor.cpp
  8. 24
    0
      glcolor.h
  9. 24
    0
      gldrawhelper.cpp
  10. 12
    0
      gldrawhelper.h
  11. 231
    0
      glfontengine.cpp
  12. 70
    0
      glfontengine.h
  13. 12
    0
      glgui/Makefile
  14. 18
    0
      glgui/button.cpp
  15. 28
    0
      glgui/button.h
  16. 7
    0
      glgui/object.cpp
  17. 29
    0
      glgui/object.h
  18. 37
    0
      glgui/textlabel.cpp
  19. 32
    0
      glgui/textlabel.h
  20. 39
    0
      glgui/window.cpp
  21. 36
    0
      glgui/window.h
  22. 35
    0
      glrect.cpp
  23. 22
    0
      glrect.h
  24. 172
    0
      gltexture.cpp
  25. 63
    0
      gltexture.h
  26. 177
    0
      main.cpp
  27. 149
    0
      matrix.cpp
  28. 42
    0
      matrix.h
  29. 77
    0
      quaternion.cpp
  30. 30
    0
      quaternion.h
  31. 91
    0
      rotationsmatrix.cpp
  32. 30
    0
      rotationsmatrix.h
  33. 82
    0
      sdlfuncs.cpp
  34. 12
    0
      sdlfuncs.h

+ 2
- 0
.gitignore View File

@@ -0,0 +1,2 @@
1
+*.o
2
+*.a

+ 22
- 0
Makefile View File

@@ -0,0 +1,22 @@
1
+COMPILER = g++
2
+OBJECTS = emath.o emath_opengl.o glcolor.o gldrawhelper.o glfontengine.o glrect.o gltexture.o matrix.o quaternion.o rotationsmatrix.o glgui/glgui.a
3
+VERSION = 0.0.1
4
+
5
+
6
+sgllib: $(OBJECTS)
7
+
8
+sglar: $(OBJECTS)
9
+	rm -f sgllib.a
10
+	ar rfc sgllib.a $(OBJECTS)
11
+	
12
+%.o: %.cpp %.h
13
+	$(COMPILER) -c `sdl-config --cflags` $<
14
+	
15
+	
16
+glgui/glgui.a:
17
+	cd glgui; $(MAKE);
18
+	
19
+clean:
20
+	rm -f $(OBJECTS)
21
+	cd glgui; $(MAKE) clean
22
+	@echo Done cleaning...

+ 243
- 0
emath.cpp View File

@@ -0,0 +1,243 @@
1
+#include "emath.h"
2
+
3
+Punkt3D::Punkt3D() {
4
+	x = y = z = 0.0f;
5
+}
6
+
7
+Punkt3D::Punkt3D(float _x, float _y, float _z) {
8
+	set(_x, _y, _z);
9
+}
10
+
11
+void Punkt3D::set(float _x, float _y, float _z) {
12
+	x = _x;
13
+	y = _y;
14
+	z = _z;
15
+}
16
+
17
+float Punkt3D::abs() {
18
+	return sqrt(x*x+y*y+z*z);
19
+}
20
+
21
+Punkt3D Punkt3D::kreuzprodukt(const Punkt3D &b) {
22
+	Punkt3D erg;
23
+	erg.x = y*b.z - z*b.y;
24
+	erg.y = z*b.x - x*b.z;
25
+	erg.z = x*b.y - y*b.x;
26
+	
27
+	return erg;
28
+}
29
+
30
+void Punkt3D::normalize() {
31
+	float a = abs();
32
+	x /= a;
33
+	y /= a;
34
+	z /= a;
35
+}
36
+
37
+Punkt3D Punkt3D::getNormalized() {
38
+	Punkt3D ret(*this);
39
+	ret.normalize();
40
+	return ret;
41
+}
42
+
43
+bool Punkt3D::isNormalized() {
44
+	return (abs()==1);
45
+}
46
+
47
+float Punkt3D::calcAngle(Punkt3D b) {
48
+	if(abs()*b.abs()==0.0f)
49
+		return 0.0f;
50
+	return rad2deg(std::acos(((*this)*b)/(abs()*b.abs())));
51
+}
52
+
53
+Punkt3D Punkt3D::getOrtographic() {
54
+	Punkt3D erg;
55
+	if(!x) {
56
+		erg.x = 0.0f;
57
+		erg.y = z;
58
+		erg.z = -y;
59
+	} else if(!y) {
60
+		erg.x = z;
61
+		erg.y = 0;
62
+		erg.z = -x;
63
+	} else {
64
+		// z, oder genereller fall
65
+		erg.x = y;
66
+		erg.y = -x;
67
+		erg.z = 0.0f;
68
+	}
69
+	return erg;
70
+}
71
+
72
+void Punkt3D::print(std::string coordname) {
73
+	if(coordname!="")
74
+		coordname.append(" ");
75
+	std::cout << coordname << "Coord: (" << x << ", " << y << ", " << z << ")" << std::endl;
76
+}
77
+
78
+
79
+
80
+Punkt3D Punkt3D::operator+(const Punkt3D &b) {
81
+	Punkt3D c;
82
+	c.x =  x + b.x;
83
+	c.y =  y + b.y;
84
+	c.z =  z + b.z;
85
+	
86
+	return c;
87
+}
88
+
89
+Punkt3D Punkt3D::operator-(const Punkt3D &b) {
90
+	Punkt3D c;
91
+	c.x =  x - b.x;
92
+	c.y =  y - b.y;
93
+	c.z =  z - b.z;
94
+	
95
+	return c;
96
+}
97
+
98
+Punkt3D& Punkt3D::operator+=(const Punkt3D &b) {
99
+	x +=  b.x;
100
+	y +=  b.y;
101
+	z +=  b.z;
102
+	
103
+	return *this;
104
+}
105
+
106
+Punkt3D& Punkt3D::operator-=(const Punkt3D &b) {
107
+	x -=  b.x;
108
+	y -=  b.y;
109
+	z -=  b.z;
110
+	
111
+	return *this;
112
+}
113
+
114
+Punkt3D Punkt3D::operator+(const float &_m) {
115
+	return Punkt3D(x+_m, y+_m, z+_m);
116
+}
117
+
118
+Punkt3D Punkt3D::operator-(const float &_m) {
119
+	return Punkt3D(x-_m, y-_m, z-_m);
120
+}
121
+
122
+Punkt3D Punkt3D::operator*(const float &_m) {
123
+	return Punkt3D(x*_m, y*_m, z*_m);
124
+}
125
+
126
+Punkt3D Punkt3D::operator/(const float &_m) {
127
+	return Punkt3D(x/_m, y/_m, z/_m);
128
+}
129
+
130
+Punkt3D& Punkt3D::operator+=(const float &_m) {
131
+	x += _m;
132
+	y += _m;
133
+	z += _m;
134
+	return *this;
135
+}
136
+Punkt3D& Punkt3D::operator-=(const float &_m) {
137
+	x -= _m;
138
+	y -= _m;
139
+	z -= _m;
140
+	return *this;
141
+}
142
+Punkt3D& Punkt3D::operator*=(const float &_m) {
143
+	x *= _m;
144
+	y *= _m;
145
+	z *= _m;
146
+	return *this;
147
+}
148
+Punkt3D& Punkt3D::operator/=(const float &_m) {
149
+	x /= _m;
150
+	y /= _m;
151
+	z /= _m;
152
+	return *this;
153
+}
154
+
155
+float Punkt3D::operator*(const Punkt3D& _m) {
156
+	return x * _m.x + y * _m.y + z * _m.z;
157
+}
158
+
159
+Punkt3D Punkt3D::operator-() {
160
+	return Punkt3D(-x, -y, -z);
161
+}
162
+
163
+bool Punkt3D::operator==(const Punkt3D& b) {
164
+	return ( x==b.x && y==b.y && z==b.z);
165
+}
166
+bool Punkt3D::operator!=(const Punkt3D& b) {
167
+	return !(*this==b);
168
+}
169
+// Freunde
170
+
171
+Punkt3D operator+(const float& _m, const Punkt3D& b) {
172
+	return Punkt3D(b.x+_m, b.y+_m, b.z+_m);
173
+}
174
+
175
+Punkt3D operator-(const float& _m, const Punkt3D& b) {
176
+	return Punkt3D(b.x-_m, b.y-_m, b.z-_m);
177
+}
178
+
179
+Punkt3D operator*(const float& _m, const Punkt3D& b) {
180
+	return Punkt3D(b.x*_m, b.y*_m, b.z*_m);
181
+}
182
+
183
+Punkt3D operator/(const float& _m, const Punkt3D& b) {
184
+	return Punkt3D(b.x/_m, b.y/_m, b.z/_m);
185
+}
186
+
187
+float abs(Punkt3D p) {
188
+	return p.abs();
189
+}
190
+
191
+// OpenGL Funktionen für Punkt3D
192
+
193
+void glVertex3f(Punkt3D p) {
194
+	glVertex3f(p.x, p.y, p.z);
195
+}
196
+
197
+void glTranslatef(Punkt3D p) {
198
+	glTranslatef(p.x, p.y, p.z);
199
+}
200
+
201
+void glNormal(Punkt3D p) {
202
+	glNormal3f(p.x, p.y, p.z);
203
+}
204
+
205
+void glRotatef(float deg, Punkt3D vec) {
206
+	glRotatef(deg, vec.x, vec.y, vec.z);
207
+}
208
+
209
+// Trigonometrische Funktionen
210
+
211
+float deg2rad(float deg) {
212
+	return (deg/180.0f)*3.1415926535897932384626433f;
213
+}
214
+
215
+float rad2deg(float rad) {
216
+	return (rad/3.1415926535897932384626433f)*180.0f;
217
+}
218
+
219
+float ssin(float c) {
220
+	int t=(int)c;
221
+	if(t!=c)
222
+		return sin(deg2rad(c));
223
+	t = t % 360;
224
+	if(t<0)
225
+		t = 360 + t;
226
+	switch(t) {
227
+		case 0:
228
+			return 0.0f;
229
+		case 90:
230
+			return 1.0f;
231
+		case 180:
232
+			return 0.0f;
233
+		case 270:
234
+			return -1.0f;
235
+		default:
236
+// 			std::cout << "ssin sollte keinen defaultwert zurückliefern. c=" << c << ", t=" << t << std::endl;
237
+			return sin(deg2rad(c));
238
+	}
239
+}
240
+
241
+float scos(float c) {
242
+	return ssin(c+90.0f);
243
+}

+ 74
- 0
emath.h View File

@@ -0,0 +1,74 @@
1
+#ifndef __EMATH_H
2
+#define __EMATH_H
3
+
4
+#include <iostream>
5
+#include <cmath>
6
+#include <SDL_opengl.h>
7
+
8
+class Punkt3D {
9
+	public:
10
+		Punkt3D();
11
+		Punkt3D(float, float, float);
12
+		
13
+		float x, y, z;
14
+		
15
+		void set(float, float, float);
16
+		float abs();
17
+		Punkt3D kreuzprodukt(const Punkt3D&);
18
+		void normalize();
19
+		Punkt3D getNormalized();
20
+		bool isNormalized();
21
+		float calcAngle(Punkt3D);
22
+		Punkt3D getOrtographic();
23
+		
24
+		void print(std::string="");
25
+		
26
+		
27
+		// Operatoren
28
+		Punkt3D operator+(const Punkt3D&);
29
+		Punkt3D operator-(const Punkt3D&);
30
+		Punkt3D& operator+=(const Punkt3D&);
31
+		Punkt3D& operator-=(const Punkt3D&);
32
+		
33
+		Punkt3D operator+(const float&);
34
+		Punkt3D operator-(const float&);
35
+		Punkt3D operator*(const float&);
36
+		Punkt3D operator/(const float&);
37
+		Punkt3D& operator+=(const float&);
38
+		Punkt3D& operator-=(const float&);
39
+		Punkt3D& operator*=(const float&);
40
+		Punkt3D& operator/=(const float&);
41
+		
42
+		float operator*(const Punkt3D&);
43
+		
44
+		Punkt3D operator-();
45
+
46
+		bool operator==(const Punkt3D&);
47
+		bool operator!=(const Punkt3D&);
48
+		
49
+		friend Punkt3D operator+(const float&, const Punkt3D&);
50
+		friend Punkt3D operator-(const float&, const Punkt3D&);
51
+		friend Punkt3D operator*(const float&, const Punkt3D&);
52
+		friend Punkt3D operator/(const float&, const Punkt3D&);
53
+};
54
+
55
+float abs(Punkt3D);
56
+
57
+// OpenGL-Funktionen für Punkt3D
58
+
59
+void glVertex3f(Punkt3D);
60
+void glTranslatef(Punkt3D);
61
+void glNormal3f(Punkt3D);
62
+
63
+void glRotatef(float, Punkt3D);
64
+
65
+float deg2rad(float deg);
66
+float rad2deg(float rad);
67
+
68
+
69
+
70
+// *grml* scheiss cmath-sinus
71
+float ssin(float);
72
+float scos(float);
73
+
74
+#endif

+ 46
- 0
emath_opengl.cpp View File

@@ -0,0 +1,46 @@
1
+#include "emath_opengl.h"
2
+
3
+void rotFrom2VecTo2Vec(Punkt3D a, Punkt3D b, Punkt3D c, Punkt3D d, Punkt3D *rvec, float *rvecdeg, Punkt3D *rvec2, float *rvecdeg2) {
4
+	Punkt3D rotvec, rotvec2, nullvec;
5
+	float rotvecdeg = 0.0f, rotvecdeg2 = 0.0f;
6
+	
7
+	rotvec = a.kreuzprodukt(b);
8
+	rotvecdeg = a.calcAngle(b);
9
+	if(std::abs(rotvecdeg)==180.0f) {
10
+		rotvec = a.getOrtographic();
11
+	}
12
+	
13
+	if(rotvec==nullvec) {
14
+		rotvec.set(1.0f, 0.0f, 0.0f);
15
+		rotvecdeg = 0.0f;
16
+	}
17
+	
18
+	Rotationsmatrix rotnobj(rotvec, -rotvecdeg);
19
+	c = Rotationsmatrix(rotvec, rotvecdeg) * c;
20
+	
21
+	rotvec2 = c.kreuzprodukt(d);
22
+	rotvecdeg2 = c.calcAngle(d);
23
+	if(std::abs(rotvecdeg2)==180.0f) {
24
+		rotvec2 = b;
25
+	}
26
+	
27
+	if(rotvec2==nullvec) {
28
+		rotvec2.set(1.0f, 0.0f, 0.0f);
29
+		rotvecdeg2 = 0.0f;
30
+	}
31
+	
32
+	rotvec2 = rotnobj * rotvec2;
33
+	
34
+	*rvec = rotvec;
35
+	*rvecdeg = rotvecdeg;
36
+	*rvec2 = rotvec2;
37
+	*rvecdeg2 = rotvecdeg2;
38
+}
39
+
40
+void rotFrom2VecTo2Vec(Punkt3D a, Punkt3D b, Punkt3D c, Punkt3D d) {
41
+	Punkt3D rvec, rvec2;
42
+	float rvecdeg, rvecdeg2;
43
+	rotFrom2VecTo2Vec(a, b, c, d, &rvec, &rvecdeg, &rvec2, &rvecdeg2);
44
+	glRotatef(rvecdeg, rvec);
45
+	glRotatef(rvecdeg2, rvec2);
46
+}

+ 11
- 0
emath_opengl.h View File

@@ -0,0 +1,11 @@
1
+#ifndef __EMATH_OPENGL
2
+#define __EMATH_OPENGL
3
+
4
+#include <cmath>
5
+#include "emath.h"
6
+#include "rotationsmatrix.h"
7
+#include <SDL_opengl.h>
8
+
9
+void rotFrom2VecTo2Vec(Punkt3D a, Punkt3D b, Punkt3D c, Punkt3D d, Punkt3D *, float *, Punkt3D *, float *);
10
+void rotFrom2VecTo2Vec(Punkt3D a, Punkt3D b, Punkt3D c, Punkt3D d);
11
+#endif

+ 30
- 0
glcolor.cpp View File

@@ -0,0 +1,30 @@
1
+#include "glcolor.h"
2
+
3
+GLColor::GLColor() {
4
+	set(0.0f, 0.0f, 0.0f, 1.0f);
5
+	setalpha = true;
6
+}
7
+
8
+GLColor::GLColor(const SDL_Color &c) {
9
+	set(c.r/255.0f, c.g/255.0f, c.b/255.0f);
10
+	setalpha = true;
11
+}
12
+
13
+void GLColor::set(float _r, float _g, float _b, float _a) {
14
+	r = _r;
15
+	g = _g;
16
+	b = _b;
17
+	a = _a;
18
+}
19
+
20
+SDL_Color GLColor::getSDLColor() {
21
+	SDL_Color c = {(Uint8)(r*255.0f), (Uint8)(g*255), (Uint8)(b*255), (Uint8)(a*255)};
22
+}
23
+
24
+void glColorGLC(GLColor c) {
25
+	if(c.setalpha) {
26
+		glColor4f(c.r, c.g, c.b, c.a);
27
+	} else {
28
+		glColor3f(c.r, c.g, c.b);
29
+	}	
30
+}

+ 24
- 0
glcolor.h View File

@@ -0,0 +1,24 @@
1
+#ifndef __GLCOLOR_H
2
+#define __GLCOLOR_H
3
+
4
+#include <SDL.h>
5
+#include <SDL_opengl.h>
6
+
7
+class GLColor {
8
+	public:
9
+		float r, g, b, a;
10
+		bool setalpha;
11
+		
12
+		
13
+		GLColor();
14
+		GLColor(const SDL_Color&);
15
+		
16
+		void set(float _r, float _g, float _b, float _a=1.0f);
17
+		SDL_Color getSDLColor();
18
+};
19
+
20
+void glColorGLC(GLColor c);
21
+// a) setalpha entscheiden lassen
22
+// b) zwei funktionen, eine setzt alpha mit in richtung 3f, 4f..
23
+
24
+#endif

+ 24
- 0
gldrawhelper.cpp View File

@@ -0,0 +1,24 @@
1
+#include "gldrawhelper.h"
2
+
3
+void GLDrawSDLRect(SDL_Rect *rect, GLRect *tex) {
4
+	GLRect tmptex;
5
+	if(tex) {
6
+		tmptex = *tex;
7
+	} else {
8
+		tmptex.set(0.0f, 0.0f, 1.0f, 1.0f);
9
+	}
10
+	
11
+	glBegin(GL_QUADS);
12
+		glTexCoord2f(tmptex.x, tmptex.y);
13
+		glVertex2i(rect->x, rect->y+rect->h);
14
+		
15
+		glTexCoord2f(tmptex.x+tmptex.w, tmptex.y);
16
+		glVertex2i(rect->x+rect->w, rect->y+rect->h);
17
+		
18
+		glTexCoord2f(tmptex.x+tmptex.w, tmptex.y+tmptex.h);
19
+		glVertex2i(rect->x+rect->w, rect->y);
20
+		
21
+		glTexCoord2f(tmptex.x, tmptex.y+tmptex.h);
22
+		glVertex2i(rect->x, rect->y);
23
+	glEnd();
24
+}

+ 12
- 0
gldrawhelper.h View File

@@ -0,0 +1,12 @@
1
+#ifndef __GLDRAWHELPER_H
2
+#define __GLDRAWHELPER_H
3
+
4
+#include <SDL.h>
5
+#include <SDL_opengl.h>
6
+
7
+#include "glrect.h"
8
+
9
+void GLDrawSDLRect(SDL_Rect *rect, GLRect *tex=0);
10
+
11
+
12
+#endif

+ 231
- 0
glfontengine.cpp View File

@@ -0,0 +1,231 @@
1
+#include "glfontengine.h"
2
+
3
+
4
+bool GLFontEngine::addFont(std::string fontfile, std::string fontname) {
5
+	GLFont *tmp = new GLFont(fontfile, 0.685f);
6
+	if(!tmp->font->isLoaded()) {
7
+		delete(tmp);
8
+		std::cerr << fontfile << " konnte als Font nicht geladen werden" << std::endl;
9
+		return false;
10
+	}
11
+	
12
+	fontpool[fontname] = tmp;
13
+	return false;
14
+}
15
+
16
+void GLFontEngine::quit() {
17
+	for(std::map<std::string, GLFont*>::iterator iter = fontpool.begin(); iter != fontpool.end(); iter++) {
18
+		delete(iter->second); // unloaded texture.. der dtor
19
+		fontpool.erase(iter);
20
+	}
21
+}
22
+
23
+void GLFontEngine::prepare2DbyPushingMatrix() {
24
+	const SDL_Surface *screen = SDL_GetVideoSurface();
25
+	glMatrixMode(GL_PROJECTION);
26
+	glPushMatrix();
27
+	glLoadIdentity();
28
+	glOrtho(0, screen->w, screen->h, 0, -1, 1);	
29
+// 	glOrtho(0, 640, 480, 0, -1, 1);	
30
+	glMatrixMode(GL_MODELVIEW);
31
+	glPushMatrix();
32
+	glLoadIdentity();
33
+}
34
+
35
+void GLFontEngine::regain3DbyPoppingMatrix() {
36
+	glMatrixMode(GL_PROJECTION);
37
+	glPopMatrix();
38
+	glMatrixMode(GL_MODELVIEW);
39
+	glPopMatrix();
40
+}
41
+
42
+void GLFontEngine::paintSDLRect(SDL_Rect r) {
43
+	glBegin(GL_QUADS);
44
+		glTexCoord2f(0.0f, 1.0f);
45
+		glVertex2i(r.x, r.y);
46
+			
47
+		glTexCoord2f(0.0f, 0.0f);
48
+		glVertex2i(r.x, r.y+r.h);
49
+		
50
+		glTexCoord2f(1.0f, 0.0f);
51
+		glVertex2i(r.x+r.w, r.y+r.h);
52
+		
53
+		glTexCoord2f(1.0f, 1.0f);
54
+		glVertex2i(r.x+r.w, r.y);
55
+		
56
+	glEnd();
57
+}
58
+
59
+GLFontEngine::GLFontEngine() {
60
+	init();
61
+	fontloaded = fontSelect("");
62
+}
63
+
64
+GLFontEngine::GLFontEngine(std::string fontstr) {
65
+	init();
66
+	fontloaded = fontSelect(fontstr);
67
+}
68
+
69
+void GLFontEngine::init() {
70
+// 	r = g = b = a = 1.0f;
71
+	fsize = 16;
72
+}
73
+
74
+void GLFontEngine::setColor(float _r, float _g, float _b, float _a) {
75
+// 	r = _r;
76
+// 	g = _g;
77
+// 	b = _b;
78
+// 	a = _a;
79
+	col.set(_r, _g, _b, _a);
80
+}
81
+
82
+void GLFontEngine::setColor(GLColor c) {
83
+	col = c;
84
+}
85
+
86
+bool GLFontEngine::fontSelect(std::string fontstr) {
87
+	if(fontpool.size()==0) {
88
+		fontloaded = false;
89
+		return false;
90
+	}
91
+	fontloaded = true; // fontstr oder fallbackfont
92
+	if(fontpool[fontstr])
93
+		font = fontpool[fontstr];
94
+	else {
95
+		//fallbackfont - ersten aus der liste
96
+		font = (fontpool.begin())->second;
97
+	}
98
+		
99
+	return true;
100
+}
101
+
102
+void GLFontEngine::renderText(std::string text, SDL_Rect pos) {
103
+	renderLine(text, pos);
104
+}
105
+
106
+void GLFontEngine::renderLine(std::string text, SDL_Rect pos) {
107
+// 	glColor4f(r, g, b, a);
108
+	glColorGLC(col);
109
+	
110
+	if(fontloaded) {
111
+		font->font->selectTexture();
112
+		
113
+		float step = pos.w / (float)text.length();
114
+		float tex_x, tex_y;
115
+		float tex_len = (1.0f/16.0f);
116
+		
117
+		for(unsigned int i=0; i<text.length(); i++) {
118
+			int ch = text[i];
119
+			if(ch<0) {
120
+				ch += 256; // char geht wohl in machen fällen nur von -128 bis +127
121
+			}
122
+// 			std::cout << (int)text[i] << " ";
123
+			tex_x = ((ch-1)%16 / 16.0f);
124
+			tex_y = 1.0f-(floor((ch-1)/16) / 16.0f)-tex_len;
125
+
126
+// 			std::cout << text[i] << " == " <<  tex_x << ", " << tex_y << std::endl;
127
+			glBegin(GL_QUADS);
128
+				glTexCoord2f(tex_x, tex_y+tex_len);
129
+				glVertex2f(pos.x+i*step,			pos.y);
130
+				
131
+				glTexCoord2f(tex_x, tex_y);
132
+				glVertex2f(pos.x+i*step,			pos.y+pos.h);
133
+				
134
+				glTexCoord2f(tex_x+tex_len, tex_y);
135
+				glVertex2f(pos.x+(i+1)*step,	pos.y+pos.h);
136
+				
137
+				glTexCoord2f(tex_x+tex_len, tex_y+tex_len);
138
+				glVertex2f(pos.x+(i+1)*step,	pos.y);
139
+			glEnd();
140
+		}
141
+// 		std::cout << std::endl;
142
+	} else {
143
+		glBegin(GL_QUADS);
144
+			glVertex2f(pos.x, 		pos.y);
145
+			glVertex2f(pos.x+pos.w, pos.y);
146
+			glVertex2f(pos.x+pos.w, pos.y+pos.h);
147
+			glVertex2f(pos.x, 		pos.y+pos.h);
148
+		glEnd();
149
+	}
150
+}
151
+
152
+void GLFontEngine::renderLine(std::string str, int x, int y, bool center, SDL_Rect *rendered_to) {
153
+	SDL_Rect m = { x, y, getTextWidth(str), fsize};
154
+	if(center)
155
+		m.x = m.x - m.w/2;
156
+	
157
+	renderLine(str, m);
158
+	
159
+	if(rendered_to)
160
+		*rendered_to = m;
161
+	
162
+	return;
163
+}
164
+
165
+void GLFontEngine::renderLines(std::string str, int x, int y, bool center, SDL_Rect *rendered_to, int wrap, int paintbackground) {
166
+	if(wrap) {
167
+		// \n einfügen, wenns zu groß ist
168
+		for(unsigned int i=0, a=0; i<str.length(); i++, a++) {
169
+			if(str[i]!='\n') {
170
+				if(a*font->charwidth*fsize>wrap) {
171
+					str.insert(i, "\n");
172
+				}
173
+			} else {
174
+				a=0;
175
+			}
176
+		}
177
+	}
178
+	
179
+	if(paintbackground) {
180
+		std::cout << "Paint Background implementieren.. ;) " << std::endl;
181
+	}
182
+	
183
+	SDL_Rect m;
184
+	int strlpos;
185
+	int max_width = 0;
186
+	int linecount = 0;
187
+	std::string rstr;
188
+	
189
+	while(str!="") {
190
+		
191
+		if((strlpos = str.find('\n'))!=std::string::npos) {
192
+			
193
+			rstr = str.substr(0, strlpos);
194
+			str = str.substr(strlpos+1);
195
+		} else {
196
+			rstr = str;
197
+			str = "";
198
+		}
199
+		renderLine(rstr, x, y+(int)(1.1*linecount*fsize), center, &m);
200
+		
201
+		max_width = std::max(max_width, (int)m.w);
202
+		linecount++;
203
+	}
204
+	
205
+	if(rendered_to) {
206
+		m.w = max_width;
207
+		m.y = y;
208
+		*rendered_to = m;
209
+	}
210
+	
211
+	return;
212
+}
213
+
214
+void GLFontEngine::setSize(int s) {
215
+	fsize = s;
216
+}
217
+
218
+int GLFontEngine::getSize() {
219
+	return fsize;
220
+}
221
+
222
+void GLFontEngine::getSDLRect(const std::string &str, SDL_Rect *r) {
223
+	r->w = getTextWidth(str);
224
+	r->h = getSize();
225
+}
226
+
227
+int GLFontEngine::getTextWidth(const std::string &moep) {
228
+	return (int)(moep.length()*font->charwidth*fsize);
229
+}
230
+
231
+std::map<std::string, GLFont*> GLFontEngine::fontpool;

+ 70
- 0
glfontengine.h View File

@@ -0,0 +1,70 @@
1
+#ifndef __GLFONTENGINE_H
2
+#define __GLFONTENGINE_H
3
+
4
+#include <iostream>
5
+#include <string>
6
+#include <vector>
7
+#include <map>
8
+#include <cmath>
9
+
10
+
11
+#include <SDL.h>
12
+#include <SDL_opengl.h>
13
+#include "gltexture.h"
14
+#include "glcolor.h"
15
+
16
+class GLFont {
17
+	public:
18
+		GLFont(std::string fontfile, float cwdth) {
19
+			charwidth = cwdth;
20
+			font = new GLTexture(fontfile);
21
+		}
22
+		~GLFont() {
23
+			delete(font);
24
+		}
25
+		GLTexture* font;
26
+		float charwidth;
27
+};
28
+
29
+class GLFontEngine {
30
+	private:
31
+		static std::map<std::string, GLFont*> fontpool;
32
+		
33
+		GLFont *font;
34
+		bool fontloaded;
35
+		GLColor col;
36
+// 		float r, g, b, a;
37
+		int fsize;
38
+		
39
+		void init();
40
+	public:
41
+		static bool addFont(std::string fontfile, std::string fontname);
42
+		static void quit();
43
+		
44
+		static void prepare2DbyPushingMatrix();
45
+		static void regain3DbyPoppingMatrix();
46
+		static void paintSDLRect(SDL_Rect);
47
+		
48
+		GLFontEngine();
49
+		GLFontEngine(std::string);
50
+		bool fontSelect(std::string);
51
+		void setColor(float, float, float, float=1.0f);
52
+		void setColor(GLColor);
53
+		
54
+		void renderText(std::string, SDL_Rect); // wrapper
55
+// 		void renderText(std::string, int x, int y, *SDL_Rect=0, bool center=false);
56
+// 		void renderLines(std::string, int x, int y, 
57
+
58
+		void renderLine(std::string, SDL_Rect);
59
+		void renderLine(std::string, int x, int y, bool center=false, SDL_Rect *rendered_to=0);
60
+		void renderLines(std::string, int x, int y, bool center=false, SDL_Rect *rendered_to=0, int wrap=0, int paintbackground=0);
61
+		
62
+		void setSize(int);
63
+		int getSize();
64
+		void getSDLRect(const std::string&, SDL_Rect*);
65
+		int getTextWidth(const std::string&);
66
+		
67
+};
68
+
69
+
70
+#endif

+ 12
- 0
glgui/Makefile View File

@@ -0,0 +1,12 @@
1
+# COMPILER = g++
2
+OBJECTS = button.o object.o textlabel.o window.o
3
+
4
+glguilib: $(OBJECTS)
5
+	rm glgui.a -f
6
+	ar rfc glgui.a $(OBJECTS)
7
+
8
+%.o: %.cpp %.h
9
+	$(COMPILER) -c `sdl-config --cflags` $<
10
+
11
+clean:
12
+	rm -f $(OBJECTS)

+ 18
- 0
glgui/button.cpp View File

@@ -0,0 +1,18 @@
1
+#include "button.h"
2
+
3
+GLGuiButton::GLGuiButton(int _eventid, int _x, int _y, bool _center, int _wrap) : GLGuiTextLabel(_x, _y, _center, _wrap) {
4
+	highlightable = true;
5
+	clickable = true;
6
+	eventid = _eventid;
7
+	highlightcol.set(1.0f, 0.0f, 0.0f, 1.0f);
8
+}
9
+GLGuiButton::GLGuiButton(int _eventid, std::string str, int _x, int _y, bool _center, int _wrap) : GLGuiTextLabel(str, _x, _y, _center, _wrap){
10
+	highlightable = true;
11
+	clickable = true;
12
+	eventid = _eventid;
13
+	highlightcol.set(1.0f, 0.0f, 0.0f, 1.0f);
14
+}
15
+
16
+void GLGuiButton::onMouseOver(int m_x, int m_y) {
17
+	// Längste stelle finden, dann collision
18
+}

+ 28
- 0
glgui/button.h View File

@@ -0,0 +1,28 @@
1
+#ifndef __GLGUIBUTTON_H
2
+#define __GLGUIBUTTON_H
3
+
4
+#include <iostream>
5
+#include <string>
6
+#include <SDL/SDL.h>
7
+#include <SDL/SDL_opengl.h>
8
+
9
+#include "textlabel.h"
10
+
11
+#define GLGUI_BUTTONDOWN 		SDL_USEREVENT+10
12
+
13
+class GLGuiButton : public GLGuiTextLabel {
14
+	protected:
15
+		int eventid;
16
+		GLColor highlightcol;
17
+		
18
+	public:
19
+		GLGuiButton(int _eventid, int _x, int _y, bool _center=false, int _wrap=0);
20
+		GLGuiButton(int _eventid, std::string str, int _x, int _y, bool _center=false, int _wrap=0);
21
+		
22
+		void setHighlightColor();
23
+		
24
+		void onMouseOver(int m_x, int m_y);
25
+		void onMouseClick(int m_x, int m_y);
26
+};
27
+
28
+#endif

+ 7
- 0
glgui/object.cpp View File

@@ -0,0 +1,7 @@
1
+#include "object.h"
2
+
3
+GLGuiObject::GLGuiObject() {
4
+	highlightable = false;
5
+	clickable = false;
6
+	keyboardable = false;
7
+}

+ 29
- 0
glgui/object.h View File

@@ -0,0 +1,29 @@
1
+#ifndef __GLGUIOBJECT_H
2
+#define __GLGUIOBJECT_H
3
+
4
+#include <iostream>
5
+#include <string>
6
+#include <SDL/SDL.h>
7
+#include <SDL/SDL_opengl.h>
8
+
9
+class GLGuiObject {
10
+	protected:
11
+		SDL_Rect pos;
12
+		bool highlightable, clickable, keyboardable;
13
+	public:
14
+		GLGuiObject();
15
+		virtual ~GLGuiObject() { };
16
+	
17
+		virtual void render()=0;
18
+		
19
+		// Optionen
20
+		bool isHighlightable() { return highlightable; };
21
+		bool isClickable() { return clickable; };
22
+		bool isKeyboardable() { return keyboardable; };
23
+		
24
+		virtual void onMouseOver(int m_x, int m_y) { };
25
+		virtual void onMouseClick(int m_x, int m_y, int m_button) { };
26
+		virtual void onKeyboardInput(char c) { };
27
+};
28
+
29
+#endif

+ 37
- 0
glgui/textlabel.cpp View File

@@ -0,0 +1,37 @@
1
+#include "textlabel.h"
2
+
3
+GLGuiTextLabel::GLGuiTextLabel(int _x, int _y, bool _center, int _wrap) {
4
+	setPos(_x, _y);
5
+	center = _center;
6
+	wrap = _wrap;
7
+	col.set(1.0f, 1.0f, 1.0f, 1.0f);
8
+}
9
+
10
+GLGuiTextLabel::GLGuiTextLabel(std::string str, int _x, int _y, bool _center, int _wrap) {
11
+	setPos(_x, _y);
12
+	center = _center;
13
+	wrap = _wrap;
14
+	setText(str);
15
+	col.set(1.0f, 1.0f, 1.0f, 1.0f);
16
+}
17
+
18
+void GLGuiTextLabel::setText(std::string str) {
19
+	text = str;
20
+}
21
+
22
+void GLGuiTextLabel::setColor(GLColor c) {
23
+	col = c;
24
+}
25
+
26
+void GLGuiTextLabel::setPos(int _x, int _y) {
27
+	x = _x;
28
+	y = _y;
29
+}
30
+
31
+void GLGuiTextLabel::render() {
32
+	glColorGLC(col);
33
+	
34
+	GLFontEngine::prepare2DbyPushingMatrix();
35
+		fontengine.renderLines(text, x, y, center, 0, wrap, 0);
36
+	GLFontEngine::regain3DbyPoppingMatrix();
37
+}

+ 32
- 0
glgui/textlabel.h View File

@@ -0,0 +1,32 @@
1
+#ifndef __GLGUITEXTLABEL_H
2
+#define __GLGUITEXTLABEL_H
3
+
4
+#include <iostream>
5
+#include <string>
6
+#include <SDL/SDL.h>
7
+#include <SDL/SDL_opengl.h>
8
+
9
+#include "object.h"
10
+#include "../glcolor.h"
11
+#include "../glfontengine.h"
12
+
13
+class GLGuiTextLabel : public GLGuiObject{
14
+	protected:
15
+		GLFontEngine fontengine;
16
+		std::string text;
17
+		
18
+		GLColor col;
19
+		int x, y;
20
+		bool center;
21
+		int wrap;
22
+	public:
23
+		GLGuiTextLabel(int _x, int _y, bool _center=false, int _wrap=0);
24
+		GLGuiTextLabel(std::string, int _x, int _y, bool _center=false, int _wrap=0);
25
+
26
+		void setText(std::string str);
27
+		void setColor(GLColor);
28
+		void setPos(int _x, int _y);
29
+		virtual void render();
30
+};
31
+
32
+#endif

+ 39
- 0
glgui/window.cpp View File

@@ -0,0 +1,39 @@
1
+#include "window.h"
2
+
3
+GLGuiWindow::GLGuiWindow(SDL_Rect _pos, GLColor _bgcolor) {
4
+	pos = _pos;
5
+	bgcolor = _bgcolor;
6
+}
7
+
8
+void GLGuiWindow::addItem(GLGuiObject *gobj) {
9
+	// Objekt wird in delete gelöscht
10
+	items.push_back(gobj);
11
+	if(gobj->isHighlightable())
12
+		highlightable.push_back(gobj);
13
+	if(gobj->isClickable())
14
+		clickable.push_back(gobj);
15
+	if(gobj->isKeyboardable())
16
+		keyboardable.push_back(gobj);
17
+	
18
+}
19
+
20
+void GLGuiWindow::render() {
21
+	if(renderbackground) {
22
+		glColorGLC(bgcolor);
23
+		GLDrawSDLRect(&pos);
24
+	}
25
+	
26
+	for(unsigned int i=0; i<items.size(); i++) {
27
+		items[i]->render();
28
+	}
29
+}
30
+
31
+SDL_Rect GLGuiWindow::getPos() {
32
+	return pos;
33
+}
34
+
35
+GLGuiWindow::~GLGuiWindow() {
36
+	for(unsigned int i=0; i<items.size(); i++) {
37
+		delete(items[i]);
38
+	}
39
+}

+ 36
- 0
glgui/window.h View File

@@ -0,0 +1,36 @@
1
+#ifndef __GLGUIWINDOW_H
2
+#define __GLGUIWINDOW_H
3
+
4
+#include <iostream>
5
+#include <string>
6
+#include <vector>
7
+#include <SDL/SDL.h>
8
+#include <SDL/SDL_opengl.h>
9
+
10
+#include "../glcolor.h"
11
+#include "../gldrawhelper.h"
12
+#include "object.h"
13
+
14
+
15
+class GLGuiWindow : public GLGuiObject {
16
+	private:
17
+		bool renderbackground;
18
+		SDL_Rect pos;
19
+		GLColor bgcolor;
20
+		
21
+		std::vector<GLGuiObject*> items;
22
+		
23
+		std::vector<GLGuiObject*> highlightable;
24
+		std::vector<GLGuiObject*> clickable;
25
+		std::vector<GLGuiObject*> keyboardable;
26
+	public:
27
+		GLGuiWindow(SDL_Rect _pos, GLColor _bgcolor);
28
+		~GLGuiWindow();
29
+		
30
+		void addItem(GLGuiObject*);
31
+		SDL_Rect getPos();
32
+		void render();
33
+
34
+};
35
+
36
+#endif

+ 35
- 0
glrect.cpp View File

@@ -0,0 +1,35 @@
1
+#include "glrect.h"
2
+
3
+GLRect::GLRect() {
4
+	set(0.0f, 0.0f, 0.0f, 0.0f);
5
+}
6
+
7
+GLRect::GLRect(float _x, float _y, float _w, float _h) {
8
+	set(_x, _y, _w, _h);
9
+}
10
+
11
+GLRect::GLRect(const SDL_Rect &r) {
12
+	set(r.x, r.y, r.w, r.h);
13
+}
14
+
15
+void GLRect::set(float _x, float _y, float _w, float _h) {
16
+	x = _x;
17
+	y = _y;
18
+	w = _w;
19
+	h = _h;
20
+}
21
+
22
+void GLRect::setPoint(float _x, float _y) {
23
+	x = _x;
24
+	y = _y;
25
+}
26
+
27
+void GLRect::setDim(float _w, float _h) {
28
+	w = _w;
29
+	h = _h;
30
+}
31
+
32
+SDL_Rect GLRect::getSDLRect() {
33
+	SDL_Rect tmp = {(Uint8)x, (Uint8)y, (Uint8)w, (Uint8)h};
34
+	return tmp;
35
+}

+ 22
- 0
glrect.h View File

@@ -0,0 +1,22 @@
1
+#ifndef __GLRECT_H
2
+#define __GLRECT_H
3
+
4
+#include <SDL.h>
5
+
6
+class GLRect {
7
+	public:
8
+		float x, y;
9
+		float w, h;
10
+		
11
+		GLRect();
12
+		GLRect(float, float, float, float);
13
+		GLRect(const SDL_Rect&);
14
+		
15
+		void set(float, float, float, float);
16
+		void setPoint(float, float);
17
+		void setDim(float, float);
18
+		SDL_Rect getSDLRect();
19
+	
20
+};
21
+
22
+#endif

+ 172
- 0
gltexture.cpp View File

@@ -0,0 +1,172 @@
1
+#include "gltexture.h"
2
+
3
+GLTexture::GLTexture() {
4
+	init();
5
+	alltextures.push_back(this);
6
+}
7
+
8
+GLTexture::GLTexture(std::string fname, GLint _minfilter, GLint _magfilter, GLint _wraps, GLint _wrapt) {
9
+	init();
10
+	setParameter(_minfilter, _magfilter, _wraps, _wrapt);
11
+	loadImage(fname);
12
+	
13
+	alltextures.push_back(this);
14
+}
15
+
16
+GLTexture::GLTexture(SDL_Surface *srfc) {
17
+	init();
18
+	loadSurface(srfc);
19
+}
20
+
21
+void GLTexture::init() {
22
+	loaded = false;
23
+	texconverted = false;
24
+	keepsurface = false;
25
+	donotreload = false;
26
+	
27
+	minfilter = GL_LINEAR;
28
+	magfilter = GL_LINEAR;
29
+	
30
+	wraps = GL_REPEAT;
31
+	wrapt = GL_REPEAT;
32
+	
33
+	tex = 0;
34
+	width = height = 0;
35
+}
36
+
37
+bool GLTexture::loadLocalSurface() {
38
+	if(!texconverted)
39
+		convertLocalSurface();
40
+	
41
+	width  = tex->w;
42
+	height = tex->h;
43
+	
44
+	glGenTextures(1, &texint);
45
+	glBindTexture(GL_TEXTURE_2D, texint);
46
+	
47
+	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, minfilter);
48
+	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, magfilter);
49
+	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wraps);
50
+	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapt);
51
+
52
+	gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA8, width, height, GL_BGRA, GL_UNSIGNED_BYTE, tex->pixels);
53
+	
54
+	loaded = true;
55
+	
56
+	return true;
57
+}
58
+
59
+void GLTexture::convertLocalSurface() {
60
+	tex = SDL_DisplayFormatAlpha(tex);
61
+	mirrorSurfaceMiddleX(tex);
62
+	
63
+	texconverted = true;
64
+}
65
+
66
+bool GLTexture::loadImage(std::string fname) {
67
+	if(isLoaded())
68
+		unloadTexture();
69
+	filename = fname;
70
+	tex = IMG_Load(filename.c_str());
71
+	
72
+	if(!tex) {
73
+		std::cerr << "Textur konnte nicht geladen werden: " << filename << std::endl;
74
+		return false;
75
+	}
76
+	
77
+	bool ret = loadLocalSurface();
78
+	keepsurface = false;
79
+	SDL_FreeSurface(tex);
80
+	tex = 0;
81
+	
82
+	return ret;
83
+}
84
+
85
+bool GLTexture::loadSurface(SDL_Surface *srfc, bool noreloading) {
86
+	if(isLoaded())
87
+		unloadTexture();
88
+	if(filename!="")
89
+		filename = "";
90
+	
91
+	tex = srfc;
92
+	
93
+	keepsurface = true;
94
+	donotreload = noreloading;
95
+	
96
+	return loadLocalSurface();
97
+}
98
+
99
+bool GLTexture::selectTexture() {
100
+	if(!loaded) {
101
+		std::cerr << "Textur wurde nicht geladen: " << filename << std::endl;
102
+		return false;
103
+	}
104
+	
105
+	glBindTexture(GL_TEXTURE_2D, texint);
106
+	
107
+	return true;
108
+}
109
+
110
+void GLTexture::unloadTexture() {
111
+	glDeleteTextures(1, &texint);
112
+	
113
+	loaded = false;
114
+	// Entweder tex == image, wurde schon gelöscht oder wir müssen es nich löschen
115
+
116
+}
117
+
118
+bool GLTexture::setParameter(GLint _minfilter, GLint _magfilter, GLint _wraps, GLint _wrapt) {
119
+	if(_minfilter!=0)
120
+		minfilter = _minfilter;
121
+	if(_magfilter!=0)
122
+		magfilter = _magfilter;
123
+	if(_wraps!=0)
124
+		wraps = _wraps;
125
+	if(_wrapt!=0)
126
+		wrapt = _wrapt;
127
+	
128
+	if(!loaded)
129
+		return true;
130
+	
131
+	if(keepsurface&&tex!=0) {
132
+		unloadTexture();
133
+		return loadLocalSurface();
134
+	} else if(filename!="") {
135
+		return loadImage("filename");
136
+	} else {
137
+		// keine datei, kein surface
138
+		unloadTexture();
139
+		std::cerr << "Couldn't reload GLTexture " << this << "- No surface, no file!" << std::endl;
140
+		return false;
141
+	}
142
+}
143
+
144
+int GLTexture::getW() {
145
+	return width;	
146
+}
147
+
148
+int GLTexture::getH() {
149
+	return height;	
150
+}
151
+
152
+bool GLTexture::isLoaded() {
153
+	return loaded;
154
+}
155
+
156
+void GLTexture::reloadAll() {
157
+	for(unsigned int i=0; i<alltextures.size(); i++)
158
+		alltextures[i]->setParameter();
159
+}
160
+
161
+GLTexture::~GLTexture() {
162
+	if(isLoaded())
163
+		unloadTexture();
164
+		
165
+	for(unsigned int i=0; i<alltextures.size(); i++) {
166
+		if(alltextures[i]==this) {
167
+			alltextures.erase(alltextures.begin()+i);
168
+		}
169
+	}
170
+}
171
+
172
+std::vector<GLTexture*> GLTexture::alltextures;

+ 63
- 0
gltexture.h View File

@@ -0,0 +1,63 @@
1
+#ifndef __GLTEXTUR_H
2
+#define __GLTEXTUR_H
3
+
4
+#include <iostream>
5
+#include <string>
6
+#include <vector>
7
+#include <SDL.h>
8
+#include <SDL_image.h>
9
+#include <SDL_opengl.h>
10
+#include "sdlfuncs.h"
11
+
12
+class GLTexture {
13
+	private:
14
+		GLuint texint;
15
+		SDL_Surface *tex;
16
+		int width, height;
17
+		
18
+		std::string filename; // wenn von datei geladen
19
+		
20
+		// Parameter
21
+		GLint minfilter;
22
+		GLint magfilter;
23
+		GLint wraps;
24
+		GLint wrapt;
25
+		
26
+		bool loaded;
27
+		bool texconverted;
28
+		bool keepsurface;
29
+		bool donotreload;
30
+		
31
+		void init();
32
+		bool loadLocalSurface();
33
+		void convertLocalSurface();
34
+		
35
+		static std::vector<GLTexture*> alltextures;
36
+	protected:
37
+		GLTexture(SDL_Surface*);
38
+		bool loadSurface(SDL_Surface*, bool = false);
39
+	public:
40
+		static void reloadAll();
41
+	
42
+		GLTexture();
43
+		GLTexture(std::string, GLint=GL_LINEAR, GLint=GL_LINEAR, GLint=GL_REPEAT, GLint=GL_REPEAT);
44
+		~GLTexture();
45
+
46
+		//load
47
+		
48
+		bool loadImage(std::string);
49
+		
50
+		bool selectTexture();
51
+		void unloadTexture();
52
+		bool setParameter(GLint=0, GLint=0, GLint=0, GLint=0);
53
+		inline bool select() { return selectTexture(); }
54
+		
55
+		int getW();
56
+		int getH();
57
+		
58
+
59
+		
60
+		bool isLoaded();
61
+};
62
+
63
+#endif

+ 177
- 0
main.cpp View File

@@ -0,0 +1,177 @@
1
+#define VERSION "0.2.2 DEV"
2
+
3
+#include <iostream>
4
+#include <string>
5
+
6
+#include <SDL.h>
7
+#include <SDL_opengl.h>
8
+
9
+#include "gltexture.h"
10
+#include "glfontengine.h"
11
+
12
+// #define GL_DEBUG_MOVEMENT false
13
+
14
+void setupGL();
15
+void resizeOpenGLWindow(int, int, float=1.0f, float=100.0f);
16
+
17
+int main(int argc, char **argv) {
18
+	std::cout << "ARGV: " << argv[0] << std::endl;
19
+	//Vars
20
+	SDL_Surface *screen;
21
+	bool quit = false;
22
+	SDL_Event event;
23
+	int w_width  = 640;
24
+	int w_height = 480;
25
+	int w_bbp    = 32;
26
+	float znear = 1.0f;
27
+	float zfar  = 100.0f;
28
+
29
+	if(SDL_Init(SDL_INIT_VIDEO)<0) {
30
+		std::cerr << "Konnte SDL nicht initialisieren" << SDL_GetError() << std::endl;
31
+		return 1;
32
+	}
33
+		
34
+	// Setting video mode
35
+	int videoFlags;
36
+	const SDL_VideoInfo *videoInfo;
37
+
38
+	videoInfo = SDL_GetVideoInfo();
39
+	
40
+	if(!videoInfo) {
41
+		std::cerr << "Video query failed: " << SDL_GetError() << std::endl;
42
+		return 1;
43
+	}
44
+	
45
+	videoFlags  = SDL_OPENGL;
46
+	videoFlags |= SDL_GL_DOUBLEBUFFER;
47
+	videoFlags |= SDL_HWPALETTE;
48
+	videoFlags |= SDL_RESIZABLE;
49
+	if(videoInfo->hw_available)
50
+		videoFlags |= SDL_HWSURFACE;
51
+	else
52
+		videoFlags |= SDL_SWSURFACE;
53
+	if(videoInfo->blit_hw)
54
+		videoFlags |= SDL_HWACCEL;
55
+		
56
+// 	videoFlags |=SDL_FULLSCREEN;
57
+	
58
+// 	SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 5 );
59
+// 	SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 5 );
60
+// 	SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 5 );
61
+// 	SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 16 );
62
+	SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
63
+	
64
+	if(!(screen = SDL_SetVideoMode(w_width, w_height, w_bbp, videoFlags))) {
65
+		std::cerr << "Kein SDL-Screen verfügbar: " << SDL_GetError() << std::endl;
66
+		return 1;
67
+	}
68
+	SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
69
+	
70
+	SDL_WM_SetCaption("GLGui Test, 0);
71
+	
72
+	// blank screen
73
+// 	SDL_FillRect(screen, 0, SDL_MapRGBA(screen->format, 0, 0, 0, 0));
74
+	
75
+	// GL Optionen
76
+	setupGL();
77
+		
78
+	resizeOpenGLWindow(w_width, w_height, znear, zfar);
79
+	
80
+	// Keyrepeat, falls benötigt auskommentieren
81
+	SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
82
+	
83
+	// Fontengine initliaisieren, für alle die Fonts brauchen
84
+	GLFontEngine::addFont("font_big_better_alpha.png", "default");
85
+	
86
+	// Programmvariablen
87
+	Snake3D snake3d;
88
+	
89
+	//movement & keyboardcontrol
90
+	Uint8 *keys = SDL_GetKeyState(NULL);;
91
+	SDLKey tkey;
92
+	typedef enum { move_up=0, move_down, move_left, move_right} nummovekeys;
93
+	SDLKey movekeys[4];
94
+	
95
+	bool pause = true;
96
+	bool blend = false;
97
+	bool extralines = false;
98
+	bool fullscreen = false;
99
+	
100
+	// splash screen
101
+	bool splashscreen = true;
102
+	snake3d.pause(true);
103
+	SDL_Rect splashpos = { 40, 40, screen->w -80, 310 };
104
+	GLFontEngine fontengine("default");
105
+	
106
+#ifdef GL_DEBUG_MOVEMENT
107
+	movekeys[move_up]    = SDLK_i;
108
+	movekeys[move_down]  = SDLK_k;
109
+	movekeys[move_left]  = SDLK_j;
110
+	movekeys[move_right] = SDLK_l;
111
+#else
112
+	movekeys[move_up]    = SDLK_UP;
113
+	movekeys[move_down]  = SDLK_DOWN;
114
+	movekeys[move_left]  = SDLK_LEFT;
115
+	movekeys[move_right] = SDLK_RIGHT;
116
+#endif
117
+	
118
+	while(!quit) {
119
+		if(SDL_PollEvent(&event)) {
120
+			switch(event.type) {
121
+				case SDL_QUIT:
122
+					quit = true;
123
+				break;
124
+				case SDL_KEYDOWN:
125
+					keys = SDL_GetKeyState(NULL);
126
+					tkey = event.key.keysym.sym;
127
+
128
+				break;
129
+				case SDL_KEYUP:
130
+					keys = SDL_GetKeyState(NULL);
131
+				break;
132
+			}
133
+		} else {
134
+		
135
+			glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
136
+			glLoadIdentity();
137
+
138
+			SDL_Delay(10);
139
+		}
140
+		
141
+	}
142
+	GLFontEngine::quit();
143
+	SDL_Quit();
144
+	
145
+	return 0;
146
+}
147
+
148
+void setupGL() {
149
+	glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
150
+	glClearDepth(1.0f);
151
+	glDepthFunc(GL_LEQUAL);
152
+	glEnable(GL_DEPTH_TEST);
153
+	glEnable(GL_TEXTURE_2D);
154
+	glShadeModel(GL_SMOOTH);
155
+	glBlendFunc(GL_SRC_ALPHA, GL_ONE);
156
+// 	glBlendFunc(GL_SRC_COLOR,GL_DST_COLOR);
157
+	
158
+// 	glBlendFunc(GL_DST_COLOR, GL_SRC_ALPHA);
159
+// 	glBlendFunc(GL_ONE_MINUS_SRC_ALPHA, GL_SRC_ALPHA);
160
+	glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
161
+	glEnable(GL_ALPHA_TEST);
162
+	glAlphaFunc(GL_GREATER, 0.1);
163
+}
164
+
165
+void resizeOpenGLWindow(int width, int height, float znear, float zfar) {
166
+	if(height==0)
167
+		height = 1;
168
+
169
+	glViewport(0, 0, (GLsizei)width, (GLsizei)height);
170
+	
171
+	glMatrixMode(GL_PROJECTION);
172
+	glLoadIdentity();
173
+	gluPerspective(45.0f, (GLfloat)width/(GLfloat)height, znear, zfar);
174
+	
175
+	glMatrixMode(GL_MODELVIEW);
176
+	glLoadIdentity();
177
+}

+ 149
- 0
matrix.cpp View File

@@ -0,0 +1,149 @@
1
+#include "matrix.h"
2
+
3
+Matrix::Matrix(int _m, int _n) {
4
+	m = _m;
5
+	n = _n;
6
+	allocate();
7
+}
8
+
9
+Matrix::Matrix(int _m, int _n, const float **src) {
10
+	m = _m;
11
+	n = _n;
12
+	allocate();
13
+	copyFromSource(src);
14
+}
15
+
16
+Matrix::Matrix(Punkt3D d) {
17
+	m = 3;
18
+	n = 1;
19
+	allocate();
20
+	c[0][0] = d.x;
21
+	c[1][0] = d.y;
22
+	c[2][0] = d.z;
23
+}
24
+
25
+Matrix::Matrix(const Matrix& mat) {
26
+	m = mat.m;
27
+	n = mat.n;
28
+	allocate();
29
+	copyFromSource(mat.getMatrix());
30
+}
31
+
32
+void Matrix::allocate() {
33
+	c = new float*[m];
34
+	for(int i=0; i<m; i++) {
35
+		c[i] = new float[n];
36
+		for(int t=0; t<n; t++)
37
+			c[i][t] = 0;
38
+	}
39
+}
40
+
41
+void Matrix::copyFromSource(const float** src) {
42
+	for(int i=0; i<m; i++) {
43
+		for(int t=0; t<n; t++)
44
+			c[i][t] = src[i][t];
45
+	}
46
+}
47
+
48
+const float **Matrix::getMatrix() const {
49
+	return (const float**)c;
50
+}
51
+
52
+bool Matrix::set(float d, int _m, int _n) {
53
+	if(_m>=m||_n>=n)
54
+		return false;
55
+	c[_m][_n] = d;
56
+	return true;
57
+}
58
+
59
+Matrix Matrix::operator*(const Matrix &d) {
60
+	
61
+	if(n!=d.m)
62
+		return Matrix(1,1);
63
+	Matrix erg(m, d.n);
64
+	
65
+	
66
+	for(int a=0; a<m; a++) {
67
+		for(int b=0; b<d.n; b++) {
68
+			float zerg = 0.0f;
69
+			for(int _c=0; _c<n; _c++) {
70
+				zerg += c[a][_c] * d.c[_c][b];
71
+			}
72
+			erg.c[a][b] = zerg;
73
+		}
74
+	}
75
+
76
+	return erg;
77
+}
78
+
79
+Matrix& Matrix::operator=(const Matrix& mat) {
80
+	if(mat.m<=m&&mat.n<=n) {
81
+		copyFromSource(mat.getMatrix());
82
+	}
83
+	return *this;
84
+}
85
+
86
+// Matrix Matrix::operator+(const float &f) {
87
+// 	Matrix tmp(*this);
88
+// 	for(int i=0; i<m; i++) {
89
+// 		for(int j=0; j<n; j++) {
90
+// 			tmp.c[i][j] += f;
91
+// 		}
92
+// 	}
93
+// 	return tmp;
94
+// }
95
+// 
96
+// Matrix Matrix::operator-(const float &f) {
97
+// 	Matrix tmp(*this);
98
+// 	for(int i=0; i<m; i++) {
99
+// 		for(int j=0; j<n; j++) {
100
+// 			tmp.c[i][j] -= f;
101
+// 		}
102
+// 	}
103
+// 	return tmp;
104
+// }
105
+// 
106
+// Matrix Matrix::operator*(const float &f) {
107
+// 	Matrix tmp(*this);
108
+// 	for(int i=0; i<m; i++) {
109
+// 		for(int j=0; j<n; j++) {
110
+// 			tmp.c[i][j] *= f;
111
+// 		}
112
+// 	}
113
+// 	return tmp;
114
+// }
115
+// 
116
+// Matrix Matrix::operator/(const float &f) {
117
+// 	Matrix tmp(*this);
118
+// 	for(int i=0; i<m; i++) {
119
+// 		for(int j=0; j<n; j++) {
120
+// 			tmp.c[i][j] /= f;
121
+// 		}
122
+// 	}
123
+// 	return tmp;
124
+// }
125
+
126
+int Matrix::getM() const {
127
+	return m;
128
+}
129
+
130
+int Matrix::getN() const {
131
+	return n;
132
+}
133
+
134
+void Matrix::print(std::string s) const {
135
+	std::cout << "Matrix " << s << "(" << m << "," << n << ")" << std::endl;
136
+	for(int a=0; a<m; a++) {
137
+		for(int b=0; b<n; b++) {
138
+			std::cout << std::setw(10) << c[a][b];
139
+		}
140
+		std::cout << std::endl;
141
+	}
142
+}
143
+
144
+Matrix::~Matrix() {
145
+	for(int i=0; i<m; i++) {
146
+		delete[](c[i]);
147
+	}
148
+	delete[](c);
149
+}

+ 42
- 0
matrix.h View File

@@ -0,0 +1,42 @@
1
+#ifndef __MATRIX_H
2
+#define __MATRIX_H
3
+
4
+#include <iostream>
5
+#include <iomanip>
6
+
7
+#include "emath.h"
8
+
9
+class Matrix {
10
+	protected:
11
+		float **c;
12
+		int m, n;
13
+	
14
+		void allocate();
15
+		void copyFromSource(const float**);
16
+	public:
17
+		Matrix(int, int);
18
+		Matrix(int, int, const float**);
19
+		Matrix(Punkt3D);
20
+		Matrix(const Matrix&);
21
+		~Matrix();
22
+		
23
+		const float **getMatrix() const;
24
+		bool set(float, int, int);
25
+		int getM() const;
26
+		int getN() const;
27
+		
28
+		Matrix operator*(const Matrix&);
29
+		Matrix& operator=(const Matrix&);
30
+		
31
+// 		Matrix operator+(const float&);
32
+// 		Matrix operator-(const float&);
33
+// 		Matrix operator*(const float&);
34
+// 		Matrix operator/(const float&);
35
+
36
+		void print(std::string="") const;
37
+
38
+};
39
+
40
+
41
+
42
+#endif

+ 77
- 0
quaternion.cpp View File

@@ -0,0 +1,77 @@
1
+#include "quaternion.h"
2
+
3
+Quaternion::Quaternion() {
4
+	w = 1.0f;
5
+	x = y = z = 0.0f;
6
+}
7
+
8
+void Quaternion::createFromRotation(float deg, float _x, float _y, float _z) {
9
+	float tmpres = sin(deg2rad(deg) / 2.0f);
10
+	
11
+	w = cos(deg2rad(deg) / 2.0f);
12
+	x = _x * tmpres;
13
+	y = _y * tmpres;
14
+	z = _z * tmpres;
15
+}
16
+
17
+Quaternion Quaternion::operator*(const Quaternion &q) {
18
+	Quaternion ret;
19
+
20
+	ret.w = w*q.w - x*q.x - y*q.y - z*q.z;
21
+	ret.x = w*q.x + x*q.w + y*q.z - z*q.y;
22
+	ret.y = w*q.y + y*q.w + z*q.x - x*q.z;
23
+	ret.z = w*q.z + z*q.w + x*q.y - y*q.x;
24
+
25
+	return ret;
26
+}
27
+
28
+void Quaternion::createGlMatrix(GLfloat *matrix) {
29
+	if(!matrix)
30
+		return;
31
+
32
+	matrix[0]  = 1.0f - 2.0f * ( y*y + z*z);
33
+	matrix[1]  = 2.0f * ( x*y + z*w);
34
+	matrix[2]  = 2.0f * ( x*z - y*w);
35
+	matrix[3]  = 0.0f;
36
+
37
+	matrix[4]  = 2.0f * ( x*y - z*w);
38
+	matrix[5]  = 1.0f - 2.0f * ( x*x + z*z);
39
+	matrix[6]  = 2.0f * ( z*y + x*w);
40
+	matrix[7]  = 0.0f;
41
+
42
+	matrix[8]  = 2.0f * ( x*z + y*w);
43
+	matrix[9]  = 2.0f * ( y*z - x*w);
44
+	matrix[10] = 1.0f - 2.0f * ( x*x + y*y);
45
+	matrix[11] = 0.0f;
46
+
47
+	matrix[12] = 0.0f;
48
+	matrix[13] = 0.0f;
49
+	matrix[14] = 0.0f;
50
+	matrix[15] = 1.0f;
51
+	
52
+}
53
+
54
+Punkt3D Quaternion::getDirectionVector() {
55
+	GLfloat matrix[16];
56
+	createGlMatrix(matrix);
57
+	
58
+	return getDirectionVector(matrix);
59
+}
60
+
61
+Punkt3D Quaternion::getDirectionVector(GLfloat *matrix) {
62
+	if(!matrix)
63
+		return Punkt3D();
64
+	Punkt3D tmp;
65
+	
66
+	tmp.x = matrix[8];
67
+	tmp.y = matrix[9];
68
+	tmp.z = matrix[10];
69
+	
70
+	return tmp;
71
+}
72
+
73
+void glMultMatrixf(Quaternion q) {
74
+	GLfloat matrix[16];
75
+	q.createGlMatrix(matrix);
76
+	glMultMatrixf(matrix);
77
+}

+ 30
- 0
quaternion.h View File

@@ -0,0 +1,30 @@
1
+#ifndef __QUATERNION_H
2
+#define __QUATERNION_H
3
+
4
+#include <iostream>
5
+#include <cmath>
6
+#include <SDL_opengl.h>
7
+
8
+#include "emath.h"
9
+
10
+class Quaternion {
11
+	private:
12
+		float w;
13
+		float x,y,z;
14
+	public:
15
+		Quaternion();
16
+		void createFromRotation(float, float, float, float);
17
+		
18
+
19
+		void createGlMatrix(GLfloat*);
20
+		Punkt3D getDirectionVector();
21
+		Punkt3D getDirectionVector(GLfloat*);
22
+		
23
+		Quaternion operator*(const Quaternion&);
24
+		
25
+
26
+};
27
+
28
+void glMultMatrixf(Quaternion);
29
+
30
+#endif

+ 91
- 0
rotationsmatrix.cpp View File

@@ -0,0 +1,91 @@
1
+#include "rotationsmatrix.h"
2
+
3
+Rotationsmatrix::Rotationsmatrix() : Matrix(3,3) {
4
+	set(x_axis, 0.0f);
5
+}
6
+
7
+Rotationsmatrix::Rotationsmatrix(axis _ax, float _deg) : Matrix(3,3) {
8
+	set(_ax, _deg);
9
+}
10
+
11
+Rotationsmatrix::Rotationsmatrix(Punkt3D _rotvec, float _deg) : Matrix(3,3) {
12
+	set(_rotvec, _deg);	
13
+}
14
+
15
+void Rotationsmatrix::set(Punkt3D _rotvec, float _deg) {
16
+	rotvec = _rotvec;
17
+	rotvec.normalize();
18
+	deg = _deg;
19
+	initRot();
20
+}
21
+
22
+void Rotationsmatrix::set(axis _ax, float _deg) {
23
+	deg = _deg;
24
+	
25
+	switch(_ax) {
26
+		case x_axis:
27
+			rotvec.set(1.0f, 0.0f, 0.0f);
28
+		break;
29
+		case y_axis:
30
+			rotvec.set(0.0f, 1.0f, 0.0f);
31
+		break;
32
+		case z_axis:
33
+			rotvec.set(0.0f, 0.0f, 1.0f);
34
+		break;	
35
+	}
36
+	initRot();
37
+}
38
+
39
+void Rotationsmatrix::set(float _deg) {
40
+	deg = _deg;
41
+	initRot();
42
+}
43
+
44
+void Rotationsmatrix::initRot() {
45
+	c[0][0] = scos(deg) + pow(rotvec.x,2.0f) * (1 - scos(deg));
46
+	c[0][1] = rotvec.x * rotvec.y * (1 - scos(deg)) - rotvec.z * ssin(deg);
47
+	c[0][2] = rotvec.x * rotvec.z * (1 - scos(deg)) + rotvec.y * ssin(deg);
48
+
49
+	c[1][0] = rotvec.y * rotvec.x * (1 - scos(deg)) + rotvec.z * ssin(deg);
50
+	c[1][1] = scos(deg) + pow(rotvec.y,2.0f) * (1 - scos(deg));
51
+	c[1][2] = rotvec.y * rotvec.z * (1 - scos(deg)) - rotvec.x * ssin(deg);
52
+
53
+	c[2][0] = rotvec.z * rotvec.x * (1 - scos(deg)) - rotvec.y * ssin(deg);
54
+	c[2][1] = rotvec.z * rotvec.y * (1 - scos(deg)) + rotvec.x * ssin(deg);
55
+	c[2][2] = scos(deg) + pow(rotvec.z,2.0f) * (1 - scos(deg));
56
+}
57
+
58
+Punkt3D Rotationsmatrix::operator*(const Punkt3D &p) {
59
+	Matrix erg(3,1);
60
+	Punkt3D erg2;
61
+
62
+	erg =  (((Matrix*)(this))->operator*(Matrix(p)));
63
+
64
+	const float **mat = erg.getMatrix();
65
+	erg2.set(mat[0][0], mat[1][0], mat[2][0]);
66
+
67
+	return erg2;
68
+}
69
+
70
+Rotationsmatrix Rotationsmatrix::operator*(const Rotationsmatrix &p) {
71
+	Matrix m(3, 3);
72
+	Rotationsmatrix rotmat;
73
+	m = ((Matrix)(*this) * (Matrix)p);
74
+	const float **mat = m.getMatrix();
75
+	rotmat.c[0][0] = mat[0][0];
76
+	rotmat.c[0][1] = mat[0][1];
77
+	rotmat.c[0][2] = mat[0][2];
78
+	rotmat.c[1][0] = mat[1][0];
79
+	rotmat.c[1][1] = mat[1][1];
80
+	rotmat.c[1][2] = mat[1][2];
81
+	rotmat.c[2][0] = mat[2][0];
82
+	rotmat.c[2][1] = mat[2][1];
83
+	rotmat.c[2][2] = mat[2][2];
84
+
85
+	return rotmat;
86
+}
87
+
88
+
89
+Rotationsmatrix::~Rotationsmatrix() {
90
+
91
+}

+ 30
- 0
rotationsmatrix.h View File

@@ -0,0 +1,30 @@
1
+#ifndef __ROTATIONSMATRIX_H
2
+#define __ROTATIONSMATRIX_H
3
+
4
+#include <cmath>
5
+#include "emath.h"
6
+#include "matrix.h"
7
+
8
+enum axis { x_axis=0, y_axis, z_axis };
9
+
10
+class Rotationsmatrix : public Matrix {
11
+	private:
12
+		float deg;
13
+		Punkt3D rotvec;
14
+		
15
+		void initRot();
16
+	public:
17
+		Rotationsmatrix();
18
+		Rotationsmatrix(axis, float);
19
+		Rotationsmatrix(Punkt3D, float);
20
+		~Rotationsmatrix();
21
+		
22
+		void set(float);
23
+		void set(axis, float);
24
+		void set(Punkt3D, float);
25
+		
26
+		Punkt3D operator*(const Punkt3D&);
27
+		Rotationsmatrix operator*(const Rotationsmatrix&);
28
+};
29
+
30
+#endif

+ 82
- 0
sdlfuncs.cpp View File

@@ -0,0 +1,82 @@
1
+#include "sdlfuncs.h"
2
+
3
+Uint32 getPixel(SDL_Surface *surface, int x, int y)
4
+{
5
+    int bpp = surface->format->BytesPerPixel;
6
+    /* Here p is the address to the pixel we want to retrieve */
7
+    Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;
8
+
9
+    switch(bpp) {
10
+    case 1:
11
+        return *p;
12
+
13
+    case 2:
14
+        return *(Uint16 *)p;
15
+
16
+    case 3:
17
+        if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
18
+            return p[0] << 16 | p[1] << 8 | p[2];
19
+        else
20
+            return p[0] | p[1] << 8 | p[2] << 16;
21
+
22
+    case 4:
23
+        return *(Uint32 *)p;
24
+
25
+    default:
26
+        return 0;       /* shouldn't happen, but avoids warnings */
27
+    }
28
+}
29
+
30
+void setPixel(SDL_Surface *surface, int x, int y, Uint32 pixel)
31
+{
32
+    int bpp = surface->format->BytesPerPixel;
33
+    /* Here p is the address to the pixel we want to set */
34
+    Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;
35
+
36
+    switch(bpp) {
37
+    case 1:
38
+        *p = pixel;
39
+        break;
40
+
41
+    case 2:
42
+        *(Uint16 *)p = pixel;
43
+        break;
44
+
45
+    case 3:
46
+        if(SDL_BYTEORDER == SDL_BIG_ENDIAN) {
47
+            p[0] = (pixel >> 16) & 0xff;
48
+            p[1] = (pixel >> 8) & 0xff;
49
+            p[2] = pixel & 0xff;
50
+        } else {
51
+            p[0] = pixel & 0xff;
52
+            p[1] = (pixel >> 8) & 0xff;
53
+            p[2] = (pixel >> 16) & 0xff;
54
+        }
55
+        break;
56
+
57
+    case 4:
58
+        *(Uint32 *)p = pixel;
59
+        break;
60
+    }
61
+}
62
+
63
+void swapPixel(SDL_Surface *surface, int x1, int y1, int x2, int y2) {
64
+	Uint32 a,b;
65
+	a = getPixel(surface, x1, y1);
66
+	b = getPixel(surface, x2, y2);
67
+	
68
+	setPixel(surface, x1, y1, b);
69
+	setPixel(surface, x2, y2, a);
70
+}
71
+
72
+void mirrorSurfaceMiddleX(SDL_Surface *surface) {
73
+	SDL_LockSurface(surface);
74
+	int upto = surface->h / 2;
75
+
76
+	for(int i=0; i<upto; i++) {
77
+		for(int x=0; x<surface->w; x++) {
78
+			swapPixel(surface, x, i, x, surface->h-1-i);
79
+		}
80
+	}
81
+	SDL_UnlockSurface(surface);
82
+}

+ 12
- 0
sdlfuncs.h View File

@@ -0,0 +1,12 @@
1
+#ifndef __SDLFUNCS_H
2
+#define __SDLFUNCS_H
3
+
4
+#include <iostream>
5
+#include <SDL.h>
6
+
7
+Uint32 getPixel(SDL_Surface *surface, int x, int y);
8
+void setPixel(SDL_Surface *surface, int x, int y, Uint32 pixel);
9
+void swapPixel(SDL_Surface *surface, int x1, int y1, int x2, int y2);
10
+void mirrorSurfaceMiddleX(SDL_Surface *surface);
11
+
12
+#endif

Loading…
Cancel
Save