]> git.tdb.fi Git - libs/gui.git/commitdiff
Move Image from mspgl to here
authorMikko Rasa <tdb@tdb.fi>
Sun, 17 Aug 2008 21:34:32 +0000 (21:34 +0000)
committerMikko Rasa <tdb@tdb.fi>
Sun, 17 Aug 2008 21:34:32 +0000 (21:34 +0000)
Support hiding the cursor
Update svn:ignore

Build
source/gbase/image.cpp [new file with mode: 0644]
source/gbase/image.h [new file with mode: 0644]
source/gbase/pixelformat.h [new file with mode: 0644]
source/gbase/window.cpp
source/gbase/window.h

diff --git a/Build b/Build
index e68abea116a842c0bf562d9194db0234ade3c915..8c834be037858b481c49574b05dedb50ed5a7b61 100644 (file)
--- a/Build
+++ b/Build
@@ -25,6 +25,12 @@ package "mspgbase"
                };
        };
 
+       feature "devil" "Include DevIL support for loading image files";
+       if "with_devil"
+       {
+               require "devil";
+       };
+
        headers "gbase"
        {
                source "source/gbase";
diff --git a/source/gbase/image.cpp b/source/gbase/image.cpp
new file mode 100644 (file)
index 0000000..9953de7
--- /dev/null
@@ -0,0 +1,114 @@
+/* $Id$
+
+This file is part of libmspgbase
+Copyright © 2007-2008  Mikko Rasa, Mikkosoft Productions
+Distributed under the LGPL
+*/
+
+#ifdef WITH_DEVIL
+#include <IL/il.h>
+#endif
+#include <msp/core/except.h>
+#include "image.h"
+
+using namespace std;
+
+namespace Msp {
+namespace Graphics {
+
+Image::Image()
+{
+#ifdef WITH_DEVIL
+       static bool init_done=false;
+
+       if(!init_done)
+       {
+               ilInit();
+               ilEnable(IL_ORIGIN_SET);
+               ilOriginFunc(IL_ORIGIN_LOWER_LEFT);
+               init_done=true;
+       }
+
+       ilGenImages(1, &id);
+#else
+       throw Exception("DevIL support not compiled in");
+#endif
+}
+
+Image::~Image()
+{
+#ifdef WITH_DEVIL
+       ilDeleteImages(1, &id);
+#endif
+}
+
+void Image::load_file(const string &fn)
+{
+#ifdef WITH_DEVIL
+       ilBindImage(id);
+       if(!ilLoadImage(const_cast<char *>(fn.c_str())))
+               throw Exception("Error loading image "+fn);
+#else
+       (void)fn;
+#endif
+}
+
+void Image::load_memory(const void *data, unsigned size)
+{
+#ifdef WITH_DEVIL
+       ilBindImage(id);
+       if(!ilLoadL(IL_TYPE_UNKNOWN, const_cast<void *>(data), size))
+               throw Exception("Error loading image from lump");
+#else
+       (void)data; (void)size;
+#endif
+}
+
+PixelFormat Image::get_format() const
+{
+#ifdef WITH_DEVIL
+       switch(ilGetInteger(IL_IMAGE_FORMAT))
+       {
+       case IL_COLOR_INDEX: return COLOR_INDEX;
+       case IL_LUMINANCE: return LUMINANCE;
+       case IL_LUMINANCE_ALPHA: return LUMINANCE_ALPHA;
+       case IL_RGB: return RGB;
+       case IL_RGBA: return RGBA;
+       case IL_BGR: return BGR;
+       case IL_BGRA: return BGRA;
+       default: throw InvalidParameterValue("Unknown pixel format in image");
+       }
+#else
+       return RGB;
+#endif
+}
+
+unsigned Image::get_width() const
+{
+#ifdef WITH_DEVIL
+       return ilGetInteger(IL_IMAGE_WIDTH);
+#else
+       return 0;
+#endif
+}
+
+unsigned Image::get_height() const
+{
+#ifdef WITH_DEVIL
+       return ilGetInteger(IL_IMAGE_HEIGHT);
+#else
+       return 0;
+#endif
+}
+
+const void *Image::get_data() const
+{
+#ifdef WITH_DEVIL
+       return ilGetData();
+#else
+       return 0;
+#endif
+}
+
+} // namespace Graphics
+} // namespace Msp
diff --git a/source/gbase/image.h b/source/gbase/image.h
new file mode 100644 (file)
index 0000000..da04b18
--- /dev/null
@@ -0,0 +1,37 @@
+/* $Id$
+
+This file is part of libmspgbase
+Copyright © 2007-2008  Mikko Rasa, Mikkosoft Productions
+Distributed under the LGPL
+*/
+
+#ifndef MSP_GBASE_IMAGE_H_
+#define MSP_GBASE_IMAGE_H_
+
+#include <string>
+#include "pixelformat.h"
+
+namespace Msp {
+namespace Graphics {
+
+class Image
+{
+private:
+       unsigned id;
+
+public:
+       Image();
+       ~Image();
+
+       void load_file(const std::string &);
+       void load_memory(const void *, unsigned);
+       PixelFormat get_format() const;
+       unsigned get_width() const;
+       unsigned get_height() const;
+       const void *get_data() const;
+};
+
+} // namespace Graphics
+} // namespace Msp
+
+#endif
diff --git a/source/gbase/pixelformat.h b/source/gbase/pixelformat.h
new file mode 100644 (file)
index 0000000..01572d4
--- /dev/null
@@ -0,0 +1,28 @@
+/* $Id$
+
+This file is part of libmspgl
+Copyright © 2008  Mikko Rasa, Mikkosoft Productions
+Distributed under the LGPL
+*/
+
+#ifndef MSP_GBASE_PIXELFORMAT_H_
+#define MSP_GBASE_PIXELFORMAT_H_
+
+namespace Msp {
+namespace Graphics {
+
+enum PixelFormat
+{
+       COLOR_INDEX,
+       LUMINANCE,
+       LUMINANCE_ALPHA,
+       RGB,
+       RGBA,
+       BGR,
+       BGRA
+};
+
+} // namespace Graphics
+} // namespace Msp
+
+#endif
index 3ba8d767a90e9edf5b9392bd2ef5d3d5b4f62e2d..9f2361e4e3c90ab93243294d0d206cf648e15ffb 100644 (file)
@@ -54,6 +54,9 @@ Window::~Window()
                CloseWindow(window);
 #else
                XDestroyWindow(display.get_display(), window);
+
+       if(invisible_cursor)
+               XFreeCursor(display.get_display(), invisible_cursor);
 #endif
 
        display.remove_window(this);
@@ -143,6 +146,43 @@ void Window::reconfigure(const WindowOptions &opts)
                display.restore_mode();
 }
 
