Ejemplo n.º 1
0
INT WINAPI
WinMain(HINSTANCE hInstance, HINSTANCE prev, LPSTR lpCmdLine, int shown)
{
    LARGE_INTEGER freq;
    long long start;
    long long dt;

    /* GUI */
    struct gui_input in;
    struct gui_font font;
    struct demo_gui gui;

    /* Window */
    QueryPerformanceFrequency(&freq);
    xw.wc.style = CS_HREDRAW|CS_VREDRAW;
    xw.wc.lpfnWndProc = wnd_proc;
    xw.wc.hInstance = hInstance;
    xw.wc.lpszClassName = "GUI";
    RegisterClass(&xw.wc);
    xw.hWnd = CreateWindowEx(
        0, xw.wc.lpszClassName, "Demo",
        WS_OVERLAPPED|WS_CAPTION|WS_SYSMENU|WS_MINIMIZEBOX|WS_MAXIMIZEBOX|WS_VISIBLE,
        CW_USEDEFAULT, CW_USEDEFAULT,
        WINDOW_WIDTH, WINDOW_HEIGHT,
        0, 0, hInstance, 0);

    xw.hdc = GetDC(xw.hWnd);
    GetClientRect(xw.hWnd, &xw.rect);
    xw.backbuffer = surface_new(xw.hdc, xw.rect.right, xw.rect.bottom);
    xw.font = font_new(xw.hdc, "Times New Roman", 14);
    xw.width = xw.rect.right;
    xw.height = xw.rect.bottom;

    /* GUI */
    memset(&in, 0, sizeof in);
    memset(&gui, 0, sizeof gui);
    font.userdata.ptr = &xw;
    font.height = (gui_float)xw.font->height;
    font.width = font_get_text_width;
    gui.running = gui_true;
    gui.memory = malloc(MAX_MEMORY);
    init_demo(&gui, &font);

    while (gui.running && !quit) {
        /* Input */
        MSG msg;
        start = timestamp(freq);
        gui_input_begin(&in);
        while (PeekMessage(&msg, xw.hWnd, 0, 0, PM_REMOVE)) {
            if (msg.message == WM_KEYDOWN) key(&in, &msg, gui_true);
            else if (msg.message == WM_KEYUP) key(&in, &msg, gui_false);
            else if (msg.message == WM_LBUTTONDOWN) btn(&in, &msg, gui_true);
            else if (msg.message == WM_LBUTTONUP) btn(&in, &msg, gui_false);
            else if (msg.message == WM_MOUSEMOVE) motion(&in, &msg);
            else if (msg.message == WM_CHAR) text(&in, &msg);
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
        gui_input_end(&in);

        /* GUI */
        run_demo(&gui, &in);

        /* Draw */
        surface_begin(xw.backbuffer);
        surface_clear(xw.backbuffer, 100, 100, 100);
        draw(xw.backbuffer, &gui.stack);
        surface_end(xw.backbuffer, xw.hdc);

        /* Timing */
        dt = timestamp(freq) - start;
        if (dt < DTIME) Sleep(DTIME - dt);
    }

    free(gui.memory);
    font_del(xw.font);
    surface_del(xw.backbuffer);
    ReleaseDC(xw.hWnd, xw.hdc);
    return 0;
}
Ejemplo n.º 2
0
int
main(int argc, char *argv[])
{
    long dt;
    long started;
    XWindow xw;
    struct demo_gui gui;

    /* Platform */
    UNUSED(argc); UNUSED(argv);
    memset(&xw, 0, sizeof xw);
    xw.dpy = XOpenDisplay(NULL);
    xw.root = DefaultRootWindow(xw.dpy);
    xw.screen = XDefaultScreen(xw.dpy);
    xw.vis = XDefaultVisual(xw.dpy, xw.screen);
    xw.cmap = XCreateColormap(xw.dpy,xw.root,xw.vis,AllocNone);
    xw.swa.colormap = xw.cmap;
    xw.swa.event_mask =
        ExposureMask | KeyPressMask | KeyReleaseMask |
        ButtonPress | ButtonReleaseMask| ButtonMotionMask |
        Button1MotionMask | Button3MotionMask | Button4MotionMask | Button5MotionMask|
        PointerMotionMask;
    xw.win = XCreateWindow(xw.dpy, xw.root, 0, 0, WINDOW_WIDTH, WINDOW_HEIGHT, 0,
        XDefaultDepth(xw.dpy, xw.screen), InputOutput,
        xw.vis, CWEventMask | CWColormap, &xw.swa);
    XStoreName(xw.dpy, xw.win, "X11");
    XMapWindow(xw.dpy, xw.win);
    XGetWindowAttributes(xw.dpy, xw.win, &xw.attr);
    xw.width = (unsigned int)xw.attr.width;
    xw.height = (unsigned int)xw.attr.height;
    xw.surf = surface_create(xw.dpy, xw.screen, xw.win, xw.width, xw.height);
    xw.font = font_create(xw.dpy, "fixed");

    /* GUI */
    memset(&gui, 0, sizeof gui);
    zr_command_queue_init_fixed(&gui.queue, calloc(MAX_MEMORY, 1), MAX_MEMORY);
    gui.font.userdata = zr_handle_ptr(xw.font);
    gui.font.height = (zr_float)xw.font->height;
    gui.font.width = font_get_text_width;
    init_demo(&gui);

    while (gui.running) {
        /* Input */
        XEvent evt;
        started = timestamp();
        zr_input_begin(&gui.input);
        while (XCheckWindowEvent(xw.dpy, xw.win, xw.swa.event_mask, &evt)) {
            if (evt.type == KeyPress)
                key(&xw, &gui.input, &evt, zr_true);
            else if (evt.type == KeyRelease) key(&xw, &gui.input, &evt, zr_false);
            else if (evt.type == ButtonPress) btn(&gui.input, &evt, zr_true);
            else if (evt.type == ButtonRelease) btn(&gui.input, &evt, zr_false);
            else if (evt.type == MotionNotify) motion(&gui.input, &evt);
            else if (evt.type == Expose || evt.type == ConfigureNotify)
                resize(&xw, xw.surf);
        }
        zr_input_end(&gui.input);

        /* GUI */
        run_demo(&gui);

        /* Draw */
        XClearWindow(xw.dpy, xw.win);
        surface_clear(xw.surf, 0x00646464);
        draw(xw.surf, &gui.queue);
        surface_blit(xw.win, xw.surf, xw.width, xw.height);
        XFlush(xw.dpy);

        /* Timing */
        dt = timestamp() - started;
        if (dt < DTIME)
            sleep_for(DTIME - dt);
    }

    free(zr_buffer_memory(&gui.queue.buffer));
    font_del(xw.dpy, xw.font);
    surface_del(xw.surf);
    XUnmapWindow(xw.dpy, xw.win);
    XFreeColormap(xw.dpy, xw.cmap);
    XDestroyWindow(xw.dpy, xw.win);
    XCloseDisplay(xw.dpy);
    return 0;
}