embed_fonts
This commit is contained in:
parent
20d662424e
commit
5dbd00a65c
1
.gitignore
vendored
1
.gitignore
vendored
@ -15,3 +15,4 @@ libs/
|
|||||||
# user files
|
# user files
|
||||||
.old*/
|
.old*/
|
||||||
current.config
|
current.config
|
||||||
|
src/generated/
|
||||||
12
Makefile
12
Makefile
@ -4,8 +4,10 @@
|
|||||||
|
|
||||||
all: build_exec_dbg
|
all: build_exec_dbg
|
||||||
|
|
||||||
|
rebuild_all: rebuild_kerep rebuild_imgui rebuild_imgui_node_editor embed_fonts
|
||||||
|
|
||||||
# creates executable using profile info generated by profile
|
# creates executable using profile info generated by profile
|
||||||
build_exec: rebuild_kerep rebuild_imgui profile
|
build_exec: rebuild_all profile
|
||||||
@cbuild/call_task.sh build_exec 2>&1 | tee -a make_raw.log
|
@cbuild/call_task.sh build_exec 2>&1 | tee -a make_raw.log
|
||||||
|
|
||||||
# creates executable with debug info and no optimizations
|
# creates executable with debug info and no optimizations
|
||||||
@ -67,3 +69,11 @@ rebuild_kerep:
|
|||||||
# recompile imgui.a in the next build task
|
# recompile imgui.a in the next build task
|
||||||
rebuild_imgui:
|
rebuild_imgui:
|
||||||
@cbuild/call_task.sh rebuild_imgui
|
@cbuild/call_task.sh rebuild_imgui
|
||||||
|
|
||||||
|
rebuild_imgui_node_editor:
|
||||||
|
@cbuild/call_task.sh rebuild_imgui_node_editor
|
||||||
|
|
||||||
|
# writes ttf fonts fron ./fonts/ to C compressed arrays in C source files
|
||||||
|
# builds static library from font arrays definitions
|
||||||
|
embed_fonts:
|
||||||
|
@cbuild/call_task.sh embed_fonts
|
||||||
|
|||||||
2
cbuild
2
cbuild
@ -1 +1 @@
|
|||||||
Subproject commit 2bebe76c7e14155392a167bdda61113d0039e188
|
Subproject commit e83a7affefe85ec7573ce7465e543747bbf5d477
|
||||||
@ -1,6 +1,6 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
CBUILD_VERSION=6
|
CBUILD_VERSION=6
|
||||||
CONFIG_VERSION=3
|
CONFIG_VERSION=4
|
||||||
|
|
||||||
PROJECT="GraphC"
|
PROJECT="GraphC"
|
||||||
CMP_C="gcc"
|
CMP_C="gcc"
|
||||||
@ -47,6 +47,22 @@ case "$TASK" in
|
|||||||
rebuild_imgui)
|
rebuild_imgui)
|
||||||
TASK_SCRIPT=tasks/rebuild_imgui.sh
|
TASK_SCRIPT=tasks/rebuild_imgui.sh
|
||||||
;;
|
;;
|
||||||
|
rebuild_imgui_node_editor)
|
||||||
|
TASK_SCRIPT=tasks/rebuild_imgui_node_editor.sh
|
||||||
|
;;
|
||||||
|
|
||||||
|
# writes ttf fonts fron ./fonts/ to C compressed arrays in C source files
|
||||||
|
# builds static library from font arrays definitions
|
||||||
|
embed_fonts)
|
||||||
|
SRC_CPP=""
|
||||||
|
SRC_C=""
|
||||||
|
C_ARGS="-O0 -fpic"
|
||||||
|
WARN_C="-Wno-unused-const-variable"
|
||||||
|
CPP_ARGS="$C_ARGS"
|
||||||
|
STATIC_LIB_FILE="fonts_embedded.a"
|
||||||
|
TASK_SCRIPT=tasks/embed_fonts.sh
|
||||||
|
;;
|
||||||
|
|
||||||
# creates executable using profile info generated by build_profile
|
# creates executable using profile info generated by build_profile
|
||||||
build_exec)
|
build_exec)
|
||||||
# -flto applies more optimizations across object files
|
# -flto applies more optimizations across object files
|
||||||
|
|||||||
1
fonts/.gitignore
vendored
Normal file
1
fonts/.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
generated/
|
||||||
BIN
fonts/Cousine-Regular.ttf
Normal file
BIN
fonts/Cousine-Regular.ttf
Normal file
Binary file not shown.
BIN
fonts/DroidSans.ttf
Normal file
BIN
fonts/DroidSans.ttf
Normal file
Binary file not shown.
388
fonts/binary_to_compressed_c.cpp
Normal file
388
fonts/binary_to_compressed_c.cpp
Normal file
@ -0,0 +1,388 @@
|
|||||||
|
// dear imgui
|
||||||
|
// (binary_to_compressed_c.cpp)
|
||||||
|
// Helper tool to turn a file into a C array, if you want to embed font data in your source code.
|
||||||
|
|
||||||
|
// The data is first compressed with stb_compress() to reduce source code size,
|
||||||
|
// then encoded in Base85 to fit in a string so we can fit roughly 4 bytes of compressed data into 5 bytes of source code (suggested by @mmalex)
|
||||||
|
// (If we used 32-bit constants it would require take 11 bytes of source code to encode 4 bytes, and be endianness dependent)
|
||||||
|
// Note that even with compression, the output array is likely to be bigger than the binary file..
|
||||||
|
// Load compressed TTF fonts with ImGui::GetIO().Fonts->AddFontFromMemoryCompressedTTF()
|
||||||
|
|
||||||
|
// Build with, e.g:
|
||||||
|
// # cl.exe binary_to_compressed_c.cpp
|
||||||
|
// # g++ binary_to_compressed_c.cpp
|
||||||
|
// # clang++ binary_to_compressed_c.cpp
|
||||||
|
// You can also find a precompiled Windows binary in the binary/demo package available from https://github.com/ocornut/imgui
|
||||||
|
|
||||||
|
// Usage:
|
||||||
|
// binary_to_compressed_c.exe [-base85] [-nocompress] [-nostatic] <inputfile> <symbolname>
|
||||||
|
// Usage example:
|
||||||
|
// # binary_to_compressed_c.exe myfont.ttf MyFont > myfont.cpp
|
||||||
|
// # binary_to_compressed_c.exe -base85 myfont.ttf MyFont > myfont.cpp
|
||||||
|
|
||||||
|
#define _CRT_SECURE_NO_WARNINGS
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <assert.h>
|
||||||
|
|
||||||
|
// stb_compress* from stb.h - declaration
|
||||||
|
typedef unsigned int stb_uint;
|
||||||
|
typedef unsigned char stb_uchar;
|
||||||
|
stb_uint stb_compress(stb_uchar* out, stb_uchar* in, stb_uint len);
|
||||||
|
|
||||||
|
static bool binary_to_compressed_c(const char* filename, const char* symbol, bool use_base85_encoding, bool use_compression, bool use_static);
|
||||||
|
|
||||||
|
int main(int argc, char** argv)
|
||||||
|
{
|
||||||
|
if (argc < 3)
|
||||||
|
{
|
||||||
|
printf("Syntax: %s [-base85] [-nocompress] [-nostatic] <inputfile> <symbolname>\n", argv[0]);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int argn = 1;
|
||||||
|
bool use_base85_encoding = false;
|
||||||
|
bool use_compression = true;
|
||||||
|
bool use_static = true;
|
||||||
|
while (argn < (argc - 2) && argv[argn][0] == '-')
|
||||||
|
{
|
||||||
|
if (strcmp(argv[argn], "-base85") == 0) { use_base85_encoding = true; argn++; }
|
||||||
|
else if (strcmp(argv[argn], "-nocompress") == 0) { use_compression = false; argn++; }
|
||||||
|
else if (strcmp(argv[argn], "-nostatic") == 0) { use_static = false; argn++; }
|
||||||
|
else
|
||||||
|
{
|
||||||
|
fprintf(stderr, "Unknown argument: '%s'\n", argv[argn]);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ret = binary_to_compressed_c(argv[argn], argv[argn + 1], use_base85_encoding, use_compression, use_static);
|
||||||
|
if (!ret)
|
||||||
|
fprintf(stderr, "Error opening or reading file: '%s'\n", argv[argn]);
|
||||||
|
return ret ? 0 : 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
char Encode85Byte(unsigned int x)
|
||||||
|
{
|
||||||
|
x = (x % 85) + 35;
|
||||||
|
return (char)((x >= '\\') ? x + 1 : x);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool binary_to_compressed_c(const char* filename, const char* symbol, bool use_base85_encoding, bool use_compression, bool use_static)
|
||||||
|
{
|
||||||
|
// Read file
|
||||||
|
FILE* f = fopen(filename, "rb");
|
||||||
|
if (!f) return false;
|
||||||
|
int data_sz;
|
||||||
|
if (fseek(f, 0, SEEK_END) || (data_sz = (int)ftell(f)) == -1 || fseek(f, 0, SEEK_SET)) { fclose(f); return false; }
|
||||||
|
char* data = new char[data_sz + 4];
|
||||||
|
if (fread(data, 1, data_sz, f) != (size_t)data_sz) { fclose(f); delete[] data; return false; }
|
||||||
|
memset((void*)(((char*)data) + data_sz), 0, 4);
|
||||||
|
fclose(f);
|
||||||
|
|
||||||
|
// Compress
|
||||||
|
int maxlen = data_sz + 512 + (data_sz >> 2) + sizeof(int); // total guess
|
||||||
|
char* compressed = use_compression ? new char[maxlen] : data;
|
||||||
|
int compressed_sz = use_compression ? stb_compress((stb_uchar*)compressed, (stb_uchar*)data, data_sz) : data_sz;
|
||||||
|
if (use_compression)
|
||||||
|
memset(compressed + compressed_sz, 0, maxlen - compressed_sz);
|
||||||
|
|
||||||
|
// Output as Base85 encoded
|
||||||
|
FILE* out = stdout;
|
||||||
|
fprintf(out, "// File: '%s' (%d bytes)\n", filename, (int)data_sz);
|
||||||
|
fprintf(out, "// Exported using binary_to_compressed_c.cpp\n");
|
||||||
|
const char* static_str = use_static ? "static " : "";
|
||||||
|
const char* compressed_str = use_compression ? "compressed_" : "";
|
||||||
|
if (use_base85_encoding)
|
||||||
|
{
|
||||||
|
fprintf(out, "%sconst char %s_%sdata_base85[%d+1] =\n \"", static_str, symbol, compressed_str, (int)((compressed_sz + 3) / 4)*5);
|
||||||
|
char prev_c = 0;
|
||||||
|
for (int src_i = 0; src_i < compressed_sz; src_i += 4)
|
||||||
|
{
|
||||||
|
// This is made a little more complicated by the fact that ??X sequences are interpreted as trigraphs by old C/C++ compilers. So we need to escape pairs of ??.
|
||||||
|
unsigned int d = *(unsigned int*)(compressed + src_i);
|
||||||
|
for (unsigned int n5 = 0; n5 < 5; n5++, d /= 85)
|
||||||
|
{
|
||||||
|
char c = Encode85Byte(d);
|
||||||
|
fprintf(out, (c == '?' && prev_c == '?') ? "\\%c" : "%c", c);
|
||||||
|
prev_c = c;
|
||||||
|
}
|
||||||
|
if ((src_i % 112) == 112 - 4)
|
||||||
|
fprintf(out, "\"\n \"");
|
||||||
|
}
|
||||||
|
fprintf(out, "\";\n\n");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
fprintf(out, "%sconst unsigned int %s_%ssize = %d;\n", static_str, symbol, compressed_str, (int)compressed_sz);
|
||||||
|
fprintf(out, "%sconst unsigned int %s_%sdata[%d/4] =\n{", static_str, symbol, compressed_str, (int)((compressed_sz + 3) / 4)*4);
|
||||||
|
int column = 0;
|
||||||
|
for (int i = 0; i < compressed_sz; i += 4)
|
||||||
|
{
|
||||||
|
unsigned int d = *(unsigned int*)(compressed + i);
|
||||||
|
if ((column++ % 12) == 0)
|
||||||
|
fprintf(out, "\n 0x%08x, ", d);
|
||||||
|
else
|
||||||
|
fprintf(out, "0x%08x, ", d);
|
||||||
|
}
|
||||||
|
fprintf(out, "\n};\n\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Cleanup
|
||||||
|
delete[] data;
|
||||||
|
if (use_compression)
|
||||||
|
delete[] compressed;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// stb_compress* from stb.h - definition
|
||||||
|
|
||||||
|
//////////////////// compressor ///////////////////////
|
||||||
|
|
||||||
|
static stb_uint stb_adler32(stb_uint adler32, stb_uchar *buffer, stb_uint buflen)
|
||||||
|
{
|
||||||
|
const unsigned long ADLER_MOD = 65521;
|
||||||
|
unsigned long s1 = adler32 & 0xffff, s2 = adler32 >> 16;
|
||||||
|
unsigned long blocklen, i;
|
||||||
|
|
||||||
|
blocklen = buflen % 5552;
|
||||||
|
while (buflen) {
|
||||||
|
for (i=0; i + 7 < blocklen; i += 8) {
|
||||||
|
s1 += buffer[0], s2 += s1;
|
||||||
|
s1 += buffer[1], s2 += s1;
|
||||||
|
s1 += buffer[2], s2 += s1;
|
||||||
|
s1 += buffer[3], s2 += s1;
|
||||||
|
s1 += buffer[4], s2 += s1;
|
||||||
|
s1 += buffer[5], s2 += s1;
|
||||||
|
s1 += buffer[6], s2 += s1;
|
||||||
|
s1 += buffer[7], s2 += s1;
|
||||||
|
|
||||||
|
buffer += 8;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (; i < blocklen; ++i)
|
||||||
|
s1 += *buffer++, s2 += s1;
|
||||||
|
|
||||||
|
s1 %= ADLER_MOD, s2 %= ADLER_MOD;
|
||||||
|
buflen -= blocklen;
|
||||||
|
blocklen = 5552;
|
||||||
|
}
|
||||||
|
return (s2 << 16) + s1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static unsigned int stb_matchlen(stb_uchar *m1, stb_uchar *m2, stb_uint maxlen)
|
||||||
|
{
|
||||||
|
stb_uint i;
|
||||||
|
for (i=0; i < maxlen; ++i)
|
||||||
|
if (m1[i] != m2[i]) return i;
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
|
||||||
|
// simple implementation that just takes the source data in a big block
|
||||||
|
|
||||||
|
static stb_uchar *stb__out;
|
||||||
|
static FILE *stb__outfile;
|
||||||
|
static stb_uint stb__outbytes;
|
||||||
|
|
||||||
|
static void stb__write(unsigned char v)
|
||||||
|
{
|
||||||
|
fputc(v, stb__outfile);
|
||||||
|
++stb__outbytes;
|
||||||
|
}
|
||||||
|
|
||||||
|
//#define stb_out(v) (stb__out ? *stb__out++ = (stb_uchar) (v) : stb__write((stb_uchar) (v)))
|
||||||
|
#define stb_out(v) do { if (stb__out) *stb__out++ = (stb_uchar) (v); else stb__write((stb_uchar) (v)); } while (0)
|
||||||
|
|
||||||
|
static void stb_out2(stb_uint v) { stb_out(v >> 8); stb_out(v); }
|
||||||
|
static void stb_out3(stb_uint v) { stb_out(v >> 16); stb_out(v >> 8); stb_out(v); }
|
||||||
|
static void stb_out4(stb_uint v) { stb_out(v >> 24); stb_out(v >> 16); stb_out(v >> 8 ); stb_out(v); }
|
||||||
|
|
||||||
|
static void outliterals(stb_uchar *in, int numlit)
|
||||||
|
{
|
||||||
|
while (numlit > 65536) {
|
||||||
|
outliterals(in,65536);
|
||||||
|
in += 65536;
|
||||||
|
numlit -= 65536;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (numlit == 0) ;
|
||||||
|
else if (numlit <= 32) stb_out (0x000020 + numlit-1);
|
||||||
|
else if (numlit <= 2048) stb_out2(0x000800 + numlit-1);
|
||||||
|
else /* numlit <= 65536) */ stb_out3(0x070000 + numlit-1);
|
||||||
|
|
||||||
|
if (stb__out) {
|
||||||
|
memcpy(stb__out,in,numlit);
|
||||||
|
stb__out += numlit;
|
||||||
|
} else
|
||||||
|
fwrite(in, 1, numlit, stb__outfile);
|
||||||
|
}
|
||||||
|
|
||||||
|
static int stb__window = 0x40000; // 256K
|
||||||
|
|
||||||
|
static int stb_not_crap(int best, int dist)
|
||||||
|
{
|
||||||
|
return ((best > 2 && dist <= 0x00100)
|
||||||
|
|| (best > 5 && dist <= 0x04000)
|
||||||
|
|| (best > 7 && dist <= 0x80000));
|
||||||
|
}
|
||||||
|
|
||||||
|
static stb_uint stb__hashsize = 32768;
|
||||||
|
|
||||||
|
// note that you can play with the hashing functions all you
|
||||||
|
// want without needing to change the decompressor
|
||||||
|
#define stb__hc(q,h,c) (((h) << 7) + ((h) >> 25) + q[c])
|
||||||
|
#define stb__hc2(q,h,c,d) (((h) << 14) + ((h) >> 18) + (q[c] << 7) + q[d])
|
||||||
|
#define stb__hc3(q,c,d,e) ((q[c] << 14) + (q[d] << 7) + q[e])
|
||||||
|
|
||||||
|
static unsigned int stb__running_adler;
|
||||||
|
|
||||||
|
static int stb_compress_chunk(stb_uchar *history,
|
||||||
|
stb_uchar *start,
|
||||||
|
stb_uchar *end,
|
||||||
|
int length,
|
||||||
|
int *pending_literals,
|
||||||
|
stb_uchar **chash,
|
||||||
|
stb_uint mask)
|
||||||
|
{
|
||||||
|
(void)history;
|
||||||
|
int window = stb__window;
|
||||||
|
stb_uint match_max;
|
||||||
|
stb_uchar *lit_start = start - *pending_literals;
|
||||||
|
stb_uchar *q = start;
|
||||||
|
|
||||||
|
#define STB__SCRAMBLE(h) (((h) + ((h) >> 16)) & mask)
|
||||||
|
|
||||||
|
// stop short of the end so we don't scan off the end doing
|
||||||
|
// the hashing; this means we won't compress the last few bytes
|
||||||
|
// unless they were part of something longer
|
||||||
|
while (q < start+length && q+12 < end) {
|
||||||
|
int m;
|
||||||
|
stb_uint h1,h2,h3,h4, h;
|
||||||
|
stb_uchar *t;
|
||||||
|
int best = 2, dist=0;
|
||||||
|
|
||||||
|
if (q+65536 > end)
|
||||||
|
match_max = (stb_uint)(end-q);
|
||||||
|
else
|
||||||
|
match_max = 65536;
|
||||||
|
|
||||||
|
#define stb__nc(b,d) ((d) <= window && ((b) > 9 || stb_not_crap((int)(b),(int)(d))))
|
||||||
|
|
||||||
|
#define STB__TRY(t,p) /* avoid retrying a match we already tried */ \
|
||||||
|
if (p ? dist != (int)(q-t) : 1) \
|
||||||
|
if ((m = stb_matchlen(t, q, match_max)) > best) \
|
||||||
|
if (stb__nc(m,q-(t))) \
|
||||||
|
best = m, dist = (int)(q - (t))
|
||||||
|
|
||||||
|
// rather than search for all matches, only try 4 candidate locations,
|
||||||
|
// chosen based on 4 different hash functions of different lengths.
|
||||||
|
// this strategy is inspired by LZO; hashing is unrolled here using the
|
||||||
|
// 'hc' macro
|
||||||
|
h = stb__hc3(q,0, 1, 2); h1 = STB__SCRAMBLE(h);
|
||||||
|
t = chash[h1]; if (t) STB__TRY(t,0);
|
||||||
|
h = stb__hc2(q,h, 3, 4); h2 = STB__SCRAMBLE(h);
|
||||||
|
h = stb__hc2(q,h, 5, 6); t = chash[h2]; if (t) STB__TRY(t,1);
|
||||||
|
h = stb__hc2(q,h, 7, 8); h3 = STB__SCRAMBLE(h);
|
||||||
|
h = stb__hc2(q,h, 9,10); t = chash[h3]; if (t) STB__TRY(t,1);
|
||||||
|
h = stb__hc2(q,h,11,12); h4 = STB__SCRAMBLE(h);
|
||||||
|
t = chash[h4]; if (t) STB__TRY(t,1);
|
||||||
|
|
||||||
|
// because we use a shared hash table, can only update it
|
||||||
|
// _after_ we've probed all of them
|
||||||
|
chash[h1] = chash[h2] = chash[h3] = chash[h4] = q;
|
||||||
|
|
||||||
|
if (best > 2)
|
||||||
|
assert(dist > 0);
|
||||||
|
|
||||||
|
// see if our best match qualifies
|
||||||
|
if (best < 3) { // fast path literals
|
||||||
|
++q;
|
||||||
|
} else if (best > 2 && best <= 0x80 && dist <= 0x100) {
|
||||||
|
outliterals(lit_start, (int)(q-lit_start)); lit_start = (q += best);
|
||||||
|
stb_out(0x80 + best-1);
|
||||||
|
stb_out(dist-1);
|
||||||
|
} else if (best > 5 && best <= 0x100 && dist <= 0x4000) {
|
||||||
|
outliterals(lit_start, (int)(q-lit_start)); lit_start = (q += best);
|
||||||
|
stb_out2(0x4000 + dist-1);
|
||||||
|
stb_out(best-1);
|
||||||
|
} else if (best > 7 && best <= 0x100 && dist <= 0x80000) {
|
||||||
|
outliterals(lit_start, (int)(q-lit_start)); lit_start = (q += best);
|
||||||
|
stb_out3(0x180000 + dist-1);
|
||||||
|
stb_out(best-1);
|
||||||
|
} else if (best > 8 && best <= 0x10000 && dist <= 0x80000) {
|
||||||
|
outliterals(lit_start, (int)(q-lit_start)); lit_start = (q += best);
|
||||||
|
stb_out3(0x100000 + dist-1);
|
||||||
|
stb_out2(best-1);
|
||||||
|
} else if (best > 9 && dist <= 0x1000000) {
|
||||||
|
if (best > 65536) best = 65536;
|
||||||
|
outliterals(lit_start, (int)(q-lit_start)); lit_start = (q += best);
|
||||||
|
if (best <= 0x100) {
|
||||||
|
stb_out(0x06);
|
||||||
|
stb_out3(dist-1);
|
||||||
|
stb_out(best-1);
|
||||||
|
} else {
|
||||||
|
stb_out(0x04);
|
||||||
|
stb_out3(dist-1);
|
||||||
|
stb_out2(best-1);
|
||||||
|
}
|
||||||
|
} else { // fallback literals if no match was a balanced tradeoff
|
||||||
|
++q;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// if we didn't get all the way, add the rest to literals
|
||||||
|
if (q-start < length)
|
||||||
|
q = start+length;
|
||||||
|
|
||||||
|
// the literals are everything from lit_start to q
|
||||||
|
*pending_literals = (int)(q - lit_start);
|
||||||
|
|
||||||
|
stb__running_adler = stb_adler32(stb__running_adler, start, (stb_uint)(q - start));
|
||||||
|
return (int)(q - start);
|
||||||
|
}
|
||||||
|
|
||||||
|
static int stb_compress_inner(stb_uchar *input, stb_uint length)
|
||||||
|
{
|
||||||
|
int literals = 0;
|
||||||
|
stb_uint len,i;
|
||||||
|
|
||||||
|
stb_uchar **chash;
|
||||||
|
chash = (stb_uchar**) malloc(stb__hashsize * sizeof(stb_uchar*));
|
||||||
|
if (chash == NULL) return 0; // failure
|
||||||
|
for (i=0; i < stb__hashsize; ++i)
|
||||||
|
chash[i] = NULL;
|
||||||
|
|
||||||
|
// stream signature
|
||||||
|
stb_out(0x57); stb_out(0xbc);
|
||||||
|
stb_out2(0);
|
||||||
|
|
||||||
|
stb_out4(0); // 64-bit length requires 32-bit leading 0
|
||||||
|
stb_out4(length);
|
||||||
|
stb_out4(stb__window);
|
||||||
|
|
||||||
|
stb__running_adler = 1;
|
||||||
|
|
||||||
|
len = stb_compress_chunk(input, input, input+length, length, &literals, chash, stb__hashsize-1);
|
||||||
|
assert(len == length);
|
||||||
|
|
||||||
|
outliterals(input+length - literals, literals);
|
||||||
|
|
||||||
|
free(chash);
|
||||||
|
|
||||||
|
stb_out2(0x05fa); // end opcode
|
||||||
|
|
||||||
|
stb_out4(stb__running_adler);
|
||||||
|
|
||||||
|
return 1; // success
|
||||||
|
}
|
||||||
|
|
||||||
|
stb_uint stb_compress(stb_uchar *out, stb_uchar *input, stb_uint length)
|
||||||
|
{
|
||||||
|
stb__out = out;
|
||||||
|
stb__outfile = NULL;
|
||||||
|
|
||||||
|
stb_compress_inner(input, length);
|
||||||
|
|
||||||
|
return (stb_uint)(stb__out - out);
|
||||||
|
}
|
||||||
2
kerep
2
kerep
@ -1 +1 @@
|
|||||||
Subproject commit 20537813ecbd08041318e832d645a982c09ac7a1
|
Subproject commit 9938893ac15165e206b2a3af48701c25d1124765
|
||||||
13
src/gui/fonts.cpp
Normal file
13
src/gui/fonts.cpp
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
#include "gui_internal.hpp"
|
||||||
|
|
||||||
|
ImFont* ImFont_LoadFromFile(const char* file_path, f32 font_size){
|
||||||
|
ImGuiIO& io = ImGui::GetIO();
|
||||||
|
font_size *= getMainWindowDPI();
|
||||||
|
return io.Fonts->AddFontFromFileTTF(file_path, font_size);
|
||||||
|
}
|
||||||
|
|
||||||
|
ImFont* _ImFont_LoadEmbedded(const void* data, int data_size, f32 font_size){
|
||||||
|
ImGuiIO& io = ImGui::GetIO();
|
||||||
|
font_size *= getMainWindowDPI();
|
||||||
|
return io.Fonts->AddFontFromMemoryCompressedTTF(data, data_size, font_size);
|
||||||
|
}
|
||||||
@ -6,11 +6,51 @@
|
|||||||
#include "../../imgui/imgui.h"
|
#include "../../imgui/imgui.h"
|
||||||
#include "../../imgui/backends/imgui_impl_sdl2.h"
|
#include "../../imgui/backends/imgui_impl_sdl2.h"
|
||||||
#include "../../imgui/backends/imgui_impl_opengl3.h"
|
#include "../../imgui/backends/imgui_impl_opengl3.h"
|
||||||
|
#include "../../kerep/src/Filesystem/filesystem.h"
|
||||||
|
|
||||||
|
//////////////////////////////////////
|
||||||
|
// Fonts //
|
||||||
|
//////////////////////////////////////
|
||||||
|
#include "../generated/fonts_embedded.h"
|
||||||
|
|
||||||
|
#define __CAT3(A,B,C) A##B##C
|
||||||
|
#define embedded_font_data(FONT_NAME) __CAT3(font_,FONT_NAME,_compressed_data)
|
||||||
|
#define embedded_font_size(FONT_NAME) __CAT3(font_,FONT_NAME,_compressed_size)
|
||||||
|
|
||||||
|
f32 getMainWindowDPI();
|
||||||
|
ImFont* ImFont_LoadFromFile(const char* file_path, f32 font_size);
|
||||||
|
ImFont* _ImFont_LoadEmbedded(const void* data, int data_size, f32 font_size);
|
||||||
|
|
||||||
|
#define ImFont_LoadEmbedded(FONT_NAME, FONT_SIZE) _ImFont_LoadEmbedded( \
|
||||||
|
embedded_font_data(FONT_NAME), \
|
||||||
|
embedded_font_size(FONT_NAME), \
|
||||||
|
FONT_SIZE)
|
||||||
|
|
||||||
|
//////////////////////////////////////
|
||||||
|
// Variables and constants //
|
||||||
|
//////////////////////////////////////
|
||||||
const u8 frame_rate_max=60; // frames per second
|
const u8 frame_rate_max=60; // frames per second
|
||||||
extern ImVec4 clear_color; // background color for main window
|
extern ImVec4 clear_color; // background color for main window
|
||||||
|
|
||||||
|
//////////////////////////////////////
|
||||||
|
// UI Functions //
|
||||||
|
//////////////////////////////////////
|
||||||
/// @brief frees all allocated resources
|
/// @brief frees all allocated resources
|
||||||
void main_window_destroy();
|
void main_window_destroy();
|
||||||
void draw_demo_windows(ImGuiIO&);
|
void draw_demo_windows(ImGuiIO&);
|
||||||
void draw_bg_window();
|
void draw_bg_window();
|
||||||
|
|
||||||
|
void node_editor_open(const char* title);
|
||||||
|
void node_editor_draw();
|
||||||
|
void node_editor_close();
|
||||||
|
|
||||||
|
//////////////////////////////////////
|
||||||
|
// Macros //
|
||||||
|
//////////////////////////////////////
|
||||||
|
#define SDL_ERROR_SAFETHROW() { \
|
||||||
|
const char* sdl_error=SDL_GetError(); \
|
||||||
|
safethrow_msg(cptr_concat("SDL Error: ", sdl_error),;); \
|
||||||
|
SDL_ClearError(); \
|
||||||
|
}
|
||||||
|
#define SDL_TRY_ZERO(FUNC_CALL) if(FUNC_CALL != 0) SDL_ERROR_SAFETHROW();
|
||||||
|
#define SDL_TRY_ONE(FUNC_CALL) if(FUNC_CALL != 1) SDL_ERROR_SAFETHROW();
|
||||||
|
|||||||
@ -1,17 +1,22 @@
|
|||||||
#include "gui_internal.hpp"
|
#include "gui_internal.hpp"
|
||||||
|
|
||||||
|
#define default_font_name DroidSans
|
||||||
|
const f32 default_font_size=14.0f;
|
||||||
ImVec4 clear_color = RGBAHexToF(35,35,50,255);
|
ImVec4 clear_color = RGBAHexToF(35,35,50,255);
|
||||||
bool loop_running=false;
|
bool loop_running=false;
|
||||||
SDL_Window* sdl_window;
|
SDL_Window* sdl_window;
|
||||||
SDL_GLContext gl_context;
|
SDL_GLContext gl_context;
|
||||||
|
|
||||||
#define SDL_ERROR_SAFETHROW() { \
|
f32 getMainWindowDPI(){
|
||||||
const char* sdl_error=SDL_GetError(); \
|
int w=0, h=0;
|
||||||
safethrow_msg(cptr_concat("SDL Error: ", sdl_error),;); \
|
SDL_GL_GetDrawableSize(sdl_window, &w, &h);
|
||||||
SDL_ClearError(); \
|
int sim_w=0, sim_h=0;
|
||||||
|
SDL_GetWindowSize(sdl_window, &sim_w, &sim_h);
|
||||||
|
f32 wdpi=w/sim_w;
|
||||||
|
f32 hdpi=h/sim_h;
|
||||||
|
f32 dpi=SDL_sqrtf(wdpi*wdpi + hdpi*hdpi);
|
||||||
|
return dpi;
|
||||||
}
|
}
|
||||||
#define SDL_TRY_ZERO(FUNC_CALL) if(FUNC_CALL != 0) SDL_ERROR_SAFETHROW();
|
|
||||||
#define SDL_TRY_ONE(FUNC_CALL) if(FUNC_CALL != 1) SDL_ERROR_SAFETHROW();
|
|
||||||
|
|
||||||
Maybe main_window_open(const char* window_title){
|
Maybe main_window_open(const char* window_title){
|
||||||
SDL_TRY_ZERO(SDL_Init(SDL_INIT_VIDEO));
|
SDL_TRY_ZERO(SDL_Init(SDL_INIT_VIDEO));
|
||||||
@ -31,7 +36,10 @@ Maybe main_window_open(const char* window_title){
|
|||||||
SDL_TRY_ZERO( SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1));
|
SDL_TRY_ZERO( SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1));
|
||||||
SDL_TRY_ZERO( SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24));
|
SDL_TRY_ZERO( SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24));
|
||||||
SDL_TRY_ZERO( SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8));
|
SDL_TRY_ZERO( SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8));
|
||||||
SDL_WindowFlags window_flags = (SDL_WindowFlags)(SDL_WINDOW_OPENGL|SDL_WINDOW_RESIZABLE|SDL_WINDOW_ALLOW_HIGHDPI);
|
SDL_WindowFlags window_flags = (SDL_WindowFlags)(
|
||||||
|
SDL_WINDOW_OPENGL |
|
||||||
|
SDL_WINDOW_RESIZABLE |
|
||||||
|
SDL_WINDOW_ALLOW_HIGHDPI);
|
||||||
sdl_window = SDL_CreateWindow(window_title,
|
sdl_window = SDL_CreateWindow(window_title,
|
||||||
SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
|
SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
|
||||||
1280, 720, window_flags);
|
1280, 720, window_flags);
|
||||||
@ -52,23 +60,17 @@ Maybe main_window_open(const char* window_title){
|
|||||||
// io.ConfigViewportsNoTaskBarIcon = true;
|
// io.ConfigViewportsNoTaskBarIcon = true;
|
||||||
io.ConfigWindowsMoveFromTitleBarOnly = true;
|
io.ConfigWindowsMoveFromTitleBarOnly = true;
|
||||||
|
|
||||||
// Setup Dear ImGui style
|
|
||||||
ImGui::StyleColorsDark();
|
|
||||||
|
|
||||||
// When viewports are enabled we tweak WindowRounding/WindowBg so platform windows can look identical to regular ones.
|
|
||||||
ImGuiStyle& style = ImGui::GetStyle();
|
|
||||||
if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
|
|
||||||
{
|
|
||||||
style.WindowRounding = 0.0f;
|
|
||||||
style.Colors[ImGuiCol_WindowBg].w = 1.0f;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Setup Platform/Renderer backends
|
// Setup Platform/Renderer backends
|
||||||
if(ImGui_ImplSDL2_InitForOpenGL(sdl_window, gl_context) != true)
|
if(ImGui_ImplSDL2_InitForOpenGL(sdl_window, gl_context) != true)
|
||||||
SDL_ERROR_SAFETHROW();
|
SDL_ERROR_SAFETHROW();
|
||||||
if(ImGui_ImplOpenGL3_Init(glsl_version) != true)
|
if(ImGui_ImplOpenGL3_Init(glsl_version) != true)
|
||||||
SDL_ERROR_SAFETHROW();
|
SDL_ERROR_SAFETHROW();
|
||||||
|
|
||||||
|
// Setup Dear ImGui style
|
||||||
|
ImGui::StyleColorsDark();
|
||||||
|
io.FontDefault=ImFont_LoadEmbedded(default_font_name, default_font_size);
|
||||||
|
|
||||||
|
node_editor_open("node editor");
|
||||||
return MaybeNull;
|
return MaybeNull;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -115,6 +117,7 @@ Maybe draw_frame(){
|
|||||||
// Draw UI
|
// Draw UI
|
||||||
draw_bg_window();
|
draw_bg_window();
|
||||||
draw_demo_windows(io);
|
draw_demo_windows(io);
|
||||||
|
node_editor_draw();
|
||||||
|
|
||||||
// Rendering
|
// Rendering
|
||||||
ImGui::Render();
|
ImGui::Render();
|
||||||
@ -177,6 +180,7 @@ Maybe main_window_close(){
|
|||||||
}
|
}
|
||||||
|
|
||||||
void main_window_destroy(){
|
void main_window_destroy(){
|
||||||
|
node_editor_close();
|
||||||
ImGui_ImplOpenGL3_Shutdown();
|
ImGui_ImplOpenGL3_Shutdown();
|
||||||
ImGui_ImplSDL2_Shutdown();
|
ImGui_ImplSDL2_Shutdown();
|
||||||
ImGui::DestroyContext();
|
ImGui::DestroyContext();
|
||||||
|
|||||||
43
src/gui/node_editor.cpp
Normal file
43
src/gui/node_editor.cpp
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
#include "gui_internal.hpp"
|
||||||
|
#include "../../imgui-node-editor/imgui_node_editor.h"
|
||||||
|
namespace NE = ax::NodeEditor;
|
||||||
|
|
||||||
|
NE::EditorContext* editor_context=nullptr;
|
||||||
|
const char* editor_title;
|
||||||
|
|
||||||
|
void node_editor_open(const char* title){
|
||||||
|
NE::Config config;
|
||||||
|
config.SettingsFile = "node_editor.json";
|
||||||
|
editor_context=NE::CreateEditor(&config);
|
||||||
|
editor_title=title;
|
||||||
|
}
|
||||||
|
|
||||||
|
void node_editor_close(){
|
||||||
|
NE::DestroyEditor(editor_context);
|
||||||
|
editor_context=nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
void node_editor_draw(){
|
||||||
|
if(editor_context==nullptr)
|
||||||
|
return;
|
||||||
|
|
||||||
|
ImGui::Begin(editor_title);
|
||||||
|
NE::SetCurrentEditor(editor_context);
|
||||||
|
NE::Begin(editor_title, ImVec2(0.0, 0.0f));
|
||||||
|
int uniqueId = 1;
|
||||||
|
// Start drawing nodes.
|
||||||
|
NE::BeginNode(uniqueId++);
|
||||||
|
ImGui::Text("Node A");
|
||||||
|
NE::BeginPin(uniqueId++, NE::PinKind::Input);
|
||||||
|
ImGui::Text("-> In");
|
||||||
|
NE::EndPin();
|
||||||
|
ImGui::SameLine();
|
||||||
|
NE::BeginPin(uniqueId++, NE::PinKind::Output);
|
||||||
|
ImGui::Text("Out ->");
|
||||||
|
NE::EndPin();
|
||||||
|
NE::EndNode();
|
||||||
|
NE::End();
|
||||||
|
NE::SetCurrentEditor(nullptr);
|
||||||
|
ImGui::End();
|
||||||
|
}
|
||||||
|
|
||||||
@ -1,6 +1,5 @@
|
|||||||
#!/usr/bin/bash
|
#!/usr/bin/bash
|
||||||
|
try_delete_dir_or_file .rebuild_kerep.tmp
|
||||||
myprint "${WHITE}deleting .kerep_rebuild.tmp"
|
try_delete_dir_or_file .rebuild_imgui.tmp
|
||||||
rm -rf .kerep_rebuild.tmp
|
try_delete_dir_or_file .rebuild_imgui_node_editor.tmp
|
||||||
myprint "${WHITE}deleting .imgui_rebuild.tmp"
|
try_delete_dir_or_file fonts/generated
|
||||||
rm -rf .imgui_rebuild.tmp
|
|
||||||
|
|||||||
38
tasks/embed_fonts.sh
Normal file
38
tasks/embed_fonts.sh
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
cd fonts
|
||||||
|
SRC_C=""
|
||||||
|
HEADER="generated/fonts_embedded.h"
|
||||||
|
|
||||||
|
function append_def_to_header {
|
||||||
|
local c_src_file=$1
|
||||||
|
local header_file=$2
|
||||||
|
sed '3!d;' $c_src_file | sed 's/const/static const/' >> $header_file
|
||||||
|
sed '4!d;' $c_src_file | sed 's/const/extern const/' | sed 's/\[.*/[];/' >> $header_file
|
||||||
|
}
|
||||||
|
|
||||||
|
rm -rf generated
|
||||||
|
mkdir generated
|
||||||
|
mkdir generated/src
|
||||||
|
$CMP_CPP binary_to_compressed_c.cpp -o generated/binary_to_compressed_c.exe
|
||||||
|
for ttf_file in $(ls *.ttf); do
|
||||||
|
c_var_name="font_$(basename $ttf_file .ttf | tr ' ' '_' | tr '-' '_')"
|
||||||
|
c_src_file="generated/src/$c_var_name.c"
|
||||||
|
echo "$ttf_file : $c_var_name > c_src_file"
|
||||||
|
generated/binary_to_compressed_c.exe -nostatic "$ttf_file" "$c_var_name" > "$c_src_file"
|
||||||
|
SRC_C="$SRC_C fonts/$c_src_file"
|
||||||
|
append_def_to_header "$c_src_file" "$HEADER"
|
||||||
|
done
|
||||||
|
cd ..
|
||||||
|
mkdir -p src/generated/
|
||||||
|
cp fonts/$HEADER /src/generated/
|
||||||
|
myprint "${GREEN}font arrays external definitions have been written to ${CYAN}/src/generated$HEADER"
|
||||||
|
|
||||||
|
clean_dir "$OBJDIR/objects"
|
||||||
|
clean_dir "$OBJDIR/libs"
|
||||||
|
compile_c "$C_ARGS" "$SRC_C"
|
||||||
|
pack_static_lib "$STATIC_LIB_FILE"
|
||||||
|
rm -rf $OUTDIR/fonts_embedded.a
|
||||||
|
|
||||||
|
cp "$OBJDIR/out/fonts_embedded.a" libs/
|
||||||
|
myprint "$OBJDIR/out/fonts_embedded.a -> libs/"
|
||||||
@ -22,6 +22,7 @@ function handle_static_dependency {
|
|||||||
|
|
||||||
handle_static_dependency kerep $KEREP_BUILD_TASK
|
handle_static_dependency kerep $KEREP_BUILD_TASK
|
||||||
handle_static_dependency imgui $KEREP_BUILD_TASK
|
handle_static_dependency imgui $KEREP_BUILD_TASK
|
||||||
|
handle_static_dependency imgui-node-editor $KEREP_BUILD_TASK
|
||||||
|
|
||||||
# copy all precompiled libs
|
# copy all precompiled libs
|
||||||
cp libs/* $OBJDIR/libs/
|
cp libs/* $OBJDIR/libs/
|
||||||
|
|||||||
3
tasks/rebuild_imgui_node_editor.sh
Normal file
3
tasks/rebuild_imgui_node_editor.sh
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
touch .rebuild_imgui_node_editor.tmp
|
||||||
|
myprint "${YELLOW}imgui_node_editor.a will be rebuilt in the next build task"
|
||||||
Loading…
Reference in New Issue
Block a user