Skip to content

Instantly share code, notes, and snippets.

@RageshAntony
Last active May 2, 2024 10:28
Show Gist options
  • Save RageshAntony/d25232763682d2a15acbabaa1cca08bc to your computer and use it in GitHub Desktop.
Save RageshAntony/d25232763682d2a15acbabaa1cca08bc to your computer and use it in GitHub Desktop.
Kotlin/Native C Interop - Usage in Kotlin class
headers = nuklear.h App.h nuklear_glfw_gl2.h nk_defines.h /usr/include/GLFW/glfw3.h /usr/include/GLFW/glfw3native.h
compiler-options = -framework OpenGl
package = glfw
linkerOpts.osx = -L/opt/local/lib -L/usr/local/lib -lglfw
linkerOpts.linux = -L/usr/lib64 -L/usr/lib/x86_64-linux-gnu -lglfw -lGLU -lGL -lglut
linkerOpts.mingw = -lglfw
plugins {
kotlin("multiplatform") version "1.5.31"
}
group = "me.sudharsanarajalingam"
version = "1.0-SNAPSHOT"
repositories {
mavenCentral()
}
allprojects {
configurations {
all {
resolutionStrategy {
force("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.5.2-native-mt")
}
}
}
}
kotlin {
val hostOs = System.getProperty("os.name")
val isMingwX64 = hostOs.startsWith("Windows")
val nativeTarget = when {
hostOs == "Mac OS X" -> macosX64("native")
hostOs == "Linux" -> linuxX64("native")
isMingwX64 -> mingwX64("native")
else -> throw GradleException("Host OS is not supported in Kotlin/Native.")
}
// val main by compilations.getting
// val interop by main.cinterops.creating
nativeTarget.apply {
val main by compilations.getting
main.cinterops.create("applib") {
defFile(project.file("src/nativeInterop/cinterop/App.def"))
packageName ("com.myapp.applib")
compilerOpts ("-I/src/nativeInterop/cinterop")
linkerOpts ("-I/src/nativeInterop/cinterop")
// 3
includeDirs{
// allHeaders ("src/nativeInterop/cinterop","/Library/Developer/glfw/include/GLFW")
allHeaders("/usr/include","src/nativeInterop/cinterop")
}
}
binaries {
executable {
entryPoint = "main"
}
}
}
sourceSets {
val nativeMain by getting {
dependencies {
implementation("io.ktor:ktor-client-curl:1.6.3")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.5.2-native-mt")
// df
}
}
val nativeTest by getting
}
}
import com.myapp.applib.*
import kotlinx.cinterop.*
import kotlinx.cinterop.nativeHeap.alloc
import platform.posix.exit
import platform.posix.fprintf
import platform.posix.stdout
import kotlinx.cinterop.*
import interop.*
fun main() {
var ctx: CPointer<nk_context>? = null
var bg: nk_colorf
val width = 0
val height = 0
var glfwInitialized = glfwInit()
if (glfwInitialized == 0) {
println("Failed to initialize GLFW")
return
}
var window = glfwCreateWindow(640, 480, "OpenGL on Kotlin! Wow!", null, null);
if (window == null)
{
println("Failed to create GLFW window")
glfwTerminate()
return
}
println("hello world! glfw initialized? = $glfwInitialized, window pointer = $window")
glfwMakeContextCurrent(window)
ctx = nk_glfw3_init(window, NK_GLFW3_INSTALL_CALLBACKS)
while(glfwWindowShouldClose(window) == 0)
{
glClear(GL_COLOR_BUFFER_BIT)
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwTerminate()
}
#include <stdio.h>
#define NK_INCLUDE_FIXED_TYPES
#define NK_INCLUDE_STANDARD_IO
#define NK_INCLUDE_STANDARD_VARARGS
#define NK_INCLUDE_DEFAULT_ALLOCATOR
#define NK_INCLUDE_VERTEX_BUFFER_OUTPUT
#define NK_INCLUDE_FONT_BAKING
#define NK_INCLUDE_DEFAULT_FONT
#define NK_IMPLEMENTATION
#define NK_GLFW_GL2_IMPLEMENTATION
#define NK_KEYSTATE_BASED_INPUT
static void error_callback(int e, const char *d)
{
printf("Error %d: %s\n", e, d);
}
https://github.com/Immediate-Mode-UI/Nuklear/tree/master/demo/glfw_opengl2
https://github.com/Immediate-Mode-UI/Nuklear/blob/master/nuklear.h
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment