SFML community forums
Help => General => Topic started by: JQ on August 15, 2018, 01:04:31 pm
-
Hello! I've got every time error when I trying in my class set font to Text.
Full error:" Exception thrown: read access violation.
**_Pnode** was 0xFFFFFFFFFFFFFFE6."
Full script with main()
#include "stdafx.h"
#include <SFML\Graphics.hpp>
#include <SFML\Window.hpp>
#include "JQEText.h"
using namespace sf;
using namespace std;
ContextSettings settings;
Font font;
Text test;
map<int,string> numbers;
//numbers["count"] = 6;
int main()
{
#pragma region Start
settings.antialiasingLevel = 8;
RenderWindow engine{ VideoMode{ 1920,1000 }, "JQEngine",0,settings };
engine.setFramerateLimit(60);
Event event;
engine.setPosition(Vector2i(0,0));
font.loadFromFile("Arkitech.ttf");
JQEText text = JQEText(25, font, "test", Vector2f(0, 0), Color(255, 255, 255));
//mousePosition.setColor(Color(0, 0, 0, 255));
#pragma endregion
while (true)
{
engine.clear(Color(255 * 0.4, 255 * 0.4, 255*0.4, 255));
engine.pollEvent(event);
if (event.type == Event::Closed)
{
engine.close();
break;
}
else if (event.type == Event::MouseButtonPressed)
{
//Mouse::getPosition(engine).x Mouse::getPosition(engine).y;
}
engine.draw(test);
engine.draw(text);
engine.display();
}
return 0;
}
Full class.h
#pragma once
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <SFML\Graphics.hpp>
#include <SFML\Window.hpp>
#include <cstdlib>
#include <ctime>
#include <string>
#include <Windows.h>
using namespace sf;
using namespace std;
class JQEText : public Drawable
{
public:
JQEText(int size, Font font, string ttext, Vector2f position, Color color);
~JQEText();
Text text;
private:
void draw(RenderTarget& target, RenderStates state) const override;
};
Full class.cpp
#include "stdafx.h"
#include "JQText.h"
JQText::JQText(int size, Font font, string ttext, Vector2f position, Color color)
{
text.setCharacterSize(size);
text.setFont(font); // when I added "//" before that command I don't get this error
text.setString(ttext);
text.setPosition(position);
text.setFillColor(color);
}
JQText::~JQText()
{
}
void JQText::draw(RenderTarget& target, RenderStates state) const
{
target.draw(text, state);
}
-
Don't use globals. SFML doesn't support having it's resources (ContextSettings, Font and Text in your case) as globals. It's also a bad design.
-
Don't use globals. SFML doesn't support having it's resources (ContextSettings, Font and Text in your case) as globals. It's also a bad design.
How Can I make this resources don't global? I tried something like that:
#include "stdafx.h"
#include <SFML\Graphics.hpp>
#include <SFML\Window.hpp>
#include "JQEText.h"
using namespace sf;
using namespace std;
map<int,string> numbers;
//numbers["count"] = 6;
int main()
{
#pragma region Start
ContextSettings settings;
Font font;
settings.antialiasingLevel = 8;
RenderWindow engine{ VideoMode{ 1920,1000 }, "JQEngine",0,settings };
engine.setFramerateLimit(60);
Event event;
engine.setPosition(Vector2i(0,0));
font.loadFromFile("Arkitech.ttf");
JQEText text = JQEText(25, font, "test", Vector2f(0, 0), Color(255, 255, 255));
//mousePosition.setColor(Color(0, 0, 0, 255));
#pragma endregion
//rest of code
}
And I still got this error