From 597b7c26e444789bc80fe17115d30538d66075ae Mon Sep 17 00:00:00 2001 From: That_One_Nerd Date: Thu, 23 May 2024 14:08:48 -0400 Subject: [PATCH] Render loop works. There's nothing there, but it works. --- include/sharpsoft/all.hpp | 2 ++ include/sharpsoft/internal.hpp | 3 ++ include/sharpsoft/interop.hpp | 35 ++++++++++++++++++++++ include/sharpsoft/macros.hpp | 3 ++ src/main.cpp | 4 +-- src/sharpsoft/global_manager.cpp | 34 +++++++++++++++------ src/sharpsoft/threading.cpp | 51 ++++++++++++++++++++++++++++++++ 7 files changed, 121 insertions(+), 11 deletions(-) create mode 100644 include/sharpsoft/interop.hpp create mode 100644 include/sharpsoft/macros.hpp create mode 100644 src/sharpsoft/threading.cpp diff --git a/include/sharpsoft/all.hpp b/include/sharpsoft/all.hpp index 2f0ea77..420fa3e 100644 --- a/include/sharpsoft/all.hpp +++ b/include/sharpsoft/all.hpp @@ -1,4 +1,6 @@ #include "basic_types.hpp" #include "enums.hpp" #include "global_misc.hpp" +#include "interop.hpp" +#include "macros.hpp" #include "window_types.hpp" diff --git a/include/sharpsoft/internal.hpp b/include/sharpsoft/internal.hpp index 4838372..931c195 100644 --- a/include/sharpsoft/internal.hpp +++ b/include/sharpsoft/internal.hpp @@ -8,6 +8,9 @@ namespace sharp namespace internal { void add_window(window_base* win_ptr, size_t size); + + void render_iter(); + void render_loop(); } } #endif diff --git a/include/sharpsoft/interop.hpp b/include/sharpsoft/interop.hpp new file mode 100644 index 0000000..5128ac9 --- /dev/null +++ b/include/sharpsoft/interop.hpp @@ -0,0 +1,35 @@ +#pragma once + +#include "macros.hpp" + +#ifdef SH_PROS_ACTIVE +#include "pros/rtos.hpp" +#else +#error Unsupported library. Interoperability cannot be used here. +#endif + +namespace sharp +{ + class thread + { + private: + void (*func)(); + bool active; + +#ifdef SH_PROS_ACTIVE + pros::Task* underlying; +#endif + + public: + ~thread(); + thread(void (*function)()); + + static void delay(int time_ms); + + bool is_active() const; + + void join(); + void start(); + void stop(); + }; +} diff --git a/include/sharpsoft/macros.hpp b/include/sharpsoft/macros.hpp new file mode 100644 index 0000000..9b7710d --- /dev/null +++ b/include/sharpsoft/macros.hpp @@ -0,0 +1,3 @@ +#ifdef _PROS_INCLUDE_LIBLVGL_LLEMU_H +#define SH_PROS_ACTIVE +#endif diff --git a/src/main.cpp b/src/main.cpp index 896e150..4d60de3 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -9,7 +9,7 @@ class test_window : public sharp::window_base protected: void paint() override { - printf("Printed: %d", test_variable); + printf("Printed: %d\n", test_variable); } public: @@ -29,7 +29,7 @@ void initialize() test_window moment = test_window(); moment.test_variable = 3; - sharp::add_window(&moment); + sharp::add_window(moment); sharp::start(); } diff --git a/src/sharpsoft/global_manager.cpp b/src/sharpsoft/global_manager.cpp index f520446..38a41f0 100644 --- a/src/sharpsoft/global_manager.cpp +++ b/src/sharpsoft/global_manager.cpp @@ -1,9 +1,11 @@ +#include "pros/rtos.hpp" #define SHARPSOFT_INTERNAL #include #include #include "sharpsoft/global_misc.hpp" #include "sharpsoft/internal.hpp" +#include "sharpsoft/interop.hpp" using namespace sharp; using std::vector; @@ -16,12 +18,12 @@ const global_properties global_properties::defaults = bool init = false; bool started = false; color back_col; +thread* render_thread = nullptr; vector windows; void sharp::initialize() { - if (init) return; sharp::initialize(global_properties::defaults); } void sharp::initialize(const global_properties& props) @@ -65,6 +67,24 @@ bool sharp::is_initialized() return init; } +void sharp::internal::render_iter() +{ + int window_count = windows.size(); + for (int i = 0; i < window_count; i++) + { + window_base* win = windows.at(i); + win->paint(); + } +} +void sharp::internal::render_loop() +{ + while (true) + { + render_iter(); + thread::delay(100); // TODO: should be something else. + } +} + void sharp::internal::add_window(window_base* win_ptr, size_t size) { void* copy_raw = malloc(size); @@ -74,17 +94,12 @@ void sharp::internal::add_window(window_base* win_ptr, size_t size) windows.push_back(copy); } -void sharp::test() -{ - window_base* item = windows.at(0); - item->paint(); -} - void sharp::start() { if (!init || started) return; - // TODO + render_thread = new thread(internal::render_loop); + render_thread->start(); started = true; } @@ -92,7 +107,8 @@ void sharp::end() { if (!init || !started) return; - // TODO + render_thread->stop(); + delete render_thread; started = false; } diff --git a/src/sharpsoft/threading.cpp b/src/sharpsoft/threading.cpp new file mode 100644 index 0000000..b6e4df1 --- /dev/null +++ b/src/sharpsoft/threading.cpp @@ -0,0 +1,51 @@ +#include "pros/rtos.hpp" +#include "sharpsoft/interop.hpp" + +using namespace sharp; + +bool thread::is_active() const +{ + return active; +} + +#ifdef SH_PROS_ACTIVE +void thread::delay(int time_ms) +{ + pros::delay(time_ms); +} + +thread::thread(void (*function)()) +{ + active = false; + func = function; + underlying = nullptr; +} + +thread::~thread() +{ + if (active) stop(); +} + +void thread::join() +{ + underlying->join(); +} + +void thread::start() +{ + if (active) return; + + underlying = new pros::Task(func); + + active = true; +} +void thread::stop() +{ + if (!active) return; + + underlying->remove(); + delete underlying; + + active = false; +} +#endif