Support iOS for ogs-blue (#7)
This commit was merged in pull request #7.
This commit is contained in:
1 parent
37c4e6f442
commit
2b556a4a58
58 files changed
+710
-300
No files matched your search
@@ -0,0 +1,87 @@
|
||||
#ifndef EGL_H
|
||||
#define EGL_H
|
||||
|
||||
#include "eglConst.h"
|
||||
#include <libEGL/libEGL.h>
|
||||
#include <libGLESv2/GLES/gl.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
typedef struct EGLState {
|
||||
void *caLayer;
|
||||
EGLConfig cfg;
|
||||
EGLContext ctx;
|
||||
EGLDisplay display;
|
||||
EGLSurface surface;
|
||||
} EGLState;
|
||||
|
||||
bool eglInit(EGLState *st);
|
||||
bool eglMakeCurrentContext(EGLState *st);
|
||||
bool eglResetSurface(EGLState *st);
|
||||
|
||||
// Source
|
||||
|
||||
#if defined(EGL_IMPLEMENTATION)
|
||||
|
||||
bool eglInit(EGLState *st) {
|
||||
st->display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
|
||||
if (st->display == EGL_NO_DISPLAY) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (eglInitialize(st->display, NULL, NULL) == EGL_FALSE) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (eglBindAPI(EGL_OPENGL_ES_API) == EGL_FALSE) {
|
||||
return false;
|
||||
}
|
||||
|
||||
EGLint cfgCount = 0;
|
||||
if (
|
||||
eglChooseConfig(st->display, EGL_CFG, &st->cfg, 1, &cfgCount) == EGL_FALSE ||
|
||||
cfgCount < 1
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
|
||||
st->ctx = eglCreateContext(st->display, st->cfg, EGL_NO_CONTEXT, EGL_CTX);
|
||||
if (st->ctx == EGL_NO_CONTEXT) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool eglMakeCurrentContext(EGLState *st) {
|
||||
if (eglMakeCurrent(st->display, st->surface, st->surface, st->ctx) == EGL_FALSE) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool eglResetSurface(EGLState *st) {
|
||||
// Destroy previous surface
|
||||
if (st->surface != EGL_NO_SURFACE) {
|
||||
eglDestroySurface(st->display, st->surface);
|
||||
st->surface = EGL_NO_SURFACE;
|
||||
}
|
||||
|
||||
// Create new surface
|
||||
// ANGLE on iOS takes a CALayer as the native window
|
||||
st->surface = eglCreateWindowSurface(
|
||||
st->display,
|
||||
st->cfg,
|
||||
(EGLNativeWindowType)st->caLayer,
|
||||
NULL
|
||||
);
|
||||
if (st->surface == EGL_NO_SURFACE) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
#endif // EGL_IMPLEMENTATION
|
||||
|
||||
#endif // EGL_H
|
||||
@@ -0,0 +1,33 @@
|
||||
#ifndef EGL_CONST_H
|
||||
#define EGL_CONST_H
|
||||
|
||||
#include <libEGL/libEGL.h>
|
||||
|
||||
// Header
|
||||
|
||||
extern const EGLint EGL_CFG[];
|
||||
extern const EGLint EGL_CTX[];
|
||||
|
||||
// Source
|
||||
|
||||
#if defined(EGL_CONST_IMPLEMENTATION)
|
||||
|
||||
const EGLint EGL_CFG[] = {
|
||||
EGL_RENDERABLE_TYPE, EGL_OPENGL_ES3_BIT,
|
||||
EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
|
||||
EGL_RED_SIZE, 8,
|
||||
EGL_GREEN_SIZE, 8,
|
||||
EGL_BLUE_SIZE, 8,
|
||||
EGL_ALPHA_SIZE, 8,
|
||||
EGL_DEPTH_SIZE, 16,
|
||||
EGL_NONE
|
||||
};
|
||||
|
||||
const EGLint EGL_CTX[] = {
|
||||
EGL_CONTEXT_CLIENT_VERSION, 3,
|
||||
EGL_NONE
|
||||
};
|
||||
|
||||
#endif // EGL_CONST_IMPLEMENTATION
|
||||
|
||||
#endif // EGL_CONST_H
|
||||
@@ -1,6 +1,18 @@
|
||||
#ifndef RAYBRI_AUX_H
|
||||
#define RAYBRI_AUX_H
|
||||
|
||||
// Header
|
||||
|
||||
void *raybriGLLoader(const char *name);
|
||||
void raybriLoadExtensions(void);
|
||||
|
||||
// Source
|
||||
|
||||
#if defined(RAYBRI_AUX_IMPLEMENTATION)
|
||||
|
||||
#include <dlfcn.h>
|
||||
#include "raybriAux.h"
|
||||
#include "raybriConst.h"
|
||||
#include "rlgl.h"
|
||||
#include <stddef.h>
|
||||
|
||||
// raylib is built as a static/dynamic library with GLFW baked in, but we never
|
||||
@@ -19,3 +31,11 @@ void *raybriGLLoader(const char *name) {
|
||||
|
||||
return dlsym(handle, name);
|
||||
}
|
||||
|
||||
void raybriLoadExtensions(void) {
|
||||
rlLoadExtensions(raybriGLLoader);
|
||||
}
|
||||
|
||||
#endif // RAYBRI_AUX_IMPLEMENTATION
|
||||
|
||||
#endif // RAYBRI_AUX_H
|
||||
@@ -1,29 +0,0 @@
|
||||
#include "raybri.h"
|
||||
#include "raybriAux.h"
|
||||
#include "rlgl.h"
|
||||
|
||||
void raybriInit(void) {
|
||||
//SetTraceLogLevel(LOG_WARNING);
|
||||
|
||||
// Load OpenGL extensions
|
||||
rlLoadExtensions((void *)raybriGLLoader);
|
||||
// Initialize raylib graphics layer with a placeholder 1x1 framebuffer size
|
||||
// Graphics layer includes default texture, shader, render batch and OpenGL state
|
||||
rlglInit(1, 1);
|
||||
// Background is painted in blue
|
||||
rlClearColor(0, 0, 255, 255);
|
||||
}
|
||||
|
||||
void raybriRender(int width, int height) {
|
||||
// Set the drawing area to the current pixel size
|
||||
rlViewport(0, 0, width, height);
|
||||
// Clear color and depth buffers for the next frame
|
||||
rlClearScreenBuffers();
|
||||
// Draw any queued geometry
|
||||
rlDrawRenderBatchActive();
|
||||
}
|
||||
|
||||
void raybriShutdown(void) {
|
||||
// Release rlgl buffers, shaders and textures
|
||||
rlglClose();
|
||||
}
|
||||
@@ -1,8 +1,42 @@
|
||||
#ifndef RAYBRI_H
|
||||
#define RAYBRI_H
|
||||
|
||||
// Header
|
||||
|
||||
void raybriInit(void);
|
||||
void raybriRender(int width, int height);
|
||||
void raybriShutdown(void);
|
||||
|
||||
// Source
|
||||
|
||||
#if defined(RAYBRI_IMPLEMENTATION)
|
||||
|
||||
#include "rlgl.h"
|
||||
|
||||
void raybriInit(void) {
|
||||
//SetTraceLogLevel(LOG_WARNING);
|
||||
|
||||
// Initialize raylib graphics layer with a placeholder 1x1 framebuffer size
|
||||
// Graphics layer includes default texture, shader, render batch and OpenGL state
|
||||
rlglInit(1, 1);
|
||||
// Background is painted in blue
|
||||
rlClearColor(0, 0, 255, 255);
|
||||
}
|
||||
|
||||
void raybriRender(int width, int height) {
|
||||
// Set the drawing area to the current pixel size
|
||||
rlViewport(0, 0, width, height);
|
||||
// Clear color and depth buffers for the next frame
|
||||
rlClearScreenBuffers();
|
||||
// Draw any queued geometry
|
||||
rlDrawRenderBatchActive();
|
||||
}
|
||||
|
||||
void raybriShutdown(void) {
|
||||
// Release rlgl buffers, shaders and textures
|
||||
rlglClose();
|
||||
}
|
||||
|
||||
#endif // RAYBRI_IMPLEMENTATION
|
||||
|
||||
#endif // RAYBRI_H
|
||||
@@ -1,6 +0,0 @@
|
||||
#ifndef RAYBRI_AUX_H
|
||||
#define RAYBRI_AUX_H
|
||||
|
||||
void *raybriGLLoader(const char *name);
|
||||
|
||||
#endif // RAYBRI_AUX_H
|
||||
@@ -1,3 +0,0 @@
|
||||
#include "raybriConst.h"
|
||||
|
||||
const char RAYBRI_MAC_GL[] = "/System/Library/Frameworks/OpenGL.framework/OpenGL";
|
||||
@@ -1,6 +1,16 @@
|
||||
#ifndef RAYBRI_CONST_H
|
||||
#define RAYBRI_CONST_H
|
||||
|
||||
// Header
|
||||
|
||||
extern const char RAYBRI_MAC_GL[];
|
||||
|
||||
// Source
|
||||
|
||||
#if defined(RAYBRI_CONST_IMPLEMENTATION)
|
||||
|
||||
const char RAYBRI_MAC_GL[] = "/System/Library/Frameworks/OpenGL.framework/OpenGL";
|
||||
|
||||
#endif // RAYBRI_CONST_IMPLEMENTATION
|
||||
|
||||
#endif // RAYBRI_CONST_H
|
||||
@@ -0,0 +1,19 @@
|
||||
import UIKit
|
||||
|
||||
func renderDraw(_ vm: RenderVM) {
|
||||
guard eglMakeCurrentContext(&vm.glView.st) else { return }
|
||||
raybriRender(vm.pixelSize[0], vm.pixelSize[1])
|
||||
eglSwapBuffers(vm.glView.st.display, vm.glView.st.surface)
|
||||
}
|
||||
|
||||
func renderPixelSize(_ view: UIView) -> [Int32] {
|
||||
let scale = view.contentScaleFactor
|
||||
let w = Int32(max(1, (view.bounds.width * scale).rounded()))
|
||||
let h = Int32(max(1, (view.bounds.height * scale).rounded()))
|
||||
return [w, h]
|
||||
}
|
||||
|
||||
func renderRecreateSurface(_ vm: RenderVM) {
|
||||
eglResetSurface(&vm.glView.st)
|
||||
eglMakeCurrentContext(&vm.glView.st)
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
import Foundation
|
||||
import QuartzCore
|
||||
import UIKit
|
||||
|
||||
func renderDeinit(_ vm: RenderVM) {
|
||||
vm.timer?.invalidate()
|
||||
vm.timer = nil
|
||||
eglMakeCurrentContext(&vm.glView.st)
|
||||
raybriShutdown()
|
||||
|
||||
eglMakeCurrent(vm.glView.st.display, nil, nil, nil)
|
||||
eglDestroySurface(vm.glView.st.display, vm.glView.st.surface)
|
||||
eglDestroyContext(vm.glView.st.display, vm.glView.st.ctx)
|
||||
eglTerminate(vm.glView.st.display)
|
||||
}
|
||||
|
||||
func renderInit(_ vm: RenderVM) {
|
||||
// On iOS EGL/ANGLE renders into the UIView's layer
|
||||
let layer = Unmanaged.passUnretained(vm.glView.layer).toOpaque()
|
||||
vm.glView.st.caLayer = layer
|
||||
|
||||
guard eglInit(&vm.glView.st) else { fatalError("ERROR Could not initialize EGL") }
|
||||
guard eglResetSurface(&vm.glView.st) else { fatalError("ERROR Could not reset EGL surface") }
|
||||
guard eglMakeCurrentContext(&vm.glView.st) else { fatalError("ERROR Could not make EGL context current") }
|
||||
|
||||
raybriInit()
|
||||
}
|
||||
|
||||
func renderStartTimer(_ vm: RenderVM) {
|
||||
// Make a time mark for calculating time delta
|
||||
vm.startTime = CACurrentMediaTime()
|
||||
|
||||
// The frame is presented by eglSwapBuffers into the view's layer, not by
|
||||
// CoreGraphics, so the timer drives the rendering itself instead of
|
||||
// invalidating the view for a draw(_:) pass the way macOS does
|
||||
let timer = Timer(timeInterval: 1.0 / RENDER_FPS, repeats: true) { [weak vm] _ in
|
||||
guard let vm else { return }
|
||||
renderDraw(vm)
|
||||
}
|
||||
vm.timer = timer
|
||||
// .common means holding buttons in UI does not pause this rendering timer
|
||||
RunLoop.main.add(timer, forMode: .common)
|
||||
}
|
||||
@@ -1,11 +1,35 @@
|
||||
import UIKit
|
||||
|
||||
final class RenderGLView: UIView {
|
||||
var st = EGLState()
|
||||
|
||||
override init(frame: CGRect) {
|
||||
super.init(frame: frame)
|
||||
isOpaque = true
|
||||
isUserInteractionEnabled = false
|
||||
}
|
||||
|
||||
required init?(coder: NSCoder) {
|
||||
fatalError("init(coder:) has not been implemented")
|
||||
}
|
||||
|
||||
deinit {
|
||||
renderSet(F.willDeinit, true)
|
||||
}
|
||||
|
||||
override func didMoveToWindow() {
|
||||
super.didMoveToWindow()
|
||||
guard let window else { return }
|
||||
contentScaleFactor = window.screen.nativeScale
|
||||
layer.contentsScale = contentScaleFactor
|
||||
|
||||
renderSet(F.didPrepareIOSGL, true);
|
||||
}
|
||||
|
||||
override func layoutSubviews() {
|
||||
super.layoutSubviews()
|
||||
let ps = renderPixelSize(self)
|
||||
renderSet(F.reshapePixelSize, ps)
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
import SwiftUI
|
||||
import UIKit
|
||||
|
||||
struct RenderView: UIViewRepresentable {
|
||||
func makeUIView(context: Context) -> RenderGLView {
|
||||
RenderVM.shared!.glView
|
||||
}
|
||||
|
||||
func updateUIView(_ uiView: RenderGLView, context: Context) {}
|
||||
}
|
||||
@@ -13,3 +13,7 @@ func renderPixelSize(_ view: NSView) -> [Int32] {
|
||||
let h = Int32(max(1, backing.height))
|
||||
return [w, h]
|
||||
}
|
||||
|
||||
func renderRecreateSurface(_ vm: RenderVM) {
|
||||
// No-op on macOS
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
import AppKit
|
||||
|
||||
let RENDER_OPENGL_ATTRS: [NSOpenGLPixelFormatAttribute] = [
|
||||
UInt32(NSOpenGLPFAOpenGLProfile), UInt32(NSOpenGLProfileVersion3_2Core),
|
||||
UInt32(NSOpenGLPFAColorSize), 24,
|
||||
UInt32(NSOpenGLPFAAlphaSize), 8,
|
||||
UInt32(NSOpenGLPFADepthSize), 24,
|
||||
UInt32(NSOpenGLPFADoubleBuffer),
|
||||
UInt32(NSOpenGLPFAAccelerated),
|
||||
0
|
||||
]
|
||||
@@ -3,8 +3,8 @@ import RaylibBridge
|
||||
import QuartzCore
|
||||
|
||||
func renderDeinit(_ vm: RenderVM) {
|
||||
print("ИГР renderD")
|
||||
vm.timer?.invalidate()
|
||||
vm.timer = nil
|
||||
vm.glView.openGLContext?.makeCurrentContext()
|
||||
raybriShutdown()
|
||||
}
|
||||
@@ -16,6 +16,7 @@ func renderInit(_ vm: RenderVM) {
|
||||
var swapInterval: Int32 = 1
|
||||
vm.glView.openGLContext?.setValues(&swapInterval, for: .swapInterval)
|
||||
|
||||
raybriLoadExtensions()
|
||||
raybriInit()
|
||||
}
|
||||
|
||||
@@ -15,9 +15,20 @@ final class RenderGLView: NSOpenGLView {
|
||||
fatalError("init(coder:) has not been implemented")
|
||||
}
|
||||
|
||||
deinit {
|
||||
renderSet(F.willDeinit, true)
|
||||
}
|
||||
|
||||
override func draw(_ dirtyRect: NSRect) {
|
||||
let vm = RenderVM.shared!
|
||||
if vm.didPrepareGL {
|
||||
renderDraw(vm)
|
||||
}
|
||||
}
|
||||
|
||||
override func prepareOpenGL() {
|
||||
super.prepareOpenGL()
|
||||
renderSet(F.didPrepareGL, true)
|
||||
renderSet(F.didPrepareMacGL, true)
|
||||
}
|
||||
|
||||
override func reshape() {
|
||||
@@ -33,15 +44,4 @@ final class RenderGLView: NSOpenGLView {
|
||||
let ps = renderPixelSize(self)
|
||||
renderSet(F.backingPixelSize, ps)
|
||||
}
|
||||
|
||||
override func draw(_ dirtyRect: NSRect) {
|
||||
let vm = RenderVM.shared!
|
||||
if vm.didPrepareGL {
|
||||
renderDraw(vm)
|
||||
}
|
||||
}
|
||||
|
||||
deinit {
|
||||
renderSet(F.willDeinit, true)
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,6 @@
|
||||
import AppKit
|
||||
import SwiftUI
|
||||
|
||||
/// Render view for SwiftUI
|
||||
struct RenderView: NSViewRepresentable {
|
||||
func makeNSView(context: Context) -> RenderGLView {
|
||||
RenderVM.shared!.glView
|
||||
|
||||
@@ -1,15 +1,2 @@
|
||||
import AppKit
|
||||
|
||||
let RENDER_FPS = 60.0
|
||||
|
||||
let RENDER_MODEL_CUBE = "/cube/cube.gltf"
|
||||
|
||||
let RENDER_OPENGL_ATTRS: [NSOpenGLPixelFormatAttribute] = [
|
||||
UInt32(NSOpenGLPFAOpenGLProfile), UInt32(NSOpenGLProfileVersion3_2Core),
|
||||
UInt32(NSOpenGLPFAColorSize), 24,
|
||||
UInt32(NSOpenGLPFAAlphaSize), 8,
|
||||
UInt32(NSOpenGLPFADepthSize), 24,
|
||||
UInt32(NSOpenGLPFADoubleBuffer),
|
||||
UInt32(NSOpenGLPFAAccelerated),
|
||||
0
|
||||
]
|
||||
Reference in new issue
Block a user