diff options
-rw-r--r-- | .gitmodules | 3 | ||||
-rw-r--r-- | Makefile | 11 | ||||
-rw-r--r-- | README.md | 8 | ||||
m--------- | deps/libf8 | 0 | ||||
-rw-r--r-- | include/unicode.h | 12 | ||||
-rw-r--r-- | src/engine.c | 2 | ||||
-rw-r--r-- | src/target/test.c | 89 | ||||
-rw-r--r-- | src/text.c | 2 | ||||
-rw-r--r-- | src/unicode.c | 54 |
9 files changed, 30 insertions, 151 deletions
diff --git a/.gitmodules b/.gitmodules index d493dd7..a984125 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,6 @@ [submodule "deps/munit"] path = deps/munit url = https://github.com/nemequ/munit.git +[submodule "deps/libf8"] + path = deps/libf8 + url = git@github.com:Oxore/libf8.git @@ -4,6 +4,7 @@ QQ=@ TARGET_TETRIS:=tetris TARGET_TEST:=test MUNIT:=deps/munit +LIBF8:=deps/libf8 ifeq ($(wildcard $(MUNIT)/*),) NOTEST=1 @@ -21,6 +22,7 @@ INCLUDE+=$(PREFIX)/include endif INCLUDE+=include INCLUDE+=$(MUNIT) +INCLUDE+=$(LIBF8) INCLUDE:=$(INCLUDE:%=-I%) #COMMON+=-fsanitize=address @@ -46,6 +48,8 @@ endif LDFLAGS+=$(COMMON) LDFLAGS_TETRIS+=$(LDFLAGS) +LDFLAGS_TETRIS+=-L$(LIBF8) +LDFLAGS_TETRIS+=-l:libf8.a LDFLAGS_TETRIS+=-lcsfml-graphics LDFLAGS_TETRIS+=-lcsfml-window LDFLAGS_TETRIS+=-lcsfml-system @@ -62,11 +66,11 @@ ifndef NOTEST all: $(TARGET_TEST) endif -$(TARGET_TETRIS): $(OBJECTS) $(TARGET)/$(TARGET_TETRIS).c.o +$(TARGET_TETRIS): $(OBJECTS) $(TARGET)/$(TARGET_TETRIS).c.o $(LIBF8)/libf8.a $(QQ) echo " LD $@" $(Q) $(CC) -o $@ $^ $(LDFLAGS_TETRIS) -$(TARGET_TEST): $(TARGET)/$(TARGET_TEST).c.o $(MUNIT)/munit.c.o $(BUILD)/unicode.c.o +$(TARGET_TEST): $(TARGET)/$(TARGET_TEST).c.o $(MUNIT)/munit.c.o $(QQ) echo " LD $@" $(Q) $(CC) -o $@ $^ $(LDFLAGS_TEST) @@ -79,6 +83,9 @@ $(BUILD): $(TARGET): $(Q) mkdir -p $@ +$(LIBF8)/libf8.a: $(LIBF8) + make -C $< + $(MUNIT)/munit.c.o: $(MUNIT)/munit.c $(QQ) echo " CC $@" $(Q) $(CC) -c $(CFLAGS) -o $@ $< @@ -10,9 +10,11 @@ This is unfinished tetris project written in pure C with CSFML library. It is un __Dependencies__ -`SFML` -`CSFML` -`libyaml` +- `SFML` - Graphics and controls +- `CSFML` - C SFML bindings +- `libyaml` - YAML parser +- [`munit`](https://github.com/nemequ/munit) - Testing framework (submodule) +- [`libf8`](https://github.com/Oxore/libf8) - Unicode helper functions (submodule) __Compilation and use__ diff --git a/deps/libf8 b/deps/libf8 new file mode 160000 +Subproject 599a11a1627c8369b43b462a505030936575ede diff --git a/include/unicode.h b/include/unicode.h deleted file mode 100644 index 4e5910a..0000000 --- a/include/unicode.h +++ /dev/null @@ -1,12 +0,0 @@ -/* - * unicode.h - * - * Unicode - module with some helper unicode functions. - * - * SFML uses UTF32 encoding for texts and there is no need to use any full - * featured library just for a couple of encoding conversion functions. - * - * */ - -size_t utf8_strlen(char *string); -void utf8to32_strcpy(wchar_t *dest, char *src); diff --git a/src/engine.c b/src/engine.c index 2a5755b..bad04f5 100644 --- a/src/engine.c +++ b/src/engine.c @@ -25,6 +25,7 @@ * * */ +#include <f8.h> #include <stdio.h> #include <stdlib.h> #include <string.h> @@ -38,7 +39,6 @@ #include "vector.h" #include "text.h" #include "field.h" -#include "unicode.h" #include "painter.h" #include "engine.h" diff --git a/src/target/test.c b/src/target/test.c index b807b92..a92642a 100644 --- a/src/target/test.c +++ b/src/target/test.c @@ -1,79 +1,20 @@ #include "munit.h" -#include "unicode.h" - -struct u_pair { - char *utf8; - wchar_t *utf32; -}; - -struct u_pair text_fixture[] = { - { - .utf8 = "Single byte ascii symbols string", - .utf32 = L"Single byte ascii symbols string" - }, - { - .utf8 = "\u041A\u0438\u0440\u0438\u043B\u043B\u0438\u0446\u0430", - .utf32 = L"\u041A\u0438\u0440\u0438\u043B\u043B\u0438\u0446\u0430" - }, - { - .utf8 = "\u0814\u0820\u080C\u081D\u0813\u0829\u0809\u080C", - .utf32 = L"\u0814\u0820\u080C\u081D\u0813\u0829\u0809\u080C" - }, - { - .utf8 = "𓁹", - .utf32 = L"𓁹" - } -}; - - -static void *test_utf8_strlen_setup(const MunitParameter params[], - void *user_data) -{ - (void) params; - (void) user_data; - - return text_fixture; -} - -static MunitResult test_utf8_strlen(const MunitParameter params[], - void *fixture) +static void *test_dummy_setup( + const MunitParameter params[], + void *user_data) { (void) params; - struct u_pair *f = fixture; - for (int i = 0; i < 4; i++) - munit_assert_ulong(utf8_strlen(f[i].utf8) * (i + 1), - ==, strlen(f[i].utf8)); - - return MUNIT_OK; -} - -static void *test_utf8to32_strcpy_setup(const MunitParameter params[], - void *user_data) -{ - (void) params; - (void) user_data; - - return text_fixture; + return user_data; } -static MunitResult test_utf8to32_strcpy(const MunitParameter params[], - void *fixture) +static MunitResult test_dummy( + const MunitParameter params[], + void *fixture) { (void) params; - - struct u_pair *f = fixture; - size_t len; - wchar_t *str; - - for (int i = 0; i < 4; i++) { - len = utf8_strlen(f[i].utf8); - str = malloc((len + 1) * sizeof(wchar_t)); - utf8to32_strcpy(str, f[i].utf8); - munit_assert_uint(memcmp(str, f[i].utf32, len), ==, 0); - free(str); - } + (void) fixture; return MUNIT_OK; } @@ -82,17 +23,9 @@ static const MunitSuite test_suite = { "unit", (MunitTest[]){ { - "/unicode/utf8_strlen", - test_utf8_strlen, - test_utf8_strlen_setup, - NULL, - MUNIT_TEST_OPTION_NONE, - NULL - }, - { - "/unicode/utf8to32_strcpy", - test_utf8to32_strcpy, - test_utf8to32_strcpy_setup, + "/dummy/dummy", + test_dummy, + test_dummy_setup, NULL, MUNIT_TEST_OPTION_NONE, NULL @@ -1,3 +1,4 @@ +#include <f8.h> #include <stdio.h> #include <stdlib.h> #include <string.h> @@ -6,7 +7,6 @@ #include "idlist.h" #include "vector.h" #include "text.h" -#include "unicode.h" static FILE *openFile(char *filename) { diff --git a/src/unicode.c b/src/unicode.c deleted file mode 100644 index de25ffb..0000000 --- a/src/unicode.c +++ /dev/null @@ -1,54 +0,0 @@ -#include <stdlib.h> -#include <string.h> - -#include "unicode.h" - -static inline size_t utf8_char_len(unsigned char c) -{ - if (c > 0x00 && c < 0xC0) - return 1; - else if (c >= 0xC2 && c < 0xE0) - return 2; - else if (c >= 0xE0 && c < 0xF0) - return 3; - else if (c >= 0xF0 && c < 0xF5) - return 4; - else - return 0; -} - -size_t utf8_strlen(char *string) -{ - size_t len = 0, keep = 0; - for (char *c = string; *c; (keep ? --keep : ++len), ++c) - if (!keep) - keep = (keep = utf8_char_len(*c)) ? keep - 1 : keep; - return len; -} - -void utf8to32_strcpy(wchar_t *dest, char *src) -{ - wchar_t *dc = dest; - char *c = src; - size_t len = 0; - while (*c) { - int clen = utf8_char_len(*c); - if (clen == 1) { - dc[len] = c[0] & 0x7f; - } else if (clen == 2) { - dc[len] = ((c[0] & 0x1f) << 6) | ((c[1] & 0x3f) << 0); - } else if (clen == 3) { - dc[len] = ((c[0] & 0x0f) << 12) | ((c[1] & 0x3f) << 6) - | ((c[2] & 0x3f) << 0); - } else if (clen == 4) { - dc[len] = ((c[0] & 0x07) << 18) | ((c[1] & 0x3f) << 12) - | ((c[2] & 0x3f) << 6) | ((c[3] & 0x3f) << 0); - } else { - dc[len] = 0; - return; - } - c += clen; - ++len; - } - dc[len] = 0; -} |