Font rendering

Text boxes may be created where the text is left, center, or right
aligned.
This commit is contained in:
FroggyGreen 2024-07-13 21:31:23 -05:00
parent 08c58e719c
commit b3e114c2a1
16 changed files with 1048 additions and 121 deletions

3
.gitmodules vendored
View File

@ -7,3 +7,6 @@
[submodule "external/freetype"]
path = external/freetype
url = https://gitlab.freedesktop.org/freetype/freetype.git
[submodule "external/utf8"]
path = external/utf8
url = https://github.com/sheredom/utf8.h

1
external/utf8 vendored Submodule

@ -0,0 +1 @@
Subproject commit 2aa5709fe39c66d2868c0d52d42788899b90dc92

View File

@ -22,7 +22,7 @@ foreach(SHADER_FILE ${SHADER_FILES})
endforeach()
add_custom_target(shaders ALL DEPENDS ${SHADER_SPIRV_BINARIES})
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -O3")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -O3 -fPIC")
add_library(sofrenderer SHARED
"src/logger.c"
"src/renderer.c"
@ -49,3 +49,6 @@ target_include_directories(sofrenderer PRIVATE "${PROJECT_SOURCE_DIR}/../externa
# freetype
add_subdirectory(../external/freetype freetype)
target_link_libraries(sofrenderer PRIVATE freetype)
# utf8.h
target_include_directories(sofrenderer PRIVATE "${PROJECT_SOURCE_DIR}/../external/utf8")

View File

@ -0,0 +1,14 @@
#version 460
layout(location = 0) in vec4 fragColor;
layout(location = 1) in vec2 fragTex;
layout(location = 0) out vec4 outColor;
layout(set = 1, binding = 0) uniform sampler2D texSampler;
void main()
{
vec4 sampled = vec4(1.0, 1.0, 1.0, texture(texSampler, fragTex).r);
outColor = fragColor * sampled;
}

View File

@ -0,0 +1,33 @@
#version 460
// vertex geometry data
layout(location = 0) in vec2 inPos;
layout(location = 1) in vec2 inTex;
// instance data
layout(push_constant, std430) uniform UniformPushConstant
{
vec2 pos;
vec2 scale;
vec4 color;
} upc;
layout(location = 0) out vec4 fragColor;
layout(location = 1) out vec2 fragTex;
layout(binding = 0) uniform Camera
{
mat4 view;
mat4 perspective;
mat4 orthographic;
} camera;
void main()
{
vec2 pos = inPos * upc.scale + upc.pos;
gl_Position = camera.orthographic * vec4(pos, -1.0, 1.0);
fragColor = upc.color;
fragTex = inTex;
}

View File

@ -37,7 +37,7 @@
* https://youtu.be/wg8hZxMRwcw
*/
#define HASH_TABLE_KEY_LEN 16
#define HASH_TABLE_KEY_LEN 64
typedef struct hash_table_entry
{
char key[HASH_TABLE_KEY_LEN];

File diff suppressed because it is too large Load Diff

View File

@ -109,6 +109,14 @@ typedef enum
} mesh_pipeline_mode;
typedef enum
{
TEXT_ALIGN_LEFT,
TEXT_ALIGN_CENTER,
TEXT_ALIGN_RIGHT
} text_alignment;
//
//
// FUNCTIONS
@ -137,8 +145,15 @@ u8 insert_mesh(const char* restrict pipeline_name, const char* restrict mesh_nam
u8 remove_mesh(const char* restrict pipeline_name, const char* restrict mesh_name);
u8 load_mesh_instance_data(const char* restrict pipeline_name, const char* restrict mesh_name, const mesh_instance_data* restrict instance_data);
u8 init_vulkan();
void shutdown_vulkan();
u8 insert_font(const char* restrict name);
u8 remove_font(const char* restrict name);
u8 draw_text_box(const char* restrict name);
u8 insert_text_box(const char* restrict name, const char* restrict utf8_str, const char* restrict font_name,
i32 x, i32 y, i32 width, i32 height,
i32 font_size, i32 spacing, i32 line_spacing, text_alignment alignment);
u8 remove_text_box(const char* restrict name);
u8 init_renderer();
void shutdown_renderer();
u8 begin_render_frame();
u8 end_render_frame();

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -0,0 +1,93 @@
Copyright 2012 Google Inc. All Rights Reserved.
This Font Software is licensed under the SIL Open Font License, Version 1.1.
This license is copied below, and is also available with a FAQ at:
http://scripts.sil.org/OFL
-----------------------------------------------------------
SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007
-----------------------------------------------------------
PREAMBLE
The goals of the Open Font License (OFL) are to stimulate worldwide
development of collaborative font projects, to support the font creation
efforts of academic and linguistic communities, and to provide a free and
open framework in which fonts may be shared and improved in partnership
with others.
The OFL allows the licensed fonts to be used, studied, modified and
redistributed freely as long as they are not sold by themselves. The
fonts, including any derivative works, can be bundled, embedded,
redistributed and/or sold with any software provided that any reserved
names are not used by derivative works. The fonts and derivatives,
however, cannot be released under any other type of license. The
requirement for fonts to remain under this license does not apply
to any document created using the fonts or their derivatives.
DEFINITIONS
"Font Software" refers to the set of files released by the Copyright
Holder(s) under this license and clearly marked as such. This may
include source files, build scripts and documentation.
"Reserved Font Name" refers to any names specified as such after the
copyright statement(s).
"Original Version" refers to the collection of Font Software components as
distributed by the Copyright Holder(s).
"Modified Version" refers to any derivative made by adding to, deleting,
or substituting -- in part or in whole -- any of the components of the
Original Version, by changing formats or by porting the Font Software to a
new environment.
"Author" refers to any designer, engineer, programmer, technical
writer or other person who contributed to the Font Software.
PERMISSION & CONDITIONS
Permission is hereby granted, free of charge, to any person obtaining
a copy of the Font Software, to use, study, copy, merge, embed, modify,
redistribute, and sell modified and unmodified copies of the Font
Software, subject to the following conditions:
1) Neither the Font Software nor any of its individual components,
in Original or Modified Versions, may be sold by itself.
2) Original or Modified Versions of the Font Software may be bundled,
redistributed and/or sold with any software, provided that each copy
contains the above copyright notice and this license. These can be
included either as stand-alone text files, human-readable headers or
in the appropriate machine-readable metadata fields within text or
binary files as long as those fields can be easily viewed by the user.
3) No Modified Version of the Font Software may use the Reserved Font
Name(s) unless explicit written permission is granted by the corresponding
Copyright Holder. This restriction only applies to the primary font name as
presented to the users.
4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font
Software shall not be used to promote, endorse or advertise any
Modified Version, except to acknowledge the contribution(s) of the
Copyright Holder(s) and the Author(s) or with their explicit written
permission.
5) The Font Software, modified or unmodified, in part or in whole,
must be distributed entirely under this license, and must not be
distributed under any other license. The requirement for fonts to
remain under this license does not apply to any document created
using the Font Software.
TERMINATION
This license becomes null and void if any of the above conditions are
not met.
DISCLAIMER
THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE
COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL
DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM
OTHER DEALINGS IN THE FONT SOFTWARE.

