From 06111415b117fc47b5b8bf6396d855616778f3b0 Mon Sep 17 00:00:00 2001 From: Bjørn Lindeijer Date: Thu, 14 Jul 2005 00:41:48 +0000 Subject: Committed resource manager cleanup patch by Doener, and properly implemented the custom mouse cursor option, which is now also dynamically changeable through the setup window. --- src/engine.cpp | 9 ++- src/graphics.cpp | 29 +-------- src/graphics.h | 3 - src/gui/gui.cpp | 127 ++++++++++++++++++++++++++++---------- src/gui/gui.h | 37 ++++++++--- src/gui/setup.cpp | 44 ++++++++----- src/gui/setup.h | 24 ++++--- src/main.cpp | 1 + src/resources/resourcemanager.cpp | 74 ++++++---------------- src/resources/resourcemanager.h | 6 +- 10 files changed, 192 insertions(+), 162 deletions(-) (limited to 'src') diff --git a/src/engine.cpp b/src/engine.cpp index 11e653b7..2ee6406f 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -282,11 +282,14 @@ Map *Engine::getCurrentMap() void Engine::setCurrentMap(Map *newMap) { std::string oldMusic = ""; + if (mCurrentMap) { oldMusic = mCurrentMap->getProperty("music"); } + std::string newMusic = newMap->getProperty("music"); - if(newMusic!=oldMusic) { + + if (newMusic != oldMusic) { newMusic = std::string(TMW_DATADIR) + "data/music/" + newMusic; sound.playMusic(newMusic.c_str(), -1); } @@ -542,8 +545,6 @@ void Engine::draw() statusWindow->update(); } - gui->draw(); - std::stringstream debugStream; debugStream << "[" << fps << " fps] " << mouseTileX << ", " << mouseTileY; @@ -556,4 +557,6 @@ void Engine::draw() debugInfo->setCaption(debugStream.str()); debugInfo->adjustSize(); + + gui->draw(); } diff --git a/src/graphics.cpp b/src/graphics.cpp index 16b8f67b..5eae65bf 100644 --- a/src/graphics.cpp +++ b/src/graphics.cpp @@ -31,8 +31,7 @@ extern volatile int framesToDraw; SDL_Surface *screen; -Graphics::Graphics(): - mouseCursor(NULL) +Graphics::Graphics() { if (useOpenGL) { #ifdef USE_OPENGL @@ -53,19 +52,6 @@ Graphics::Graphics(): #endif } - if (config.getValue("cursor", 1) == 1) - { - // Hide the system mouse cursor - SDL_ShowCursor(SDL_DISABLE); - - // Load the mouse cursor - ResourceManager *resman = ResourceManager::getInstance(); - mouseCursor = resman->getImage("graphics/gui/mouse.png"); - if (!mouseCursor) { - logger->error("Unable to load mouse cursor."); - } - } - // Initialize for drawing _beginDraw(); } @@ -74,8 +60,6 @@ Graphics::~Graphics() { // Deinitialize for drawing _endDraw(); - - mouseCursor->decRef(); } int Graphics::getWidth() @@ -140,17 +124,6 @@ void Graphics::drawImageRect( void Graphics::updateScreen() { - int mouseX, mouseY; - Uint8 button = SDL_GetMouseState(&mouseX, &mouseY); - - if (SDL_GetAppState() & SDL_APPMOUSEFOCUS || button & SDL_BUTTON(1)) - { - // Draw mouse before flipping - if (mouseCursor != NULL) { - mouseCursor->draw(screen, mouseX - 5, mouseY - 2); - } - } - if (useOpenGL) { #ifdef USE_OPENGL glFlush(); diff --git a/src/graphics.h b/src/graphics.h index 78d4a3e6..5b57ff8b 100644 --- a/src/graphics.h +++ b/src/graphics.h @@ -109,9 +109,6 @@ class Graphics : public gcn::SDLGraphics { * Returns the height of the screen. */ int getHeight(); - - private: - Image *mouseCursor; }; #endif diff --git a/src/gui/gui.cpp b/src/gui/gui.cpp index 37e5daff..ce8f428b 100644 --- a/src/gui/gui.cpp +++ b/src/gui/gui.cpp @@ -30,6 +30,7 @@ #include "../engine.h" #include "../game.h" #include "../log.h" +#include "../resources/resourcemanager.h" extern Being* autoTarget; @@ -46,7 +47,10 @@ gcn::ImageFont *hitYellowFont; // Font used to display speech and player names gcn::ImageFont *speechFont; -Gui::Gui(Graphics *graphics) +Gui::Gui(Graphics *graphics): + mHostImageLoader(NULL), + mMouseCursor(NULL), + mCustomCursor(false) { // Set graphics guiGraphics = graphics; @@ -59,18 +63,17 @@ Gui::Gui(Graphics *graphics) // Set image loader #ifdef USE_OPENGL if (useOpenGL) { - hostImageLoader = new gcn::SDLImageLoader(); - imageLoader = new gcn::OpenGLImageLoader(hostImageLoader); + mHostImageLoader = new gcn::SDLImageLoader(); + mImageLoader = new gcn::OpenGLImageLoader(mHostImageLoader); } else { - hostImageLoader = NULL; - imageLoader = new gcn::SDLImageLoader(); + mImageLoader = new gcn::SDLImageLoader(); } #else - imageLoader = new gcn::SDLImageLoader(); + mImageLoader = new gcn::SDLImageLoader(); #endif - gcn::Image::setImageLoader(imageLoader); + gcn::Image::setImageLoader(mImageLoader); // Set focus handler delete mFocusHandler; @@ -86,7 +89,7 @@ Gui::Gui(Graphics *graphics) // Set global font try { - guiFont = new gcn::ImageFont( + mGuiFont = new gcn::ImageFont( TMW_DATADIR "data/graphics/gui/sansserif8.png", " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[" "\\]^_`abcdefghijklmnopqrstuvwxyz{|}~|" @@ -95,7 +98,7 @@ Gui::Gui(Graphics *graphics) catch (gcn::Exception e) { try { - guiFont = new gcn::ImageFont( + mGuiFont = new gcn::ImageFont( "data/graphics/gui/sansserif8.png", " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVW" "XYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~|" @@ -130,7 +133,7 @@ Gui::Gui(Graphics *graphics) } } - gcn::Widget::setGlobalFont(guiFont); + gcn::Widget::setGlobalFont(mGuiFont); // Load hits' colourful fonts try { @@ -146,22 +149,26 @@ Gui::Gui(Graphics *graphics) } catch (gcn::Exception e) { - try { - hitRedFont = new gcn::ImageFont( - "data/graphics/gui/hits_red.png", - "0123456789"); - hitBlueFont = new gcn::ImageFont( - "data/graphics/gui/hits_blue.png", - "0123456789"); - hitYellowFont = new gcn::ImageFont( - "data/graphics/gui/hits_yellow.png", - "mis"); - } - catch (gcn::Exception e) - { - logger->error("Unable to load colored hits' fonts!"); - } + try { + hitRedFont = new gcn::ImageFont( + "data/graphics/gui/hits_red.png", + "0123456789"); + hitBlueFont = new gcn::ImageFont( + "data/graphics/gui/hits_blue.png", + "0123456789"); + hitYellowFont = new gcn::ImageFont( + "data/graphics/gui/hits_yellow.png", + "mis"); + } + catch (gcn::Exception e) + { + logger->error("Unable to load colored hits' fonts!"); + } } + + // Initialize mouse cursor and listen for changes to the option + setUseCustomCursor(config.getValue("customcursor", 1) == 1); + config.addListener("customcursor", this); } Gui::~Gui() @@ -171,14 +178,18 @@ Gui::~Gui() delete hitBlueFont; delete hitYellowFont; - delete guiFont; + if (mMouseCursor) { + mMouseCursor->decRef(); + } + + delete mGuiFont; delete guiTop; - delete imageLoader; -#ifdef USE_OPENGL - if (hostImageLoader) { - delete hostImageLoader; + delete mImageLoader; + + if (mHostImageLoader) { + delete mHostImageLoader; } -#endif + delete guiInput; } @@ -194,7 +205,18 @@ void Gui::logic() void Gui::draw() { guiGraphics->pushClipArea(guiTop->getDimension()); + guiTop->draw(guiGraphics); + + int mouseX, mouseY; + Uint8 button = SDL_GetMouseState(&mouseX, &mouseY); + + if ((SDL_GetAppState() & SDL_APPMOUSEFOCUS || button & SDL_BUTTON(1)) + && mCustomCursor) + { + mMouseCursor->draw(screen, mouseX - 5, mouseY - 2); + } + guiGraphics->popClipArea(); } @@ -219,9 +241,48 @@ void Gui::mousePress(int mx, int my, int button) } } - gcn::ImageFont *Gui::getFont() { - return guiFont; + return mGuiFont; +} + +void +Gui::setUseCustomCursor(bool customCursor) +{ + if (customCursor != mCustomCursor) + { + mCustomCursor = customCursor; + + if (mCustomCursor) + { + // Hide the SDL mouse cursor + SDL_ShowCursor(SDL_DISABLE); + + // Load the mouse cursor + ResourceManager *resman = ResourceManager::getInstance(); + mMouseCursor = resman->getImage("graphics/gui/mouse.png"); + if (!mMouseCursor) { + logger->error("Unable to load mouse cursor."); + } + } + else + { + // Show the SDL mouse cursor + SDL_ShowCursor(SDL_ENABLE); + + // Unload the mouse cursor + if (mMouseCursor) { + mMouseCursor->decRef(); + mMouseCursor = NULL; + } + } + } } +void +Gui::optionChanged(const std::string &name) +{ + if (name == "customcursor") { + setUseCustomCursor(config.getValue("customcursor", 1) == 1); + } +} diff --git a/src/gui/gui.h b/src/gui/gui.h index dfd72ec7..02ea7caa 100644 --- a/src/gui/gui.h +++ b/src/gui/gui.h @@ -28,6 +28,7 @@ #include #include "windowcontainer.h" #include "../graphics.h" +#include "../configuration.h" /** * \defgroup GUI Core GUI related classes (widgets) @@ -42,7 +43,7 @@ * * \ingroup GUI */ -class Gui : public gcn::Gui, public gcn::MouseListener +class Gui : public gcn::Gui, public gcn::MouseListener, ConfigListener { public: /** @@ -58,30 +59,46 @@ class Gui : public gcn::Gui, public gcn::MouseListener /** * Works around Guichan bug */ - void logic(); + void + logic(); /** * Draws the whole Gui by calling draw functions down in the * Gui hierarchy. It also draws the mouse pointer. */ - void draw(); + void + draw(); /** * Handles mouse press on map. */ - void mousePress(int mx, int my, int button); + void + mousePress(int mx, int my, int button); /** * Return game font */ - gcn::ImageFont *getFont(); + gcn::ImageFont* + getFont(); + + /** + * Sets whether a custom cursor should be rendered. + */ + void + setUseCustomCursor(bool customCursor); + + /** + * ConfigListener method. + */ + void + optionChanged(const std::string &name); private: -#ifdef USE_OPENGL - gcn::ImageLoader *hostImageLoader; /**< For loading images in GL */ -#endif - gcn::ImageLoader *imageLoader; /**< For loading images */ - gcn::ImageFont *guiFont; /**< The global GUI font */ + gcn::ImageLoader *mHostImageLoader; /**< For loading images in GL */ + gcn::ImageLoader *mImageLoader; /**< For loading images */ + gcn::ImageFont *mGuiFont; /**< The global GUI font */ + Image *mMouseCursor; /**< Mouse cursor image */ + bool mCustomCursor; /**< Show custom cursor */ }; extern Gui *gui; /**< The GUI system */ diff --git a/src/gui/setup.cpp b/src/gui/setup.cpp index a1c1f500..a9421874 100644 --- a/src/gui/setup.cpp +++ b/src/gui/setup.cpp @@ -88,6 +88,7 @@ Setup::Setup(): fsCheckBox = new CheckBox("Full screen", false); openGlCheckBox = new CheckBox("OpenGL", false); openGlCheckBox->setEnabled(false); + customCursorCheckBox = new CheckBox("Custom cursor"); alphaLabel = new gcn::Label("Gui opacity"); alphaSlider = new Slider(0.2, 1.0); audioLabel = new gcn::Label("Audio settings"); @@ -105,28 +106,32 @@ Setup::Setup(): alphaSlider->setEventId("guialpha"); sfxSlider->setEventId("sfx"); musicSlider->setEventId("music"); + customCursorCheckBox->setEventId("customcursor"); // Set dimensions/positions - setContentSize(SETUP_WIDTH, 216); + setContentSize(SETUP_WIDTH, 226); + videoLabel->setPosition(SETUP_WIDTH - videoLabel->getWidth() - 5, 10); scrollArea->setDimension(gcn::Rectangle(10, 30, 90, 50)); modeList->setDimension(gcn::Rectangle(0, 0, 60, 50)); fsCheckBox->setPosition(110, 30); openGlCheckBox->setPosition(110, 50); - alphaSlider->setDimension(gcn::Rectangle(10, 90, 100, 10)); - alphaLabel->setPosition(20 + alphaSlider->getWidth(), 87); - audioLabel->setPosition(SETUP_WIDTH - videoLabel->getWidth() - 5, 110); - soundCheckBox->setPosition(10, 130); - sfxSlider->setDimension(gcn::Rectangle(10, 150, 100, 10)); - musicSlider->setDimension(gcn::Rectangle(10, 170, 100, 10)); - sfxLabel->setPosition(20 + sfxSlider->getWidth(), 147); - musicLabel->setPosition(20 + musicSlider->getWidth(), 167); + customCursorCheckBox->setPosition(110, 70); + alphaSlider->setDimension(gcn::Rectangle(10, 100, 100, 10)); + alphaLabel->setPosition(20 + alphaSlider->getWidth(), 97); + + audioLabel->setPosition(SETUP_WIDTH - videoLabel->getWidth() - 5, 120); + soundCheckBox->setPosition(10, 140); + sfxSlider->setDimension(gcn::Rectangle(10, 160, 100, 10)); + musicSlider->setDimension(gcn::Rectangle(10, 180, 100, 10)); + sfxLabel->setPosition(20 + sfxSlider->getWidth(), 157); + musicLabel->setPosition(20 + musicSlider->getWidth(), 177); cancelButton->setPosition( SETUP_WIDTH - 5 - cancelButton->getWidth(), - 216 - 5 - cancelButton->getHeight()); + 226 - 5 - cancelButton->getHeight()); applyButton->setPosition( cancelButton->getX() - 5 - applyButton->getWidth(), - 216 - 5 - applyButton->getHeight()); + 226 - 5 - applyButton->getHeight()); // Listen for actions applyButton->addActionListener(this); @@ -134,31 +139,33 @@ Setup::Setup(): alphaSlider->addActionListener(this); sfxSlider->addActionListener(this); musicSlider->addActionListener(this); + customCursorCheckBox->addActionListener(this); // Assemble dialog add(videoLabel); add(scrollArea); add(fsCheckBox); add(openGlCheckBox); + add(customCursorCheckBox); add(audioLabel); add(soundCheckBox); - add(applyButton); - add(cancelButton); add(alphaSlider); add(alphaLabel); add(sfxSlider); add(musicSlider); add(sfxLabel); add(musicLabel); + add(applyButton); + add(cancelButton); setLocationRelativeTo(getParent()); // Load default settings modeList->setSelected(-1); - if (config.getValue("screen", 0) == 1) { - fsCheckBox->setMarked(true); - } + + fsCheckBox->setMarked(config.getValue("screen", 0)); soundCheckBox->setMarked(config.getValue("sound", 0)); + customCursorCheckBox->setMarked(config.getValue("customcursor", 1)); alphaSlider->setValue(config.getValue("guialpha", 0.8)); sfxSlider->setValue(config.getValue("sfxVolume", 100)); musicSlider->setValue(config.getValue("musicVolume", 60)); @@ -198,6 +205,11 @@ void Setup::action(const std::string &eventId) { config.setValue("guialpha", alphaSlider->getValue()); } + else if (eventId == "customcursor") + { + config.setValue("customcursor", + customCursorCheckBox->isMarked() ? 1 : 0); + } else if (eventId == "apply") { setVisible(false); diff --git a/src/gui/setup.h b/src/gui/setup.h index 30eda305..02eebae1 100644 --- a/src/gui/setup.h +++ b/src/gui/setup.h @@ -31,7 +31,8 @@ * * \ingroup Interface */ -class ModeListModel : public gcn::ListModel { +class ModeListModel : public gcn::ListModel +{ public: /** * Constructor. @@ -62,25 +63,27 @@ class ModeListModel : public gcn::ListModel { * * \ingroup GUI */ -class Setup : public Window, public gcn::ActionListener { +class Setup : public Window, public gcn::ActionListener +{ private: + ModeListModel *modeListModel; + // Dialog widgets - gcn::Label *videoLabel, *audioLabel; gcn::ListBox *modeList; - ModeListModel *modeListModel; gcn::ScrollArea *scrollArea; + gcn::Label *videoLabel, *audioLabel; + gcn::Label *alphaLabel; + gcn::Label *sfxLabel, *musicLabel; gcn::CheckBox *fsCheckBox; gcn::CheckBox *openGlCheckBox; - gcn::Slider *alphaSlider; - gcn::Label *alphaLabel; gcn::CheckBox *soundCheckBox; + gcn::CheckBox *customCursorCheckBox; + gcn::Slider *alphaSlider; gcn::Slider *sfxSlider, *musicSlider; - gcn::Label *sfxLabel, *musicLabel; gcn::Button *applyButton; gcn::Button *cancelButton; - public: - + public: /** * Constructor. */ @@ -94,7 +97,8 @@ class Setup : public Window, public gcn::ActionListener { /** * Event handling method. */ - void action(const std::string& eventId); + void + action(const std::string& eventId); }; diff --git a/src/main.cpp b/src/main.cpp index 3c0d13a9..0778bc82 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -176,6 +176,7 @@ void init_engine() config.setValue("musicVolume", 60); config.setValue("fpslimit", 0); config.setValue("updatehost", "http://themanaworld.org/"); + config.setValue("customcursor", 1); // Checking if the configuration file exists... otherwise creates it with // default options ! diff --git a/src/resources/resourcemanager.cpp b/src/resources/resourcemanager.cpp index a884f63e..50d0f4b8 100644 --- a/src/resources/resourcemanager.cpp +++ b/src/resources/resourcemanager.cpp @@ -84,82 +84,44 @@ ResourceManager::get(const E_RESOURCE_TYPE &type, const std::string &idPath) logger->log("ResourceManager::get(%s)", idPath.c_str()); + int fileSize; + void *buffer = loadFile(idPath, fileSize); + + if (!buffer) { + logger->log("Warning: resource doesn't exist!"); + return NULL; + } + Resource *resource = NULL; // Create an object of the specified type. switch (type) { - case MAP: - logger->log("Warning: Map resource not supported."); - break; case MUSIC: { - // Load the music resource file - int fileSize; - void *buffer = loadFile(idPath, fileSize); - - if (buffer != NULL) - { - // Let the music class load it - resource = Music::load(buffer, fileSize); - - // Cleanup - free(buffer); - } - else { - logger->log("Warning: resource doesn't exist!"); - } + // Let the music class load it + resource = Music::load(buffer, fileSize); } break; case IMAGE: { - // Load the image resource file - int fileSize; - void *buffer = loadFile(idPath, fileSize); - - if (buffer != NULL) - { - // Let the image class load it - resource = Image::load(buffer, fileSize); - - // Cleanup - free(buffer); - } - else { - logger->log("Warning: resource doesn't exist!"); - } + // Let the image class load it + resource = Image::load(buffer, fileSize); } break; - case SCRIPT: - logger->log("Warning: Script resource not supported."); - break; - case TILESET: - logger->log("Warning: Tileset resource not supported."); - break; case SOUND_EFFECT: { - // Load the sample resource file - int fileSize; - void *buffer = loadFile(idPath, fileSize); - - if (buffer != NULL) - { - // Let the sound effect class load it - resource = SoundEffect::load(buffer, fileSize); - - // Cleanup - free(buffer); - } - else { - logger->log("Warning: resource doesn't exist!"); - } + // Let the sound effect class load it + resource = SoundEffect::load(buffer, fileSize); } break; - default: - logger->log("Warning: Unknown resource type"); + default: + /* Nothing to do here, just avoid compiler warnings... */ break; } + free(buffer); + if (resource) { resource->incRef(); resource->setIdPath(idPath); diff --git a/src/resources/resourcemanager.h b/src/resources/resourcemanager.h index f95acadc..e3ad1e94 100644 --- a/src/resources/resourcemanager.h +++ b/src/resources/resourcemanager.h @@ -42,11 +42,11 @@ class ResourceManager */ enum E_RESOURCE_TYPE { - MAP, + //MAP, MUSIC, IMAGE, - SCRIPT, - TILESET, + //SCRIPT, + //TILESET, SOUND_EFFECT }; -- cgit v1.2.3-70-g09d2