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
Alter Hase
Zitat von »"xxendlessxx"«
Wie sollte man am besten die Gamestates verwalten und abfragen?
Alter Hase
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 |
#include <SFML/Window.hpp> #include <SFML/Graphics.hpp> #include "../header/cGame.hpp" #include "../header/cIntro_State.hpp" cGame Game; int main() { Game.Initialize(); // load the intro Game.ChangeState( cIntroState::Instance() ); while(Game.GetGameStatus() == true) { //Events Game.ManageEvents(); //Logic //Render Game.Clear(); Game.Render(); } Game.CleanUp(); return 0; } |
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 |
#include <SFML/Window.hpp> #include <SFML/Graphics.hpp> #include <vector> #ifndef _CGAME_HPP_ #define _CGAME_HPP_ class cGameState; class cGame { public: sf::RenderWindow Window; sf::Event Event; sf::Font Font; void Initialize(); void CleanUp(); void ChangeState(cGameState* State); void PushState(cGameState* State); void PopState(); void ManageEvents(); void Clear(); void Render(); bool GetGameStatus(); void SetGameStatus(bool NewGameStatus); private: // the stack of states std::vector<cGameState*> States; bool GameIsRunning; }; #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 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
#include "../header/cGame.hpp" #include <vector> void cGame::Initialize() { this->Window.Create(sf::VideoMode(1024,768),"Projektarbeit",sf::Style::Close); this->SetGameStatus(true); } void cGame::CleanUp() { // cleanup the all states while ( !States.empty() ) { States.back()->CleanUp(); States.pop_back(); } this->Window.Close(); } void cGame::Render() { States.back()->Render(this); } void cGame::Clear() { States.back()->Clear(this); } void cGame::ManageEvents() { while(this->Window.GetEvent(this->Event)) { switch(this->Event.Type) { case sf::Event::Closed: { this->GameIsRunning = false; break; } case sf::Event::KeyPressed: { switch(this->Event.Key.Code) { case sf::Key::Escape: { this->GameIsRunning = false; break; } } break; } } } } bool cGame::GetGameStatus() { return this->GameIsRunning; } void SetGameStatus(bool NewGameStatus) { this->GameIsRunning = NewGameStatus; } void cGame::ChangeState(cGameState* State) { // cleanup the current state if ( !States.empty() ) { States.back()->CleanUp(); States.pop_back(); } // store and init the new state States.push_back(State); States.back()->Initialize(); } void cGame::PushState(cGameState* State) { // pause current state if ( !States.empty() ) { States.back()->Pause(); } // store and init the new state States.push_back(State); States.back()->Initialize(); } void cGame::PopState() { // cleanup the current state if ( !States.empty() ) { States.back()->CleanUp(); States.pop_back(); } // resume previous state if ( !States.empty() ) { States.back()->Resume(); } } |
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 |
#include <SFML/Window.hpp> #include <SFML/Graphics.hpp> #ifndef __CGAMESTATE_HPP__ #define __CGAMESTATE_HPP__ #include "../header/cGame.hpp" class cGameState { public: virtual void Initialize() = 0; virtual void CleanUp() = 0; virtual void Pause() = 0; virtual void Resume() = 0; virtual void ManageEvents(cGame* game) = 0; virtual void Clear(cGame* game) = 0; virtual void Render(cGame* game) = 0; void ChangeState(cGame* game, cGameState* state) { game->ChangeState(state); } protected: cGameState() { } }; #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 |
#ifndef __INTRO_STATE_HPP__ #define __INTRO_STATE_HPP__ #include "../header/cGameState.hpp" class cIntroState : public cGameState { public: void Initialize(); void CleanUp(); void Pause(); void Resume(); void ManageEvents(cGame* game); void Clear(cGame* game); void Render(cGame* game); static cIntroState* Instance() { return &m_IntroState; } protected: cIntroState() { } private: static cIntroState m_IntroState; }; #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 |
#include "../header/cGame.hpp" #include "../header/cGameState.hpp" #include "../header/cIntro_State.hpp" cIntroState cIntroState::m_IntroState; void cIntroState::Initialize() { printf("Intro wird Initialisiert!"); } void cIntroState::CleanUp() { printf("Intro wird aufgeraeumt!"); } void cIntroState::Pause() { printf("Intro Pause\n"); } void cIntroState::Resume() { printf("Intro Resume\n"); } void cIntroState::ManageEvents(cGame* game) { while(game->Window.GetEvent(game->Event)) { switch(game->Event.Type) { case sf::Event::Closed: { game->SetGameStatus(false); break; } case sf::Event::KeyPressed: { switch(game->Event.Key.Code) { case sf::Key::Escape: { game->SetGameStatus(false); break; } } break; } } } } void cIntroState::Clear(cGame* game) { game->Window.Clear(sf::Color(255,0,0)); } void cIntroState::Render(cGame* game) { game->Window.Display(); } |
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 |
1>------ Erstellen gestartet: Projekt: Projektarbeit, Konfiguration: Release Win32 ------ 1>Kompilieren... 1>cGame.cpp 1>.\source\cGame.cpp(14) : error C2027: Verwendung des undefinierten Typs "cGameState" 1> c:\my dropbox\projektarbeit\source\source\../header/cGame.hpp(8): Siehe Deklaration von 'cGameState' 1>.\source\cGame.cpp(14) : error C2227: Links von "->CleanUp" muss sich ein Zeiger auf Klassen-/Struktur-/Union-/generischen Typ befinden. 1>.\source\cGame.cpp(21) : error C2027: Verwendung des undefinierten Typs "cGameState" 1> c:\my dropbox\projektarbeit\source\source\../header/cGame.hpp(8): Siehe Deklaration von 'cGameState' 1>.\source\cGame.cpp(21) : error C2227: Links von "->Render" muss sich ein Zeiger auf Klassen-/Struktur-/Union-/generischen Typ befinden. 1>.\source\cGame.cpp(25) : error C2027: Verwendung des undefinierten Typs "cGameState" 1> c:\my dropbox\projektarbeit\source\source\../header/cGame.hpp(8): Siehe Deklaration von 'cGameState' 1>.\source\cGame.cpp(25) : error C2227: Links von "->Clear" muss sich ein Zeiger auf Klassen-/Struktur-/Union-/generischen Typ befinden. 1>.\source\cGame.cpp(59) : error C2673: 'SetGameStatus': Globale Funktionen haben keinen this-Zeiger 1>.\source\cGame.cpp(59) : error C2227: Links von "->GameIsRunning" muss sich ein Zeiger auf Klassen-/Struktur-/Union-/generischen Typ befinden. 1>.\source\cGame.cpp(66) : error C2027: Verwendung des undefinierten Typs "cGameState" 1> c:\my dropbox\projektarbeit\source\source\../header/cGame.hpp(8): Siehe Deklaration von 'cGameState' 1>.\source\cGame.cpp(66) : error C2227: Links von "->CleanUp" muss sich ein Zeiger auf Klassen-/Struktur-/Union-/generischen Typ befinden. 1>.\source\cGame.cpp(71) : error C2027: Verwendung des undefinierten Typs "cGameState" 1> c:\my dropbox\projektarbeit\source\source\../header/cGame.hpp(8): Siehe Deklaration von 'cGameState' 1>.\source\cGame.cpp(71) : error C2227: Links von "->Initialize" muss sich ein Zeiger auf Klassen-/Struktur-/Union-/generischen Typ befinden. 1>.\source\cGame.cpp(78) : error C2027: Verwendung des undefinierten Typs "cGameState" 1> c:\my dropbox\projektarbeit\source\source\../header/cGame.hpp(8): Siehe Deklaration von 'cGameState' 1>.\source\cGame.cpp(78) : error C2227: Links von "->Pause" muss sich ein Zeiger auf Klassen-/Struktur-/Union-/generischen Typ befinden. 1>.\source\cGame.cpp(82) : error C2027: Verwendung des undefinierten Typs "cGameState" 1> c:\my dropbox\projektarbeit\source\source\../header/cGame.hpp(8): Siehe Deklaration von 'cGameState' 1>.\source\cGame.cpp(82) : error C2227: Links von "->Initialize" muss sich ein Zeiger auf Klassen-/Struktur-/Union-/generischen Typ befinden. 1>.\source\cGame.cpp(89) : error C2027: Verwendung des undefinierten Typs "cGameState" 1> c:\my dropbox\projektarbeit\source\source\../header/cGame.hpp(8): Siehe Deklaration von 'cGameState' 1>.\source\cGame.cpp(89) : error C2227: Links von "->CleanUp" muss sich ein Zeiger auf Klassen-/Struktur-/Union-/generischen Typ befinden. 1>.\source\cGame.cpp(95) : error C2027: Verwendung des undefinierten Typs "cGameState" 1> c:\my dropbox\projektarbeit\source\source\../header/cGame.hpp(8): Siehe Deklaration von 'cGameState' 1>.\source\cGame.cpp(95) : error C2227: Links von "->Resume" muss sich ein Zeiger auf Klassen-/Struktur-/Union-/generischen Typ befinden. 1>Das Buildprotokoll wurde unter "file://c:\My Dropbox\Projektarbeit\Source\Temp\BuildLog.htm" gespeichert. 1>Projektarbeit - 20 Fehler, 0 Warnung(en) ========== Erstellen: 0 erfolgreich, Fehler bei 1, 0 aktuell, 0 übersprungen ========== |
Alter Hase
Werbeanzeige