View File

@ -373,6 +373,7 @@
;; Let cot = cotangent(fov/2). This maps a frustrum [-cot, cot]x[-cot/ratio, cot/ratio]x[near, far] (x and y dimensions are given on the near plane)
;; to the rectangular prism [-1, 1]x[1, -1]x[1, 0] (Vulkan clip space).
;; We are projecting onto the near plane and choosing a reverse-z convention: higher depth values correspond to closer positions.
;; note that a counterclockwise (resp. clockwise) ordering of points will get mapped to a clockwise (resp. counterclockwise) ordering
(define perspective-projection
(lambda (fov ratio near far)
(let ([cotangent-fov (/ 1 (tan (/ fov 2)))])
@ -382,12 +383,13 @@
0 0 [/ near (- far near)] -1
0 0 [/ (* near far) (- far near)] 0))))
;; This maps a rectangular prism [0, width]x[0, height]x[near, far] to the rectangular prism [-1, 1]x[1, -1]x[1, 0] (Vulkan clip space).
;; This maps a rectangular prism [0, width]x[0, height]x[near, far] to the rectangular prism [-1, 1]x[-1, 1]x[1, 0] (Vulkan clip space).
;; We are choosing a reverse-z convention: higher depth values correspond to closer positions.
;; no y-flip, unlike the perspective projection. This will be used to map points to screen coordinates.
(define orthographic-projection
(lambda (width height near far)
(fvec
[/ 2 width] 0 0 0
0 [- (/ 2 height)] 0 0
0 [/ 2 height] 0 0
0 0 [/ 1 (- far near)] 0
-1 1 [/ far (- far near)] 1)))
-1 -1 [/ far (- far near)] 1)))

