Skip to content

Commit

Permalink
replace text with terminal naming
Browse files Browse the repository at this point in the history
  • Loading branch information
jwt2706 committed Aug 23, 2024
1 parent 0248ebf commit e5f0dc6
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 61 deletions.
20 changes: 20 additions & 0 deletions kernel/include/terminal.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#ifndef TERMINAL_H
#define TERMINAL_H

#include <stdint.h>
#include <limine.h>

struct terminal {
struct limine_framebuffer *framebuffer;
int cursor_x;
int cursor_y;
uint32_t color;
int scale;
};

void terminal_init(struct terminal *term, struct limine_framebuffer *framebuffer, uint32_t color, int scale);
void terminal_write(struct terminal *term, const char *str);
void terminal_write_char(struct terminal *term, char c);
void terminal_event(struct terminal *term, char c);

#endif
10 changes: 0 additions & 10 deletions kernel/include/text.h

This file was deleted.

8 changes: 6 additions & 2 deletions kernel/src/kernel.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <stdbool.h>
#include <limine.h>
#include "../include/colors.h"
#include "../include/text.h"
#include "../include/terminal.h"

// Set the base revision to 2, this is recommended as this is the latest
// base revision described by the Limine boot protocol specification.
Expand Down Expand Up @@ -63,7 +63,11 @@ void _start(void) {
// Fetch the first framebuffer.
struct limine_framebuffer *framebuffer = framebuffer_request.response->framebuffers[0];

draw_string(framebuffer, "BaboscOS booted up successfully!", 10, 10, COLOR_WHITE, 2);
// init the terminal
struct terminal term;
terminal_init(&term, framebuffer, COLOR_BLUE, 2);

terminal_write(&term, "BaboscOS booted up successfully!");

// We're done, just hang...
hcf();
Expand Down
78 changes: 78 additions & 0 deletions kernel/src/terminal.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#include "../include/terminal.h"
#include "../include/font.h"

void terminal_init(struct terminal *term, struct limine_framebuffer *framebuffer, uint32_t color, int scale) {
term->framebuffer = framebuffer;
term->cursor_x = 0;
term->cursor_y = 0;
term->color = color;
term->scale = scale;
}

/*
This will basically just invoke terminal_write_char for each character in the string
but take into account, special things like '\n' for new lines.
*/
void terminal_write(struct terminal *term, const char *str) {
int start_x = term->cursor_x;
while (*str) {
if (*str == '\n') { // if the character is a newline..
term->cursor_y += 8 * term->scale; // ..offset y
term->cursor_x = start_x; // ..and reset x
} else {
terminal_write_char(term, *str);
term->cursor_x += 8 * term->scale; // offset x for each new character so they don't just print on top of each other
}
str++;
}
}

/*
This just writes one character to the terminal
It looks up the right glyph to draw from the font array (see ../include/font.h)
*/
void terminal_write_char(struct terminal *term, char c) {
if (c < 32 || c > 127) return; // only handle ASCII characters

const uint8_t *glyph = font[c - 32]; // find the glyph for the character that we need to draw

for (int i = 0; i < 8; i++) { // loop through each row and column of the glyph (8x8)
for (int j = 0; j < 8; j++) {

// glyph[i] is a byte representing a row of the glyph
// (1 << j) creates a mask to check the j-th bit of the byte
// if the bit is set, the pixel should be drawn
if (glyph[i] & (1 << j)) {

// next, we need to scale each pixel according to the scale factor (if we want bigger text, for example)
for (int dy = 0; dy < term->scale; dy++) { // vertical scale
for (int dx = 0; dx < term->scale; dx++) { // horizontal scale

// now that we know what we need to draw, we just calculate the position in the framebuffer
uint32_t *framebuffer_address = (uint32_t *)term->framebuffer->address;
int vertical_position = term->cursor_y + i * term->scale + dy;
int horizontal_position = term->cursor_x + j * term->scale + dx;
int pixels_per_row = term->framebuffer->pitch / 4; // convert pitch from bytes to number of 32-bit pixels per row

// and set the pixel color
framebuffer_address[vertical_position * pixels_per_row + horizontal_position] = term->color;
}
}
}
}
}
}

/*
This handles special input stuff, like backspace, enter, etc.
*/
void terminal_event(struct terminal *term, char c) {
if (c == '\b') {
if (term->cursor_x > 0) {
term->cursor_x -= 8 * term->scale;
terminal_write_char(term, ' ');
}
} else {
terminal_write_char(term, c);
}
}
49 changes: 0 additions & 49 deletions kernel/src/text.c

This file was deleted.

0 comments on commit e5f0dc6

Please sign in to comment.