[SFML] Breakout Help :(

Started by
15 comments, last by superman3275 11 years, 6 months ago
Today, I started programming breakout. I have the main functionality done, but whenever I run the program it gives me the error:

0xC0000005: Access violation writing location 0x00000005

in Visual C++ 2010 Express Edition. Here's the Rest of the Code:
Paddle.h:

#pragma once
class Paddle
{
public:
Paddle(float speedx, sf::Image &PaddleImage);
~Paddle(void);
void Update(sf::RenderWindow &Window);
void Draw(sf::RenderWindow &Window);
sf::FloatRect GetCollision();
sf::Image GetPaddleImage();
sf::Sprite GetPaddleSprite();
float GetWidth();
float GetHeight();
float GetSpeedX();
private:
sf::FloatRect Collision;
sf::Image PaddleImage;
sf::Sprite PaddleSprite;
float Width;
float Height;
float speedx;
};

Paddle.cpp:

#include "StdAfx.h"
#include "Paddle.h"
//Constructor sets all the values, we have no memory allocated so Destructor does nothing;
Paddle::Paddle(float speedx, sf::Image &PaddleImage)
{
this->speedx = speedx;
this->PaddleImage = PaddleImage;
PaddleSprite.SetImage(this->PaddleImage);
PaddleSprite.Move(350, 750);
Width = this->PaddleImage.GetWidth();
Height = this->PaddleImage.GetHeight();
}

Paddle::~Paddle(void)
{
}
//Updates the floatrects position to test for, well, collision and checks for input;
void Paddle::Update(sf::RenderWindow &Window)
{
Collision.Left = PaddleSprite.GetPosition().x;
Collision.Top = PaddleSprite.GetPosition().y;
Collision.Right = PaddleSprite.GetPosition().x + Width;
Collision.Bottom = PaddleSprite.GetPosition().y + Height;
if (Window.GetInput().IsKeyDown(sf::Key::Left) && PaddleSprite.GetPosition().x > 0) PaddleSprite.Move(-speedx * Window.GetFrameTime(), 0);
else if (Window.GetInput().IsKeyDown(sf::Key::Right) && PaddleSprite.GetPosition().x < 700) PaddleSprite.Move(speedx * Window.GetFrameTime(), 0);
}
//NECESSARY GET FUNCTIONS;
void Paddle::Draw(sf::RenderWindow &Window)
{
Window.Draw(PaddleSprite);
}
sf::FloatRect Paddle::GetCollision()
{
return Collision;
}
sf::Image Paddle::GetPaddleImage()
{
return PaddleImage;
}
sf::Sprite Paddle::GetPaddleSprite()
{
return PaddleSprite;
}
float Paddle::GetWidth()
{
return Width;
}
float Paddle::GetHeight()
{
return Height;
}
float Paddle::GetSpeedX()
{
return speedx;
}

Ball.h:

#pragma once
class Paddle;
class Block;
class Ball
{
public:
Ball(sf::Image &BallImage, float speedx, float speedy);
~Ball(void);
void Update(sf::RenderWindow &Window);
void Draw(sf::RenderWindow &Window);
void Test_For_Collision(sf::FloatRect LeftWall, sf::FloatRect RightWall, sf::FloatRect Top, sf::FloatRect Bottom, Paddle MyPaddle, Block Blocks[20]);
sf::FloatRect GetCollision();
sf::Image GetBallImage();
sf::Sprite GetBallSprite();
float GetSpeedX();
float GetSpeedY();
private:
sf::FloatRect Collision;
sf::Image BallImage;
sf::Sprite BallSprite;
float speedx, speedy, Width, Height;
};

Ball.cpp:

#include "StdAfx.h"
#include "Ball.h"

Ball::Ball(sf::Image &BallImage, float speedx, float speedy)
{
Width = 25;
Height = 25;
this->speedx = speedx;
this->speedy = speedy;
this->BallImage = BallImage;
BallSprite.SetImage(this->BallImage);
}

Ball::~Ball(void)
{
}
void Ball::Update(sf::RenderWindow &Window)
{
BallSprite.Move(speedx * Window.GetFrameTime(), speedy * Window.GetFrameTime());
Collision.Left = BallSprite.GetPosition().x;
Collision.Top = BallSprite.GetPosition().y;
Collision.Right = BallSprite.GetPosition().x + Width;
Collision.Bottom = BallSprite.GetPosition().y + Height;
}
void Ball::Draw(sf::RenderWindow &Window)
{
Window.Draw(BallSprite);
}
void Ball::Test_For_Collision(sf::FloatRect LeftWall, sf::FloatRect RightWall, sf::FloatRect Top, sf::FloatRect Bottom, Paddle MyPaddle, Block Blocks[20])
{
for (int index = -1; index < 19; ++index)
{
if (Collision.Intersects(Blocks[index].GetCollision()) && Blocks[index].GetIsHit() == false)
{
speedy = -speedy;
Blocks[index].SetIsHit(true);
}
}
if (Collision.Intersects(MyPaddle.GetCollision()))
{
speedy = -speedy;
}
if (Collision.Intersects(LeftWall))
{
speedy = -speedy;
}
if (Collision.Intersects(RightWall))
{
speedy = -speedy;
}
if (Collision.Intersects(Top))
{
speedx = -speedx;
}
if (Collision.Intersects(Bottom))
{
speedx = -speedx;
}
}
sf::FloatRect Ball::GetCollision()
{
return Collision;
}
sf::Image Ball::GetBallImage()
{
return BallImage;
}
sf::Sprite Ball::GetBallSprite()
{
return BallSprite;
}
float Ball::GetSpeedX()
{
return speedx;
}
float Ball::GetSpeedY()
{
return speedy;
}

Block.h:

#pragma once
class Block
{
public:
//Constructors and Destructors
Block(void);
Block(float movex, float movey, sf::Image &BlockImage);
~Block(void);
//Member Functions
void Initialize(float movex, float movey, sf::Image &BlockImage);
void Draw(sf::RenderWindow &Window);
//Get Functions
sf::FloatRect GetCollision();
sf::Image GetBlockImage();
sf::Sprite GetBlockSprite();
float GetWidth();
float GetHeight();
bool GetIsHit();
void SetIsHit(bool x);
//No set functions (I don't want to be able to change values outside of constructor/initialize function)
private:
sf::FloatRect Collision;
sf::Image BlockImage;
sf::Sprite BlockSprite;
float Width;
float Height;
bool IsHit;
};

Block.cpp:

#include "StdAfx.h"
#include "Block.h"

Block::Block(void)
{
}

Block::~Block(void)
{
}
Block::Block(float movex, float movey, sf::Image &iBlockImage)
{
Width = 30;
Height = 10;
IsHit = false;
BlockImage = iBlockImage;
BlockSprite.SetImage(BlockImage);
BlockSprite.Move(movex, movey);
Collision.Left = movex;
Collision.Top = movey;
Collision.Right = movex + BlockImage.GetWidth();
Collision.Bottom = movey + BlockImage.GetWidth();
}
void Block::Initialize(float movex, float movey, sf::Image &BlockImage)
{
Width = 30;
Height = 10;
IsHit = false;
this->BlockImage = BlockImage;
BlockSprite.SetImage(this->BlockImage);
BlockSprite.Move(movex, movey);
Collision.Left = movex;
Collision.Top = movey;
Collision.Right = movex + this->BlockImage.GetWidth();
Collision.Bottom = movey + this->BlockImage.GetWidth();
}
void Block::Draw(sf::RenderWindow &Window)
{
if (IsHit == false)
{
Window.Draw(BlockSprite);
}
else if (IsHit == true)
{
return;
}
else
{
std::cout << "The Block you Tried to Draw to the Window In Member Function Block::Draw() was not Initialized!/n";
}
}
sf::FloatRect Block::GetCollision()
{
return Collision;
}
sf::Image Block::GetBlockImage()
{
return BlockImage;
}
sf::Sprite Block::GetBlockSprite()
{
return BlockSprite;
}
float Block::GetWidth()
{
return Width;
}
float Block::GetHeight()
{
return Height;
}
bool Block::GetIsHit()
{
return IsHit;
}
void Block::SetIsHit(bool x)
{
IsHit = x;
}

Main.cpp:

// BREAKOUT.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"

int _tmain(int argc, _TCHAR* argv[])
{
sf::RenderWindow App(sf::VideoMode(800, 600, 32), "Breakout!");
sf::Event Event;
sf::Image iRedPaddle;
sf::Image iGreenBall;
sf::Image iBlock;
sf::FloatRect LeftWall(0, 0, 1, 600);
sf::FloatRect RightWall(800, 0, 799, 600);
sf::FloatRect Top(0, 0, 800, 1);
sf::FloatRect Bottom(0, 599, 800, 600);
if (!iRedPaddle.LoadFromFile("images/RedPaddle.png"))
std::cout << "Could not load file: RedPaddle.png in folder: images" << std::endl;
if (!iGreenBall.LoadFromFile("images/GreenBall.png"))
std::cout << "Could not load file: GreenBall.png in folder: images" << std::endl;
if (!iBlock.LoadFromFile("images/Block.png"))
std::cout << "Could not load file: Block.png in folder: images" << std::endl;
Paddle PlayerPaddle(200, iRedPaddle);
Ball AI_Ball(iGreenBall, 200, 200);
Block TheBoard[20];
for (int x = 80, y = 25, index = 5; x < 200, index > 0; x = x+20, --index)
{
TheBoard[index].Initialize(x, y, iBlock);
}
for (int x = 80, y = 35, index = 10; x < 200, index > 5; x = x+20, --index)
{
TheBoard[index].Initialize(x, y, iBlock);
}
for (int x = 80, y = 45, index = 15; x < 200, index > 10; x = x+20, --index)
{
TheBoard[index].Initialize(x, y, iBlock);
}
for (int x = 80, y = 55, index = 20; x < 200, index > 15; x = x+20, --index)
{
TheBoard[index].Initialize(x, y, iBlock);
}
while (App.IsOpened())
{
while (App.GetEvent(Event))
{
if (Event.Type == sf::Event::Closed)
App.Close();
}
AI_Ball.Update(App);
AI_Ball.Test_For_Collision(LeftWall, RightWall, Top, Bottom, PlayerPaddle, TheBoard);
PlayerPaddle.Update(App);
App.Clear(sf::Color::White);
AI_Ball.Draw(App);
PlayerPaddle.Draw(App);
for (int index = -1; index < 19; ++index)
TheBoard[index].Draw(App);
App.Display();
}
return 0;
}

I'm almost certain it has something to do with arrays and references, so I'ms till looking. I read around and it said that this is only an error in Visual C++ because of a problem with them, but I'm not sure.

I'm a game programmer and computer science ninja !

Here's my 2D RPG-Ish Platformer Programmed in Python + Pygame, with a Custom Level Editor and Rendering System!

Here's my Custom IDE / Debugger Programmed in Pure Python and Designed from the Ground Up for Programming Education!

Want to ask about Python, Flask, wxPython, Pygame, C++, HTML5, CSS3, Javascript, jQuery, C++, Vimscript, SFML 1.6 / 2.0, or anything else? Recruiting for a game development team and need a passionate programmer? Just want to talk about programming? Email me here:

hobohm.business@gmail.com

or Personal-Message me on here !

Advertisement
Could you tell where in your code and when this error occurs? Just dumping a ton of code and a fairly vague error message doesn't really help here.

EDIT:

Also try to add some spacing and formatting to your code, and maybe add some documentation here and there as this code is pretty hard to read as it is.
Documenting code is also just good a good practice, even if it is just for fairly simple or trivial code. You don't want to have to figure out what a function does after not looking at it for a couple of months

I gets all your texture budgets!

0xC0000005: Access violation writing location 0x00000005

I haven't looked into your code but this means 2 things.

1) You are trying to read on an address where no object has been created.
2) You are reading out of an array. example: m_Array[max+1];

