1 /* nuklear - 1.32.0 - public domain */
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <stdint.h>
5 #include <stdarg.h>
6 #include <string.h>
7 #include <math.h>
8 #include <assert.h>
9 #include <math.h>
10 #include <limits.h>
11 #include <time.h>
12 
13 #include <allegro5/allegro.h>
14 
15 #define WINDOW_WIDTH 1200
16 #define WINDOW_HEIGHT 800
17 
18 #define NK_INCLUDE_FIXED_TYPES
19 #define NK_INCLUDE_STANDARD_IO
20 #define NK_INCLUDE_STANDARD_VARARGS
21 #define NK_INCLUDE_DEFAULT_ALLOCATOR
22 #define NK_IMPLEMENTATION
23 #define NK_ALLEGRO5_IMPLEMENTATION
24 #include "../../nuklear.h"
25 #include "nuklear_allegro5.h"
26 
27 
28 #define UNUSED(a) (void)a
29 #define MIN(a,b) ((a) < (b) ? (a) : (b))
30 #define MAX(a,b) ((a) < (b) ? (b) : (a))
31 #define LEN(a) (sizeof(a)/sizeof(a)[0])
32 
33 /* ===============================================================
34  *
35  *                          EXAMPLE
36  *
37  * ===============================================================*/
38 /* This are some code examples to provide a small overview of what can be
39  * done with this library. To try out an example uncomment the include
40  * and the corresponding function. */
41 /*#include "../style.c"*/
42 /*#include "../calculator.c"*/
43 #include "../overview.c"
44 /*#include "../node_editor.c"*/
45 
46 /* ===============================================================
47  *
48  *                          DEMO
49  *
50  * ===============================================================*/
error_callback(int e,const char * d)51 static void error_callback(int e, const char *d)
52 {printf("Error %d: %s\n", e, d);}
53 
main(void)54 int main(void)
55 {
56     /* Platform */
57     ALLEGRO_DISPLAY *display = NULL;
58     ALLEGRO_EVENT_QUEUE *event_queue = NULL;
59 
60     if (!al_init()) {
61         fprintf(stdout, "failed to initialize allegro5!\n");
62         exit(1);
63     }
64 
65     al_install_mouse();
66     al_set_mouse_wheel_precision(150);
67     al_install_keyboard();
68 
69     al_set_new_display_flags(ALLEGRO_WINDOWED|ALLEGRO_RESIZABLE|ALLEGRO_OPENGL);
70     al_set_new_display_option(ALLEGRO_SAMPLE_BUFFERS, 1, ALLEGRO_SUGGEST);
71     al_set_new_display_option(ALLEGRO_SAMPLES, 8, ALLEGRO_SUGGEST);
72     display = al_create_display(WINDOW_WIDTH, WINDOW_HEIGHT);
73     if (!display) {
74         fprintf(stdout, "failed to create display!\n");
75         exit(1);
76     }
77 
78     event_queue = al_create_event_queue();
79     if (!event_queue) {
80         fprintf(stdout, "failed to create event_queue!\n");
81         al_destroy_display(display);
82         exit(1);
83     }
84 
85     al_register_event_source(event_queue, al_get_display_event_source(display));
86     al_register_event_source(event_queue, al_get_mouse_event_source());
87     al_register_event_source(event_queue, al_get_keyboard_event_source());
88 
89     NkAllegro5Font *font;
90     font = nk_allegro5_font_create_from_file("../../../extra_font/Roboto-Regular.ttf", 12, 0);
91     struct nk_context *ctx;
92 
93     ctx = nk_allegro5_init(font, display, WINDOW_WIDTH, WINDOW_HEIGHT);
94 
95     /* style.c */
96     /*set_style(ctx, THEME_WHITE);*/
97     /*set_style(ctx, THEME_RED);*/
98     /*set_style(ctx, THEME_BLUE);*/
99     /*set_style(ctx, THEME_DARK);*/
100 
101     while(1)
102     {
103         ALLEGRO_EVENT ev;
104         ALLEGRO_TIMEOUT timeout;
105         al_init_timeout(&timeout, 0.06);
106 
107         bool get_event = al_wait_for_event_until(event_queue, &ev, &timeout);
108 
109         if (get_event && ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) {
110             break;
111         }
112 
113         /* Very Important: Always do nk_input_begin / nk_input_end even if
114            there are no events, otherwise internal nuklear state gets messed up */
115         nk_input_begin(ctx);
116         if (get_event) {
117             while (get_event) {
118                 nk_allegro5_handle_event(&ev);
119                 get_event = al_get_next_event(event_queue, &ev);
120             }
121         }
122         nk_input_end(ctx);
123 
124         /* GUI */
125         if (nk_begin(ctx, "Demo", nk_rect(50, 50, 200, 200),
126             NK_WINDOW_BORDER|NK_WINDOW_MOVABLE|NK_WINDOW_SCALABLE|
127             NK_WINDOW_CLOSABLE|NK_WINDOW_MINIMIZABLE|NK_WINDOW_TITLE))
128         {
129             enum {EASY, HARD};
130             static int op = EASY;
131             static int property = 20;
132 
133             nk_layout_row_static(ctx, 30, 80, 1);
134             if (nk_button_label(ctx, "button"))
135                 fprintf(stdout, "button pressed\n");
136             nk_layout_row_dynamic(ctx, 30, 2);
137             if (nk_option_label(ctx, "easy", op == EASY)) op = EASY;
138             if (nk_option_label(ctx, "hard", op == HARD)) op = HARD;
139             nk_layout_row_dynamic(ctx, 22, 1);
140             nk_property_int(ctx, "Compression:", 0, &property, 100, 10, 1);
141         }
142         nk_end(ctx);
143 
144         /* -------------- EXAMPLES ---------------- */
145         /*calculator(ctx);*/
146         overview(ctx);
147         /*node_editor(ctx);*/
148         /* ----------------------------------------- */
149 
150         /* Draw */
151         al_clear_to_color(al_map_rgb(19, 43, 81));
152         /* IMPORTANT: `nk_allegro5_render` changes the target backbuffer
153         to the display set at initialization and does not restore it.
154         Change it if you want to draw somewhere else. */
155         nk_allegro5_render();
156         al_flip_display();
157     }
158 
159     nk_allegro5_font_del(font);
160     nk_allegro5_shutdown();
161     al_destroy_display(display);
162     al_destroy_event_queue(event_queue);
163     return 0;
164 }
165 
166