Using images in LWJGL 3 [solved]

Started by Misterpin, March 08, 2018, 15:16:23

Previous topic - Next topic

Misterpin

Good afternoon!
I notice that each class has a constructor called Testclass(ByteBuffer container). And how can I create NkImage, STBImage, GLFWImage?
And how can I create NkContext instance?
Thanks!

spasi

First of all, read Memory management in LWJGL 3.

The methods that struct classes support for creating new instances (using GLFWImage as an example):

// Constructor, struct backed by whatever memory is backing the specified container
GLFWImage(ByteBuffer container)

// Uses memAlloc to allocate an uninitialized struct. The struct must be freed explicitly.
GLFWImage GLFWImage.malloc()
// Uses memCalloc to allocate a zero-filled struct. The struct must be freed explicitly.
GLFWImage GLFWImage.calloc()
// Uses ByteBuffer.allocateDirect to allocate a zero-filled struct
GLFWImage GLFWImage.create()
// Returns a struct that wraps the specified pointer address (unsafe)
GLFWImage GLFWImage.create(long address)

// Same as above, but an array of structs is returned
GLFWImage.Buffer GLFWImage.malloc(int capacity)
GLFWImage.Buffer GLFWImage.calloc(int capacity)
GLFWImage.Buffer GLFWImage.create(int capacity)
GLFWImage.Buffer GLFWImage.create(long address, int capacity)

// Same as above, but the struct is backed by "stack" memory. If the current stack frame goes away, the struct goes away automatically.
GLFWImage GLFWImage.mallocStack(MemoryStack stack)
GLFWImage GLFWImage.callocStack(MemoryStack stack)
GLFWImage.Buffer GLFWImage.mallocStack(int capacity, MemoryStack stack)
GLFWImage.Buffer GLFWImage.callocStack(int capacity, MemoryStack stack)


You may use any of the above, depending on the use-case. It helps to have a good mental model of the methods that allocate memory. In terms of performance, from fastest to slowest:

- mallocStack
- callocStack
- malloc
- calloc
- create

And in terms of convenience, from more easiest to hardest:

- create (GCed automatically)
- malloc/callocStack (use the MemoryStack API to manage "stack frames", usually with try-with-resources blocks)
- malloc/calloc (must call .free() explicitly when the memory is no longer used)