Hosting courtesy of Sourceforge

SourceForge Logo
Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members   File Members  

MapEditor.cpp

Go to the documentation of this file.
00001 //
00002 //                                 MapEditor.cpp
00003 //                             -------------------
00004 //    begin                : Tue Mar 19 2002
00005 //    copyright           : (C) 2002 by Rob Renaud
00006 //    email                : rrenaud@eden.rutgers.edu
00007 //
00008 //   This program is free software; you can redistribute it and/or modify
00009 //   it under the terms of the GNU General Public License as published by
00010 //   the Free Software Foundation; either version 2 of the License, or
00011 //   (at your option) any later version.
00012 //
00013 
00014 #include "MapEditor.h"
00015 #include <iostream>
00016 #include <algorithm>
00017 
00018 using namespace Editor;
00019 
00020 const int MapEditor::WndPos::TILE_MAT_HORIZ_OFFSET = 50;
00021 const int MapEditor::WndPos::TILE_MAT_VERT_OFFSET =10;
00022 const int MapEditor::WndPos::TILE_SET_HORIZ_OFFSET=10;
00023 const int MapEditor::WndPos::TILE_SET_VERT_OFFSET = 50;
00024 
00025 MapEditor::EditorTileMatrixReader::EditorTileMatrixReader(MapEditor& ed) :
00026 editor(ed)
00027 { }
00028 
00029 void MapEditor::EditorTileMatrixReader::getValue(std::istream& in) {
00030         Util::Matrix<int> tileIndicies(editor.tileMatrix.numRows(), editor.tileMatrix.numCols(), -1);
00031         in >> tileIndicies;
00032         for (int r = 0; r < tileIndicies.numRows(); r++) {
00033                 for (int c = 0; c < tileIndicies.numCols(); c++) {
00034         //              std::cout << tileIndicies(r,c) << " ";
00035                         int tileIndex = tileIndicies(r,c);
00036                         editor.tileMatrix(r,c) = new EditorTile(editor.tileSetEd.copyTile(tileIndex), tileIndex);
00037                 }
00038         //      std::cout << "\n" << std::endl;
00039         }
00040 }
00041 
00042 void MapEditor::EditorTileMatrixReader::finishingAction() {
00043         // validate readings
00044         for (int r = 0; r < editor.tileMatrix.numRows(); r++) {
00045                 for (int c = 0; c < editor.tileMatrix.numCols(); c++) {
00046                         if (editor.tileMatrix(r,c) == NULL) {
00047                                 throw Util::GeneralException("NULL tile after reading file " + editor.filename+ " at (" +
00048                                  Util::toString(r) + " , " + Util::toString(c) + ")", __FILE__, __LINE__);
00049                         }
00050                 }
00051         }
00052 }
00053 
00054 MapEditor::MapEditor(const std::string& name) :
00055 tileMatrix(Game::Constants::MAP_NUM_ROWS, Game::Constants::MAP_NUM_COLS, NULL),
00056 filename(name),
00057 startLocPic(new Graphics::SingleFrameImage("Editor/Data/StartPic.bmp"))
00058 {
00059         std::ifstream in(name.c_str());
00060         if (!in.good()) throw Util::GeneralException("Could not open " + filename + " for reading", __FILE__, __LINE__);
00061         Parse::SimpleParser parser(in);
00062 
00063         parser.addAction("TileSet",                     new Parse::StdRead<TileSetEditor>(tileSetEd));
00064         parser.addAction("StartLocation",       new Parse::NaturalListParser<Game::MapLocation>(startLocs));
00065         parser.addAction("MapData",                     new EditorTileMatrixReader(*this));     
00066 
00067         parser.read();
00068 
00069 }
00070 
00071 void MapEditor::changeMapName(const std::string& str) {
00072         filename = str;
00073 }
00074 
00075 void MapEditor::handleMouseClick(SDL_MouseButtonEvent& event) {
00076         int r = (event.x - WndPos::TILE_MAT_HORIZ_OFFSET) / Game::Constants::TILE_WIDTH;
00077         int c = (event.y - WndPos::TILE_MAT_VERT_OFFSET) / Game::Constants::TILE_HEIGHT;
00078         
00079         if (r < tileMatrix.numRows() && c < tileMatrix.numCols() && r >= 0 && c>=0) {
00080                 if (SDL_BUTTON_LEFT  == event.button  ) tileMatrix(r,c)->incrementTile(tileSetEd);
00081                 else if (SDL_BUTTON_RIGHT == event.button ) toggleStartLoc(r,c);
00082                 else if (SDL_BUTTON_MIDDLE == event.button) tileMatrix(r,c)->decrementTile(tileSetEd);
00083         }
00084 }
00085 
00086 MapEditor::~MapEditor() {
00087         saveMap();
00088         for (int i = 0; i < tileMatrix.numRows(); i++) {
00089                 for (int j = 0; j < tileMatrix.numCols(); j++) {
00090                         delete tileMatrix(i,j);
00091                 }
00092         }
00093         delete startLocPic;
00094         Util::freeDynamicContainer(startLocs);
00095 }
00096 
00097 void MapEditor::saveMap() {
00098         std::ofstream out(filename.c_str());
00099         out << tileSetEd;
00100         out << "MapData=\n";
00101         for (int c = 0; c < tileMatrix.numCols(); c++) {
00102                 for (int r = 0; r < tileMatrix.numRows(); r++) {
00103                         out << tileMatrix(r,c)->getIndex() << " ";
00104                 }
00105                 out << std::endl;
00106         }
00107         for (std::list<Game::MapLocation*>::iterator i = startLocs.begin(); i != startLocs.end(); i++) {
00108                 out << "StartLocation {\n";
00109                 out << "\tx=" << (*i)->getMapX() << "\n";
00110                 out << "\ty=" << (*i)->getMapY() << "\n";
00111                 out << "}\n";
00112         }
00113 }
00114 
00115 void MapEditor::toggleStartLoc(int row, int col) {
00116         std::list<Game::MapLocation*>::iterator check = std::find_if(startLocs.begin(), startLocs.end(), Game::MapLocAt(row, col));
00117         if (check != startLocs.end()) {
00118                 delete *check;
00119                 startLocs.erase(check);
00120         } else {
00121                 startLocs.push_back(new Game::MapLocation(row,col));
00122         }               
00123 }
00124 
00125 void MapEditor::draw(SDL_Surface* surf) {
00126         SDL_Rect rect;
00127 
00128         // draw tile matrix
00129         rect.x = WndPos::TILE_MAT_HORIZ_OFFSET;
00130         rect.y = WndPos::TILE_MAT_VERT_OFFSET;
00131         for (int r = 0; r < tileMatrix.numRows(); r++) {
00132                 rect.y = WndPos::TILE_MAT_VERT_OFFSET;
00133                 for (int c = 0; c < tileMatrix.numCols(); c++) {
00134                         tileMatrix(r,c)->drawAt(surf,&rect);
00135                         rect.y += Game::Constants::TILE_HEIGHT;
00136                 }
00137                 rect.x += Game::Constants::TILE_WIDTH;
00138         }
00139 
00140         for (std::list<Game::MapLocation*>::iterator it = startLocs.begin(); it != startLocs.end(); it++) {
00141                 rect.x = (*it)->getMapX()*Game::Constants::TILE_WIDTH+WndPos::TILE_MAT_HORIZ_OFFSET;
00142                 rect.y = (*it)->getMapY()*Game::Constants::TILE_HEIGHT+WndPos::TILE_MAT_VERT_OFFSET;            
00143                 startLocPic->drawAt(surf,&rect);
00144         }
00145 
00146         // draw tile set editor
00147         rect.x = WndPos::TILE_MAT_HORIZ_OFFSET + WndPos::TILE_SET_HORIZ_OFFSET
00148                 + Game::Constants::TILE_WIDTH * tileMatrix.numRows();
00149 
00150         rect.y = WndPos::TILE_MAT_VERT_OFFSET + WndPos::TILE_SET_VERT_OFFSET;
00151 
00152         tileSetEd.drawAt(surf, &rect);  
00153 }               
00154 

Generated on Tue May 21 07:26:51 2002 for BomberLAN by doxygen1.2.12 written by Dimitri van Heesch, © 1997-2001