hi, I’m trying to convert some C code (full code i...
# kotlin-native
a
hi, I’m trying to convert some C code (full code in 🧵) to Kotlin Native
Copy code
Model model = LoadModel("resources/model.obj")
Texture2D texture = LoadTexture("resources/texture.png");
model.materials[0].maps[MATERIAL_MAP_DIFFUSE].texture = texture; // Set map diffuse texture
I can get close, but for some reason the generated cinterop Kotlin code has texture as a
val
, not a
var
Copy code
val model: CValue<Model> = LoadModel("resources/model.obj")
val texture: CValue<Texture> = LoadTexture("resources/texture.png")

model.useContents {
  // model.materials[0].maps[MATERIAL_MAP_DIFFUSE].texture = texture
  materials!![0].maps!![MATERIAL_MAP_DIFFUSE].texture = texture // ERROR val cannot be reassigned
}
any hints are appreciated!
lib.c
v
What does IDEA say the type of
.texture
is? when you have over it?
a
snip.kt
texture is a
class Texture(rawPtr: kotlinx.cinterop.NativePtr) : CStructVar
o
https://kotlinlang.slack.com/archives/C3SGXARS6/p1593990644146500 Looks relevant Seems it’s not possible directly to set, only to copy
l
The texture is stored by value. When you set the value in C, it's not just changing a reference like in Kotlin. It's copying the fields one-by-one (I'm sure some implementations may copy all the memory of the struct instead, but same effect). This goes against Kotlin's set method, so you can just copy the relevant fields (or memset if you feel like being efficient, but dangerous)
a
so you can just copy the relevant fields (or memset if you feel like being efficient, but dangerous)
how? :) The problem is that
texture
is a
val
alright, I think I figured it out. I misread the thread that Oleg posted and thought it wasn’t possible, but it suggested using the ‘place’ function this seems to work:
Copy code
val model: CValue<Model> = LoadModel("resources/model.obj")
val texture: CValue<Texture> = LoadTexture("resources/texture.png")

model.useContents {
  texture.place(materials!![0].maps!![MATERIAL_MAP_DIFFUSE].texture.ptr)
}