Skip to content

Commit

Permalink
nterm: fix scrolling
Browse files Browse the repository at this point in the history
  • Loading branch information
jewelcodes committed Oct 6, 2024
1 parent 9fdabb7 commit b826519
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 25 deletions.
4 changes: 2 additions & 2 deletions nterm/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ int main(int argc, char **argv) {
terminal.pixelCount = terminal.width * terminal.height;
terminal.pitch = terminal.width * 4;
terminal.lineSize = terminal.pitch * 16;
terminal.totalSize = terminal.lineSize * terminal.height;
terminal.totalSize = terminal.pitch * terminal.height;
terminal.cursor = 1;
terminal.echo = 1;
terminal.bg = ttyColors[0];
Expand All @@ -103,7 +103,7 @@ int main(int argc, char **argv) {
terminal.redraw = 1;

terminal.wchar = terminal.width / 8;
terminal.hchar = terminal.height / 8;
terminal.hchar = terminal.height / 16;
terminal.x = 0;
terminal.y = 0;
terminal.buffer = malloc(terminal.pixelCount * 4);
Expand Down
49 changes: 26 additions & 23 deletions nterm/src/render.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,26 @@
#include <unistd.h>
#include <string.h>

/* ntermDrawCursor(): draws the cursor on the frame buffer
* params: none
* returns: nothing
*/

void ntermDrawCursor() {
if(!terminal.cursor) return;

// get pixel offset
int x = terminal.x * 8;
int y = terminal.y * 16;
uint32_t *fb = (uint32_t *)((uintptr_t)terminal.buffer + (y * terminal.pitch) + (x * 4));

// I cursor
for(int i = 0; i < 16; i++) {
*fb = terminal.fg;
fb += terminal.width;
}
}

/* ntermCheckBoundaries(): checks the boundaries and scrolls if necessary
* params: none
* returns: 1 if scrolled, zero if not
Expand All @@ -23,8 +43,8 @@ int ntermCheckBoundaries() {

if(terminal.y >= terminal.hchar) {
// scroll up by one line
uint32_t *secondLine = (uint32_t *)((uintptr_t)terminal.buffer + (16*terminal.pitch));
size_t size = (terminal.hchar - 1) * 16 * terminal.pitch;
uint32_t *secondLine = (uint32_t *)((uintptr_t)terminal.buffer + terminal.lineSize);
size_t size = (terminal.hchar - 1) * terminal.lineSize;
memcpy(terminal.buffer, secondLine, size);

// clear the scrolled line, which is also pointed to by size
Expand All @@ -40,6 +60,8 @@ int ntermCheckBoundaries() {
terminal.x = 0;
terminal.y = terminal.hchar - 1;

if(terminal.cursor) ntermDrawCursor();

// and update the entire screen
lseek(terminal.lfb, 0, SEEK_SET);
write(terminal.lfb, terminal.buffer, terminal.totalSize);
Expand All @@ -49,26 +71,6 @@ int ntermCheckBoundaries() {
return 0;
}

/* ntermDrawCursor(): draws the cursor on the frame buffer
* params: none
* returns: nothing
*/

void ntermDrawCursor() {
if(!terminal.cursor) return;

// get pixel offset
int x = terminal.x * 8;
int y = terminal.y * 16;
uint32_t *fb = (uint32_t *)((uintptr_t)terminal.buffer + (y * terminal.pitch) + (x * 4));

// I cursor
for(int i = 0; i < 16; i++) {
*fb = terminal.fg;
fb += terminal.width;
}
}

/* ntermEraseCursor(): erases the cursor from the frame buffer
* params: none
* returns: nothing
Expand Down Expand Up @@ -116,7 +118,8 @@ void ntermPutc(char c) {

terminal.x = 0;
terminal.y++;
ntermCheckBoundaries();
if(ntermCheckBoundaries()) return;

ntermDrawCursor();

ptr = (uint32_t *)((uintptr_t)terminal.buffer + (terminal.y * terminal.lineSize));
Expand Down

0 comments on commit b826519

Please sign in to comment.