• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Gaius Baltar

Drawing Text Access Violation using SFML

2 posts in this topic

call stack:
 
> sfml-graphics-d-2.dll!std::_Tree<std::_Tmap_traits<unsigned int,sf::Font::Page,std::less<unsigned int>,std::allocator<std::pair<unsigned int const ,sf::Font::Page> >,0> >::_Lbound(const unsigned int & _Keyval) Line 2109 C++
  sfml-graphics-d-2.dll!std::_Tree<std::_Tmap_traits<unsigned int,sf::Font::Page,std::less<unsigned int>,std::allocator<std::pair<unsigned int const ,sf::Font::Page> >,0> >::lower_bound(const unsigned int & _Keyval) Line 1575 C++
  sfml-graphics-d-2.dll!std::map<unsigned int,sf::Font::Page,std::less<unsigned int>,std::allocator<std::pair<unsigned int const ,sf::Font::Page> > >::operator[](const unsigned int & _Keyval) Line 226 C++
  sfml-graphics-d-2.dll!sf::Font::getGlyph(unsigned int codePoint, unsigned int characterSize, bool bold) Line 295 C++
  sfml-graphics-d-2.dll!sf::Text::ensureGeometryUpdate() Line 269 C++
  sfml-graphics-d-2.dll!sf::Text::getLocalBounds() Line 214 C++
  Engine.exe!Text::getLocalBounds() Line 41 C++
  Engine.exe!Button::setTextSize(unsigned int textSize) Line 56 C++
  Engine.exe!Button::setString(std::basic_string<char,std::char_traits<char>,std::allocator<char> > text) Line 47 C++
  Engine.exe!Button::Button(std::basic_string<char,std::char_traits<char>,std::allocator<char> > text, unsigned int textSize, sf::Vector2<float> pos, sf::Color textColor, sf::Color buttonFaceColor) Line 7 C++
  Engine.exe!Missile_Command::initButtons() Line 56 C++
  Engine.exe!Missile_Command::init() Line 29 C++
  Engine.exe!Missile_Command::Missile_Command() Line 6 C++
  Engine.exe!main() Line 4 C++
  [External Code]
  [Frames below may be incorrect and/or missing, no symbols loaded for kernel32.dll]
 
code:
 

void Text::draw(sf::RenderTarget & target, sf::RenderStates states) const{
target.draw(text);
}

 
which was called by:
 

void Button::draw(sf::RenderTarget & target, sf::RenderStates states) const{
target.draw(buttonFace);
target.draw(text);
}

 
No pointers were used to my knowledge.
 
 
The exact error is as follows:
Unhandled exception at 0x0F21F95C (sfml-graphics-d-2.dll) in Engine.exe: 0xC0000005: Access violation reading location 0x0000000D.
 
I've been trying to solve this for quite some time now. I think it has something to do with my sf::Text object not being properly initialized or something, but I couldn't find anything.

 

 
Here's the my Text class if anyone's interested:
 
[spoiler]

#include "init.h"
 
Text::Text(sf::Vector2f pos, size_t size, sf::Color color, std::string text, std::string font){
setPosition(pos);
setSize(size);
setColor(color);
setText(text);
setFont(font);
}
 
Text::Text(std::string font, std::string text){
setText(text);
setFont(font);
}
 
std::string Text::getString() const{
return text.getString();
}
 
sf::Text Text::getText() const{
return text;
}
 
sf::Color Text::getColor() const{
return text.getColor();
}
 
int Text::getSize() const{
return text.getCharacterSize();
}
 
sf::Vector2f Text::getPosition() const{
return text.getPosition();
}
 
sf::Font Text::getFont() const{
return font;
}
 
sf::FloatRect Text::getLocalBounds() const{
return text.getLocalBounds();
}
 
void Text::setText(std::string text){
if (text != "" && text != " ")
this->text.setString(text);
}
 
void Text::setColor(sf::Color color){
text.setColor(color);
}
 
void Text::setSize(size_t size){
text.setCharacterSize(size);
}
 
void Text::setPosition(sf::Vector2f pos){
text.setPosition(pos);
}
 
void Text::setFont(std::string file){
if (!font.loadFromFile(file)){
std::exit(-1);
}
text.setFont(font);
}
 
void Text::draw(sf::RenderTarget & target, sf::RenderStates states) const{
target.draw(text);
}

[/spoiler]
 
"text" and "font" are private class members.

0

Share this post


Link to post
Share on other sites

How are you using it? Could you post the code where you actually configure the text you're using?

The sf::Text depends on the font you're passing on to it, it stores just a pointer to it, so you must make sure that pointer is valid (as in not destroy the font before destroying the text).

So, how are you passing that font along? Are you, by any chance using something like myFont.getFont() ? It looks like you may be passing a font that is valid at a given time, but then comes out of scope and generates undefined behavior.

Edited by dejaime
0

Share this post


Link to post
Share on other sites

Ok, I think I found the source of the problem.

_quit = ui::Button("Quit", 32, sf::Vector2f(0, 200), sf::Color::Black, "segoeuil.ttf");
_start = ui::Button("Start", 32, sf::Vector2f(0, 300), sf::Color::Black, "segoeuil.ttf");

For some reason, re instantiating the Button objects and then drawing them gives me the out of range error.

I found that if I immediately instantiated the Button objects the error wouldn't appear.

 

I made a minimal working example to demonstrate.

 

main.cpp:

#include <SFML/Graphics.hpp>
class Text : public sf::Drawable
{
	sf::Font font;
	sf::Text text;
public:
	Text(std::string string){
		if (!font.loadFromFile("segoeuil.ttf")) // Nothing wrong with loading the font
			std::exit(-1);
		text = sf::Text(string, font, 30);
	}
	Text(){ }

	virtual void draw(sf::RenderTarget & target, sf::RenderStates states) const{
		target.draw(text);
	}
};

int main(){
	sf::RenderWindow window(sf::VideoMode(800, 600), "Text test");

	Text text("Text");
        //Text text; -> same result

	// Error (on draw) -> Access Violation
	// Commenting this line out, solves the problem
	text = Text("Text");

	while (window.isOpen()){
		window.clear();
		window.draw(text);
		window.display();
	}

	return 0;
}

Edit: Solved! I was missing a = operator overload, because SFML stores it's fonts as pointers in it's text objects.

 

so this is what I added to make the above code work:

Text& operator=(Text other){
	font = other.font;
	text = other.text;
	text.setFont(font);
	return *this;
}
Edited by Gaius Baltar
0

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  
Followers 0