Kaynağa Gözat

sys: add support for Kinc main loop callback

wip/deprecate
Sam Hocevar 2 yıl önce
ebeveyn
işleme
6af228a946
3 değiştirilmiş dosya ile 43 ekleme ve 6 silme
  1. +16
    -0
      doc/examples/window.cpp
  2. +8
    -0
      src/include/lol/engine/private/sys/init.h
  3. +19
    -6
      src/sys/init.cpp

+ 16
- 0
doc/examples/window.cpp Dosyayı Görüntüle

@@ -14,11 +14,27 @@
# include "config.h"
#endif

#include <kinc/graphics4/graphics.h>
#include <lol/engine/sys>
#include <cmath>

int main(int argc, char **argv)
{
int frame = 0;

// Open a simple window
lol::sys::init(argc, argv, "window", 640, 480);

// Update routine: clear the background color
lol::sys::add_callback([&frame]() {
uint32_t alpha = uint32_t(127.0f + 127.0f * std::cos(++frame * 0.1f));
kinc_g4_begin(0);
kinc_g4_clear(KINC_G4_CLEAR_COLOR, 0x800000 | alpha * 0x0101, 0.f, 0);
kinc_g4_end(0);
kinc_g4_swap_buffers();
return true;
});

lol::sys::run();
return EXIT_SUCCESS;
}

+ 8
- 0
src/include/lol/engine/private/sys/init.h Dosyayı Görüntüle

@@ -17,6 +17,7 @@
// -------------------------------
//

#include <functional> // std::function
#include <string> // std::string

//
@@ -52,13 +53,20 @@
namespace lol::sys
{

// Initialise the system and automatically create a window.
extern void init(int argc, char *argv[],
std::string const &name, int width, int height,
std::string const &projectdir = LOL_CONFIG_PROJECTDIR,
std::string const &solutiondir = LOL_CONFIG_SOLUTIONDIR,
std::string const &sourcesubdir = LOL_CONFIG_SOURCESUBDIR);

// Register an update callback. The callback can return false to automatically unregister.
extern void add_callback(std::function<bool()>);

// Run the main loop.
extern void run();

// Stop the main loop.
extern void stop();

extern void add_data_dir(std::string const &dir);


+ 19
- 6
src/sys/init.cpp Dosyayı Görüntüle

@@ -13,12 +13,13 @@
#include <lol/engine/sys>
#include <lol/msg>

#include <vector> // std::vector
#include <string> // std::string
#include <algorithm> // std::erase_if
#include <cctype>
#if !defined _LIBCPP_HAS_NO_FILESYSTEM_LIBRARY
# include <filesystem> // std::filesystem::exists
#endif
#include <string> // std::string
#include <vector> // std::vector

#include <kinc/system.h>

@@ -29,6 +30,8 @@ namespace lol::sys
static std::vector<std::filesystem::path> data_dir;
#endif

static std::vector<std::function<int()>> g_callbacks;

void init(int argc, char *argv[],
std::string const &name, int width, int height,
std::string const &projectdir,
@@ -37,10 +40,6 @@ void init(int argc, char *argv[],
{
using namespace std;

#if LOL_USE_KINC
kinc_init(name.c_str(), width, height, nullptr, nullptr);
#endif

#if !defined _LIBCPP_HAS_NO_FILESYSTEM_LIBRARY
msg::debug("project dir: “%s”\n", std::filesystem::path(projectdir).generic_string().c_str());
msg::debug("solution dir: “%s”\n", std::filesystem::path(solutiondir).generic_string().c_str());
@@ -115,6 +114,20 @@ void init(int argc, char *argv[],
msg::debug("data dir %d/%d: “%s”\n", i + 1, int(data_dir.size()),
data_dir[i].generic_string().c_str());
#endif

#if LOL_USE_KINC
kinc_init(name.c_str(), width, height, nullptr, nullptr);

// Call all the registered callbacks and remove the ones that return false
kinc_set_update_callback([](void *){
std::erase_if(g_callbacks, [](auto fn) { return !fn(); });
} , nullptr);
#endif
}

void add_callback(std::function<bool()> fn)
{
g_callbacks.push_back(fn);
}

void run()


Yükleniyor…
İptal
Kaydet