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

Pages: [1]
1
General / Re: Makefile undefined references
« on: May 23, 2022, 06:45:56 pm »
Thanks! I ended up using CMake and was so much easier  :) , here is the CMakeLists.txt that I made, if someone is having the same issue:

cmake_minimum_required(VERSION 3.22)
project(sorting-visualizer)

set(SFML_DIR "lib/SFML-2.5.1/lib/cmake/SFML")


find_package(SFML COMPONENTS graphics window REQUIRED)

add_executable(sorting-visualizer src/main.cpp src/Sortable.cpp src/SortAlgorithms.cpp src/SortController.cpp src/Utils.cpp)

target_link_libraries(sorting-visualizer sfml-graphics sfml-window)
 

2
General / Re: Makefile undefined references
« on: May 23, 2022, 05:52:04 pm »
I decided to use CMake instead, using CMake will be easier to port the project to other platforms

3
General / [SOLVED] Makefile undefined references
« on: May 23, 2022, 05:34:28 pm »
Hi, I saw that there already are Makefile threads, but I couldn't find the solution after hours of trying.

This is my project structure:

sorting-visualizer/
├─ lib/
│  ├─ SFML-2.5.1/
│  │  ├─ include/
│  │  ├─ lib/
├─ src/
│  ├─ [cpp and h fiiles...]
├─ Makefile

Im trying to build the project using Makefile, here is the output when I try to run make: https://pastebin.com/xa2QgC3E

And here is my Makefile:

# SFML
SFML_DIR=./lib/SFML-2.5.1

SFML_LIBS=-lsfml-graphics -lsfml-window -lsfml-system

# Steps
steps: compile clean

# Compile source files
main.o: ./src/main.cpp
        g++ -c ./src/main.cpp -I$(SFML_DIR)/include

Sortable.o: ./src/Sortable.cpp
        g++ -c ./src/Sortable.cpp -I$(SFML_DIR)/include

SortAlgorithms.o: ./src/SortAlgorithms.cpp
        g++ -c ./src/SortAlgorithms.cpp -I$(SFML_DIR)/include

SortController.o: ./src/SortController.cpp
        g++ -c ./src/SortController.cpp -I$(SFML_DIR)/include

Utils.o: ./src/Utils.cpp
        g++ -c ./src/Utils.cpp -I$(SFML_DIR)/include

# Compile binary
compile: main.o Sortable.o SortAlgorithms.o SortController.o Utils.o
        g++ *.o -o sorting-visualizer -L$(SFML_DIR)/lib $(LIBS)

        @echo
        @echo Done! Run ./sorting-visualizer to open

# Clean object files after building
clean:
        @rm *.o
 

Im using Arch Linux, should I use CMake instead? It would be great if the project could be built easily for Windows and macOS too, that's why I'm including the libraries instead of using the package manager.

Thanks!

4
Hello,
I have a function to sort a vector, the function is inside a while loop that loops until the vector is sorted.

Here is the function:
void bubbleSort(std::vector<Sortable>& sortElements, int timeSleep) {
                for (int n = 0; n < sortElements.size() - 1; n++) {
                        if (sortElements[n].value > sortElements[n + 1].value) {
                                // Swap positions
                                auto currElement = sortElements[n];
                                auto tempElement = sortElements[n + 1];
                                sortElements[n + 1] = currElement;
                                sortElements[n] = tempElement;
                        }

                        // Wait timeSleep microseconds between iterations
                        sf::sleep(sf::microseconds(timeSleep));
                }
        }
 

I wanted to implement a time sleep between each cycle, works good above 1000 microseconds (1ms), I also tested if there is any difference in time between 1000 and 1500 microseconds and there is a difference in time, so everything works great for now. But, when timeSleep is 999 or less, the time that each cycle takes is the same, from 1 to 999 microseconds (just like the sleep being skipped).

I also tried this alternative, but does something similar, under a certain range just rounds up to n miliseconds:
std::this_thread::sleep_for(std::chrono::microseconds(timeSleep));

The function is being executed in a detached thread.

ty!

5
Graphics / Re: Can't update shapes on window
« on: April 13, 2022, 03:39:25 pm »
When you render each object, you are using value as the x position, instead of the order in the vector. So when the objects are shuffled, they still have the same value, so they will always be in the same spot.

Try this:
int index = 0;
for (Sortable sortable : sortController.sortElements) {
    sf::RectangleShape shape = sortable.shape();

    shape.setPosition(sf::Vector2f(sortable.width * index++, sortController.winHeight - sortable.height));
    window.draw(shape);
    std::cout << "Shape n" << sortable.value << " drawed with height " << shape.getSize().y << std::endl;
}

Works perfectly now, thank you so much!  ;D

