vurtun / nuklear

A single-header ANSI C gui library
13.67k stars 1.11k forks source link

Run Error on VS2013 #296

Open AlexiaChen opened 7 years ago

AlexiaChen commented 7 years ago

My Code as follows, thats just use official demo calculator:

#define NK_IMPLEMENTATION
#define NK_INCLUDE_DEFAULT_FONT
#define NK_INCLUDE_DEFAULT_ALLOCATOR
#define NK_INCLUDE_VERTEX_BUFFER_OUTPUT
#include "nuklear.h"

#include <malloc.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>

#define MAX_MEMORY 1024*1024*20  // 20MB

static void
calculator(struct nk_context *ctx)
{
    if (nk_begin(ctx, "Calculator", nk_rect(10, 10, 180, 250),
        NK_WINDOW_BORDER|NK_WINDOW_NO_SCROLLBAR|NK_WINDOW_MOVABLE))
    {
        static int set = 0, prev = 0, op = 0;
        static const char numbers[] = "789456123";
        static const char ops[] = "+-*/";
        static double a = 0, b = 0;
        static double *current = &a;

        size_t i = 0;
        int solve = 0;
        {

            int len; char buffer[256];
        nk_layout_row_dynamic(ctx, 35, 1);
        len = _snprintf(buffer, 256, "%.2f", *current);
        nk_edit_string(ctx, NK_EDIT_SIMPLE, buffer, &len, 255, nk_filter_float);
        buffer[len] = 0;
        *current = atof(buffer);}

        nk_layout_row_dynamic(ctx, 35, 4);
        for (i = 0; i < 16; ++i) {
            if (i >= 12 && i < 15) {
                if (i > 12) continue;
                if (nk_button_label(ctx, "C")) {
                    a = b = op = 0; current = &a; set = 0;
                } if (nk_button_label(ctx, "0")) {
                    *current = *current*10.0f; set = 0;
                } if (nk_button_label(ctx, "=")) {
                    solve = 1; prev = op; op = 0;
                }
            } else if (((i+1) % 4)) {
                if (nk_button_text(ctx, &numbers[(i/4)*3+i%4], 1)) {
                    *current = *current * 10.0f + numbers[(i/4)*3+i%4] - '0';
                    set = 0;
                }
            } else if (nk_button_text(ctx, &ops[i/4], 1)) {
                if (!set) {
                    if (current != &b) {
                        current = &b;
                    } else {
                        prev = op;
                        solve = 1;
                    }
                }
                op = ops[i/4];
                set = 1;
            }
        }
        if (solve) {
            if (prev == '+') a = a + b;
            if (prev == '-') a = a - b;
            if (prev == '*') a = a * b;
            if (prev == '/') a = a / b;
            current = &a;
            if (set) current = &b;
            b = 0; set = 0;
        }
    }
    nk_end(ctx);
}

int main()
{

    struct nk_context ctx;
    struct nk_user_font font;

    nk_init_default(&ctx, NULL);

    calculator(&ctx);
    return 0;
}

I found the library need a font to load by function nk_init_default, But How Can I use struct nk_user_font to load a specific font in folder extra_font? there are no detailed documentations. and run-welled demo.

Edit(vurtun): fixed code formatting

jacmoe commented 7 years ago

The actual demos are in the subdirectories of the demo directory :) There are 10 demos in a dir each in the demo dir.

You are not setting up your graphics and you are not looping. But check out the demo code to see how it's done.

vurtun commented 7 years ago

@AlexiaChen like @jacmoe already said so far you only have the platform independent part but you are missing the platform depended part. Basically I would suggest first giving the platform demo of choice inside the /demo folder a look. Each demo optionally includes the calculator and provides a basic overview on how to bind nuklear to a platform. If you get one of my demos to run you basically just have to add your specific UI code.

AlexiaChen commented 7 years ago

@vurtun Thanks, I kooked through the demo directory, and not checked GUI sub directory,sorry. Let me see How it works.