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

Pages: [1]
1
System / Vector2 - type safety between Point and Vector (dimension)
« on: March 10, 2024, 09:31:46 am »
I have recently used Vector2 for several functions, in some cases as Point and in some cases as Vector (dimension).
I have found that it is not the best especially if function parameters need both, point/s and vector/s.
I have created class Line which just has two points and an enum for line type (straight, ray, segment).
It was much better and safe however, it needs to create a line and sometimes you have just a point and vector or two points.

So another step is to split general sf::Vector2 into two types:
- Point2
- Vec2
And define operations over them. I found it as really big advantage as it can forbid meaningless operations.
Point2f pointA {1.f,1.f};
Point2f pointB {2.f,2.f};
Vec2f vecA {3.f, 0.f};

auto bLessA = pointB - pointA; // bLessA is Vec2f because operand- for two points are not defined
auto pointC = bLessA + vecA; // Error as there is no overloaded operand+ for two points
auto aAddVecA = pointA + vecA; // type is Point2f

This allows me to define function parameters much more safely (used C++20):

template <typename T>
concept arithmetic = std::integral<T> || std::floating_point<T>; // more strict definition to general definition

template <arithmetic T>
auto linesIntersection (const Point<T>& pointA, const Vec<T>& vecA, const Point<T> pointP, const Vec<T>& vecP)
-> std::optional<Point<T>>;

template <arithmetic T>
auto linesIntersection (const Point<T>& pointA, const Point<T>& pointB, const Point<T>& pointP, const Point<T>& pointQ)
-> std::optional<Point<T>>;
 

Would not be useful that type safety even for SFML?

2
Graphics / setFrameLimit - curious effect
« on: February 14, 2024, 05:50:43 pm »
I am just curious as it is nothing really important.

I have just measured in average on function that generate Voronoi diagram which is inside the main sfml event loop.

auto vorStartTime = std::chrono::steady_clock::now();
            voronoi.generate();
auto vorEndTime = std::chrono::steady_clock::now();
 

Then I print each second the average for this function. Each iteration I add vorEndTime - vorStartTime to the overall time and then divide by a number of loops for that time. Than just checking if elapsed at least 1 second from the last loop and if so print the average.

But what is really strange that it gives me 0.3 - 0.6 ms with setFrameLimit to 60.
But if I switch off any FrameLimit, i get 0.14 - 0.16 ms.

The function
voronoi.generate() is using only sf::Vector2f from the whole SFML library for points.
 


I am just curious how it can be possible that setting frame limit has effect on function with has practically nothing to do with SFML. Or it can be possible that setting the frame limit makes the processor more idle so that the max turbo frequency of processor is not used (intel i7-1265u 1.8 MHz)

3
Graphics / sf::Rect
« on: February 04, 2024, 09:12:06 am »
I have looked at the implementation of the Rectangle class and I was surprised to find static_cast.

Why is static_cast there?

from SFML 3.0 branch
template <typename T>
constexpr bool Rect<T>::contains(const Vector2<T>& point) const
{
    // Not using &#39;std::min&#39; and &#39;std::max&#39; to avoid depending on &#39;<algorithm>&#39;
    const auto min = [](T a, T b) { return (a < b) ? a : b; };
    const auto max = [](T a, T b) { return (a < b) ? b : a; };

    // Rectangles with negative dimensions are allowed, so we must handle them correctly

    // Compute the real min and max of the rectangle on both axes
    const T minX = min(left, static_cast<T>(left + width));
    const T maxX = max(left, static_cast<T>(left + width));
    const T minY = min(top, static_cast<T>(top + height));
    const T maxY = max(top, static_cast<T>(top + height));

    return (point.x >= minX) && (point.x < maxX) && (point.y >= minY) && (point.y < maxY);
}
 

4
System / std::istream& operator >> for sf::String
« on: December 02, 2023, 11:52:54 am »
I overloaded operator >> for std::istream and sf::String to handle UTF8 encoding.
However I can see there are a lot of copies there:
- from stream to std::string (UTF8)
- from std::string (UTF8) to std::basic_string<sf::Uint32> using sfml function decode
- from std::basic_string<sf::Uint32> to sf::String

I need to use the middle step through std::basic_string<sf::Uint32> as there is no std::back_inserter for sf::String. Is there possibility to decrease number of steps.
note: SFML 2.6.1

Some ideas I have:
- making std::basic_string<sf::Uint32> static and add clear at the end of overload so there is no need to for memory allocation each time

std::istream& operator>> (std::istream& in, sf::String& string)
{
    std::string u8string;
    in >> u8string;
    auto begin = u8string.begin();
    auto end = u8string. end();
    std::basic_string<sf::Uint32> stringUint32;
    auto output = std::back_inserter(stringUint32);
    while (begin < end)
    {
        sf::Uint32 codepoint;
        begin = sf::Utf<8>::decode(begin, end, codepoint);
        *output++ = codepoint;
    }
    string = stringUint32;
    return in;
}
 

