fix animations target and compiler warnings

This commit is contained in:
hippoz 2023-04-13 03:47:38 +03:00
parent 88aa8f8202
commit 7b75cdb768
Signed by: hippoz
GPG key ID: 56C4E02A85F2FBED
16 changed files with 41 additions and 37 deletions

View file

@ -1,6 +1,6 @@
CC?=cc
CFLAGS_LIBS:=`pkg-config --cflags --libs xcb cairo pangocairo` -lm
CFLAGS:=$(CFLAGS) -pipe -Wall -Wextra -Wshadow -std=c99 -pedantic $(CFLAGS_LIBS)
CC=clang
LIBS:=`pkg-config --libs xcb cairo pangocairo` -lm
CFLAGS:=$(CFLAGS) -pipe -Wall -Wextra -Wshadow -std=c99 -pedantic `pkg-config --cflags xcb cairo pangocairo`
BUILD=build
OBJ=$(BUILD)
@ -20,7 +20,7 @@ release: CFLAGS+=-O2 -flto=auto -DNDEBUG
release: clean $(BUILD) $(BIN)
$(BIN): $(OBJS)
$(CC) $(CFLAGS) $(OBJS) -o $@
$(CC) $(CFLAGS) $(LIBS) $(OBJS) -o $@
-include $(DEPS)

View file

