Update Kha/Backends/Kinc-HL/kinc-bridge/kinc.c.h

This commit is contained in:
Onek8 2025-02-01 13:53:43 +00:00
parent 20db9cc24d
commit 181b860360

View File

@ -1,127 +1,129 @@
#include <kinc/audio2/audio.h> #include <kinc/audio2/audio.h>
#include <kinc/graphics4/graphics.h> #include <kinc/graphics4/graphics.h>
#include <kinc/io/filereader.h> #include <kinc/io/filereader.h>
#include <kinc/log.h> #include <kinc/log.h>
#include <kinc/system.h> #include <kinc/system.h>
#include <kinc/window.h> #include <kinc/window.h>
#include <hl.h> #include <hl.h>
void frame(); void frame();
static bool visible = true; static bool visible = true;
static bool paused = false; static bool paused = false;
typedef void (*FN_AUDIO_CALL_CALLBACK)(int); typedef void (*FN_AUDIO_CALL_CALLBACK)(int);
typedef float (*FN_AUDIO_READ_SAMPLE)(void); typedef float (*FN_AUDIO_READ_SAMPLE)(void);
void (*audioCallCallback)(int); void (*audioCallCallback)(int);
float (*audioReadSample)(void); float (*audioReadSample)(void);
static void update(void *data) { static void update(void *data) {
if (paused) { if (paused) {
return; return;
} }
kinc_a2_update(); kinc_a2_update();
int windowCount = kinc_count_windows(); int windowCount = kinc_count_windows();
for (int windowIndex = 0; windowIndex < windowCount; ++windowIndex) { for (int windowIndex = 0; windowIndex < windowCount; ++windowIndex) {
if (visible) { if (visible) {
kinc_g4_begin(windowIndex); kinc_g4_begin(windowIndex);
frame(); frame();
kinc_g4_end(windowIndex); kinc_g4_end(windowIndex);
} }
} }
if (!kinc_g4_swap_buffers()) { if (!kinc_g4_swap_buffers()) {
kinc_log(KINC_LOG_LEVEL_ERROR, "Graphics context lost."); kinc_log(KINC_LOG_LEVEL_ERROR, "Graphics context lost.");
} }
} }
static bool mixThreadregistered = false; static bool mixThreadregistered = false;
static void mix(kinc_a2_buffer_t *buffer, int samples) { static void mix(kinc_a2_buffer_t *buffer, uint32_t samples, void *userdata) {
#ifdef KORE_MULTITHREADED_AUDIO #ifdef KINC_MULTITHREADED_AUDIO
if (!mixThreadregistered) { if (!mixThreadregistered) {
vdynamic *ret; vdynamic *ret;
hl_register_thread(&ret); hl_register_thread(&ret);
mixThreadregistered = true; mixThreadregistered = true;
} }
hl_blocking(true); hl_blocking(true);
#endif #endif
audioCallCallback(samples); audioCallCallback(samples);
for (int i = 0; i < samples; ++i) { for (uint32_t i = 0; i < samples; i += 2) {
float value = audioReadSample(); float left_value = audioReadSample();
*(float *)&buffer->data[buffer->write_location] = value; float right_value = audioReadSample();
buffer->write_location += 4; *(float *)&buffer->channels[0][buffer->write_location] = left_value;
if (buffer->write_location >= buffer->data_size) { *(float *)&buffer->channels[1][buffer->write_location] = right_value;
buffer->write_location = 0; buffer->write_location += 1;
} if (buffer->write_location >= buffer->data_size) {
} buffer->write_location = 0;
}
#ifdef KORE_MULTITHREADED_AUDIO }
hl_blocking(false);
#endif #ifdef KINC_MULTITHREADED_AUDIO
} hl_blocking(false);
#endif
void hl_init_kore(vbyte *title, int width, int height, int samplesPerPixel, bool vSync, int windowMode, int windowFeatures) { }
kinc_log(KINC_LOG_LEVEL_INFO, "Starting KincHL");
void hl_init_kore(vbyte *title, int width, int height, int samplesPerPixel, bool vSync, int windowMode, int windowFeatures) {
kinc_window_options_t win; kinc_log(KINC_LOG_LEVEL_INFO, "Starting KincHL");
kinc_window_options_set_defaults(&win);
win.title = (char *)title; kinc_window_options_t win;
win.width = width; kinc_window_options_set_defaults(&win);
win.height = height; win.title = (char *)title;
win.x = -1; win.width = width;
win.y = -1; win.height = height;
win.mode = (kinc_window_mode_t)windowMode; win.x = -1;
win.window_features = windowFeatures; win.y = -1;
kinc_framebuffer_options_t frame; win.mode = (kinc_window_mode_t)windowMode;
kinc_framebuffer_options_set_defaults(&frame); win.window_features = windowFeatures;
frame.vertical_sync = vSync; kinc_framebuffer_options_t frame;
frame.samples_per_pixel = samplesPerPixel; kinc_framebuffer_options_set_defaults(&frame);
kinc_init((char *)title, width, height, &win, &frame); frame.vertical_sync = vSync;
frame.samples_per_pixel = samplesPerPixel;
kinc_set_update_callback(update, NULL); kinc_init((char *)title, width, height, &win, &frame);
}
kinc_set_update_callback(update, NULL);
void hl_kinc_init_audio(vclosure *callCallback, vclosure *readSample, int *outSamplesPerSecond) { }
audioCallCallback = *((FN_AUDIO_CALL_CALLBACK *)(&callCallback->fun));
audioReadSample = *((FN_AUDIO_READ_SAMPLE *)(&readSample->fun)); void hl_kinc_init_audio(vclosure *callCallback, vclosure *readSample, int *outSamplesPerSecond) {
kinc_a2_set_callback(mix); audioCallCallback = *((FN_AUDIO_CALL_CALLBACK *)(&callCallback->fun));
kinc_a2_init(); audioReadSample = *((FN_AUDIO_READ_SAMPLE *)(&readSample->fun));
*outSamplesPerSecond = kinc_a2_samples_per_second; kinc_a2_set_callback(mix, NULL);
} kinc_a2_init();
*outSamplesPerSecond = kinc_a2_samples_per_second();
void hl_run_kore(void) { }
kinc_start();
} void hl_run_kore(void) {
kinc_start();
vbyte *hl_kinc_file_contents(vbyte *name, int *size) { }
int len;
int p = 0; vbyte *hl_kinc_file_contents(vbyte *name, int *size) {
vbyte *content; int len;
kinc_file_reader_t file; int p = 0;
if (!kinc_file_reader_open(&file, (char *)name, KINC_FILE_TYPE_ASSET)) { vbyte *content;
return NULL; kinc_file_reader_t file;
} if (!kinc_file_reader_open(&file, (char *)name, KINC_FILE_TYPE_ASSET)) {
hl_blocking(true); return NULL;
len = (int)kinc_file_reader_size(&file); }
if (size) { hl_blocking(true);
*size = len; len = (int)kinc_file_reader_size(&file);
} if (size) {
hl_blocking(false); *size = len;
content = (vbyte *)hl_gc_alloc_noptr(size ? len : len + 1); }
hl_blocking(true); hl_blocking(false);
if (!size) { content = (vbyte *)hl_gc_alloc_noptr(size ? len : len + 1);
content[len] = 0; // final 0 for UTF8 hl_blocking(true);
} if (!size) {
kinc_file_reader_read(&file, content, len); content[len] = 0; // final 0 for UTF8
kinc_file_reader_close(&file); }
hl_blocking(false); kinc_file_reader_read(&file, content, len);
return content; kinc_file_reader_close(&file);
} hl_blocking(false);
return content;
}