commit 9de0171fc5e339fc26868da97535376684db603c Author: seba Date: Sat Feb 9 13:43:23 2008 +0100 Initialisierung des GIT-Repositorys diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e0292b1 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +*.o +*.a diff --git a/Makefile b/Makefile new file mode 100755 index 0000000..b900815 --- /dev/null +++ b/Makefile @@ -0,0 +1,22 @@ +COMPILER = g++ +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 +VERSION = 0.0.1 + + +sgllib: $(OBJECTS) + +sglar: $(OBJECTS) + rm -f sgllib.a + ar rfc sgllib.a $(OBJECTS) + +%.o: %.cpp %.h + $(COMPILER) -c `sdl-config --cflags` $< + + +glgui/glgui.a: + cd glgui; $(MAKE); + +clean: + rm -f $(OBJECTS) + cd glgui; $(MAKE) clean + @echo Done cleaning... \ No newline at end of file diff --git a/emath.cpp b/emath.cpp new file mode 100644 index 0000000..70f4e97 --- /dev/null +++ b/emath.cpp @@ -0,0 +1,243 @@ +#include "emath.h" + +Punkt3D::Punkt3D() { + x = y = z = 0.0f; +} + +Punkt3D::Punkt3D(float _x, float _y, float _z) { + set(_x, _y, _z); +} + +void Punkt3D::set(float _x, float _y, float _z) { + x = _x; + y = _y; + z = _z; +} + +float Punkt3D::abs() { + return sqrt(x*x+y*y+z*z); +} + +Punkt3D Punkt3D::kreuzprodukt(const Punkt3D &b) { + Punkt3D erg; + erg.x = y*b.z - z*b.y; + erg.y = z*b.x - x*b.z; + erg.z = x*b.y - y*b.x; + + return erg; +} + +void Punkt3D::normalize() { + float a = abs(); + x /= a; + y /= a; + z /= a; +} + +Punkt3D Punkt3D::getNormalized() { + Punkt3D ret(*this); + ret.normalize(); + return ret; +} + +bool Punkt3D::isNormalized() { + return (abs()==1); +} + +float Punkt3D::calcAngle(Punkt3D b) { + if(abs()*b.abs()==0.0f) + return 0.0f; + return rad2deg(std::acos(((*this)*b)/(abs()*b.abs()))); +} + +Punkt3D Punkt3D::getOrtographic() { + Punkt3D erg; + if(!x) { + erg.x = 0.0f; + erg.y = z; + erg.z = -y; + } else if(!y) { + erg.x = z; + erg.y = 0; + erg.z = -x; + } else { + // z, oder genereller fall + erg.x = y; + erg.y = -x; + erg.z = 0.0f; + } + return erg; +} + +void Punkt3D::print(std::string coordname) { + if(coordname!="") + coordname.append(" "); + std::cout << coordname << "Coord: (" << x << ", " << y << ", " << z << ")" << std::endl; +} + + + +Punkt3D Punkt3D::operator+(const Punkt3D &b) { + Punkt3D c; + c.x = x + b.x; + c.y = y + b.y; + c.z = z + b.z; + + return c; +} + +Punkt3D Punkt3D::operator-(const Punkt3D &b) { + Punkt3D c; + c.x = x - b.x; + c.y = y - b.y; + c.z = z - b.z; + + return c; +} + +Punkt3D& Punkt3D::operator+=(const Punkt3D &b) { + x += b.x; + y += b.y; + z += b.z; + + return *this; +} + +Punkt3D& Punkt3D::operator-=(const Punkt3D &b) { + x -= b.x; + y -= b.y; + z -= b.z; + + return *this; +} + +Punkt3D Punkt3D::operator+(const float &_m) { + return Punkt3D(x+_m, y+_m, z+_m); +} + +Punkt3D Punkt3D::operator-(const float &_m) { + return Punkt3D(x-_m, y-_m, z-_m); +} + +Punkt3D Punkt3D::operator*(const float &_m) { + return Punkt3D(x*_m, y*_m, z*_m); +} + +Punkt3D Punkt3D::operator/(const float &_m) { + return Punkt3D(x/_m, y/_m, z/_m); +} + +Punkt3D& Punkt3D::operator+=(const float &_m) { + x += _m; + y += _m; + z += _m; + return *this; +} +Punkt3D& Punkt3D::operator-=(const float &_m) { + x -= _m; + y -= _m; + z -= _m; + return *this; +} +Punkt3D& Punkt3D::operator*=(const float &_m) { + x *= _m; + y *= _m; + z *= _m; + return *this; +} +Punkt3D& Punkt3D::operator/=(const float &_m) { + x /= _m; + y /= _m; + z /= _m; + return *this; +} + +float Punkt3D::operator*(const Punkt3D& _m) { + return x * _m.x + y * _m.y + z * _m.z; +} + +Punkt3D Punkt3D::operator-() { + return Punkt3D(-x, -y, -z); +} + +bool Punkt3D::operator==(const Punkt3D& b) { + return ( x==b.x && y==b.y && z==b.z); +} +bool Punkt3D::operator!=(const Punkt3D& b) { + return !(*this==b); +} +// Freunde + +Punkt3D operator+(const float& _m, const Punkt3D& b) { + return Punkt3D(b.x+_m, b.y+_m, b.z+_m); +} + +Punkt3D operator-(const float& _m, const Punkt3D& b) { + return Punkt3D(b.x-_m, b.y-_m, b.z-_m); +} + +Punkt3D operator*(const float& _m, const Punkt3D& b) { + return Punkt3D(b.x*_m, b.y*_m, b.z*_m); +} + +Punkt3D operator/(const float& _m, const Punkt3D& b) { + return Punkt3D(b.x/_m, b.y/_m, b.z/_m); +} + +float abs(Punkt3D p) { + return p.abs(); +} + +// OpenGL Funktionen für Punkt3D + +void glVertex3f(Punkt3D p) { + glVertex3f(p.x, p.y, p.z); +} + +void glTranslatef(Punkt3D p) { + glTranslatef(p.x, p.y, p.z); +} + +void glNormal(Punkt3D p) { + glNormal3f(p.x, p.y, p.z); +} + +void glRotatef(float deg, Punkt3D vec) { + glRotatef(deg, vec.x, vec.y, vec.z); +} + +// Trigonometrische Funktionen + +float deg2rad(float deg) { + return (deg/180.0f)*3.1415926535897932384626433f; +} + +float rad2deg(float rad) { + return (rad/3.1415926535897932384626433f)*180.0f; +} + +float ssin(float c) { + int t=(int)c; + if(t!=c) + return sin(deg2rad(c)); + t = t % 360; + if(t<0) + t = 360 + t; + switch(t) { + case 0: + return 0.0f; + case 90: + return 1.0f; + case 180: + return 0.0f; + case 270: + return -1.0f; + default: +// std::cout << "ssin sollte keinen defaultwert zurückliefern. c=" << c << ", t=" << t << std::endl; + return sin(deg2rad(c)); + } +} + +float scos(float c) { + return ssin(c+90.0f); +} diff --git a/emath.h b/emath.h new file mode 100644 index 0000000..d230329 --- /dev/null +++ b/emath.h @@ -0,0 +1,74 @@ +#ifndef __EMATH_H +#define __EMATH_H + +#include +#include +#include + +class Punkt3D { + public: + Punkt3D(); + Punkt3D(float, float, float); + + float x, y, z; + + void set(float, float, float); + float abs(); + Punkt3D kreuzprodukt(const Punkt3D&); + void normalize(); + Punkt3D getNormalized(); + bool isNormalized(); + float calcAngle(Punkt3D); + Punkt3D getOrtographic(); + + void print(std::string=""); + + + // Operatoren + Punkt3D operator+(const Punkt3D&); + Punkt3D operator-(const Punkt3D&); + Punkt3D& operator+=(const Punkt3D&); + Punkt3D& operator-=(const Punkt3D&); + + Punkt3D operator+(const float&); + Punkt3D operator-(const float&); + Punkt3D operator*(const float&); + Punkt3D operator/(const float&); + Punkt3D& operator+=(const float&); + Punkt3D& operator-=(const float&); + Punkt3D& operator*=(const float&); + Punkt3D& operator/=(const float&); + + float operator*(const Punkt3D&); + + Punkt3D operator-(); + + bool operator==(const Punkt3D&); + bool operator!=(const Punkt3D&); + + friend Punkt3D operator+(const float&, const Punkt3D&); + friend Punkt3D operator-(const float&, const Punkt3D&); + friend Punkt3D operator*(const float&, const Punkt3D&); + friend Punkt3D operator/(const float&, const Punkt3D&); +}; + +float abs(Punkt3D); + +// OpenGL-Funktionen für Punkt3D + +void glVertex3f(Punkt3D); +void glTranslatef(Punkt3D); +void glNormal3f(Punkt3D); + +void glRotatef(float, Punkt3D); + +float deg2rad(float deg); +float rad2deg(float rad); + + + +// *grml* scheiss cmath-sinus +float ssin(float); +float scos(float); + +#endif diff --git a/emath_opengl.cpp b/emath_opengl.cpp new file mode 100644 index 0000000..53b6468 --- /dev/null +++ b/emath_opengl.cpp @@ -0,0 +1,46 @@ +#include "emath_opengl.h" + +void rotFrom2VecTo2Vec(Punkt3D a, Punkt3D b, Punkt3D c, Punkt3D d, Punkt3D *rvec, float *rvecdeg, Punkt3D *rvec2, float *rvecdeg2) { + Punkt3D rotvec, rotvec2, nullvec; + float rotvecdeg = 0.0f, rotvecdeg2 = 0.0f; + + rotvec = a.kreuzprodukt(b); + rotvecdeg = a.calcAngle(b); + if(std::abs(rotvecdeg)==180.0f) { + rotvec = a.getOrtographic(); + } + + if(rotvec==nullvec) { + rotvec.set(1.0f, 0.0f, 0.0f); + rotvecdeg = 0.0f; + } + + Rotationsmatrix rotnobj(rotvec, -rotvecdeg); + c = Rotationsmatrix(rotvec, rotvecdeg) * c; + + rotvec2 = c.kreuzprodukt(d); + rotvecdeg2 = c.calcAngle(d); + if(std::abs(rotvecdeg2)==180.0f) { + rotvec2 = b; + } + + if(rotvec2==nullvec) { + rotvec2.set(1.0f, 0.0f, 0.0f); + rotvecdeg2 = 0.0f; + } + + rotvec2 = rotnobj * rotvec2; + + *rvec = rotvec; + *rvecdeg = rotvecdeg; + *rvec2 = rotvec2; + *rvecdeg2 = rotvecdeg2; +} + +void rotFrom2VecTo2Vec(Punkt3D a, Punkt3D b, Punkt3D c, Punkt3D d) { + Punkt3D rvec, rvec2; + float rvecdeg, rvecdeg2; + rotFrom2VecTo2Vec(a, b, c, d, &rvec, &rvecdeg, &rvec2, &rvecdeg2); + glRotatef(rvecdeg, rvec); + glRotatef(rvecdeg2, rvec2); +} diff --git a/emath_opengl.h b/emath_opengl.h new file mode 100644 index 0000000..f696817 --- /dev/null +++ b/emath_opengl.h @@ -0,0 +1,11 @@ +#ifndef __EMATH_OPENGL +#define __EMATH_OPENGL + +#include +#include "emath.h" +#include "rotationsmatrix.h" +#include + +void rotFrom2VecTo2Vec(Punkt3D a, Punkt3D b, Punkt3D c, Punkt3D d, Punkt3D *, float *, Punkt3D *, float *); +void rotFrom2VecTo2Vec(Punkt3D a, Punkt3D b, Punkt3D c, Punkt3D d); +#endif diff --git a/glcolor.cpp b/glcolor.cpp new file mode 100644 index 0000000..e115eb7 --- /dev/null +++ b/glcolor.cpp @@ -0,0 +1,30 @@ +#include "glcolor.h" + +GLColor::GLColor() { + set(0.0f, 0.0f, 0.0f, 1.0f); + setalpha = true; +} + +GLColor::GLColor(const SDL_Color &c) { + set(c.r/255.0f, c.g/255.0f, c.b/255.0f); + setalpha = true; +} + +void GLColor::set(float _r, float _g, float _b, float _a) { + r = _r; + g = _g; + b = _b; + a = _a; +} + +SDL_Color GLColor::getSDLColor() { + SDL_Color c = {(Uint8)(r*255.0f), (Uint8)(g*255), (Uint8)(b*255), (Uint8)(a*255)}; +} + +void glColorGLC(GLColor c) { + if(c.setalpha) { + glColor4f(c.r, c.g, c.b, c.a); + } else { + glColor3f(c.r, c.g, c.b); + } +} diff --git a/glcolor.h b/glcolor.h new file mode 100644 index 0000000..145de9a --- /dev/null +++ b/glcolor.h @@ -0,0 +1,24 @@ +#ifndef __GLCOLOR_H +#define __GLCOLOR_H + +#include +#include + +class GLColor { + public: + float r, g, b, a; + bool setalpha; + + + GLColor(); + GLColor(const SDL_Color&); + + void set(float _r, float _g, float _b, float _a=1.0f); + SDL_Color getSDLColor(); +}; + +void glColorGLC(GLColor c); +// a) setalpha entscheiden lassen +// b) zwei funktionen, eine setzt alpha mit in richtung 3f, 4f.. + +#endif diff --git a/gldrawhelper.cpp b/gldrawhelper.cpp new file mode 100644 index 0000000..94b2c02 --- /dev/null +++ b/gldrawhelper.cpp @@ -0,0 +1,24 @@ +#include "gldrawhelper.h" + +void GLDrawSDLRect(SDL_Rect *rect, GLRect *tex) { + GLRect tmptex; + if(tex) { + tmptex = *tex; + } else { + tmptex.set(0.0f, 0.0f, 1.0f, 1.0f); + } + + glBegin(GL_QUADS); + glTexCoord2f(tmptex.x, tmptex.y); + glVertex2i(rect->x, rect->y+rect->h); + + glTexCoord2f(tmptex.x+tmptex.w, tmptex.y); + glVertex2i(rect->x+rect->w, rect->y+rect->h); + + glTexCoord2f(tmptex.x+tmptex.w, tmptex.y+tmptex.h); + glVertex2i(rect->x+rect->w, rect->y); + + glTexCoord2f(tmptex.x, tmptex.y+tmptex.h); + glVertex2i(rect->x, rect->y); + glEnd(); +} diff --git a/gldrawhelper.h b/gldrawhelper.h new file mode 100644 index 0000000..ecf83aa --- /dev/null +++ b/gldrawhelper.h @@ -0,0 +1,12 @@ +#ifndef __GLDRAWHELPER_H +#define __GLDRAWHELPER_H + +#include +#include + +#include "glrect.h" + +void GLDrawSDLRect(SDL_Rect *rect, GLRect *tex=0); + + +#endif diff --git a/glfontengine.cpp b/glfontengine.cpp new file mode 100644 index 0000000..9dc68e1 --- /dev/null +++ b/glfontengine.cpp @@ -0,0 +1,231 @@ +#include "glfontengine.h" + + +bool GLFontEngine::addFont(std::string fontfile, std::string fontname) { + GLFont *tmp = new GLFont(fontfile, 0.685f); + if(!tmp->font->isLoaded()) { + delete(tmp); + std::cerr << fontfile << " konnte als Font nicht geladen werden" << std::endl; + return false; + } + + fontpool[fontname] = tmp; + return false; +} + +void GLFontEngine::quit() { + for(std::map::iterator iter = fontpool.begin(); iter != fontpool.end(); iter++) { + delete(iter->second); // unloaded texture.. der dtor + fontpool.erase(iter); + } +} + +void GLFontEngine::prepare2DbyPushingMatrix() { + const SDL_Surface *screen = SDL_GetVideoSurface(); + glMatrixMode(GL_PROJECTION); + glPushMatrix(); + glLoadIdentity(); + glOrtho(0, screen->w, screen->h, 0, -1, 1); +// glOrtho(0, 640, 480, 0, -1, 1); + glMatrixMode(GL_MODELVIEW); + glPushMatrix(); + glLoadIdentity(); +} + +void GLFontEngine::regain3DbyPoppingMatrix() { + glMatrixMode(GL_PROJECTION); + glPopMatrix(); + glMatrixMode(GL_MODELVIEW); + glPopMatrix(); +} + +void GLFontEngine::paintSDLRect(SDL_Rect r) { + glBegin(GL_QUADS); + glTexCoord2f(0.0f, 1.0f); + glVertex2i(r.x, r.y); + + glTexCoord2f(0.0f, 0.0f); + glVertex2i(r.x, r.y+r.h); + + glTexCoord2f(1.0f, 0.0f); + glVertex2i(r.x+r.w, r.y+r.h); + + glTexCoord2f(1.0f, 1.0f); + glVertex2i(r.x+r.w, r.y); + + glEnd(); +} + +GLFontEngine::GLFontEngine() { + init(); + fontloaded = fontSelect(""); +} + +GLFontEngine::GLFontEngine(std::string fontstr) { + init(); + fontloaded = fontSelect(fontstr); +} + +void GLFontEngine::init() { +// r = g = b = a = 1.0f; + fsize = 16; +} + +void GLFontEngine::setColor(float _r, float _g, float _b, float _a) { +// r = _r; +// g = _g; +// b = _b; +// a = _a; + col.set(_r, _g, _b, _a); +} + +void GLFontEngine::setColor(GLColor c) { + col = c; +} + +bool GLFontEngine::fontSelect(std::string fontstr) { + if(fontpool.size()==0) { + fontloaded = false; + return false; + } + fontloaded = true; // fontstr oder fallbackfont + if(fontpool[fontstr]) + font = fontpool[fontstr]; + else { + //fallbackfont - ersten aus der liste + font = (fontpool.begin())->second; + } + + return true; +} + +void GLFontEngine::renderText(std::string text, SDL_Rect pos) { + renderLine(text, pos); +} + +void GLFontEngine::renderLine(std::string text, SDL_Rect pos) { +// glColor4f(r, g, b, a); + glColorGLC(col); + + if(fontloaded) { + font->font->selectTexture(); + + float step = pos.w / (float)text.length(); + float tex_x, tex_y; + float tex_len = (1.0f/16.0f); + + for(unsigned int i=0; icharwidth*fsize>wrap) { + str.insert(i, "\n"); + } + } else { + a=0; + } + } + } + + if(paintbackground) { + std::cout << "Paint Background implementieren.. ;) " << std::endl; + } + + SDL_Rect m; + int strlpos; + int max_width = 0; + int linecount = 0; + std::string rstr; + + while(str!="") { + + if((strlpos = str.find('\n'))!=std::string::npos) { + + rstr = str.substr(0, strlpos); + str = str.substr(strlpos+1); + } else { + rstr = str; + str = ""; + } + renderLine(rstr, x, y+(int)(1.1*linecount*fsize), center, &m); + + max_width = std::max(max_width, (int)m.w); + linecount++; + } + + if(rendered_to) { + m.w = max_width; + m.y = y; + *rendered_to = m; + } + + return; +} + +void GLFontEngine::setSize(int s) { + fsize = s; +} + +int GLFontEngine::getSize() { + return fsize; +} + +void GLFontEngine::getSDLRect(const std::string &str, SDL_Rect *r) { + r->w = getTextWidth(str); + r->h = getSize(); +} + +int GLFontEngine::getTextWidth(const std::string &moep) { + return (int)(moep.length()*font->charwidth*fsize); +} + +std::map GLFontEngine::fontpool; diff --git a/glfontengine.h b/glfontengine.h new file mode 100644 index 0000000..f9e0e0f --- /dev/null +++ b/glfontengine.h @@ -0,0 +1,70 @@ +#ifndef __GLFONTENGINE_H +#define __GLFONTENGINE_H + +#include +#include +#include +#include +#include + + +#include +#include +#include "gltexture.h" +#include "glcolor.h" + +class GLFont { + public: + GLFont(std::string fontfile, float cwdth) { + charwidth = cwdth; + font = new GLTexture(fontfile); + } + ~GLFont() { + delete(font); + } + GLTexture* font; + float charwidth; +}; + +class GLFontEngine { + private: + static std::map fontpool; + + GLFont *font; + bool fontloaded; + GLColor col; +// float r, g, b, a; + int fsize; + + void init(); + public: + static bool addFont(std::string fontfile, std::string fontname); + static void quit(); + + static void prepare2DbyPushingMatrix(); + static void regain3DbyPoppingMatrix(); + static void paintSDLRect(SDL_Rect); + + GLFontEngine(); + GLFontEngine(std::string); + bool fontSelect(std::string); + void setColor(float, float, float, float=1.0f); + void setColor(GLColor); + + void renderText(std::string, SDL_Rect); // wrapper +// void renderText(std::string, int x, int y, *SDL_Rect=0, bool center=false); +// void renderLines(std::string, int x, int y, + + void renderLine(std::string, SDL_Rect); + void renderLine(std::string, int x, int y, bool center=false, SDL_Rect *rendered_to=0); + void renderLines(std::string, int x, int y, bool center=false, SDL_Rect *rendered_to=0, int wrap=0, int paintbackground=0); + + void setSize(int); + int getSize(); + void getSDLRect(const std::string&, SDL_Rect*); + int getTextWidth(const std::string&); + +}; + + +#endif diff --git a/glgui/Makefile b/glgui/Makefile new file mode 100755 index 0000000..ae867f7 --- /dev/null +++ b/glgui/Makefile @@ -0,0 +1,12 @@ +# COMPILER = g++ +OBJECTS = button.o object.o textlabel.o window.o + +glguilib: $(OBJECTS) + rm glgui.a -f + ar rfc glgui.a $(OBJECTS) + +%.o: %.cpp %.h + $(COMPILER) -c `sdl-config --cflags` $< + +clean: + rm -f $(OBJECTS) diff --git a/glgui/button.cpp b/glgui/button.cpp new file mode 100644 index 0000000..512a29c --- /dev/null +++ b/glgui/button.cpp @@ -0,0 +1,18 @@ +#include "button.h" + +GLGuiButton::GLGuiButton(int _eventid, int _x, int _y, bool _center, int _wrap) : GLGuiTextLabel(_x, _y, _center, _wrap) { + highlightable = true; + clickable = true; + eventid = _eventid; + highlightcol.set(1.0f, 0.0f, 0.0f, 1.0f); +} +GLGuiButton::GLGuiButton(int _eventid, std::string str, int _x, int _y, bool _center, int _wrap) : GLGuiTextLabel(str, _x, _y, _center, _wrap){ + highlightable = true; + clickable = true; + eventid = _eventid; + highlightcol.set(1.0f, 0.0f, 0.0f, 1.0f); +} + +void GLGuiButton::onMouseOver(int m_x, int m_y) { + // Längste stelle finden, dann collision +} diff --git a/glgui/button.h b/glgui/button.h new file mode 100644 index 0000000..a705d4c --- /dev/null +++ b/glgui/button.h @@ -0,0 +1,28 @@ +#ifndef __GLGUIBUTTON_H +#define __GLGUIBUTTON_H + +#include +#include +#include +#include + +#include "textlabel.h" + +#define GLGUI_BUTTONDOWN SDL_USEREVENT+10 + +class GLGuiButton : public GLGuiTextLabel { + protected: + int eventid; + GLColor highlightcol; + + public: + GLGuiButton(int _eventid, int _x, int _y, bool _center=false, int _wrap=0); + GLGuiButton(int _eventid, std::string str, int _x, int _y, bool _center=false, int _wrap=0); + + void setHighlightColor(); + + void onMouseOver(int m_x, int m_y); + void onMouseClick(int m_x, int m_y); +}; + +#endif diff --git a/glgui/object.cpp b/glgui/object.cpp new file mode 100644 index 0000000..19b759f --- /dev/null +++ b/glgui/object.cpp @@ -0,0 +1,7 @@ +#include "object.h" + +GLGuiObject::GLGuiObject() { + highlightable = false; + clickable = false; + keyboardable = false; +} diff --git a/glgui/object.h b/glgui/object.h new file mode 100644 index 0000000..13890cb --- /dev/null +++ b/glgui/object.h @@ -0,0 +1,29 @@ +#ifndef __GLGUIOBJECT_H +#define __GLGUIOBJECT_H + +#include +#include +#include +#include + +class GLGuiObject { + protected: + SDL_Rect pos; + bool highlightable, clickable, keyboardable; + public: + GLGuiObject(); + virtual ~GLGuiObject() { }; + + virtual void render()=0; + + // Optionen + bool isHighlightable() { return highlightable; }; + bool isClickable() { return clickable; }; + bool isKeyboardable() { return keyboardable; }; + + virtual void onMouseOver(int m_x, int m_y) { }; + virtual void onMouseClick(int m_x, int m_y, int m_button) { }; + virtual void onKeyboardInput(char c) { }; +}; + +#endif diff --git a/glgui/textlabel.cpp b/glgui/textlabel.cpp new file mode 100644 index 0000000..1fbfcbe --- /dev/null +++ b/glgui/textlabel.cpp @@ -0,0 +1,37 @@ +#include "textlabel.h" + +GLGuiTextLabel::GLGuiTextLabel(int _x, int _y, bool _center, int _wrap) { + setPos(_x, _y); + center = _center; + wrap = _wrap; + col.set(1.0f, 1.0f, 1.0f, 1.0f); +} + +GLGuiTextLabel::GLGuiTextLabel(std::string str, int _x, int _y, bool _center, int _wrap) { + setPos(_x, _y); + center = _center; + wrap = _wrap; + setText(str); + col.set(1.0f, 1.0f, 1.0f, 1.0f); +} + +void GLGuiTextLabel::setText(std::string str) { + text = str; +} + +void GLGuiTextLabel::setColor(GLColor c) { + col = c; +} + +void GLGuiTextLabel::setPos(int _x, int _y) { + x = _x; + y = _y; +} + +void GLGuiTextLabel::render() { + glColorGLC(col); + + GLFontEngine::prepare2DbyPushingMatrix(); + fontengine.renderLines(text, x, y, center, 0, wrap, 0); + GLFontEngine::regain3DbyPoppingMatrix(); +} diff --git a/glgui/textlabel.h b/glgui/textlabel.h new file mode 100644 index 0000000..c2050c8 --- /dev/null +++ b/glgui/textlabel.h @@ -0,0 +1,32 @@ +#ifndef __GLGUITEXTLABEL_H +#define __GLGUITEXTLABEL_H + +#include +#include +#include +#include + +#include "object.h" +#include "../glcolor.h" +#include "../glfontengine.h" + +class GLGuiTextLabel : public GLGuiObject{ + protected: + GLFontEngine fontengine; + std::string text; + + GLColor col; + int x, y; + bool center; + int wrap; + public: + GLGuiTextLabel(int _x, int _y, bool _center=false, int _wrap=0); + GLGuiTextLabel(std::string, int _x, int _y, bool _center=false, int _wrap=0); + + void setText(std::string str); + void setColor(GLColor); + void setPos(int _x, int _y); + virtual void render(); +}; + +#endif diff --git a/glgui/window.cpp b/glgui/window.cpp new file mode 100644 index 0000000..0cc3e2f --- /dev/null +++ b/glgui/window.cpp @@ -0,0 +1,39 @@ +#include "window.h" + +GLGuiWindow::GLGuiWindow(SDL_Rect _pos, GLColor _bgcolor) { + pos = _pos; + bgcolor = _bgcolor; +} + +void GLGuiWindow::addItem(GLGuiObject *gobj) { + // Objekt wird in delete gelöscht + items.push_back(gobj); + if(gobj->isHighlightable()) + highlightable.push_back(gobj); + if(gobj->isClickable()) + clickable.push_back(gobj); + if(gobj->isKeyboardable()) + keyboardable.push_back(gobj); + +} + +void GLGuiWindow::render() { + if(renderbackground) { + glColorGLC(bgcolor); + GLDrawSDLRect(&pos); + } + + for(unsigned int i=0; irender(); + } +} + +SDL_Rect GLGuiWindow::getPos() { + return pos; +} + +GLGuiWindow::~GLGuiWindow() { + for(unsigned int i=0; i +#include +#include +#include +#include + +#include "../glcolor.h" +#include "../gldrawhelper.h" +#include "object.h" + + +class GLGuiWindow : public GLGuiObject { + private: + bool renderbackground; + SDL_Rect pos; + GLColor bgcolor; + + std::vector items; + + std::vector highlightable; + std::vector clickable; + std::vector keyboardable; + public: + GLGuiWindow(SDL_Rect _pos, GLColor _bgcolor); + ~GLGuiWindow(); + + void addItem(GLGuiObject*); + SDL_Rect getPos(); + void render(); + +}; + +#endif diff --git a/glrect.cpp b/glrect.cpp new file mode 100644 index 0000000..480b6b4 --- /dev/null +++ b/glrect.cpp @@ -0,0 +1,35 @@ +#include "glrect.h" + +GLRect::GLRect() { + set(0.0f, 0.0f, 0.0f, 0.0f); +} + +GLRect::GLRect(float _x, float _y, float _w, float _h) { + set(_x, _y, _w, _h); +} + +GLRect::GLRect(const SDL_Rect &r) { + set(r.x, r.y, r.w, r.h); +} + +void GLRect::set(float _x, float _y, float _w, float _h) { + x = _x; + y = _y; + w = _w; + h = _h; +} + +void GLRect::setPoint(float _x, float _y) { + x = _x; + y = _y; +} + +void GLRect::setDim(float _w, float _h) { + w = _w; + h = _h; +} + +SDL_Rect GLRect::getSDLRect() { + SDL_Rect tmp = {(Uint8)x, (Uint8)y, (Uint8)w, (Uint8)h}; + return tmp; +} diff --git a/glrect.h b/glrect.h new file mode 100644 index 0000000..886e4de --- /dev/null +++ b/glrect.h @@ -0,0 +1,22 @@ +#ifndef __GLRECT_H +#define __GLRECT_H + +#include + +class GLRect { + public: + float x, y; + float w, h; + + GLRect(); + GLRect(float, float, float, float); + GLRect(const SDL_Rect&); + + void set(float, float, float, float); + void setPoint(float, float); + void setDim(float, float); + SDL_Rect getSDLRect(); + +}; + +#endif diff --git a/gltexture.cpp b/gltexture.cpp new file mode 100644 index 0000000..a519f4f --- /dev/null +++ b/gltexture.cpp @@ -0,0 +1,172 @@ +#include "gltexture.h" + +GLTexture::GLTexture() { + init(); + alltextures.push_back(this); +} + +GLTexture::GLTexture(std::string fname, GLint _minfilter, GLint _magfilter, GLint _wraps, GLint _wrapt) { + init(); + setParameter(_minfilter, _magfilter, _wraps, _wrapt); + loadImage(fname); + + alltextures.push_back(this); +} + +GLTexture::GLTexture(SDL_Surface *srfc) { + init(); + loadSurface(srfc); +} + +void GLTexture::init() { + loaded = false; + texconverted = false; + keepsurface = false; + donotreload = false; + + minfilter = GL_LINEAR; + magfilter = GL_LINEAR; + + wraps = GL_REPEAT; + wrapt = GL_REPEAT; + + tex = 0; + width = height = 0; +} + +bool GLTexture::loadLocalSurface() { + if(!texconverted) + convertLocalSurface(); + + width = tex->w; + height = tex->h; + + glGenTextures(1, &texint); + glBindTexture(GL_TEXTURE_2D, texint); + + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, minfilter); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, magfilter); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wraps); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapt); + + gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA8, width, height, GL_BGRA, GL_UNSIGNED_BYTE, tex->pixels); + + loaded = true; + + return true; +} + +void GLTexture::convertLocalSurface() { + tex = SDL_DisplayFormatAlpha(tex); + mirrorSurfaceMiddleX(tex); + + texconverted = true; +} + +bool GLTexture::loadImage(std::string fname) { + if(isLoaded()) + unloadTexture(); + filename = fname; + tex = IMG_Load(filename.c_str()); + + if(!tex) { + std::cerr << "Textur konnte nicht geladen werden: " << filename << std::endl; + return false; + } + + bool ret = loadLocalSurface(); + keepsurface = false; + SDL_FreeSurface(tex); + tex = 0; + + return ret; +} + +bool GLTexture::loadSurface(SDL_Surface *srfc, bool noreloading) { + if(isLoaded()) + unloadTexture(); + if(filename!="") + filename = ""; + + tex = srfc; + + keepsurface = true; + donotreload = noreloading; + + return loadLocalSurface(); +} + +bool GLTexture::selectTexture() { + if(!loaded) { + std::cerr << "Textur wurde nicht geladen: " << filename << std::endl; + return false; + } + + glBindTexture(GL_TEXTURE_2D, texint); + + return true; +} + +void GLTexture::unloadTexture() { + glDeleteTextures(1, &texint); + + loaded = false; + // Entweder tex == image, wurde schon gelöscht oder wir müssen es nich löschen + +} + +bool GLTexture::setParameter(GLint _minfilter, GLint _magfilter, GLint _wraps, GLint _wrapt) { + if(_minfilter!=0) + minfilter = _minfilter; + if(_magfilter!=0) + magfilter = _magfilter; + if(_wraps!=0) + wraps = _wraps; + if(_wrapt!=0) + wrapt = _wrapt; + + if(!loaded) + return true; + + if(keepsurface&&tex!=0) { + unloadTexture(); + return loadLocalSurface(); + } else if(filename!="") { + return loadImage("filename"); + } else { + // keine datei, kein surface + unloadTexture(); + std::cerr << "Couldn't reload GLTexture " << this << "- No surface, no file!" << std::endl; + return false; + } +} + +int GLTexture::getW() { + return width; +} + +int GLTexture::getH() { + return height; +} + +bool GLTexture::isLoaded() { + return loaded; +} + +void GLTexture::reloadAll() { + for(unsigned int i=0; isetParameter(); +} + +GLTexture::~GLTexture() { + if(isLoaded()) + unloadTexture(); + + for(unsigned int i=0; i GLTexture::alltextures; diff --git a/gltexture.h b/gltexture.h new file mode 100644 index 0000000..0fa564b --- /dev/null +++ b/gltexture.h @@ -0,0 +1,63 @@ +#ifndef __GLTEXTUR_H +#define __GLTEXTUR_H + +#include +#include +#include +#include +#include +#include +#include "sdlfuncs.h" + +class GLTexture { + private: + GLuint texint; + SDL_Surface *tex; + int width, height; + + std::string filename; // wenn von datei geladen + + // Parameter + GLint minfilter; + GLint magfilter; + GLint wraps; + GLint wrapt; + + bool loaded; + bool texconverted; + bool keepsurface; + bool donotreload; + + void init(); + bool loadLocalSurface(); + void convertLocalSurface(); + + static std::vector alltextures; + protected: + GLTexture(SDL_Surface*); + bool loadSurface(SDL_Surface*, bool = false); + public: + static void reloadAll(); + + GLTexture(); + GLTexture(std::string, GLint=GL_LINEAR, GLint=GL_LINEAR, GLint=GL_REPEAT, GLint=GL_REPEAT); + ~GLTexture(); + + //load + + bool loadImage(std::string); + + bool selectTexture(); + void unloadTexture(); + bool setParameter(GLint=0, GLint=0, GLint=0, GLint=0); + inline bool select() { return selectTexture(); } + + int getW(); + int getH(); + + + + bool isLoaded(); +}; + +#endif diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..4db8011 --- /dev/null +++ b/main.cpp @@ -0,0 +1,177 @@ +#define VERSION "0.2.2 DEV" + +#include +#include + +#include +#include + +#include "gltexture.h" +#include "glfontengine.h" + +// #define GL_DEBUG_MOVEMENT false + +void setupGL(); +void resizeOpenGLWindow(int, int, float=1.0f, float=100.0f); + +int main(int argc, char **argv) { + std::cout << "ARGV: " << argv[0] << std::endl; + //Vars + SDL_Surface *screen; + bool quit = false; + SDL_Event event; + int w_width = 640; + int w_height = 480; + int w_bbp = 32; + float znear = 1.0f; + float zfar = 100.0f; + + if(SDL_Init(SDL_INIT_VIDEO)<0) { + std::cerr << "Konnte SDL nicht initialisieren" << SDL_GetError() << std::endl; + return 1; + } + + // Setting video mode + int videoFlags; + const SDL_VideoInfo *videoInfo; + + videoInfo = SDL_GetVideoInfo(); + + if(!videoInfo) { + std::cerr << "Video query failed: " << SDL_GetError() << std::endl; + return 1; + } + + videoFlags = SDL_OPENGL; + videoFlags |= SDL_GL_DOUBLEBUFFER; + videoFlags |= SDL_HWPALETTE; + videoFlags |= SDL_RESIZABLE; + if(videoInfo->hw_available) + videoFlags |= SDL_HWSURFACE; + else + videoFlags |= SDL_SWSURFACE; + if(videoInfo->blit_hw) + videoFlags |= SDL_HWACCEL; + +// videoFlags |=SDL_FULLSCREEN; + +// SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 5 ); +// SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 5 ); +// SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 5 ); +// SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 16 ); + SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 ); + + if(!(screen = SDL_SetVideoMode(w_width, w_height, w_bbp, videoFlags))) { + std::cerr << "Kein SDL-Screen verfügbar: " << SDL_GetError() << std::endl; + return 1; + } + SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 ); + + SDL_WM_SetCaption("GLGui Test, 0); + + // blank screen +// SDL_FillRect(screen, 0, SDL_MapRGBA(screen->format, 0, 0, 0, 0)); + + // GL Optionen + setupGL(); + + resizeOpenGLWindow(w_width, w_height, znear, zfar); + + // Keyrepeat, falls benötigt auskommentieren + SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL); + + // Fontengine initliaisieren, für alle die Fonts brauchen + GLFontEngine::addFont("font_big_better_alpha.png", "default"); + + // Programmvariablen + Snake3D snake3d; + + //movement & keyboardcontrol + Uint8 *keys = SDL_GetKeyState(NULL);; + SDLKey tkey; + typedef enum { move_up=0, move_down, move_left, move_right} nummovekeys; + SDLKey movekeys[4]; + + bool pause = true; + bool blend = false; + bool extralines = false; + bool fullscreen = false; + + // splash screen + bool splashscreen = true; + snake3d.pause(true); + SDL_Rect splashpos = { 40, 40, screen->w -80, 310 }; + GLFontEngine fontengine("default"); + +#ifdef GL_DEBUG_MOVEMENT + movekeys[move_up] = SDLK_i; + movekeys[move_down] = SDLK_k; + movekeys[move_left] = SDLK_j; + movekeys[move_right] = SDLK_l; +#else + movekeys[move_up] = SDLK_UP; + movekeys[move_down] = SDLK_DOWN; + movekeys[move_left] = SDLK_LEFT; + movekeys[move_right] = SDLK_RIGHT; +#endif + + while(!quit) { + if(SDL_PollEvent(&event)) { + switch(event.type) { + case SDL_QUIT: + quit = true; + break; + case SDL_KEYDOWN: + keys = SDL_GetKeyState(NULL); + tkey = event.key.keysym.sym; + + break; + case SDL_KEYUP: + keys = SDL_GetKeyState(NULL); + break; + } + } else { + + glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + glLoadIdentity(); + + SDL_Delay(10); + } + + } + GLFontEngine::quit(); + SDL_Quit(); + + return 0; +} + +void setupGL() { + glClearColor(0.0f, 0.0f, 0.0f, 0.0f); + glClearDepth(1.0f); + glDepthFunc(GL_LEQUAL); + glEnable(GL_DEPTH_TEST); + glEnable(GL_TEXTURE_2D); + glShadeModel(GL_SMOOTH); + glBlendFunc(GL_SRC_ALPHA, GL_ONE); +// glBlendFunc(GL_SRC_COLOR,GL_DST_COLOR); + +// glBlendFunc(GL_DST_COLOR, GL_SRC_ALPHA); +// glBlendFunc(GL_ONE_MINUS_SRC_ALPHA, GL_SRC_ALPHA); + glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); + glEnable(GL_ALPHA_TEST); + glAlphaFunc(GL_GREATER, 0.1); +} + +void resizeOpenGLWindow(int width, int height, float znear, float zfar) { + if(height==0) + height = 1; + + glViewport(0, 0, (GLsizei)width, (GLsizei)height); + + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + gluPerspective(45.0f, (GLfloat)width/(GLfloat)height, znear, zfar); + + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); +} diff --git a/matrix.cpp b/matrix.cpp new file mode 100644 index 0000000..f865e97 --- /dev/null +++ b/matrix.cpp @@ -0,0 +1,149 @@ +#include "matrix.h" + +Matrix::Matrix(int _m, int _n) { + m = _m; + n = _n; + allocate(); +} + +Matrix::Matrix(int _m, int _n, const float **src) { + m = _m; + n = _n; + allocate(); + copyFromSource(src); +} + +Matrix::Matrix(Punkt3D d) { + m = 3; + n = 1; + allocate(); + c[0][0] = d.x; + c[1][0] = d.y; + c[2][0] = d.z; +} + +Matrix::Matrix(const Matrix& mat) { + m = mat.m; + n = mat.n; + allocate(); + copyFromSource(mat.getMatrix()); +} + +void Matrix::allocate() { + c = new float*[m]; + for(int i=0; i=m||_n>=n) + return false; + c[_m][_n] = d; + return true; +} + +Matrix Matrix::operator*(const Matrix &d) { + + if(n!=d.m) + return Matrix(1,1); + Matrix erg(m, d.n); + + + for(int a=0; a +#include + +#include "emath.h" + +class Matrix { + protected: + float **c; + int m, n; + + void allocate(); + void copyFromSource(const float**); + public: + Matrix(int, int); + Matrix(int, int, const float**); + Matrix(Punkt3D); + Matrix(const Matrix&); + ~Matrix(); + + const float **getMatrix() const; + bool set(float, int, int); + int getM() const; + int getN() const; + + Matrix operator*(const Matrix&); + Matrix& operator=(const Matrix&); + +// Matrix operator+(const float&); +// Matrix operator-(const float&); +// Matrix operator*(const float&); +// Matrix operator/(const float&); + + void print(std::string="") const; + +}; + + + +#endif diff --git a/quaternion.cpp b/quaternion.cpp new file mode 100644 index 0000000..881535c --- /dev/null +++ b/quaternion.cpp @@ -0,0 +1,77 @@ +#include "quaternion.h" + +Quaternion::Quaternion() { + w = 1.0f; + x = y = z = 0.0f; +} + +void Quaternion::createFromRotation(float deg, float _x, float _y, float _z) { + float tmpres = sin(deg2rad(deg) / 2.0f); + + w = cos(deg2rad(deg) / 2.0f); + x = _x * tmpres; + y = _y * tmpres; + z = _z * tmpres; +} + +Quaternion Quaternion::operator*(const Quaternion &q) { + Quaternion ret; + + ret.w = w*q.w - x*q.x - y*q.y - z*q.z; + ret.x = w*q.x + x*q.w + y*q.z - z*q.y; + ret.y = w*q.y + y*q.w + z*q.x - x*q.z; + ret.z = w*q.z + z*q.w + x*q.y - y*q.x; + + return ret; +} + +void Quaternion::createGlMatrix(GLfloat *matrix) { + if(!matrix) + return; + + matrix[0] = 1.0f - 2.0f * ( y*y + z*z); + matrix[1] = 2.0f * ( x*y + z*w); + matrix[2] = 2.0f * ( x*z - y*w); + matrix[3] = 0.0f; + + matrix[4] = 2.0f * ( x*y - z*w); + matrix[5] = 1.0f - 2.0f * ( x*x + z*z); + matrix[6] = 2.0f * ( z*y + x*w); + matrix[7] = 0.0f; + + matrix[8] = 2.0f * ( x*z + y*w); + matrix[9] = 2.0f * ( y*z - x*w); + matrix[10] = 1.0f - 2.0f * ( x*x + y*y); + matrix[11] = 0.0f; + + matrix[12] = 0.0f; + matrix[13] = 0.0f; + matrix[14] = 0.0f; + matrix[15] = 1.0f; + +} + +Punkt3D Quaternion::getDirectionVector() { + GLfloat matrix[16]; + createGlMatrix(matrix); + + return getDirectionVector(matrix); +} + +Punkt3D Quaternion::getDirectionVector(GLfloat *matrix) { + if(!matrix) + return Punkt3D(); + Punkt3D tmp; + + tmp.x = matrix[8]; + tmp.y = matrix[9]; + tmp.z = matrix[10]; + + return tmp; +} + +void glMultMatrixf(Quaternion q) { + GLfloat matrix[16]; + q.createGlMatrix(matrix); + glMultMatrixf(matrix); +} diff --git a/quaternion.h b/quaternion.h new file mode 100644 index 0000000..1b58aaf --- /dev/null +++ b/quaternion.h @@ -0,0 +1,30 @@ +#ifndef __QUATERNION_H +#define __QUATERNION_H + +#include +#include +#include + +#include "emath.h" + +class Quaternion { + private: + float w; + float x,y,z; + public: + Quaternion(); + void createFromRotation(float, float, float, float); + + + void createGlMatrix(GLfloat*); + Punkt3D getDirectionVector(); + Punkt3D getDirectionVector(GLfloat*); + + Quaternion operator*(const Quaternion&); + + +}; + +void glMultMatrixf(Quaternion); + +#endif diff --git a/rotationsmatrix.cpp b/rotationsmatrix.cpp new file mode 100644 index 0000000..c40f64b --- /dev/null +++ b/rotationsmatrix.cpp @@ -0,0 +1,91 @@ +#include "rotationsmatrix.h" + +Rotationsmatrix::Rotationsmatrix() : Matrix(3,3) { + set(x_axis, 0.0f); +} + +Rotationsmatrix::Rotationsmatrix(axis _ax, float _deg) : Matrix(3,3) { + set(_ax, _deg); +} + +Rotationsmatrix::Rotationsmatrix(Punkt3D _rotvec, float _deg) : Matrix(3,3) { + set(_rotvec, _deg); +} + +void Rotationsmatrix::set(Punkt3D _rotvec, float _deg) { + rotvec = _rotvec; + rotvec.normalize(); + deg = _deg; + initRot(); +} + +void Rotationsmatrix::set(axis _ax, float _deg) { + deg = _deg; + + switch(_ax) { + case x_axis: + rotvec.set(1.0f, 0.0f, 0.0f); + break; + case y_axis: + rotvec.set(0.0f, 1.0f, 0.0f); + break; + case z_axis: + rotvec.set(0.0f, 0.0f, 1.0f); + break; + } + initRot(); +} + +void Rotationsmatrix::set(float _deg) { + deg = _deg; + initRot(); +} + +void Rotationsmatrix::initRot() { + c[0][0] = scos(deg) + pow(rotvec.x,2.0f) * (1 - scos(deg)); + c[0][1] = rotvec.x * rotvec.y * (1 - scos(deg)) - rotvec.z * ssin(deg); + c[0][2] = rotvec.x * rotvec.z * (1 - scos(deg)) + rotvec.y * ssin(deg); + + c[1][0] = rotvec.y * rotvec.x * (1 - scos(deg)) + rotvec.z * ssin(deg); + c[1][1] = scos(deg) + pow(rotvec.y,2.0f) * (1 - scos(deg)); + c[1][2] = rotvec.y * rotvec.z * (1 - scos(deg)) - rotvec.x * ssin(deg); + + c[2][0] = rotvec.z * rotvec.x * (1 - scos(deg)) - rotvec.y * ssin(deg); + c[2][1] = rotvec.z * rotvec.y * (1 - scos(deg)) + rotvec.x * ssin(deg); + c[2][2] = scos(deg) + pow(rotvec.z,2.0f) * (1 - scos(deg)); +} + +Punkt3D Rotationsmatrix::operator*(const Punkt3D &p) { + Matrix erg(3,1); + Punkt3D erg2; + + erg = (((Matrix*)(this))->operator*(Matrix(p))); + + const float **mat = erg.getMatrix(); + erg2.set(mat[0][0], mat[1][0], mat[2][0]); + + return erg2; +} + +Rotationsmatrix Rotationsmatrix::operator*(const Rotationsmatrix &p) { + Matrix m(3, 3); + Rotationsmatrix rotmat; + m = ((Matrix)(*this) * (Matrix)p); + const float **mat = m.getMatrix(); + rotmat.c[0][0] = mat[0][0]; + rotmat.c[0][1] = mat[0][1]; + rotmat.c[0][2] = mat[0][2]; + rotmat.c[1][0] = mat[1][0]; + rotmat.c[1][1] = mat[1][1]; + rotmat.c[1][2] = mat[1][2]; + rotmat.c[2][0] = mat[2][0]; + rotmat.c[2][1] = mat[2][1]; + rotmat.c[2][2] = mat[2][2]; + + return rotmat; +} + + +Rotationsmatrix::~Rotationsmatrix() { + +} diff --git a/rotationsmatrix.h b/rotationsmatrix.h new file mode 100644 index 0000000..3297662 --- /dev/null +++ b/rotationsmatrix.h @@ -0,0 +1,30 @@ +#ifndef __ROTATIONSMATRIX_H +#define __ROTATIONSMATRIX_H + +#include +#include "emath.h" +#include "matrix.h" + +enum axis { x_axis=0, y_axis, z_axis }; + +class Rotationsmatrix : public Matrix { + private: + float deg; + Punkt3D rotvec; + + void initRot(); + public: + Rotationsmatrix(); + Rotationsmatrix(axis, float); + Rotationsmatrix(Punkt3D, float); + ~Rotationsmatrix(); + + void set(float); + void set(axis, float); + void set(Punkt3D, float); + + Punkt3D operator*(const Punkt3D&); + Rotationsmatrix operator*(const Rotationsmatrix&); +}; + +#endif diff --git a/sdlfuncs.cpp b/sdlfuncs.cpp new file mode 100644 index 0000000..675e678 --- /dev/null +++ b/sdlfuncs.cpp @@ -0,0 +1,82 @@ +#include "sdlfuncs.h" + +Uint32 getPixel(SDL_Surface *surface, int x, int y) +{ + int bpp = surface->format->BytesPerPixel; + /* Here p is the address to the pixel we want to retrieve */ + Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp; + + switch(bpp) { + case 1: + return *p; + + case 2: + return *(Uint16 *)p; + + case 3: + if(SDL_BYTEORDER == SDL_BIG_ENDIAN) + return p[0] << 16 | p[1] << 8 | p[2]; + else + return p[0] | p[1] << 8 | p[2] << 16; + + case 4: + return *(Uint32 *)p; + + default: + return 0; /* shouldn't happen, but avoids warnings */ + } +} + +void setPixel(SDL_Surface *surface, int x, int y, Uint32 pixel) +{ + int bpp = surface->format->BytesPerPixel; + /* Here p is the address to the pixel we want to set */ + Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp; + + switch(bpp) { + case 1: + *p = pixel; + break; + + case 2: + *(Uint16 *)p = pixel; + break; + + case 3: + if(SDL_BYTEORDER == SDL_BIG_ENDIAN) { + p[0] = (pixel >> 16) & 0xff; + p[1] = (pixel >> 8) & 0xff; + p[2] = pixel & 0xff; + } else { + p[0] = pixel & 0xff; + p[1] = (pixel >> 8) & 0xff; + p[2] = (pixel >> 16) & 0xff; + } + break; + + case 4: + *(Uint32 *)p = pixel; + break; + } +} + +void swapPixel(SDL_Surface *surface, int x1, int y1, int x2, int y2) { + Uint32 a,b; + a = getPixel(surface, x1, y1); + b = getPixel(surface, x2, y2); + + setPixel(surface, x1, y1, b); + setPixel(surface, x2, y2, a); +} + +void mirrorSurfaceMiddleX(SDL_Surface *surface) { + SDL_LockSurface(surface); + int upto = surface->h / 2; + + for(int i=0; iw; x++) { + swapPixel(surface, x, i, x, surface->h-1-i); + } + } + SDL_UnlockSurface(surface); +} diff --git a/sdlfuncs.h b/sdlfuncs.h new file mode 100644 index 0000000..3cf394e --- /dev/null +++ b/sdlfuncs.h @@ -0,0 +1,12 @@ +#ifndef __SDLFUNCS_H +#define __SDLFUNCS_H + +#include +#include + +Uint32 getPixel(SDL_Surface *surface, int x, int y); +void setPixel(SDL_Surface *surface, int x, int y, Uint32 pixel); +void swapPixel(SDL_Surface *surface, int x1, int y1, int x2, int y2); +void mirrorSurfaceMiddleX(SDL_Surface *surface); + +#endif