changeset 10:c91c3272101b

core: implement fonts, closes #2444
author David Demelier <markand@malikania.fr>
date Tue, 07 Jan 2020 20:28:35 +0100
parents 66f318fd97a0
children d1cdb90d9558
files Makefile src/font.c src/font.h src/main.c
diffstat 4 files changed, 194 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/Makefile	Tue Jan 07 20:22:22 2020 +0100
+++ b/Makefile	Tue Jan 07 20:28:35 2020 +0100
@@ -19,11 +19,12 @@
 .POSIX:
 
 CC=             clang
-CFLAGS=         -O3 -DNDEBUG -std=c99 -Wall -Wextra
+CFLAGS=         -O3 -DNDEBUG -D_XOPEN_SOURCE=700 -std=c99 -Wall -Wextra
 CPPFLAGS=       -MMD
 PROG=           molko
 SRCS=           src/animation.c \
                 src/clock.c \
+                src/font.c \
                 src/image.c \
                 src/main.c \
                 src/sprite.c \
@@ -32,8 +33,8 @@
 OBJS=           ${SRCS:.c=.o}
 DEPS=           ${SRCS:.c=.d}
 
-SDL_CFLAGS=     `pkg-config --cflags SDL2 SDL2_image`
-SDL_LDFLAGS=    `pkg-config --libs SDL2 SDL2_image`
+SDL_CFLAGS=     `pkg-config --cflags sdl2 SDL2_image SDL2_ttf`
+SDL_LDFLAGS=    `pkg-config --libs sdl2 SDL2_image SDL2_ttf`
 
 .SUFFIXES:
 .SUFFIXES: .c .o
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/font.c	Tue Jan 07 20:28:35 2020 +0100
@@ -0,0 +1,95 @@
+/*
+ * font.c -- basic font management
+ *
+ * Copyright (c) 2020 David Demelier <markand@malikania.fr>
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <assert.h>
+#include <stdbool.h>
+#include <stdio.h>
+
+#include <SDL_ttf.h>
+
+#include "font.h"
+#include "texture_p.h"
+
+struct font {
+	TTF_Font *handle;
+};
+
+struct font *
+font_openf(const char *path, unsigned size)
+{
+	assert(path);
+
+	struct font *f;
+
+	if (!(f = calloc(1, sizeof (struct font))))
+		return NULL;
+	if (!(f->handle = TTF_OpenFont(path, size))) {
+		free(f);
+		return NULL;
+	}
+
+	return f;
+}
+
+struct font *
+font_openb(const void *buffer, size_t buflen, unsigned size)
+{
+	struct font *f;
+	SDL_RWops *ops;
+
+	assert(buffer);
+
+	if (!(f = calloc(1, sizeof (struct font))))
+		return NULL;
+
+	if (!(ops = SDL_RWFromConstMem(buffer, buflen))) {
+		free(f);
+		return NULL;
+	}
+
+	if (!(f->handle = TTF_OpenFontRW(ops, true, size))) {
+		free(f);
+		return NULL;
+	}
+
+	return NULL;
+}
+
+struct texture *
+font_render(struct font *font, const char *text, uint32_t color)
+{
+	assert(font);
+	assert(text);
+
+	/* TODO: refactor this with window.c */
+	SDL_Color fg = {
+		.r = color >> 24 & 0xff,
+		.g = color >> 16 & 0xff,
+		.b = color >> 8  & 0xff,
+		.a = color       & 0xff
+	};
+
+	return texture_from_surface(TTF_RenderUTF8_Blended(font->handle, text, fg));
+}
+
+void
+font_close(struct font *font)
+{
+	TTF_CloseFont(font->handle);
+	free(font);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/font.h	Tue Jan 07 20:28:35 2020 +0100
@@ -0,0 +1,77 @@
+/*
+ * font.h -- basic font management
+ *
+ * Copyright (c) 2020 David Demelier <markand@malikania.fr>
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#ifndef MOLKO_FONT_H
+#define MOLKO_FONT_H
+
+/**
+ * \file font.h
+ * \brief Basic font management.
+ */
+
+#include <stddef.h>
+
+struct font;
+struct texture;
+
+/**
+ * Open font from path file.
+ *
+ * \pre path != NULL
+ * \param path the path to the font
+ * \param size the desired size
+ * \return the font or NULL on error
+ */
+struct font *
+font_openf(const char *path, unsigned size);
+
+/**
+ * Open font from memory buffer.
+ *
+ * \pre buffer != NULL
+ * \param buffer the memory buffer
+ * \param buflen the memory buffer length
+ * \param size the desired size
+ * \warning The buffer must remain valid until font is closed
+ * \return the font or NULL on error
+ */
+struct font *
+font_openb(const void *buffer, size_t buflen, unsigned size);
+
+/**
+ * Render a text.
+ *
+ * \pre font != NULL
+ * \pre text != NULL
+ * \param font the font handle
+ * \param text the text in UTF-8
+ * \param color the color
+ */
+struct texture *
+font_render(struct font *font, const char *text, uint32_t color);
+
+/**
+ * Close the font.
+ *
+ * \pre font != NULL
+ * \param font the font handle
+ */
+void
+font_close(struct font *font);
+
+#endif /* !MOLKO_FONT_H */
--- a/src/main.c	Tue Jan 07 20:22:22 2020 +0100
+++ b/src/main.c	Tue Jan 07 20:28:35 2020 +0100
@@ -19,6 +19,7 @@
 #include <stdio.h>
 
 #include "animation.h"
+#include "font.h"
 #include "clock.h"
 #include "image.h"
 #include "sprite.h"
@@ -26,16 +27,22 @@
 #include "window.h"
 
 #include <SDL.h>
+#include <SDL_ttf.h>
+#include <SDL_image.h>
 
 int
 main(int argc, char **argv)
 {
 	(void)argc;
 	(void)argv;
+	SDL_Init(SDL_INIT_VIDEO);
+	TTF_Init();
+	IMG_Init(IMG_INIT_PNG);
 
-	struct texture *logo;
+	struct texture *logo, *label;
 	struct sprite sprite;
 	struct clock clock;
+	struct font *font;
 	struct animation animation;
 
 	window_init("Molko's Adventure", 640, 480);
@@ -44,15 +51,20 @@
 	clock_start(&clock);
 
 	logo = image_openf("E:\\dev\\molko\\explosion.png");
+	font = font_openf("E:\\dev\\molko\\DejaVuSans.ttf", 10);
 
-	if (!logo)
+	if (!logo || !font) {
+		printf("%s\n", SDL_GetError());
 		exit(1);
+	}
 
+	label = font_render(font, "Hello World", 0xffffffff);
 	sprite_init(&sprite, logo, 256, 256);
 	animation_init(&animation, &sprite, 20);
+
 	setvbuf(stdout, NULL, _IONBF, 0);
 
-	for (;;) {
+	while (!animation_is_complete(&animation)) {
 		uint64_t ticks = clock_elapsed(&clock);
 
 		clock_start(&clock);
@@ -65,12 +77,11 @@
 			}
 		}
 
-		//animation_update(&animation, ticks);
+		animation_update(&animation, ticks);
 		window_clear();
-		sprite_draw(&sprite, 4, 0, 10, 10);
-		//animation_draw(&animation, 10, 10);
+		texture_draw(label, 30, 30);
+		animation_draw(&animation, 10, 10);
 		window_present();
-		printf("%llu\n", ticks);
 		SDL_Delay(50);
 	}