package net.jselby.kotgb.lwjgl.ui

import org.lwjgl.nanovg.NanoVGGL3.*
import org.lwjgl.system.MemoryUtil.NULL
import org.lwjgl.system.NativeResource

/**
 * The NanoVG handler is able to process events and render
 *  GLFW windows via NanoVG.
 */
abstract class NanoVGWindow(flags : Int
                                = NVG_STENCIL_STROKES or NVG_DEBUG or NVG_ANTIALIAS)
                : Window(), NativeResource {

    private var nvg = nvgCreate(flags)

    init {
        if (nvg == NULL) {
            error("Unable to create NanoVG instance!")
        }
    }

    /**
     * Draws a Window.
     *
     * A OpenGL context is already available and ready,
     *  and NanoVG will be automatically handled for you.
     *
     * Events should not be handled here.
     *
     * @param window The active window which will be drawn to
     * @param nvg    A instance of NanoVG, ready to be used for rendering.
     */
    abstract fun draw(nvg : Long)

    final override fun draw() = draw(nvg)

    override fun close() {
        super<Window>.close()
        nvgDelete(nvg)
    }
}