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

Pages: [1] 2 3 ... 15
1
Window / Re: Not clearing window
« on: March 25, 2023, 04:04:22 am »
The display is double buffered.
This means there are two buffers for your rendering: the front buffer and the back buffer. The front buffer is what is shown on the monitor. The back buffer is where drawing happens.
When the program calls the window's display() function, the two buffers are swapped.

If the window isn't cleared each frame, you have to make sure every pixel of the window is overwritten by something. For example drawing a full window background sprite, or a tile map, etc. What you are drawing on top of isn't the last frame rendered, but the second last frame, since the last frame is in the other buffer.
If pixels aren't overwritten, you'll get flickering anywhere the two buffers don't have the same pixels.

2
General / Re: Whats wrong with my collision detection?
« on: March 25, 2023, 03:04:15 am »
Let's look at the D key case:
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::D))
        {
            if (playerRight >= squareLeft)
            {
                player->move(5, 0);
            }
        }
The logic here is checking if the playerRight is to the right of the squareLeft. That could mean the player is to the left of the square but overlapping it, or the player is entirely to the right of the square and not overlapping. You don't want to move right if the player is to the left and overlapping.
Also there's no Y axis check in that one. It only considers the X positions, it doesn't check if they overlap on the Y.

To do a full overlap test you can do something like this:
bool overlap = (playerLeft <= squareRight) &&
               (playerRight >= squareLeft) &&
               (playerTop <= squareBottom) &&
               (playerBottom >= squareTop);
 
That will tell if the player and square are currently definitely overlapping.
Although what you really want is to tell if they are going to overlap after a move, not if they currently overlap.

So you could find the desired movement first (don't do the move, just work out what it would be based on the keys), calculate the left, right, top, bottom if the move happened, check if that was an overlap, and don't do the move if overlap is true.

The other way would be to do the move regardless, then correct it afterwards (push the player out of the square). That will make them sit flush a bit better, but it's trickier to implement.

Generally I prefer to just use Box2D and let it deal with everything. :)

3
General / Re: Failed to load image
« on: March 09, 2023, 08:41:29 am »
The most likely cause is that the path to the image doesn't fit with the working directory.
One annoyance with Visual Studio is that by default it sets the working directory to $(ProjectDir). This means any relative file access will be relative to where your project file is, not where the exe was built.
You can change that in the project properties, under Debugging / Working Directory. I set it to $(TargetDir), which is where the exe goes.

So the things to check:
- where is the exe being placed?
- where is the image you are trying to load?
- what string did you give SFML as the path to the image?

(This setting only has an effect when running from inside of Visual Studio. If you run the exe from explorer, the working directory is set to where the exe is)

4
C / Re: How sfClock_getElapsedTime() works ?
« on: March 09, 2023, 02:48:17 am »
I built CSFML myself (VS2022) and tried several of the code samples you gave. Everything worked. I left the not working two clock one run for an hour and it was consistently giving around 200 microseconds (mostly from printing to the console).

I see from the error message that you are using the Borland or Embarcadero 32 bit compiler. I wonder if that is part of the issue? SFML on windows uses QueryPerformanceCounter and does some 64 bit maths with it, maybe there's a problem there with bcc32?

5
C / Re: How sfClock_getElapsedTime() works ?
« on: March 08, 2023, 10:16:42 am »
The SFML and CSFML clocks works on integer microsecond precision (I don't know where that might be documented, I'm just digging through the source code). So for your first bit of code reading the elapsed time is too close to resetting the clock, it would have been quicker than 1 microsecond.

In the next part, the C language doesn't have operator overloading, so it's not possible to subtract sfTimes from each other, you need to get the numeric value out of the sfTime (using sfTime_asMicroseconds as you did further down).

The last one looks like the same as the first one, reading elapsed time less than a microsecond after making the clock.

