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

Pages: [1] 2 3 ... 224
1
As an example of collision, you could take a look at:
https://github.com/SFML/SFML/wiki/Source%3A-Rectangular-Boundary-Collision

If you feel that it doesn't already do for you the collision that you need, it at least shows an example of how to get the points of a rectangle (using local points and its transform) as well as using an inverse transform to convert into local co-ordinates of a different shape.

2
Feature requests / Re: export vertexes from the Shape class
« on: March 27, 2024, 03:15:22 pm »
You can get each of the vertices by simply getting the shape's points:
for (std::size_t i{ 0u }; i < shape.getPointCount() ; ++i)
    std::cout << "Point " << i << ": (" << shape.getPoint(i).x << ", " << shape.getPoint(i).y << ")" << std::endl;

However, this is likely not how you would build the shape to draw (other than using its original primitive type).
Much more likely is you'd like to be able to build that shape with separated triangles (as any shape can be built this way) as you can then batch them together using that same primitive.
So, you could extract the triangles from the original shapes and then draw them as triangles. Since everything is just triangles, you can also batch them together if required.
To extract the triangles from a shape, you could use this:
https://github.com/SFML/SFML/wiki/Source%3A-Triangles-Extractor

Note that best results are observed if shapes don't change often and/or there are very many (to reduce those significant draw calls) and they are causing an speed issue.

3
Graphics / Re: Draw pixels in real-time
« on: March 17, 2024, 06:14:51 pm »
An sf::Image is basically just an array of pixels in standard RAM.
Updating (or loading) the texture from an image copies this memory (either fully or in part) to the graphics card memory (the 'image' of a texture is stored on graphics card memory). This is then used by the graphics card to when drawing the sprite (in this case). Updating the texture from the image is the heaviest part of this operation but - depending on how much you may be processing pixels - CPU image processing may also be heavy.

An sf::RenderTexture is similar to an sf::Texture in that it already resides in graphics card memory and can be used by the graphics card for both drawing to (when changing it) and drawing from (when drawing the sprite in this case).
So, in theory, this is lightest process since you don't need to transfer anything (much) from CPU to GPU to be able to draw the render texture. However, anything you do change does need to be sent to the graphics card so if that is every pixel every time, this could be even heavier as each vertex also has extra information (i.e. a position and a texture co-ordinate) that wouldn't change.
If you are only changing (modifying pixel colours) 'some' pixels or are 'moving' (changing their positions rather than them having static pixel positions) them, this could be more efficient.
Also, this method also allows you to let the graphics card do some processing. e.g. you can use vertex or geometry shaders to modify the 'pixels' (vertices) by just giving parameters.

Note that you do something similar using fragment shaders and an image/texture.



In conclusion:

If you are modifying every pixel every time (whether it needs modifying or not), using an sf::Image is likely the simplest and possibly fastest method. Use this for video input, for example.

If you are only modifying a few pixels and/or not modifying them often, a vertex array with a render texture could be more efficient.

Note that an sf::VertexBuffer can also add additional features for this but may not be more efficient if transferring a lot of information.

4
Graphics / Re: Optimization of hexagon-grid
« on: March 12, 2024, 09:41:18 pm »
You should note that setting frame rate limit only caps the highest possible rate so it won't go above that rate. It can't force a slow rate to be quicker, for example.
If you are also using v-sync, it could be limiting to your display's refresh rate. It's generally recommended to only use v-sync or a frame rate limit, not both.

5
You are randomly changing their positions, sizes and shapes on every frame so they will continue to move.

Loop through them all and set all of their positions before the main window loop: while (window.isOpen())

Then, loop through them all and draw (and only draw) them inside the main window loop. (between window.clear and window.display as you have already been doing)

6
Graphics / Re: Perspective distortion issue [Solved]
« on: March 06, 2024, 05:55:04 pm »
Just in case you hadn't seen the cause of the issue, it's because - in 2D - both of the triangles have no idea about the other triangles so one doesn't realise it's supposed to be a part of a pair to make a quad (and then have to share some of its texture with another one).

