QLong avatar QLong committed d977477

Add FileUtils to handle resrouce directory

Comments (0)

Files changed (7)

     Srcs/GEAudioOut.cpp \
     Srcs/GEAudioMixer.cpp \
     Srcs/GEAudioBufferPlayInstance.cpp \
-    Srcs/GEAudioBuffer.cpp
+    Srcs/GEAudioBuffer.cpp \
+    Srcs/fileutils.cpp
 HEADERS += \
     Srcs/transitionscene.h \
     Srcs/textitem.h \
     Srcs/GEAudioOut.h \
     Srcs/GEAudioMixer.h \
     Srcs/GEAudioBufferPlayInstance.h \
-    Srcs/GEAudioBuffer.h
+    Srcs/GEAudioBuffer.h \
+    Srcs/fileutils.h
 FORMS +=
 
 # Please do not modify the following two lines. Required for deployment.

Srcs/fileutils.cpp

+#include "fileutils.h"
+#include <QCoreApplication>
+#include <QDir>
+
+static FileUtils * g_pFileUtils = NULL;
+
+FileUtils *FileUtils::shareFileUtils()
+{
+    if (g_pFileUtils == NULL)
+    {
+        g_pFileUtils = new FileUtils();
+    }
+    return g_pFileUtils;
+}
+
+void FileUtils::setResourceDirectory(const QString &directoryName)
+{
+    //this code, until the last QDir is to set the CWD, aka Current Working Directory
+    QDir dir(QCoreApplication::applicationDirPath());
+    if (dir.dirName().toLower() == "bin")
+    {
+        dir.cdUp();
+    }
+#ifdef WIN32
+    if (dir.dirName().toLower() == "debug" || dir.dirName().toLower() == "release")
+    {
+        dir.cdUp();
+    }
+#endif
+
+    m_Directory = directoryName;
+    if (m_Directory.size() > 0 && !m_Directory.endsWith('/'))
+    {
+        m_Directory.append("/");
+    }
+
+    QDir::setCurrent(dir.absolutePath()+"/"+m_Directory);
+}
+
+QString &FileUtils::getResourceDirectory()
+{
+    return m_Directory;
+}
+
+FileUtils::FileUtils()
+{
+}
+#ifndef FILEUTILS_H
+#define FILEUTILS_H
+
+#include <QString>
+
+class FileUtils
+{
+public:
+    static FileUtils * shareFileUtils();
+
+    /**
+    @brief  Set the resource directory; we will find resources relative to this directory.
+    @param pszDirectoryName  Relative path to root.
+    */
+    void setResourceDirectory(const QString &directoryName);
+
+    /**
+    @brief  Get the resource directory
+    */
+    QString & getResourceDirectory();
+
+protected:
+    FileUtils();
+
+protected:
+    QString m_Directory;
+};
+
+#endif // FILEUTILS_H

Srcs/game_engine.h

 #include "director.h"
 #include "sprite.h"
 #include "audiomanager.h"
+#include "fileutils.h"
 
 #endif // GAME_ENGINE_H
 }
 
 Sprite* Sprite::spriteWithImage(const char* filename) {
-    Sprite* newsprite = new Sprite("resource/"+QString(filename));
+    Sprite* newsprite = new Sprite(QString(filename));
     return newsprite;
 }
 
 {
     QApplication::setGraphicsSystem("raster");
     QApplication app(argc, argv);
-    //this code, until the last QDir is to set the CWD, aka Current Working Directory
-    QDir dir(QCoreApplication::applicationDirPath());
-    if (dir.dirName().toLower() == "bin")
-    {
-        dir.cdUp();
-    }
-#ifdef WIN32
-    if (dir.dirName().toLower() == "debug" || dir.dirName().toLower() == "release")
-    {
-        dir.cdUp();
-    }
-#endif
-    QDir::setCurrent(dir.absolutePath());
+
+    FileUtils::shareFileUtils()->setResourceDirectory("resource");
+
     //set up audio playback
+#if !defined(Q_OS_WIN)
     audioManager::setUpAudioManager();
+#endif
     //here is the second main_menu to change
     Director::sharedDirector()->startWithScene(main_menu::scene());
     return app.exec();
     //taking screen size, saving it to variable called winSize
     winSize = Director::sharedDirector()->winSizeInPixels();
 
-    QFileInfo info("wallpapper.jpg");
-    qDebug() << info.absoluteFilePath();
-
     //we'll start by adding a sprite that represents the background for the menu
     background = Sprite::spriteWithImage("wallpaper.jpg");
     background->setPos(winSize.width()/2,winSize.height()/2);
     if (counter < cards.size()) {
         cards.at(counter)->runSequence(Sequence::sequenceWithActions(action::easeinout(action::moveBy(0.5,300,200)),action::FuncCall(this,SLOT(animation())),NULL));
         cards.at(counter)->runAction(action::easeinout(action::rotateBy(0.5,360)));
+#if !defined(Q_OS_WIN)
         audioManager::playSound("alert.wav");
         audioManager::playSound("play_card.wav");
+#endif
         counter++;
     }
 }
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.