Sign in to follow this  
superman3275

[SFML] Breakout Help :(

Recommended Posts

superman3275    2061
Today, I started programming breakout. I have the main functionality done, but whenever I run the program it gives me the error:
[CODE]
0xC0000005: Access violation writing location 0x00000005
[/CODE]
in Visual C++ 2010 Express Edition. Here's the Rest of the Code:
Paddle.h:
[CODE]
#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;
};
[/CODE]
Paddle.cpp:
[CODE]
#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;
}
[/CODE]
Ball.h:
[CODE]
#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;
};
[/CODE]
Ball.cpp:
[CODE]
#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;
}
[/CODE]
Block.h:
[CODE]
#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;
};
[/CODE]
Block.cpp:
[CODE]
#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;
}
[/CODE]
Main.cpp:
[CODE]
// 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;
}
[/CODE]
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.

Share this post


Link to post
Share on other sites
Radikalizm    4807
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 Edited by Radikalizm

Share this post


Link to post
Share on other sites
[CODE]0xC0000005: Access violation writing location 0x00000005[/CODE]

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:
[CODE]
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);
}
}
[/CODE]

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


~EngineProgrammer

Share this post


Link to post
Share on other sites
superman3275    2061
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 :(

Share this post


Link to post
Share on other sites
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:

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

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

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

Share this post


Link to post
Share on other sites
Look at my code, look at your code, now look back at my code, and back at your code......

I do Block* pBlocks !!
Not Block* pBlocks[[[[[[[[....]]]]]]]] [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]

Share this post


Link to post
Share on other sites
superman3275    2061
This is my error code now:
1>------ Build started: Project: BREAKOUT, Configuration: Debug Win32 ------
1> stdafx.cpp
1> Ball.cpp
1>c:\users om\documents\visual studio 2010\projects\breakout\ball.cpp(38): error C2065: 'Blocks' : undeclared identifier
1>c:\users om\documents\visual studio 2010\projects\breakout\ball.cpp(38): error C2228: left of '.GetCollision' must have class/struct/union
1>c:\users om\documents\visual studio 2010\projects\breakout\ball.cpp(38): error C2065: 'Blocks' : undeclared identifier
1>c:\users om\documents\visual studio 2010\projects\breakout\ball.cpp(38): error C2228: left of '.GetIsHit' must have class/struct/union
1>c:\users om\documents\visual studio 2010\projects\breakout\ball.cpp(41): error C2065: 'Blocks' : undeclared identifier
1>c:\users om\documents\visual studio 2010\projects\breakout\ball.cpp(41): error C2228: left of '.SetIsHit' must have class/struct/union
1> BREAKOUT.cpp
1>c:\users om\documents\visual studio 2010\projects\breakout\breakout.cpp(29): warning C4244: 'argument' : conversion from 'int' to 'float', possible loss of data
1>c:\users om\documents\visual studio 2010\projects\breakout\breakout.cpp(29): warning C4244: 'argument' : conversion from 'int' to 'float', possible loss of data
1>c:\users om\documents\visual studio 2010\projects\breakout\breakout.cpp(34): warning C4244: 'argument' : conversion from 'int' to 'float', possible loss of data
1>c:\users om\documents\visual studio 2010\projects\breakout\breakout.cpp(34): warning C4244: 'argument' : conversion from 'int' to 'float', possible loss of data
1>c:\users om\documents\visual studio 2010\projects\breakout\breakout.cpp(39): warning C4244: 'argument' : conversion from 'int' to 'float', possible loss of data
1>c:\users om\documents\visual studio 2010\projects\breakout\breakout.cpp(39): warning C4244: 'argument' : conversion from 'int' to 'float', possible loss of data
1>c:\users om\documents\visual studio 2010\projects\breakout\breakout.cpp(43): warning C4244: 'argument' : conversion from 'int' to 'float', possible loss of data
1>c:\users om\documents\visual studio 2010\projects\breakout\breakout.cpp(43): warning C4244: 'argument' : conversion from 'int' to 'float', possible loss of data
1> Generating Code...
1> Skipping... (no relevant changes detected)
1> Paddle.cpp
1> Block.cpp
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
(Ignore the conversion problems, that's not a real bug it happens for a reason!)

Share this post


Link to post
Share on other sites
superman3275    2061
Dang, I'm so glad I wrote some error code that prints out if the block I'm printing on is uninitialized. I was acting on a block that was uninitialized. Now to figure it out.

Share this post


Link to post
Share on other sites
Do you know why this happens? Because you are thinking I'm going to handle over all the code..

Block* pBlocks, you can clearly see a ' * ' in my code.
This is a dereference. Or in other words, this is a pointer. It stores an address of an object it's location.

[url="http://www.cplusplus.com/doc/tutorial/pointers/"]http://www.cplusplus...orial/pointers/[/url]

I found 2 errors, I can't find any error at the moment. Try using break points. Put some breakpoints in your main functions and go over the methods one by one. When you get the break error after jumping over a method, it means you are doing some strange stuff inside that method.


~EngineProgrammer

Share this post


Link to post
Share on other sites
superman3275    2061
Okay, the problem was that I was starting at Blocks[20], which meant that it was not calling the function correctly, and luckily I TOLD it to print out an error message if I did that, but I just forgot about it. #facepalm Edited by superman3275

Share this post


Link to post
Share on other sites
superman3275    2061
I fixed it, I'm sorry I tried to do that, but it didn't work because that wasn't the problem. Sorry.
EDIT: I don't understand why I didn't just make the array of blocks member variables of the ball class, considering the ball was going to be the only other object interacting with them. My next goal: Change the code to make the Block array part of the ball class. Edited by superman3275

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this