5
Graphics / Error compiling with gcc++
« on: August 05, 2023, 02:45:19 pm »
I use sf::CircleShape with SFML 2.6 and GCC 13.1.

So far till now I only build debug version and without errors. However for the first time I created release version and I get error. Do you have idea where can be problem?

`_ZThn8_N2sf11CircleShapeD1Ev' referenced in section `.rdata$_ZTVN2sf11CircleShapeE[_ZTVN2sf11CircleShapeE]' of C:\C++\_LIB\SFML-2.6.0\lib\libsfml-graphics-s.a(CircleShape.cpp.obj): defined in discarded section `.gnu.linkonce.t._ZN2sf11CircleShapeD1Ev[_ZThn8_N2sf11CircleShapeD1Ev]' of obj\Release\Voronoi\Voronoi.o (symbol from plugin)
collect2.exe: error: ld returned 1 exit status


info:
[ 33.3%] gcc.exe -std=c++20 -m64 -Wsign-conversion -std=c++23 -DSFML_STATIC -DTGUI_STATIC -fomit-frame-pointer -fexpensive-optimizations -flto -O3 -pedantic -Wextra -Wall -std=c++20 -m64 -DSFML_STATIC -IC:\C++\_LIB\SFML-2.6.0\include -IC:\C++\_LIB\TGUI-1.0\include -Iscr -Iscr\Voronoi -ITest -IVoronoi -c C:\C++\TEST\TEST_Voronoi_1\main.cpp -o obj\Release\main.o
[ 66.7%] gcc.exe -std=c++20 -m64 -Wsign-conversion -std=c++23 -DSFML_STATIC -DTGUI_STATIC -fomit-frame-pointer -fexpensive-optimizations -flto -O3 -pedantic -Wextra -Wall -std=c++20 -m64 -DSFML_STATIC -IC:\C++\_LIB\SFML-2.6.0\include -IC:\C++\_LIB\TGUI-1.0\include -Iscr -Iscr\Voronoi -ITest -IVoronoi -c C:\C++\TEST\TEST_Voronoi_1\Voronoi\Voronoi.cpp -o obj\Release\Voronoi\Voronoi.o
[100.0%] g++.exe -LC:\C++\_LIB\SFML-2.6.0\lib -LC:\C++\_LIB\TGUI-1.0\lib -o bin\Release\Voronoi.exe obj\Release\main.o obj\Release\Voronoi\Voronoi.o  -static-libstdc++ -static-libgcc -static -m64 -O3 -flto -s -static-libstdc++ -static-libgcc -static -m64 -lsfml-graphics-s -lsfml-window-s -lsfml-audio-s -lsfml-network-s -lsfml-system-s -lfreetype -lglu32 -lopengl32 -lopenal32 -logg -lflac -lvorbis -lvorbisfile -lvorbisenc -lgdi32 -lwinmm -lws2_32   -mwindows

note: TGUI is not needed now but I will use it later.

6
Graphics / SFML 3.0 future proof
« on: July 11, 2023, 02:10:16 pm »
I am preparing just a small project that will create Voronoi map from a set of points and create borderlines that can user define by layers (eg. gradients) and the owner of the "cell". After everything I am thinking of creating some noise so that borders will be not just lines from one Voronoi point to another.

I have already done the first part - the Voronoi diagram.



Right now there is the second part, transferring all this information to each cell, to each border (of the cell), and to each border line which can consist of several cells. Because borderline can have lots of points (after some noise is applied) and will not be changed often I think about using sf::VertexBuffer which will contain all border lines and will be drawn just one draw call.

However I would like it to do it as much as possible to close the prepared SFML 3.0 so that there will need no large refactoring. I know that there will be no quads in SFML 3.0 if I read it correctly, so any layers of borderline should be just triangles. Is it right?

7
Graphics / sf::Tranformable setOrigin
« on: May 27, 2023, 08:31:48 pm »
We have 2 overloads of sf::setOrigin:

setOrigin (float x, float y)
setOrigin (const Vector2f& origin)
 

There is another useful overload for getting origin to the center of the shape as it is quite useful as a lot of transformation has sence about center of the entity. For that it would be useful simple function without parameter. Or it could be through enum but it seems to me that function is used quite a lot and it would be really useful having it directly in the class.
setOrigin()
 

SFML has several types of entities:
CircleShape - it is easy by the radius
RectangleShape - it is easy by the size
ConvexShape - average of points
Sprite - from rectangle
Text - from its bounds

8
Graphics / Black line is not black
« on: May 27, 2023, 05:46:22 pm »
I am doing axis of graph by simple using sf::Vertex array.
const sf::Color         AXIS_COLOR          (sf::Color::Black);
   