6
Graphics / [SOLVED] Can't update shapes on window
« on: April 13, 2022, 03:17:11 pm »
Hello,

Im trying to create a sorting visualizer. I have a vector of objects, and each object represents an element in the array to be sorted, im displaying the objects iterating the vector and calling the method shape() of the object that I create that returns a sf::RectangleShape, the result is the one that I was expecting:



Here is the part when I render the shapes:

main.cpp
window.clear(sf::Color::Black);
std::cout << "Window cleared!" << std::endl;

// Draw sortables
for (Sortable sortable : sortController.sortElements) {
    sf::RectangleShape shape = sortable.shape();

    shape.setPosition(sf::Vector2f(sortable.width * sortable.value, sortController.winHeight - sortable.height));
    window.draw(shape);
    std::cout << "Shape n" << sortable.value << " drawed with height " << shape.getSize().y << std::endl;
}

    std::cout << "ended for and displaying" << std::endl;

    window.display();

As you can see, Im storing the objects in sortElements, a vector inside the sortController object.

The problem is that i have a method in sortController that randomizes the array when I press space, I made the std::cout just after draw to see if the shapes height and values are being randomized, and yes, the output changes, here is an example:

Window cleared!
Shape n0 drawed with height 40
Shape n1 drawed with height 80
Shape n2 drawed with height 120
Shape n3 drawed with height 160
Shape n4 drawed with height 200
Shape n5 drawed with height 240
Shape n6 drawed with height 280
Shape n7 drawed with height 320
Shape n8 drawed with height 360
Shape n9 drawed with height 400
ended for and displaying
Random method called!
Window cleared!
Shape n3 drawed with height 160
Shape n1 drawed with height 80
Shape n6 drawed with height 280
Shape n2 drawed with height 120
Shape n0 drawed with height 40
Shape n7 drawed with height 320
Shape n5 drawed with height 240
Shape n8 drawed with height 360
Shape n4 drawed with height 200
Shape n9 drawed with height 400
ended for and displaying

So the thing is that the window is not changing at all. I also have this event to change the number of elements to sort and updates perfectly:

// Change number of sortables (increase)
case sf::Keyboard::Right:
    numOfElements++;
    sortController.clear(); // empty array
    sortController.populate(numOfElements); // fill array with numOfElements shapes
    std::cout << "Num of elements changed to: " << numOfElements << std::endl;
    break;

If you need it, here is the shape method and the method used to randomize the array of objects:
Sortable.cpp
sf::RectangleShape Sortable::shape() {
        return sf::RectangleShape(sf::Vector2f(width, height));
}

SortController.cpp
void SortController::randomize() {
        auto rd = std::random_device{};
        auto rng = std::default_random_engine{ rd() };
        std::shuffle(std::begin(sortElements), std::end(sortElements), rng);
};

I can provide more code if needed, but I wanted to do it more readable, ty

7
TYSM! It's finally working  :)

8
Oh, I didn't saw the bug already reported on github.

Where is the option to change the Conformance mode? I went to Project > Properties but i couldn't find it.



Thanks

9
Hello,

I've been trying to setup a project with SFML but I can't compile the tutorial, I followed the steps carefully, tried debug, release, static, non-static, etc... But Im still getting this error when I try to compile:

Output:
Build started...
1>------ Build started: Project: 04-hello-sfml, Configuration: Debug x64 ------
1>main.cpp
1>C:\Users\pc2\Documents\sfml\include\SFML\System\Vector2.inl(291,34): error C2370: 'sf::Vector2<T>::UnitX': redefinition; different storage class
1>C:\Users\pc2\Documents\sfml\include\SFML\System\Vector2.hpp(204): message : see declaration of 'sf::Vector2<T>::UnitX'
1>C:\Users\pc2\Documents\sfml\include\SFML\System\Vector2.inl(294,34): error C2370: 'sf::Vector2<T>::UnitY': redefinition; different storage class
1>C:\Users\pc2\Documents\sfml\include\SFML\System\Vector2.hpp(205): message : see declaration of 'sf::Vector2<T>::UnitY'
1>Done building project "04-hello-sfml.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

The code that im trying to compile is the tutorial one https://www.sfml-dev.org/tutorials/2.5/start-vc.php. Im including only SFML/Graphics.hpp

Also, when I go into Vector2.inl to see what's going on, the IDE is throwing a lot of errors like "Vector2 is not a template", "name followed by '::' must be a class or namespace name", etc... Maybe the problem is the compiler or the C++ version, I don't know.

IDE: Visual Studio 2022
SFML version: Last one from Snapshots (windows-vc17-64.zip), I don't know how to see the version
IDE C++ Language Standard: ISO C++20 Standard
Compiler: Microsoft (R) C/C++ Optimizing Compiler Version 19.31.31105 for x86

Pages: [1]