r126 - trunk/src

DONOTREPLY at icculus.org DONOTREPLY at icculus.org
Wed Aug 31 09:09:29 EDT 2005


Author: jonas
Date: 2005-08-31 09:09:29 -0400 (Wed, 31 Aug 2005)
New Revision: 126

Modified:
   trunk/src/editor.cpp
   trunk/src/editor.h
   trunk/src/physics.cpp
   trunk/src/scenario.cpp
   trunk/src/scenario.h
Log:
allow placing of objects + map buffering, reloading and saving

Modified: trunk/src/editor.cpp
===================================================================
--- trunk/src/editor.cpp	2005-08-31 09:42:45 UTC (rev 125)
+++ trunk/src/editor.cpp	2005-08-31 13:09:29 UTC (rev 126)
@@ -9,10 +9,13 @@
 Editor::Editor() {
     run_action(EDIT_RESET_ACTIONS);
     string place_name="";
+    save_name="newmap.txt";
+    box=NULL;
 }
 
 Editor::~Editor() {
     closeBox();
+    saveBuf(save_name);
 }
 
 void Editor::run_action(Uint32 action, Uint16 x, Uint16 y) {
@@ -29,11 +32,46 @@
     } else if (action==EDIT_ACT_BOX) {
         if (box) box->act(box->getCurrentEntry(x,y));
     } else if (action==EDIT_PLACE_OBJECT) {
-        scenario->pool->addObjectbyName(place_name,place_image,x,y,scenario->pool->getNextObjectName(place_name));
+        string next_name=scenario->pool->getNextObjectName(place_name);
+        if (scenario->pool->addObjectbyName(place_name,place_image,x,y,next_name)) {
+            appendtoBuf(place_name+" "+place_image+" "+itos(x)+" "+itos(y)+" "+next_name);
+        }
         action_mouse_pressed[SDL_BUTTON_LEFT]=EDIT_ACT_BOX;
     } else { }
 }
 
+void Editor::appendtoBuf(string line) {
+    scenario->mapbuf.push_back(line);
+}
+
+string Editor::removefromBuf(string match) {
+    for (Uint16 linenum=0; linenum<scenario->mapbuf.size(); linenum++) {
+        if (scenario->mapbuf[linenum].find(match)!=string::npos) {
+            vector<string>::iterator it=scenario->mapbuf.begin()+linenum;
+            string returnstring=scenario->mapbuf[linenum];
+            scenario->mapbuf.erase(it);
+            return returnstring;
+        }
+    }
+    return "";
+}
+
+int Editor::saveBuf(string filename) {
+    ofstream outfile;
+    outfile.open(filename.c_str());
+    if (outfile.is_open()) {
+        for (Uint16 linenum=0; linenum<scenario->mapbuf.size(); linenum++) {
+            outfile << scenario->mapbuf[linenum] << endl;
+        }
+        outfile.close();
+        cout << "Saved map to " << filename << endl;
+        return 0;
+    } else {
+        cout << "Failed to save map to " << filename << ": Could not open file!" << endl;
+        return -1;
+    }
+}
+
 Box* Editor::setBox(Box* newbox) {
     return box=newbox;   
 }
@@ -170,6 +208,8 @@
             editor->place_image="viking1.bmp";
         } else if (editor->place_name=="Water") {
             editor->place_image="water.bmp";
+        } else if (editor->place_name=="Exit") {
+            editor->place_image="exit.bmp";
         } else if (editor->place_name=="Teleporter") {
             editor->place_image="viking1.bmp";
         } else if (editor->place_name=="Wind") {

Modified: trunk/src/editor.h
===================================================================
--- trunk/src/editor.h	2005-08-31 09:42:45 UTC (rev 125)
+++ trunk/src/editor.h	2005-08-31 13:09:29 UTC (rev 126)
@@ -74,13 +74,23 @@
         }
         Box* box;
     protected:
+        /// Name the saved map file
+        string save_name;
         /// Object to place when clicking the left mouse button
         string place_name;
         /// Image name of the placed object
         string place_image;
         /// Action type for the mouse buttons when clicking
         Uint32 action_mouse_pressed[6];
+        /// Action type for the mouse buttons when releasing the button
         Uint32 action_mouse_released[6];
+        /// Append a command to the buffered map file
+        void appendtoBuf(string);
+        /* TODO: add header modifiers */
+        /// Remove the first line in the buffered map file matching the specified string
+        string removefromBuf(string);
+        /// Save the map file buffer to the specified file if possible
+        int saveBuf(string);
 };
 
 #endif

Modified: trunk/src/physics.cpp
===================================================================
--- trunk/src/physics.cpp	2005-08-31 09:42:45 UTC (rev 125)
+++ trunk/src/physics.cpp	2005-08-31 13:09:29 UTC (rev 126)
@@ -95,7 +95,7 @@
         if (input->keyState(KEY_RIGHT)) scenario->player->in_right(dt);
         if (input->keyState(KEY_UP))    scenario->player->in_up(dt);
         if (input->keyState(KEY_DOWN))  scenario->player->in_down(dt);
-    } else {
+    } else if (scenario->player!=NULL) {
         scenario->player->unsetState(STATE_MLEFT);
         scenario->player->unsetState(STATE_MRIGHT);
     }