I think it's here:

void Ball::Test_For_Collision(sf::FloatRect LeftWall, sf::FloatRect RightWall, sf::FloatRect Top, sf::FloatRect Bottom, Paddle MyPaddle, Block Blocks[20])
{
for (int index = -1; index < 19; ++index)
{
if (Collision.Intersects(Blocks[index].GetCollision()) && Blocks[index].GetIsHit() == false)
{
speedy = -speedy;
Blocks[index].SetIsHit(true);
}
}


You start with index -1, but array[-1] is out of its memory. So memory access violation.


~EngineProgrammer
But it increments it to zero on the first loop, negating that problem. I did try your fix though, but it didn't work. I'll start trying to comment my code and add whitespace, but still, this error is eluding my face off :(

I'm a game programmer and computer science ninja !

Here's my 2D RPG-Ish Platformer Programmed in Python + Pygame, with a Custom Level Editor and Rendering System!

Here's my Custom IDE / Debugger Programmed in Pure Python and Designed from the Ground Up for Programming Education!

Want to ask about Python, Flask, wxPython, Pygame, C++, HTML5, CSS3, Javascript, jQuery, C++, Vimscript, SFML 1.6 / 2.0, or anything else? Recruiting for a game development team and need a passionate programmer? Just want to talk about programming? Email me here:

hobohm.business@gmail.com

or Personal-Message me on here !

The increment happens at the end of a loop iteration, not at the beginning, so you should initialize it at a value of 0 and not at -1 as that will indeed cause an access violation.

I gets all your texture budgets!

So, the initialized value( -1 ) will be the first value inside the for-loop. It's tricky because you see ++index in the same line.

You can see a for loop like this:

int index = 0;
do
{
// your collision
// increment index
} while( index < 19 )


Also, just noticed:
void Ball::Test_For_Collision(sf::FloatRect LeftWall, sf::FloatRect RightWall, sf::FloatRect Top, sf::FloatRect Bottom, Paddle MyPaddle, Block Blocks[20]) {...}

Block Blocks[20] is a parameter of you. This parameter creates 20 new Blocks that has not been initialized. Meaning they will give a break error indeed because you are asking their Collosions but the object itself hasn't been created...

Block& Blocks[] -> this should fix it. If not, Block* pBlocks will fix it then.


~EngineProgrammer
I have far more detail on the problem now:
It can't read from a location I'm trying to get it to read from. It's changed from error 0x00000005 to 0xccccccd0.

I'm a game programmer and computer science ninja !

Here's my 2D RPG-Ish Platformer Programmed in Python + Pygame, with a Custom Level Editor and Rendering System!

Here's my Custom IDE / Debugger Programmed in Pure Python and Designed from the Ground Up for Programming Education!

Want to ask about Python, Flask, wxPython, Pygame, C++, HTML5, CSS3, Javascript, jQuery, C++, Vimscript, SFML 1.6 / 2.0, or anything else? Recruiting for a game development team and need a passionate programmer? Just want to talk about programming? Email me here:

hobohm.business@gmail.com

or Personal-Message me on here !

Okay, I fixed all the for loops, but now it says an array of references is illegal.

I'm a game programmer and computer science ninja !

Here's my 2D RPG-Ish Platformer Programmed in Python + Pygame, with a Custom Level Editor and Rendering System!

Here's my Custom IDE / Debugger Programmed in Pure Python and Designed from the Ground Up for Programming Education!

Want to ask about Python, Flask, wxPython, Pygame, C++, HTML5, CSS3, Javascript, jQuery, C++, Vimscript, SFML 1.6 / 2.0, or anything else? Recruiting for a game development team and need a passionate programmer? Just want to talk about programming? Email me here:

hobohm.business@gmail.com

or Personal-Message me on here !

use the Block* pBlocks?

I don't work with dynamic or static arrays so kinda forgot what I can do and cannot do with them. smile.png
How would I implement that:
Block* pBlocks[] doesn't work either.

I'm a game programmer and computer science ninja !

Here's my 2D RPG-Ish Platformer Programmed in Python + Pygame, with a Custom Level Editor and Rendering System!

Here's my Custom IDE / Debugger Programmed in Pure Python and Designed from the Ground Up for Programming Education!

Want to ask about Python, Flask, wxPython, Pygame, C++, HTML5, CSS3, Javascript, jQuery, C++, Vimscript, SFML 1.6 / 2.0, or anything else? Recruiting for a game development team and need a passionate programmer? Just want to talk about programming? Email me here:

hobohm.business@gmail.com

or Personal-Message me on here !

This topic is closed to new replies.

Advertisement