Waiting for Hapax to mention Elastic Sprite that "fixes" exactly this issue ;)

https://github.com/Hapaxia/SelbaWard/wiki/Elastic-Sprite
Haha! It exists because it solves the issue! :)

Elastic Sprite is entirely 2D and automatically 'solves' perspective (in the same way apps like Photoshop create perspective when given 2D corners).
It's also therefore compatible with SFML's 2D vertices (since z cannot be used with those).

I have long considered a more involved solution allowing multiple faces but if that much 3D is needed, it's likely that going raw OpenGL/Vulcan is the better option. Maybe in the future... ;D

7
Graphics / Re: LineStrip VertexArray and ConvexShape problem
« on: March 06, 2024, 05:41:31 pm »
Glad you got what you need working and thank you for the suggestions.

Selba Ward's Polygon has already been recently upgraded to allow either direction as well as multiple other features. However, I would strongly suggest against automatically calculating the direction of the vertices as this would entail processing every edge/angle before even beginning and this can be extremely time-consuming as larger shapes are involved. Personally, I think that insisting that the provider of the vertices knows in which direction they are in is preferred over unnecessary work during the triangulation. Of course, if the direction needs to be calculated 'automatically', it could be done 'before' triangulation if the provider is unable to know the direction. This, at least, can be done just once if they keep consistent.

Providing control points instead of actual points is adding another step. Again, one that can be skipped without the ability but, since Selba Ward's Spline already does that work, Polygon doesn't need it as well as it can simply take the actual points exported from Spline.

You've done a lot of work in this area and probably learnt a lot, which is admirable! I must admit, though, that considering you only had a single specific problem and that problem was already provided for, it seems like you got caught in a rabbit hole (I do that too!). I suppose, then, I'm asking if you got your actual initial problem solved (whether using Selba Ward, your own triangulator, or a simple vertex array as I initially suggested) and can move on with that project?

8
General / Re: Can't change shape's attributes outside of int main()
« on: February 15, 2024, 02:13:28 am »
Yes, using the example I posted in my most recent reply, you can see that you can pass the object to the function, modify it, and then when you return to main, it will be changed.

Note - VERY IMPORTANT - that you must pass by reference* to be able to modify it. If you pass it by value, it will make a copy, modify the copy and then the copy is lost when the function ends.

* if you don't know what this means, it's part of C++ and means that you can act directly upon the actual passed object rather than a copy. That's what the "&" is for next to the parameter type in my example. See https://en.cppreference.com/w/cpp/language/reference for more information of references.

9
General / Re: Can't change shape's attributes outside of int main()
« on: February 14, 2024, 11:48:16 pm »
That piece of code is not being called by anything; it's not enclosed.

When the app is started, the first code to be executed is main().
At no point can main call this command.

It brings me to my first example: avoid using objects globally (as is your sf::CircleShape).
However, "global code" is impossible. When should it be executed?


If you want to call a separate piece of code, you can create a function:
#include <SFML/Graphics.hpp>

void setCircleFillColor(sf::CircleShape& circle)
{
    circle.setFillColor(sf::Color(150, 150, 150));
}

int main()
{
    sf::CircleShape shape(200);
    shape.setPosition(sf::Vector2f(5, 5));
    setCircleFillColor(shape);
}
Note that the code is enclosed in a function and that function is called from main.

Another option is to enclose it in a class. Note that the class would need to be instantiated (an objected created of it) and that would be within main (or one of its functions).

10
Graphics / Re: setFrameLimit - curious effect
« on: February 14, 2024, 08:43:23 pm »
setFramerate() sleeps the thread.
This helps with "over-producing" with no limit and can reduce power used. However, it's not strictly set to that rate.
Once a thread is "slept", it has the possibility of "over-sleeping" as there's no real way of insisting on coming back by a specific time (accurately); this is dependent on operating system's task scheduler.

