Files
2026-10-22 16:25:06 +03:00

111 lines
3.1 KiB
Plaintext

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#include <stdlib.h>
#include <string>
#include "raylib.h"
#include "raybri.h"
// iOS entry points. The prebuilt raylib.framework (ghera/raylib-iOS) owns
// main()/AppDelegate/SceneDelegate and a full-screen EGL/ANGLE window; it
// calls these three callbacks (see src/platforms/rcore_ios.c). We therefore
// drive the same raybri_* scene as the macOS build from here, and present the
// SwiftUI UI inside raylib's own window (see RenderIOS.swift).
//
// The prebuilt framework already ran rlLoadExtensions()/rlglInit() inside
// InitWindow(), so raybri_init() only compiles the shaders and builds meshes.
#if defined(__cplusplus)
extern "C" void rcios_present_overlay(void); // implemented in Swift (RenderIOS.swift)
#else
void rcios_present_overlay(void);
#endif
// The GLSL sources live in renderConstShader.swift (shared with the macOS
// build, with the platform #version already substituted there). These Swift
// accessors return malloc'd copies that sr_shader() releases.
#if defined(__cplusplus)
extern "C" {
#endif
char *rcios_shader_main_vs(void);
char *rcios_shader_main_fs(void);
char *rcios_shader_shadow_vs(void);
char *rcios_shader_shadow_fs(void);
#if defined(__cplusplus)
}
#endif
static std::string sr_shader(char *(*accessor)(void))
{
char *source = accessor();
std::string shader = source ? source : "";
free(source);
return shader;
}
static double sr_start_time = 0.0;
static int sr_window_ready = 0;
double rcios_time_now(void)
{
return CACurrentMediaTime();
}
extern "C" void ios_ready(void)
{
SetTraceLogLevel(LOG_WARNING);
// Size is ignored on PLATFORM_IOS; the fork sizes the window itself.
InitWindow(0, 0, "OGS chkgfx");
// On iOS raylib's InitWindow loaded the GL functions and called rlglInit,
// so raybri_init() must not repeat it (see raybri.c, RAYBRI_PLATFORM_IOS).
std::string main_vs = sr_shader(rcios_shader_main_vs);
std::string main_fs = sr_shader(rcios_shader_main_fs);
std::string shadow_vs = sr_shader(rcios_shader_shadow_vs);
std::string shadow_fs = sr_shader(rcios_shader_shadow_fs);
raybri_init(
main_vs.c_str(),
main_fs.c_str(),
shadow_vs.c_str(),
shadow_fs.c_str(),
(raybri_config){ 45.0f, 0.1f, 1000.0f, 256, 40.0f }
);
// Same layout as the macOS app bundle: <bundle>/cube/cube.gltf
NSString *gltf = [[[NSBundle mainBundle] resourcePath]
stringByAppendingPathComponent:@"cube/cube.gltf"];
raybri_load_model([gltf UTF8String]);
sr_start_time = rcios_time_now();
sr_window_ready = 1;
// Show ChkgfxView (the SwiftUI title) above the raylib scene.
rcios_present_overlay();
}
extern "C" void ios_update(bool viewSizeChanged)
{
if (!sr_window_ready) return;
int w = GetRenderWidth();
int h = GetRenderHeight();
raybri_resize(w, h);
float t = (float)(rcios_time_now() - sr_start_time);
raybri_set_rotation(t * 6.0f, t * 18.0f, 0.0f);
BeginDrawing();
raybri_render();
EndDrawing();
}
extern "C" void ios_destroy(void)
{
if (!sr_window_ready) return;
raybri_shutdown();
CloseWindow();
sr_window_ready = 0;
}