From 541c00c93fcd98f766cce661aa83ef4ffe713e57 Mon Sep 17 00:00:00 2001 From: pixel Date: Fri, 28 Mar 2003 12:30:26 +0000 Subject: First part of the backend separation --- include/Makefile.am | 4 +- include/base.h | 2 +- include/engine.h | 4 +- include/font.h | 12 +++-- include/gettext.h | 3 ++ include/glbase.h | 2 +- include/glcolor.h | 17 ------- include/glfont.h | 11 ++--- include/glshape.h | 16 +++--- include/glsprite.h | 30 ++---------- include/gltexture.h | 28 ----------- include/glwidgets.h | 2 +- include/mcolor.h | 1 + include/sprite.h | 6 +++ include/texture.h | 18 +++++-- lib/Makefile.am | 2 +- lib/base.cc | 28 +++++------ lib/engine.cc | 5 +- lib/font.cc | 7 ++- lib/glcolor.cc | 13 ----- lib/glfont.cc | 10 +--- lib/glshape.cc | 21 +++++--- lib/glsprite.cc | 137 ++++++---------------------------------------------- lib/gltexture.cc | 120 --------------------------------------------- lib/glwidgets.cc | 4 +- lib/mcolor.cc | 6 +++ lib/shape.cc | 35 ++++++++------ lib/sprite.cc | 22 +++++++++ lib/texture.cc | 111 ++++++++++++++++++++++++++++++++++++++++-- src/test.cc | 51 +++++++++---------- 30 files changed, 282 insertions(+), 446 deletions(-) delete mode 100644 include/glcolor.h delete mode 100644 include/gltexture.h delete mode 100644 lib/glcolor.cc delete mode 100644 lib/gltexture.cc diff --git a/include/Makefile.am b/include/Makefile.am index 948adba..60c38b6 100644 --- a/include/Makefile.am +++ b/include/Makefile.am @@ -1,5 +1,5 @@ pkginclude_HEADERS = \ -engine.h glbase.h glcolor.h glfont.h gltexture.h glshape.h glwidgets.h sprite.h \ -base.h font.h shape.h mcolor.h +engine.h glbase.h glfont.h glshape.h glwidgets.h sprite.h \ +base.h font.h shape.h mcolor.h glsprite.h noinst_HEADERS = gettext.h diff --git a/include/base.h b/include/base.h index 65326bc..4e4cd5a 100644 --- a/include/base.h +++ b/include/base.h @@ -2,7 +2,6 @@ #define __BASE_H__ #include -#include #include namespace mogltk { @@ -13,6 +12,7 @@ namespace mogltk { int GetWidth(void); int GetHeight(void); virtual void Flip(void); + SDL_Surface * getsurface(); protected: base(int, int, int, int); void setsurface(SDL_Surface *) throw (GeneralException); diff --git a/include/engine.h b/include/engine.h index a72e7b1..66f2f3c 100644 --- a/include/engine.h +++ b/include/engine.h @@ -4,8 +4,8 @@ #include #include #include -#include "base.h" -#include "glbase.h" +#include +#include namespace mogltk { class engine : public Base { diff --git a/include/font.h b/include/font.h index fcc2e03..3377ef0 100644 --- a/include/font.h +++ b/include/font.h @@ -5,8 +5,8 @@ #include #include #include -#include -#include +#include +#include namespace mogltk { class font : public Base { @@ -31,12 +31,14 @@ namespace mogltk { Uint8 * sizes; Uint16 nbentries, nbcT, nbT; Uint8 flags, maxX, maxY, nbcU, nbcV, base, inter; - texture ** fonttex; - Uint16 * corresp; int cx, cy, ox; ColorP textcolor; int shadow, wspace; - virtual texture * alloctexture(); + texture * alloctexture(); + void Bind(int); + private: + texture ** fonttex; + Uint16 * corresp; }; extern font * SystemFont; }; diff --git a/include/gettext.h b/include/gettext.h index 8b262f4..248bf2e 100644 --- a/include/gettext.h +++ b/include/gettext.h @@ -66,4 +66,7 @@ initializer for static 'char[]' or 'const char[]' variables. */ #define gettext_noop(String) String +#define _(Text) dgettext ("Baltisot", Text) +#define N_(Text) Text + #endif /* _LIBGETTEXT_H */ diff --git a/include/glbase.h b/include/glbase.h index 90732ef..7fa3b06 100644 --- a/include/glbase.h +++ b/include/glbase.h @@ -4,7 +4,7 @@ #include #include #include -#include "base.h" +#include namespace mogltk { class glbase : public base { diff --git a/include/glcolor.h b/include/glcolor.h deleted file mode 100644 index f62334b..0000000 --- a/include/glcolor.h +++ /dev/null @@ -1,17 +0,0 @@ -#ifndef __GLCOLOR_H__ -#define __GLCOLOR_H__ - -#include -#include -#include "mcolor.h" - -namespace mogltk { - class glColorP : public ColorP { - public: - glColorP(const Color &); - glColorP(Uint8, Uint8, Uint8, Uint8); - void Bind(); - }; -}; - -#endif diff --git a/include/glfont.h b/include/glfont.h index 1931467..2076675 100644 --- a/include/glfont.h +++ b/include/glfont.h @@ -2,20 +2,15 @@ #define __GLFONT_H__ #include -#include "font.h" +#include +#include namespace mogltk { class glfont : public font { public: glfont(Handle *); virtual ~glfont(); - void drawentry(Uint16, int, int, glColorP = WHITE); - - protected: - virtual gltexture * alloctexture(); - - private: - void Bind(int); + void drawentry(Uint16, int, int, ColorP = WHITE); }; }; diff --git a/include/glshape.h b/include/glshape.h index 465f60b..bca4410 100644 --- a/include/glshape.h +++ b/include/glshape.h @@ -3,18 +3,20 @@ #include #include -#include -#include +#include +#include #include namespace mogltk { class glshape : public shape { public: - virtual void box(int x1, int y1, int x2, int y2, glColorP = WHITE); - virtual void box3d(int x1, int y1, int x2, int y2, glColorP = DOS_WHITE, glColorP = DOS_HIGH_WHITE, glColorP = DOS_GRAY, int = 2, bool = false); - virtual void box(int x1, int y1, int x2, int y2, glColorP, glColorP, glColorP, glColorP); - virtual void tbox(gltexture *, int x1, int y1, int x2, int y2, int tx1, int ty1, int tx2, int ty2, glColorP = WHITE); - virtual void tbox(gltexture *, int x1, int y1, int x2, int y2, glColorP, glColorP, glColorP, glColorP, int tx1, int ty1, int tx2, int ty2); + virtual void box(int x1, int y1, int x2, int y2, ColorP = WHITE); + virtual void box3d(int x1, int y1, int x2, int y2, ColorP = DOS_WHITE, ColorP = DOS_HIGH_WHITE, ColorP = DOS_GRAY, int = 2, bool = false); + virtual void box(int x1, int y1, int x2, int y2, ColorP, ColorP, ColorP, ColorP); + virtual void tbox(texture *, int x1, int y1, int x2, int y2, int tx = 0, int ty = 0, double = 1.0, ColorP = WHITE); + virtual void tbox(texture *, int x1, int y1, int x2, int y2, int tx1, int ty1, int tx2, int ty2, ColorP = WHITE); + virtual void tbox(texture *, int x1, int y1, int x2, int y2, ColorP, ColorP, ColorP, ColorP, int tx = 0, int ty = 0, double = 1.0); + virtual void tbox(texture *, int x1, int y1, int x2, int y2, ColorP, ColorP, ColorP, ColorP, int tx1, int ty1, int tx2, int ty2); private: bool Enter(bool); virtual bool Enter(); diff --git a/include/glsprite.h b/include/glsprite.h index 3f481c5..a7b2bfd 100644 --- a/include/glsprite.h +++ b/include/glsprite.h @@ -4,9 +4,9 @@ #include #include #include -#include -#include "gltexture.h" -#include "sprite.h" +#include +#include +#include namespace mogltk { class glSprite : public Sprite { @@ -15,31 +15,7 @@ namespace mogltk { glSprite(Uint8 *, int, int); virtual ~glSprite(); virtual void draw(int, int, ColorP = WHITE); - private: - class TexList : public Base { - public: - TexList(int); - virtual ~TexList(); - glSprite * sprheader; - const texture * GetTex() const; - texture * GetTex(); - static const TexList * GetHead(); - const TexList * GetNext() const; - TexList * GetNext(); - void Bind() const; - private: - texture * tex; - static TexList * header; - TexList * next, * prev; - }; - glSprite * next, * prev; - TexList * tlist; - int sx, sy, posx, posy; - void alloc(); - bool canfit(int, int, int, int) const; - bool intersect(int, int, int, int) const; }; - }; #endif diff --git a/include/gltexture.h b/include/gltexture.h deleted file mode 100644 index a5137db..0000000 --- a/include/gltexture.h +++ /dev/null @@ -1,28 +0,0 @@ -#ifndef __GLTEXTURE_H__ -#define __GLTEXTURE_H__ - -#include -#include -#include -#include -#include -#include - -namespace mogltk { - class gltexture : public texture { - public: - gltexture(int = 256, int = 256, bool = false) throw (GeneralException); - gltexture(Handle *, bool = false) throw (GeneralException); - virtual ~gltexture(); - void Generate(); - void Taint(); - void Bind(bool = true); - static void Unbind(void); - private: - GLuint tex; - bool texture_allocated, planar, tainted; - static gltexture * active; - }; -}; - -#endif diff --git a/include/glwidgets.h b/include/glwidgets.h index 0ee9cea..86688ef 100644 --- a/include/glwidgets.h +++ b/include/glwidgets.h @@ -1,7 +1,7 @@ #ifndef __GLWIDGETS_H__ #define __GLWIDGETS_H__ -#include "Exceptions.h" +#include namespace mogltk { class widget : public Base { diff --git a/include/mcolor.h b/include/mcolor.h index cd91923..bbad479 100644 --- a/include/mcolor.h +++ b/include/mcolor.h @@ -9,6 +9,7 @@ namespace mogltk { public: ColorP(const Color &); ColorP(Uint8, Uint8, Uint8, Uint8); + void Bind(); static Color Min; static Color Max; Color c; diff --git a/include/sprite.h b/include/sprite.h index aed711e..dcc4dda 100644 --- a/include/sprite.h +++ b/include/sprite.h @@ -14,6 +14,12 @@ namespace mogltk { Sprite(Uint8 *, int, int); virtual ~Sprite(); void draw(int, int, ColorP = WHITE); + protected: + void Bind(); + int GetSX(); + int GetSY(); + int GetPX(); + int GetPY(); private: class TexList : public Base { public: diff --git a/include/texture.h b/include/texture.h index 904eeae..4f14ba4 100644 --- a/include/texture.h +++ b/include/texture.h @@ -2,6 +2,7 @@ #define __TEXTURE_H__ #include +#include #include #include #include @@ -9,22 +10,29 @@ namespace mogltk { class texture : public Base { public: - texture(int = 256, int = 256) throw (GeneralException); - texture(Handle *) throw (GeneralException); + texture(int = 256, int = 256, bool = false) throw (GeneralException); + texture(Handle *, bool = false) throw (GeneralException); virtual ~texture(); SDL_Surface * GetSurface(); Uint32 * GetPixels(); static SDL_Surface * LoadNTEX(Handle * h) throw (GeneralException) ; - Uint32 GetWidth(); - Uint32 GetHeight(); + void Generate(); + void Bind(bool = true); + GLuint GetWidth(); + GLuint GetHeight(); + static void Unbind(void); + void Taint(void); private: - Uint32 width, height; + GLuint width, height, tex; + bool texture_allocated; SDL_Surface * surface; + bool planar, tainted; #ifdef TRACE_TEXTURES static texture * header; static texture * footer; texture * next, * prev; #endif + static texture * active; }; }; diff --git a/lib/Makefile.am b/lib/Makefile.am index c1bc885..f7fd3be 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -5,5 +5,5 @@ LIBS = @SDL_LIBS@ @BALTISOT_LIBS@ INCLUDES = -I.. -I../include -I$(includedir) lib_LTLIBRARIES = libmogltk.la -libmogltk_la_SOURCES = engine.cc glbase.cc glcolor.cc glfont.cc gltexture.cc \ +libmogltk_la_SOURCES = engine.cc glbase.cc glfont.cc glsprite.cc \ glshape.cc glwidgets.cc sprite.cc base.cc font.cc shape.cc mcolor.cc texture.cc diff --git a/lib/base.cc b/lib/base.cc index 049f53c..8741978 100644 --- a/lib/base.cc +++ b/lib/base.cc @@ -8,12 +8,7 @@ #endif #include "gettext.h" -int mogltk::base::setup(int w, int h, int flags) : surface(0) throw(GeneralException) { - if (inited) { - printm(M_WARNING, "mogltk::base::setup called twice, ignoring second call...\n"); - return -1; - } - +mogltk::base::base(int w, int h, int flags) throw(GeneralException) : surface(0) { width = w; height = h; @@ -33,15 +28,16 @@ int mogltk::base::setup(int w, int h, int flags) : surface(0) throw(GeneralExcep printm(M_INFO, "Video resolution: %dx%dx%d (ratio = %3.2f)\n", surface->w, surface->h, surface->format->BitsPerPixel, ratio); - inited = 1; - SDL_ShowCursor(0); SDL_FillRect(surface, NULL, 0); - SDL_SwapBuffers(); + SDL_Flip(surface); SDL_FillRect(surface, NULL, 0); mogltk::engine::postsetup(); } +mogltk::base::~base() { +} + int mogltk::base::GetWidth(void) { return width; } @@ -50,23 +46,17 @@ int mogltk::base::GetHeight(void) { return height; } -int mogltk::base::GetInited(void) { - return inited; -} - void mogltk::base::Flip() { printm(M_INFO, "Flipping\n"); mogltk::engine::pollevents(); - SDL_SwapBuffers(); + SDL_Flip(surface); SDL_FillRect(surface, NULL, 0); } -int mogltk::base::setup(int w, int h, int flags) : surface(0) { +mogltk::base::base(int w, int h, int flags, int) : surface(0) { width = w; height = h; - inited = 1; - SDL_ShowCursor(0); } @@ -75,3 +65,7 @@ void mogltk::base::setsurface(SDL_Surface * _surface) throw (GeneralException) { throw GeneralException("Can't set video surface twice"); surface = _surface; } + +SDL_Surface * mogltk::base::getsurface() { + return surface; +} diff --git a/lib/engine.cc b/lib/engine.cc index 99843c0..036aa25 100644 --- a/lib/engine.cc +++ b/lib/engine.cc @@ -6,10 +6,7 @@ #ifdef HAVE_CONFIG_H #include "config.h" #endif - -#ifndef _ -#define _(x) x -#endif +#include "gettext.h" bool mogltk::engine::inited = false, mogltk::engine::postsetuped = false; bool mogltk::engine::appactive = false, mogltk::engine::cursorvisible = false, mogltk::engine::quitrequest = false; diff --git a/lib/font.cc b/lib/font.cc index 204e60d..9e77ae2 100644 --- a/lib/font.cc +++ b/lib/font.cc @@ -167,7 +167,7 @@ void mogltk::font::drawentry(Uint16 entry, int x, int y, ColorP c) { int trueentry, cx, cy, px, py; return; - +#if 0 was2D = mogltk::glbase::is2D(); if (!was2D) @@ -201,6 +201,7 @@ void mogltk::font::drawentry(Uint16 entry, int x, int y, ColorP c) { if (!was2D) mogltk::glbase::Leave2DMode(); +#endif } void mogltk::font::putcursor(int x, int y) { @@ -318,3 +319,7 @@ mogltk::font * mogltk::SystemFont; mogltk::texture * mogltk::font::alloctexture() { return new mogltk::texture(256, 256); } + +void mogltk::font::Bind(int f) { + fonttex[f]->Bind(); +} diff --git a/lib/glcolor.cc b/lib/glcolor.cc deleted file mode 100644 index c38b4e5..0000000 --- a/lib/glcolor.cc +++ /dev/null @@ -1,13 +0,0 @@ -#include -#include "glcolor.h" - -mogltk::glColorP::glColorP(const Color & ac) : ColorP(ac) { -} - -mogltk::glColorP::glColorP(Uint8 ar, Uint8 ag, Uint8 ab, Uint8 aa) : ColorP(ar, ag, ab, aa) { -} - -void mogltk::glColorP::Bind() { - glColor4d((double) MIN(MAX(c.R, Min.R), Max.R) / 255, (double) MIN(MAX(c.G, Min.G), Max.G) / 255, (double) MIN(MAX(c.B, Min.B), Max.B) / 255, (double) MIN(MAX(c.A, Min.A), Max.A) / 255); -} - diff --git a/lib/glfont.cc b/lib/glfont.cc index 38f5c78..cd2fca3 100644 --- a/lib/glfont.cc +++ b/lib/glfont.cc @@ -11,7 +11,7 @@ mogltk::glfont::glfont(Handle * ffont) : font(ffont) { mogltk::glfont::~glfont() { } -void mogltk::glfont::drawentry(Uint16 entry, int x, int y, glColorP c) { +void mogltk::glfont::drawentry(Uint16 entry, int x, int y, ColorP c) { bool was2D; int trueentry, cx, cy, px, py; @@ -49,11 +49,3 @@ void mogltk::glfont::drawentry(Uint16 entry, int x, int y, glColorP c) { if (!was2D) mogltk::engine::glbase_o->Leave2DMode(); } - -void mogltk::glfont::Bind(int index) { - ((mogltk::gltexture *) fonttex[index])->Bind(); -} - -mogltk::gltexture * mogltk::glfont::alloctexture() { - return new mogltk::gltexture(256, 256, true); -} diff --git a/lib/glshape.cc b/lib/glshape.cc index 831be32..c38d062 100644 --- a/lib/glshape.cc +++ b/lib/glshape.cc @@ -1,7 +1,7 @@ #include #include "glbase.h" #include "glshape.h" -#include "gltexture.h" +#include "texture.h" #include "glfont.h" #include "engine.h" @@ -9,7 +9,7 @@ #define ENTERT bool was2d = Enter(false) #define LEAVE Leave(was2d) -void mogltk::glshape::box(int x1, int y1, int x2, int y2, glColorP c) { +void mogltk::glshape::box(int x1, int y1, int x2, int y2, ColorP c) { ENTER; c.Bind(); @@ -23,7 +23,7 @@ void mogltk::glshape::box(int x1, int y1, int x2, int y2, glColorP c) { LEAVE; } -void mogltk::glshape::box(int x1, int y1, int x2, int y2, glColorP c1, glColorP c2, glColorP c3, glColorP c4) { +void mogltk::glshape::box(int x1, int y1, int x2, int y2, ColorP c1, ColorP c2, ColorP c3, ColorP c4) { ENTER; glBegin(GL_TRIANGLE_STRIP); @@ -36,7 +36,10 @@ void mogltk::glshape::box(int x1, int y1, int x2, int y2, glColorP c1, glColorP LEAVE; } -void mogltk::glshape::tbox(mogltk::gltexture * t, int x1, int y1, int x2, int y2, int tx1, int ty1, int tx2, int ty2, glColorP c) { +void mogltk::glshape::tbox(mogltk::texture * t, int x1, int y1, int x2, int y2, int tx, int ty, double f, ColorP c) { + shape::tbox(t, x1, y1, x2, y2, tx, ty, f, c); +} +void mogltk::glshape::tbox(mogltk::texture * t, int x1, int y1, int x2, int y2, int tx1, int ty1, int tx2, int ty2, ColorP c) { ENTERT; c.Bind(); @@ -51,7 +54,11 @@ void mogltk::glshape::tbox(mogltk::gltexture * t, int x1, int y1, int x2, int y2 LEAVE; } -void mogltk::glshape::tbox(mogltk::gltexture * t, int x1, int y1, int x2, int y2, glColorP c1, glColorP c2, glColorP c3, glColorP c4, int tx1, int ty1, int tx2, int ty2) { +void mogltk::glshape::tbox(mogltk::texture * t, int x1, int y1, int x2, int y2, ColorP c1, ColorP c2, ColorP c3, ColorP c4, int tx, int ty, double f) { + shape::tbox(t, x1, y1, x2, y2, c1, c2, c3, c4, tx, ty, f); +} + +void mogltk::glshape::tbox(mogltk::texture * t, int x1, int y1, int x2, int y2, ColorP c1, ColorP c2, ColorP c3, ColorP c4, int tx1, int ty1, int tx2, int ty2) { ENTERT; t->Bind(); @@ -65,7 +72,7 @@ void mogltk::glshape::tbox(mogltk::gltexture * t, int x1, int y1, int x2, int y2 LEAVE; } -void mogltk::glshape::box3d(int x1, int y1, int x2, int y2, glColorP face, glColorP shade1, glColorP shade2, int depth, bool bevel) { +void mogltk::glshape::box3d(int x1, int y1, int x2, int y2, ColorP face, ColorP shade1, ColorP shade2, int depth, bool bevel) { ENTER; if (!bevel) { @@ -111,7 +118,7 @@ bool mogltk::glshape::Enter(bool unbind) { if (!was2D) mogltk::engine::glbase_o->Enter2DMode(); - if (unbind) mogltk::gltexture::Unbind(); + if (unbind) mogltk::texture::Unbind(); return was2D; } diff --git a/lib/glsprite.cc b/lib/glsprite.cc index 282eefe..68350c9 100644 --- a/lib/glsprite.cc +++ b/lib/glsprite.cc @@ -1,146 +1,37 @@ #include #include "glbase.h" -#include "sprite.h" +#include "glsprite.h" +#include "engine.h" #define TEXSIZE 256 -mogltk::Sprite::TexList * mogltk::Sprite::TexList::header = 0; - -mogltk::Sprite::Sprite(Handle * h, int asx, int asy) : sx(asx), sy(asy) { - alloc(); - for (int y = 0; y < sy; y++) { - h->read(tlist->GetTex()->GetPixels() + TEXSIZE * y + posx, sx * 4); - } -} - -mogltk::Sprite::Sprite(Uint8 * p, int asx, int asy) : sx(asx), sy(asy) { - alloc(); - for (int y = 0; y < sy; y++) { - bcopy(p, tlist->GetTex()->GetPixels() + TEXSIZE * y + posx, sx * 4); - p += sx * 4; - } -} - -mogltk::Sprite::~Sprite() { - if (prev) - prev->next = next; - else - tlist->sprheader = next; - - if (next) - next->prev = prev; - - if (!tlist->sprheader) { - delete tlist; - } -} - -void mogltk::Sprite::alloc() { - /* FIXMEEEEEEEEEEEEE */ - bool found = false; - TexList * p; - - for (p = tlist; p && !found; p = p->GetNext()) { - for (posy = 0; (posy < (TEXSIZE - sy)) && !found; posy++) { - for (posx = 0; (posx < (TEXSIZE - sx)) && !found; posx++) { - if (p->sprheader->canfit(posx, posy, posx + sx, posy + sy)) { - found = true; - } - } - } - } - - if (!found) { -#ifdef DEBUG - printm(M_INFO, "Allocating a new texture for sprites\n"); -#endif - posx = posy = 0; - p = new TexList(TEXSIZE); - } - - tlist = p; -} - -mogltk::Sprite::TexList::TexList(int size) { - tex = new texture(size, size, true); - - if (header) - header->next->prev = this; - prev = 0; - next = header; - header = this; -} - -mogltk::Sprite::TexList::~TexList() { - delete tex; - - if (prev) - prev->next = next; - else - header = next; - - if (next) - next->prev = prev; -} - -const mogltk::texture * mogltk::Sprite::TexList::GetTex() const { - return tex; -} - -mogltk::texture * mogltk::Sprite::TexList::GetTex() { - return tex; -} - -const mogltk::Sprite::TexList * mogltk::Sprite::TexList::GetHead() { - return header; -} - -const mogltk::Sprite::TexList * mogltk::Sprite::TexList::GetNext() const { - return next; -} - -mogltk::Sprite::TexList * mogltk::Sprite::TexList::GetNext() { - return next; -} - -void mogltk::Sprite::TexList::Bind() const { - tex->Bind(); +mogltk::glSprite::glSprite(Handle * h, int asx, int asy) : Sprite(h, asx, asy) { } -bool mogltk::Sprite::intersect(int x1, int y1, int x2, int y2) const { - int sx1 = posx, sy1 = posy, sx2 = posx + sx, sy2 = posy + sy; - - return !((sx1 >= x2) || (sx2 <= x1) || (sy1 >= y1) || (sy2 <= y2)); +mogltk::glSprite::glSprite(Uint8 * p, int asx, int asy) : Sprite(p, asx, asy) { } -bool mogltk::Sprite::canfit(int x1, int y1, int x2, int y2) const { - if (!intersect(x1, y1, x2, y2)) - return true; - else - if (next) - return next->canfit(x1, y1, x2, y2); - else - return false; +mogltk::glSprite::~glSprite() { } -void mogltk::Sprite::draw(int dx, int dy, ColorP c) { +void mogltk::glSprite::draw(int dx, int dy, ColorP c) { bool was2D; - was2D = mogltk::glbase::is2D(); + was2D = mogltk::engine::glbase_o->is2D(); if (!was2D) - mogltk::glbase::Enter2DMode(); + mogltk::engine::glbase_o->Enter2DMode(); c.Bind(); - tlist->Bind(); + Bind(); glBegin(GL_TRIANGLE_STRIP); - glTexCoord2i(posx , posy ); glVertex2i(dx , dy ); - glTexCoord2i(posx + sx - 1, posy ); glVertex2i(dx + sx - 1, dy ); - glTexCoord2i(posx , posy + sy - 1); glVertex2i(dx , dy + sy - 1); - glTexCoord2i(posx + sx - 1, posy + sy - 1); glVertex2i(dx + sx - 1, dy + sy - 1); + glTexCoord2i(GetPX() , GetPY() ); glVertex2i(dx , dy ); + glTexCoord2i(GetPX() + GetSX() - 1, GetPY() ); glVertex2i(dx + GetSX() - 1, dy ); + glTexCoord2i(GetPX() , GetPY() + GetSY() - 1); glVertex2i(dx , dy + GetSY() - 1); + glTexCoord2i(GetPX() + GetSX() - 1, GetPY() + GetSY() - 1); glVertex2i(dx + GetSX() - 1, dy + GetSY() - 1); glEnd(); if (!was2D) - mogltk::glbase::Leave2DMode(); + mogltk::engine::glbase_o->Leave2DMode(); } diff --git a/lib/gltexture.cc b/lib/gltexture.cc deleted file mode 100644 index ff79112..0000000 --- a/lib/gltexture.cc +++ /dev/null @@ -1,120 +0,0 @@ -#include -#include -#include -#include -#include "gltexture.h" -#include "engine.h" -#ifdef HAVE_CONFIG_H -#include "config.h" -#endif -#include "gettext.h" - -mogltk::gltexture * mogltk::gltexture::active = 0; - -mogltk::gltexture::gltexture(int w, int h, bool plane) throw (GeneralException) : - texture(w, h), texture_allocated(false), planar(plane), tainted(true) { -} - -mogltk::gltexture::gltexture(Handle * h, bool plane) throw (GeneralException) : - texture(h), texture_allocated(false), planar(plane), tainted(true) { -} - -mogltk::gltexture::~gltexture() { - if (texture_allocated) { - glDeleteTextures(1, &tex); - } -} - -void mogltk::gltexture::Generate() { - if (texture_allocated) { - glDeleteTextures(1, &tex); - } - - glGenTextures(1, &tex); -#ifdef DEBUG - printm(M_INFO, _("Generated gltexture index: %i\n"), tex); -#endif - - glBindTexture(GL_TEXTURE_2D, tex); - -#if 0 - if (planar) { -#ifdef DEBUG - printm(M_INFO, _("Generating planar gltexture: %i\n"), tex); -#endif - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, surface->pixels); - } else { -#endif -#ifdef DEBUG - printm(M_INFO, _("Generating 3D gltexture: %i\n"), tex); -#endif - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); - gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA, GetWidth(), GetHeight(), GL_RGBA, GL_UNSIGNED_BYTE, GetPixels()); -// glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, surface->pixels); -#if 0 - } -#endif - - texture_allocated = true; - tainted = false; -} - -void mogltk::gltexture::Bind(bool expand) { - if ((!texture_allocated) || tainted) - Generate(); - glEnable(GL_TEXTURE_2D); - if (active == this) - return; -#ifdef DEBUG - printm(M_INFO, _("Binding gltexture index %i.\n"), tex); -#endif - glBindTexture(GL_TEXTURE_2D, tex); - if (expand) { - glMatrixMode(GL_TEXTURE); - glLoadIdentity(); - glScaled(1 / (double) GetWidth(), 1 / (double) GetHeight(), 1); - glMatrixMode(GL_MODELVIEW); - } - - active = this; - -#ifdef TRACE_TEXTURES - if (header == this) - return; - - if (prev) { - prev->next = next; - } - - if (next) { - next->prev = prev; - } - - if (footer = this) { - footer = prev; - } - - header->prev = this; - header = this; -#endif -} - -void mogltk::gltexture::Unbind(void) { - if (active) { - glBindTexture(GL_TEXTURE_2D, 0); - glDisable(GL_TEXTURE_2D); - active = 0; -#ifdef DEBUG - printm(M_INFO, _("Unbinding gltexture.\n")); -#endif - } -} - -void mogltk::gltexture::Taint(void) { - tainted = true; -} diff --git a/lib/glwidgets.cc b/lib/glwidgets.cc index 0ac5f8b..f8e0389 100644 --- a/lib/glwidgets.cc +++ b/lib/glwidgets.cc @@ -7,9 +7,7 @@ #include "config.h" #endif -#ifndef _ -#define _(x) x -#endif +#include "gettext.h" mogltk::widget * mogltk::widget::cur_root = 0; diff --git a/lib/mcolor.cc b/lib/mcolor.cc index 91c8a1d..04e0467 100644 --- a/lib/mcolor.cc +++ b/lib/mcolor.cc @@ -1,4 +1,5 @@ #include +#include #include "mcolor.h" Color mogltk::ColorP::Min(0, 0, 0, 0), mogltk::ColorP::Max = WHITE; @@ -8,3 +9,8 @@ mogltk::ColorP::ColorP(const Color & ac) : c(ac) { mogltk::ColorP::ColorP(Uint8 ar, Uint8 ag, Uint8 ab, Uint8 aa) : c(ar, ag, ab, aa) { } + +void mogltk::ColorP::Bind() { + glColor4d((double) MIN(MAX(c.R, Min.R), Max.R) / 255, (double) MIN(MAX(c.G, Min.G), Max.G) / 255, (double) MIN(MAX(c.B, Min.B), Max.B) / 255, (double) MIN(MAX(c.A, Min.A), Max.A) / 255); +} + diff --git a/lib/shape.cc b/lib/shape.cc index 088deb7..9386e41 100644 --- a/lib/shape.cc +++ b/lib/shape.cc @@ -1,4 +1,5 @@ #include +#include "engine.h" #include "base.h" #include "shape.h" #include "texture.h" @@ -8,6 +9,7 @@ #define LEAVE Leave(flag) void mogltk::shape::box(int x1, int y1, int x2, int y2, ColorP c) { +#if 0 ENTER; c.Bind(); @@ -19,9 +21,11 @@ void mogltk::shape::box(int x1, int y1, int x2, int y2, ColorP c) { glEnd(); LEAVE; +#endif } void mogltk::shape::box(int x1, int y1, int x2, int y2, ColorP c1, ColorP c2, ColorP c3, ColorP c4) { +#if 0 ENTER; glBegin(GL_TRIANGLE_STRIP); @@ -32,6 +36,7 @@ void mogltk::shape::box(int x1, int y1, int x2, int y2, ColorP c1, ColorP c2, Co glEnd(); LEAVE; +#endif } void mogltk::shape::hline(int x1, int x2, int y, ColorP c) { @@ -113,6 +118,7 @@ void mogltk::shape::tbox(mogltk::texture * t, int x1, int y1, int x2, int y2, Co } void mogltk::shape::tbox(mogltk::texture * t, int x1, int y1, int x2, int y2, int tx1, int ty1, int tx2, int ty2, ColorP c) { +#if 0 ENTERT; c.Bind(); @@ -125,9 +131,11 @@ void mogltk::shape::tbox(mogltk::texture * t, int x1, int y1, int x2, int y2, in glEnd(); LEAVE; +#endif } void mogltk::shape::tbox(mogltk::texture * t, int x1, int y1, int x2, int y2, ColorP c1, ColorP c2, ColorP c3, ColorP c4, int tx1, int ty1, int tx2, int ty2) { +#if 0 ENTERT; t->Bind(); @@ -139,9 +147,11 @@ void mogltk::shape::tbox(mogltk::texture * t, int x1, int y1, int x2, int y2, Co glEnd(); LEAVE; +#endif } void mogltk::shape::box3d(int x1, int y1, int x2, int y2, ColorP face, ColorP shade1, ColorP shade2, int depth, bool bevel) { +#if 0 ENTER; if (!bevel) { @@ -179,6 +189,7 @@ void mogltk::shape::box3d(int x1, int y1, int x2, int y2, ColorP face, ColorP sh glEnd(); LEAVE; +#endif } void mogltk::shape::obox3d(int x1, int y1, int x2, int y2, ColorP shade1, ColorP shade2, bool bevel) { @@ -266,22 +277,16 @@ void mogltk::shape::button(int x1, int y1, int x2, int y2, LEAVE; } -bool mogltk::shape::Enter(bool unbind) { - bool was2D = mogltk::glbase::is2D(); - - if (!was2D) - mogltk::glbase::Enter2DMode(); - - if (unbind) mogltk::texture::Unbind(); - - return was2D; -} - bool mogltk::shape::Enter() { - return Enter(false); + if (SDL_MUSTLOCK(mogltk::engine::base_o->getsurface())) { + SDL_LockSurface(mogltk::engine::base_o->getsurface()); + return true; + } else { + return false; + } } -void mogltk::shape::Leave(bool was2D) { - if (!was2D) - mogltk::glbase::Leave2DMode(); +void mogltk::shape::Leave(bool locked) { + if (locked) + SDL_UnlockSurface(mogltk::engine::base_o->getsurface()); } diff --git a/lib/sprite.cc b/lib/sprite.cc index 282eefe..60f944c 100644 --- a/lib/sprite.cc +++ b/lib/sprite.cc @@ -124,6 +124,7 @@ bool mogltk::Sprite::canfit(int x1, int y1, int x2, int y2) const { } void mogltk::Sprite::draw(int dx, int dy, ColorP c) { +#if 0 bool was2D; was2D = mogltk::glbase::is2D(); @@ -143,4 +144,25 @@ void mogltk::Sprite::draw(int dx, int dy, ColorP c) { if (!was2D) mogltk::glbase::Leave2DMode(); +#endif +} + +void mogltk::Sprite::Bind() { + tlist->Bind(); +} + +int mogltk::Sprite::GetPX() { + return posx; +} + +int mogltk::Sprite::GetPY() { + return posy; +} + +int mogltk::Sprite::GetSX() { + return sx; +} + +int mogltk::Sprite::GetSY() { + return sy; } diff --git a/lib/texture.cc b/lib/texture.cc index 36297ac..80853ca 100644 --- a/lib/texture.cc +++ b/lib/texture.cc @@ -1,5 +1,6 @@ #include #include +#include #include #include "texture.h" #include "engine.h" @@ -8,6 +9,8 @@ #endif #include "gettext.h" +#define DEBUG 1 + #ifdef TRACE_TEXTURES mogltk::texture * mogltk::texture::header = 0; mogltk::texture * mogltk::texture::footer = 0; @@ -15,7 +18,7 @@ mogltk::texture * mogltk::texture::footer = 0; mogltk::texture * mogltk::texture::active = 0; -mogltk::texture::texture(int w, int h) throw (GeneralException) : width(w), height(h), +mogltk::texture::texture(int w, int h, bool plane) throw (GeneralException) : width(w), height(h), texture_allocated(false), planar(plane), tainted(true) { if ((!ISPOT(w)) || (!ISPOT(h))) throw GeneralException(_("Size of the texture not a power of 2!")); @@ -49,7 +52,9 @@ mogltk::texture::texture(int w, int h) throw (GeneralException) : width(w), heig #endif } -mogltk::texture::texture(Handle * h) throw (GeneralException) { +mogltk::texture::texture(Handle * h, bool plane) throw (GeneralException) : + texture_allocated(false), planar(plane), tainted(true) { + SDL_Surface * temp; temp = LoadNTEX(h); @@ -118,6 +123,10 @@ mogltk::texture::~texture() { SDL_FreeSurface(surface); } + if (texture_allocated) { + glDeleteTextures(1, &tex); + } + #ifdef TRACE_TEXTURES if (prev) { prev->next = next; @@ -145,14 +154,108 @@ SDL_Surface * mogltk::texture::GetSurface() { return surface; } -Uint32 mogltk::texture::GetWidth() { +void mogltk::texture::Generate() { + if (texture_allocated) { + glDeleteTextures(1, &tex); + } + + glGenTextures(1, &tex); +#ifdef DEBUG + printm(M_INFO, _("Generated texture index: %i\n"), tex); +#endif + + glBindTexture(GL_TEXTURE_2D, tex); + +#if 0 + if (planar) { +#ifdef DEBUG + printm(M_INFO, _("Generating planar texture: %i\n"), tex); +#endif + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, surface->pixels); + } else { +#endif +#ifdef DEBUG + printm(M_INFO, _("Generating 3D texture: %i\n"), tex); +#endif + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); + gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA, width, height, GL_RGBA, GL_UNSIGNED_BYTE, surface->pixels); +// glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, surface->pixels); +#if 0 + } +#endif + + texture_allocated = true; + tainted = false; +} + +void mogltk::texture::Bind(bool expand) { + if ((!texture_allocated) || tainted) + Generate(); + glEnable(GL_TEXTURE_2D); + if (active == this) + return; +#ifdef DEBUG + printm(M_INFO, _("Binding texture index %i.\n"), tex); +#endif + glBindTexture(GL_TEXTURE_2D, tex); + if (expand) { + glMatrixMode(GL_TEXTURE); + glLoadIdentity(); + glScaled(1 / (double) width, 1 / (double) height, 1); + glMatrixMode(GL_MODELVIEW); + } + + active = this; + +#ifdef TRACE_TEXTURES + if (header == this) + return; + + if (prev) { + prev->next = next; + } + + if (next) { + next->prev = prev; + } + + if (footer = this) { + footer = prev; + } + + header->prev = this; + header = this; +#endif +} + +GLuint mogltk::texture::GetWidth() { return width; } -Uint32 mogltk::texture::GetHeight() { +GLuint mogltk::texture::GetHeight() { return height; } +void mogltk::texture::Unbind(void) { + if (active) { + glBindTexture(GL_TEXTURE_2D, 0); + glDisable(GL_TEXTURE_2D); + active = 0; +#ifdef DEBUG + printm(M_INFO, _("Unbinding texture.\n")); +#endif + } +} + +void mogltk::texture::Taint(void) { + tainted = true; +} + #ifdef WORDS_BIGENDIAN #define NTEX_SIGNATURE 0x4e544558 #else diff --git a/src/test.cc b/src/test.cc index 1057033..645e033 100644 --- a/src/test.cc +++ b/src/test.cc @@ -5,11 +5,11 @@ #include #include #include "glbase.h" -#include "gltexture.h" +#include "texture.h" #include "glfont.h" #include "glwidgets.h" #include "engine.h" -#include "sprite.h" +#include "glsprite.h" #include "glshape.h" CODE_BEGINS @@ -17,9 +17,10 @@ virtual int startup() throw (GeneralException) { int sx1, sx2, sy1, sy2; double t = 0; verbosity = M_INFO; - mogltk::glbase::setup(); + mogltk::glbase * gl = new mogltk::glbase(); + mogltk::glshape * sh = new mogltk::glshape(); new Archive("datas.paq"); - mogltk::font font(&Input("font-2.bin")); + mogltk::glfont * font = new mogltk::glfont(&Input("font-2.bin")); mogltk::Sprite * s = new mogltk::Sprite(&Input("cursor.rgba"), 25, 25); mogltk::engine::setcursorvisible(true); @@ -36,41 +37,41 @@ virtual int startup() throw (GeneralException) { sy1 = 240 + 240 * sin(0.692 * t + 8.21); sy2 = 240 + 240 * sin(1.029 * t + 2.42); - mogltk::glbase::Enter2DMode(); + gl->Enter2DMode(); - mogltk::shape::tbox(mytex, 50, 50, 561, 561, BLACK, RED, LIME, BLUE); - mogltk::shape::box(400, 100, 450, 150, BLACK, RED, LIME, BLUE); + sh->tbox(mytex, 50, 50, 561, 561, BLACK, RED, LIME, BLUE); + sh->box(400, 100, 450, 150, BLACK, RED, LIME, BLUE); - mogltk::shape::box(5, 5, 150, 80, CORNFLOWERBLUE, DEEPSKYBLUE, MIDNIGHTBLUE, NAVY); - font.setshadow(1); - font.putcursor(10, 30); - font.setcolor(WHITE); - font.printf( + sh->box(5, 5, 150, 80, CORNFLOWERBLUE, DEEPSKYBLUE, MIDNIGHTBLUE, NAVY); + font->setshadow(1); + font->putcursor(10, 30); + font->setcolor(WHITE); + font->printf( "PixelPawa!\n" "It works!!\n" "I can't believe it!\n" ); - mogltk::shape::box3d(50, 150, 150, 200); - mogltk::shape::obox3d(50, 250, 150, 300); - mogltk::shape::window(50, 350, 150, 400, "Titre plus beau ;)"); - mogltk::shape::box3d(180, 130, 320, 220); - mogltk::shape::button(200, 150, 300, 200, "Bouton"); + sh->box3d(50, 150, 150, 200); + sh->obox3d(50, 250, 150, 300); + sh->window(50, 350, 150, 400, "Titre plus beau ;)"); + sh->box3d(180, 130, 320, 220); + sh->button(200, 150, 300, 200, "Bouton"); - mogltk::shape::box(MIN(sx1, sx2), MIN(sy1, sy2), MAX(sx1, sx2), MAX(sy1, sy2), AlphaBlue); + sh->box(MIN(sx1, sx2), MIN(sy1, sy2), MAX(sx1, sx2), MAX(sy1, sy2), AlphaBlue); - font.putcursor(550, 400); - font.printf("FPS: %.2f\n", mogltk::engine::FPS()); - font.printf("mx: %i\n", mogltk::engine::mouseX()); - font.printf("my: %i\n", mogltk::engine::mouseY()); - font.printf("t: %.2fs\n", (double) SDL_GetTicks() / 1000); + font->putcursor(550, 400); + font->printf("FPS: %.2f\n", mogltk::engine::FPS()); + font->printf("mx: %i\n", mogltk::engine::mouseX()); + font->printf("my: %i\n", mogltk::engine::mouseY()); + font->printf("t: %.2fs\n", (double) SDL_GetTicks() / 1000); s->draw(mogltk::engine::mouseX() - 8, mogltk::engine::mouseY() - 6); - mogltk::glbase::Leave2DMode(); + gl->Leave2DMode(); - mogltk::glbase::Flip(); + gl->Flip(); t = (double) SDL_GetTicks() / 1000; } -- cgit v1.2.3