Assertion Debug Error(Using SDL)Can anyone detect error in the code?

YourFace

<span style="color:#9C9C9C;"><strong>Runner Up - T
Reaction score
91
Code:
#include "SDL.h"
#include "SDL_image.h"
#include <string>
#include <vector>
#include <cstdlib>

const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int SCREEN_BPP = 32;

const int FRAMES_PER_SECOND = 20;

const int DOT_WIDTH = 20;
const int DOT_HEIGHT = 20;

SDL_Surface* screen = NULL;
SDL_Surface* dot = NULL;
//direction of snake
const int DUP = 0;
const int DDOWN = 1;
const int DRIGHT = 2;
const int DLEFT = 3;
bool Game_Over = false;
std::vector<SDL_Rect>TurnLocation;
std::vector<int>TurnDirection;
SDL_Event event;
//the thing getting objects to grow its tail
class Snake
{
	private:
	SDL_Rect box;
	SDL_Rect last_turn;
	int num_follower;
	int direction;
	bool dChangeable;
	public:
	Snake();
	void move();
	void change_direction();
	void show();
	int get_direction();
	int get_follower();
	SDL_Rect get_location();
	bool Snake_Death(SDL_Rect fbox);
	void increase_follower();
};

class Timer
{
	private:
	int startTicks;
	int pauseTicks;

	bool started;
	bool paused;

	public:
	Timer();
	int get_ticks();
	void start();
	void stop();
	void pause();
	void unpause();
	bool is_started();
	bool is_paused();
};

class Follower
{
	private:
	SDL_Rect fbox;
	int direction;
	bool lastfollower;
	int followers;
	public:
	void make_last_follower();
	void set_last_follower(Follower *last_follower);
	void show();
	void follow();
	void set_direction();
	SDL_Rect follower_location();
	Follower(Snake *mySnake, std::vector<Follower> myFollower);
	~Follower();
};

class Food
{
	private:
	SDL_Rect food_location;

	public:
	Food();
	void change_location();
	void food_show();
	SDL_Rect food_get_location();
};
SDL_Surface* load_image(std::string filename)
{
	SDL_Surface* loadedImage = NULL;
	SDL_Surface* optimizedImage = NULL;

	loadedImage = IMG_Load(filename.c_str());
	if(loadedImage != NULL)
	{
		optimizedImage = SDL_DisplayFormat(loadedImage);
		SDL_FreeSurface(loadedImage);
		if(optimizedImage != NULL)
		{
			SDL_SetColorKey(optimizedImage, SDL_SRCCOLORKEY, SDL_MapRGB(optimizedImage->format, 0, 0xFF,0xFF));
		}
	}
	return optimizedImage;
}

bool init()
{
	if(SDL_Init(SDL_INIT_EVERYTHING) == -1)
	{
		return false;
	}

	srand(SDL_GetTicks());


	screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE);

	if( screen == NULL)
	{
		return false;
	}

	SDL_WM_SetCaption("GARRY ROCKSS!", NULL);

	return true;
}

bool load_files()
{
	dot = load_image("dot.bmp");

	if(dot == NULL)
	{
		return false;
	}

	return true;
}
void apply_surface(int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect* clip = NULL)
{
	SDL_Rect offset;
	offset.x = x;
	offset.y = y;

	SDL_BlitSurface( dot, clip, screen, &offset);
}
void clean_up(std::vector<Follower> *theFollower)
{
	SDL_FreeSurface(dot);

	theFollower->clear();
	TurnLocation.clear();
	TurnDirection.clear();

	SDL_Quit();
}


bool check_collision(SDL_Rect A, SDL_Rect B)
{
	int ALeft, ARight;
	int AUp, ADown;
	int BLeft, BRight;
	int BUp, BDown;

	 ALeft = A.x;
	 ARight = A.x + A.w;
	 AUp = A.y;
	 ADown = A.y + A.h;
	 BLeft = B.x;
	 BRight = B.x +B.w;
	 BUp = B.y;
	 BDown = B.y + B.h;

	if ( ALeft> BRight)
	{
		return false;
	}

	if ( ARight< BLeft)
	{
		return false;
	}

	if( AUp > BDown)
	{
		return false;
	}
	if(ADown <BUp)
	{
		return false;
	}
	
	return true;
}
//THIS IS WHERE THE SNAKE CODE BEGINS
//THIS IS WHERE THE SNAKE CODE BEGINS
//THIS IS WHERE THE SNAKE CODE BEGINS
Snake::Snake()
{
	last_turn.x = 0;
	last_turn.y = 0;
	box.x = 0;
	box.y = 0;
	direction = DDOWN;
	num_follower = 0;
	dChangeable = false;
}
void Snake::increase_follower()
{
	num_follower++;
}

