summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Makefile5
-rw-r--r--TODO1
-rw-r--r--font.c137
-rw-r--r--font.h25
-rw-r--r--main.c12
-rw-r--r--window.c243
-rw-r--r--window.h11
7 files changed, 236 insertions, 198 deletions
diff --git a/Makefile b/Makefile
index 2f785c9..e887652 100644
--- a/Makefile
+++ b/Makefile
@@ -1,14 +1,17 @@
CFLAGS=-std=c99
+LFLAGS=-lSDL2
OUT=editor
SRC=main.c \
buffer.c \
editor.c \
file.c \
+ font.c \
input.c \
row.c \
syntax.c \
terminal.c \
+ window.c
${OUT}: ${SRC}
- ${CC} ${CFLAGS} ${SRC} -o ${OUT}
+ ${CC} ${CFLAGS} ${LFLAGS} ${SRC} -o ${OUT}
diff --git a/TODO b/TODO
index 460325d..bf6e0a1 100644
--- a/TODO
+++ b/TODO
@@ -1,2 +1,3 @@
- Write comments in some of the messy code
+- Unify coding style; struct should have CamelCase names
- Try and keep editor_state out of row.c
diff --git a/font.c b/font.c
new file mode 100644
index 0000000..00b7f4d
--- /dev/null
+++ b/font.c
@@ -0,0 +1,137 @@
+#include "font.h"
+
+/* Create a texture containing all of the glyphs in a specified BDF file. */
+SDL_Texture *font_create_texture(SDL_Renderer *renderer, BDFFontInfo *font)
+{
+ SDL_Texture *result;
+ SDL_Surface *surface = SDL_CreateRGBSurface(0, 8 * font->num_chars, font->bounds.h, 32, 0, 0, 0, 0);
+
+ printf("Made surface of size %dx%d\n", surface->w, surface->h);
+ SDL_LockSurface(surface);
+ for (int i = 0; i < font->num_chars; i++) {
+ BDFFontChar chair = font->chars[i];
+ for (int y = 0; y < font->bounds.h; y++) {
+ int bits_for_row = chair.bitmap[y];
+ for (int x = 0; x < 8; x++) {
+ int bit_index = 7 - x;
+ int current_bit = bits_for_row & (1 << bit_index);
+ int xp = x + i * 8;
+ int yp = y;
+ ((Uint32*)(surface->pixels))[xp + yp * surface->w] = current_bit ? 0xffffffff : 0;
+ }
+ }
+ }
+ SDL_UnlockSurface(surface);
+
+ result = SDL_CreateTextureFromSurface(renderer, surface);
+ if (result == NULL) {
+ fprintf(stderr, "Failed to create texture: %s\n", SDL_GetError());
+ }
+ SDL_FreeSurface(surface);
+ return result;
+}
+
+/*
+ * Loads a BDF font file into a data structure.
+ * TODO: This is extremely atrocious!
+ */
+BDFFontInfo font_load(const char *filename)
+{
+ FILE *fp;
+ char *line = NULL;
+ size_t len = 0;
+ ssize_t bytes_read;
+
+ BDFFontInfo result;
+ result.bounds.x = 0;
+ result.bounds.y = 0;
+ result.bounds.w = 0;
+ result.bounds.h = 0;
+ result.chars = NULL;
+ result.num_chars = 0;
+
+ fp = fopen(filename, "r");
+ if (fp == NULL) {
+ fprintf(stderr, "Failed to open file from %s\n", filename);
+ return;
+ }
+
+ int is_reading_bitmap = 0;
+ int char_counter = -1;
+ while ((bytes_read = getline(&line, &len, fp)) != -1) {
+ if (strncmp(line, "FONTBOUNDINGBOX", 15) == 0) {
+ sscanf(line, "FONTBOUNDINGBOX %d %d %d %d\n", &result.bounds.w,
+ &result.bounds.h,
+ &result.bounds.x,
+ &result.bounds.y);
+
+ }
+
+ if (strncmp(line, "CHARS", 5) == 0) {
+ sscanf(line, "CHARS %d\n", &result.num_chars);
+ result.chars = malloc(result.num_chars * sizeof(BDFFontChar));
+ }
+
+ if (strncmp(line, "STARTCHAR", 9) == 0) {
+ char_counter++;
+ result.chars[char_counter].code_point = 0;
+ result.chars[char_counter].next_glyph_offset.x = 0;
+ result.chars[char_counter].next_glyph_offset.y = 0;
+ result.chars[char_counter].bounds.x = 0;
+ result.chars[char_counter].bounds.y = 0;
+ result.chars[char_counter].bounds.w = 0;
+ result.chars[char_counter].bounds.h = 0;
+ result.chars[char_counter].bitmap = NULL;
+ result.chars[char_counter].bitmap_size = 0;
+ }
+
+ if (strncmp(line, "ENCODING", 8) == 0) {
+ int code_point = 0;
+ sscanf(line, "ENCODING %d\n", &code_point);
+ if (code_point < 256)
+ result.char_index_for_code_point[code_point] = char_counter;
+ result.chars[char_counter].code_point = code_point;
+ }
+
+ if (strncmp(line, "DWIDTH", 6) == 0) {
+ sscanf(line, "DWIDTH %d %d\n", &result.chars[char_counter].next_glyph_offset.x,
+ &result.chars[char_counter].next_glyph_offset.y);
+ }
+
+ if (strncmp(line, "BBX", 3) == 0) {
+ sscanf(line, "BBX %d %d %d %d\n", &result.chars[char_counter].bounds.w,
+ &result.chars[char_counter].bounds.h,
+ &result.chars[char_counter].bounds.x,
+ &result.chars[char_counter].bounds.y);
+ result.chars[char_counter].bounds.x += char_counter * 8;
+ result.chars[char_counter].bounds.y = -result.chars[char_counter].bounds.y;
+ }
+
+ if (strncmp(line, "BITMAP", 6) == 0) {
+ is_reading_bitmap = 1;
+ }
+
+ if (is_reading_bitmap) {
+ if (strncmp(line, "ENDCHAR", 7) == 0) {
+ is_reading_bitmap = 0;
+ continue;
+ }
+
+ BDFFontChar *chair = &result.chars[char_counter];
+ chair->bitmap = realloc(chair->bitmap, chair->bitmap_size++);
+ chair->bitmap[chair->bitmap_size - 1] = strtol(line, NULL, 16);
+ }
+ }
+
+ fclose(fp);
+ if (line) { free(line); }
+
+ return result;
+}
+
+void font_destroy(BDFFontInfo *font)
+{
+ for (int i = 0; i < font->num_chars; i++)
+ free(font->chars[i].bitmap);
+ free(font->chars);
+}
diff --git a/font.h b/font.h
new file mode 100644
index 0000000..3a19cb1
--- /dev/null
+++ b/font.h
@@ -0,0 +1,25 @@
+#ifndef _FONT_H
+#define _FONT_H
+
+#include <SDL2/SDL.h>
+
+typedef struct {
+ int code_point;
+ SDL_Point next_glyph_offset;
+ SDL_Rect bounds;
+ char *bitmap;
+ int bitmap_size;
+} BDFFontChar;
+
+typedef struct {
+ SDL_Rect bounds;
+ BDFFontChar *chars;
+ int num_chars;
+ int char_index_for_code_point[128];
+} BDFFontInfo;
+
+BDFFontInfo font_load(const char *);
+SDL_Texture *font_create_texture(SDL_Renderer *, BDFFontInfo *);
+void font_destroy(BDFFontInfo *);
+
+#endif
diff --git a/main.c b/main.c
index 1d17f79..3718c41 100644
--- a/main.c
+++ b/main.c
@@ -1,11 +1,11 @@
#include "input.h"
#include "file.h"
#include "editor.h"
-#include "terminal.h"
+#include "window.h"
int main(int argc, char** argv)
{
- enable_raw_mode();
+ window_init();
struct editor_state editor;
init_editor(&editor);
@@ -16,10 +16,12 @@ int main(int argc, char** argv)
editor_set_status_message(&editor, "HELP: Ctrl+Q = quit, Ctrl+S = save, Ctrl+F = find");
- while (1) {
- editor_refresh_screen(&editor);
- editor_process_keypress(&editor);
+ while (window_handle_event()) {
+ window_redraw(&editor);
+ // editor_refresh_screen(&editor);
+ // editor_process_keypress(&editor);
}
+ window_destroy();
return 0;
}
diff --git a/window.c b/window.c
index b7bc5fb..b3b54f0 100644
--- a/window.c
+++ b/window.c
@@ -1,221 +1,80 @@
-#include <SDL2/SDL.h>
-#include <stdio.h>
-#include <stdlib.h>
-
-typedef struct {
- int code_point;
- SDL_Point next_glyph_offset;
- SDL_Rect bounds;
- char *bitmap;
- int bitmap_size;
-} BDFFontChar;
-
-typedef struct {
- SDL_Rect bounds;
- BDFFontChar *chars;
- int num_chars;
- int char_index_for_code_point[128];
-} BDFFontInfo;
-
-/* Create a texture containing all of the glyphs in a specified BDF file. */
-SDL_Texture *bdf_create_texture(SDL_Renderer *renderer, BDFFontInfo *font)
-{
- SDL_Texture *result;
- SDL_Surface *surface = SDL_CreateRGBSurface(0, 8 * font->num_chars, font->bounds.h, 32, 0, 0, 0, 0);
-
- printf("Made surface of size %dx%d\n", surface->w, surface->h);
- SDL_LockSurface(surface);
- for (int i = 0; i < font->num_chars; i++) {
- BDFFontChar chair = font->chars[i];
- for (int y = 0; y < font->bounds.h; y++) {
- int bits_for_row = chair.bitmap[y];
- for (int x = 0; x < 8; x++) {
- int bit_index = 7 - x;
- int current_bit = bits_for_row & (1 << bit_index);
- int xp = x + i * 8;
- int yp = y;
- ((Uint32*)(surface->pixels))[xp + yp * surface->w] = current_bit ? 0xffffffff : 0;
- }
- }
- }
- SDL_UnlockSurface(surface);
-
- result = SDL_CreateTextureFromSurface(renderer, surface);
- if (result == NULL) {
- fprintf(stderr, "Failed to create texture: %s\n", SDL_GetError());
- }
- SDL_FreeSurface(surface);
- return result;
-}
-
-/*
- * Loads a BDF font file into a data structure.
- * TODO: This is extremely atrocious!
- */
-BDFFontInfo bdf_read_font(const char *filename)
-{
- FILE *fp;
- char *line = NULL;
- size_t len = 0;
- ssize_t bytes_read;
-
- BDFFontInfo result;
- result.bounds.x = 0;
- result.bounds.y = 0;
- result.bounds.w = 0;
- result.bounds.h = 0;
- result.chars = NULL;
- result.num_chars = 0;
-
- fp = fopen(filename, "r");
- if (fp == NULL) {
- fprintf(stderr, "Failed to open file from %s\n", filename);
- return;
- }
-
- int is_reading_bitmap = 0;
- int char_counter = -1;
- while ((bytes_read = getline(&line, &len, fp)) != -1) {
- if (strncmp(line, "FONTBOUNDINGBOX", 15) == 0) {
- sscanf(line, "FONTBOUNDINGBOX %d %d %d %d\n", &result.bounds.w,
- &result.bounds.h,
- &result.bounds.x,
- &result.bounds.y);
-
- }
-
- if (strncmp(line, "CHARS", 5) == 0) {
- sscanf(line, "CHARS %d\n", &result.num_chars);
- result.chars = malloc(result.num_chars * sizeof(BDFFontChar));
- }
-
- if (strncmp(line, "STARTCHAR", 9) == 0) {
- char_counter++;
- result.chars[char_counter].code_point = 0;
- result.chars[char_counter].next_glyph_offset.x = 0;
- result.chars[char_counter].next_glyph_offset.y = 0;
- result.chars[char_counter].bounds.x = 0;
- result.chars[char_counter].bounds.y = 0;
- result.chars[char_counter].bounds.w = 0;
- result.chars[char_counter].bounds.h = 0;
- result.chars[char_counter].bitmap = NULL;
- result.chars[char_counter].bitmap_size = 0;
- }
-
- if (strncmp(line, "ENCODING", 8) == 0) {
- int code_point = 0;
- sscanf(line, "ENCODING %d\n", &code_point);
- if (code_point < 256)
- result.char_index_for_code_point[code_point] = char_counter;
- result.chars[char_counter].code_point = code_point;
- }
-
- if (strncmp(line, "DWIDTH", 6) == 0) {
- sscanf(line, "DWIDTH %d %d\n", &result.chars[char_counter].next_glyph_offset.x,
- &result.chars[char_counter].next_glyph_offset.y);
- }
-
- if (strncmp(line, "BBX", 3) == 0) {
- sscanf(line, "BBX %d %d %d %d\n", &result.chars[char_counter].bounds.w,
- &result.chars[char_counter].bounds.h,
- &result.chars[char_counter].bounds.x,
- &result.chars[char_counter].bounds.y);
- result.chars[char_counter].bounds.x += char_counter * 8;
- result.chars[char_counter].bounds.y = -result.chars[char_counter].bounds.y;
- }
-
- if (strncmp(line, "BITMAP", 6) == 0) {
- is_reading_bitmap = 1;
- }
-
- if (is_reading_bitmap) {
- if (strncmp(line, "ENDCHAR", 7) == 0) {
- is_reading_bitmap = 0;
- continue;
- }
-
- BDFFontChar *chair = &result.chars[char_counter];
- chair->bitmap = realloc(chair->bitmap, chair->bitmap_size++);
- chair->bitmap[chair->bitmap_size - 1] = strtol(line, NULL, 16);
- }
- }
+#include "window.h"
- fclose(fp);
- if (line) { free(line); }
+#include <SDL2/SDL.h>
+#include "font.h"
- return result;
-}
+static SDL_Window *window = NULL;
+static SDL_Renderer *renderer = NULL;
+static SDL_Texture *font_texture = NULL;
-BDFFontChar bdf_char_by_code_point(BDFFontInfo *font, int code_point)
-{
- for (int i = 0; i < font->num_chars; i++)
- if (font->chars[i].code_point == code_point)
- return font->chars[i];
-}
+static BDFFontInfo font;
-int main(int argc, char **argv)
+void window_init()
{
- SDL_Window *window = NULL;
- SDL_Renderer *renderer = NULL;
-
- BDFFontInfo font = bdf_read_font("ter-u12n.bdf");
- printf("Loaded font with %d chars, width %d\n", font.num_chars, font.bounds.w);
-
+ /* TODO: There should be a 'panic' method of sorts to be reused here. */
if (SDL_Init(SDL_INIT_VIDEO) != 0) {
fprintf(stderr, "Failed to initalize SDL: %s\n", SDL_GetError());
- return 1;
+ return;
}
window = SDL_CreateWindow("Bitmap font test", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
640, 480, SDL_WINDOW_SHOWN);
if (window == NULL) {
fprintf(stderr, "Failed to create a window: %s\n", SDL_GetError());
- return 1;
+ return;
}
renderer = SDL_CreateRenderer(window, -1, 0);
if (renderer == NULL) {
fprintf(stderr, "Failed to create a renderer: %s\n", SDL_GetError());
- return 1;
+ return;
}
- SDL_Texture *font_texture = bdf_create_texture(renderer, &font);
- SDL_ShowWindow(window);
-
- const char *testmsg = "Hello, welcome to the bitmap font test!";
-
- int is_running = 1;
- while (is_running) {
- static SDL_Event e;
- while (SDL_PollEvent(&e)) {
- switch (e.type) {
- case SDL_QUIT:
- is_running = 0;
- break;
- }
- }
+ font = font_load("ter-u12n.bdf");
+ font_texture = font_create_texture(renderer, &font);
- SDL_RenderClear(renderer);
- SDL_Rect dstrect = { 0, 0, 0, 0 };
- for (int i = 0; i < strlen(testmsg); i++) {
- const char letter = testmsg[i];
- const int char_index = font.char_index_for_code_point[letter];
- BDFFontChar *glyph = &font.chars[char_index];
+ SDL_ShowWindow(window);
+}
- dstrect.w = glyph->bounds.w;
- dstrect.h = glyph->bounds.h;
- SDL_RenderCopy(renderer, font_texture, &glyph->bounds, &dstrect);
+int window_handle_event()
+{
+ static SDL_Event e;
+ SDL_WaitEvent(&e);
+ switch (e.type) {
+ case SDL_QUIT:
+ return 0;
+ }
+ return 1;
+}
- dstrect.x += glyph->next_glyph_offset.x;
- dstrect.y += glyph->next_glyph_offset.y;
- }
- SDL_RenderPresent(renderer);
- SDL_UpdateWindowSurface(window);
+void window_redraw(struct editor_state *editor)
+{
+ const char *testmsg = "Hello, welcome to the bitmap font test!";
+ SDL_RenderClear(renderer);
+ SDL_Rect dstrect = { 0, 0, 0, 0 };
+ for (int i = 0; i < strlen(testmsg); i++) {
+ const char letter = testmsg[i];
+ const int char_index = font.char_index_for_code_point[letter];
+ BDFFontChar *glyph = &font.chars[char_index];
+
+ dstrect.w = glyph->bounds.w;
+ dstrect.h = glyph->bounds.h;
+ SDL_RenderCopy(renderer, font_texture, &glyph->bounds, &dstrect);
+
+ dstrect.x += glyph->next_glyph_offset.x;
+ dstrect.y += glyph->next_glyph_offset.y;
}
+ SDL_RenderPresent(renderer);
+ SDL_UpdateWindowSurface(window);
+}
+
+void window_destroy()
+{
+ font_destroy(&font);
+ SDL_DestroyTexture(font_texture);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
- SDL_Quit();
- return 0;
+ SDL_Quit();
}
diff --git a/window.h b/window.h
new file mode 100644
index 0000000..0175838
--- /dev/null
+++ b/window.h
@@ -0,0 +1,11 @@
+#ifndef _WINDOW_H
+#define _WINDOW_H
+
+struct editor_state;
+
+void window_init();
+int window_handle_event();
+void window_redraw(struct editor_state *editor);
+void window_destroy();
+
+#endif