Modified: trunk/src/scenario.cpp
===================================================================
--- trunk/src/scenario.cpp	2005-08-31 09:42:45 UTC (rev 125)
+++ trunk/src/scenario.cpp	2005-08-31 13:09:29 UTC (rev 126)
@@ -49,23 +49,14 @@
     background=NULL;
     area=NULL;
     player=NULL;
-    name="";
 }
 
-int Scenario::loadMap(string mapname) {
-    reinitMap();
-    sfxeng->playMusic((config.datadir + "01theme.wav").c_str());
+int Scenario::loadMapBuf(string mapname) {
     name=mapname;
     ifstream mapfile;
     string tmpline;
-    string image;
-    string arg1,arg2,arg3;
-    Uint16 x,y;
-    background=NULL;
-    area=NULL;
     string loadfile=config.datadir+name;
-    bool header=false;
-    
+
     mapfile.open(loadfile.c_str());
     if (mapfile) {
         cout << "Loading new map: " << loadfile << endl;
@@ -74,53 +65,68 @@
         return -2;
     }
 
+    mapbuf.clear();
+    while (getline(mapfile,tmpline)) {
+        mapbuf.push_back(tmpline);
+    }
+
+    mapfile.close();
+    mapfile.clear();
+    return 0;
+}
+
+int Scenario::reloadMap() {
+    reinitMap();
+    sfxeng->playMusic((config.datadir + "01theme.wav").c_str());
+    string image;
+    string cname,arg1,arg2,arg3;
+    Uint16 x,y;
+    bool header=false;
+
     //check every line
-    while (getline(mapfile,tmpline)) {
+    for (Uint16 linenum=0; linenum<mapbuf.size(); linenum++) {
         //parse the header, always start a header with #HEADER and end it with #ENDHEADER!
         if (header) {
-            name.erase();
+            cname.erase();
             arg1.erase();
             arg2.erase();
             arg3.erase();
-            std::istringstream tmpstream(tmpline);
-            tmpstream >> name >> arg1 >> arg2 >> arg3;
-            if (name[0]=='#') {
-                if (name=="#ENDHEADER") header=false;
+            std::istringstream tmpstream(mapbuf[linenum]);
+            tmpstream >> cname >> arg1 >> arg2 >> arg3;
+            if (cname[0]=='#') {
+                if (cname=="#ENDHEADER") header=false;
                 continue;
             }
             continue;
         }
-        name.erase();
+        cname.erase();
         image.erase();
         x=0;
         y=0;
         arg1=arg2="0";
         arg3="";
-        std::istringstream tmpstream(tmpline);
-        tmpstream >> name >> image >> x >> y >> arg1 >> arg2 >> arg3;
+        std::istringstream tmpstream(mapbuf[linenum]);
+        tmpstream >> cname >> image >> x >> y >> arg1 >> arg2 >> arg3;
 
         //Skip empty lines
-        if (name.empty()) continue;
+        if (cname.empty()) continue;
         //Skip comments
-        if (name[0]=='#') {
-            if (name=="#HEADER") header=true;
+        if (cname[0]=='#') {
+            if (cname=="#HEADER") header=true;
             continue;
         }
 
-        if (name=="Background" && (!background)) {
+        if (cname=="Background" && (!background)) {
             background=new Background(image);
             if (background) area=background->getPos();
         //Background has to be first!
         } else if (background) {
-            pool->addObjectbyName(name,image,x,y,arg1,arg2,arg3);
+            pool->addObjectbyName(cname,image,x,y,arg1,arg2,arg3);
         } else {
-            cout << "No background found yet, skipping " << name << " ...\n";
+            cout << "No background found yet, skipping " << cname << " ...\n";
         }
     }
 
-    mapfile.close();
-    mapfile.clear();
-
     //Has a background been found?
     if (background) {
         if (pool->playerspool.size()>0) player=*pool->playerspool.begin();
@@ -133,6 +139,11 @@
     }
 }
 
+int Scenario::loadMap(string mapname) {
+    if (!loadMapBuf(mapname) && !reloadMap()) return 0;
+    else return -1;
+}
+
 Uint16 Scenario::getDirection(const SDL_Rect& src, const SDL_Rect& dest) const {
     Uint16 dir=NOTHING;
     if ((src.x+src.w) < (dest.x+dest.w)) dir|=DIR_RIGHT;

Modified: trunk/src/scenario.h
===================================================================
--- trunk/src/scenario.h	2005-08-31 09:42:45 UTC (rev 125)
+++ trunk/src/scenario.h	2005-08-31 13:09:29 UTC (rev 126)
@@ -61,6 +61,10 @@
         ObjectsPool* pool;
         /// Physic Handler
         PhysicHandler* physic;
+        /// Used to load the map file into a buffer
+        int loadMapBuf(string mapname);
+        /// Reload the map file using the buffered map file
+        int reloadMap();
         ///\brief Loads and initializes the map data
         ///
         /// Parses the map file and tries to add the objects by using addObjectByName()
@@ -71,6 +75,7 @@
         bool failed;
         /// Name of the map file
         string name;
+        std::vector<string> mapbuf;
     private:
         inline void reinitMap();
 };




More information about the lostpenguins-commits mailing list