6
Window / Re: sf::keyboard::key seems to be missing
« on: March 04, 2023, 02:12:09 pm »
The isKeyPressed parts and the window clear, draw and display should be outside of the event loop (the while loop that does pollEvent), but inside of the outer while loop.
Try this: (Basically I moved the closing brace of the while loop up to above the isKeyPressed bit)

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

int main()
{
        sf::RenderWindow window(sf::VideoMode(512, 512), "SFML Tutorial", sf::Style::Close | sf::Style::Resize);
        sf::RectangleShape player(sf::Vector2f(100.0f, 100.0f));
        player.setFillColor(sf::Color::Red);

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

                while (window.pollEvent(evnt))
                {
                        switch (evnt.type)
                        {
                        case sf::Event::Closed:
                                window.close();
                                break;
                        case sf::Event::Resized:
                                printf ("Width: %i, Height: %i\n", evnt.size.width, evnt.size.height);
                                break;
                        case sf::Event::TextEntered:
                                if (evnt.text.unicode < 128)
                                {
                                        printf("%c", evnt.text.unicode);
                                }
                        }
                }

                if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
                {
                        player.move(-0.1f, 0.0f);
                }

                if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
                {
                        player.move(0.1f, 0.0f);
                }

                if (sf::Keyboard::isKeyPressed(sf::Keyboard::W))
                {
                        player.move(0.0f, -0.1f);
                }

                if (sf::Keyboard::isKeyPressed(sf::Keyboard::S))
                {
                        player.move(0.0f, 0.1f);
                }

                window.clear();
                window.draw(player);
                window.display();
        }

        return 0;
}

7
Graphics / Re: Cant use Shape class in another class
« on: March 04, 2023, 02:03:19 pm »
Shape is part of the sf namespace, so you need to do:
class Ball : public sf::Shape {

It worked in main.cpp because of the using namespace sf; near the top.
"using namespace" only applies to the cpp file it is in, not others in the project.

8
Compiling doesn't load textures, the code has no idea what size they are until the program is run by the end user.
The limit is determined based on the properties of the graphics card at run time. Different cards can have different limits, older cards might be 2048x2048 or lower, newer ones (like my RTX2080TI) are 32768x32768.

9
General / Re: Smooth character movement with Arrow keys
« on: February 21, 2023, 11:36:34 pm »
There are two ways of checking the keyboard: event based (reading KeyPressed, KeyReleased or TextEntered events) or immediate (sf::Keyboard::isKeyPressed).
The immediate check goes outside of the event loop, otherwise it only gets called when some event comes in. In this case, the pause then repeat is caused by the TextEntered event, which is doing the standard OS behavior.

Making your code like this should fix it:
while (window.pollEvent(event))
{
        if (event.type == sf::Event::Closed)
                window.close();
}

if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) {
        rectangle.move(0, -0.5);
        y -= 0.05;
        cout << x << ", " << y << endl;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down)) {
        rectangle.move(0, 0.5);
        y += 0.05;
        cout << x << ", " << y << endl;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) {
        rectangle.move(-0.5, 0);
        x -= 0.05;
        cout << x << ", " << y << endl;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) {
        rectangle.move(0.5, 0);
        x += 0.05;
        cout << x << ", " << y << endl;
}
 

10
Window / Re: Getting error with linker - __imp_ files are missing.
« on: February 09, 2023, 09:04:22 am »
The functions mentioned in the errors are all from user32.lib, you need to add it to the linker input.

11
Graphics / Re: Texture.update doesn't work as expected
« on: February 07, 2023, 12:06:43 am »
sf::Mouse::getPosition() gives you the coordinates of the mouse on the monitor, with 0,0 in the top left corner. But if your window is smaller than the monitor and placed in the middle, it won't match.
So when you go to set a pixel, the coordinates are probably too large and outside of the window.

You can get the coordinates inside of the window by doing:
sf::Mouse::getPosition(window)

