Hi, I recently faced an access violation error. I was wrapping the sf::Texture and sf::Sprite classes into a GameObject class. Here is the data structure:
>Bitmaps
>System
>system_logo.png
>Framework
>System
>EventManager
>GameObject
>GUIElement
>MusicObject
>SoundObject
>WindowManager
>Stages
>LanguageSelect
>Stage
>Title
>main.cpp
main.cpp
#include "Framework/System/WindowManager/WindowManager.h"
#include "Framework/System/EventManager/EventManager.h"
#include "Framework/System/GameProperties/GameProperties.h"
#include "Framework/System/InputManager/InputManager.h"
#include "Stages/Stage/Stage.h"
#include "Stages/LanguageSelect/LanguageSelect.h"
#include "Stages/Title/Title.h"
int main()
{
GameProperties gameProperties;
WindowManager windowManager(640, 360, "Rabid Streets", false);
EventManager eventManager;
InputManager inputManager;
Stage* currentStage;
bool languageSelected = false;
if (languageSelected == false) {
currentStage = &LanguageSelect();
}
else {
currentStage = &Title();
}
if (!currentStage->Initialize()) {
//return 0;
}
while (windowManager.WindowIsOpen()) {
while (windowManager.PollEvents(eventManager.GetEvents())) {
if (eventManager.IsLostFocus()) {
gameProperties.SetIsGamePaused(true);
}
}
currentStage->Render();
windowManager.ClearWindow();
currentStage->Draw();
windowManager.DisplayWindowContents();
}
currentStage->Destroy();
return 0;
}
LanguageSelect.cpp
#include "LanguageSelect.h"
#include "../../Framework/System/WindowManager/WindowManager.h"
LanguageSelect::LanguageSelect() {
}
bool LanguageSelect::Initialize() {
//logo is initialized
sf::Texture texLogo;
if (!texLogo.loadFromFile("Bitmaps/System/system_logo.png")) {
return false;
}
logo.SetTexture(texLogo);
logo.SetGameObjectPosition(320, 50);
return true;
}
void LanguageSelect::Render() {
}
void LanguageSelect::Draw() {
logo.Draw(&WindowManager::GetWindow());
}
void LanguageSelect::Destroy() {
}
LanguageSelect.h
#ifndef LANGUAGESELECT_H
#define LANGUAGESELECT_H
#include "../Stage/Stage.h"
#include <SFML/Graphics.hpp>
#include "../../Framework/System/GUIElement/GUIElement.h"
class LanguageSelect : public Stage {
public:
LanguageSelect();
bool Initialize();
void Render();
void Draw();
void Destroy();
private:
GUIElement logo;
};
#endif
GUIElement.cpp
#include "GUIElement.h"
#include <iostream>
GUIElement::GUIElement() {
}
void GUIElement::SetGameObjectPosition(float xP, float yP) {
x = xP;
y = yP;
RefreshPosition();
}
void GUIElement::Draw(sf::RenderWindow* window) {
window->draw(*sprite);
}
void GUIElement::SetTexture(sf::Texture& tex) {
texture = &tex;
std::cout << texture << std::endl;
sprite->setTexture(tex);
sprite->setOrigin(sf::Vector2f(sprite->getTexture()->getSize().x * 0.5f, sprite->getTexture()->getSize().y * 0.5f));
}
void GUIElement::SetSpriteRect(int rX, int rY, int rWidth, int rHeight) {
sprite->setTextureRect(sf::IntRect(rX, rY, rWidth, rHeight));
}
void GUIElement::SetX(float value) {
x = value;
RefreshPosition();
}
void GUIElement::SetY(float value) {
y = value;
RefreshPosition();
}
float GUIElement::GetX() {
return x;
}
float GUIElement::GetY() {
return y;
}
void GUIElement::Move(float xP, float yP) {
x += xP;
y += yP;
RefreshPosition();
}
void GUIElement::SetColor(sf::Color color) {
sprite->setColor(color);
}
void GUIElement::RefreshPosition() {
sprite->setPosition(sf::Vector2f(x, y));
}
GUIElement.h
#ifndef GUIELEMENT_H
#define GUIELEMENT_H
#include <SFML/Graphics.hpp>
class GUIElement {
public:
GUIElement();
void SetGameObjectPosition(float xP, float yP);
void Draw(sf::RenderWindow* window);
void SetTexture(sf::Texture& tex);
void SetSpriteRect(int rX, int rY, int rWidth, int rHeight);
void SetX(float value);
void SetY(float value);
float GetX();
float GetY();
void Move(float x, float y);
void SetColor(sf::Color color);
protected:
sf::Texture* texture;
sf::Sprite* sprite;
float x;
float y;
private:
void RefreshPosition();
};
#endif
This function gives error:
void GUIElement::SetTexture(sf::Texture& tex) {
texture = &tex;
std::cout << texture << std::endl;
sprite->setTexture(tex);
sprite->setOrigin(sf::Vector2f(sprite->getTexture()->getSize().x * 0.5f, sprite->getTexture()->getSize().y * 0.5f));
}
This line:
sprite->setTexture(tex);
Thanks.