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

Author Topic: Sprite not drawing, but no errors display.  (Read 3043 times)

0 Members and 1 Guest are viewing this topic.

asblargh

  • Newbie
  • *
  • Posts: 3
  • Eyelashes!
    • View Profile
Sprite not drawing, but no errors display.
« on: April 04, 2017, 12:49:06 am »
This used to work, but it showed all 9 health bars in a single 64x64 tile. I fixed the way it calculates how to select the correct one instead of all 9, so that "should" work.

Health tiles, 640x64 image with 64x64 tiles, first 64x64 tile is blank for 0 health


Code checks the health's percentage by increments of 10%, returning 0-10, then selects the correct tile. It should work...
if (int_durability < 10 && int_durability > 0)
{
    std::cout << "oh" << std::endl;
    health_quad = &health_vertices[(i + j * map_width) * 4];

    health_quad[0].position = sf::Vector2f(i * tile_size.x, j * tile_size.y);
    health_quad[1].position = sf::Vector2f((i + 1) * tile_size.x, j * tile_size.y);
    health_quad[2].position = sf::Vector2f((i + 1) * tile_size.x, (j + 1) * tile_size.y);
    health_quad[3].position = sf::Vector2f(i * tile_size.x, (j + 1) * tile_size.y);

    health_quad[0].texCoords = sf::Vector2f(int_durability * tile_size.x, 0);
    health_quad[1].texCoords = sf::Vector2f((int_durability + 1) * tile_size.x, 0);
    health_quad[2].texCoords = sf::Vector2f((int_durability + 1) * tile_size.x, tile_size.y);
    health_quad[3].texCoords = sf::Vector2f(int_durability * tile_size.x, tile_size.y);
}
else
{
    health_quad[0].position = sf::Vector2f(i * tile_size.x, j * tile_size.y);
    health_quad[1].position = sf::Vector2f((i + 1) * tile_size.x, j * tile_size.y);
    health_quad[2].position = sf::Vector2f((i + 1) * tile_size.x, (j + 1) * tile_size.y);
    health_quad[3].position = sf::Vector2f(i * tile_size.x, (j + 1) * tile_size.y);

    health_quad[0].texCoords = sf::Vector2f(0, 0);
    health_quad[1].texCoords = sf::Vector2f(64, 0);
    health_quad[2].texCoords = sf::Vector2f(64, 64);
    health_quad[3].texCoords = sf::Vector2f(0, 64);
}

This is the part of the code that turns the current health sf::RenderTexture that displays over the tilemap into a sprite, which is then called to draw from the main game loop.

///DISPLAY TILES
    states.transform *= getTransform();
    states.texture = &m_tileset;

    r_texture.clear();
    r_texture.draw(environment_vertices, states);
    r_texture.draw(floor_vertices, states);
    r_texture.draw(block_vertices, states);
    r_texture.draw(object_vertices, states);
    r_texture.display();

    loaded_map.setTexture(r_texture.getTexture());

    ///DISPLAY HEALTH READINGS
    states.texture = &h_texture;

    r_texture_health.clear();
    r_texture_health.draw(health_vertices, states);
    r_texture_health.display();

    loaded_health_displays.setTexture(r_texture_health.getTexture());

Accessed in the main loop by:

window.draw(tmap.getHealthTexture());

Code to return the sprite:

sf::Sprite getHealthTexture()
{
    return loaded_health_displays;
}

health_vertices is declared like every other tile vertex:
        object_vertices.setPrimitiveType(sf::Quads);
        object_vertices.resize(map_width * map_height * 4);

        health_vertices.setPrimitiveType(sf::Quads);
        health_vertices.resize(map_width * map_height * 4);

Any ideas? Like I previously mentioned, it correctly gets the texture because it used to display it incorrectly, but when I "fixed" that I encountered this flaw in my design. It also correctly selects the tiles that have less than maximum durability, so that's not the issue. (aka it says "oh" for every damaged tile)

Thanks in advance!
« Last Edit: April 04, 2017, 12:50:58 am by asblargh »

Hapax

  • Hero Member
  • *****
  • Posts: 3360
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Sprite not drawing, but no errors display.
« Reply #1 on: April 04, 2017, 01:15:49 am »
I must admit that I'm not entirely sure of the thing you are trying to fix.

This used to work
When did it work? What was the code at that point?


One thing to note is that health_quad[0].position is being set the same way regardless of the if condition so should be done outside of that conditional block (and therefore only coded once instead of twice).

Moving that to happen before if condition, you may also notice that the health_quad assignment would also need to be moved before; this also highlights that health_quad is not updated to the new health_vertices if the condition is false.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

asblargh

  • Newbie
  • *
  • Posts: 3
  • Eyelashes!
    • View Profile
Re: Sprite not drawing, but no errors display.
« Reply #2 on: April 04, 2017, 01:30:06 am »
I must admit that I'm not entirely sure of the thing you are trying to fix.

Once I've compiled the RenderTexture for the loaded_health_displays sprite and sent it to the window.draw() function, nothing shows up.

It used to work, but at that time, it would display the entire health.png in a single 64x64 tile, erratically, really. I'm just using that as a confirmation that it actually loads the texture correctly, because I haven't messed with that aspect since.

I fixed the errors that you've pointed out, also, I'm using your NinePatch.hpp, so thanks for that!

Hapax

  • Hero Member
  • *****
  • Posts: 3360
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Sprite not drawing, but no errors display.
« Reply #3 on: April 04, 2017, 03:27:27 am »
I'm not sure what the transform is that you're using for these objects. Should it be applied to both "render sprites"? Also, why is it multiplied?

I'm using your NinePatch.hpp, so thanks for that!
You are welcome! I'm very pleased that you find it useful.
One obvious question, though: why are you not using Selba Ward's Progress Bar too? :P
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

asblargh

  • Newbie
  • *
  • Posts: 3
  • Eyelashes!
    • View Profile
Re: Sprite not drawing, but no errors display.
« Reply #4 on: April 04, 2017, 03:47:49 am »
I'm not sure what the transform is that you're using for these objects. Should it be applied to both "render sprites"? Also, why is it multiplied?

I'm using your NinePatch.hpp, so thanks for that!
You are welcome! I'm very pleased that you find it useful.
One obvious question, though: why are you not using Selba Ward's Progress Bar too? :P

Just following this tilemap here, even though the one I use now is modified a decent amount to fit my needs. https://www.sfml-dev.org/tutorials/2.4/graphics-vertex-array.php#example-tile-map

Ha, I thought about it but I figured what I had planned would be a little less intensive (even though I'm still getting thousands of FPS, ha).

Hapax

  • Hero Member
  • *****
  • Posts: 3360
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Sprite not drawing, but no errors display.
« Reply #5 on: April 04, 2017, 06:02:45 pm »
Okay. I was just checking to see if you were using any custom transformations or something weird first ;D

The problem I can see, though, is that everything is being transformed more than once.
Co-ordinates are transformed when drawing to the render texture and then the render sprite is transformed when drawing to the window.
You should probably only be doing one of these things i.e. states.transform needs to be modified whenever it's used. However, depending on your setup, you may not even need to use states on one of them at all.

My suggestion:
if your render texture matches the window, transform to the texture then draw the render sprite without transformations.
if your render texture matches the object, draw the object without transformations to the render texture and then applies the transformation to the render sprite when drawing to the window.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*