void Snake::move()
{

	if((box.x < 0) || (box.y < 0) || (box.x + DOT_WIDTH > SCREEN_WIDTH) ||(box.y + DOT_HEIGHT >SCREEN_HEIGHT))
	{
		Game_Over = true;
	}
	else 
	{
		switch(direction)
		{
		case DUP: box.y -= DOT_HEIGHT; break;
		case DDOWN: box.y += DOT_HEIGHT; break;
		case DRIGHT: box.x += DOT_WIDTH; break;
		case DLEFT: box.x -= DOT_WIDTH; break;
		}
	last_turn = get_location();
	TurnLocation.push_back(get_location());
	TurnDirection.push_back(get_direction());
	}
}

void Snake::change_direction()
{
	dChangeable = false;
	if(event.type == SDL_KEYDOWN)
	{
		SDL_Rect* p2last_turn = &last_turn;
		
		if(p2last_turn != NULL)
		{
		if(box.x>last_turn.x + DOT_WIDTH)
		{
			dChangeable = true;
		}
		if( box.y>last_turn.y + DOT_HEIGHT)
		{
			dChangeable = true;
		}
		if(box.x < last_turn.x - DOT_WIDTH)
		{
			dChangeable = true;
		}
		if(box.y < last_turn.y - DOT_HEIGHT)
		{
			dChangeable = true;
		}
		}
		else if(p2last_turn == NULL)
		{
			dChangeable = true;
		}
		if (dChangeable == true)
		{
			switch(event.key.keysym.sym)
			{
			case SDLK_UP: 
			if(direction == DDOWN)
			{
				break;
			}
			else
			{
				direction = DUP; break;
			}
			case SDLK_DOWN: 
				if(direction == DUP)
			{
				break;
			}
				else{
			direction = DDOWN; break;
			}
			case SDLK_RIGHT: 
				if(direction == DLEFT)
				{
				break;
				}
				else
				{
				direction = DRIGHT; break;
				}
			case SDLK_LEFT: 
					if(direction == DRIGHT)
					{
						break;
					}
				else
					{
					direction = DLEFT; break;
					}
			}
		}
	}
}

void Snake::show()
{
	apply_surface(box.x, box.y, dot, screen);
}

int Snake::get_direction()
{
	return direction;
}

int Snake::get_follower()
{
	return num_follower;
}
bool Snake::Snake_Death(SDL_Rect fbox)
{
	if((box.x <= 0) || (box.x >= SCREEN_WIDTH))
	{
		Game_Over = true;
		return true;
	}
	if((box.y <= 0) ||(box.y >= SCREEN_HEIGHT))
	{
		Game_Over = true;
		return true;
	}
		if(check_collision(box, fbox) == true)
		{
			Game_Over = true;
			return true;
		}
	return false;
	
}
SDL_Rect Snake::get_location()
{
	return box;
}


//THIS IS WHERE THE FOLLOWER CODE BEGINS
Follower::Follower(Snake *mySnake, std::vector<Follower> myFollower)
{
	if(mySnake->get_follower() == 0)
	{
		fbox = mySnake->get_location();
		switch(mySnake->get_direction())
		{
		case DUP:
		 fbox.y +=DOT_HEIGHT; break;
		case DDOWN:
			 fbox.y -=DOT_HEIGHT; break;
		case DRIGHT:
			 fbox.x -=DOT_HEIGHT; break;
		case DLEFT:
			 fbox.y +=DOT_HEIGHT; break;
		}
	}
	else
	{
		int i; 
		i =  myFollower.size();
		i--;
	
		direction = TurnDirection.back();

		switch(direction)
		{
			case DUP:
			fbox.x = myFollower[i].fbox.x; 
			fbox.y = myFollower[i].fbox.y + DOT_HEIGHT;
			break;
			case DDOWN:
			fbox.x = myFollower[i].fbox.x; 
			fbox.y = myFollower[i].fbox.y-DOT_HEIGHT;
			case DRIGHT:
			fbox.x = myFollower[i].fbox.x-DOT_WIDTH; 
			fbox.y = myFollower[i].fbox.y;
			case DLEFT:
			fbox.x = myFollower[i].fbox.x + DOT_WIDTH; 
			fbox.y = myFollower[i].fbox.y;
			
		}
	}
	lastfollower = true;
	mySnake->increase_follower();
}

Follower::~Follower()
{
	lastfollower = false;
	followers = 0 ;
	direction = -1;
	fbox.x = -1;
	fbox.y = -1;
}

void Follower::set_last_follower(Follower *last_follower)
{
	last_follower->lastfollower = false;
}

