From 6897c86f62c6f5ed59ad0dade2c81946844fa5dd Mon Sep 17 00:00:00 2001
From: Andrei Karas <akaras@inbox.ru>
Date: Sun, 27 Jan 2013 13:29:38 +0300
Subject: Add ability to maps to play music files with map name.

If music attribute missing, music can be played with name MAP.ogg
---
 src/resources/mapreader.cpp | 16 +++++++++++++++-
 src/resources/mapreader.h   |  2 ++
 2 files changed, 17 insertions(+), 1 deletion(-)

(limited to 'src')

diff --git a/src/resources/mapreader.cpp b/src/resources/mapreader.cpp
index db3b31f25..a8388e5a9 100644
--- a/src/resources/mapreader.cpp
+++ b/src/resources/mapreader.cpp
@@ -244,6 +244,8 @@ Map *MapReader::readMap(const std::string &filename,
         map->setProperty("_realfilename", filename);
     }
 
+    if (map->getProperty("music").empty())
+        updateMusic(map);
     return map;
 }
 
@@ -274,10 +276,11 @@ Map *MapReader::readMap(XmlNodePtr node, const std::string &path)
 
     Map *const map = new Map(w, h, tilew, tileh);
 
+    const std::string fileName = path.substr(path.rfind("/") + 1);
+    map->setProperty("shortName", fileName);
 #ifdef USE_OPENGL
     if (graphicsManager.getUseAtlases())
     {
-        const std::string fileName = path.substr(path.rfind("/") + 1);
         const MapDB::MapInfo *const info = MapDB::getMapAtlas(fileName);
         if (info)
         {
@@ -843,6 +846,7 @@ Map *MapReader::createEmptyMap(const std::string &filename,
     Map *const map = new Map(300, 300, 32, 32);
     map->setProperty("_filename", realFilename);
     map->setProperty("_realfilename", filename);
+    updateMusic(map);
     map->setCustom(true);
     MapLayer *layer = new MapLayer(0, 0, 300, 300, false);
     map->addLayer(layer);
@@ -851,3 +855,13 @@ Map *MapReader::createEmptyMap(const std::string &filename,
 
     return map;
 }
+
+void MapReader::updateMusic(Map *const map)
+{
+    std::string name = map->getProperty("shortName");
+    const size_t p = name.rfind(".");
+    if (p != std::string::npos)
+        name = name.substr(0, p);
+    name += ".ogg";
+    map->setProperty("music", name);
+}
diff --git a/src/resources/mapreader.h b/src/resources/mapreader.h
index 9168206c3..26e47a74c 100644
--- a/src/resources/mapreader.h
+++ b/src/resources/mapreader.h
@@ -75,6 +75,8 @@ class MapReader final
          */
         static Tileset *readTileset(XmlNodePtr node, const std::string &path,
                                     Map *const map) A_WARN_UNUSED;
+
+        static void updateMusic(Map *const map);
 };
 
 #endif
-- 
cgit v1.2.3-70-g09d2