Stilllegung des Forums
Das Forum wurde am 05.06.2023 nach über 20 Jahren stillgelegt (weitere Informationen und ein kleiner Rückblick).
Registrierungen, Anmeldungen und Postings sind nicht mehr möglich. Öffentliche Inhalte sind weiterhin zugänglich.
Das Team von spieleprogrammierer.de bedankt sich bei der Community für die vielen schönen Jahre.
Wenn du eine deutschsprachige Spieleentwickler-Community suchst, schau doch mal im Discord und auf ZFX vorbei!
Werbeanzeige
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
#ifndef _CSETTINGS_H_ #define _CSETTINGS_H_ //Standard #include <string> #include <fstream> //Allgemein #include "defines.hpp" #include "enums.hpp" //namespace engine ---------------------------------------------------------------------------------------------------- namespace engine { //--------------------------------------------------------------------------------------------------------------------- //CSettings //--------------------------------------------------------------------------------------------------------------------- class CSettings { public: //------------------------------------------------------------------------------------------------------------- //Konstruktor //------------------------------------------------------------------------------------------------------------- CSettings(); //------------------------------------------------------------------------------------------------------------- //Destruktor //------------------------------------------------------------------------------------------------------------- ~CSettings(); //------------------------------------------------------------------------------------------------------------- //Methoden //------------------------------------------------------------------------------------------------------------- //Settings aus Datei laden EExitCode loadFromFile(const std::string& stFileName); //Settings in Datei schreiben EExitCode writeToFile(const std::string& stFileName); //------------------------------------------------------------------------------------------------------------- //Interface //------------------------------------------------------------------------------------------------------------- inline int getScreenWidth(){return m_screenWidth;} inline void setScreenWidth(const int screenWidth){m_screenWidth = screenWidth;} inline int getScreenHeight(){return m_screenHeight;} inline void setScreenHeight(const int screenHeight){m_screenHeight = screenHeight;} inline int getScreenDepth(){return m_screenDepth;} inline void setScreenDepth(const int screenDepth){m_screenDepth = screenDepth;} inline std::string getTitle(){return m_stTitle;} inline void setTitle(const std::string stTitle){m_stTitle = stTitle;} inline bool getVSync(){return m_bVSync;} inline void setVSync(const bool bVSync){m_bVSync = bVSync;} inline bool getParticleEffects(){return m_bParticleEffects;} inline void setParticleEffects(const bool bParticleEffects){m_bParticleEffects = bParticleEffects;} inline ESetting getParticleEffectsQuality(){return m_particleEffectsQuality;} inline void setParticleEffectsQuality(const ESetting particleEffectsQuality){m_particleEffectsQuality = particleEffectsQuality;} inline bool getSound(){return m_bSound;} inline void setSound(const bool bSound){m_bSound = bSound;} inline ESetting getSoundQuality(){return m_soundQuality;} inline void setSoundQuality(const ESetting soundQuality){m_soundQuality = soundQuality;} inline int getSoundVolume(){return m_soundVolume;} inline void setSoundVolume(const int soundVolume){m_soundVolume = soundVolume;} inline bool getMusic(){return m_bMusic;} inline void setMusic(const bool bMusic){m_bMusic = bMusic;} inline ESetting getMusicQuality(){return m_musicQuality;} inline void setMusicQuality(const ESetting musicQuality){m_musicQuality = musicQuality;} inline int getMusicVolume(){return m_musicVolume;} inline void setMusicVolume(const int musicVolume){m_musicVolume = musicVolume;} private: //------------------------------------------------------------------------------------------------------------- //Elemente //------------------------------------------------------------------------------------------------------------- int m_screenWidth; //Fensterbreite int m_screenHeight; //Fensterhöhe int m_screenDepth; //Farbtiefe std::string m_stTitle; //Fenstertitel bool m_bFullscreen; //Vollbild bool m_bVSync; //VSync bool m_bParticleEffects; //Partikeleffekte ESetting m_particleEffectsQuality; //Qualität der Partikeleffekte bool m_bSound; //Sound ESetting m_soundQuality; //Qualität des Sounds int m_soundVolume; //Lautstärke des Sounds bool m_bMusic; //Musik ESetting m_musicQuality; //Qualität der Musik int m_musicVolume; //Lautstärke der Musik };//CSettings --------------------------------------------------------------------------------------------------------- }//namespace engine --------------------------------------------------------------------------------------------------- #endif |
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
#include "CSettings.hpp" //namespace engine ---------------------------------------------------------------------------------------------------- namespace engine { //--------------------------------------------------------------------------------------------------------------------- //Konstruktor //--------------------------------------------------------------------------------------------------------------------- CSettings::CSettings() { m_screenWidth = 0; m_screenHeight = 0; m_screenDepth = 0; m_stTitle = ""; m_bFullscreen = false; m_bVSync = false; m_bParticleEffects = false; m_particleEffectsQuality = SETTING_NONE; m_bSound = false; m_soundQuality = SETTING_NONE; m_soundVolume = 0; m_bMusic = false; m_musicQuality = SETTING_NONE; m_musicVolume = 0; }//Konstruktor -------------------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------------------------------- //Destruktor //--------------------------------------------------------------------------------------------------------------------- CSettings::~CSettings() { }//Destruktor --------------------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------------------------------- //Funktion : loadFromFile //Aufgabe : Settings aus Datei laden //Rückgabe : ExitCode //--------------------------------------------------------------------------------------------------------------------- EExitCode CSettings::loadFromFile(const std::string& stFileName) { std::ifstream input(stFileName, std::ios::binary); if(input == NULL) { return(RESULT_ERROR); } else { input.read((char*) this, sizeof(CSettings)); input.close(); return(RESULT_OK); } return(RESULT_OK); }//loadFromFile ------------------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------------------------------- //Funktion : writeToFile //Aufgabe : Settings in Datei schreiben //Rückgabe : ExitCode //--------------------------------------------------------------------------------------------------------------------- EExitCode CSettings::writeToFile(const std::string& stFileName) { std::ofstream output(stFileName, std::ios::binary); if(output == NULL) { return(RESULT_ERROR); } else { output.write((char*) this, sizeof(CSettings)); output.close(); return(RESULT_OK); } return(RESULT_OK); }//writeToFile -------------------------------------------------------------------------------------------------------- }//namespace engine --------------------------------------------------------------------------------------------------- |
C-/C++-Quelltext |
|
1 |
CSettings* m_pSettings
|
C-/C++-Quelltext |
|
1 |
m_pSettings = NULL
|
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 |
EExitCode CEve::init() { if(m_bInitialized != true) { //Settings erstellen und laden m_pSettings = new CSettings(); m_pSettings->loadFromFile("settings.dat"); |
C-/C++-Quelltext |
|
1 |
m_pSettings->loadFromFile("settings.dat"); |
Community-Fossil
Beruf: Teamleiter Mobile Applikationen & Senior Software Engineer
Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von »BlueCobold« (03.11.2013, 17:11)
Echt? Ist es nicht eher so, daß der Compiler nur bei der Angabe "inline" entscheidet, ob das ne gute Idee ist oder nicht, aber ohne Angabe "inline" gar nicht auf die Idee kommt, Inline zu verwenden?Zitat
Kleiner Nebenhinweis: Das ganze "inline" kannst Du Dir schenken. Das
macht der Compiler oder lässt es bleiben, unabhängig von Deiner Angabe.
Das ist mir schon klar, wie man an meinem ersten Post sieht... wird die Zeile nicht aufgerufen, gibt es auch kein Problem. Irgendwelche Voschläge?Zitat
Dein Problem ist übrigens das hier:
input.read((char*) this, sizeof(CSettings));
Community-Fossil
Beruf: Teamleiter Mobile Applikationen & Senior Software Engineer
Echt.Echt?
Nein. Das war mal. Vor langer Zeit. Irgendwann.Ist es nicht eher so, daß der Compiler nur bei der Angabe "inline" entscheidet, ob das ne gute Idee ist oder nicht, aber ohne Angabe "inline" gar nicht auf die Idee kommt, Inline zu verwenden?
Klar, lies alle Member einzeln und schreib sie einzeln. Alignment/Packing macht sonst noch ganze andere wilde Dinge, die bei einem anderen Compiler plötzlich dazu führen, dass Deine Files nicht mehr gelesen werden können.Das ist mir schon klar, wie man an meinem ersten Post sieht... wird die Zeile nicht aufgerufen, gibt es auch kein Problem. Irgendwelche Voschläge?
Zum Beispiel? Also verabschiede ich mich besser mal davon, Instanzen "am Stück" in Dateien zu schreiben und zu lesen!? Mhm...Zitat
Alignment/Packing macht sonst noch ganze andere wilde Dinge
Ich werde eben langsam alt Danke für den Hinweis!Zitat
Nein. Das war mal. Vor langer Zeit. Irgendwann.
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
//--------------------------------------------------------------------------------------------------------------------- //CSettings //--------------------------------------------------------------------------------------------------------------------- class CSettings { public: //----------------------------------------------------------------------------------------------------- //Konstruktoren //----------------------------------------------------------------------------------------------------- CSettings(); CSettings(const int screenWidth, const int screenHeight, const int screenDepth, const std::string& stTitle, const bool bFullscreen, const bool bVSync, const bool bParticleEffects, const ESetting particleEffectsQuality, const bool bSound, const ESetting soundQuality, const int soundVolume, const bool bMusic, const ESetting musicQuality, const int musicVolume); //----------------------------------------------------------------------------------------------------- //Destruktor //----------------------------------------------------------------------------------------------------- ~CSettings(); //----------------------------------------------------------------------------------------------------- //Methoden //----------------------------------------------------------------------------------------------------- //Settings aus Datei laden EExitCode loadFromFile(const std::string& stFileName); //Settings in Datei schreiben EExitCode writeToFile(const std::string& stFileName); //----------------------------------------------------------------------------------------------------- //Interface //----------------------------------------------------------------------------------------------------- private: //----------------------------------------------------------------------------------------------------- //Elemente //----------------------------------------------------------------------------------------------------- int m_screenWidth; //Fensterbreite int m_screenHeight; //Fensterhöhe int m_screenDepth; //Farbtiefe std:: string m_stTitle; //Fenstertitel bool m_bFullscreen; //Vollbild bool m_bVSync; //VSync bool m_bParticleEffects; //Partikeleffekte ESetting m_particleEffectsQuality; //Qualität der Partikeleffekte bool m_bSound; //Sound ESetting m_soundQuality; //Qualität des Sounds int m_soundVolume; //Lautstärke des Sounds bool m_bMusic; //Musik ESetting m_musicQuality; //Qualität der Musik int m_musicVolume; //Lautstärke der Musik };//CSettings --------------------------------------------------------------------------------------------------------- |
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
//--------------------------------------------------------------------------------------------------------------------- //Konstruktor //--------------------------------------------------------------------------------------------------------------------- CSettings::CSettings() : m_screenWidth(0) , m_screenHeight(0) , m_screenDepth(0) , m_stTitle("") , m_bFullscreen(false) , m_bVSync(false) , m_bParticleEffects(false) , m_particleEffectsQuality(SETTING_NONE) , m_bSound(false) , m_soundQuality(SETTING_NONE) , m_soundVolume(0) , m_bMusic(false) , m_musicQuality(SETTING_NONE) , m_musicVolume(0) { }//Konstruktor -------------------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------------------------------- //Konstruktor //--------------------------------------------------------------------------------------------------------------------- CSettings::CSettings(const int screenWidth, const int screenHeight, const int screenDepth, const std::string& stTitle, const bool bFullscreen, const bool bVSync, const bool bParticleEffects, const ESetting particleEffectsQuality, const bool bSound, const ESetting soundQuality, const int soundVolume, const bool bMusic, const ESetting musicQuality, const int musicVolume) : m_screenWidth(screenWidth) , m_screenHeight(screenHeight) , m_screenDepth(32) , m_stTitle(stTitle) , m_bFullscreen(bFullscreen) , m_bVSync(bVSync) , m_bParticleEffects(bParticleEffects) , m_particleEffectsQuality(particleEffectsQuality) , m_bSound(bSound) , m_soundQuality(soundQuality) , m_soundVolume(soundVolume) , m_bMusic(bMusic) , m_musicQuality(musicQuality) , m_musicVolume(musicVolume) { }//Konstruktor -------------------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------------------------------- //Destruktor //--------------------------------------------------------------------------------------------------------------------- CSettings::~CSettings() { }//Destruktor --------------------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------------------------------- //Funktion : loadFromFile //Aufgabe : Settings aus Datei laden //Rückgabe : ExitCode //--------------------------------------------------------------------------------------------------------------------- EExitCode CSettings::loadFromFile(const std::string& stFileName) { std::ifstream input(stFileName, std::ios::binary); if(!input.good()) { input.clear(); return(RESULT_ERROR); } else { input.read((char*) &m_screenWidth, sizeof(m_screenWidth)); input.read((char*) &m_screenHeight, sizeof(m_screenHeight)); input.read((char*) &m_screenDepth, sizeof(m_screenDepth)); int size = 0; input.read((char*) &size, sizeof(size)); m_stTitle.resize(size); input.read((char*) (m_stTitle.c_str()), size); input.read((char*) &m_bFullscreen, sizeof(m_bFullscreen)); input.read((char*) &m_bVSync, sizeof(m_bVSync)); input.read((char*) &m_bParticleEffects, sizeof(m_bParticleEffects)); input.read((char*) &m_particleEffectsQuality, sizeof(m_particleEffectsQuality)); input.read((char*) &m_bSound, sizeof(m_bSound)); input.read((char*) &m_soundQuality, sizeof(m_soundQuality)); input.read((char*) &m_soundVolume, sizeof(m_soundVolume)); input.read((char*) &m_bMusic, sizeof(m_bMusic)); input.read((char*) &m_musicQuality, sizeof(m_musicQuality)); input.read((char*) &m_musicVolume, sizeof(m_musicVolume)); input.close(); return(RESULT_OK); } }//loadFromFile ------------------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------------------------------- //Funktion : writeToFile //Aufgabe : Settings in Datei schreiben //Rückgabe : ExitCode //--------------------------------------------------------------------------------------------------------------------- EExitCode CSettings::writeToFile(const std::string& stFileName) { std::ofstream output(stFileName, std::ios::binary); if(!output.good()) { output.clear(); return(RESULT_ERROR); } else { output.write((char*) &m_screenWidth, sizeof(m_screenWidth)); output.write((char*) &m_screenHeight, sizeof(m_screenHeight)); output.write((char*) &m_screenDepth, sizeof(m_screenDepth)); int size = m_stTitle.length(); output.write((char*) &size, sizeof(size)); output.write(m_stTitle.c_str(), size); output.write((char*) &m_bFullscreen, sizeof(m_bFullscreen)); output.write((char*) &m_bVSync, sizeof(m_bVSync)); output.write((char*) &m_bParticleEffects, sizeof(m_bParticleEffects)); output.write((char*) &m_particleEffectsQuality, sizeof(m_particleEffectsQuality)); output.write((char*) &m_bSound, sizeof(m_bSound)); output.write((char*) &m_soundQuality, sizeof(m_soundQuality)); output.write((char*) &m_soundVolume, sizeof(m_soundVolume)); output.write((char*) &m_bMusic, sizeof(m_bMusic)); output.write((char*) &m_musicQuality, sizeof(m_musicQuality)); output.write((char*) &m_musicVolume, sizeof(m_musicVolume)); output.close(); return(RESULT_OK); } }//writeToFile -------------------------------------------------------------------------------------------------------- |
Das was du tust funktioniert nur zufällig. Theoretisch könnte das Programm genau so gut abstürzen, sobald es mit einem anderen Compiler oder einer anderen Implementierung der Standardbibliothek kompiliert wird. Das saubere Verhalten wäre, die String-Größe auszulesen, einen Buffer mit der entsprechenden Größe anzulegen, diesen zu füllen, und dem String dann den Bufferinhalt zuzuweisen.
Werbeanzeige