summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOxore <oxore@protonmail.com>2019-09-22 22:29:33 +0300
committerOxore <oxore@protonmail.com>2019-09-22 22:29:56 +0300
commit66fbdd735e19ee473aa3287133bd72e61fe56452 (patch)
treeaa263918907ea2fb81e9bbba9cfb534afe2a031c
parent8c04c4bf9004afda45a68b8e1d4522d36478cd62 (diff)
Add playground target, refactor Makefile's clean-ish rules
-rw-r--r--Makefile24
-rw-r--r--pg.mk27
-rw-r--r--src/target/pg.c112
3 files changed, 157 insertions, 6 deletions
diff --git a/Makefile b/Makefile
index 59cc835..08e39ab 100644
--- a/Makefile
+++ b/Makefile
@@ -65,6 +65,10 @@ ifndef NOTEST
all: $(TARGET_TEST)
endif
+.PHONY: pg
+pg:
+ make -f pg.mk $@ PREFIX=$(PREFIX)
+
$(TARGET_TETRIS): $(OBJECTS) $(TARGET)/$(TARGET_TETRIS).c.o $(LIBF8)/libf8.a \
$(CJSON)/cJSON.o
$(QQ) echo " LD $@"
@@ -81,7 +85,7 @@ $(OBJECTS): | $(BUILD)/ $(TARGET)/
$(Q) mkdir -p $@
$(LIBF8)/libf8.a: $(LIBF8)
- make -C $<
+ make -C $< NOTEST=1
$(CJSON)/cJSON.c.o: $(CJSON)/cJSON.c
$(QQ) echo " CC $@"
@@ -98,8 +102,16 @@ $(BUILD)/%.c.o: $(SRC)/%.c
-include $(DEPENDS)
clean:
- $(Q) $(RM) -rfv $(TARGET_TETRIS) $(TARGET_TEST) $(BUILD)
- $(Q) $(RM) -rfv $(MUNIT)/*.d $(MUNIT)/*.o
- $(Q) $(RM) -rfv $(CJSON)/*.d $(CJSON)/*.o
-
-.PHONY: all clean
+ $(Q) $(RM) -rfv $(OBJECTS) $(DEPENDS) \
+ $(TARGET_TETRIS) $(TARGET_TEST) \
+ $(TARGET)/$(TARGET_TETRIS).c.o $(TARGET)/$(TARGET_TETRIS).c.d \
+ $(TARGET)/$(TARGET_TEST).c.o $(TARGET)/$(TARGET_TEST).c.d
+ make -f pg.mk $@
+
+mrproper: clean
+ $(Q) $(RM) -rfv $(BUILD) \
+ $(MUNIT)/*.d $(MUNIT)/*.o \
+ $(CJSON)/*.d $(CJSON)/*.o
+ make -C $(LIBF8) clean
+
+.PHONY: all clean mrproper
diff --git a/pg.mk b/pg.mk
new file mode 100644
index 0000000..41686e1
--- /dev/null
+++ b/pg.mk
@@ -0,0 +1,27 @@
+# playground target for trying things with SFML
+
+LIBF8=deps/libf8
+
+ifneq ($(PREFIX),)
+LDFLAGS+=-L$(PREFIX)/lib
+LDFLAGS+=-Wl,-rpath=$(PREFIX)/lib
+INCLUDE+=-I$(PREFIX)/include
+endif
+
+INCLUDE+=-I$(LIBF8)
+
+LDFLAGS+=-lcsfml-graphics
+LDFLAGS+=-lcsfml-window
+LDFLAGS+=-lcsfml-system
+LDFLAGS+=-lyaml
+
+CFLAGS+=-Wno-deprecated-declarations
+
+pg: src/target/pg.c $(LIBF8)/libf8.a
+ $(CC) $(LDFLAGS) $(CFLAGS) $(INCLUDE) $^ -o $@
+
+$(LIBF8)/libf8.a: $(LIBF8)
+ make -C $< NOTEST=1
+
+clean:
+ rm -rfv pg
diff --git a/src/target/pg.c b/src/target/pg.c
new file mode 100644
index 0000000..071f2d0
--- /dev/null
+++ b/src/target/pg.c
@@ -0,0 +1,112 @@
+/*
+ * pg.c
+ *
+ * Playground template to try things, mostly SFML related.
+ *
+ * */
+
+#include <SFML/Graphics.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <f8.h>
+
+static size_t utf8_detect_back(const char *text)
+{
+ if ((*text-- & 0x80) == 0)
+ return 1;
+
+ if ((*text-- & 0xE0) == 0xC0)
+ return 2;
+
+ if ((*text-- & 0xF0) == 0xE0)
+ return 3;
+
+ if ((*text-- & 0xF8) == 0xF0)
+ return 4;
+
+ return 0;
+}
+
+static void handleWindowEvents(sfRenderWindow *window, char *text, size_t size)
+{
+ sfEvent event;
+
+ while (sfRenderWindow_pollEvent(window, &event)) {
+ if (event.type == sfEvtClosed)
+ sfRenderWindow_close(window);
+
+ if (size == 0)
+ continue;
+
+ if (event.type == sfEvtTextEntered) {
+ size_t len = strnlen(text, size - 1);
+
+ if (event.text.unicode == L'\b') {
+ len -= len > 0 ? 1 : 0;
+ size_t clen = utf8_detect_back(&text[len]);
+ memset(&text[len + 1 - clen], 0, clen);
+ } else {
+ utf32to8_strcpy_s(&text[len], size - len, &event.text.unicode);
+ }
+ }
+ }
+}
+
+int main()
+{
+ /* Initialization */
+
+ char utf8_text[BUFSIZ] = {0};
+ int32_t utf32_text[BUFSIZ] = {0};
+
+ sfRenderWindow *window;
+
+ sfVideoMode mode = (sfVideoMode){450, 570, 32};
+ window = sfRenderWindow_create(mode, "CSFML", sfResize | sfClose, NULL);
+ if (!window)
+ exit(EXIT_FAILURE);
+ sfRenderWindow_setFramerateLimit(window, 60);
+
+ sfFont *font = sfFont_createFromFile("./dat/arial.ttf");
+ sfText *text = sfText_create();
+ sfText_setPosition(text, (sfVector2f){100, 100});
+ sfText_setString(text, "aaaaaaaaaaaa");
+ sfText_setColor(text, (sfColor){255, 255, 255, 255});
+ sfText_setFillColor(text, (sfColor){255, 255, 255, 255});
+ sfText_setCharacterSize(text, 20);
+ sfText_setFont(text, font);
+
+ sfRectangleShape *rect = sfRectangleShape_create();
+ sfRectangleShape_setPosition(rect, (sfVector2f){100, 100});
+ sfRectangleShape_setSize(rect, (sfVector2f){150, 30});
+ sfRectangleShape_setOutlineThickness(rect, 1);
+ sfRectangleShape_setOutlineColor(rect, sfGreen);
+ sfRectangleShape_setFillColor(rect, (sfColor){0, 0, 0, 0});
+
+ /* Main loop */
+
+ while (sfRenderWindow_isOpen(window)) {
+ handleWindowEvents(window, utf8_text, 40);
+
+ sfText_setUnicodeString(text, utf8to32_strcpy_s(utf32_text, sizeof(utf8_text), utf8_text));
+ sfRenderWindow_clear(window, (sfColor){26, 26, 26, 255});
+ sfRenderWindow_drawRectangleShape(window, rect, NULL);
+ sfRenderWindow_drawText(window, text, NULL);
+ sfRenderWindow_display(window);
+ }
+
+ /* Cleanup */
+
+ sfRectangleShape_destroy(rect);
+
+ sfText_destroy(text);
+ sfFont_destroy(font);
+
+ if (window) {
+ sfRenderWindow_destroy(window);
+ window = 0;
+ }
+
+ return EXIT_SUCCESS;
+}