void Follower::show()
{
	apply_surface(fbox.x,fbox.y, dot, screen);
}
void Follower::follow()
{
	if(direction == DUP)
	{
		fbox.y -= DOT_HEIGHT;
	}
	if(direction == DDOWN)
	{
		fbox.y += DOT_HEIGHT;
	}
	if(direction == DRIGHT)
	{
		fbox.x +=DOT_WIDTH;
	}
	if(direction == DLEFT)
	{
		fbox.x -=DOT_WIDTH;
	}
}

void Follower::set_direction()
{
	if(TurnLocation.size() != 0)
	{
	for(int i = 0; i<TurnLocation.size(); i++)
	{
		if((fbox.x == TurnLocation[i].x) &&(fbox.y == TurnLocation[i].y))
		{
		direction = TurnDirection[i];
		if(lastfollower == true)
		{
			TurnLocation.erase(TurnLocation.begin() + i);
			TurnDirection.erase(TurnDirection.begin() + i);
		}
		}
		}
	}
}
void Follower::make_last_follower()
{
	lastfollower = true;
}

SDL_Rect Follower::follower_location()
{
	return fbox;
}

Timer::Timer()
{
	startTicks = 0;
	pauseTicks = 0;
	bool started = false;
	bool paused = false;
}

void Timer::start()
{
	started = true;
	startTicks = SDL_GetTicks();
}

void Timer::stop()
{
	started = false;
	paused = false;
}

void Timer::pause()
{
	paused = true;

	pauseTicks = SDL_GetTicks() - startTicks;
}


void Timer::unpause()
{
	if(paused == true)
	{
		paused = false;
		startTicks = SDL_GetTicks() - pauseTicks;
	}
}


int Timer::get_ticks()
{
	if((started == true) && (paused == true))
	{
		return pauseTicks;
	}
	else if(started == true)
	{
		return SDL_GetTicks() - startTicks;
	}
}

Food::Food()
{
	food_location.x = rand() % SCREEN_WIDTH;
	food_location.y = rand() % SCREEN_HEIGHT;
}

void Food::change_location()
{
	food_location.x = rand() % SCREEN_WIDTH;
	food_location.y = rand() % SCREEN_HEIGHT;
}
void Food::food_show()
{
	apply_surface(food_location.x, food_location.y, dot, screen);
}
SDL_Rect Food::food_get_location()
{
	return food_location;
}


int main(int argc, char* args[])
{
	bool quit = false;

	if(init() == false)
	{
		return 1;
	}

	if(load_files() == false)
	{
		return 1;
	}

	Timer fps;
	Snake theSnake;
	Food theFood;
	std::vector<Follower> theFollower;
	
	while((quit == false) &&(Game_Over == false))
	{
		fps.start();
		while(SDL_PollEvent(&event))
		{
			theSnake.change_direction();
			if(event.type == SDL_QUIT)
			{
				quit = true;
				Game_Over = true;
			}
		}

		if(check_collision(theSnake.get_location(), theFood.food_get_location()) == true)
		{	
			Follower newFollower(&theSnake,theFollower);
			theFollower.push_back(newFollower);
			for(int i = 0; i<theFollower.size(); i++)
			{
				theFollower[i].set_last_follower(&theFollower[i]);
			}
			int b = theFollower.size() - 1;
			theFollower[b].make_last_follower();
			theFood.change_location();
		}
		theSnake.move();
		SDL_FillRect(screen, &screen->clip_rect, SDL_MapRGB(screen->format, 0xFF,0xFF,0xFF));
		theSnake.show();
		theFood.food_show();
		if(theFollower.size() != 0)
		{
			for(int i = 0; i<theFollower.size(); i++)
			{
				if(theSnake.Snake_Death(theFollower[i].follower_location()) == true)
				{
				quit = true;
				}
				theFollower[i].follow();
				theFollower[i].show();

			}
		}

		if(fps.get_ticks() < 1000/FRAMES_PER_SECOND)
		{
			SDL_Delay((1000/FRAMES_PER_SECOND) - fps.get_ticks());
		}

	}
	clean_up(&theFollower);
	return 0;
}
 

s3rius

Linux is only free if your time is worthless.
Reaction score
130
Holy batman, that's a lot of code to debug.

My eagle eyes have spotted a potential problem:

newFollower.makeFollower(theSnake.get_location(), theSnake.get_direction(), theFollower.back().follower_location());

That part - in my eyes you've never added any elements to theFollower when this line runs first. theFollower.back() will give a debug assertion error if the vector is empty.

Do you know how to use break points? (I don't know how much programming you've done..)

