From fae0c2bfe95a122de0f165791690dc18928a931c Mon Sep 17 00:00:00 2001 From: pixel Date: Fri, 14 Nov 2003 17:21:14 +0000 Subject: Started working on widgets. --- include/base.h | 1 + include/engine.h | 2 + include/glbase.h | 3 + include/glwidgets.h | 33 +++++----- include/mcolor.h | 1 + include/widgets.h | 52 ++++++++++++++++ lib/base.cc | 16 +++++ lib/engine.cc | 7 ++- lib/glbase.cc | 24 ++++++-- lib/glwidgets.cc | 51 +++------------- lib/shape.cc | 2 +- lib/widgets.cc | 173 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/test.cc | 47 ++++++++++---- 13 files changed, 331 insertions(+), 81 deletions(-) create mode 100644 include/widgets.h create mode 100644 lib/widgets.cc diff --git a/include/base.h b/include/base.h index 592d8d9..2607cf5 100644 --- a/include/base.h +++ b/include/base.h @@ -16,6 +16,7 @@ namespace mogltk { virtual void Enter2DMode(); virtual void Leave2DMode(); virtual bool is2D(); + virtual void changeviewport(int x = 0, int y = 0, unsigned int w = 0, unsigned int h = 0); protected: base(int, int, int, int); void setsurface(SDL_Surface *) throw (GeneralException); diff --git a/include/engine.h b/include/engine.h index 420f576..6bd2697 100644 --- a/include/engine.h +++ b/include/engine.h @@ -6,6 +6,7 @@ #include #include #include +#include namespace mogltk { class engine : public Base { @@ -35,6 +36,7 @@ namespace mogltk { static void setkeyevent(keyevent *); static glbase * glbase_o; static base * base_o; + static widget * root; private: static bool inited; static bool postsetuped; diff --git a/include/glbase.h b/include/glbase.h index 6328d94..7e6d454 100644 --- a/include/glbase.h +++ b/include/glbase.h @@ -15,12 +15,15 @@ namespace mogltk { virtual void Leave2DMode(void); virtual void Flip(void); virtual bool is2D(void); + virtual void changeviewport(int x = 0, int y = 0, unsigned int w = 0, unsigned int h = 0); + void changefovy(GLdouble); static void glVertex(GLshort, GLshort, GLshort = 0, GLshort = 1); static void glVertex(GLint, GLint, GLint = 0, GLint = 1); static void glVertex(GLfloat, GLfloat, GLfloat = 0.0, GLfloat = 1.0); static void glVertex(GLdouble, GLdouble, GLdouble = 0.0, GLdouble = 1.0); private: int twoD; + GLdouble ratio, fovy; }; }; diff --git a/include/glwidgets.h b/include/glwidgets.h index 86688ef..b606a14 100644 --- a/include/glwidgets.h +++ b/include/glwidgets.h @@ -1,20 +1,15 @@ -#ifndef __GLWIDGETS_H__ -#define __GLWIDGETS_H__ - -#include - -namespace mogltk { - class widget : public Base { - public: - virtual ~widget(); - bool PointerEvent(int x, int y); +#ifndef __GLWIDGETS_H__ +#define __GLWIDGETS_H__ + +#include + +namespace mogltk { + class glRoot : public Root { + public: + glRoot(shape *); protected: - widget(widget * father, int x, int y, int sx, int sy) throw (GeneralException); - private: - int x, y, sx, sy; - widget * father, * next, * prev, * child, * root; - static widget * cur_root, * focused; - }; -}; - -#endif + virtual void draw(); + }; +}; + +#endif diff --git a/include/mcolor.h b/include/mcolor.h index 6be0a5a..4cf3113 100644 --- a/include/mcolor.h +++ b/include/mcolor.h @@ -1,6 +1,7 @@ #ifndef __MCOLOR_H__ #define __MCOLOR_H__ +#include #include #include diff --git a/include/widgets.h b/include/widgets.h new file mode 100644 index 0000000..bef2682 --- /dev/null +++ b/include/widgets.h @@ -0,0 +1,52 @@ +#ifndef __WIDGETS_H__ +#define __WIDGETS_H__ + +#include +#include + +namespace mogltk { + class widget : public Base { + public: + virtual ~widget(); + void move(int x, int y); + void resize(int sx, int sy); + int GetX(); + int GetY(); + int GetH(); + int GetW(); + int GetAX(); + int GetAY(); + int GetAX2(); + int GetAY2(); + void fulldraw(); + shape * Shaper(); + protected: + widget(widget * father, int x, int y, int sx, int sy, int type, String name, shape *); + virtual void draw() = 0; + private: + void computeabs(); + int x, y, sx, sy, ax, ay, ax2, ay2; + widget * father, * next, * prev, * child, * root; + static widget * focused; + int type; + String name, caption; + shape * sh; + void idraw(); + }; + + class drawer : public Base { + public: + virtual void draw(widget *) = 0; + }; + + class Root : public widget { + public: + Root(shape *, drawer * = 0); + void setdrawer(drawer *); + protected: + virtual void draw(); + drawer * dr; + }; +}; + +#endif diff --git a/lib/base.cc b/lib/base.cc index 49e883e..5282617 100644 --- a/lib/base.cc +++ b/lib/base.cc @@ -78,3 +78,19 @@ void mogltk::base::Leave2DMode() { bool mogltk::base::is2D() { return true; } + +void mogltk::base::changeviewport(int x, int y, unsigned int w, unsigned int h) { + SDL_Rect r; + + if ((w == 0) && (h == 0)) { + w = GetWidth() - x; + h = GetHeight() - y; + } + + r.x = x; + r.y = y; + r.w = w; + r.h = h; + + SDL_SetClipRect(surface, &r); +} diff --git a/lib/engine.cc b/lib/engine.cc index 40426c1..6e899e2 100644 --- a/lib/engine.cc +++ b/lib/engine.cc @@ -14,6 +14,7 @@ int mogltk::engine::mx, mogltk::engine::my, mogltk::engine::mz = 0, mogltk::engi int mogltk::engine::frames, mogltk::engine::locked = 0; double mogltk::engine::curfps = -1; Uint32 mogltk::engine::curticks; +mogltk::widget * mogltk::engine::root = 0; mogltk::glbase * mogltk::engine::glbase_o = 0; mogltk::base * mogltk::engine::base_o = 0; @@ -32,11 +33,11 @@ void mogltk::engine::keyevent::down(SDL_keysym) { int mogltk::engine::setup() throw(GeneralException) { if (inited) { - printm(M_WARNING, _("mogltk::engine::setup() called twice, ignoring second call.\n")); + printm(M_WARNING, FUNCNAME + _(" called twice, ignoring second call.\n")); return -1; } if (SDL_Init(0) < 0) { - throw GeneralException(_("Unable to start SDL base system")); + throw GeneralException(FUNCNAME + _(": Unable to start SDL base system")); } atexit(SDL_Quit); @@ -47,7 +48,7 @@ int mogltk::engine::setup() throw(GeneralException) { int mogltk::engine::postsetup() throw(GeneralException) { if (postsetuped) { - printm(M_WARNING, _("mogltk::engine::postsetup() called twice, ignoring second call.\n")); + printm(M_WARNING, FUNCNAME + _(" called twice, ignoring second call.\n")); return -1; } diff --git a/lib/glbase.cc b/lib/glbase.cc index b6d41ef..ebbfe08 100644 --- a/lib/glbase.cc +++ b/lib/glbase.cc @@ -8,7 +8,7 @@ #include "config.h" #endif -mogltk::glbase::glbase(int w, int h, int flags) throw (GeneralException) : mogltk::base(w, h, flags, 0), twoD(0) { +mogltk::glbase::glbase(int w, int h, int flags) throw (GeneralException) : mogltk::base(w, h, flags, 0), twoD(0), fovy(60.0) { SDL_Surface * surface; mogltk::engine::setup(); @@ -23,8 +23,7 @@ mogltk::glbase::glbase(int w, int h, int flags) throw (GeneralException) : moglt mogltk::engine::glbase_o = this; mogltk::engine::base_o = this; - float ratio = surface->w; - ratio /= surface->h; + ratio = (GLdouble) surface->w / surface->h; printm(M_INFO, "Video resolution: %dx%dx%d (ratio = %3.2f)\n", surface->w, surface->h, surface->format->BitsPerPixel, ratio); printm(M_INFO, "\n"); @@ -50,7 +49,7 @@ mogltk::glbase::glbase(int w, int h, int flags) throw (GeneralException) : moglt glMatrixMode(GL_PROJECTION); glLoadIdentity(); - gluPerspective(60.0, ratio, 1.0, 1024.0); + gluPerspective(fovy, ratio, 1.0, 1024.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); @@ -145,3 +144,20 @@ void mogltk::glbase::glVertex(GLdouble x, GLdouble y, GLdouble z, GLdouble w) { glVertex4d(x, y, z, w); } +void mogltk::glbase::changeviewport(int x, int y, unsigned int w, unsigned int h) { + if ((w == 0) && (h == 0)) { + w = GetWidth() - x; + h = GetHeight() - y; + } + + ratio = (GLdouble) w / h; + glViewport(x, y, w, h); + if (!engine::base_o->is2D()) + gluPerspective(fovy, ratio, 1.0, 1024.0); +} + +void mogltk::glbase::changefovy(GLdouble nfoyv) { + fovy = nfoyv; + if (!engine::base_o->is2D()) + gluPerspective(fovy, ratio, 1.0, 1024.0); +} diff --git a/lib/glwidgets.cc b/lib/glwidgets.cc index f8e0389..511cdc4 100644 --- a/lib/glwidgets.cc +++ b/lib/glwidgets.cc @@ -1,43 +1,8 @@ -#include -#include -#include "glfont.h" -#include "glwidgets.h" - -#ifdef HAVE_CONFIG_H -#include "config.h" -#endif - -#include "gettext.h" - -mogltk::widget * mogltk::widget::cur_root = 0; - -mogltk::widget::widget(widget * _father, int _x, int _y, int _sx, int _sy) throw (GeneralException) : - x(_x), y(_y), sx(_sx), sy(_sy), father(_father) { - - if (!father) - father = cur_root; - - if (father) { - next = father->child; - father->child = this; - root = father->root; - } else { - next = 0; - cur_root = root = this; - } - prev = 0; - child = 0; -} - -mogltk::widget::~widget() { - while(child) - delete child; - - if (prev) - prev->next = next; - else if (father) - father->child = next; - - if (next) - next->prev = prev; -} +#include +#include +#include "mcolor.h" +#include "glwidgets.h" + +void mogltk::glRoot::draw() { + Shaper()->box(GetAX(), GetAY(), GetAX2(), GetAY2()); +} diff --git a/lib/shape.cc b/lib/shape.cc index f566f3d..60e9522 100644 --- a/lib/shape.cc +++ b/lib/shape.cc @@ -104,7 +104,7 @@ void mogltk::fill::insert(int x1, int y1, int x2, int y2) { SWAP(x1, x2); SWAP(y1, y2); } - + dx = x1 - x2; dy = y1 - y2; diff --git a/lib/widgets.cc b/lib/widgets.cc new file mode 100644 index 0000000..1f540f2 --- /dev/null +++ b/lib/widgets.cc @@ -0,0 +1,173 @@ +#include +#include +#include "font.h" +#include "widgets.h" +#include "engine.h" + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "gettext.h" + +mogltk::widget * mogltk::widget::focused = 0; + +mogltk::widget::widget(widget * _father, int _x, int _y, int _sx, int _sy, int _type, String _name, mogltk::shape * _sh) : + x(_x), y(_y), sx(_sx), sy(_sy), father(_father), type(_type), name(_name), sh(_sh) { + + if (!father) { + root = this; + father = this; + next = 0; + x = 0; + y = 0; + sx = engine::base_o->GetWidth(); + sy = engine::base_o->GetHeight(); + } else { + next = father->child; + if (next) + next->prev = this; + father->child = this; + root = father->root; + } + prev = 0; + child = 0; + + computeabs(); +} + +mogltk::widget::~widget() { + while(child) + delete child; + + if (prev) + prev->next = next; + else + father->child = next; + + if (next) + next->prev = prev; +} + +void mogltk::widget::computeabs() { + if (father != this) { + ax = father->ax + x; + ay = father->ay + y; + } else { + ax = x; + ay = y; + } + ax2 = ax + sx; + ay2 = ay + sy; +} + +void mogltk::widget::move(int nx, int ny) { + x = nx; + y = ny; + + computeabs(); +} + +void mogltk::widget::resize(int nsx, int nsy) { + sx = nsx; + sy = nsy; + + ax2 = ax + sx; + ay2 = ay + sy; +} + +int mogltk::widget::GetX() { + return x; +} + +int mogltk::widget::GetY() { + return y; +} + +int mogltk::widget::GetH() { + return sx; +} + +int mogltk::widget::GetW() { + return sy; +} + +int mogltk::widget::GetAX() { + return ax; +} + +int mogltk::widget::GetAY() { + return ay; +} + +int mogltk::widget::GetAX2() { + return ax2; +} + +int mogltk::widget::GetAY2() { + return ay2; +} + +mogltk::shape * mogltk::widget::Shaper() { + return sh; +} + +void mogltk::widget::fulldraw() { + bool was2D; + + if (!(was2D = mogltk::engine::glbase_o->is2D())) + mogltk::engine::glbase_o->Enter2DMode(); + + texture::Unbind(); + mogltk::ColorP::Max = WHITE; + mogltk::ColorP::Min = BLACK; + mogltk::ColorP::Min.A = 0; + + root->idraw(); + + if (!was2D) + mogltk::engine::glbase_o->Leave2DMode(); +} + +void mogltk::widget::idraw() { + int x1, y1, x2, y2; + + if (next) + next->idraw(); + + x1 = MAX(GetAX(), father->GetAX()); + y1 = MAX(GetAY(), father->GetAY()); + + x2 = MIN(GetAX2(), father->GetAX2()); + y2 = MIN(GetAY2(), father->GetAY2()); + + engine::base_o->changeviewport(x1, y1, x2 - x1, y2 - y1); + + draw(); + + if (child) + child->idraw(); +} + +/* + * Predefined widgets. + */ + +/* Here is Root */ + +mogltk::Root::Root(mogltk::shape * sh, mogltk::drawer * _dr) : widget(0, 0, 0, 0, 0, 0, "Root", sh), dr(_dr) { + if (engine::root) + delete engine::root; + engine::root = this; +} + +void mogltk::Root::draw() { + if (dr) + dr->draw(this); + else + Shaper()->box(GetAX(), GetAY(), GetAX2(), GetAY2()); +} + +void mogltk::Root::setdrawer(drawer * _dr) { + dr = _dr; +} diff --git a/src/test.cc b/src/test.cc index 0ef8a57..91c393b 100644 --- a/src/test.cc +++ b/src/test.cc @@ -3,11 +3,9 @@ #include #include #include -extern "C" { #include #include #include -} #ifdef HAVE_CONFIG_H #include "config.h" #else @@ -18,6 +16,8 @@ extern "C" { #include #include #include +#include +#include #include "glbase.h" #include "texture.h" #include "glfont.h" @@ -25,6 +25,7 @@ extern "C" { #include "engine.h" #include "glsprite.h" #include "glshape.h" +#include "glwidgets.h" #define SPIN .50 #define TILT .20 @@ -273,7 +274,7 @@ CODE_BEGINS void checkluastack(lua_State * L) { int n = lua_gettop(L); - int i; + int i, j; String t; printm(M_INFO, "Inspecting LUA stack\n"); @@ -301,7 +302,16 @@ void checkluastack(lua_State * L) { t.set("(String) %s", lua_tostring(L, i)); break; case LUA_TTABLE: - t.set("(Table)"); + printm(M_INFO, String(i) + ": (Table) Exploring:\n"); + lua_pushnil(L); /* first key */ + j = 0; + while (lua_next(L, -2) != 0) { + /* `key' is at index -2 and `value' at index -1 */ + printm(M_INFO, " %s - %s\n", lua_typename(L, lua_type(L, -2)), + lua_typename(L, lua_type(L, -1))); + lua_pop(L, 1); /* removes `value'; keeps `key' for next iteration */ + } + t = ""; break; case LUA_TFUNCTION: t = "(Function)"; @@ -310,7 +320,8 @@ void checkluastack(lua_State * L) { t = "Unknown"; } - printm(M_INFO, String(i) + ": " + t + "\n"); + if (t != "") + printm(M_INFO, String(i) + ": " + t + "\n"); } } @@ -384,6 +395,7 @@ void initstars() { #ifdef LUA L = lua_open(); luaopen_math(L); + lua_pop(L, 1); lua_atpanic(L, myluaerror); luaL_loadfile(L, "particules.lua"); lua_call(L, 0, 0); @@ -501,7 +513,7 @@ virtual int startup() throw (GeneralException) { verbosity = M_INFO; new Archive("datas.paq"); - bdlload("cl.bdl"); +// bdlload("cl.bdl"); #if 1 mogltk::base * gl = new mogltk::glbase(); @@ -509,18 +521,29 @@ virtual int startup() throw (GeneralException) { mogltk::font * font = new mogltk::glfont(&Input("font-2.bin")); mogltk::Sprite * s = new mogltk::glSprite(&Input("cursor.rgba"), 25, 25); mogltk::Sprite * p = new mogltk::glSprite(&Input("particule.rgba"), 16, 16); + mogltk::widget * w = new mogltk::Root(sh); #else mogltk::base * gl = new mogltk::base(); mogltk::shape * sh = new mogltk::shape(); mogltk::font * font = new mogltk::font(&Input("font-2.bin")); mogltk::Sprite * s = new mogltk::Sprite(&Input("cursor.rgba"), 25, 25); mogltk::Sprite * p = new mogltk::Sprite(&Input("particule.rgba"), 16, 16); + mogltk::widget * w = new mogltk::Root(sh); #endif mogltk::engine::setcursorvisible(true); mogltk::engine::setappactive(true); mogltk::texture * mytex = new mogltk::texture(&Input("pattern6.tex"), true); + + Lua * l = new Lua(); + LuaInput::pushconstruct(l); + LuaOutput::pushconstruct(l); + l->open_math(); + l->declarefunc("print", myprint); + l->load(&Input("particules.lua")); + l->call("testing"); + delete l; Color AlphaBlue(AQUA); AlphaBlue.A = 50; @@ -537,13 +560,15 @@ virtual int startup() throw (GeneralException) { gl->Enter2DMode(); - sh->tbox(mytex, 50, 50, 561, 561, BLACK, RED, LIME, BLUE); + w->fulldraw(); + +/* sh->tbox(mytex, 50, 50, 561, 561, BLACK, RED, LIME, BLUE); */ // sh->box(400, 100, 450, 150, BLACK, RED, LIME, BLUE); // sh->box(5, 5, 150, 80, CORNFLOWERBLUE, DEEPSKYBLUE, MIDNIGHTBLUE, NAVY); - mogltk::ColorP::Max.A = 100; +/* mogltk::ColorP::Max.A = 100; sh->box(5, 5, 400, 300, CORNFLOWERBLUE, DEEPSKYBLUE, MIDNIGHTBLUE, NAVY); - mogltk::ColorP::Max.A = 255; + mogltk::ColorP::Max.A = 255; */ // font->setshadow(1); // font->putcursor(10, 30); // font->setcolor(WHITE); @@ -573,11 +598,11 @@ virtual int startup() throw (GeneralException) { font->printf("my: %i\n", mogltk::engine::mouseY()); font->printf("t: %.2fs\n", SDL_GetTicks() / 1000.0); - p->draw(mogltk::engine::mouseX() - 8, mogltk::engine::mouseY() - 8); +/* p->draw(mogltk::engine::mouseX() - 8, mogltk::engine::mouseY() - 8); */ s->draw(mogltk::engine::mouseX() - 8, mogltk::engine::mouseY() - 6); // sh->line(320, 240, mogltk::engine::mouseX(), mogltk::engine::mouseY()); - mogltk::ColorP::Min.A = 200; +/* mogltk::ColorP::Min.A = 200; */ // p->draw(sx1, sy1, AlphaBlue); // p->draw(sx2, sy2, AlphaBlue); -- cgit v1.2.3