@ -22,9 +22,9 @@ typedef struct AppState {
int count;
char counter_text[12];
uint64_t sidebar_animate_duration_ms;
int64_t sidebar_animate_duration_ms;
uint64_t sidebar_animate_time_ms;
int64_t sidebar_animate_time_ms;
double sidebar_animate_target_w;
double sidebar_animate_start_w;
} AppState;
@ -60,7 +60,7 @@ int app_handle(struct UINode *node, void *data, int event_type, size_t d, void *
/* sidebar */
{
UINode *sidebar = node_new(node, "sidebar");
sidebar->width_policy = UI_SIZE_POLICY_STATIC;
sidebar->width_policy = UI_SIZE_POLICY_FIXED;
sidebar->height_policy = UI_SIZE_POLICY_GROW;
sidebar->rect.w = 200;
dispatcher_new(sidebar, UI_EVENT_TIMER_END, SIDEBAR_ANIAMTE, state, app_handle);
@ -108,19 +108,24 @@ int app_handle(struct UINode *node, void *data, int event_type, size_t d, void *
break;
}
case SIDEBAR_ANIAMTE: {
uint64_t dt = *(uint64_t*)p;
int64_t dt = *(int64_t*)p;
state->sidebar_animate_time_ms += dt;
state->sidebar_node->rect.w = lerp(state->sidebar_animate_start_w, state->sidebar_animate_target_w, (double)state->sidebar_animate_time_ms / (double)state->sidebar_animate_duration_ms);
if (state->sidebar_node->rect.w < 0) {
state->sidebar_node->rect.w = 0;
}
node_request_relayout(state->sidebar_node->parent);
if (state->sidebar_animate_time_ms >= state->sidebar_animate_duration_ms) {
state->sidebar_node->rect.w = state->sidebar_animate_target_w;
}
node_request_relayout(state->sidebar_node);
if (state->sidebar_node->rect.w == state->sidebar_animate_target_w) {
state->sidebar_animate_time_ms = 0;
return 0;
}
window_sched_timer(node->window, state->sidebar_node, 10);
return 1;
}
case INCREMENT_COUNT: {
@ -142,7 +147,7 @@ int app_handle(struct UINode *node, void *data, int event_type, size_t d, void *
return 0;
}
int main()
int main(void)
{
UIWindow *window = window_new(800, 600);
if (!window) {

View file

@ -58,7 +58,6 @@ int text_node_handle(UINode *node, enum UIEvent ev, size_t d, void *p)
pango_layout_get_size(n->layout, &w, NULL);
*(double*)p = pango_units_to_double(w);
return 1;
break;
}
case UI_EVENT_GET_HEIGHT: {
if (!n->layout) {
@ -68,7 +67,6 @@ int text_node_handle(UINode *node, enum UIEvent ev, size_t d, void *p)
pango_layout_get_size(n->layout, NULL, &h);
*(double*)p = pango_units_to_double(h);
return 1;
break;
}
case UI_EVENT_RELAYOUT: {
if (!n->layout) {
@ -91,4 +89,3 @@ int text_node_handle(UINode *node, enum UIEvent ev, size_t d, void *p)
}
return 0;
}

View file

@ -10,6 +10,8 @@
#include <time.h>
#include <xcb/xcb.h>
void _window_process_xcb_event(UIWindow *window, xcb_generic_event_t *event);
int window_invalidate_node(UIWindow *window, UINode *node)
{
if (node->flags & UI_NODE_COMPONENT) {
@ -285,14 +287,14 @@ int window_turn(UIWindow *window)
fds[0].fd = xcb_get_file_descriptor(window->_xcb_connection);
fds[0].events = POLLIN;
uint64_t frame_peak_ms = 0;
uint64_t frame_peak_last_measurement_ms = 0;
int64_t frame_peak_ms = 0;
int64_t frame_peak_last_measurement_ms = 0;
for (;;) {
int64_t poll_timeout;
/* compute `poll_timeout` based on active timers */
{
uint64_t now = time_current_ms();
int64_t now = time_current_ms();
int64_t lowest = 0;
bool has_timer = false;
for (int i = 0; i < UI_WINDOW_MAX_TIMERS; i++) {
@ -325,7 +327,7 @@ int window_turn(UIWindow *window)
clear_summary();
uint64_t frame_start_ms = time_current_ms();
int64_t frame_start_ms = time_current_ms();
for (int i = 0; i < UI_WINDOW_MAX_POLL; i++) {
if (fds[i].revents & POLLNVAL) {
fprintf(stderr, "err: window_turn(): poll got POLLNVAL\n");
@ -354,10 +356,10 @@ int window_turn(UIWindow *window)
for (int j = 0; j < UI_WINDOW_MAX_TIMERS; j++) {
UIWindowTimer *timer = &window->timers[j];
if (timer->present) {
uint64_t now = time_current_ms();
int64_t now = time_current_ms();
int64_t distance = (timer->started_at_ms + timer->duration_ms) - now;
if (distance <= 0) {
uint64_t dt = time_current_ms() - timer->started_at_ms;
int64_t dt = time_current_ms() - timer->started_at_ms;
UINode *target = timer->target;
timer->present = false;
timer->started_at_ms = 0;
@ -381,8 +383,8 @@ int window_turn(UIWindow *window)
has_looped_once = true;
uint64_t frame_end_ms = time_current_ms();
uint64_t frame_delta_ms = frame_end_ms - frame_start_ms;
int64_t frame_end_ms = time_current_ms();
int64_t frame_delta_ms = frame_end_ms - frame_start_ms;
if (frame_delta_ms > frame_peak_ms || frame_end_ms - frame_peak_last_measurement_ms >= 3000) {
frame_peak_last_measurement_ms = frame_end_ms;
frame_peak_ms = frame_delta_ms;
@ -394,7 +396,7 @@ int window_turn(UIWindow *window)
return 0;
}
UIWindowTimer *window_sched_timer(UIWindow *window, UINode *target, uint64_t duration_ms)
UIWindowTimer *window_sched_timer(UIWindow *window, UINode *target, int64_t duration_ms)
{
for (int i = 0; i < UI_WINDOW_MAX_TIMERS; i++) {
if (!window->timers[i].present) {

View file

@ -13,8 +13,8 @@
typedef struct UIWindowTimer {
bool present;
uint64_t started_at_ms;
uint64_t duration_ms;
int64_t started_at_ms;
int64_t duration_ms;
UINode *target; // TODO: what if the node gets deleted while the timer is still running?
} UIWindowTimer;
@ -38,7 +38,7 @@ UIWindow *window_new(int width, int height);
bool window_flush_invalidated(UIWindow *window);
int window_attach_root(UIWindow *window, UINode *root);
int window_turn(UIWindow *window);
UIWindowTimer *window_sched_timer(UIWindow *window, UINode *target, uint64_t duration_ms);
UIWindowTimer *window_sched_timer(UIWindow *window, UINode *target, int64_t duration_ms);
#endif // _UI__WINDOW_H