If you left-click on the bar to the left of your script, you can set a break point (the red dot):
Unbenannt.png

When you run your program it will execute until it hits a break point, then it'll stop and show you the code.
You can hover over variables and look at their current state:
Unbenannt.png

(By pressing F5 you can continue program execution).
This way you can put break points in front of places where you think the vector could bug out, and look at it's content.
So if theFollower is empty when you call .back() then you got your culprit.

If that's not the case, examine the debug assertion that fails. The important bit is the expression:

Unbenannt.png

If it's "vector iterator + offset out of range" then you should look at parts like vector.back().
If it's "vector subscript out of range" then it's probably where you loop through all elements of theFollower and show them. (you loop too far, the vector[x] (with x being a number outside the vector's range) causes this error, for example)
 
General chit-chat
Help Users
  • No one is chatting at the moment.
  • Ghan Ghan:
    Still lurking
    +3
  • The Helper The Helper:
    I am great and it is fantastic to see you my friend!
    +1
  • The Helper The Helper:
    If you are new to the site please check out the Recipe and Food Forum https://www.thehelper.net/forums/recipes-and-food.220/
  • Monovertex Monovertex:
    How come you're so into recipes lately? Never saw this much interest in this topic in the old days of TH.net
  • Monovertex Monovertex:
    Hmm, how do I change my signature?
  • tom_mai78101 tom_mai78101:
    Signatures can be edit in your account profile. As for the old stuffs, I'm thinking it's because Blizzard is now under Microsoft, and because of Microsoft Xbox going the way it is, it's dreadful.
  • The Helper The Helper:
    I am not big on the recipes I am just promoting them - I use the site as a practice place promoting stuff
    +2
  • Monovertex Monovertex:
    @tom_mai78101 I must be blind. If I go on my profile I don't see any area to edit the signature; If I go to account details (settings) I don't see any signature area either.
  • The Helper The Helper:
    You can get there if you click the bell icon (alerts) and choose preferences from the bottom, signature will be in the menu on the left there https://www.thehelper.net/account/preferences
  • The Helper The Helper:
    I think I need to split the Sci/Tech news forum into 2 one for Science and one for Tech but I am hating all the moving of posts I would have to do
  • The Helper The Helper:
    What is up Old Mountain Shadow?
  • The Helper The Helper:
    Happy Thursday!
    +1
  • Varine Varine:
    Crazy how much 3d printing has come in the last few years. Sad that it's not as easily modifiable though
  • Varine Varine:
    I bought an Ender 3 during the pandemic and tinkered with it all the time. Just bought a Sovol, not as easy. I'm trying to make it use a different nozzle because I have a fuck ton of Volcanos, and they use what is basically a modified volcano that is just a smidge longer, and almost every part on this thing needs to be redone to make it work
  • Varine Varine:
    Luckily I have a 3d printer for that, I guess. But it's ridiculous. The regular volcanos are 21mm, these Sovol versions are about 23.5mm
  • Varine Varine:
    So, 2.5mm longer. But the thing that measures the bed is about 1.5mm above the nozzle, so if I swap it with a volcano then I'm 1mm behind it. So cool, new bracket to swap that, but THEN the fan shroud to direct air at the part is ALSO going to be .5mm to low, and so I need to redo that, but by doing that it is a little bit off where it should be blowing and it's throwing it at the heating block instead of the part, and fuck man
  • Varine Varine:
    I didn't realize they designed this entire thing to NOT be modded. I would have just got a fucking Bambu if I knew that, the whole point was I could fuck with this. And no one else makes shit for Sovol so I have to go through them, and they have... interesting pricing models. So I have a new extruder altogether that I'm taking apart and going to just design a whole new one to use my nozzles. Dumb design.
  • Varine Varine:
    Can't just buy a new heatblock, you need to get a whole hotend - so block, heater cartridge, thermistor, heatbreak, and nozzle. And they put this fucking paste in there so I can't take the thermistor or cartridge out with any ease, that's 30 dollars. Or you can get the whole extrudor with the direct driver AND that heatblock for like 50, but you still can't get any of it to come apart
  • Varine Varine:
    Partsbuilt has individual parts I found but they're expensive. I think I can get bits swapped around and make this work with generic shit though
  • Ghan Ghan:
    Heard Houston got hit pretty bad by storms last night. Hope all is well with TH.
  • The Helper The Helper:
    Power back on finally - all is good here no damage
    +2
  • V-SNES V-SNES:
    Happy Friday!
    +1
  • The Helper The Helper:
    New recipe is another summer dessert Berry and Peach Cheesecake - https://www.thehelper.net/threads/recipe-berry-and-peach-cheesecake.194169/

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top