Support the file browser with the Allegro5 demo

This commit is contained in:
Cameron Cawley 2023-11-16 21:32:58 +00:00
parent 09f9b60fba
commit d15dd1db44
2 changed files with 74 additions and 2 deletions

View File

@ -41,6 +41,7 @@
/*#define INCLUDE_STYLE */
/*#define INCLUDE_CALCULATOR */
/*#define INCLUDE_CANVAS */
/*#define INCLUDE_FILE_BROWSER */
#define INCLUDE_OVERVIEW
/*#define INCLUDE_NODE_EDITOR */
@ -48,6 +49,7 @@
#define INCLUDE_STYLE
#define INCLUDE_CALCULATOR
#define INCLUDE_CANVAS
#define INCLUDE_FILE_BROWSER
#define INCLUDE_OVERVIEW
#define INCLUDE_NODE_EDITOR
#endif
@ -61,6 +63,9 @@
#ifdef INCLUDE_CANVAS
#include "../../demo/common/canvas.c"
#endif
#ifdef INCLUDE_FILE_BROWSER
#include "../../demo/common/file_browser.c"
#endif
#ifdef INCLUDE_OVERVIEW
#include "../../demo/common/overview.c"
#endif
@ -73,6 +78,26 @@
* DEMO
*
* ===============================================================*/
static void
die(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
fputs("\n", stderr);
exit(EXIT_FAILURE);
}
static struct nk_image
icon_load(const char *filename)
{
ALLEGRO_BITMAP *bitmap = al_load_bitmap(filename);
if (!bitmap) die("[SDL]: failed to load image: %s", filename);
return nk_image_ptr(bitmap);
}
int main(void)
{
/* Platform */
@ -81,11 +106,21 @@ int main(void)
NkAllegro5Font *font;
struct nk_context *ctx;
#ifdef INCLUDE_FILE_BROWSER
struct file_browser browser;
struct media media;
#endif
if (!al_init()) {
fprintf(stdout, "failed to initialize allegro5!\n");
exit(1);
}
if (!al_init_image_addon()) {
fprintf(stdout, "Unable to initialize required allegro5 image addon\n");
exit(1);
}
al_install_mouse();
al_set_mouse_wheel_precision(150);
al_install_keyboard();
@ -110,7 +145,7 @@ int main(void)
al_register_event_source(event_queue, al_get_mouse_event_source());
al_register_event_source(event_queue, al_get_keyboard_event_source());
font = nk_allegro5_font_create_from_file("../../../extra_font/Roboto-Regular.ttf", 12, 0);
font = nk_allegro5_font_create_from_file("../../extra_font/Roboto-Regular.ttf", 12, 0);
ctx = nk_allegro5_init(font, display, WINDOW_WIDTH, WINDOW_HEIGHT);
@ -128,6 +163,23 @@ int main(void)
#endif
#endif
#ifdef INCLUDE_FILE_BROWSER
/* icons */
media.icons.home = icon_load("../../demo/common/filebrowser/icon/home.png");
media.icons.directory = icon_load("../../demo/common/filebrowser/icon/directory.png");
media.icons.computer = icon_load("../../demo/common/filebrowser/icon/computer.png");
media.icons.desktop = icon_load("../../demo/common/filebrowser/icon/desktop.png");
media.icons.default_file = icon_load("../../demo/common/filebrowser/icon/default.png");
media.icons.text_file = icon_load("../../demo/common/filebrowser/icon/text.png");
media.icons.music_file = icon_load("../../demo/common/filebrowser/icon/music.png");
media.icons.font_file = icon_load("../../demo/common/filebrowser/icon/font.png");
media.icons.img_file = icon_load("../../demo/common/filebrowser/icon/img.png");
media.icons.movie_file = icon_load("../../demo/common/filebrowser/icon/movie.png");
media_init(&media);
file_browser_init(&browser, &media);
#endif
while(1)
{
bool get_event;
@ -179,6 +231,9 @@ int main(void)
#ifdef INCLUDE_CANVAS
canvas(ctx);
#endif
#ifdef INCLUDE_FILE_BROWSER
file_browser_run(&browser, ctx);
#endif
#ifdef INCLUDE_OVERVIEW
overview(ctx);
#endif
@ -196,6 +251,21 @@ int main(void)
al_flip_display();
}
#ifdef INCLUDE_FILE_BROWSER
al_destroy_bitmap(media.icons.home.handle.ptr);
al_destroy_bitmap(media.icons.directory.handle.ptr);
al_destroy_bitmap(media.icons.computer.handle.ptr);
al_destroy_bitmap(media.icons.desktop.handle.ptr);
al_destroy_bitmap(media.icons.default_file.handle.ptr);
al_destroy_bitmap(media.icons.text_file.handle.ptr);
al_destroy_bitmap(media.icons.music_file.handle.ptr);
al_destroy_bitmap(media.icons.font_file.handle.ptr);
al_destroy_bitmap(media.icons.img_file.handle.ptr);
al_destroy_bitmap(media.icons.movie_file.handle.ptr);
file_browser_free(&browser);
#endif
nk_allegro5_font_del(font);
nk_allegro5_shutdown();
al_destroy_display(display);

View File

@ -317,7 +317,9 @@ nk_allegro5_render()
} break;
case NK_COMMAND_IMAGE: {
const struct nk_command_image *i = (const struct nk_command_image *)cmd;
al_draw_bitmap_region(i->img.handle.ptr, 0, 0, i->w, i->h, i->x, i->y, 0);
int width = al_get_bitmap_width(i->img.handle.ptr);
int height = al_get_bitmap_height(i->img.handle.ptr);
al_draw_scaled_bitmap(i->img.handle.ptr, 0, 0, width, height, i->x, i->y, i->w, i->h, 0);
} break;
case NK_COMMAND_RECT_MULTI_COLOR:
default: break;