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 |
bool Emitter::GenerateParticles(int amount) { //Particle erstellen ind in die Liste schieben for(int i=0;i<amount;i++) { //zufallszahl erzeugen switch(rand()%3) { case 0: { //Temp. Particle Particle *pTemp = NULL; //neues Particle auf dem Heap pTemp = new Particle(a_Image[0],p_Screen); //Prüfen if(!pTemp) return false; //Dann in Liste laden L_Particle.push_back(pTemp); } break; |
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
void Emitter::Show(int x, int y) { X = x - 5 + ( rand() % 25 ); Y = y - 5 + ( rand() % y ); for(i=L_Particle.begin();i != L_Particle.end();++i) { if( (*i)->IsDead() ) { delete (*i); (*i) = NULL; } else { (*i)->Show(X,Y); } } } |
C-/C++-Quelltext |
|
1 2 3 4 |
#include <vector> #include <cstdlib> #include <list> #include "Particle.hpp" |
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 |
//Temp. Particle Particle *pTemp = new Particle(a_Image[0],p_Screen); //kannst du gleich so machen /* Überprüfung macht keinen Sinn, da new standardmässig eine exception wirft, wenn was nicht in Orndung ist.. wenn überhaupt könntest du die exception fangen und dann false zurückgeben, aber exception sind imo die bessere Fehlerbehandlungsmethode if(!pTemp) return false; */ //Dann in Liste laden L_Particle.push_back(pTemp); |
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
for(i=L_Particle.begin();i != L_Particle.end();++i) { if( (*i)->IsDead() ) { delete (*i); (*i) = NULL; } else { (*i)->Show(X,Y); } } |
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
for(i=L_Particle.begin();i != L_Particle.end();) { if( (*i)->IsDead() ) { delete (*i); i = L_Particle.erase ( i ); // erase gibt dir einen neuen Iterator zurück, da der alte ungültig wird } else { (*i)->Show(X,Y); ++i; // hier inkrementieren } } |
Zitat von »"E122"«
Hast du auf Multithreaded-(Debug)-DLL gestellt ?
PS: Es heißt voraus mit einem r
Zitat von »"drakon"«
C-/C++-Quelltext
1 2 3 4 5 6 7 8 9 //Temp. Particle Particle *pTemp = new Particle(a_Image[0],p_Screen); //kannst du gleich so machen /* Überprüfung macht keinen Sinn, da new standardmässig eine exception wirft, wenn was nicht in Orndung ist.. wenn überhaupt könntest du die exception fangen und dann false zurückgeben, aber exception sind imo die bessere Fehlerbehandlungsmethode if(!pTemp) return false; */ //Dann in Liste laden L_Particle.push_back(pTemp);
C-/C++-Quelltext
1 2 3 4 5 6 7 8 9 10 11 12 13 14 for(i=L_Particle.begin();i != L_Particle.end();++i) { if( (*i)->IsDead() ) { delete (*i); (*i) = NULL; } else { (*i)->Show(X,Y); } }
Du kannst einem Iterator nicht einfach 0 zuweisen. Mach es über die passende Methode:
C-/C++-Quelltext
1 2 3 4 5 6 7 8 9 10 11 12 13 14 for(i=L_Particle.begin();i != L_Particle.end();) { if( (*i)->IsDead() ) { delete (*i); i = L_Particle.erase ( i ); // erase gibt dir einen neuen Iterator zurück, da der alte ungültig wird } else { (*i)->Show(X,Y); ++i; // hier inkrementieren } }
Zitat von »"drakon"«
Zeig am besten mal ein möglichst kleines, aber komplettes Programm, dass den Fehler erzeugt..
Zitat von »"drakon"«
Probier das ganze so weit wie möglich einzugrenzen, so, dass du den Fehler noch kriegst, aber ansonsten das Programm komplett läuffähig ist.
Am gezeigten Code liegts nicht.
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 |
#ifndef PARTICLE_HPP #define PARTICLE_HPP #include <string> #include <cstdlib> #include "SDL_image.h" #include "Timer.hpp" using namespace std; class Particle { private: SDL_Surface *p_Screen; SDL_Surface *p_Type; int Frame; int X,Y; public: Particle(SDL_Surface *type,SDL_Surface *screen); ~Particle(); void Blit(int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect *clip); void Show(int x, int y); bool IsDead(); }; #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 |
#include "Particle.hpp" Particle::Particle(SDL_Surface *type, SDL_Surface *screen) { p_Screen = NULL; p_Type = NULL; X = 0; Y = 0; Frame = 0; //Zeiger auf die Screen holen p_Screen = screen; //Zeiger auf den Particle_type holen p_Type = type; } Particle::~Particle() { if(p_Screen) p_Screen = NULL; if(p_Type) { SDL_FreeSurface(p_Type); p_Type = NULL; } } void Particle::Blit( int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect *clip = NULL ) { //Holds offsets SDL_Rect offset; //Get offsets offset.x = x; offset.y = y; //Blit SDL_BlitSurface( source, clip, destination, &offset ); } void Particle::Show(int x,int y) { X = x - 5 + (rand()%25); Y = y - 5 + (rand()%25); Blit(X,Y,p_Type,p_Screen); Frame++; } bool Particle::IsDead() { if(Frame>10) { return true; } return false; } |
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 |
#ifndef EMITTER_HPP #define EMITTER_HPP #include "Particle.hpp" #include <list> using namespace std; class Emitter { private: int X,Y; //für die Particle list<Particle*> L_Particle; list<Particle*>::iterator i; //Gesamtzahl der zu ladenden Bilder; int TotalImages; //Array Für die Particle bilder SDL_Surface *a_Image[4]; //Zeiger auf Screen SDL_Surface *p_Screen; //Zeiger für temp. type SDL_Surface *p_PTemp; public: Emitter(); ~Emitter();//standart bool Init(SDL_Surface* screen,Timer &timer); SDL_Surface* LoadImage(string filename); bool GenerateParticles(int amount); void Show(int x,int y); }; #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 |
#include "Emitter.hpp" Emitter::Emitter() { //Gesamtzahl der bilder festlegen TotalImages = 4; //Bilder-Array auf Null setzen for(int i=0;i<TotalImages;i++) { a_Image[i] = NULL; } p_Screen = NULL; } SDL_Surface* Emitter::LoadImage(std::string filename) { SDL_Surface* old = NULL; SDL_Surface* optimized = NULL; old = IMG_Load(filename.c_str()); if(old) { optimized = SDL_DisplayFormat(old); SDL_FreeSurface(old); old = NULL; } return optimized; } bool Emitter::Init(SDL_Surface* screen,Timer &timer) { //Randomsaat setzen srand(timer.Get_Ticks()); //Zeiger auf Screen holen p_Screen = screen; //Prüfen if(!p_Screen) return false; //Bilder laden a_Image[0] = LoadImage("red.bmp"); a_Image[1] = LoadImage("green.bmp"); a_Image[2] = LoadImage("blue.bmp"); a_Image[3] = LoadImage("shimmer.bmp"); //Prüfen ob die bilder geladen werden konnten for(int i=0;i<TotalImages;i++) { if(!a_Image[i]) return false; } return true; } bool Emitter::GenerateParticles(int amount) { //Particle erstellen ind in die Liste schieben for(int i=0;i<amount;i++) { //zufallszahl erzeugen switch(rand()%3) { case 0: { //Temp. Particle Particle *pTemp = NULL; //neues Particle auf dem Heap pTemp = new Particle(a_Image[0],p_Screen); //Prüfen if(!pTemp) return false; //Dann in Liste laden L_Particle.push_back(pTemp); } break; case 1: { Particle *pTemp = NULL; pTemp = new Particle(a_Image[1],p_Screen); if(!pTemp) return false; L_Particle.push_back(pTemp); } break; case 2: { Particle *pTemp = NULL; pTemp = new Particle(a_Image[2],p_Screen); if(!pTemp) return false; L_Particle.push_back(pTemp); } break; }//swirch }//for //Prüfen ob die Liste frisch gebackene Particle enthält if(L_Particle.size() == 0) return false; //ansonsten return true; } void Emitter::Show(int x, int y) { X = x - 5 + ( rand() % 25 ); Y = y - 5 + ( rand() % 25 ); /*for(i=L_Particle.begin();i != L_Particle.end();) { if( (*i)->IsDead() ) { delete (*i); i = L_Particle.erase ( i ); //Auskommentiert läuft das Spiel } else { (*i)->Show(X,Y); ++i; // hier inkrementieren } } */ } |
Werbeanzeige