From c00cd54ca5ed959cbccff7aa7261fb5025d1832c Mon Sep 17 00:00:00 2001 From: pixel Date: Wed, 26 Mar 2003 14:07:43 +0000 Subject: bleh --- lib/Makefile.am | 3 +- lib/base.cc | 77 ++++++++++++++ lib/engine.cc | 10 +- lib/font.cc | 318 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ lib/glbase.cc | 37 ++----- lib/glcolor.cc | 8 +- lib/glfont.cc | 281 ++----------------------------------------------- lib/glshape.cc | 187 +++------------------------------ lib/mcolor.cc | 10 ++ lib/shape.cc | 287 ++++++++++++++++++++++++++++++++++++++++++++++++++ 10 files changed, 736 insertions(+), 482 deletions(-) create mode 100644 lib/base.cc create mode 100644 lib/font.cc create mode 100644 lib/mcolor.cc create mode 100644 lib/shape.cc (limited to 'lib') diff --git a/lib/Makefile.am b/lib/Makefile.am index a13b4c3..bbb603c 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -6,5 +6,4 @@ INCLUDES = -I.. -I../include -I$(includedir) lib_LTLIBRARIES = libmogltk.la libmogltk_la_SOURCES = engine.cc glbase.cc glcolor.cc glfont.cc gltexture.cc \ -glshape.cc glwidgets.cc sprite.cc - +glshape.cc glwidgets.cc sprite.cc base.cc font.cc shape.cc mcolor.cc diff --git a/lib/base.cc b/lib/base.cc new file mode 100644 index 0000000..049f53c --- /dev/null +++ b/lib/base.cc @@ -0,0 +1,77 @@ +#include +#include +#include "base.h" +#include "engine.h" +#include "generic.h" +#ifdef HAVE_CONFIG_H +#include "config.h" +#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; + } + + width = w; + height = h; + + mogltk::engine::setup(); + if (SDL_InitSubSystem(SDL_INIT_VIDEO) < 0) { + throw GeneralException(String("Couldn't initialise Video SubSystem: ") + SDL_GetError()); + } + + if (!(surface = SDL_SetVideoMode(width, height, 0, flags))) { + throw GeneralException(String("Couldn't set GL mode: ") + SDL_GetError()); + } + + mogltk::engine::base_o = this; + + float ratio = surface->w; + ratio /= surface->h; + + 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_FillRect(surface, NULL, 0); + mogltk::engine::postsetup(); +} + +int mogltk::base::GetWidth(void) { + return width; +} + +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_FillRect(surface, NULL, 0); +} + +int mogltk::base::setup(int w, int h, int flags) : surface(0) { + width = w; + height = h; + + inited = 1; + + SDL_ShowCursor(0); +} + +void mogltk::base::setsurface(SDL_Surface * _surface) throw (GeneralException) { + if (surface) + throw GeneralException("Can't set video surface twice"); + surface = _surface; +} diff --git a/lib/engine.cc b/lib/engine.cc index 7ccc995..99843c0 100644 --- a/lib/engine.cc +++ b/lib/engine.cc @@ -18,6 +18,9 @@ int mogltk::engine::frames, mogltk::engine::locked = 0; double mogltk::engine::curfps = -1; Uint32 mogltk::engine::curticks; +mogltk::glbase * mogltk::engine::glbase_o = 0; +mogltk::base * mogltk::engine::base_o = 0; + #define UPDATERATE 1000 int mogltk::engine::setup() throw(GeneralException) { @@ -32,8 +35,6 @@ int mogltk::engine::setup() throw(GeneralException) { inited = true; - mogltk::SystemFont = new mogltk::font(&Input("font.bin")); - return 0; } @@ -48,6 +49,11 @@ int mogltk::engine::postsetup() throw(GeneralException) { postsetuped = true; + if (glbase_o) + mogltk::SystemFont = new mogltk::glfont(&Input("font.bin")); + else + mogltk::SystemFont = new mogltk::font(&Input("font.bin")); + return 0; } diff --git a/lib/font.cc b/lib/font.cc new file mode 100644 index 0000000..7cc57f6 --- /dev/null +++ b/lib/font.cc @@ -0,0 +1,318 @@ +#include +#include "base.h" +#include "font.h" +#include "Input.h" +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +Uint8 prescale2[4] = { 0, 85, 170, 255 }, prescale3[8] = { 0, 36, 72, 109, 145, 182, 218, 255 }; + +#define STRBUFSIZ 512 + +/* + +font file format +================ + +off|siz|description +---+---+------------------------------- + 0 | 2 | Number of entries = nbentries + 2 | 1 | Flags + 3 | 1 | maxX (maximum width) + 4 | 1 | maxY (maximum height) + 5 | 1 | base (bottom line from top) + 6 | 1 | inter (size of the interline) + 7 | X | char entries +7+X| Y | char map + +X = (maxX * maxY + 1) * nbentries +Y = nbentries * 4 + + +Flags: +===== + +0000000R + +R = RGBA (=1) or Alpha (=0) + +RGBA in 1232 format: +ABBGGGRR + + +Each entries: +============ + +off|siz|description +---+---+------------------------------- + 0 | 1 | True size of the entry + 1 | Z | Datas + +Z = maxX * maxY +Datas are stored in order X first, then Y. + +Char map: +======== + +nbentries entries, each entry = 4 bytes = 2 uint16 + +off|siz|description +---+---+------------------------------- + 0 | 2 | Unicode (?) + 2 | 2 | Corresponding char entry + +I'm not sure about my word 'Unicode'. I write this only to say it's an attempt +to make the fonts "internationals". If the "unicode" is < 255, then it should +match only one byte in the string. Otherwise, it should match two bytes. + + +Variables comments +================== + +nbcU = number of chars on X by texture +nbcV = number of chars on Y by texture +nbcT = number of char by texture +nbT = number of textures + +*/ + +mogltk::font::font(Handle * ffont) : textcolor(255, 255, 255, 255), shadow(0), wspace(0) { + int i; + + nbentries = ffont->readU16(); + flags = ffont->readU8(); + maxX = ffont->readU8(); + maxY = ffont->readU8(); + base = ffont->readU8(); + inter = ffont->readU8(); + + nbcU = 256 / maxX; + nbcV = 256 / maxY; + + nbcT = nbcU * nbcV; + + nbT = nbentries / nbcT; + + if (nbentries % nbcT) { + nbT++; + } + + printm(M_INFO, "Creating font texture: %i entries, flags = 0x%02x, maxX = %i, maxY = %i\n", nbentries, flags, maxX, maxY); + printm(M_INFO, "Which makes %i texture(s), with %i char by texture, %i on X, and %i on Y\n", nbT, nbcT, nbcU, nbcV); + + fonttex = (texture **) malloc(nbT * sizeof(texture *)); + + for (i = 0; i < nbT; i++) { + fonttex[i] = new texture(256, 256, true); + } + + sizes = (Uint8 *) malloc(nbentries * sizeof(Uint8)); + + Uint8 * curtex = (Uint8 *) fonttex[0]->GetSurface()->pixels; + Uint32 curU = 0, curV = 0, curT = 0; + for (i = 0; i < nbentries; i++) { + sizes[i] = ffont->readU8(); + for (int v = 0; v < maxY; v++) { + for (int u = 0; u < maxX; u++) { + Uint8 f; + f = ffont->readU8(); + if (flags & 1) { + Uint8 r, g, b, a; + r = f & 3; + g = (f >> 2) & 7; + b = (f >> 5) & 3; + a = (f >> 7) & 1; + curtex[(curU + u + (curV + v) * 256) * 4 + 0] = prescale2[r]; + curtex[(curU + u + (curV + v) * 256) * 4 + 1] = prescale3[g]; + curtex[(curU + u + (curV + v) * 256) * 4 + 2] = prescale2[b]; + curtex[(curU + u + (curV + v) * 256) * 4 + 3] = a ? 255 : 0; + } else { + curtex[(curU + u + (curV + v) * 256) * 4 + 0] = 255; + curtex[(curU + u + (curV + v) * 256) * 4 + 1] = 255; + curtex[(curU + u + (curV + v) * 256) * 4 + 2] = 255; + curtex[(curU + u + (curV + v) * 256) * 4 + 3] = f; + } + } + } + if (((curU += maxX) + maxX) > 256) { + curU = 0; + if (((curV += maxY) + maxY) > 256) { + curV = 0; + if ((curT + 1) != nbT) + curtex = (Uint8 *) fonttex[++curT]->GetSurface()->pixels; + } + } + } + + corresp = (Uint16 *) malloc(nbentries * 2 * sizeof(Uint16)); + + for (i = 0; i < 2 * nbentries; i++) { + corresp[i] = ffont->readU16(); + } +} + +mogltk::font::~font() { + int i; + for (i = 0; i < nbT; i++) { + delete fonttex[i]; + } + + free((void *&) fonttex); + free(sizes); +} + +void mogltk::font::drawentry(Uint16 entry, int x, int y, ColorP c) { + bool was2D; + int trueentry, cx, cy, px, py; + + return; + + was2D = mogltk::glbase::is2D(); + + if (!was2D) + mogltk::glbase::Enter2DMode(); + + if (shadow) { + int os = shadow; + shadow = 0; + + drawentry(entry, x + os, y + os, BLACK); + + shadow = os; + } + + y -= base; + + Bind(entry / nbcT); + c.Bind(); + trueentry = entry % nbcT; + cx = trueentry % nbcU; + cy = trueentry / nbcU; + px = cx * maxX; + py = cy * maxY; + + glBegin(GL_TRIANGLE_STRIP); + glTexCoord2i(px , py ); glVertex2i(x , y ); + glTexCoord2i(px + maxX - 1, py ); glVertex2i(x + maxX - 1, y ); + glTexCoord2i(px , py + maxY - 1); glVertex2i(x , y + maxY - 1); + glTexCoord2i(px + maxX - 1, py + maxY - 1); glVertex2i(x + maxX - 1, y + maxY - 1); + glEnd(); + + if (!was2D) + mogltk::glbase::Leave2DMode(); +} + +void mogltk::font::putcursor(int x, int y) { + cx = ox = x; + cy = y; +} + +void mogltk::font::putentry(Uint16 entry, ColorP c) { + drawentry(entry, cx, cy, c); + cx += sizes[entry] + wspace; +} + +void mogltk::font::putchar(char ch, ColorP c) { + Uint16 * p; + int i; + + for (i = 0, p = corresp; i < nbentries; i++, p++) { + if (*(p++) == ch) { + putentry(*p, c); + return; + } + } +} + +int mogltk::font::getchar(char ch) const { + Uint16 * p; + int i; + + for (i = 0, p = corresp; i < nbentries; i++, p++) { + if (*(p++) == ch) { + return *p; + } + } + + return -1; +} + +void mogltk::font::newline(void) { + cx = ox; + cy += inter; +} + +int mogltk::font::printf(const ugly_string & m, va_list ap) { + char * p; + static char buffer[STRBUFSIZ + 1]; + int r; + +#ifdef HAVE_VSNPRINTF + r = vsnprintf(buffer, STRBUFSIZ, m.p, ap); +#else + r = vsprintf(buffer, m.p, ap); +#endif + + for (p = buffer; *p; p++) { + if (*p == '\n') { + newline(); + } else { + putchar(*p, textcolor); + } + } + + return r; +} + +int mogltk::font::printf(const ugly_string & m, ...) { + va_list ap; + int r; + + va_start(ap, m); + r = printf(m, ap); + va_end(ap); + + return r; +} + +int mogltk::font::printf(const char * p, ...) { + ugly_string m; + va_list ap; + int r; + + m.p = p; + + va_start(ap, p); + r = printf(m, ap); + va_end(ap); + + return r; +} + +void mogltk::font::setcolor(ColorP c) { + textcolor = c; +} + +void mogltk::font::setshadow(int s) { + shadow = s; +} + +void mogltk::font::setwspace(int w) { + wspace = w; +} + +int mogltk::font::singletextsize(const String & s) const { + unsigned int i; + int r = 0; + + for (i = 0; i < s.strlen(); i++) { + r += sizes[getchar(s[i])]; + } + + return r; +} + +mogltk::font * mogltk::SystemFont; + + diff --git a/lib/glbase.cc b/lib/glbase.cc index 86d0774..df093dd 100644 --- a/lib/glbase.cc +++ b/lib/glbase.cc @@ -8,26 +8,20 @@ #include "config.h" #endif -int mogltk::glbase::width, mogltk::glbase::height, mogltk::glbase::inited = 0, mogltk::glbase::twoD = 0; -SDL_Surface * mogltk::glbase::surface = 0; +mogltk::glbase::glbase(int w, int h, int flags) throw (GeneralException) : mogltk::base(w, h, flags, 0), twoD(0) { + SDL_Surface * surface; -int mogltk::glbase::setup(int w, int h, int flags) throw(GeneralException) { - if (inited) { - printm(M_WARNING, "mogltk::glbase::setup called twice, ignoring second call...\n"); - return -1; - } - - width = w; - height = h; - mogltk::engine::setup(); if (SDL_InitSubSystem(SDL_INIT_VIDEO) < 0) { throw GeneralException(String("Couldn't initialise Video SubSystem: ") + SDL_GetError()); } - if (!(surface = SDL_SetVideoMode(width, height, 0, flags | SDL_OPENGL))) { + if (!(surface = SDL_SetVideoMode(w, h, 0, flags | SDL_OPENGL))) { throw GeneralException(String("Couldn't set GL mode: ") + SDL_GetError()); } + + mogltk::engine::glbase_o = this; + mogltk::engine::base_o = this; float ratio = surface->w; ratio /= surface->h; @@ -41,8 +35,6 @@ int mogltk::glbase::setup(int w, int h, int flags) throw(GeneralException) { printm(M_INFO, String("Version : ") + (char *) glGetString(GL_VERSION) + "\n"); printm(M_INFO, String("Extensions: ") + (char *) glGetString(GL_EXTENSIONS) + "\n"); - inited = 1; - glViewport(0, 0, surface->w, surface->h); glCullFace(GL_BACK); @@ -67,23 +59,14 @@ int mogltk::glbase::setup(int w, int h, int flags) throw(GeneralException) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - SDL_ShowCursor(0); SDL_GL_SwapBuffers(); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - return 0; -} - -int mogltk::glbase::GetWidth(void) { - return width; -} - -int mogltk::glbase::GetHeight(void) { - return height; + setsurface(surface); + mogltk::engine::postsetup(); } -int mogltk::glbase::GetInited(void) { - return inited; +mogltk::glbase::~glbase() { } void mogltk::glbase::Enter2DMode(void) { @@ -99,7 +82,7 @@ void mogltk::glbase::Enter2DMode(void) { glMatrixMode(GL_PROJECTION); glPushMatrix(); glLoadIdentity(); - glOrtho(0.0, width, height, 0.0, 0.0, 1.0); + glOrtho(0.0, GetWidth(), GetHeight(), 0.0, 0.0, 1.0); glMatrixMode(GL_MODELVIEW); glPushMatrix(); diff --git a/lib/glcolor.cc b/lib/glcolor.cc index c274620..c38b4e5 100644 --- a/lib/glcolor.cc +++ b/lib/glcolor.cc @@ -1,15 +1,13 @@ #include #include "glcolor.h" -Color mogltk::ColorP::Min(0, 0, 0, 0), mogltk::ColorP::Max = WHITE; - -mogltk::ColorP::ColorP(const Color & ac) : c(ac) { +mogltk::glColorP::glColorP(const Color & ac) : ColorP(ac) { } -mogltk::ColorP::ColorP(Uint8 ar, Uint8 ag, Uint8 ab, Uint8 aa) : c(ar, ag, ab, aa) { +mogltk::glColorP::glColorP(Uint8 ar, Uint8 ag, Uint8 ab, Uint8 aa) : ColorP(ar, ag, ab, aa) { } -void mogltk::ColorP::Bind() { +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 80a8b1d..66e36c4 100644 --- a/lib/glfont.cc +++ b/lib/glfont.cc @@ -1,175 +1,24 @@ -#include +#include "engine.h" #include "glbase.h" #include "glfont.h" -#include "Input.h" #ifdef HAVE_CONFIG_H #include "config.h" #endif -Uint8 prescale2[4] = { 0, 85, 170, 255 }, prescale3[8] = { 0, 36, 72, 109, 145, 182, 218, 255 }; - -#define STRBUFSIZ 512 - -/* - -font file format -================ - -off|siz|description ----+---+------------------------------- - 0 | 2 | Number of entries = nbentries - 2 | 1 | Flags - 3 | 1 | maxX (maximum width) - 4 | 1 | maxY (maximum height) - 5 | 1 | base (bottom line from top) - 6 | 1 | inter (size of the interline) - 7 | X | char entries -7+X| Y | char map - -X = (maxX * maxY + 1) * nbentries -Y = nbentries * 4 - - -Flags: -===== - -0000000R - -R = RGBA (=1) or Alpha (=0) - -RGBA in 1232 format: -ABBGGGRR - - -Each entries: -============ - -off|siz|description ----+---+------------------------------- - 0 | 1 | True size of the entry - 1 | Z | Datas - -Z = maxX * maxY -Datas are stored in order X first, then Y. - -Char map: -======== - -nbentries entries, each entry = 4 bytes = 2 uint16 - -off|siz|description ----+---+------------------------------- - 0 | 2 | Unicode (?) - 2 | 2 | Corresponding char entry - -I'm not sure about my word 'Unicode'. I write this only to say it's an attempt -to make the fonts "internationals". If the "unicode" is < 255, then it should -match only one byte in the string. Otherwise, it should match two bytes. - - -Variables comments -================== - -nbcU = number of chars on X by texture -nbcV = number of chars on Y by texture -nbcT = number of char by texture -nbT = number of textures - -*/ - -mogltk::font::font(Handle * ffont) : textcolor(255, 255, 255, 255), shadow(0), wspace(0) { - int i; - - nbentries = ffont->readU16(); - flags = ffont->readU8(); - maxX = ffont->readU8(); - maxY = ffont->readU8(); - base = ffont->readU8(); - inter = ffont->readU8(); - - nbcU = 256 / maxX; - nbcV = 256 / maxY; - - nbcT = nbcU * nbcV; - - nbT = nbentries / nbcT; - - if (nbentries % nbcT) { - nbT++; - } - - printm(M_INFO, "Creating font texture: %i entries, flags = 0x%02x, maxX = %i, maxY = %i\n", nbentries, flags, maxX, maxY); - printm(M_INFO, "Which makes %i texture(s), with %i char by texture, %i on X, and %i on Y\n", nbT, nbcT, nbcU, nbcV); - - fonttex = (texture **) malloc(nbT * sizeof(texture *)); - - for (i = 0; i < nbT; i++) { - fonttex[i] = new texture(256, 256, true); - } - - sizes = (Uint8 *) malloc(nbentries * sizeof(Uint8)); - - Uint8 * curtex = (Uint8 *) fonttex[0]->GetSurface()->pixels; - Uint32 curU = 0, curV = 0, curT = 0; - for (i = 0; i < nbentries; i++) { - sizes[i] = ffont->readU8(); - for (int v = 0; v < maxY; v++) { - for (int u = 0; u < maxX; u++) { - Uint8 f; - f = ffont->readU8(); - if (flags & 1) { - Uint8 r, g, b, a; - r = f & 3; - g = (f >> 2) & 7; - b = (f >> 5) & 3; - a = (f >> 7) & 1; - curtex[(curU + u + (curV + v) * 256) * 4 + 0] = prescale2[r]; - curtex[(curU + u + (curV + v) * 256) * 4 + 1] = prescale3[g]; - curtex[(curU + u + (curV + v) * 256) * 4 + 2] = prescale2[b]; - curtex[(curU + u + (curV + v) * 256) * 4 + 3] = a ? 255 : 0; - } else { - curtex[(curU + u + (curV + v) * 256) * 4 + 0] = 255; - curtex[(curU + u + (curV + v) * 256) * 4 + 1] = 255; - curtex[(curU + u + (curV + v) * 256) * 4 + 2] = 255; - curtex[(curU + u + (curV + v) * 256) * 4 + 3] = f; - } - } - } - if (((curU += maxX) + maxX) > 256) { - curU = 0; - if (((curV += maxY) + maxY) > 256) { - curV = 0; - if ((curT + 1) != nbT) - curtex = (Uint8 *) fonttex[++curT]->GetSurface()->pixels; - } - } - } - - corresp = (Uint16 *) malloc(nbentries * 2 * sizeof(Uint16)); - - for (i = 0; i < 2 * nbentries; i++) { - corresp[i] = ffont->readU16(); - } +mogltk::glfont::glfont(Handle * ffont) : font(ffont) { } -mogltk::font::~font() { - int i; - for (i = 0; i < nbT; i++) { - delete fonttex[i]; - } - - free((void *&) fonttex); - free(sizes); +mogltk::glfont::~glfont() { } -void mogltk::font::drawentry(Uint16 entry, int x, int y, ColorP c) { +void mogltk::glfont::drawentry(Uint16 entry, int x, int y, glColorP c) { bool was2D; int trueentry, cx, cy, px, py; - was2D = mogltk::glbase::is2D(); + was2D = mogltk::engine::glbase_o->is2D(); if (!was2D) - mogltk::glbase::Enter2DMode(); + mogltk::engine::glbase_o->Enter2DMode(); if (shadow) { int os = shadow; @@ -198,123 +47,9 @@ void mogltk::font::drawentry(Uint16 entry, int x, int y, ColorP c) { glEnd(); if (!was2D) - mogltk::glbase::Leave2DMode(); + mogltk::engine::glbase_o->Leave2DMode(); } -void mogltk::font::Bind(int index) { +void mogltk::glfont::Bind(int index) { fonttex[index]->Bind(); } - -void mogltk::font::putcursor(int x, int y) { - cx = ox = x; - cy = y; -} - -void mogltk::font::putentry(Uint16 entry, ColorP c) { - drawentry(entry, cx, cy, c); - cx += sizes[entry] + wspace; -} - -void mogltk::font::putchar(char ch, ColorP c) { - Uint16 * p; - int i; - - for (i = 0, p = corresp; i < nbentries; i++, p++) { - if (*(p++) == ch) { - putentry(*p, c); - return; - } - } -} - -int mogltk::font::getchar(char ch) const { - Uint16 * p; - int i; - - for (i = 0, p = corresp; i < nbentries; i++, p++) { - if (*(p++) == ch) { - return *p; - } - } - - return -1; -} - -void mogltk::font::newline(void) { - cx = ox; - cy += inter; -} - -int mogltk::font::printf(const ugly_string & m, va_list ap) { - char * p; - static char buffer[STRBUFSIZ + 1]; - int r; - -#ifdef HAVE_VSNPRINTF - r = vsnprintf(buffer, STRBUFSIZ, m.p, ap); -#else - r = vsprintf(buffer, m.p, ap); -#endif - - for (p = buffer; *p; p++) { - if (*p == '\n') { - newline(); - } else { - putchar(*p, textcolor); - } - } - - return r; -} - -int mogltk::font::printf(const ugly_string & m, ...) { - va_list ap; - int r; - - va_start(ap, m); - r = printf(m, ap); - va_end(ap); - - return r; -} - -int mogltk::font::printf(const char * p, ...) { - ugly_string m; - va_list ap; - int r; - - m.p = p; - - va_start(ap, p); - r = printf(m, ap); - va_end(ap); - - return r; -} - -void mogltk::font::setcolor(ColorP c) { - textcolor = c; -} - -void mogltk::font::setshadow(int s) { - shadow = s; -} - -void mogltk::font::setwspace(int w) { - wspace = w; -} - -int mogltk::font::singletextsize(const String & s) const { - unsigned int i; - int r = 0; - - for (i = 0; i < s.strlen(); i++) { - r += sizes[getchar(s[i])]; - } - - return r; -} - -mogltk::font * mogltk::SystemFont; - - diff --git a/lib/glshape.cc b/lib/glshape.cc index 6c314fb..7b93dfb 100644 --- a/lib/glshape.cc +++ b/lib/glshape.cc @@ -4,11 +4,11 @@ #include "gltexture.h" #include "glfont.h" -#define ENTER bool was2d = in2D(true) -#define ENTERT bool was2d = in2D(false) -#define LEAVE out2D(was2d) +#define ENTER bool was2d = Enter(true) +#define ENTERT bool was2d = Enter(false) +#define LEAVE Leave(was2d) -void mogltk::shape::box(int x1, int y1, int x2, int y2, ColorP c) { +void mogltk::glshape::box(int x1, int y1, int x2, int y2, glColorP c) { ENTER; c.Bind(); @@ -22,7 +22,7 @@ void mogltk::shape::box(int x1, int y1, int x2, int y2, ColorP c) { LEAVE; } -void mogltk::shape::box(int x1, int y1, int x2, int y2, ColorP c1, ColorP c2, ColorP c3, ColorP c4) { +void mogltk::glshape::box(int x1, int y1, int x2, int y2, glColorP c1, glColorP c2, glColorP c3, glColorP c4) { ENTER; glBegin(GL_TRIANGLE_STRIP); @@ -35,85 +35,7 @@ void mogltk::shape::box(int x1, int y1, int x2, int y2, ColorP c1, ColorP c2, Co LEAVE; } -void mogltk::shape::hline(int x1, int x2, int y, ColorP c) { - box(x1, y, x2, y, c); -} - -void mogltk::shape::hline3d(int x1, int x2, int y, ColorP shade1, ColorP shade2, bool bevel) { - ENTER; - - if (!bevel) { - hline(x1, x2, y, shade2); - hline(x1, x2, y + 1, shade1); - } else { - hline(x1, x2, y, shade1); - hline(x1, x2, y + 1, shade2); - } - - LEAVE; -} - -void mogltk::shape::vline(int x, int y1, int y2, ColorP c) { - box(x, y1, x, y2, c); -} - -void mogltk::shape::vline3d(int x, int y1, int y2, ColorP shade1, ColorP shade2, bool bevel) { - ENTER; - - if (!bevel) { - vline(x, y1, y2, shade2); - vline(x + 1, y1, y2, shade1); - } else { - vline(x, y1, y2, shade1); - vline(x + 1, y1, y2, shade2); - } - - LEAVE; -} - -void mogltk::shape::hline(int x1, int x2, int y, ColorP c1, ColorP c2) { - box(x1, y, x2, y, c1, c2, c1, c2); -} - -void mogltk::shape::vline(int x, int y1, int y2, ColorP c1, ColorP c2) { - box(x, y1, x, y2, c1, c1, c2, c2); -} - -void mogltk::shape::pixel(int x, int y, ColorP c) { - box(x, y, x, y, c); -} - -void mogltk::shape::obox(int x1, int y1, int x2, int y2, ColorP c) { - ENTER; - - hline(x1, x2, y1, c); - hline(x1, x2, y2, c); - vline(x1, y1, y2, c); - vline(x2, y1, y2, c); - - LEAVE; -} - -void mogltk::shape::obox(int x1, int y1, int x2, int y2, ColorP c1, ColorP c2, ColorP c3, ColorP c4) { - ENTER; - - hline(x1, x2, y1, c1, c2); - hline(x1, x2, y2, c3, c4); - vline(x1, y1, y2, c1, c3); - vline(x2, y1, y2, c2, c4); - - LEAVE; -} - -void mogltk::shape::tbox(mogltk::texture * t, int x1, int y1, int x2, int y2, int tx, int ty, double f, ColorP c) { - tbox(t, x1, y1, x2, y2, tx, ty, tx + (int) ((x2 - x1) * f), ty + (int) ((y2 - y1) * f), c); -} - -void mogltk::shape::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) { - tbox(t, x1, y1, x2, y2, c1, c2, c3, c4, tx, ty, tx + (int) ((x2 - x1) * f), ty + (int) ((y2 - y1) * f)); -} - -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) { +void mogltk::glshape::tbox(mogltk::texture * t, int x1, int y1, int x2, int y2, int tx1, int ty1, int tx2, int ty2, glColorP c) { ENTERT; c.Bind(); @@ -128,7 +50,7 @@ void mogltk::shape::tbox(mogltk::texture * t, int x1, int y1, int x2, int y2, in LEAVE; } -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) { +void mogltk::glshape::tbox(mogltk::texture * 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) { ENTERT; t->Bind(); @@ -142,7 +64,7 @@ void mogltk::shape::tbox(mogltk::texture * t, int x1, int y1, int x2, int y2, Co LEAVE; } -void mogltk::shape::box3d(int x1, int y1, int x2, int y2, ColorP face, ColorP shade1, ColorP shade2, int depth, bool bevel) { +void mogltk::glshape::box3d(int x1, int y1, int x2, int y2, glColorP face, glColorP shade1, glColorP shade2, int depth, bool bevel) { ENTER; if (!bevel) { @@ -182,92 +104,7 @@ void mogltk::shape::box3d(int x1, int y1, int x2, int y2, ColorP face, ColorP sh LEAVE; } -void mogltk::shape::obox3d(int x1, int y1, int x2, int y2, ColorP shade1, ColorP shade2, bool bevel) { - ENTER; - - if (!bevel) { - obox(x1 + 1, y1 + 1, x2, y2, shade1); - obox(x1, y1, x2 - 1, y2 - 1, shade2); - } else { - obox(x1, y1, x2 - 1, y2 - 1, shade1); - obox(x1 + 1, y1 + 1, x2, y2, shade2); - } - - LEAVE; -} - -void mogltk::shape::window(int x1, int y1, int x2, int y2, const String & title, - ColorP titlecolor, ColorP titlebackcolor, - ColorP front, ColorP shade1, ColorP shade2) { - ENTER; - - box3d(x1, y1, x2, y2, front, shade1, shade2); - hline3d(x1 + 2, x2 - 2, y1 + 19, shade1, shade2, 1); - box(x1 + 2, y1 + 2, x2 - 2, y1 + 18, titlebackcolor); - text((x1 + x2) / 2, y1 + 2, title, titlecolor, CENTER); - - LEAVE; -} - -void mogltk::shape::text(int x, int y, const String & text, ColorP textcolor, align_t align) { - int tsize = SystemFont->singletextsize(text); - switch (align) { - case LEFT: - SystemFont->putcursor(x, y); - break; - case CENTER: - SystemFont->putcursor(x - (tsize / 2), y); - break; - case RIGHT: - SystemFont->putcursor(x - tsize, y); - break; - } - SystemFont->setcolor(textcolor); - SystemFont->setshadow(0); - SystemFont->printf(text); -} - -void mogltk::shape::text3d(int x, int y, const String & atext, ColorP textcolor, - ColorP shade1, ColorP shade2, - align_t align, bool bevel) { - ENTER; - - SystemFont->setwspace(1); - if (!bevel) { - text(x - 1, y - 1, atext, shade2, align); - text(x + 1, y + 1, atext, shade1, align); - } else { - text(x - 1, y - 1, atext, shade1, align); - text(x + 1, y + 1, atext, shade2, align); - } - text(x, y, atext, textcolor, align); - SystemFont->setwspace(0); - - LEAVE; -} - -void mogltk::shape::button(int x1, int y1, int x2, int y2, - const String & atext, bool bevel, - ColorP front, ColorP shade1, ColorP shade2, - ColorP round, ColorP textcolor, - ColorP tshade1, ColorP tshade2) { - ENTER; - - box3d(x1, y1, x2, y2, front, shade1, shade2, 2, bevel); - hline(x1, x2, y1 - 1, round); - hline(x1, x2, y2 + 1, round); - vline(x1 - 1, y1, y2, round); - vline(x2 + 1, y1, y2, round); - pixel(x1, y1, round); - pixel(x1, y2, round); - pixel(x2, y1, round); - pixel(x2, y2, round); - text3d((x1 + x2) / 2, (y1 + y2) / 2 - 8, atext, textcolor, tshade1, tshade2, CENTER, bevel); - - LEAVE; -} - -bool mogltk::shape::in2D(bool unbind) { +bool mogltk::glshape::Enter(bool unbind) { bool was2D = mogltk::glbase::is2D(); if (!was2D) @@ -278,7 +115,11 @@ bool mogltk::shape::in2D(bool unbind) { return was2D; } -void mogltk::shape::out2D(bool was2D) { +bool mogltk::glshape::Enter() { + return Enter(true); +} + +void mogltk::glshape::Leave(bool was2D) { if (!was2D) mogltk::glbase::Leave2DMode(); } diff --git a/lib/mcolor.cc b/lib/mcolor.cc new file mode 100644 index 0000000..91c8a1d --- /dev/null +++ b/lib/mcolor.cc @@ -0,0 +1,10 @@ +#include +#include "mcolor.h" + +Color mogltk::ColorP::Min(0, 0, 0, 0), mogltk::ColorP::Max = WHITE; + +mogltk::ColorP::ColorP(const Color & ac) : c(ac) { +} + +mogltk::ColorP::ColorP(Uint8 ar, Uint8 ag, Uint8 ab, Uint8 aa) : c(ar, ag, ab, aa) { +} diff --git a/lib/shape.cc b/lib/shape.cc new file mode 100644 index 0000000..088deb7 --- /dev/null +++ b/lib/shape.cc @@ -0,0 +1,287 @@ +#include +#include "base.h" +#include "shape.h" +#include "texture.h" +#include "font.h" + +#define ENTER bool flag = Enter() +#define LEAVE Leave(flag) + +void mogltk::shape::box(int x1, int y1, int x2, int y2, ColorP c) { + ENTER; + + c.Bind(); + glBegin(GL_TRIANGLE_STRIP); + glVertex2i(x1, y1); + glVertex2i(x2 + 1, y1); + glVertex2i(x1, y2 + 1); + glVertex2i(x2 + 1, y2 + 1); + glEnd(); + + LEAVE; +} + +void mogltk::shape::box(int x1, int y1, int x2, int y2, ColorP c1, ColorP c2, ColorP c3, ColorP c4) { + ENTER; + + glBegin(GL_TRIANGLE_STRIP); + c1.Bind(); glVertex2i(x1, y1); + c2.Bind(); glVertex2i(x2 + 1, y1); + c3.Bind(); glVertex2i(x1, y2 + 1); + c4.Bind(); glVertex2i(x2 + 1, y2 + 1); + glEnd(); + + LEAVE; +} + +void mogltk::shape::hline(int x1, int x2, int y, ColorP c) { + box(x1, y, x2, y, c); +} + +void mogltk::shape::hline3d(int x1, int x2, int y, ColorP shade1, ColorP shade2, bool bevel) { + ENTER; + + if (!bevel) { + hline(x1, x2, y, shade2); + hline(x1, x2, y + 1, shade1); + } else { + hline(x1, x2, y, shade1); + hline(x1, x2, y + 1, shade2); + } + + LEAVE; +} + +void mogltk::shape::vline(int x, int y1, int y2, ColorP c) { + box(x, y1, x, y2, c); +} + +void mogltk::shape::vline3d(int x, int y1, int y2, ColorP shade1, ColorP shade2, bool bevel) { + ENTER; + + if (!bevel) { + vline(x, y1, y2, shade2); + vline(x + 1, y1, y2, shade1); + } else { + vline(x, y1, y2, shade1); + vline(x + 1, y1, y2, shade2); + } + + LEAVE; +} + +void mogltk::shape::hline(int x1, int x2, int y, ColorP c1, ColorP c2) { + box(x1, y, x2, y, c1, c2, c1, c2); +} + +void mogltk::shape::vline(int x, int y1, int y2, ColorP c1, ColorP c2) { + box(x, y1, x, y2, c1, c1, c2, c2); +} + +void mogltk::shape::pixel(int x, int y, ColorP c) { + box(x, y, x, y, c); +} + +void mogltk::shape::obox(int x1, int y1, int x2, int y2, ColorP c) { + ENTER; + + hline(x1, x2, y1, c); + hline(x1, x2, y2, c); + vline(x1, y1, y2, c); + vline(x2, y1, y2, c); + + LEAVE; +} + +void mogltk::shape::obox(int x1, int y1, int x2, int y2, ColorP c1, ColorP c2, ColorP c3, ColorP c4) { + ENTER; + + hline(x1, x2, y1, c1, c2); + hline(x1, x2, y2, c3, c4); + vline(x1, y1, y2, c1, c3); + vline(x2, y1, y2, c2, c4); + + LEAVE; +} + +void mogltk::shape::tbox(mogltk::texture * t, int x1, int y1, int x2, int y2, int tx, int ty, double f, ColorP c) { + tbox(t, x1, y1, x2, y2, tx, ty, tx + (int) ((x2 - x1) * f), ty + (int) ((y2 - y1) * f), c); +} + +void mogltk::shape::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) { + tbox(t, x1, y1, x2, y2, c1, c2, c3, c4, tx, ty, tx + (int) ((x2 - x1) * f), ty + (int) ((y2 - y1) * f)); +} + +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) { + ENTERT; + + c.Bind(); + t->Bind(); + glBegin(GL_TRIANGLE_STRIP); + glTexCoord2i(tx1, ty1); glVertex2i(x1, y1); + glTexCoord2i(tx2 + 1, ty1); glVertex2i(x2 + 1, y1); + glTexCoord2i(tx1, ty2 + 1); glVertex2i(x1, y2 + 1); + glTexCoord2i(tx2 + 1, ty2 + 1); glVertex2i(x2 + 1, y2 + 1); + glEnd(); + + LEAVE; +} + +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) { + ENTERT; + + t->Bind(); + glBegin(GL_TRIANGLE_STRIP); + c1.Bind(); glTexCoord2i(tx1, ty1); glVertex2i(x1, y1); + c2.Bind(); glTexCoord2i(tx2 + 1, ty1); glVertex2i(x2 + 1, y1); + c3.Bind(); glTexCoord2i(tx1, ty2 + 1); glVertex2i(x1, y2 + 1); + c4.Bind(); glTexCoord2i(tx2 + 1, ty2 + 1); glVertex2i(x2 + 1, y2 + 1); + glEnd(); + + LEAVE; +} + +void mogltk::shape::box3d(int x1, int y1, int x2, int y2, ColorP face, ColorP shade1, ColorP shade2, int depth, bool bevel) { + ENTER; + + if (!bevel) { + shade1.Bind(); + } else { + shade2.Bind(); + } + glBegin(GL_TRIANGLE_FAN); + glVertex2i(x1, y1); + glVertex2i(x2 + 1, y1); + glVertex2i(x2 + 1 - depth, y1 + depth); + glVertex2i(x1 + depth, y2 + 1 - depth); + glVertex2i(x1, y2 + 1); + glEnd(); + + if (!bevel) { + shade2.Bind(); + } else { + shade1.Bind(); + } + glBegin(GL_TRIANGLE_FAN); + glVertex2i(x2 + 1, y2 + 1); + glVertex2i(x1, y2 + 1); + glVertex2i(x1 + depth, y2 + 1 - depth); + glVertex2i(x2 + 1 - depth, y1 + depth); + glVertex2i(x2 + 1, y1); + glEnd(); + + face.Bind(); + glBegin(GL_TRIANGLE_STRIP); + glVertex2i(x1 + depth, y1 + depth); + glVertex2i(x2 + 1 - depth, y1 + depth); + glVertex2i(x1 + depth, y2 + 1 - depth); + glVertex2i(x2 + 1 - depth, y2 + 1 - depth); + glEnd(); + + LEAVE; +} + +void mogltk::shape::obox3d(int x1, int y1, int x2, int y2, ColorP shade1, ColorP shade2, bool bevel) { + ENTER; + + if (!bevel) { + obox(x1 + 1, y1 + 1, x2, y2, shade1); + obox(x1, y1, x2 - 1, y2 - 1, shade2); + } else { + obox(x1, y1, x2 - 1, y2 - 1, shade1); + obox(x1 + 1, y1 + 1, x2, y2, shade2); + } + + LEAVE; +} + +void mogltk::shape::window(int x1, int y1, int x2, int y2, const String & title, + ColorP titlecolor, ColorP titlebackcolor, + ColorP front, ColorP shade1, ColorP shade2) { + ENTER; + + box3d(x1, y1, x2, y2, front, shade1, shade2); + hline3d(x1 + 2, x2 - 2, y1 + 19, shade1, shade2, 1); + box(x1 + 2, y1 + 2, x2 - 2, y1 + 18, titlebackcolor); + text((x1 + x2) / 2, y1 + 2, title, titlecolor, CENTER); + + LEAVE; +} + +void mogltk::shape::text(int x, int y, const String & text, ColorP textcolor, align_t align) { + int tsize = SystemFont->singletextsize(text); + switch (align) { + case LEFT: + SystemFont->putcursor(x, y); + break; + case CENTER: + SystemFont->putcursor(x - (tsize / 2), y); + break; + case RIGHT: + SystemFont->putcursor(x - tsize, y); + break; + } + SystemFont->setcolor(textcolor); + SystemFont->setshadow(0); + SystemFont->printf(text); +} + +void mogltk::shape::text3d(int x, int y, const String & atext, ColorP textcolor, + ColorP shade1, ColorP shade2, + align_t align, bool bevel) { + ENTER; + + SystemFont->setwspace(1); + if (!bevel) { + text(x - 1, y - 1, atext, shade2, align); + text(x + 1, y + 1, atext, shade1, align); + } else { + text(x - 1, y - 1, atext, shade1, align); + text(x + 1, y + 1, atext, shade2, align); + } + text(x, y, atext, textcolor, align); + SystemFont->setwspace(0); + + LEAVE; +} + +void mogltk::shape::button(int x1, int y1, int x2, int y2, + const String & atext, bool bevel, + ColorP front, ColorP shade1, ColorP shade2, + ColorP round, ColorP textcolor, + ColorP tshade1, ColorP tshade2) { + ENTER; + + box3d(x1, y1, x2, y2, front, shade1, shade2, 2, bevel); + hline(x1, x2, y1 - 1, round); + hline(x1, x2, y2 + 1, round); + vline(x1 - 1, y1, y2, round); + vline(x2 + 1, y1, y2, round); + pixel(x1, y1, round); + pixel(x1, y2, round); + pixel(x2, y1, round); + pixel(x2, y2, round); + text3d((x1 + x2) / 2, (y1 + y2) / 2 - 8, atext, textcolor, tshade1, tshade2, CENTER, bevel); + + 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); +} + +void mogltk::shape::Leave(bool was2D) { + if (!was2D) + mogltk::glbase::Leave2DMode(); +} -- cgit v1.2.3