diff options
| author | Kirill Petrashin <kirill8201@yandex.ru> | 2026-06-21 15:01:30 +0300 |
|---|---|---|
| committer | Kirill Petrashin <kirill8201@yandex.ru> | 2026-06-21 15:01:30 +0300 |
| commit | cb49c9bfa880464862b99d9b7a47a927552989d4 (patch) | |
| tree | 5528753165c97c3b2461f85d045eae34c84ddbb0 | |
| parent | 3fed5969034c464e36358fc45a99b57b1f308c11 (diff) | |
| download | wafflestone-cb49c9bfa880464862b99d9b7a47a927552989d4.tar.xz | |
Output
| -rw-r--r-- | framebuffer.c | 25 | ||||
| -rw-r--r-- | framebuffer.h | 8 | ||||
| -rw-r--r-- | main.c | 10 |
3 files changed, 36 insertions, 7 deletions
diff --git a/framebuffer.c b/framebuffer.c index 66b8c2f..7199052 100644 --- a/framebuffer.c +++ b/framebuffer.c @@ -1,4 +1,5 @@ #include <stdlib.h> +#include <stdio.h> #include <termios.h> #include <unistd.h> #include <sys/ioctl.h> @@ -50,12 +51,12 @@ ret: void fb_fill(Framebuffer fb, Pixel pixel) { for (size_t row = 0; row < fb.height; row++) { for (size_t col = 0; col < fb.width; col++) { - fb_put(fb, row, col, pixel); + fb.fb[row][col] = pixel; } } } -void inline fb_put(Framebuffer fb, size_t row, size_t col, Pixel pixel) { +void fb_put(Framebuffer fb, size_t row, size_t col, Pixel pixel) { fb.fb[row][col] = pixel; } @@ -82,6 +83,7 @@ void term_cleanup(void) { write(STDOUT_FILENO, "\x1b[?25h", 6); /* Show the cursor */ write(STDOUT_FILENO, "\x1b[?1049l", 8); /* Alternative screen buffer */ + write(STDOUT_FILENO, "\x1b[0m", 4); /* Reset styling (colour) */ } void term_update_size(void) { @@ -109,5 +111,22 @@ void term_update_size(void) { void fb_print(Framebuffer fb) { write(STDOUT_FILENO, "\x1b[H", 3); /* Move cursor to top-left */ - /* TODO: finish me */ + for (size_t row = 0; row < fb.height; row++) { + for (size_t col = 0; col < fb.width; col++) { + set_fg(fb.fb[row][col].fg); + set_bg(fb.fb[row][col].bg); + write(STDOUT_FILENO, &(fb.fb[row][col].ch), sizeof(wchar_t)); + } + } + fflush(stdout); +} + +static inline void set_fg(Colour col) { + fprintf(stdout, "\x1b[38;2;%u;%u;%um", col.r, col.g, col.g); + fflush(stdout); +} + +static inline void set_bg(Colour col) { + fprintf(stdout, "\x1b[48;2;%u;%u;%um", col.r, col.g, col.g); + fflush(stdout); } diff --git a/framebuffer.h b/framebuffer.h index 373cfb4..cadcb00 100644 --- a/framebuffer.h +++ b/framebuffer.h @@ -34,7 +34,7 @@ typedef struct Pixel_s { /* Solid colour pixels */ #define P_BLACK (Pixel){C_BLACK, C_BLACK, ' '} #define P_WHITE (Pixel){C_WHITE, C_WHITE, ' '} -#define P_RED (Pixel){C_RED, C_RED, ' '} +#define P_RED (Pixel){C_RED, C_RED, '4'} #define P_GREEN (Pixel){C_GREEN, C_GREEN, ' '} #define P_BLUE (Pixel){C_BLUE, C_BLUE, ' '} #define P_YELLOW (Pixel){C_YELLOW, C_YELLOW, ' '} @@ -58,7 +58,7 @@ void fb_fill(Framebuffer fb, Pixel pixel); #define fb_clear(fb) fb_fill(fb, P_BLACK) /* Puts a pixel onto fb in a specified position */ -void inline fb_put(Framebuffer fb, size_t row, size_t col, Pixel pixel); +void fb_put(Framebuffer fb, size_t row, size_t col, Pixel pixel); /* Initializes the terminal and sets TERM_WIDTH and TERM_HEIGHT */ void term_init(void); @@ -72,7 +72,9 @@ void term_update_size(void); /* Prints fb to the screen */ void fb_print(Framebuffer fb); -/* c is a byte */ +static inline void set_fg(Colour col); +static inline void set_bg(Colour col); + #define get_input(c) read(STDIN_FILENO, &c, 1) #endif /* FRAMEBUFFER_H_ */ @@ -1,12 +1,20 @@ #include <stdio.h> #include <unistd.h> +#include <math.h> #include "framebuffer.h" int main(void) { term_init(); + fprintf(stderr, "%zux%zu\n", term_width, term_height); - fprintf(stderr, "Not yet implemented\n"); + Framebuffer fb = fb_new(term_width, term_height); + fb_fill(fb, P_RED); + for (int i = 0; i < 10; i ++) { + fb.fb[i][i] = P_CYAN; + fb_print(fb); + usleep(1000000); + } char c; get_input(c); |
