Navigation
Artikel
Stuff
RSS Feeds
|
Tutorials - Programmieren eines einfachen 2D LeveleditorsSprachenübersicht/Programmierung/C / C++/ C#/Spieleprogrammierung Keywords: Leveleditor programmieren, 2D, DirectX, Level speichern, laden, stream, struktur, map, engine Inhaltsverzeichnis
Vorwort Top
Feldstruktur Top
Code: struct LevelData { unsigned int iMapID; //Speichert die ID der map unsigned int iWidth; //Speichert die Breite der map unsigned int iHeight; //Speichert die Anzahl der Felder in der Höhe };
Feld Top
Code: struct MapField { char* strBitmapName; TeleportField Teleport; };
Levelübergänge Top
Code: struct TeleportField { int iMapID; int iTargetWidth; int iTargetHeight; int iTeleporttype; };
Grundgerüst Top
Code: //This class manages the 2D Levels class Level { public: //Ruft die 2. Funktion auf, mit weniger Informationen void Init(LevelData *pLevelData) { } //Diese Funktion initialisiert die Klasse, dabei wird ein Pointer zu den Headerinformationen übergeben void Init(LevelData *pLevelData,std::string strStandardFilename,int iWidth, int iHeight) { } //Diese Funktion ändert die Größe der map, ohne die Felder zu verändern void SetSizeOfMap(int iWidth, int iHeight) { } //Hier wird eine komplett neue Map mit dem Standarddateinamen erstellt void MakeNewMap(int iWidth, int iHeight,std::string strStandardFilename) { } //Gibt ein Feld zurück MapField* GetField(int iWidth, int iHeight) { } //Speichert das Level int SaveLvl(std::string strFilename) { } //Ladet das Level void LoadLvl(std::string strFilename) { } private: //This struct saves the header of the file LevelData *m_pLevelDatas; };
Code: //Initialize the class void Init(LevelData *pLevelData) { this->Init(pLevelData,"",0,0); } //Initialize the class void Init(LevelData *pLevelData,std::string strStandardFilename,int iWidth, int iHeight) { m_pLevelDatas = pLevelData; m_pLevelDatas->iWidth = iWidth; m_pLevelDatas->iHeight = iHeight; if(iWidth && iHeight) this->MakeNewMap(iWidth,iHeight,pStandardFilename); }
Code: vector < vector <MapField> > m_MapField;
Code: //Reserviert iWidth Elemente m_MapField.resize(m_pLevelDatas->iWidth); //Geht alle Elemente durch und reserviert für jedes iHeight Elemente for(int i = 0;i < m_pLevelDatas->iWidth;i++) { //Reserviert iHeight Elemente m_MapField[i].resize(m_pLevelDatas->iHeight); //Setzt alle auf den Standard for(int z = 0;z < m_pLevelDatas->iHeight;z++) { m_MapField[i][z].strBitmapName = (char*)strStandardFilename; m_MapField[i][z].Teleport.iMapID = 0; } }
Code: //Resize it an sets it 0 for(int i = 0;i < m_pLevelDatas->iWidth;i++) m_MapField[i].clear(); m_MapField.clear();
Code: //Makes a new map (deletes all old datas) void MakeNewMap(int iWidth, int iHeight,std::string strStandardFilename) { m_pLevelDatas->iWidth = iWidth; m_pLevelDatas->iHeight = iHeight; //Clears it if there is already a version if(!m_bMap) m_bMap = true; else { //Resize it an sets it 0 for(int i = 0;i < m_pLevelDatas->iWidth;i++) m_MapField[i].clear(); m_MapField.clear(); } //Creats iWidth elements m_MapField.resize(m_pLevelDatas->iWidth); //Creates for each iWidth element iHeight elements for(int i = 0;i < m_pLevelDatas->iWidth;i++) { m_MapField[i].resize(m_pLevelDatas->iHeight); for(int z = 0;z < m_pLevelDatas->iHeight;z++) { //Sets it 0/pStandardFilename m_MapField[i][z].strBitmapName = (char*)strStandardFilename; m_MapField[i][z].Teleport.iMapID = 0; } } }
Code: //Returns the Field MapField* GetField(int iX, int iY) { if ((iX > m_pLevelDatas->iWidth) || (iY > iHeight)) { std::cout << "Error, field doesn't exist." << std::endl; } return &m_MapField[iX-1][iY-1]; }
Speichern/Laden Top
Code: //Saves the level int SaveLvl(std:string strFilename) { if(!m_bMap) return 1; std::ofstream ofStream; //Opens the file binary ofStream.open(strFilename.c_str(),fstream::out | fstream::binary); //Writes the LevelData ofStream.write((char*)m_pLevelDatas, sizeof(*m_pLevelDatas)); //Writes all Fields into the file for(int z = 0;z < m_pLevelDatas->iWidth;z++) for(int i = 0;i < m_pLevelDatas->iHeight;i++) ofStream.write((char*)&m_MapField[z][i], sizeof(m_MapField[z][i])); //Close the file ofStream.close(); return 0; }
Code: //Loads the level void LoadLvl(std::string strFilename) { std::ifstream ifStream; //Opens the file binary ifStream.open(strFilename.c_str(),std::fstream::in | std::fstream::binary); //Reads it ifStream.read((char*)m_pLevelDatas,sizeof(*m_pLevelDatas)); this->MakeNewMap(m_pLevelDatas->iWidth, m_pLevelDatas->iHeight, ""); for(int z = 0;z < m_pLevelDatas->iWidth;z++) for(int i = 0;i < m_pLevelDatas->iHeight;i++) ifStream.read((char*)&m_MapField[z][i],sizeof(m_MapField[z][i])); //Close the file ifStream.close(); }
Der gesammte Code Top
Code: //********************* Level.h ****************************// // This class manages the 2D Levels of the Engine // //**************************************************************************************// //Include files #include <iostream> #include <fstream> #include <vector> //Saves the stuff struct LevelData { unsigned int iMapID; unsigned int iWidth; unsigned int iHeight; }; //Saves the informations if its a teleport field struct TeleportField { int iMapID; int iTargetWidth; int iTargetHeight; int iTeleporttype; }; //Saves the field informations struct MapField { char* strBitmapName; TeleportField Teleport; }; //This class manages the 2D Levels class Level { public: //Initialize the class void Init(LevelData *pLevelData) { this->Init(pLevelData,"",0,0); } //Initialize the class void Init(LevelData *pLevelData,std::string strStandardFilename,int iWidth, int iHeight) { m_pLevelDatas = pLevelData; m_pLevelDatas->iWidth = iWidth; m_pLevelDatas->iHeight = iHeight; if(iWidth && iHeight) this->MakeNewMap(iWidth,iHeight,strStandardFilename); } void SetSizeOfMap(int iWidth, int iHeight) { } //Makes a new map (deletes all old datas) void MakeNewMap(int iWidth, int iHeight,std::string strStandardFilename) { m_pLevelDatas->iWidth = iWidth; m_pLevelDatas->iHeight = iHeight; //Clears it if there is already a version if(!m_bMap) m_bMap = true; else { //Resize it an sets it 0 for(int i = 0;i < m_pLevelDatas->iWidth;i++) m_MapField[i].clear(); m_MapField.clear(); } //Creats iWidth elements m_MapField.resize(m_pLevelDatas->iWidth); //Creates for each iWidth element iHeight elements for(int i = 0;i < m_pLevelDatas->iWidth;i++) { m_MapField[i].resize(m_pLevelDatas->iHeight); for(int z = 0;z < m_pLevelDatas->iHeight;z++) { //Sets it 0/pStandardFilename m_MapField[i][z].strBitmapName = (char*)strStandardFilename; m_MapField[i][z].Teleport.iMapID = 0; } } } //Returns the Field MapField* GetField(int iX, int iY) { if ((iX > m_pLevelDatas->iWidth) || (iY > m_pLevelDatas->iHeight)) { std::cout << "Error, field doesn't exist." << std::endl; } return &m_MapField[iX-1][iY-1]; } //Gives the map out, only for test (DOS) void GiveMapOut() { for(int i = 0;i < m_pLevelDatas->iHeight;i++) { for(int z = 0;z < m_pLevelDatas->iWidth;z++) { std::cout << m_MapField[z][i].pBitmapName << " "; } std::cout << std::endl; } } //Saves the level int SaveLvl(std::string strFilename) { if(!m_bMap) return 1; std::ofstream ofStream; //Opens the file binary ofStream.open(strFilename.c_str(),std::fstream::out | std::fstream::binary); //Writes the LevelData ofStream.write((char*)m_pLevelDatas, sizeof(*m_pLevelDatas)); for(int z = 0;z < m_pLevelDatas->iWidth;z++) for(int i = 0;i < m_pLevelDatas->iHeight;i++) ofStream.write((char*)&m_MapField[z][i], sizeof(m_MapField[z][i])); //Close the file ofStream.close(); return 0; } //Loads the level void LoadLvl(std::string strFilename) { std::ifstream ifStream; //Opens the file binary ifStream.open(strFilename.c_str(),std::fstream::in | std::fstream::binary); //Reads it ifStream.read((char*)m_pLevelDatas,sizeof(*m_pLevelDatas)); this->MakeNewMap(m_pLevelDatas->iWidth, m_pLevelDatas->iHeight, ""); for(int z = 0;z < m_pLevelDatas->iWidth;z++) for(int i = 0;i < m_pLevelDatas->iHeight;i++) ifStream.read((char*)&m_MapField[z][i],sizeof(m_MapField[z][i])); //Close the file ifStream.close(); } private: //If the map exist bool m_bMap; //The map std::vector < std::vector <MapField> > m_MapField; //This struct saves the header of the file LevelData *m_pLevelDatas; };
Code: //******************************* FOR MORE SEE THE .h file ********************************// #include "Level.h" #include <cstdio> LevelData LevelInfo; Level Lvl; using namespace std; int main() { LevelInfo.iMapID = 36; Lvl.Init(&LevelInfo,"Test.jpg",5,4); Lvl.GetField(1,1)->pBitmapName = "sdf.png"; Lvl.GetField(3,4)->pBitmapName = "sdf.png"; Lvl.SetSizeOfMap(1,1); Lvl.GiveMapOut(); Lvl.SaveLvl("test.drg"); Lvl.LoadLvl("test.drg"); cout << "ende" << endl; cout.flush(); getchar(); return 0; }
Weitere Theorie Top
Pseudocode: //Geht alle Felder durch for(int iHeight = 0;iHeight < LevelInfo.iHeight;iHeight++) { for(int iWidth = 0;iWidth < LevelInfo.iWidth;iWidth++) { //Zeichnet das Sprite (pBitmapName) //Wenn das Sprite nicht ausserhalb des Bildschirms ist (60 ober und links vom Bildschirm, und über den unteren und rechten Teil des Bildschirms if(((x+ScrollX> -60) && (x+ScrollX < MeineXAuslösung) && ((y+ScrollY > -60) && (y+ScrollY < MeineXAuflösung)))) MeineDrawFunktion(Lvl.GetField(x,y)->pBitmapName,x+ScrollX,y+ScrollY); //Sprite ist 50 Breit x += 50; } //Sprite ist 50 hoch y += 50; }
Schlusswort Top
Gibt es noch irgendwelche Fragen, oder wollen Sie über den Artikel diskutieren? Sprachenübersicht/Programmierung/C / C++/ C#/Spieleprogrammierung/Programmieren eines einfachen 2D Leveleditors |