1
Graphics / sf::Text is giving me an Access Violation read location error
« on: September 10, 2015, 08:56:11 am »
I'm using Visual Studio Express 2013 on Windows 8 , and using SFML 2.1 from here
When ever I try to draw a text (which the font is loaded properly) it gives me this
Here is the code
Button.h
Button.cpp
UserInterface.cpp
When ever I try to draw a text (which the font is loaded properly) it gives me this
Quote
sfml-graphics-d-2.dll!0f76dce2() Unknown
[Frames below may be incorrect and/or missing, no symbols loaded for sfml-graphics-d-2.dll]
sfml-graphics-d-2.dll!0f772d9e() Unknown
sfml-graphics-d-2.dll!0f769bc1() Unknown
sfml-graphics-d-2.dll!0f75c7dd() Unknown
sfml-graphics-d-2.dll!0f7b29ba() Unknown
sfml-graphics-d-2.dll!0f78e583() Unknown
> SpellswordEditor.exe!Button::draw(sf::RenderTarget & target, sf::RenderStates states) Line 34 C++
sfml-graphics-d-2.dll!0f78e583() Unknown
SpellswordEditor.exe!UserInterface::update() Line 71 C++
SpellswordEditor.exe!main() Line 63 C++
[External Code]
Here is the code
Button.h
#ifndef BUTTON_H
#define BUTTON_H
#include <SFML/Graphics.hpp>
#include <iostream>
class Button : public sf::Drawable, public sf::Transformable
{
public:
Button();
Button(sf::Vector2f s, sf::Vector2f p, const char* f, const char* displayText = "Button", sf::Color tc = sf::Color::Black);
~Button();
bool isClicked();
void setPosition(sf::Vector2f p);
void setSize(sf::Vector2f s);
void setColor(sf::Color c);
void update(sf::Event& e, sf::RenderWindow& window);
private:
virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const
{
// apply the transform
states.transform *= getTransform();
switch (state)
{
case State::normal:
target.draw(normalStyle);
case State::inactive:
target.draw(clickStyle);
default:
target.draw(normalStyle);
}
target.draw(text);
};
enum State{
normal = 0,
hover = 1,
clicked = 2,
inactive = 3,
};
void checkMouseInButton();
State state;
bool isActive;
bool mouseInButton;
sf::Vector2f position;
sf::Vector2i mousePosition;
sf::Vector2f size;
sf::RectangleShape normalStyle;
sf::RectangleShape hoverStyle;
sf::RectangleShape clickStyle;
sf::Color textColor;
sf::Font font;
sf::Text text;
};
#endif BUTTON_H
#define BUTTON_H
#include <SFML/Graphics.hpp>
#include <iostream>
class Button : public sf::Drawable, public sf::Transformable
{
public:
Button();
Button(sf::Vector2f s, sf::Vector2f p, const char* f, const char* displayText = "Button", sf::Color tc = sf::Color::Black);
~Button();
bool isClicked();
void setPosition(sf::Vector2f p);
void setSize(sf::Vector2f s);
void setColor(sf::Color c);
void update(sf::Event& e, sf::RenderWindow& window);
private:
virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const
{
// apply the transform
states.transform *= getTransform();
switch (state)
{
case State::normal:
target.draw(normalStyle);
case State::inactive:
target.draw(clickStyle);
default:
target.draw(normalStyle);
}
target.draw(text);
};
enum State{
normal = 0,
hover = 1,
clicked = 2,
inactive = 3,
};
void checkMouseInButton();
State state;
bool isActive;
bool mouseInButton;
sf::Vector2f position;
sf::Vector2i mousePosition;
sf::Vector2f size;
sf::RectangleShape normalStyle;
sf::RectangleShape hoverStyle;
sf::RectangleShape clickStyle;
sf::Color textColor;
sf::Font font;
sf::Text text;
};
#endif BUTTON_H
Button.cpp
#include "Button.h"
Button::Button()
{
}
Button::Button(sf::Vector2f s, sf::Vector2f p, const char* f, const char* displayText, sf::Color tc) : textColor(tc), size(s), position(p)
{
if (font.loadFromFile(f))
{
isActive = true;
text.setFont(font);
text.setString(displayText);
text.setCharacterSize(size.x);
text.setColor(sf::Color::Black);
text.setPosition(position);
setPosition(position);
setSize(size);
setColor(sf::Color::Blue);
}
}
void Button::setPosition(sf::Vector2f p)
{
normalStyle.setPosition(p);
hoverStyle.setPosition(p);
clickStyle.setPosition(p);
}
void Button::setSize(sf::Vector2f s)
{
normalStyle.setSize(s);
hoverStyle.setSize(s);
clickStyle.setSize(s);
}
void Button::setColor(sf::Color c)
{
normalStyle.setFillColor(c);
hoverStyle.setFillColor(c);
clickStyle.setFillColor(c);
}
bool Button::isClicked()
{
if (state == State::clicked)
return true;
return false;
}
void Button::checkMouseInButton()
{
mouseInButton = mousePosition.x <= position.x + size.x
&& mousePosition.x >= position.x
&& mousePosition.y <= position.y + size.y
&& mousePosition.y >= position.y;
}
void Button::update(sf::Event& e, sf::RenderWindow& window)
{
if (isActive)
{
mousePosition = sf::Mouse::getPosition(window);
checkMouseInButton();
if (mouseInButton)
{
state = State::hover;
}
}
else{
state = State::normal;
}
}
Button::~Button()
{
}
Button::Button()
{
}
Button::Button(sf::Vector2f s, sf::Vector2f p, const char* f, const char* displayText, sf::Color tc) : textColor(tc), size(s), position(p)
{
if (font.loadFromFile(f))
{
isActive = true;
text.setFont(font);
text.setString(displayText);
text.setCharacterSize(size.x);
text.setColor(sf::Color::Black);
text.setPosition(position);
setPosition(position);
setSize(size);
setColor(sf::Color::Blue);
}
}
void Button::setPosition(sf::Vector2f p)
{
normalStyle.setPosition(p);
hoverStyle.setPosition(p);
clickStyle.setPosition(p);
}
void Button::setSize(sf::Vector2f s)
{
normalStyle.setSize(s);
hoverStyle.setSize(s);
clickStyle.setSize(s);
}
void Button::setColor(sf::Color c)
{
normalStyle.setFillColor(c);
hoverStyle.setFillColor(c);
clickStyle.setFillColor(c);
}
bool Button::isClicked()
{
if (state == State::clicked)
return true;
return false;
}
void Button::checkMouseInButton()
{
mouseInButton = mousePosition.x <= position.x + size.x
&& mousePosition.x >= position.x
&& mousePosition.y <= position.y + size.y
&& mousePosition.y >= position.y;
}
void Button::update(sf::Event& e, sf::RenderWindow& window)
{
if (isActive)
{
mousePosition = sf::Mouse::getPosition(window);
checkMouseInButton();
if (mouseInButton)
{
state = State::hover;
}
}
else{
state = State::normal;
}
}
Button::~Button()
{
}
UserInterface.cpp
#include "UserInterface.h"
#include <iostream>
UserInterface::UserInterface(sf::Event& e, float window_w, float window_h, sf::RenderWindow &w) : window(w), window_width(window_w), window_height(window_h), event(e)
{
createToolbar();
createMapView();
createPropMenu();
button = Button(sf::Vector2f(100.0f, 75.0f), sf::Vector2f(0.0f, 0.0f), "Fonts/MontereyFLF.ttf");
}
UserInterface::~UserInterface()
{
}
void UserInterface::createToolbar()
{
toolbarView.setSize(window_width, window_height * 0.05f);
toolbarView.setCenter(window_width / 2, (window_height * 0.05f) / 2);
toolbarView.setViewport(sf::FloatRect(0, 0, 1.0f, 0.05f));
toolbarBackground.setSize(sf::Vector2f(toolbarView.getSize().x, toolbarView.getSize().y));
toolbarBackground.setFillColor(sf::Color(100, 100, 100, 255));
toolbarBackground.setOutlineThickness(-3);
toolbarBackground.setOutlineColor(sf::Color(150, 150, 150, 255));
}
void UserInterface::createMapView()
{
mapView.setSize(window_width * 0.8f, window_height * 0.95f);
mapView.setCenter(window_width/2, (window_height * 0.95f)/2);
mapView.setViewport(sf::FloatRect(0, 0.05f, 0.8f, 0.95f));
}
void UserInterface::createPropMenu()
{
propMenuView.setSize(window_width * 0.2f, window_height * 0.95f);
propMenuView.setCenter((window_width * 0.2f) / 2, (window_height * 0.95f) / 2);
propMenuView.setViewport(sf::FloatRect(0.8f, 0.05f, 0.2f, 0.95f));
propMenuBackground.setSize(sf::Vector2f(propMenuView.getSize().x, propMenuView.getSize().y));
propMenuBackground.setFillColor(sf::Color(100, 100, 100, 255));
propMenuBackground.setOutlineThickness(-3);
propMenuBackground.setOutlineColor(sf::Color(150, 150, 150, 255));
}
void UserInterface::moveMapView(float amountX, float amountY)
{
mapView.move(sf::Vector2f(amountX, amountY));
window.setView(mapView);
}
sf::Vector2f UserInterface::getMapCenter()
{
return mapView.getCenter();
}
sf::Vector2f UserInterface::getMouseToWorldCords()
{
return window.mapPixelToCoords(sf::Mouse::getPosition(window),window.getView());
}
void UserInterface::update()
{
window.setView(toolbarView);
window.draw(toolbarBackground);
button.update(event, window);
window.draw(button);
window.setView(propMenuView);
window.draw(propMenuBackground);
window.setView(mapView);
};
#include <iostream>
UserInterface::UserInterface(sf::Event& e, float window_w, float window_h, sf::RenderWindow &w) : window(w), window_width(window_w), window_height(window_h), event(e)
{
createToolbar();
createMapView();
createPropMenu();
button = Button(sf::Vector2f(100.0f, 75.0f), sf::Vector2f(0.0f, 0.0f), "Fonts/MontereyFLF.ttf");
}
UserInterface::~UserInterface()
{
}
void UserInterface::createToolbar()
{
toolbarView.setSize(window_width, window_height * 0.05f);
toolbarView.setCenter(window_width / 2, (window_height * 0.05f) / 2);
toolbarView.setViewport(sf::FloatRect(0, 0, 1.0f, 0.05f));
toolbarBackground.setSize(sf::Vector2f(toolbarView.getSize().x, toolbarView.getSize().y));
toolbarBackground.setFillColor(sf::Color(100, 100, 100, 255));
toolbarBackground.setOutlineThickness(-3);
toolbarBackground.setOutlineColor(sf::Color(150, 150, 150, 255));
}
void UserInterface::createMapView()
{
mapView.setSize(window_width * 0.8f, window_height * 0.95f);
mapView.setCenter(window_width/2, (window_height * 0.95f)/2);
mapView.setViewport(sf::FloatRect(0, 0.05f, 0.8f, 0.95f));
}
void UserInterface::createPropMenu()
{
propMenuView.setSize(window_width * 0.2f, window_height * 0.95f);
propMenuView.setCenter((window_width * 0.2f) / 2, (window_height * 0.95f) / 2);
propMenuView.setViewport(sf::FloatRect(0.8f, 0.05f, 0.2f, 0.95f));
propMenuBackground.setSize(sf::Vector2f(propMenuView.getSize().x, propMenuView.getSize().y));
propMenuBackground.setFillColor(sf::Color(100, 100, 100, 255));
propMenuBackground.setOutlineThickness(-3);
propMenuBackground.setOutlineColor(sf::Color(150, 150, 150, 255));
}
void UserInterface::moveMapView(float amountX, float amountY)
{
mapView.move(sf::Vector2f(amountX, amountY));
window.setView(mapView);
}
sf::Vector2f UserInterface::getMapCenter()
{
return mapView.getCenter();
}
sf::Vector2f UserInterface::getMouseToWorldCords()
{
return window.mapPixelToCoords(sf::Mouse::getPosition(window),window.getView());
}
void UserInterface::update()
{
window.setView(toolbarView);
window.draw(toolbarBackground);
button.update(event, window);
window.draw(button);
window.setView(propMenuView);
window.draw(propMenuBackground);
window.setView(mapView);
};