window.H

00001 /*
00002  * This file is part of the "Archon" framework.
00003  * (http://files3d.sourceforge.net)
00004  *
00005  * Copyright © 2002 by Kristian Spangsege and Brian Kristiansen.
00006  *
00007  * Permission to use, copy, modify, and distribute this software and
00008  * its documentation under the terms of the GNU General Public License is
00009  * hereby granted. No representations are made about the suitability of
00010  * this software for any purpose. It is provided "as is" without express
00011  * or implied warranty. See the GNU General Public License
00012  * (http://www.gnu.org/copyleft/gpl.html) for more details.
00013  *
00014  * The characters in this file are ISO8859-1 encoded.
00015  *
00016  * The documentation in this file is in "Doxygen" style
00017  * (http://www.doxygen.org).
00018  */
00019 
00020 #ifndef ARCHON_UTILITIES_WINDOW_H
00021 #define ARCHON_UTILITIES_WINDOW_H
00022 
00023 #include <string>
00024 
00025 #include <archon/util/exception.H>
00026 #include <archon/util/ref.H>
00027 #include <archon/util/window_keys.H>
00028 
00029 namespace Archon
00030 {
00031   namespace Utilities
00032   {
00033     using namespace std;
00034 
00035     class Window
00036     {
00037     public:
00038       struct KeyboardHandler
00039       {
00040         virtual void press(Key, unsigned long milliSecondTime) = 0;
00041         virtual void release(Key) = 0;
00042         virtual ~KeyboardHandler() {}
00043       };
00044 
00045       struct MouseHandler
00046       {
00047         virtual void press(unsigned button, unsigned x, unsigned y, unsigned long milliSecondTime) = 0;
00048         virtual void release(unsigned button, unsigned x, unsigned y) = 0;
00049         virtual void motion(unsigned xAbs, unsigned yAbs, int xRel, int yRel) = 0;
00050         virtual ~MouseHandler() {}
00051       };
00052 
00053       struct WindowHandler
00054       {
00055         virtual void quit() = 0;
00056         virtual ~WindowHandler() {}
00057       };
00058 
00059       enum DrawMode { set, add, sub };
00060 
00061       /* Mouse cursor example:
00062 
00063       //XPM
00064 
00065       static const char *arrow[] =
00066       {
00067       //width height num_colors chars_per_pixel
00068       "    32    32        3            1",
00069       //colors
00070       "X c #000000",
00071       ". c #ffffff",
00072       "  c None",
00073       //pixels
00074       "X                               ",
00075       "XX                              ",
00076       "X.X                             ",
00077       "X..X                            ",
00078       "X...X                           ",
00079       "X....X                          ",
00080       "X.....X                         ",
00081       "X......X                        ",
00082       "X.......X                       ",
00083       "X........X                      ",
00084       "X.....XXXXX                     ",
00085       "X..X..X                         ",
00086       "X.X X..X                        ",
00087       "XX  X..X                        ",
00088       "X    X..X                       ",
00089       "     X..X                       ",
00090       "      X..X                      ",
00091       "      X..X                      ",
00092       "       XX                       ",
00093       "                                ",
00094       "                                ",
00095       "                                ",
00096       "                                ",
00097       "                                ",
00098       "                                ",
00099       "                                ",
00100       "                                ",
00101       "                                ",
00102       "                                ",
00103       "                                ",
00104       "                                ",
00105       "                                ",
00106       "0,0"
00107       };
00108 
00109       */
00110 
00111       class MouseCursor: public virtual RefObjectBase
00112       {
00113         friend class Window;
00114         void *data; // SDL_Cursor
00115         MouseCursor(const char *image[]); // See example
00116       public:
00117         ~MouseCursor();
00118       };
00119 
00120     private:
00121       void *dataHolder;
00122       DrawMode drawMode;
00123       unsigned long drawColor;
00124       Key *keyXlat;
00125       KeyboardHandler *keyboardHandler;
00126       MouseHandler *mouseHandler;
00127       WindowHandler *windowHandler;
00128       void *defaultMouseCursor;
00129       int mouseX;
00130       int mouseY;
00131     
00132       unsigned long combineRGBA(unsigned r, unsigned g,
00133                                 unsigned b, unsigned a);
00134 
00135       void initializeKeyXlat();
00136 
00137     public:
00145       Window(unsigned width,
00146              unsigned height,
00147              unsigned bitsPerPixel = 24,
00148              bool fullScreen = false,
00149              bool openGlSupport = false,
00150              KeyboardHandler * = 0,
00151              MouseHandler * = 0,
00152              WindowHandler * = 0);
00153       ~Window();
00154 
00155       Ref<MouseCursor> newMouseCursor(const char *image[]);
00156       void setMouseCursor(Ref<MouseCursor>);
00157       void setDefaultMouseCursor();
00158       void showMouseCursor();
00159       void hideMouseCursor();
00160 
00161 
00162       // The mouse position is only updated when calling executeEventQueue
00163       int getMouseX() const { return mouseX; }
00164       int getMouseY() const { return mouseY; }
00165 
00166       void swapBuffers(); // Use in open GL context
00167 
00168       void discardEventQueue();
00169       void executeEventQueue(unsigned long milliSecondTime);
00170 
00171       unsigned getWidth()  const;
00172       unsigned getHeight() const;
00173 
00174       void setDrawMode(DrawMode mode) { drawMode = mode; }
00175       void setColor(unsigned red,
00176                     unsigned green,
00177                     unsigned blue,
00178                     unsigned alpha = 255)
00179       {
00180         drawColor = combineRGBA(red, green, blue, alpha);
00181       }
00182 
00183       struct Update
00184       {
00185         Update(Window *);
00186         ~Update();
00187 
00188         void clear();
00189 
00190 
00191 
00192         void drawPoint(unsigned x, unsigned y)
00193         {
00194           if(x >= width || y >= height)
00195             ARCHON_THROW1(ArgumentException,
00196                           "coordinates out of range");
00197           drawPoint(x, y, drawMode, drawColor);
00198         }
00199 
00200         void drawPoint(unsigned x, unsigned y, DrawMode mode)
00201         {
00202           if(x >= width || y >= height)
00203             ARCHON_THROW1(ArgumentException,
00204                           "coordinates out of range");
00205           drawPoint(x, y, mode, drawColor);
00206         }
00207 
00208         void drawPoint(unsigned x, unsigned y, DrawMode mode,
00209                        unsigned red, unsigned green,
00210                        unsigned blue, unsigned alpha = 255)
00211         {
00212           if(x >= width || y >= height)
00213             ARCHON_THROW1(ArgumentException,
00214                           "coordinates out of range");
00215           drawPoint(x, y, mode, frame->combineRGBA(red, green, blue, alpha));
00216         }
00217 
00218 
00219 
00220         void drawLine(unsigned x1, unsigned y1, unsigned x2, unsigned y2)
00221         {
00222           if(x1 >= width || y1 >= height || x2 >= width || y2 >= height)
00223             ARCHON_THROW1(ArgumentException,
00224                           "coordinates out of range");
00225           drawLine(x1, y1, x2, y2, drawMode, drawColor);
00226         }
00227 
00228         void drawLine(unsigned x1, unsigned y1, unsigned x2, unsigned y2,
00229                       DrawMode mode)
00230         {
00231           if(x1 >= width || y1 >= height || x2 >= width || y2 >= height)
00232             ARCHON_THROW1(ArgumentException,
00233                           "coordinates out of range");
00234           drawLine(x1, y1, x2, y2, mode, drawColor);
00235         }
00236 
00237         void drawLine(unsigned x1, unsigned y1, unsigned x2, unsigned y2,
00238                       unsigned red, unsigned green,
00239                       unsigned blue, unsigned alpha = 255)
00240         {
00241           if(x1 >= width || y1 >= height || x2 >= width || y2 >= height)
00242             ARCHON_THROW1(ArgumentException,
00243                           "coordinates out of range");
00244           drawLine(x1, y1, x2, y2, drawMode,
00245                    frame->combineRGBA(red, green, blue, alpha));
00246         }
00247 
00248       private:
00249         const unsigned bytesPerPixel;
00250         const unsigned bytesPerScanline;
00251         void *const pixels;
00252         const unsigned width;
00253         const unsigned height;
00254         const DrawMode drawMode;
00255         const unsigned long drawColor;
00256         Window *const frame;
00257 
00258         void drawPoint(unsigned x, unsigned y,
00259                        DrawMode mode, unsigned long color);
00260         void drawLine(unsigned x1, unsigned y1, unsigned x2, unsigned y2,
00261                       DrawMode mode, unsigned long color);
00262       };
00263 
00264       friend struct Update;
00265     };
00266   }
00267 }
00268 
00269 #endif // ARCHON_UTILITIES_WINDOW_H

Generated on Sun Jul 30 22:55:43 2006 for Archon by  doxygen 1.4.4