From 0498e6e5f0fe36cc4a211733c15f97e4e4995660 Mon Sep 17 00:00:00 2001 From: scrawl Date: Wed, 13 May 2015 15:03:21 +0200 Subject: [PATCH] Readded window icon --- apps/openmw/engine.cpp | 26 +++++++++++++++++++++- apps/openmw/engine.hpp | 1 + components/CMakeLists.txt | 2 +- components/sdlutil/imagetosurface.cpp | 28 ++++++++++++++++++++++++ components/sdlutil/imagetosurface.hpp | 20 +++++++++++++++++ components/sdlutil/sdlgraphicswindow.cpp | 2 +- components/sdlutil/sdlgraphicswindow.hpp | 4 ++-- 7 files changed, 78 insertions(+), 5 deletions(-) create mode 100644 components/sdlutil/imagetosurface.cpp create mode 100644 components/sdlutil/imagetosurface.hpp diff --git a/apps/openmw/engine.cpp b/apps/openmw/engine.cpp index 9270b07e81..7f98f6ad7f 100644 --- a/apps/openmw/engine.cpp +++ b/apps/openmw/engine.cpp @@ -3,8 +3,11 @@ #include #include +#include + #include #include +#include #include @@ -14,6 +17,7 @@ #include #include +#include #include #include @@ -346,7 +350,7 @@ void OMW::Engine::createWindow(Settings::Manager& settings) return; } - // TODO: set window icon + setWindowIcon(); SDLUtil::setupWindowingSystemInterface(); @@ -377,6 +381,26 @@ void OMW::Engine::createWindow(Settings::Manager& settings) mViewer->realize(); } +void OMW::Engine::setWindowIcon() +{ + boost::filesystem::ifstream windowIconStream; + std::string windowIcon = (mResDir / "mygui" / "openmw.png").string(); + windowIconStream.open(windowIcon); + if (windowIconStream.fail()) + std::cerr << "Failed to open " << windowIcon << std::endl; + osgDB::ReaderWriter* reader = osgDB::Registry::instance()->getReaderWriterForExtension("png"); + osgDB::ReaderWriter::ReadResult result = reader->readImage(windowIconStream); + if (!result.success()) + std::cerr << "Failed to read " << windowIcon << ": " << result.message() << std::endl; + else + { + osg::ref_ptr image = result.getImage(); + SDL_Surface* surface = SDLUtil::imageToSurface(image); + SDL_SetWindowIcon(mWindow, surface); + SDL_FreeSurface(surface); + } +} + void OMW::Engine::prepareEngine (Settings::Manager & settings) { mEnvironment.setStateManager ( diff --git a/apps/openmw/engine.hpp b/apps/openmw/engine.hpp index 420121a8e3..bb70c288d8 100644 --- a/apps/openmw/engine.hpp +++ b/apps/openmw/engine.hpp @@ -116,6 +116,7 @@ namespace OMW void prepareEngine (Settings::Manager & settings); void createWindow(Settings::Manager& settings); + void setWindowIcon(); public: Engine(Files::ConfigurationManager& configurationManager); diff --git a/components/CMakeLists.txt b/components/CMakeLists.txt index d62ff3f009..e18871a64e 100644 --- a/components/CMakeLists.txt +++ b/components/CMakeLists.txt @@ -127,7 +127,7 @@ add_component_dir (fontloader ) add_component_dir (sdlutil - sdlgraphicswindow + sdlgraphicswindow imagetosurface ) add_component_dir (version diff --git a/components/sdlutil/imagetosurface.cpp b/components/sdlutil/imagetosurface.cpp new file mode 100644 index 0000000000..36e503c749 --- /dev/null +++ b/components/sdlutil/imagetosurface.cpp @@ -0,0 +1,28 @@ +#include "imagetosurface.hpp" + +#include +#include + +namespace SDLUtil +{ + +SDL_Surface* imageToSurface(osg::Image *image) +{ + int width = image->s(); + int height = image->t(); + SDL_Surface* surface = SDL_CreateRGBSurface(0, width, height, 32, 0xFF000000,0x00FF0000,0x0000FF00,0x000000FF); + + for(int x = 0; x < width; ++x) + for(int y = 0; y < height; ++y) + { + osg::Vec4f clr = image->getColor(x, (height-1)-y); + int bpp = surface->format->BytesPerPixel; + Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp; + *(Uint32*)(p) = SDL_MapRGBA(surface->format, static_cast(clr.r() * 255), + static_cast(clr.g() * 255), static_cast(clr.b() * 255), static_cast(clr.a() * 255)); + } + + return surface; +} + +} diff --git a/components/sdlutil/imagetosurface.hpp b/components/sdlutil/imagetosurface.hpp new file mode 100644 index 0000000000..2c5df5cbd5 --- /dev/null +++ b/components/sdlutil/imagetosurface.hpp @@ -0,0 +1,20 @@ +#ifndef OPENMW_COMPONENTS_SDLUTIL_IMAGETOSURFACE_H +#define OPENMW_COMPONENTS_SDLUTIL_IMAGETOSURFACE_H + +struct SDL_Surface; + +namespace osg +{ + class Image; +} + +namespace SDLUtil +{ + + /// Convert an osg::Image to an SDL_Surface. + /// @note The returned surface must be freed using SDL_FreeSurface. + SDL_Surface* imageToSurface(osg::Image* image); + +} + +#endif diff --git a/components/sdlutil/sdlgraphicswindow.cpp b/components/sdlutil/sdlgraphicswindow.cpp index 5a9db5923e..1424337ab6 100644 --- a/components/sdlutil/sdlgraphicswindow.cpp +++ b/components/sdlutil/sdlgraphicswindow.cpp @@ -269,4 +269,4 @@ void setupWindowingSystemInterface() osg::GraphicsContext::setWindowingSystemInterface(new SDL2WindowingSystemInterface); } -} // namespace TK +} diff --git a/components/sdlutil/sdlgraphicswindow.hpp b/components/sdlutil/sdlgraphicswindow.hpp index 27b9e8e288..cfe40d98e9 100644 --- a/components/sdlutil/sdlgraphicswindow.hpp +++ b/components/sdlutil/sdlgraphicswindow.hpp @@ -91,7 +91,7 @@ public: /** Set mouse cursor to a specific shape.*/ virtual void setCursor(MouseCursor cursor); - /** WindowData is used to pass in the SDL2 window handle attached the GraphicsContext::Traits structure. */ + /** WindowData is used to pass in the SDL2 window handle attached to the GraphicsContext::Traits structure. */ struct WindowData : public osg::Referenced { WindowData(SDL_Window *window) : mWindow(window) @@ -103,6 +103,6 @@ public: void setupWindowingSystemInterface(); -} // namespace TK +} #endif /* OSGGRAPHICSWINDOW_H */