demo: move to fbuf for HUD, making it portable

This commit is contained in:
nick black 2021-08-05 03:34:45 -04:00
parent 3a7306e1d2
commit 4ed0e09e26
No known key found for this signature in database
GPG Key ID: 5F43400C21CBFACC
4 changed files with 88 additions and 63 deletions

View File

@ -90,7 +90,7 @@ set_package_properties(Threads PROPERTIES TYPE REQUIRED)
find_package(ZLIB) find_package(ZLIB)
set_package_properties(ZLIB PROPERTIES TYPE REQUIRED) set_package_properties(ZLIB PROPERTIES TYPE REQUIRED)
# platform-specific logics # platform-specific logics
if(MSYS OR WIN32) if(WIN32)
set(LIBRT_LIBRARIES wsock32 ws2_32) set(LIBRT_LIBRARIES wsock32 ws2_32)
elseif(NOT APPLE) elseif(NOT APPLE)
find_library(LIBM m REQUIRED) find_library(LIBM m REQUIRED)
@ -107,11 +107,13 @@ pkg_search_module(TERMINFO REQUIRED tinfo>=6.1 ncursesw>=6.1)
set_property(GLOBAL APPEND PROPERTY PACKAGES_FOUND terminfo) set_property(GLOBAL APPEND PROPERTY PACKAGES_FOUND terminfo)
set_package_properties(terminfo PROPERTIES TYPE REQUIRED) set_package_properties(terminfo PROPERTIES TYPE REQUIRED)
set(PKGCONF_REQ_PRIV "${TERMINFO_LIBRARIES}") set(PKGCONF_REQ_PRIV "${TERMINFO_LIBRARIES}")
if(NOT WIN32)
if(${USE_READLINE}) if(${USE_READLINE})
pkg_search_module(READLINE REQUIRED readline>=8.0) pkg_search_module(READLINE REQUIRED readline>=8.0)
set_property(GLOBAL APPEND PROPERTY PACKAGES_FOUND readline) set_property(GLOBAL APPEND PROPERTY PACKAGES_FOUND readline)
set_package_properties(readline PROPERTIES TYPE REQUIRED) set_package_properties(readline PROPERTIES TYPE REQUIRED)
endif() endif()
endif()
if(${USE_FFMPEG}) if(${USE_FFMPEG})
pkg_check_modules(AVCODEC REQUIRED libavcodec>=57.0) pkg_check_modules(AVCODEC REQUIRED libavcodec>=57.0)
pkg_check_modules(AVFORMAT REQUIRED libavformat>=57.0) pkg_check_modules(AVFORMAT REQUIRED libavformat>=57.0)
@ -207,20 +209,20 @@ target_link_libraries(notcurses-core
"${ZLIB_LIBRARIES}" "${ZLIB_LIBRARIES}"
"${TERMINFO_LIBRARIES}" "${TERMINFO_LIBRARIES}"
"${LIBM}" "${LIBM}"
"${LIBRT_LIBRARIES}"
"${unistring}" "${unistring}"
PUBLIC PUBLIC
Threads::Threads Threads::Threads
"${LIBRT_LIBRARIES}"
) )
target_link_libraries(notcurses-core-static target_link_libraries(notcurses-core-static
PRIVATE PRIVATE
"${ZLIB_STATIC_LIBRARIES}" "${ZLIB_STATIC_LIBRARIES}"
"${TERMINFO_STATIC_LIBRARIES}" "${TERMINFO_STATIC_LIBRARIES}"
"${LIBM}" "${LIBM}"
"${LIBRT_LIBRARIES}"
"${unistring}" "${unistring}"
PUBLIC PUBLIC
Threads::Threads Threads::Threads
"${LIBRT_LIBRARIES}"
) )
target_link_directories(notcurses-core target_link_directories(notcurses-core
PRIVATE PRIVATE
@ -272,6 +274,14 @@ target_include_directories(notcurses-static
"${PROJECT_BINARY_DIR}/include" "${PROJECT_BINARY_DIR}/include"
"${TERMINFO_INCLUDE_DIRS}" "${TERMINFO_INCLUDE_DIRS}"
) )
target_compile_definitions(notcurses
PRIVATE
_GNU_SOURCE _DEFAULT_SOURCE
)
target_compile_definitions(notcurses-static
PRIVATE
_GNU_SOURCE _DEFAULT_SOURCE
)
target_link_libraries(notcurses target_link_libraries(notcurses
PUBLIC PUBLIC
notcurses-core notcurses-core
@ -519,6 +529,10 @@ target_link_libraries(notcurses-demo
# notcurses-info # notcurses-info
file(GLOB INFOSRCS CONFIGURE_DEPENDS src/info/*.c) file(GLOB INFOSRCS CONFIGURE_DEPENDS src/info/*.c)
add_executable(notcurses-info ${INFOSRCS} ${COMPATSRC}) add_executable(notcurses-info ${INFOSRCS} ${COMPATSRC})
target_compile_definitions(notcurses-info
PRIVATE
_GNU_SOURCE _DEFAULT_SOURCE
)
target_include_directories(notcurses-info target_include_directories(notcurses-info
PRIVATE PRIVATE
src src
@ -572,6 +586,7 @@ target_include_directories(ncneofetch
include include
"${CMAKE_REQUIRED_INCLUDES}" "${CMAKE_REQUIRED_INCLUDES}"
"${PROJECT_BINARY_DIR}/include" "${PROJECT_BINARY_DIR}/include"
src
) )
target_link_libraries(ncneofetch target_link_libraries(ncneofetch
PRIVATE PRIVATE
@ -870,8 +885,8 @@ install(FILES ${MARKDOWN} DESTINATION ${CMAKE_INSTALL_DOCDIR})
install(TARGETS notcurses-demo DESTINATION bin) install(TARGETS notcurses-demo DESTINATION bin)
install(TARGETS notcurses-info DESTINATION bin) install(TARGETS notcurses-info DESTINATION bin)
install(TARGETS notcurses-input DESTINATION bin) install(TARGETS notcurses-input DESTINATION bin)
install(TARGETS nctetris DESTINATION bin)
install(TARGETS ncneofetch DESTINATION bin) install(TARGETS ncneofetch DESTINATION bin)
install(TARGETS nctetris DESTINATION bin)
if(NOT ${USE_MULTIMEDIA} STREQUAL "none") if(NOT ${USE_MULTIMEDIA} STREQUAL "none")
install(TARGETS ncls DESTINATION bin) install(TARGETS ncls DESTINATION bin)
install(TARGETS ncplayer DESTINATION bin) install(TARGETS ncplayer DESTINATION bin)

View File

@ -1,6 +1,8 @@
#include "demo.h" #include "demo.h"
#include <pthread.h> #include <pthread.h>
#include <inttypes.h> #include <inttypes.h>
#include "lib/fbuf.h"
#include "lib/internal.h"
// we provide a heads-up display throughout the demo, detailing the demos we're // we provide a heads-up display throughout the demo, detailing the demos we're
// about to run, running, and just runned. the user can move this HUD with // about to run, running, and just runned. the user can move this HUD with
@ -69,13 +71,13 @@ hud_standard_bg_rgb(struct ncplane* n){
static int static int
count_debug_lines(const char* output, size_t outputlen){ count_debug_lines(const char* output, size_t outputlen){
int lines = 0; int ll = 0;
for(size_t i = 0 ; i < outputlen ; ++i){ for(size_t i = 0 ; i < outputlen ; ++i){
if(output[i] == '\n'){ if(output[i] == '\n'){
++lines; ++ll;
} }
} }
return lines; return ll;
} }
static void static void
@ -88,20 +90,15 @@ debug_toggle(struct notcurses* nc){
} }
int dimy, dimx; int dimy, dimx;
notcurses_term_dim_yx(nc, &dimy, &dimx); notcurses_term_dim_yx(nc, &dimy, &dimx);
char* output = NULL; fbuf f;
size_t outputlen = 0; if(fbuf_init_small(&f)){
FILE* mstream = open_memstream(&output, &outputlen);
if(mstream == NULL){
return;
}
notcurses_debug(nc, mstream);
if(fclose(mstream)){
return; return;
} }
notcurses_debug_fbuf(nc, &f);
ncplane_options nopts = { ncplane_options nopts = {
.y = 3, .y = 3,
.x = NCALIGN_CENTER, .x = NCALIGN_CENTER,
.rows = count_debug_lines(output, outputlen) + 1, .rows = count_debug_lines(f.buf, f.used) + 1,
// make it one column longer than the maximum debug output, so that a full // make it one column longer than the maximum debug output, so that a full
// line of output doesn't cause trigger a newline. we'll make the last // line of output doesn't cause trigger a newline. we'll make the last
// column transparent. // column transparent.
@ -111,7 +108,7 @@ debug_toggle(struct notcurses* nc){
}; };
struct ncplane* n = ncplane_create(notcurses_stdplane(nc), &nopts); struct ncplane* n = ncplane_create(notcurses_stdplane(nc), &nopts);
if(n == NULL){ if(n == NULL){
free(output); fbuf_free(&f);
return; return;
} }
uint64_t channels = 0; uint64_t channels = 0;
@ -121,11 +118,13 @@ debug_toggle(struct notcurses* nc){
ncplane_set_scrolling(n, true); ncplane_set_scrolling(n, true);
ncplane_set_fg_rgb(n, 0x0a0a0a); ncplane_set_fg_rgb(n, 0x0a0a0a);
ncplane_set_bg_rgb(n, 0xffffe5); ncplane_set_bg_rgb(n, 0xffffe5);
if(ncplane_puttext(n, 0, NCALIGN_LEFT, output, &outputlen) < 0){ size_t b = f.used;
free(output); if(ncplane_puttext(n, 0, NCALIGN_LEFT, f.buf, &b) < 0){
fbuf_free(&f);
ncplane_destroy(n); ncplane_destroy(n);
return; return;
} }
fbuf_free(&f);
for(int y = 0 ; y < ncplane_dim_y(n) ; ++y){ for(int y = 0 ; y < ncplane_dim_y(n) ; ++y){
nccell c = CELL_TRIVIAL_INITIALIZER; nccell c = CELL_TRIVIAL_INITIALIZER;
nccell_set_fg_alpha(&c, NCALPHA_TRANSPARENT); nccell_set_fg_alpha(&c, NCALPHA_TRANSPARENT);
@ -134,7 +133,6 @@ debug_toggle(struct notcurses* nc){
nccell_release(n, &c); nccell_release(n, &c);
} }
ncplane_putstr_aligned(n, ncplane_dim_y(n) - 1, NCALIGN_CENTER, "Press Alt+d to hide this window"); ncplane_putstr_aligned(n, ncplane_dim_y(n) - 1, NCALIGN_CENTER, "Press Alt+d to hide this window");
free(output);
debug = n; debug = n;
} }

View File

@ -8,48 +8,12 @@ void notcurses_debug_caps(const notcurses* nc, FILE* debugfp){
(void)debugfp; (void)debugfp;
} }
static void
ncpile_debug(const ncpile* p, FILE* debugfp){
fprintf(debugfp, " ************************* %16p pile ****************************\n", p);
const ncplane* n = p->top;
const ncplane* prev = NULL;
int planeidx = 0;
while(n){
fprintf(debugfp, "%04d off y: %3d x: %3d geom y: %3d x: %3d curs y: %3d x: %3d %p %.4s\n",
planeidx, n->absy, n->absx, n->leny, n->lenx, n->y, n->x, n, n->name);
if(n->boundto || n->bnext || n->bprev || n->blist){
fprintf(debugfp, " bound %p ← %p → %p binds %p\n",
n->boundto, n->bprev, n->bnext, n->blist);
}
if(n->bprev && (*n->bprev != n)){
fprintf(stderr, " WARNING: expected *->bprev %p, got %p\n", n, *n->bprev);
}
if(n->above != prev){
fprintf(stderr, " WARNING: expected ->above %p, got %p\n", prev, n->above);
}
if(ncplane_pile_const(n) != p){
fprintf(stderr, " WARNING: expected pile %p, got %p\n", p, ncplane_pile_const(n));
}
prev = n;
n = n->below;
++planeidx;
}
if(p->bottom != prev){
fprintf(stderr, " WARNING: expected ->bottom %p, got %p\n", prev, p->bottom);
}
}
void notcurses_debug(const notcurses* nc, FILE* debugfp){ void notcurses_debug(const notcurses* nc, FILE* debugfp){
const ncpile* p = ncplane_pile(nc->stdplane); fbuf f;
fprintf(debugfp, " -------------------------- notcurses debug state -----------------------------\n"); if(fbuf_init_small(&f)){
const ncpile* p0 = p; return;
do{ }
ncpile_debug(p0, debugfp); notcurses_debug_fbuf(nc, &f);
const ncpile* prev = p0; fbuf_finalize(&f, debugfp, true);
p0 = p0->next; fbuf_free(&f);
if(p0->prev != prev){
fprintf(stderr, "WARNING: expected ->prev %p, got %p\n", prev, p0->prev);
}
}while(p != p0);
fprintf(debugfp, " ______________________________________________________________________________\n");
} }

View File

@ -30,6 +30,7 @@ extern "C" {
#include "termdesc.h" #include "termdesc.h"
#include "egcpool.h" #include "egcpool.h"
#include "sprite.h" #include "sprite.h"
#include "fbuf.h"
#define API __attribute__((visibility("default"))) #define API __attribute__((visibility("default")))
#define ALLOC __attribute__((malloc)) __attribute__((warn_unused_result)) #define ALLOC __attribute__((malloc)) __attribute__((warn_unused_result))
@ -644,6 +645,53 @@ plane_debug(const ncplane* n, bool details){
} }
} }
static inline void
ncpile_debug(const ncpile* p, fbuf* f){
fbuf_printf(f, " ************************* %16p pile ****************************\n", p);
const ncplane* n = p->top;
const ncplane* prev = NULL;
int planeidx = 0;
while(n){
fbuf_printf(f, "%04d off y: %3d x: %3d geom y: %3d x: %3d curs y: %3d x: %3d %p %.4s\n",
planeidx, n->absy, n->absx, n->leny, n->lenx, n->y, n->x, n, n->name);
if(n->boundto || n->bnext || n->bprev || n->blist){
fbuf_printf(f, " bound %p ← %p → %p binds %p\n",
n->boundto, n->bprev, n->bnext, n->blist);
}
if(n->bprev && (*n->bprev != n)){
fbuf_printf(f, " WARNING: expected *->bprev %p, got %p\n", n, *n->bprev);
}
if(n->above != prev){
fbuf_printf(f, " WARNING: expected ->above %p, got %p\n", prev, n->above);
}
if(ncplane_pile_const(n) != p){
fbuf_printf(f, " WARNING: expected pile %p, got %p\n", p, ncplane_pile_const(n));
}
prev = n;
n = n->below;
++planeidx;
}
if(p->bottom != prev){
fbuf_printf(f, " WARNING: expected ->bottom %p, got %p\n", prev, p->bottom);
}
}
static inline void
notcurses_debug_fbuf(const notcurses* nc, fbuf* f){
const ncpile* p = ncplane_pile(nc->stdplane);
fbuf_printf(f, " -------------------------- notcurses debug state -----------------------------\n");
const ncpile* p0 = p;
do{
ncpile_debug(p0, f);
const ncpile* prev = p0;
p0 = p0->next;
if(p0->prev != prev){
fbuf_printf(f, "WARNING: expected ->prev %p, got %p\n", prev, p0->prev);
}
}while(p != p0);
fbuf_printf(f, " ______________________________________________________________________________\n");
}
// cell coordinates *within the sprixel*, not absolute // cell coordinates *within the sprixel*, not absolute
int sprite_wipe(const notcurses* nc, sprixel* s, int y, int x); int sprite_wipe(const notcurses* nc, sprixel* s, int y, int x);
void sprixel_free(sprixel* s); void sprixel_free(sprixel* s);