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.


Topics - MediocreVeg1

Pages: [1]
1
System / [SOLVED] Frame-independent movement with sf::Clock
« on: January 05, 2021, 09:37:50 am »
I've tried to implement frame-independent movement for my player, and it should be working, but I'm still seeing slowdown in player movement.

Not sure if this is relevant to the question, but for my player I'm using tile movement (Basically when you press a direction key the player will move a fixed distance over a few frames until it reaches the next tile. When I press a direction key, the player's clock is restarted and a bool isMoving is set to true until the player reaches the next tile. With that said, here's just a minimal example of where I've used the clock for frame-independent movement:

if (isMoving)
{
    if (clock.getElapsedTime().asSeconds() > (1 / 30))
    {
        move(/* For now this is 2 pixels in the desired direction */);
        movePixelsLeft -= speed; // when this reaches 0 it means the player has reached the next tile
        clock.restart();
    }
    if (movePixelsLeft <= 0)
    {
        isMoving = false;
        if (getColMapPosNum() > 1)
        {
            isMoving = false;
            // some other stuff
        }
    }
}
 

On a side note, my game seems to be unable to go over 40FPS, is there anything i can do to improve performance?

2
Hi, sorry for posting again so quickly, but I'm having an issue with multiple inheritance of sf::RectangleShape and sf::Text.
The reason I'm doing this is because I need to implement textboxes and I'd prefer to use inheritance over encapsulation (in which case the rectangle and text would be separate private objects) since i have also used inheritance for my player, NPCs and tile maps. However, when I try to draw this, I get the following error:
‘sf::Drawable’ is an ambiguous base of ‘Textbox’
Here is my Textbox constructor (where i also draw the textbox) in case you need it.
Textbox::Textbox(sf::Vector2f position, sf::Vector2f dimensions, std::vector<std::string> text, sf::RenderWindow& window)
{
    RectangleShape::setPosition(position);
    Text::setPosition(position.x + 3, position.y + 6);
    if (!font.loadFromFile("assets/DeterminationMono.ttf"))
        exit(1);
    Text::setFont(font);

    for (std::string i : text)
    {
        Text::setString(i);
        window.draw(*this);
    }
}
 

Once again, it's pretty late at night here, so I may respond in some hours :)

3
Graphics / Textures not loading properly in sprites in vectors
« on: December 20, 2020, 05:54:36 pm »
Sorry about the ambiguous title, it's a bit hard to explain :-[
I'm also a new to this forum and SFML in general, so please bear with me.
Basically, I created a class Npc which is a child class of sf::Sprite and has a few extra variables and objects in it, one of which is sf::Texture texture. I also made an std::vector<Npc> to contain multiple NPC sprites for a single map. However, when i run my program, only the texture of the last Npc object actually shows, while the others are simply a blank, white square. Since I've used a for loop to put the information (lilke position, texture file, etc.), the code for all NPCs is identical, so i fail to understand why only the last NPC's texture is showing.

The code for The vector is somewhat confusing to show since I've made a parser and used std::variant, but if you ask for it (or anything else like a screenshot of the game), I'll try my best to show only the necessary infromation. It's also night where I am, so it could take some hours before I respond in the morning or afternoon. Thanks in advance  :D

Pages: [1]
anything