my problem is that when I want to divide the code into smaller functions it throws me a runtime error: access violation.
Since everything is passed by value I have no idea why such an error would occur. There is nothing to do about memory.
General code:
// main.cpp
#include "stdafx.h"
#include "SFML/Graphics.hpp"
#include "GameEngine.h"
int main()
{
sf::RenderWindow* window = new sf::RenderWindow(sf::VideoMode(200, 200), "SFML works!");
GameEngine game(window);
game.run();
delete window;
return 0;
}
// DrawableVertices.h
#pragma once
#include "SFML\Graphics.hpp"
using namespace sf;
class DrawableVertices : public Drawable, public Transformable
{
VertexArray vertices;
Texture* pTexture;
public:
virtual void draw(RenderTarget& target, RenderStates states) const;
DrawableVertices(VertexArray vertices, Texture* pTexture = nullptr);
DrawableVertices();
Texture* getTexture();
VertexArray* getVertices();
};
// DrawableVertices.cpp
#include "stdafx.h"
#include "DrawableVertices.h"
DrawableVertices::DrawableVertices(sf::VertexArray _vertices, sf::Texture* _pTexture)
{
vertices = _vertices;
pTexture = pTexture;
}
DrawableVertices::DrawableVertices()
{
vertices = sf::VertexArray(sf::Points, 1);
pTexture = nullptr;
}
void DrawableVertices::draw(sf::RenderTarget& target, sf::RenderStates states) const
{
states.transform *= getTransform();
states.texture = pTexture;
target.draw(vertices, states);
}
sf::Texture* DrawableVertices::getTexture() { return pTexture; }
sf::VertexArray* DrawableVertices::getVertices() { return &vertices; }
Working code (all in one function):
//GameEngine.h
#pragma once
#include "DrawableVertices.h"
class GameEngine
{
RenderWindow* pWindow;
public:
GameEngine(RenderWindow* window);
void run();
};
//GameEngine.cpp
#include "stdafx.h"
#include "GameEngine.h"
GameEngine::GameEngine(RenderWindow* window)
{
pWindow = window;
}
void GameEngine::run()
{
const int X_DIFF = 8;
const int PLAYER_Y = 24;
const int PLAYER_X = 48;
VertexArray playerShape(TrianglesStrip, 4);
playerShape[0].position = Vector2f(X_DIFF, 0);
playerShape[0].color = Color(0, 127, 255);
playerShape[1].position = Vector2f(PLAYER_X - X_DIFF, 0);
playerShape[1].color = Color(0, 127, 255);
playerShape[2].position = Vector2f(0, PLAYER_Y);
playerShape[2].color = Color(0, 0, 255);
playerShape[3].position = Vector2f(PLAYER_X, PLAYER_Y);
playerShape[3].color = Color(0, 0, 255);
DrawableVertices player(playerShape);
player.setOrigin(PLAYER_X / 2, PLAYER_Y / 2);
player.setPosition(64, 64);
while (pWindow->isOpen())
{
Event event;
while (pWindow->pollEvent(event))
{
if (event.type == Event::Closed)
pWindow->close();
}
pWindow->clear();
pWindow->draw(player);
pWindow->display();
}
}
Not working code, throwing access violation while drawing player [ pWindow->draw(player) line ]
//GameEngine.h
#pragma once
#include "DrawableVertices.h"
class GameEngine
{
RenderWindow* pWindow;
DrawableVertices player;
void createPlayer();
public:
GameEngine(RenderWindow* window);
void run();
};
// GameEngine.cpp
#include "stdafx.h"
#include "GameEngine.h"
GameEngine::GameEngine(RenderWindow* window)
{
pWindow = window;
createPlayer();
}
void GameEngine::createPlayer()
{
const int X_DIFF = 8;
const int PLAYER_Y = 24;
const int PLAYER_X = 48;
VertexArray playerShape(TrianglesStrip, 4);
playerShape[0].position = Vector2f(X_DIFF, 0);
playerShape[0].color = Color(0, 127, 255);
playerShape[1].position = Vector2f(PLAYER_X - X_DIFF, 0);
playerShape[1].color = Color(0, 127, 255);
playerShape[2].position = Vector2f(0, PLAYER_Y);
playerShape[2].color = Color(0, 0, 255);
playerShape[3].position = Vector2f(PLAYER_X, PLAYER_Y);
playerShape[3].color = Color(0, 0, 255);
player = DrawableVertices(playerShape);
player.setOrigin(PLAYER_X / 2, PLAYER_Y / 2);
player.setPosition(64, 64);
}
void GameEngine::run()
{
while (pWindow->isOpen())
{
Event event;
while (pWindow->pollEvent(event))
{
if (event.type == Event::Closed)
pWindow->close();
}
pWindow->clear();
pWindow->draw(player);
pWindow->display();
}
}
What's worse when I display all player's information like position etc. inside run() method it shows the proper result. It means that player is initialised properly and it exists like it is supposed to.
Sorry for a lot of code, but wanted to show everything that could be useful. Obviously my whole code is bigger, I just put the part making problems into a separate project.
// Edit
Yeah, debugger says that pTexture member of player has a bad pointer.
image from debugging working and not working code
I suppose that overloading = operator might fix the problem. However, I want to understand how it works that this code works without any problems:
void GameEngine::run()
{
DrawableVertices player = DrawableVertices(playerShape);
// ...
pWindow->draw(player);
}
And packing it into a fuction doesn't:
void GameEngine::createPlayer()
{
// ...
DrawableVertices player = DrawableVertices(playerShape);
}
void GameEngine::run()
{
createPlayer();
// ...
pWindow->draw(player);
}