aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--font.c64
-rw-r--r--font.h2
-rw-r--r--main.c4
-rw-r--r--seat.c2
-rw-r--r--state.h16
5 files changed, 85 insertions, 3 deletions
diff --git a/font.c b/font.c
index 9198b3a..2a082b3 100644
--- a/font.c
+++ b/font.c
@@ -59,6 +59,70 @@ bool init_freetype(client_state* state, const char* file) {
fprintf(stderr, "Failed to set font size\n");
return false;
}
+ return true;
+}
+
+bool init_atlas(client_state* state) {
+ FT_Int32 load_flags = FT_LOAD_RENDER | FT_LOAD_TARGET_(FT_RENDER_MODE_SDF);
+ for (int i = 32; i < 128; i++) {
+ if (FT_Load_Char(state->ft_face, i, load_flags)) {
+ fprintf(stderr, "Failed to render character %c\n", (char)i);
+ }
+ printf("%c: %u, %u\n", (char)i, state->ft_face->glyph->bitmap.width, state->ft_face->glyph->bitmap.rows);
+ state->atlas.width += state->ft_face->glyph->bitmap.width;
+ state->atlas.height = MAX(state->atlas.height, state->ft_face->glyph->bitmap.rows);
+ }
+
+ glActiveTexture(GL_TEXTURE0);
+ glGenTextures(1, &state->atlas.texture);
+ glBindTexture(GL_TEXTURE_2D, state->atlas.texture);
+
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
+
+ glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
+ glTexImage2D(
+ GL_TEXTURE_2D,
+ 0,
+ GL_RED,
+ (GLsizei) state->atlas.width,
+ (GLsizei) state->atlas.height,
+ 0,
+ GL_RED,
+ GL_UNSIGNED_BYTE,
+ NULL);
+
+ uint32_t x = 0;
+ for (int i = 32; i < 128; i++) {
+ if (FT_Load_Char(state->ft_face, i, load_flags)) {
+ fprintf(stderr, "Failed to render character %c\n", (char)i);
+ }
+
+ if (FT_Render_Glyph(state->ft_face->glyph, FT_RENDER_MODE_NORMAL)) {
+ fprintf(stderr, "ERROR: could not render glyph of a character with code %d\n", i);
+ }
+
+ state->atlas.metrics[i].advance_x = (float)state->ft_face->glyph->advance.x / 64;
+ state->atlas.metrics[i].advance_y = (float)state->ft_face->glyph->advance.y / 64;
+ state->atlas.metrics[i].bitmap_width = state->ft_face->glyph->bitmap.width;
+ state->atlas.metrics[i].bitmap_height = state->ft_face->glyph->bitmap.rows;
+ state->atlas.metrics[i].texture_offset = x;
+
+ glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
+ glTexSubImage2D(
+ GL_TEXTURE_2D,
+ 0,
+ x,
+ 0,
+ state->ft_face->glyph->bitmap.width,
+ state->ft_face->glyph->bitmap.rows,
+ GL_RED,
+ GL_UNSIGNED_BYTE,
+ state->ft_face->glyph->bitmap.buffer);
+ x += state->ft_face->glyph->bitmap.width;
+ }
return true;
}
diff --git a/font.h b/font.h
index ecb4a01..ee51443 100644
--- a/font.h
+++ b/font.h
@@ -2,7 +2,9 @@
#define FONT_H_
#include "state.h"
+
char* get_font(const char* font_name);
bool init_freetype(client_state* state, const char* font);
+bool init_atlas(client_state* state);
#endif // FONT_H_
diff --git a/main.c b/main.c
index 4489928..4be21fd 100644
--- a/main.c
+++ b/main.c
@@ -36,12 +36,14 @@ int main() {
}
char* font = get_font("Monospace");
- printf("font: %s\n", font);
if (!init_freetype(&state, font)) {
fprintf(stderr, "Failed to Initalize FreeType\n");
}
free(font);
+ init_atlas(&state);
+
+
// event loop
while (state.running) {
poll_events(&state);
diff --git a/seat.c b/seat.c
index 528a007..f90935c 100644
--- a/seat.c
+++ b/seat.c
@@ -12,8 +12,6 @@ void setup_seat(client_state* state) {
void capabilities(void *data, struct wl_seat *wl_seat, uint32_t capabilities) {
client_state* state = data;
-
- printf("seat capablities\n");
if (capabilities & WL_SEAT_CAPABILITY_KEYBOARD && !state->keyboard) {
setup_keyboard(state);
}
diff --git a/state.h b/state.h
index 465e9b6..1929cad 100644
--- a/state.h
+++ b/state.h
@@ -16,6 +16,20 @@
#include "./glad/glad.h"
+#define MAX(a, b) (((a) > (b)) ? (a) : (b))
+
+typedef struct {
+ float advance_x, advance_y;
+ uint32_t bitmap_width, bitmap_height;
+ uint32_t texture_offset;
+} metrics_t;
+
+typedef struct {
+ uint32_t width, height;
+ GLuint texture;
+ metrics_t metrics[128];
+} atlas_t;
+
typedef struct {
// wayland
struct wl_display* display;
@@ -53,6 +67,8 @@ typedef struct {
FT_Library ft_library;
FT_Face ft_face;
+ atlas_t atlas;
+
bool running;
} client_state;