aboutsummaryrefslogtreecommitdiffstats
path: root/3rdparty/imgui/examples/example_glfw_opengl2/main.cpp
blob: e6e53c8229e0aa7e81e6cdcb6240aa3307a7880f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// dear imgui: standalone example application for GLFW + OpenGL2, using legacy fixed pipeline
// If you are new to dear imgui, see examples/README.txt and documentation at the top of imgui.cpp.
// (GLFW is a cross-platform general purpose library for handling windows, inputs, OpenGL/Vulkan graphics context creation, etc.)

// **DO NOT USE THIS CODE IF YOUR CODE/ENGINE IS USING MODERN OPENGL (SHADERS, VBO, VAO, etc.)**
// **Prefer using the code in the example_glfw_opengl2/ folder**
// See imgui_impl_glfw.cpp for details.

#include "imgui.h"
#include "imgui_impl_glfw.h"
#include "imgui_impl_opengl2.h"
#include <stdio.h>
#include <GLFW/glfw3.h>

static void glfw_error_callback(int error, const char* description)
{
    fprintf(stderr, "Glfw Error %d: %s\n", error, description);
}

int main(int, char**)
{
    // Setup window
    glfwSetErrorCallback(glfw_error_callback);
    if (!glfwInit())
        return 1;
    GLFWwindow* window = glfwCreateWindow(1280, 720, "Dear ImGui GLFW+OpenGL2 example", NULL, NULL);
    if (window == NULL)
        return 1;
    glfwMakeContextCurrent(window);
    glfwSwapInterval(1); // Enable vsync

    // Setup Dear ImGui binding
    IMGUI_CHECKVERSION();
    ImGui::CreateContext();
    ImGuiIO& io = ImGui::GetIO(); (void)io;
    //io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;  // Enable Keyboard Controls
    //io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad;   // Enable Gamepad Controls

    ImGui_ImplGlfw_InitForOpenGL(window, true);
    ImGui_ImplOpenGL2_Init();

    // Setup style
    ImGui::StyleColorsDark();
    //ImGui::StyleColorsClassic();

    // Load Fonts
    // - If no fonts are loaded, dear imgui will use the default font. You can also load multiple fonts and use ImGui::PushFont()/PopFont() to select them. 
    // - AddFontFromFileTTF() will return the ImFont* so you can store it if you need to select the font among multiple. 
    // - If the file cannot be loaded, the function will return NULL. Please handle those errors in your application (e.g. use an assertion, or display an error and quit).
    // - The fonts will be rasterized at a given size (w/ oversampling) and stored into a texture when calling ImFontAtlas::Build()/GetTexDataAsXXXX(), which ImGui_ImplXXXX_NewFrame below will call.
    // - Read 'misc/fonts/README.txt' for more instructions and details.
    // - Remember that in C/C++ if you want to include a backslash \ in a string literal you need to write a double backslash \\ !
    //io.Fonts->AddFontDefault();
    //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Roboto-Medium.ttf", 16.0f);
    //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Cousine-Regular.ttf", 15.0f);
    //io.Fonts->AddFontFromFileTTF("../../misc/fonts/DroidSans.ttf", 16.0f);
    //io.Fonts->AddFontFromFileTTF("../../misc/fonts/ProggyTiny.ttf", 10.0f);
    //ImFont* font = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf", 18.0f, NULL, io.Fonts->GetGlyphRangesJapanese());
    //IM_ASSERT(font != NULL);

    bool show_demo_window = true;
    bool show_another_window = false;
    ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f);

    // Main loop
    while (!glfwWindowShouldClose(window))
    {
        // Poll and handle events (inputs, window resize, etc.)
        // You can read the io.WantCaptureMouse, io.WantCaptureKeyboard flags to tell if dear imgui wants to use your inputs.
        // - When io.WantCaptureMouse is true, do not dispatch mouse input data to your main application.
        // - When io.WantCaptureKeyboard is true, do not dispatch keyboard input data to your main application.
        // Generally you may always pass all inputs to dear imgui, and hide them from your application based on those two flags.
        glfwPollEvents();

        // Start the Dear ImGui frame
        ImGui_ImplOpenGL2_NewFrame();
        ImGui_ImplGlfw_NewFrame();
        ImGui::NewFrame();

        // 1. Show the big demo window (Most of the sample code is in ImGui::ShowDemoWindow()! You can browse its code to learn more about Dear ImGui!).
        if (show_demo_window)
            ImGui::ShowDemoWindow(&show_demo_window);

        // 2. Show a simple window that we create ourselves. We use a Begin/End pair to created a named window.
        {
            static float f = 0.0f;
            static int counter = 0;

            ImGui::Begin("Hello, world!");                          // Create a window called "Hello, world!" and append into it.

            ImGui::Text("This is some useful text.");               // Display some text (you can use a format strings too)
            ImGui::Checkbox("Demo Window", &show_demo_window);      // Edit bools storing our window open/close state
            ImGui::Checkbox("Another Window", &show_another_window);

            ImGui::SliderFloat("float", &f, 0.0f, 1.0f);            // Edit 1 float using a slider from 0.0f to 1.0f    
            ImGui::ColorEdit3("clear color", (float*)&clear_color); // Edit 3 floats representing a color

            if (ImGui::Button("Button"))                            // Buttons return true when clicked (most widgets return true when edited/activated)
                counter++;
            ImGui::SameLine();
            ImGui::Text("counter = %d", counter);

            ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate);
            ImGui::End();
        }

        // 3. Show another simple window.
        if (show_another_window)
        {
            ImGui::Begin("Another Window", &show_another_window);   // Pass a pointer to our bool variable (the window will have a closing button that will clear the bool when clicked)
            ImGui::Text("Hello from another window!");
            if (ImGui::Button("Close Me"))
                show_another_window = false;
            ImGui::End();
        }

        // Rendering
        ImGui::Render();
        int display_w, display_h;
        glfwGetFramebufferSize(window, &display_w, &display_h);
        glViewport(0, 0, display_w, display_h);
        glClearColor(clear_color.x, clear_color.y, clear_color.z, clear_color.w);
        glClear(GL_COLOR_BUFFER_BIT);
        //glUseProgram(0); // You may want this if using this code in an OpenGL 3+ context where shaders may be bound, but prefer using the GL3+ code.
        ImGui_ImplOpenGL2_RenderDrawData(ImGui::GetDrawData());

        glfwMakeContextCurrent(window);
        glfwSwapBuffers(window);
    }

    // Cleanup
    ImGui_ImplOpenGL2_Shutdown();
    ImGui_ImplGlfw_Shutdown();
    ImGui::DestroyContext();

    glfwDestroyWindow(window);
    glfwTerminate();

    return 0;
}
s="k">return NULL; memset(d, 0, sizeof(*d)); d->domain_id = domid; atomic_set(&d->refcnt, 1); spin_lock_init(&d->big_lock); spin_lock_init(&d->page_alloc_lock); spin_lock_init(&d->pause_lock); INIT_LIST_HEAD(&d->page_list); INIT_LIST_HEAD(&d->xenpage_list); return d; } void free_domain(struct domain *d) { struct vcpu *v; int i; sched_destroy_domain(d); for ( i = MAX_VIRT_CPUS-1; i >= 0; i-- ) if ( (v = d->vcpu[i]) != NULL ) free_vcpu_struct(v); xfree(d); } struct vcpu *alloc_vcpu( struct domain *d, unsigned int vcpu_id, unsigned int cpu_id) { struct vcpu *v; BUG_ON(d->vcpu[vcpu_id] != NULL); if ( (v = alloc_vcpu_struct(d, vcpu_id)) == NULL ) return NULL; v->domain = d; v->vcpu_id = vcpu_id; v->vcpu_info = &d->shared_info->vcpu_info[vcpu_id]; spin_lock_init(&v->pause_lock); v->runstate.state = is_idle_vcpu(v) ? RUNSTATE_running : RUNSTATE_offline; v->runstate.state_entry_time = NOW(); if ( (vcpu_id != 0) && !is_idle_domain(d) ) set_bit(_VCPUF_down, &v->vcpu_flags); if ( sched_init_vcpu(v, cpu_id) < 0 ) { free_vcpu_struct(v); return NULL; } d->vcpu[vcpu_id] = v; if ( vcpu_id != 0 ) d->vcpu[v->vcpu_id-1]->next_in_list = v; return v; } struct vcpu *alloc_idle_vcpu(unsigned int cpu_id) { struct domain *d; struct vcpu *v; unsigned int vcpu_id = cpu_id % MAX_VIRT_CPUS; d = (vcpu_id == 0) ? domain_create(IDLE_DOMAIN_ID, 0) : idle_vcpu[cpu_id - vcpu_id]->domain; BUG_ON(d == NULL); v = alloc_vcpu(d, vcpu_id, cpu_id); idle_vcpu[cpu_id] = v; return v; } struct domain *domain_create(domid_t domid, unsigned int domcr_flags) { struct domain *d, **pd; if ( (d = alloc_domain(domid)) == NULL ) return NULL; if ( domcr_flags & DOMCRF_hvm ) d->is_hvm = 1; rangeset_domain_initialise(d); if ( !is_idle_domain(d) ) { set_bit(_DOMF_ctrl_pause, &d->domain_flags); if ( evtchn_init(d) != 0 ) goto fail1; if ( grant_table_create(d) != 0 ) goto fail2; } if ( arch_domain_create(d) != 0 ) goto fail3; d->iomem_caps = rangeset_new(d, "I/O Memory", RANGESETF_prettyprint_hex); d->irq_caps = rangeset_new(d, "Interrupts", 0); if ( (d->iomem_caps == NULL) || (d->irq_caps == NULL) ) goto fail4; if ( !is_idle_domain(d) ) { write_lock(&domlist_lock); pd = &domain_list; /* NB. domain_list maintained in order of domid. */ for ( pd = &domain_list; *pd != NULL; pd = &(*pd)->next_in_list ) if ( (*pd)->domain_id > d->domain_id ) break; d->next_in_list = *pd; *pd = d; d->next_in_hashbucket = domain_hash[DOMAIN_HASH(domid)]; domain_hash[DOMAIN_HASH(domid)] = d; write_unlock(&domlist_lock); } return d; fail4: arch_domain_destroy(d); fail3: if ( !is_idle_domain(d) ) grant_table_destroy(d); fail2: if ( !is_idle_domain(d) ) evtchn_destroy(d); fail1: rangeset_domain_destroy(d); free_domain(d); return NULL; } struct domain *find_domain_by_id(domid_t dom) { struct domain *d; read_lock(&domlist_lock); d = domain_hash[DOMAIN_HASH(dom)]; while ( d != NULL ) { if ( d->domain_id == dom ) { if ( unlikely(!get_domain(d)) ) d = NULL; break; } d = d->next_in_hashbucket; } read_unlock(&domlist_lock); return d; } void domain_kill(struct domain *d) { domain_pause(d); if ( test_and_set_bit(_DOMF_dying, &d->domain_flags) ) return; gnttab_release_mappings(d); domain_relinquish_resources(d); put_domain(d); send_guest_global_virq(dom0, VIRQ_DOM_EXC); } void __domain_crash(struct domain *d) { if ( d == current->domain ) { printk("Domain %d (vcpu#%d) crashed on cpu#%d:\n", d->domain_id, current->vcpu_id, smp_processor_id()); show_execution_state(guest_cpu_user_regs()); } else { printk("Domain %d reported crashed by domain %d on cpu#%d:\n", d->domain_id, current->domain->domain_id, smp_processor_id()); } domain_shutdown(d, SHUTDOWN_crash); } void __domain_crash_synchronous(void) { __domain_crash(current->domain); for ( ; ; ) do_softirq(); } void domain_shutdown(struct domain *d, u8 reason) { struct vcpu *v; if ( d->domain_id == 0 ) dom0_shutdown(reason); d->shutdown_code = reason; set_bit(_DOMF_shutdown, &d->domain_flags); for_each_vcpu ( d, v ) vcpu_sleep_nosync(v); send_guest_global_virq(dom0, VIRQ_DOM_EXC); } void domain_pause_for_debugger(void) { struct domain *d = current->domain; struct vcpu *v; set_bit(_DOMF_ctrl_pause, &d->domain_flags); for_each_vcpu ( d, v ) vcpu_sleep_nosync(v); send_guest_global_virq(dom0, VIRQ_DEBUGGER); } /* Release resources belonging to task @p. */ void domain_destroy(struct domain *d) { struct domain **pd; atomic_t old, new; BUG_ON(!test_bit(_DOMF_dying, &d->domain_flags)); /* May be already destroyed, or get_domain() can race us. */ _atomic_set(old, 0); _atomic_set(new, DOMAIN_DESTROYED); old = atomic_compareandswap(old, new, &d->refcnt); if ( _atomic_read(old) != 0 ) return; /* Delete from task list and task hashtable. */ write_lock(&domlist_lock); pd = &domain_list; while ( *pd != d ) pd = &(*pd)->next_in_list; *pd = d->next_in_list; pd = &domain_hash[DOMAIN_HASH(d->domain_id)]; while ( *pd != d ) pd = &(*pd)->next_in_hashbucket; *pd = d->next_in_hashbucket; write_unlock(&domlist_lock); rangeset_domain_destroy(d); evtchn_destroy(d); grant_table_destroy(d); arch_domain_destroy(d); free_domain(d); send_guest_global_virq(dom0, VIRQ_DOM_EXC); } void vcpu_pause(struct vcpu *v) { ASSERT(v != current); spin_lock(&v->pause_lock); if ( v->pause_count++ == 0 ) set_bit(_VCPUF_paused, &v->vcpu_flags); spin_unlock(&v->pause_lock); vcpu_sleep_sync(v); } void vcpu_unpause(struct vcpu *v) { int wake; ASSERT(v != current); spin_lock(&v->pause_lock); wake = (--v->pause_count == 0); if ( wake ) clear_bit(_VCPUF_paused, &v->vcpu_flags); spin_unlock(&v->pause_lock); if ( wake ) vcpu_wake(v); } void domain_pause(struct domain *d) { struct vcpu *v; ASSERT(d != current->domain); spin_lock(&d->pause_lock); if ( d->pause_count++ == 0 ) set_bit(_DOMF_paused, &d->domain_flags); spin_unlock(&d->pause_lock); for_each_vcpu( d, v ) vcpu_sleep_sync(v); } void domain_unpause(struct domain *d) { struct vcpu *v; int wake; ASSERT(d != current->domain); spin_lock(&d->pause_lock); wake = (--d->pause_count == 0); if ( wake ) clear_bit(_DOMF_paused, &d->domain_flags); spin_unlock(&d->pause_lock); if ( wake ) for_each_vcpu( d, v ) vcpu_wake(v); } void domain_pause_by_systemcontroller(struct domain *d) { struct vcpu *v; BUG_ON(current->domain == d); if ( !test_and_set_bit(_DOMF_ctrl_pause, &d->domain_flags) ) { for_each_vcpu ( d, v ) vcpu_sleep_sync(v); } } void domain_unpause_by_systemcontroller(struct domain *d) { struct vcpu *v; if ( test_and_clear_bit(_DOMF_ctrl_pause, &d->domain_flags) ) { for_each_vcpu ( d, v ) vcpu_wake(v); } } /* * set_info_guest is used for final setup, launching, and state modification * of domains other than domain 0. ie. the domains that are being built by * the userspace dom0 domain builder. */ int set_info_guest(struct domain *d, xen_domctl_vcpucontext_t *vcpucontext) { int rc = 0; struct vcpu_guest_context *c = NULL; unsigned long vcpu = vcpucontext->vcpu; struct vcpu *v; if ( (vcpu >= MAX_VIRT_CPUS) || ((v = d->vcpu[vcpu]) == NULL) ) return -EINVAL; if ( (c = xmalloc(struct vcpu_guest_context)) == NULL ) return -ENOMEM; domain_pause(d); rc = -EFAULT; if ( copy_from_guest(c, vcpucontext->ctxt, 1) == 0 ) rc = arch_set_info_guest(v, c); domain_unpause(d); xfree(c); return rc; } int boot_vcpu(struct domain *d, int vcpuid, struct vcpu_guest_context *ctxt) { struct vcpu *v = d->vcpu[vcpuid]; BUG_ON(test_bit(_VCPUF_initialised, &v->vcpu_flags)); return arch_set_info_guest(v, ctxt); } long do_vcpu_op(int cmd, int vcpuid, XEN_GUEST_HANDLE(void) arg) { struct domain *d = current->domain; struct vcpu *v; struct vcpu_guest_context *ctxt; long rc = 0; if ( (vcpuid < 0) || (vcpuid >= MAX_VIRT_CPUS) ) return -EINVAL; if ( (v = d->vcpu[vcpuid]) == NULL ) return -ENOENT; switch ( cmd ) { case VCPUOP_initialise: if ( (ctxt = xmalloc(struct vcpu_guest_context)) == NULL ) { rc = -ENOMEM; break; } if ( copy_from_guest(ctxt, arg, 1) ) { xfree(ctxt); rc = -EFAULT; break; } LOCK_BIGLOCK(d); rc = -EEXIST; if ( !test_bit(_VCPUF_initialised, &v->vcpu_flags) ) rc = boot_vcpu(d, vcpuid, ctxt); UNLOCK_BIGLOCK(d); xfree(ctxt); break; case VCPUOP_up: if ( !test_bit(_VCPUF_initialised, &v->vcpu_flags) ) rc = -EINVAL; else if ( test_and_clear_bit(_VCPUF_down, &v->vcpu_flags) ) vcpu_wake(v); break; case VCPUOP_down: if ( !test_and_set_bit(_VCPUF_down, &v->vcpu_flags) ) vcpu_sleep_nosync(v); break; case VCPUOP_is_up: rc = !test_bit(_VCPUF_down, &v->vcpu_flags); break; case VCPUOP_get_runstate_info: { struct vcpu_runstate_info runstate; vcpu_runstate_get(v, &runstate); if ( copy_to_guest(arg, &runstate, 1) ) rc = -EFAULT; break; } default: rc = arch_do_vcpu_op(cmd, v, arg); break; } return rc; } long vm_assist(struct domain *p, unsigned int cmd, unsigned int type) { if ( type > MAX_VMASST_TYPE ) return -EINVAL; switch ( cmd ) { case VMASST_CMD_enable: set_bit(type, &p->vm_assist); return 0; case VMASST_CMD_disable: clear_bit(type, &p->vm_assist); return 0; } return -ENOSYS; } /* * Local variables: * mode: C * c-set-style: "BSD" * c-basic-offset: 4 * tab-width: 4 * indent-tabs-mode: nil * End: */