Reputation: 13
I have recently started coding in C++ for the in-take assignment I need to make for my next school, and I'm seeing a big problem now and I don't know how to fix it. As soon as I try to run my code it said "Project.exe has stopped working". I tried to find solutions on the internet but I haven't found anything that solves my problem. This is my code (the SDL part are not the parts causing trouble, but I included them anyway for the completeness of the code):
//Using SDL and standard IO
#include <SDL.h>
#include <stdio.h>
//Screen dimension constants
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
class particle {
public:
void setxPos(int xx){
xP=xx;
};
void setyPos(int yy){
yP=yy;
};
void setxVel(int xv){
xVel=xv;
};
void setyVel(int yv){
yVel=yv;
};
int getxPos(){
return (xP);
};
int getyPos(){
return (yP);
};
protected:
int xP, yP;
int xVel = 0;
int yVel = 0;
};
void draw(SDL_Renderer* tempRend, particle drawJelly[6][4]){ //Function created for drawing the lines between the particles
SDL_RenderDrawLine( tempRend, drawJelly[1][1].getxPos(), drawJelly[1][1].getyPos(), drawJelly[2][1].getxPos(), drawJelly[2][1].getyPos() );
SDL_RenderDrawLine( tempRend, drawJelly[2][1].getxPos(), drawJelly[2][1].getyPos(), drawJelly[3][1].getxPos(), drawJelly[3][1].getyPos() );
SDL_RenderDrawLine( tempRend, drawJelly[3][1].getxPos(), drawJelly[3][1].getyPos(), drawJelly[4][1].getxPos(), drawJelly[4][1].getyPos() );
SDL_RenderDrawLine( tempRend, drawJelly[4][1].getxPos(), drawJelly[4][1].getyPos(), drawJelly[5][1].getxPos(), drawJelly[5][1].getyPos() );
SDL_RenderDrawLine( tempRend, drawJelly[5][1].getxPos(), drawJelly[5][1].getyPos(), drawJelly[6][1].getxPos(), drawJelly[6][1].getyPos() );
SDL_RenderDrawLine( tempRend, drawJelly[6][1].getxPos(), drawJelly[6][1].getyPos(), drawJelly[6][2].getxPos(), drawJelly[6][2].getyPos() );
SDL_RenderDrawLine( tempRend, drawJelly[6][2].getxPos(), drawJelly[6][2].getyPos(), drawJelly[6][3].getxPos(), drawJelly[6][3].getyPos() );
SDL_RenderDrawLine( tempRend, drawJelly[6][3].getxPos(), drawJelly[6][3].getyPos(), drawJelly[6][4].getxPos(), drawJelly[6][4].getyPos() );
SDL_RenderDrawLine( tempRend, drawJelly[6][4].getxPos(), drawJelly[6][4].getyPos(), drawJelly[5][4].getxPos(), drawJelly[5][4].getyPos() );
SDL_RenderDrawLine( tempRend, drawJelly[5][4].getxPos(), drawJelly[5][4].getyPos(), drawJelly[4][4].getxPos(), drawJelly[4][4].getyPos() );
SDL_RenderDrawLine( tempRend, drawJelly[4][4].getxPos(), drawJelly[4][4].getyPos(), drawJelly[3][4].getxPos(), drawJelly[3][4].getyPos() );
SDL_RenderDrawLine( tempRend, drawJelly[3][4].getxPos(), drawJelly[3][4].getyPos(), drawJelly[2][4].getxPos(), drawJelly[2][4].getyPos() );
SDL_RenderDrawLine( tempRend, drawJelly[2][4].getxPos(), drawJelly[2][4].getyPos(), drawJelly[1][4].getxPos(), drawJelly[1][4].getyPos() );
SDL_RenderDrawLine( tempRend, drawJelly[1][4].getxPos(), drawJelly[1][4].getyPos(), drawJelly[1][3].getxPos(), drawJelly[1][3].getyPos() );
SDL_RenderDrawLine( tempRend, drawJelly[1][3].getxPos(), drawJelly[1][3].getyPos(), drawJelly[1][2].getxPos(), drawJelly[1][2].getyPos() );
SDL_RenderDrawLine( tempRend, drawJelly[1][2].getxPos(), drawJelly[1][2].getyPos(), drawJelly[1][1].getxPos(), drawJelly[1][1].getyPos() );
SDL_RenderPresent( tempRend );
SDL_Delay(100); //Wait .5 seconds for clarity of the drawing
}
void position(particle posJelly[6][4]){
int a=0;
while (a<6){ //Give all the jelly particles a position in the 20x20 grid
int b=0;
while(b<4){
posJelly[a][b].setxPos((20*a));
posJelly[a][b].setyPos((20*b));
b--;
}
a--;
}
}
int main( int argc, char* args[] )
{
SDL_Window* window = NULL;
SDL_Renderer* renderer = NULL;
SDL_Surface* surface = NULL;
SDL_Init( SDL_INIT_VIDEO );
window = SDL_CreateWindow( "Jelly Game", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN );
renderer = SDL_CreateRenderer( window, -1, 0);
surface = SDL_GetWindowSurface( window );
SDL_SetRenderDrawColor( renderer, 0, 255, 0, 255 );
SDL_RenderPresent( renderer );
particle jelly[6][4]; //New object from class "particle" with name jelly
position(jelly); //Run function "position()" on particle jelly to set starting coördinates
bool inGame=true; //Start the game-loop
while(inGame){
draw(renderer, jelly);
inGame=false;
}
SDL_Delay( 2000 ); //Wait 2 seconds
SDL_DestroyWindow( window ); //Destroy the window "window"
SDL_Quit(); //Quit SDL
return 0; //End the program
}
Now, I already found where the cause lies, and that is in this part:
void position(particle posJelly[6][4]){
int a=0;
while (a<6){ //Give all the jelly particles a position in the 20x20 grid
int b=0;
while(b<4){
posJelly[a][b].setxPos((20*a));
posJelly[a][b].setyPos((20*b));
b--;
}
a--;
}
}
Because when I delete
position(jelly);
or remove
posJelly[a][b].setxPos((20*a));
posJelly[a][b].setyPos((20*b));
inside the position function, it doesn't give the error. (It doesnt work properly because it has no correct coordinates to draw from and to, but it 'works')
Again, I have searched in a lot of other articles and tutorials why this doesn't work or how this can be made work-able, but have found nothing yet.
The 'game' will be about a blob of jelly, made up out of 24 particles (6x4 but only the outer particles will be painted on the screen) that have a desirable distance to their neigbors, and then when they move closer or further away from eachother they will be drawn/pushed back to that distance, thus creating the 'bouncing' effect a blob of jelly has.
Please help!
Also, I know the Draw() function isn't very proffessional, but it's not going to be a full-release game anyway, and since the blob is always going to be 6 by 4, this is just a way easier way of drawing than finding the outer particles by loops and stuff.
Upvotes: 1
Views: 108
Reputation: 14360
In this case the error has to do with negative values to array indexes. You see an error like that when you program has raised an exeption and you don't handle it.
You should change:
a--
and b--
for a++
and b++
The first time your code reach the lines:
posJelly[a][b].setxPos((20*a));
posJelly[a][b].setyPos((20*b));
a==0
and b==0
but the second time (after a--
and b--
) you would have something like this:
posJelly[-1][-1].setxPos((20*a)); // illegal C++ no negative inidices are allowed
posJelly[-1][-1].setyPos((20*b)); // illegal C++ no negative inidices are allowed
Upvotes: 0
Reputation: 180595
You are trying to access the arrays with a negative index. You set b to 0 in the first iteration and than then you decrement it to make it -1. Then you use b in the next iteration the program goes boom. You probably meant to use ++ instead of --.
Upvotes: 1
Reputation: 9570
replace a decrementing operator with an increment: a--
and b--
with a++
andb++
, respectively.
Upvotes: 0