12
Graphics / Re: sf::Angle not working?
« on: January 22, 2023, 06:08:08 am »
Using a number like 180 directly doesn't let the angle class know if you are referring to degrees or radians. So it doesn't allow directly setting the value, you need to use one of the helper functions that build an angle for you:
sf::Angle angle = sf::degrees(180.0f);
sf::Angle angle2 = sf::radians(3.14f);

view.rotate(sf::degrees(180.0f));

13
General / Re: How can I make a background repeat infinitely?
« on: January 19, 2023, 02:41:01 pm »
Here's a little one I made.
Let's say the window and the background texture are 1920x1080.
It uses a single sprite of the background set to 3 times the width of the texture (so it repeats 3 times).
It then snaps the sprite to positions that are multiples of the width (1920). So as the camera pans to the side, the background will ocassionally jump 1920 pixels so it's always filling the window.

It can actually be done with a 2 time repeating texture, but handling -X positions becomes a little trickier, so I just used 3 repeats.

Some code fragments
// Make a sprite with the background texture like normal.
// Then set it like this:
tex->setRepeated(true);
sf::Vector2u texSize = tex->getSize();
sprite->setTextureRect(sf::IntRect(sf::Vector2i(0, 0), sf::Vector2i(texSize.x * 3, texSize.y)));

// Later in your rendering code do this:
sf::View view = window.getView();
sf::Vector2f pos = view.getCenter() - view.getSize() * 0.5f; // Find top left corner of the view
pos.x = int(pos.x / texSize.x) * int(texSize.x)-int(texSize.x); // Snap the X coord to multiples of width
sprite->setPosition(pos);
window.draw(*sprite);
 

Or if I was doing it for myself, I'd probably do a shader version.

14
Graphics / Re: Maximum Image size?
« on: January 16, 2023, 03:17:12 am »
I'm assuming you are doing a 32 bit build of the project.
SFML's Image class uses a Vector2u to store the resolution. This can theoretically handle 4 billion x 4 billion resolution. But it uses an std::vector of bytes to store the pixel data. In 32 bit projects, std::vector is limited to 4 billion (roughly) items. At 1 byte each, that's 4GB of ram.

A 30000x30000 image at 4 bytes per pixel is 3.4GB. That sounds ok, but 32bit apps have only 4GB total address space for everything, not just the image. Plus if you are on windows, 32 bit apps are only allowed to access 2GB normally, or a bit over 3GB if you enable the large address aware flag.

In a 64 bit project, I just made an 80000x80000 SFML Image and it was fine, which is 24GB of ram (I've got 32GB on my PC).

There's another issue though.
SFML uses STB Image (3rd party library by Sean Barrett) for image loading/saving. From what I can see, STB Image is made for 32bit. For example its PNG saving code returns the total size in memory of the data to save as a signed int, so it would be limited to 2GB size before things go wrong. It also has to allocate more ram as part of the saving on top of the already large amount in SFML.
I can get a PNG to save with 20000x20000. At 25000x25000 and above it fails with an error. At 40000x40000 and above it crashes the program.

So... doing a 64bit build will fix the image class. But you'll need to add alternative saving code to handle those sizes.

15
General discussions / Re: Including OpenCV
« on: December 30, 2022, 12:06:15 pm »
I recently added OpenCV to an SFML project to do video processing (working on a motion detection thing for security camera footage).

From a code point of view the setup was basically (this is for visual studio):
#include "opencv2/opencv.hpp"

#ifdef _DEBUG
        #pragma comment(lib, "opencv_world460d.lib")
#else
        #pragma comment(lib, "opencv_world460.lib")
#endif
 

The usual stuff needs to be set up as with any library: includes path, library path and dlls next to the project exe.

Of course OpenCV's images aren't directly compatible with SFML, you'll need to convert them if you need images. So far the only easy way I've found is manually looping over each pixel and converting them.

Pages: [1] 2 3 ... 15
anything