So, I'd guess that the framerate limit is sleeping during each frame and then the operating system is keeping it a little longer than your hoped-for length of time before returning it.

I would expect it to be faster (shorter frame time) without a framerate limit.

11
General / Re: Can't change shape's attributes outside of int main()
« on: February 14, 2024, 08:35:02 pm »
I think I was slightly confused by your mention of it working only in main.

Could you provide a small example* code of what you mean?
*A complete and minimal example would be great if possible.

12
Graphics / Re: LineStrip VertexArray and ConvexShape problem
« on: February 14, 2024, 04:32:55 pm »
I'm glad you've at least tried Selba Ward; it is there for people to use!

I mentioned Spline only as a simple and easy way to draw a polygon's outline. Simply export Polygon's positions and set all Spline's positions from that export. Remember to close the Spline. You can also use a thickness for Spline so the outline is no longer infinitely thin (and drawn as a pixel thickness). You can also round the corners. Note that Spline creates the entire polyline as a single object and draws it with a single draw call whereas drawing multiple sf::RectangleShapes (one for each edge) is one draw per edge. FYI, Selba Ward's Line can also draw a simple line with or without thickness and also used to use sf::RectangleShape internally but was replaced by custom vertex array. Still, Spline's better ;)

Another thing to note is that if anything in Selba Ward isn't generic enough to be useful in multiple scenarios, I'd like to consider updates and improvements, and if anything is completely missing from Selba Ward, I consider adding it.
Selba Ward is still improving and Polygon is actually pretty new so somethings are yet to come.

My responses were to help with the creation of the object that you were having trouble with. Hope you at least got that shape made! Note that I didn't mention Selba Ward at first and tried to explain how to create the object yourself. :)

My point about any complex being able to be represented by triangles still stands. However, this is separate from being able to triangulate from a given set of points. Does the "butterfly shape" have holes? Polygon can handle holes but must be informed where they are; hole vertices must also be in opposite (clockwise) direction. In addition, the triangulation method must be set to "EarClip" rather than the more simple "BasicEarClip" to be able to handle the holes. Note that Polygon can only handle "simple polygons"; that is, a polygon that never crosses itself. Holes are technically not part of a "simple polygon" but Polygon converts it into a simple polygon first (as a bonus!).

If you could provide the vertices for the "butterfly shape" and an image of what it should look like, I'd like to do some tests.

It's worth noting that Polygon is designed to be a way to provide a filled polygonal area from a set of points representing its boundary. It's not, however, designed to have visual effects that manipulate those points (such as corner rounding). Those effects should be done separately and given to Polygon to create the shape. In fact, Spline can do this; it can handle Bezier curves and can export its interpolated position so a curved boundary could be created in Spline and then passed to Polygon to create the shape.
It was considered to have a base for both Spline and Polygon as they have similar concepts but decided against so that each Selba Ward drawable can be used independently.

Reading what you wrote about the "butterfly shape" again, I realise that you are creating a polygon where edges cross. This is not a simple polygon and cannot be handled automatically by Polygon. It breaks the rule that vertices must be passed in the same direction. The "butterfly" has vertices in both clockwise and anti-clockwise directions.
Probably better to not have allowed a polygon definition to do this than to try to fix it when the mistake is made.
This could be considered "ill-formed" as it has a "twisted" and not flat face. In fact, if that shape (like a bow-tie, I'm imagining) is required, it can be created; the point in the centre should be included since it exists. It would basically be 6 vertices (5 different ones) which would be the rectangle (in anti-clockwise direction) with the centre vertex between the two vertices of each opposing edge (either top and bottom or left and right).
It could be thought of this way: why is a shape with 2 triangles trying be defined without giving one of the points of each triangle?

Pre-processing can "fix" this. It can already be done before giving the points to Polygon (by specifying them correctly) but features that check and/or adjust for this could be included in Polygon at a later time.

As an aside, Polygon should, in the future, also be able to use monotone triangulation (uses scanline).

I hope I've addressed your questions and helped with your issues. If I missed anything, feel free to let me know.
Also, I'm open to ideas for Polygon and also, more generally, Selba Ward.


EDIT (2024/02/15):
Selba Ward's Polygon can now (as of v1.4), in fact, also work points that are in a clockwise order!
And more!
For more info, see this: https://github.com/Hapaxia/SelbaWard/pull/46

13
General / Re: Can't change shape's attributes outside of int main()
« on: February 14, 2024, 01:00:07 am »
Glad you got it to work!  :)

