Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
ex_gui_display_fallback.c
Go to the documentation of this file.
1
16
17#define ALLEGRO_UNSTABLE 1
18
19#include <stdio.h>
20#include <stdlib.h>
21#include <string.h>
22#include <unistd.h>
23
24#include <allegro5/allegro_primitives.h>
25
26#include "nilorea/n_common.h"
27#include "nilorea/n_log.h"
28#include "nilorea/n_gui.h"
29
30static int log_level = LOG_ERR;
31static int failures = 0;
32
34static void expect_true(const char* what, int ok) {
35 if (!ok) {
36 n_log(LOG_ERR, "FAIL: %s", what);
37 failures++;
38 } else {
39 n_log(LOG_INFO, "ok: %s", what);
40 }
41}
42
44static int process_args(int argc, char** argv) {
45 int opt = 0;
46
47 while ((opt = getopt(argc, argv, "V:")) != -1) {
48 if (opt == 'V') {
49 log_level = atoi(optarg);
50 } else {
51 return -1;
52 }
53 }
54 return 0;
55}
56
58static void draw_into(N_GUI_CTX* gui, int w, int h) {
59 int old_flags = al_get_new_bitmap_flags();
60
61 al_set_new_bitmap_flags(ALLEGRO_MEMORY_BITMAP);
62 ALLEGRO_BITMAP* target = al_create_bitmap(w, h);
63 al_set_new_bitmap_flags(old_flags);
64 if (!target) {
65 expect_true("a memory bitmap to draw into", 0);
66 return;
67 }
68 al_set_target_bitmap(target);
69 al_clear_to_color(al_map_rgb(0, 0, 0));
71 al_set_target_bitmap(NULL);
72 al_destroy_bitmap(target);
73}
74
75int main(int argc, char** argv) {
77 if (process_args(argc, argv) != 0) {
78 fprintf(stderr, "Usage: %s [-V LOG_LEVEL]\n", argv[0]);
79 return 2;
80 }
82
83 if (!al_init() || !al_init_font_addon() || !al_init_primitives_addon()) {
84 n_log(LOG_ERR, "allegro init failed");
85 return 1;
86 }
87 ALLEGRO_FONT* font = al_create_builtin_font();
88 if (!font) {
89 n_log(LOG_ERR, "al_create_builtin_font failed");
90 return 1;
91 }
93 if (!gui) {
94 al_destroy_font(font);
95 return 1;
96 }
97 int win = n_gui_add_window(gui, "W", 10.0f, 10.0f, 200.0f, 100.0f);
98 n_gui_add_label(gui, win, "a label", 10.0f, 10.0f, 150.0f, 20.0f, N_GUI_ALIGN_LEFT);
100
101 expect_true("a new context has no display size", gui->display_w == 0.0f && gui->display_h == 0.0f);
102
103 draw_into(gui, 640, 400);
104 expect_true("with none set, a draw takes the target's size",
105 gui->display_w == 640.0f && gui->display_h == 400.0f);
106
107 draw_into(gui, 1024, 300);
108 expect_true("and follows it when the target changes, as a resize would",
109 gui->display_w == 1024.0f && gui->display_h == 300.0f);
110
111 n_gui_set_display_size(gui, 800.0f, 600.0f);
112 draw_into(gui, 640, 400);
113 expect_true("once a size is set, the target no longer overrides it",
114 gui->display_w == 800.0f && gui->display_h == 600.0f);
115
117 al_destroy_font(font);
118 if (failures) {
119 fprintf(stderr, "ex_gui_display_fallback: %d failure(s)\n", failures);
120 return 1;
121 }
122 printf("ex_gui_display_fallback: ok\n");
123 return 0;
124}
void process_args(int argc, char **argv)
static int failures
Definition ex_atlas.c:51
int main(void)
Definition ex_atlas.c:117
static void expect_true(const char *label, int cond)
Definition ex_atlas.c:53
static int log_level
static N_GUI_CTX * gui
static void draw_into(N_GUI_CTX *gui, int w, int h)
draw one pass of the context into a memory bitmap of the given size
FONT * font
the default 8x8 font, valid after allegro_init()
void n_gui_set_display_size(N_GUI_CTX *ctx, float w, float h)
set the display (viewport) size so global scrollbars can be computed.
Definition n_gui.c:12348
#define N_GUI_ALIGN_LEFT
left aligned text
Definition n_gui.h:212
int n_gui_add_label(N_GUI_CTX *ctx, int window_id, const char *text, float x, float y, float w, float h, int align)
Add a static text label.
Definition n_gui.c:3639
void n_gui_draw(N_GUI_CTX *ctx)
draw all visible GUI windows and widgets
Definition n_gui.c:11735
int n_gui_add_window(N_GUI_CTX *ctx, const char *title, float x, float y, float w, float h)
Add a new pseudo-window to the context.
Definition n_gui.c:1277
N_GUI_CTX * n_gui_new_ctx(ALLEGRO_FONT *default_font)
Create a new GUI context.
Definition n_gui.c:1103
void n_gui_destroy_ctx(N_GUI_CTX **ctx)
Destroy a GUI context and all its windows/widgets.
Definition n_gui.c:1209
void n_gui_open_window(N_GUI_CTX *ctx, int window_id)
Open (show) a window.
Definition n_gui.c:1436
The top-level GUI context that holds all windows.
Definition n_gui.h:1669
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
Definition n_log.h:113
#define LOG_ERR
error conditions
Definition n_log.h:100
void set_log_level(const int log_level)
Set the global log level value ( static int LOG_LEVEL ).
Definition n_log.c:123
#define LOG_INFO
informational
Definition n_log.h:106
Common headers and low-level functions & define.
GUI system: buttons, sliders, text areas, checkboxes, scrollbars, dropdown menus, windows.
Generic log system.