Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: First occurence of tutorial code (Sprites) always fails  (Read 1051 times)

0 Members and 1 Guest are viewing this topic.

Naoshikuu

  • Newbie
  • *
  • Posts: 1
    • View Profile
First occurence of tutorial code (Sprites) always fails
« on: February 27, 2017, 10:53:30 pm »
Hello; // I'm new here so please tell me if I behaved badly somehow !

I am using SFML to create a little game but as I was innocently following the tutorial about the Textures / Sprites, the copy-pasted version of the tutorial gave a (weird?) bug. 
Here's the code I'm using:

#include <iostream>
#include "tests.h" // where you can find the "testSFMLfromTests"; really the same code as follows:

using namespace std;

void testSFMLfromMain() {  // pretty much the tutorial code about window & textures & sprites
        // window
        sf::RenderWindow window(sf::VideoMode(1920, 1200), "tests SFML");
        // texture definition & load
        sf::Texture texture;
        if (!texture.loadFromFile("texture.jpg"))
        {
                // error...
        }
        // sprite definition & load
        sf::Sprite sprite;
        sprite.setTexture(texture);

        sf::Event event;
        // main loop
        while (window.isOpen())
        {
                while (window.pollEvent(event))
                {
                        if (event.type == sf::Event::Closed)
                        window.close();
                }

        window.clear(sf::Color::Black);
        window.draw(sprite);
        window.display();
    }
}


int main() {
        testSFMLfromTests(); // WHITE WINDOW
        testSFMLfromMain(); // Texture displayed
        testSFMLfromTests();// Texture displayed
        testSFMLfromMain(); // Texture displayed...
        return 0;
}
 

As said in the comments of the main, the little "testSFML" bit of code (create window, create&load texture/sprite, display.) always displays a white window the first time it is called.
I have put the same code in a file "tests.cpp" (the original problem didn't come from the main so I checked that)

No matter whether I put the function "fromMain" or "fromTests" first, the one that is first will display a white window while every following call will give out the expected image.

I should mention that I am compiling using a Makefile - I used the same options as in my game, for the test:
(sorry I didn't find a Makefile of shell-like language)

---- Makefile ------
CC=g++
OPT=-g -std=c++11 -lglut
CFLAGS= -Wall -Wextra -Wreorder
LIBS= -lsfml-graphics -lsfml-window -lsfml-system

# Executable(s)
exe: main.o tests.o
        $(CC) $(OPT) $(CFLAGS) main.o tests.o -o exe $(LIBS)


# Objet(s)
main.o: main.cpp tests.h
        $(CC) $(OPT) $(CFLAGS) -c main.cpp
tests.o: tests.cpp tests.h
        $(CC) $(OPT) $(CFLAGS) -c tests.cpp

# Cleaner
clean:
        rm *.o exe
-------------------------------------------------

So, I don't really know what I could have done wrong so that the tuto code doesn't work right away, but I would like to know if there is a way out of this ? I noticed that if I put the texture loading inside the main loop, it works pretty fine but I don't really want that in a game...
I guess I could create and close a window instantly, but before ever knowing how, I'd like to understand what's going on ! ^^

Sorry if there's something stupid I done, thanks for the help !

Hapax

  • Hero Member
  • *****
  • Posts: 3351
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: First occurence of tutorial code (Sprites) always fails
« Reply #1 on: March 01, 2017, 09:24:06 pm »
If your error is occurring in "testSFMLfromTests", you should probably show that.

Are you getting any output to the console?

EDIT: fixed typo ("show" was "should")
« Last Edit: March 02, 2017, 08:57:43 pm by Hapax »
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

 

anything