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 |
// Game.hpp // #ifndef GAME_HPP #define GAME_HPP #include "Player.hpp" #include "Banana.hpp" #include "Text.hpp" class CGame { public: CGame (); void Init (); void Run (); void Quit (); private: void ProcessEvents (); bool m_bGameRun; // Läuft das Spiel noch? bool m_bMenu; // Läuft das Hauptmenü noch? int m_Score; // Der Score für ein Spiel CText *m_pText; // Text }; #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 98 99 100 101 102 103 104 |
// Game.cpp // #include "Game.hpp" // Konstruktor // // Aufgabe: Allgemmeine Initialisierungen // CGame::CGame() { // Sprite-Instanzen auf NULL stellen m_pText = NULL; } // Konstruktor // Init // // Aufgabe: Initialisierung von den meisten Objekten/Variablen // void CGame::Init() { // Klassen-Instanzen initialisieren m_pText = new CText; // Spiel und Hauptmenü laufen m_bGameRun = true; m_bMenu = true; // Score auf 0 setzen m_Score = 0; } // Init // Run // // Aufgabe: Der eigentliche "Herzschlag" des Spiels // void CGame::Run() { char TextBuffer_Score[15]; // Die "Herzschlag"-Schleife while (m_bGameRun == true) { // Hauptmenü aufrufen, falls noch aktiv if (m_bMenu == true) { Menu (); } // Menu // Framework updaten und Buffer löschen g_pFramework->Update (); g_pFramework->Clear (); // Events überprüfen ProcessEvents (); sprintf_s(TextBuffer_Score, "Score: %i", m_Score); m_pText->Initialise(g_pFramework->GetRenderer(), "Schriftarten/comic.ttf", 50, TextBuffer_Score, { 0, 0, 0, 0 }); // Erreichten Score anzeigen m_pText->display (1320, 15, g_pFramework->GetRenderer()); // Falls 'H' gedrückt wird, zum Hauptmenü // zurückkehren und Spielerstand zurücksetzen if (g_pFramework->KeyDown(SDL_SCANCODE_H)) m_bMenu = true; // Spiel darstellen g_pFramework->Render (); } } // Run // Quit // // Aufgabe: Instanzen sauber freigeben // void CGame::Quit() { // Instanzen werden sauber freigegeben if (m_pText != NULL) { delete m_pText; m_pText = NULL; } } // Quit |
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 |
// Text.hpp // #ifndef TEXT_HPP #define TEXT_HPP #include <SDL_ttf.h> #include <SDL.h> #include <string> #include "Framework.hpp" class CText { public: CText (); void Initialise(SDL_Renderer *renderer, const string &font_path, int font_size, const string &message_text, const SDL_Color &color); void display (int x, int y, SDL_Renderer *renderer); static SDL_Texture *loadFont (SDL_Renderer *renderer, const string &font_path, int font_size, const string &message_text, const SDL_Color &color); private: SDL_Texture *m_text_texture = nullptr; mutable SDL_Rect m_text_rect; }; #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 |
// Text.cpp // #include "Text.hpp" CText::CText () { } void CText::Initialise(SDL_Renderer *renderer, const string &font_path, int font_size, const string &message_text, const SDL_Color &color) { m_text_texture = loadFont(renderer, font_path, font_size, message_text, color); SDL_QueryTexture(m_text_texture, nullptr, nullptr, &m_text_rect.w, &m_text_rect.h); } void CText::display(int x, int y, SDL_Renderer *renderer) { m_text_rect.x = x; m_text_rect.y = y; SDL_RenderCopy (renderer, m_text_texture, nullptr, &m_text_rect); SDL_DestroyTexture(m_text_texture); } SDL_Texture *CText::loadFont(SDL_Renderer *renderer, const string &font_path, int font_size, const string &message_text, const SDL_Color &color) { TTF_Font *font = TTF_OpenFont(font_path.c_str(), font_size); if (!font) { cout << "failed to load font!" << endl; } auto m_text_surface = TTF_RenderText_Solid (font, message_text.c_str(), color); if (!m_text_surface) { cout << "failed to load surface!" << endl; } if (!message_text.c_str()) { cout << "failed to load message!" << endl; } auto text_texture = SDL_CreateTextureFromSurface(renderer, m_text_surface); if (!text_texture) { cout << "failed to load texture!" << endl; } SDL_FreeSurface (m_text_surface); return text_texture; } |
Dieser Beitrag wurde bereits 6 mal editiert, zuletzt von »Hannibal_TheHacker« (17.05.2017, 18:29)
Hi Max und Willkommen im Forum!
Kleiner Tipp zum Einstieg: Verwende C++ Code-Tags, für besseres Syntax-Highlighting.
[cpp] statt [code] (analog natürlich auch die schließenden Tags)
Da werden die Antworten nur so reinrauschen!
Ich sehe auf jeden Fall schon mal ein OpenFont ohne CloseFont. Dazu kommt das du den Font nicht jedes Frame neu laden solltest. Wenn die Methode schon Initialise heißt macht es doch Sinn sie einmalig initial aufzurufen
Werbeanzeige