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

Pages: [1]
1
System / Re: Frame-independent movement with sf::Clock
« on: January 05, 2021, 01:06:46 pm »
That worked, thanks a lot   ;D

2
System / Re: Frame-independent movement with sf::Clock
« on: January 05, 2021, 10:34:57 am »
Addressing Point 1: Before this, I did originally move 2 pixels every frame, but since my game is sometimes suffering from frame drops, it ends up affecting player speed.  When the game starts, the FPS also takes a few seconds to reach about 35-40FPS, so the player is much slower during that time

Quote
A common and simple solution is to choose a fixed speed (in units/s) and then move the player by speed * elapsed_time every frame.

Problem is, I have to move exactly 48 pixels (the size of a tile), so since the elapsed time could vary, the player might end up in-between tiles.

[EDIT] I added an fps counter to my game and indeed, the player slows down when the frame-rate drops significantly

3
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?

4
Hmm, I guess I'll just have to deal with encapsulation then, but thanks for the reply  :D

5
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 :)

6
Graphics / Re: Textures not loading properly in sprites in vectors
« on: December 20, 2020, 08:11:42 pm »
Sorry for the constant replies, i now understand that it has to do with the copying of the textures in the vector. I'll try looking for more libraries with better resource management, thanks for the help.

7
Graphics / Re: Textures not loading properly in sprites in vectors
« on: December 20, 2020, 08:07:28 pm »
Hmm, i checked the tutorial and another page on the forum, and the issue in both seems to be that, due to the texture being in a loop/function, it is erased when the loop/function ends. However, by specifically putting the texture in the Npc class, meaning that the texture will last as long as the object, shouldn't this not be a problem in the first place?

8
Graphics / Re: Textures not loading properly in sprites in vectors
« on: December 20, 2020, 08:00:55 pm »
Thanks for the reply, I'll look into it :)

9
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]