sf::Vertex              axis[] =
{
    sf::Vertex          (sf::Vector2f       (0.f, GRAPH_SIZE/2), AXIS_COLOR),
    sf::Vertex          (sf::Vector2f       (GRAPH_SIZE, GRAPH_SIZE/2), AXIS_COLOR),
    sf::Vertex          (sf::Vector2f       (GRAPH_SIZE/2, 0.f), AXIS_COLOR),
    sf::Vertex          (sf::Vector2f       (GRAPH_SIZE/2, GRAPH_SIZE), AXIS_COLOR),
};

// and draw it

window.draw (axis, 4, sf::Lines);
 

I am using antialiasing:

sf::ContextSettings settings;
settings.antialiasingLevel = 8;
sf::RenderWindow            window           ({SCREEN_WIDTH,SCREEN_HEIGHT},APPLICATION_NAME, WINDOW_STYLE, settings);
 

However the axis is practically gray and if I set antialiasing to 0, I have a black line.
Is this because of basic coordination use floats?

Is there a way to have both, really black and antialiasing?

9
Window / Clock in sf:Window
« on: May 17, 2023, 07:56:03 pm »
I can see that sf::Window has member sf::Clock and it is used for FrameLimit.

Why there is no public function to provide time between frames if sf::Window has this information?

10
Graphics / sf::VertexBuffer
« on: May 10, 2023, 05:02:55 pm »
I have found that there is the possibility to use VertexBuffer which is stored in the memory of a graphical card.

I have an idea that it can be used eg. for borders in 4x game which is pretty static and updated once per huge time.

Is it in that case not better to have split game entities into static ones and moveable ones and store static ones in sf::VertexBuffer, especially the ones with a larger number of vertices?

11
Window / FPS
« on: April 10, 2023, 09:05:58 am »
In my previous test (see https://en.sfml-dev.org/forums/index.php?topic=28998.0 I add simple fps counter:

        stepTime = std::chrono::steady_clock::now();
        duration = stepTime - startTime;
        startTime = stepTime;
        durationSec = duration.count();
        ++frames;
        fpsDuration += duration;
        if (fpsDuration > fpsUpdate)
        {
            fpsText.setString(std::to_string(static_cast<int>(frames/fpsDuration.count())));
            fpsDuration = std::chrono::duration<long double>::zero();
            frames = 0;
        }
 

And this is setting of window:
sf::ContextSettings settings;
settings.antialiasingLevel = 2;
sf::RenderWindow window{{static_cast<int>(width),static_cast<int>(height)}, "SFLM app", sf::Style::Fullscreen, settings };
 

I get following FPS for these setting using release version with gcc
1. 1400-1500 fps/s
800x600 - antialiasing 2
sf::Style::Default
2. 260-280 fps/s
1920x1080 - antialiasing 2
sf::Style::Default
3. 400-420 fps/s
1920x1080 - antialiasing 0
sf::Style::Default
4. 190-200 fps/s
1920x1080 - antialiasing 8
sf::Style::Default

Is it normal to have such drop of FPS between these resolutions? I am little worried that 200 FPS means 5 ms per frame, with 60 FPS has max 16 ms per second, just this simple rendering is taking 1/3 of max. time available.
As right now application does almost nothing (see link), what would be FPS with scalling resolution up in larger monitors switching antialiasing on?

note: Intel(R) Core(TM) i5-8265U CPU @ 1.60GHz   1.80 GHz, Intel(R) UHD Graphics 620

12
Graphics / View zoom and text
« on: April 05, 2023, 10:36:08 pm »
I would like to create world map which has a lot of labels. This map can be rotated, moved and zoomed and for that view is perfect. However these labels should not be zoomed.

What can be best solution with SFML?

I am thinking to have the second view for these labels only. However it means, that view cannot use zoom. That zoom must be transferred to move relating to the position of center of the view and applied individually to the texts and view applied on the text can use same moving as world map view and rotation skipped.

Another idea is to use transform matrix on position of object with world map view to get pixel on screen and than render text on that position.

Or is there another better way to do it?


13
Graphics / VertexArray and TriangleFans
« on: August 01, 2022, 10:59:27 pm »
I am trying to do fireworks.
My idea is to draw circle shapes even on tails. However this means just too many shapes and too many drawing calls that sometimes fps drops just too low.

So I was thinking using vertex arrays and triangle fans so using 1 call will draw me firework with the whole tail.
note: I do not want to use lines or rectangles as I cannot get gradient I want.

Is it possible to use vertex array (or own container as deque or vector) for triangle fans and if so how it can  be done, how can I tell how many points is single triangle fan and how many it should draw from the array.

So far it seems to me that it is possible only for same primitives as points, lines and triangles or is there a way to achive it with more complex primitives?

Pages: [1]