+void Window::show_cursor(bool s)
+{
+#ifdef WIN32
+       ShowCursor(s);
+#else
+       ::Display *dpy=display.get_display();
+
+       if(s)
+               XUndefineCursor(dpy, window);
+       else
+       {
+               if(!invisible_cursor)
+               {
+                       int screen=DefaultScreen(dpy);
+                       char data=0;
+                       XImage *img=XCreateImage(dpy, DefaultVisual(dpy, screen), 1, XYBitmap, 0, &data, 1, 1, 8, 1);
+
+                       Pixmap pm=XCreatePixmap(dpy, window, 1, 1, 1);
+                       GC gc=XCreateGC(dpy, pm, 0, 0);
+                       XPutImage(dpy, pm, gc, img, 0, 0, 0, 0, 1, 1);
+
+                       XColor black;
+                       black.pixel=BlackPixel(dpy, screen);
+                       XQueryColor(dpy, DefaultColormap(dpy, screen), &black);
+
+                       invisible_cursor=XCreatePixmapCursor(dpy, pm, pm, &black, &black, 0, 0);
+
+                       XFreeGC(dpy, gc);
+                       XFreePixmap(dpy, pm);
+                       img->data=0;
+                       XDestroyImage(img);
+               }
+               XDefineCursor(dpy, window, invisible_cursor);
+       }
+#endif
+}
+
 void Window::show()
 {
 #ifdef WIN32
@@ -220,6 +260,7 @@ void Window::init()
        ::Display *dpy=display.get_display();
 
        wm_delete_window=XInternAtom(dpy, "WM_DELETE_WINDOW", true);
+       invisible_cursor=0;
 
        XSetWindowAttributes attr;
        attr.override_redirect=options.fullscreen;
index e85f86ac7128e522e7869d9a9faec8f5e811b7f7..f422296f8d3be7e98f6a33ad31932d9197b3b85f 100644 (file)
@@ -44,6 +44,7 @@ protected:
        WindowHandle window;
 #ifndef WIN32
        Atom wm_delete_window;
+       Cursor invisible_cursor;
 #endif
 
 public:
@@ -53,6 +54,7 @@ public:
 
        void set_title(const std::string &);
        void reconfigure(const WindowOptions &);
+       void show_cursor(bool);
 
        Display &get_display() const { return display; }
        const WindowOptions &get_options() const { return options; }