Animation for Nuklear
NukAnim is one C99 header: a port of ImAnim by Soufiane KHIAT, with ImAnimate's animate by Raidcore. It has tweens, easing, color spaces, clips and timelines, motion paths, procedural motion, and the Nuklear side: scrolling, style crossfades, text effects and an inspector. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@@ -0,0 +1,29 @@
|
||||
name: ci
|
||||
|
||||
on:
|
||||
push:
|
||||
pull_request:
|
||||
|
||||
jobs:
|
||||
test:
|
||||
name: ${{ matrix.name }}
|
||||
runs-on: ${{ matrix.os }}
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
include:
|
||||
- { name: linux gcc, os: ubuntu-latest, cmake: -DCMAKE_C_COMPILER=gcc }
|
||||
- { name: linux clang, os: ubuntu-latest, cmake: -DCMAKE_C_COMPILER=clang }
|
||||
- { name: linux sanitizers, os: ubuntu-latest, cmake: -DCMAKE_C_COMPILER=clang -DNUKANIM_SANITIZE=ON }
|
||||
- { name: macos clang, os: macos-latest, cmake: "" }
|
||||
- { name: windows msvc, os: windows-latest, cmake: "" }
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
with:
|
||||
submodules: true
|
||||
- name: Build
|
||||
run: |
|
||||
cmake -S . -B build -DCMAKE_BUILD_TYPE=Release ${{ matrix.cmake }}
|
||||
cmake --build build --config Release
|
||||
- name: Test
|
||||
run: ctest --test-dir build -C Release --output-on-failure
|
||||
@@ -0,0 +1 @@
|
||||
/build*/
|
||||
@@ -0,0 +1,3 @@
|
||||
[submodule "external/nuklear"]
|
||||
path = external/nuklear
|
||||
url = https://github.com/Immediate-Mode-UI/Nuklear.git
|
||||
@@ -0,0 +1,54 @@
|
||||
cmake_minimum_required(VERSION 3.16)
|
||||
project(NukAnim C)
|
||||
|
||||
add_library(nukanim INTERFACE)
|
||||
target_include_directories(nukanim INTERFACE ${CMAKE_CURRENT_SOURCE_DIR})
|
||||
|
||||
if(NOT CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
|
||||
return()
|
||||
endif()
|
||||
|
||||
option(NUKANIM_SANITIZE "Build the tests with AddressSanitizer and UBSan" OFF)
|
||||
enable_testing()
|
||||
set(NUKLEAR ${CMAKE_CURRENT_SOURCE_DIR}/external/nuklear)
|
||||
if(NOT EXISTS ${NUKLEAR}/nuklear.h)
|
||||
message(FATAL_ERROR "Nuklear is missing: git submodule update --init")
|
||||
endif()
|
||||
|
||||
if(MSVC)
|
||||
set(STRICT /W4 /WX /wd5287)
|
||||
set(QUIET /W1)
|
||||
else()
|
||||
set(STRICT -std=c99 -Wall -Wextra -pedantic -Werror)
|
||||
set(QUIET -w)
|
||||
find_library(MATH m)
|
||||
endif()
|
||||
|
||||
add_library(nuklear_tests STATIC tests/nuklear.c)
|
||||
target_include_directories(nuklear_tests PUBLIC ${NUKLEAR})
|
||||
target_compile_options(nuklear_tests PRIVATE ${QUIET})
|
||||
if(MATH)
|
||||
target_link_libraries(nuklear_tests PUBLIC ${MATH})
|
||||
endif()
|
||||
|
||||
foreach(test core clip path fx nk)
|
||||
add_executable(test_${test} tests/${test}.c)
|
||||
target_link_libraries(test_${test} PRIVATE nukanim nuklear_tests)
|
||||
target_compile_options(test_${test} PRIVATE ${STRICT})
|
||||
if(NUKANIM_SANITIZE)
|
||||
target_compile_options(test_${test} PRIVATE -fsanitize=address,undefined -fno-sanitize-recover=all -fno-omit-frame-pointer)
|
||||
target_link_options(test_${test} PRIVATE -fsanitize=address,undefined)
|
||||
endif()
|
||||
add_test(NAME ${test} COMMAND test_${test})
|
||||
endforeach()
|
||||
|
||||
add_library(nuklear_examples STATIC examples/nuklear.c)
|
||||
target_include_directories(nuklear_examples PUBLIC ${NUKLEAR} examples)
|
||||
target_compile_options(nuklear_examples PRIVATE ${QUIET})
|
||||
if(MATH)
|
||||
target_link_libraries(nuklear_examples PUBLIC ${MATH})
|
||||
endif()
|
||||
|
||||
add_executable(clips examples/clips.c)
|
||||
target_link_libraries(clips PRIVATE nukanim nuklear_examples)
|
||||
target_compile_options(clips PRIVATE ${STRICT})
|
||||
@@ -0,0 +1,23 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2026 Alpaq92
|
||||
Copyright (c) 2025 SoufianeKHIAT (ImAnim)
|
||||
Copyright (c) 2024-2025 Raidcore (ImAnimate)
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
@@ -0,0 +1,97 @@
|
||||
# NukAnim
|
||||
|
||||
Animation for [Nuklear](https://github.com/Immediate-Mode-UI/Nuklear) in one C99 header, which includes
|
||||
`<math.h>` and nothing else (and `<stdlib.h>` only for Nuklear's default allocator). It is a port of
|
||||
[ImAnim](https://github.com/soufianekhiat/ImAnim) by Soufiane Khiat, with the hover idiom of
|
||||
[ImAnimate](https://github.com/RaidcoreGG/ImAnimate) by Raidcore.
|
||||
|
||||

|
||||
|
||||
## Use
|
||||
|
||||
```c
|
||||
#include "nuklear.h"
|
||||
#define NUKANIM_IMPLEMENTATION /* in one C file */
|
||||
#include "nukanim.h"
|
||||
|
||||
struct nka_context *anim = nka_create(NULL); /* malloc, under NK_INCLUDE_DEFAULT_ALLOCATOR */
|
||||
|
||||
/* Every frame, before the UI: */
|
||||
nka_update(anim, dt);
|
||||
|
||||
float alpha = nka_tween_float(anim, nka_id("panel"), nka_id("alpha"), open ? 1.0f : 0.0f, 0.25f,
|
||||
nka_ease(NKA_EASE_OUT_CUBIC), NKA_POLICY_CROSSFADE, 0);
|
||||
|
||||
/* After it: an app that only draws on input keeps drawing while something moves. */
|
||||
if (nka_busy(anim)) request_frame();
|
||||
```
|
||||
|
||||
## What it does
|
||||
|
||||
**Tweens** of floats, vectors, ints and colors, keyed by an id and a channel, with crossfade, cut, queue,
|
||||
additive and multiply policies; targets relative to a window, computed by a callback, or eased per axis.
|
||||
|
||||
**Easing**: the thirty classic curves, steps, cubic béziers, springs and your own functions. ImAnimate's
|
||||
curves are among them, and so is its way of animating a value that keeps its own state:
|
||||
|
||||
```c
|
||||
static float size = 1;
|
||||
if (nk_input_is_mouse_hovering_rect(&ctx->input, bounds))
|
||||
nka_animate(anim, 1, 1.1f, 150, &size, NKA_EASE_OUT_CUBIC);
|
||||
else
|
||||
nka_animate(anim, 1.1f, 1, 250, &size, NKA_EASE_OUT_CUBIC);
|
||||
```
|
||||
|
||||

|
||||
|
||||
**Colors** blend in sRGB, linear sRGB, HSV, OKLAB or OKLCH.
|
||||
|
||||

|
||||
|
||||
**Clips**: keyframed timelines with loops, delays, markers, staggering across lists and grids, variation
|
||||
from loop to loop, layering and chaining. They save to and load from memory.
|
||||
|
||||

|
||||
|
||||
**Paths**: lines, béziers and Catmull-Rom splines to move along at constant speed, morph between, and set
|
||||
text on.
|
||||
|
||||

|
||||
|
||||
**Procedural motion**: oscillators, shake, wiggle, noise, gradients, 2D transforms and drag snapping.
|
||||
|
||||

|
||||
|
||||
**Nuklear**: eased scrolling for windows and groups, whole-style crossfades, text that arrives glyph by
|
||||
glyph, and an inspector window with clip timelines and a profiler. Tested with Nuklear 4.13.3.
|
||||
|
||||

|
||||
|
||||

|
||||
|
||||
## Differences from ImAnim
|
||||
|
||||
- A context you create holds the state, instead of globals; `nka_update(anim, dt)` once a frame replaces
|
||||
the `dt` of every call, and `nka_busy` tells an app that only draws on input when to keep going.
|
||||
- Nuklear cannot turn text: glyphs on a path stay upright, and the rotate effect fades. Scaled text needs
|
||||
a backend that draws Nuklear's vertex output.
|
||||
- Parametric curves are computed exactly rather than read from lookup tables.
|
||||
- Clip files became buffers you read and write yourself, little-endian and checksummed.
|
||||
- Bugs fixed on the way, among them: the queue policy only waited a frame, additive tweens added their
|
||||
delta every frame, garbage collection dropped tweens in use, back and elastic curves ignored their
|
||||
parameters, finite loops never ended, markers at the end of a clip never fired, finished gradient,
|
||||
transform, morph and style tweens restarted forever, noise channels sampled the noise where it is
|
||||
flattest, and shakes only shook one axis.
|
||||
|
||||
## Tests
|
||||
|
||||
```sh
|
||||
git submodule update --init
|
||||
cmake -S . -B build && cmake --build build && ctest --test-dir build
|
||||
```
|
||||
|
||||
`build/clips` renders the clips above, run from the repository's root; `tools/gif.py` makes them GIFs.
|
||||
|
||||
## License
|
||||
|
||||
MIT. [LICENSE](LICENSE) keeps ImAnim's and ImAnimate's notices.
|
||||
|
After Width: | Height: | Size: 237 KiB |
|
After Width: | Height: | Size: 143 KiB |
|
After Width: | Height: | Size: 165 KiB |
|
After Width: | Height: | Size: 82 KiB |
|
After Width: | Height: | Size: 491 KiB |
|
After Width: | Height: | Size: 301 KiB |
|
After Width: | Height: | Size: 292 KiB |
|
After Width: | Height: | Size: 127 KiB |
@@ -0,0 +1,611 @@
|
||||
/* The README's clips: Nuklear's anti-aliased vertex output, rasterized in
|
||||
* software, written as raw RGBA frames for tools/gif.py to turn into GIFs.
|
||||
*
|
||||
* clips <scene> <out.raw> run from the repository's root
|
||||
*/
|
||||
#define _CRT_SECURE_NO_WARNINGS
|
||||
#include <math.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "nk.h"
|
||||
#define NUKANIM_IMPLEMENTATION
|
||||
#include "nukanim.h"
|
||||
|
||||
#define DT 0.03f
|
||||
#define FONTS "external/nuklear/extra_font/"
|
||||
|
||||
struct vertex { float x, y, u, v; nk_byte c[4]; };
|
||||
|
||||
static struct nk_context ctx;
|
||||
static struct nka_context *anim;
|
||||
static struct nk_font *font, *bold, *small;
|
||||
static struct nk_draw_null_texture null_tex;
|
||||
static unsigned char *tex, *fb;
|
||||
static int tex_w, tex_h, fb_w, fb_h;
|
||||
|
||||
static const struct nk_color ink = { 230, 232, 236, 255 }, dim = { 138, 143, 152, 255 };
|
||||
static const struct nk_color ground = { 22, 24, 29, 255 }, panel = { 31, 34, 41, 255 }, line = { 48, 52, 62, 255 };
|
||||
static const struct nk_color cyan = { 91, 194, 231, 255 }, copper = { 204, 120, 88, 255 };
|
||||
|
||||
static void sample(float u, float v, float *out)
|
||||
{
|
||||
float x = u * (float)tex_w - 0.5f, y = v * (float)tex_h - 0.5f;
|
||||
int x0 = (int)floorf(x), y0 = (int)floorf(y), i, j, k;
|
||||
float fx = x - (float)x0, fy = y - (float)y0;
|
||||
out[0] = out[1] = out[2] = out[3] = 0;
|
||||
for (j = 0; j < 2; ++j)
|
||||
for (i = 0; i < 2; ++i) {
|
||||
int tx = x0 + i < 0 ? 0 : (x0 + i >= tex_w ? tex_w - 1 : x0 + i);
|
||||
int ty = y0 + j < 0 ? 0 : (y0 + j >= tex_h ? tex_h - 1 : y0 + j);
|
||||
float w = (i ? fx : 1 - fx) * (j ? fy : 1 - fy);
|
||||
const unsigned char *p = tex + ((size_t)ty * (size_t)tex_w + (size_t)tx) * 4;
|
||||
for (k = 0; k < 4; ++k) out[k] += w * (float)p[k] / 255.0f;
|
||||
}
|
||||
}
|
||||
|
||||
/* Edges in 1/256 pixel fixed point, so two triangles sharing an edge agree on
|
||||
* every pixel; one on the edge goes to the triangle whose top or left edge it
|
||||
* is, and nothing blends twice or not at all. */
|
||||
struct fixed { long long x, y; };
|
||||
|
||||
static struct fixed fix(const struct vertex *v)
|
||||
{
|
||||
struct fixed f;
|
||||
f.x = (long long)floorf(v->x * 256 + 0.5f);
|
||||
f.y = (long long)floorf(v->y * 256 + 0.5f);
|
||||
return f;
|
||||
}
|
||||
|
||||
static long long edge(struct fixed a, struct fixed b, long long x, long long y)
|
||||
{
|
||||
return (b.x - a.x) * (y - a.y) - (b.y - a.y) * (x - a.x);
|
||||
}
|
||||
|
||||
static int top_left(struct fixed a, struct fixed b)
|
||||
{
|
||||
return (b.y == a.y && b.x > a.x) || b.y < a.y;
|
||||
}
|
||||
|
||||
static int covers(long long w, int tl) { return w > 0 || (w == 0 && tl); }
|
||||
|
||||
static void triangle(const struct vertex *v0, const struct vertex *v1, const struct vertex *v2, struct nk_rect clip)
|
||||
{
|
||||
const struct vertex *v[3];
|
||||
struct fixed f[3];
|
||||
long long area;
|
||||
int x0, x1, y0, y1, x, y, k, tl[3];
|
||||
v[0] = v0;
|
||||
v[1] = v1;
|
||||
v[2] = v2;
|
||||
for (k = 0; k < 3; ++k) f[k] = fix(v[k]);
|
||||
area = edge(f[0], f[1], f[2].x, f[2].y);
|
||||
if (area == 0) return;
|
||||
if (area < 0) {
|
||||
struct fixed t = f[1];
|
||||
f[1] = f[2];
|
||||
f[2] = t;
|
||||
v[1] = v2;
|
||||
v[2] = v1;
|
||||
area = -area;
|
||||
}
|
||||
x0 = (int)floorf(fminf(v0->x, fminf(v1->x, v2->x)));
|
||||
x1 = (int)ceilf(fmaxf(v0->x, fmaxf(v1->x, v2->x)));
|
||||
y0 = (int)floorf(fminf(v0->y, fminf(v1->y, v2->y)));
|
||||
y1 = (int)ceilf(fmaxf(v0->y, fmaxf(v1->y, v2->y)));
|
||||
if (x0 < (int)floorf(clip.x)) x0 = (int)floorf(clip.x);
|
||||
if (y0 < (int)floorf(clip.y)) y0 = (int)floorf(clip.y);
|
||||
if (x1 > (int)ceilf(clip.x + clip.w)) x1 = (int)ceilf(clip.x + clip.w);
|
||||
if (y1 > (int)ceilf(clip.y + clip.h)) y1 = (int)ceilf(clip.y + clip.h);
|
||||
if (x0 < 0) x0 = 0;
|
||||
if (y0 < 0) y0 = 0;
|
||||
if (x1 > fb_w) x1 = fb_w;
|
||||
if (y1 > fb_h) y1 = fb_h;
|
||||
tl[0] = top_left(f[1], f[2]);
|
||||
tl[1] = top_left(f[2], f[0]);
|
||||
tl[2] = top_left(f[0], f[1]);
|
||||
for (y = y0; y < y1; ++y)
|
||||
for (x = x0; x < x1; ++x) {
|
||||
long long px = (long long)x * 256 + 128, py = (long long)y * 256 + 128, e[3];
|
||||
float w, t[4], s[4], u = 0, vv = 0;
|
||||
unsigned char *d;
|
||||
e[0] = edge(f[1], f[2], px, py);
|
||||
e[1] = edge(f[2], f[0], px, py);
|
||||
e[2] = edge(f[0], f[1], px, py);
|
||||
if (!covers(e[0], tl[0]) || !covers(e[1], tl[1]) || !covers(e[2], tl[2])) continue;
|
||||
s[0] = s[1] = s[2] = s[3] = 0;
|
||||
for (k = 0; k < 3; ++k) {
|
||||
int c;
|
||||
w = (float)e[k] / (float)area;
|
||||
u += w * v[k]->u;
|
||||
vv += w * v[k]->v;
|
||||
for (c = 0; c < 4; ++c) s[c] += w * (float)v[k]->c[c] / 255.0f;
|
||||
}
|
||||
sample(u, vv, t);
|
||||
for (k = 0; k < 4; ++k) s[k] *= t[k];
|
||||
d = fb + ((size_t)y * (size_t)fb_w + (size_t)x) * 4;
|
||||
for (k = 0; k < 3; ++k) d[k] = (unsigned char)((float)d[k] * (1 - s[3]) + s[k] * 255.0f * s[3] + 0.5f);
|
||||
d[3] = 255;
|
||||
}
|
||||
}
|
||||
|
||||
static void render(struct nk_color clear)
|
||||
{
|
||||
static const struct nk_draw_vertex_layout_element layout[] = {
|
||||
{ NK_VERTEX_POSITION, NK_FORMAT_FLOAT, 0 },
|
||||
{ NK_VERTEX_TEXCOORD, NK_FORMAT_FLOAT, 8 },
|
||||
{ NK_VERTEX_COLOR, NK_FORMAT_R8G8B8A8, 16 },
|
||||
{ NK_VERTEX_LAYOUT_END }
|
||||
};
|
||||
struct nk_convert_config cfg;
|
||||
struct nk_buffer cmds, verts, idx;
|
||||
const struct nk_draw_command *cmd;
|
||||
const nk_draw_index *offset;
|
||||
const struct vertex *vtx;
|
||||
int i;
|
||||
for (i = 0; i < fb_w * fb_h; ++i) {
|
||||
fb[i * 4 + 0] = clear.r;
|
||||
fb[i * 4 + 1] = clear.g;
|
||||
fb[i * 4 + 2] = clear.b;
|
||||
fb[i * 4 + 3] = 255;
|
||||
}
|
||||
memset(&cfg, 0, sizeof cfg);
|
||||
cfg.vertex_layout = layout;
|
||||
cfg.vertex_size = sizeof(struct vertex);
|
||||
cfg.vertex_alignment = 4;
|
||||
cfg.tex_null = null_tex;
|
||||
cfg.circle_segment_count = 48;
|
||||
cfg.curve_segment_count = 32;
|
||||
cfg.arc_segment_count = 32;
|
||||
cfg.global_alpha = 1;
|
||||
cfg.shape_AA = NK_ANTI_ALIASING_ON;
|
||||
cfg.line_AA = NK_ANTI_ALIASING_ON;
|
||||
nk_buffer_init_default(&cmds);
|
||||
nk_buffer_init_default(&verts);
|
||||
nk_buffer_init_default(&idx);
|
||||
nk_convert(&ctx, &cmds, &verts, &idx, &cfg);
|
||||
vtx = (const struct vertex *)nk_buffer_memory_const(&verts);
|
||||
offset = (const nk_draw_index *)nk_buffer_memory_const(&idx);
|
||||
nk_draw_foreach(cmd, &ctx, &cmds) {
|
||||
unsigned int e;
|
||||
for (e = 0; e + 2 < cmd->elem_count; e += 3)
|
||||
triangle(&vtx[offset[e]], &vtx[offset[e + 1]], &vtx[offset[e + 2]], cmd->clip_rect);
|
||||
offset += cmd->elem_count;
|
||||
}
|
||||
nk_buffer_free(&cmds);
|
||||
nk_buffer_free(&verts);
|
||||
nk_buffer_free(&idx);
|
||||
nk_clear(&ctx);
|
||||
}
|
||||
|
||||
static void setup(int w, int h)
|
||||
{
|
||||
struct nk_font_atlas atlas;
|
||||
const void *image;
|
||||
struct nk_color table[NK_COLOR_COUNT];
|
||||
int i;
|
||||
fb_w = w;
|
||||
fb_h = h;
|
||||
fb = (unsigned char *)malloc((size_t)w * (size_t)h * 4);
|
||||
nk_font_atlas_init_default(&atlas);
|
||||
nk_font_atlas_begin(&atlas);
|
||||
font = nk_font_atlas_add_from_file(&atlas, FONTS "Roboto-Regular.ttf", 17, 0);
|
||||
bold = nk_font_atlas_add_from_file(&atlas, FONTS "Roboto-Bold.ttf", 18, 0);
|
||||
small = nk_font_atlas_add_from_file(&atlas, FONTS "Roboto-Regular.ttf", 13, 0);
|
||||
if (!font || !bold || !small) {
|
||||
fprintf(stderr, "clips: run from the repository's root, the fonts are in " FONTS "\n");
|
||||
exit(1);
|
||||
}
|
||||
image = nk_font_atlas_bake(&atlas, &tex_w, &tex_h, NK_FONT_ATLAS_RGBA32);
|
||||
tex = (unsigned char *)malloc((size_t)tex_w * (size_t)tex_h * 4);
|
||||
memcpy(tex, image, (size_t)tex_w * (size_t)tex_h * 4);
|
||||
nk_font_atlas_end(&atlas, nk_handle_id(0), &null_tex);
|
||||
nk_init_default(&ctx, &font->handle);
|
||||
for (i = 0; i < NK_COLOR_COUNT; ++i) table[i] = panel;
|
||||
table[NK_COLOR_TEXT] = ink;
|
||||
table[NK_COLOR_WINDOW] = ground;
|
||||
table[NK_COLOR_BORDER] = line;
|
||||
table[NK_COLOR_BUTTON_HOVER] = line;
|
||||
table[NK_COLOR_BUTTON_ACTIVE] = line;
|
||||
nk_style_from_table(&ctx, table);
|
||||
ctx.style.window.padding = nk_vec2(0, 0);
|
||||
ctx.style.window.spacing = nk_vec2(0, 0);
|
||||
anim = nka_create(0);
|
||||
}
|
||||
|
||||
static struct nk_command_buffer *canvas(void)
|
||||
{
|
||||
nk_begin(&ctx, "clip", nk_rect(0, 0, (float)fb_w, (float)fb_h), NK_WINDOW_NO_SCROLLBAR | NK_WINDOW_BACKGROUND);
|
||||
return nk_window_get_canvas(&ctx);
|
||||
}
|
||||
|
||||
static void text(struct nk_command_buffer *out, const struct nk_font *f, float x, float y, const char *s,
|
||||
struct nk_color c)
|
||||
{
|
||||
nk_draw_text(out, nk_rect(x, y, 400, f->handle.height + 4), s, (int)strlen(s), &f->handle,
|
||||
nk_rgba(0, 0, 0, 0), c);
|
||||
}
|
||||
|
||||
static float text_width(const struct nk_font *f, const char *s)
|
||||
{
|
||||
return f->handle.width(f->handle.userdata, f->handle.height, s, (int)strlen(s));
|
||||
}
|
||||
|
||||
/* Scenes take the frame's time and draw into one window that fills the clip. */
|
||||
|
||||
static void easing(float time)
|
||||
{
|
||||
static const char *names[] = { "out cubic", "in out sine", "out back", "out elastic", "out bounce",
|
||||
"spring", "steps", "bezier" };
|
||||
struct nka_ease eases[8];
|
||||
struct nk_command_buffer *out = canvas();
|
||||
float target = fmodf(time, 4.4f) < 2.2f ? 1.0f : 0.0f;
|
||||
int i;
|
||||
eases[0] = nka_ease(NKA_EASE_OUT_CUBIC);
|
||||
eases[1] = nka_ease(NKA_EASE_IN_OUT_SINE);
|
||||
eases[2] = nka_ease(NKA_EASE_OUT_BACK);
|
||||
eases[3] = nka_ease(NKA_EASE_OUT_ELASTIC);
|
||||
eases[4] = nka_ease(NKA_EASE_OUT_BOUNCE);
|
||||
eases[5] = nka_ease_spring(1, 180, 18, 0);
|
||||
eases[6] = nka_ease_steps(6, 0);
|
||||
eases[7] = nka_ease_bezier(0.68f, -0.55f, 0.27f, 1.55f);
|
||||
for (i = 0; i < 8; ++i) {
|
||||
float y = 26 + (float)i * 33, x0 = 170, x1 = (float)fb_w - 80;
|
||||
float k = nka_tween_float(anim, nka_id(names[i]), 0, target, 1.4f, eases[i], NKA_POLICY_CROSSFADE, 0);
|
||||
float x = x0 + (x1 - x0) * k;
|
||||
text(out, font, 28, y - 10, names[i], dim);
|
||||
nk_stroke_line(out, x0, y, x1, y, 2, line);
|
||||
nk_fill_circle(out, nk_rect(x - 8, y - 8, 16, 16), i & 1 ? copper : cyan);
|
||||
}
|
||||
nk_end(&ctx);
|
||||
}
|
||||
|
||||
static void hover(float time)
|
||||
{
|
||||
static const char *labels[] = { "Home", "Search", "Library", "Profile" };
|
||||
static float size[4] = { 1, 1, 1, 1 };
|
||||
const float pi = 3.14159265f;
|
||||
struct nk_command_buffer *out = canvas();
|
||||
struct nk_vec2 mouse;
|
||||
float sweep = fmodf(time, 4.0f) / 4.0f;
|
||||
int i;
|
||||
mouse.x = 40 + ((float)fb_w - 80) * (0.5f - 0.5f * cosf(sweep * 2 * pi));
|
||||
mouse.y = (float)fb_h * 0.5f + 34 * sinf(sweep * 4 * pi);
|
||||
for (i = 0; i < 4; ++i) {
|
||||
float w = 120, h = 64, cx = (float)fb_w * ((float)i + 0.5f) / 4, cy = (float)fb_h * 0.5f;
|
||||
struct nk_rect r = nk_rect(cx - w / 2, cy - h / 2, w, h);
|
||||
int over = mouse.x >= r.x && mouse.x <= r.x + r.w && mouse.y >= r.y && mouse.y <= r.y + r.h;
|
||||
struct nk_colorf fill;
|
||||
if (over) nka_animate(anim, 1, 1.12f, 180, &size[i], NKA_EASE_OUT_CUBIC);
|
||||
else nka_animate(anim, 1.12f, 1, 260, &size[i], NKA_EASE_OUT_CUBIC);
|
||||
fill = nka_tween_color(anim, nka_id(labels[i]), 1, nk_color_cf(over ? cyan : panel), 0.25f,
|
||||
nka_ease(NKA_EASE_OUT_CUBIC), NKA_POLICY_CROSSFADE, NKA_COL_OKLAB, nk_color_cf(panel));
|
||||
w *= size[i];
|
||||
h *= size[i];
|
||||
nk_fill_rect(out, nk_rect(cx - w / 2, cy - h / 2, w, h), 12 * size[i], nk_rgb_cf(fill));
|
||||
text(out, bold, cx - text_width(bold, labels[i]) / 2, cy - 11, labels[i], over ? ground : ink);
|
||||
}
|
||||
text(out, small, 24, (float)fb_h - 30, "nka_animate(a, 1, 1.12f, 180, &size, NKA_EASE_OUT_CUBIC)", dim);
|
||||
nk_fill_circle(out, nk_rect(mouse.x - 6, mouse.y - 6, 12, 12), ink);
|
||||
nk_end(&ctx);
|
||||
}
|
||||
|
||||
static struct nk_colorf rgb(float r, float g, float b)
|
||||
{
|
||||
struct nk_colorf c;
|
||||
c.r = r; c.g = g; c.b = b; c.a = 1;
|
||||
return c;
|
||||
}
|
||||
|
||||
static void colors(float time)
|
||||
{
|
||||
static const char *names[] = { "sRGB", "linear", "HSV", "OKLAB", "OKLCH" };
|
||||
static const int spaces[] = { NKA_COL_SRGB, NKA_COL_SRGB_LINEAR, NKA_COL_HSV, NKA_COL_OKLAB, NKA_COL_OKLCH };
|
||||
struct nk_colorf from = rgb(0.12f, 0.42f, 1.0f), to = rgb(1.0f, 0.82f, 0.12f);
|
||||
struct nk_command_buffer *out = canvas();
|
||||
float k = nka_tween_float(anim, nka_id("blend"), 0, fmodf(time, 4.8f) < 2.4f ? 1.0f : 0.0f, 1.6f,
|
||||
nka_ease(NKA_EASE_IN_OUT_CUBIC), NKA_POLICY_CROSSFADE, 0);
|
||||
int i, s;
|
||||
for (i = 0; i < 5; ++i) {
|
||||
float y = 22 + (float)i * 46, x0 = 110, w = 400;
|
||||
text(out, font, 24, y + 4, names[i], dim);
|
||||
for (s = 0; s < 100; ++s)
|
||||
nk_fill_rect(out, nk_rect(x0 + w * (float)s / 100, y, w / 100 + 1, 28), 0,
|
||||
nk_rgb_cf(nka_color_blend(from, to, (float)s / 99, spaces[i])));
|
||||
nk_stroke_circle(out, nk_rect(x0 + w * k - 7, y + 7, 14, 14), 2, ink);
|
||||
nk_fill_rect(out, nk_rect(x0 + w + 30, y - 2, 64, 32), 8, nk_rgb_cf(nka_color_blend(from, to, k, spaces[i])));
|
||||
}
|
||||
nk_end(&ctx);
|
||||
}
|
||||
|
||||
static void paths(float time)
|
||||
{
|
||||
nk_hash loop = nka_id("loop"), wave = nka_id("wave");
|
||||
struct nka_text_path_opts opts = nka_text_path_opts_default();
|
||||
struct nk_command_buffer *out = canvas();
|
||||
struct nk_vec2 p, prev;
|
||||
float angle, progress;
|
||||
int i, lap = (int)(time / 3.0f);
|
||||
nka_path_begin(anim, loop, nk_vec2(40, 230));
|
||||
nka_path_cubic_to(anim, nk_vec2(60, 40), nk_vec2(250, 30), nk_vec2(270, 130));
|
||||
nka_path_catmull_to(anim, nk_vec2(200, 200), 0.5f);
|
||||
nka_path_catmull_to(anim, nk_vec2(300, 250), 0.5f);
|
||||
nka_path_end(anim);
|
||||
if (!nka_path_has_arc_lut(anim, loop)) nka_path_build_arc_lut(anim, loop, 128);
|
||||
nka_path_begin(anim, wave, nk_vec2(340, 170));
|
||||
nka_path_cubic_to(anim, nk_vec2(420, 60), nk_vec2(500, 280), nk_vec2(620, 150));
|
||||
nka_path_end(anim);
|
||||
for (i = 0, prev = nka_path_evaluate(anim, loop, 0); i <= 120; ++i, prev = p) {
|
||||
p = nka_path_evaluate(anim, loop, (float)i / 120);
|
||||
if (i) nk_stroke_line(out, prev.x, prev.y, p.x, p.y, 2, line);
|
||||
}
|
||||
for (i = 0, prev = nka_path_evaluate(anim, wave, 0); i <= 120; ++i, prev = p) {
|
||||
p = nka_path_evaluate(anim, wave, (float)i / 120);
|
||||
if (i) nk_stroke_line(out, prev.x, prev.y, p.x, p.y, 1, line);
|
||||
}
|
||||
p = nka_tween_path(anim, 1, (nk_hash)lap, loop, 2.4f, nka_ease(NKA_EASE_IN_OUT_CUBIC), NKA_POLICY_CROSSFADE);
|
||||
angle = nka_tween_path_angle(anim, 2, (nk_hash)lap, loop, 2.4f, nka_ease(NKA_EASE_IN_OUT_CUBIC), NKA_POLICY_CROSSFADE);
|
||||
nk_stroke_line(out, p.x, p.y, p.x + 22 * cosf(angle), p.y + 22 * sinf(angle), 3, copper);
|
||||
nk_fill_circle(out, nk_rect(p.x - 8, p.y - 8, 16, 16), cyan);
|
||||
progress = fmodf(time, 3.0f) / 2.2f;
|
||||
opts.color = ink;
|
||||
opts.align = NKA_TEXT_ALIGN_CENTER;
|
||||
opts.font = &bold->handle;
|
||||
nka_text_path_animated(anim, &ctx, wave, "text follows paths too", progress, &opts);
|
||||
nk_end(&ctx);
|
||||
}
|
||||
|
||||
static void stagger(float time)
|
||||
{
|
||||
static const char *names[] = { "fade", "slide up", "bounce", "wave", "typewriter", "scale" };
|
||||
static const int effects[] = { NKA_TEXT_FX_FADE, NKA_TEXT_FX_SLIDE_UP, NKA_TEXT_FX_BOUNCE,
|
||||
NKA_TEXT_FX_WAVE, NKA_TEXT_FX_TYPEWRITER, NKA_TEXT_FX_SCALE };
|
||||
struct nka_text_stagger_opts opts = nka_text_stagger_opts_default();
|
||||
struct nk_command_buffer *out = canvas();
|
||||
const char *line_text = "Text arrives glyph by glyph";
|
||||
float progress = fmodf(time, 4.5f) / 3.0f;
|
||||
int i;
|
||||
opts.char_delay = 0.035f;
|
||||
opts.char_duration = 0.45f;
|
||||
opts.effect_intensity = 14;
|
||||
opts.color = ink;
|
||||
opts.font = &bold->handle;
|
||||
for (i = 0; i < 6; ++i) {
|
||||
float y = 24 + (float)i * 42;
|
||||
text(out, small, 24, y + 4, names[i], dim);
|
||||
opts.effect = effects[i];
|
||||
opts.pos = nk_vec2(130, y);
|
||||
nka_text_stagger(anim, &ctx, line_text, effects[i] == NKA_TEXT_FX_WAVE ? fmodf(time, 4.5f) / 4.5f
|
||||
: (progress < 1 ? progress : 1), &opts);
|
||||
}
|
||||
nk_end(&ctx);
|
||||
}
|
||||
|
||||
static void styles(float time)
|
||||
{
|
||||
static int registered, check = 1, option = 1;
|
||||
static float slider = 0.6f;
|
||||
static nk_size progress = 64;
|
||||
nk_hash dark = nka_id("dark"), light = nka_id("light");
|
||||
if (!registered) {
|
||||
struct nk_color table[NK_COLOR_COUNT];
|
||||
int i;
|
||||
nka_style_register_current(anim, &ctx, dark);
|
||||
for (i = 0; i < NK_COLOR_COUNT; ++i) table[i] = nk_rgb(236, 238, 242);
|
||||
table[NK_COLOR_TEXT] = nk_rgb(32, 36, 44);
|
||||
table[NK_COLOR_WINDOW] = nk_rgb(250, 250, 252);
|
||||
table[NK_COLOR_BORDER] = nk_rgb(214, 218, 226);
|
||||
table[NK_COLOR_BUTTON] = nk_rgb(226, 230, 238);
|
||||
table[NK_COLOR_BUTTON_HOVER] = nk_rgb(214, 218, 228);
|
||||
table[NK_COLOR_SLIDER] = nk_rgb(214, 218, 226);
|
||||
table[NK_COLOR_SLIDER_CURSOR] = copper;
|
||||
table[NK_COLOR_TOGGLE_CURSOR] = copper;
|
||||
table[NK_COLOR_SELECT_ACTIVE] = copper;
|
||||
table[NK_COLOR_PROPERTY] = nk_rgb(226, 230, 238);
|
||||
nka_style_register_table(anim, &ctx, light, table);
|
||||
ctx.style.slider.cursor_normal = nk_style_item_color(cyan);
|
||||
ctx.style.checkbox.cursor_normal = nk_style_item_color(cyan);
|
||||
ctx.style.option.cursor_normal = nk_style_item_color(cyan);
|
||||
nka_style_register_current(anim, &ctx, dark);
|
||||
registered = 1;
|
||||
}
|
||||
nka_style_tween(anim, &ctx, 1, fmodf(time, 5.0f) < 2.5f ? light : dark, 0.9f,
|
||||
nka_ease(NKA_EASE_IN_OUT_CUBIC), NKA_COL_OKLAB);
|
||||
ctx.style.window.padding = nk_vec2(28, 22);
|
||||
ctx.style.window.spacing = nk_vec2(12, 12);
|
||||
if (nk_begin(&ctx, "style", nk_rect(0, 0, (float)fb_w, (float)fb_h), NK_WINDOW_NO_SCROLLBAR)) {
|
||||
nk_layout_row_dynamic(&ctx, 26, 1);
|
||||
nk_label(&ctx, "nka_style_tween blends whole Nuklear styles", NK_TEXT_LEFT);
|
||||
nk_layout_row_dynamic(&ctx, 28, 2);
|
||||
nk_checkbox_label(&ctx, "Animate", &check);
|
||||
nk_slider_float(&ctx, 0, &slider, 1, 0.01f);
|
||||
nk_option_label(&ctx, "Color space: OKLAB", option);
|
||||
nk_progress(&ctx, &progress, 100, NK_FIXED);
|
||||
nk_layout_row_dynamic(&ctx, 36, 3);
|
||||
nk_button_label(&ctx, "Cancel");
|
||||
nk_button_label(&ctx, "Apply");
|
||||
nk_button_label(&ctx, "OK");
|
||||
}
|
||||
nk_end(&ctx);
|
||||
}
|
||||
|
||||
static struct nk_rect cell(struct nk_command_buffer *out, int i, const char *name)
|
||||
{
|
||||
float w = ((float)fb_w - 16 * 4) / 3, h = ((float)fb_h - 16 * 3) / 2;
|
||||
struct nk_rect r = nk_rect(16 + (float)(i % 3) * (w + 16), 16 + (float)(i / 3) * (h + 16), w, h);
|
||||
nk_fill_rect(out, r, 10, panel);
|
||||
text(out, small, r.x + 12, r.y + 8, name, dim);
|
||||
return r;
|
||||
}
|
||||
|
||||
static void procedural(float time)
|
||||
{
|
||||
static struct nk_vec2 trail[24];
|
||||
static int trail_n;
|
||||
const float pi = 3.14159265f;
|
||||
struct nk_command_buffer *out = canvas();
|
||||
struct nk_rect r;
|
||||
struct nk_vec2 c, v;
|
||||
struct nka_gradient g;
|
||||
struct nka_transform x;
|
||||
int i;
|
||||
|
||||
r = cell(out, 0, "oscillate");
|
||||
c = nk_vec2(r.x + r.w / 2, r.y + r.h / 2 + 8);
|
||||
for (i = 0; i < 5; ++i) {
|
||||
float y = nka_oscillate(anim, nka_id_mix(nka_id("osc"), (nk_hash)i), 26, 0.7f, NKA_WAVE_SINE, (float)i * 0.12f);
|
||||
nk_fill_circle(out, nk_rect(c.x - 52 + (float)i * 26 - 7, c.y + y - 7, 14, 14), cyan);
|
||||
}
|
||||
|
||||
r = cell(out, 1, "wiggle");
|
||||
c = nk_vec2(r.x + r.w / 2, r.y + r.h / 2 + 8);
|
||||
v = nka_wiggle_vec2(anim, nka_id("wiggle"), nk_vec2(42, 28), 1.4f);
|
||||
nk_fill_circle(out, nk_rect(c.x + v.x - 9, c.y + v.y - 9, 18, 18), copper);
|
||||
|
||||
r = cell(out, 2, "shake");
|
||||
c = nk_vec2(r.x + r.w / 2, r.y + r.h / 2 + 8);
|
||||
if (fmodf(time, 1.5f) < DT) nka_trigger_shake(anim, nka_id("shake"));
|
||||
v = nka_shake_vec2(anim, nka_id("shake"), nk_vec2(12, 5), 28, 0.55f);
|
||||
nk_fill_rect(out, nk_rect(c.x - 44 + v.x, c.y - 18 + v.y, 88, 36), 8, line);
|
||||
text(out, small, c.x - 20 + v.x, c.y - 9 + v.y, "wrong", ink);
|
||||
|
||||
r = cell(out, 3, "noise");
|
||||
c = nk_vec2(r.x + r.w / 2, r.y + r.h / 2 + 8);
|
||||
v = nka_noise_channel_vec2(anim, nka_id("noise"), nk_vec2(0.9f, 0.7f), nk_vec2(170, 100), 0);
|
||||
trail[trail_n++ % 24] = nk_vec2(c.x + v.x, c.y + v.y);
|
||||
for (i = (trail_n < 24 ? trail_n : 24) - 1; i >= 0; --i) {
|
||||
struct nk_vec2 p = trail[(trail_n - 1 - i) % 24];
|
||||
struct nk_color k = cyan;
|
||||
k.a = (nk_byte)(255 - i * 10);
|
||||
nk_fill_circle(out, nk_rect(p.x - 6 + (float)i * 0.15f, p.y - 6 + (float)i * 0.15f,
|
||||
12 - (float)i * 0.3f, 12 - (float)i * 0.3f), k);
|
||||
}
|
||||
|
||||
r = cell(out, 4, "gradient");
|
||||
{
|
||||
struct nka_gradient a = nka_gradient_three_color(nk_color_cf(cyan), rgb(0.95f, 0.95f, 0.98f), nk_color_cf(copper));
|
||||
struct nka_gradient b = nka_gradient_two_color(rgb(0.36f, 0.20f, 0.62f), rgb(1.0f, 0.78f, 0.25f));
|
||||
g = nka_tween_gradient(anim, nka_id("gradient"), 0, fmodf(time, 4.0f) < 2.0f ? &a : &b, 1.2f,
|
||||
nka_ease(NKA_EASE_IN_OUT_CUBIC), NKA_POLICY_CROSSFADE, NKA_COL_OKLAB);
|
||||
for (i = 0; i < 64; ++i)
|
||||
nk_fill_rect(out, nk_rect(r.x + 16 + (r.w - 32) * (float)i / 64, r.y + 44, (r.w - 32) / 64 + 1, 56), 0,
|
||||
nk_rgb_cf(nka_gradient_sample(&g, (float)i / 63, NKA_COL_OKLAB)));
|
||||
}
|
||||
|
||||
r = cell(out, 5, "transform");
|
||||
c = nk_vec2(r.x + r.w / 2, r.y + r.h / 2 + 8);
|
||||
x = nka_tween_transform(anim, nka_id("square"), 0,
|
||||
fmodf(time, 3.0f) < 1.5f ? nka_transform(c, 0, nk_vec2(1, 1))
|
||||
: nka_transform(c, pi / 2, nk_vec2(1.5f, 0.8f)),
|
||||
1.1f, nka_ease(NKA_EASE_OUT_BACK), NKA_POLICY_CROSSFADE, NKA_ROTATION_CW);
|
||||
{
|
||||
float pts[8];
|
||||
static const float corner[4][2] = { { -22, -22 }, { 22, -22 }, { 22, 22 }, { -22, 22 } };
|
||||
for (i = 0; i < 4; ++i) {
|
||||
struct nk_vec2 p = nka_transform_apply(x, nk_vec2(corner[i][0], corner[i][1]));
|
||||
pts[i * 2] = p.x;
|
||||
pts[i * 2 + 1] = p.y;
|
||||
}
|
||||
nk_fill_polygon(out, pts, 4, cyan);
|
||||
}
|
||||
nk_end(&ctx);
|
||||
}
|
||||
|
||||
static void timeline(float time)
|
||||
{
|
||||
static int made;
|
||||
static float started = -1;
|
||||
nk_hash clip = nka_id("tile"), scale = nka_id("scale"), alpha = nka_id("alpha"), lift = nka_id("lift");
|
||||
struct nka_stagger_grid_opts grid = nka_stagger_grid_opts_default();
|
||||
struct nk_command_buffer *out;
|
||||
int i;
|
||||
if (!made) {
|
||||
nka_clip_begin(anim, clip);
|
||||
nka_clip_key_float(anim, scale, 0, 0.4f, NKA_EASE_OUT_BACK, 0);
|
||||
nka_clip_key_float(anim, scale, 0.6f, 1, NKA_EASE_LINEAR, 0);
|
||||
nka_clip_key_float(anim, alpha, 0, 0, NKA_EASE_OUT_CUBIC, 0);
|
||||
nka_clip_key_float(anim, alpha, 0.35f, 1, NKA_EASE_LINEAR, 0);
|
||||
nka_clip_key_float(anim, lift, 0, 26, NKA_EASE_OUT_CUBIC, 0);
|
||||
nka_clip_key_float(anim, lift, 0.5f, 0, NKA_EASE_LINEAR, 0);
|
||||
nka_clip_end(anim);
|
||||
made = 1;
|
||||
}
|
||||
grid.cols = 6;
|
||||
grid.rows = 2;
|
||||
grid.from = NKA_STAGGER_CENTER;
|
||||
grid.delay = 0.09f;
|
||||
if (started < 0 || time - started >= 3.0f) {
|
||||
for (i = 0; i < 12; ++i) nka_play_with_delay(anim, clip, (nk_hash)(100 + i), nka_stagger_grid_delay_index(anim, i, &grid));
|
||||
started = time;
|
||||
}
|
||||
nk_style_push_vec2(&ctx, &ctx.style.window.padding, nk_vec2(16, 12));
|
||||
nk_begin(&ctx, "clip", nk_rect(0, 0, (float)fb_w, (float)fb_h), NK_WINDOW_NO_SCROLLBAR | NK_WINDOW_BACKGROUND);
|
||||
out = nk_window_get_canvas(&ctx);
|
||||
for (i = 0; i < 12; ++i) {
|
||||
float s = 1, k = 1, y = 0, cx = 16 + 52 + (float)(i % 6) * 101, cy = 52 + (float)(i / 6) * 80;
|
||||
struct nk_color c = i % 2 ? copper : cyan;
|
||||
nka_instance_get_float(anim, (nk_hash)(100 + i), scale, &s);
|
||||
nka_instance_get_float(anim, (nk_hash)(100 + i), alpha, &k);
|
||||
nka_instance_get_float(anim, (nk_hash)(100 + i), lift, &y);
|
||||
c.a = (nk_byte)(255 * (k < 0 ? 0 : (k > 1 ? 1 : k)));
|
||||
nk_fill_rect(out, nk_rect(cx - 44 * s, cy + y - 30 * s, 88 * s, 60 * s), 10 * s, c);
|
||||
}
|
||||
nk_layout_row_dynamic(&ctx, 150, 1);
|
||||
nk_spacer(&ctx);
|
||||
nka_show_debug_timeline(anim, &ctx, 100 + 2);
|
||||
nk_end(&ctx);
|
||||
nk_style_pop_vec2(&ctx);
|
||||
}
|
||||
|
||||
struct scene { const char *name; int w, h; float seconds; void (*draw)(float time); };
|
||||
|
||||
static const struct scene scenes[] = {
|
||||
{ "easing", 640, 290, 4.4f, easing },
|
||||
{ "hover", 640, 220, 4.0f, hover },
|
||||
{ "colors", 640, 250, 4.8f, colors },
|
||||
{ "paths", 640, 290, 3.0f, paths },
|
||||
{ "stagger", 640, 280, 4.5f, stagger },
|
||||
{ "styles", 640, 230, 5.0f, styles },
|
||||
{ "procedural", 640, 300, 6.0f, procedural },
|
||||
{ "timeline", 640, 330, 3.0f, timeline },
|
||||
};
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
const struct scene *s = 0;
|
||||
unsigned int header[4];
|
||||
FILE *f;
|
||||
int i, frames;
|
||||
for (i = 0; argc == 3 && i < (int)(sizeof scenes / sizeof *scenes); ++i)
|
||||
if (!strcmp(argv[1], scenes[i].name)) s = &scenes[i];
|
||||
if (!s) {
|
||||
fprintf(stderr, "usage: clips <scene> <out.raw>; scenes:");
|
||||
for (i = 0; i < (int)(sizeof scenes / sizeof *scenes); ++i) fprintf(stderr, " %s", scenes[i].name);
|
||||
fprintf(stderr, "\n");
|
||||
return 2;
|
||||
}
|
||||
setup(s->w, s->h);
|
||||
f = fopen(argv[2], "wb");
|
||||
if (!f) return 1;
|
||||
header[0] = 0x464b414e; /* NAKF */
|
||||
header[1] = (unsigned int)s->w;
|
||||
header[2] = (unsigned int)s->h;
|
||||
header[3] = (unsigned int)(DT * 1000 + 0.5f);
|
||||
fwrite(header, sizeof header, 1, f);
|
||||
frames = (int)(s->seconds / DT + 0.5f);
|
||||
for (i = 0; i < frames; ++i) {
|
||||
nka_update(anim, i ? DT : 0);
|
||||
nk_input_begin(&ctx);
|
||||
nk_input_end(&ctx);
|
||||
s->draw((float)i * DT);
|
||||
render(ground);
|
||||
fwrite(fb, (size_t)fb_w * (size_t)fb_h * 4, 1, f);
|
||||
}
|
||||
fclose(f);
|
||||
nka_destroy(anim);
|
||||
nk_free(&ctx);
|
||||
free(fb);
|
||||
free(tex);
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
#ifndef EXAMPLES_NK_H
|
||||
#define EXAMPLES_NK_H
|
||||
|
||||
#define NK_INCLUDE_FIXED_TYPES
|
||||
#define NK_INCLUDE_STANDARD_IO
|
||||
#define NK_INCLUDE_DEFAULT_ALLOCATOR
|
||||
#define NK_INCLUDE_VERTEX_BUFFER_OUTPUT
|
||||
#define NK_INCLUDE_FONT_BAKING
|
||||
#include "nuklear.h"
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,7 @@
|
||||
/* Nuklear's implementation on its own, so its warnings stay out of the examples. */
|
||||
#define _CRT_SECURE_NO_WARNINGS
|
||||
#ifdef _MSC_VER
|
||||
#pragma warning(disable: 4116 4127 4701 5287)
|
||||
#endif
|
||||
#define NK_IMPLEMENTATION
|
||||
#include "nk.h"
|
||||
@@ -0,0 +1,462 @@
|
||||
#include <stdio.h>
|
||||
|
||||
#define NK_INCLUDE_DEFAULT_ALLOCATOR
|
||||
#include "nuklear.h"
|
||||
#define NUKANIM_IMPLEMENTATION
|
||||
#include "nukanim.h"
|
||||
|
||||
static int failures;
|
||||
|
||||
static void check(int ok, const char *what, int line)
|
||||
{
|
||||
if (ok) return;
|
||||
printf("core.c:%d: %s\n", line, what);
|
||||
failures++;
|
||||
}
|
||||
|
||||
#define CHECK(c) check((c) != 0, #c, __LINE__)
|
||||
#define NEAR(x, y, eps) (fabsf((float)(x) - (float)(y)) <= (eps))
|
||||
|
||||
static const struct nka_ease linear = { NKA_EASE_LINEAR, 0, 0, 0, 0 };
|
||||
|
||||
/* ImAnimate's curves, as ImAnimate.cpp writes them, in its ECurve order. */
|
||||
static float imanimate(int curve, float p)
|
||||
{
|
||||
const float pi = 3.14159f;
|
||||
switch (curve) {
|
||||
case 0: return p;
|
||||
case 1: return 1 - cosf((p * pi) / 2);
|
||||
case 2: return sinf((p * pi) / 2);
|
||||
case 3: return -(cosf(pi * p) - 1) / 2;
|
||||
case 4: return p * p;
|
||||
case 5: return 1 - (1 - p) * (1 - p);
|
||||
case 6: return p < 0.5f ? 2 * p * p : 1 - powf(-2 * p + 2, 2) / 2.0f;
|
||||
case 7: return p * p * p;
|
||||
case 8: return 1 - powf(1 - p, 3);
|
||||
case 9: return p < 0.5f ? 4 * p * p * p : 1 - powf(-2 * p + 2, 3) / 2.0f;
|
||||
case 10: return p * p * p * p;
|
||||
case 11: return 1 - powf(1 - p, 4);
|
||||
case 12: return p < 0.5f ? 8 * p * p * p * p : 1 - powf(-2 * p + 2, 4) / 2.0f;
|
||||
case 13: return p * p * p * p * p;
|
||||
case 14: return 1 - powf(1 - p, 5);
|
||||
case 15: return p < 0.5f ? 16 * p * p * p * p * p : 1 - powf(-2 * p + 2, 5) / 2.0f;
|
||||
case 16: return p == 0 ? 0 : powf(2, 10 * p - 10);
|
||||
case 17: return p == 1 ? 1 : 1 - powf(2, -10 * p);
|
||||
case 18: return p == 0 ? 0 : p == 1 ? 1 : p < 0.5f ? powf(2, 20 * p - 10) / 2.0f : (2 - powf(2, -20 * p + 10)) / 2.0f;
|
||||
case 19: return 1 - sqrtf(1 - powf(p, 2));
|
||||
case 20: return sqrtf(1 - powf(p - 1, 2));
|
||||
default: return p < 0.5f ? (1 - sqrtf(1 - powf(2 * p, 2))) / 2.0f : (sqrtf(1 - powf(-2 * p + 2, 2)) + 1) / 2.0f;
|
||||
}
|
||||
}
|
||||
|
||||
static const int imanimate_curves[22] = {
|
||||
NKA_EASE_LINEAR,
|
||||
NKA_EASE_IN_SINE, NKA_EASE_OUT_SINE, NKA_EASE_IN_OUT_SINE,
|
||||
NKA_EASE_IN_QUAD, NKA_EASE_OUT_QUAD, NKA_EASE_IN_OUT_QUAD,
|
||||
NKA_EASE_IN_CUBIC, NKA_EASE_OUT_CUBIC, NKA_EASE_IN_OUT_CUBIC,
|
||||
NKA_EASE_IN_QUART, NKA_EASE_OUT_QUART, NKA_EASE_IN_OUT_QUART,
|
||||
NKA_EASE_IN_QUINT, NKA_EASE_OUT_QUINT, NKA_EASE_IN_OUT_QUINT,
|
||||
NKA_EASE_IN_EXPO, NKA_EASE_OUT_EXPO, NKA_EASE_IN_OUT_EXPO,
|
||||
NKA_EASE_IN_CIRC, NKA_EASE_OUT_CIRC, NKA_EASE_IN_OUT_CIRC
|
||||
};
|
||||
|
||||
static float square(float t) { return t * t; }
|
||||
|
||||
static void test_easing(void)
|
||||
{
|
||||
struct nka_context *a = nka_create(0);
|
||||
int type, i, k;
|
||||
for (type = NKA_EASE_LINEAR; type <= NKA_EASE_IN_OUT_BOUNCE; ++type) {
|
||||
CHECK(NEAR(nka_eval_preset(type, 0), 0, 1e-6f));
|
||||
CHECK(NEAR(nka_eval_preset(type, 1), 1, 1e-5f));
|
||||
}
|
||||
for (type = NKA_EASE_IN_QUAD; type <= NKA_EASE_IN_BOUNCE; type += 3) {
|
||||
CHECK(NEAR(nka_eval_preset(type + 2, 0.5f), 0.5f, 1e-5f));
|
||||
for (k = 0; k <= 20; ++k) {
|
||||
float t = (float)k / 20;
|
||||
CHECK(NEAR(nka_eval_preset(type + 1, t), 1 - nka_eval_preset(type, 1 - t), 1e-5f));
|
||||
}
|
||||
}
|
||||
for (i = 0; i < 22; ++i)
|
||||
for (k = 0; k <= 64; ++k) {
|
||||
float t = (float)k / 64;
|
||||
CHECK(NEAR(nka_eval_preset(imanimate_curves[i], t), imanimate(i, t), 1e-5f));
|
||||
}
|
||||
|
||||
for (k = 0; k <= 20; ++k) {
|
||||
float t = (float)k / 20;
|
||||
struct nka_ease in_back = nka_ease(NKA_EASE_IN_BACK), in_el = nka_ease(NKA_EASE_IN_ELASTIC);
|
||||
in_back.p0 = 1.70158f;
|
||||
in_el.p0 = 1;
|
||||
in_el.p1 = 0.3f;
|
||||
CHECK(NEAR(nka_eval(a, nka_ease_back(1.70158f), t), nka_eval_preset(NKA_EASE_OUT_BACK, t), 1e-5f));
|
||||
CHECK(NEAR(nka_eval(a, in_back, t), nka_eval_preset(NKA_EASE_IN_BACK, t), 1e-5f));
|
||||
CHECK(NEAR(nka_eval(a, nka_ease_elastic(1, 0.3f), t), nka_eval_preset(NKA_EASE_OUT_ELASTIC, t), 1e-5f));
|
||||
CHECK(NEAR(nka_eval(a, in_el, t), nka_eval_preset(NKA_EASE_IN_ELASTIC, t), 1e-5f));
|
||||
CHECK(nka_eval(a, nka_ease_elastic(0.5f, 0.3f), t) == nka_eval(a, nka_ease_elastic(0.5f, 0.3f), t));
|
||||
CHECK(NEAR(nka_eval(a, nka_ease_bezier(0, 0, 1, 1), t), t, 1e-4f));
|
||||
}
|
||||
CHECK(NEAR(nka_eval(a, nka_ease_back(3), 0.8f), 1 - nka__back(0.2f, 3), 1e-6f));
|
||||
CHECK(nka_eval(a, nka_ease_back(3), 0.8f) > 1);
|
||||
|
||||
CHECK(nka_eval(a, nka_ease_steps(4, 0), 0) == 0);
|
||||
CHECK(nka_eval(a, nka_ease_steps(4, 0), 0.3f) == 0.25f);
|
||||
CHECK(nka_eval(a, nka_ease_steps(4, 0), 0.99f) == 0.75f);
|
||||
CHECK(nka_eval(a, nka_ease_steps(4, 0), 1) == 1);
|
||||
CHECK(nka_eval(a, nka_ease_steps(4, 1), 0) == 0.25f);
|
||||
CHECK(nka_eval(a, nka_ease_steps(4, 1), 0.3f) == 0.5f);
|
||||
CHECK(nka_eval(a, nka_ease_steps(4, 1), 1) == 1);
|
||||
CHECK(NEAR(nka_eval(a, nka_ease_steps(4, 2), 0), 0.2f, 1e-6f));
|
||||
CHECK(NEAR(nka_eval(a, nka_ease_steps(4, 2), 0.3f), 0.4f, 1e-6f));
|
||||
CHECK(NEAR(nka_eval(a, nka_ease_steps(4, 2), 0.99f), 0.8f, 1e-6f));
|
||||
CHECK(nka_eval(a, nka_ease_steps(4, 2), 1) == 1);
|
||||
|
||||
/* CSS ease at the middle */
|
||||
CHECK(NEAR(nka_eval(a, nka_ease_bezier(0.25f, 0.1f, 0.25f, 1), 0.5f), 0.8024f, 1e-3f));
|
||||
|
||||
CHECK(NEAR(nka_eval(a, nka_ease_spring(0, 0, 0, 0), 0), 0, 1e-6f));
|
||||
CHECK(NEAR(nka_eval(a, nka_ease_spring(0, 0, 0, 0), 1), 1, 1e-3f));
|
||||
CHECK(nka_eval(a, nka_ease_spring(1, 120, 20, 8), 0.01f) > nka_eval(a, nka_ease_spring(1, 120, 20, 0), 0.01f));
|
||||
{
|
||||
float prev = 0;
|
||||
for (k = 1; k <= 50; ++k) {
|
||||
float v = nka_eval(a, nka_ease_spring(1, 120, 100, 0), (float)k / 50);
|
||||
CHECK(v > prev && v < 1);
|
||||
prev = v;
|
||||
}
|
||||
CHECK(NEAR(nka_eval(a, nka_ease_spring(1, 100, 20, 0), 0.5f), 1 - expf(-5) * (1 + 5), 1e-5f));
|
||||
}
|
||||
|
||||
CHECK(nka_eval(a, nka_ease_custom(3), 0.5f) == 0.5f);
|
||||
nka_register_ease(a, 3, square);
|
||||
CHECK(nka_custom_ease(a, 3) == square);
|
||||
CHECK(nka_eval(a, nka_ease_custom(3), 0.5f) == 0.25f);
|
||||
nka_register_ease(a, 16, square);
|
||||
CHECK(nka_custom_ease(a, 16) == 0);
|
||||
nka_destroy(a);
|
||||
}
|
||||
|
||||
static void test_tweens(void)
|
||||
{
|
||||
struct nka_context *a = nka_create(0);
|
||||
nk_hash id = nka_id("box"), x = nka_id("x");
|
||||
float v;
|
||||
|
||||
nka_update(a, 0.016f);
|
||||
CHECK(nka_tween_float(a, id, x, 0, 1, linear, NKA_POLICY_CROSSFADE, 0) == 0);
|
||||
CHECK(a->chans.len == 0);
|
||||
CHECK(nka_tween_float(a, id, x, 10, 1, linear, NKA_POLICY_CROSSFADE, 0) == 0);
|
||||
CHECK(a->chans.len == 1 && nka_busy(a));
|
||||
nka_update(a, 0.25f);
|
||||
CHECK(NEAR(nka_tween_float(a, id, x, 10, 1, linear, NKA_POLICY_CROSSFADE, 0), 2.5f, 1e-4f));
|
||||
nka_update(a, 0.5f);
|
||||
CHECK(NEAR(nka_tween_float(a, id, x, 10, 1, linear, NKA_POLICY_CROSSFADE, 0), 7.5f, 1e-4f));
|
||||
CHECK(NEAR(nka_tween_float(a, id, x, 0, 1, linear, NKA_POLICY_CROSSFADE, 0), 7.5f, 1e-4f));
|
||||
nka_update(a, 0.5f);
|
||||
CHECK(NEAR(nka_tween_float(a, id, x, 0, 1, linear, NKA_POLICY_CROSSFADE, 0), 3.75f, 1e-4f));
|
||||
CHECK(nka_busy(a));
|
||||
nka_update(a, 0.6f);
|
||||
CHECK(nka_tween_float(a, id, x, 0, 1, linear, NKA_POLICY_CROSSFADE, 0) == 0);
|
||||
CHECK(!nka_busy(a));
|
||||
|
||||
nka_set_lazy_init(a, 0);
|
||||
CHECK(nka_tween_float(a, id, nka_id("eager"), 3, 1, linear, NKA_POLICY_CROSSFADE, 3) == 3);
|
||||
CHECK(a->chans.len == 2 && !nka_busy(a));
|
||||
nka_set_lazy_init(a, 1);
|
||||
|
||||
CHECK(nka_tween_float(a, id, nka_id("cut"), 5, 1, linear, NKA_POLICY_CUT, 0) == 5);
|
||||
CHECK(!nka_busy(a));
|
||||
|
||||
v = nka_tween_float(a, id, nka_id("q"), 10, 1, linear, NKA_POLICY_QUEUE, 0);
|
||||
CHECK(v == 0);
|
||||
nka_update(a, 0.5f);
|
||||
CHECK(NEAR(nka_tween_float(a, id, nka_id("q"), 20, 1, linear, NKA_POLICY_QUEUE, 0), 5, 1e-4f));
|
||||
nka_update(a, 0.25f);
|
||||
CHECK(NEAR(nka_tween_float(a, id, nka_id("q"), 20, 1, linear, NKA_POLICY_QUEUE, 0), 7.5f, 1e-4f));
|
||||
nka_update(a, 0.25f);
|
||||
CHECK(NEAR(nka_tween_float(a, id, nka_id("q"), 20, 1, linear, NKA_POLICY_QUEUE, 0), 10, 1e-4f));
|
||||
nka_update(a, 0.5f);
|
||||
CHECK(NEAR(nka_tween_float(a, id, nka_id("q"), 20, 1, linear, NKA_POLICY_QUEUE, 0), 15, 1e-4f));
|
||||
|
||||
CHECK(nka_tween_float(a, id, nka_id("add"), 5, 1, linear, NKA_POLICY_ADDITIVE, 0) == 0);
|
||||
nka_update(a, 1);
|
||||
CHECK(nka_tween_float(a, id, nka_id("add"), 5, 1, linear, NKA_POLICY_ADDITIVE, 0) == 5);
|
||||
nka_update(a, 1);
|
||||
CHECK(nka_tween_float(a, id, nka_id("add"), 5, 1, linear, NKA_POLICY_ADDITIVE, 0) == 5);
|
||||
CHECK(nka_tween_float(a, id, nka_id("add"), 3, 1, linear, NKA_POLICY_ADDITIVE, 0) == 5);
|
||||
nka_update(a, 1);
|
||||
CHECK(nka_tween_float(a, id, nka_id("add"), 3, 1, linear, NKA_POLICY_ADDITIVE, 0) == 8);
|
||||
|
||||
CHECK(nka_tween_float(a, id, nka_id("mul"), 3, 1, linear, NKA_POLICY_MULTIPLY, 2) == 2);
|
||||
nka_update(a, 0.5f);
|
||||
CHECK(NEAR(nka_tween_float(a, id, nka_id("mul"), 3, 1, linear, NKA_POLICY_MULTIPLY, 2), 4, 1e-4f));
|
||||
nka_update(a, 0.5f);
|
||||
CHECK(nka_tween_float(a, id, nka_id("mul"), 3, 1, linear, NKA_POLICY_MULTIPLY, 2) == 6);
|
||||
|
||||
nka_tween_float(a, id, nka_id("re"), 10, 1, linear, NKA_POLICY_CROSSFADE, 0);
|
||||
nka_update(a, 0.5f);
|
||||
CHECK(NEAR(nka_tween_float(a, id, nka_id("re"), 10, 1, linear, NKA_POLICY_CROSSFADE, 0), 5, 1e-4f));
|
||||
nka_rebase_float(a, id, nka_id("re"), 20);
|
||||
nka_update(a, 0.25f);
|
||||
CHECK(NEAR(nka_tween_float(a, id, nka_id("re"), 20, 1, linear, NKA_POLICY_CROSSFADE, 0), 12.5f, 1e-4f));
|
||||
nka_update(a, 0.25f);
|
||||
CHECK(nka_tween_float(a, id, nka_id("re"), 20, 1, linear, NKA_POLICY_CROSSFADE, 0) == 20);
|
||||
nka_rebase_float(a, id, nka_id("none"), 1);
|
||||
|
||||
CHECK(nka_tween_int(a, id, nka_id("i"), 10, 1, linear, NKA_POLICY_CROSSFADE, 0) == 0);
|
||||
nka_update(a, 0.25f);
|
||||
CHECK(nka_tween_int(a, id, nka_id("i"), 10, 1, linear, NKA_POLICY_CROSSFADE, 0) == 3);
|
||||
nka_update(a, 0.04f);
|
||||
CHECK(nka_tween_int(a, id, nka_id("i"), 10, 1, linear, NKA_POLICY_CROSSFADE, 0) == 3);
|
||||
nka_update(a, 1);
|
||||
CHECK(nka_tween_int(a, id, nka_id("i"), 10, 1, linear, NKA_POLICY_CROSSFADE, 0) == 10);
|
||||
nka_rebase_int(a, id, nka_id("i"), -4);
|
||||
CHECK(nka_tween_int(a, id, nka_id("i"), -4, 1, linear, NKA_POLICY_CROSSFADE, 0) == -4);
|
||||
|
||||
{
|
||||
struct nk_vec2 init = { 0, 0 }, to = { 100, 50 }, r;
|
||||
struct nka_vec4 z4 = { 0, 0, 0, 0 }, t4 = { 1, 2, 3, 4 }, r4;
|
||||
struct nka_ease_axes axes;
|
||||
axes.x = linear;
|
||||
axes.y = nka_ease(NKA_EASE_IN_QUAD);
|
||||
axes.z = linear;
|
||||
axes.w = linear;
|
||||
r = nka_tween_vec2(a, id, nka_id("v2"), to, 1, linear, NKA_POLICY_CROSSFADE, init);
|
||||
CHECK(r.x == 0 && r.y == 0);
|
||||
nka_tween_vec2_per_axis(a, id, nka_id("pa"), to, 1, axes, NKA_POLICY_CROSSFADE);
|
||||
r4 = nka_tween_vec4(a, id, nka_id("v4"), t4, 1, linear, NKA_POLICY_CROSSFADE, z4);
|
||||
CHECK(r4.w == 0);
|
||||
nka_update(a, 0.5f);
|
||||
r = nka_tween_vec2(a, id, nka_id("v2"), to, 1, linear, NKA_POLICY_CROSSFADE, init);
|
||||
CHECK(NEAR(r.x, 50, 1e-3f) && NEAR(r.y, 25, 1e-3f));
|
||||
r = nka_tween_vec2_per_axis(a, id, nka_id("pa"), to, 1, axes, NKA_POLICY_CROSSFADE);
|
||||
CHECK(NEAR(r.x, 50, 1e-3f) && NEAR(r.y, 12.5f, 1e-3f));
|
||||
r4 = nka_tween_vec4(a, id, nka_id("v4"), t4, 1, linear, NKA_POLICY_CROSSFADE, z4);
|
||||
CHECK(NEAR(r4.x, 0.5f, 1e-4f) && NEAR(r4.w, 2, 1e-4f));
|
||||
nka_rebase_vec2(a, id, nka_id("v2"), init);
|
||||
nka_rebase_vec4(a, id, nka_id("v4"), z4);
|
||||
nka_update(a, 0.5f);
|
||||
r = nka_tween_vec2(a, id, nka_id("v2"), init, 1, linear, NKA_POLICY_CROSSFADE, init);
|
||||
CHECK(r.x == 0 && r.y == 0);
|
||||
r4 = nka_tween_vec4(a, id, nka_id("v4"), z4, 1, linear, NKA_POLICY_CROSSFADE, z4);
|
||||
CHECK(r4.x == 0 && r4.w == 0);
|
||||
}
|
||||
{
|
||||
struct nk_vec2 half = { 0.5f, 0.5f }, bias = { 10, 0 }, size = { 200, 100 }, r;
|
||||
nka_set_anchor(a, NKA_ANCHOR_WINDOW, size);
|
||||
CHECK(nka_anchor(a, NKA_ANCHOR_WINDOW).x == 200 && nka_anchor(a, 9).x == 0);
|
||||
r = nka_tween_vec2_rel(a, id, nka_id("rel"), half, bias, 1, linear, NKA_POLICY_CUT, NKA_ANCHOR_WINDOW);
|
||||
CHECK(r.x == 110 && r.y == 50);
|
||||
CHECK(nka_tween_float_rel(a, id, nka_id("relf"), 0.25f, 1, 1, linear, NKA_POLICY_CUT, NKA_ANCHOR_WINDOW, 1) == 26);
|
||||
}
|
||||
nka_destroy(a);
|
||||
}
|
||||
|
||||
static float forty_two(void *user) { (void)user; return 42; }
|
||||
|
||||
static void test_time(void)
|
||||
{
|
||||
struct nka_context *a = nka_create(0);
|
||||
nk_hash id = nka_id("t");
|
||||
nka_tween_float(a, id, 0, 1, 1, linear, NKA_POLICY_CROSSFADE, 0);
|
||||
nka_set_time_scale(a, 0.5f);
|
||||
CHECK(nka_time_scale(a) == 0.5f);
|
||||
nka_update(a, 1);
|
||||
CHECK(nka_time(a) == 0.5);
|
||||
CHECK(NEAR(nka_tween_float(a, id, 0, 1, 1, linear, NKA_POLICY_CROSSFADE, 0), 0.5f, 1e-5f));
|
||||
nka_set_time_scale(a, -1);
|
||||
CHECK(nka_time_scale(a) == 0);
|
||||
nka_update(a, 1);
|
||||
CHECK(nka_time(a) == 0.5);
|
||||
CHECK(nka_tween_float_resolved(a, id, 1, forty_two, 0, 1, linear, NKA_POLICY_CUT) == 42);
|
||||
nka_destroy(a);
|
||||
}
|
||||
|
||||
static void test_colors(void)
|
||||
{
|
||||
struct nka_context *a = nka_create(0);
|
||||
struct nk_colorf black = { 0, 0, 0, 1 }, white = { 1, 1, 1, 1 }, red = { 1, 0, 0, 1 }, pink = { 1, 0, 0.6f, 1 }, c;
|
||||
int space, r, g, b;
|
||||
for (space = NKA_COL_SRGB; space <= NKA_COL_OKLCH; ++space)
|
||||
for (r = 0; r <= 4; ++r)
|
||||
for (g = 0; g <= 4; ++g)
|
||||
for (b = 0; b <= 4; ++b) {
|
||||
struct nka_vec4 in = { (float)r / 4, (float)g / 4, (float)b / 4, 0.5f };
|
||||
struct nka_vec4 out = nka__from_space(nka__to_space(in, space), space);
|
||||
CHECK(NEAR(in.x, out.x, 1e-4f) && NEAR(in.y, out.y, 1e-4f) && NEAR(in.z, out.z, 1e-4f) && out.w == 0.5f);
|
||||
}
|
||||
c = nka_color_blend(red, pink, 0.5f, NKA_COL_HSV);
|
||||
CHECK(NEAR(c.r, 1, 1e-4f) && NEAR(c.g, 0, 1e-4f) && NEAR(c.b, 0.3f, 1e-4f));
|
||||
for (space = NKA_COL_SRGB; space <= NKA_COL_OKLCH; ++space) {
|
||||
c = nka_color_blend(red, pink, 0, space);
|
||||
CHECK(NEAR(c.r, 1, 1e-4f) && NEAR(c.b, 0, 1e-4f));
|
||||
c = nka_color_blend(red, pink, 1, space);
|
||||
CHECK(NEAR(c.r, 1, 1e-4f) && NEAR(c.b, 0.6f, 1e-4f));
|
||||
}
|
||||
c = nka_color_blend(black, white, 0.5f, NKA_COL_SRGB_LINEAR);
|
||||
CHECK(NEAR(c.r, 0.7354f, 1e-3f));
|
||||
|
||||
c = nka_tween_color(a, 1, 2, white, 1, linear, NKA_POLICY_CROSSFADE, NKA_COL_OKLAB, black);
|
||||
CHECK(c.r == 0 && c.a == 1);
|
||||
nka_update(a, 0.5f);
|
||||
c = nka_tween_color(a, 1, 2, white, 1, linear, NKA_POLICY_CROSSFADE, NKA_COL_OKLAB, black);
|
||||
CHECK(c.r > 0.3f && c.r < 0.6f && NEAR(c.r, c.g, 1e-4f));
|
||||
nka_update(a, 0.5f);
|
||||
c = nka_tween_color(a, 1, 2, white, 1, linear, NKA_POLICY_CROSSFADE, NKA_COL_OKLAB, black);
|
||||
CHECK(c.r == 1 && c.g == 1 && c.b == 1);
|
||||
nka_rebase_color(a, 1, 2, red);
|
||||
nka_update(a, 0.1f);
|
||||
c = nka_tween_color(a, 1, 2, red, 1, linear, NKA_POLICY_CROSSFADE, NKA_COL_OKLAB, black);
|
||||
CHECK(c.r == 1 && c.g == 0);
|
||||
{
|
||||
struct nka_ease_axes axes;
|
||||
axes.x = axes.y = axes.z = axes.w = linear;
|
||||
c = nka_tween_color_per_axis(a, 1, 3, red, 0, axes, NKA_POLICY_CUT, NKA_COL_OKLCH);
|
||||
CHECK(NEAR(c.r, 1, 1e-4f) && NEAR(c.g, 0, 1e-4f));
|
||||
}
|
||||
nka_destroy(a);
|
||||
}
|
||||
|
||||
static void test_animate(void)
|
||||
{
|
||||
struct nka_context *a = nka_create(0);
|
||||
float size = 1, other = 0.5f;
|
||||
nka_update(a, 0);
|
||||
nka_animate(a, 1, 1.1f, 500, &size, NKA_EASE_IN_CUBIC);
|
||||
CHECK(size == 1 && nka_busy(a));
|
||||
nka_update(a, 0.25f);
|
||||
nka_animate(a, 1, 1.1f, 500, &size, NKA_EASE_IN_CUBIC);
|
||||
CHECK(NEAR(size, 1.0125f, 1e-5f));
|
||||
nka_animate(a, 1.1f, 1, 500, &size, NKA_EASE_OUT_CUBIC);
|
||||
CHECK(NEAR(size, 1.0125f, 1e-5f));
|
||||
nka_update(a, 0.125f);
|
||||
nka_animate(a, 1.1f, 1, 500, &size, NKA_EASE_OUT_CUBIC);
|
||||
CHECK(NEAR(size, 1.1f - 0.1f * (1 - 0.25f * 0.25f * 0.25f), 1e-5f));
|
||||
nka_update(a, 0.125f);
|
||||
nka_animate(a, 1.1f, 1, 500, &size, NKA_EASE_OUT_CUBIC);
|
||||
CHECK(size == 1);
|
||||
nka_update(a, 0.1f);
|
||||
nka_animate(a, 1.1f, 1, 500, &size, NKA_EASE_OUT_CUBIC);
|
||||
CHECK(size == 1 && !nka_busy(a));
|
||||
|
||||
nka_animate(a, 0, 1, 1000, &other, NKA_EASE_LINEAR);
|
||||
CHECK(NEAR(other, 0.5f, 1e-6f));
|
||||
nka_update(a, 0.25f);
|
||||
nka_animate(a, 0, 1, 1000, &other, NKA_EASE_LINEAR);
|
||||
CHECK(NEAR(other, 0.75f, 1e-5f));
|
||||
other = 3;
|
||||
nka_animate(a, 0, 1, 1000, &other, NKA_EASE_LINEAR);
|
||||
CHECK(other == 1);
|
||||
other = 0.2f;
|
||||
nka_animate(a, 0, 1, 0, &other, NKA_EASE_LINEAR);
|
||||
CHECK(other == 1);
|
||||
nka_destroy(a);
|
||||
}
|
||||
|
||||
static void test_gc_and_map(void)
|
||||
{
|
||||
struct nka_context *a = nka_create(0);
|
||||
int i, frame;
|
||||
for (i = 0; i < 1000; ++i) nka_tween_float(a, (nk_hash)i, 0, 1, 1, linear, NKA_POLICY_CUT, 0);
|
||||
CHECK(a->chans.len == 1000);
|
||||
for (frame = 0; frame < 10; ++frame) {
|
||||
nka_update(a, 0.016f);
|
||||
for (i = 0; i < 1000; i += 2) nka_tween_float(a, (nk_hash)i, 0, 1, 1, linear, NKA_POLICY_CUT, 0);
|
||||
}
|
||||
nka_gc(a, 5);
|
||||
CHECK(a->chans.len == 500);
|
||||
for (i = 0; i < 1000; ++i) {
|
||||
struct nka__chan *c = (struct nka__chan *)nka__get(&a->chans, nka__key((nk_hash)i, 0, NKA__FLOAT));
|
||||
CHECK((c != 0) == !(i & 1));
|
||||
if (c) CHECK(c->cur[0] == 1 && c->id == (nk_hash)i);
|
||||
}
|
||||
nka_clear(a);
|
||||
CHECK(a->chans.len == 0 && a->chans.cap == 0);
|
||||
nka_reserve(a, 100);
|
||||
CHECK(a->chans.cap == 256);
|
||||
|
||||
{
|
||||
static unsigned char in[40000];
|
||||
struct nka__map m = { 0, 0, 0, 0, sizeof(int) };
|
||||
nk_hash s = 7;
|
||||
for (i = 0; i < 40000; ++i) {
|
||||
int *v = (int *)nka__put(a, &m, (nk_hash)i * 2654435761u, 0);
|
||||
*v = i;
|
||||
in[i] = 1;
|
||||
}
|
||||
for (i = 0; i < 60000; ++i) {
|
||||
int k;
|
||||
s = s * 1103515245u + 12345u;
|
||||
k = (int)((s >> 8) % 40000);
|
||||
nka__del(&m, (nk_hash)k * 2654435761u);
|
||||
in[k] = 0;
|
||||
}
|
||||
frame = 0;
|
||||
for (i = 0; i < 40000; ++i) {
|
||||
int *v = (int *)nka__get(&m, (nk_hash)i * 2654435761u);
|
||||
frame += in[i];
|
||||
CHECK((v != 0) == (in[i] != 0));
|
||||
if (v) CHECK(*v == i);
|
||||
}
|
||||
CHECK(m.len == frame);
|
||||
nka__map_free(a, &m);
|
||||
*(int *)nka__put(a, &m, 0, 0) = 10;
|
||||
*(int *)nka__put(a, &m, 1, 0) = 11;
|
||||
CHECK(m.len == 2 && *(int *)nka__get(&m, 0) == 10 && *(int *)nka__get(&m, 1) == 11);
|
||||
nka__del(&m, 0);
|
||||
CHECK(!nka__get(&m, 0) && *(int *)nka__get(&m, 1) == 11);
|
||||
nka__map_free(a, &m);
|
||||
}
|
||||
CHECK(nka_id("") == 2166136261u && nka_id("a") == 0xe40c292cu && nka_id(0) == 2166136261u);
|
||||
nka_destroy(a);
|
||||
}
|
||||
|
||||
static int live;
|
||||
|
||||
static void *count_alloc(nk_handle h, void *old, nk_size n)
|
||||
{
|
||||
(void)h; (void)old;
|
||||
live++;
|
||||
return malloc(n);
|
||||
}
|
||||
|
||||
static void count_free(nk_handle h, void *p)
|
||||
{
|
||||
(void)h;
|
||||
live--;
|
||||
free(p);
|
||||
}
|
||||
|
||||
static void test_allocator(void)
|
||||
{
|
||||
struct nk_allocator al;
|
||||
struct nka_context *a;
|
||||
float v = 0;
|
||||
int i;
|
||||
al.userdata.ptr = 0;
|
||||
al.alloc = count_alloc;
|
||||
al.free = count_free;
|
||||
a = nka_create(&al);
|
||||
for (i = 0; i < 300; ++i) nka_tween_float(a, (nk_hash)i, 1, 1, 1, linear, NKA_POLICY_CROSSFADE, 0);
|
||||
nka_animate(a, 0, 1, 100, &v, NKA_EASE_LINEAR);
|
||||
CHECK(live > 1);
|
||||
nka_destroy(a);
|
||||
CHECK(live == 0);
|
||||
CHECK(nka_tween_float(0, 1, 1, 3, 1, linear, NKA_POLICY_CROSSFADE, 0) == 3);
|
||||
nka_update(0, 1);
|
||||
nka_destroy(0);
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
test_easing();
|
||||
test_tweens();
|
||||
test_time();
|
||||
test_colors();
|
||||
test_animate();
|
||||
test_gc_and_map();
|
||||
test_allocator();
|
||||
if (failures) printf("%d failed\n", failures);
|
||||
else printf("core: ok\n");
|
||||
return failures != 0;
|
||||
}
|
||||
@@ -0,0 +1,951 @@
|
||||
#include <stdio.h>
|
||||
|
||||
#define NK_INCLUDE_DEFAULT_ALLOCATOR
|
||||
#include "nuklear.h"
|
||||
#define NUKANIM_IMPLEMENTATION
|
||||
#include "nukanim.h"
|
||||
|
||||
static int failures;
|
||||
|
||||
static void check(int ok, const char *what, int line)
|
||||
{
|
||||
if (ok) return;
|
||||
printf("fx.c:%d: %s\n", line, what);
|
||||
failures++;
|
||||
}
|
||||
|
||||
#define CHECK(c) check((c) != 0, #c, __LINE__)
|
||||
#define NEAR(x, y, eps) (fabsf((float)(x) - (float)(y)) <= (eps))
|
||||
|
||||
static const struct nka_ease linear = { NKA_EASE_LINEAR, 0, 0, 0, 0 };
|
||||
static const float tau = 6.2831853f;
|
||||
|
||||
static struct nk_colorf rgba(float r, float g, float b, float a)
|
||||
{
|
||||
struct nk_colorf c;
|
||||
c.r = r; c.g = g; c.b = b; c.a = a;
|
||||
return c;
|
||||
}
|
||||
|
||||
static struct nk_colorf average(struct nk_colorf x, struct nk_colorf y)
|
||||
{
|
||||
return rgba((x.r + y.r) / 2, (x.g + y.g) / 2, (x.b + y.b) / 2, (x.a + y.a) / 2);
|
||||
}
|
||||
|
||||
static int same_color(struct nk_colorf x, struct nk_colorf y, float eps)
|
||||
{
|
||||
return NEAR(x.r, y.r, eps) && NEAR(x.g, y.g, eps) && NEAR(x.b, y.b, eps) && NEAR(x.a, y.a, eps);
|
||||
}
|
||||
|
||||
static int unit_color(struct nk_colorf c)
|
||||
{
|
||||
return c.r >= 0 && c.r <= 1 && c.g >= 0 && c.g <= 1 && c.b >= 0 && c.b <= 1 && c.a >= 0 && c.a <= 1;
|
||||
}
|
||||
|
||||
static int same_vec2(struct nk_vec2 x, struct nk_vec2 y, float eps) { return NEAR(x.x, y.x, eps) && NEAR(x.y, y.y, eps); }
|
||||
|
||||
static int same_gradient(const struct nka_gradient *x, const struct nka_gradient *y)
|
||||
{
|
||||
int i;
|
||||
if (x->count != y->count) return 0;
|
||||
for (i = 0; i < x->count; ++i)
|
||||
if (x->positions[i] != y->positions[i] || !same_color(x->colors[i], y->colors[i], 0)) return 0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
static struct nka_transform xf(float x, float y, float rotation, float sx, float sy)
|
||||
{
|
||||
return nka_transform(nka__vec2(x, y), rotation, nka__vec2(sx, sy));
|
||||
}
|
||||
|
||||
static int same_xf(struct nka_transform x, struct nka_transform y)
|
||||
{
|
||||
return x.position.x == y.position.x && x.position.y == y.position.y && x.rotation == y.rotation
|
||||
&& x.scale.x == y.scale.x && x.scale.y == y.scale.y;
|
||||
}
|
||||
|
||||
static void test_waves(void)
|
||||
{
|
||||
static const float phase[5] = { 0, 0.25f, 0.5f, 0.75f, 0.125f };
|
||||
static const float shape[4][5] = {
|
||||
{ 0, 1, 0, -1, 0.70710678f },
|
||||
{ -1, 0, 1, 0, -0.5f },
|
||||
{ -1, -0.5f, 0, 0.5f, -0.75f },
|
||||
{ 1, 1, -1, -1, 1 }
|
||||
};
|
||||
struct nka_context *a = nka_create(0);
|
||||
struct nk_vec2 amp2 = { 2, 3 }, f2 = { 1, 2 }, p2 = { 0.25f, 0.125f }, v2;
|
||||
struct nka_vec4 amp4 = { 1, 2, 3, 4 }, f4 = { 1, 1, 1, 1 }, p4 = { 0, 0.25f, 0.5f, 0.75f }, v4;
|
||||
int wave, k;
|
||||
|
||||
for (wave = NKA_WAVE_SINE; wave <= NKA_WAVE_SQUARE; ++wave)
|
||||
for (k = 0; k < 5; ++k) {
|
||||
CHECK(NEAR(nka_oscillate(a, 1, 3, 1, wave, phase[k]), 3 * shape[wave][k], 1e-5f));
|
||||
CHECK(NEAR(nka_oscillate(a, 1, 3, 1, wave, phase[k] + 2), 3 * shape[wave][k], 1e-5f));
|
||||
CHECK(NEAR(nka_oscillate(a, 1, 3, 1, wave, phase[k] - 1), 3 * shape[wave][k], 1e-5f));
|
||||
}
|
||||
CHECK(nka_busy(a));
|
||||
v2 = nka_oscillate_vec2(a, 2, amp2, f2, NKA_WAVE_SINE, p2);
|
||||
CHECK(NEAR(v2.x, 2, 1e-5f) && NEAR(v2.y, 3 * 0.70710678f, 1e-5f));
|
||||
v4 = nka_oscillate_vec4(a, 3, amp4, f4, NKA_WAVE_TRIANGLE, p4);
|
||||
CHECK(NEAR(v4.x, -1, 1e-5f) && NEAR(v4.y, 0, 1e-5f) && NEAR(v4.z, 3, 1e-5f) && NEAR(v4.w, 0, 1e-5f));
|
||||
CHECK(nka_oscillate_int(a, 4, 5, 1, NKA_WAVE_SAWTOOTH, 0.75f) == 3);
|
||||
CHECK(nka_oscillate_int(a, 4, 5, 1, NKA_WAVE_SAWTOOTH, 0.25f) == -3);
|
||||
CHECK(nka_oscillate_int(a, 4, 5, 1, NKA_WAVE_SAWTOOTH, 0.5f) == 0);
|
||||
CHECK(nka_oscillate_int(a, 4, 5, 1, NKA_WAVE_SINE, 0.25f) == 5);
|
||||
nka_destroy(a);
|
||||
|
||||
for (wave = NKA_WAVE_SINE; wave <= NKA_WAVE_SQUARE; ++wave) {
|
||||
a = nka_create(0);
|
||||
for (k = 1; k <= 8; ++k) {
|
||||
nka_update(a, 0.125f);
|
||||
CHECK(NEAR(nka_oscillate(a, 7, 2, 2, wave, 0), 2 * shape[wave][k % 4], 1e-5f));
|
||||
}
|
||||
nka_destroy(a);
|
||||
}
|
||||
|
||||
a = nka_create(0);
|
||||
nka_update(a, 0.1f);
|
||||
v2 = nka_oscillate_vec2(a, 2, amp2, f2, NKA_WAVE_SINE, p2);
|
||||
CHECK(NEAR(v2.x, 2 * sinf(tau * (0.1f + 0.25f)), 1e-5f) && NEAR(v2.y, 3 * sinf(tau * (0.2f + 0.125f)), 1e-5f));
|
||||
nka_destroy(a);
|
||||
|
||||
for (wave = NKA_WAVE_SINE; wave <= NKA_WAVE_SQUARE; ++wave) {
|
||||
struct nk_vec2 f2b = { 1.3f, 0.7f }, p2b = { 0.1f, 0.6f };
|
||||
struct nka_vec4 f4b = { 0.9f, 1.7f, 2.3f, 0.4f }, p4b = { 0, 0.3f, 0.5f, 0.9f };
|
||||
float lo = 0, hi = 0;
|
||||
int out = 0;
|
||||
a = nka_create(0);
|
||||
for (k = 0; k < 400; ++k) {
|
||||
float v;
|
||||
int iv;
|
||||
nka_update(a, 0.0137f);
|
||||
v = nka_oscillate(a, 1, 4, 1.1f, wave, 0);
|
||||
iv = nka_oscillate_int(a, 2, 7, 0.9f, wave, 0.2f);
|
||||
v2 = nka_oscillate_vec2(a, 3, amp2, f2b, wave, p2b);
|
||||
v4 = nka_oscillate_vec4(a, 4, amp4, f4b, wave, p4b);
|
||||
out += fabsf(v) > 4 + 1e-5f || iv < -7 || iv > 7 || !nka_busy(a);
|
||||
out += fabsf(v2.x) > 2 + 1e-5f || fabsf(v2.y) > 3 + 1e-5f;
|
||||
out += fabsf(v4.x) > 1 + 1e-5f || fabsf(v4.y) > 2 + 1e-5f || fabsf(v4.z) > 3 + 1e-5f || fabsf(v4.w) > 4 + 1e-5f;
|
||||
if (v < lo) lo = v;
|
||||
if (v > hi) hi = v;
|
||||
}
|
||||
CHECK(out == 0 && lo < -3.8f && hi > 3.8f);
|
||||
nka_destroy(a);
|
||||
}
|
||||
|
||||
{
|
||||
struct nk_colorf gray = rgba(0.5f, 0.5f, 0.5f, 1), red = rgba(1, 0, 0, 1), sky = rgba(0.2f, 0.5f, 1, 1);
|
||||
struct nk_colorf beige = rgba(0.6f, 0.5f, 0.4f, 0.8f), c;
|
||||
struct nka_vec4 d = { 0.25f, 0.25f, 0.25f, 0 }, hue = { 0.5f, 0, 0, 0 }, dim = { 0, 0, -0.5f, 0 };
|
||||
struct nka_vec4 turn = { 0, 0, 0.5f, 0 }, big = { 2, 2, 2, 2 }, lch, rot;
|
||||
int space, out = 0;
|
||||
a = nka_create(0);
|
||||
c = nka_oscillate_color(a, 5, gray, d, 1, NKA_WAVE_SINE, 0.25f, NKA_COL_SRGB);
|
||||
CHECK(same_color(c, rgba(0.75f, 0.75f, 0.75f, 1), 1e-5f));
|
||||
c = nka_oscillate_color(a, 5, red, hue, 1, NKA_WAVE_SINE, 0.25f, NKA_COL_HSV);
|
||||
CHECK(same_color(c, rgba(0, 1, 1, 1), 1e-4f));
|
||||
c = nka_oscillate_color(a, 5, sky, dim, 1, NKA_WAVE_SINE, 0.25f, NKA_COL_HSV);
|
||||
CHECK(same_color(c, rgba(0.1f, 0.25f, 0.5f, 1), 1e-4f));
|
||||
c = nka_oscillate_color(a, 5, beige, turn, 1, NKA_WAVE_SINE, 0.25f, NKA_COL_OKLCH);
|
||||
lch = nka__to_space(nka__cv(beige), NKA_COL_OKLCH);
|
||||
rot = nka__to_space(nka__cv(c), NKA_COL_OKLCH);
|
||||
CHECK(NEAR(rot.x, lch.x, 1e-3f) && NEAR(rot.y, lch.y, 1e-3f) && NEAR(fmodf(rot.z - lch.z + 1, 1), 0.5f, 1e-3f));
|
||||
CHECK(c.a == 0.8f);
|
||||
for (space = NKA_COL_SRGB; space <= NKA_COL_OKLCH; ++space)
|
||||
for (k = 0; k < 100; ++k) {
|
||||
nka_update(a, 0.05f);
|
||||
out += !unit_color(nka_oscillate_color(a, 6, sky, big, 1.3f, k & 3, 0, space));
|
||||
}
|
||||
CHECK(out == 0);
|
||||
nka_destroy(a);
|
||||
}
|
||||
}
|
||||
|
||||
static void test_frames(void)
|
||||
{
|
||||
struct nka_context *a = nka_create(0);
|
||||
struct nk_vec2 one = { 1, 1 }, half = { 0.5f, 0.5f }, zero = { 0, 0 };
|
||||
struct nka__fx_clock *c;
|
||||
float v;
|
||||
int k;
|
||||
|
||||
nka_update(a, 0.25f);
|
||||
v = nka_oscillate(a, 9, 1, 0.5f, NKA_WAVE_SAWTOOTH, 0);
|
||||
CHECK(v == -0.75f && v == nka_oscillate(a, 9, 1, 0.5f, NKA_WAVE_SAWTOOTH, 0));
|
||||
CHECK(nka_oscillate_vec2(a, 9, one, half, NKA_WAVE_SAWTOOTH, zero).x == v);
|
||||
nka_update(a, 0.25f);
|
||||
CHECK(nka_oscillate(a, 9, 1, 0.5f, NKA_WAVE_SAWTOOTH, 0) == -0.5f);
|
||||
c = (struct nka__fx_clock *)nka__get(&a->fx_clocks, nka_id_mix(9, (nk_hash)NKA__FX_OSC));
|
||||
CHECK(c && c->time == 0.5);
|
||||
if (c) c->time = 86400;
|
||||
for (k = 0; k < 15; ++k) {
|
||||
nka_update(a, 1.0f / 60);
|
||||
v = nka_oscillate(a, 9, 1, 4, NKA_WAVE_SAWTOOTH, 0.5f);
|
||||
}
|
||||
CHECK(NEAR(v, 0, 1e-4f));
|
||||
|
||||
nka_update(a, 0.25f);
|
||||
v = nka_wiggle(a, 9, 10, 3);
|
||||
CHECK(v == nka_wiggle(a, 9, 10, 3));
|
||||
v = nka_noise_channel_float(a, 9, 1.3f, 10, 0);
|
||||
CHECK(v == nka_noise_channel_float(a, 9, 1.3f, 10, 0));
|
||||
nka_smooth_noise_float(a, 9, 10, 1.3f);
|
||||
c = (struct nka__fx_clock *)nka__get(&a->fx_clocks, nka_id_mix(9, (nk_hash)NKA__FX_WIGGLE));
|
||||
CHECK(c && c->time == 0.25);
|
||||
c = (struct nka__fx_clock *)nka__get(&a->fx_clocks, nka_id_mix(9, (nk_hash)NKA__FX_NOISE));
|
||||
CHECK(c && c->time == 0.25);
|
||||
|
||||
nka_trigger_shake(a, 9);
|
||||
nka_update(a, 0.25f);
|
||||
v = nka_shake(a, 9, 10, 20, 1);
|
||||
CHECK(v != 0 && v == nka_shake(a, 9, 10, 20, 1));
|
||||
c = (struct nka__fx_clock *)nka__get(&a->fx_clocks, nka_id_mix(9, (nk_hash)NKA__FX_SHAKE));
|
||||
CHECK(c && c->since == 0.25);
|
||||
nka_destroy(a);
|
||||
}
|
||||
|
||||
static void test_shake(void)
|
||||
{
|
||||
struct nka_context *a = nka_create(0);
|
||||
const float dt = 1.0f / 64;
|
||||
struct nk_vec2 i2 = { 10, 10 }, v2;
|
||||
struct nka_vec4 i4 = { 1, 2, 3, 4 }, ci = { 0.3f, 0.3f, 0.3f, 0 }, v4;
|
||||
struct nk_colorf gray = rgba(0.5f, 0.5f, 0.5f, 1), c = gray;
|
||||
int frame, moved = 0, out = 0, axes[4] = { 0, 0, 0, 0 }, k;
|
||||
float v;
|
||||
|
||||
nka_update(a, dt);
|
||||
CHECK(nka_shake(a, 1, 10, 20, 0.5f) == 0 && !nka_busy(a));
|
||||
nka_trigger_shake(a, 1);
|
||||
for (frame = 1; frame <= 40; ++frame) {
|
||||
float env = 1 - (float)frame * dt / 0.5f;
|
||||
nka_update(a, dt);
|
||||
v = nka_shake(a, 1, 10, 20, 0.5f);
|
||||
if (env > 0) {
|
||||
out += fabsf(v) > 10 * env * env + 1e-4f || !nka_busy(a);
|
||||
moved += fabsf(v) > 0.5f;
|
||||
} else {
|
||||
out += v != 0 || nka_busy(a);
|
||||
}
|
||||
}
|
||||
CHECK(out == 0 && moved > 5);
|
||||
nka_trigger_shake(a, 1);
|
||||
nka_update(a, dt);
|
||||
v = nka_shake(a, 1, 10, 20, 0.5f);
|
||||
CHECK(v != 0 && nka_busy(a));
|
||||
|
||||
moved = 0;
|
||||
nka_trigger_shake(a, 2);
|
||||
for (frame = 1; frame <= 40; ++frame) {
|
||||
nka_update(a, dt);
|
||||
v2 = nka_shake_vec2(a, 2, i2, 20, 0.5f);
|
||||
v4 = nka_shake_vec4(a, 2, i4, 20, 0.5f);
|
||||
k = nka_shake_int(a, 2, 10, 20, 0.5f);
|
||||
c = nka_shake_color(a, 2, gray, ci, 20, 0.5f, NKA_COL_OKLAB);
|
||||
moved += fabsf(v2.y) > 0.5f;
|
||||
axes[0] += fabsf(v4.x) > 0.05f;
|
||||
axes[1] += fabsf(v4.y) > 0.1f;
|
||||
axes[2] += fabsf(v4.z) > 0.15f;
|
||||
axes[3] += fabsf(v4.w) > 0.2f;
|
||||
out += fabsf(v2.x) > 10 || fabsf(v2.y) > 10 || k < -10 || k > 10 || !unit_color(c);
|
||||
out += fabsf(v4.x) > 1 || fabsf(v4.y) > 2 || fabsf(v4.z) > 3 || fabsf(v4.w) > 4;
|
||||
}
|
||||
CHECK(out == 0 && moved > 5 && axes[0] > 5 && axes[1] > 5 && axes[2] > 5 && axes[3] > 5);
|
||||
CHECK(same_color(c, gray, 1e-4f));
|
||||
nka_trigger_shake(a, 2);
|
||||
nka_update(a, dt);
|
||||
v2 = nka_shake_vec2(a, 2, i2, 20, 0.5f);
|
||||
CHECK(v2.x != 0 && v2.x == nka_shake(a, 2, 10, 20, 0.5f));
|
||||
|
||||
for (frame = 0; frame < 4; ++frame) {
|
||||
nka_update(a, dt);
|
||||
c = nka_shake_color(a, 3, gray, ci, 20, 0.5f, NKA_COL_SRGB);
|
||||
}
|
||||
CHECK(same_color(c, gray, 0) && !nka_busy(a));
|
||||
nka_trigger_shake(a, 4);
|
||||
nka_update(a, dt);
|
||||
CHECK(nka_shake(a, 4, 10, 20, 0) == 0 && !nka_busy(a));
|
||||
nka_trigger_shake(a, 5);
|
||||
nka_update(a, dt);
|
||||
v = nka_shake(a, 5, 10, 0, 1);
|
||||
CHECK(v == v && fabsf(v) <= 10);
|
||||
nka_destroy(a);
|
||||
}
|
||||
|
||||
static void test_wiggle(void)
|
||||
{
|
||||
struct nka_context *a = nka_create(0);
|
||||
const float dt = 1.0f / 60;
|
||||
struct nk_vec2 amp2 = { 3, 7 }, v2;
|
||||
struct nka_vec4 amp4 = { 1, 2, 3, 4 }, v4;
|
||||
struct nk_colorf sky = rgba(0.2f, 0.5f, 1, 1);
|
||||
float prev = 0, v;
|
||||
int frame, changed = 0, out = 0, k, space;
|
||||
for (frame = 0; frame < 600; ++frame) {
|
||||
nka_update(a, dt);
|
||||
v = nka_wiggle(a, 1, 10, 3);
|
||||
out += fabsf(v) > 10 || !nka_busy(a);
|
||||
if (frame) {
|
||||
out += fabsf(v - prev) > 10 * 2 * 1.5f * 3 * dt + 1e-4f;
|
||||
changed += fabsf(v - prev) > 0.01f;
|
||||
}
|
||||
prev = v;
|
||||
v2 = nka_wiggle_vec2(a, 2, amp2, 2);
|
||||
v4 = nka_wiggle_vec4(a, 3, amp4, 5);
|
||||
k = nka_wiggle_int(a, 4, 6, 1);
|
||||
out += fabsf(v2.x) > 3 || fabsf(v2.y) > 7 || k < -6 || k > 6;
|
||||
out += fabsf(v4.x) > 1 || fabsf(v4.y) > 2 || fabsf(v4.z) > 3 || fabsf(v4.w) > 4;
|
||||
for (space = NKA_COL_SRGB; space <= NKA_COL_OKLCH; ++space)
|
||||
out += !unit_color(nka_wiggle_color(a, (nk_hash)(5 + space), sky, amp4, 2, space));
|
||||
}
|
||||
CHECK(out == 0 && changed > 200);
|
||||
nka_destroy(a);
|
||||
}
|
||||
|
||||
static float roughness(int type, int octaves, int dims)
|
||||
{
|
||||
struct nka_noise_opts o = nka_noise_opts_default();
|
||||
float sum = 0, h = 0.02f;
|
||||
int i;
|
||||
o.type = type;
|
||||
o.octaves = octaves;
|
||||
for (i = 1; i < 400; ++i) {
|
||||
float x = (float)i * 0.0371f, y = 0.37f;
|
||||
if (dims == 3)
|
||||
sum += fabsf(nka_noise_3d(x + h, y, 0.5f, &o) - 2 * nka_noise_3d(x, y, 0.5f, &o) + nka_noise_3d(x - h, y, 0.5f, &o));
|
||||
else
|
||||
sum += fabsf(nka_noise_2d(x + h, y, &o) - 2 * nka_noise_2d(x, y, &o) + nka_noise_2d(x - h, y, &o));
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
|
||||
static void test_noise(void)
|
||||
{
|
||||
struct nka_noise_opts o = nka_noise_opts_default();
|
||||
int type, octaves, i, j;
|
||||
float v;
|
||||
|
||||
CHECK(o.type == NKA_NOISE_PERLIN && o.octaves == 4 && o.persistence == 0.5f && o.lacunarity == 2 && o.seed == 0);
|
||||
CHECK(nka_noise_2d(1.3f, 2.7f, 0) == nka_noise_2d(1.3f, 2.7f, &o));
|
||||
CHECK(nka_noise_3d(1.3f, 2.7f, 0.1f, 0) == nka_noise_3d(1.3f, 2.7f, 0.1f, &o));
|
||||
for (type = NKA_NOISE_PERLIN; type <= NKA_NOISE_WORLEY; ++type) {
|
||||
int differs2 = 0, differs3 = 0, same = 1;
|
||||
for (octaves = 1; octaves <= 6; octaves += 5) {
|
||||
float lo2 = 2, hi2 = -2, lo3 = 2, hi3 = -2;
|
||||
o.type = type;
|
||||
o.octaves = octaves;
|
||||
o.seed = 3;
|
||||
for (i = 0; i < 64; ++i)
|
||||
for (j = 0; j < 64; ++j) {
|
||||
float x = (float)i * 0.173f - 5, y = (float)j * 0.191f - 6, z = (float)((i * 7 + j) % 13) * 0.37f;
|
||||
float n2 = nka_noise_2d(x, y, &o), n3 = nka_noise_3d(x, y, z, &o);
|
||||
if (n2 < lo2) lo2 = n2;
|
||||
if (n2 > hi2) hi2 = n2;
|
||||
if (n3 < lo3) lo3 = n3;
|
||||
if (n3 > hi3) hi3 = n3;
|
||||
}
|
||||
CHECK(lo2 >= -1 && hi2 <= 1 && hi2 - lo2 > 0.4f);
|
||||
CHECK(lo3 >= -1 && hi3 <= 1 && hi3 - lo3 > 0.4f);
|
||||
}
|
||||
o.octaves = 3;
|
||||
for (i = 0; i < 50; ++i) {
|
||||
float x = (float)i * 0.731f - 9, y = (float)i * 0.317f + 2, z = (float)i * 0.113f;
|
||||
o.seed = 7;
|
||||
v = nka_noise_2d(x, y, &o);
|
||||
same &= v == nka_noise_2d(x, y, &o);
|
||||
o.seed = 8;
|
||||
differs2 += v != nka_noise_2d(x, y, &o);
|
||||
o.seed = 7;
|
||||
v = nka_noise_3d(x, y, z, &o);
|
||||
same &= v == nka_noise_3d(x, y, z, &o);
|
||||
o.seed = 8;
|
||||
differs3 += v != nka_noise_3d(x, y, z, &o);
|
||||
}
|
||||
CHECK(same && differs2 > 40 && differs3 > 40);
|
||||
CHECK(roughness(type, 4, 2) > 1.5f * roughness(type, 1, 2));
|
||||
CHECK(roughness(type, 4, 3) > 1.5f * roughness(type, 1, 3));
|
||||
}
|
||||
|
||||
for (type = NKA_NOISE_SIMPLEX; type <= NKA_NOISE_WORLEY; ++type) {
|
||||
struct nka_noise_opts perlin = nka_noise_opts_default();
|
||||
int differs = 0;
|
||||
o = perlin;
|
||||
o.type = type;
|
||||
for (i = 0; i < 50; ++i) {
|
||||
float x = (float)i * 0.531f - 7, y = (float)i * 0.277f + 1, z = (float)i * 0.193f - 3;
|
||||
differs += nka_noise_3d(x, y, z, &o) != nka_noise_3d(x, y, z, &perlin);
|
||||
}
|
||||
CHECK(differs > 40);
|
||||
}
|
||||
|
||||
o = nka_noise_opts_default();
|
||||
o.octaves = 0;
|
||||
v = nka_noise_2d(0.3f, 0.4f, &o);
|
||||
o.octaves = 1;
|
||||
CHECK(v == nka_noise_2d(0.3f, 0.4f, &o));
|
||||
{
|
||||
float first = nka_noise_2d(0.3f, 0.4f, &o), second = nka_noise_2d(0.6f, 0.8f, &o);
|
||||
o.octaves = 2;
|
||||
o.persistence = -1;
|
||||
v = nka_noise_2d(0.3f, 0.4f, &o);
|
||||
CHECK(NEAR(v, (first - second) / 2, 1e-6f));
|
||||
}
|
||||
|
||||
{
|
||||
struct nka_context *a = nka_create(0);
|
||||
struct nk_vec2 f2 = { 0.5f, 0.7f }, a2 = { 30, 20 }, v2;
|
||||
struct nka_vec4 f4 = { 0.3f, 0.4f, 0.5f, 0.6f }, a4 = { 1, 2, 3, 4 }, v4, big = { 1, 1, 1, 1 };
|
||||
struct nk_colorf sky = rgba(0.2f, 0.5f, 1, 1);
|
||||
float prev = 0, sprev = 0, s;
|
||||
int frame, changed = 0, schanged = 0, out = 0, space;
|
||||
o = nka_noise_opts_default();
|
||||
o.type = NKA_NOISE_SIMPLEX;
|
||||
for (frame = 1; frame <= 128; ++frame) {
|
||||
nka_update(a, 1.0f / 64);
|
||||
v = nka_noise_channel_float(a, 1, 1.5f, 5, &o);
|
||||
out += v != nka_noise_2d((float)frame / 64 * 1.5f, NKA__FX_ROW0, &o) * 5;
|
||||
out += fabsf(v) > 5 || !nka_busy(a);
|
||||
changed += v != prev;
|
||||
prev = v;
|
||||
v2 = nka_noise_channel_vec2(a, 2, f2, a2, &o);
|
||||
out += fabsf(v2.x) > 30 || fabsf(v2.y) > 20;
|
||||
v4 = nka_noise_channel_vec4(a, 3, f4, a4, &o);
|
||||
out += fabsf(v4.x) > 1 || fabsf(v4.y) > 2 || fabsf(v4.z) > 3 || fabsf(v4.w) > 4;
|
||||
s = nka_smooth_noise_float(a, 4, 10, 2);
|
||||
schanged += s != sprev;
|
||||
sprev = s;
|
||||
out += fabsf(s) > 10;
|
||||
v2 = nka_smooth_noise_vec2(a, 5, a2, 1);
|
||||
out += fabsf(v2.x) > 30 || fabsf(v2.y) > 20;
|
||||
v4 = nka_smooth_noise_vec4(a, 6, a4, 1);
|
||||
out += fabsf(v4.x) > 1 || fabsf(v4.y) > 2 || fabsf(v4.z) > 3 || fabsf(v4.w) > 4;
|
||||
for (space = NKA_COL_SRGB; space <= NKA_COL_OKLCH; ++space) {
|
||||
out += !unit_color(nka_noise_channel_color(a, (nk_hash)(7 + space), sky, big, 2, &o, space));
|
||||
out += !unit_color(nka_smooth_noise_color(a, (nk_hash)(20 + space), sky, big, 2, space));
|
||||
}
|
||||
}
|
||||
CHECK(out == 0 && changed > 100 && schanged > 100);
|
||||
nka_destroy(a);
|
||||
}
|
||||
}
|
||||
|
||||
static void test_gradients(void)
|
||||
{
|
||||
struct nk_colorf red = rgba(1, 0, 0, 1), green = rgba(0, 1, 0, 1), blue = rgba(0, 0, 1, 1);
|
||||
struct nk_colorf white = rgba(1, 1, 1, 1), black = rgba(0, 0, 0, 1);
|
||||
struct nk_color byte_red = { 255, 0, 0, 255 };
|
||||
struct nka_gradient g = { 0 }, h = { 0 }, x, y, r;
|
||||
int i, space, sorted = 1, inside = 1, same = 1;
|
||||
|
||||
CHECK(nka_gradient_add(&g, 1, blue) && nka_gradient_add(&g, 0, red) && nka_gradient_add(&g, 0.5f, green));
|
||||
CHECK(g.count == 3 && g.positions[0] == 0 && g.positions[1] == 0.5f && g.positions[2] == 1);
|
||||
CHECK(same_color(g.colors[0], red, 0) && same_color(g.colors[1], green, 0) && same_color(g.colors[2], blue, 0));
|
||||
for (space = NKA_COL_SRGB; space <= NKA_COL_OKLCH; ++space) {
|
||||
CHECK(same_color(nka_gradient_sample(&g, 0, space), red, 0));
|
||||
CHECK(same_color(nka_gradient_sample(&g, 0.5f, space), green, 0));
|
||||
CHECK(same_color(nka_gradient_sample(&g, 1, space), blue, 0));
|
||||
CHECK(same_color(nka_gradient_sample(&g, -3, space), red, 0));
|
||||
CHECK(same_color(nka_gradient_sample(&g, 7, space), blue, 0));
|
||||
CHECK(same_color(nka_gradient_sample(&g, 0.25f, space), nka_color_blend(red, green, 0.5f, space), 1e-6f));
|
||||
CHECK(same_color(nka_gradient_sample(&g, 0.875f, space), nka_color_blend(green, blue, 0.75f, space), 1e-6f));
|
||||
}
|
||||
CHECK(same_color(nka_gradient_sample(&g, 0.25f, NKA_COL_SRGB), rgba(0.5f, 0.5f, 0, 1), 1e-6f));
|
||||
CHECK(!same_color(nka_gradient_sample(&g, 0.25f, NKA_COL_OKLAB), rgba(0.5f, 0.5f, 0, 1), 0.05f));
|
||||
|
||||
CHECK(same_color(nka_gradient_sample(&h, 0.3f, NKA_COL_OKLAB), white, 0));
|
||||
CHECK(same_color(nka_gradient_sample(0, 0.3f, NKA_COL_OKLAB), white, 0));
|
||||
CHECK(nka_gradient_add(&h, 0.4f, red) && same_color(nka_gradient_sample(&h, 0.9f, NKA_COL_OKLAB), red, 0));
|
||||
CHECK(!nka_gradient_add(0, 0, red));
|
||||
|
||||
h = nka_gradient_two_color(black, white);
|
||||
CHECK(nka_gradient_add(&h, 0.5f, red) && nka_gradient_add(&h, 0.5f, blue));
|
||||
CHECK(h.count == 4 && same_color(h.colors[1], red, 0) && same_color(h.colors[2], blue, 0));
|
||||
CHECK(same_color(nka_gradient_sample(&h, 0.5f, NKA_COL_SRGB), red, 0));
|
||||
CHECK(same_color(nka_gradient_sample(&h, 0.499f, NKA_COL_SRGB), red, 0.01f));
|
||||
CHECK(same_color(nka_gradient_sample(&h, 0.501f, NKA_COL_SRGB), blue, 0.01f));
|
||||
|
||||
nka__zero(&x, sizeof x);
|
||||
for (i = 0; i < NKA_GRADIENT_MAX; ++i)
|
||||
CHECK(nka_gradient_add(&x, (float)(NKA_GRADIENT_MAX - 1 - i) / (NKA_GRADIENT_MAX - 1), i & 1 ? white : black));
|
||||
CHECK(!nka_gradient_add(&x, 0.5f, red) && x.count == NKA_GRADIENT_MAX);
|
||||
for (i = 1; i < x.count; ++i) sorted &= x.positions[i] > x.positions[i - 1];
|
||||
CHECK(sorted);
|
||||
|
||||
nka__zero(&y, sizeof y);
|
||||
CHECK(nka_gradient_add_rgba(&y, 0.3f, byte_red) && same_color(y.colors[0], red, 0) && y.positions[0] == 0.3f);
|
||||
r = nka_gradient_solid(green);
|
||||
CHECK(r.count == 2 && r.positions[0] == 0 && r.positions[1] == 1);
|
||||
CHECK(same_color(r.colors[0], green, 0) && same_color(r.colors[1], green, 0));
|
||||
CHECK(same_color(nka_gradient_sample(&r, 0.37f, NKA_COL_OKLAB), green, 1e-4f));
|
||||
r = nka_gradient_two_color(red, blue);
|
||||
CHECK(r.count == 2 && same_color(r.colors[0], red, 0) && same_color(r.colors[1], blue, 0));
|
||||
r = nka_gradient_three_color(red, green, blue);
|
||||
CHECK(r.count == 3 && r.positions[1] == 0.5f && same_color(r.colors[1], green, 0) && same_color(r.colors[2], blue, 0));
|
||||
|
||||
x = nka_gradient_two_color(red, blue);
|
||||
y = nka_gradient_three_color(black, white, black);
|
||||
for (space = NKA_COL_SRGB; space <= NKA_COL_OKLCH; ++space) {
|
||||
r = nka_gradient_lerp(&x, &y, 0, space);
|
||||
CHECK(r.count == 3);
|
||||
for (i = 0; i < 3; ++i) CHECK(same_color(r.colors[i], nka_gradient_sample(&x, r.positions[i], space), 0));
|
||||
r = nka_gradient_lerp(&x, &y, 1, space);
|
||||
for (i = 0; i < 3; ++i) CHECK(same_color(r.colors[i], nka_gradient_sample(&y, r.positions[i], space), 0));
|
||||
}
|
||||
r = nka_gradient_lerp(&x, &y, 0.5f, NKA_COL_SRGB);
|
||||
for (i = 0; i <= 20; ++i) {
|
||||
float t = (float)i / 20;
|
||||
same &= same_color(nka_gradient_sample(&r, t, NKA_COL_SRGB),
|
||||
average(nka_gradient_sample(&x, t, NKA_COL_SRGB), nka_gradient_sample(&y, t, NKA_COL_SRGB)), 1e-6f);
|
||||
}
|
||||
CHECK(same && same_color(nka_gradient_sample(&r, 0.5f, NKA_COL_SRGB), rgba(0.75f, 0.5f, 0.75f, 1), 1e-6f));
|
||||
|
||||
r = nka_gradient_lerp(&h, &h, 0.5f, NKA_COL_SRGB);
|
||||
CHECK(r.count == 4);
|
||||
for (i = 0; i <= 1000; ++i)
|
||||
same &= same_color(nka_gradient_sample(&r, (float)i / 1000, NKA_COL_SRGB), nka_gradient_sample(&h, (float)i / 1000, NKA_COL_SRGB), 1e-6f);
|
||||
CHECK(same && same_color(nka_gradient_sample(&r, 0.501f, NKA_COL_SRGB), blue, 0.01f));
|
||||
r = nka_gradient_lerp(&h, &g, 0.5f, NKA_COL_SRGB);
|
||||
CHECK(r.count == 4);
|
||||
for (i = 0; i <= 1000; ++i) {
|
||||
float t = (float)i / 1000;
|
||||
same &= same_color(nka_gradient_sample(&r, t, NKA_COL_SRGB),
|
||||
average(nka_gradient_sample(&h, t, NKA_COL_SRGB), nka_gradient_sample(&g, t, NKA_COL_SRGB)), 1e-6f);
|
||||
}
|
||||
CHECK(same);
|
||||
|
||||
nka__zero(&x, sizeof x);
|
||||
nka__zero(&y, sizeof y);
|
||||
for (i = 0; i < NKA_GRADIENT_MAX; ++i) {
|
||||
nka_gradient_add(&x, (float)i / (NKA_GRADIENT_MAX - 1), i & 1 ? red : blue);
|
||||
nka_gradient_add(&y, ((float)i + 0.5f) / NKA_GRADIENT_MAX, i & 1 ? green : black);
|
||||
}
|
||||
r = nka_gradient_lerp(&x, &y, 0.5f, NKA_COL_OKLAB);
|
||||
CHECK(r.count == NKA_GRADIENT_MAX && r.positions[0] == 0 && r.positions[NKA_GRADIENT_MAX - 1] == 1);
|
||||
for (i = 1; i < r.count; ++i) sorted &= r.positions[i] >= r.positions[i - 1];
|
||||
for (i = 0; i < r.count; ++i) inside &= unit_color(r.colors[i]);
|
||||
CHECK(sorted && inside);
|
||||
|
||||
nka__zero(&y, sizeof y);
|
||||
nka_gradient_add(&y, 0, black);
|
||||
nka_gradient_add(&y, 1, white);
|
||||
for (i = 0; i < NKA_GRADIENT_MAX - 2; ++i) {
|
||||
float p = ((float)i + 0.5f) / (NKA_GRADIENT_MAX - 1);
|
||||
nka_gradient_add(&y, p, rgba(p, p, p, 1));
|
||||
}
|
||||
r = nka_gradient_lerp(&x, &y, 0.5f, NKA_COL_SRGB);
|
||||
CHECK(r.count == NKA_GRADIENT_MAX);
|
||||
for (i = 0; i <= 300; ++i) {
|
||||
float t = (float)i / 300;
|
||||
same &= same_color(nka_gradient_sample(&r, t, NKA_COL_SRGB),
|
||||
average(nka_gradient_sample(&x, t, NKA_COL_SRGB), nka_gradient_sample(&y, t, NKA_COL_SRGB)), 1e-5f);
|
||||
}
|
||||
CHECK(same);
|
||||
r = nka_gradient_lerp(0, 0, 0.5f, NKA_COL_OKLAB);
|
||||
CHECK(r.count == 0);
|
||||
}
|
||||
|
||||
static void test_gradient_tween(void)
|
||||
{
|
||||
struct nka_context *a = nka_create(0);
|
||||
struct nka_gradient sunset = nka_gradient_three_color(rgba(1, 0.3f, 0, 1), rgba(1, 0.6f, 0.2f, 1), rgba(0.4f, 0.1f, 0.3f, 1));
|
||||
struct nka_gradient ocean = nka_gradient_two_color(rgba(0, 0.3f, 0.6f, 1), rgba(0, 0.9f, 0.9f, 1));
|
||||
struct nka_gradient forest = nka_gradient_solid(rgba(0.1f, 0.3f, 0.1f, 1)), r, r2, mid;
|
||||
int i, same = 1;
|
||||
|
||||
nka_update(a, 0.125f);
|
||||
r = nka_tween_gradient(a, 1, 2, &sunset, 1, linear, NKA_POLICY_CROSSFADE, NKA_COL_SRGB);
|
||||
CHECK(same_gradient(&r, &sunset) && !nka_busy(a));
|
||||
nka_update(a, 0.125f);
|
||||
r = nka_tween_gradient(a, 1, 2, &ocean, 1, linear, NKA_POLICY_CROSSFADE, NKA_COL_SRGB);
|
||||
CHECK(nka_busy(a));
|
||||
for (i = 0; i <= 20; ++i)
|
||||
same &= same_color(nka_gradient_sample(&r, (float)i / 20, NKA_COL_SRGB), nka_gradient_sample(&sunset, (float)i / 20, NKA_COL_SRGB), 1e-6f);
|
||||
nka_update(a, 0.5f);
|
||||
r = nka_tween_gradient(a, 1, 2, &ocean, 1, linear, NKA_POLICY_CROSSFADE, NKA_COL_SRGB);
|
||||
r2 = nka_tween_gradient(a, 1, 2, &ocean, 1, linear, NKA_POLICY_CROSSFADE, NKA_COL_SRGB);
|
||||
CHECK(same_gradient(&r, &r2) && nka_busy(a));
|
||||
for (i = 0; i <= 20; ++i) {
|
||||
float t = (float)i / 20;
|
||||
same &= same_color(nka_gradient_sample(&r, t, NKA_COL_SRGB),
|
||||
average(nka_gradient_sample(&sunset, t, NKA_COL_SRGB), nka_gradient_sample(&ocean, t, NKA_COL_SRGB)), 1e-6f);
|
||||
}
|
||||
CHECK(same);
|
||||
nka_update(a, 0.5f);
|
||||
r = nka_tween_gradient(a, 1, 2, &ocean, 1, linear, NKA_POLICY_CROSSFADE, NKA_COL_SRGB);
|
||||
CHECK(same_gradient(&r, &ocean) && !nka_busy(a));
|
||||
nka_update(a, 0.5f);
|
||||
r = nka_tween_gradient(a, 1, 2, &ocean, 1, linear, NKA_POLICY_CROSSFADE, NKA_COL_SRGB);
|
||||
CHECK(same_gradient(&r, &ocean) && !nka_busy(a));
|
||||
|
||||
nka_update(a, 0.125f);
|
||||
nka_tween_gradient(a, 1, 2, &forest, 1, linear, NKA_POLICY_CROSSFADE, NKA_COL_OKLAB);
|
||||
nka_update(a, 0.25f);
|
||||
mid = nka_tween_gradient(a, 1, 2, &forest, 1, linear, NKA_POLICY_CROSSFADE, NKA_COL_OKLAB);
|
||||
r = nka_tween_gradient(a, 1, 2, &sunset, 1, linear, NKA_POLICY_CROSSFADE, NKA_COL_OKLAB);
|
||||
for (i = 0; i <= 20; ++i)
|
||||
same &= same_color(nka_gradient_sample(&r, (float)i / 20, NKA_COL_OKLAB), nka_gradient_sample(&mid, (float)i / 20, NKA_COL_OKLAB), 1e-5f);
|
||||
CHECK(same && nka_busy(a));
|
||||
|
||||
r = nka_tween_gradient(a, 1, 2, &ocean, 1, linear, NKA_POLICY_CUT, NKA_COL_OKLAB);
|
||||
CHECK(same_gradient(&r, &ocean));
|
||||
nka_update(a, 0.125f);
|
||||
r = nka_tween_gradient(a, 1, 2, &ocean, 1, linear, NKA_POLICY_CUT, NKA_COL_OKLAB);
|
||||
CHECK(same_gradient(&r, &ocean) && !nka_busy(a));
|
||||
nka_destroy(a);
|
||||
}
|
||||
|
||||
static float turned(float from, float to, int mode, float t)
|
||||
{
|
||||
return nka_transform_lerp(xf(0, 0, from, 1, 1), xf(0, 0, to, 1, 1), t, mode).rotation;
|
||||
}
|
||||
|
||||
static int same_angle(float x, float y) { return NEAR(cosf(x), cosf(y), 1e-4f) && NEAR(sinf(x), sinf(y), 1e-4f); }
|
||||
|
||||
static void test_transforms(void)
|
||||
{
|
||||
struct nka_transform id = nka_transform_identity(), t1 = xf(10, 20, 0.5f, 2, 2), t2 = xf(-3, 4, -1.2f, 0.5f, 0.5f), r;
|
||||
struct nka_transform ts[4];
|
||||
struct nk_vec2 p = { 7, -2 }, q;
|
||||
float m[6], m2[6], late = tau - 0.1f;
|
||||
int i, k, mode;
|
||||
|
||||
CHECK(id.position.x == 0 && id.position.y == 0 && id.scale.x == 1 && id.scale.y == 1 && id.rotation == 0);
|
||||
q = nka_transform_apply(id, p);
|
||||
CHECK(q.x == 7 && q.y == -2);
|
||||
q = nka_transform_apply(xf(1, 2, 0, 3, 4), p);
|
||||
CHECK(q.x == 22 && q.y == -6);
|
||||
CHECK(same_vec2(nka_transform_apply(xf(0, 0, NKA__PI / 2, 1, 1), nka__vec2(1, 0)), nka__vec2(0, 1), 1e-6f));
|
||||
|
||||
r = nka_transform_compose(t1, t2);
|
||||
CHECK(same_vec2(nka_transform_apply(r, p), nka_transform_apply(t1, nka_transform_apply(t2, p)), 1e-4f));
|
||||
CHECK(NEAR(r.rotation, -0.7f, 1e-6f) && r.scale.x == 1 && r.scale.y == 1);
|
||||
t1.scale = nka__vec2(2, 0.5f);
|
||||
r = nka_transform_compose(t1, t2);
|
||||
CHECK(same_vec2(r.position, nka_transform_apply(t1, t2.position), 1e-5f) && r.scale.x == 1 && r.scale.y == 0.25f);
|
||||
t1.scale = nka__vec2(2, 2);
|
||||
r = nka_transform_inverse(t1);
|
||||
CHECK(same_vec2(nka_transform_apply(r, nka_transform_apply(t1, p)), p, 1e-4f));
|
||||
CHECK(same_vec2(nka_transform_apply(t1, nka_transform_apply(r, p)), p, 1e-4f));
|
||||
t1.scale = nka__vec2(3, 0.25f);
|
||||
r = nka_transform_compose(nka_transform_inverse(t1), t1);
|
||||
CHECK(same_vec2(r.position, nka__vec2(0, 0), 1e-4f) && NEAR(r.rotation, 0, 1e-6f));
|
||||
CHECK(NEAR(r.scale.x, 1, 1e-6f) && NEAR(r.scale.y, 1, 1e-6f));
|
||||
r = nka_transform_inverse(xf(0, 0, 0, 0, 2));
|
||||
CHECK(r.scale.x == 1 && r.scale.y == 0.5f);
|
||||
|
||||
CHECK(NEAR(turned(0.1f, late, NKA_ROTATION_SHORTEST, 1), -0.1f, 1e-5f));
|
||||
CHECK(NEAR(turned(0.1f, late, NKA_ROTATION_SHORTEST, 0.5f), 0, 1e-5f));
|
||||
CHECK(NEAR(turned(0.1f, late, NKA_ROTATION_LONGEST, 1), late, 1e-5f));
|
||||
CHECK(NEAR(turned(0.1f, late, NKA_ROTATION_LONGEST, 0.5f), NKA__PI, 1e-5f));
|
||||
CHECK(NEAR(turned(0.1f, late, NKA_ROTATION_CW, 1), late, 1e-5f));
|
||||
CHECK(NEAR(turned(0.1f, late, NKA_ROTATION_CCW, 1), -0.1f, 1e-5f));
|
||||
CHECK(NEAR(turned(0.1f, late, NKA_ROTATION_DIRECT, 0.5f), NKA__PI, 1e-5f));
|
||||
CHECK(NEAR(turned(late, 0.1f, NKA_ROTATION_SHORTEST, 1), tau + 0.1f, 1e-5f));
|
||||
CHECK(NEAR(turned(late, 0.1f, NKA_ROTATION_CW, 1), tau + 0.1f, 1e-5f));
|
||||
CHECK(NEAR(turned(late, 0.1f, NKA_ROTATION_CCW, 1), 0.1f, 1e-5f));
|
||||
CHECK(NEAR(turned(late, 0.1f, NKA_ROTATION_DIRECT, 1), 0.1f, 1e-5f));
|
||||
CHECK(NEAR(turned(3, -3, NKA_ROTATION_SHORTEST, 1), tau - 3, 1e-5f));
|
||||
CHECK(NEAR(turned(3, -3, NKA_ROTATION_SHORTEST, 0.5f), NKA__PI, 1e-5f));
|
||||
CHECK(NEAR(turned(3, -3, NKA_ROTATION_CW, 1), tau - 3, 1e-5f));
|
||||
CHECK(NEAR(turned(3, -3, NKA_ROTATION_CCW, 1), -3, 1e-5f));
|
||||
CHECK(NEAR(turned(3, -3, NKA_ROTATION_LONGEST, 1), -3, 1e-5f));
|
||||
CHECK(NEAR(turned(3, -3, NKA_ROTATION_DIRECT, 0.5f), 0, 1e-6f));
|
||||
CHECK(NEAR(turned(-3, 3, NKA_ROTATION_SHORTEST, 1), 3 - tau, 1e-5f));
|
||||
CHECK(NEAR(turned(-3, 3, NKA_ROTATION_SHORTEST, 0.5f), -NKA__PI, 1e-5f));
|
||||
CHECK(NEAR(turned(-3, 3, NKA_ROTATION_CW, 1), 3, 1e-5f));
|
||||
CHECK(NEAR(turned(-3, 3, NKA_ROTATION_CCW, 1), 3 - tau, 1e-5f));
|
||||
CHECK(NEAR(turned(-3, 3, NKA_ROTATION_LONGEST, 1), 3, 1e-5f));
|
||||
for (mode = NKA_ROTATION_SHORTEST; mode <= NKA_ROTATION_DIRECT; ++mode) {
|
||||
CHECK(same_angle(turned(3, -3, mode, 1), -3) && same_angle(turned(0.1f, late, mode, 1), late));
|
||||
CHECK(turned(1, 2, mode, 0) == 1);
|
||||
}
|
||||
for (k = 1; k <= 10; ++k) {
|
||||
CHECK(turned(3, -3, NKA_ROTATION_CW, (float)k / 10) > turned(3, -3, NKA_ROTATION_CW, (float)(k - 1) / 10));
|
||||
CHECK(turned(-3, 3, NKA_ROTATION_CCW, (float)k / 10) < turned(-3, 3, NKA_ROTATION_CCW, (float)(k - 1) / 10));
|
||||
}
|
||||
|
||||
ts[0] = xf(5, -7, 0.7f, 2, 3);
|
||||
ts[1] = xf(1, 2, -2.5f, -1.5f, 0.5f);
|
||||
ts[2] = xf(0, 0, 3, 1, -2);
|
||||
ts[3] = id;
|
||||
for (i = 0; i < 4; ++i) {
|
||||
nka_transform_to_matrix(ts[i], m);
|
||||
r = nka_transform_from_matrix(m[0], m[1], m[3], m[4], m[2], m[5]);
|
||||
nka_transform_to_matrix(r, m2);
|
||||
for (k = 0; k < 6; ++k) CHECK(NEAR(m[k], m2[k], 1e-5f));
|
||||
CHECK(same_vec2(nka_transform_apply(r, p), nka_transform_apply(ts[i], p), 1e-4f));
|
||||
}
|
||||
nka_transform_to_matrix(ts[0], m);
|
||||
r = nka_transform_from_matrix(m[0], m[1], m[3], m[4], m[2], m[5]);
|
||||
CHECK(NEAR(r.rotation, 0.7f, 1e-6f) && NEAR(r.scale.x, 2, 1e-5f) && NEAR(r.scale.y, 3, 1e-5f));
|
||||
CHECK(r.position.x == 5 && r.position.y == -7);
|
||||
nka_transform_to_matrix(ts[1], m);
|
||||
r = nka_transform_from_matrix(m[0], m[1], m[3], m[4], m[2], m[5]);
|
||||
CHECK(NEAR(r.rotation, -2.5f, 1e-5f) && NEAR(r.scale.x, -1.5f, 1e-5f) && NEAR(r.scale.y, 0.5f, 1e-5f));
|
||||
r = nka_transform_from_matrix(0, -1, 1, 0, 4, 6);
|
||||
CHECK(NEAR(r.rotation, NKA__PI / 2, 1e-6f) && NEAR(r.scale.x, 1, 1e-6f) && NEAR(r.scale.y, 1, 1e-6f));
|
||||
nka_transform_to_matrix(id, 0);
|
||||
}
|
||||
|
||||
static void test_transform_tween(void)
|
||||
{
|
||||
struct nka_context *a = nka_create(0);
|
||||
struct nka_transform t0 = xf(10, 10, 3, 1, 1), t1 = xf(30, -10, -3, 2, 0.5f), r, r2;
|
||||
|
||||
nka_update(a, 0.125f);
|
||||
r = nka_tween_transform(a, 1, 2, t0, 1, linear, NKA_POLICY_CROSSFADE, NKA_ROTATION_SHORTEST);
|
||||
CHECK(same_xf(r, t0) && !nka_busy(a));
|
||||
nka_update(a, 0.125f);
|
||||
r = nka_tween_transform(a, 1, 2, t1, 1, linear, NKA_POLICY_CROSSFADE, NKA_ROTATION_SHORTEST);
|
||||
CHECK(same_xf(r, t0) && nka_busy(a));
|
||||
nka_update(a, 0.5f);
|
||||
r = nka_tween_transform(a, 1, 2, t1, 1, linear, NKA_POLICY_CROSSFADE, NKA_ROTATION_SHORTEST);
|
||||
r2 = nka_tween_transform(a, 1, 2, t1, 1, linear, NKA_POLICY_CROSSFADE, NKA_ROTATION_SHORTEST);
|
||||
CHECK(same_xf(r, r2) && nka_busy(a));
|
||||
CHECK(NEAR(r.position.x, 20, 1e-4f) && NEAR(r.position.y, 0, 1e-4f) && NEAR(r.scale.x, 1.5f, 1e-5f) && NEAR(r.scale.y, 0.75f, 1e-5f));
|
||||
CHECK(NEAR(r.rotation, NKA__PI, 1e-4f));
|
||||
nka_update(a, 0.5f);
|
||||
r = nka_tween_transform(a, 1, 2, t1, 1, linear, NKA_POLICY_CROSSFADE, NKA_ROTATION_SHORTEST);
|
||||
CHECK(same_xf(r, t1) && !nka_busy(a));
|
||||
nka_update(a, 0.5f);
|
||||
r = nka_tween_transform(a, 1, 2, t1, 1, linear, NKA_POLICY_CROSSFADE, NKA_ROTATION_SHORTEST);
|
||||
CHECK(same_xf(r, t1) && !nka_busy(a));
|
||||
r = nka_tween_transform(a, 1, 2, t1, 1, linear, NKA_POLICY_CROSSFADE, NKA_ROTATION_CW);
|
||||
CHECK(same_xf(r, t1) && !nka_busy(a));
|
||||
|
||||
nka_update(a, 0.125f);
|
||||
nka_tween_transform(a, 1, 2, t0, 1, linear, NKA_POLICY_CROSSFADE, NKA_ROTATION_CW);
|
||||
nka_update(a, 0.5f);
|
||||
r = nka_tween_transform(a, 1, 2, t0, 1, linear, NKA_POLICY_CROSSFADE, NKA_ROTATION_CW);
|
||||
CHECK(NEAR(r.rotation, 0, 1e-4f) && nka_busy(a));
|
||||
|
||||
r = nka_tween_transform(a, 1, 2, t1, 1, linear, NKA_POLICY_CUT, NKA_ROTATION_SHORTEST);
|
||||
CHECK(same_xf(r, t1));
|
||||
nka_update(a, 0.125f);
|
||||
r = nka_tween_transform(a, 1, 2, t1, 1, linear, NKA_POLICY_CUT, NKA_ROTATION_SHORTEST);
|
||||
CHECK(same_xf(r, t1) && !nka_busy(a));
|
||||
nka_destroy(a);
|
||||
}
|
||||
|
||||
static void test_drag(void)
|
||||
{
|
||||
struct nka_context *a = nka_create(0);
|
||||
struct nka_drag_opts o = nka_drag_opts_default();
|
||||
struct nka_drag_feedback fb;
|
||||
struct nk_vec2 pts[3];
|
||||
int i, over = 0;
|
||||
|
||||
CHECK(o.snap_grid.x == 0 && o.snap_grid.y == 0 && o.snap_points == 0 && o.snap_points_count == 0);
|
||||
CHECK(o.snap_duration == 0.2f && o.overshoot == 0 && o.ease_type == NKA_EASE_OUT_CUBIC);
|
||||
fb = nka_drag_update(a, 1, nka__vec2(5, 5));
|
||||
CHECK(!fb.is_dragging && fb.position.x == 5 && fb.velocity.x == 0);
|
||||
fb = nka_drag_release(a, 1, nka__vec2(5, 5), &o);
|
||||
CHECK(!fb.is_snapping && fb.snap_progress == 1 && fb.position.x == 5);
|
||||
|
||||
nka_update(a, 0.125f);
|
||||
fb = nka_drag_begin(a, 1, nka__vec2(100, 100));
|
||||
CHECK(fb.is_dragging && !fb.is_snapping && fb.position.x == 100 && fb.offset.x == 0 && fb.velocity.x == 0);
|
||||
fb = nka_drag_update(a, 1, nka__vec2(100, 100));
|
||||
CHECK(fb.is_dragging && fb.velocity.x == 0 && fb.velocity.y == 0);
|
||||
nka_update(a, 0.125f);
|
||||
fb = nka_drag_update(a, 1, nka__vec2(110, 100));
|
||||
CHECK(fb.velocity.x == 80 && fb.velocity.y == 0 && fb.offset.x == 10 && fb.offset.y == 0);
|
||||
nka_update(a, 0.125f);
|
||||
fb = nka_drag_update(a, 1, nka__vec2(115, 102));
|
||||
CHECK(fb.velocity.x == 40 && fb.velocity.y == 16);
|
||||
fb = nka_drag_update(a, 1, nka__vec2(115, 102));
|
||||
CHECK(fb.velocity.x == 40 && fb.velocity.y == 16 && fb.is_dragging);
|
||||
|
||||
o.snap_grid = nka__vec2(32, 32);
|
||||
o.snap_duration = 0.5f;
|
||||
o.ease_type = NKA_EASE_LINEAR;
|
||||
nka_update(a, 0.125f);
|
||||
fb = nka_drag_release(a, 1, nka__vec2(115, 102), &o);
|
||||
CHECK(!fb.is_dragging && fb.is_snapping && fb.snap_progress == 0 && nka_busy(a));
|
||||
CHECK(fb.position.x == 115 && fb.position.y == 102 && fb.velocity.x == 40);
|
||||
for (i = 1; i <= 4; ++i) {
|
||||
nka_update(a, 0.125f);
|
||||
fb = nka_drag_release(a, 1, fb.position, &o);
|
||||
if (i < 4)
|
||||
CHECK(fb.is_snapping && nka_busy(a) && fb.snap_progress == (float)i / 4
|
||||
&& NEAR(fb.position.x, 115 + 13 * (float)i / 4, 1e-4f) && NEAR(fb.position.y, 102 - 6 * (float)i / 4, 1e-4f));
|
||||
}
|
||||
CHECK(!fb.is_snapping && !nka_busy(a) && fb.snap_progress == 1 && fb.position.x == 128 && fb.position.y == 96);
|
||||
CHECK(fb.offset.x == 28 && fb.offset.y == -4);
|
||||
nka_update(a, 0.125f);
|
||||
fb = nka_drag_release(a, 1, fb.position, &o);
|
||||
CHECK(!fb.is_snapping && !nka_busy(a) && fb.position.x == 128 && fb.position.y == 96);
|
||||
|
||||
pts[0] = nka__vec2(0, 0);
|
||||
pts[1] = nka__vec2(50, 50);
|
||||
pts[2] = nka__vec2(100, 0);
|
||||
o = nka_drag_opts_default();
|
||||
o.snap_grid = nka__vec2(32, 32);
|
||||
o.snap_points = pts;
|
||||
o.snap_points_count = 3;
|
||||
o.snap_duration = 0.25f;
|
||||
nka_update(a, 0.125f);
|
||||
nka_drag_begin(a, 2, nka__vec2(10, 10));
|
||||
nka_update(a, 0.125f);
|
||||
nka_drag_update(a, 2, nka__vec2(40, 45));
|
||||
nka_update(a, 0.125f);
|
||||
fb = nka_drag_release(a, 2, nka__vec2(40, 45), &o);
|
||||
CHECK(fb.is_snapping && nka_busy(a));
|
||||
nka_update(a, 0.125f);
|
||||
fb = nka_drag_release(a, 2, fb.position, &o);
|
||||
CHECK(fb.is_snapping && NEAR(fb.position.x, 48.75f, 1e-4f) && NEAR(fb.position.y, 49.375f, 1e-4f));
|
||||
nka_update(a, 0.125f);
|
||||
fb = nka_drag_release(a, 2, fb.position, &o);
|
||||
CHECK(!fb.is_snapping && fb.position.x == 50 && fb.position.y == 50 && !nka_busy(a));
|
||||
|
||||
o = nka_drag_opts_default();
|
||||
pts[0] = nka__vec2(100, 0);
|
||||
o.snap_points = pts;
|
||||
o.snap_points_count = 1;
|
||||
o.snap_duration = 1;
|
||||
o.overshoot = 1;
|
||||
nka_update(a, 0.125f);
|
||||
nka_drag_begin(a, 3, nka__vec2(0, 0));
|
||||
fb = nka_drag_release(a, 3, nka__vec2(0, 0), &o);
|
||||
for (i = 1; i <= 8; ++i) {
|
||||
nka_update(a, 0.125f);
|
||||
fb = nka_drag_release(a, 3, fb.position, &o);
|
||||
if (i < 8) CHECK(NEAR(fb.position.x, 100 * nka_eval_preset(NKA_EASE_OUT_BACK, (float)i / 8), 2e-3f));
|
||||
over += fb.position.x > 100.5f;
|
||||
}
|
||||
CHECK(over > 0 && fb.position.x == 100 && !fb.is_snapping);
|
||||
o.overshoot = 0;
|
||||
over = 0;
|
||||
nka_drag_begin(a, 4, nka__vec2(0, 0));
|
||||
fb = nka_drag_release(a, 4, nka__vec2(0, 0), &o);
|
||||
for (i = 1; i <= 8; ++i) {
|
||||
nka_update(a, 0.125f);
|
||||
fb = nka_drag_release(a, 4, fb.position, &o);
|
||||
over += fb.position.x > 100;
|
||||
}
|
||||
CHECK(over == 0 && fb.position.x == 100);
|
||||
|
||||
o.snap_duration = 0;
|
||||
nka_drag_begin(a, 5, nka__vec2(30, 0));
|
||||
fb = nka_drag_release(a, 5, nka__vec2(30, 0), &o);
|
||||
CHECK(!fb.is_snapping && fb.position.x == 100 && fb.snap_progress == 1);
|
||||
o.snap_duration = 1;
|
||||
nka_drag_begin(a, 6, nka__vec2(100, 0));
|
||||
nka_update(a, 0.125f);
|
||||
fb = nka_drag_release(a, 6, nka__vec2(100, 0), &o);
|
||||
CHECK(!fb.is_snapping && fb.position.x == 100 && !nka_busy(a));
|
||||
|
||||
nka_update(a, 0.125f);
|
||||
nka_drag_begin(a, 7, nka__vec2(1, 1));
|
||||
nka_drag_cancel(a, 7);
|
||||
fb = nka_drag_update(a, 7, nka__vec2(2, 2));
|
||||
CHECK(!fb.is_dragging && !fb.is_snapping);
|
||||
o = nka_drag_opts_default();
|
||||
o.snap_grid = nka__vec2(32, 32);
|
||||
nka_drag_begin(a, 8, nka__vec2(10, 10));
|
||||
fb = nka_drag_release(a, 8, nka__vec2(10, 10), &o);
|
||||
CHECK(fb.is_snapping);
|
||||
nka_drag_cancel(a, 8);
|
||||
nka_update(a, 0.125f);
|
||||
fb = nka_drag_release(a, 8, nka__vec2(10, 10), 0);
|
||||
CHECK(!fb.is_snapping && fb.position.x == 10 && fb.position.y == 10 && !nka_busy(a));
|
||||
|
||||
nka_drag_begin(a, 0, nka__vec2(1, 1));
|
||||
nka_drag_begin(a, 1, nka__vec2(9, 9));
|
||||
fb = nka_drag_update(a, 0, nka__vec2(2, 2));
|
||||
CHECK(fb.is_dragging && fb.offset.x == 1 && fb.offset.y == 1);
|
||||
nka_destroy(a);
|
||||
}
|
||||
|
||||
static int live;
|
||||
|
||||
static void *count_alloc(nk_handle h, void *old, nk_size n)
|
||||
{
|
||||
(void)h; (void)old;
|
||||
live++;
|
||||
return malloc(n);
|
||||
}
|
||||
|
||||
static void count_free(nk_handle h, void *p)
|
||||
{
|
||||
(void)h;
|
||||
live--;
|
||||
free(p);
|
||||
}
|
||||
|
||||
static void touch(struct nka_context *a, nk_hash id, const struct nka_gradient *g, int frame)
|
||||
{
|
||||
nka_oscillate(a, id, 1, 1, NKA_WAVE_SINE, 0);
|
||||
nka_trigger_shake(a, id);
|
||||
nka_wiggle(a, id, 1, 1);
|
||||
nka_noise_channel_float(a, id, 1, 1, 0);
|
||||
nka_tween_gradient(a, id, 0, g, 1, linear, NKA_POLICY_CROSSFADE, NKA_COL_OKLAB);
|
||||
nka_tween_transform(a, id, 0, nka_transform_identity(), 1, linear, NKA_POLICY_CROSSFADE, NKA_ROTATION_SHORTEST);
|
||||
if (frame) nka_drag_update(a, id, nka__vec2(1, 1));
|
||||
else nka_drag_begin(a, id, nka__vec2(0, 0));
|
||||
}
|
||||
|
||||
static void test_memory(void)
|
||||
{
|
||||
struct nk_allocator al;
|
||||
struct nka_context *a;
|
||||
struct nka_gradient g = nka_gradient_two_color(rgba(1, 0, 0, 1), rgba(0, 0, 1, 1)), r;
|
||||
struct nka_drag_feedback fb;
|
||||
int i, frame;
|
||||
|
||||
al.userdata.ptr = 0;
|
||||
al.alloc = count_alloc;
|
||||
al.free = count_free;
|
||||
a = nka_create(&al);
|
||||
nka_update(a, 0.1f);
|
||||
for (i = 0; i < 40; ++i) touch(a, (nk_hash)i, &g, 0);
|
||||
CHECK(a->fx_clocks.len == 160 && a->fx_grads.len == 40 && a->fx_xforms.len == 40 && a->fx_drags.len == 40);
|
||||
for (frame = 1; frame <= 10; ++frame) {
|
||||
nka_update(a, 0.1f);
|
||||
for (i = 0; i < 40; i += 2) touch(a, (nk_hash)i, &g, frame);
|
||||
}
|
||||
nka_gc(a, 5);
|
||||
CHECK(a->fx_clocks.len == 80 && a->fx_grads.len == 20 && a->fx_xforms.len == 20 && a->fx_drags.len == 20);
|
||||
CHECK(live > 1);
|
||||
nka_clear(a);
|
||||
CHECK(!a->fx_clocks.cap && !a->fx_grads.cap && !a->fx_xforms.cap && !a->fx_drags.cap);
|
||||
touch(a, 1, &g, 0);
|
||||
CHECK(a->fx_clocks.len == 4 && a->fx_drags.len == 1);
|
||||
nka_destroy(a);
|
||||
CHECK(live == 0);
|
||||
|
||||
CHECK(nka_oscillate(0, 1, 2, 1, NKA_WAVE_SINE, 0.25f) == 2);
|
||||
CHECK(nka_shake(0, 1, 2, 1, 1) == 0 && nka_wiggle(0, 1, 0, 1) == 0 && nka_noise_channel_float(0, 1, 1, 0, 0) == 0);
|
||||
nka_trigger_shake(0, 1);
|
||||
r = nka_tween_gradient(0, 1, 2, &g, 1, linear, NKA_POLICY_CROSSFADE, NKA_COL_OKLAB);
|
||||
CHECK(same_gradient(&r, &g));
|
||||
r = nka_tween_gradient(0, 1, 2, 0, 1, linear, NKA_POLICY_CROSSFADE, NKA_COL_OKLAB);
|
||||
CHECK(r.count == 0);
|
||||
CHECK(nka_tween_transform(0, 1, 2, nka_transform_identity(), 1, linear, NKA_POLICY_CROSSFADE, NKA_ROTATION_SHORTEST).scale.x == 1);
|
||||
fb = nka_drag_begin(0, 1, nka__vec2(3, 4));
|
||||
CHECK(fb.position.x == 3 && fb.is_dragging);
|
||||
fb = nka_drag_update(0, 1, nka__vec2(3, 4));
|
||||
CHECK(!fb.is_dragging);
|
||||
fb = nka_drag_release(0, 1, nka__vec2(3, 4), 0);
|
||||
CHECK(fb.position.y == 4 && !fb.is_snapping);
|
||||
nka_drag_cancel(0, 1);
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
test_waves();
|
||||
test_frames();
|
||||
test_shake();
|
||||
test_wiggle();
|
||||
test_noise();
|
||||
test_gradients();
|
||||
test_gradient_tween();
|
||||
test_transforms();
|
||||
test_transform_tween();
|
||||
test_drag();
|
||||
test_memory();
|
||||
if (failures) printf("%d failed\n", failures);
|
||||
else printf("fx: ok\n");
|
||||
return failures != 0;
|
||||
}
|
||||
@@ -0,0 +1,531 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#define NK_INCLUDE_DEFAULT_ALLOCATOR
|
||||
#include "nuklear.h"
|
||||
#define NUKANIM_IMPLEMENTATION
|
||||
#include "nukanim.h"
|
||||
|
||||
static int failures;
|
||||
|
||||
static void check(int ok, const char *what, int line)
|
||||
{
|
||||
if (ok) return;
|
||||
printf("nk.c:%d: %s\n", line, what);
|
||||
failures++;
|
||||
}
|
||||
|
||||
#define CHECK(c) check((c) != 0, #c, __LINE__)
|
||||
#define NEAR(x, y, eps) (fabsf((float)(x) - (float)(y)) <= (eps))
|
||||
|
||||
static const struct nka_ease linear = { NKA_EASE_LINEAR, 0, 0, 0, 0 };
|
||||
static struct nk_context ctx;
|
||||
static struct nk_user_font font;
|
||||
|
||||
static float text_width(nk_handle h, float height, const char *s, int len)
|
||||
{
|
||||
(void)h; (void)s;
|
||||
return (float)len * height * 0.5f;
|
||||
}
|
||||
|
||||
/* One frame of a 200x200 window holding 50 rows, or a group of them. */
|
||||
static nk_uint scroll_frame(struct nka_context *a, int group, int action, const struct nk_vec2 *wheel)
|
||||
{
|
||||
nk_uint x = 0, y = 0;
|
||||
int i;
|
||||
nk_input_begin(&ctx);
|
||||
if (wheel) {
|
||||
nk_input_motion(&ctx, 50, 50);
|
||||
nk_input_scroll(&ctx, *wheel);
|
||||
}
|
||||
nk_input_end(&ctx);
|
||||
if (nk_begin(&ctx, "list", nk_rect(0, 0, 200, 200), 0)) {
|
||||
if (group) {
|
||||
nk_layout_row_dynamic(&ctx, 150, 1);
|
||||
if (nk_group_begin(&ctx, "g", 0)) {
|
||||
nk_layout_row_dynamic(&ctx, 20, 1);
|
||||
for (i = 0; i < 50; ++i) nk_label(&ctx, "row", NK_TEXT_LEFT);
|
||||
if (action == 1) nka_scroll_to_y(a, &ctx, 300, 1, linear);
|
||||
if (action == 2) nka_scroll_to_bottom(a, &ctx, 1, linear);
|
||||
nka_scroll(a, &ctx);
|
||||
nk_group_end(&ctx);
|
||||
}
|
||||
nk_group_get_scroll(&ctx, "g", &x, &y);
|
||||
} else {
|
||||
nk_layout_row_dynamic(&ctx, 20, 1);
|
||||
for (i = 0; i < 50; ++i) nk_label(&ctx, "row", NK_TEXT_LEFT);
|
||||
if (action == 1) nka_scroll_to_y(a, &ctx, 300, 1, linear);
|
||||
if (action == 2) nka_scroll_to_bottom(a, &ctx, 1, linear);
|
||||
if (action == 3) nka_scroll_to_top(a, &ctx, 1, linear);
|
||||
nka_scroll(a, &ctx);
|
||||
}
|
||||
}
|
||||
nk_end(&ctx);
|
||||
if (!group) y = nk_window_find(&ctx, "list")->scrollbar.y;
|
||||
nk_clear(&ctx);
|
||||
return y;
|
||||
}
|
||||
|
||||
static struct nka__nk_scroll *last_scroll(struct nka_context *a)
|
||||
{
|
||||
int i;
|
||||
for (i = 0; i < a->nk_scrolls.cap; ++i)
|
||||
if (a->nk_scrolls.keys[i]) return (struct nka__nk_scroll *)nka__at(&a->nk_scrolls, i);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void test_scroll(int group)
|
||||
{
|
||||
struct nka_context *a = nka_create(0);
|
||||
struct nk_vec2 up = { 0, 1 };
|
||||
nk_uint max, y;
|
||||
nka_update(a, 0.016f);
|
||||
CHECK(scroll_frame(a, group, 0, 0) == 0);
|
||||
nka_update(a, 0);
|
||||
CHECK(scroll_frame(a, group, 1, 0) == 0);
|
||||
CHECK(nka_busy(a));
|
||||
nka_update(a, 0.25f);
|
||||
CHECK(scroll_frame(a, group, 0, 0) == 75);
|
||||
nka_update(a, 0.25f);
|
||||
CHECK(scroll_frame(a, group, 0, 0) == 150);
|
||||
nka_update(a, 0.5f);
|
||||
CHECK(scroll_frame(a, group, 0, 0) == 300);
|
||||
CHECK(!nka_busy(a));
|
||||
nka_update(a, 0.5f);
|
||||
CHECK(scroll_frame(a, group, 0, 0) == 300);
|
||||
|
||||
nka_update(a, 0);
|
||||
CHECK(scroll_frame(a, group, 2, 0) == 300);
|
||||
max = (nk_uint)last_scroll(a)->max[1];
|
||||
CHECK(max > 300 && max < 50 * 30);
|
||||
nka_update(a, 0.5f);
|
||||
y = scroll_frame(a, group, 0, 0);
|
||||
CHECK(y > 300 && y < max);
|
||||
nka_update(a, 0.5f);
|
||||
CHECK(scroll_frame(a, group, 0, 0) == max);
|
||||
nka_update(a, 0.5f);
|
||||
CHECK(scroll_frame(a, group, 0, 0) == max);
|
||||
|
||||
if (!group) {
|
||||
nka_update(a, 0);
|
||||
scroll_frame(a, group, 3, 0);
|
||||
nka_update(a, 0.25f);
|
||||
y = scroll_frame(a, group, 0, &up);
|
||||
CHECK(last_scroll(a)->active[1]);
|
||||
nka_update(a, 0.25f);
|
||||
y = scroll_frame(a, group, 0, 0);
|
||||
CHECK(!last_scroll(a)->active[1]);
|
||||
nka_update(a, 0.25f);
|
||||
CHECK(scroll_frame(a, group, 0, 0) == y);
|
||||
}
|
||||
nka_destroy(a);
|
||||
}
|
||||
|
||||
static void test_style(void)
|
||||
{
|
||||
struct nka_context *a = nka_create(0);
|
||||
struct nk_color table[NK_COLOR_COUNT];
|
||||
struct nk_style keep = ctx.style, base, wide, out;
|
||||
struct nk_user_font other = font;
|
||||
nk_hash d = nka_id("default"), red = nka_id("red"), big = nka_id("big");
|
||||
int i;
|
||||
|
||||
for (i = 0; i < NK_COLOR_COUNT; ++i) table[i] = nk_rgba(200, 30, 30, 255);
|
||||
nka_style_register_current(a, &ctx, d);
|
||||
nka_style_register_table(a, &ctx, red, table);
|
||||
CHECK(memcmp(&keep, &ctx.style, sizeof keep) == 0);
|
||||
CHECK(nka_style_exists(a, d) && nka_style_exists(a, red) && !nka_style_exists(a, big));
|
||||
|
||||
base = ctx.style;
|
||||
wide = ctx.style;
|
||||
wide.window.rounding = base.window.rounding + 10;
|
||||
wide.window.padding = nk_vec2(base.window.padding.x + 20, base.window.padding.y);
|
||||
wide.text.color = nk_rgba(0, 0, 0, 255);
|
||||
wide.font = &other;
|
||||
wide.button.normal = nk_style_item_hide();
|
||||
nka_style_register(a, big, &wide);
|
||||
|
||||
nka_style_blend_to(a, d, big, 0, &out, NKA_COL_SRGB);
|
||||
CHECK(memcmp(&out, &base, sizeof out) == 0);
|
||||
nka_style_blend_to(a, d, big, 1, &out, NKA_COL_SRGB);
|
||||
CHECK(memcmp(&out, &wide, sizeof out) == 0);
|
||||
nka_style_blend_to(a, d, big, 0.4f, &out, NKA_COL_SRGB);
|
||||
CHECK(NEAR(out.window.rounding, base.window.rounding + 4, 1e-4f));
|
||||
CHECK(NEAR(out.window.padding.x, base.window.padding.x + 8, 1e-4f));
|
||||
CHECK(out.text.color.r == (nk_byte)(base.text.color.r * 0.6f + 0.5f));
|
||||
CHECK(out.font == base.font && out.button.normal.type == NK_STYLE_ITEM_COLOR);
|
||||
nka_style_blend_to(a, d, big, 0.6f, &out, NKA_COL_SRGB);
|
||||
CHECK(out.font == &other && out.button.normal.type == wide.button.normal.type);
|
||||
|
||||
nka_style_blend_to(a, d, red, 0.5f, &out, NKA_COL_OKLAB);
|
||||
CHECK(out.window.background.r > base.window.background.r && out.window.background.r < 200);
|
||||
CHECK(out.window.rounding == base.window.rounding);
|
||||
|
||||
nka_update(a, 0.016f);
|
||||
nka_style_tween(a, &ctx, 1, red, 1, linear, NKA_COL_SRGB);
|
||||
CHECK(memcmp(&keep, &ctx.style, sizeof keep) == 0 && nka_busy(a));
|
||||
nka_update(a, 0.5f);
|
||||
nka_style_tween(a, &ctx, 1, red, 1, linear, NKA_COL_SRGB);
|
||||
CHECK(ctx.style.window.background.r > keep.window.background.r && ctx.style.window.background.r < 200);
|
||||
nka_update(a, 0.5f);
|
||||
nka_style_tween(a, &ctx, 1, red, 1, linear, NKA_COL_SRGB);
|
||||
CHECK(!nka_busy(a));
|
||||
nka_style_blend_to(a, d, red, 1, &out, NKA_COL_SRGB);
|
||||
CHECK(memcmp(&out, &ctx.style, sizeof out) == 0);
|
||||
nka_update(a, 0.5f);
|
||||
nka_style_tween(a, &ctx, 1, red, 1, linear, NKA_COL_SRGB);
|
||||
CHECK(!nka_busy(a) && memcmp(&out, &ctx.style, sizeof out) == 0);
|
||||
nka_style_tween(a, &ctx, 1, d, 1, linear, NKA_COL_SRGB);
|
||||
CHECK(nka_busy(a));
|
||||
nka_update(a, 1);
|
||||
nka_style_tween(a, &ctx, 1, d, 1, linear, NKA_COL_SRGB);
|
||||
CHECK(memcmp(&keep, &ctx.style, sizeof keep) == 0);
|
||||
|
||||
nka_style_unregister(a, red);
|
||||
CHECK(!nka_style_exists(a, red));
|
||||
out = keep;
|
||||
nka_style_blend_to(a, d, red, 0.5f, &out, NKA_COL_SRGB);
|
||||
CHECK(memcmp(&keep, &out, sizeof out) == 0);
|
||||
nka_destroy(a);
|
||||
ctx.style = keep;
|
||||
}
|
||||
|
||||
struct glyph { float x, y, height; nk_byte alpha; char c; const struct nk_user_font *font; };
|
||||
|
||||
/* Draws text at (20, 40) in a fresh window and collects the glyphs it drew. */
|
||||
static int stagger(struct nka_context *a, const char *text, float progress, struct nka_text_stagger_opts *o,
|
||||
struct glyph *out)
|
||||
{
|
||||
const struct nk_command *cmd;
|
||||
int n = 0;
|
||||
nk_input_begin(&ctx);
|
||||
nk_input_end(&ctx);
|
||||
o->pos = nk_vec2(20, 40);
|
||||
if (nk_begin(&ctx, "text", nk_rect(0, 0, 300, 200), NK_WINDOW_NO_SCROLLBAR))
|
||||
nka_text_stagger(a, &ctx, text, progress, o);
|
||||
nk_end(&ctx);
|
||||
nk_foreach(cmd, &ctx) {
|
||||
const struct nk_command_text *t = (const struct nk_command_text *)cmd;
|
||||
if (cmd->type != NK_COMMAND_TEXT || n == 16) continue;
|
||||
out[n].x = t->x;
|
||||
out[n].y = t->y;
|
||||
out[n].height = t->height;
|
||||
out[n].alpha = t->foreground.a;
|
||||
out[n].c = t->string[0];
|
||||
out[n].font = t->font;
|
||||
++n;
|
||||
}
|
||||
nk_clear(&ctx);
|
||||
return n;
|
||||
}
|
||||
|
||||
static void test_text(void)
|
||||
{
|
||||
struct nka_context *a = nka_create(0);
|
||||
struct nka_text_stagger_opts o = nka_text_stagger_opts_default();
|
||||
struct glyph g[16];
|
||||
int n, i;
|
||||
|
||||
CHECK(NEAR(nka_text_stagger_duration("Hello", &o), 0.5f, 1e-6f));
|
||||
CHECK(nka_text_stagger_duration("", &o) == 0 && nka_text_stagger_duration("h\xc3\xa9llo", 0) == nka_text_stagger_duration("hello", 0));
|
||||
o.letter_spacing = 2;
|
||||
CHECK(nka_text_stagger_width(&ctx, "Hello", &o) == 5 * 8 + 4 * 2);
|
||||
o.letter_spacing = 0;
|
||||
|
||||
o.ease = linear;
|
||||
nka_update(a, 0.016f);
|
||||
n = stagger(a, "Hello", 0.3f, &o, g);
|
||||
CHECK(n == 3);
|
||||
for (i = 0; i < n; ++i) CHECK(g[i].x == 20 + i * 8 && g[i].y == 40 && g[i].c == "Hello"[i]);
|
||||
CHECK(NEAR(g[0].alpha, 128, 1) && NEAR(g[1].alpha, 85, 1) && NEAR(g[2].alpha, 43, 1));
|
||||
n = stagger(a, "Hello", 0.8f, &o, g);
|
||||
CHECK(n == 5 && g[2].alpha == 255 && NEAR(g[3].alpha, 213, 1) && NEAR(g[4].alpha, 170, 1));
|
||||
n = stagger(a, "Hello", 1, &o, g);
|
||||
CHECK(n == 5 && g[4].alpha == 255 && g[4].font == &font);
|
||||
CHECK(stagger(a, "Hello", 0, &o, g) == 0);
|
||||
CHECK(stagger(a, "h\xc3\xa9llo", 1, &o, g) == 5);
|
||||
|
||||
o.effect = NKA_TEXT_FX_SLIDE_UP;
|
||||
n = stagger(a, "Hello", 0.8f, &o, g);
|
||||
CHECK(g[0].y == 40 && g[3].y > 40 && g[4].y > g[3].y);
|
||||
o.effect = NKA_TEXT_FX_SLIDE_RIGHT;
|
||||
n = stagger(a, "Hello", 0.8f, &o, g);
|
||||
CHECK(g[0].x == 20 && g[3].x < 44);
|
||||
|
||||
o.effect = NKA_TEXT_FX_SCALE;
|
||||
n = stagger(a, "Hello", 0.8f, &o, g);
|
||||
CHECK(n == 5 && g[2].height == 16 && g[2].font == &font);
|
||||
CHECK(g[3].height < 16 && g[3].font != &font && g[3].font->height == g[3].height && g[3].font->width == font.width);
|
||||
CHECK(g[4].height < g[3].height && g[4].y > 40);
|
||||
CHECK(a->nk_fonts && a->nk_fonts->used == 2);
|
||||
nka_update(a, 0.016f);
|
||||
CHECK(a->nk_fonts->used == 0);
|
||||
|
||||
o.effect = NKA_TEXT_FX_BOUNCE;
|
||||
n = stagger(a, "Hello", 0.8f, &o, g);
|
||||
CHECK(n == 5 && g[3].height > 16);
|
||||
|
||||
o.effect = NKA_TEXT_FX_ROTATE;
|
||||
n = stagger(a, "Hello", 0.8f, &o, g);
|
||||
CHECK(n == 5 && g[3].height == 16 && g[3].alpha < 255);
|
||||
|
||||
o.effect = NKA_TEXT_FX_TYPEWRITER;
|
||||
n = stagger(a, "Hello", 0.3f, &o, g);
|
||||
CHECK(n == 3 && g[2].alpha == 255);
|
||||
|
||||
o.effect = NKA_TEXT_FX_WAVE;
|
||||
n = stagger(a, "Hello", 0, &o, g);
|
||||
CHECK(n == 5 && g[0].y == 40 && g[1].y != 40);
|
||||
|
||||
o.effect = NKA_TEXT_FX_FADE;
|
||||
o.font_scale = 2;
|
||||
n = stagger(a, "Hello", 1, &o, g);
|
||||
CHECK(n == 5 && g[1].x == 20 + 16 && g[1].height == 32);
|
||||
nka_destroy(a);
|
||||
}
|
||||
|
||||
static int path_text(struct nka_context *a, nk_hash path, const char *text, float progress,
|
||||
const struct nka_text_path_opts *o, struct glyph *out)
|
||||
{
|
||||
const struct nk_command *cmd;
|
||||
int n = 0;
|
||||
nk_input_begin(&ctx);
|
||||
nk_input_end(&ctx);
|
||||
if (nk_begin(&ctx, "text", nk_rect(0, 0, 300, 200), NK_WINDOW_NO_SCROLLBAR))
|
||||
nka_text_path_animated(a, &ctx, path, text, progress, o);
|
||||
nk_end(&ctx);
|
||||
nk_foreach(cmd, &ctx) {
|
||||
const struct nk_command_text *t = (const struct nk_command_text *)cmd;
|
||||
if (cmd->type != NK_COMMAND_TEXT || n == 16) continue;
|
||||
out[n].x = t->x;
|
||||
out[n].y = t->y;
|
||||
out[n].height = t->height;
|
||||
out[n].alpha = t->foreground.a;
|
||||
out[n].c = t->string[0];
|
||||
out[n].font = t->font;
|
||||
++n;
|
||||
}
|
||||
nk_clear(&ctx);
|
||||
return n;
|
||||
}
|
||||
|
||||
static void test_text_path(void)
|
||||
{
|
||||
struct nka_context *a = nka_create(0);
|
||||
struct nka_text_path_opts o = nka_text_path_opts_default();
|
||||
struct glyph g[16];
|
||||
nk_hash line = nka_id("line"), down = nka_id("down");
|
||||
int n;
|
||||
nka_path_begin(a, line, nk_vec2(10, 100));
|
||||
nka_path_line_to(a, nk_vec2(210, 100));
|
||||
nka_path_end(a);
|
||||
nka_path_begin(a, down, nk_vec2(150, 20));
|
||||
nka_path_line_to(a, nk_vec2(150, 180));
|
||||
nka_path_end(a);
|
||||
nka_update(a, 0.016f);
|
||||
CHECK(nka_text_path_width(&ctx, "abcd", &o) == 32);
|
||||
n = path_text(a, line, "abcd", 1, &o, g);
|
||||
CHECK(n == 4 && g[0].x == 10 && g[1].x == 18 && g[3].x == 34 && g[0].y == 84 && g[2].c == 'c');
|
||||
CHECK(nka_path_has_arc_lut(a, line));
|
||||
o.align = NKA_TEXT_ALIGN_CENTER;
|
||||
n = path_text(a, line, "abcd", 1, &o, g);
|
||||
CHECK(n == 4 && g[0].x == 94);
|
||||
o.align = NKA_TEXT_ALIGN_END;
|
||||
n = path_text(a, line, "abcd", 1, &o, g);
|
||||
CHECK(n == 4 && g[0].x == 178 && g[3].x == 202);
|
||||
o.align = NKA_TEXT_ALIGN_START;
|
||||
o.offset = -12;
|
||||
n = path_text(a, line, "abcd", 1, &o, g);
|
||||
CHECK(n == 3 && g[0].c == 'b' && g[0].x == 6);
|
||||
o.offset = 0;
|
||||
o.flip_y = 1;
|
||||
n = path_text(a, line, "abcd", 1, &o, g);
|
||||
CHECK(n == 4 && g[0].y == 100);
|
||||
o.flip_y = 0;
|
||||
n = path_text(a, line, "abcd", 0.5f, &o, g);
|
||||
CHECK(n == 2 && g[1].alpha == 255);
|
||||
n = path_text(a, line, "abcd", 0.625f, &o, g);
|
||||
CHECK(n == 3 && NEAR(g[2].alpha, 128, 1));
|
||||
CHECK(path_text(a, line, "abcd", 0, &o, g) == 0);
|
||||
n = path_text(a, down, "ab", 1, &o, g);
|
||||
CHECK(n == 2 && g[0].x == 146 && g[0].y == 8 && g[1].y == 16);
|
||||
o.font_scale = 2;
|
||||
n = path_text(a, line, "abcd", 1, &o, g);
|
||||
CHECK(n == 4 && g[1].x == 26 && g[0].height == 32 && g[0].font != &font);
|
||||
CHECK(path_text(a, nka_id("missing"), "abcd", 1, &o, g) == 0);
|
||||
nka_destroy(a);
|
||||
}
|
||||
|
||||
static double clock_now;
|
||||
|
||||
static double fake_clock(void *user)
|
||||
{
|
||||
(void)user;
|
||||
return clock_now;
|
||||
}
|
||||
|
||||
static int window_texts(const char *needle)
|
||||
{
|
||||
const struct nk_command *cmd;
|
||||
int n = 0, len = (int)strlen(needle);
|
||||
nk_foreach(cmd, &ctx) {
|
||||
const struct nk_command_text *t = (const struct nk_command_text *)cmd;
|
||||
if (cmd->type == NK_COMMAND_TEXT && t->length >= len && !strncmp(t->string, needle, (size_t)len)) ++n;
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
static void test_inspector(void)
|
||||
{
|
||||
struct nka_context *a = nka_create(0);
|
||||
struct nka__nk_profiler *p;
|
||||
int open = 1, i;
|
||||
nka_profiler_begin(a, "ignored");
|
||||
CHECK(!nka_profiler_is_enabled(a));
|
||||
nka_profiler_enable(a, 1);
|
||||
nka_profiler_begin_frame(a);
|
||||
nka_profiler_begin(a, "nothing without a clock");
|
||||
nka_profiler_end(a);
|
||||
p = a->nk_profiler;
|
||||
CHECK(p && p->count == 0);
|
||||
nka_profiler_set_clock(a, fake_clock, 0);
|
||||
for (i = 0; i < 2; ++i) {
|
||||
nka_profiler_begin_frame(a);
|
||||
nka_profiler_begin(a, "layout");
|
||||
clock_now += 0.002;
|
||||
nka_profiler_begin(a, "a section with a name longer than thirty-one characters");
|
||||
clock_now += 0.001;
|
||||
nka_profiler_end(a);
|
||||
nka_profiler_end(a);
|
||||
nka_profiler_begin(a, "a section with a name longer than thirty-one characters, again");
|
||||
clock_now += 0.001;
|
||||
nka_profiler_end(a);
|
||||
nka_profiler_end_frame(a);
|
||||
}
|
||||
CHECK(p->count == 2 && p->at == 2);
|
||||
CHECK(NEAR(p->sections[0].ms, 3, 1e-3f) && p->sections[0].calls == 1);
|
||||
CHECK(NEAR(p->sections[1].ms, 2, 1e-3f) && p->sections[1].calls == 2);
|
||||
CHECK(NEAR(p->frame_ms, 4, 1e-3f) && NEAR(p->frame[1], 4, 1e-3f) && NEAR(p->sections[1].history[1], 2, 1e-3f));
|
||||
|
||||
nka_update(a, 0.016f);
|
||||
nka_tween_float(a, 7, 8, 1, 1, linear, NKA_POLICY_CROSSFADE, 0);
|
||||
nk_input_begin(&ctx);
|
||||
nk_input_end(&ctx);
|
||||
nka_show_unified_inspector(a, &ctx, &open);
|
||||
CHECK(open && nk_window_find(&ctx, "NukAnim") != 0);
|
||||
CHECK(window_texts("Time scale") == 2 && window_texts("Moving") == 1 && window_texts("0x00000007") == 1);
|
||||
nk_clear(&ctx);
|
||||
open = 0;
|
||||
nk_input_begin(&ctx);
|
||||
nk_input_end(&ctx);
|
||||
nka_show_unified_inspector(a, &ctx, &open);
|
||||
CHECK(window_texts("Time scale") == 0);
|
||||
nk_clear(&ctx);
|
||||
nka_destroy(a);
|
||||
}
|
||||
|
||||
static int count_commands(enum nk_command_type type)
|
||||
{
|
||||
const struct nk_command *cmd;
|
||||
int n = 0;
|
||||
nk_foreach(cmd, &ctx) n += cmd->type == type;
|
||||
return n;
|
||||
}
|
||||
|
||||
/* The timeline's background, the first filled rect in its ground color. */
|
||||
static struct nk_rect timeline_rect(void)
|
||||
{
|
||||
const struct nk_command *cmd;
|
||||
nk_foreach(cmd, &ctx) {
|
||||
const struct nk_command_rect_filled *f = (const struct nk_command_rect_filled *)cmd;
|
||||
if (cmd->type == NK_COMMAND_RECT_FILLED && f->color.r == 30 && f->color.g == 32 && f->color.b == 40)
|
||||
return nk_rect(f->x, f->y, f->w, f->h);
|
||||
}
|
||||
return nk_rect(0, 0, 0, 0);
|
||||
}
|
||||
|
||||
static void test_timeline(void)
|
||||
{
|
||||
struct nka_context *a = nka_create(0);
|
||||
nk_hash clip = nka_id("fade in"), inst;
|
||||
struct nk_rect r;
|
||||
float label, x0, w;
|
||||
nka_clip_begin(a, clip);
|
||||
nka_clip_key_float(a, nka_id("alpha"), 0, 0, NKA_EASE_OUT_CUBIC, 0);
|
||||
nka_clip_key_float(a, nka_id("alpha"), 1, 1, NKA_EASE_LINEAR, 0);
|
||||
nka_clip_key_vec2(a, nka_id("pos"), 0, nk_vec2(0, 0), NKA_EASE_IN_OUT_SINE, 0);
|
||||
nka_clip_key_vec2(a, nka_id("pos"), 2, nk_vec2(10, 0), NKA_EASE_LINEAR, 0);
|
||||
nka_clip_set_delay(a, 0.5f);
|
||||
CHECK(nka_clip_end(a) == NKA_OK);
|
||||
nka_update(a, 0.016f);
|
||||
inst = nka_play(a, clip, 9);
|
||||
CHECK(inst == 9);
|
||||
nka_update(a, 1.0f);
|
||||
|
||||
nk_input_begin(&ctx);
|
||||
nk_input_end(&ctx);
|
||||
if (nk_begin(&ctx, "timeline", nk_rect(0, 0, 400, 300), NK_WINDOW_NO_SCROLLBAR)) {
|
||||
nka_show_debug_timeline(a, &ctx, inst);
|
||||
nka_show_debug_timeline(a, &ctx, 12345);
|
||||
}
|
||||
nk_end(&ctx);
|
||||
CHECK(window_texts("Clip 0x") == 1 && window_texts("No instance 0x00003039") == 1);
|
||||
CHECK(window_texts("float") == 1 && window_texts("vec2") == 1 && window_texts("delay end") == 1);
|
||||
CHECK(window_texts("0.0s") == 1 && window_texts("2.0s") == 1 && window_texts("2.5s") == 0);
|
||||
CHECK(count_commands(NK_COMMAND_TRIANGLE_FILLED) == 1 && count_commands(NK_COMMAND_CIRCLE_FILLED) == 4);
|
||||
r = timeline_rect();
|
||||
CHECK(r.w > 300 && r.h == 130);
|
||||
nk_clear(&ctx);
|
||||
|
||||
label = font.width(font.userdata, font.height, "color rel", 9) + 8;
|
||||
x0 = r.x + 4 + label;
|
||||
w = r.w - label - 8;
|
||||
nk_input_begin(&ctx);
|
||||
nk_input_motion(&ctx, (int)(x0 + 0.5f / 2.5f * w), (int)(r.y + 44 + 10));
|
||||
nk_input_end(&ctx);
|
||||
if (nk_begin(&ctx, "timeline", nk_rect(0, 0, 400, 300), NK_WINDOW_NO_SCROLLBAR))
|
||||
nka_show_debug_timeline(a, &ctx, inst);
|
||||
nk_end(&ctx);
|
||||
CHECK(window_texts("0.000s out cubic") == 1);
|
||||
nk_clear(&ctx);
|
||||
nka_destroy(a);
|
||||
}
|
||||
|
||||
static void test_anchors(void)
|
||||
{
|
||||
struct nka_context *a = nka_create(0);
|
||||
struct nk_vec2 s;
|
||||
nk_input_begin(&ctx);
|
||||
nk_input_end(&ctx);
|
||||
if (nk_begin(&ctx, "anchored", nk_rect(10, 10, 240, 160), 0)) {
|
||||
nka_anchor_update(a, &ctx);
|
||||
nk_layout_row_dynamic(&ctx, 20, 1);
|
||||
CHECK(NEAR(nka_tween_float_rel(a, 1, 2, 0.5f, 0, 1, linear, NKA_POLICY_CUT, NKA_ANCHOR_WINDOW, 0), 120, 1e-4f));
|
||||
}
|
||||
nk_end(&ctx);
|
||||
nk_clear(&ctx);
|
||||
s = nka_anchor(a, NKA_ANCHOR_WINDOW);
|
||||
CHECK(s.x == 240 && s.y == 160);
|
||||
s = nka_anchor(a, NKA_ANCHOR_WINDOW_CONTENT);
|
||||
CHECK(s.x > 0 && s.x < 240 && s.y > 0 && s.y < 160);
|
||||
nka_destroy(a);
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
font.height = 16;
|
||||
font.width = text_width;
|
||||
nk_init_default(&ctx, &font);
|
||||
test_scroll(0);
|
||||
test_scroll(1);
|
||||
test_style();
|
||||
test_text();
|
||||
test_text_path();
|
||||
test_inspector();
|
||||
test_timeline();
|
||||
test_anchors();
|
||||
nk_free(&ctx);
|
||||
if (failures) printf("%d failed\n", failures);
|
||||
else printf("nk: ok\n");
|
||||
return failures != 0;
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
/* Nuklear's implementation on its own, so its warnings stay out of the tests. */
|
||||
#ifdef _MSC_VER
|
||||
#pragma warning(disable: 4116 4127 4701 5287)
|
||||
#endif
|
||||
#define NK_INCLUDE_DEFAULT_ALLOCATOR
|
||||
#define NK_IMPLEMENTATION
|
||||
#include "nuklear.h"
|
||||
@@ -0,0 +1,604 @@
|
||||
#include <stdio.h>
|
||||
|
||||
#define NK_INCLUDE_DEFAULT_ALLOCATOR
|
||||
#include "nuklear.h"
|
||||
#define NUKANIM_IMPLEMENTATION
|
||||
#include "nukanim.h"
|
||||
|
||||
static int failures;
|
||||
|
||||
static void check(int ok, const char *what, int line)
|
||||
{
|
||||
if (ok) return;
|
||||
printf("path.c:%d: %s\n", line, what);
|
||||
failures++;
|
||||
}
|
||||
|
||||
#define CHECK(c) check((c) != 0, #c, __LINE__)
|
||||
#define NEAR(x, y, eps) (fabsf((float)(x) - (float)(y)) <= (eps))
|
||||
|
||||
static const struct nka_ease linear = { NKA_EASE_LINEAR, 0, 0, 0, 0 };
|
||||
static const float pi = 3.14159265f;
|
||||
|
||||
static struct nk_vec2 v2(float x, float y)
|
||||
{
|
||||
struct nk_vec2 v;
|
||||
v.x = x; v.y = y;
|
||||
return v;
|
||||
}
|
||||
|
||||
static int near2(struct nk_vec2 p, struct nk_vec2 q, float eps) { return NEAR(p.x, q.x, eps) && NEAR(p.y, q.y, eps); }
|
||||
static float len2(struct nk_vec2 p) { return sqrtf(p.x * p.x + p.y * p.y); }
|
||||
|
||||
/* The central difference between two points 2h apart. */
|
||||
static struct nk_vec2 slope(struct nk_vec2 lo, struct nk_vec2 hi, float h)
|
||||
{
|
||||
return v2((hi.x - lo.x) / (2 * h), (hi.y - lo.y) / (2 * h));
|
||||
}
|
||||
|
||||
static void test_curves(void)
|
||||
{
|
||||
struct nk_vec2 p0 = v2(0, 0), p1 = v2(10, 40), p2 = v2(50, -20), p3 = v2(80, 30), q;
|
||||
const float tension[3] = { 0, 0.5f, 1 }, h = 1e-3f;
|
||||
int i, k;
|
||||
|
||||
CHECK(near2(nka_bezier_quadratic(p0, p1, p2, 0), p0, 0));
|
||||
CHECK(near2(nka_bezier_quadratic(p0, p1, p2, 1), p2, 0));
|
||||
q = nka_bezier_quadratic(p0, p1, p2, 0.5f);
|
||||
CHECK(near2(q, v2((p0.x + 2 * p1.x + p2.x) / 4, (p0.y + 2 * p1.y + p2.y) / 4), 1e-4f));
|
||||
CHECK(near2(nka_bezier_cubic(p0, p1, p2, p3, 0), p0, 0));
|
||||
CHECK(near2(nka_bezier_cubic(p0, p1, p2, p3, 1), p3, 0));
|
||||
q = nka_bezier_cubic(p0, p1, p2, p3, 0.5f);
|
||||
CHECK(near2(q, v2((p0.x + 3 * p1.x + 3 * p2.x + p3.x) / 8, (p0.y + 3 * p1.y + 3 * p2.y + p3.y) / 8), 1e-4f));
|
||||
for (k = 0; k < 3; ++k) {
|
||||
CHECK(near2(nka_catmull_rom(p0, p1, p2, p3, 0, tension[k]), p1, 1e-4f));
|
||||
CHECK(near2(nka_catmull_rom(p0, p1, p2, p3, 1, tension[k]), p2, 1e-4f));
|
||||
}
|
||||
q = nka_catmull_rom(p0, p1, p2, p3, 0.5f, 0);
|
||||
CHECK(near2(q, v2((-p0.x + 9 * p1.x + 9 * p2.x - p3.x) / 16, (-p0.y + 9 * p1.y + 9 * p2.y - p3.y) / 16), 1e-4f));
|
||||
q = nka_catmull_rom_deriv(p0, p1, p2, p3, 0, 0);
|
||||
CHECK(near2(q, v2((p2.x - p0.x) / 2, (p2.y - p0.y) / 2), 1e-4f));
|
||||
CHECK(len2(nka_catmull_rom_deriv(p0, p1, p2, p3, 0, 1)) < 1e-5f);
|
||||
CHECK(len2(nka_catmull_rom_deriv(p0, p1, p2, p3, 1, 1)) < 1e-5f);
|
||||
|
||||
for (i = 0; i <= 10; ++i) {
|
||||
float t = (float)i / 10;
|
||||
q = slope(nka_bezier_quadratic(p0, p1, p2, t - h), nka_bezier_quadratic(p0, p1, p2, t + h), h);
|
||||
CHECK(near2(nka_bezier_quadratic_deriv(p0, p1, p2, t), q, 0.05f));
|
||||
q = slope(nka_bezier_cubic(p0, p1, p2, p3, t - h), nka_bezier_cubic(p0, p1, p2, p3, t + h), h);
|
||||
CHECK(near2(nka_bezier_cubic_deriv(p0, p1, p2, p3, t), q, 0.05f));
|
||||
for (k = 0; k < 3; ++k) {
|
||||
q = slope(nka_catmull_rom(p0, p1, p2, p3, t - h, tension[k]), nka_catmull_rom(p0, p1, p2, p3, t + h, tension[k]), h);
|
||||
CHECK(near2(nka_catmull_rom_deriv(p0, p1, p2, p3, t, tension[k]), q, 0.05f));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void circle(struct nka_context *a, nk_hash id, float r)
|
||||
{
|
||||
float k = 0.5522847498f * r;
|
||||
nka_path_begin(a, id, v2(r, 0));
|
||||
nka_path_cubic_to(a, v2(r, k), v2(k, r), v2(0, r));
|
||||
nka_path_cubic_to(a, v2(-k, r), v2(-r, k), v2(-r, 0));
|
||||
nka_path_cubic_to(a, v2(-r, -k), v2(-k, -r), v2(0, -r));
|
||||
nka_path_cubic_to(a, v2(k, -r), v2(r, -k), v2(r, 0));
|
||||
nka_path_end(a);
|
||||
}
|
||||
|
||||
static void test_build(void)
|
||||
{
|
||||
struct nka_context *a = nka_create(0);
|
||||
nk_hash line = nka_id("line"), ring = nka_id("ring"), tri = nka_id("tri"), cr = nka_id("cr");
|
||||
const struct nk_vec2 pts[4] = { { 0, 0 }, { 50, 40 }, { 100, -10 }, { 150, 30 } };
|
||||
struct nka__path_data *p;
|
||||
struct nk_vec2 q;
|
||||
float acc;
|
||||
int i;
|
||||
|
||||
CHECK(!nka_path_exists(a, line) && nka_path_length(a, line) == 0);
|
||||
CHECK(near2(nka_path_evaluate(a, line, 0.5f), v2(0, 0), 0));
|
||||
CHECK(near2(nka_path_tangent(a, line, 0.5f), v2(1, 0), 0) && nka_path_angle(a, line, 0.5f) == 0);
|
||||
nka_path_line_to(a, v2(1, 1));
|
||||
nka_path_end(a);
|
||||
CHECK(a->path_map.len == 0);
|
||||
|
||||
nka_path_begin(a, line, v2(10, 20));
|
||||
nka_path_line_to(a, v2(40, 60));
|
||||
CHECK(!nka_path_exists(a, line));
|
||||
nka_path_end(a);
|
||||
CHECK(nka_path_exists(a, line));
|
||||
CHECK(nka_path_length(a, line) == 50);
|
||||
CHECK(near2(nka_path_evaluate(a, line, 0), v2(10, 20), 0));
|
||||
CHECK(near2(nka_path_evaluate(a, line, 1), v2(40, 60), 0));
|
||||
CHECK(near2(nka_path_evaluate(a, line, 0.5f), v2(25, 40), 1e-4f));
|
||||
CHECK(near2(nka_path_evaluate(a, line, -3), v2(10, 20), 0) && near2(nka_path_evaluate(a, line, 7), v2(40, 60), 0));
|
||||
CHECK(near2(nka_path_tangent(a, line, 0.3f), v2(0.6f, 0.8f), 1e-6f));
|
||||
nka_path_build_arc_lut(a, line, 16);
|
||||
nka_path_begin(a, line, v2(0, 0));
|
||||
nka_path_line_to(a, v2(0, 10));
|
||||
CHECK(nka_path_length(a, line) == 50 && nka_path_has_arc_lut(a, line));
|
||||
nka_path_end(a);
|
||||
CHECK(nka_path_length(a, line) == 10 && !nka_path_has_arc_lut(a, line));
|
||||
CHECK(near2(nka_path_evaluate(a, line, 1), v2(0, 10), 0) && a->path_map.len == 1);
|
||||
|
||||
circle(a, ring, 100);
|
||||
CHECK(NEAR(nka_path_length(a, ring), 2 * pi * 100, 0.5f));
|
||||
for (i = 0; i <= 16; ++i) {
|
||||
float t = (float)i / 16;
|
||||
q = nka_path_evaluate(a, ring, t);
|
||||
CHECK(NEAR(len2(q), 100, 0.05f));
|
||||
CHECK(NEAR(nka_path_angle(a, ring, t), atan2f(q.x, -q.y), 2e-3f) || NEAR(fabsf(atan2f(q.x, -q.y)), pi, 2e-3f));
|
||||
}
|
||||
CHECK(near2(nka_path_evaluate(a, ring, 0.125f), v2(70.7107f, 70.7107f), 0.05f));
|
||||
|
||||
nka_path_begin(a, tri, v2(0, 0));
|
||||
nka_path_line_to(a, v2(30, 0));
|
||||
nka_path_line_to(a, v2(30, 40));
|
||||
nka_path_close(a);
|
||||
nka_path_close(a);
|
||||
nka_path_end(a);
|
||||
p = nka__path_get(a, tri);
|
||||
CHECK(p && p->n == 3);
|
||||
CHECK(near2(nka_path_evaluate(a, tri, 1), v2(0, 0), 0));
|
||||
CHECK(NEAR(nka_path_length(a, tri), 120, 1e-3f));
|
||||
|
||||
nka_path_begin(a, cr, pts[0]);
|
||||
for (i = 1; i < 4; ++i) nka_path_catmull_to(a, pts[i], 0.5f);
|
||||
nka_path_end(a);
|
||||
p = nka__path_get(a, cr);
|
||||
CHECK(p && p->n == 3);
|
||||
if (p) {
|
||||
CHECK(near2(p->segs[0].p[0], pts[0], 0) && near2(p->segs[0].p[1], pts[0], 0));
|
||||
CHECK(near2(p->segs[0].p[2], pts[1], 0) && near2(p->segs[0].p[3], pts[2], 0));
|
||||
CHECK(near2(p->segs[1].p[0], pts[0], 0) && near2(p->segs[1].p[3], pts[3], 0));
|
||||
CHECK(near2(p->segs[2].p[0], pts[1], 0) && near2(p->segs[2].p[2], pts[3], 0) && near2(p->segs[2].p[3], pts[3], 0));
|
||||
acc = 0;
|
||||
for (i = 0; i < 3; ++i) {
|
||||
acc += p->segs[i].length;
|
||||
CHECK(near2(nka_path_evaluate(a, cr, acc / p->seg_len), pts[i + 1], 1e-3f));
|
||||
}
|
||||
}
|
||||
|
||||
nka_path_begin(a, nka_id("lost"), v2(0, 0));
|
||||
nka_path_line_to(a, v2(5, 0));
|
||||
nka_path_begin(a, nka_id("kept"), v2(1, 2));
|
||||
nka_path_end(a);
|
||||
CHECK(!nka_path_exists(a, nka_id("lost")) && nka_path_exists(a, nka_id("kept")));
|
||||
CHECK(near2(nka_path_evaluate(a, nka_id("kept"), 0.5f), v2(1, 2), 0) && nka_path_length(a, nka_id("kept")) == 0);
|
||||
|
||||
nka_path_begin(a, 0, v2(0, 0));
|
||||
nka_path_line_to(a, v2(10, 0));
|
||||
nka_path_end(a);
|
||||
nka_path_begin(a, 1, v2(0, 0));
|
||||
nka_path_line_to(a, v2(20, 0));
|
||||
nka_path_end(a);
|
||||
CHECK(nka_path_length(a, 0) == 10 && nka_path_length(a, 1) == 20);
|
||||
nka_destroy(a);
|
||||
}
|
||||
|
||||
static void test_tangents(void)
|
||||
{
|
||||
struct nka_context *a = nka_create(0);
|
||||
nk_hash ring = nka_id("ring"), cusp = nka_id("cusp"), dot = nka_id("dot"), stop = nka_id("stop");
|
||||
struct nk_vec2 q, d;
|
||||
int i;
|
||||
|
||||
circle(a, ring, 100);
|
||||
for (i = 0; i <= 40; ++i) {
|
||||
float t = (float)i / 40;
|
||||
d = nka_path_tangent(a, ring, t);
|
||||
q = nka_path_evaluate(a, ring, t);
|
||||
CHECK(NEAR(len2(d), 1, 1e-5f));
|
||||
CHECK(nka_path_angle(a, ring, t) == atan2f(d.y, d.x));
|
||||
CHECK(NEAR(q.x * d.x + q.y * d.y, 0, 1));
|
||||
CHECK(q.x * d.y - q.y * d.x > 0);
|
||||
}
|
||||
|
||||
nka_path_begin(a, cusp, v2(0, 0));
|
||||
nka_path_cubic_to(a, v2(0, 0), v2(0, 50), v2(50, 50));
|
||||
nka_path_end(a);
|
||||
CHECK(near2(nka_path_tangent(a, cusp, 0), v2(0, 1), 1e-3f));
|
||||
CHECK(NEAR(nka_path_angle(a, cusp, 0), pi / 2, 1e-3f));
|
||||
CHECK(near2(nka_path_tangent(a, cusp, 1), v2(1, 0), 1e-6f));
|
||||
|
||||
nka_path_begin(a, stop, v2(0, 0));
|
||||
nka_path_catmull_to(a, v2(0, 40), 1);
|
||||
nka_path_catmull_to(a, v2(40, 40), 1);
|
||||
nka_path_end(a);
|
||||
CHECK(near2(nka_path_tangent(a, stop, 0), v2(0, 1), 1e-3f));
|
||||
CHECK(near2(nka_path_tangent(a, stop, 1), v2(1, 0), 1e-3f));
|
||||
|
||||
nka_path_begin(a, dot, v2(5, 5));
|
||||
nka_path_line_to(a, v2(5, 5));
|
||||
nka_path_end(a);
|
||||
CHECK(near2(nka_path_tangent(a, dot, 0.5f), v2(1, 0), 0) && nka_path_angle(a, dot, 0.5f) == 0);
|
||||
CHECK(nka_path_length(a, dot) == 0 && near2(nka_path_evaluate(a, dot, 0.5f), v2(5, 5), 0));
|
||||
nka_path_build_arc_lut(a, dot, 64);
|
||||
CHECK(!nka_path_has_arc_lut(a, dot));
|
||||
nka_destroy(a);
|
||||
}
|
||||
|
||||
static void uneven(struct nka_context *a, nk_hash id)
|
||||
{
|
||||
nka_path_begin(a, id, v2(0, 0));
|
||||
nka_path_line_to(a, v2(50, 0));
|
||||
nka_path_cubic_to(a, v2(140, 0), v2(145, 0), v2(150, 0));
|
||||
nka_path_end(a);
|
||||
}
|
||||
|
||||
static void test_arc_length(void)
|
||||
{
|
||||
struct nka_context *a = nka_create(0);
|
||||
nk_hash id = nka_id("uneven"), ring = nka_id("ring"), none = nka_id("none");
|
||||
struct nk_vec2 before[11], q, d;
|
||||
int i;
|
||||
|
||||
uneven(a, id);
|
||||
CHECK(!nka_path_has_arc_lut(a, id));
|
||||
CHECK(NEAR(nka_path_length(a, id), 150, 1e-3f));
|
||||
CHECK(fabsf(nka_path_evaluate(a, id, 0.4f).x - 60) > 5);
|
||||
CHECK(NEAR(nka_path_distance_to_t(a, id, 75), 0.5f, 1e-6f));
|
||||
CHECK(nka_path_distance_to_t(a, id, -5) == 0 && nka_path_distance_to_t(a, id, 500) == 1);
|
||||
for (i = 0; i <= 10; ++i) before[i] = nka_path_evaluate(a, id, (float)i / 10);
|
||||
|
||||
nka_path_build_arc_lut(a, id, 256);
|
||||
CHECK(nka_path_has_arc_lut(a, id) && nka__path_get(a, id)->lut_n == 257);
|
||||
CHECK(NEAR(nka_path_length(a, id), 150, 1e-2f));
|
||||
for (i = 0; i <= 10; ++i) CHECK(near2(nka_path_evaluate(a, id, (float)i / 10), before[i], 0));
|
||||
/* The table steps evenly in t, so the step across the corner at 50 interpolates two speeds. */
|
||||
for (i = 0; i <= 30; ++i) {
|
||||
float dist = (float)i * 5;
|
||||
q = nka_path_evaluate_at_distance(a, id, dist);
|
||||
CHECK(NEAR(q.x, dist, fabsf(dist - 50) < 2 ? 0.25f : 0.01f) && q.y == 0);
|
||||
CHECK(near2(nka_path_tangent_at_distance(a, id, dist), v2(1, 0), 1e-6f));
|
||||
CHECK(nka_path_angle_at_distance(a, id, dist) == 0);
|
||||
}
|
||||
for (i = 0; i <= 300; ++i) CHECK(NEAR(nka_path_evaluate_at_distance(a, id, (float)i / 2).x, (float)i / 2, 0.25f));
|
||||
nka_path_begin(a, nka_id("smooth"), v2(0, 0));
|
||||
nka_path_cubic_to(a, v2(90, 0), v2(95, 0), v2(100, 0));
|
||||
nka_path_end(a);
|
||||
CHECK(fabsf(nka_path_evaluate(a, nka_id("smooth"), 0.5f).x - 50) > 30);
|
||||
nka_path_build_arc_lut(a, nka_id("smooth"), 256);
|
||||
for (i = 0; i <= 200; ++i)
|
||||
CHECK(NEAR(nka_path_evaluate_at_distance(a, nka_id("smooth"), (float)i / 2).x, (float)i / 2, 0.01f));
|
||||
CHECK(near2(nka_path_evaluate_at_distance(a, id, -10), v2(0, 0), 0));
|
||||
CHECK(near2(nka_path_evaluate_at_distance(a, id, 1000), v2(150, 0), 1e-4f));
|
||||
CHECK(nka_path_distance_to_t(a, id, -1) == 0 && nka_path_distance_to_t(a, id, 1e6f) == 1);
|
||||
CHECK(NEAR(nka_path_distance_to_t(a, id, 25), 25.0f / 150, 1e-3f));
|
||||
|
||||
circle(a, ring, 100);
|
||||
for (i = 0; i <= 10; ++i) before[i] = nka_path_evaluate(a, ring, (float)i / 10);
|
||||
nka_path_build_arc_lut(a, ring, 0);
|
||||
CHECK(nka__path_get(a, ring)->lut_n == 65);
|
||||
nka_path_build_arc_lut(a, ring, 512);
|
||||
CHECK(NEAR(nka_path_length(a, ring), 628.4f, 0.02f));
|
||||
for (i = 0; i <= 10; ++i) CHECK(near2(nka_path_evaluate(a, ring, (float)i / 10), before[i], 0));
|
||||
for (i = 0; i < 24; ++i) {
|
||||
float dist = nka_path_length(a, ring) * (float)i / 24, turn = 2 * pi * (float)i / 24;
|
||||
q = nka_path_evaluate_at_distance(a, ring, dist);
|
||||
d = nka_path_tangent_at_distance(a, ring, dist);
|
||||
CHECK(near2(v2(q.x / len2(q), q.y / len2(q)), v2(cosf(turn), sinf(turn)), 2e-3f));
|
||||
CHECK(NEAR(len2(d), 1, 1e-5f));
|
||||
CHECK(nka_path_angle_at_distance(a, ring, dist) == atan2f(d.y, d.x));
|
||||
}
|
||||
|
||||
uneven(a, id);
|
||||
CHECK(!nka_path_has_arc_lut(a, id) && NEAR(nka_path_length(a, id), 150, 1e-3f));
|
||||
|
||||
nka_path_build_arc_lut(a, none, 64);
|
||||
CHECK(!nka_path_has_arc_lut(a, none) && nka_path_distance_to_t(a, none, 5) == 0);
|
||||
CHECK(near2(nka_path_evaluate_at_distance(a, none, 5), v2(0, 0), 0));
|
||||
CHECK(near2(nka_path_tangent_at_distance(a, none, 5), v2(1, 0), 0) && nka_path_angle_at_distance(a, none, 5) == 0);
|
||||
nka_destroy(a);
|
||||
}
|
||||
|
||||
static void test_tweens(void)
|
||||
{
|
||||
struct nka_context *a = nka_create(0);
|
||||
nk_hash id = nka_id("mover"), ch = nka_id("pos"), path = nka_id("L"), flat = nka_id("flat");
|
||||
struct nk_vec2 q;
|
||||
int i;
|
||||
|
||||
nka_update(a, 0.016f);
|
||||
q = nka_tween_path(a, id, ch, path, 1, linear, NKA_POLICY_CROSSFADE);
|
||||
CHECK(near2(q, v2(0, 0), 0) && a->chans.len == 0 && !nka_busy(a));
|
||||
CHECK(nka_tween_path_angle(a, id, ch, path, 1, linear, NKA_POLICY_CROSSFADE) == 0 && a->chans.len == 0);
|
||||
|
||||
nka_path_begin(a, path, v2(0, 0));
|
||||
nka_path_line_to(a, v2(100, 0));
|
||||
nka_path_line_to(a, v2(100, 100));
|
||||
nka_path_end(a);
|
||||
|
||||
CHECK(near2(nka_tween_path(a, id, ch, path, 1, linear, NKA_POLICY_CROSSFADE), v2(0, 0), 0) && nka_busy(a));
|
||||
CHECK(nka_tween_path_angle(a, id, ch, path, 1, linear, NKA_POLICY_CROSSFADE) == 0);
|
||||
nka_update(a, 0.25f);
|
||||
CHECK(!nka_busy(a));
|
||||
CHECK(near2(nka_tween_path(a, id, ch, path, 1, linear, NKA_POLICY_CROSSFADE), v2(50, 0), 1e-3f) && nka_busy(a));
|
||||
CHECK(nka_tween_path_angle(a, id, ch, path, 1, linear, NKA_POLICY_CROSSFADE) == 0);
|
||||
nka_update(a, 0.5f);
|
||||
CHECK(near2(nka_tween_path(a, id, ch, path, 1, linear, NKA_POLICY_CROSSFADE), v2(100, 50), 1e-3f));
|
||||
CHECK(NEAR(nka_tween_path_angle(a, id, ch, path, 1, linear, NKA_POLICY_CROSSFADE), pi / 2, 1e-6f));
|
||||
nka_update(a, 0.25f);
|
||||
CHECK(near2(nka_tween_path(a, id, ch, path, 1, linear, NKA_POLICY_CROSSFADE), v2(100, 100), 0));
|
||||
nka_update(a, 0.25f);
|
||||
CHECK(near2(nka_tween_path(a, id, ch, path, 1, linear, NKA_POLICY_CROSSFADE), v2(100, 100), 0));
|
||||
CHECK(NEAR(nka_tween_path_angle(a, id, ch, path, 1, linear, NKA_POLICY_CROSSFADE), pi / 2, 1e-6f));
|
||||
CHECK(!nka_busy(a));
|
||||
|
||||
/* As upstream: a cut still animates, and a new ease does not start over. */
|
||||
CHECK(near2(nka_tween_path(a, id, nka_id("cut"), path, 1, linear, NKA_POLICY_CUT), v2(0, 0), 0) && nka_busy(a));
|
||||
nka_tween_path(a, id, nka_id("ease"), path, 1, linear, NKA_POLICY_CROSSFADE);
|
||||
nka_update(a, 0.5f);
|
||||
q = nka_tween_path(a, id, nka_id("ease"), path, 1, nka_ease(NKA_EASE_IN_QUAD), NKA_POLICY_CROSSFADE);
|
||||
CHECK(near2(q, v2(100, 0), 1e-3f));
|
||||
nka_update(a, 0.25f);
|
||||
q = nka_tween_path(a, id, nka_id("ease"), path, 1, nka_ease(NKA_EASE_IN_QUAD), NKA_POLICY_CROSSFADE);
|
||||
CHECK(near2(q, v2(100, 50), 1e-3f));
|
||||
|
||||
uneven(a, flat);
|
||||
nka_tween_path(a, id, nka_id("raw"), flat, 1, linear, NKA_POLICY_CROSSFADE);
|
||||
nka_update(a, 0.5f);
|
||||
q = nka_tween_path(a, id, nka_id("raw"), flat, 1, linear, NKA_POLICY_CROSSFADE);
|
||||
CHECK(q.x > 100);
|
||||
nka_path_build_arc_lut(a, flat, 256);
|
||||
q = nka_tween_path(a, id, nka_id("raw"), flat, 1, linear, NKA_POLICY_CROSSFADE);
|
||||
CHECK(NEAR(q.x, 75, 0.02f));
|
||||
|
||||
circle(a, nka_id("ring"), 100);
|
||||
nka_path_build_arc_lut(a, nka_id("ring"), 256);
|
||||
nka_tween_path_angle(a, id, nka_id("spin"), nka_id("ring"), 1, linear, NKA_POLICY_CROSSFADE);
|
||||
nka_update(a, 0.25f);
|
||||
CHECK(NEAR(fabsf(nka_tween_path_angle(a, id, nka_id("spin"), nka_id("ring"), 1, linear, NKA_POLICY_CROSSFADE)), pi, 2e-3f));
|
||||
nka_update(a, 0.25f);
|
||||
CHECK(NEAR(nka_tween_path_angle(a, id, nka_id("spin"), nka_id("ring"), 1, linear, NKA_POLICY_CROSSFADE), -pi / 2, 2e-3f));
|
||||
|
||||
for (i = 0; i < 10; ++i) nka_update(a, 0.1f);
|
||||
nka_gc(a, 5);
|
||||
CHECK(a->chans.len == 0);
|
||||
CHECK(near2(nka_tween_path(a, id, ch, path, 1, linear, NKA_POLICY_CROSSFADE), v2(0, 0), 0) && nka_busy(a));
|
||||
nka_clear(a);
|
||||
CHECK(nka_path_exists(a, path) && nka_path_has_arc_lut(a, flat));
|
||||
nka_destroy(a);
|
||||
}
|
||||
|
||||
static void test_morph(void)
|
||||
{
|
||||
struct nka_context *a = nka_create(0);
|
||||
nk_hash pa = nka_id("a"), pb = nka_id("b"), sq = nka_id("square"), ring = nka_id("ring"), bare = nka_id("bare");
|
||||
struct nka_morph_opts o = nka_morph_opts_default();
|
||||
volatile float zero = 0;
|
||||
struct nk_vec2 q;
|
||||
int i;
|
||||
|
||||
CHECK(o.samples == 64 && o.match_endpoints && o.use_arc_length);
|
||||
nka_path_begin(a, pa, v2(0, 0));
|
||||
nka_path_line_to(a, v2(100, 0));
|
||||
nka_path_end(a);
|
||||
nka_path_begin(a, pb, v2(0, 100));
|
||||
nka_path_line_to(a, v2(30, 100));
|
||||
nka_path_line_to(a, v2(100, 100));
|
||||
nka_path_end(a);
|
||||
nka_path_morph(a, pa, pb, 0.5f, 0, 0);
|
||||
CHECK(nka_path_has_arc_lut(a, pa) && !nka_path_has_arc_lut(a, pb));
|
||||
for (i = 0; i <= 10; ++i) {
|
||||
float t = (float)i / 10;
|
||||
CHECK(near2(nka_path_morph(a, pa, pb, t, 0, 0), nka_path_evaluate_at_distance(a, pa, t * nka_path_length(a, pa)), 0));
|
||||
CHECK(near2(nka_path_morph(a, pa, pb, t, 1, 0), nka_path_evaluate_at_distance(a, pb, t * nka_path_length(a, pb)), 0));
|
||||
CHECK(near2(nka_path_morph(a, pa, pb, t, -2, 0), nka_path_morph(a, pa, pb, t, 0, 0), 0));
|
||||
CHECK(near2(nka_path_morph(a, pa, pb, t, 0.5f, 0), v2(100 * t, 50), 1e-3f));
|
||||
CHECK(near2(nka_path_morph(a, pa, pb, t, 0.25f, &o), v2(100 * t, 25), 1e-3f));
|
||||
CHECK(near2(nka_path_morph_tangent(a, pa, pb, t, 0.5f, 0), v2(1, 0), 1e-4f));
|
||||
CHECK(NEAR(nka_path_morph_angle(a, pa, pb, t, 0.5f, 0), 0, 1e-4f));
|
||||
CHECK(near2(nka_path_morph_tangent(a, pa, pb, t, 0, 0), nka_path_tangent_at_distance(a, pa, t * nka_path_length(a, pa)), 0));
|
||||
}
|
||||
CHECK(nka_path_has_arc_lut(a, pa) && nka_path_has_arc_lut(a, pb));
|
||||
|
||||
/* A blend leaving 0 or reaching 1 does not jump, even where t is far from arc length. */
|
||||
nka_path_begin(a, nka_id("smooth"), v2(0, 0));
|
||||
nka_path_cubic_to(a, v2(90, 0), v2(95, 0), v2(100, 0));
|
||||
nka_path_end(a);
|
||||
for (i = 0; i <= 10; ++i) {
|
||||
float t = (float)i / 10;
|
||||
CHECK(near2(nka_path_morph(a, nka_id("smooth"), pb, t, 0, 0), nka_path_morph(a, nka_id("smooth"), pb, t, 1e-4f, 0), 0.05f));
|
||||
CHECK(near2(nka_path_morph(a, pb, nka_id("smooth"), t, 1, 0), nka_path_morph(a, pb, nka_id("smooth"), t, 1 - 1e-4f, 0), 0.05f));
|
||||
}
|
||||
o.use_arc_length = 0;
|
||||
for (i = 0; i <= 10; ++i) {
|
||||
float t = (float)i / 10;
|
||||
CHECK(near2(nka_path_morph(a, nka_id("smooth"), pb, t, 0, &o), nka_path_evaluate(a, nka_id("smooth"), t), 0));
|
||||
CHECK(near2(nka_path_morph_tangent(a, nka_id("smooth"), pb, t, 0, &o), nka_path_tangent(a, nka_id("smooth"), t), 0));
|
||||
}
|
||||
o = nka_morph_opts_default();
|
||||
|
||||
nka_path_begin(a, sq, v2(50, -50));
|
||||
nka_path_line_to(a, v2(50, 50));
|
||||
nka_path_line_to(a, v2(-50, 50));
|
||||
nka_path_line_to(a, v2(-50, -50));
|
||||
nka_path_close(a);
|
||||
nka_path_end(a);
|
||||
circle(a, ring, 70);
|
||||
o.samples = 32;
|
||||
nka_path_morph(a, sq, ring, 0.5f, 0.5f, &o);
|
||||
CHECK(nka__path_get(a, sq)->lut_n == 65 && nka__path_get(a, ring)->lut_n == 65);
|
||||
for (i = 0; i < 32; i += 3) {
|
||||
float t = (float)i / 31;
|
||||
struct nk_vec2 x = nka_path_evaluate_at_distance(a, sq, t * nka_path_length(a, sq));
|
||||
struct nk_vec2 y = nka_path_evaluate_at_distance(a, ring, t * nka_path_length(a, ring));
|
||||
q = nka_path_morph(a, sq, ring, t, 0.5f, &o);
|
||||
CHECK(near2(q, v2((x.x + y.x) / 2, (x.y + y.y) / 2), 1e-3f));
|
||||
}
|
||||
CHECK(near2(nka_path_morph(a, sq, ring, 0, 0.5f, &o), v2(60, -25), 1e-3f));
|
||||
CHECK(near2(nka_path_morph(a, sq, ring, 1, 0.5f, &o), v2(60, -25), 1e-3f));
|
||||
|
||||
o.use_arc_length = 0;
|
||||
nka_path_begin(a, bare, v2(0, 0));
|
||||
nka_path_quadratic_to(a, v2(50, 100), v2(100, 0));
|
||||
nka_path_end(a);
|
||||
q = nka_path_morph(a, bare, pa, 0.5f, 0.5f, &o);
|
||||
CHECK(!nka_path_has_arc_lut(a, bare));
|
||||
CHECK(near2(q, v2(50, 25), 0.1f));
|
||||
|
||||
o = nka_morph_opts_default();
|
||||
o.samples = 1;
|
||||
CHECK(near2(nka_path_morph(a, pa, pb, 0.5f, 0.5f, &o), v2(50, 50), 1e-3f));
|
||||
o.samples = -5;
|
||||
CHECK(near2(nka_path_morph(a, pa, pb, 0.25f, 0.5f, &o), v2(25, 50), 1e-3f));
|
||||
CHECK(near2(nka_path_morph(a, pa, nka_id("missing"), 0.5f, 0.5f, 0), v2(25, 0), 1e-3f));
|
||||
CHECK(near2(nka_path_morph(a, pa, pb, zero / zero, 0.5f, 0), v2(0, 50), 0));
|
||||
CHECK(near2(nka_path_morph(a, pa, pb, 0.5f, zero / zero, 0), v2(50, 0), 0));
|
||||
nka_destroy(a);
|
||||
}
|
||||
|
||||
static void test_morph_tween(void)
|
||||
{
|
||||
struct nka_context *a = nka_create(0);
|
||||
nk_hash pa = nka_id("a"), pb = nka_id("b"), id = nka_id("mt"), ch = nka_id("c");
|
||||
struct nk_vec2 q;
|
||||
|
||||
nka_path_begin(a, pa, v2(0, 0));
|
||||
nka_path_line_to(a, v2(100, 0));
|
||||
nka_path_end(a);
|
||||
nka_path_begin(a, pb, v2(0, 100));
|
||||
nka_path_cubic_to(a, v2(30, 100), v2(70, 100), v2(100, 100));
|
||||
nka_path_end(a);
|
||||
|
||||
nka_update(a, 0.016f);
|
||||
CHECK(nka_get_morph_blend(a, id, ch) == 0);
|
||||
q = nka_tween_path_morph(a, id, ch, pa, pb, 1, 1, linear, linear, NKA_POLICY_CROSSFADE, 0);
|
||||
CHECK(near2(q, v2(0, 0), 0) && nka_busy(a) && nka_get_morph_blend(a, id, ch) == 0);
|
||||
nka_update(a, 0.5f);
|
||||
q = nka_tween_path_morph(a, id, ch, pa, pb, 1, 1, linear, linear, NKA_POLICY_CROSSFADE, 0);
|
||||
CHECK(NEAR(nka_get_morph_blend(a, id, ch), 0.5f, 1e-5f));
|
||||
CHECK(near2(q, v2(50, 50), 0.01f));
|
||||
nka_update(a, 0.25f);
|
||||
q = nka_tween_path_morph(a, id, ch, pa, pb, 1, 1, linear, linear, NKA_POLICY_CROSSFADE, 0);
|
||||
CHECK(NEAR(nka_get_morph_blend(a, id, ch), 0.75f, 1e-5f) && near2(q, v2(75, 75), 0.01f));
|
||||
nka_update(a, 0.25f);
|
||||
q = nka_tween_path_morph(a, id, ch, pa, pb, 1, 1, linear, linear, NKA_POLICY_CROSSFADE, 0);
|
||||
CHECK(nka_get_morph_blend(a, id, ch) == 1 && near2(q, v2(100, 100), 0));
|
||||
nka_update(a, 0.1f);
|
||||
q = nka_tween_path_morph(a, id, ch, pa, pb, 1, 1, linear, linear, NKA_POLICY_CROSSFADE, 0);
|
||||
CHECK(near2(q, v2(100, 100), 0) && !nka_busy(a));
|
||||
|
||||
nka_tween_path_morph(a, id, ch, pa, pb, 0, 1, linear, linear, NKA_POLICY_CROSSFADE, 0);
|
||||
nka_update(a, 0.5f);
|
||||
q = nka_tween_path_morph(a, id, ch, pa, pb, 0, 1, linear, linear, NKA_POLICY_CROSSFADE, 0);
|
||||
CHECK(NEAR(nka_get_morph_blend(a, id, ch), 0.5f, 1e-5f) && near2(q, v2(100, 50), 1e-3f) && nka_busy(a));
|
||||
q = nka_tween_path_morph(a, id, ch, pa, pb, 1, 1, linear, linear, NKA_POLICY_CUT, 0);
|
||||
CHECK(nka_get_morph_blend(a, id, ch) == 1 && near2(q, v2(100, 100), 0));
|
||||
nka_update(a, 2);
|
||||
q = nka_tween_path_morph(a, id, ch, pa, pb, 1, 1, linear, linear, NKA_POLICY_CUT, 0);
|
||||
CHECK(near2(q, v2(100, 100), 0) && !nka_busy(a));
|
||||
|
||||
q = nka_tween_path_morph(a, id, nka_id("cut"), pa, pb, 0.25f, 1, linear, linear, NKA_POLICY_CUT, 0);
|
||||
CHECK(nka_get_morph_blend(a, id, nka_id("cut")) == 0.25f && near2(q, v2(0, 25), 1e-3f) && nka_busy(a));
|
||||
CHECK(nka_get_morph_blend(a, id, nka_id("none")) == 0 && nka_get_morph_blend(0, id, ch) == 0);
|
||||
nka_destroy(a);
|
||||
}
|
||||
|
||||
static void test_quads(void)
|
||||
{
|
||||
struct nk_vec2 quad[4], glyph[4];
|
||||
int i;
|
||||
|
||||
nka_make_glyph_quad(quad, v2(10, 20), 0, 8, 12, 0);
|
||||
CHECK(near2(quad[0], v2(6, 20), 1e-5f) && near2(quad[1], v2(14, 20), 1e-5f));
|
||||
CHECK(near2(quad[2], v2(14, 8), 1e-5f) && near2(quad[3], v2(6, 8), 1e-5f));
|
||||
nka_make_glyph_quad(quad, v2(10, 20), 0, 8, 12, 3);
|
||||
CHECK(near2(quad[0], v2(6, 17), 1e-5f) && near2(quad[2], v2(14, 5), 1e-5f));
|
||||
nka_make_glyph_quad(glyph, v2(0, 0), pi / 2, 8, 12, 0);
|
||||
CHECK(near2(glyph[0], v2(0, -4), 1e-5f) && near2(glyph[1], v2(0, 4), 1e-5f));
|
||||
CHECK(near2(glyph[2], v2(12, 4), 1e-5f) && near2(glyph[3], v2(12, -4), 1e-5f));
|
||||
nka_make_glyph_quad(quad, v2(0, 0), 0, 8, 12, 0);
|
||||
nka_transform_quad(quad, v2(0, 0), pi / 2, v2(0, 0));
|
||||
for (i = 0; i < 4; ++i) CHECK(near2(quad[i], glyph[i], 1e-5f));
|
||||
|
||||
quad[0] = v2(0, 0); quad[1] = v2(10, 0); quad[2] = v2(10, 10); quad[3] = v2(0, 10);
|
||||
nka_transform_quad(quad, v2(5, 5), pi / 2, v2(100, 50));
|
||||
CHECK(near2(quad[0], v2(110, 50), 1e-4f) && near2(quad[1], v2(110, 60), 1e-4f));
|
||||
CHECK(near2(quad[2], v2(100, 60), 1e-4f) && near2(quad[3], v2(100, 50), 1e-4f));
|
||||
nka_transform_quad(quad, v2(0, 0), 0, v2(-100, -50));
|
||||
CHECK(near2(quad[0], v2(10, 0), 1e-4f) && near2(quad[2], v2(0, 10), 1e-4f));
|
||||
nka_transform_quad(0, v2(0, 0), 1, v2(0, 0));
|
||||
nka_make_glyph_quad(0, v2(0, 0), 1, 1, 1, 1);
|
||||
}
|
||||
|
||||
static int live;
|
||||
|
||||
static void *count_alloc(nk_handle h, void *old, nk_size n)
|
||||
{
|
||||
(void)h; (void)old;
|
||||
live++;
|
||||
return malloc(n);
|
||||
}
|
||||
|
||||
static void count_free(nk_handle h, void *p)
|
||||
{
|
||||
(void)h;
|
||||
live--;
|
||||
free(p);
|
||||
}
|
||||
|
||||
static void wave(struct nka_context *a, nk_hash id, int n)
|
||||
{
|
||||
int i;
|
||||
nka_path_begin(a, id, v2(0, 0));
|
||||
for (i = 0; i < n; ++i) nka_path_quadratic_to(a, v2((float)i * 20 + 10, i & 1 ? 20.0f : -20.0f), v2((float)i * 20 + 20, 0));
|
||||
nka_path_end(a);
|
||||
}
|
||||
|
||||
static void frame(struct nka_context *a)
|
||||
{
|
||||
nka_update(a, 0.016f);
|
||||
wave(a, 1, 20);
|
||||
nka_path_build_arc_lut(a, 1, 128);
|
||||
wave(a, 2, 5);
|
||||
nka_path_morph(a, 1, 2, 0.5f, 0.5f, 0);
|
||||
nka_tween_path(a, 7, 7, 1, 1, linear, NKA_POLICY_CROSSFADE);
|
||||
nka_tween_path_morph(a, 7, 8, 1, 2, 1, 1, linear, linear, NKA_POLICY_CROSSFADE, 0);
|
||||
}
|
||||
|
||||
static void test_memory(void)
|
||||
{
|
||||
struct nk_allocator al;
|
||||
struct nka_context *a;
|
||||
int i, settled;
|
||||
al.userdata.ptr = 0;
|
||||
al.alloc = count_alloc;
|
||||
al.free = count_free;
|
||||
a = nka_create(&al);
|
||||
for (i = 0; i < 3; ++i) frame(a);
|
||||
settled = live;
|
||||
for (i = 0; i < 100; ++i) frame(a);
|
||||
CHECK(live == settled);
|
||||
CHECK(nka_path_exists(a, 1) && nka_path_has_arc_lut(a, 1) && nka_path_has_arc_lut(a, 2));
|
||||
wave(a, 1, 300);
|
||||
wave(a, 1, 3);
|
||||
nka_path_build_arc_lut(a, 1, 4000);
|
||||
CHECK(nka__path_get(a, 1)->n == 3);
|
||||
nka_path_begin(a, 9, v2(0, 0));
|
||||
nka_path_line_to(a, v2(1, 0));
|
||||
nka_destroy(a);
|
||||
CHECK(live == 0);
|
||||
nka_path_begin(0, 1, v2(0, 0));
|
||||
nka_path_line_to(0, v2(1, 0));
|
||||
nka_path_end(0);
|
||||
CHECK(!nka_path_exists(0, 1) && near2(nka_path_morph(0, 1, 2, 0.5f, 0.5f, 0), v2(0, 0), 0));
|
||||
CHECK(near2(nka_tween_path(0, 1, 1, 1, 1, linear, NKA_POLICY_CROSSFADE), v2(0, 0), 0));
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
test_curves();
|
||||
test_build();
|
||||
test_tangents();
|
||||
test_arc_length();
|
||||
test_tweens();
|
||||
test_morph();
|
||||
test_morph_tween();
|
||||
test_quads();
|
||||
test_memory();
|
||||
if (failures) printf("%d failed\n", failures);
|
||||
else printf("path: ok\n");
|
||||
return failures != 0;
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
#!/usr/bin/env python3
|
||||
"""gif.py CLIPS OUTDIR SCENE...
|
||||
|
||||
Runs the clips example for each scene and turns its raw frames into
|
||||
OUTDIR/SCENE.gif. Needs Pillow. Run from the repository's root.
|
||||
"""
|
||||
import os
|
||||
import struct
|
||||
import subprocess
|
||||
import sys
|
||||
import tempfile
|
||||
|
||||
from PIL import Image
|
||||
|
||||
|
||||
def frames(path):
|
||||
with open(path, "rb") as f:
|
||||
magic, w, h, ms = struct.unpack("<4I", f.read(16))
|
||||
if magic != 0x464B414E:
|
||||
sys.exit(f"{path}: not a clips file")
|
||||
size = w * h * 4
|
||||
while True:
|
||||
data = f.read(size)
|
||||
if len(data) < size:
|
||||
return
|
||||
yield ms, Image.frombytes("RGBA", (w, h), data).convert("RGB")
|
||||
|
||||
|
||||
def main(argv):
|
||||
if len(argv) < 3:
|
||||
sys.exit(__doc__)
|
||||
clips, outdir, scenes = argv[0], argv[1], argv[2:]
|
||||
os.makedirs(outdir, exist_ok=True)
|
||||
for scene in scenes:
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
raw = os.path.join(tmp, scene + ".raw")
|
||||
subprocess.run([clips, scene, raw], check=True)
|
||||
images, ms = [], 30
|
||||
for ms, image in frames(raw):
|
||||
images.append(image)
|
||||
picks = images[:: max(1, len(images) // 16)]
|
||||
w, h = picks[0].size
|
||||
mosaic = Image.new("RGB", (w, h * len(picks)))
|
||||
for k, image in enumerate(picks):
|
||||
mosaic.paste(image, (0, k * h))
|
||||
palette = mosaic.quantize(colors=255, method=Image.Quantize.MEDIANCUT)
|
||||
quantized = [image.quantize(palette=palette, dither=Image.Dither.NONE) for image in images]
|
||||
out = os.path.join(outdir, scene + ".gif")
|
||||
quantized[0].save(out, save_all=True, append_images=quantized[1:], duration=ms, loop=0, optimize=True)
|
||||
print(f"{out}: {len(images)} frames, {os.path.getsize(out) // 1024} KiB")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main(sys.argv[1:])
|
||||