added basic SFML

now using App and SFML's loop, but haven't properly set up data vs
logic vs UI yet.
added setuptimers()
sortdata currently member var, should either make vec, or attach it to
a different object
This commit is contained in:
2026-08-03 02:05:08 +02:00
parent bcd9fe91c3
commit 401841f700
4 changed files with 121 additions and 43 deletions
+25 -6
View File
@@ -1,13 +1,29 @@
cmake_minimum_required(VERSION 3.24)
project(cppout)
cmake_minimum_required(VERSION 4.0)
project(cppout LANGUAGES CXX)
file(GLOB SOURCE_FILES "*/*.cpp")
file(GLOB SOURCE_FILES "src/*.cpp")
find_package(Curses REQUIRED)
include_directories(${CURSES_INCLUDE_DIR})
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
include(FetchContent)
FetchContent_Declare(SFML
GIT_REPOSITORY https://github.com/SFML/SFML.git
GIT_TAG 3.1.0
GIT_SHALLOW ON
EXCLUDE_FROM_ALL
SYSTEM)
FetchContent_MakeAvailable(SFML)
add_executable(${PROJECT_NAME} ${SOURCE_FILES})
add_custom_command(
TARGET ${PROJECT_NAME} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_directory
"${CMAKE_CURRENT_SOURCE_DIR}/assets"
"$<TARGET_FILE_DIR:${PROJECT_NAME}>/assets"
COMMENT "copying assets to build dir"
)
if(MSVC)
target_compile_option(${PROJECT_NAME} PRIVATE /W4)
else()
@@ -18,4 +34,7 @@ else()
)
endif()
target_link_libraries(${PROJECT_NAME} ${CURSES_LIBRARIES})
target_compile_features(${PROJECT_NAME} PRIVATE cxx_std_20)
target_link_libraries(${PROJECT_NAME} PRIVATE SFML::Graphics)
set_property(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY VS_STARTUP_PROJECT ${PROJECT_NAME})
BIN
View File
Binary file not shown.
+12
View File
@@ -0,0 +1,12 @@
#include <string>
#include <vector>
struct SortingData {
std::string name;
int target;
std::vector<int> data;
SortingData(std::string name, std::vector<int> data)
: name(name), data(data) {}
};
+84 -37
View File
@@ -1,54 +1,101 @@
// for now juist CLI, print an arr, then print each it
#include <SFML/Graphics/Color.hpp>
#include <SFML/Graphics/Font.hpp>
#include <SFML/Graphics/RenderWindow.hpp>
#include <SFML/Graphics/Text.hpp>
#include <SFML/Window/Window.hpp>
#include <chrono>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <memory>
#include <string>
#include <thread>
#include <vector>
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include "timer.hpp"
#include "sorters.hpp"
struct SortingData {
std::string name;
int target;
std::vector<int> data;
SortingData(std::string name, std::vector<int> data)
: name(name), data(data) {}
};
using namespace std::chrono;
using namespace std;
void displaynumbers(SortingData in_arr) {
for (size_t i = 0; i < in_arr.data.size(); ++i) {
printf("array:%d ", in_arr.data[i]);
}
printf("\n");
for (size_t i = 0; i < in_arr.data.size(); ++i) {
printf("array:%d ", in_arr.data[i]);
}
printf("\n");
}
void stepbubblesort(std::vector<int> &data, size_t step) {
data[step%data.size()]++;
}
void stepbubblesort(vector<int> &data, size_t step) {
data[step%data.size()]++;
}
vector<int> dataprovider(){
return vector<int> {9,8,1,2,7,3,6,4,5};
}
void setuptimers(TimerManager &mytimermanager, SortingData &mysd){
unique_ptr<Timer> mytimer = make_unique<Timer>(
"sort1", 0,
[&mysd](Timer &t) { stepbubblesort(mysd.data, t.triggercounter); },
milliseconds(1000));
unique_ptr<Timer> mydisplaytimer = make_unique<Timer>("display", 0,
[&mysd](Timer &t){displaynumbers(mysd);},
milliseconds(1000));
mytimermanager.add_timer(move(mytimer));
mytimermanager.add_timer(move(mydisplaytimer));
}
struct App{
sf::RenderWindow mywindow;
sf::Font myfont;
sf::Text text;
TimerManager mytimermanager;
SortingData mysortdata;
sf::Font load_font(const string &filename) {
sf::Font font;
if (!font.openFromFile(filename)){
fprintf(stderr, "failed to load font");
std::exit(1);
}
return font;
}
App():mywindow(sf::VideoMode({800,600}), "mywindow"),
myfont(load_font("assets/NS-R.ttf")),
text(myfont),
mysortdata("arr1", dataprovider()) {
text.setFont(myfont);
text.setString("Hello world");
text.setCharacterSize(24);
text.setFillColor(sf::Color::Red);
setuptimers(mytimermanager, mysortdata);
}
void run(){
while (mywindow.isOpen()){
while (const optional event = mywindow.pollEvent()){
if (event->is<sf::Event::Closed>())
mywindow.close();
}
mywindow.clear(sf::Color::Black);
mywindow.draw(text);
mytimermanager.update_timers();
mywindow.display();
this_thread::sleep_for(milliseconds(1000));
}
}
};
int main() {
std::vector<int> dummydata = {9,8,1,2,7,3,6,4,5};
SortingData mysd("arr1", dummydata);
std::unique_ptr<Timer> mytimer = std::make_unique<Timer>(
"sort1", 0,
[&mysd](Timer &t) { stepbubblesort(mysd.data, t.triggercounter); },
std::chrono::milliseconds(1000));
std::unique_ptr<Timer> mydisplaytimer = std::make_unique<Timer>("display", 0, [&mysd](Timer &t){displaynumbers(mysd);}, std::chrono::milliseconds(1000));
TimerManager mytimermanager;
mytimermanager.add_timer(std::move(mytimer));
mytimermanager.add_timer(std::move(mydisplaytimer));
while (true) {
mytimermanager.update_timers();
std::this_thread::sleep_for(std::chrono::milliseconds(100));
};
App myapp;
myapp.run();
}