Skip to content

Instantly share code, notes, and snippets.

@alinz
Forked from mochja/main.c
Created February 26, 2019 15:45
Show Gist options
  • Select an option

  • Save alinz/4e479f13963c618b92178cf7608f75a1 to your computer and use it in GitHub Desktop.

Select an option

Save alinz/4e479f13963c618b92178cf7608f75a1 to your computer and use it in GitHub Desktop.

Revisions

  1. @mochja mochja revised this gist Apr 7, 2017. 1 changed file with 10 additions and 11 deletions.
    21 changes: 10 additions & 11 deletions main.c
    Original file line number Diff line number Diff line change
    @@ -37,10 +37,9 @@ int main(void)

    YGConfigRef config = YGConfigNew();
    YGNodeRef root = YGNodeNewWithConfig(config);
    YGNodeStyleSetWidth(root, windowWidth);
    YGNodeStyleSetHeight(root, windowHeight);
    YGNodeStyleSetFlexDirection(root, YGFlexDirectionRow);
    YGNodeStyleSetPadding(root, YGEdgeAll, 20);
    YGNodeStyleSetMargin(root, YGEdgeAll, 20);

    YGNodeRef image = YGNodeNew();
    YGNodeStyleSetWidth(image, 80);
    @@ -56,7 +55,7 @@ int main(void)
    YGNodeInsertChild(root, image, 0);
    YGNodeInsertChild(root, text, 1);

    YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
    YGNodeCalculateLayout(root, windowWidth, windowHeight, YGDirectionLTR);

    /* Render here */
    glClearColor(0.25f, 0.25f, 0.25f, 1.0f);
    @@ -72,18 +71,18 @@ int main(void)

    glColor3f(0.0, 0.0, 1.0);
    glRecti(
    YGNodeLayoutGetLeft(text),
    YGNodeLayoutGetTop(text),
    YGNodeLayoutGetLeft(text) + YGNodeLayoutGetWidth(text),
    YGNodeLayoutGetTop(text) + YGNodeLayoutGetHeight(text)
    YGNodeLayoutGetLeft(root) + YGNodeLayoutGetLeft(text),
    YGNodeLayoutGetTop(root) + YGNodeLayoutGetTop(text),
    YGNodeLayoutGetLeft(root) + YGNodeLayoutGetLeft(text) + YGNodeLayoutGetWidth(text),
    YGNodeLayoutGetTop(root) + YGNodeLayoutGetTop(text) + YGNodeLayoutGetHeight(text)
    );

    glColor3f(1.0, 0.0, 0.0);
    glRecti(
    YGNodeLayoutGetLeft(image),
    YGNodeLayoutGetTop(image),
    YGNodeLayoutGetLeft(image) + YGNodeLayoutGetWidth(image),
    YGNodeLayoutGetTop(image) + YGNodeLayoutGetHeight(image)
    YGNodeLayoutGetLeft(root) + YGNodeLayoutGetLeft(image),
    YGNodeLayoutGetTop(root) + YGNodeLayoutGetTop(image),
    YGNodeLayoutGetLeft(root) + YGNodeLayoutGetLeft(image) + YGNodeLayoutGetWidth(image),
    YGNodeLayoutGetTop(root) + YGNodeLayoutGetTop(image) + YGNodeLayoutGetHeight(image)
    );
    glFlush();

  2. @mochja mochja created this gist Apr 7, 2017.
    102 changes: 102 additions & 0 deletions main.c
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,102 @@
    #include <GLFW/glfw3.h>
    #include <yoga/Yoga.h>
    #include <stdlib.h>

    int main(void)
    {
    GLFWwindow* window;

    /* Initialize the library */
    if (!glfwInit())
    return -1;

    /* Create a windowed mode window and its OpenGL context */
    window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
    if (!window)
    {
    glfwTerminate();
    return -1;
    }

    /* Make the window's context current */
    glfwMakeContextCurrent(window);


    /* Loop until the user closes the window */
    while (!glfwWindowShouldClose(window))
    {
    int width, height;
    glfwGetFramebufferSize(window, &width, &height);
    glViewport(0, 0, width, height);

    int windowWidth, windowHeight;
    glfwGetWindowSize(window, &windowWidth, &windowHeight);

    /* Make it 2D */
    glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0, windowWidth, windowHeight, 0, 0, 1);

    YGConfigRef config = YGConfigNew();
    YGNodeRef root = YGNodeNewWithConfig(config);
    YGNodeStyleSetWidth(root, windowWidth);
    YGNodeStyleSetHeight(root, windowHeight);
    YGNodeStyleSetFlexDirection(root, YGFlexDirectionRow);
    YGNodeStyleSetPadding(root, YGEdgeAll, 20);

    YGNodeRef image = YGNodeNew();
    YGNodeStyleSetWidth(image, 80);
    YGNodeStyleSetHeight(image, 80);
    YGNodeStyleSetAlignSelf(image, YGAlignCenter);
    YGNodeStyleSetMargin(image, YGEdgeEnd, 20);

    YGNodeRef text = YGNodeNew();
    YGNodeStyleSetHeight(text, 25);
    YGNodeStyleSetAlignSelf(text, YGAlignCenter);
    YGNodeStyleSetFlexGrow(text, 1);

    YGNodeInsertChild(root, image, 0);
    YGNodeInsertChild(root, text, 1);

    YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);

    /* Render here */
    glClearColor(0.25f, 0.25f, 0.25f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT);

    glColor3f(1.0, 1.0, 1.0);
    glRecti(
    YGNodeLayoutGetLeft(root),
    YGNodeLayoutGetTop(root),
    YGNodeLayoutGetLeft(root) + YGNodeLayoutGetWidth(root),
    YGNodeLayoutGetTop(root) + YGNodeLayoutGetHeight(root)
    );

    glColor3f(0.0, 0.0, 1.0);
    glRecti(
    YGNodeLayoutGetLeft(text),
    YGNodeLayoutGetTop(text),
    YGNodeLayoutGetLeft(text) + YGNodeLayoutGetWidth(text),
    YGNodeLayoutGetTop(text) + YGNodeLayoutGetHeight(text)
    );

    glColor3f(1.0, 0.0, 0.0);
    glRecti(
    YGNodeLayoutGetLeft(image),
    YGNodeLayoutGetTop(image),
    YGNodeLayoutGetLeft(image) + YGNodeLayoutGetWidth(image),
    YGNodeLayoutGetTop(image) + YGNodeLayoutGetHeight(image)
    );
    glFlush();

    YGNodeFreeRecursive(root);
    YGConfigFree(config);

    /* Swap front and back buffers */
    glfwSwapBuffers(window);

    /* Poll for and process events */
    glfwPollEvents();
    }

    glfwTerminate();
    return 0;
    }