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

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - Naoshikuu

Pages: [1]
1
Graphics / 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 !

Pages: [1]