Can't load image with SFML - c++

Am trying to load an image with SFML using Xcode, but everytime I run the program, the window (which has been created using the code) flashes and disappears...
Here's the code I am using:
#include <SFML/Graphics.hpp>
#include <iostream>
#define SCREEN_WIDTH 1024
#define SCREEN_HEIGHT 1024
int main()
{
sf::RenderWindow window(sf::VideoMode(SCREEN_WIDTH, SCREEN_HEIGHT), "Orsome Game");
while(window.isOpen())
{
sf::Event e;
while(window.pollEvent(e))
{
switch (e.type)
{
case sf::Event::Closed:
window.close();
break;
}
}
sf::Image image;
if(!image.loadFromFile("sprite.png")){
return -1;
}
window.clear(sf::Color(255,255,255));
window.display();
}
return EXIT_SUCCESS;
}
Have also put the image file where the c++ file is, but it still doesn't work!

I would put an sf::Style::Default in the RenderWindow arguments, straight after "Orsome Game" window name. Because you are using the pollEvent, that would allow you to exit the window by pressing the x button in the top right corner, like a default application layout.
Is the picture in the same directory as your solution file? Or your main.cpp?
It should be, of course.
And, maybe, try loading your image with Texture and Sprite, like this:
sf::Texture texture;
texture.loadFromFile("picturename.png");
sf::Sprite sprite;
sprite.setTexture(texture);
inside the while window is open loop.
To draw it:
// after window.clear() ...
//... your code
window.draw(sprite);
//... other things to draw
// and then, your window.display();
But, probably, your sf::Image should also work. Tell me how it goes
EDIT:
Try this code and see if the window opens.
Otherwise, that might be the problem of setting up the SFML on your computer and you will have to find some good tutorial on how to set it up properly! But let's see:
#include <SFML/Graphics.hpp>
#include <iostream>
using namespace sf;
int main()
{
RenderWindow window(VideoMode(640, 640), "Test", Style::Default);
while (window.isOpen()) {
Event _event;
while (window.pollEvent(_event)) {
switch (_event.type) {
case Event::Closed:
window.close();
break;
}
}
window.clear();
// draw
// ...
window.display();
}
return 0;
}

Related

Why does SFML window not show when window is in a class?

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

Drawing in the main window from another class with SFML

I need some help because I'm trying to make a Tetris game, as my first project in C++, but I have some problems.
I would like to keep the main window declaration in the main file, and draw on this same window from a class BoxRenderer to draw all the Tetrominos, background etc.
But my Sprite doesn't show up, I have a black screen, here's my code:
main.cpp:
// SpaceOdyssey.cpp : définit le point d'entrée pour l'application console.
//
#include "stdafx.h"
#include <iostream>
#include <SFML/Graphics.hpp>
#include "BoxRenderer.h"
using namespace std;
int main()
{
sf::RenderWindow window(sf::VideoMode(200, 280), "Tetris-like by Orionss");
sf::Texture bgTexture;
if (!bgTexture.loadFromFile("sprites\\background.png"))
return EXIT_FAILURE;
BoxRenderer renderer(bgTexture);
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
if (event.type == sf::Event::Closed)
window.close();
window.clear();
renderer.render(window);
window.display();
}
return 0;
}
BoxRenderer.cpp:
#include "stdafx.h"
#include "BoxRenderer.h"
BoxRenderer::BoxRenderer(sf::Texture bgTexture)
{
m_bgTexture = bgTexture;
}
void BoxRenderer::render(sf::RenderWindow& win)
{
m_bgTexture.update(win);
sf::Sprite background(m_bgTexture);
win.draw(background);
}
From void sf::Texture::update(const Window &window) reference:
Update the texture from the contents of a window.
You are clearing the window just before that, and this overwrites your texture, so it's completely black. You don't want to be calling m_bgTexture.update(win); at all.

C++ SFML library code not working for drawing a text

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...

C++ SFML Draw to other window

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.

SFML textEntered not working properly

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.