summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorcflip <cflip@cflip.net>2023-01-11 11:18:24 -0700
committercflip <cflip@cflip.net>2023-01-11 11:18:24 -0700
commit61c387397d03c479ca110a02bfa78cd39fa82656 (patch)
treec0c4a538867eef968e9c977053470999e89400bd
parent9e6f24187ebe2577fde7c7a971a8cc408f4aeaac (diff)
Get editor size from the window instead of the terminal
Previously the editor would resize itself upon startup by finding the size of the current terminal window, but now that it is running in an actual window it should get the number of rows and columns from there. This also adds parameters for the window size in rows and columns to the window_init() function.
-rw-r--r--editor.c3
-rw-r--r--main.c2
-rw-r--r--window.c18
-rw-r--r--window.h3
4 files changed, 18 insertions, 8 deletions
diff --git a/editor.c b/editor.c
index 14eada7..43a469a 100644
--- a/editor.c
+++ b/editor.c
@@ -28,8 +28,7 @@ void init_editor(struct editor_state* editor)
editor->status_message_time = 0;
editor->syntax = NULL;
- if (get_window_size(&editor->screen_rows, &editor->screen_cols) == -1)
- die("get_window_size");
+ window_get_size(&editor->screen_rows, &editor->screen_cols);
editor->screen_rows -= 2;
}
diff --git a/main.c b/main.c
index 2e6bdc4..e3a9399 100644
--- a/main.c
+++ b/main.c
@@ -5,7 +5,7 @@
int main(int argc, char** argv)
{
- window_init();
+ window_init("Text editor", 40, 80);
struct editor_state editor;
init_editor(&editor);
diff --git a/window.c b/window.c
index 315e75e..219c0ca 100644
--- a/window.c
+++ b/window.c
@@ -13,7 +13,7 @@ static SDL_Texture *font_texture = NULL;
static PSFFont font;
-void window_init()
+void window_init(const char *title, int rows, int cols)
{
/* TODO: There should be a 'panic' method of sorts to be reused here. */
if (SDL_Init(SDL_INIT_VIDEO) != 0) {
@@ -21,8 +21,11 @@ void window_init()
return;
}
- window = SDL_CreateWindow("Bitmap font test", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
- 640, 480, SDL_WINDOW_SHOWN);
+ font = font_load("terminus/ter-u12n.psf");
+ int window_width = cols * font.width;
+ int window_height = rows * font.height;
+
+ window = SDL_CreateWindow(title, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, window_width, window_height, 0);
if (window == NULL) {
fprintf(stderr, "Failed to create a window: %s\n", SDL_GetError());
return;
@@ -34,7 +37,6 @@ void window_init()
return;
}
- font = font_load("terminus/ter-u12n.psf");
font_texture = font_create_texture(renderer, &font);
SDL_ShowWindow(window);
@@ -144,6 +146,14 @@ void window_redraw(struct editor_state *editor)
SDL_UpdateWindowSurface(window);
}
+void window_get_size(int *rows, int *cols)
+{
+ int w, h;
+ SDL_GetWindowSize(window, &w, &h);
+ *cols = w / font.width;
+ *rows = h / font.height;
+}
+
void window_destroy()
{
font_destroy(&font);
diff --git a/window.h b/window.h
index 1c2e019..a414f6f 100644
--- a/window.h
+++ b/window.h
@@ -3,9 +3,10 @@
struct editor_state;
-void window_init();
+void window_init(const char *title, int rows, int cols);
int window_handle_event(struct editor_state *editor);
void window_redraw(struct editor_state *editor);
+void window_get_size(int *rows, int *cols);
void window_destroy();
#endif