View File

@ -105,6 +105,15 @@
[else
(error "mesh-pipeline-mode" "Invalid mode-symbol" mode-symbol)])))
(define text-alignment
(lambda (text-alignment-symbol)
(cond
[(eq? text-alignment-symbol 'text-align-left) 0]
[(eq? text-alignment-symbol 'text-align-center) 1]
[(eq? text-alignment-symbol 'text-align-right) 2]
[else
(error "text-alignment" "Invalid text-alignment-symbol" text-alignment-symbol)])))
(define pop-window-event?
(foreign-procedure "can_pop_window_event" () boolean))
@ -250,20 +259,39 @@
(define load-mesh-instance-data
(foreign-procedure "load_mesh_instance_data" (string string (* mesh-instance-data)) boolean))
(define %init-vulkan
(foreign-procedure "init_vulkan" () boolean))
(define insert-font
(foreign-procedure "insert_font" (string) boolean))
(define init-vulkan!
(define remove-font
(foreign-procedure "remove_font" (string) boolean))
(define draw-text-box
(foreign-procedure "draw_text_box" (string) boolean))
(define %insert-text-box
(foreign-procedure "insert_text_box" (string string string integer-32 integer-32 integer-32 integer-32 integer-32 integer-32 integer-32 int) boolean))
(define insert-text-box
(lambda (name utf8-str font-name x y width height font-size spacing line-spacing alignment)
(%insert-text-box name utf8-str font-name x y width height font-size spacing line-spacing (text-alignment alignment))))
(define remove-text-box
(foreign-procedure "remove_text_box" (string) boolean))
(define %init-renderer
(foreign-procedure "init_renderer" () boolean))
(define init-renderer!
(lambda (width height)
(%init-vulkan)
(%init-renderer)
(set-camera-view! (look-at (fvec -1 -1 5) (fvec 0 0 0) (fvec 0 0 1)))
(set-camera-perspective-projection!
(perspective-projection (/ pi 4) (/ height width) perspective-near-plane perspective-far-plane))
(set-camera-orthographic-projection!
(orthographic-projection width height orthographic-near-plane orthographic-far-plane))))
(define shutdown-vulkan
(foreign-procedure "shutdown_vulkan" () void))
(define shutdown-renderer
(foreign-procedure "shutdown_renderer" () void))
(define begin-render-frame
(foreign-procedure "begin_render_frame" () boolean))

View File

@ -27,15 +27,19 @@
(lambda (name width height)
(init-window! name width height)
(init-camera!)
(init-vulkan! width height)
(init-renderer! width height)
(init-grid-selector)
(insert-font "NotoSerif/NotoSerif-Regular")
(insert-text-box "test1" "Hi /agdg/. You know you've made it when you have petty haters who are bitching instead of just making a game." "NotoSerif/NotoSerif-Regular" 200 150 350 100 16 1 24 'text-align-left)
(insert-text-box "test2" "Hi /agdg/. You know you've made it when you have petty haters who are bitching instead of just making a game." "NotoSerif/NotoSerif-Regular" 200 300 350 100 16 1 24 'text-align-right)
(insert-text-box "test3" "Hi /agdg/. You know you've made it when you have petty haters who are bitching instead of just making a game." "NotoSerif/NotoSerif-Regular" 200 450 350 100 16 1 24 'text-align-center)
(set! game-running? #t)
(set! game-scene 'world-scene)))
(define shutdown-game!
(lambda ()
(shutdown-grid-selector)
(shutdown-vulkan)
(shutdown-renderer)
(shutdown-camera!)
(shutdown-window!)))
@ -80,7 +84,11 @@
(cond
[(eq? game-scene 'world-scene) #t]
[(eq? game-scene 'world-scene)
(draw-text-box "test1")
(draw-text-box "test2")
(draw-text-box "test3")
#t]
[(eq? game-scene 'view-scene) (render-grid-selector) #t]