diff options
author | pixel <pixel> | 2004-11-27 21:48:01 +0000 |
---|---|---|
committer | pixel <pixel> | 2004-11-27 21:48:01 +0000 |
commit | f9969775e6d2f798a3bfea5c58fc5478dad07eae (patch) | |
tree | c82d4ed9c87d7e09c71be3554ac5d0486c774eda | |
parent | 1ae229afb9bff4a3636c08632032b509e1e80ec4 (diff) |
Large dos2unix commit...
-rw-r--r-- | include/base.h | 114 | ||||
-rw-r--r-- | include/engine.h | 202 | ||||
-rw-r--r-- | include/font.h | 166 | ||||
-rw-r--r-- | include/gettext.h | 126 | ||||
-rw-r--r-- | include/glbase.h | 106 | ||||
-rw-r--r-- | include/glfont.h | 76 | ||||
-rw-r--r-- | include/glshape.h | 114 | ||||
-rw-r--r-- | include/glsprite.h | 88 | ||||
-rw-r--r-- | include/glwidgets.h | 76 | ||||
-rw-r--r-- | include/mcolor.h | 92 | ||||
-rw-r--r-- | include/shape.h | 394 | ||||
-rw-r--r-- | include/sprite.h | 146 | ||||
-rw-r--r-- | include/texture.h | 126 | ||||
-rw-r--r-- | include/widgets.h | 542 | ||||
-rw-r--r-- | lib/base.cc | 356 | ||||
-rw-r--r-- | lib/engine.cc | 818 | ||||
-rw-r--r-- | lib/font.cc | 1410 | ||||
-rw-r--r-- | lib/glbase.cc | 534 | ||||
-rw-r--r-- | lib/glfont.cc | 144 | ||||
-rw-r--r-- | lib/glshape.cc | 506 | ||||
-rw-r--r-- | lib/glsprite.cc | 210 | ||||
-rw-r--r-- | lib/glwidgets.cc | 58 | ||||
-rw-r--r-- | lib/mcolor.cc | 132 | ||||
-rw-r--r-- | lib/shape.cc | 1856 | ||||
-rw-r--r-- | lib/sprite.cc | 380 | ||||
-rw-r--r-- | lib/texture.cc | 722 | ||||
-rw-r--r-- | lib/widgets.cc | 2364 | ||||
-rw-r--r-- | src/test.cc | 2616 |
28 files changed, 7237 insertions, 7237 deletions
diff --git a/include/base.h b/include/base.h index 8573cff..c08c055 100644 --- a/include/base.h +++ b/include/base.h @@ -1,57 +1,57 @@ -/*
- * mogltk
- * Copyright (C) 1999-2004 Nicolas "Pixel" Noble
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- */
-
-/* $Id: base.h,v 1.9 2004-11-27 21:44:50 pixel Exp $ */
-
-#ifndef __BASE_H__
-#define __BASE_H__
-
-#include <SDL.h>
-#include <Exceptions.h>
-#include <texture.h>
-
-namespace mogltk {
- struct rect {
- int x, y, w, h;
- };
- class base : public Base {
- public:
- base(int w = 640, int h = 480, int flags = 0) throw(GeneralException);
- virtual ~base();
- int GetWidth(void);
- int GetHeight(void);
- virtual void Flip(bool clear = true);
- SDL_Surface * getsurface();
- 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);
- void ToggleFullscreen();
- virtual texture * GrabTexture();
- virtual SDL_Surface * GrabSurface();
- protected:
- base(int, int, int, int);
- void setsurface(SDL_Surface *) throw (GeneralException);
- private:
- int width, height;
- SDL_Surface * surface;
- };
-};
-
-#endif
+/* + * mogltk + * Copyright (C) 1999-2004 Nicolas "Pixel" Noble + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +/* $Id: base.h,v 1.10 2004-11-27 21:48:01 pixel Exp $ */ + +#ifndef __BASE_H__ +#define __BASE_H__ + +#include <SDL.h> +#include <Exceptions.h> +#include <texture.h> + +namespace mogltk { + struct rect { + int x, y, w, h; + }; + class base : public Base { + public: + base(int w = 640, int h = 480, int flags = 0) throw(GeneralException); + virtual ~base(); + int GetWidth(void); + int GetHeight(void); + virtual void Flip(bool clear = true); + SDL_Surface * getsurface(); + 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); + void ToggleFullscreen(); + virtual texture * GrabTexture(); + virtual SDL_Surface * GrabSurface(); + protected: + base(int, int, int, int); + void setsurface(SDL_Surface *) throw (GeneralException); + private: + int width, height; + SDL_Surface * surface; + }; +}; + +#endif diff --git a/include/engine.h b/include/engine.h index 95b12f7..73d12dd 100644 --- a/include/engine.h +++ b/include/engine.h @@ -1,101 +1,101 @@ -/*
- * mogltk
- * Copyright (C) 1999-2004 Nicolas "Pixel" Noble
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- */
-
-/* $Id: engine.h,v 1.19 2004-11-27 21:44:50 pixel Exp $ */
-
-#ifndef __ENGINE_H__
-#define __ENGINE_H__
-
-#include <SDL.h>
-#include <Handle.h>
-#include <Exceptions.h>
-#include <base.h>
-#include <glbase.h>
-#include <widgets.h>
-
-namespace mogltk {
- class engine : public Base {
- public:
- class keyevent : public Base {
- public:
- keyevent();
- virtual ~keyevent();
- virtual void down(SDL_keysym) = 0;
- virtual void up(SDL_keysym) = 0;
- protected:
- keyevent * old_handler, * new_handler;
- };
- class mouseevent : public Base {
- public:
- mouseevent();
- virtual ~mouseevent();
- virtual void move(SDL_MouseMotionEvent) = 0;
- virtual void action(SDL_MouseButtonEvent) = 0;
- protected:
- mouseevent * old_handler, * new_handler;
- };
- static int setup() throw(GeneralException);
- static int postsetup() throw(GeneralException);
- static int GetInited();
- static SDL_RWops * RWFromHandle(Handle *) throw (GeneralException);
- static void pollevents() throw (GeneralException);
- static void setappactive(bool);
- static bool getappactive();
- static void setcursorvisible(bool);
- static bool getcursorvisible();
- static void quit();
- static bool quitrequested();
- static int mouseX();
- static int mouseY();
- static int mouseZ();
- static void setmouseX(int);
- static void setmouseY(int);
- static void setmouseZ(int);
- static int mousebuttons();
- static double FPS();
- static void lockmouse();
- static void unlockmouse();
- static void setkeyevent(keyevent *);
- static void setmouseevent(mouseevent *);
- static keyevent * getkeyevent();
- static mouseevent * getmouseevent();
- static glbase * glbase_o;
- static base * base_o;
- static widget * root;
- private:
- static bool inited;
- static bool postsetuped;
- static bool appactive;
- static bool cursorvisible;
- static bool quitrequest;
- static int mx;
- static int my;
- static int mz;
- static int mbuttons;
- static int frames;
- static double curfps;
- static Uint32 curticks;
- static int locked;
- static keyevent * keyevent_h;
- static mouseevent * mouseevent_h;
- static void updatemouse();
- };
-};
-
-#endif
+/* + * mogltk + * Copyright (C) 1999-2004 Nicolas "Pixel" Noble + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +/* $Id: engine.h,v 1.20 2004-11-27 21:48:01 pixel Exp $ */ + +#ifndef __ENGINE_H__ +#define __ENGINE_H__ + +#include <SDL.h> +#include <Handle.h> +#include <Exceptions.h> +#include <base.h> +#include <glbase.h> +#include <widgets.h> + +namespace mogltk { + class engine : public Base { + public: + class keyevent : public Base { + public: + keyevent(); + virtual ~keyevent(); + virtual void down(SDL_keysym) = 0; + virtual void up(SDL_keysym) = 0; + protected: + keyevent * old_handler, * new_handler; + }; + class mouseevent : public Base { + public: + mouseevent(); + virtual ~mouseevent(); + virtual void move(SDL_MouseMotionEvent) = 0; + virtual void action(SDL_MouseButtonEvent) = 0; + protected: + mouseevent * old_handler, * new_handler; + }; + static int setup() throw(GeneralException); + static int postsetup() throw(GeneralException); + static int GetInited(); + static SDL_RWops * RWFromHandle(Handle *) throw (GeneralException); + static void pollevents() throw (GeneralException); + static void setappactive(bool); + static bool getappactive(); + static void setcursorvisible(bool); + static bool getcursorvisible(); + static void quit(); + static bool quitrequested(); + static int mouseX(); + static int mouseY(); + static int mouseZ(); + static void setmouseX(int); + static void setmouseY(int); + static void setmouseZ(int); + static int mousebuttons(); + static double FPS(); + static void lockmouse(); + static void unlockmouse(); + static void setkeyevent(keyevent *); + static void setmouseevent(mouseevent *); + static keyevent * getkeyevent(); + static mouseevent * getmouseevent(); + static glbase * glbase_o; + static base * base_o; + static widget * root; + private: + static bool inited; + static bool postsetuped; + static bool appactive; + static bool cursorvisible; + static bool quitrequest; + static int mx; + static int my; + static int mz; + static int mbuttons; + static int frames; + static double curfps; + static Uint32 curticks; + static int locked; + static keyevent * keyevent_h; + static mouseevent * mouseevent_h; + static void updatemouse(); + }; +}; + +#endif diff --git a/include/font.h b/include/font.h index d69fc50..9530a37 100644 --- a/include/font.h +++ b/include/font.h @@ -1,83 +1,83 @@ -/*
- * mogltk
- * Copyright (C) 1999-2004 Nicolas "Pixel" Noble
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- */
-
-/* $Id: font.h,v 1.9 2004-11-27 21:44:50 pixel Exp $ */
-
-#ifndef __FONT_H__
-#define __FONT_H__
-
-#include <SDL.h>
-#include <stdarg.h>
-#include <BString.h>
-#include <Handle.h>
-#include <texture.h>
-#include <mcolor.h>
-#include <base.h>
-
-namespace mogltk {
- class font : public Base {
- public:
- font(Handle *);
- virtual ~font();
- virtual void drawentry(Uint16, int, int, ColorP = WHITE);
- void drawtotex(texture *, Uint16, int, int, ColorP = WHITE);
- void putcursor(int, int);
- void putentry(Uint16, ColorP = WHITE);
- void putentryontex(texture *, Uint16, ColorP = WHITE);
- void drawchar(char, ColorP = WHITE);
- void drawcharontex(texture *, char, ColorP = WHITE);
- void newline(void);
- int printf(const ugly_string &, ...);
- int printf(const char *, ...);
- int printf(const ugly_string &, va_list);
- rect size(const ugly_string &, ...);
- rect size(const char *, ...);
- rect size(const ugly_string &, va_list);
- rect printtotex(texture *, const ugly_string &, ...);
- rect printtotex(texture *, const char *, ...);
- rect printtotex(texture *, const ugly_string &, va_list);
- texture * printtex(rect *, const ugly_string &, ...);
- texture * printtex(rect *, const char *, ...);
- texture * printtex(rect *, const ugly_string &, va_list);
- void setcolor(ColorP);
- void setshadow(int);
- void setwspace(int);
- int findchar(char) const;
- int singletextsize(const String &) const;
-
- protected:
- Uint8 * sizes;
- Uint16 nbentries, nbcT, nbT;
- Uint8 flags, maxX, maxY, nbcU, nbcV, base, inter;
- int cx, cy, ox;
- ColorP textcolor;
- int shadow, wspace;
- texture * alloctexture();
- void Bind(int);
- private:
- void checknbind(int, ColorP);
- texture ** fonttex;
- texture ** fontcache[16];
- Uint16 * corresp;
- };
- extern font * SystemFont;
- extern font * FixedFont;
-};
-
-#endif
+/* + * mogltk + * Copyright (C) 1999-2004 Nicolas "Pixel" Noble + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +/* $Id: font.h,v 1.10 2004-11-27 21:48:01 pixel Exp $ */ + +#ifndef __FONT_H__ +#define __FONT_H__ + +#include <SDL.h> +#include <stdarg.h> +#include <BString.h> +#include <Handle.h> +#include <texture.h> +#include <mcolor.h> +#include <base.h> + +namespace mogltk { + class font : public Base { + public: + font(Handle *); + virtual ~font(); + virtual void drawentry(Uint16, int, int, ColorP = WHITE); + void drawtotex(texture *, Uint16, int, int, ColorP = WHITE); + void putcursor(int, int); + void putentry(Uint16, ColorP = WHITE); + void putentryontex(texture *, Uint16, ColorP = WHITE); + void drawchar(char, ColorP = WHITE); + void drawcharontex(texture *, char, ColorP = WHITE); + void newline(void); + int printf(const ugly_string &, ...); + int printf(const char *, ...); + int printf(const ugly_string &, va_list); + rect size(const ugly_string &, ...); + rect size(const char *, ...); + rect size(const ugly_string &, va_list); + rect printtotex(texture *, const ugly_string &, ...); + rect printtotex(texture *, const char *, ...); + rect printtotex(texture *, const ugly_string &, va_list); + texture * printtex(rect *, const ugly_string &, ...); + texture * printtex(rect *, const char *, ...); + texture * printtex(rect *, const ugly_string &, va_list); + void setcolor(ColorP); + void setshadow(int); + void setwspace(int); + int findchar(char) const; + int singletextsize(const String &) const; + + protected: + Uint8 * sizes; + Uint16 nbentries, nbcT, nbT; + Uint8 flags, maxX, maxY, nbcU, nbcV, base, inter; + int cx, cy, ox; + ColorP textcolor; + int shadow, wspace; + texture * alloctexture(); + void Bind(int); + private: + void checknbind(int, ColorP); + texture ** fonttex; + texture ** fontcache[16]; + Uint16 * corresp; + }; + extern font * SystemFont; + extern font * FixedFont; +}; + +#endif diff --git a/include/gettext.h b/include/gettext.h index c4fe094..b530d9c 100644 --- a/include/gettext.h +++ b/include/gettext.h @@ -1,63 +1,63 @@ -/* Convenience header for conditional use of GNU <libintl.h>.
- Copyright (C) 1995-1998, 2000-2002 Free Software Foundation, Inc.
-
- This program is free software; you can redistribute it and/or modify it
- under the terms of the GNU Library General Public License as published
- by the Free Software Foundation; either version 2, or (at your option)
- any later version.
-
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- Library General Public License for more details.
-
- You should have received a copy of the GNU Library General Public
- License along with this program; if not, write to the Free Software
- Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
- USA. */
-
-#ifndef _LIBGETTEXT_H
-#define _LIBGETTEXT_H 1
-
-/* NLS can be disabled through the configure --disable-nls option. */
-#if ENABLE_NLS
-
-/* Get declarations of GNU message catalog functions. */
-# include <libintl.h>
-
-#else
-
-/* Disabled NLS.
- The casts to 'const char *' serve the purpose of producing warnings
- for invalid uses of the value returned from these functions.
- On pre-ANSI systems without 'const', the config.h file is supposed to
- contain "#define const". */
-# define gettext(Msgid) ((const char *) (Msgid))
-# define dgettext(Domainname, Msgid) ((const char *) (Msgid))
-# define dcgettext(Domainname, Msgid, Category) ((const char *) (Msgid))
-# define ngettext(Msgid1, Msgid2, N) \
- ((N) == 1 ? (const char *) (Msgid1) : (const char *) (Msgid2))
-# define dngettext(Domainname, Msgid1, Msgid2, N) \
- ((N) == 1 ? (const char *) (Msgid1) : (const char *) (Msgid2))
-# define dcngettext(Domainname, Msgid1, Msgid2, N, Category) \
- ((N) == 1 ? (const char *) (Msgid1) : (const char *) (Msgid2))
-# define textdomain(Domainname) ((const char *) (Domainname))
-# define bindtextdomain(Domainname, Dirname) ((const char *) (Dirname))
-# define bind_textdomain_codeset(Domainname, Codeset) ((const char *) (Codeset))
-# define setlocale(a, b)
-
-#endif
-
-/* A pseudo function call that serves as a marker for the automated
- extraction of messages, but does not call gettext(). The run-time
- translation is done at a different place in the code.
- The argument, String, should be a literal string. Concatenated strings
- and other string expressions won't work.
- The macro's expansion is not parenthesized, so that it is suitable as
- 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 */
+/* Convenience header for conditional use of GNU <libintl.h>. + Copyright (C) 1995-1998, 2000-2002 Free Software Foundation, Inc. + + This program is free software; you can redistribute it and/or modify it + under the terms of the GNU Library General Public License as published + by the Free Software Foundation; either version 2, or (at your option) + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public + License along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, + USA. */ + +#ifndef _LIBGETTEXT_H +#define _LIBGETTEXT_H 1 + +/* NLS can be disabled through the configure --disable-nls option. */ +#if ENABLE_NLS + +/* Get declarations of GNU message catalog functions. */ +# include <libintl.h> + +#else + +/* Disabled NLS. + The casts to 'const char *' serve the purpose of producing warnings + for invalid uses of the value returned from these functions. + On pre-ANSI systems without 'const', the config.h file is supposed to + contain "#define const". */ +# define gettext(Msgid) ((const char *) (Msgid)) +# define dgettext(Domainname, Msgid) ((const char *) (Msgid)) +# define dcgettext(Domainname, Msgid, Category) ((const char *) (Msgid)) +# define ngettext(Msgid1, Msgid2, N) \ + ((N) == 1 ? (const char *) (Msgid1) : (const char *) (Msgid2)) +# define dngettext(Domainname, Msgid1, Msgid2, N) \ + ((N) == 1 ? (const char *) (Msgid1) : (const char *) (Msgid2)) +# define dcngettext(Domainname, Msgid1, Msgid2, N, Category) \ + ((N) == 1 ? (const char *) (Msgid1) : (const char *) (Msgid2)) +# define textdomain(Domainname) ((const char *) (Domainname)) +# define bindtextdomain(Domainname, Dirname) ((const char *) (Dirname)) +# define bind_textdomain_codeset(Domainname, Codeset) ((const char *) (Codeset)) +# define setlocale(a, b) + +#endif + +/* A pseudo function call that serves as a marker for the automated + extraction of messages, but does not call gettext(). The run-time + translation is done at a different place in the code. + The argument, String, should be a literal string. Concatenated strings + and other string expressions won't work. + The macro's expansion is not parenthesized, so that it is suitable as + 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 4528d78..2936549 100644 --- a/include/glbase.h +++ b/include/glbase.h @@ -1,53 +1,53 @@ -/*
- * mogltk
- * Copyright (C) 1999-2004 Nicolas "Pixel" Noble
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- */
-
-/* $Id: glbase.h,v 1.11 2004-11-27 21:44:50 pixel Exp $ */
-
-#ifndef __GLBASE_H__
-#define __GLBASE_H__
-
-#include <SDL.h>
-#include <SDL_opengl.h>
-#include <Exceptions.h>
-#include <base.h>
-
-namespace mogltk {
- class glbase : public base {
- public:
- glbase(int w = 640, int h = 480, int flags = 0) throw(GeneralException);
- virtual ~glbase();
- virtual void Enter2DMode(void);
- virtual void Leave2DMode(void);
- virtual void Flip(bool clear = true);
- 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);
- virtual texture * GrabTexture();
- virtual SDL_Surface * GrabSurface();
- private:
- int twoD;
- GLdouble ratio, fovy;
- };
-};
-
-#endif
+/* + * mogltk + * Copyright (C) 1999-2004 Nicolas "Pixel" Noble + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +/* $Id: glbase.h,v 1.12 2004-11-27 21:48:01 pixel Exp $ */ + +#ifndef __GLBASE_H__ +#define __GLBASE_H__ + +#include <SDL.h> +#include <SDL_opengl.h> +#include <Exceptions.h> +#include <base.h> + +namespace mogltk { + class glbase : public base { + public: + glbase(int w = 640, int h = 480, int flags = 0) throw(GeneralException); + virtual ~glbase(); + virtual void Enter2DMode(void); + virtual void Leave2DMode(void); + virtual void Flip(bool clear = true); + 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); + virtual texture * GrabTexture(); + virtual SDL_Surface * GrabSurface(); + private: + int twoD; + GLdouble ratio, fovy; + }; +}; + +#endif diff --git a/include/glfont.h b/include/glfont.h index bd36f68..b0ab2f6 100644 --- a/include/glfont.h +++ b/include/glfont.h @@ -1,38 +1,38 @@ -/*
- * mogltk
- * Copyright (C) 1999-2004 Nicolas "Pixel" Noble
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- */
-
-/* $Id: glfont.h,v 1.15 2004-11-27 21:44:50 pixel Exp $ */
-
-#ifndef __GLFONT_H__
-#define __GLFONT_H__
-
-#include <SDL.h>
-#include <font.h>
-#include <mcolor.h>
-
-namespace mogltk {
- class glfont : public font {
- public:
- glfont(Handle *);
- virtual ~glfont();
- void drawentry(Uint16, int, int, ColorP = WHITE);
- };
-};
-
-#endif
+/* + * mogltk + * Copyright (C) 1999-2004 Nicolas "Pixel" Noble + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +/* $Id: glfont.h,v 1.16 2004-11-27 21:48:01 pixel Exp $ */ + +#ifndef __GLFONT_H__ +#define __GLFONT_H__ + +#include <SDL.h> +#include <font.h> +#include <mcolor.h> + +namespace mogltk { + class glfont : public font { + public: + glfont(Handle *); + virtual ~glfont(); + void drawentry(Uint16, int, int, ColorP = WHITE); + }; +}; + +#endif diff --git a/include/glshape.h b/include/glshape.h index 0226ac2..0647633 100644 --- a/include/glshape.h +++ b/include/glshape.h @@ -1,57 +1,57 @@ -/*
- * mogltk
- * Copyright (C) 1999-2004 Nicolas "Pixel" Noble
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- */
-
-/* $Id: glshape.h,v 1.14 2004-11-27 21:44:50 pixel Exp $ */
-
-#ifndef __GLSHAPE_H__
-#define __GLSHAPE_H__
-
-#include <Exceptions.h>
-#include <BString.h>
-#include <mcolor.h>
-#include <texture.h>
-#include <shape.h>
-
-namespace mogltk {
- class glshape : public shape {
- public:
- virtual void pixel(int x, int y, ColorP = WHITE);
- 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 obox(int x1, int y1, int x2, int y2, ColorP = DOS_WHITE);
- virtual void obox(int x1, int y1, int x2, int y2, ColorP, ColorP, ColorP, ColorP);
- virtual void hline(int x1, int x2, int y, ColorP = DOS_WHITE);
- virtual void hline(int x1, int x2, int y, ColorP, ColorP);
- virtual void vline(int x, int y1, int y2, ColorP = DOS_WHITE);
- virtual void vline(int x, int y1, int y2, 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);
- virtual void fdraw(fill * f, ColorP = WHITE, int sx = 0, int sy = 0);
- virtual void sdraw(fill * f, ColorP = WHITE, int sx = 0, int sy = 0);
- private:
- bool Enter(bool);
- virtual bool Enter();
- virtual void Leave(bool);
- };
-};
-
-#endif
+/* + * mogltk + * Copyright (C) 1999-2004 Nicolas "Pixel" Noble + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +/* $Id: glshape.h,v 1.15 2004-11-27 21:48:01 pixel Exp $ */ + +#ifndef __GLSHAPE_H__ +#define __GLSHAPE_H__ + +#include <Exceptions.h> +#include <BString.h> +#include <mcolor.h> +#include <texture.h> +#include <shape.h> + +namespace mogltk { + class glshape : public shape { + public: + virtual void pixel(int x, int y, ColorP = WHITE); + 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 obox(int x1, int y1, int x2, int y2, ColorP = DOS_WHITE); + virtual void obox(int x1, int y1, int x2, int y2, ColorP, ColorP, ColorP, ColorP); + virtual void hline(int x1, int x2, int y, ColorP = DOS_WHITE); + virtual void hline(int x1, int x2, int y, ColorP, ColorP); + virtual void vline(int x, int y1, int y2, ColorP = DOS_WHITE); + virtual void vline(int x, int y1, int y2, 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); + virtual void fdraw(fill * f, ColorP = WHITE, int sx = 0, int sy = 0); + virtual void sdraw(fill * f, ColorP = WHITE, int sx = 0, int sy = 0); + private: + bool Enter(bool); + virtual bool Enter(); + virtual void Leave(bool); + }; +}; + +#endif diff --git a/include/glsprite.h b/include/glsprite.h index 3758a3c..91c3f76 100644 --- a/include/glsprite.h +++ b/include/glsprite.h @@ -1,44 +1,44 @@ -/*
- * mogltk
- * Copyright (C) 1999-2004 Nicolas "Pixel" Noble
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- */
-
-/* $Id: glsprite.h,v 1.6 2004-11-27 21:44:50 pixel Exp $ */
-
-#ifndef __GLSPRITE_H__
-#define __GLSPRITE_H__
-
-#include <vector>
-#include <Exceptions.h>
-#include <Handle.h>
-#include <mcolor.h>
-#include <texture.h>
-#include <sprite.h>
-
-namespace mogltk {
- class glSprite : public Sprite {
- public:
- glSprite(Handle *, int, int);
- glSprite(Uint8 *, int, int);
- virtual ~glSprite();
- virtual void draw(int, int, ColorP = WHITE, float = 1.0, float = 1.0);
- void bindcorner(float, float);
- virtual void drawrotate(int, int, double, ColorP = WHITE);
- };
-};
-
-#endif
+/* + * mogltk + * Copyright (C) 1999-2004 Nicolas "Pixel" Noble + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +/* $Id: glsprite.h,v 1.7 2004-11-27 21:48:01 pixel Exp $ */ + +#ifndef __GLSPRITE_H__ +#define __GLSPRITE_H__ + +#include <vector> +#include <Exceptions.h> +#include <Handle.h> +#include <mcolor.h> +#include <texture.h> +#include <sprite.h> + +namespace mogltk { + class glSprite : public Sprite { + public: + glSprite(Handle *, int, int); + glSprite(Uint8 *, int, int); + virtual ~glSprite(); + virtual void draw(int, int, ColorP = WHITE, float = 1.0, float = 1.0); + void bindcorner(float, float); + virtual void drawrotate(int, int, double, ColorP = WHITE); + }; +}; + +#endif diff --git a/include/glwidgets.h b/include/glwidgets.h index f4c6c39..cd87a5f 100644 --- a/include/glwidgets.h +++ b/include/glwidgets.h @@ -1,38 +1,38 @@ -/*
- * mogltk
- * Copyright (C) 1999-2004 Nicolas "Pixel" Noble
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- */
-
-/* $Id: glwidgets.h,v 1.8 2004-11-27 21:44:50 pixel Exp $ */
-
-#ifndef __GLWIDGETS_H__
-#define __GLWIDGETS_H__
-
-#include <widgets.h>
-
-namespace mogltk {
- namespace widgets {
- class glRoot : public Root {
- public:
- glRoot(shape *);
- protected:
- virtual void draw();
- };
- };
-};
-
-#endif
+/* + * mogltk + * Copyright (C) 1999-2004 Nicolas "Pixel" Noble + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +/* $Id: glwidgets.h,v 1.9 2004-11-27 21:48:01 pixel Exp $ */ + +#ifndef __GLWIDGETS_H__ +#define __GLWIDGETS_H__ + +#include <widgets.h> + +namespace mogltk { + namespace widgets { + class glRoot : public Root { + public: + glRoot(shape *); + protected: + virtual void draw(); + }; + }; +}; + +#endif diff --git a/include/mcolor.h b/include/mcolor.h index e718af4..b383fee 100644 --- a/include/mcolor.h +++ b/include/mcolor.h @@ -1,46 +1,46 @@ -/*
- * mogltk
- * Copyright (C) 1999-2004 Nicolas "Pixel" Noble
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- */
-
-/* $Id: mcolor.h,v 1.8 2004-11-27 21:44:50 pixel Exp $ */
-
-#ifndef __MCOLOR_H__
-#define __MCOLOR_H__
-
-#include <SDL.h>
-#include <Exceptions.h>
-#include <Color.h>
-
-namespace mogltk {
- class ColorP : public Base {
- public:
- ColorP(const Color & = WHITE);
- ColorP(Uint8, Uint8, Uint8, Uint8);
- bool operator==(const ColorP &);
- Color * operator->();
- void Bind();
- void Norm();
- Uint32 toSDL(SDL_PixelFormat * = 0);
- void fromSDL(Uint32, SDL_PixelFormat * = 0);
- static Color Min;
- static Color Max;
- Color c;
- };
-};
-
-#endif
+/* + * mogltk + * Copyright (C) 1999-2004 Nicolas "Pixel" Noble + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +/* $Id: mcolor.h,v 1.9 2004-11-27 21:48:01 pixel Exp $ */ + +#ifndef __MCOLOR_H__ +#define __MCOLOR_H__ + +#include <SDL.h> +#include <Exceptions.h> +#include <Color.h> + +namespace mogltk { + class ColorP : public Base { + public: + ColorP(const Color & = WHITE); + ColorP(Uint8, Uint8, Uint8, Uint8); + bool operator==(const ColorP &); + Color * operator->(); + void Bind(); + void Norm(); + Uint32 toSDL(SDL_PixelFormat * = 0); + void fromSDL(Uint32, SDL_PixelFormat * = 0); + static Color Min; + static Color Max; + Color c; + }; +}; + +#endif diff --git a/include/shape.h b/include/shape.h index d5932cd..72feba2 100644 --- a/include/shape.h +++ b/include/shape.h @@ -1,197 +1,197 @@ -/*
- * mogltk
- * Copyright (C) 1999-2004 Nicolas "Pixel" Noble
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- */
-
-/* $Id: shape.h,v 1.15 2004-11-27 21:44:50 pixel Exp $ */
-
-#ifndef __SHAPE_H__
-#define __SHAPE_H__
-
-#include <vector>
-#include <Exceptions.h>
-#include <BString.h>
-#include <mcolor.h>
-#include <texture.h>
-
-namespace mogltk {
- typedef enum {
- LEFT,
- CENTER,
- RIGHT
- } align_t;
- class fillwalker : public Base {
- public:
- fillwalker();
- virtual ~fillwalker();
- virtual void step(int x, int y);
- };
- class segwalker : public Base {
- public:
- segwalker();
- virtual ~segwalker();
- virtual void step(int x1, int y1, int x2, int y2);
- };
- class fill : public Base {
- public:
- fill();
- virtual ~fill();
- void walk(fillwalker *);
- void swalk(segwalker *);
- void insert(int, int, int, int);
- void insertfix(int, int);
- int GetMinX() const;
- int GetMinY() const;
- int GetMaxX() const;
- int GetMaxY() const;
- texture * GetTexture();
- texture * Talloc();
- texture * GetSTexture();
- texture * STalloc();
- Color last;
- private:
- void insert(int, int);
- struct segment {
- int x1, y1, x2, y2;
- };
- class sline : public Base {
- public:
- sline(int, fill *);
- virtual ~sline();
- int GetY() const;
- void insert(int, int);
- void insertfix(int, int);
- void walk(fillwalker *);
- private:
- class point : public Base {
- public:
- point(int, sline *);
- virtual ~point();
- int GetX() const;
- int GetY() const;
- void walk(fillwalker *);
- point * look(int);
- private:
- int x;
- point * next;
- sline * header;
- };
- int y;
- sline * next;
- fill * header;
- sline * look(int);
- protected:
- point * pheader;
- friend class point;
- private:
- int count() const;
- };
- int minX, minY, maxX, maxY;
- texture * cached;
- texture * scached;
- std::vector<segment> segments;
- protected:
- sline * header;
- friend class sline;
- };
- class shape : public Base {
- public:
- shape(SDL_Surface * = 0);
- virtual void pixel(int x, int y, ColorP = WHITE);
- virtual void circle(int x, int y, int r, ColorP = WHITE);
- virtual void pcircle(int x, int y, int r, ColorP = WHITE);
- virtual fill * fcircle(int x, int y, int r);
- virtual void fdraw(fill *, ColorP = WHITE, int sx = 0, int sy = 0);
- virtual void sdraw(fill *, ColorP = WHITE, int sx = 0, int sy = 0);
- virtual void box(int x1, int y1, int x2, int y2, ColorP = DOS_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 obox(int x1, int y1, int x2, int y2, ColorP = DOS_WHITE);
- virtual void obox3d(int x1, int y1, int x2, int y2, ColorP = DOS_HIGH_WHITE, ColorP = DOS_GRAY, bool = false);
- virtual void obox(int x1, int y1, int x2, int y2, ColorP, ColorP, ColorP, ColorP);
- virtual void hline(int x1, int x2, int y, ColorP = DOS_WHITE);
- virtual void hline3d(int x1, int x2, int y, ColorP = DOS_HIGH_WHITE, ColorP = DOS_GRAY, bool = false);
- virtual void hline(int x1, int x2, int y, ColorP, ColorP);
- virtual void vline(int x, int y1, int y2, ColorP = DOS_WHITE);
- virtual void vline3d(int x, int y1, int y2, ColorP = DOS_HIGH_WHITE, ColorP = DOS_GRAY, bool = false);
- virtual void vline(int x, int y1, int y2, ColorP, ColorP);
- virtual void line(int x1, int y1, int x2, int y2, ColorP = WHITE);
- 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);
- virtual void window(int x1, int y1, int x2, int y2,
- const String & title = "",
- ColorP titlecolor = DOS_HIGH_WHITE,
- ColorP titlebackcolor = DOS_MAGENTA,
- ColorP front = DOS_WHITE,
- ColorP shade1 = DOS_HIGH_WHITE,
- ColorP shade2 = DOS_GRAY);
- virtual void text(int x, int y, const String &,
- ColorP textcolor = DOS_HIGH_WHITE,
- align_t align = LEFT);
- virtual void text3d(int x, int y, const String &,
- ColorP textcolor = DOS_BLACK,
- ColorP shade1 = DOS_HIGH_WHITE,
- ColorP shade2 = DOS_GRAY,
- align_t align = LEFT,
- bool bevel = false);
- virtual void button(int x1, int y1, int x2, int y2, const String &,
- bool bevel = false,
- ColorP front = DOS_WHITE,
- ColorP shade1 = DOS_HIGH_WHITE,
- ColorP shade2 = DOS_GRAY,
- ColorP round = DOS_BLACK,
- ColorP textcolor = DOS_BLACK,
- ColorP tshade1 = DOS_HIGH_WHITE,
- ColorP tshade2 = DOS_GRAY);
- SDL_Surface * GetSurf();
- private:
- void bsubline_1(int x1, int y1, int x2, int y2, ColorP = WHITE);
- void bsubline_2(int x1, int y1, int x2, int y2, ColorP = WHITE);
- void bsubline_3(int x1, int y1, int x2, int y2, ColorP = WHITE);
- void bsubline_4(int x1, int y1, int x2, int y2, ColorP = WHITE);
- virtual bool Enter();
- virtual void Leave(bool);
- SDL_Surface * surf;
- };
-
- class filldrawer : public fillwalker {
- public:
- filldrawer(fill *, texture *, ColorP = DOS_WHITE);
- virtual ~filldrawer();
- virtual void step(int x, int y);
- private:
- fill * f;
- texture * t;
- ColorP c;
- int oldx, oldy;
- };
- class segdrawer : public segwalker {
- public:
- segdrawer(fill *, texture *, ColorP = DOS_WHITE);
- virtual ~segdrawer();
- virtual void step(int x1, int y1, int x2, int y2);
- private:
- fill * f;
- texture * t;
- ColorP c;
- shape * sh;
- };
-};
-
-#endif
+/* + * mogltk + * Copyright (C) 1999-2004 Nicolas "Pixel" Noble + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +/* $Id: shape.h,v 1.16 2004-11-27 21:48:01 pixel Exp $ */ + +#ifndef __SHAPE_H__ +#define __SHAPE_H__ + +#include <vector> +#include <Exceptions.h> +#include <BString.h> +#include <mcolor.h> +#include <texture.h> + +namespace mogltk { + typedef enum { + LEFT, + CENTER, + RIGHT + } align_t; + class fillwalker : public Base { + public: + fillwalker(); + virtual ~fillwalker(); + virtual void step(int x, int y); + }; + class segwalker : public Base { + public: + segwalker(); + virtual ~segwalker(); + virtual void step(int x1, int y1, int x2, int y2); + }; + class fill : public Base { + public: + fill(); + virtual ~fill(); + void walk(fillwalker *); + void swalk(segwalker *); + void insert(int, int, int, int); + void insertfix(int, int); + int GetMinX() const; + int GetMinY() const; + int GetMaxX() const; + int GetMaxY() const; + texture * GetTexture(); + texture * Talloc(); + texture * GetSTexture(); + texture * STalloc(); + Color last; + private: + void insert(int, int); + struct segment { + int x1, y1, x2, y2; + }; + class sline : public Base { + public: + sline(int, fill *); + virtual ~sline(); + int GetY() const; + void insert(int, int); + void insertfix(int, int); + void walk(fillwalker *); + private: + class point : public Base { + public: + point(int, sline *); + virtual ~point(); + int GetX() const; + int GetY() const; + void walk(fillwalker *); + point * look(int); + private: + int x; + point * next; + sline * header; + }; + int y; + sline * next; + fill * header; + sline * look(int); + protected: + point * pheader; + friend class point; + private: + int count() const; + }; + int minX, minY, maxX, maxY; + texture * cached; + texture * scached; + std::vector<segment> segments; + protected: + sline * header; + friend class sline; + }; + class shape : public Base { + public: + shape(SDL_Surface * = 0); + virtual void pixel(int x, int y, ColorP = WHITE); + virtual void circle(int x, int y, int r, ColorP = WHITE); + virtual void pcircle(int x, int y, int r, ColorP = WHITE); + virtual fill * fcircle(int x, int y, int r); + virtual void fdraw(fill *, ColorP = WHITE, int sx = 0, int sy = 0); + virtual void sdraw(fill *, ColorP = WHITE, int sx = 0, int sy = 0); + virtual void box(int x1, int y1, int x2, int y2, ColorP = DOS_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 obox(int x1, int y1, int x2, int y2, ColorP = DOS_WHITE); + virtual void obox3d(int x1, int y1, int x2, int y2, ColorP = DOS_HIGH_WHITE, ColorP = DOS_GRAY, bool = false); + virtual void obox(int x1, int y1, int x2, int y2, ColorP, ColorP, ColorP, ColorP); + virtual void hline(int x1, int x2, int y, ColorP = DOS_WHITE); + virtual void hline3d(int x1, int x2, int y, ColorP = DOS_HIGH_WHITE, ColorP = DOS_GRAY, bool = false); + virtual void hline(int x1, int x2, int y, ColorP, ColorP); + virtual void vline(int x, int y1, int y2, ColorP = DOS_WHITE); + virtual void vline3d(int x, int y1, int y2, ColorP = DOS_HIGH_WHITE, ColorP = DOS_GRAY, bool = false); + virtual void vline(int x, int y1, int y2, ColorP, ColorP); + virtual void line(int x1, int y1, int x2, int y2, ColorP = WHITE); + 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); + virtual void window(int x1, int y1, int x2, int y2, + const String & title = "", + ColorP titlecolor = DOS_HIGH_WHITE, + ColorP titlebackcolor = DOS_MAGENTA, + ColorP front = DOS_WHITE, + ColorP shade1 = DOS_HIGH_WHITE, + ColorP shade2 = DOS_GRAY); + virtual void text(int x, int y, const String &, + ColorP textcolor = DOS_HIGH_WHITE, + align_t align = LEFT); + virtual void text3d(int x, int y, const String &, + ColorP textcolor = DOS_BLACK, + ColorP shade1 = DOS_HIGH_WHITE, + ColorP shade2 = DOS_GRAY, + align_t align = LEFT, + bool bevel = false); + virtual void button(int x1, int y1, int x2, int y2, const String &, + bool bevel = false, + ColorP front = DOS_WHITE, + ColorP shade1 = DOS_HIGH_WHITE, + ColorP shade2 = DOS_GRAY, + ColorP round = DOS_BLACK, + ColorP textcolor = DOS_BLACK, + ColorP tshade1 = DOS_HIGH_WHITE, + ColorP tshade2 = DOS_GRAY); + SDL_Surface * GetSurf(); + private: + void bsubline_1(int x1, int y1, int x2, int y2, ColorP = WHITE); + void bsubline_2(int x1, int y1, int x2, int y2, ColorP = WHITE); + void bsubline_3(int x1, int y1, int x2, int y2, ColorP = WHITE); + void bsubline_4(int x1, int y1, int x2, int y2, ColorP = WHITE); + virtual bool Enter(); + virtual void Leave(bool); + SDL_Surface * surf; + }; + + class filldrawer : public fillwalker { + public: + filldrawer(fill *, texture *, ColorP = DOS_WHITE); + virtual ~filldrawer(); + virtual void step(int x, int y); + private: + fill * f; + texture * t; + ColorP c; + int oldx, oldy; + }; + class segdrawer : public segwalker { + public: + segdrawer(fill *, texture *, ColorP = DOS_WHITE); + virtual ~segdrawer(); + virtual void step(int x1, int y1, int x2, int y2); + private: + fill * f; + texture * t; + ColorP c; + shape * sh; + }; +}; + +#endif diff --git a/include/sprite.h b/include/sprite.h index dd701f5..176f793 100644 --- a/include/sprite.h +++ b/include/sprite.h @@ -1,73 +1,73 @@ -/*
- * mogltk
- * Copyright (C) 1999-2004 Nicolas "Pixel" Noble
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- */
-
-/* $Id: sprite.h,v 1.12 2004-11-27 21:44:50 pixel Exp $ */
-
-#ifndef __SPRITE_H__
-#define __SPRITE_H__
-
-#include <vector>
-#include <Exceptions.h>
-#include <Handle.h>
-#include <mcolor.h>
-#include <texture.h>
-
-namespace mogltk {
- class Sprite : public Base {
- public:
- Sprite(Handle *, int, int) throw (GeneralException);
- Sprite(Uint8 *, int, int);
- virtual ~Sprite();
- virtual void draw(int, int, ColorP = WHITE, float = 1.0, float = 1.0);
- int GetSX();
- int GetSY();
- static Sprite * Cursor;
- protected:
- void Bind();
- int GetPX();
- int GetPY();
- private:
- class TexList : public Base {
- public:
- TexList(int);
- virtual ~TexList();
- Sprite * sprheader;
- const texture * GetTex() const;
- texture * GetTex();
- static const TexList * GetHead();
- const TexList * GetNext() const;
- TexList * GetNext();
- void Bind() const;
- SDL_Surface * GetSurface();
- private:
- texture * tex;
- static TexList * header;
- TexList * next, * prev;
- };
- Sprite * 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
+/* + * mogltk + * Copyright (C) 1999-2004 Nicolas "Pixel" Noble + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +/* $Id: sprite.h,v 1.13 2004-11-27 21:48:01 pixel Exp $ */ + +#ifndef __SPRITE_H__ +#define __SPRITE_H__ + +#include <vector> +#include <Exceptions.h> +#include <Handle.h> +#include <mcolor.h> +#include <texture.h> + +namespace mogltk { + class Sprite : public Base { + public: + Sprite(Handle *, int, int) throw (GeneralException); + Sprite(Uint8 *, int, int); + virtual ~Sprite(); + virtual void draw(int, int, ColorP = WHITE, float = 1.0, float = 1.0); + int GetSX(); + int GetSY(); + static Sprite * Cursor; + protected: + void Bind(); + int GetPX(); + int GetPY(); + private: + class TexList : public Base { + public: + TexList(int); + virtual ~TexList(); + Sprite * sprheader; + const texture * GetTex() const; + texture * GetTex(); + static const TexList * GetHead(); + const TexList * GetNext() const; + TexList * GetNext(); + void Bind() const; + SDL_Surface * GetSurface(); + private: + texture * tex; + static TexList * header; + TexList * next, * prev; + }; + Sprite * 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/texture.h b/include/texture.h index ceaacd1..3143b02 100644 --- a/include/texture.h +++ b/include/texture.h @@ -1,63 +1,63 @@ -/*
- * mogltk
- * Copyright (C) 1999-2004 Nicolas "Pixel" Noble
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- */
-
-/* $Id: texture.h,v 1.8 2004-11-27 21:44:50 pixel Exp $ */
-
-#ifndef __TEXTURE_H__
-#define __TEXTURE_H__
-
-#include <SDL.h>
-#include <SDL_opengl.h>
-#include <Handle.h>
-#include <Exceptions.h>
-#include <generic.h>
-
-namespace mogltk {
- class texture : public Base {
- public:
- texture(int, int, bool = false) throw (GeneralException);
- texture(Handle *, bool = false) throw (GeneralException);
- texture(int, int, int, int);
- virtual ~texture();
- SDL_Surface * GetSurface();
- Uint32 * GetPixels();
- SDL_PixelFormat * GetFormat();
- static SDL_Surface * LoadNTEX(Handle * h) throw (GeneralException) ;
- void Generate();
- void Bind(bool = true);
- GLuint GetWidth();
- GLuint GetHeight();
- static void Unbind(void);
- void Taint(void);
- static void Taintall(void);
- void DumpBMP(const String &);
- private:
- GLuint width, height, tex;
- bool texture_allocated;
- SDL_Surface * surface;
- bool planar, tainted, taintable;
- static texture * header;
- static texture * footer;
- texture * next, * prev;
- static texture * active;
- void recTaint(void);
- };
-};
-
-#endif
+/* + * mogltk + * Copyright (C) 1999-2004 Nicolas "Pixel" Noble + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +/* $Id: texture.h,v 1.9 2004-11-27 21:48:01 pixel Exp $ */ + +#ifndef __TEXTURE_H__ +#define __TEXTURE_H__ + +#include <SDL.h> +#include <SDL_opengl.h> +#include <Handle.h> +#include <Exceptions.h> +#include <generic.h> + +namespace mogltk { + class texture : public Base { + public: + texture(int, int, bool = false) throw (GeneralException); + texture(Handle *, bool = false) throw (GeneralException); + texture(int, int, int, int); + virtual ~texture(); + SDL_Surface * GetSurface(); + Uint32 * GetPixels(); + SDL_PixelFormat * GetFormat(); + static SDL_Surface * LoadNTEX(Handle * h) throw (GeneralException) ; + void Generate(); + void Bind(bool = true); + GLuint GetWidth(); + GLuint GetHeight(); + static void Unbind(void); + void Taint(void); + static void Taintall(void); + void DumpBMP(const String &); + private: + GLuint width, height, tex; + bool texture_allocated; + SDL_Surface * surface; + bool planar, tainted, taintable; + static texture * header; + static texture * footer; + texture * next, * prev; + static texture * active; + void recTaint(void); + }; +}; + +#endif diff --git a/include/widgets.h b/include/widgets.h index 600bcec..6469ea7 100644 --- a/include/widgets.h +++ b/include/widgets.h @@ -1,271 +1,271 @@ -/*
- * mogltk
- * Copyright (C) 1999-2004 Nicolas "Pixel" Noble
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- */
-
-/* $Id: widgets.h,v 1.7 2004-10-19 01:27:29 pixel Exp $ */
-
-#ifndef __WIDGETS_H__
-#define __WIDGETS_H__
-
-#include <Exceptions.h>
-#include <shape.h>
-#include <base.h>
-
-namespace mogltk {
- enum event_t {
- E_TIMER,
- E_MOUSE_START_DRAG,
- E_MOUSE_DRAG,
- E_MOUSE_DRAG_OVER,
- E_MOUSE_END_DRAG,
- E_MOUSE_END_DRAG_OVER,
- E_MOUSE_MOVE,
- E_MOUSE_OUT,
- E_MOUSE_DOWN,
- E_MOUSE_UP,
- E_MOUSE_CLICK,
- E_MOUSE_OUT_CLICK,
- E_MOUSE_DBL_CLICK,
- };
- class widget : public Base {
- public:
- virtual ~widget();
- virtual void move(int x, int y);
- virtual void resize(int sx, int sy);
- int GetX();
- int GetY();
- int GetH();
- int GetW();
- int GetAX();
- int GetAY();
- int GetAX2();
- int GetAY2();
- virtual mogltk::rect GetDrawRect();
- widget * Father();
- widget * Child();
- widget * Next();
- widget * Prev();
- widget * InnerPanel();
- virtual widget * find_widget(int x, int y);
- void fulldraw();
- shape * Shaper();
- void mainloop();
- void m_event(int x, int y, event_t event);
- bool inside(int x, int y);
- bool GetVisible();
- virtual void SetVisible(bool);
- bool GetEnabled();
- virtual void SetEnabled(bool);
- void MoveOnTop();
- static void check_timed_events();
- protected:
- widget(widget * father, int x, int y, int sx, int sy, int type, String name, shape *);
- virtual void draw();
- virtual bool process_event(int x, int y, event_t event);
- virtual void resize_notify();
- virtual widget * create_panel();
- void set_exclusive(widget *);
- void unset_exclusive(widget *);
- void add_out_move();
- void remove_out_move();
- void add_out_click();
- void remove_out_click();
- void set_timed_event(Uint32);
- void set_absolute_timed_event(Uint32);
- String caption;
- private:
- int x, y, sx, sy, ax, ay, ax2, ay2;
- widget * father, * next, * prev, * child, * last, * root, * panel;
- static widget * focused;
- int type;
- String name;
- shape * sh;
- widget * exclusive;
- bool visible;
- bool enabled;
- class timed_event {
- public:
- timed_event(widget * _w, Uint32 _t) : w(_w), t(_t) { }
- widget * w;
- Uint32 t;
- };
- static std::vector<timed_event> timed_events;
-
- void computeabs();
- void idraw();
- bool ievent(int x, int y, event_t event);
- void icomputeabs();
- void iresize_notify();
- };
-
- namespace widgets {
- class drawer : public Base {
- public:
- virtual void draw(widget *) = 0;
- };
-
- class action : public Base {
- public:
- virtual void do_action(widget *) = 0;
- };
-
- class Root : public widget {
- public:
- Root(shape *, drawer * = 0);
- void setdrawer(drawer *);
- protected:
- virtual void draw();
- drawer * dr;
- };
-
- class Panel : public widget {
- public:
- Panel(shape *, widget * father, int x, int y, int w, int h);
- };
-
- class Button : public widget {
- public:
- Button(action *, shape *, widget * father, int x, int y, int w, int h, const String & caption);
- protected:
- virtual void draw();
- virtual bool process_event(int, int, event_t);
- bool bevel;
- private:
- bool dragging;
- action * a;
- };
-
- class Label : public widget {
- public:
- Label(shape *, widget * father, int x, int y, int w, int h, const String & caption, const ColorP & color = BLACK);
- protected:
- virtual void draw();
- private:
- ColorP color;
- };
-
- class SmartBox : public widget {
- public:
- SmartBox(shape *, widget * father, int x, int y, int w, int h, const String & caption);
- virtual rect GetDrawRect();
- protected:
- virtual void draw();
- virtual bool process_event(int, int, event_t);
- private:
- int ox, oy, oax, oay;
- };
-
- class MsgBox : public SmartBox {
- public:
- MsgBox(shape *, widget * father, const String & caption, const String & text);
- virtual ~MsgBox();
- };
-
- class Frame : public widget {
- public:
- Frame(shape *, widget * father, int x, int y, int w, int h);
- virtual rect GetDrawRect();
- protected:
- virtual void draw();
- };
-
- class ContextMenu : public widget {
- public:
- ContextMenu(shape *, widget * father, int x, int y);
- virtual ~ContextMenu();
- void addnode(const String &, action *);
- void addline();
- void addsub(const String &, ContextMenu *);
- ContextMenu * createsub(const String &);
- virtual void move(int x, int y);
- virtual void resize(int w, int h);
- virtual void SetVisible(bool);
- virtual void SetEnabled(int i, bool);
- void StickyDisplay();
- protected:
- virtual void draw();
- virtual bool process_event(int x, int y, event_t event);
- class node : public Base {
- public:
- node(const String &, int x, int y, int w, int h, action * = 0, ContextMenu * sub = 0, ContextMenu * father = 0, bool = false);
- String GetCaption();
- action * GetAction();
- ContextMenu * GetSub();
- ContextMenu * GetFather();
- bool GetLine();
- bool GetEnabled();
- void SetEnabled(bool);
- int GetX();
- int GetY();
- int GetW();
- int GetH();
- private:
- String caption;
- int x, y, w, h;
- action * a;
- ContextMenu * sub, * father;
- bool line, enabled;
- };
-
- std::vector<node> nodes;
- int selected;
- ContextMenu * subselected;
- private:
- bool in_click, sticky;
-
- bool iin_click();
- };
-
- class Menu : public widget {
- public:
- Menu(shape *, widget * father);
- void addnode(const String &, action *);
- void addsub(const String &, ContextMenu * sub);
- ContextMenu * createsub(const String &);
- virtual void SetEnabled(int i, bool);
- void SetCaption(int i, const String &);
- protected:
- virtual void resize_notify();
- virtual void draw();
- virtual bool process_event(int, int, event_t);
- class node : public Base {
- public:
- node(const String & caption, ContextMenu * sub, action * a, int x);
- String GetCaption();
- void SetCaption(const String &);
- ContextMenu * GetSub();
- action * GetAction();
- int GetX();
- bool GetEnabled();
- void SetEnabled(bool);
- private:
- String caption;
- ContextMenu * sub;
- action * a;
- int x;
- bool enabled;
- };
- private:
- std::vector<node> nodes;
- int cur_x;
- int selected;
- };
- };
-};
-
-#endif
+/* + * mogltk + * Copyright (C) 1999-2004 Nicolas "Pixel" Noble + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +/* $Id: widgets.h,v 1.8 2004-11-27 21:48:01 pixel Exp $ */ + +#ifndef __WIDGETS_H__ +#define __WIDGETS_H__ + +#include <Exceptions.h> +#include <shape.h> +#include <base.h> + +namespace mogltk { + enum event_t { + E_TIMER, + E_MOUSE_START_DRAG, + E_MOUSE_DRAG, + E_MOUSE_DRAG_OVER, + E_MOUSE_END_DRAG, + E_MOUSE_END_DRAG_OVER, + E_MOUSE_MOVE, + E_MOUSE_OUT, + E_MOUSE_DOWN, + E_MOUSE_UP, + E_MOUSE_CLICK, + E_MOUSE_OUT_CLICK, + E_MOUSE_DBL_CLICK, + }; + class widget : public Base { + public: + virtual ~widget(); + virtual void move(int x, int y); + virtual void resize(int sx, int sy); + int GetX(); + int GetY(); + int GetH(); + int GetW(); + int GetAX(); + int GetAY(); + int GetAX2(); + int GetAY2(); + virtual mogltk::rect GetDrawRect(); + widget * Father(); + widget * Child(); + widget * Next(); + widget * Prev(); + widget * InnerPanel(); + virtual widget * find_widget(int x, int y); + void fulldraw(); + shape * Shaper(); + void mainloop(); + void m_event(int x, int y, event_t event); + bool inside(int x, int y); + bool GetVisible(); + virtual void SetVisible(bool); + bool GetEnabled(); + virtual void SetEnabled(bool); + void MoveOnTop(); + static void check_timed_events(); + protected: + widget(widget * father, int x, int y, int sx, int sy, int type, String name, shape *); + virtual void draw(); + virtual bool process_event(int x, int y, event_t event); + virtual void resize_notify(); + virtual widget * create_panel(); + void set_exclusive(widget *); + void unset_exclusive(widget *); + void add_out_move(); + void remove_out_move(); + void add_out_click(); + void remove_out_click(); + void set_timed_event(Uint32); + void set_absolute_timed_event(Uint32); + String caption; + private: + int x, y, sx, sy, ax, ay, ax2, ay2; + widget * father, * next, * prev, * child, * last, * root, * panel; + static widget * focused; + int type; + String name; + shape * sh; + widget * exclusive; + bool visible; + bool enabled; + class timed_event { + public: + timed_event(widget * _w, Uint32 _t) : w(_w), t(_t) { } + widget * w; + Uint32 t; + }; + static std::vector<timed_event> timed_events; + + void computeabs(); + void idraw(); + bool ievent(int x, int y, event_t event); + void icomputeabs(); + void iresize_notify(); + }; + + namespace widgets { + class drawer : public Base { + public: + virtual void draw(widget *) = 0; + }; + + class action : public Base { + public: + virtual void do_action(widget *) = 0; + }; + + class Root : public widget { + public: + Root(shape *, drawer * = 0); + void setdrawer(drawer *); + protected: + virtual void draw(); + drawer * dr; + }; + + class Panel : public widget { + public: + Panel(shape *, widget * father, int x, int y, int w, int h); + }; + + class Button : public widget { + public: + Button(action *, shape *, widget * father, int x, int y, int w, int h, const String & caption); + protected: + virtual void draw(); + virtual bool process_event(int, int, event_t); + bool bevel; + private: + bool dragging; + action * a; + }; + + class Label : public widget { + public: + Label(shape *, widget * father, int x, int y, int w, int h, const String & caption, const ColorP & color = BLACK); + protected: + virtual void draw(); + private: + ColorP color; + }; + + class SmartBox : public widget { + public: + SmartBox(shape *, widget * father, int x, int y, int w, int h, const String & caption); + virtual rect GetDrawRect(); + protected: + virtual void draw(); + virtual bool process_event(int, int, event_t); + private: + int ox, oy, oax, oay; + }; + + class MsgBox : public SmartBox { + public: + MsgBox(shape *, widget * father, const String & caption, const String & text); + virtual ~MsgBox(); + }; + + class Frame : public widget { + public: + Frame(shape *, widget * father, int x, int y, int w, int h); + virtual rect GetDrawRect(); + protected: + virtual void draw(); + }; + + class ContextMenu : public widget { + public: + ContextMenu(shape *, widget * father, int x, int y); + virtual ~ContextMenu(); + void addnode(const String &, action *); + void addline(); + void addsub(const String &, ContextMenu *); + ContextMenu * createsub(const String &); + virtual void move(int x, int y); + virtual void resize(int w, int h); + virtual void SetVisible(bool); + virtual void SetEnabled(int i, bool); + void StickyDisplay(); + protected: + virtual void draw(); + virtual bool process_event(int x, int y, event_t event); + class node : public Base { + public: + node(const String &, int x, int y, int w, int h, action * = 0, ContextMenu * sub = 0, ContextMenu * father = 0, bool = false); + String GetCaption(); + action * GetAction(); + ContextMenu * GetSub(); + ContextMenu * GetFather(); + bool GetLine(); + bool GetEnabled(); + void SetEnabled(bool); + int GetX(); + int GetY(); + int GetW(); + int GetH(); + private: + String caption; + int x, y, w, h; + action * a; + ContextMenu * sub, * father; + bool line, enabled; + }; + + std::vector<node> nodes; + int selected; + ContextMenu * subselected; + private: + bool in_click, sticky; + + bool iin_click(); + }; + + class Menu : public widget { + public: + Menu(shape *, widget * father); + void addnode(const String &, action *); + void addsub(const String &, ContextMenu * sub); + ContextMenu * createsub(const String &); + virtual void SetEnabled(int i, bool); + void SetCaption(int i, const String &); + protected: + virtual void resize_notify(); + virtual void draw(); + virtual bool process_event(int, int, event_t); + class node : public Base { + public: + node(const String & caption, ContextMenu * sub, action * a, int x); + String GetCaption(); + void SetCaption(const String &); + ContextMenu * GetSub(); + action * GetAction(); + int GetX(); + bool GetEnabled(); + void SetEnabled(bool); + private: + String caption; + ContextMenu * sub; + action * a; + int x; + bool enabled; + }; + private: + std::vector<node> nodes; + int cur_x; + int selected; + }; + }; +}; + +#endif diff --git a/lib/base.cc b/lib/base.cc index 61426eb..13d6052 100644 --- a/lib/base.cc +++ b/lib/base.cc @@ -1,178 +1,178 @@ -/*
- * mogltk
- * Copyright (C) 1999-2004 Nicolas "Pixel" Noble
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- */
-
-/* $Id: base.cc,v 1.13 2004-11-27 21:44:52 pixel Exp $ */
-
-#include <stdio.h>
-#include <SDL.h>
-#include "base.h"
-#include "engine.h"
-#include "generic.h"
-#ifdef HAVE_CONFIG_H
-#include "config.h"
-#endif
-#include "gettext.h"
-
-mogltk::base::base(int w, int h, int flags) throw(GeneralException) : surface(0) {
- 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_HWSURFACE))) {
- throw GeneralException(String("Couldn't set SDL 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);
-
- SDL_ShowCursor(SDL_DISABLE);
- SDL_FillRect(surface, NULL, 0);
- SDL_Flip(surface);
- SDL_FillRect(surface, NULL, 0);
- mogltk::engine::postsetup();
-}
-
-mogltk::base::~base() {
-}
-
-int mogltk::base::GetWidth(void) {
- return width;
-}
-
-int mogltk::base::GetHeight(void) {
- return height;
-}
-
-void mogltk::base::Flip(bool clear) {
- mogltk::engine::pollevents();
- SDL_Flip(surface);
- if (clear)
- SDL_FillRect(surface, NULL, 0);
-}
-
-mogltk::base::base(int w, int h, int flags, int) : surface(0) {
- width = w;
- height = h;
-
- SDL_ShowCursor(SDL_DISABLE);
-}
-
-void mogltk::base::setsurface(SDL_Surface * _surface) throw (GeneralException) {
- if (surface)
- throw GeneralException("Can't set video surface twice");
- surface = _surface;
-}
-
-SDL_Surface * mogltk::base::getsurface() {
- return surface;
-}
-
-void mogltk::base::Enter2DMode() {
-}
-
-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);
-}
-
-void mogltk::base::ToggleFullscreen() {
- int newflags = surface->flags;
-
- if (surface->flags & SDL_FULLSCREEN)
- newflags &= ~SDL_FULLSCREEN;
- else
- newflags |= SDL_FULLSCREEN;
-
- texture::Taintall();
-
- surface = SDL_SetVideoMode(surface->w, surface->h, surface->format->BitsPerPixel,
- SDL_HWSURFACE | newflags);
-}
-
-inline static unsigned int nextpower(unsigned int n) {
- unsigned int i;
-
- if (!n)
- return n;
-
- if (ISPOT(n))
- return n;
-
- for (i = 31; i >= 0; i--) {
- if ((n >> i) & 1) {
- return 1 << (i + 1);
- }
- }
-}
-
-mogltk::texture * mogltk::base::GrabTexture() {
- int w = nextpower(GetWidth());
- int h = nextpower(GetHeight());
- texture * r = new texture(w, h);
-
- SDL_BlitSurface(getsurface(), NULL, r->GetSurface(), NULL);
-
- return r;
-}
-
-SDL_Surface * mogltk::base::GrabSurface() {
- SDL_Surface * r = SDL_CreateRGBSurface(SDL_SWSURFACE, GetWidth(), GetHeight(), 24,
-#if SDL_BYTEORDER == SDL_BIG_ENDIAN
- 0xff000000,
- 0x00ff0000,
- 0x0000ff00,
- 0x00000000
-#else
- 0x000000ff,
- 0x0000ff00,
- 0x00ff0000,
- 0x00000000
-#endif
- );
-
- SDL_BlitSurface(getsurface(), NULL, r, NULL);
-
- return r;
-}
+/* + * mogltk + * Copyright (C) 1999-2004 Nicolas "Pixel" Noble + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +/* $Id: base.cc,v 1.14 2004-11-27 21:48:03 pixel Exp $ */ + +#include <stdio.h> +#include <SDL.h> +#include "base.h" +#include "engine.h" +#include "generic.h" +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif +#include "gettext.h" + +mogltk::base::base(int w, int h, int flags) throw(GeneralException) : surface(0) { + 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_HWSURFACE))) { + throw GeneralException(String("Couldn't set SDL 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); + + SDL_ShowCursor(SDL_DISABLE); + SDL_FillRect(surface, NULL, 0); + SDL_Flip(surface); + SDL_FillRect(surface, NULL, 0); + mogltk::engine::postsetup(); +} + +mogltk::base::~base() { +} + +int mogltk::base::GetWidth(void) { + return width; +} + +int mogltk::base::GetHeight(void) { + return height; +} + +void mogltk::base::Flip(bool clear) { + mogltk::engine::pollevents(); + SDL_Flip(surface); + if (clear) + SDL_FillRect(surface, NULL, 0); +} + +mogltk::base::base(int w, int h, int flags, int) : surface(0) { + width = w; + height = h; + + SDL_ShowCursor(SDL_DISABLE); +} + +void mogltk::base::setsurface(SDL_Surface * _surface) throw (GeneralException) { + if (surface) + throw GeneralException("Can't set video surface twice"); + surface = _surface; +} + +SDL_Surface * mogltk::base::getsurface() { + return surface; +} + +void mogltk::base::Enter2DMode() { +} + +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); +} + +void mogltk::base::ToggleFullscreen() { + int newflags = surface->flags; + + if (surface->flags & SDL_FULLSCREEN) + newflags &= ~SDL_FULLSCREEN; + else + newflags |= SDL_FULLSCREEN; + + texture::Taintall(); + + surface = SDL_SetVideoMode(surface->w, surface->h, surface->format->BitsPerPixel, + SDL_HWSURFACE | newflags); +} + +inline static unsigned int nextpower(unsigned int n) { + unsigned int i; + + if (!n) + return n; + + if (ISPOT(n)) + return n; + + for (i = 31; i >= 0; i--) { + if ((n >> i) & 1) { + return 1 << (i + 1); + } + } +} + +mogltk::texture * mogltk::base::GrabTexture() { + int w = nextpower(GetWidth()); + int h = nextpower(GetHeight()); + texture * r = new texture(w, h); + + SDL_BlitSurface(getsurface(), NULL, r->GetSurface(), NULL); + + return r; +} + +SDL_Surface * mogltk::base::GrabSurface() { + SDL_Surface * r = SDL_CreateRGBSurface(SDL_SWSURFACE, GetWidth(), GetHeight(), 24, +#if SDL_BYTEORDER == SDL_BIG_ENDIAN + 0xff000000, + 0x00ff0000, + 0x0000ff00, + 0x00000000 +#else + 0x000000ff, + 0x0000ff00, + 0x00ff0000, + 0x00000000 +#endif + ); + + SDL_BlitSurface(getsurface(), NULL, r, NULL); + + return r; +} diff --git a/lib/engine.cc b/lib/engine.cc index e0e1efd..d9e479f 100644 --- a/lib/engine.cc +++ b/lib/engine.cc @@ -1,409 +1,409 @@ -/*
- * mogltk
- * Copyright (C) 1999-2004 Nicolas "Pixel" Noble
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- */
-
-/* $Id: engine.cc,v 1.27 2004-11-27 21:44:52 pixel Exp $ */
-
-#include <SDL.h>
-#include <Input.h>
-#include "engine.h"
-#include "glfont.h"
-#include "glsprite.h"
-
-#ifdef HAVE_CONFIG_H
-#include "config.h"
-#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;
-int mogltk::engine::mx, mogltk::engine::my, mogltk::engine::mz = 0, mogltk::engine::mbuttons;
-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;
-
-mogltk::engine::keyevent * mogltk::engine::keyevent_h = 0;
-mogltk::engine::mouseevent * mogltk::engine::mouseevent_h = 0;
-
-#define UPDATERATE 1000
-
-mogltk::engine::keyevent::keyevent() {
- new_handler = 0;
- if ((old_handler = getkeyevent()))
- old_handler->new_handler = this;
- setkeyevent(this);
-}
-
-mogltk::engine::keyevent::~keyevent() {
- if (new_handler)
- new_handler->old_handler = old_handler;
- if (old_handler)
- old_handler->new_handler = new_handler;
-
- if (getkeyevent() == this)
- setkeyevent(old_handler);
-}
-
-void mogltk::engine::keyevent::up(SDL_keysym) {
- printm(M_INFO, "Generic keyevent::up called\n");
-}
-
-void mogltk::engine::keyevent::down(SDL_keysym) {
- printm(M_INFO, "Generic keyevent::down called\n");
-}
-
-class keyhandler_t : public mogltk::engine::keyevent {
- virtual void down(SDL_keysym keysym) {
- if (keysym.sym == SDLK_ESCAPE)
- mogltk::engine::quit();
- else if ((keysym.sym == SDLK_RETURN) && (keysym.mod & KMOD_ALT))
- mogltk::engine::base_o->ToggleFullscreen();
- else if (old_handler)
- old_handler->down(keysym);
- }
- virtual void up(SDL_keysym keysym) {
- if (old_handler)
- old_handler->up(keysym);
- }
-} basic_keyhandler;
-
-mogltk::engine::mouseevent::mouseevent() {
- new_handler = 0;
- if ((old_handler = getmouseevent()))
- old_handler->new_handler = this;
- setmouseevent(this);
-}
-
-mogltk::engine::mouseevent::~mouseevent() {
- if (new_handler)
- new_handler->old_handler = old_handler;
- if (old_handler)
- old_handler->new_handler = new_handler;
-
- if (getmouseevent() == this)
- setmouseevent(old_handler);
-}
-
-void mogltk::engine::mouseevent::move(SDL_MouseMotionEvent) {
- printm(M_INFO, "Generic mouseevent::move called\n");
-}
-
-void mogltk::engine::mouseevent::action(SDL_MouseButtonEvent) {
- printm(M_INFO, "Generic mouseevent::action called\n");
-}
-
-int mogltk::engine::setup() throw(GeneralException) {
- if (inited) {
- printm(M_WARNING, FUNCNAME + _(" called twice, ignoring second call.\n"));
- return -1;
- }
- if (SDL_Init(0) < 0) {
- throw GeneralException(FUNCNAME + _(": Unable to start SDL base system"));
- }
- atexit(SDL_Quit);
-
- inited = true;
-
- return 0;
-}
-
-int mogltk::engine::postsetup() throw(GeneralException) {
- if (postsetuped) {
- printm(M_WARNING, FUNCNAME + _(" called twice, ignoring second call.\n"));
- return -1;
- }
-
- SDL_EnableUNICODE(1);
- curticks = SDL_GetTicks();
-
- postsetuped = true;
-
- if (glbase_o) {
- mogltk::SystemFont = new mogltk::glfont(&Input("font.bin"));
- mogltk::FixedFont = new mogltk::glfont(&Input("fixed-font.bin"));
- Sprite::Cursor = new mogltk::glSprite(&Input("cursor.rgba"), 25, 25);
- } else {
- mogltk::SystemFont = new mogltk::font(&Input("font.bin"));
- mogltk::FixedFont = new mogltk::font(&Input("fixed-font.bin"));
- Sprite::Cursor = new mogltk::Sprite(&Input("cursor.rgba"), 25, 25);
- }
-
- return 0;
-}
-
-int mogltk::engine::GetInited() {
- return inited;
-}
-
-class embedRWops : public Base {
- public:
- embedRWops(Handle *);
- int seek(int, int);
- int read(void *, int, int);
- int write(const void *, int, int);
- private:
- Handle * h;
-};
-
-embedRWops::embedRWops(Handle * ah) : h(ah) {}
-
-int embedRWops::seek(int offset, int whence) {
- return h->seek(offset, whence);
-}
-
-int embedRWops::read(void * ptr, int size, int num) {
- return h->read(ptr, size * num);
-}
-
-int embedRWops::write(const void * ptr, int size, int num) {
- return h->write(ptr, size * num);
-}
-
-static int embedRWseek(SDL_RWops * context, int offset, int whence) {
- if (context->hidden.unknown.data1)
- return ((embedRWops *)(context->hidden.unknown.data1))->seek(offset, whence);
- return -1;
-}
-
-static int embedRWread(SDL_RWops * context, void * ptr, int size, int num) {
- if (context->hidden.unknown.data1)
- return ((embedRWops *)(context->hidden.unknown.data1))->read(ptr, size, num);
- return -1;
-}
-
-static int embedRWwrite(SDL_RWops * context, const void * ptr, int size, int num) {
- if (context->hidden.unknown.data1)
- return ((embedRWops *)(context->hidden.unknown.data1))->write(ptr, size, num);
- return -1;
-}
-
-static int embedRWclose(SDL_RWops * context) {
- if (context->hidden.unknown.data1) {
- delete ((embedRWops *)(context->hidden.unknown.data1));
- context->hidden.unknown.data1 = 0;
- return 0;
- }
- return -1;
-}
-
-SDL_RWops * mogltk::engine::RWFromHandle(Handle * h) throw (GeneralException) {
- SDL_RWops * r = 0;
- if (h) {
- if (!(r = SDL_AllocRW()))
- throw GeneralException(_("Couldn't allocate memory for SDL_RWops"));
- r->hidden.unknown.data1 = (void *) new embedRWops(h);
- r->seek = embedRWseek;
- r->read = embedRWread;
- r->write = embedRWwrite;
- r->close = embedRWclose;
- }
- return r;
-}
-
-void mogltk::engine::pollevents() throw (GeneralException) {
- SDL_Event event;
- bool hastoreturn = appactive;
-
- if (!postsetuped)
- postsetup();
-
- if (appactive) {
- Uint32 ticks = SDL_GetTicks();
- frames++;
- if (ticks - curticks > UPDATERATE) {
- curfps = (double) frames * 1000 / (ticks - curticks);
- frames = 0;
- curticks = ticks;
- }
- } else {
- curfps = -1;
- }
-
- while(true) {
- if (hastoreturn || quitrequest)
- if (!SDL_PollEvent(NULL)) {
- updatemouse();
- return;
- }
- if (!SDL_WaitEvent(&event)) {
- throw GeneralException("Error polling for SDL Event");
- }
- switch(event.type) {
- case SDL_ACTIVEEVENT:
- switch (event.active.state) {
- case SDL_APPMOUSEFOCUS:
- printm(M_INFO, String("Application ") + (event.active.gain ? "gained" : "loosed") + " mouse focus\n");
- if (cursorvisible)
- hastoreturn = true;
- break;
- case SDL_APPINPUTFOCUS:
- printm(M_INFO, String("Application ") + (event.active.gain ? "gained" : "loosed") + " input focus\n");
- if (cursorvisible)
- hastoreturn = true;
- break;
- case SDL_APPACTIVE:
- printm(M_INFO, String("Application was ") + (event.active.gain ? "restored" : "iconified"));
- if (cursorvisible)
- hastoreturn = true;
- break;
- }
- break;
- case SDL_KEYDOWN:
- case SDL_KEYUP:
- printm(M_INFO, String("Key ") + event.key.keysym.scancode + " on keyboard " + event.key.which + (event.key.state == SDL_PRESSED ? " pressed" : " released") + "\n");
- printm(M_INFO, "SDL keysym: %i - Unicode: %04x = `%c`- modifiers: %04x\n", event.key.keysym.sym, event.key.keysym.unicode, event.key.keysym.unicode, event.key.keysym.mod);
- if (keyevent_h) {
- if (event.key.state == SDL_PRESSED) {
- keyevent_h->down(event.key.keysym);
- } else {
- keyevent_h->up(event.key.keysym);
- }
- }
- break;
- case SDL_MOUSEMOTION:
- printm(M_INFO, "Mouse slept over the screen - (%i, %i)\n", event.motion.x, event.motion.y);
- if (locked) {
- if (!((event.motion.x == mx) && (event.motion.y == my))) {
- SDL_WarpMouse(mx, my);
- }
- }
- if (cursorvisible)
- hastoreturn = true;
- if (mouseevent_h) {
- mouseevent_h->move(event.motion);
- }
- break;
- case SDL_MOUSEBUTTONDOWN:
- case SDL_MOUSEBUTTONUP:
- printm(M_INFO, String().set("Mouse button %02x ", event.button.button) + String((event.button.state == SDL_PRESSED ? "pressed" : "released")) + " at (" + event.button.x + ", " + event.button.y + ")\n");
- if (event.button.state == SDL_PRESSED) {
- if (event.button.button == SDL_BUTTON_WHEELUP) {
- mz--;
- }
- if (event.button.button == SDL_BUTTON_WHEELDOWN) {
- mz++;
- }
- }
- if (mouseevent_h) {
- mouseevent_h->action(event.button);
- }
- break;
- case SDL_QUIT:
- printm(M_INFO, "Quit requested\n");
- hastoreturn = quitrequest = true;
- break;
- case SDL_VIDEOEXPOSE:
- printm(M_INFO, "Needs to redraw\n");
- hastoreturn = true;
- break;
- default:
- printm(M_INFO, "Unknow event: %i\n", event.type);
- break;
- }
- }
-}
-
-void mogltk::engine::setappactive(bool p) {
- appactive = p;
-}
-
-bool mogltk::engine::getappactive() {
- return appactive;
-}
-
-void mogltk::engine::setcursorvisible(bool p) {
- cursorvisible = p;
-}
-
-bool mogltk::engine::getcursorvisible() {
- return cursorvisible;
-}
-
-void mogltk::engine::quit() {
- quitrequest = true;
-}
-
-bool mogltk::engine::quitrequested() {
- return quitrequest;
-}
-
-void mogltk::engine::updatemouse() {
- mbuttons = SDL_GetMouseState(&mx, &my);
-}
-
-int mogltk::engine::mouseX() {
- return mx;
-}
-
-int mogltk::engine::mouseY() {
- return my;
-}
-
-int mogltk::engine::mouseZ() {
- return mz;
-}
-
-void mogltk::engine::setmouseX(int _mx) {
- mx = _mx;
- SDL_WarpMouse(mx, my);
-}
-
-void mogltk::engine::setmouseY(int _my) {
- my = _my;
- SDL_WarpMouse(mx, my);
-}
-
-void mogltk::engine::setmouseZ(int _mz) {
- mz = _mz;
-}
-
-int mogltk::engine::mousebuttons() {
- return mbuttons;
-}
-
-double mogltk::engine::FPS() {
- return curfps;
-}
-
-void mogltk::engine::lockmouse() {
- locked = 1;
-}
-
-void mogltk::engine::unlockmouse() {
- locked = 0;
-}
-
-void mogltk::engine::setkeyevent(keyevent * k) {
- keyevent_h = k;
-}
-
-void mogltk::engine::setmouseevent(mouseevent * m) {
- mouseevent_h = m;
-}
-
-mogltk::engine::keyevent * mogltk::engine::getkeyevent() {
- return keyevent_h;
-}
-
-mogltk::engine::mouseevent * mogltk::engine::getmouseevent() {
- return mouseevent_h;
-}
+/* + * mogltk + * Copyright (C) 1999-2004 Nicolas "Pixel" Noble + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +/* $Id: engine.cc,v 1.28 2004-11-27 21:48:03 pixel Exp $ */ + +#include <SDL.h> +#include <Input.h> +#include "engine.h" +#include "glfont.h" +#include "glsprite.h" + +#ifdef HAVE_CONFIG_H +#include "config.h" +#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; +int mogltk::engine::mx, mogltk::engine::my, mogltk::engine::mz = 0, mogltk::engine::mbuttons; +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; + +mogltk::engine::keyevent * mogltk::engine::keyevent_h = 0; +mogltk::engine::mouseevent * mogltk::engine::mouseevent_h = 0; + +#define UPDATERATE 1000 + +mogltk::engine::keyevent::keyevent() { + new_handler = 0; + if ((old_handler = getkeyevent())) + old_handler->new_handler = this; + setkeyevent(this); +} + +mogltk::engine::keyevent::~keyevent() { + if (new_handler) + new_handler->old_handler = old_handler; + if (old_handler) + old_handler->new_handler = new_handler; + + if (getkeyevent() == this) + setkeyevent(old_handler); +} + +void mogltk::engine::keyevent::up(SDL_keysym) { + printm(M_INFO, "Generic keyevent::up called\n"); +} + +void mogltk::engine::keyevent::down(SDL_keysym) { + printm(M_INFO, "Generic keyevent::down called\n"); +} + +class keyhandler_t : public mogltk::engine::keyevent { + virtual void down(SDL_keysym keysym) { + if (keysym.sym == SDLK_ESCAPE) + mogltk::engine::quit(); + else if ((keysym.sym == SDLK_RETURN) && (keysym.mod & KMOD_ALT)) + mogltk::engine::base_o->ToggleFullscreen(); + else if (old_handler) + old_handler->down(keysym); + } + virtual void up(SDL_keysym keysym) { + if (old_handler) + old_handler->up(keysym); + } +} basic_keyhandler; + +mogltk::engine::mouseevent::mouseevent() { + new_handler = 0; + if ((old_handler = getmouseevent())) + old_handler->new_handler = this; + setmouseevent(this); +} + +mogltk::engine::mouseevent::~mouseevent() { + if (new_handler) + new_handler->old_handler = old_handler; + if (old_handler) + old_handler->new_handler = new_handler; + + if (getmouseevent() == this) + setmouseevent(old_handler); +} + +void mogltk::engine::mouseevent::move(SDL_MouseMotionEvent) { + printm(M_INFO, "Generic mouseevent::move called\n"); +} + +void mogltk::engine::mouseevent::action(SDL_MouseButtonEvent) { + printm(M_INFO, "Generic mouseevent::action called\n"); +} + +int mogltk::engine::setup() throw(GeneralException) { + if (inited) { + printm(M_WARNING, FUNCNAME + _(" called twice, ignoring second call.\n")); + return -1; + } + if (SDL_Init(0) < 0) { + throw GeneralException(FUNCNAME + _(": Unable to start SDL base system")); + } + atexit(SDL_Quit); + + inited = true; + + return 0; +} + +int mogltk::engine::postsetup() throw(GeneralException) { + if (postsetuped) { + printm(M_WARNING, FUNCNAME + _(" called twice, ignoring second call.\n")); + return -1; + } + + SDL_EnableUNICODE(1); + curticks = SDL_GetTicks(); + + postsetuped = true; + + if (glbase_o) { + mogltk::SystemFont = new mogltk::glfont(&Input("font.bin")); + mogltk::FixedFont = new mogltk::glfont(&Input("fixed-font.bin")); + Sprite::Cursor = new mogltk::glSprite(&Input("cursor.rgba"), 25, 25); + } else { + mogltk::SystemFont = new mogltk::font(&Input("font.bin")); + mogltk::FixedFont = new mogltk::font(&Input("fixed-font.bin")); + Sprite::Cursor = new mogltk::Sprite(&Input("cursor.rgba"), 25, 25); + } + + return 0; +} + +int mogltk::engine::GetInited() { + return inited; +} + +class embedRWops : public Base { + public: + embedRWops(Handle *); + int seek(int, int); + int read(void *, int, int); + int write(const void *, int, int); + private: + Handle * h; +}; + +embedRWops::embedRWops(Handle * ah) : h(ah) {} + +int embedRWops::seek(int offset, int whence) { + return h->seek(offset, whence); +} + +int embedRWops::read(void * ptr, int size, int num) { + return h->read(ptr, size * num); +} + +int embedRWops::write(const void * ptr, int size, int num) { + return h->write(ptr, size * num); +} + +static int embedRWseek(SDL_RWops * context, int offset, int whence) { + if (context->hidden.unknown.data1) + return ((embedRWops *)(context->hidden.unknown.data1))->seek(offset, whence); + return -1; +} + +static int embedRWread(SDL_RWops * context, void * ptr, int size, int num) { + if (context->hidden.unknown.data1) + return ((embedRWops *)(context->hidden.unknown.data1))->read(ptr, size, num); + return -1; +} + +static int embedRWwrite(SDL_RWops * context, const void * ptr, int size, int num) { + if (context->hidden.unknown.data1) + return ((embedRWops *)(context->hidden.unknown.data1))->write(ptr, size, num); + return -1; +} + +static int embedRWclose(SDL_RWops * context) { + if (context->hidden.unknown.data1) { + delete ((embedRWops *)(context->hidden.unknown.data1)); + context->hidden.unknown.data1 = 0; + return 0; + } + return -1; +} + +SDL_RWops * mogltk::engine::RWFromHandle(Handle * h) throw (GeneralException) { + SDL_RWops * r = 0; + if (h) { + if (!(r = SDL_AllocRW())) + throw GeneralException(_("Couldn't allocate memory for SDL_RWops")); + r->hidden.unknown.data1 = (void *) new embedRWops(h); + r->seek = embedRWseek; + r->read = embedRWread; + r->write = embedRWwrite; + r->close = embedRWclose; + } + return r; +} + +void mogltk::engine::pollevents() throw (GeneralException) { + SDL_Event event; + bool hastoreturn = appactive; + + if (!postsetuped) + postsetup(); + + if (appactive) { + Uint32 ticks = SDL_GetTicks(); + frames++; + if (ticks - curticks > UPDATERATE) { + curfps = (double) frames * 1000 / (ticks - curticks); + frames = 0; + curticks = ticks; + } + } else { + curfps = -1; + } + + while(true) { + if (hastoreturn || quitrequest) + if (!SDL_PollEvent(NULL)) { + updatemouse(); + return; + } + if (!SDL_WaitEvent(&event)) { + throw GeneralException("Error polling for SDL Event"); + } + switch(event.type) { + case SDL_ACTIVEEVENT: + switch (event.active.state) { + case SDL_APPMOUSEFOCUS: + printm(M_INFO, String("Application ") + (event.active.gain ? "gained" : "loosed") + " mouse focus\n"); + if (cursorvisible) + hastoreturn = true; + break; + case SDL_APPINPUTFOCUS: + printm(M_INFO, String("Application ") + (event.active.gain ? "gained" : "loosed") + " input focus\n"); + if (cursorvisible) + hastoreturn = true; + break; + case SDL_APPACTIVE: + printm(M_INFO, String("Application was ") + (event.active.gain ? "restored" : "iconified")); + if (cursorvisible) + hastoreturn = true; + break; + } + break; + case SDL_KEYDOWN: + case SDL_KEYUP: + printm(M_INFO, String("Key ") + event.key.keysym.scancode + " on keyboard " + event.key.which + (event.key.state == SDL_PRESSED ? " pressed" : " released") + "\n"); + printm(M_INFO, "SDL keysym: %i - Unicode: %04x = `%c`- modifiers: %04x\n", event.key.keysym.sym, event.key.keysym.unicode, event.key.keysym.unicode, event.key.keysym.mod); + if (keyevent_h) { + if (event.key.state == SDL_PRESSED) { + keyevent_h->down(event.key.keysym); + } else { + keyevent_h->up(event.key.keysym); + } + } + break; + case SDL_MOUSEMOTION: + printm(M_INFO, "Mouse slept over the screen - (%i, %i)\n", event.motion.x, event.motion.y); + if (locked) { + if (!((event.motion.x == mx) && (event.motion.y == my))) { + SDL_WarpMouse(mx, my); + } + } + if (cursorvisible) + hastoreturn = true; + if (mouseevent_h) { + mouseevent_h->move(event.motion); + } + break; + case SDL_MOUSEBUTTONDOWN: + case SDL_MOUSEBUTTONUP: + printm(M_INFO, String().set("Mouse button %02x ", event.button.button) + String((event.button.state == SDL_PRESSED ? "pressed" : "released")) + " at (" + event.button.x + ", " + event.button.y + ")\n"); + if (event.button.state == SDL_PRESSED) { + if (event.button.button == SDL_BUTTON_WHEELUP) { + mz--; + } + if (event.button.button == SDL_BUTTON_WHEELDOWN) { + mz++; + } + } + if (mouseevent_h) { + mouseevent_h->action(event.button); + } + break; + case SDL_QUIT: + printm(M_INFO, "Quit requested\n"); + hastoreturn = quitrequest = true; + break; + case SDL_VIDEOEXPOSE: + printm(M_INFO, "Needs to redraw\n"); + hastoreturn = true; + break; + default: + printm(M_INFO, "Unknow event: %i\n", event.type); + break; + } + } +} + +void mogltk::engine::setappactive(bool p) { + appactive = p; +} + +bool mogltk::engine::getappactive() { + return appactive; +} + +void mogltk::engine::setcursorvisible(bool p) { + cursorvisible = p; +} + +bool mogltk::engine::getcursorvisible() { + return cursorvisible; +} + +void mogltk::engine::quit() { + quitrequest = true; +} + +bool mogltk::engine::quitrequested() { + return quitrequest; +} + +void mogltk::engine::updatemouse() { + mbuttons = SDL_GetMouseState(&mx, &my); +} + +int mogltk::engine::mouseX() { + return mx; +} + +int mogltk::engine::mouseY() { + return my; +} + +int mogltk::engine::mouseZ() { + return mz; +} + +void mogltk::engine::setmouseX(int _mx) { + mx = _mx; + SDL_WarpMouse(mx, my); +} + +void mogltk::engine::setmouseY(int _my) { + my = _my; + SDL_WarpMouse(mx, my); +} + +void mogltk::engine::setmouseZ(int _mz) { + mz = _mz; +} + +int mogltk::engine::mousebuttons() { + return mbuttons; +} + +double mogltk::engine::FPS() { + return curfps; +} + +void mogltk::engine::lockmouse() { + locked = 1; +} + +void mogltk::engine::unlockmouse() { + locked = 0; +} + +void mogltk::engine::setkeyevent(keyevent * k) { + keyevent_h = k; +} + +void mogltk::engine::setmouseevent(mouseevent * m) { + mouseevent_h = m; +} + +mogltk::engine::keyevent * mogltk::engine::getkeyevent() { + return keyevent_h; +} + +mogltk::engine::mouseevent * mogltk::engine::getmouseevent() { + return mouseevent_h; +} diff --git a/lib/font.cc b/lib/font.cc index 2a17d20..07ade78 100644 --- a/lib/font.cc +++ b/lib/font.cc @@ -1,705 +1,705 @@ -/*
- * mogltk
- * Copyright (C) 1999-2004 Nicolas "Pixel" Noble
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- */
-
-/* $Id: font.cc,v 1.15 2004-11-27 21:44:53 pixel Exp $ */
-
-#include <stdarg.h>
-#include <SDL.h>
-#include <Input.h>
-#include "base.h"
-#include "font.h"
-#ifdef HAVE_CONFIG_H
-#include "config.h"
-#endif
-#include "engine.h"
-
-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::ColorP colorcached[16] = {
- DOS_BLACK,
- DOS_BLUE,
- DOS_GREEN,
- DOS_CYAN,
- DOS_RED,
- DOS_MAGENTA,
- DOS_BRAWN,
- DOS_WHITE,
- DOS_GRAY,
- DOS_HIGH_BLUE,
- DOS_HIGH_GREEN,
- DOS_HIGH_CYAN,
- DOS_HIGH_RED,
- DOS_HIGH_MAGENTA,
- DOS_YELLOW,
- DOS_HIGH_WHITE
-};
-
-mogltk::font::font(Handle * ffont) : textcolor(255, 255, 255, 255), shadow(0), wspace(0) {
- int i, j;
-
- 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 < 16; i++) {
- fontcache[i] = (texture **) malloc(nbT * sizeof(texture *));
- }
-
- for (i = 0; i < nbT; i++) {
- fonttex[i] = alloctexture();
- for (j = 0; j < 15; j++) {
- fontcache[j][i] = 0;
- }
- fontcache[15][i] = fonttex[i];
- }
-
- 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, j;
- for (i = 0; i < nbT; i++)
- for (j = 0; j < 16; j++)
- if (fontcache[j][i])
- delete fontcache[j][i];
-
- for (i = 0; i < 16; i++)
- free((void *&) fontcache[i]);
-
- free((void *&) fonttex);
- free(sizes);
-}
-
-void mogltk::font::drawentry(Uint16 entry, int x, int y, ColorP c) {
- bool locked = false;
- int trueentry, cx, cy, px, py;
- SDL_Rect src, dst;
-
- if (SDL_MUSTLOCK(mogltk::engine::base_o->getsurface())) {
- locked = true;
- SDL_LockSurface(mogltk::engine::base_o->getsurface());
- }
-
- if (shadow) {
- int os = shadow;
- shadow = 0;
-
- drawentry(entry, x + os, y + os, BLACK);
-
- shadow = os;
- }
-
- checknbind(entry / nbcT, c);
-
- y -= base;
-
- trueentry = entry % nbcT;
- cx = trueentry % nbcU;
- cy = trueentry / nbcU;
- px = cx * maxX;
- py = cy * maxY;
-
- src.x = px; src.y = py; src.w = maxX; src.h = maxY;
- dst.x = x; dst.y = y; dst.w = maxX; dst.h = maxY;
-
- SDL_BlitSurface(fonttex[entry / nbcT]->GetSurface(), &src, mogltk::engine::base_o->getsurface(), &dst);
-
- if (locked)
- SDL_UnlockSurface(mogltk::engine::base_o->getsurface());
-}
-
-void mogltk::font::drawtotex(texture * t, Uint16 entry, int x, int y, ColorP c) {
- bool locked = false;
- int trueentry, cx, cy, px, py;
- SDL_Rect src, dst;
-
- if (SDL_MUSTLOCK(t->GetSurface())) {
- locked = true;
- SDL_LockSurface(t->GetSurface());
- }
-
- if (shadow) {
- int os = shadow;
- shadow = 0;
-
- drawtotex(t, entry, x + os, y + os, BLACK);
-
- shadow = os;
- }
-
- checknbind(entry / nbcT, c);
-
- y -= base;
-
- trueentry = entry % nbcT;
- cx = trueentry % nbcU;
- cy = trueentry / nbcU;
- px = cx * maxX;
- py = cy * maxY;
-
- src.x = px; src.y = py; src.w = maxX; src.h = maxY;
- dst.x = x; dst.y = y; dst.w = maxX; dst.h = maxY;
-
- SDL_BlitSurface(fonttex[entry / nbcT]->GetSurface(), &src, t->GetSurface(), &dst);
-
- if (locked)
- SDL_UnlockSurface(t->GetSurface());
-}
-
-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::putentryontex(texture * t, Uint16 entry, ColorP c) {
- drawtotex(t, entry, cx, cy, c);
- cx += sizes[entry] + wspace;
-}
-
-void mogltk::font::drawchar(char ch, ColorP c) {
- Uint16 * p;
- int i;
-
- for (i = 0, p = corresp; i < nbentries; i++, p++) {
- if (*(p++) == ch) {
- putentry(*p, c);
- return;
- }
- }
-}
-
-void mogltk::font::drawcharontex(texture * t, char ch, ColorP c) {
- Uint16 * p;
- int i;
-
- for (i = 0, p = corresp; i < nbentries; i++, p++) {
- if (*(p++) == ch) {
- putentryontex(t, *p, c);
- return;
- }
- }
-}
-
-int mogltk::font::findchar(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 {
- drawchar(*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;
-}
-
-mogltk::rect mogltk::font::size(const ugly_string & m, va_list ap) {
- char * p;
- static char buffer[STRBUFSIZ + 1];
- rect r;
- int mw, w;
-
- r.x = cx;
- r.y = cy;
- r.h = inter;
- r.w = 0;
- mw = 0;
- w = 0;
-
-#ifdef HAVE_VSNPRINTF
- vsnprintf(buffer, STRBUFSIZ, m.p, ap);
-#else
- vsprintf(buffer, m.p, ap);
-#endif
-
- for (p = buffer; *p; p++) {
- if (*p == '\n') {
- if (*(p+1)) {
- r.h += inter;
- if (w > mw) {
- mw = w;
- }
- w = 0;
- }
- } else {
- w += sizes[findchar(*p)] + wspace;
- }
- }
-
- if (w > mw) {
- mw = w;
- }
-
- r.w = mw;
-
- return r;
-}
-
-mogltk::rect mogltk::font::size(const ugly_string & m, ...) {
- va_list ap;
- rect r;
-
- va_start(ap, m);
- r = size(m, ap);
- va_end(ap);
-
- return r;
-}
-
-mogltk::rect mogltk::font::size(const char * p, ...) {
- ugly_string m;
- va_list ap;
- rect r;
-
- m.p = p;
-
- va_start(ap, p);
- r = size(m, ap);
- va_end(ap);
-
- return r;
-}
-
-mogltk::rect mogltk::font::printtotex(texture * t, const ugly_string & m, va_list ap) {
- char * p;
- static char buffer[STRBUFSIZ + 1];
- rect r;
- int mw, w;
-
- r.x = cx;
- r.y = cy;
- r.h = inter;
- r.w = 0;
- mw = 0;
- w = 0;
-
-#ifdef HAVE_VSNPRINTF
- vsnprintf(buffer, STRBUFSIZ, m.p, ap);
-#else
- vsprintf(buffer, m.p, ap);
-#endif
-
- for (p = buffer; *p; p++) {
- if (*p == '\n') {
- if (*(p+1)) {
- r.h += inter;
- if (w > mw) {
- mw = w;
- }
- w = 0;
- }
- } else {
- w += sizes[findchar(*p)] + wspace;
- }
- }
-
- if (w > mw) {
- mw = w;
- }
-
- r.w = mw;
-
- for (p = buffer; *p; p++) {
- if (*p == '\n') {
- newline();
- } else {
- drawcharontex(t, *p, textcolor);
- }
- }
-
- return r;
-}
-
-mogltk::rect mogltk::font::printtotex(texture * t, const ugly_string & m, ...) {
- va_list ap;
- rect r;
-
- va_start(ap, m);
- r = printtotex(t, m, ap);
- va_end(ap);
-
- return r;
-}
-
-mogltk::rect mogltk::font::printtotex(texture * t, const char * p, ...) {
- ugly_string m;
- va_list ap;
- rect r;
-
- m.p = p;
-
- va_start(ap, p);
- r = printtotex(t, m, ap);
- va_end(ap);
-
- return r;
-}
-
-inline static unsigned int nextpower(unsigned int n) {
- unsigned int i;
-
- if (!n)
- return n;
-
- if (ISPOT(n))
- return n;
-
- for (i = 31; i >= 0; i--) {
- if ((n >> i) & 1) {
- return 1 << (i + 1);
- }
- }
-}
-
-mogltk::texture * mogltk::font::printtex(rect * _r, const ugly_string & m, va_list ap) {
- rect r;
- char * p;
- static char buffer[STRBUFSIZ + 1];
- int mw, w, pw, ph;
- texture * t;
- int ocx, ocy, oox;
-
- ocx = cx;
- ocy = cy;
- oox = ox;
-
- cx = ox = 0;
- cy = base;
-
- r.x = cx;
- r.y = cy;
- r.h = inter;
- r.w = 0;
- mw = 0;
- w = 0;
-
-#ifdef HAVE_VSNPRINTF
- vsnprintf(buffer, STRBUFSIZ, m.p, ap);
-#else
- vsprintf(buffer, m.p, ap);
-#endif
-
- for (p = buffer; *p; p++) {
- if (*p == '\n') {
- if (*(p+1)) {
- r.h += inter;
- if (w > mw) {
- mw = w;
- }
- w = 0;
- }
- } else {
- w += sizes[findchar(*p)] + wspace;
- }
- }
-
- if (w > mw) {
- mw = w;
- }
-
- r.w = mw;
-
- pw = nextpower(r.w);
- ph = nextpower(r.h);
-
- t = new texture(pw, ph);
-
- for (p = buffer; *p; p++) {
- if (*p == '\n') {
- newline();
- } else {
- drawcharontex(t, *p, textcolor);
- }
- }
-
- if (_r)
- *_r = r;
-
- return t;
-}
-
-mogltk::texture * mogltk::font::printtex(rect * r, const ugly_string & m, ...) {
- va_list ap;
- texture * t;
-
- va_start(ap, m);
- t = printtex(r, m, ap);
- va_end(ap);
-
- return t;
-}
-
-mogltk::texture * mogltk::font::printtex(rect * r, const char * p, ...) {
- ugly_string m;
- va_list ap;
- texture * t;
-
- m.p = p;
-
- va_start(ap, p);
- t = printtex(r, m, ap);
- va_end(ap);
-
- return t;
-}
-
-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[findchar(s[i])] + wspace;
- }
-
- return r;
-}
-
-mogltk::font * mogltk::SystemFont;
-mogltk::font * mogltk::FixedFont;
-
-mogltk::texture * mogltk::font::alloctexture() {
- return new mogltk::texture(256, 256);
-}
-
-void mogltk::font::Bind(int f) {
- fonttex[f]->Bind();
-}
-
-void mogltk::font::checknbind(int index, ColorP c) {
- int i, x, y;
- ColorP oldmax = ColorP::Max, t;
- ColorP::Max = c.c;
- SDL_PixelFormat * f = fonttex[0]->GetSurface()->format;
-
- for (i = 0; i < 15; i++)
- if (c == colorcached[i])
- break;
-
- if (!fontcache[i][index]) {
- fontcache[i][index] = alloctexture();
- for (y = 0; y < 256; y++) {
- for (x = 0; x < 256; x++) {
- t.fromSDL(fontcache[15][index]->GetPixels()[(y << 8) + x], f);
- fontcache[i][index]->GetPixels()[(y << 8) + x] = t.toSDL(f);
- }
- }
- }
-
- fonttex[index] = fontcache[i][index];
-
- ColorP::Max = oldmax.c;
-}
+/* + * mogltk + * Copyright (C) 1999-2004 Nicolas "Pixel" Noble + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +/* $Id: font.cc,v 1.16 2004-11-27 21:48:03 pixel Exp $ */ + +#include <stdarg.h> +#include <SDL.h> +#include <Input.h> +#include "base.h" +#include "font.h" +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif +#include "engine.h" + +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::ColorP colorcached[16] = { + DOS_BLACK, + DOS_BLUE, + DOS_GREEN, + DOS_CYAN, + DOS_RED, + DOS_MAGENTA, + DOS_BRAWN, + DOS_WHITE, + DOS_GRAY, + DOS_HIGH_BLUE, + DOS_HIGH_GREEN, + DOS_HIGH_CYAN, + DOS_HIGH_RED, + DOS_HIGH_MAGENTA, + DOS_YELLOW, + DOS_HIGH_WHITE +}; + +mogltk::font::font(Handle * ffont) : textcolor(255, 255, 255, 255), shadow(0), wspace(0) { + int i, j; + + 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 < 16; i++) { + fontcache[i] = (texture **) malloc(nbT * sizeof(texture *)); + } + + for (i = 0; i < nbT; i++) { + fonttex[i] = alloctexture(); + for (j = 0; j < 15; j++) { + fontcache[j][i] = 0; + } + fontcache[15][i] = fonttex[i]; + } + + 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, j; + for (i = 0; i < nbT; i++) + for (j = 0; j < 16; j++) + if (fontcache[j][i]) + delete fontcache[j][i]; + + for (i = 0; i < 16; i++) + free((void *&) fontcache[i]); + + free((void *&) fonttex); + free(sizes); +} + +void mogltk::font::drawentry(Uint16 entry, int x, int y, ColorP c) { + bool locked = false; + int trueentry, cx, cy, px, py; + SDL_Rect src, dst; + + if (SDL_MUSTLOCK(mogltk::engine::base_o->getsurface())) { + locked = true; + SDL_LockSurface(mogltk::engine::base_o->getsurface()); + } + + if (shadow) { + int os = shadow; + shadow = 0; + + drawentry(entry, x + os, y + os, BLACK); + + shadow = os; + } + + checknbind(entry / nbcT, c); + + y -= base; + + trueentry = entry % nbcT; + cx = trueentry % nbcU; + cy = trueentry / nbcU; + px = cx * maxX; + py = cy * maxY; + + src.x = px; src.y = py; src.w = maxX; src.h = maxY; + dst.x = x; dst.y = y; dst.w = maxX; dst.h = maxY; + + SDL_BlitSurface(fonttex[entry / nbcT]->GetSurface(), &src, mogltk::engine::base_o->getsurface(), &dst); + + if (locked) + SDL_UnlockSurface(mogltk::engine::base_o->getsurface()); +} + +void mogltk::font::drawtotex(texture * t, Uint16 entry, int x, int y, ColorP c) { + bool locked = false; + int trueentry, cx, cy, px, py; + SDL_Rect src, dst; + + if (SDL_MUSTLOCK(t->GetSurface())) { + locked = true; + SDL_LockSurface(t->GetSurface()); + } + + if (shadow) { + int os = shadow; + shadow = 0; + + drawtotex(t, entry, x + os, y + os, BLACK); + + shadow = os; + } + + checknbind(entry / nbcT, c); + + y -= base; + + trueentry = entry % nbcT; + cx = trueentry % nbcU; + cy = trueentry / nbcU; + px = cx * maxX; + py = cy * maxY; + + src.x = px; src.y = py; src.w = maxX; src.h = maxY; + dst.x = x; dst.y = y; dst.w = maxX; dst.h = maxY; + + SDL_BlitSurface(fonttex[entry / nbcT]->GetSurface(), &src, t->GetSurface(), &dst); + + if (locked) + SDL_UnlockSurface(t->GetSurface()); +} + +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::putentryontex(texture * t, Uint16 entry, ColorP c) { + drawtotex(t, entry, cx, cy, c); + cx += sizes[entry] + wspace; +} + +void mogltk::font::drawchar(char ch, ColorP c) { + Uint16 * p; + int i; + + for (i = 0, p = corresp; i < nbentries; i++, p++) { + if (*(p++) == ch) { + putentry(*p, c); + return; + } + } +} + +void mogltk::font::drawcharontex(texture * t, char ch, ColorP c) { + Uint16 * p; + int i; + + for (i = 0, p = corresp; i < nbentries; i++, p++) { + if (*(p++) == ch) { + putentryontex(t, *p, c); + return; + } + } +} + +int mogltk::font::findchar(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 { + drawchar(*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; +} + +mogltk::rect mogltk::font::size(const ugly_string & m, va_list ap) { + char * p; + static char buffer[STRBUFSIZ + 1]; + rect r; + int mw, w; + + r.x = cx; + r.y = cy; + r.h = inter; + r.w = 0; + mw = 0; + w = 0; + +#ifdef HAVE_VSNPRINTF + vsnprintf(buffer, STRBUFSIZ, m.p, ap); +#else + vsprintf(buffer, m.p, ap); +#endif + + for (p = buffer; *p; p++) { + if (*p == '\n') { + if (*(p+1)) { + r.h += inter; + if (w > mw) { + mw = w; + } + w = 0; + } + } else { + w += sizes[findchar(*p)] + wspace; + } + } + + if (w > mw) { + mw = w; + } + + r.w = mw; + + return r; +} + +mogltk::rect mogltk::font::size(const ugly_string & m, ...) { + va_list ap; + rect r; + + va_start(ap, m); + r = size(m, ap); + va_end(ap); + + return r; +} + +mogltk::rect mogltk::font::size(const char * p, ...) { + ugly_string m; + va_list ap; + rect r; + + m.p = p; + + va_start(ap, p); + r = size(m, ap); + va_end(ap); + + return r; +} + +mogltk::rect mogltk::font::printtotex(texture * t, const ugly_string & m, va_list ap) { + char * p; + static char buffer[STRBUFSIZ + 1]; + rect r; + int mw, w; + + r.x = cx; + r.y = cy; + r.h = inter; + r.w = 0; + mw = 0; + w = 0; + +#ifdef HAVE_VSNPRINTF + vsnprintf(buffer, STRBUFSIZ, m.p, ap); +#else + vsprintf(buffer, m.p, ap); +#endif + + for (p = buffer; *p; p++) { + if (*p == '\n') { + if (*(p+1)) { + r.h += inter; + if (w > mw) { + mw = w; + } + w = 0; + } + } else { + w += sizes[findchar(*p)] + wspace; + } + } + + if (w > mw) { + mw = w; + } + + r.w = mw; + + for (p = buffer; *p; p++) { + if (*p == '\n') { + newline(); + } else { + drawcharontex(t, *p, textcolor); + } + } + + return r; +} + +mogltk::rect mogltk::font::printtotex(texture * t, const ugly_string & m, ...) { + va_list ap; + rect r; + + va_start(ap, m); + r = printtotex(t, m, ap); + va_end(ap); + + return r; +} + +mogltk::rect mogltk::font::printtotex(texture * t, const char * p, ...) { + ugly_string m; + va_list ap; + rect r; + + m.p = p; + + va_start(ap, p); + r = printtotex(t, m, ap); + va_end(ap); + + return r; +} + +inline static unsigned int nextpower(unsigned int n) { + unsigned int i; + + if (!n) + return n; + + if (ISPOT(n)) + return n; + + for (i = 31; i >= 0; i--) { + if ((n >> i) & 1) { + return 1 << (i + 1); + } + } +} + +mogltk::texture * mogltk::font::printtex(rect * _r, const ugly_string & m, va_list ap) { + rect r; + char * p; + static char buffer[STRBUFSIZ + 1]; + int mw, w, pw, ph; + texture * t; + int ocx, ocy, oox; + + ocx = cx; + ocy = cy; + oox = ox; + + cx = ox = 0; + cy = base; + + r.x = cx; + r.y = cy; + r.h = inter; + r.w = 0; + mw = 0; + w = 0; + +#ifdef HAVE_VSNPRINTF + vsnprintf(buffer, STRBUFSIZ, m.p, ap); +#else + vsprintf(buffer, m.p, ap); +#endif + + for (p = buffer; *p; p++) { + if (*p == '\n') { + if (*(p+1)) { + r.h += inter; + if (w > mw) { + mw = w; + } + w = 0; + } + } else { + w += sizes[findchar(*p)] + wspace; + } + } + + if (w > mw) { + mw = w; + } + + r.w = mw; + + pw = nextpower(r.w); + ph = nextpower(r.h); + + t = new texture(pw, ph); + + for (p = buffer; *p; p++) { + if (*p == '\n') { + newline(); + } else { + drawcharontex(t, *p, textcolor); + } + } + + if (_r) + *_r = r; + + return t; +} + +mogltk::texture * mogltk::font::printtex(rect * r, const ugly_string & m, ...) { + va_list ap; + texture * t; + + va_start(ap, m); + t = printtex(r, m, ap); + va_end(ap); + + return t; +} + +mogltk::texture * mogltk::font::printtex(rect * r, const char * p, ...) { + ugly_string m; + va_list ap; + texture * t; + + m.p = p; + + va_start(ap, p); + t = printtex(r, m, ap); + va_end(ap); + + return t; +} + +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[findchar(s[i])] + wspace; + } + + return r; +} + +mogltk::font * mogltk::SystemFont; +mogltk::font * mogltk::FixedFont; + +mogltk::texture * mogltk::font::alloctexture() { + return new mogltk::texture(256, 256); +} + +void mogltk::font::Bind(int f) { + fonttex[f]->Bind(); +} + +void mogltk::font::checknbind(int index, ColorP c) { + int i, x, y; + ColorP oldmax = ColorP::Max, t; + ColorP::Max = c.c; + SDL_PixelFormat * f = fonttex[0]->GetSurface()->format; + + for (i = 0; i < 15; i++) + if (c == colorcached[i]) + break; + + if (!fontcache[i][index]) { + fontcache[i][index] = alloctexture(); + for (y = 0; y < 256; y++) { + for (x = 0; x < 256; x++) { + t.fromSDL(fontcache[15][index]->GetPixels()[(y << 8) + x], f); + fontcache[i][index]->GetPixels()[(y << 8) + x] = t.toSDL(f); + } + } + } + + fonttex[index] = fontcache[i][index]; + + ColorP::Max = oldmax.c; +} diff --git a/lib/glbase.cc b/lib/glbase.cc index abd3f1e..2edeec5 100644 --- a/lib/glbase.cc +++ b/lib/glbase.cc @@ -1,267 +1,267 @@ -/*
- * mogltk
- * Copyright (C) 1999-2004 Nicolas "Pixel" Noble
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- */
-
-/* $Id: glbase.cc,v 1.21 2004-10-19 01:27:30 pixel Exp $ */
-
-#include <stdio.h>
-#include <SDL.h>
-#include <SDL_opengl.h>
-#include <Output.h>
-#include <Handle.h>
-#include "glbase.h"
-#include "engine.h"
-#include "generic.h"
-#ifdef HAVE_CONFIG_H
-#include "config.h"
-#endif
-
-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;
- GLint bits;
-
- mogltk::engine::setup();
- if (SDL_InitSubSystem(SDL_INIT_VIDEO) < 0) {
- throw GeneralException(String("Couldn't initialise Video SubSystem: ") + SDL_GetError());
- }
-
-#if 1
- SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
- SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
- SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
-// SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 32);
- SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8);
- SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
- SDL_GL_SetAttribute(SDL_GL_ACCUM_RED_SIZE, 8);
- SDL_GL_SetAttribute(SDL_GL_ACCUM_GREEN_SIZE, 8);
- SDL_GL_SetAttribute(SDL_GL_ACCUM_BLUE_SIZE, 8);
-#endif
- if (!(surface = SDL_SetVideoMode(w, h, 0, flags | SDL_OPENGL | SDL_HWSURFACE))) {
- throw GeneralException(String("Couldn't set GL mode: ") + SDL_GetError());
- }
-
- mogltk::engine::glbase_o = this;
- mogltk::engine::base_o = this;
-
- ratio = (GLdouble) surface->w / surface->h;
-
- glGetIntegerv(GL_STENCIL_BITS, &bits);
-
- printm(M_INFO, "Video resolution: %dx%dx%d (ratio = %3.2f)\n", surface->w, surface->h, surface->format->BitsPerPixel, ratio);
- printm(M_INFO, "\n");
- printm(M_INFO, "OpenGL infos\n");
- printm(M_INFO, "------------\n");
- printm(M_INFO, String("Vendor : ") + (char *) glGetString(GL_VENDOR) + "\n");
- printm(M_INFO, String("Renderer : ") + (char *) glGetString(GL_RENDERER) + "\n");
- printm(M_INFO, String("Version : ") + (char *) glGetString(GL_VERSION) + "\n");
- printm(M_INFO, String("Extensions: ") + (char *) glGetString(GL_EXTENSIONS) + "\n");
-
- glViewport(0, 0, surface->w, surface->h);
-
- glCullFace(GL_BACK);
- glFrontFace(GL_CCW);
- glEnable(GL_CULL_FACE);
-
- glClearColor(0, 0, 0, 0);
- glShadeModel(GL_SMOOTH);
-
- glEnable(GL_DEPTH_TEST);
- glDepthFunc(GL_LEQUAL);
- glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
-
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- gluPerspective(fovy, ratio, 1.0, 1024.0);
-
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
-
- glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
-
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
-
- SDL_GL_SwapBuffers();
- SDL_ShowCursor(SDL_DISABLE);
-
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
-
- setsurface(surface);
- mogltk::engine::postsetup();
-}
-
-mogltk::glbase::~glbase() {
-}
-
-void mogltk::glbase::Enter2DMode(void) {
- if (twoD)
- return;
-
- glPushAttrib(GL_ENABLE_BIT);
- glDisable(GL_DEPTH_TEST);
- glDisable(GL_CULL_FACE);
- glEnable(GL_BLEND);
- glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
-
- glMatrixMode(GL_PROJECTION);
- glPushMatrix();
- glLoadIdentity();
- glOrtho(0.0, GetWidth(), GetHeight(), 0.0, 0.0, 1.0);
-
- glMatrixMode(GL_MODELVIEW);
- glPushMatrix();
- glLoadIdentity();
-
- twoD = 1;
-}
-
-void mogltk::glbase::Leave2DMode(void) {
- if (!twoD)
- return;
-
- glMatrixMode(GL_MODELVIEW);
- glPopMatrix();
- glMatrixMode(GL_PROJECTION);
- glPopMatrix();
-
- glPopAttrib();
-
- twoD = 0;
-}
-
-void mogltk::glbase::Flip(bool clear) {
- mogltk::engine::pollevents();
- SDL_GL_SwapBuffers();
- if (clear)
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
-}
-
-bool mogltk::glbase::is2D() {
- return twoD;
-}
-
-void mogltk::glbase::glVertex(GLshort x, GLshort y, GLshort z, GLshort w) {
-#ifdef DEBUG
- printm(M_INFO, "Calling glVertex(%i, %i, %i, %i) (shorts)\n", x, y, z, w);
-#endif
- glVertex4i(x, y, z, w);
-}
-
-void mogltk::glbase::glVertex(GLint x, GLint y, GLint z, GLint w) {
-#ifdef DEBUG
- printm(M_INFO, "Calling glVertex(%i, %i, %i, %i) (ints)\n", x, y, z, w);
-#endif
- glVertex4i(x, y, z, w);
-}
-
-void mogltk::glbase::glVertex(GLfloat x, GLfloat y, GLfloat z, GLfloat w) {
-#ifdef DEBUG
- printm(M_INFO, "Calling glVertex(%f, %f, %f, %f) (floats)\n", x, y, z, w);
-#endif
- glVertex4f(x, y, z, w);
-}
-
-void mogltk::glbase::glVertex(GLdouble x, GLdouble y, GLdouble z, GLdouble w) {
-#ifdef DEBUG
- printm(M_INFO, "Calling glVertex(%f, %f, %f, %f) (doubles)\n", x, y, z, w);
-#endif
- glVertex4d(x, y, z, w);
-}
-
-void mogltk::glbase::changeviewport(int x, int y, unsigned int w, unsigned int h) {
-#if 0
- 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);
-#endif
- glScissor(x, GetHeight() - y - h - 1, w + 1, h + 1);
- glEnable(GL_SCISSOR_TEST);
-}
-
-void mogltk::glbase::changefovy(GLdouble nfoyv) {
- fovy = nfoyv;
- if (!engine::base_o->is2D())
- gluPerspective(fovy, ratio, 1.0, 1024.0);
-}
-
-inline static unsigned int nextpower(unsigned int n) {
- unsigned int i;
-
- if (!n)
- return n;
-
- if (ISPOT(n))
- return n;
-
- for (i = 31; i >= 0; i--) {
- if ((n >> i) & 1) {
- return 1 << (i + 1);
- }
- }
-}
-
-mogltk::texture * mogltk::glbase::GrabTexture() {
- int w = nextpower(GetWidth());
- int h = nextpower(GetHeight());
- texture * r = new texture(w, h);
- SDL_Surface * t;
-
- t = GrabSurface();
- SDL_BlitSurface(t, NULL, r->GetSurface(), NULL);
- SDL_FreeSurface(t);
-
- return r;
-}
-
-SDL_Surface * mogltk::glbase::GrabSurface() {
- int i;
- SDL_Surface * r;
- Uint8 * pixels = (Uint8 *) malloc(GetWidth() * GetHeight() * 3);
- Uint8 * s, * d;
-
- glReadPixels(0, 0, GetWidth(), GetHeight(), GL_RGB, GL_UNSIGNED_BYTE, pixels);
-
- r = SDL_CreateRGBSurface(SDL_SWSURFACE, GetWidth(), GetHeight(), 24,
-#if SDL_BYTEORDER == SDL_BIG_ENDIAN
- 0xff000000,
- 0x00ff0000,
- 0x0000ff00,
- 0x00000000
-#else
- 0x000000ff,
- 0x0000ff00,
- 0x00ff0000,
- 0x00000000
-#endif
- );
-
- for (i = 0; i < GetHeight(); i++) {
- s = pixels + i * GetWidth() * 3;
- d = ((Uint8 *) r->pixels) + (GetHeight() - i - 1) * GetWidth() * 3;
- memcpy(d, s, GetWidth() * 3);
- }
-
- free(pixels);
-
- return r;
-}
+/* + * mogltk + * Copyright (C) 1999-2004 Nicolas "Pixel" Noble + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +/* $Id: glbase.cc,v 1.22 2004-11-27 21:48:03 pixel Exp $ */ + +#include <stdio.h> +#include <SDL.h> +#include <SDL_opengl.h> +#include <Output.h> +#include <Handle.h> +#include "glbase.h" +#include "engine.h" +#include "generic.h" +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +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; + GLint bits; + + mogltk::engine::setup(); + if (SDL_InitSubSystem(SDL_INIT_VIDEO) < 0) { + throw GeneralException(String("Couldn't initialise Video SubSystem: ") + SDL_GetError()); + } + +#if 1 + SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8); + SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8); + SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8); +// SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 32); + SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8); + SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); + SDL_GL_SetAttribute(SDL_GL_ACCUM_RED_SIZE, 8); + SDL_GL_SetAttribute(SDL_GL_ACCUM_GREEN_SIZE, 8); + SDL_GL_SetAttribute(SDL_GL_ACCUM_BLUE_SIZE, 8); +#endif + if (!(surface = SDL_SetVideoMode(w, h, 0, flags | SDL_OPENGL | SDL_HWSURFACE))) { + throw GeneralException(String("Couldn't set GL mode: ") + SDL_GetError()); + } + + mogltk::engine::glbase_o = this; + mogltk::engine::base_o = this; + + ratio = (GLdouble) surface->w / surface->h; + + glGetIntegerv(GL_STENCIL_BITS, &bits); + + printm(M_INFO, "Video resolution: %dx%dx%d (ratio = %3.2f)\n", surface->w, surface->h, surface->format->BitsPerPixel, ratio); + printm(M_INFO, "\n"); + printm(M_INFO, "OpenGL infos\n"); + printm(M_INFO, "------------\n"); + printm(M_INFO, String("Vendor : ") + (char *) glGetString(GL_VENDOR) + "\n"); + printm(M_INFO, String("Renderer : ") + (char *) glGetString(GL_RENDERER) + "\n"); + printm(M_INFO, String("Version : ") + (char *) glGetString(GL_VERSION) + "\n"); + printm(M_INFO, String("Extensions: ") + (char *) glGetString(GL_EXTENSIONS) + "\n"); + + glViewport(0, 0, surface->w, surface->h); + + glCullFace(GL_BACK); + glFrontFace(GL_CCW); + glEnable(GL_CULL_FACE); + + glClearColor(0, 0, 0, 0); + glShadeModel(GL_SMOOTH); + + glEnable(GL_DEPTH_TEST); + glDepthFunc(GL_LEQUAL); + glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); + + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + gluPerspective(fovy, ratio, 1.0, 1024.0); + + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); + + glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); + + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + + SDL_GL_SwapBuffers(); + SDL_ShowCursor(SDL_DISABLE); + + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + + setsurface(surface); + mogltk::engine::postsetup(); +} + +mogltk::glbase::~glbase() { +} + +void mogltk::glbase::Enter2DMode(void) { + if (twoD) + return; + + glPushAttrib(GL_ENABLE_BIT); + glDisable(GL_DEPTH_TEST); + glDisable(GL_CULL_FACE); + glEnable(GL_BLEND); + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + + glMatrixMode(GL_PROJECTION); + glPushMatrix(); + glLoadIdentity(); + glOrtho(0.0, GetWidth(), GetHeight(), 0.0, 0.0, 1.0); + + glMatrixMode(GL_MODELVIEW); + glPushMatrix(); + glLoadIdentity(); + + twoD = 1; +} + +void mogltk::glbase::Leave2DMode(void) { + if (!twoD) + return; + + glMatrixMode(GL_MODELVIEW); + glPopMatrix(); + glMatrixMode(GL_PROJECTION); + glPopMatrix(); + + glPopAttrib(); + + twoD = 0; +} + +void mogltk::glbase::Flip(bool clear) { + mogltk::engine::pollevents(); + SDL_GL_SwapBuffers(); + if (clear) + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); +} + +bool mogltk::glbase::is2D() { + return twoD; +} + +void mogltk::glbase::glVertex(GLshort x, GLshort y, GLshort z, GLshort w) { +#ifdef DEBUG + printm(M_INFO, "Calling glVertex(%i, %i, %i, %i) (shorts)\n", x, y, z, w); +#endif + glVertex4i(x, y, z, w); +} + +void mogltk::glbase::glVertex(GLint x, GLint y, GLint z, GLint w) { +#ifdef DEBUG + printm(M_INFO, "Calling glVertex(%i, %i, %i, %i) (ints)\n", x, y, z, w); +#endif + glVertex4i(x, y, z, w); +} + +void mogltk::glbase::glVertex(GLfloat x, GLfloat y, GLfloat z, GLfloat w) { +#ifdef DEBUG + printm(M_INFO, "Calling glVertex(%f, %f, %f, %f) (floats)\n", x, y, z, w); +#endif + glVertex4f(x, y, z, w); +} + +void mogltk::glbase::glVertex(GLdouble x, GLdouble y, GLdouble z, GLdouble w) { +#ifdef DEBUG + printm(M_INFO, "Calling glVertex(%f, %f, %f, %f) (doubles)\n", x, y, z, w); +#endif + glVertex4d(x, y, z, w); +} + +void mogltk::glbase::changeviewport(int x, int y, unsigned int w, unsigned int h) { +#if 0 + 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); +#endif + glScissor(x, GetHeight() - y - h - 1, w + 1, h + 1); + glEnable(GL_SCISSOR_TEST); +} + +void mogltk::glbase::changefovy(GLdouble nfoyv) { + fovy = nfoyv; + if (!engine::base_o->is2D()) + gluPerspective(fovy, ratio, 1.0, 1024.0); +} + +inline static unsigned int nextpower(unsigned int n) { + unsigned int i; + + if (!n) + return n; + + if (ISPOT(n)) + return n; + + for (i = 31; i >= 0; i--) { + if ((n >> i) & 1) { + return 1 << (i + 1); + } + } +} + +mogltk::texture * mogltk::glbase::GrabTexture() { + int w = nextpower(GetWidth()); + int h = nextpower(GetHeight()); + texture * r = new texture(w, h); + SDL_Surface * t; + + t = GrabSurface(); + SDL_BlitSurface(t, NULL, r->GetSurface(), NULL); + SDL_FreeSurface(t); + + return r; +} + +SDL_Surface * mogltk::glbase::GrabSurface() { + int i; + SDL_Surface * r; + Uint8 * pixels = (Uint8 *) malloc(GetWidth() * GetHeight() * 3); + Uint8 * s, * d; + + glReadPixels(0, 0, GetWidth(), GetHeight(), GL_RGB, GL_UNSIGNED_BYTE, pixels); + + r = SDL_CreateRGBSurface(SDL_SWSURFACE, GetWidth(), GetHeight(), 24, +#if SDL_BYTEORDER == SDL_BIG_ENDIAN + 0xff000000, + 0x00ff0000, + 0x0000ff00, + 0x00000000 +#else + 0x000000ff, + 0x0000ff00, + 0x00ff0000, + 0x00000000 +#endif + ); + + for (i = 0; i < GetHeight(); i++) { + s = pixels + i * GetWidth() * 3; + d = ((Uint8 *) r->pixels) + (GetHeight() - i - 1) * GetWidth() * 3; + memcpy(d, s, GetWidth() * 3); + } + + free(pixels); + + return r; +} diff --git a/lib/glfont.cc b/lib/glfont.cc index 350f721..5b28591 100644 --- a/lib/glfont.cc +++ b/lib/glfont.cc @@ -1,72 +1,72 @@ -/*
- * mogltk
- * Copyright (C) 1999-2004 Nicolas "Pixel" Noble
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- */
-
-/* $Id: glfont.cc,v 1.24 2004-11-27 21:44:53 pixel Exp $ */
-
-#include "engine.h"
-#include "glbase.h"
-#include "glfont.h"
-#ifdef HAVE_CONFIG_H
-#include "config.h"
-#endif
-
-mogltk::glfont::glfont(Handle * ffont) : font(ffont) {
-}
-
-mogltk::glfont::~glfont() {
-}
-
-void mogltk::glfont::drawentry(Uint16 entry, int x, int y, ColorP c) {
- bool was2D;
- int trueentry, cx, cy, px, py;
-
- was2D = mogltk::engine::glbase_o->is2D();
-
- if (!was2D)
- mogltk::engine::glbase_o->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 , py ); glVertex2i(x + maxX , y );
- glTexCoord2i(px , py + maxY ); glVertex2i(x , y + maxY );
- glTexCoord2i(px + maxX , py + maxY ); glVertex2i(x + maxX , y + maxY );
- glEnd();
-
- if (!was2D)
- mogltk::engine::glbase_o->Leave2DMode();
-}
+/* + * mogltk + * Copyright (C) 1999-2004 Nicolas "Pixel" Noble + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +/* $Id: glfont.cc,v 1.25 2004-11-27 21:48:03 pixel Exp $ */ + +#include "engine.h" +#include "glbase.h" +#include "glfont.h" +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +mogltk::glfont::glfont(Handle * ffont) : font(ffont) { +} + +mogltk::glfont::~glfont() { +} + +void mogltk::glfont::drawentry(Uint16 entry, int x, int y, ColorP c) { + bool was2D; + int trueentry, cx, cy, px, py; + + was2D = mogltk::engine::glbase_o->is2D(); + + if (!was2D) + mogltk::engine::glbase_o->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 , py ); glVertex2i(x + maxX , y ); + glTexCoord2i(px , py + maxY ); glVertex2i(x , y + maxY ); + glTexCoord2i(px + maxX , py + maxY ); glVertex2i(x + maxX , y + maxY ); + glEnd(); + + if (!was2D) + mogltk::engine::glbase_o->Leave2DMode(); +} diff --git a/lib/glshape.cc b/lib/glshape.cc index 937173d..8fa718e 100644 --- a/lib/glshape.cc +++ b/lib/glshape.cc @@ -1,253 +1,253 @@ -/*
- * mogltk
- * Copyright (C) 1999-2004 Nicolas "Pixel" Noble
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- */
-
-/* $Id: glshape.cc,v 1.15 2004-11-27 21:44:53 pixel Exp $ */
-
-#include <SDL_opengl.h>
-#include "glbase.h"
-#include "glshape.h"
-#include "texture.h"
-#include "glfont.h"
-#include "engine.h"
-
-#define ENTER bool was2d = Enter(true)
-#define ENTERT bool was2d = Enter(false)
-#define LEAVE Leave(was2d)
-
-void mogltk::glshape::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::glshape::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::glshape::hline(int x1, int x2, int y, ColorP c1, ColorP c2) {
- box(x1, y, x2, y, c1, c2, c1, c2);
-}
-
-void mogltk::glshape::hline(int x1, int x2, int y, ColorP c) {
- box(x1, y, x2, y, c, c, c, c);
-}
-
-void mogltk::glshape::vline(int x, int y1, int y2, ColorP c1, ColorP c2) {
- box(x, y1, x, y2, c1, c1, c2, c2);
-}
-
-void mogltk::glshape::vline(int x, int y1, int y2, ColorP c) {
- box(x, y1, x, y2, c, c, c, c);
-}
-
-void mogltk::glshape::pixel(int x, int y, ColorP c) {
- box(x, y, x, y, c);
-}
-
-void mogltk::glshape::obox(int x1, int y1, int x2, int y2, ColorP c) {
- ENTER;
- shape::obox(x1, y1, x2, y2, c);
- LEAVE;
-}
-
-void mogltk::glshape::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 + 1, y2 - 1, c1, c3);
- vline(x2, y1 + 1, y2 - 1, c2, c4);
-
- LEAVE;
-}
-
-void mogltk::glshape::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::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();
- 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::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) {
- tbox(t, x1, y1, x2, y2, c1, c2, c3, c4, tx, ty, tx + (int) ((x2 - x1) * f), ty + (int) ((y2 - y1) * 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();
- 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::glshape::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;
-}
-
-bool mogltk::glshape::Enter(bool unbind) {
- bool was2D = mogltk::engine::glbase_o->is2D();
-
- if (!was2D)
- mogltk::engine::glbase_o->Enter2DMode();
-
- if (unbind) mogltk::texture::Unbind();
-
- return was2D;
-}
-
-bool mogltk::glshape::Enter() {
- return Enter(true);
-}
-
-void mogltk::glshape::Leave(bool was2D) {
- if (!was2D)
- mogltk::engine::glbase_o->Leave2DMode();
-}
-
-void mogltk::glshape::fdraw(fill * f, ColorP c, int sx, int sy) {
- ENTERT;
-
- if (!f)
- return;
-
- texture * t = f->GetTexture();
-
- if (!t) {
- filldrawer * w = new filldrawer(f, t = f->Talloc(), WHITE);
- f->walk(w);
- delete w;
- }
-
- c.Bind();
- t->Bind();
- int w = t->GetWidth();
- int h = t->GetHeight();
- int x = f->GetMinX() + sx;
- int y = f->GetMinY() + sy;
-
- glBegin(GL_TRIANGLE_STRIP);
- glTexCoord2i(0, 0); glVertex2i(x , y );
- glTexCoord2i(w, 0); glVertex2i(x + w, y );
- glTexCoord2i(0, h); glVertex2i(x , y + h);
- glTexCoord2i(w, h); glVertex2i(x + w, y + h);
- glEnd();
-
- LEAVE;
-}
-
-void mogltk::glshape::sdraw(fill * f, ColorP c, int sx, int sy) {
- ENTERT;
-
- if (!f)
- return;
-
- texture * t = f->GetSTexture();
-
- if (!t) {
- segdrawer * w = new segdrawer(f, t = f->STalloc(), WHITE);
- f->swalk(w);
- delete w;
- }
-
- c.Bind();
- t->Bind();
- int w = t->GetWidth();
- int h = t->GetHeight();
- int x = f->GetMinX() + sx;
- int y = f->GetMinY() + sy;
-
- glBegin(GL_TRIANGLE_STRIP);
- glTexCoord2i(0, 0); glVertex2i(x , y );
- glTexCoord2i(w, 0); glVertex2i(x + w, y );
- glTexCoord2i(0, h); glVertex2i(x , y + h);
- glTexCoord2i(w, h); glVertex2i(x + w, y + h);
- glEnd();
-
- LEAVE;
-}
+/* + * mogltk + * Copyright (C) 1999-2004 Nicolas "Pixel" Noble + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +/* $Id: glshape.cc,v 1.16 2004-11-27 21:48:03 pixel Exp $ */ + +#include <SDL_opengl.h> +#include "glbase.h" +#include "glshape.h" +#include "texture.h" +#include "glfont.h" +#include "engine.h" + +#define ENTER bool was2d = Enter(true) +#define ENTERT bool was2d = Enter(false) +#define LEAVE Leave(was2d) + +void mogltk::glshape::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::glshape::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::glshape::hline(int x1, int x2, int y, ColorP c1, ColorP c2) { + box(x1, y, x2, y, c1, c2, c1, c2); +} + +void mogltk::glshape::hline(int x1, int x2, int y, ColorP c) { + box(x1, y, x2, y, c, c, c, c); +} + +void mogltk::glshape::vline(int x, int y1, int y2, ColorP c1, ColorP c2) { + box(x, y1, x, y2, c1, c1, c2, c2); +} + +void mogltk::glshape::vline(int x, int y1, int y2, ColorP c) { + box(x, y1, x, y2, c, c, c, c); +} + +void mogltk::glshape::pixel(int x, int y, ColorP c) { + box(x, y, x, y, c); +} + +void mogltk::glshape::obox(int x1, int y1, int x2, int y2, ColorP c) { + ENTER; + shape::obox(x1, y1, x2, y2, c); + LEAVE; +} + +void mogltk::glshape::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 + 1, y2 - 1, c1, c3); + vline(x2, y1 + 1, y2 - 1, c2, c4); + + LEAVE; +} + +void mogltk::glshape::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::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(); + 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::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) { + tbox(t, x1, y1, x2, y2, c1, c2, c3, c4, tx, ty, tx + (int) ((x2 - x1) * f), ty + (int) ((y2 - y1) * 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(); + 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::glshape::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; +} + +bool mogltk::glshape::Enter(bool unbind) { + bool was2D = mogltk::engine::glbase_o->is2D(); + + if (!was2D) + mogltk::engine::glbase_o->Enter2DMode(); + + if (unbind) mogltk::texture::Unbind(); + + return was2D; +} + +bool mogltk::glshape::Enter() { + return Enter(true); +} + +void mogltk::glshape::Leave(bool was2D) { + if (!was2D) + mogltk::engine::glbase_o->Leave2DMode(); +} + +void mogltk::glshape::fdraw(fill * f, ColorP c, int sx, int sy) { + ENTERT; + + if (!f) + return; + + texture * t = f->GetTexture(); + + if (!t) { + filldrawer * w = new filldrawer(f, t = f->Talloc(), WHITE); + f->walk(w); + delete w; + } + + c.Bind(); + t->Bind(); + int w = t->GetWidth(); + int h = t->GetHeight(); + int x = f->GetMinX() + sx; + int y = f->GetMinY() + sy; + + glBegin(GL_TRIANGLE_STRIP); + glTexCoord2i(0, 0); glVertex2i(x , y ); + glTexCoord2i(w, 0); glVertex2i(x + w, y ); + glTexCoord2i(0, h); glVertex2i(x , y + h); + glTexCoord2i(w, h); glVertex2i(x + w, y + h); + glEnd(); + + LEAVE; +} + +void mogltk::glshape::sdraw(fill * f, ColorP c, int sx, int sy) { + ENTERT; + + if (!f) + return; + + texture * t = f->GetSTexture(); + + if (!t) { + segdrawer * w = new segdrawer(f, t = f->STalloc(), WHITE); + f->swalk(w); + delete w; + } + + c.Bind(); + t->Bind(); + int w = t->GetWidth(); + int h = t->GetHeight(); + int x = f->GetMinX() + sx; + int y = f->GetMinY() + sy; + + glBegin(GL_TRIANGLE_STRIP); + glTexCoord2i(0, 0); glVertex2i(x , y ); + glTexCoord2i(w, 0); glVertex2i(x + w, y ); + glTexCoord2i(0, h); glVertex2i(x , y + h); + glTexCoord2i(w, h); glVertex2i(x + w, y + h); + glEnd(); + + LEAVE; +} diff --git a/lib/glsprite.cc b/lib/glsprite.cc index 7091ca3..49db695 100644 --- a/lib/glsprite.cc +++ b/lib/glsprite.cc @@ -1,105 +1,105 @@ -/*
- * mogltk
- * Copyright (C) 1999-2004 Nicolas "Pixel" Noble
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- */
-
-/* $Id: glsprite.cc,v 1.10 2004-11-27 21:44:53 pixel Exp $ */
-
-#include <SDL.h>
-#include "glbase.h"
-#include "glsprite.h"
-#include "engine.h"
-
-#define TEXSIZE 256
-
-mogltk::glSprite::glSprite(Handle * h, int asx, int asy) : Sprite(h, asx, asy) {
-}
-
-mogltk::glSprite::glSprite(Uint8 * p, int asx, int asy) : Sprite(p, asx, asy) {
-}
-
-mogltk::glSprite::~glSprite() {
-}
-
-void mogltk::glSprite::draw(int dx, int dy, ColorP c, float sx, float sy) {
- bool was2D;
-
- if (!(was2D = mogltk::engine::glbase_o->is2D()))
- mogltk::engine::glbase_o->Enter2DMode();
-
- c.Bind();
-
- Bind();
- glBegin(GL_TRIANGLE_STRIP);
- glTexCoord2i(GetPX() , GetPY() ); glVertex2i(dx , dy );
- glTexCoord2i(GetPX() + GetSX(), GetPY() ); glVertex2i(dx + GetSX() * sx, dy );
- glTexCoord2i(GetPX() , GetPY() + GetSY()); glVertex2i(dx , dy + GetSY() * sy);
- glTexCoord2i(GetPX() + GetSX(), GetPY() + GetSY()); glVertex2i(dx + GetSX() * sx, dy + GetSY() * sy);
- glEnd();
-
- if (!was2D)
- mogltk::engine::glbase_o->Leave2DMode();
-}
-
-void mogltk::glSprite::bindcorner(float fx, float fy) {
- if (fx > 1)
- fx = 1;
- if (fy > 1)
- fy = 1;
- if (fx < 0)
- fx = 0;
- if (fy < 0)
- fy = 0;
- glTexCoord2i(GetPX() + fx * GetSX(), GetPY() + fy * GetSY());
-}
-
-void mogltk::glSprite::drawrotate(int cx, int cy, double a, ColorP c) {
- bool was2D;
-
- int sx, sy;
-
- sx = GetSX() / 2;
- sy = GetSY() / 2;
-
- was2D = mogltk::engine::glbase_o->is2D();
-
- if (!was2D)
- mogltk::engine::glbase_o->Enter2DMode();
-
- glMatrixMode(GL_MODELVIEW);
- glPushMatrix();
- glTranslated(cx, cy, 0);
- glRotated(a, 0, 0, 1);
-
-// glMatrixMode(GL_TEXTURE);
-// glRotated(a, 0, 0, 1);
-
- c.Bind();
-
- Bind();
- glBegin(GL_TRIANGLE_STRIP);
- glTexCoord2i(GetPX() , GetPY() ); glVertex2i( - sx, - sy);
- glTexCoord2i(GetPX() + GetSX(), GetPY() ); glVertex2i(GetSX() - sx, - sy);
- glTexCoord2i(GetPX() , GetPY() + GetSY()); glVertex2i( - sx, GetSY() - sy);
- glTexCoord2i(GetPX() + GetSX(), GetPY() + GetSY()); glVertex2i(GetSX() - sx, GetSY() - sy);
- glEnd();
-
- glPopMatrix();
-
- if (!was2D)
- mogltk::engine::glbase_o->Leave2DMode();
-}
+/* + * mogltk + * Copyright (C) 1999-2004 Nicolas "Pixel" Noble + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +/* $Id: glsprite.cc,v 1.11 2004-11-27 21:48:03 pixel Exp $ */ + +#include <SDL.h> +#include "glbase.h" +#include "glsprite.h" +#include "engine.h" + +#define TEXSIZE 256 + +mogltk::glSprite::glSprite(Handle * h, int asx, int asy) : Sprite(h, asx, asy) { +} + +mogltk::glSprite::glSprite(Uint8 * p, int asx, int asy) : Sprite(p, asx, asy) { +} + +mogltk::glSprite::~glSprite() { +} + +void mogltk::glSprite::draw(int dx, int dy, ColorP c, float sx, float sy) { + bool was2D; + + if (!(was2D = mogltk::engine::glbase_o->is2D())) + mogltk::engine::glbase_o->Enter2DMode(); + + c.Bind(); + + Bind(); + glBegin(GL_TRIANGLE_STRIP); + glTexCoord2i(GetPX() , GetPY() ); glVertex2i(dx , dy ); + glTexCoord2i(GetPX() + GetSX(), GetPY() ); glVertex2i(dx + GetSX() * sx, dy ); + glTexCoord2i(GetPX() , GetPY() + GetSY()); glVertex2i(dx , dy + GetSY() * sy); + glTexCoord2i(GetPX() + GetSX(), GetPY() + GetSY()); glVertex2i(dx + GetSX() * sx, dy + GetSY() * sy); + glEnd(); + + if (!was2D) + mogltk::engine::glbase_o->Leave2DMode(); +} + +void mogltk::glSprite::bindcorner(float fx, float fy) { + if (fx > 1) + fx = 1; + if (fy > 1) + fy = 1; + if (fx < 0) + fx = 0; + if (fy < 0) + fy = 0; + glTexCoord2i(GetPX() + fx * GetSX(), GetPY() + fy * GetSY()); +} + +void mogltk::glSprite::drawrotate(int cx, int cy, double a, ColorP c) { + bool was2D; + + int sx, sy; + + sx = GetSX() / 2; + sy = GetSY() / 2; + + was2D = mogltk::engine::glbase_o->is2D(); + + if (!was2D) + mogltk::engine::glbase_o->Enter2DMode(); + + glMatrixMode(GL_MODELVIEW); + glPushMatrix(); + glTranslated(cx, cy, 0); + glRotated(a, 0, 0, 1); + +// glMatrixMode(GL_TEXTURE); +// glRotated(a, 0, 0, 1); + + c.Bind(); + + Bind(); + glBegin(GL_TRIANGLE_STRIP); + glTexCoord2i(GetPX() , GetPY() ); glVertex2i( - sx, - sy); + glTexCoord2i(GetPX() + GetSX(), GetPY() ); glVertex2i(GetSX() - sx, - sy); + glTexCoord2i(GetPX() , GetPY() + GetSY()); glVertex2i( - sx, GetSY() - sy); + glTexCoord2i(GetPX() + GetSX(), GetPY() + GetSY()); glVertex2i(GetSX() - sx, GetSY() - sy); + glEnd(); + + glPopMatrix(); + + if (!was2D) + mogltk::engine::glbase_o->Leave2DMode(); +} diff --git a/lib/glwidgets.cc b/lib/glwidgets.cc index be3617f..5ff8c23 100644 --- a/lib/glwidgets.cc +++ b/lib/glwidgets.cc @@ -1,29 +1,29 @@ -/*
- * mogltk
- * Copyright (C) 1999-2004 Nicolas "Pixel" Noble
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- */
-
-/* $Id: glwidgets.cc,v 1.8 2004-11-27 21:44:53 pixel Exp $ */
-
-#include <SDL.h>
-#include <SDL_opengl.h>
-#include "mcolor.h"
-#include "glwidgets.h"
-
-void mogltk::widgets::glRoot::draw() {
- Shaper()->box(GetAX(), GetAY(), GetAX2(), GetAY2());
-}
+/* + * mogltk + * Copyright (C) 1999-2004 Nicolas "Pixel" Noble + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +/* $Id: glwidgets.cc,v 1.9 2004-11-27 21:48:03 pixel Exp $ */ + +#include <SDL.h> +#include <SDL_opengl.h> +#include "mcolor.h" +#include "glwidgets.h" + +void mogltk::widgets::glRoot::draw() { + Shaper()->box(GetAX(), GetAY(), GetAX2(), GetAY2()); +} diff --git a/lib/mcolor.cc b/lib/mcolor.cc index df75007..e2a921d 100644 --- a/lib/mcolor.cc +++ b/lib/mcolor.cc @@ -1,66 +1,66 @@ -/*
- * mogltk
- * Copyright (C) 1999-2004 Nicolas "Pixel" Noble
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- */
-
-/* $Id: mcolor.cc,v 1.7 2004-11-27 21:44:53 pixel Exp $ */
-
-#include <SDL.h>
-#include <SDL_opengl.h>
-#include "engine.h"
-#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) {
-}
-
-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);
-}
-
-Uint32 mogltk::ColorP::toSDL(SDL_PixelFormat * f) {
- if (!f)
- f = mogltk::engine::base_o->getsurface()->format;
-
- return SDL_MapRGBA(f, MIN(MAX(c.R, Min.R), Max.R), MIN(MAX(c.G, Min.G), Max.G), MIN(MAX(c.B, Min.B), Max.B), MIN(MAX(c.A, Min.A), Max.A));
-}
-
-void mogltk::ColorP::fromSDL(Uint32 p, SDL_PixelFormat * f) {
- if (!f)
- f = mogltk::engine::base_o->getsurface()->format;
-
- SDL_GetRGBA(p, f, &c.R, &c.G, &c.B, &c.A);
-}
-
-void mogltk::ColorP::Norm() {
- c.R = MIN(MAX(c.R, Min.R), Max.R);
- c.G = MIN(MAX(c.R, Min.G), Max.G);
- c.B = MIN(MAX(c.R, Min.B), Max.B);
- c.A = MIN(MAX(c.R, Min.A), Max.A);
-}
-
-bool mogltk::ColorP::operator==(const ColorP & nc) {
- return c == nc.c;
-}
-
-Color * mogltk::ColorP::operator->() {
- return &c;
-}
+/* + * mogltk + * Copyright (C) 1999-2004 Nicolas "Pixel" Noble + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +/* $Id: mcolor.cc,v 1.8 2004-11-27 21:48:03 pixel Exp $ */ + +#include <SDL.h> +#include <SDL_opengl.h> +#include "engine.h" +#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) { +} + +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); +} + +Uint32 mogltk::ColorP::toSDL(SDL_PixelFormat * f) { + if (!f) + f = mogltk::engine::base_o->getsurface()->format; + + return SDL_MapRGBA(f, MIN(MAX(c.R, Min.R), Max.R), MIN(MAX(c.G, Min.G), Max.G), MIN(MAX(c.B, Min.B), Max.B), MIN(MAX(c.A, Min.A), Max.A)); +} + +void mogltk::ColorP::fromSDL(Uint32 p, SDL_PixelFormat * f) { + if (!f) + f = mogltk::engine::base_o->getsurface()->format; + + SDL_GetRGBA(p, f, &c.R, &c.G, &c.B, &c.A); +} + +void mogltk::ColorP::Norm() { + c.R = MIN(MAX(c.R, Min.R), Max.R); + c.G = MIN(MAX(c.R, Min.G), Max.G); + c.B = MIN(MAX(c.R, Min.B), Max.B); + c.A = MIN(MAX(c.R, Min.A), Max.A); +} + +bool mogltk::ColorP::operator==(const ColorP & nc) { + return c == nc.c; +} + +Color * mogltk::ColorP::operator->() { + return &c; +} diff --git a/lib/shape.cc b/lib/shape.cc index 2a493cb..a7ed137 100644 --- a/lib/shape.cc +++ b/lib/shape.cc @@ -1,928 +1,928 @@ -/*
- * mogltk
- * Copyright (C) 1999-2004 Nicolas "Pixel" Noble
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- */
-
-/* $Id: shape.cc,v 1.18 2004-11-27 21:44:53 pixel Exp $ */
-
-#include <math.h>
-#include <limits.h>
-#include <SDL.h>
-#include <sys/types.h>
-#include <generic.h>
-#include "engine.h"
-#include "base.h"
-#include "shape.h"
-#include "texture.h"
-#include "font.h"
-
-#define ENTER bool flag = Enter()
-#define LEAVE Leave(flag)
-
-mogltk::fillwalker::fillwalker() {
-}
-
-mogltk::fillwalker::~fillwalker() {
-}
-
-void mogltk::fillwalker::step(int x, int y) {
-}
-
-mogltk::segwalker::segwalker() {
-}
-
-mogltk::segwalker::~segwalker() {
-}
-
-void mogltk::segwalker::step(int x1, int y1, int x2, int y2) {
-}
-
-mogltk::fill::fill() : minX(INT_MAX), minY(INT_MAX), maxX(INT_MIN), maxY(INT_MIN), cached(0), scached(0), header(0) {
-}
-
-mogltk::fill::~fill() {
- if (header)
- delete header;
-
- if (cached)
- delete cached;
-
- if (scached)
- delete scached;
-}
-
-void mogltk::fill::walk(fillwalker * w) {
- if (header)
- header->walk(w);
-}
-
-void mogltk::fill::swalk(segwalker * s) {
- std::vector<struct segment>::iterator i;
-
- for (i = segments.begin(); i != segments.end(); i++) {
- s->step(i->x1, i->y1, i->x2, i->y2);
- }
-}
-
-void mogltk::fill::insert(int x, int y) {
- if (cached) {
- delete cached;
- cached = 0;
- }
-
- if (scached) {
- delete scached;
- scached = 0;
- }
-
- if (!header) {
- new sline(y, this);
- }
- header->insert(x, y);
-
- if (x > maxX)
- maxX = x;
-
- if (x < minX)
- minX = x;
-
- if (y > maxY)
- maxY = y;
-
- if (y < minY)
- minY = y;
-}
-
-void mogltk::fill::insert(int x1, int y1, int x2, int y2) {
- int dx, dy, y;
- double x, i, i2;
-
- struct segment s;
- s.x1 = x1;
- s.y1 = y1;
- s.x2 = x2;
- s.y2 = y2;
- segments.push_back(s);
-
- if (y1 == y2)
- return;
-
- if (y2 < y1) {
- SWAP(x1, x2);
- SWAP(y1, y2);
- }
-
- dx = x1 - x2;
- dy = y1 - y2;
-
- x = x1;
- i = ((double) dx) / ((double) dy);
- i2 = i / 2;
- for (y = y1; y < y2; y++, x += i)
- insert(x + i2, y);
-}
-
-void mogltk::fill::insertfix(int x, int y) {
- if (!header)
- return;
-
- header->insertfix(x, y);
-}
-
-int mogltk::fill::GetMaxX() const {
- return maxX;
-}
-
-int mogltk::fill::GetMaxY() const {
- return maxY;
-}
-
-int mogltk::fill::GetMinX() const {
- return minX;
-}
-
-int mogltk::fill::GetMinY() const {
- return minY;
-}
-
-mogltk::texture * mogltk::fill::GetTexture() {
- return cached;
-}
-
-mogltk::texture * mogltk::fill::GetSTexture() {
- return scached;
-}
-
-mogltk::texture * mogltk::fill::Talloc() {
- if (cached)
- return cached;
-
- int x; int y;
-
- for (x = 1; x <= (maxX - minX); x <<= 1);
- for (y = 1; y <= (maxY - minY); y <<= 1);
-
- cached = new texture(x, y, true);
- return cached;
-}
-
-mogltk::texture * mogltk::fill::STalloc() {
- if (scached)
- return scached;
-
- int x; int y;
-
- for (x = 1; x <= (maxX - minX); x <<= 1);
- for (y = 1; y <= (maxY - minY); y <<= 1);
-
- scached = new texture(x, y, true);
- return scached;
-}
-
-mogltk::fill::sline::sline(int _y, fill * _header) : y(_y), header(_header), pheader(0) {
- if (!header->header) {
- header->header = this;
- } else {
- if (header->header->y > y) {
- next = header->header;
- header->header = this;
- } else {
- sline * p;
- for (p = header->header; p->next; p = p->next) {
- if (p->next->y > y) {
- next = p->next;
- p->next = this;
- return;
- }
- }
- p->next = this;
- next = 0;
- }
- }
-}
-
-mogltk::fill::sline::~sline() {
- if (pheader)
- delete pheader;
- if (next)
- delete next;
-}
-
-int mogltk::fill::sline::GetY() const {
- return y;
-}
-
-void mogltk::fill::sline::insert(int ax, int ay) {
- sline * f;
-
- if (ay == y) {
- if (!pheader)
- new point(ax, this);
- else if (!pheader->look(ax))
- new point(ax, this);
- } else {
- f = header->header->look(ay);
-
- if (!f) {
- f = new sline(ay, header);
- }
-
- f->insert(ax, ay);
- }
-}
-
-void mogltk::fill::sline::insertfix(int ax, int ay) {
- sline * f;
-
- if (ay == y) {
- if (count() & 1) {
- insert(ax, ay);
- }
- } else {
- f = header->header->look(ay);
- if (!f)
- return;
-
- f->insertfix(ax, ay);
- }
-}
-
-int mogltk::fill::sline::count() const {
- int r = 0;
-
- if (next)
- r = next->count();
-
- return r + 1;
-}
-
-void mogltk::fill::sline::walk(fillwalker * w) {
- if (pheader)
- pheader->walk(w);
- if (next)
- next->walk(w);
-}
-
-mogltk::fill::sline::point::point(int _x, sline * _header) : x(_x), header(_header) {
- if (!header->pheader) {
- header->pheader = this;
- } else {
- if (header->pheader->x > x) {
- next = header->pheader;
- header->pheader = this;
- } else {
- point * p;
- for (p = header->pheader; p->next; p = p->next) {
- if (p->next->x > x) {
- next = p->next;
- p->next = this;
- return;
- }
- }
- p->next = this;
- next = 0;
- }
- }
-}
-
-mogltk::fill::sline::point::~point() {
- if (next)
- delete next;
-}
-
-int mogltk::fill::sline::point::GetX() const {
- return x;
-}
-
-int mogltk::fill::sline::point::GetY() const {
- return header->GetY();
-}
-
-void mogltk::fill::sline::point::walk(fillwalker * w) {
- w->step(GetX(), GetY());
- if (next)
- next->walk(w);
-}
-
-mogltk::fill::sline::point * mogltk::fill::sline::point::look(int _x) {
- if (x > _x)
- return 0;
-
- if (x == _x)
- return this;
-
- if (!next)
- return 0;
-
- return next->look(_x);
-}
-
-mogltk::fill::sline * mogltk::fill::sline::look(int _y) {
- if (y > _y)
- return 0;
-
- if (y == _y)
- return this;
-
- if (!next)
- return 0;
-
- return next->look(_y);
-}
-
-mogltk::shape::shape(SDL_Surface * _surf) : surf(_surf) {
- if (!surf)
- surf = mogltk::engine::base_o->getsurface();
-}
-
-SDL_Surface * mogltk::shape::GetSurf() {
- return surf;
-}
-
-void mogltk::shape::box(int x1, int y1, int x2, int y2, ColorP c) {
- ENTER;
-
- SDL_Rect rect;
-
- rect.x = x1; rect.y = y1; rect.w = x2 - x1 + 1; rect.h = y2 - y1 + 1;
- SDL_FillRect(surf, &rect, c.toSDL(surf->format));
-
- LEAVE;
-}
-
-void mogltk::shape::hline(int x1, int x2, int y, ColorP c) {
- for (; x1 <= x2; x1++) {
- pixel(x1, 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) {
- for (; y1 <= y2; y1++) {
- pixel(x, y1, 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::bsubline_1(int x1, int y1, int x2, int y2, ColorP c) {
- int x, y, ddx, ddy, e;
- ddx = abs(x2 - x1);
- ddy = abs(y2 - y1) << 1;
- e = ddx - ddy;
- ddx <<= 1;
-
- if (x1 > x2) {
- SWAP(x1, x2);
- SWAP(y1, y2);
- }
-
- ENTER;
-
- for (x = x1, y = y1; x <= x2; x++) {
- pixel(x, y, c);
- if (e < 0) {
- y++;
- e += ddx - ddy;
- } else {
- e -= ddy;
- }
- }
-
- LEAVE;
-}
-
-void mogltk::shape::bsubline_2(int x1, int y1, int x2, int y2, ColorP c) {
- int x, y, ddx, ddy, e;
- ddx = abs(x2 - x1) << 1;
- ddy = abs(y2 - y1);
- e = ddy - ddx;
- ddy <<= 1;
-
- if (y1 > y2) {
- SWAP(x1, x2);
- SWAP(y1, y2);
- }
-
- ENTER;
-
- for (y = y1, x = x1; y <= y2; y++) {
- pixel(x, y, c);
- if (e < 0) {
- x++;
- e += ddy - ddx;
- } else {
- e -= ddx;
- }
- }
-
- LEAVE;
-}
-
-void mogltk::shape::bsubline_3(int x1, int y1, int x2, int y2, ColorP c) {
- int x, y, ddx, ddy, e;
- ddx = abs(x1 - x2) << 1;
- ddy = abs(y2 - y1);
- e = ddy - ddx;
- ddy <<= 1;
-
- if (y1 > y2) {
- SWAP(x1, x2);
- SWAP(y1, y2);
- }
-
- ENTER;
-
- for (y = y1, x = x1; y <= y2; y++) {
- pixel(x, y, c);
- if (e < 0) {
- x--;
- e += ddy - ddx;
- } else {
- e -= ddx;
- }
- }
-
- LEAVE;
-}
-
-void mogltk::shape::bsubline_4(int x1, int y1, int x2, int y2, ColorP c) {
- int x, y, ddx, ddy, e;
- ddy = abs(y2 - y1) << 1;
- ddx = abs(x1 - x2);
- e = ddx - ddy;
- ddx <<= 1;
-
- if (x1 > x2) {
- SWAP(x1, x2);
- SWAP(y1, y2);
- }
-
- for (x = x1, y = y1; x <= x2; x++) {
- pixel(x, y, c);
- if (e < 0) {
- y--;
- e += ddx - ddy;
- } else {
- e -= ddy;
- }
- }
-}
-
-void mogltk::shape::line(int x1, int y1, int x2, int y2, ColorP c) {
- if ((x1 == x2) && (y1 == y2)) {
- printm(M_INFO, "Line is a pixel...\n");
- pixel(x1, y1, c);
- return;
- }
-
- if (x1 == x2) {
- vline(x1, MIN(y1, y2), MAX(y1, y2), c);
- return;
- }
-
- if (y1 == y2) {
- hline(MIN(x1, x2), MAX(x1, x2), y1, c);
- return;
- }
-
- float k = float(y2 - y1) / float(x2 - x1);
-
- if ((k >= 0) && (k <= 1)) {
- bsubline_1(x1, y1, x2, y2, c);
- } else if (k > 1) {
- bsubline_2(x1, y1, x2, y2, c);
- } else if ((k < 0) && (k >= -1)) {
- bsubline_4(x1, y1, x2, y2, c);
- } else {
- bsubline_3(x1, y1, x2, y2, c);
- }
-}
-
-void mogltk::shape::pixel(int x, int y, ColorP c) {
- ENTER;
-
- int bpp = surf->format->BytesPerPixel;
- Uint8 *p = (Uint8 *)surf->pixels + y * surf->pitch + x * bpp;
- Uint32 pixel = c.toSDL(surf->format);
-
- if ((x < 0) || (y < 0) || (x >= surf->w) || (y >= surf->h)) {
- printm(M_INFO, "Pixel culling, out of bounds.\n");
- return;
- }
-
- 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;
- }
-
- LEAVE;
-}
-
-void mogltk::shape::circle(int x0, int y0, int r, ColorP c) {
- ENTER;
-
- int x = 0;
- int y = r - 1;
- int d = 3 - 2 * r;
- int dI = 10 - 4 * r;
- int rI = 6;
-
- while (x <= y) {
- pixel(x0 + x, y0 + y, c);
- pixel(x0 - x, y0 + y, c);
- pixel(x0 + x, y0 - y, c);
- pixel(x0 - x, y0 - y, c);
- pixel(x0 + y, y0 + x, c);
- pixel(x0 - y, y0 + x, c);
- pixel(x0 + y, y0 - x, c);
- pixel(x0 - y, y0 - x, c);
- if (d >= 0) {
- d += dI;
- dI += 8;
- y -= 1;
- } else {
- d += rI;
- dI += 4;
- }
- rI += 4;
- x += 1;
- }
-
- LEAVE;
-}
-
-mogltk::fill * mogltk::shape::fcircle(int x0, int y0, int r) {
- int x = 0;
- int y = r - 1;
- int d = 3 - 2 * r;
- int dI = 10 - 4 * r;
- int rI = 6;
- mogltk::fill * f = new fill();
- bool t = false;
- int ox = 0, oy = 0;
-
- while (x <= y) {
-/*
- if (t) {
- f->insert(x0 - x, y0 - y);
- f->insert(x0 + x, y0 - y);
- f->insert(x0 - x, y0 + y);
- f->insert(x0 + x, y0 + y);
- }
- f->insert(x0 - y, y0 - x);
- f->insert(x0 + y, y0 - x);
- f->insert(x0 - y, y0 + x);
- f->insert(x0 + y, y0 + x);
-*/
- if (t) {
- f->insert(x0 - ox, y0 - oy, x0 - x, y0 - y);
- f->insert(x0 + ox, y0 - oy, x0 + x, y0 - y);
- f->insert(x0 - ox, y0 + oy, x0 - x, y0 + y);
- f->insert(x0 + ox, y0 + oy, x0 + x, y0 + y);
- f->insert(x0 - oy, y0 - ox, x0 - y, y0 - x);
- f->insert(x0 + oy, y0 - ox, x0 + y, y0 - x);
- f->insert(x0 - oy, y0 + ox, x0 - y, y0 + x);
- f->insert(x0 + oy, y0 + ox, x0 + y, y0 + x);
- }
- ox = x;
- oy = y;
- t = true;
- if (d >= 0) {
- d += dI;
- dI += 8;
- y -= 1;
- } else {
- d += rI;
- dI += 4;
- }
- rI += 4;
- x += 1;
- }
- return f;
-}
-
-void mogltk::shape::pcircle(int x, int y, int r, ColorP c) {
- fill * f = fcircle(x, y, r);
- fdraw(f, c);
- delete f;
-}
-
-mogltk::filldrawer::filldrawer(fill * _f, texture * _t, ColorP _c) : f(_f), t(_t), c(_c), oldx(-1), oldy(-1) {
-}
-
-mogltk::filldrawer::~filldrawer() {
-}
-
-void mogltk::filldrawer::step(int x, int y) {
- if (oldy != y) {
- oldx = -1;
- }
- if (oldx == -1) {
- oldx = x;
- } else {
-/* s->hline(oldx, x, y, c); */
- Uint32 * p = t->GetPixels();
- int i,
- first = t->GetWidth() * (y - f->GetMinY()) + oldx - f->GetMinX(),
- last = first - oldx + x;
- SDL_PixelFormat * format = t->GetFormat();
-
- for (i = first; i <= last; i++) {
- if (i > (t->GetWidth() * t->GetHeight())) {
- printm(M_ERROR, "Big problem here. i = %i and W, H = %i, %i\n", i, t->GetWidth(), t->GetHeight());
- printm(M_ERROR, "Initially, wanted to draw hline(%i, %i, %i)\n", oldx, x, y);
- printm(M_ERROR, "With MinX = %i and MinY = %i --> hline(%i, %i, %i)\n", f->GetMinX(), f->GetMinY(), oldx - f->GetMinX(), x - f->GetMinX(), y - f->GetMinY());
- printm(M_ERROR, "For info, MaxX = %i and MaxY = %i\n", f->GetMaxX(), f->GetMaxY());
- exit(-1);
- }
- p[i] = c.toSDL(format);
- }
-
- oldx = -1;
- }
- oldy = y;
-}
-
-void mogltk::shape::fdraw(fill * f, ColorP c, int sx, int sy) {
- ENTER;
- if (!f)
- return;
- if (!f->GetTexture()) {
- filldrawer * w = new filldrawer(f, f->Talloc(), c);
- f->walk(w);
- delete w;
- f->last = c.c;
- }
- SDL_PixelFormat * format = f->GetTexture()->GetFormat();
-
- if (f->last == c.c) {
- Uint32 * p = f->GetTexture()->GetPixels();
- int i, n = f->GetTexture()->GetWidth() * f->GetTexture()->GetHeight();
- for (i = 0; i < n; i++)
- if (p[i] & 0xff000000)
- p[i] = c.toSDL(format);
- }
- SDL_Rect r;
- r.x = f->GetMinX() + sx;
- r.y = f->GetMinY() + sy;
- SDL_BlitSurface(f->GetTexture()->GetSurface(), 0, surf, &r);
-
- f->last = c.c;
- LEAVE;
-}
-
-mogltk::segdrawer::segdrawer(fill * _f, texture * _t, ColorP _c) : f(_f), t(_t), c(_c), sh(new shape(_t->GetSurface())) {
-}
-
-mogltk::segdrawer::~segdrawer() {
- delete sh;
-}
-
-void mogltk::segdrawer::step(int x1, int y1, int x2, int y2) {
- sh->line(x1 - f->GetMinX(), y1 - f->GetMinY(), x2 - f->GetMinX(), y2 - f->GetMinY(), c);
-}
-
-void mogltk::shape::sdraw(fill * f, ColorP c, int sx, int sy) {
- ENTER;
-
- if (!f)
- return;
-
- if (!f->GetSTexture()) {
- segdrawer * w = new segdrawer(f, f->STalloc(), c);
- f->swalk(w);
- delete w;
- f->last = c.c;
- }
- SDL_PixelFormat * format = f->GetSTexture()->GetFormat();
-
- if (f->last == c.c) {
- Uint32 * p = f->GetSTexture()->GetPixels();
- int i, n = f->GetSTexture()->GetWidth() * f->GetSTexture()->GetHeight();
- for (i = 0; i < n; i++)
- if (p[i] & 0xff000000)
- p[i] = c.toSDL(format);
- }
- SDL_Rect r;
- r.x = f->GetMinX() + sx;
- r.y = f->GetMinY() + sy;
- SDL_BlitSurface(f->GetSTexture()->GetSurface(), 0, surf, &r);
-
- f->last = c.c;
- LEAVE;
-}
-
-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 + 1, y2 - 1, c);
- vline(x2, y1 + 1, y2 - 1, c);
-
- LEAVE;
-}
-
-void mogltk::shape::box3d(int x1, int y1, int x2, int y2, ColorP face, ColorP shade1, ColorP shade2, int depth, bool bevel) {
- ENTER;
-
- int i;
-
- for (i = 0; i < depth; i++) {
- hline(x1 + i, x2 - i, y1 + i, bevel ? shade2 : shade1);
- vline(x1 + i, y1 + i, y2 - i, bevel ? shade2 : shade1);
- }
-
- for (i = 0; i < depth; i++) {
- hline(x1 + i, x2 - i, y2 - i, bevel ? shade1 : shade2);
- vline(x2 - i, y1 + i, y2 - i, bevel ? shade1 : shade2);
- }
-
- box(x1 + depth, y1 + depth, x2 - depth, y2 - depth, face);
-
- 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() {
- if (SDL_MUSTLOCK(surf)) {
- SDL_LockSurface(surf);
- return true;
- } else {
- return false;
- }
-}
-
-void mogltk::shape::Leave(bool locked) {
- if (locked)
- SDL_UnlockSurface(surf);
-}
-
-void mogltk::shape::tbox(texture *, int x1, int y1, int x2, int y2, int tx, int ty, double f, ColorP c) {
-}
-
-void mogltk::shape::tbox(texture *, int x1, int y1, int x2, int y2, int tx1, int ty1, int tx2, int ty2, ColorP c) {
-}
-
-void mogltk::shape::tbox(texture *, int x1, int y1, int x2, int y2, ColorP, ColorP, ColorP, ColorP, int tx, int ty, double f) {
-}
-
-void mogltk::shape::tbox(texture *, int x1, int y1, int x2, int y2, ColorP, ColorP, ColorP, ColorP, int tx1, int ty1, int tx2, int ty2) {
-}
-
-void mogltk::shape::vline(int x, int y1, int y2, ColorP c1, ColorP c2) {
-}
-
-void mogltk::shape::hline(int x1, int x2, int y, ColorP c1, ColorP c2) {
-}
-
-void mogltk::shape::box(int x1, int y1, int x2, int y2, ColorP c1, ColorP c2, ColorP c3, ColorP c4) {
-}
-
-void mogltk::shape::obox(int x1, int y1, int x2, int y2, ColorP c1, ColorP c2, ColorP c3, ColorP c4) {
-}
+/* + * mogltk + * Copyright (C) 1999-2004 Nicolas "Pixel" Noble + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +/* $Id: shape.cc,v 1.19 2004-11-27 21:48:03 pixel Exp $ */ + +#include <math.h> +#include <limits.h> +#include <SDL.h> +#include <sys/types.h> +#include <generic.h> +#include "engine.h" +#include "base.h" +#include "shape.h" +#include "texture.h" +#include "font.h" + +#define ENTER bool flag = Enter() +#define LEAVE Leave(flag) + +mogltk::fillwalker::fillwalker() { +} + +mogltk::fillwalker::~fillwalker() { +} + +void mogltk::fillwalker::step(int x, int y) { +} + +mogltk::segwalker::segwalker() { +} + +mogltk::segwalker::~segwalker() { +} + +void mogltk::segwalker::step(int x1, int y1, int x2, int y2) { +} + +mogltk::fill::fill() : minX(INT_MAX), minY(INT_MAX), maxX(INT_MIN), maxY(INT_MIN), cached(0), scached(0), header(0) { +} + +mogltk::fill::~fill() { + if (header) + delete header; + + if (cached) + delete cached; + + if (scached) + delete scached; +} + +void mogltk::fill::walk(fillwalker * w) { + if (header) + header->walk(w); +} + +void mogltk::fill::swalk(segwalker * s) { + std::vector<struct segment>::iterator i; + + for (i = segments.begin(); i != segments.end(); i++) { + s->step(i->x1, i->y1, i->x2, i->y2); + } +} + +void mogltk::fill::insert(int x, int y) { + if (cached) { + delete cached; + cached = 0; + } + + if (scached) { + delete scached; + scached = 0; + } + + if (!header) { + new sline(y, this); + } + header->insert(x, y); + + if (x > maxX) + maxX = x; + + if (x < minX) + minX = x; + + if (y > maxY) + maxY = y; + + if (y < minY) + minY = y; +} + +void mogltk::fill::insert(int x1, int y1, int x2, int y2) { + int dx, dy, y; + double x, i, i2; + + struct segment s; + s.x1 = x1; + s.y1 = y1; + s.x2 = x2; + s.y2 = y2; + segments.push_back(s); + + if (y1 == y2) + return; + + if (y2 < y1) { + SWAP(x1, x2); + SWAP(y1, y2); + } + + dx = x1 - x2; + dy = y1 - y2; + + x = x1; + i = ((double) dx) / ((double) dy); + i2 = i / 2; + for (y = y1; y < y2; y++, x += i) + insert(x + i2, y); +} + +void mogltk::fill::insertfix(int x, int y) { + if (!header) + return; + + header->insertfix(x, y); +} + +int mogltk::fill::GetMaxX() const { + return maxX; +} + +int mogltk::fill::GetMaxY() const { + return maxY; +} + +int mogltk::fill::GetMinX() const { + return minX; +} + +int mogltk::fill::GetMinY() const { + return minY; +} + +mogltk::texture * mogltk::fill::GetTexture() { + return cached; +} + +mogltk::texture * mogltk::fill::GetSTexture() { + return scached; +} + +mogltk::texture * mogltk::fill::Talloc() { + if (cached) + return cached; + + int x; int y; + + for (x = 1; x <= (maxX - minX); x <<= 1); + for (y = 1; y <= (maxY - minY); y <<= 1); + + cached = new texture(x, y, true); + return cached; +} + +mogltk::texture * mogltk::fill::STalloc() { + if (scached) + return scached; + + int x; int y; + + for (x = 1; x <= (maxX - minX); x <<= 1); + for (y = 1; y <= (maxY - minY); y <<= 1); + + scached = new texture(x, y, true); + return scached; +} + +mogltk::fill::sline::sline(int _y, fill * _header) : y(_y), header(_header), pheader(0) { + if (!header->header) { + header->header = this; + } else { + if (header->header->y > y) { + next = header->header; + header->header = this; + } else { + sline * p; + for (p = header->header; p->next; p = p->next) { + if (p->next->y > y) { + next = p->next; + p->next = this; + return; + } + } + p->next = this; + next = 0; + } + } +} + +mogltk::fill::sline::~sline() { + if (pheader) + delete pheader; + if (next) + delete next; +} + +int mogltk::fill::sline::GetY() const { + return y; +} + +void mogltk::fill::sline::insert(int ax, int ay) { + sline * f; + + if (ay == y) { + if (!pheader) + new point(ax, this); + else if (!pheader->look(ax)) + new point(ax, this); + } else { + f = header->header->look(ay); + + if (!f) { + f = new sline(ay, header); + } + + f->insert(ax, ay); + } +} + +void mogltk::fill::sline::insertfix(int ax, int ay) { + sline * f; + + if (ay == y) { + if (count() & 1) { + insert(ax, ay); + } + } else { + f = header->header->look(ay); + if (!f) + return; + + f->insertfix(ax, ay); + } +} + +int mogltk::fill::sline::count() const { + int r = 0; + + if (next) + r = next->count(); + + return r + 1; +} + +void mogltk::fill::sline::walk(fillwalker * w) { + if (pheader) + pheader->walk(w); + if (next) + next->walk(w); +} + +mogltk::fill::sline::point::point(int _x, sline * _header) : x(_x), header(_header) { + if (!header->pheader) { + header->pheader = this; + } else { + if (header->pheader->x > x) { + next = header->pheader; + header->pheader = this; + } else { + point * p; + for (p = header->pheader; p->next; p = p->next) { + if (p->next->x > x) { + next = p->next; + p->next = this; + return; + } + } + p->next = this; + next = 0; + } + } +} + +mogltk::fill::sline::point::~point() { + if (next) + delete next; +} + +int mogltk::fill::sline::point::GetX() const { + return x; +} + +int mogltk::fill::sline::point::GetY() const { + return header->GetY(); +} + +void mogltk::fill::sline::point::walk(fillwalker * w) { + w->step(GetX(), GetY()); + if (next) + next->walk(w); +} + +mogltk::fill::sline::point * mogltk::fill::sline::point::look(int _x) { + if (x > _x) + return 0; + + if (x == _x) + return this; + + if (!next) + return 0; + + return next->look(_x); +} + +mogltk::fill::sline * mogltk::fill::sline::look(int _y) { + if (y > _y) + return 0; + + if (y == _y) + return this; + + if (!next) + return 0; + + return next->look(_y); +} + +mogltk::shape::shape(SDL_Surface * _surf) : surf(_surf) { + if (!surf) + surf = mogltk::engine::base_o->getsurface(); +} + +SDL_Surface * mogltk::shape::GetSurf() { + return surf; +} + +void mogltk::shape::box(int x1, int y1, int x2, int y2, ColorP c) { + ENTER; + + SDL_Rect rect; + + rect.x = x1; rect.y = y1; rect.w = x2 - x1 + 1; rect.h = y2 - y1 + 1; + SDL_FillRect(surf, &rect, c.toSDL(surf->format)); + + LEAVE; +} + +void mogltk::shape::hline(int x1, int x2, int y, ColorP c) { + for (; x1 <= x2; x1++) { + pixel(x1, 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) { + for (; y1 <= y2; y1++) { + pixel(x, y1, 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::bsubline_1(int x1, int y1, int x2, int y2, ColorP c) { + int x, y, ddx, ddy, e; + ddx = abs(x2 - x1); + ddy = abs(y2 - y1) << 1; + e = ddx - ddy; + ddx <<= 1; + + if (x1 > x2) { + SWAP(x1, x2); + SWAP(y1, y2); + } + + ENTER; + + for (x = x1, y = y1; x <= x2; x++) { + pixel(x, y, c); + if (e < 0) { + y++; + e += ddx - ddy; + } else { + e -= ddy; + } + } + + LEAVE; +} + +void mogltk::shape::bsubline_2(int x1, int y1, int x2, int y2, ColorP c) { + int x, y, ddx, ddy, e; + ddx = abs(x2 - x1) << 1; + ddy = abs(y2 - y1); + e = ddy - ddx; + ddy <<= 1; + + if (y1 > y2) { + SWAP(x1, x2); + SWAP(y1, y2); + } + + ENTER; + + for (y = y1, x = x1; y <= y2; y++) { + pixel(x, y, c); + if (e < 0) { + x++; + e += ddy - ddx; + } else { + e -= ddx; + } + } + + LEAVE; +} + +void mogltk::shape::bsubline_3(int x1, int y1, int x2, int y2, ColorP c) { + int x, y, ddx, ddy, e; + ddx = abs(x1 - x2) << 1; + ddy = abs(y2 - y1); + e = ddy - ddx; + ddy <<= 1; + + if (y1 > y2) { + SWAP(x1, x2); + SWAP(y1, y2); + } + + ENTER; + + for (y = y1, x = x1; y <= y2; y++) { + pixel(x, y, c); + if (e < 0) { + x--; + e += ddy - ddx; + } else { + e -= ddx; + } + } + + LEAVE; +} + +void mogltk::shape::bsubline_4(int x1, int y1, int x2, int y2, ColorP c) { + int x, y, ddx, ddy, e; + ddy = abs(y2 - y1) << 1; + ddx = abs(x1 - x2); + e = ddx - ddy; + ddx <<= 1; + + if (x1 > x2) { + SWAP(x1, x2); + SWAP(y1, y2); + } + + for (x = x1, y = y1; x <= x2; x++) { + pixel(x, y, c); + if (e < 0) { + y--; + e += ddx - ddy; + } else { + e -= ddy; + } + } +} + +void mogltk::shape::line(int x1, int y1, int x2, int y2, ColorP c) { + if ((x1 == x2) && (y1 == y2)) { + printm(M_INFO, "Line is a pixel...\n"); + pixel(x1, y1, c); + return; + } + + if (x1 == x2) { + vline(x1, MIN(y1, y2), MAX(y1, y2), c); + return; + } + + if (y1 == y2) { + hline(MIN(x1, x2), MAX(x1, x2), y1, c); + return; + } + + float k = float(y2 - y1) / float(x2 - x1); + + if ((k >= 0) && (k <= 1)) { + bsubline_1(x1, y1, x2, y2, c); + } else if (k > 1) { + bsubline_2(x1, y1, x2, y2, c); + } else if ((k < 0) && (k >= -1)) { + bsubline_4(x1, y1, x2, y2, c); + } else { + bsubline_3(x1, y1, x2, y2, c); + } +} + +void mogltk::shape::pixel(int x, int y, ColorP c) { + ENTER; + + int bpp = surf->format->BytesPerPixel; + Uint8 *p = (Uint8 *)surf->pixels + y * surf->pitch + x * bpp; + Uint32 pixel = c.toSDL(surf->format); + + if ((x < 0) || (y < 0) || (x >= surf->w) || (y >= surf->h)) { + printm(M_INFO, "Pixel culling, out of bounds.\n"); + return; + } + + 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; + } + + LEAVE; +} + +void mogltk::shape::circle(int x0, int y0, int r, ColorP c) { + ENTER; + + int x = 0; + int y = r - 1; + int d = 3 - 2 * r; + int dI = 10 - 4 * r; + int rI = 6; + + while (x <= y) { + pixel(x0 + x, y0 + y, c); + pixel(x0 - x, y0 + y, c); + pixel(x0 + x, y0 - y, c); + pixel(x0 - x, y0 - y, c); + pixel(x0 + y, y0 + x, c); + pixel(x0 - y, y0 + x, c); + pixel(x0 + y, y0 - x, c); + pixel(x0 - y, y0 - x, c); + if (d >= 0) { + d += dI; + dI += 8; + y -= 1; + } else { + d += rI; + dI += 4; + } + rI += 4; + x += 1; + } + + LEAVE; +} + +mogltk::fill * mogltk::shape::fcircle(int x0, int y0, int r) { + int x = 0; + int y = r - 1; + int d = 3 - 2 * r; + int dI = 10 - 4 * r; + int rI = 6; + mogltk::fill * f = new fill(); + bool t = false; + int ox = 0, oy = 0; + + while (x <= y) { +/* + if (t) { + f->insert(x0 - x, y0 - y); + f->insert(x0 + x, y0 - y); + f->insert(x0 - x, y0 + y); + f->insert(x0 + x, y0 + y); + } + f->insert(x0 - y, y0 - x); + f->insert(x0 + y, y0 - x); + f->insert(x0 - y, y0 + x); + f->insert(x0 + y, y0 + x); +*/ + if (t) { + f->insert(x0 - ox, y0 - oy, x0 - x, y0 - y); + f->insert(x0 + ox, y0 - oy, x0 + x, y0 - y); + f->insert(x0 - ox, y0 + oy, x0 - x, y0 + y); + f->insert(x0 + ox, y0 + oy, x0 + x, y0 + y); + f->insert(x0 - oy, y0 - ox, x0 - y, y0 - x); + f->insert(x0 + oy, y0 - ox, x0 + y, y0 - x); + f->insert(x0 - oy, y0 + ox, x0 - y, y0 + x); + f->insert(x0 + oy, y0 + ox, x0 + y, y0 + x); + } + ox = x; + oy = y; + t = true; + if (d >= 0) { + d += dI; + dI += 8; + y -= 1; + } else { + d += rI; + dI += 4; + } + rI += 4; + x += 1; + } + return f; +} + +void mogltk::shape::pcircle(int x, int y, int r, ColorP c) { + fill * f = fcircle(x, y, r); + fdraw(f, c); + delete f; +} + +mogltk::filldrawer::filldrawer(fill * _f, texture * _t, ColorP _c) : f(_f), t(_t), c(_c), oldx(-1), oldy(-1) { +} + +mogltk::filldrawer::~filldrawer() { +} + +void mogltk::filldrawer::step(int x, int y) { + if (oldy != y) { + oldx = -1; + } + if (oldx == -1) { + oldx = x; + } else { +/* s->hline(oldx, x, y, c); */ + Uint32 * p = t->GetPixels(); + int i, + first = t->GetWidth() * (y - f->GetMinY()) + oldx - f->GetMinX(), + last = first - oldx + x; + SDL_PixelFormat * format = t->GetFormat(); + + for (i = first; i <= last; i++) { + if (i > (t->GetWidth() * t->GetHeight())) { + printm(M_ERROR, "Big problem here. i = %i and W, H = %i, %i\n", i, t->GetWidth(), t->GetHeight()); + printm(M_ERROR, "Initially, wanted to draw hline(%i, %i, %i)\n", oldx, x, y); + printm(M_ERROR, "With MinX = %i and MinY = %i --> hline(%i, %i, %i)\n", f->GetMinX(), f->GetMinY(), oldx - f->GetMinX(), x - f->GetMinX(), y - f->GetMinY()); + printm(M_ERROR, "For info, MaxX = %i and MaxY = %i\n", f->GetMaxX(), f->GetMaxY()); + exit(-1); + } + p[i] = c.toSDL(format); + } + + oldx = -1; + } + oldy = y; +} + +void mogltk::shape::fdraw(fill * f, ColorP c, int sx, int sy) { + ENTER; + if (!f) + return; + if (!f->GetTexture()) { + filldrawer * w = new filldrawer(f, f->Talloc(), c); + f->walk(w); + delete w; + f->last = c.c; + } + SDL_PixelFormat * format = f->GetTexture()->GetFormat(); + + if (f->last == c.c) { + Uint32 * p = f->GetTexture()->GetPixels(); + int i, n = f->GetTexture()->GetWidth() * f->GetTexture()->GetHeight(); + for (i = 0; i < n; i++) + if (p[i] & 0xff000000) + p[i] = c.toSDL(format); + } + SDL_Rect r; + r.x = f->GetMinX() + sx; + r.y = f->GetMinY() + sy; + SDL_BlitSurface(f->GetTexture()->GetSurface(), 0, surf, &r); + + f->last = c.c; + LEAVE; +} + +mogltk::segdrawer::segdrawer(fill * _f, texture * _t, ColorP _c) : f(_f), t(_t), c(_c), sh(new shape(_t->GetSurface())) { +} + +mogltk::segdrawer::~segdrawer() { + delete sh; +} + +void mogltk::segdrawer::step(int x1, int y1, int x2, int y2) { + sh->line(x1 - f->GetMinX(), y1 - f->GetMinY(), x2 - f->GetMinX(), y2 - f->GetMinY(), c); +} + +void mogltk::shape::sdraw(fill * f, ColorP c, int sx, int sy) { + ENTER; + + if (!f) + return; + + if (!f->GetSTexture()) { + segdrawer * w = new segdrawer(f, f->STalloc(), c); + f->swalk(w); + delete w; + f->last = c.c; + } + SDL_PixelFormat * format = f->GetSTexture()->GetFormat(); + + if (f->last == c.c) { + Uint32 * p = f->GetSTexture()->GetPixels(); + int i, n = f->GetSTexture()->GetWidth() * f->GetSTexture()->GetHeight(); + for (i = 0; i < n; i++) + if (p[i] & 0xff000000) + p[i] = c.toSDL(format); + } + SDL_Rect r; + r.x = f->GetMinX() + sx; + r.y = f->GetMinY() + sy; + SDL_BlitSurface(f->GetSTexture()->GetSurface(), 0, surf, &r); + + f->last = c.c; + LEAVE; +} + +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 + 1, y2 - 1, c); + vline(x2, y1 + 1, y2 - 1, c); + + LEAVE; +} + +void mogltk::shape::box3d(int x1, int y1, int x2, int y2, ColorP face, ColorP shade1, ColorP shade2, int depth, bool bevel) { + ENTER; + + int i; + + for (i = 0; i < depth; i++) { + hline(x1 + i, x2 - i, y1 + i, bevel ? shade2 : shade1); + vline(x1 + i, y1 + i, y2 - i, bevel ? shade2 : shade1); + } + + for (i = 0; i < depth; i++) { + hline(x1 + i, x2 - i, y2 - i, bevel ? shade1 : shade2); + vline(x2 - i, y1 + i, y2 - i, bevel ? shade1 : shade2); + } + + box(x1 + depth, y1 + depth, x2 - depth, y2 - depth, face); + + 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() { + if (SDL_MUSTLOCK(surf)) { + SDL_LockSurface(surf); + return true; + } else { + return false; + } +} + +void mogltk::shape::Leave(bool locked) { + if (locked) + SDL_UnlockSurface(surf); +} + +void mogltk::shape::tbox(texture *, int x1, int y1, int x2, int y2, int tx, int ty, double f, ColorP c) { +} + +void mogltk::shape::tbox(texture *, int x1, int y1, int x2, int y2, int tx1, int ty1, int tx2, int ty2, ColorP c) { +} + +void mogltk::shape::tbox(texture *, int x1, int y1, int x2, int y2, ColorP, ColorP, ColorP, ColorP, int tx, int ty, double f) { +} + +void mogltk::shape::tbox(texture *, int x1, int y1, int x2, int y2, ColorP, ColorP, ColorP, ColorP, int tx1, int ty1, int tx2, int ty2) { +} + +void mogltk::shape::vline(int x, int y1, int y2, ColorP c1, ColorP c2) { +} + +void mogltk::shape::hline(int x1, int x2, int y, ColorP c1, ColorP c2) { +} + +void mogltk::shape::box(int x1, int y1, int x2, int y2, ColorP c1, ColorP c2, ColorP c3, ColorP c4) { +} + +void mogltk::shape::obox(int x1, int y1, int x2, int y2, ColorP c1, ColorP c2, ColorP c3, ColorP c4) { +} diff --git a/lib/sprite.cc b/lib/sprite.cc index ef5d7a8..bd0df94 100644 --- a/lib/sprite.cc +++ b/lib/sprite.cc @@ -1,190 +1,190 @@ -/*
- * mogltk
- * Copyright (C) 1999-2004 Nicolas "Pixel" Noble
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- */
-
-/* $Id: sprite.cc,v 1.14 2004-11-27 21:44:53 pixel Exp $ */
-
-#include <SDL.h>
-#include "engine.h"
-#include "base.h"
-#include "sprite.h"
-
-#define TEXSIZE 1024
-
-mogltk::Sprite * mogltk::Sprite::Cursor;
-
-mogltk::Sprite::TexList * mogltk::Sprite::TexList::header = 0;
-
-mogltk::Sprite::Sprite(Handle * h, int asx, int asy) throw (GeneralException) : 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) {
- printm(M_INFO, "Allocating a new texture for sprites\n");
- posx = posy = 0;
- p = new TexList(TEXSIZE);
- }
-
- tlist = p;
-}
-
-mogltk::Sprite::TexList::TexList(int size) {
- tex = new texture(size, size, true);
-
- if (header)
- header->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();
-}
-
-SDL_Surface * mogltk::Sprite::TexList::GetSurface() {
- return tex->GetSurface();
-}
-
-
-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));
-}
-
-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;
-}
-
-void mogltk::Sprite::draw(int dx, int dy, ColorP c, float sx, float sy) {
- bool locked = false;
-
- if (SDL_MUSTLOCK(mogltk::engine::base_o->getsurface())) {
- locked = true;
- SDL_LockSurface(mogltk::engine::base_o->getsurface());
- }
-
- SDL_Rect src, dst;
-
- src.x = posx; src.y = posy; src.w = sx; src.h = sy;
- dst.x = dx; dst.y = dy; dst.w = sx; dst.h = sy;
-
- SDL_BlitSurface(tlist->GetSurface(), &src, mogltk::engine::base_o->getsurface(), &dst);
-
- if (locked)
- SDL_UnlockSurface(mogltk::engine::base_o->getsurface());
-}
-
-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;
-}
+/* + * mogltk + * Copyright (C) 1999-2004 Nicolas "Pixel" Noble + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +/* $Id: sprite.cc,v 1.15 2004-11-27 21:48:03 pixel Exp $ */ + +#include <SDL.h> +#include "engine.h" +#include "base.h" +#include "sprite.h" + +#define TEXSIZE 1024 + +mogltk::Sprite * mogltk::Sprite::Cursor; + +mogltk::Sprite::TexList * mogltk::Sprite::TexList::header = 0; + +mogltk::Sprite::Sprite(Handle * h, int asx, int asy) throw (GeneralException) : 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) { + printm(M_INFO, "Allocating a new texture for sprites\n"); + posx = posy = 0; + p = new TexList(TEXSIZE); + } + + tlist = p; +} + +mogltk::Sprite::TexList::TexList(int size) { + tex = new texture(size, size, true); + + if (header) + header->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(); +} + +SDL_Surface * mogltk::Sprite::TexList::GetSurface() { + return tex->GetSurface(); +} + + +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)); +} + +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; +} + +void mogltk::Sprite::draw(int dx, int dy, ColorP c, float sx, float sy) { + bool locked = false; + + if (SDL_MUSTLOCK(mogltk::engine::base_o->getsurface())) { + locked = true; + SDL_LockSurface(mogltk::engine::base_o->getsurface()); + } + + SDL_Rect src, dst; + + src.x = posx; src.y = posy; src.w = sx; src.h = sy; + dst.x = dx; dst.y = dy; dst.w = sx; dst.h = sy; + + SDL_BlitSurface(tlist->GetSurface(), &src, mogltk::engine::base_o->getsurface(), &dst); + + if (locked) + SDL_UnlockSurface(mogltk::engine::base_o->getsurface()); +} + +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 ea504ab..a06c345 100644 --- a/lib/texture.cc +++ b/lib/texture.cc @@ -1,361 +1,361 @@ -/*
- * mogltk
- * Copyright (C) 1999-2004 Nicolas "Pixel" Noble
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- */
-
-/* $Id: texture.cc,v 1.10 2004-11-27 21:44:53 pixel Exp $ */
-
-#include <sys/types.h>
-#include <SDL.h>
-#include <SDL_opengl.h>
-#include <generic.h>
-#include "texture.h"
-#include "engine.h"
-#ifdef HAVE_CONFIG_H
-#include "config.h"
-#endif
-#include "gettext.h"
-
-#define DEBUG 1
-
-mogltk::texture * mogltk::texture::header = 0;
-mogltk::texture * mogltk::texture::footer = 0;
-
-mogltk::texture * mogltk::texture::active = 0;
-
-mogltk::texture::texture(int w, int h, bool plane) throw (GeneralException) : width(w), height(h),
- texture_allocated(false), planar(plane), tainted(true), taintable(true) {
- if ((!ISPOT(w)) || (!ISPOT(h)))
- throw GeneralException(_("Size of the texture not a power of 2!"));
-
- if (!(surface = SDL_CreateRGBSurface(SDL_SWSURFACE, width, height, 32,
-#if SDL_BYTEORDER == SDL_BIG_ENDIAN
- 0xff000000,
- 0x00ff0000,
- 0x0000ff00,
- 0x000000ff
-#else
- 0x000000ff,
- 0x0000ff00,
- 0x00ff0000,
- 0xff000000
-#endif
- ))) {
- throw GeneralException(_("Can't create RGB Surface"));
- }
-
- SDL_FillRect(surface, 0, 0);
-
- next = 0;
- prev = footer;
- footer = this;
- if (!header) {
- header = this;
- }
- if (prev) {
- prev->next = this;
- }
-}
-
-mogltk::texture::texture(Handle * h, bool plane) throw (GeneralException) :
- texture_allocated(false), planar(plane), tainted(true), taintable(true) {
-
- SDL_Surface * temp;
-
- temp = LoadNTEX(h);
- width = temp->w;
- height = temp->h;
-
-#ifdef DEBUG
- printm(M_INFO, "Creating texture from file: size %ix%i\n", height, width);
-#endif
-
- if ((!ISPOT(width)) || (!ISPOT(height))) {
- SDL_FreeSurface(temp);
- throw GeneralException(_("Size of the texture not a power of 2!"));
- }
-
- SDL_PixelFormat f;
-
- f.palette = 0;
- f.BitsPerPixel = 32;
- f.BytesPerPixel = 4;
-#if SDL_BYTEORDER == SDL_BIG_ENDIAN
- f.Amask = 0x000000ff;
- f.Bmask = 0x0000ff00;
- f.Gmask = 0x00ff0000;
- f.Rmask = 0xff000000;
- f.Ashift = 0;
- f.Bshift = 8;
- f.Gshift = 16;
- f.Rshift = 24;
-#else
- f.Rmask = 0x000000ff;
- f.Gmask = 0x0000ff00;
- f.Bmask = 0x00ff0000;
- f.Amask = 0xff000000;
- f.Rshift = 0;
- f.Gshift = 8;
- f.Bshift = 16;
- f.Ashift = 24;
-#endif
- f.Rloss = 0;
- f.Gloss = 0;
- f.Bloss = 0;
- f.Aloss = 0;
-
- if (!(surface = SDL_ConvertSurface(temp, &f, 0))) {
- throw GeneralException("Could not convert texture to OpenGL format");
- }
-
- SDL_FreeSurface(temp);
-
- next = 0;
- prev = footer;
- footer = this;
- if (!header) {
- header = this;
- }
- if (prev) {
- prev->next = this;
- }
-}
-
-inline static unsigned int nextpower(unsigned int n) {
- unsigned int i;
-
- if (!n)
- return n;
-
- if (ISPOT(n))
- return n;
-
- for (i = 31; i >= 0; i--) {
- if ((n >> i) & 1) {
- return 1 << (i + 1);
- }
- }
-}
-
-mogltk::texture::texture(int x, int y, int w, int h) : width(nextpower(w)), height(nextpower(h)),
- texture_allocated(true), planar(false), tainted(false), taintable(false) {
- glGenTextures(1, &tex);
- glBindTexture(GL_TEXTURE_2D, tex);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
- 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);
- glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
- glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, x, y, w, h);
-}
-
-mogltk::texture::~texture() {
- if (surface) {
- SDL_FreeSurface(surface);
- }
-
- if (texture_allocated) {
- glDeleteTextures(1, &tex);
- }
-
- if (prev) {
- prev->next = next;
- }
-
- if (next) {
- next->prev = prev;
- }
-
- if (this == footer) {
- footer = prev;
- }
-
- if (this == header) {
- header = next;
- }
-}
-
-Uint32 * mogltk::texture::GetPixels() {
- if (surface)
- return (Uint32 *) surface->pixels;
- else
- return 0;
-}
-
-SDL_Surface * mogltk::texture::GetSurface() {
- return surface;
-}
-
-SDL_PixelFormat * mogltk::texture::GetFormat() {
- if (surface)
- return surface->format;
- else
- return 0;
-}
-
-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;
- glBindTexture(GL_TEXTURE_2D, tex);
- if (expand) {
- glMatrixMode(GL_TEXTURE);
- glLoadIdentity();
- glScaled(1 / (double) width, 1 / (double) height, 1);
- glMatrixMode(GL_MODELVIEW);
- }
-
- active = this;
-
- if (header == this)
- return;
-
- if (prev) {
- prev->next = next;
- }
-
- if (next) {
- next->prev = prev;
- }
-
- if (footer = this) {
- footer = prev;
- }
-
- next = header;
- prev = 0;
- header->prev = this;
- header = this;
-}
-
-GLuint mogltk::texture::GetWidth() {
- return width;
-}
-
-GLuint mogltk::texture::GetHeight() {
- return height;
-}
-
-void mogltk::texture::Unbind(void) {
- if (active) {
- glBindTexture(GL_TEXTURE_2D, 0);
- glDisable(GL_TEXTURE_2D);
- active = 0;
- }
-}
-
-void mogltk::texture::Taint(void) {
- if (taintable)
- tainted = true;
-}
-
-void mogltk::texture::Taintall(void) {
- if (header)
- header->recTaint();
-}
-
-void mogltk::texture::recTaint(void) {
- Taint();
- if (next)
- next->recTaint();
-}
-
-#ifdef WORDS_BIGENDIAN
-#define NTEX_SIGNATURE 0x4e544558
-#else
-#define NTEX_SIGNATURE 0x5845544e
-#endif
-
-SDL_Surface * mogltk::texture::LoadNTEX(Handle * h) throw (GeneralException) {
- SDL_Surface * r;
- char buffer[5];
- Uint16 height, width;
-
- h->read(buffer, 4);
- buffer[4] = 0;
- if (*((Uint32 *) buffer) != NTEX_SIGNATURE)
- throw GeneralException("Texture file " + h->GetName() + " corrupted");
-
- height = h->readU16();
- width = h->readU16();
-
- if (!(r = SDL_CreateRGBSurface(SDL_SWSURFACE, width, height, 32,
-#if SDL_BYTEORDER == SDL_BIG_ENDIAN
- 0xff000000,
- 0x00ff0000,
- 0x0000ff00,
- 0x000000ff
-#else
- 0x000000ff,
- 0x0000ff00,
- 0x00ff0000,
- 0xff000000
-#endif
- ))) {
- throw GeneralException(_("Can't create RGB Surface for LoadNTEX"));
- }
-
- h->read(r->pixels, height * width * 4);
-
- return r;
-}
-
-void mogltk::texture::DumpBMP(const String & n) {
- if (surface)
- SDL_SaveBMP(surface, n.to_charp());
-}
+/* + * mogltk + * Copyright (C) 1999-2004 Nicolas "Pixel" Noble + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +/* $Id: texture.cc,v 1.11 2004-11-27 21:48:03 pixel Exp $ */ + +#include <sys/types.h> +#include <SDL.h> +#include <SDL_opengl.h> +#include <generic.h> +#include "texture.h" +#include "engine.h" +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif +#include "gettext.h" + +#define DEBUG 1 + +mogltk::texture * mogltk::texture::header = 0; +mogltk::texture * mogltk::texture::footer = 0; + +mogltk::texture * mogltk::texture::active = 0; + +mogltk::texture::texture(int w, int h, bool plane) throw (GeneralException) : width(w), height(h), + texture_allocated(false), planar(plane), tainted(true), taintable(true) { + if ((!ISPOT(w)) || (!ISPOT(h))) + throw GeneralException(_("Size of the texture not a power of 2!")); + + if (!(surface = SDL_CreateRGBSurface(SDL_SWSURFACE, width, height, 32, +#if SDL_BYTEORDER == SDL_BIG_ENDIAN + 0xff000000, + 0x00ff0000, + 0x0000ff00, + 0x000000ff +#else + 0x000000ff, + 0x0000ff00, + 0x00ff0000, + 0xff000000 +#endif + ))) { + throw GeneralException(_("Can't create RGB Surface")); + } + + SDL_FillRect(surface, 0, 0); + + next = 0; + prev = footer; + footer = this; + if (!header) { + header = this; + } + if (prev) { + prev->next = this; + } +} + +mogltk::texture::texture(Handle * h, bool plane) throw (GeneralException) : + texture_allocated(false), planar(plane), tainted(true), taintable(true) { + + SDL_Surface * temp; + + temp = LoadNTEX(h); + width = temp->w; + height = temp->h; + +#ifdef DEBUG + printm(M_INFO, "Creating texture from file: size %ix%i\n", height, width); +#endif + + if ((!ISPOT(width)) || (!ISPOT(height))) { + SDL_FreeSurface(temp); + throw GeneralException(_("Size of the texture not a power of 2!")); + } + + SDL_PixelFormat f; + + f.palette = 0; + f.BitsPerPixel = 32; + f.BytesPerPixel = 4; +#if SDL_BYTEORDER == SDL_BIG_ENDIAN + f.Amask = 0x000000ff; + f.Bmask = 0x0000ff00; + f.Gmask = 0x00ff0000; + f.Rmask = 0xff000000; + f.Ashift = 0; + f.Bshift = 8; + f.Gshift = 16; + f.Rshift = 24; +#else + f.Rmask = 0x000000ff; + f.Gmask = 0x0000ff00; + f.Bmask = 0x00ff0000; + f.Amask = 0xff000000; + f.Rshift = 0; + f.Gshift = 8; + f.Bshift = 16; + f.Ashift = 24; +#endif + f.Rloss = 0; + f.Gloss = 0; + f.Bloss = 0; + f.Aloss = 0; + + if (!(surface = SDL_ConvertSurface(temp, &f, 0))) { + throw GeneralException("Could not convert texture to OpenGL format"); + } + + SDL_FreeSurface(temp); + + next = 0; + prev = footer; + footer = this; + if (!header) { + header = this; + } + if (prev) { + prev->next = this; + } +} + +inline static unsigned int nextpower(unsigned int n) { + unsigned int i; + + if (!n) + return n; + + if (ISPOT(n)) + return n; + + for (i = 31; i >= 0; i--) { + if ((n >> i) & 1) { + return 1 << (i + 1); + } + } +} + +mogltk::texture::texture(int x, int y, int w, int h) : width(nextpower(w)), height(nextpower(h)), + texture_allocated(true), planar(false), tainted(false), taintable(false) { + glGenTextures(1, &tex); + glBindTexture(GL_TEXTURE_2D, tex); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + 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); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL); + glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, x, y, w, h); +} + +mogltk::texture::~texture() { + if (surface) { + SDL_FreeSurface(surface); + } + + if (texture_allocated) { + glDeleteTextures(1, &tex); + } + + if (prev) { + prev->next = next; + } + + if (next) { + next->prev = prev; + } + + if (this == footer) { + footer = prev; + } + + if (this == header) { + header = next; + } +} + +Uint32 * mogltk::texture::GetPixels() { + if (surface) + return (Uint32 *) surface->pixels; + else + return 0; +} + +SDL_Surface * mogltk::texture::GetSurface() { + return surface; +} + +SDL_PixelFormat * mogltk::texture::GetFormat() { + if (surface) + return surface->format; + else + return 0; +} + +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; + glBindTexture(GL_TEXTURE_2D, tex); + if (expand) { + glMatrixMode(GL_TEXTURE); + glLoadIdentity(); + glScaled(1 / (double) width, 1 / (double) height, 1); + glMatrixMode(GL_MODELVIEW); + } + + active = this; + + if (header == this) + return; + + if (prev) { + prev->next = next; + } + + if (next) { + next->prev = prev; + } + + if (footer = this) { + footer = prev; + } + + next = header; + prev = 0; + header->prev = this; + header = this; +} + +GLuint mogltk::texture::GetWidth() { + return width; +} + +GLuint mogltk::texture::GetHeight() { + return height; +} + +void mogltk::texture::Unbind(void) { + if (active) { + glBindTexture(GL_TEXTURE_2D, 0); + glDisable(GL_TEXTURE_2D); + active = 0; + } +} + +void mogltk::texture::Taint(void) { + if (taintable) + tainted = true; +} + +void mogltk::texture::Taintall(void) { + if (header) + header->recTaint(); +} + +void mogltk::texture::recTaint(void) { + Taint(); + if (next) + next->recTaint(); +} + +#ifdef WORDS_BIGENDIAN +#define NTEX_SIGNATURE 0x4e544558 +#else +#define NTEX_SIGNATURE 0x5845544e +#endif + +SDL_Surface * mogltk::texture::LoadNTEX(Handle * h) throw (GeneralException) { + SDL_Surface * r; + char buffer[5]; + Uint16 height, width; + + h->read(buffer, 4); + buffer[4] = 0; + if (*((Uint32 *) buffer) != NTEX_SIGNATURE) + throw GeneralException("Texture file " + h->GetName() + " corrupted"); + + height = h->readU16(); + width = h->readU16(); + + if (!(r = SDL_CreateRGBSurface(SDL_SWSURFACE, width, height, 32, +#if SDL_BYTEORDER == SDL_BIG_ENDIAN + 0xff000000, + 0x00ff0000, + 0x0000ff00, + 0x000000ff +#else + 0x000000ff, + 0x0000ff00, + 0x00ff0000, + 0xff000000 +#endif + ))) { + throw GeneralException(_("Can't create RGB Surface for LoadNTEX")); + } + + h->read(r->pixels, height * width * 4); + + return r; +} + +void mogltk::texture::DumpBMP(const String & n) { + if (surface) + SDL_SaveBMP(surface, n.to_charp()); +} diff --git a/lib/widgets.cc b/lib/widgets.cc index c70ad95..55c709c 100644 --- a/lib/widgets.cc +++ b/lib/widgets.cc @@ -1,1182 +1,1182 @@ -/*
- * mogltk
- * Copyright (C) 1999-2004 Nicolas "Pixel" Noble
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- */
-
-/* $Id: widgets.cc,v 1.9 2004-10-19 01:27:30 pixel Exp $ */
-
-#include <SDL.h>
-#include <vector>
-#include <Input.h>
-#include "font.h"
-#include "engine.h"
-#include "widgets.h"
-#include "sprite.h"
-
-#ifdef HAVE_CONFIG_H
-#include "config.h"
-#endif
-
-#include "gettext.h"
-
-mogltk::widget * dragged_widget = 0;
-
-std::vector<mogltk::widget *> out_move, out_click;
-std::vector<mogltk::widget::timed_event> mogltk::widget::timed_events;
-
-
-class widget_mouse_event : public mogltk::engine::mouseevent {
- public:
- widget_mouse_event(mogltk::widget *);
- virtual void move(SDL_MouseMotionEvent);
- virtual void action(SDL_MouseButtonEvent);
- private:
- mogltk::widget * root;
- int mouse_x_down, mouse_y_down;
- bool mouse_down, mouse_drag;
- Uint32 old_click;
-};
-
-widget_mouse_event::widget_mouse_event(mogltk::widget * _root) : root(_root), mouse_down(false), mouse_drag(false) {
-}
-
-void widget_mouse_event::move(SDL_MouseMotionEvent m) {
- int mx, my;
- bool out_threshold;
- std::vector<mogltk::widget *>::iterator i;
- std::vector<mogltk::widget *> out_move_stack;
-
- mx = mogltk::engine::mouseX();
- my = mogltk::engine::mouseY();
-
- if (mouse_down) {
- int dx, dy;
-
- dx = ABS(mouse_x_down - mx);
- dy = ABS(mouse_y_down - my);
-
- out_threshold = (dx <= 1) && (dy <= 1);
- }
-
- if (mouse_down && (out_threshold || mouse_drag)) {
- if (!mouse_drag) {
- printm(M_INFO, "mouse_start_drag\n");
- // generate a mouse_start_drag event
- root->m_event(mouse_x_down, mouse_y_down, mogltk::E_MOUSE_START_DRAG);
- }
- if (dragged_widget) {
- printm(M_INFO, "mouse_drag_over");
- // generate a mouse_drag_over event
- dragged_widget->m_event(mx, my, mogltk::E_MOUSE_DRAG_OVER);
- }
-
- printm(M_INFO, "mouse_drag\n");
- // generate a mouse_drag event
- root->m_event(mx, my, mogltk::E_MOUSE_DRAG);
- mouse_drag = true;
- } else {
- printm(M_INFO, "mouse_move\n");
- root->m_event(mx, my, mogltk::E_MOUSE_MOVE);
- // generate a mouse_move event
- }
-
- for (i = out_move.begin(); i != out_move.end(); i++) {
- out_move_stack.push_back(*i);
- }
-
- for (i = out_move_stack.begin(); i != out_move_stack.end(); i++) {
- if (!(*i)->inside(mx, my)) {
- (*i)->m_event(mx, my, mogltk::E_MOUSE_OUT);
- // generate a mouse_out event
- }
- }
-
- out_move_stack.empty();
-}
-
-void widget_mouse_event::action(SDL_MouseButtonEvent b) {
- int mx, my;
- std::vector<mogltk::widget *>::iterator i;
- std::vector<mogltk::widget *> out_click_stack;
-
- mx = mogltk::engine::mouseX();
- my = mogltk::engine::mouseY();
-
- if (b.button == 1) {
- if (b.state) {
- mouse_x_down = mx;
- mouse_y_down = my;
- mouse_down = true;
- printm(M_INFO, "mouse_down\n");
- root->m_event(mx, my, mogltk::E_MOUSE_DOWN);
- // generate a mouse_down event;
- for (i = out_click.begin(); i != out_click.end(); i++) {
- out_click_stack.push_back(*i);
- }
-
- for (i = out_click_stack.begin(); i != out_click_stack.end(); i++) {
- if (!(*i)->inside(mx, my)) {
- (*i)->m_event(mx, my, mogltk::E_MOUSE_OUT_CLICK);
- // generate a mouse_out_click event
- }
- }
-
- out_click_stack.empty();
-
- } else {
- mouse_down = false;
- if (mouse_drag) {
- mouse_drag = false;
- printm(M_INFO, "mouse_end_drag\n");
- root->m_event(mx, my, mogltk::E_MOUSE_END_DRAG);
- // generate a mouse_end_drag event
- if (dragged_widget) {
- printm(M_INFO, "mouse_end_drag_over");
- dragged_widget->m_event(mx, my, mogltk::E_MOUSE_END_DRAG_OVER);
- // generate a mouse_end_drag_over event
- dragged_widget = 0;
- }
- } else {
- printm(M_INFO, "mouse_click\n");
- root->m_event(mx, my, mogltk::E_MOUSE_CLICK);
- // generate a mouse_click event
-
- for (i = out_click.begin(); i != out_click.end(); i++) {
- out_click_stack.push_back(*i);
- }
-
- for (i = out_click_stack.begin(); i != out_click_stack.end(); i++) {
- if (!(*i)->inside(mx, my)) {
- (*i)->m_event(mx, my, mogltk::E_MOUSE_OUT_CLICK);
- // generate a mouse_out_click event
- }
- }
-
- out_click_stack.empty();
-
- if ((SDL_GetTicks() - old_click) < 500) {
- printm(M_INFO, "mouse_dbl_click\n");
- root->m_event(mx, my, mogltk::E_MOUSE_DBL_CLICK);
- // generate a mouse_dbl_click event
- }
-
- old_click = SDL_GetTicks();
- }
- printm(M_INFO, "mouse_up\n");
- root->m_event(mx, my, mogltk::E_MOUSE_UP);
- // generate a mouse_up event.
- }
- }
-}
-
-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), prev(0), child(0), last(0), panel(0), type(_type), name(_name), sh(_sh), exclusive(0), visible(true), enabled(true) {
- 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;
- if (!father->last)
- father->last = this;
- father->child = this;
- root = father->root;
- }
-
- computeabs();
-}
-
-mogltk::widget::~widget() {
- while(child)
- delete child;
-
- if (prev)
- prev->next = next;
- else
- father->child = next;
-
- if (next)
- next->prev = prev;
- else
- father->last = 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();
-
- if (child)
- child->icomputeabs();
-}
-
-void mogltk::widget::resize(int nsx, int nsy) {
- sx = nsx;
- sy = nsy;
-
- ax2 = ax + sx;
- ay2 = ay + sy;
-
- if (child)
- child->iresize_notify();
-}
-
-int mogltk::widget::GetX() {
- return x;
-}
-
-int mogltk::widget::GetY() {
- return y;
-}
-
-int mogltk::widget::GetH() {
- return sy;
-}
-
-int mogltk::widget::GetW() {
- return sx;
-}
-
-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::rect mogltk::widget::GetDrawRect() {
- rect r;
- r.x = 0;
- r.y = 0;
- r.w = GetW();
- r.h = GetH();
-
- return r;
-}
-
-mogltk::widget * mogltk::widget::Father() {
- return father;
-}
-
-mogltk::widget * mogltk::widget::Child() {
- return child;
-}
-
-mogltk::widget * mogltk::widget::Next() {
- return next;
-}
-
-mogltk::widget * mogltk::widget::Prev() {
- return prev;
-}
-
-mogltk::widget * mogltk::widget::InnerPanel() {
- if (!panel)
- panel = create_panel();
- return panel;
-}
-
-mogltk::widget * mogltk::widget::find_widget(int _x, int _y) {
- widget * r = 0;
-
- if (visible && enabled && (_x >= ax) && (_y >= ay) && (_x <= ax2) && (_y <= ay2)) {
- if (child)
- r = child->find_widget(_x, _y);
- if (!r)
- r = this;
- }
-
- if (!r && next) {
- r = next->find_widget(_x, _y);
- }
-
- return r;
-}
-
-mogltk::shape * mogltk::widget::Shaper() {
- return sh;
-}
-
-void mogltk::widget::fulldraw() {
- bool was2D = true;
-
- if (mogltk::engine::glbase_o)
- 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();
-
- if (!visible)
- return;
-
- 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();
-}
-
-bool mogltk::widget::inside(int xe, int ye) {
- if (!visible)
- return false;
- return !((xe < ax) || (xe > ax2) || (ye < ay) || (ye > ay2));
-}
-
-void mogltk::widget::m_event(int x, int y, mogltk::event_t event) {
- switch (event) {
- case E_MOUSE_DRAG_OVER:
- case E_MOUSE_END_DRAG_OVER:
- case E_MOUSE_OUT:
- case E_MOUSE_OUT_CLICK:
- process_event(x, y, event);
- break;
- default:
- ievent(x, y, event);
- }
-}
-
-bool mogltk::widget::ievent(int xe, int ye, mogltk::event_t event) {
- if (prev)
- if (prev->ievent(xe, ye, event))
- return true;
-
- if (!inside(xe, ye))
- return false;
-
- if (exclusive)
- return exclusive->ievent(xe, ye, event);
-
- if (last)
- if (last->ievent(xe, ye, event))
- return true;
-
- if (!enabled || !visible)
- return false;
-
- return process_event(xe, ye, event);
-}
-
-void mogltk::widget::draw() {
-}
-
-bool mogltk::widget::process_event(int, int, mogltk::event_t) {
- return false;
-}
-
-void mogltk::widget::resize_notify() {
-}
-
-mogltk::widget * mogltk::widget::create_panel() {
- mogltk::rect r = GetDrawRect();
- return new widgets::Panel(Shaper(), this, r.x, r.y, r.w, r.h);
-}
-
-void mogltk::widget::icomputeabs() {
- computeabs();
-
- if (next)
- next->computeabs();
-
- if (child)
- child->computeabs();
-}
-
-void mogltk::widget::iresize_notify() {
- if (next)
- next->iresize_notify();
- if (child)
- child->iresize_notify();
- resize_notify();
-}
-
-void mogltk::widget::set_exclusive(mogltk::widget * w) {
- w->exclusive = this;
-}
-
-void mogltk::widget::unset_exclusive(mogltk::widget * w) {
- w->exclusive = 0;
-}
-
-bool mogltk::widget::GetVisible() {
- return visible;
-}
-
-void mogltk::widget::SetVisible(bool _visible) {
- visible = _visible;
-}
-
-bool mogltk::widget::GetEnabled() {
- return enabled;
-}
-
-void mogltk::widget::SetEnabled(bool _enabled) {
- enabled = _enabled;
-}
-
-void mogltk::widget::MoveOnTop() {
- if (!prev)
- return;
-
- if (!next) {
- father->last = prev;
- } else {
- next->prev = prev;
- }
- prev->next = next;
- prev = 0;
- next = father->child;
-
- next->prev = this;
- father->child = this;
-}
-
-void mogltk::widget::check_timed_events() {
- std::vector<timed_event>::iterator i;
- Uint32 t = SDL_GetTicks();
- widget * w;
-
- for (i = timed_events.begin(); i != timed_events.end(); i++) {
- if (i->t <= t) {
- w = i->w;
- timed_events.erase(i);
- w->process_event(engine::mouseX(), engine::mouseY(), E_TIMER);
- i = timed_events.begin();
- if (i == timed_events.end())
- return ;
- }
- }
-}
-
-void mogltk::widget::add_out_move() {
- std::vector<widget *>::iterator i;
-
- for (i = out_move.begin(); i != out_move.end(); i++) {
- if (*i == this)
- return;
- }
-
- out_move.push_back(this);
-}
-
-void mogltk::widget::remove_out_move() {
- std::vector<widget *>::iterator i;
-
- for (i = out_move.begin(); i != out_move.end(); i++) {
- if (*i == this) {
- out_move.erase(i);
- return;
- }
- }
-}
-
-void mogltk::widget::add_out_click() {
- std::vector<widget *>::iterator i;
-
- for (i = out_click.begin(); i != out_click.end(); i++) {
- if (*i == this)
- return;
- }
-
- out_click.push_back(this);
-}
-
-void mogltk::widget::remove_out_click() {
- std::vector<widget *>::iterator i;
-
- for (i = out_click.begin(); i != out_click.end(); i++) {
- if (*i == this) {
- out_click.erase(i);
- return;
- }
- }
-}
-
-void mogltk::widget::set_timed_event(Uint32 t) {
- timed_events.push_back(timed_event(this, SDL_GetTicks() + t));
-}
-
-void mogltk::widget::set_absolute_timed_event(Uint32 t) {
- timed_events.push_back(timed_event(this, t));
-}
-
-
-/*
- * Predefined widgets.
- */
-
-/***************
-* Here is Root *
-***************/
-
-mogltk::widgets::Root::Root(mogltk::shape * sh, mogltk::widgets::drawer * _dr) : widget(0, 0, 0, 0, 0, 0, "Root", sh), dr(_dr) {
- if (engine::root)
- delete engine::root;
- engine::root = this;
-}
-
-void mogltk::widgets::Root::draw() {
- if (dr)
- dr->draw(this);
- else
- Shaper()->box(GetAX(), GetAY(), GetAX2(), GetAY2());
-}
-
-void mogltk::widgets::Root::setdrawer(drawer * _dr) {
- dr = _dr;
-}
-
-
-/**************************
-* A simple clipping Panel *
-**************************/
-
-mogltk::widgets::Panel::Panel(shape * sh, widget * father, int x, int y, int w, int h) : widget(father, x, y, w, h, 0, "Panel", sh) {
-}
-
-
-/***********************
-* The classical Button *
-***********************/
-
-mogltk::widgets::Button::Button(action * _a, shape * sh, widget * father, int x, int y, int w, int h, const String & _caption) : widget(father, x, y, w, h, 0, "Button", sh), bevel(false), dragging(false), a(_a) {
- caption = _caption;
-}
-
-void mogltk::widgets::Button::draw() {
- Shaper()->button(GetAX() + 1, GetAY() + 1, GetAX2() - 1, GetAY2() - 1, caption, bevel);
-}
-
-bool mogltk::widgets::Button::process_event(int xe, int ye, mogltk::event_t event) {
- switch (event) {
- case E_MOUSE_DOWN:
- bevel = true;
- return true;
- case E_MOUSE_CLICK:
- bevel = false;
- // action here.
- if (a)
- a->do_action(this);
- return true;
- case E_MOUSE_START_DRAG:
- dragged_widget = this;
- dragging = true;
- return true;
- case E_MOUSE_DRAG_OVER:
- bevel = inside(xe, ye);
- return true;
- case E_MOUSE_END_DRAG_OVER:
- dragging = false;
- if (bevel)
- // action here.
- if (a)
- a->do_action(this);
- bevel = false;
- return true;
- }
- return false;
-}
-
-
-/**********************
-* The SmartBox window *
-**********************/
-
-mogltk::widgets::SmartBox::SmartBox(shape * sh, mogltk::widget * father, int x, int y, int w, int h, const String & _caption) : widget(father, x, y, w, h, 0, "SmartBox", sh) {
- caption = _caption;
-}
-
-void mogltk::widgets::SmartBox::draw() {
- Shaper()->window(GetAX(), GetAY(), GetAX2(), GetAY2(), caption);
-}
-
-bool mogltk::widgets::SmartBox::process_event(int x, int y, mogltk::event_t event) {
- switch (event) {
- case E_MOUSE_START_DRAG:
- if ((GetAY() + 20) > y) {
- dragged_widget = this;
- ox = x;
- oy = y;
- oax = GetX();
- oay = GetY();
- return true;
- }
- break;
- case E_MOUSE_DRAG_OVER:
- move(x - ox + oax, y - oy + oay);
- MoveOnTop();
- return true;
- }
- return false;
-}
-
-mogltk::rect mogltk::widgets::SmartBox::GetDrawRect() {
- rect r;
-
- r.x = 2;
- r.y = 21;
- r.w = GetW() - 4;
- r.h = GetH() - 23;
-
- return r;
-}
-
-
-/***********************
-* The MessageBox child *
-***********************/
-
-class MessageBoxAction : public mogltk::widgets::action {
- public:
- MessageBoxAction(mogltk::widget * _parent) : parent(_parent) { }
- virtual void do_action(mogltk::widget *) {
- delete parent;
- delete this;
- }
- private:
- mogltk::widget * parent;
-};
-
-mogltk::widgets::MsgBox::MsgBox(shape * sh, mogltk::widget * father, const String & caption, const String & text) : SmartBox(sh, father, 0, 0, 0, 0, caption) {
- rect size = SystemFont->size(text);
- rect lsize = size;
-
- size.w += 12;
- size.h += 60;
-
- size.x = (father->GetW() - size.w) / 2;
- size.y = (father->GetH() - size.h) / 2;
- resize(size.w, size.h);
- move(size.x, size.y);
-
- new Button(new MessageBoxAction(this), sh, this, size.w / 2 - 25, size.h - 30, 50, 24, "Ok");
- new Label(sh, this, 6, 24, lsize.w, lsize.h, text, BLACK);
-
- set_exclusive(father);
-}
-
-mogltk::widgets::MsgBox::~MsgBox() {
- unset_exclusive(Father());
-}
-
-
-/*****************
-* A simple Label *
-*****************/
-
-mogltk::widgets::Label::Label(shape * sh, mogltk::widget * father, int x, int y, int w, int h, const String & _caption, const ColorP & _color) : widget(father, x, y, w, h, 0, "Label", sh), color(_color) {
- caption = _caption;
-}
-
-void mogltk::widgets::Label::draw() {
- Shaper()->text(GetAX(), GetAY(), caption, color);
-}
-
-
-/*************************
-* The classical 3D Frame *
-*************************/
-
-mogltk::widgets::Frame::Frame(shape * sh, widget * father, int x, int y, int w, int h) : widget(father, x, y, w, h, 0, "Frame", sh) {
-}
-
-void mogltk::widgets::Frame::draw() {
- Shaper()->obox3d(GetAX(), GetAY(), GetAX2(), GetAY2());
-}
-
-mogltk::rect mogltk::widgets::Frame::GetDrawRect() {
- rect r;
-
- r.x = 4;
- r.y = 4;
- r.w = GetW() - 8;
- r.h = GetH() - 8;
-
- return r;
-}
-
-
-/***********************************
-* The huge and bloated ContextMenu *
-***********************************/
-
-mogltk::widgets::ContextMenu::node::node(const String & _caption, int _x, int _y, int _w, int _h, mogltk::widgets::action * _a, mogltk::widgets::ContextMenu * _sub, mogltk::widgets::ContextMenu * _father, bool _line) : caption(_caption), x(_x), y(_y), w(_w), h(_h), a(_a), sub(_sub), father(_father), line(_line), enabled(true) {
-}
-
-String mogltk::widgets::ContextMenu::node::GetCaption() {
- return caption;
-}
-
-mogltk::widgets::action * mogltk::widgets::ContextMenu::node::GetAction() {
- return a;
-}
-
-mogltk::widgets::ContextMenu * mogltk::widgets::ContextMenu::node::GetFather() {
- return father;
-}
-
-mogltk::widgets::ContextMenu * mogltk::widgets::ContextMenu::node::GetSub() {
- return sub;
-}
-
-bool mogltk::widgets::ContextMenu::node::GetLine() {
- return line;
-}
-
-bool mogltk::widgets::ContextMenu::node::GetEnabled() {
- return enabled;
-}
-
-void mogltk::widgets::ContextMenu::node::SetEnabled(bool _enabled) {
- enabled = _enabled;
-}
-
-int mogltk::widgets::ContextMenu::node::GetX() {
- return x;
-}
-
-int mogltk::widgets::ContextMenu::node::GetY() {
- return y;
-}
-
-int mogltk::widgets::ContextMenu::node::GetW() {
- return w;
-}
-
-int mogltk::widgets::ContextMenu::node::GetH() {
- return h;
-}
-
-mogltk::widgets::ContextMenu::ContextMenu(shape * sh, mogltk::widget * father, int x, int y) : widget(father, x, y, 8, 4, 1, "ContextMenu", sh), selected(-1), subselected(0), in_click(false), sticky(false) {
-}
-
-mogltk::widgets::ContextMenu::~ContextMenu() {
- std::vector<node>::iterator i;
- ContextMenu * sub;
-
- for (i = nodes.begin(); i != nodes.end(); i++) {
- if ((sub = i->GetSub()))
- delete sub;
- }
-}
-
-void mogltk::widgets::ContextMenu::addnode(const String & caption, mogltk::widgets::action * a) {
- rect size;
-
- size = SystemFont->size(caption);
- nodes.push_back(node(caption, 4, GetH() - 2, size.w, size.h, a, 0, this, false));
- size.w += 8;
- if (GetW() > size.w)
- size.w = GetW();
- resize(size.w, GetH() + size.h);
-}
-
-void mogltk::widgets::ContextMenu::addsub(const String & caption, mogltk::widgets::ContextMenu * sub) {
- rect size;
-
- size = SystemFont->size(caption + " >");
- nodes.push_back(node(caption + " >", 4, GetH() - 2, size.w, size.h, 0, sub, this, false));
- size.w += 8;
- if (GetW() > size.w)
- size.w = GetW();
- resize(size.w, GetH() + size.h);
-}
-
-void mogltk::widgets::ContextMenu::addline() {
- nodes.push_back(node("", 2, GetH() - 1, 0, 0, 0, 0, this, true));
- resize(GetW(), GetH() + 4);
-}
-
-mogltk::widgets::ContextMenu * mogltk::widgets::ContextMenu::createsub(const String & caption) {
- ContextMenu * r;
-
- r = new ContextMenu(Shaper(), Father(), GetX() + GetW(), GetY() + GetH());
- r->SetVisible(false);
-
- addsub(caption, r);
-
- return r;
-}
-
-void mogltk::widgets::ContextMenu::move(int x, int y) {
- int dx, dy;
- std::vector<node>::iterator i;
- ContextMenu * sub;
-
- dx = x - GetX();
- dy = y - GetY();
- widget::move(x, y);
-
- for (i = nodes.begin(); i != nodes.end(); i++) {
- if ((sub = i->GetSub()))
- sub->move(sub->GetX() + dx, sub->GetY() + dy);
- }
-}
-
-void mogltk::widgets::ContextMenu::resize(int w, int h) {
- int dw;
- std::vector<node>::iterator i;
- ContextMenu * sub;
-
- dw = w - GetW();
- widget::resize(w, h);
-
- for (i = nodes.begin(); i != nodes.end(); i++) {
- if ((sub = i->GetSub()))
- sub->move(sub->GetX() + dw, sub->GetY());
- }
-}
-
-void mogltk::widgets::ContextMenu::SetVisible(bool visible) {
- if (!visible && sticky)
- return;
- widget::SetVisible(visible);
- if (!visible && subselected) {
- subselected->SetVisible(false);
- }
-
- if (visible) {
- add_out_click();
- MoveOnTop();
- }
-}
-
-void mogltk::widgets::ContextMenu::SetEnabled(int i, bool e) {
- nodes[i].SetEnabled(e);
-}
-
-void mogltk::widgets::ContextMenu::StickyDisplay() {
- sticky = true;
- SetVisible(true);
-}
-
-void mogltk::widgets::ContextMenu::draw() {
- std::vector<node>::iterator i;
- int n;
- rect size;
-
- Shaper()->box3d(GetAX(), GetAY(), GetAX2(), GetAY2());
-
- for (i = nodes.begin(), n = 0; i != nodes.end(); i++, n++) {
- size = SystemFont->size(i->GetCaption());
- if (i->GetLine()) {
- Shaper()->hline3d(GetAX() + i->GetX(), GetAX() + i->GetX() + GetW() - 4, GetAY() + i->GetY());
- } else {
- if (n == selected) {
- Shaper()->box(GetAX() + 2, GetAY() + i->GetY(), GetAX() + GetW() - 2, GetAY() + i->GetY() + i->GetH(), DOS_MAGENTA);
- }
- Shaper()->text(GetAX() + i->GetX(), GetAY() + i->GetY(), i->GetCaption(), i->GetEnabled() ? BLACK : DOS_GRAY);
- }
- }
-}
-
-bool mogltk::widgets::ContextMenu::process_event(int xe, int ye, mogltk::event_t event) {
- std::vector<node>::iterator i;
- action * a;
- int n;
-
- switch (event) {
- case E_MOUSE_MOVE:
- sticky = false;
- selected = -1;
- for (i = nodes.begin(), n = 0; i != nodes.end(); i++, n++) {
- if (!i->GetLine()) {
- int ax, ax2, ay, ay2;
-
- ax = GetAX() + 2;
- ay = GetAY() + i->GetY();
- ax2 = GetAX() + GetW() - 2;
- ay2 = GetAY() + i->GetY() + i->GetH();
-
- if (!((xe < ax) || (xe > ax2) || (ye < ay) || (ye > ay2))) {
- selected = n;
- if (subselected) {
- subselected->SetVisible(false);
- subselected->selected = -1;
- subselected->subselected = 0;
- }
- if ((subselected = i->GetSub())) {
- subselected->SetVisible(true);
- subselected->selected = -1;
- subselected->subselected = 0;
- }
- }
- }
- }
- add_out_move();
- return true;
- case E_MOUSE_CLICK:
- a = 0;
- if (selected != -1) {
- if ((a = nodes[selected].GetAction()) && nodes[selected].GetEnabled()) {
- a->do_action(this);
- in_click = false;
- SetVisible(false);
- return true;
- }
- }
- in_click = true;
- break;
- case E_MOUSE_DOWN:
- in_click = true;
- break;
- case E_MOUSE_OUT:
- selected = -1;
- remove_out_move();
- in_click = false;
- return true;
- case E_MOUSE_OUT_CLICK:
- if (iin_click())
- return true;
- selected = -1;
- subselected = 0;
- SetVisible(false);
- return true;
- }
-
- return false;
-}
-
-bool mogltk::widgets::ContextMenu::iin_click() {
- if (in_click)
- return true;
-
- if (subselected)
- return subselected->iin_click();
-
- return false;
-}
-
-
-/********************
-* The Menu topframe *
-********************/
-
-mogltk::widgets::Menu::node::node(const String & _caption, mogltk::widgets::ContextMenu * _sub, mogltk::widgets::action * _a, int _x) : caption(_caption), sub(_sub), a(_a), x(_x) {
-}
-
-String mogltk::widgets::Menu::node::GetCaption() {
- return caption;
-}
-
-void mogltk::widgets::Menu::node::SetCaption(const String & s) {
- caption = s;
-}
-
-mogltk::widgets::ContextMenu * mogltk::widgets::Menu::node::GetSub() {
- return sub;
-}
-
-mogltk::widgets::action * mogltk::widgets::Menu::node::GetAction() {
- return a;
-}
-
-int mogltk::widgets::Menu::node::GetX() {
- return x;
-}
-
-bool mogltk::widgets::Menu::node::GetEnabled() {
- return enabled;
-}
-
-void mogltk::widgets::Menu::node::SetEnabled(bool _enabled) {
- enabled = _enabled;
-}
-
-mogltk::widgets::Menu::Menu(shape * sh, mogltk::widget * father) : widget(father, 0, 0, 0, 0, 0, "Menu", sh), cur_x(0), selected(-1) {
- rect r = father->GetDrawRect();
- move(r.x, r.y);
- resize(r.w, 16);
-}
-
-void mogltk::widgets::Menu::resize_notify() {
- rect r = Father()->GetDrawRect();
- resize(r.w, 16);
-}
-
-void mogltk::widgets::Menu::addnode(const String & caption, mogltk::widgets::action * a) {
- nodes.push_back(node(caption, 0, a, cur_x));
- cur_x += SystemFont->singletextsize(caption) + 6;
-}
-
-void mogltk::widgets::Menu::addsub(const String & caption, mogltk::widgets::ContextMenu * sub) {
- nodes.push_back(node(caption, sub, 0, cur_x));
- cur_x += SystemFont->singletextsize(caption) + 6;
-}
-
-mogltk::widgets::ContextMenu * mogltk::widgets::Menu::createsub(const String & caption) {
- ContextMenu * r = new ContextMenu(Shaper(), Father(), GetX() + cur_x, GetY() + 16);
- addsub(caption, r);
- r->SetVisible(false);
- return r;
-}
-
-void mogltk::widgets::Menu::SetEnabled(int i, bool e) {
- nodes[i].SetEnabled(e);
-}
-
-void mogltk::widgets::Menu::SetCaption(int i, const String & s) {
- nodes[i].SetCaption(s);
-}
-
-void mogltk::widgets::Menu::draw(void) {
- std::vector<node>::iterator i;
- int n;
-
- Shaper()->box(GetAX(), GetAY(), GetAX2(), GetAY2(), DOS_WHITE);
-
- for (i = nodes.begin(), n = 0; i != nodes.end(); i++, n++) {
- if (selected == n) {
- if ((i + 1) == nodes.end()) {
- Shaper()->box(GetAX() + i->GetX(), GetAY(), cur_x, GetAY2(), DOS_MAGENTA);
- } else {
- Shaper()->box(GetAX() + i->GetX(), GetAY(), GetAX() + i[1].GetX(), GetAY2(), DOS_MAGENTA);
- }
- }
- Shaper()->text(GetAX() + i->GetX() + 3, GetAY(), i->GetCaption(), BLACK);
- }
-}
-
-bool mogltk::widgets::Menu::process_event(int mx, int my, mogltk::event_t event) {
- std::vector<node>::iterator i;
- action * a;
- ContextMenu * sub;
- int n;
-
- switch (event) {
- case E_MOUSE_MOVE:
- selected = -1;
- for (i = nodes.begin(), n = 0; i != nodes.end(); i++, n++) {
- int ax, ax2, ay, ay2;
-
- ax = GetAX() + i->GetX();
- ay = GetAY();
- if ((i + 1) != nodes.end()) {
- ax2 = GetAX() + i[1].GetX();
- } else {
- ax2 = GetAX() + cur_x;
- }
- ay2 = GetAY2();
-
- if (!((mx < ax) || (mx > ax2) || (my < ay) || (my > ay2))) {
- selected = n;
- }
- }
- add_out_move();
- return true;
- case E_MOUSE_CLICK:
- a = 0;
- if (selected != -1) {
- if ((a = nodes[selected].GetAction()) && nodes[selected].GetEnabled()) {
- a->do_action(this);
- return true;
- } else if ((sub = nodes[selected].GetSub()) && nodes[selected].GetEnabled()) {
- sub->StickyDisplay();
- return true;
- }
- }
- break;
- case E_MOUSE_OUT:
- selected = -1;
- remove_out_move();
- return true;
- }
- return false;
-}
-
-
-/*
- * The mainloop widget thing.
- */
-
-void mogltk::widget::mainloop() {
- bool end_event_loop = false;
- widget_mouse_event * mouse = new widget_mouse_event(this);
- widget * w;
- int mx, my;
-
- while (!end_event_loop && !engine::quitrequested()) {
- root->fulldraw();
- mx = mogltk::engine::mouseX();
- my = mogltk::engine::mouseY();
-
- mogltk::ColorP::Max.A = 50;
- Sprite::Cursor->draw(mx - 6, my - 3, BLACK);
- mogltk::ColorP::Max.A = 255;
- Sprite::Cursor->draw(mx - 8, my - 6);
-
- if (w = root->find_widget(mx, my)) {
- sh->box(w->GetAX(), w->GetAY(), w->GetAX2(), w->GetAY2(), Color(100, 150, 60, 50));
- }
-
- engine::base_o->Flip();
- widget::check_timed_events();
- }
-
- delete mouse;
-}
+/* + * mogltk + * Copyright (C) 1999-2004 Nicolas "Pixel" Noble + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +/* $Id: widgets.cc,v 1.10 2004-11-27 21:48:03 pixel Exp $ */ + +#include <SDL.h> +#include <vector> +#include <Input.h> +#include "font.h" +#include "engine.h" +#include "widgets.h" +#include "sprite.h" + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "gettext.h" + +mogltk::widget * dragged_widget = 0; + +std::vector<mogltk::widget *> out_move, out_click; +std::vector<mogltk::widget::timed_event> mogltk::widget::timed_events; + + +class widget_mouse_event : public mogltk::engine::mouseevent { + public: + widget_mouse_event(mogltk::widget *); + virtual void move(SDL_MouseMotionEvent); + virtual void action(SDL_MouseButtonEvent); + private: + mogltk::widget * root; + int mouse_x_down, mouse_y_down; + bool mouse_down, mouse_drag; + Uint32 old_click; +}; + +widget_mouse_event::widget_mouse_event(mogltk::widget * _root) : root(_root), mouse_down(false), mouse_drag(false) { +} + +void widget_mouse_event::move(SDL_MouseMotionEvent m) { + int mx, my; + bool out_threshold; + std::vector<mogltk::widget *>::iterator i; + std::vector<mogltk::widget *> out_move_stack; + + mx = mogltk::engine::mouseX(); + my = mogltk::engine::mouseY(); + + if (mouse_down) { + int dx, dy; + + dx = ABS(mouse_x_down - mx); + dy = ABS(mouse_y_down - my); + + out_threshold = (dx <= 1) && (dy <= 1); + } + + if (mouse_down && (out_threshold || mouse_drag)) { + if (!mouse_drag) { + printm(M_INFO, "mouse_start_drag\n"); + // generate a mouse_start_drag event + root->m_event(mouse_x_down, mouse_y_down, mogltk::E_MOUSE_START_DRAG); + } + if (dragged_widget) { + printm(M_INFO, "mouse_drag_over"); + // generate a mouse_drag_over event + dragged_widget->m_event(mx, my, mogltk::E_MOUSE_DRAG_OVER); + } + + printm(M_INFO, "mouse_drag\n"); + // generate a mouse_drag event + root->m_event(mx, my, mogltk::E_MOUSE_DRAG); + mouse_drag = true; + } else { + printm(M_INFO, "mouse_move\n"); + root->m_event(mx, my, mogltk::E_MOUSE_MOVE); + // generate a mouse_move event + } + + for (i = out_move.begin(); i != out_move.end(); i++) { + out_move_stack.push_back(*i); + } + + for (i = out_move_stack.begin(); i != out_move_stack.end(); i++) { + if (!(*i)->inside(mx, my)) { + (*i)->m_event(mx, my, mogltk::E_MOUSE_OUT); + // generate a mouse_out event + } + } + + out_move_stack.empty(); +} + +void widget_mouse_event::action(SDL_MouseButtonEvent b) { + int mx, my; + std::vector<mogltk::widget *>::iterator i; + std::vector<mogltk::widget *> out_click_stack; + + mx = mogltk::engine::mouseX(); + my = mogltk::engine::mouseY(); + + if (b.button == 1) { + if (b.state) { + mouse_x_down = mx; + mouse_y_down = my; + mouse_down = true; + printm(M_INFO, "mouse_down\n"); + root->m_event(mx, my, mogltk::E_MOUSE_DOWN); + // generate a mouse_down event; + for (i = out_click.begin(); i != out_click.end(); i++) { + out_click_stack.push_back(*i); + } + + for (i = out_click_stack.begin(); i != out_click_stack.end(); i++) { + if (!(*i)->inside(mx, my)) { + (*i)->m_event(mx, my, mogltk::E_MOUSE_OUT_CLICK); + // generate a mouse_out_click event + } + } + + out_click_stack.empty(); + + } else { + mouse_down = false; + if (mouse_drag) { + mouse_drag = false; + printm(M_INFO, "mouse_end_drag\n"); + root->m_event(mx, my, mogltk::E_MOUSE_END_DRAG); + // generate a mouse_end_drag event + if (dragged_widget) { + printm(M_INFO, "mouse_end_drag_over"); + dragged_widget->m_event(mx, my, mogltk::E_MOUSE_END_DRAG_OVER); + // generate a mouse_end_drag_over event + dragged_widget = 0; + } + } else { + printm(M_INFO, "mouse_click\n"); + root->m_event(mx, my, mogltk::E_MOUSE_CLICK); + // generate a mouse_click event + + for (i = out_click.begin(); i != out_click.end(); i++) { + out_click_stack.push_back(*i); + } + + for (i = out_click_stack.begin(); i != out_click_stack.end(); i++) { + if (!(*i)->inside(mx, my)) { + (*i)->m_event(mx, my, mogltk::E_MOUSE_OUT_CLICK); + // generate a mouse_out_click event + } + } + + out_click_stack.empty(); + + if ((SDL_GetTicks() - old_click) < 500) { + printm(M_INFO, "mouse_dbl_click\n"); + root->m_event(mx, my, mogltk::E_MOUSE_DBL_CLICK); + // generate a mouse_dbl_click event + } + + old_click = SDL_GetTicks(); + } + printm(M_INFO, "mouse_up\n"); + root->m_event(mx, my, mogltk::E_MOUSE_UP); + // generate a mouse_up event. + } + } +} + +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), prev(0), child(0), last(0), panel(0), type(_type), name(_name), sh(_sh), exclusive(0), visible(true), enabled(true) { + 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; + if (!father->last) + father->last = this; + father->child = this; + root = father->root; + } + + computeabs(); +} + +mogltk::widget::~widget() { + while(child) + delete child; + + if (prev) + prev->next = next; + else + father->child = next; + + if (next) + next->prev = prev; + else + father->last = 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(); + + if (child) + child->icomputeabs(); +} + +void mogltk::widget::resize(int nsx, int nsy) { + sx = nsx; + sy = nsy; + + ax2 = ax + sx; + ay2 = ay + sy; + + if (child) + child->iresize_notify(); +} + +int mogltk::widget::GetX() { + return x; +} + +int mogltk::widget::GetY() { + return y; +} + +int mogltk::widget::GetH() { + return sy; +} + +int mogltk::widget::GetW() { + return sx; +} + +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::rect mogltk::widget::GetDrawRect() { + rect r; + r.x = 0; + r.y = 0; + r.w = GetW(); + r.h = GetH(); + + return r; +} + +mogltk::widget * mogltk::widget::Father() { + return father; +} + +mogltk::widget * mogltk::widget::Child() { + return child; +} + +mogltk::widget * mogltk::widget::Next() { + return next; +} + +mogltk::widget * mogltk::widget::Prev() { + return prev; +} + +mogltk::widget * mogltk::widget::InnerPanel() { + if (!panel) + panel = create_panel(); + return panel; +} + +mogltk::widget * mogltk::widget::find_widget(int _x, int _y) { + widget * r = 0; + + if (visible && enabled && (_x >= ax) && (_y >= ay) && (_x <= ax2) && (_y <= ay2)) { + if (child) + r = child->find_widget(_x, _y); + if (!r) + r = this; + } + + if (!r && next) { + r = next->find_widget(_x, _y); + } + + return r; +} + +mogltk::shape * mogltk::widget::Shaper() { + return sh; +} + +void mogltk::widget::fulldraw() { + bool was2D = true; + + if (mogltk::engine::glbase_o) + 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(); + + if (!visible) + return; + + 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(); +} + +bool mogltk::widget::inside(int xe, int ye) { + if (!visible) + return false; + return !((xe < ax) || (xe > ax2) || (ye < ay) || (ye > ay2)); +} + +void mogltk::widget::m_event(int x, int y, mogltk::event_t event) { + switch (event) { + case E_MOUSE_DRAG_OVER: + case E_MOUSE_END_DRAG_OVER: + case E_MOUSE_OUT: + case E_MOUSE_OUT_CLICK: + process_event(x, y, event); + break; + default: + ievent(x, y, event); + } +} + +bool mogltk::widget::ievent(int xe, int ye, mogltk::event_t event) { + if (prev) + if (prev->ievent(xe, ye, event)) + return true; + + if (!inside(xe, ye)) + return false; + + if (exclusive) + return exclusive->ievent(xe, ye, event); + + if (last) + if (last->ievent(xe, ye, event)) + return true; + + if (!enabled || !visible) + return false; + + return process_event(xe, ye, event); +} + +void mogltk::widget::draw() { +} + +bool mogltk::widget::process_event(int, int, mogltk::event_t) { + return false; +} + +void mogltk::widget::resize_notify() { +} + +mogltk::widget * mogltk::widget::create_panel() { + mogltk::rect r = GetDrawRect(); + return new widgets::Panel(Shaper(), this, r.x, r.y, r.w, r.h); +} + +void mogltk::widget::icomputeabs() { + computeabs(); + + if (next) + next->computeabs(); + + if (child) + child->computeabs(); +} + +void mogltk::widget::iresize_notify() { + if (next) + next->iresize_notify(); + if (child) + child->iresize_notify(); + resize_notify(); +} + +void mogltk::widget::set_exclusive(mogltk::widget * w) { + w->exclusive = this; +} + +void mogltk::widget::unset_exclusive(mogltk::widget * w) { + w->exclusive = 0; +} + +bool mogltk::widget::GetVisible() { + return visible; +} + +void mogltk::widget::SetVisible(bool _visible) { + visible = _visible; +} + +bool mogltk::widget::GetEnabled() { + return enabled; +} + +void mogltk::widget::SetEnabled(bool _enabled) { + enabled = _enabled; +} + +void mogltk::widget::MoveOnTop() { + if (!prev) + return; + + if (!next) { + father->last = prev; + } else { + next->prev = prev; + } + prev->next = next; + prev = 0; + next = father->child; + + next->prev = this; + father->child = this; +} + +void mogltk::widget::check_timed_events() { + std::vector<timed_event>::iterator i; + Uint32 t = SDL_GetTicks(); + widget * w; + + for (i = timed_events.begin(); i != timed_events.end(); i++) { + if (i->t <= t) { + w = i->w; + timed_events.erase(i); + w->process_event(engine::mouseX(), engine::mouseY(), E_TIMER); + i = timed_events.begin(); + if (i == timed_events.end()) + return ; + } + } +} + +void mogltk::widget::add_out_move() { + std::vector<widget *>::iterator i; + + for (i = out_move.begin(); i != out_move.end(); i++) { + if (*i == this) + return; + } + + out_move.push_back(this); +} + +void mogltk::widget::remove_out_move() { + std::vector<widget *>::iterator i; + + for (i = out_move.begin(); i != out_move.end(); i++) { + if (*i == this) { + out_move.erase(i); + return; + } + } +} + +void mogltk::widget::add_out_click() { + std::vector<widget *>::iterator i; + + for (i = out_click.begin(); i != out_click.end(); i++) { + if (*i == this) + return; + } + + out_click.push_back(this); +} + +void mogltk::widget::remove_out_click() { + std::vector<widget *>::iterator i; + + for (i = out_click.begin(); i != out_click.end(); i++) { + if (*i == this) { + out_click.erase(i); + return; + } + } +} + +void mogltk::widget::set_timed_event(Uint32 t) { + timed_events.push_back(timed_event(this, SDL_GetTicks() + t)); +} + +void mogltk::widget::set_absolute_timed_event(Uint32 t) { + timed_events.push_back(timed_event(this, t)); +} + + +/* + * Predefined widgets. + */ + +/*************** +* Here is Root * +***************/ + +mogltk::widgets::Root::Root(mogltk::shape * sh, mogltk::widgets::drawer * _dr) : widget(0, 0, 0, 0, 0, 0, "Root", sh), dr(_dr) { + if (engine::root) + delete engine::root; + engine::root = this; +} + +void mogltk::widgets::Root::draw() { + if (dr) + dr->draw(this); + else + Shaper()->box(GetAX(), GetAY(), GetAX2(), GetAY2()); +} + +void mogltk::widgets::Root::setdrawer(drawer * _dr) { + dr = _dr; +} + + +/************************** +* A simple clipping Panel * +**************************/ + +mogltk::widgets::Panel::Panel(shape * sh, widget * father, int x, int y, int w, int h) : widget(father, x, y, w, h, 0, "Panel", sh) { +} + + +/*********************** +* The classical Button * +***********************/ + +mogltk::widgets::Button::Button(action * _a, shape * sh, widget * father, int x, int y, int w, int h, const String & _caption) : widget(father, x, y, w, h, 0, "Button", sh), bevel(false), dragging(false), a(_a) { + caption = _caption; +} + +void mogltk::widgets::Button::draw() { + Shaper()->button(GetAX() + 1, GetAY() + 1, GetAX2() - 1, GetAY2() - 1, caption, bevel); +} + +bool mogltk::widgets::Button::process_event(int xe, int ye, mogltk::event_t event) { + switch (event) { + case E_MOUSE_DOWN: + bevel = true; + return true; + case E_MOUSE_CLICK: + bevel = false; + // action here. + if (a) + a->do_action(this); + return true; + case E_MOUSE_START_DRAG: + dragged_widget = this; + dragging = true; + return true; + case E_MOUSE_DRAG_OVER: + bevel = inside(xe, ye); + return true; + case E_MOUSE_END_DRAG_OVER: + dragging = false; + if (bevel) + // action here. + if (a) + a->do_action(this); + bevel = false; + return true; + } + return false; +} + + +/********************** +* The SmartBox window * +**********************/ + +mogltk::widgets::SmartBox::SmartBox(shape * sh, mogltk::widget * father, int x, int y, int w, int h, const String & _caption) : widget(father, x, y, w, h, 0, "SmartBox", sh) { + caption = _caption; +} + +void mogltk::widgets::SmartBox::draw() { + Shaper()->window(GetAX(), GetAY(), GetAX2(), GetAY2(), caption); +} + +bool mogltk::widgets::SmartBox::process_event(int x, int y, mogltk::event_t event) { + switch (event) { + case E_MOUSE_START_DRAG: + if ((GetAY() + 20) > y) { + dragged_widget = this; + ox = x; + oy = y; + oax = GetX(); + oay = GetY(); + return true; + } + break; + case E_MOUSE_DRAG_OVER: + move(x - ox + oax, y - oy + oay); + MoveOnTop(); + return true; + } + return false; +} + +mogltk::rect mogltk::widgets::SmartBox::GetDrawRect() { + rect r; + + r.x = 2; + r.y = 21; + r.w = GetW() - 4; + r.h = GetH() - 23; + + return r; +} + + +/*********************** +* The MessageBox child * +***********************/ + +class MessageBoxAction : public mogltk::widgets::action { + public: + MessageBoxAction(mogltk::widget * _parent) : parent(_parent) { } + virtual void do_action(mogltk::widget *) { + delete parent; + delete this; + } + private: + mogltk::widget * parent; +}; + +mogltk::widgets::MsgBox::MsgBox(shape * sh, mogltk::widget * father, const String & caption, const String & text) : SmartBox(sh, father, 0, 0, 0, 0, caption) { + rect size = SystemFont->size(text); + rect lsize = size; + + size.w += 12; + size.h += 60; + + size.x = (father->GetW() - size.w) / 2; + size.y = (father->GetH() - size.h) / 2; + resize(size.w, size.h); + move(size.x, size.y); + + new Button(new MessageBoxAction(this), sh, this, size.w / 2 - 25, size.h - 30, 50, 24, "Ok"); + new Label(sh, this, 6, 24, lsize.w, lsize.h, text, BLACK); + + set_exclusive(father); +} + +mogltk::widgets::MsgBox::~MsgBox() { + unset_exclusive(Father()); +} + + +/***************** +* A simple Label * +*****************/ + +mogltk::widgets::Label::Label(shape * sh, mogltk::widget * father, int x, int y, int w, int h, const String & _caption, const ColorP & _color) : widget(father, x, y, w, h, 0, "Label", sh), color(_color) { + caption = _caption; +} + +void mogltk::widgets::Label::draw() { + Shaper()->text(GetAX(), GetAY(), caption, color); +} + + +/************************* +* The classical 3D Frame * +*************************/ + +mogltk::widgets::Frame::Frame(shape * sh, widget * father, int x, int y, int w, int h) : widget(father, x, y, w, h, 0, "Frame", sh) { +} + +void mogltk::widgets::Frame::draw() { + Shaper()->obox3d(GetAX(), GetAY(), GetAX2(), GetAY2()); +} + +mogltk::rect mogltk::widgets::Frame::GetDrawRect() { + rect r; + + r.x = 4; + r.y = 4; + r.w = GetW() - 8; + r.h = GetH() - 8; + + return r; +} + + +/*********************************** +* The huge and bloated ContextMenu * +***********************************/ + +mogltk::widgets::ContextMenu::node::node(const String & _caption, int _x, int _y, int _w, int _h, mogltk::widgets::action * _a, mogltk::widgets::ContextMenu * _sub, mogltk::widgets::ContextMenu * _father, bool _line) : caption(_caption), x(_x), y(_y), w(_w), h(_h), a(_a), sub(_sub), father(_father), line(_line), enabled(true) { +} + +String mogltk::widgets::ContextMenu::node::GetCaption() { + return caption; +} + +mogltk::widgets::action * mogltk::widgets::ContextMenu::node::GetAction() { + return a; +} + +mogltk::widgets::ContextMenu * mogltk::widgets::ContextMenu::node::GetFather() { + return father; +} + +mogltk::widgets::ContextMenu * mogltk::widgets::ContextMenu::node::GetSub() { + return sub; +} + +bool mogltk::widgets::ContextMenu::node::GetLine() { + return line; +} + +bool mogltk::widgets::ContextMenu::node::GetEnabled() { + return enabled; +} + +void mogltk::widgets::ContextMenu::node::SetEnabled(bool _enabled) { + enabled = _enabled; +} + +int mogltk::widgets::ContextMenu::node::GetX() { + return x; +} + +int mogltk::widgets::ContextMenu::node::GetY() { + return y; +} + +int mogltk::widgets::ContextMenu::node::GetW() { + return w; +} + +int mogltk::widgets::ContextMenu::node::GetH() { + return h; +} + +mogltk::widgets::ContextMenu::ContextMenu(shape * sh, mogltk::widget * father, int x, int y) : widget(father, x, y, 8, 4, 1, "ContextMenu", sh), selected(-1), subselected(0), in_click(false), sticky(false) { +} + +mogltk::widgets::ContextMenu::~ContextMenu() { + std::vector<node>::iterator i; + ContextMenu * sub; + + for (i = nodes.begin(); i != nodes.end(); i++) { + if ((sub = i->GetSub())) + delete sub; + } +} + +void mogltk::widgets::ContextMenu::addnode(const String & caption, mogltk::widgets::action * a) { + rect size; + + size = SystemFont->size(caption); + nodes.push_back(node(caption, 4, GetH() - 2, size.w, size.h, a, 0, this, false)); + size.w += 8; + if (GetW() > size.w) + size.w = GetW(); + resize(size.w, GetH() + size.h); +} + +void mogltk::widgets::ContextMenu::addsub(const String & caption, mogltk::widgets::ContextMenu * sub) { + rect size; + + size = SystemFont->size(caption + " >"); + nodes.push_back(node(caption + " >", 4, GetH() - 2, size.w, size.h, 0, sub, this, false)); + size.w += 8; + if (GetW() > size.w) + size.w = GetW(); + resize(size.w, GetH() + size.h); +} + +void mogltk::widgets::ContextMenu::addline() { + nodes.push_back(node("", 2, GetH() - 1, 0, 0, 0, 0, this, true)); + resize(GetW(), GetH() + 4); +} + +mogltk::widgets::ContextMenu * mogltk::widgets::ContextMenu::createsub(const String & caption) { + ContextMenu * r; + + r = new ContextMenu(Shaper(), Father(), GetX() + GetW(), GetY() + GetH()); + r->SetVisible(false); + + addsub(caption, r); + + return r; +} + +void mogltk::widgets::ContextMenu::move(int x, int y) { + int dx, dy; + std::vector<node>::iterator i; + ContextMenu * sub; + + dx = x - GetX(); + dy = y - GetY(); + widget::move(x, y); + + for (i = nodes.begin(); i != nodes.end(); i++) { + if ((sub = i->GetSub())) + sub->move(sub->GetX() + dx, sub->GetY() + dy); + } +} + +void mogltk::widgets::ContextMenu::resize(int w, int h) { + int dw; + std::vector<node>::iterator i; + ContextMenu * sub; + + dw = w - GetW(); + widget::resize(w, h); + + for (i = nodes.begin(); i != nodes.end(); i++) { + if ((sub = i->GetSub())) + sub->move(sub->GetX() + dw, sub->GetY()); + } +} + +void mogltk::widgets::ContextMenu::SetVisible(bool visible) { + if (!visible && sticky) + return; + widget::SetVisible(visible); + if (!visible && subselected) { + subselected->SetVisible(false); + } + + if (visible) { + add_out_click(); + MoveOnTop(); + } +} + +void mogltk::widgets::ContextMenu::SetEnabled(int i, bool e) { + nodes[i].SetEnabled(e); +} + +void mogltk::widgets::ContextMenu::StickyDisplay() { + sticky = true; + SetVisible(true); +} + +void mogltk::widgets::ContextMenu::draw() { + std::vector<node>::iterator i; + int n; + rect size; + + Shaper()->box3d(GetAX(), GetAY(), GetAX2(), GetAY2()); + + for (i = nodes.begin(), n = 0; i != nodes.end(); i++, n++) { + size = SystemFont->size(i->GetCaption()); + if (i->GetLine()) { + Shaper()->hline3d(GetAX() + i->GetX(), GetAX() + i->GetX() + GetW() - 4, GetAY() + i->GetY()); + } else { + if (n == selected) { + Shaper()->box(GetAX() + 2, GetAY() + i->GetY(), GetAX() + GetW() - 2, GetAY() + i->GetY() + i->GetH(), DOS_MAGENTA); + } + Shaper()->text(GetAX() + i->GetX(), GetAY() + i->GetY(), i->GetCaption(), i->GetEnabled() ? BLACK : DOS_GRAY); + } + } +} + +bool mogltk::widgets::ContextMenu::process_event(int xe, int ye, mogltk::event_t event) { + std::vector<node>::iterator i; + action * a; + int n; + + switch (event) { + case E_MOUSE_MOVE: + sticky = false; + selected = -1; + for (i = nodes.begin(), n = 0; i != nodes.end(); i++, n++) { + if (!i->GetLine()) { + int ax, ax2, ay, ay2; + + ax = GetAX() + 2; + ay = GetAY() + i->GetY(); + ax2 = GetAX() + GetW() - 2; + ay2 = GetAY() + i->GetY() + i->GetH(); + + if (!((xe < ax) || (xe > ax2) || (ye < ay) || (ye > ay2))) { + selected = n; + if (subselected) { + subselected->SetVisible(false); + subselected->selected = -1; + subselected->subselected = 0; + } + if ((subselected = i->GetSub())) { + subselected->SetVisible(true); + subselected->selected = -1; + subselected->subselected = 0; + } + } + } + } + add_out_move(); + return true; + case E_MOUSE_CLICK: + a = 0; + if (selected != -1) { + if ((a = nodes[selected].GetAction()) && nodes[selected].GetEnabled()) { + a->do_action(this); + in_click = false; + SetVisible(false); + return true; + } + } + in_click = true; + break; + case E_MOUSE_DOWN: + in_click = true; + break; + case E_MOUSE_OUT: + selected = -1; + remove_out_move(); + in_click = false; + return true; + case E_MOUSE_OUT_CLICK: + if (iin_click()) + return true; + selected = -1; + subselected = 0; + SetVisible(false); + return true; + } + + return false; +} + +bool mogltk::widgets::ContextMenu::iin_click() { + if (in_click) + return true; + + if (subselected) + return subselected->iin_click(); + + return false; +} + + +/******************** +* The Menu topframe * +********************/ + +mogltk::widgets::Menu::node::node(const String & _caption, mogltk::widgets::ContextMenu * _sub, mogltk::widgets::action * _a, int _x) : caption(_caption), sub(_sub), a(_a), x(_x) { +} + +String mogltk::widgets::Menu::node::GetCaption() { + return caption; +} + +void mogltk::widgets::Menu::node::SetCaption(const String & s) { + caption = s; +} + +mogltk::widgets::ContextMenu * mogltk::widgets::Menu::node::GetSub() { + return sub; +} + +mogltk::widgets::action * mogltk::widgets::Menu::node::GetAction() { + return a; +} + +int mogltk::widgets::Menu::node::GetX() { + return x; +} + +bool mogltk::widgets::Menu::node::GetEnabled() { + return enabled; +} + +void mogltk::widgets::Menu::node::SetEnabled(bool _enabled) { + enabled = _enabled; +} + +mogltk::widgets::Menu::Menu(shape * sh, mogltk::widget * father) : widget(father, 0, 0, 0, 0, 0, "Menu", sh), cur_x(0), selected(-1) { + rect r = father->GetDrawRect(); + move(r.x, r.y); + resize(r.w, 16); +} + +void mogltk::widgets::Menu::resize_notify() { + rect r = Father()->GetDrawRect(); + resize(r.w, 16); +} + +void mogltk::widgets::Menu::addnode(const String & caption, mogltk::widgets::action * a) { + nodes.push_back(node(caption, 0, a, cur_x)); + cur_x += SystemFont->singletextsize(caption) + 6; +} + +void mogltk::widgets::Menu::addsub(const String & caption, mogltk::widgets::ContextMenu * sub) { + nodes.push_back(node(caption, sub, 0, cur_x)); + cur_x += SystemFont->singletextsize(caption) + 6; +} + +mogltk::widgets::ContextMenu * mogltk::widgets::Menu::createsub(const String & caption) { + ContextMenu * r = new ContextMenu(Shaper(), Father(), GetX() + cur_x, GetY() + 16); + addsub(caption, r); + r->SetVisible(false); + return r; +} + +void mogltk::widgets::Menu::SetEnabled(int i, bool e) { + nodes[i].SetEnabled(e); +} + +void mogltk::widgets::Menu::SetCaption(int i, const String & s) { + nodes[i].SetCaption(s); +} + +void mogltk::widgets::Menu::draw(void) { + std::vector<node>::iterator i; + int n; + + Shaper()->box(GetAX(), GetAY(), GetAX2(), GetAY2(), DOS_WHITE); + + for (i = nodes.begin(), n = 0; i != nodes.end(); i++, n++) { + if (selected == n) { + if ((i + 1) == nodes.end()) { + Shaper()->box(GetAX() + i->GetX(), GetAY(), cur_x, GetAY2(), DOS_MAGENTA); + } else { + Shaper()->box(GetAX() + i->GetX(), GetAY(), GetAX() + i[1].GetX(), GetAY2(), DOS_MAGENTA); + } + } + Shaper()->text(GetAX() + i->GetX() + 3, GetAY(), i->GetCaption(), BLACK); + } +} + +bool mogltk::widgets::Menu::process_event(int mx, int my, mogltk::event_t event) { + std::vector<node>::iterator i; + action * a; + ContextMenu * sub; + int n; + + switch (event) { + case E_MOUSE_MOVE: + selected = -1; + for (i = nodes.begin(), n = 0; i != nodes.end(); i++, n++) { + int ax, ax2, ay, ay2; + + ax = GetAX() + i->GetX(); + ay = GetAY(); + if ((i + 1) != nodes.end()) { + ax2 = GetAX() + i[1].GetX(); + } else { + ax2 = GetAX() + cur_x; + } + ay2 = GetAY2(); + + if (!((mx < ax) || (mx > ax2) || (my < ay) || (my > ay2))) { + selected = n; + } + } + add_out_move(); + return true; + case E_MOUSE_CLICK: + a = 0; + if (selected != -1) { + if ((a = nodes[selected].GetAction()) && nodes[selected].GetEnabled()) { + a->do_action(this); + return true; + } else if ((sub = nodes[selected].GetSub()) && nodes[selected].GetEnabled()) { + sub->StickyDisplay(); + return true; + } + } + break; + case E_MOUSE_OUT: + selected = -1; + remove_out_move(); + return true; + } + return false; +} + + +/* + * The mainloop widget thing. + */ + +void mogltk::widget::mainloop() { + bool end_event_loop = false; + widget_mouse_event * mouse = new widget_mouse_event(this); + widget * w; + int mx, my; + + while (!end_event_loop && !engine::quitrequested()) { + root->fulldraw(); + mx = mogltk::engine::mouseX(); + my = mogltk::engine::mouseY(); + + mogltk::ColorP::Max.A = 50; + Sprite::Cursor->draw(mx - 6, my - 3, BLACK); + mogltk::ColorP::Max.A = 255; + Sprite::Cursor->draw(mx - 8, my - 6); + + if (w = root->find_widget(mx, my)) { + sh->box(w->GetAX(), w->GetAY(), w->GetAX2(), w->GetAY2(), Color(100, 150, 60, 50)); + } + + engine::base_o->Flip(); + widget::check_timed_events(); + } + + delete mouse; +} diff --git a/src/test.cc b/src/test.cc index 410cfb2..3219004 100644 --- a/src/test.cc +++ b/src/test.cc @@ -1,1308 +1,1308 @@ -#include <sys/types.h>
-#define _USE_MATH_DEFINES
-#include <math.h>
-#include <SDL.h>
-#include <vector>
-//#include <lua.h>
-//#include <lauxlib.h>
-//#include <lualib.h>
-#ifdef HAVE_CONFIG_H
-#include "config.h"
-#else
-#endif
-//#include "gettext.h"
-#include <Exceptions.h>
-#include <generic.h>
-#include <Main.h>
-#include <Input.h>
-#include <Output.h>
-#include <Buffer.h>
-//#include <BLua.h>
-//#include <LuaHandle.h>
-#include "glbase.h"
-#include "texture.h"
-#include "glfont.h"
-#include "glwidgets.h"
-#include "engine.h"
-#include "glsprite.h"
-#include "glshape.h"
-#include "glwidgets.h"
-
-#if 1
-
-#define TRTIME 1500
-#define SPIN .50
-#define TILT .20
-
-#define NBSTARS 300
-#define STARSRATE 300.0
-
-#define SPHERE
-
-int zoom = 1;
-
-#define centerx 320
-#define centery 240
-
-struct tdvect {
- double x, y, z;
-};
-
-std::vector<tdvect> points;
-std::vector<tdvect> ppoints;
-
-#if 0
-static int myprint(lua_State * L) {
- const char * t = lua_tostring(L, -1);
-
- Base::printm(M_INFO, "From LUA: %s\n", t);
-
- return 0;
-}
-
-static int myluaputstar(lua_State * L) {
- tdvect t;
-
- t.x = lua_tonumber(L, -3);
- t.y = lua_tonumber(L, -2);
- t.z = lua_tonumber(L, -1);
-
- points.push_back(t);
-
- return 0;
-}
-
-int myluaerror(lua_State * L) {
- int n = lua_gettop(L);
- int i;
- String t;
-
- Base::printm(M_ERROR, "Got an LUA error\n");
- Base::printm(M_ERROR, "Inspecting LUA stack\n");
-
- if (n == 0) {
- Base::printm(M_ERROR, "Stack empty\n");
- return 0;
- }
-
- for (i = 1; i <= n; i++) {
- switch(lua_type(L, i)) {
- case LUA_TNONE:
- t = "Invalid";
- break;
- case LUA_TNIL:
- t = "(Nil)";
- break;
- case LUA_TNUMBER:
- t.set("(Number) %f", lua_tonumber(L, i));
- break;
- case LUA_TBOOLEAN:
- t.set("(Bool) %s", lua_toboolean(L, i) ? "true" : "false");
- break;
- case LUA_TSTRING:
- t.set("(String) %s", lua_tostring(L, i));
- break;
- case LUA_TTABLE:
- t.set("(Table)");
- break;
- case LUA_TFUNCTION:
- t = "(Function)";
- break;
- default:
- t = "Unknown";
- }
-
- Base::printm(M_ERROR, String(i) + ": " + t + "\n");
- }
-
- return 0;
-
- getchar();
-}
-#endif
-
-unsigned int swap32(unsigned int w) {
- return (w >> 24) | ((w >> 8) & 0x0000ff00) | ((w << 8) & 0x00ff0000) | (w << 24);
-}
-
-unsigned int readB32(FILE * f) {
- unsigned int r;
- fread(&r, 1, 4, f);
- return swap32(r);
-}
-
-unsigned short readB16(FILE * f) {
- unsigned char t[2];
- fread(t, 1, 2, f);
- return t[1] | (t[0] << 8);
-}
-
-float tofloat(unsigned int w) {
- return *((float *) &w);
-}
-
-char * sread(FILE * f) {
- char temp[256], * p = temp;
-
- do {
- *p = fgetc(f);
- } while (*(p++));
-
- return strdup(temp);
-}
-
-char * pname[16] = {
- "Unknown (not a float)",
- "ScalX",
- "ScalY",
- "ScalZ",
- "Unknown (not a float)",
- "Unknown (not a float)",
- "TransX",
- "TransY",
- "TransZ",
- "Unknown",
- "Unknown",
- "Unknown",
- "Unknown",
- "Unknown",
- "Unknown",
- "Unknown"
-};
-
-#define OUTPUT stdout
-
-unsigned short entries;
-struct tdvect * mesh;
-
-int bdlload(char * name) {
- FILE * bdl;
- int i, j;
- char sig[9], sec[5], ** bnames;
- unsigned int length, fpos, namespos, tpos, npos, r;
- unsigned short * unks;
- float f;
-
- if (!(bdl = fopen(name, "rb")))
- exit(-1);
-
- fread(sig, 1, 8, bdl);
- sig[8] = 0;
-
- if (strcmp(sig, "J3D2bdl4")) {
- fprintf(OUTPUT, "File not bdl.\n");
- exit(-1);
- }
-
- fprintf(OUTPUT, "Looking for section JNT1...\n");
-
- fseek(bdl, 32, SEEK_SET);
-
- do {
- fread(sec, 1, 4, bdl);
- sec[4] = 0;
- length = readB32(bdl);
- fprintf(OUTPUT, "Found section `%s' @ %08x, size %08x\n", sec, ftell(bdl) - 8, length);
- fseek(bdl, length - 8, SEEK_CUR);
- } while (strcmp(sec, "JNT1"));
-
- fseek(bdl, -length, SEEK_CUR);
- fpos = ftell(bdl);
-
- fprintf(OUTPUT, "Found it at 0x%08x\n", fpos);
-
- fseek(bdl, 8, SEEK_CUR);
-
- entries = readB16(bdl);
- if (readB16(bdl) != 0xffff) {
- fprintf(OUTPUT, "Something's wrong, not found FFFF\n");
- }
- if (readB32(bdl) != 0x18) {
- fprintf(OUTPUT, "Something's wrong, bones not at 0x18, can't go on.\n");
- exit(-1);
- }
- if (readB32(bdl) != (0x18 + entries * 0x40)) {
- fprintf(OUTPUT, "Something's wrong, bone index not at expected place, can't go on.\n");
- exit(-1);
- }
-
- namespos = readB32(bdl);
-
- fprintf(OUTPUT, "Reading names.\n");
-
- bnames = (char **) malloc(entries * sizeof(char *));
- unks = (unsigned short *) malloc(entries * sizeof(unsigned short));
- mesh = (struct tdvect *) malloc(entries * sizeof(struct tdvect));
-
- fseek(bdl, namespos + fpos + 4, SEEK_SET);
-
- for (i = 0; i < entries; i++) {
- unks[i] = readB16(bdl);
- npos = readB16(bdl) + fpos + namespos;
- tpos = ftell(bdl);
- fseek(bdl, npos, SEEK_SET);
- bnames[i] = sread(bdl);
- fseek(bdl, tpos, SEEK_SET);
- }
- for (i = 0; i < entries; i++) {
- fprintf(OUTPUT, "%02x: (%04x) `%s'\n", i, unks[i], bnames[i]);
- }
-
- fprintf(OUTPUT, "Dumping bones\n");
-
- fseek(bdl, fpos + 0x18, SEEK_SET);
-
- for (i = 0; i < entries; i++) {
- fprintf(OUTPUT, "Bone #%02x (%s):\n", i, bnames[i]);
- for (j = 0; j < 16; j++) {
- r = readB32(bdl);
- f = tofloat(r);
- fprintf(OUTPUT, "%x: %08x - %9.5f - %s\n", j, r, f, pname[j]);
- switch(j) {
- case 6:
- mesh[i].x = f;
- break;
- case 7:
- mesh[i].y = f;
- break;
- case 8:
- mesh[i].z = f;
- break;
- }
- }
- }
-
- return 0;
-}
-
-
-CODE_BEGINS
-
-#if 0
-void checkluastack(lua_State * L) {
- int n = lua_gettop(L);
- int i, j;
- String t;
-
- printm(M_INFO, "Inspecting LUA stack\n");
-
- if (n == 0) {
- printm(M_INFO, "Stack empty\n");
- return;
- }
-
- for (i = 1; i <= n; i++) {
- switch(lua_type(L, i)) {
- case LUA_TNONE:
- t = "Invalid";
- break;
- case LUA_TNIL:
- t = "(Nil)";
- break;
- case LUA_TNUMBER:
- t.set("(Number) %f", lua_tonumber(L, i));
- break;
- case LUA_TBOOLEAN:
- t.set("(Bool) %s", lua_toboolean(L, i) ? "true" : "false");
- break;
- case LUA_TSTRING:
- t.set("(String) %s", lua_tostring(L, i));
- break;
- case LUA_TTABLE:
- 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)";
- break;
- default:
- t = "Unknown";
- }
-
- if (t != "")
- printm(M_INFO, String(i) + ": " + t + "\n");
- }
-}
-#endif
-
-void putstar(double x, double y, double z, mogltk::Sprite * s) {
- double tx, ty, tz;
- double spin, tilt;
-
- spin = (mogltk::engine::mouseX() - centerx) / 80.0;
- tilt = (mogltk::engine::mouseY() - centery) / 80.0;
-
- // Spin --> around y
-
- tx = x * cos(spin) + z * sin(spin);
- tz = -x * sin(spin) + z * cos(spin);
-
- x = tx;
- z = tz;
-
-
- // tilt --> around x
-
- ty = y * cos(tilt) + z * sin(tilt);
- tz = -y * sin(tilt) + z * cos(tilt);
-
- y = ty;
- z = tz;
-
- tdvect t;
- t.x = x;
- t.y = y;
- t.z = z;
-
-// s->draw(centerx + t.x * zoom * mogltk::engine::mouseZ() - s->GetSX() / 2, centery + t.y * zoom * mogltk::engine::mouseZ() - s->GetSY() / 2);
- ppoints.push_back(t);
-}
-
-double t;
-
-#ifdef STARSCAPE
-int starsx[NBSTARS];
-int starsy[NBSTARS];
-#endif
-
-#ifdef SPHERE
-double starst[NBSTARS];
-double starso[NBSTARS];
-#endif
-
-//lua_State * L;
-
-void initstars() {
- int i;
-#ifdef STARSCAPE
- for (i = 0; i < NBSTARS; i++) {
- starsx[i] = rand() % 640;
- starsy[i] = rand() % 480;
- }
-
- t = 0;
-#endif
-#ifdef RING
- t = 0;
-#endif
-#ifdef SPHERE
- t = 0;
- for (i = 0; i < NBSTARS; i++) {
- starst[i] = -(rand() % 3000) / 1000.0;
- starso[i] = 2 * ((rand() % 1000) / 1000.0) * M_PI;
- }
-#endif
-#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);
- lua_pushstring(L, "putstar");
- lua_pushcfunction(L, myluaputstar);
- lua_settable(L, LUA_GLOBALSINDEX);
- lua_pushstring(L, "print");
- lua_pushcfunction(L, myprint);
- lua_settable(L, LUA_GLOBALSINDEX);
- lua_pushstring(L, "initstars");
- lua_gettable(L, LUA_GLOBALSINDEX);
- lua_call(L, 0, 0);
-#endif
-}
-
-void displaystars(mogltk::Sprite * s) {
- int i;
- double x, y, z, st;
- tdvect tv;
-
-#ifdef STARSCAPE
- for (i = 0; i < NBSTARS; i++) {
- s->draw(starsx[i], starsy[i]);
- }
-#endif
-#ifdef RING
- for (i = 0; i < NBSTARS; i++) {
- putstar(cos(t + i * 2 * M_PI / NBSTARS), 0, sin(t + i * 2 * M_PI / NBSTARS), s);
- }
-#endif
-#ifdef SPHERE
- for (i = 0; i < NBSTARS; i++) {
- st = t - starst[i];
-
- y = st / 3.0;
-
- x = cos(st * 2 * M_PI + starso[i]) * sin(y * M_PI);
- z = sin(st * 2 * M_PI + starso[i]) * sin(y * M_PI);
-
- y = cos(y * M_PI);
-
- putstar(x, y, z, s);
- }
-#endif
-#ifdef LUA
- lua_pushstring(L, "displaystars");
- lua_gettable(L, LUA_GLOBALSINDEX);
- lua_call(L, 0, 0);
-
- for (std::vector<tdvect>::iterator i = points.begin(); i != points.end(); i++) {
- tv = *i;
- putstar(tv.x, tv.y, tv.z, s);
- }
- points.clear();
-#endif
-#ifdef BDL
- for (i = 0; i < entries; i++) {
- putstar(mesh[i].x / 20, mesh[i].y / 20, mesh[i].z / 20, s);
- }
-#endif
- while (!ppoints.empty()) {
- std::vector<tdvect>::iterator m = ppoints.begin();
- for (std::vector<tdvect>::iterator i = ppoints.begin(); i != ppoints.end(); i++) {
- if (i->z > m->z)
- m = i;
- }
- tv = *m;
- ppoints.erase(m);
- // Only bare projecting... for now...
- s->draw(centerx + tv.x * zoom * mogltk::engine::mouseZ() - s->GetSX() / 2, centery + tv.y * zoom * mogltk::engine::mouseZ() - s->GetSY() / 2);
- }
-}
-
-void incrementstars(double nt) {
- int i;
-
-#ifdef STARSCAPE
- while ((t + (1 / STARSRATE)) <= nt) {
- t += 1 / STARSRATE;
-
- for (i = 0; i < NBSTARS; i++) {
- if ((++starsx[i]) >= 640) {
- starsx[i] = 0;
- starsy[i] = rand() % 480;
- }
- }
- }
-#endif
-#ifdef RING
- t = nt;
-#endif
-#ifdef SPHERE
- t = nt;
-
- for (i = 0; i < NBSTARS; i++) {
- if ((starst[i] + 3) < t) {
- starst[i] += 3.0;
- starso[i] = 2 * ((rand() % 1000) / 1000.0) * M_PI;
- }
- }
-#endif
-#ifdef LUA
- t = nt;
-
- lua_pushstring(L, "incstars");
- lua_gettable(L, LUA_GLOBALSINDEX);
- lua_pushnumber(L, t);
- lua_call(L, 1, 0);
-#endif
-}
-
-class testkey_t : public mogltk::engine::keyevent {
- public:
- virtual void down(SDL_keysym keysym) {
- if (keysym.sym == SDLK_SPACE && !start_transit) {
- start_transit = true;
- transit_time = SDL_GetTicks();
- } else if (old_handler)
- old_handler->down(keysym);
- }
- virtual void up(SDL_keysym keysym) {
- if (old_handler)
- old_handler->up(keysym);
- }
- bool start_transit;
- Uint32 transit_time;
-} * testkey;
-
-void affiche_grille(mogltk::shape * sh, mogltk::texture * tex, int grille[41][31][2]) {
- int x, y, tx1, tx2, ty1, ty2;
- static mogltk::ColorP w = WHITE;
-
- w.Bind();
- tex->Bind();
-
-// sh->box(0, 0, 640, 480, BLACK);
- for (y = 0; y < 30; y++) {
- for (x = 0; x < 40; x++) {
- tx1 = x * 20;
- ty1 = y * 20;
- tx2 = x * 20 + 20;
- ty2 = y * 20 + 20;
-
-#if 1
- glBegin(GL_TRIANGLE_STRIP);
- glTexCoord2i(tx1, ty1); glVertex2i(grille[x ][y ][0], grille[x ][y ][1]);
- glTexCoord2i(tx2, ty1); glVertex2i(grille[x + 1][y ][0], grille[x + 1][y ][1]);
- glTexCoord2i(tx1, ty2); glVertex2i(grille[x ][y + 1][0], grille[x ][y + 1][1]);
- glTexCoord2i(tx2, ty2); glVertex2i(grille[x + 1][y + 1][0], grille[x + 1][y + 1][1]);
- glEnd();
-#else
- sh->line(grille[x ][y ][0], grille[x ][y ][1],
- grille[x + 1][y ][0], grille[x + 1][y ][1]);
- sh->line(grille[x + 1][y ][0], grille[x + 1][y ][1],
- grille[x + 1][y + 1][0], grille[x + 1][y + 1][1]);
- sh->line(grille[x + 1][y + 1][0], grille[x + 1][y + 1][1],
- grille[x ][y + 1][0], grille[x ][y + 1][1]);
- sh->line(grille[x ][y + 1][0], grille[x ][y + 1][1],
- grille[x ][y ][0], grille[x ][y ][1]);
-#endif
- }
- }
-}
-
-virtual int startup() throw (GeneralException) {
- bool flag = false, flag2 = false;
- mogltk::texture * sshot = 0, * plastex = new mogltk::texture(1024, 512);
- int x, y, sx1, sx2, sy1, sy2, lastframe, curframe, px, py, lasttick = 0, curtick = 0, oldblend, seconds = 0, newseconds = 0;
- double t = 0;
- verbosity = M_INFO;
- String str;
- bool got_transit = false;
- Uint8 * plasma = (Uint8 *) malloc(640 * 480), * pplasma;
- int grille[41][31][2];
- double seeds[41][31];
- double speeds[41][31];
-
-#if 0
- srand(25);
- for (y = 0; y < 31; y++) {
- for (x = 0; x < 41; x++) {
- seeds[x][y] = 1.2 * (double) (rand() % 10000) / 10000;
- speeds[x][y] = 2.6 * (double) (rand() % 10000) / 10000;
- }
- }
-#endif
-
- testkey = new testkey_t();
-
- mogltk::engine::setmouseZ(50);
-
-#if 0
-
-#if 0
- Lua * l = new Lua();
- LuaInput::pushconstruct(l);
- LuaOutput::pushconstruct(l);
- LuaBuffer::pushconstruct(l);
- l->open_math();
- l->declarefunc("print", myprint);
-#if 0
- l->load(&Input("particules.lua"), false);
- l->dump(&Output("particules.out"));
- l->call();
-#else
- l->load(&Input("particules.out"));
-#endif
-// l->call("testing");
- delete l;
-#endif
-
-// bdlload("cl.bdl");
-
-#endif
-
- Input pl("plasma.raw");
- pl.read(plasma, 640 * 480);
-
- pplasma = (Uint8 *) plastex->GetPixels();
-
- for (y = 0; y < 480; y++) {
- for (x = 0; x < 640; x++) {
- pplasma[((y * 1024) + x) * 4 + 3] = 255 - plasma[(y * 640) + x];
- }
- }
-
- new Archive("datas.paq");
-
-#if 1
- mogltk::base * gl = new mogltk::glbase();
- mogltk::shape * sh = new mogltk::glshape();
- mogltk::font * font = new mogltk::glfont(&Input("font-2.bin"));
- mogltk::glSprite * s = new mogltk::glSprite(&Input("cursor.rgba"), 25, 25);
- mogltk::texture * sol = new mogltk::texture(&Input("sol.tex"));
- mogltk::glSprite * logo = new mogltk::glSprite(&Input("logo.raw"), 165, 124);
-#if 0
- mogltk::Sprite * p = new mogltk::glSprite(&Input("particule.rgba"), 16, 16);
- mogltk::glSprite * spr = new mogltk::glSprite(&Input("015_Over-Mind.raw"), 347, 328);
- mogltk::glSprite * eD1 = new mogltk::glSprite(&Input("elkyaD1.raw"), 53, 100);
- mogltk::glSprite * eG1 = new mogltk::glSprite(&Input("elkyaG1.raw"), 53, 100);
- mogltk::glSprite * eM1 = new mogltk::glSprite(&Input("elkyaM1.raw"), 53, 100);
-// mogltk::glSprite * spr = new mogltk::glSprite(&Input("test.raw"), 200, 200);
-#endif
- mogltk::widget * w = new mogltk::widgets::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::Sprite * spr = new mogltk::Sprite(&Input("015_Over-Mind.raw"), 347, 328);
- mogltk::widget * w = new mogltk::Root(sh);
-#endif
-
- mogltk::texture * ttex = 0;
-
- font->setcolor(WHITE);
- font->setshadow(2);
-#if 0
- mogltk::rect textrect;
- mogltk::texture * text = font->printtex(&textrect,
- "PixelPawa!\n"
- "It works!!\n"
- "I can't believe it!\n"
- );
- SDL_SaveBMP(text->GetSurface(), "test-font.bmp");
- textrect.x = 10;
- textrect.y = 30;
-#endif
-
- mogltk::engine::setcursorvisible(true);
- mogltk::engine::setappactive(true);
-
-#if 0
- mogltk::texture * mytex = new mogltk::texture(&Input("pattern6.tex"), true);
-
- Color AlphaBlue(AQUA);
- AlphaBlue.A = 50;
-
- mogltk::fill * f = sh->fcircle(320, 240, 50);
-
- initstars();
-
- px = 200;
- py = 350;
-#endif
-#if 0
- new mogltk::widgets::Button(0, sh,
- new mogltk::widgets::SmartBox(sh, w, 120, 50, 100, 100, "Test"),
- 12, 30, 50, 20, "Toto");
-#endif
- new mogltk::widgets::Frame(sh, w, 10, 10, 600, 400);
- //new mogltk::widgets::MsgBox(sh, w, "Test", "Blablabla\nCeci est un test de la\nclasse MsgBox.");
-
- class tLabel : public mogltk::widgets::Label {
- public:
- tLabel(mogltk::shape * sh, mogltk::widget * father) : Label(sh, father, 250, 200, 16, 16, "I"), tick(0) { set_timed_event(500); }
- virtual bool process_event(int mx, int my, mogltk::event_t event) {
- if (Label::process_event(mx, my, event))
- return true;
-
- if (event == mogltk::E_TIMER) {
- set_timed_event(500);
- tick = (tick + 1) % 4;
- switch (tick) {
- case 0:
- caption = "I";
- break;
- case 1:
- caption = "/";
- break;
- case 2:
- caption = "-";
- break;
- case 3:
- caption = "\\";
- break;
- }
- }
- }
- private:
- int tick;
- };
-
- mogltk::widgets::ContextMenu * c, * c2;
- //c = new mogltk::widgets::ContextMenu(sh, w, 20, 20);
- mogltk::widgets::Menu * sm;
-
- new mogltk::widgets::SmartBox(sh, w, 250, 300, 100, 70, "SmartBox");
- sm = new mogltk::widgets::Menu(sh, w);
-
- sm->addnode("Toto", 0);
- sm->addnode("Titi", 0);
- sm->addnode("Tutu", 0);
-
-#if 0
- c->addnode("Test", 0);
- c->addnode("Test2", 0);
- c->addline();
- c->addnode("Test3", 0);
- c->addnode("Test of a long entry", 0);
-
- c2 = c->createsub("Sub");
- c2->addnode("A sub", 0);
- c2->createsub("subsub")->addnode("Toto", 0);
- c2->SetEnabled(0, false);
-#endif
-
- c = sm->createsub("Sub Menu");
- c->addnode("Blah1", 0);
- c->addnode("Blah2", 0);
- c->addnode("Blah3", 0);
- c->addline();
- c2 = c->createsub("SubBlah");
- c2->addnode("SubBlah1", 0);
- c2->addnode("SubBlah2", 0);
-
- sm->MoveOnTop();
-
- new tLabel(sh, w);
-
- w->mainloop();
-
- while (!mogltk::engine::quitrequested()) {
-#if 0
- sx1 = 320 + 320 * sin(0.983 * t + 3.15);
- sx2 = 320 + 320 * sin(0.537 * t + 5.32);
- sy1 = 240 + 240 * sin(0.692 * t + 8.21);
- sy2 = 240 + 240 * sin(1.029 * t + 2.42);
-#endif
-
- gl->Enter2DMode();
-
-#if 1
- if (got_transit)
- goto do_transit;
-#endif
-
-#if 0
- 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;
- //sh->box(5, 5, 400, 300, CORNFLOWERBLUE, DEEPSKYBLUE, MIDNIGHTBLUE, NAVY);
- mogltk::ColorP::Max.A = 255;
-#if 1
- font->setshadow(1);
- font->putcursor(10, 30);
- mogltk::ColorP::Max.A = sy1 / 2;
- font->setcolor(Color(sy1 / 2, sy1 / 2, sy1 / 2, sy1 / 2));
- font->printf(
- "PixelPawa!\n"
- "It works!!\n"
- "I can't believe it!\n"
- );
- mogltk::ColorP::Max.A = 255;
- font->setcolor(WHITE);
-#else
- sh->tbox(text, textrect.x, textrect.y, textrect.x + textrect.w, textrect.y + textrect.h);
-#endif
-
- 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");
-
- sh->fdraw(f, BLUE);
- sh->sdraw(f);
-
- eM1->drawrotate(500, 400, sx1);
-
- curtick = (SDL_GetTicks() / 250) % 4;
-
- if (curtick != lasttick) {
- px -= 10;
- py += 5;
-
- if (px < -53)
- px = 640;
- if (px > 640)
- px = -53;
- if (py > 480)
- py = -100;
- lasttick = curtick;
- }
-
- switch(curtick) {
- case 0:
- eM1->draw(px, py, WHITE, 1, 1);
- break;
- case 1:
- eD1->draw(px, py, WHITE, 1, 1);
- break;
- case 2:
- eM1->draw(px, py, WHITE, 1, 1);
- break;
- case 3:
- eG1->draw(px, py, WHITE, 1, 1);
- break;
- }
-
- sh->box(MIN(sx1, sx2), MIN(sy1, sy2), MAX(sx1, sx2), MAX(sy1, sy2), AlphaBlue);
- mogltk::ColorP::Min.A = 200;
- sh->obox(MIN(sx1, sx2), MIN(sy1, sy2), MAX(sx1, sx2), MAX(sy1, sy2), AlphaBlue);
- mogltk::ColorP::Min.A = 0;
-
- p->draw(mogltk::engine::mouseX() - 8, mogltk::engine::mouseY() - 8);
- sh->line(320, 240, mogltk::engine::mouseX(), mogltk::engine::mouseY());
-
- mogltk::ColorP::Min.A = 200;
- p->draw(sx1 - 8, sy1 - 8, AlphaBlue);
- p->draw(sx2 - 8, sy2 - 8, AlphaBlue);
-
- displaystars(p);
- incrementstars(t);
-
- sh->box(0, 0, 640, 480, CORNFLOWERBLUE, DEEPSKYBLUE, MIDNIGHTBLUE, NAVY);
-
-#endif
-// sh->box(0, 0, 640, 480, CORNFLOWERBLUE, DEEPSKYBLUE, MIDNIGHTBLUE, NAVY);
-
- int x, y;
-
-#if 0
- for (y = 0; y < 31; y++) {
- for (x = 0; x < 41; x++) {
- grille[x][y][0] = x * 16 + 8 * sin(t * 6 + (x + y) / 2.0);
- grille[x][y][1] = y * 16 + 8 * cos(t * 6 + (x + y) / 2.0);
- }
- }
-#endif
- double dis, xdis, ydis, xpos, ypos, norm, tt, tt1;
-
- t += 48;
-
- tt = t - (((int) t) / 58) * 58;
-
- if (tt <= 20) {
- flag = flag2 = false;
- for (y = 0; y < 31; y++) {
- for (x = 0; x < 41; x++) {
- xpos = x - 20.5;
- ypos = y - 15.5;
- norm = sqrt(xpos * xpos + ypos * ypos);
- xpos /= norm;
- ypos /= norm;
- dis = sin(t * 6 - norm);
- xdis = dis * sin(xpos);
- ydis = dis * sin(ypos);
- grille[x][y][0] = (((x * 16 + 16 * xdis) - 320) * 1.08) + 320;
- grille[x][y][1] = (((y * 16 + 16 * ydis) - 240) * 1.08) + 240;
- }
- }
-
- affiche_grille(sh, sol, grille);
- if (tt < 4)
- sh->box(0, 0, 640, 480, Color(255, 255, 255, 255 * ((4 - tt) / 4)));
- else if (tt > 16)
- sh->box(0, 0, 640, 480, Color(0, 0, 0, 255 * ((tt - 16) / 4)));
- } else if (tt <= 23) {
- tt -= 20;
- sh->box(0, 0, 640, 480, BLACK);
- for (y = 0; y < 31; y++) {
- for (x = 0; x < 41; x++) {
- grille[x][y][0] = ((((x * 16 + 8 * sin(t * 6 + (x + y) / 2.0)) - 320) * 1.08) + 320) * tt / 3;
- grille[x][y][1] = ((((y * 16 + 8 * cos(t * 6 + (x + y) / 2.0)) - 200) * 1.08) + 200) * tt / 3;
- }
- }
- affiche_grille(sh, sol, grille);
- } else if (tt <= 36) {
- for (y = 0; y < 31; y++) {
- for (x = 0; x < 41; x++) {
- grille[x][y][0] = (((x * 16 + 8 * sin(t * 6 + (x + y) / 2.0)) - 320) * 1.08) + 320;
- grille[x][y][1] = (((y * 16 + 8 * cos(t * 6 + (x + y) / 2.0)) - 200) * 1.08) + 200;
- }
- }
- affiche_grille(sh, sol, grille);
- } else if (tt <= 38) {
- tt -= 36;
- if (!flag2) {
- sshot = gl->GrabTexture();
- flag2 = true;
- }
- sh->tbox(sshot, 0, 0, 640, 480);
- if (tt <= 0.1) {
- sh->box(0, 0, 640, 480, Color(255, 255, 255, 255 * (tt / 0.1)));
- } else if (tt < 0.5) {
- tt -= 0.1;
- sh->box(0, 0, 640, 480, Color(255, 255, 255, 255 * ((0.4 - tt) / 0.4)));
- sh->box(0, 0, 640, 480, Color(0.3 * 255, 0.2 * 255, 0.3 * 255, 127 * (tt / 0.4)));
- } else {
- sh->box(0, 0, 640, 480, Color(0.3 * 255, 0.2 * 255, 0.3 * 255, 127));
- }
- for (y = 0; y < 15; y++) {
- for (x = 0; x < 20; x++) {
- sh->obox(x * 32, y * 32, x * 32 + 32, y * 32 + 32, WHITE);
- }
- }
- } else if (tt <= 42) {
- int xx, yy, tyy;
- double ttt;
- tt -= 38;
- sh->box(0, 0, 640, 480, BLACK);
- for (y = 14; y >= 0; y--) {
- for (x = 19; x >= 0; x--) {
- double d = (33.0 - x - y) / 16.5;
- xx = x * 32;
- tyy = y * 32;
- if (tt <= d) {
- yy = y * 32;
- } else {
- ttt = tt - d;
- yy = y * 32 + ttt * ttt * ttt * 60;
- }
- sh->tbox(sshot, xx, yy, xx + 32, yy + 32, xx, tyy, xx + 32, tyy + 32);
- sh->box(xx, yy, xx + 32, yy + 32, Color(0.3 * 255, 0.2 * 255, 0.3 * 255, 127));
- sh->obox(xx, yy, xx + 32, yy + 32, WHITE);
- }
- }
- } else if (tt <= 43) {
- sh->box(0, 0, 640, 480, BLACK);
- } else if (tt <= 48) {
- tt -= 43;
- sh->tbox(sol, 0, 0, 640, 480, 0, 0, 800, 600);
- glDisable(GL_BLEND);
- glEnable(GL_ALPHA_TEST);
- glAlphaFunc(GL_GREATER, tt / 5);
- sh->tbox(plastex, 0, 0, 640, 480);
- glDisable(GL_ALPHA_TEST);
- glEnable(GL_BLEND);
- } else if (tt <= 56) {
- sh->tbox(sol, 0, 0, 640, 480, 0, 0, 800, 600);
- } else if (tt <= 58) {
- if (!flag) {
- if (sshot)
- delete sshot;
- testkey->start_transit = true;
- testkey->transit_time = SDL_GetTicks();
- flag = true;
- } else {
- glBlendFunc(GL_SRC_ALPHA, GL_ONE);
- sh->box(0, 0, 640, 480, Color(255, 255, 255, 1));
- glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
- }
- }
-
- logo->draw(10, 10, Color(255, 255, 255, 100));
-
-
- font->putcursor(530, 460);
- 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", SDL_GetTicks() / 1000.0);
-// mogltk::ColorP::Max.A = 50;
-// s->draw(mogltk::engine::mouseX() - 6, mogltk::engine::mouseY() - 3, BLACK);
-// mogltk::ColorP::Max.A = 255;
-// s->draw(mogltk::engine::mouseX() - 8, mogltk::engine::mouseY() - 6);
-
-do_transit:
- mogltk::ColorP::Min.A = 0;
- mogltk::ColorP::Max.A = 255;
- if (testkey->start_transit) {
- double t;
- int x, y;
- Uint8 * pixels;
- if (!got_transit) {
- lastframe = 0;
- got_transit = true;
- ttex = gl->GrabTexture();
- pixels = (Uint8 *) ttex->GetPixels();
- oldblend = 20;
-#if 1
- for (y = 0; y < 480; y++) {
- for (x = 0; x < 640; x++) {
-#if 0
- pixels[(y * 1024 + x) * 4 + 0] ^= 255;
- pixels[(y * 1024 + x) * 4 + 1] ^= 255;
- pixels[(y * 1024 + x) * 4 + 2] ^= 255;
-#endif
-#if 0
- pixels[(y * 1024 + x) * 4 + 3] = plasma[y * 640 + x];
-#endif
-#if 1
- pixels[(y * 1024 + x) * 4 + 3] = 30;
-#endif
-#if 0
- pixels[(y * 1024 + x) * 4 + 3] = 7;
-#endif
-#if 0
- pixels[(y * 1024 + x) * 4 + 3] = 1;
-#endif
- }
- }
-#endif
- }
- pixels = (Uint8 *) ttex->GetPixels();
- t = (double) (SDL_GetTicks() - testkey->transit_time) / TRTIME;
- curframe = 45 * (SDL_GetTicks() - testkey->transit_time) / TRTIME;
- if (t >= 1.0) {
- testkey->start_transit = false;
- }
-#if 0
- mogltk::ColorP::Max.A = 255 * (TRTIME - SDL_GetTicks() + testkey->transit_time) / TRTIME;
- mogltk::ColorP::Min.R = 255 * (SDL_GetTicks() - testkey->transit_time) / TRTIME;
- mogltk::ColorP::Min.G = 255 * (SDL_GetTicks() - testkey->transit_time) / TRTIME;
- mogltk::ColorP::Min.B = 255 * (SDL_GetTicks() - testkey->transit_time) / TRTIME;
- sh->tbox(ttex, 0, 0, 640, 480);
- mogltk::ColorP::Max.A = 255;
- mogltk::ColorP::Min.R = 0;
- mogltk::ColorP::Min.G = 0;
- mogltk::ColorP::Min.B = 0;
-#endif
-#if 0
- for (y = 0; y < 10; y++) {
- for (x = 0; x < 10; x++) {
- int cx, cy, tx1, ty1, tx2, ty2;
- cx = x * 64 + 32;
- cy = y * 48 + 24;
- tx1 = x * 64;
- ty1 = y * 48;
- tx2 = x * 64 + 64;
- ty2 = y * 48 + 48;
- glMatrixMode(GL_MODELVIEW);
- glPushMatrix();
- glTranslated(cx, cy, 0);
- glScaled((1 - t), (1 - t), 1);
- glRotated((t + ((x + y) / 30)) * 400, 0, 0, 1);
- sh->tbox(ttex, -32, -24, 32, 24, tx1, ty1, tx2, ty2);
- glPopMatrix();
- }
- }
-#endif
-#if 0
- glDisable(GL_BLEND);
- glEnable(GL_ALPHA_TEST);
- glAlphaFunc(GL_GREATER, t);
- sh->tbox(ttex, 0, 0, 640, 480);
- glDisable(GL_ALPHA_TEST);
- glEnable(GL_BLEND);
-#endif
-#if 1
- if (curframe > lastframe) {
-#if 1
- sh->box(0, 0, 640, 480, Color(40, 40, 40, 12));
- glBlendFunc(GL_SRC_ALPHA, GL_ONE);
- glPushMatrix();
- glTranslated(320, 240, 0);
- glScaled(1 + 2 * t, 1 + 2 * t, 1);
- glRotated(t * 100, 0, 0, 1);
- sh->tbox(ttex, -320, -240, 320, 240);
- glPopMatrix();
- glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
-#endif
-#if 0
- sh->box(0, 0, 640, 480, Color(25, 25, 25, 20));
- glBlendFunc(GL_SRC_ALPHA, GL_ONE);
- glPushMatrix();
- glTranslated(320, 240, 0);
- glTranslated(curframe, 0, 0); sh->tbox(ttex, -320, -240, 320, 240);
- glTranslated(0, curframe, 0); sh->tbox(ttex, -320, -240, 320, 240);
- glTranslated(-curframe, 0, 0); sh->tbox(ttex, -320, -240, 320, 240);
- glTranslated(-curframe, 0, 0); sh->tbox(ttex, -320, -240, 320, 240);
- glTranslated(0, -curframe, 0); sh->tbox(ttex, -320, -240, 320, 240);
- glTranslated(0, -curframe, 0); sh->tbox(ttex, -320, -240, 320, 240);
- glTranslated(curframe, 0, 0); sh->tbox(ttex, -320, -240, 320, 240);
- glTranslated(curframe, 0, 0); sh->tbox(ttex, -320, -240, 320, 240);
- glPopMatrix();
- glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
-#endif
-#if 0
- int x0 = 320, y0 = 240;
- int x = 0;
- int y = t * 15 - 1;
- int d = 3 - 2 * t * 15;
- int dI = 10 - 4 * t * 15;
- int rI = 6;
-
- sh->box(0, 0, 640, 480, Color(25, 25, 25, oldblend));
- glBlendFunc(GL_SRC_ALPHA, GL_ONE);
- oldblend = 0;
- while (x <= y) {
- oldblend += 16;
- glPushMatrix(); glTranslated(x0 + x, y0 + y, 0); sh->tbox(ttex, -320, -240, 320, 240); glPopMatrix();
- glPushMatrix(); glTranslated(x0 - x, y0 + y, 0); sh->tbox(ttex, -320, -240, 320, 240); glPopMatrix();
- glPushMatrix(); glTranslated(x0 + x, y0 - y, 0); sh->tbox(ttex, -320, -240, 320, 240); glPopMatrix();
- glPushMatrix(); glTranslated(x0 - x, y0 - y, 0); sh->tbox(ttex, -320, -240, 320, 240); glPopMatrix();
- glPushMatrix(); glTranslated(x0 + y, y0 + x, 0); sh->tbox(ttex, -320, -240, 320, 240); glPopMatrix();
- glPushMatrix(); glTranslated(x0 - y, y0 + x, 0); sh->tbox(ttex, -320, -240, 320, 240); glPopMatrix();
- glPushMatrix(); glTranslated(x0 + y, y0 - x, 0); sh->tbox(ttex, -320, -240, 320, 240); glPopMatrix();
- glPushMatrix(); glTranslated(x0 - y, y0 - x, 0); sh->tbox(ttex, -320, -240, 320, 240); glPopMatrix();
- if (d >= 0) {
- d += dI;
- dI += 8;
- y -= 1;
- } else {
- d += rI;
- dI += 4;
- }
- rI += 4;
- x += 1;
- }
- printm(M_INFO, "blends = %i\n", oldblend / 2);
- oldblend += 10;
- glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
-#endif
-#if 0
- sh->box(0, 0, 640, 480, Color(40, 40, 40, 12));
- glBlendFunc(GL_SRC_ALPHA, GL_ONE);
- glPushMatrix();
- glTranslated(320, 240, 0);
- glRotated(t * 25, 0, 0, 1);
- sh->tbox(ttex, -320, -240, 320, 240);
- glPopMatrix();
- glPushMatrix();
- glTranslated(320, 240, 0);
- glRotated(-t * 25, 0, 0, 1);
- sh->tbox(ttex, -320, -240, 320, 240);
- glPopMatrix();
- glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
-#endif
- lastframe = curframe;
- }
-#endif
- }
- if (!testkey->start_transit && ttex) {
- delete ttex;
- ttex = 0;
- got_transit = false;
- }
-
- gl->Leave2DMode();
-
- //gl->Flip(got_transit ? false : true);
- gl->Flip(false);
- t = (double) SDL_GetTicks() / 1000.0;
- newseconds = t;
-
- if (newseconds != seconds) {
- printm(M_INFO, "FPS: %.2f\n", mogltk::engine::FPS());
- seconds = newseconds;
- }
- }
-
-// delete mytex;
-
- return 0;
-}
-CODE_ENDS
-
-#else
-
-#if 0
-CODE_BEGINS
-virtual int startup() throw (GeneralException) {
- unsigned char palette[256][3], * pixels, c;
- char outname[256];
- int width, height, x, y;
-
- if (argc != 2)
- exit(-1);
- Input i(argv[1]);
- SDL_Init(SDL_INIT_VIDEO);
- atexit(SDL_Quit);
- width = i.readU16();
- height = i.readU16();
- i.seek(12, SEEK_CUR);
- SDL_Surface * s = SDL_SetVideoMode(width, height, 24, 0);
- pixels = (unsigned char *) s->pixels;
- for (x = 0; x < 256; x++) {
- for (y = 0; y < 3; y++) {
- palette[x][y] = i.readU8();
- }
- i.seek(1, SEEK_CUR);
- }
- for (y = 0; y < height; y++) {
- for (x = 0; x < width; x++) {
- c = i.readU8();
- pixels[(y * width + x) * 3 + 0] = palette[c][2];
- pixels[(y * width + x) * 3 + 1] = palette[c][1];
- pixels[(y * width + x) * 3 + 2] = palette[c][0];
- }
- }
-
- *strchr(argv[1], '.') = 0;
-
- sprintf(outname, "%s.bmp", argv[1]);
-
- SDL_SaveBMP(s, outname);
-
- SDL_Flip(s);
-
- SDL_Event event;
-
- while(true) {
- SDL_PollEvent(&event);
- switch (event.type) {
- case SDL_QUIT:
- exit(0);
- }
- }
-
- return 0;
-}
-CODE_ENDS
-
-#else
-CODE_BEGINS
-virtual int startup() throw (GeneralException) {
- unsigned char * pixels, c;
- char outname[256];
- int width, height, x, y;
-
- if (argc != 2)
- exit(-1);
- Input i(argv[1]);
- SDL_Init(SDL_INIT_VIDEO);
- atexit(SDL_Quit);
- i.seek(4, SEEK_SET);
- i.read(&width, 4);
- i.read(&height, 4);
- i.seek(4, SEEK_CUR);
- SDL_Surface * _s = SDL_SetVideoMode(width, height, 32, 0);
- SDL_Surface * s = SDL_CreateRGBSurface(SDL_SWSURFACE, width, height, 32,
- 0x000000ff,
- 0x0000ff00,
- 0x00ff0000,
- 0xff000000);
-
- pixels = (unsigned char *) s->pixels;
- *strchr(argv[1], '.') = 0;
-
- i.read(pixels, width * height * 4);
-
- sprintf(outname, "%s.bmp", argv[1]);
-
- SDL_SaveBMP(s, outname);
- SDL_BlitSurface(s, 0, _s, 0);
-
- SDL_Flip(_s);
-
- SDL_Event event;
-
- while(true) {
- SDL_PollEvent(&event);
- switch (event.type) {
- case SDL_QUIT:
- exit(0);
- }
- }
-
- return 0;
-}
-CODE_ENDS
-
-#endif
-
-#endif
+#include <sys/types.h> +#define _USE_MATH_DEFINES +#include <math.h> +#include <SDL.h> +#include <vector> +//#include <lua.h> +//#include <lauxlib.h> +//#include <lualib.h> +#ifdef HAVE_CONFIG_H +#include "config.h" +#else +#endif +//#include "gettext.h" +#include <Exceptions.h> +#include <generic.h> +#include <Main.h> +#include <Input.h> +#include <Output.h> +#include <Buffer.h> +//#include <BLua.h> +//#include <LuaHandle.h> +#include "glbase.h" +#include "texture.h" +#include "glfont.h" +#include "glwidgets.h" +#include "engine.h" +#include "glsprite.h" +#include "glshape.h" +#include "glwidgets.h" + +#if 1 + +#define TRTIME 1500 +#define SPIN .50 +#define TILT .20 + +#define NBSTARS 300 +#define STARSRATE 300.0 + +#define SPHERE + +int zoom = 1; + +#define centerx 320 +#define centery 240 + +struct tdvect { + double x, y, z; +}; + +std::vector<tdvect> points; +std::vector<tdvect> ppoints; + +#if 0 +static int myprint(lua_State * L) { + const char * t = lua_tostring(L, -1); + + Base::printm(M_INFO, "From LUA: %s\n", t); + + return 0; +} + +static int myluaputstar(lua_State * L) { + tdvect t; + + t.x = lua_tonumber(L, -3); + t.y = lua_tonumber(L, -2); + t.z = lua_tonumber(L, -1); + + points.push_back(t); + + return 0; +} + +int myluaerror(lua_State * L) { + int n = lua_gettop(L); + int i; + String t; + + Base::printm(M_ERROR, "Got an LUA error\n"); + Base::printm(M_ERROR, "Inspecting LUA stack\n"); + + if (n == 0) { + Base::printm(M_ERROR, "Stack empty\n"); + return 0; + } + + for (i = 1; i <= n; i++) { + switch(lua_type(L, i)) { + case LUA_TNONE: + t = "Invalid"; + break; + case LUA_TNIL: + t = "(Nil)"; + break; + case LUA_TNUMBER: + t.set("(Number) %f", lua_tonumber(L, i)); + break; + case LUA_TBOOLEAN: + t.set("(Bool) %s", lua_toboolean(L, i) ? "true" : "false"); + break; + case LUA_TSTRING: + t.set("(String) %s", lua_tostring(L, i)); + break; + case LUA_TTABLE: + t.set("(Table)"); + break; + case LUA_TFUNCTION: + t = "(Function)"; + break; + default: + t = "Unknown"; + } + + Base::printm(M_ERROR, String(i) + ": " + t + "\n"); + } + + return 0; + + getchar(); +} +#endif + +unsigned int swap32(unsigned int w) { + return (w >> 24) | ((w >> 8) & 0x0000ff00) | ((w << 8) & 0x00ff0000) | (w << 24); +} + +unsigned int readB32(FILE * f) { + unsigned int r; + fread(&r, 1, 4, f); + return swap32(r); +} + +unsigned short readB16(FILE * f) { + unsigned char t[2]; + fread(t, 1, 2, f); + return t[1] | (t[0] << 8); +} + +float tofloat(unsigned int w) { + return *((float *) &w); +} + +char * sread(FILE * f) { + char temp[256], * p = temp; + + do { + *p = fgetc(f); + } while (*(p++)); + + return strdup(temp); +} + +char * pname[16] = { + "Unknown (not a float)", + "ScalX", + "ScalY", + "ScalZ", + "Unknown (not a float)", + "Unknown (not a float)", + "TransX", + "TransY", + "TransZ", + "Unknown", + "Unknown", + "Unknown", + "Unknown", + "Unknown", + "Unknown", + "Unknown" +}; + +#define OUTPUT stdout + +unsigned short entries; +struct tdvect * mesh; + +int bdlload(char * name) { + FILE * bdl; + int i, j; + char sig[9], sec[5], ** bnames; + unsigned int length, fpos, namespos, tpos, npos, r; + unsigned short * unks; + float f; + + if (!(bdl = fopen(name, "rb"))) + exit(-1); + + fread(sig, 1, 8, bdl); + sig[8] = 0; + + if (strcmp(sig, "J3D2bdl4")) { + fprintf(OUTPUT, "File not bdl.\n"); + exit(-1); + } + + fprintf(OUTPUT, "Looking for section JNT1...\n"); + + fseek(bdl, 32, SEEK_SET); + + do { + fread(sec, 1, 4, bdl); + sec[4] = 0; + length = readB32(bdl); + fprintf(OUTPUT, "Found section `%s' @ %08x, size %08x\n", sec, ftell(bdl) - 8, length); + fseek(bdl, length - 8, SEEK_CUR); + } while (strcmp(sec, "JNT1")); + + fseek(bdl, -length, SEEK_CUR); + fpos = ftell(bdl); + + fprintf(OUTPUT, "Found it at 0x%08x\n", fpos); + + fseek(bdl, 8, SEEK_CUR); + + entries = readB16(bdl); + if (readB16(bdl) != 0xffff) { + fprintf(OUTPUT, "Something's wrong, not found FFFF\n"); + } + if (readB32(bdl) != 0x18) { + fprintf(OUTPUT, "Something's wrong, bones not at 0x18, can't go on.\n"); + exit(-1); + } + if (readB32(bdl) != (0x18 + entries * 0x40)) { + fprintf(OUTPUT, "Something's wrong, bone index not at expected place, can't go on.\n"); + exit(-1); + } + + namespos = readB32(bdl); + + fprintf(OUTPUT, "Reading names.\n"); + + bnames = (char **) malloc(entries * sizeof(char *)); + unks = (unsigned short *) malloc(entries * sizeof(unsigned short)); + mesh = (struct tdvect *) malloc(entries * sizeof(struct tdvect)); + + fseek(bdl, namespos + fpos + 4, SEEK_SET); + + for (i = 0; i < entries; i++) { + unks[i] = readB16(bdl); + npos = readB16(bdl) + fpos + namespos; + tpos = ftell(bdl); + fseek(bdl, npos, SEEK_SET); + bnames[i] = sread(bdl); + fseek(bdl, tpos, SEEK_SET); + } + for (i = 0; i < entries; i++) { + fprintf(OUTPUT, "%02x: (%04x) `%s'\n", i, unks[i], bnames[i]); + } + + fprintf(OUTPUT, "Dumping bones\n"); + + fseek(bdl, fpos + 0x18, SEEK_SET); + + for (i = 0; i < entries; i++) { + fprintf(OUTPUT, "Bone #%02x (%s):\n", i, bnames[i]); + for (j = 0; j < 16; j++) { + r = readB32(bdl); + f = tofloat(r); + fprintf(OUTPUT, "%x: %08x - %9.5f - %s\n", j, r, f, pname[j]); + switch(j) { + case 6: + mesh[i].x = f; + break; + case 7: + mesh[i].y = f; + break; + case 8: + mesh[i].z = f; + break; + } + } + } + + return 0; +} + + +CODE_BEGINS + +#if 0 +void checkluastack(lua_State * L) { + int n = lua_gettop(L); + int i, j; + String t; + + printm(M_INFO, "Inspecting LUA stack\n"); + + if (n == 0) { + printm(M_INFO, "Stack empty\n"); + return; + } + + for (i = 1; i <= n; i++) { + switch(lua_type(L, i)) { + case LUA_TNONE: + t = "Invalid"; + break; + case LUA_TNIL: + t = "(Nil)"; + break; + case LUA_TNUMBER: + t.set("(Number) %f", lua_tonumber(L, i)); + break; + case LUA_TBOOLEAN: + t.set("(Bool) %s", lua_toboolean(L, i) ? "true" : "false"); + break; + case LUA_TSTRING: + t.set("(String) %s", lua_tostring(L, i)); + break; + case LUA_TTABLE: + 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)"; + break; + default: + t = "Unknown"; + } + + if (t != "") + printm(M_INFO, String(i) + ": " + t + "\n"); + } +} +#endif + +void putstar(double x, double y, double z, mogltk::Sprite * s) { + double tx, ty, tz; + double spin, tilt; + + spin = (mogltk::engine::mouseX() - centerx) / 80.0; + tilt = (mogltk::engine::mouseY() - centery) / 80.0; + + // Spin --> around y + + tx = x * cos(spin) + z * sin(spin); + tz = -x * sin(spin) + z * cos(spin); + + x = tx; + z = tz; + + + // tilt --> around x + + ty = y * cos(tilt) + z * sin(tilt); + tz = -y * sin(tilt) + z * cos(tilt); + + y = ty; + z = tz; + + tdvect t; + t.x = x; + t.y = y; + t.z = z; + +// s->draw(centerx + t.x * zoom * mogltk::engine::mouseZ() - s->GetSX() / 2, centery + t.y * zoom * mogltk::engine::mouseZ() - s->GetSY() / 2); + ppoints.push_back(t); +} + +double t; + +#ifdef STARSCAPE +int starsx[NBSTARS]; +int starsy[NBSTARS]; +#endif + +#ifdef SPHERE +double starst[NBSTARS]; +double starso[NBSTARS]; +#endif + +//lua_State * L; + +void initstars() { + int i; +#ifdef STARSCAPE + for (i = 0; i < NBSTARS; i++) { + starsx[i] = rand() % 640; + starsy[i] = rand() % 480; + } + + t = 0; +#endif +#ifdef RING + t = 0; +#endif +#ifdef SPHERE + t = 0; + for (i = 0; i < NBSTARS; i++) { + starst[i] = -(rand() % 3000) / 1000.0; + starso[i] = 2 * ((rand() % 1000) / 1000.0) * M_PI; + } +#endif +#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); + lua_pushstring(L, "putstar"); + lua_pushcfunction(L, myluaputstar); + lua_settable(L, LUA_GLOBALSINDEX); + lua_pushstring(L, "print"); + lua_pushcfunction(L, myprint); + lua_settable(L, LUA_GLOBALSINDEX); + lua_pushstring(L, "initstars"); + lua_gettable(L, LUA_GLOBALSINDEX); + lua_call(L, 0, 0); +#endif +} + +void displaystars(mogltk::Sprite * s) { + int i; + double x, y, z, st; + tdvect tv; + +#ifdef STARSCAPE + for (i = 0; i < NBSTARS; i++) { + s->draw(starsx[i], starsy[i]); + } +#endif +#ifdef RING + for (i = 0; i < NBSTARS; i++) { + putstar(cos(t + i * 2 * M_PI / NBSTARS), 0, sin(t + i * 2 * M_PI / NBSTARS), s); + } +#endif +#ifdef SPHERE + for (i = 0; i < NBSTARS; i++) { + st = t - starst[i]; + + y = st / 3.0; + + x = cos(st * 2 * M_PI + starso[i]) * sin(y * M_PI); + z = sin(st * 2 * M_PI + starso[i]) * sin(y * M_PI); + + y = cos(y * M_PI); + + putstar(x, y, z, s); + } +#endif +#ifdef LUA + lua_pushstring(L, "displaystars"); + lua_gettable(L, LUA_GLOBALSINDEX); + lua_call(L, 0, 0); + + for (std::vector<tdvect>::iterator i = points.begin(); i != points.end(); i++) { + tv = *i; + putstar(tv.x, tv.y, tv.z, s); + } + points.clear(); +#endif +#ifdef BDL + for (i = 0; i < entries; i++) { + putstar(mesh[i].x / 20, mesh[i].y / 20, mesh[i].z / 20, s); + } +#endif + while (!ppoints.empty()) { + std::vector<tdvect>::iterator m = ppoints.begin(); + for (std::vector<tdvect>::iterator i = ppoints.begin(); i != ppoints.end(); i++) { + if (i->z > m->z) + m = i; + } + tv = *m; + ppoints.erase(m); + // Only bare projecting... for now... + s->draw(centerx + tv.x * zoom * mogltk::engine::mouseZ() - s->GetSX() / 2, centery + tv.y * zoom * mogltk::engine::mouseZ() - s->GetSY() / 2); + } +} + +void incrementstars(double nt) { + int i; + +#ifdef STARSCAPE + while ((t + (1 / STARSRATE)) <= nt) { + t += 1 / STARSRATE; + + for (i = 0; i < NBSTARS; i++) { + if ((++starsx[i]) >= 640) { + starsx[i] = 0; + starsy[i] = rand() % 480; + } + } + } +#endif +#ifdef RING + t = nt; +#endif +#ifdef SPHERE + t = nt; + + for (i = 0; i < NBSTARS; i++) { + if ((starst[i] + 3) < t) { + starst[i] += 3.0; + starso[i] = 2 * ((rand() % 1000) / 1000.0) * M_PI; + } + } +#endif +#ifdef LUA + t = nt; + + lua_pushstring(L, "incstars"); + lua_gettable(L, LUA_GLOBALSINDEX); + lua_pushnumber(L, t); + lua_call(L, 1, 0); +#endif +} + +class testkey_t : public mogltk::engine::keyevent { + public: + virtual void down(SDL_keysym keysym) { + if (keysym.sym == SDLK_SPACE && !start_transit) { + start_transit = true; + transit_time = SDL_GetTicks(); + } else if (old_handler) + old_handler->down(keysym); + } + virtual void up(SDL_keysym keysym) { + if (old_handler) + old_handler->up(keysym); + } + bool start_transit; + Uint32 transit_time; +} * testkey; + +void affiche_grille(mogltk::shape * sh, mogltk::texture * tex, int grille[41][31][2]) { + int x, y, tx1, tx2, ty1, ty2; + static mogltk::ColorP w = WHITE; + + w.Bind(); + tex->Bind(); + +// sh->box(0, 0, 640, 480, BLACK); + for (y = 0; y < 30; y++) { + for (x = 0; x < 40; x++) { + tx1 = x * 20; + ty1 = y * 20; + tx2 = x * 20 + 20; + ty2 = y * 20 + 20; + +#if 1 + glBegin(GL_TRIANGLE_STRIP); + glTexCoord2i(tx1, ty1); glVertex2i(grille[x ][y ][0], grille[x ][y ][1]); + glTexCoord2i(tx2, ty1); glVertex2i(grille[x + 1][y ][0], grille[x + 1][y ][1]); + glTexCoord2i(tx1, ty2); glVertex2i(grille[x ][y + 1][0], grille[x ][y + 1][1]); + glTexCoord2i(tx2, ty2); glVertex2i(grille[x + 1][y + 1][0], grille[x + 1][y + 1][1]); + glEnd(); +#else + sh->line(grille[x ][y ][0], grille[x ][y ][1], + grille[x + 1][y ][0], grille[x + 1][y ][1]); + sh->line(grille[x + 1][y ][0], grille[x + 1][y ][1], + grille[x + 1][y + 1][0], grille[x + 1][y + 1][1]); + sh->line(grille[x + 1][y + 1][0], grille[x + 1][y + 1][1], + grille[x ][y + 1][0], grille[x ][y + 1][1]); + sh->line(grille[x ][y + 1][0], grille[x ][y + 1][1], + grille[x ][y ][0], grille[x ][y ][1]); +#endif + } + } +} + +virtual int startup() throw (GeneralException) { + bool flag = false, flag2 = false; + mogltk::texture * sshot = 0, * plastex = new mogltk::texture(1024, 512); + int x, y, sx1, sx2, sy1, sy2, lastframe, curframe, px, py, lasttick = 0, curtick = 0, oldblend, seconds = 0, newseconds = 0; + double t = 0; + verbosity = M_INFO; + String str; + bool got_transit = false; + Uint8 * plasma = (Uint8 *) malloc(640 * 480), * pplasma; + int grille[41][31][2]; + double seeds[41][31]; + double speeds[41][31]; + +#if 0 + srand(25); + for (y = 0; y < 31; y++) { + for (x = 0; x < 41; x++) { + seeds[x][y] = 1.2 * (double) (rand() % 10000) / 10000; + speeds[x][y] = 2.6 * (double) (rand() % 10000) / 10000; + } + } +#endif + + testkey = new testkey_t(); + + mogltk::engine::setmouseZ(50); + +#if 0 + +#if 0 + Lua * l = new Lua(); + LuaInput::pushconstruct(l); + LuaOutput::pushconstruct(l); + LuaBuffer::pushconstruct(l); + l->open_math(); + l->declarefunc("print", myprint); +#if 0 + l->load(&Input("particules.lua"), false); + l->dump(&Output("particules.out")); + l->call(); +#else + l->load(&Input("particules.out")); +#endif +// l->call("testing"); + delete l; +#endif + +// bdlload("cl.bdl"); + +#endif + + Input pl("plasma.raw"); + pl.read(plasma, 640 * 480); + + pplasma = (Uint8 *) plastex->GetPixels(); + + for (y = 0; y < 480; y++) { + for (x = 0; x < 640; x++) { + pplasma[((y * 1024) + x) * 4 + 3] = 255 - plasma[(y * 640) + x]; + } + } + + new Archive("datas.paq"); + +#if 1 + mogltk::base * gl = new mogltk::glbase(); + mogltk::shape * sh = new mogltk::glshape(); + mogltk::font * font = new mogltk::glfont(&Input("font-2.bin")); + mogltk::glSprite * s = new mogltk::glSprite(&Input("cursor.rgba"), 25, 25); + mogltk::texture * sol = new mogltk::texture(&Input("sol.tex")); + mogltk::glSprite * logo = new mogltk::glSprite(&Input("logo.raw"), 165, 124); +#if 0 + mogltk::Sprite * p = new mogltk::glSprite(&Input("particule.rgba"), 16, 16); + mogltk::glSprite * spr = new mogltk::glSprite(&Input("015_Over-Mind.raw"), 347, 328); + mogltk::glSprite * eD1 = new mogltk::glSprite(&Input("elkyaD1.raw"), 53, 100); + mogltk::glSprite * eG1 = new mogltk::glSprite(&Input("elkyaG1.raw"), 53, 100); + mogltk::glSprite * eM1 = new mogltk::glSprite(&Input("elkyaM1.raw"), 53, 100); +// mogltk::glSprite * spr = new mogltk::glSprite(&Input("test.raw"), 200, 200); +#endif + mogltk::widget * w = new mogltk::widgets::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::Sprite * spr = new mogltk::Sprite(&Input("015_Over-Mind.raw"), 347, 328); + mogltk::widget * w = new mogltk::Root(sh); +#endif + + mogltk::texture * ttex = 0; + + font->setcolor(WHITE); + font->setshadow(2); +#if 0 + mogltk::rect textrect; + mogltk::texture * text = font->printtex(&textrect, + "PixelPawa!\n" + "It works!!\n" + "I can't believe it!\n" + ); + SDL_SaveBMP(text->GetSurface(), "test-font.bmp"); + textrect.x = 10; + textrect.y = 30; +#endif + + mogltk::engine::setcursorvisible(true); + mogltk::engine::setappactive(true); + +#if 0 + mogltk::texture * mytex = new mogltk::texture(&Input("pattern6.tex"), true); + + Color AlphaBlue(AQUA); + AlphaBlue.A = 50; + + mogltk::fill * f = sh->fcircle(320, 240, 50); + + initstars(); + + px = 200; + py = 350; +#endif +#if 0 + new mogltk::widgets::Button(0, sh, + new mogltk::widgets::SmartBox(sh, w, 120, 50, 100, 100, "Test"), + 12, 30, 50, 20, "Toto"); +#endif + new mogltk::widgets::Frame(sh, w, 10, 10, 600, 400); + //new mogltk::widgets::MsgBox(sh, w, "Test", "Blablabla\nCeci est un test de la\nclasse MsgBox."); + + class tLabel : public mogltk::widgets::Label { + public: + tLabel(mogltk::shape * sh, mogltk::widget * father) : Label(sh, father, 250, 200, 16, 16, "I"), tick(0) { set_timed_event(500); } + virtual bool process_event(int mx, int my, mogltk::event_t event) { + if (Label::process_event(mx, my, event)) + return true; + + if (event == mogltk::E_TIMER) { + set_timed_event(500); + tick = (tick + 1) % 4; + switch (tick) { + case 0: + caption = "I"; + break; + case 1: + caption = "/"; + break; + case 2: + caption = "-"; + break; + case 3: + caption = "\\"; + break; + } + } + } + private: + int tick; + }; + + mogltk::widgets::ContextMenu * c, * c2; + //c = new mogltk::widgets::ContextMenu(sh, w, 20, 20); + mogltk::widgets::Menu * sm; + + new mogltk::widgets::SmartBox(sh, w, 250, 300, 100, 70, "SmartBox"); + sm = new mogltk::widgets::Menu(sh, w); + + sm->addnode("Toto", 0); + sm->addnode("Titi", 0); + sm->addnode("Tutu", 0); + +#if 0 + c->addnode("Test", 0); + c->addnode("Test2", 0); + c->addline(); + c->addnode("Test3", 0); + c->addnode("Test of a long entry", 0); + + c2 = c->createsub("Sub"); + c2->addnode("A sub", 0); + c2->createsub("subsub")->addnode("Toto", 0); + c2->SetEnabled(0, false); +#endif + + c = sm->createsub("Sub Menu"); + c->addnode("Blah1", 0); + c->addnode("Blah2", 0); + c->addnode("Blah3", 0); + c->addline(); + c2 = c->createsub("SubBlah"); + c2->addnode("SubBlah1", 0); + c2->addnode("SubBlah2", 0); + + sm->MoveOnTop(); + + new tLabel(sh, w); + + w->mainloop(); + + while (!mogltk::engine::quitrequested()) { +#if 0 + sx1 = 320 + 320 * sin(0.983 * t + 3.15); + sx2 = 320 + 320 * sin(0.537 * t + 5.32); + sy1 = 240 + 240 * sin(0.692 * t + 8.21); + sy2 = 240 + 240 * sin(1.029 * t + 2.42); +#endif + + gl->Enter2DMode(); + +#if 1 + if (got_transit) + goto do_transit; +#endif + +#if 0 + 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; + //sh->box(5, 5, 400, 300, CORNFLOWERBLUE, DEEPSKYBLUE, MIDNIGHTBLUE, NAVY); + mogltk::ColorP::Max.A = 255; +#if 1 + font->setshadow(1); + font->putcursor(10, 30); + mogltk::ColorP::Max.A = sy1 / 2; + font->setcolor(Color(sy1 / 2, sy1 / 2, sy1 / 2, sy1 / 2)); + font->printf( + "PixelPawa!\n" + "It works!!\n" + "I can't believe it!\n" + ); + mogltk::ColorP::Max.A = 255; + font->setcolor(WHITE); +#else + sh->tbox(text, textrect.x, textrect.y, textrect.x + textrect.w, textrect.y + textrect.h); +#endif + + 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"); + + sh->fdraw(f, BLUE); + sh->sdraw(f); + + eM1->drawrotate(500, 400, sx1); + + curtick = (SDL_GetTicks() / 250) % 4; + + if (curtick != lasttick) { + px -= 10; + py += 5; + + if (px < -53) + px = 640; + if (px > 640) + px = -53; + if (py > 480) + py = -100; + lasttick = curtick; + } + + switch(curtick) { + case 0: + eM1->draw(px, py, WHITE, 1, 1); + break; + case 1: + eD1->draw(px, py, WHITE, 1, 1); + break; + case 2: + eM1->draw(px, py, WHITE, 1, 1); + break; + case 3: + eG1->draw(px, py, WHITE, 1, 1); + break; + } + + sh->box(MIN(sx1, sx2), MIN(sy1, sy2), MAX(sx1, sx2), MAX(sy1, sy2), AlphaBlue); + mogltk::ColorP::Min.A = 200; + sh->obox(MIN(sx1, sx2), MIN(sy1, sy2), MAX(sx1, sx2), MAX(sy1, sy2), AlphaBlue); + mogltk::ColorP::Min.A = 0; + + p->draw(mogltk::engine::mouseX() - 8, mogltk::engine::mouseY() - 8); + sh->line(320, 240, mogltk::engine::mouseX(), mogltk::engine::mouseY()); + + mogltk::ColorP::Min.A = 200; + p->draw(sx1 - 8, sy1 - 8, AlphaBlue); + p->draw(sx2 - 8, sy2 - 8, AlphaBlue); + + displaystars(p); + incrementstars(t); + + sh->box(0, 0, 640, 480, CORNFLOWERBLUE, DEEPSKYBLUE, MIDNIGHTBLUE, NAVY); + +#endif +// sh->box(0, 0, 640, 480, CORNFLOWERBLUE, DEEPSKYBLUE, MIDNIGHTBLUE, NAVY); + + int x, y; + +#if 0 + for (y = 0; y < 31; y++) { + for (x = 0; x < 41; x++) { + grille[x][y][0] = x * 16 + 8 * sin(t * 6 + (x + y) / 2.0); + grille[x][y][1] = y * 16 + 8 * cos(t * 6 + (x + y) / 2.0); + } + } +#endif + double dis, xdis, ydis, xpos, ypos, norm, tt, tt1; + + t += 48; + + tt = t - (((int) t) / 58) * 58; + + if (tt <= 20) { + flag = flag2 = false; + for (y = 0; y < 31; y++) { + for (x = 0; x < 41; x++) { + xpos = x - 20.5; + ypos = y - 15.5; + norm = sqrt(xpos * xpos + ypos * ypos); + xpos /= norm; + ypos /= norm; + dis = sin(t * 6 - norm); + xdis = dis * sin(xpos); + ydis = dis * sin(ypos); + grille[x][y][0] = (((x * 16 + 16 * xdis) - 320) * 1.08) + 320; + grille[x][y][1] = (((y * 16 + 16 * ydis) - 240) * 1.08) + 240; + } + } + + affiche_grille(sh, sol, grille); + if (tt < 4) + sh->box(0, 0, 640, 480, Color(255, 255, 255, 255 * ((4 - tt) / 4))); + else if (tt > 16) + sh->box(0, 0, 640, 480, Color(0, 0, 0, 255 * ((tt - 16) / 4))); + } else if (tt <= 23) { + tt -= 20; + sh->box(0, 0, 640, 480, BLACK); + for (y = 0; y < 31; y++) { + for (x = 0; x < 41; x++) { + grille[x][y][0] = ((((x * 16 + 8 * sin(t * 6 + (x + y) / 2.0)) - 320) * 1.08) + 320) * tt / 3; + grille[x][y][1] = ((((y * 16 + 8 * cos(t * 6 + (x + y) / 2.0)) - 200) * 1.08) + 200) * tt / 3; + } + } + affiche_grille(sh, sol, grille); + } else if (tt <= 36) { + for (y = 0; y < 31; y++) { + for (x = 0; x < 41; x++) { + grille[x][y][0] = (((x * 16 + 8 * sin(t * 6 + (x + y) / 2.0)) - 320) * 1.08) + 320; + grille[x][y][1] = (((y * 16 + 8 * cos(t * 6 + (x + y) / 2.0)) - 200) * 1.08) + 200; + } + } + affiche_grille(sh, sol, grille); + } else if (tt <= 38) { + tt -= 36; + if (!flag2) { + sshot = gl->GrabTexture(); + flag2 = true; + } + sh->tbox(sshot, 0, 0, 640, 480); + if (tt <= 0.1) { + sh->box(0, 0, 640, 480, Color(255, 255, 255, 255 * (tt / 0.1))); + } else if (tt < 0.5) { + tt -= 0.1; + sh->box(0, 0, 640, 480, Color(255, 255, 255, 255 * ((0.4 - tt) / 0.4))); + sh->box(0, 0, 640, 480, Color(0.3 * 255, 0.2 * 255, 0.3 * 255, 127 * (tt / 0.4))); + } else { + sh->box(0, 0, 640, 480, Color(0.3 * 255, 0.2 * 255, 0.3 * 255, 127)); + } + for (y = 0; y < 15; y++) { + for (x = 0; x < 20; x++) { + sh->obox(x * 32, y * 32, x * 32 + 32, y * 32 + 32, WHITE); + } + } + } else if (tt <= 42) { + int xx, yy, tyy; + double ttt; + tt -= 38; + sh->box(0, 0, 640, 480, BLACK); + for (y = 14; y >= 0; y--) { + for (x = 19; x >= 0; x--) { + double d = (33.0 - x - y) / 16.5; + xx = x * 32; + tyy = y * 32; + if (tt <= d) { + yy = y * 32; + } else { + ttt = tt - d; + yy = y * 32 + ttt * ttt * ttt * 60; + } + sh->tbox(sshot, xx, yy, xx + 32, yy + 32, xx, tyy, xx + 32, tyy + 32); + sh->box(xx, yy, xx + 32, yy + 32, Color(0.3 * 255, 0.2 * 255, 0.3 * 255, 127)); + sh->obox(xx, yy, xx + 32, yy + 32, WHITE); + } + } + } else if (tt <= 43) { + sh->box(0, 0, 640, 480, BLACK); + } else if (tt <= 48) { + tt -= 43; + sh->tbox(sol, 0, 0, 640, 480, 0, 0, 800, 600); + glDisable(GL_BLEND); + glEnable(GL_ALPHA_TEST); + glAlphaFunc(GL_GREATER, tt / 5); + sh->tbox(plastex, 0, 0, 640, 480); + glDisable(GL_ALPHA_TEST); + glEnable(GL_BLEND); + } else if (tt <= 56) { + sh->tbox(sol, 0, 0, 640, 480, 0, 0, 800, 600); + } else if (tt <= 58) { + if (!flag) { + if (sshot) + delete sshot; + testkey->start_transit = true; + testkey->transit_time = SDL_GetTicks(); + flag = true; + } else { + glBlendFunc(GL_SRC_ALPHA, GL_ONE); + sh->box(0, 0, 640, 480, Color(255, 255, 255, 1)); + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + } + } + + logo->draw(10, 10, Color(255, 255, 255, 100)); + + + font->putcursor(530, 460); + 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", SDL_GetTicks() / 1000.0); +// mogltk::ColorP::Max.A = 50; +// s->draw(mogltk::engine::mouseX() - 6, mogltk::engine::mouseY() - 3, BLACK); +// mogltk::ColorP::Max.A = 255; +// s->draw(mogltk::engine::mouseX() - 8, mogltk::engine::mouseY() - 6); + +do_transit: + mogltk::ColorP::Min.A = 0; + mogltk::ColorP::Max.A = 255; + if (testkey->start_transit) { + double t; + int x, y; + Uint8 * pixels; + if (!got_transit) { + lastframe = 0; + got_transit = true; + ttex = gl->GrabTexture(); + pixels = (Uint8 *) ttex->GetPixels(); + oldblend = 20; +#if 1 + for (y = 0; y < 480; y++) { + for (x = 0; x < 640; x++) { +#if 0 + pixels[(y * 1024 + x) * 4 + 0] ^= 255; + pixels[(y * 1024 + x) * 4 + 1] ^= 255; + pixels[(y * 1024 + x) * 4 + 2] ^= 255; +#endif +#if 0 + pixels[(y * 1024 + x) * 4 + 3] = plasma[y * 640 + x]; +#endif +#if 1 + pixels[(y * 1024 + x) * 4 + 3] = 30; +#endif +#if 0 + pixels[(y * 1024 + x) * 4 + 3] = 7; +#endif +#if 0 + pixels[(y * 1024 + x) * 4 + 3] = 1; +#endif + } + } +#endif + } + pixels = (Uint8 *) ttex->GetPixels(); + t = (double) (SDL_GetTicks() - testkey->transit_time) / TRTIME; + curframe = 45 * (SDL_GetTicks() - testkey->transit_time) / TRTIME; + if (t >= 1.0) { + testkey->start_transit = false; + } +#if 0 + mogltk::ColorP::Max.A = 255 * (TRTIME - SDL_GetTicks() + testkey->transit_time) / TRTIME; + mogltk::ColorP::Min.R = 255 * (SDL_GetTicks() - testkey->transit_time) / TRTIME; + mogltk::ColorP::Min.G = 255 * (SDL_GetTicks() - testkey->transit_time) / TRTIME; + mogltk::ColorP::Min.B = 255 * (SDL_GetTicks() - testkey->transit_time) / TRTIME; + sh->tbox(ttex, 0, 0, 640, 480); + mogltk::ColorP::Max.A = 255; + mogltk::ColorP::Min.R = 0; + mogltk::ColorP::Min.G = 0; + mogltk::ColorP::Min.B = 0; +#endif +#if 0 + for (y = 0; y < 10; y++) { + for (x = 0; x < 10; x++) { + int cx, cy, tx1, ty1, tx2, ty2; + cx = x * 64 + 32; + cy = y * 48 + 24; + tx1 = x * 64; + ty1 = y * 48; + tx2 = x * 64 + 64; + ty2 = y * 48 + 48; + glMatrixMode(GL_MODELVIEW); + glPushMatrix(); + glTranslated(cx, cy, 0); + glScaled((1 - t), (1 - t), 1); + glRotated((t + ((x + y) / 30)) * 400, 0, 0, 1); + sh->tbox(ttex, -32, -24, 32, 24, tx1, ty1, tx2, ty2); + glPopMatrix(); + } + } +#endif +#if 0 + glDisable(GL_BLEND); + glEnable(GL_ALPHA_TEST); + glAlphaFunc(GL_GREATER, t); + sh->tbox(ttex, 0, 0, 640, 480); + glDisable(GL_ALPHA_TEST); + glEnable(GL_BLEND); +#endif +#if 1 + if (curframe > lastframe) { +#if 1 + sh->box(0, 0, 640, 480, Color(40, 40, 40, 12)); + glBlendFunc(GL_SRC_ALPHA, GL_ONE); + glPushMatrix(); + glTranslated(320, 240, 0); + glScaled(1 + 2 * t, 1 + 2 * t, 1); + glRotated(t * 100, 0, 0, 1); + sh->tbox(ttex, -320, -240, 320, 240); + glPopMatrix(); + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); +#endif +#if 0 + sh->box(0, 0, 640, 480, Color(25, 25, 25, 20)); + glBlendFunc(GL_SRC_ALPHA, GL_ONE); + glPushMatrix(); + glTranslated(320, 240, 0); + glTranslated(curframe, 0, 0); sh->tbox(ttex, -320, -240, 320, 240); + glTranslated(0, curframe, 0); sh->tbox(ttex, -320, -240, 320, 240); + glTranslated(-curframe, 0, 0); sh->tbox(ttex, -320, -240, 320, 240); + glTranslated(-curframe, 0, 0); sh->tbox(ttex, -320, -240, 320, 240); + glTranslated(0, -curframe, 0); sh->tbox(ttex, -320, -240, 320, 240); + glTranslated(0, -curframe, 0); sh->tbox(ttex, -320, -240, 320, 240); + glTranslated(curframe, 0, 0); sh->tbox(ttex, -320, -240, 320, 240); + glTranslated(curframe, 0, 0); sh->tbox(ttex, -320, -240, 320, 240); + glPopMatrix(); + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); +#endif +#if 0 + int x0 = 320, y0 = 240; + int x = 0; + int y = t * 15 - 1; + int d = 3 - 2 * t * 15; + int dI = 10 - 4 * t * 15; + int rI = 6; + + sh->box(0, 0, 640, 480, Color(25, 25, 25, oldblend)); + glBlendFunc(GL_SRC_ALPHA, GL_ONE); + oldblend = 0; + while (x <= y) { + oldblend += 16; + glPushMatrix(); glTranslated(x0 + x, y0 + y, 0); sh->tbox(ttex, -320, -240, 320, 240); glPopMatrix(); + glPushMatrix(); glTranslated(x0 - x, y0 + y, 0); sh->tbox(ttex, -320, -240, 320, 240); glPopMatrix(); + glPushMatrix(); glTranslated(x0 + x, y0 - y, 0); sh->tbox(ttex, -320, -240, 320, 240); glPopMatrix(); + glPushMatrix(); glTranslated(x0 - x, y0 - y, 0); sh->tbox(ttex, -320, -240, 320, 240); glPopMatrix(); + glPushMatrix(); glTranslated(x0 + y, y0 + x, 0); sh->tbox(ttex, -320, -240, 320, 240); glPopMatrix(); + glPushMatrix(); glTranslated(x0 - y, y0 + x, 0); sh->tbox(ttex, -320, -240, 320, 240); glPopMatrix(); + glPushMatrix(); glTranslated(x0 + y, y0 - x, 0); sh->tbox(ttex, -320, -240, 320, 240); glPopMatrix(); + glPushMatrix(); glTranslated(x0 - y, y0 - x, 0); sh->tbox(ttex, -320, -240, 320, 240); glPopMatrix(); + if (d >= 0) { + d += dI; + dI += 8; + y -= 1; + } else { + d += rI; + dI += 4; + } + rI += 4; + x += 1; + } + printm(M_INFO, "blends = %i\n", oldblend / 2); + oldblend += 10; + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); +#endif +#if 0 + sh->box(0, 0, 640, 480, Color(40, 40, 40, 12)); + glBlendFunc(GL_SRC_ALPHA, GL_ONE); + glPushMatrix(); + glTranslated(320, 240, 0); + glRotated(t * 25, 0, 0, 1); + sh->tbox(ttex, -320, -240, 320, 240); + glPopMatrix(); + glPushMatrix(); + glTranslated(320, 240, 0); + glRotated(-t * 25, 0, 0, 1); + sh->tbox(ttex, -320, -240, 320, 240); + glPopMatrix(); + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); +#endif + lastframe = curframe; + } +#endif + } + if (!testkey->start_transit && ttex) { + delete ttex; + ttex = 0; + got_transit = false; + } + + gl->Leave2DMode(); + + //gl->Flip(got_transit ? false : true); + gl->Flip(false); + t = (double) SDL_GetTicks() / 1000.0; + newseconds = t; + + if (newseconds != seconds) { + printm(M_INFO, "FPS: %.2f\n", mogltk::engine::FPS()); + seconds = newseconds; + } + } + +// delete mytex; + + return 0; +} +CODE_ENDS + +#else + +#if 0 +CODE_BEGINS +virtual int startup() throw (GeneralException) { + unsigned char palette[256][3], * pixels, c; + char outname[256]; + int width, height, x, y; + + if (argc != 2) + exit(-1); + Input i(argv[1]); + SDL_Init(SDL_INIT_VIDEO); + atexit(SDL_Quit); + width = i.readU16(); + height = i.readU16(); + i.seek(12, SEEK_CUR); + SDL_Surface * s = SDL_SetVideoMode(width, height, 24, 0); + pixels = (unsigned char *) s->pixels; + for (x = 0; x < 256; x++) { + for (y = 0; y < 3; y++) { + palette[x][y] = i.readU8(); + } + i.seek(1, SEEK_CUR); + } + for (y = 0; y < height; y++) { + for (x = 0; x < width; x++) { + c = i.readU8(); + pixels[(y * width + x) * 3 + 0] = palette[c][2]; + pixels[(y * width + x) * 3 + 1] = palette[c][1]; + pixels[(y * width + x) * 3 + 2] = palette[c][0]; + } + } + + *strchr(argv[1], '.') = 0; + + sprintf(outname, "%s.bmp", argv[1]); + + SDL_SaveBMP(s, outname); + + SDL_Flip(s); + + SDL_Event event; + + while(true) { + SDL_PollEvent(&event); + switch (event.type) { + case SDL_QUIT: + exit(0); + } + } + + return 0; +} +CODE_ENDS + +#else +CODE_BEGINS +virtual int startup() throw (GeneralException) { + unsigned char * pixels, c; + char outname[256]; + int width, height, x, y; + + if (argc != 2) + exit(-1); + Input i(argv[1]); + SDL_Init(SDL_INIT_VIDEO); + atexit(SDL_Quit); + i.seek(4, SEEK_SET); + i.read(&width, 4); + i.read(&height, 4); + i.seek(4, SEEK_CUR); + SDL_Surface * _s = SDL_SetVideoMode(width, height, 32, 0); + SDL_Surface * s = SDL_CreateRGBSurface(SDL_SWSURFACE, width, height, 32, + 0x000000ff, + 0x0000ff00, + 0x00ff0000, + 0xff000000); + + pixels = (unsigned char *) s->pixels; + *strchr(argv[1], '.') = 0; + + i.read(pixels, width * height * 4); + + sprintf(outname, "%s.bmp", argv[1]); + + SDL_SaveBMP(s, outname); + SDL_BlitSurface(s, 0, _s, 0); + + SDL_Flip(_s); + + SDL_Event event; + + while(true) { + SDL_PollEvent(&event); + switch (event.type) { + case SDL_QUIT: + exit(0); + } + } + + return 0; +} +CODE_ENDS + +#endif + +#endif |