1 #include <stdlib.h>
2 #include <string/stdstring.h>
3 #include <ugui.h>
4 #include <stdio.h>
5
6 #define UGUI_MAX_OBJECTS 2
7 static UG_GUI gui;
8 static UG_WINDOW gui_window;
9 static UG_TEXTBOX gui_textbox;
10 static UG_OBJECT gui_objbuf_wnd[UGUI_MAX_OBJECTS];
11 static unsigned *frame_buf = NULL;
12 static int width = 0;
13 static int height = 0;
14 static char gui_message[4096] = {0};
15
gui_window_callback(UG_MESSAGE * msg)16 static void gui_window_callback(UG_MESSAGE *msg)
17 {
18 }
19
gui_get_framebuffer(void)20 unsigned* gui_get_framebuffer(void)
21 {
22 return frame_buf;
23 }
24
25 /* uGUI callback that draws raw pixels onto our frame buffer */
UserPixelSetFunction(UG_S16 x,UG_S16 y,UG_COLOR c)26 static void UserPixelSetFunction(UG_S16 x, UG_S16 y, UG_COLOR c)
27 {
28 frame_buf[width * y + x] = c;
29 }
30
gui_init(int w,int h,int bpp)31 void gui_init(int w, int h, int bpp)
32 {
33 width = w;
34 height = h;
35 frame_buf = (unsigned*)calloc(width * height, bpp);
36
37 /* init uGUI */
38 UG_Init(&gui, UserPixelSetFunction, width, height);
39 UG_FontSelect(&FONT_8X8);
40
41 /* create a single window with no buttons */
42 UG_WindowCreate(&gui_window, gui_objbuf_wnd, UGUI_MAX_OBJECTS, gui_window_callback);
43 UG_WindowSetForeColor(&gui_window, C_BLACK);
44
45 UG_WindowSetXStart(&gui_window, 0);
46 UG_WindowSetYStart(&gui_window, 0);
47 UG_WindowSetXEnd(&gui_window, width - 1);
48 UG_WindowSetYEnd(&gui_window, height - 1);
49
50 UG_TextboxCreate(&gui_window, &gui_textbox, TXB_ID_0, 0, 0, UG_WindowGetInnerWidth(&gui_window) - 1, UG_WindowGetInnerHeight(&gui_window) - 1);
51 UG_TextboxSetAlignment(&gui_window, TXB_ID_0, ALIGN_CENTER);
52
53 UG_WindowShow(&gui_window);
54 }
55
gui_set_message(const char * message)56 void gui_set_message(const char *message)
57 {
58 memset(gui_message, 0, sizeof(gui_message));
59
60 snprintf(gui_message, sizeof(gui_message), "%s", message);
61
62 gui_message[sizeof(gui_message) - 1] = '\0';
63
64 UG_TextboxSetText(&gui_window, TXB_ID_0, gui_message);
65 }
66
gui_window_resize(int x,int y,int width,int height)67 void gui_window_resize(int x, int y, int width, int height)
68 {
69 UG_WindowResize(&gui_window, x, y, width, height);
70 }
71
gui_set_window_title(const char * title)72 void gui_set_window_title(const char *title)
73 {
74 UG_WindowSetTitleText(&gui_window, (char*)title);
75 }
76
gui_draw(void)77 void gui_draw(void)
78 {
79 if (!string_is_empty(gui_message))
80 UG_TextboxSetText(&gui_window, TXB_ID_0, gui_message);
81 UG_Update();
82 }
83