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

Pages: [1]
1
Thanks for the detailed explanation. Everything's clear now.

PS: could you post again, so I can see if your post count really is in hex? You're at 9 in the least significant digit :P

2
This is a triple post, but I think it's the cleanest way to post it...

I think the problem with the first piece of code is that when I return the game from new_game(), game (along with its members) gets copied, and the texture getting copied causes the white sprite problem.


3
Moreover, changing the texture to a pointer and creating it with new works (but I'm pretty sure this is even worse C++):

#define SFML_STATIC

#include <stdint.h>
typedef uint64_t u64;

#define TILE_SIZE 32

#define WIDTH  640
#define HEIGHT 640

#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>

struct State {
    sf::Texture* dirt_texture;
    sf::Sprite dirt_sprite;

    sf::RenderWindow* window;
};

void update(State state) {
}

void draw(State state) {
    state.window->clear(sf::Color::Magenta);

    for (u64 x = 0; x < 64; x++) {
        for (u64 y = 0; y < 64; y++) {
            state.dirt_sprite.setPosition(x * TILE_SIZE, y * TILE_SIZE);
            state.window->draw(state.dirt_sprite);
        }
    }

    state.window->display();
}

State new_state(sf::RenderWindow* window) {
    State state;
    state.window = window;
   
    state.dirt_texture = new sf::Texture();
    if (!state.dirt_texture->loadFromFile("art/dirt.png")) {
        printf("Error loading dirt!!!!\n");
    }

    state.dirt_sprite.setTexture(*state.dirt_texture);

    return state;
}

int main() {
    sf::RenderWindow window(sf::VideoMode(WIDTH, HEIGHT), "My window");
    State state = new_state(&window);

    while (window.isOpen()) {
        sf::Event event;

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

        update(state);
        draw(state);
    }

    return 0;
}
 

This suggests that my misunderstanding is coming from C++ and not from SFML. What's the problem of the first version?

4
This is an entire, minimal, runnable example, stripped from anything that wasn't related to the problem. It's not idiomatic C++ and there are style problems, but for now I'm interested in understanding why this does not work.

The sprite seems to draw as a white square, even though I *think* I'm properly keeping the texture alive.

In my draw function I clear everything in magenta. If I don't move the sprite, only a small white square on the top left is drawn. This confirms that the sprite, of the right size, is drawn; but it's just white instead of the correct image.

#define SFML_STATIC

#include <stdint.h>
typedef uint64_t u64;

#define TILE_SIZE 32

#define WIDTH  640
#define HEIGHT 640

#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>

struct State {
    sf::Texture dirt_texture;
    sf::Sprite dirt_sprite;

    sf::RenderWindow* window;
};

void update(State state) {
}

void draw(State state) {
    state.window->clear(sf::Color::Magenta);

    for (u64 x = 0; x < 64; x++) {
        for (u64 y = 0; y < 64; y++) {
            state.dirt_sprite.setPosition(x * TILE_SIZE, y * TILE_SIZE);
            state.window->draw(state.dirt_sprite);
        }
    }

    state.window->display();
}

State new_state(sf::RenderWindow* window) {
    State state;
    state.window = window;
   
   
    if (!state.dirt_texture.loadFromFile("art/dirt.png")) {
        printf("Error loading dirt!!!!\n");
    }

    state.dirt_sprite.setTexture(state.dirt_texture);

    return state;
}

int main() {
    sf::RenderWindow window(sf::VideoMode(WIDTH, HEIGHT), "My window");
    State state = new_state(&window);

    while (window.isOpen()) {
        sf::Event event;

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

        update(state);
        draw(state);
    }

    return 0;
}
 

I do get this warning in the console output:

An internal OpenGL call failed in Texture.cpp(98).
Expression:
   glFlush()
Error description:
   GL_INVALID_OPERATION
   The specified operation is not allowed in the current state.

Pages: [1]