From: Mikko Rasa Date: Sun, 17 Aug 2008 21:34:32 +0000 (+0000) Subject: Move Image from mspgl to here X-Git-Tag: 0.9~1 X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=aeb2546fc11a7e27a02a47e85756746330253742;p=libs%2Fgui.git Move Image from mspgl to here Support hiding the cursor Update svn:ignore --- diff --git a/Build b/Build index e68abea..8c834be 100644 --- 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 index 0000000..9953de7 --- /dev/null +++ b/source/gbase/image.cpp @@ -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 +#endif +#include +#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(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(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 index 0000000..da04b18 --- /dev/null +++ b/source/gbase/image.h @@ -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 +#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 index 0000000..01572d4 --- /dev/null +++ b/source/gbase/pixelformat.h @@ -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 diff --git a/source/gbase/window.cpp b/source/gbase/window.cpp index 3ba8d76..9f2361e 100644 --- a/source/gbase/window.cpp +++ b/source/gbase/window.cpp @@ -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; diff --git a/source/gbase/window.h b/source/gbase/window.h index e85f86a..f422296 100644 --- a/source/gbase/window.h +++ b/source/gbase/window.h @@ -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; }