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.