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 |
#ifndef FRAMEWORK_HPP #define FRAMEWORK_HPP #include "Singleton.hpp" #include <iostream> #include <Windows.h> #include <SDL.h> #include <SDL_image.h> #include <SDL_ttf.h> #include <SDL_mixer.h> using namespace std; #define g_pFramework CFramework::Get() class CFramework : public TSingleton<CFramework> { public: bool Init (int ScreenWidth, int ScreenHeight, int ColorDepth, bool bFullscreen); int InitIcon (); void Quit (); void Update (); void Clear (); void Flip (); bool KeyDown (int Key_ID); SDL_Surface *GetScreen () {return m_pScreen;} SDL_Surface *m_pScreen; // Surface für den Screen private: Uint8 *m_pKeystate; // Array für aktuellen Tastaturstatus }; #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 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 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
// FRAMEWORK_CPP #include "Framework.hpp" // Init // // Aufgabe: Framework initialisieren // bool CFramework::Init (int ScreenWidth, int ScreenHeight, int ColorDepth, bool bFullscreen) { // Alle Systeme der SDL initialisieren if (SDL_Init (SDL_INIT_VIDEO | SDL_INIT_TIMER) == -1 ) { cout << "SDL konnte nicht initialisiert werden!" << endl; cout << "Fehlermeldung: " << SDL_GetError () << endl; Quit (); return (false); } // SDL mit dem Grafik- und Audiosystem initialisieren. if(SDL_Init(SDL_INIT_AUDIO) == -1) { // Ups, das hat nicht funktioniert! // Wir geben die Fehlermeldung aus. cout << "Konnte SDL_mixer nicht initialisieren! Fehler: " << SDL_GetError() << endl; return (false); } // SDL mit dem Grafiksystem initialisieren. if(SDL_Init(SDL_INIT_VIDEO) == -1) { cout << "SDL konnte nicht initialisiert werden!" << endl; cout << "Fehlermeldung: " << SDL_GetError () << endl; Quit (); return (false); } // SDL_ttf initialisieren. if(TTF_Init() == -1) { cout << "Konnte SDL_ttf nicht initialisieren! Fehler: " << TTF_GetError() << endl; return (false); } // Vollbild oder Fenstermodus einstellen if (bFullscreen == true) { m_pScreen = SDL_SetVideoMode (ScreenWidth, ScreenHeight, ColorDepth, SDL_HWSURFACE | SDL_DOUBLEBUF | SDL_FULLSCREEN); } else { m_pScreen = SDL_SetVideoMode (ScreenWidth, ScreenHeight, ColorDepth, SDL_HWSURFACE | SDL_DOUBLEBUF); } // Prüfen, ob alles funktioniert hat if (m_pScreen == NULL) { cout << "Videomodus konnte nicht gesetzt werden!" << endl; cout << "Fehlermeldung: " << SDL_GetError () << endl; Quit (); return (false); } InitIcon (); // SDL_mixer const int samplingFrequency = 44100; // 44100 Hz Abtastfrequenz Uint16 audioFormat = AUDIO_S16SYS; // 16 Bits pro Sample const int numChannels = 2; // 2 Kanäle = Stereo const int chunkSize = 4096; // ein guter Wert ... if(Mix_OpenAudio(samplingFrequency, audioFormat, numChannels, chunkSize) == -1) { cout << "Konnte SDL_mixer nicht starten! Fehler: " << Mix_GetError() << endl; return (false); } // Nun setzen wir noch den Titel des Fensters. SDL_WM_SetCaption("Pacman 0.1 PreRelease", "Pacman 0.1 PreRelease"); // Zeiger auf internes Array für Tastaturstatus ermitteln m_pKeystate = SDL_GetKeyState (NULL); // Alles ging glatt, also true zurückliefern return (true); } // Init // Quit // // Aufgabe: Framework (SDL) beenden // void CFramework::Quit () { // SDL beenden SDL_Quit (); } // Quit // Update // // Aufgabe: Timer und Keyboardstatus updaten // void CFramework::Update () { // Tastaturstatus ermitteln SDL_PumpEvents (); } // Update // Keydown // // Aufgabe: Tastendruck abfragen // bool CFramework::KeyDown (int Key_ID) { // Prüfen, ob Taste gedrückt ist return (m_pKeystate[Key_ID] ? true : false); } // KeyDown // Clear // // Aufgabe: Buffer löschen // void CFramework::Clear () { // Buffer (Surface) mit Hintergrundfarbe füllen SDL_FillRect (m_pScreen, NULL, SDL_MapRGB (m_pScreen->format, 0, 0, 0)); } // Clear // Flip // // Aufgabe: Surface umschalten (flippen) // void CFramework::Flip () { // Surface umschalten SDL_Flip (m_pScreen); } // Flip // InitIcon // // Aufgabe: Icon intialisieren // int CFramework::InitIcon () { SDL_Surface *icon; icon = SDL_LoadBMP("Data/Images/icon.bmp"); if(!icon) { // Fehler! cout << "Konnte Icon nicht laden! Fehler: " << IMG_GetError() << endl; return(false); } SDL_WM_SetIcon(icon,NULL); 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 |
#include "Framework.hpp" #include "Game.hpp" int main (int argc, char *argv[]) { CGame Game; // Framework initialisieren if( g_pFramework->Init ( 800, 600, 16, false) == false ) { getchar (); return (false); } // Spiel initialisieren Game.Init (); // Spiel laufen lassen Game.Run (); // Framework beenden g_pFramework->Quit (); exit(1); 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 42 43 44 |
#ifndef GAME_HPP #define GAME_HPP #include "Framework.hpp" #include "Bot.hpp" #include <iostream> #include <SDL.h> #include <SDL_image.h> #include <SDL_mixer.h> #include <SDL_ttf.h> class CGame { public: CGame (); // Konstruktor int Init (); // Spiel initialisieren void Run (); // Spiel laufen lassen private: CBot Bot; void ProcessEvents (); // Events bearbeiten void Render (); // Sprites rendern void SetPos (); // Position der Sprites bestimmen void ColorKey (); // Colorkey bestimmen bool m_bGameRun; // Läuft das Spiel noch? // Sprites SDL_Surface* Background; SDL_Surface* Player; SDL_Surface* Sprite; // Positionen SDL_Rect BackgroundPosition; SDL_Rect PlayerPosition; SDL_Rect SpritePosition; }; #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 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 143 144 145 146 147 148 149 150 |
#include "Game.hpp" // Konstruktor // // Aufgabe: Werte initialisieren // CGame::CGame () { m_bGameRun = false; } // Konstruktor // Init // // Aufgabe: Spiel initialisieren // int CGame::Init () { Background = IMG_Load("Data/Images/Background.bmp"); if(!Background) { // Fehler! cout << "Konnte Bild nich laden! Fehler: " << IMG_GetError() << endl; return (false); } Player = IMG_Load("Data/Images/Player.bmp"); if(!Player) { // Fehler! cout << "Konnte Bild nicht laden! Fehler: " << IMG_GetError() << endl; return (false); } Bot.Init ("Data/Images/Bot.bmp", Sprite); SetPos (); m_bGameRun = true; return 0; } // Init // Run // // Aufgabe: Spiel laufen lassen // void CGame::Run () { while (m_bGameRun == true) { // Events bearbeiten ProcessEvents (); // Framework updaten und Buffer löschen g_pFramework->Update (); g_pFramework->Clear (); // Colorkey bestimmen ColorKey (); // Rendern Bot.Render (Sprite, SpritePosition, 200, 200); Render (); // Buffer flippen g_pFramework->Flip (); } } // Run // Process Events // // Aufgabe: Events bearbeiten // void CGame::ProcessEvents () { SDL_Event Event; // Gab es ein Event? if (SDL_PollEvent (&Event)) { // Ja, also schauen welches switch (Event.type) { // Beenden? case (SDL_QUIT): { m_bGameRun = false; } break; // Wurde eine Taste gedrückt? case (SDL_KEYDOWN): { switch (Event.key.keysym.sym) { case(SDLK_ESCAPE): { m_bGameRun = false; } break; } // Event.key.keysym.sym } break; // Keydown } // Event.type } // SDL_PollEvent } // Process Events // Render // // Aufgabe: Sprites rendern // void CGame::Render () { SDL_BlitSurface(Background, 0, g_pFramework->GetScreen (), &BackgroundPosition); SDL_BlitSurface( Player, 0, g_pFramework->GetScreen (), &PlayerPosition); } // Render // SetPos // // Aufgabe: Position der Sprites bestimmen // void CGame::SetPos () { BackgroundPosition.x = 0; BackgroundPosition.y = 0; PlayerPosition.x = 100; PlayerPosition.y = 100; } // SetPos // Colorkey // // Aufgabe: Colorkey bestimmen // void CGame::ColorKey () { // Colorkey einstellen SDL_SetColorKey (Background, SDL_SRCCOLORKEY, SDL_MapRGB (Background->format, 255, 0, 255) ); SDL_SetColorKey (Player, SDL_SRCCOLORKEY, SDL_MapRGB ( Player->format, 255, 0, 255) ); } |
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
#ifndef BOT_HPP #define BOT_HPP #include "Framework.hpp" class CBot { public: CBot (); int Init (const char*, SDL_Surface*); void Render (SDL_Surface*, SDL_Rect, int, int); void Move (); private: bool m_bSetPos; }; #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 |
#include "Bot.hpp" CBot::CBot () { m_bSetPos = false; } int CBot::Init (const char* File, SDL_Surface* Bot) { Bot = IMG_Load(File); if(!Bot) { // Fehler! cout << "Konnte Bild nicht laden! Fehler: " << IMG_GetError() << endl; return (false); } return 0; } void CBot::Render (SDL_Surface* Bot, SDL_Rect BotPosition, int XPos, int YPos) { if( m_bSetPos == false ) { BotPosition.x = XPos; BotPosition.y = YPos; m_bSetPos = true; } // Colorkey einstellen SDL_SetColorKey (Bot, SDL_SRCCOLORKEY, SDL_MapRGB (Bot->format, 255, 0, 255) ); SDL_BlitSurface( Bot, 0, g_pFramework->GetScreen(), &BotPosition); } void CBot::Move () { } |
C-/C++-Quelltext |
|
1 2 |
/* Keep the compiler happy */ return(0); |
C-/C++-Quelltext |
|
1 2 |
/* Keep the compiler happy */ 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 |
#ifndef BOT_HPP #define BOT_HPP #include "Framework.hpp" class CBot { public: CBot (); int Render (const char*, int, int); void Move (); private: bool m_bInit; SDL_Surface* Sprite; SDL_Rect SpritePosition; }; #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 |
#include "Bot.hpp" CBot::CBot () { m_bInit = false; } int CBot::Render (const char* file, int XPos, int YPos) { if( m_bInit == false ) { Sprite = IMG_Load(file); if(!Sprite) { // Fehler! cout << "Konnte Bild nicht laden! Fehler: " << IMG_GetError() << endl; return (false); } SpritePosition.x = XPos; SpritePosition.y = YPos; m_bInit = true; } // Colorkey einstellen SDL_SetColorKey (Sprite, SDL_SRCCOLORKEY, SDL_MapRGB (Sprite->format, 255, 0, 255) ); SDL_BlitSurface( Sprite, 0, g_pFramework->GetScreen(), &SpritePosition); return 0; } void CBot::Move () { } |
C-/C++-Quelltext |
|
1 2 |
/* Keep the compiler happy */ return(0); |
Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von »Swoerm« (28.04.2013, 01:33)
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 |
#ifndef BOT_HPP #define BOT_HPP #include "Framework.hpp" class CBot { public: CBot (const char*, int, int); int Render (); void Move (); private: // bool m_bInit; SDL_Surface* Sprite; SDL_Rect SpritePosition; }; #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 |
#include "Bot.hpp" CBot::CBot (const char* file, int XPos, int YPos) { Sprite = IMG_Load(file); if(!Sprite) { // Fehler! cout << "Konnte Bild nicht laden! Fehler: " << IMG_GetError() << endl; g_pFramework->Quit (); } SpritePosition.x = XPos; SpritePosition.y = YPos; //m_bInit = false; } int CBot::Render () { /* if( m_bInit == false ) { Sprite = IMG_Load(file); if(!Sprite) { // Fehler! cout << "Konnte Bild nicht laden! Fehler: " << IMG_GetError() << endl; return (false); } SpritePosition.x = XPos; SpritePosition.y = YPos; m_bInit = true; }*/ // Colorkey einstellen SDL_SetColorKey (Sprite, SDL_SRCCOLORKEY, SDL_MapRGB (Sprite->format, 255, 0, 255) ); SDL_BlitSurface( Sprite, 0, g_pFramework->GetScreen(), &SpritePosition); return 0; } void CBot::Move () { } |
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 |
void CGame::Render () { SDL_BlitSurface(Background, 0, g_pFramework->GetScreen (), &BackgroundPosition); SDL_BlitSurface( Player, 0, g_pFramework->GetScreen (), &PlayerPosition); CBot Bot("Data/Images/Bot.bmp", 200, 200); Bot.Render (); } |
C-/C++-Quelltext |
|
1 2 |
/* Keep the compiler happy */ return(0); |
C-/C++-Quelltext |
|
1 2 |
/* Keep the compiler happy */ return(0); |
Werbeanzeige