Search in sources :

Example 6 with NvgRenderer

use of com.spinyowl.legui.system.renderer.nvg.NvgRenderer in project legui by SpinyOwl.

the class Demo method render.

private void render() {
    glfwMakeContextCurrent(window);
    glCapabilities = createCapabilities();
    glfwSwapInterval(0);
    renderer = new NvgRenderer();
    renderer.initialize();
    glfwMakeContextCurrent(window);
    setCapabilities(glCapabilities);
    glfwSwapInterval(0);
    while (running) {
        try {
            context.updateGlfwWindow();
            Vector2i framebufferSize = context.getFramebufferSize();
            glClearColor(1, 1, 1, 1);
            glViewport(0, 0, framebufferSize.x, framebufferSize.y);
            glClear(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
            renderer.render(frame, context);
            glfwSwapBuffers(window);
            // update system. could be moved for example to game loop.
            update();
            // When everything done we need to relayout components.
            LayoutManager.getInstance().layout(frame);
            // also we need to run animations
            AnimatorProvider.getAnimator().runAnimations();
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }
    renderer.destroy();
}
Also used : Vector2i(org.joml.Vector2i) NvgRenderer(com.spinyowl.legui.system.renderer.nvg.NvgRenderer)

Example 7 with NvgRenderer

use of com.spinyowl.legui.system.renderer.nvg.NvgRenderer in project legui by SpinyOwl.

the class SingleClassExampleImageRendering method main.

public static void main(String[] args) throws IOException {
    System.setProperty("joml.nounsafe", Boolean.TRUE.toString());
    System.setProperty("java.awt.headless", Boolean.TRUE.toString());
    if (!GLFW.glfwInit()) {
        throw new RuntimeException("Can't initialize GLFW");
    }
    long window = glfwCreateWindow(WIDTH, HEIGHT, "Single Class Example", NULL, NULL);
    glfwShowWindow(window);
    glfwMakeContextCurrent(window);
    GL.createCapabilities();
    glfwSwapInterval(0);
    // Firstly we need to create frame component for window.
    Frame frame = new Frame(WIDTH, HEIGHT);
    // we can add elements here or on the fly
    createGuiElements(frame);
    // We need to create legui context which shared by renderer and event processor.
    // Also we need to pass event processor for ui events such as click on component, key typing and etc.
    Context context = new Context(window);
    // We need to create callback keeper which will hold all of callbacks.
    // These callbacks will be used in initialization of system event processor
    // (will be added callbacks which will push system events to event queue and after that processed by SystemEventProcessor)
    CallbackKeeper keeper = new DefaultCallbackKeeper();
    // register callbacks for window. Note: all previously binded callbacks will be unbinded.
    CallbackKeeper.registerCallbacks(window, keeper);
    GLFWKeyCallbackI glfwKeyCallbackI = (w1, key, code, action, mods) -> running = !(key == GLFW_KEY_ESCAPE && action != GLFW_RELEASE);
    GLFWWindowCloseCallbackI glfwWindowCloseCallbackI = w -> running = false;
    // if we want to create some callbacks for system events you should create and put them to keeper
    // 
    // Wrong:
    // glfwSetKeyCallback(window, glfwKeyCallbackI);
    // glfwSetWindowCloseCallback(window, glfwWindowCloseCallbackI);
    // 
    // Right:
    keeper.getChainKeyCallback().add(glfwKeyCallbackI);
    keeper.getChainWindowCloseCallback().add(glfwWindowCloseCallbackI);
    // Event processor for system events. System events should be processed and translated to gui events.
    SystemEventProcessor systemEventProcessor = new SystemEventProcessorImpl();
    SystemEventProcessor.addDefaultCallbacks(keeper, systemEventProcessor);
    // Also we need to create renderer provider
    // and create renderer which will render our ui components.
    Renderer renderer = new NvgRenderer();
    // Initialization finished, so we can start render loop.
    running = true;
    // Everything can be done in one thread as well as in separated threads.
    // Here is one-thread example.
    // before render loop we need to initialize renderer
    renderer.initialize();
    while (running) {
        // Before rendering we need to update context with window size and window framebuffer size
        // {
        // int[] windowWidth = {0}, windowHeight = {0};
        // GLFW.glfwGetWindowSize(window, windowWidth, windowHeight);
        // int[] frameBufferWidth = {0}, frameBufferHeight = {0};
        // GLFW.glfwGetFramebufferSize(window, frameBufferWidth, frameBufferHeight);
        // int[] xpos = {0}, ypos = {0};
        // GLFW.glfwGetWindowPos(window, xpos, ypos);
        // double[] mx = {0}, my = {0};
        // GLFW.glfwGetCursorPos(window, mx, my);
        // 
        // context.update(windowWidth[0], windowHeight[0],
        // frameBufferWidth[0], frameBufferHeight[0],
        // xpos[0], ypos[0],
        // mx[0], my[0]
        // );
        // }
        // Also we can do it in one line
        context.updateGlfwWindow();
        Vector2i windowSize = context.getFramebufferSize();
        glClearColor(1, 1, 1, 1);
        // Set viewport size
        glViewport(0, 0, windowSize.x, windowSize.y);
        // Clear screen
        glClear(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
        // We need to relayout components.
        LayoutManager.getInstance().layout(frame);
        // render frame
        renderer.render(frame, context);
        update();
        // poll events to callbacks
        glfwPollEvents();
        glfwSwapBuffers(window);
        // Now we need to process events. Firstly we need to process system events.
        systemEventProcessor.processEvents(frame, context);
        // When system events are translated to GUI events we need to process them.
        // This event processor calls listeners added to ui components
        EventProcessorProvider.getInstance().processEvents();
        // Run animations. Should be also called cause some components use animations for updating state.
        AnimatorProvider.getAnimator().runAnimations();
    }
    // And when rendering is ended we need to destroy renderer
    renderer.destroy();
    glfwDestroyWindow(window);
    glfwTerminate();
}
Also used : Context(com.spinyowl.legui.system.context.Context) GLFW.glfwDestroyWindow(org.lwjgl.glfw.GLFW.glfwDestroyWindow) GLFW.glfwSwapBuffers(org.lwjgl.glfw.GLFW.glfwSwapBuffers) GLFW_RELEASE(org.lwjgl.glfw.GLFW.GLFW_RELEASE) GLFW.glfwCreateWindow(org.lwjgl.glfw.GLFW.glfwCreateWindow) GLFW.glfwPollEvents(org.lwjgl.glfw.GLFW.glfwPollEvents) ImageView(com.spinyowl.legui.component.ImageView) GLFW.glfwSwapInterval(org.lwjgl.glfw.GLFW.glfwSwapInterval) GL11.glViewport(org.lwjgl.opengl.GL11.glViewport) ByteBuffer(java.nio.ByteBuffer) NULL(org.lwjgl.system.MemoryUtil.NULL) GL_COLOR_BUFFER_BIT(org.lwjgl.opengl.GL11.GL_COLOR_BUFFER_BIT) Renderer(com.spinyowl.legui.system.renderer.Renderer) Style(com.spinyowl.legui.style.Style) GLFWWindowCloseCallbackI(org.lwjgl.glfw.GLFWWindowCloseCallbackI) CallbackKeeper(com.spinyowl.legui.system.context.CallbackKeeper) GL11.glClear(org.lwjgl.opengl.GL11.glClear) GLFW.glfwShowWindow(org.lwjgl.glfw.GLFW.glfwShowWindow) LayoutManager(com.spinyowl.legui.system.layout.LayoutManager) NvgRenderer(com.spinyowl.legui.system.renderer.nvg.NvgRenderer) GL_STENCIL_BUFFER_BIT(org.lwjgl.opengl.GL11.GL_STENCIL_BUFFER_BIT) Context(com.spinyowl.legui.system.context.Context) BufferedImageRGBA(com.spinyowl.legui.image.BufferedImageRGBA) ColorConstants(com.spinyowl.legui.style.color.ColorConstants) Frame(com.spinyowl.legui.component.Frame) SystemEventProcessorImpl(com.spinyowl.legui.system.handler.processor.SystemEventProcessorImpl) GLFW.glfwTerminate(org.lwjgl.glfw.GLFW.glfwTerminate) GLFWKeyCallbackI(org.lwjgl.glfw.GLFWKeyCallbackI) GL11.glClearColor(org.lwjgl.opengl.GL11.glClearColor) DefaultCallbackKeeper(com.spinyowl.legui.system.context.DefaultCallbackKeeper) IOException(java.io.IOException) GLFW(org.lwjgl.glfw.GLFW) AnimatorProvider(com.spinyowl.legui.animation.AnimatorProvider) GLFW.glfwMakeContextCurrent(org.lwjgl.glfw.GLFW.glfwMakeContextCurrent) SystemEventProcessor(com.spinyowl.legui.system.handler.processor.SystemEventProcessor) EventProcessorProvider(com.spinyowl.legui.listener.processor.EventProcessorProvider) Vector2i(org.joml.Vector2i) Widget(com.spinyowl.legui.component.Widget) GLFW_KEY_ESCAPE(org.lwjgl.glfw.GLFW.GLFW_KEY_ESCAPE) GL(org.lwjgl.opengl.GL) GLFWKeyCallbackI(org.lwjgl.glfw.GLFWKeyCallbackI) Frame(com.spinyowl.legui.component.Frame) CallbackKeeper(com.spinyowl.legui.system.context.CallbackKeeper) DefaultCallbackKeeper(com.spinyowl.legui.system.context.DefaultCallbackKeeper) NvgRenderer(com.spinyowl.legui.system.renderer.nvg.NvgRenderer) GLFWWindowCloseCallbackI(org.lwjgl.glfw.GLFWWindowCloseCallbackI) DefaultCallbackKeeper(com.spinyowl.legui.system.context.DefaultCallbackKeeper) SystemEventProcessorImpl(com.spinyowl.legui.system.handler.processor.SystemEventProcessorImpl) Renderer(com.spinyowl.legui.system.renderer.Renderer) NvgRenderer(com.spinyowl.legui.system.renderer.nvg.NvgRenderer) Vector2i(org.joml.Vector2i) SystemEventProcessor(com.spinyowl.legui.system.handler.processor.SystemEventProcessor)

Example 8 with NvgRenderer

use of com.spinyowl.legui.system.renderer.nvg.NvgRenderer in project Merlin-GUI by Bubblie01.

the class MerlinWindow method windowInit.

public static void windowInit(Window window) {
    MinecraftClient client = MinecraftClient.getInstance();
    int width = window.getWidth();
    int height = window.getHeight();
    minecraftContext = window.getHandle();
    renderer = new NvgRenderer();
    renderer.initialize();
    frame = new Frame(width, height);
    callbackKeeper = new DefaultCallbackKeeper();
    Widget widget = new Widget("Regular Window", 200, 50, 200, 200);
    frame.getContainer().add(widget);
    context = new Context(minecraftContext);
    systemEventProcessor = new SystemEventProcessorImpl();
}
Also used : Context(com.spinyowl.legui.system.context.Context) Frame(com.spinyowl.legui.component.Frame) MinecraftClient(net.minecraft.client.MinecraftClient) DefaultCallbackKeeper(com.spinyowl.legui.system.context.DefaultCallbackKeeper) SystemEventProcessorImpl(com.spinyowl.legui.system.handler.processor.SystemEventProcessorImpl) Widget(com.spinyowl.legui.component.Widget) NvgRenderer(com.spinyowl.legui.system.renderer.nvg.NvgRenderer)

Aggregations

NvgRenderer (com.spinyowl.legui.system.renderer.nvg.NvgRenderer)8 Vector2i (org.joml.Vector2i)7 Frame (com.spinyowl.legui.component.Frame)6 Context (com.spinyowl.legui.system.context.Context)6 DefaultCallbackKeeper (com.spinyowl.legui.system.context.DefaultCallbackKeeper)6 SystemEventProcessorImpl (com.spinyowl.legui.system.handler.processor.SystemEventProcessorImpl)6 Widget (com.spinyowl.legui.component.Widget)5 EventProcessorProvider (com.spinyowl.legui.listener.processor.EventProcessorProvider)5 CallbackKeeper (com.spinyowl.legui.system.context.CallbackKeeper)5 SystemEventProcessor (com.spinyowl.legui.system.handler.processor.SystemEventProcessor)5 LayoutManager (com.spinyowl.legui.system.layout.LayoutManager)5 Renderer (com.spinyowl.legui.system.renderer.Renderer)5 GLFW (org.lwjgl.glfw.GLFW)5 GLFW_KEY_ESCAPE (org.lwjgl.glfw.GLFW.GLFW_KEY_ESCAPE)5 GLFW_RELEASE (org.lwjgl.glfw.GLFW.GLFW_RELEASE)5 GLFW.glfwCreateWindow (org.lwjgl.glfw.GLFW.glfwCreateWindow)5 GLFW.glfwDestroyWindow (org.lwjgl.glfw.GLFW.glfwDestroyWindow)5 GLFW.glfwMakeContextCurrent (org.lwjgl.glfw.GLFW.glfwMakeContextCurrent)5 GLFW.glfwPollEvents (org.lwjgl.glfw.GLFW.glfwPollEvents)5 GLFW.glfwShowWindow (org.lwjgl.glfw.GLFW.glfwShowWindow)5