Branch
Hash :
d55e8643
Author :
Thomas de Grivel
Date :
2025-11-03T13:58:34
add mandelbrot_f64 as a fallback for mandelbrot_f80
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 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
/* kc3
* Copyright from 2022 to 2025 kmx.io <contact@kmx.io>
*
* Permission is hereby granted to use this software granted the above
* copyright notice and this permission paragraph are included in all
* copies and substantial portions of this software.
*
* THIS SOFTWARE IS PROVIDED "AS-IS" WITHOUT ANY GUARANTEE OF
* PURPOSE AND PERFORMANCE. IN NO EVENT WHATSOEVER SHALL THE
* AUTHOR BE CONSIDERED LIABLE FOR THE USE AND PERFORMANCE OF
* THIS SOFTWARE.
*/
//#include <stdio.h>
//#include <stdlib.h>
#include "../../../../libkc3/kc3.h"
#include "../../../../gl/gl_font.h"
#include "../../../../gl/gl_lines.h"
#include "../../../../gl/gl_ortho.h"
#include "../../../../gl/gl_sprite.h"
#include "../../../../gl/gl_text.h"
#include "../../../../gl/mat4.h"
#include "../../../demo/bg_rect.h"
#include "../../../demo/lightspeed.h"
#include "../../../demo/toasters.h"
#include "../../../demo/flies.h"
#include "../../../demo/earth.h"
#if HAVE_F80
#include "../../../demo/mandelbrot_f80.h"
#else
#include "../../../demo/mandelbrot_f64.h"
#endif
#include "../../../demo/matrix.h"
#include "../../../window.h"
#include "../../window_egl.h"
#include "../window_egl_drm.h"
#define WINDOW_EGL_DEMO_SEQUENCE_COUNT 7
const char *g_env_argv0_default = PROG;
const char *g_env_argv0_dir_default = PREFIX;
s_gl_font g_font_courier_new = {0};
s_gl_ortho g_ortho = {0};
s_gl_text g_text_fps = {0};
s_gl_text g_text_seq_title = {0};
static u8 window_egl_demo_button (s_window_egl *window, u8 button,
s64 x, s64 y);
static u8 window_egl_demo_key (s_window_egl *window, u32 keysym);
static u8 window_egl_demo_load (s_window_egl *window);
static u8 window_egl_demo_render (s_window_egl *window);
static u8 window_egl_demo_resize (s_window_egl *window, u64 w, u64 h);
static void window_egl_demo_unload (s_window_egl *window);
int main (int argc, char **argv)
{
s_window_egl window;
if (FT_Init_FreeType(&g_ft)) {
err_puts("main: failed to initialize FreeType");
return 1;
}
if (! kc3_init(NULL, &argc, &argv)) {
err_puts("kc3_init");
return 1;
}
window_egl_init(&window, 50, 50, 800, 600,
"KC3.Window.EGL demo",
WINDOW_EGL_DEMO_SEQUENCE_COUNT);
window.button = window_egl_demo_button;
window.key = window_egl_demo_key;
window.load = window_egl_demo_load;
window.render = window_egl_demo_render;
window.resize = window_egl_demo_resize;
window.unload = window_egl_demo_unload;
if (! window_egl_drm_run(&window)) {
err_puts("window_egl_drm_run -> false");
window_egl_clean(&window);
kc3_clean(NULL);
return g_kc3_exit_code;
}
window_egl_clean(&window);
kc3_clean(NULL);
FT_Done_FreeType(g_ft);
return 0;
}
u8 window_egl_demo_button (s_window_egl *window, u8 button,
s64 x, s64 y)
{
assert(window);
(void) window;
err_write_1("window_egl_demo_button: ");
err_inspect_u8(button);
err_write_1(" (");
err_inspect_s64(x);
err_write_1(", ");
err_inspect_s64(y);
err_puts(")");
if (window->seq && window->seq->button &&
! window->seq->button((s_sequence *) window->seq, button, x, y))
return false;
return true;
}
u8 window_egl_demo_key (s_window_egl *window, u32 keysym)
{
assert(window);
(void) window;
err_write_1("window_egl_demo_key: ");
err_inspect_u32(keysym);
err_write_1("\n");
switch (keysym) {
case 0xff1b: /* Escape */
case 'q':
g_kc3_exit_code = 0;
return false;
case 0xff51: /* Left */
if (! window_set_sequence_pos((s_window *) window,
(window->sequence_pos +
window->sequence_count - 1) %
window->sequence_count))
return false;
break;
case 0xff53: /* Right */
if (! window_set_sequence_pos((s_window *) window,
(window->sequence_pos + 1) %
window->sequence_count))
return false;
break;
}
if (window->seq && window->seq->key &&
! window->seq->key((s_sequence *) window->seq, keysym))
return false;
return true;
}
u8 window_egl_demo_load (s_window_egl *window)
{
f32 point_per_pixel;
assert(window);
assert(glGetError() == GL_NO_ERROR);
if (! module_load(sym_1("GL.Vertex")) ||
! module_load(sym_1("GL.Triangle")) ||
! module_load(sym_1("GL.Object")) ||
! module_load(sym_1("GL.Sphere")))
return false;
point_per_pixel = (f32) window->w / window->pixel_w;
err_write_1("point_per_pixel: ");
err_inspect_f32(point_per_pixel);
err_write_1("\n");
if (window->sequence_count != WINDOW_EGL_DEMO_SEQUENCE_COUNT) {
err_write_1("window_egl_demo_load: window->sequence_count = ");
err_inspect_u32_decimal(window->sequence_count);
err_write_1("\n");
assert(window->sequence_count == WINDOW_EGL_DEMO_SEQUENCE_COUNT);
return false;
}
if (! gl_ortho_init(&g_ortho))
return false;
gl_ortho_resize(&g_ortho, 0, window->w, 0, window->h, 0, 1);
if (! gl_font_init(&g_font_courier_new,
"fonts/Courier New/Courier New.ttf",
point_per_pixel))
return false;
if (! gl_text_init_1(&g_text_seq_title, &g_font_courier_new, "0"))
return false;
if (! gl_text_init_1(&g_text_fps, &g_font_courier_new, "0.00"))
return false;
sequence_init((s_sequence *) window->sequence, 8.0,
"01. Background rectangles",
bg_rect_load,
bg_rect_render,
bg_rect_unload,
window);
if (! gl_lines_init(&g_lines_stars) ||
! gl_lines_allocate(&g_lines_stars, LIGHTSPEED_STAR_MAX))
return false;
sequence_init((s_sequence *) window->sequence + 1, 20.0,
"02. Lightspeed",
lightspeed_load,
lightspeed_render,
lightspeed_unload,
window);
if (! gl_sprite_init(&g_sprite_toaster, "img/flaps.256.png",
4, 1, 4, 1))
return false;
if (! gl_sprite_init(&g_sprite_toast, "img/toast.128.png",
1, 1, 1, 1))
return false;
sequence_init((s_sequence *) window->sequence + 2, 60.0,
"03. Toasters",
toasters_load,
toasters_render,
toasters_unload,
window);
if (! gl_font_init(&g_font_flies,
"fonts/Courier New/Courier New.ttf",
point_per_pixel))
return false;
if (! gl_sprite_init(&g_sprite_fly, "img/fly-noto.png",
1, 1, 1, point_per_pixel))
return false;
if (! gl_sprite_init(&g_sprite_dead_fly, "img/fly-dead.png",
1, 1, 1, point_per_pixel))
return false;
sequence_init((s_sequence *) window->sequence + 3, 60.0,
"04. Flies",
flies_load,
flies_render,
flies_unload,
window);
if (! gl_sprite_init(&g_sprite_earth, "img/earth.png",
1, 1, 1, point_per_pixel))
return false;
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
assert(glGetError() == GL_NO_ERROR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
assert(glGetError() == GL_NO_ERROR);
sequence_init((s_sequence *) window->sequence + 4, 120.0,
"05. Earth",
earth_load,
earth_render,
earth_unload,
window);
#if HAVE_F80
sequence_init((s_sequence *) window->sequence + 5, 3600.0,
"06. Mandelbrot (f80)",
mandelbrot_f80_load,
mandelbrot_f80_render,
mandelbrot_f80_unload,
window);
window->sequence[5].button = (f_sequence_button)
mandelbrot_f80_button;
#else
sequence_init((s_sequence *) window->sequence + 5, 3600.0,
"06. Mandelbrot (f64)",
mandelbrot_f64_load,
mandelbrot_f64_render,
mandelbrot_f64_unload,
window);
window->sequence[5].button = (f_sequence_button)
mandelbrot_f64_button;
#endif
sequence_init((s_sequence *) window->sequence + 6, 3600.0,
"07. Matrix",
matrix_load,
matrix_render,
matrix_unload,
window);
window_set_sequence_pos((s_window *) window, 0);
return true;
}
u8 window_egl_demo_render (s_window_egl *window)
{
s_sequence_egl *seq;
const s_rgb text_color[2] = {
{1.0f, 1.0f, 1.0f},
{0.0f, 0.0f, 0.0f}
};
assert(window);
if (! window_animate((s_window *) window)) {
err_puts("window_egl_demo_render: window_animate");
return false;
}
seq = window->seq;
if (seq && seq->render && ! seq->render((s_sequence *) seq)) {
err_puts("window_egl_demo_render: seq->render");
return false;
}
mat4_init_identity(&g_ortho.model_matrix);
gl_ortho_render(&g_ortho);
glDisable(GL_DEPTH_TEST);
glEnable(GL_BLEND);
glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA,
GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
gl_font_set_size(&g_font_courier_new, 20);
gl_text_update_1(&g_text_seq_title, seq->title);
mat4_init_identity(&g_ortho.model_matrix);
gl_ortho_text_render_outline(&g_ortho, &g_text_seq_title,
20.0f, 30.0f, text_color,
text_color + 1);
mat4_init_identity(&g_ortho.model_matrix);
glDisable(GL_BLEND);
gl_ortho_bind_texture(&g_ortho, 0);
gl_ortho_color(&g_ortho, 1.0f, 1.0f, 1.0f, 1.0f);
gl_ortho_rect(&g_ortho, 19, 11,
(window->w - 40.0) * seq->t / seq->duration + 2,
4);
gl_ortho_color(&g_ortho, 0.0f, 0.0f, 0.0f, 1.0f);
gl_ortho_rect(&g_ortho, 20, 12,
(window->w - 40.0) * seq->t / seq->duration,
2);
char fps[32];
snprintf(fps, sizeof(fps), "%.1f", (f64) seq->frame / seq->t);
mat4_init_identity(&g_ortho.model_matrix);
gl_text_update_1(&g_text_fps, fps);
glEnable(GL_BLEND);
gl_ortho_text_render_outline(&g_ortho, &g_text_fps,
20, window->h - 30, text_color,
text_color + 1);
gl_ortho_render_end(&g_ortho);
return true;
}
u8 window_egl_demo_resize (s_window_egl *window, u64 w, u64 h)
{
assert(window);
(void) window;
err_write_1("window_egl_demo_resize: ");
err_inspect_u64(w);
err_write_1(" x ");
err_inspect_u64(h);
err_write_1("\n");
glViewport(0, 0, w, h);
return true;
}
void window_egl_demo_unload (s_window_egl *window)
{
assert(window);
(void) window;
gl_text_clean(&g_text_fps);
gl_text_clean(&g_text_seq_title);
gl_font_clean(&g_font_courier_new);
gl_lines_clean(&g_lines_stars);
gl_sprite_clean(&g_sprite_toaster);
gl_sprite_clean(&g_sprite_toast);
gl_font_clean(&g_font_flies);
gl_sprite_clean(&g_sprite_fly);
gl_sprite_clean(&g_sprite_dead_fly);
gl_sprite_clean(&g_sprite_earth);
gl_ortho_clean(&g_ortho);
err_puts("window_egl_demo_unload");
}