This is probably going to sound like a stupid question...
I have this piece of code in C++, that uses the SFML library:
#include <SFML/Graphics.hpp>
#include <string>
void textDisplay(sf::Text& display_text, std::string display_str, sf::Font display_font, int char_size) {
display_text.setString(display_str);
display_text.setFont(display_font);
display_text.setCharacterSize(char_size);
display_text.setStyle(sf::Text::Regular);
}
int main() {
sf::RenderWindow window(sf::VideoMode(400, 400), "Game");
sf::Font arial;
if (!arial.loadFromFile("fonts/arial/arial.ttf")) {
return 0;
}
sf::Text text;
textDisplay(text, "Hello, World!", arial, 30);
sf::CircleShape shape(100.f, 10);
shape.setFillColor(sf::Color::Yellow);
while (window.isOpen()) {
sf::Event event;
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed) {
window.close();
}
}
window.clear();
window.draw(text);
window.display();
}
return 0;
}
Because I'm going to repeat the same four lines of code over and over when I'm trying to display the text onto an SFML screen, rather than repeating the code, I created a function (textDisplay) that takes in four arguments (the Text object itself, a string, a font, and the character size) and initializes the Text object for me.
For some reason, the text doesn't display - is there anything I'm doing wrong?
Some stuff:
The window is called window.
There's no error when compiled, so I have no idea where the error is.
The board itself is just black, there's no text on window.
If the error has to do with the function itself, can you recommend an alternative to typing the same four lines over and over again?
Found it, you were setting your text with a temporary font, since its a normal variable and not a reference/pointer:
void textDisplay(sf::Text& display_text, std::string display_str, sf::Font& display_font, int char_size) { //Just make it a reference and its done! :)
display_text.setString(display_str);
display_text.setFont(display_font);
display_text.setCharacterSize(char_size);
display_text.setStyle(sf::Text::Regular);
}
Related
I am working on a basic game in C++ using SFML for graphics. It is designed to use a grid system with functions to determine what should be displayed in each square. However, the compiler won't recognize the references to the window in the functions.
To keep it easier to expand there are functions for each type of terrain to be displayed, taking the coordinates as inputs (possibly not the correct term).
Good Question. SFML recommends that you create your window in the main function, but, if you want to modify it, you can simply pass it by reference. EX:
#include <SFML/Graphics.hpp>
void doSomething(sf::RenderWindow& window) {
sf::RectangleShape shape(sf::Vector2f(100, 100));
shape.setFillColor(sf::Color::Green);
shape.setPosition(50, 50);
window.draw(shape);
window.display();
}
int main() {
sf::RenderWindow window(sf::VideoMode(500, 500), "Test", sf::Style::Close);
while (window.isOpen()) {
sf::Event event;
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed) {
window.close();
}
}
window.clear(sf::Color::Black);
//draw here
doSomething(window);
}
return 0;
}
You should take a look at https://www.sfml-dev.org/tutorials/2.5/. Specifically I would look at https://www.sfml-dev.org/tutorials/2.5/window-window.php
I am trying to create a Screen class for SFML, however for some reason, the application works when using the Xcode example but as soon as I put the window into it's own class, it does not work. Why is this and how would I fix it?
Here is my code (adapted form the example):
Edit:
After reading the comments, I have changed to the following code. This still does not show a screen and the program still quits.
#include <SFML/Audio.hpp>
#include <SFML/Graphics.hpp>
#include "ResourcePath.hpp"
class Screen{
public:
sf::RenderWindow window;
Screen(){
sf::RenderWindow window(sf::VideoMode(800, 600), "SFML window");
}
};
int main(int, char const**)
{
Screen* screen = new Screen();
// Set the Icon
sf::Image icon;
if (!icon.loadFromFile(resourcePath() + "icon.png")) {
return EXIT_FAILURE;
}
screen->window.setIcon(icon.getSize().x, icon.getSize().y, icon.getPixelsPtr());
// Load a sprite to display
sf::Texture texture;
if (!texture.loadFromFile(resourcePath() + "cute_image.jpg")) {
return EXIT_FAILURE;
}
sf::Sprite sprite(texture);
// Create a graphical text to display
sf::Font font;
if (!font.loadFromFile(resourcePath() + "sansation.ttf")) {
return EXIT_FAILURE;
}
sf::Text text("Hello SFML", font, 50);
text.setFillColor(sf::Color::Black);
// Load a music to play
sf::Music music;
if (!music.openFromFile(resourcePath() + "nice_music.ogg")) {
return EXIT_FAILURE;
}
// Play the music
music.play();
// Start the game loop
while (screen->window.isOpen())
{
// Process events
sf::Event event;
while (screen->window.pollEvent(event))
{
// Close window: exit
if (event.type == sf::Event::Closed) {
screen->window.close();
}
// Escape pressed: exit
if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Escape) {
screen->window.close();
}
}
// Clear screen
screen->window.clear();
// Draw the sprite
screen->window.draw(sprite);
// Draw the string
screen->window.draw(text);
// Update the window
screen->window.display();
}
return EXIT_SUCCESS;
}
You have a logic error in your constructor. The way you created it, you leave Screen::window uninitialized, and create another RenderWindow object which becomes inaccessible as soon as execution leaves constructor.
Instead, you should do
class Screen{
public:
sf::RenderWindow window;
Screen():
window(sf::VideoMode(800, 600), "SFML window") {}
};
Everything else should work as expected, unless it does not have any other bugs. If you're curious about the syntax I've used, you can visit this link.
Hard to say. Maybe your accessing a memory address that isn't part of your program? Try using unique pointers and make_unique to define some of your pointers.
For example:
std::unique_ptr<sf::RenderWindow> window;
window = std::make_unique<sf::RenderWindow>(sf::VideoMode(WIDTH, HEIGHT), "PushBlox",sf::Style::Default);
I did make a project using sfml before, so feel free to check out my code here --> https://github.com/FromAlaska/ComputerScience/tree/2017/Projects/Frontier
I am feeling really frustrated with draw() not working in my SFML project. My compiler gives off no errors, my eyes doesn't catch a thing that's off (as a reference I am using official tutorial). The problem is that when window load it doesn't draw a thing. It just stays a white window without any text in it.
Where could be the problem?
#include <SFML/Graphics.hpp>
int main() {
sf::RenderWindow window(sf::VideoMode(800,600), "Trying to make a game");
sf::Font font;
if (!font.loadFromFile("arial.ttf"))
{
//error
}
sf::Text text;
text.setFont(font);
text.setString("Hello, World!");
text.setCharacterSize(50);
text.setColor(sf::Color::Red);
text.setPosition(10, 50);
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
}
window.clear();
window.draw(text);
window.display();
return 0;
}
You're closing the window before drawing the text...
I know how to draw text to window created with sf::RenderWindow(); but I need to draw text to already existing game window. It makes it go black, I don't want to erase whole window, just update text on it. Code:
#include <SFML/Graphics.hpp>
#include <SFML/Graphics/Font.hpp>
#include <windows.h>
#include <iostream>
using namespace std;
int main()
{
HWND hWindow = FindWindow(0, "Counter-Strike: Global Offensive");
if(!hWindow) exit(0);
else
{
int i=0;
string tmp, str;
sf::RenderWindow window(hWindow);
sf::Font font;
if(!font.loadFromFile("verdana.ttf"))
{
cout << "error";
}
sf::Text text;
text.setFont(font);
text.setCharacterSize(17);
text.setColor(sf::Color::White);
while (window.isOpen())
{
i++;
itoa(i, (char*)tmp.c_str(), 10);
str = tmp.c_str();
text.setString(str);
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
Sleep(1000);
window.clear(sf::Color::Transparent);
window.draw(text);
window.display();
}
return 0;
}
}
Please help me do it :)
I think it won't be as easy as you might thought.
Every program I know which can manipulate the backbuffer of a game hooks the API calls. That isn't very difficult. There are some good librarys out there for this. But the real problem is that nearly every game use DirectX.
SFML use OpenGL. I don't think they can be mixed.
You should use Direct2D or another drawing libary based on DirectX.
There are even tutorials out there which explains how to code a overlay. Just use Google.
so today I started looking into SFML and I found it quite interesting so decided to learn how to play with it, but I am already hitting some problems, I am trying to use textEntered event, but it is not working properly, it shows complete nonsense and text writes itself even without me pressing any key. Heres link
Code
#include <SFML/Graphics.hpp>
#include <iostream>
int main()
{
sf::RenderWindow window(sf::VideoMode(400, 400), "SFML works!");
std::string display;
sf::Font font;
font.loadFromFile("arial.ttf");
sf::Text text;
text.setFont(font);
text.setCharacterSize(30);
text.setStyle(sf::Text::Bold);
text.setColor(sf::Color::Red);
text.setPosition(50, 50);
while (window.isOpen())
{
sf::Event Revent;
while (window.pollEvent(Revent))
{
if (sf::Event::TextEntered)
{
std::cout << static_cast<char>(Revent.text.unicode);
//text.setString(display);
}
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))
{
window.close();
}
window.clear();
//window.draw(text);
window.display();
}
return 0;
}
You wrote if (sf::Event::TextEntered), which evaluates to true (since it's not equal to 0).
You probably meant
if (Revent.type == sf::Event::TextEntered).
Using Revent.text is undefined behavior in this case (when you're not sure what type of event Revent contains) because sf::Event is an union, so only one of its members is usable at a time. You can read more about SFML events here.