First thing to note is that SFML objects shouldn't be global. It's not clear whether you mean this or not.
e.g. this is BAD:
sf::RenderWindow window;
int main()
{
}
The objects should be created inside the main function:
int main()
{
    sf::RenderWindow window;
}
It's still okay to create an object elsewhere as long as it still called by main:
sf::RenderWindow* createRenderWindow()
{
    sf::RenderWindow* pWindow = new sf::RenderWindow;
    return pWindow;
}
int main()
{
    sf::RenderWindow* pWindow = createRenderWindow();
}
Note that a pointer was used here for simplicity but this is not the safest way to do it. Use a smart pointer if you do it this way.

It's usually better to create them and then pass them:
void createRenderWindow(sf::RenderWindow& window)
{
    window.create(sf::VideoMode(960u, 540u), "");
}
int main()
{
    sf::RenderWindow window;
    createRenderWindow(window);
}

You don't need to pass objects back if you're using them temporarily:
void loadAFontButThenThrowItAway()
{
    sf::Font font;
    font.loadFromFile("fonts/arial.ttf"); // I am not checking whether it loads or not for simplicity and since it does not really matter here but you should always do that!
}
int main()
{
    loadAFontButThenThrowItAway();
}


With all that said, another thing to consider is that SFML needs to be included before using an object so, if you're doing something in a separate file, you'll likely need to include SFML headers from that file too.

14
General / Re: Can't static link SFML
« on: February 13, 2024, 06:32:14 pm »
Hello
Hello! :)

linking the SFML library statically in Visual Studio 2022
I downloaded the WinLibs MSVCRT 13.1.0
The WinLibs are for the MinGW compiler. Since you're using the latest version of Visual Studio, those are not needed and the download 'does not need to match 100%'.
With the rest of the information of your configuration, can we presume that you downloaded this one: "Visual C++ 17 (2022) - 32-bit"?

In addition, don't forget to define the SFML_STATIC macro in Preprocessor Definitions (when building statically).

15
Graphics / Re: LineStrip VertexArray and ConvexShape problem
« on: February 13, 2024, 06:21:38 pm »
Polygon requires anti-clockwise due to the calculations testing angles based on inside/outside. It could be adjusted to work with clockwise by swapping angles and sides but the ability to do both is extra calculations for both ways. However, Polygon can reverse them for you so you can provide them as clockwise and reverse them.
Maybe a future update would have the ability to do both by traversing the points in both directions.
Polygon's next update will have colour and texture; that's already ready but I'm looking into other methods and mesh refinement as well as other improvements.
An outline was considered for polygon but since it exports its positions, this can be generated super-easy, especially with Spline (also Selba Ward), which takes those exported positions directly.

With your implementation, remember that resizing the window doesn't automatically resize the view. Maybe that is involved.
https://www.sfml-dev.org/tutorials/2.6/graphics-view.php#showing-more-when-the-window-is-resized

I can't really say why your tone is shifting. I wonder if it may be because it's on a different part of the display and the angle of viewing is different...

And yes, every complex shape can be represented by triangles.
If you have any vertex array shape (not the points or lines primitives) then you can easily extract its triangles:
https://github.com/SFML/SFML/wiki/Source%3A-Triangles-Extractor

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