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

Pages: [1]
1
So I posted this on the ImGui issues tracker but they closed it and told me to post it on the SFML-ImGui issues tracker, except that one is inactive so this is my last hope to get an answer...

I'm using ImGui with SFML and I'm trying to make a text-box that stores a file path.



I followed what this post did: (https://github.com/ocornut/imgui/issues/2487#issuecomment-482474783) but what keeps happening is that the textbox will start repeating the first character typed into it until the entire buffer is filled with the same repeated character, example:





I also tried moving the buffer inside the loop and got the same result, what am I doing wrong?

2
Graphics / [SOLVED] Can't release 64x builds.
« on: January 22, 2022, 01:15:41 am »
Hey, it's been a while, since I've been using SFML casually this whole time, I didn't really bother with project configurations, and as such I've been working with 32-bit apps this whole time, but when I recently tried switching to 64-bit, I got a "cannot open source file" error for all SFML headers.





Any idea what that's about?


3
General / Setting up an SFML with Box2D in Visual Studio (2019).
« on: November 16, 2020, 12:33:35 pm »
I already had an SFML template project that I've been using for a while, and now I want to add Box2D to it, but after I thought I did everything correctly, I get external errors whenever I write anything using the Box2D library, I think the problem might be related to the additional dependencies, all the tutorials I found were outdated,can anyone walk me through the process of setting up the latest version of Box2D with SFML in Visual Studio (2019)?

4
Graphics / I can't get the math right. [SOLVED]
« on: October 02, 2020, 07:30:14 pm »
So I have a player character sprite, that rotates to follow the mouse cursor around, and a circle centered around the cursor:



I'm trying to calculate the coordinates of one of the points of the circle, that changes depending on the rotation of the player character, where the argument of the point will be equal to the rotation of the player character +90 degrees, so let's say I want to position a 10x10 blue rectangle shape at this point, it will looks like this:

If the player's rotation is equal to 180 => the point will have an argument of -90:

If the player's rotation is equal to 90 => the point will have an argument of 180:

If the player's rotation is equal to 135 => the point will have an argument of -135:


And, I'm half way there, here is my attempt at calculating the position of the rectangle (in the main loop), knowing that:

test is the blue square.
scope is the circle
25 is the radius if the circle (scope)
scope's rotation is literally equal to that of the player character:
scope.setRotation(entity_sprite.getRotation());

So the player character's rotation and the circle's rotation are interchangeable.

 test.setPosition(sf::Vector2f(player.scope.getPosition().x + cos(player.scope.getRotation() + 90) * 25, player.scope.getPosition().y + sin(player.scope.getRotation() + 90) * 25));

It...sort of works:


It doesn't appear in the gif because of low frame-rate, but the rectangle is spinning around the circle in the right directions, but it's doing it too quickly, I've had a similar problem when coding the character to follow the cursor, but I solved it by multiplying the angle outputted by the atan2 function by by 180/3.14, it doesn't seem to be as straight forward here...

So, how do I calculate the coordinates of this point correctly?

5
Graphics / Setting the origin of sf::ConvexShapes. [SOLVED]
« on: September 27, 2020, 03:07:49 pm »
Gee I didn't think I'd be back here this soon, but this time it's a simple problem:

I'm trying to make Boids, and I wrote this class for a single boid:

Boid.h:
class Boid {
public:
        sf::ConvexShape shape;
        Boid(sf::Vector2f head) {// The 'head' parameter stores the coordinates of the "head" of the boid, with other two points being relative to the head.
                shape.setPointCount(3);
                shape.setPoint(0, head);
                shape.setPoint(1, sf::Vector2f(head.x - 5, head.y + 20));
                shape.setPoint(2, sf::Vector2f(head.x + 5, head.y + 20));
                shape.setFillColor(sf::Color::Blue);
        }
};
 
And then wrote this code in the main file to generate a 100 boids with random positions within the confines of the window (with 'w' being the name of the sf::RenderWindow):

main.cpp:
std::vector<Boid>boids;
        for (int i = 0; i < 99; i++) {
                float spawn_x = rand() % w.getSize().x;
                float spawn_y = rand() % w.getSize().y;
                float angle = rand() % 360;
                boids.push_back(Boid({ spawn_x,spawn_y }));
        }
 

The result:


Ok, so far so good, but then I tried getting all the boids to rotate, and after messing around with for loops for a bit, I realized they were rotating around the top left corner, meaning that all of their origins were set to the point(0,0), so I kept that in mind and trying to set the origin of each boid to it's center modifying the code to make each boid have a random rotation when spawned:

The code now looked like this:

Boid.h:
class Boid {
public:
        sf::ConvexShape shape;
        Boid(sf::Vector2f head, float angle) {// Added the 'angle' parameter which stores the rotation of the boid.
                shape.setPointCount(3);
                shape.setPoint(0, head);
                shape.setPoint(1, sf::Vector2f(head.x - 5, head.y + 20));
                shape.setPoint(2, sf::Vector2f(head.x + 5, head.y + 20));
                //shape.setOrigin(sf::Vector2f(shape.getPoint(0).x, shape.getPoint(0).y + 10));
                shape.setOrigin(sf::Vector2f(head.x, head.y + 10));
                shape.setRotation(angle);
                shape.setFillColor(sf::Color::Blue);
        }
};
 

main.cpp:
std::vector<Boid>boids;
        for (int i = 0; i < 99; i++) {
                float spawn_x = rand() % w.getSize().x;
                float spawn_y = rand() % w.getSize().y;
                float angle = rand() % 360;
                boids.push_back(Boid({ spawn_x,spawn_y }, angle));
        }
 

The result:


From that weird blue quadrant in the corner, you can tell the boids do have different rotations, and they do have their origins in their centers(I tried changing the origins to the positions of the heads of each boid, and the quadrant became larger, so this part is definitely working correctly), but why are they all positioned at the point(0,0)?
And why don't convex shapes have "local" origins like other drawables such as Sprites and RectangleShapes?

6
Graphics / glFlush() error. [SOLVED]
« on: September 18, 2020, 10:39:01 pm »
This error comes when I'm working with textures sometimes and I don't know exactly what causes it, even weirder is that when i pops up there are no apparent issues, and everything seems to work just fine, but I want to know what causes it, and how I can avoid it:

Since I don't know exactly what causes the error I didn't post any code, but I'll do so if needed.


7
General / Getting things to happen once inside the game loop.
« on: August 05, 2020, 02:59:16 am »
I have two problems that are loosely related and I didn't want to create two separate threads into separate sub-forums:

The first is that I have a number of buttons, when the left mouse button is clicked while the cursor is inside a button's hitbox, the scene is changed depending on which button is clicked:

for (int j = init; j < limit; j++) {
                collision = buttons[j].button_collision();

                if (sf::Mouse::isButtonPressed(sf::Mouse::Button::Left) == true) {
                //Button logic goes here.
                }
                       
}
 

The problem is, when the mouse button is pressed, and the scene changes, in the fraction of a second that I'm still holding the mouse button down and the cursor happens to be on another button in the new scene, that button will be clicked and the scene will change again quickly even if that's not what I want.

So I tried changing the code to the following:

for (int j = init; j < limit; j++) {
                collision = buttons[j].button_collision();

                        if (e.type == sf::Event::MouseButtonReleased) {
                                if (e.mouseButton.button == sf::Mouse::Left) {
                                //Button logic goes here.
                                }
                         }
}
 

Now, the scene only changes when the button is clicked then released, but, for fraction of a second, if the cursor happens to be hovering on a button when the scene changes, that button will also be "clicked" even though the mouse button is released when that happens.

It's weird, the code does something completely different than the previous one, but I still end up with what is essentially the same problem.

Now that leaves with the second problem, which is a simple one:

for (int j = init; j < limit; j++) {
                collision = buttons[j].button_collision();

                if (collision == true) {
                        m_hover.play(); //'m_hover' is a blip sfx that is supposed to play when the cursor hovers on a button.
                }

                if (sf::Mouse::isButtonPressed(sf::Mouse::Button::Left) == true) {
                //Button logic goes here.
                }
                       
}
 

But since this code is included in the game loop, the sound effect ends up playing every single frame.


So my two problems are essentially the same: Executing an instruction only on the first frame in which collision=true, and not executing it again until collision=false.

How do I register a single mouse press (Or a click preferably if possible, meaning a press then a release)?
And how do I get a sound effect to play only the first time it's condition is met, until it returns to be false again?

8
Graphics / [SOLVED] Text Size And Origin
« on: July 11, 2020, 08:57:59 pm »
I'm trying to display a text prompt and a RectangleShape as a behind it that has the same exact size as the aforementioned text,to act as a bouding box,and I came up with a simple formula to do it:
int width = (text.getCharacterSize() * str.size()) + (text.getLetterSpacing() * (str.size() - 1));
With 'str' being the string containing the text displayed (In this case the words "LEVEL SELECT").
And the Rectangle's height simply being the CharacterSize.

And it...sort of works...


As you can see the box isn't quite the size of the text, and I don't know what the right way to do that would be...

My second problem is the text origin, I don't quite understand how that works, and I want to set the origin of the text to always be in the center,so that if I set the texts position to be the middle of the screen, the text will always be centered regardless of it's length.

9
Hello,I've been working on this simple shoot 'em up for a while,and I've finished most of the main gameplay loop now,I have a looping background,a character that you can move and shoot with,enemies to shoot,a working healthbar,dying/respawning mechanic...

Objects like enemies,player,and background already have their own respective header and source files,where I declared their classes and defined their related methods,but all of the objects and their related variables are declared in the main.cpp file,along with most of the game logic, like, collision, dying/respwaning, shooting, spawning/destroying enemies and projectiles,etc...

And now I want to work on other aspects of the game,such as adding other scenes (Main menu,level selection),so what I want to do is move  all of the objects I've declared into a header file (Let's call it "Game.h" ) and move all of the game loop logic into a method that I can call inside the main loop (Declared inside a class in "Game.h" and defined in "Game.cpp" ) ,but I kept getting strange errors when I tried to compile,such as a linker error in main.obj telling me that the "objects are already defined in game.obj ",even though I already commented them out in main.cpp (Note here that I only moved the objects,not what's inside the game loop),among other things,everytime I try something that I think would work I end up with another error.

TL;DR how do i move everything in the main.cpp file to another file without messing up the whole project? Preferably while also keeping the general structure of the main.cpp file intact: Event handling,game loop, and then displaying everything,which now that I think about is part of the game loop so I'm not sure...

10
Graphics / The right approach to movement and time. [SOLVED]
« on: June 14, 2020, 02:45:20 pm »
I'm making a simple shoot 'em up using SFML,applying what I'm learning from the "SFML Essentials" book,and in this book I learned that movement should be dependent on time rather than framerate,to insure a consistent experience across all different hardware.

Now,I have a general understanding of how both the Clock and Time classes work,but when it comes to applying them to enemy movement,I'm not sure exactly how to do that,and the book simply didn't linger on this issue long enough for me to understand,focusing more on animating sprites rather than moving them...

There are two was I thought of when it comes to approaching this,but I'm not sure of either of them:

1.I include all the move functions inside an if statement,so that the movement is only executed if the time elapsed between this frame and the one before it is above/below a certain threshold,but I don't know what the condition will be,as well as which unit of time to use:

sf::Time delta_time = clock.restart();

                /*Which one of these three do I use?*/
                float dt_elapsed = delta_time.asSeconds();
                float dt_elapsed = delta_time.asMilliseconds();
                float dt_elapsed = delta_time.asMicroseconds();

                if(/*What would be the condition here?*/){
                background.move(sf::Vector2f(0, 0.1 * dt_elapsed));
                enemy.move(sf::Vector2f(0, 0.15));
                }
 

This does work fine if I dt_seconds stores time in milliseconds,and the conditions is something like if (dt_elapsed >= 200) but the higher that number is the slower the movement becomes,and I don't know what the right value would be.

2.I multiply the chosen movement speed with the elapsed frame time,which is actually what's suggested in the book:

sf::Time delta_time = clock.restart();

                /*Which one of these three do I use?*/
                float dt_elapsed = delta_time.asSeconds();
                float dt_elapsed = delta_time.asMilliseconds();
                float dt_elapsed = delta_time.asMicroseconds();

               
                background.move(sf::Vector2f(0, 0.1 * dt_elapsed));
                enemy.move(sf::Vector2f(0, 0.15 * dt_elapsed));
 

Again,same problem,which unit of time do I use? Depending on whether I use seconds,milliseconds or microseconds the shape in question would either barely move or zoom across the screen (The book actually suggests I use seconds,but it doesn't linger over this specific issue for as long as I was hoping).
 
I could multiply the speed value with the elapsed time divided by a certain value x if I'm using millie/microseconds,or I could multiply the speed value directly with x,making it much bigger if I'm using seconds (We're talking values of somewhere between 150 and 200 instead 0.15 or 0.11,but is that well optimized?),but in each case I still don't know what x would be,and I don't want to just increase/decrease it until I get something that looks functional.

TL;DR How do I use the elapsed time between frames to obtain consistent movement speed across all different hardware,while making sure it's something well optimized?

11
Graphics / Vector elements keep changing textures. [SOLVED]
« on: June 07, 2020, 12:50:56 pm »
Hello,I'm trying to create a simple Shoot 'em Up game in SFML,and I created an entity class called "enemy" currently containing two members,a texture and a shape:

class enemy {
public:
        sf::Texture t;
        sf::RectangleShape s;

};
 

I created two objects from this class,one called "basic_enemy",and the other a vector called "enemies":

enemy basic_enemy;
std::vector<enemy> enemies;
 

I then wrote a function that randomly chooses between two sets of parameters to assign to the "basic_enemy" object,effectively choosing what type of enemy it will be (So far there are only two):

void enemy_spawner() {

        std::string texture_path;
        sf::Vector2f enemy_size;
        sf::Vector2f enemy_origin;


        switch (int r1 = rand() % 2) {
        case 0:
                texture_path = "res/images/enemy1.png";
                enemy_size.x = 70;
                enemy_size.y = 70;
                enemy_origin.x = 35;
                enemy_origin.y = 35; break;
        case 1:
                texture_path = "res/images/enemy2.png";
                enemy_size.x = 100;
                enemy_size.y = 100;
                enemy_origin.x = 50;
                enemy_origin.y = 50; break;

        }


        basic_enemy.t.loadFromFile(texture_path);
        basic_enemy.s.setSize(enemy_size);
        basic_enemy.s.setOrigin(enemy_origin);
        basic_enemy.s.setTexture(&basic_enemy.t);

        int spawn = rand() % 640 + enemy_origin.y;
        basic_enemy.s.setPosition(sf::Vector2f(spawn, -(enemy_origin.y)));

}
 

(I'm aware the name "enemy_spawner" is not an accurate description so I'll change it later)

Then I tried to set it up so that a new enemy type is chosen every two seconds,assigned to the "basic_enemy" object which will have it's content copied to the new element added to the "enemies" vector:

sf::Time elapsed_time;
        sf::Clock clock;
       

        while (w.isOpen()) {
                sf::Event e;
                while (w.pollEvent(e)) {
                }

               

                sf::Time delta_time = clock.restart();

                elapsed_time += delta_time;
                if (elapsed_time > sf::seconds(2)) {
                        for (int i = 0; i < 1; i++) {
                                enemy_spawner();
                                enemies.push_back(basic_enemy);
                                elapsed_time = clock.restart();
                                ii++;

                        }
                }
...
}
 

The issue here is that for some reason,everytime a new enemy type is chosen,the texture corresponding to that enemy is then re-assigned to all other existing enemies...
Here's an example of an enemy of the second type(with the "enemy2.png" texture):

And here's the same enemy roughly two seconds later,after an enemy of the first type(with the "enemy1.png" texture) was spawned:

At first I thought all of the elements of the vector were being rewritten every two seconds,but now I'm fairly certain that's not the case,because every enemy retains it's the size and x coordinates initially assigned to it for example,but still change textures for some reason...

Any idea what's causing this?

Pages: [1]