diff --git a/src/canvas.c b/src/canvas.c index c172df1..82f3bf8 100644 --- a/src/canvas.c +++ b/src/canvas.c @@ -4,6 +4,7 @@ * TODO: assert get/set positions are within canvas sizes? */ #include "canvas.h" +#include #include #include #include @@ -81,7 +82,11 @@ void canvas_schari(Canvas *canvas, int i, char c) { /* Get the character at position (x, y) * */ -char canvas_gcharyx(Canvas *canvas, int y, int x) { return canvas->rows[y][x]; } +char canvas_gcharyx(Canvas *canvas, int y, int x) { + assert(x <= canvas->num_cols); + assert(y <= canvas->num_rows); + return canvas->rows[y][x]; +} /* Get the character at index i * diff --git a/src/cursor.c b/src/cursor.c index 7d946e8..3048099 100644 --- a/src/cursor.c +++ b/src/cursor.c @@ -23,7 +23,11 @@ void cursor_move_up(Cursor *cursor, View *view) { } void cursor_move_down(Cursor *cursor, View *view) { - if (cursor->y == view_max_y) { + if (cursor->y + 1 >= view->canvas->num_rows - view->y) { + cursor->y = view->canvas->num_rows - view->y - 1; + return; + } + if (cursor->y + 1 >= view_max_y) { view_move_down(view); return; } @@ -39,7 +43,11 @@ void cursor_move_left(Cursor *cursor, View *view) { } void cursor_move_right(Cursor *cursor, View *view) { - if (cursor->x == view_max_x) { + if (cursor->x >= view->canvas->num_cols - view->x - 1) { + cursor->x = view->canvas->num_cols - view->x - 1; + return; + } + if (cursor->x + 1 >= view_max_x) { view_move_right(view); return; } diff --git a/src/frontend.c b/src/frontend.c index 010db35..a73321a 100644 --- a/src/frontend.c +++ b/src/frontend.c @@ -146,12 +146,34 @@ void front_setcharcursor(char ch) { } void redraw_canvas_win() { - for (int x = 0; x < view_max_x; x++) { - for (int y = 0; y < view_max_y; y++) { + // find max ranges to draw canvas + int max_x = view_max_x; + int max_y = view_max_y; + + if (max_x >= view->canvas->num_cols - view->x) + (max_x = view->canvas->num_cols - view->x); + if (max_y >= view->canvas->num_rows - view->y) + (max_y = view->canvas->num_rows - view->y); + + // draw canvas onto window + for (int x = 0; x < max_x; x++) { + for (int y = 0; y < max_y; y++) { mvwaddch(canvas_win, y + 1, x + 1, canvas_gcharyx(view->canvas, y + view->y, x + view->x)); } } + + // draw fill in rest of window + for (int x = max_x; x < view_max_x; x++) { + for (int y = 0; y < view_max_y; y++) { + mvwaddch(canvas_win, y + 1, x + 1, 'X'); + } + } + for (int y = max_y; y < view_max_y; y++) { + for (int x = 0; x < view_max_x; x++) { + mvwaddch(canvas_win, y + 1, x + 1, 'X'); + } + } } void refresh_screen() { diff --git a/src/view.c b/src/view.c index 699379d..4a865c0 100644 --- a/src/view.c +++ b/src/view.c @@ -23,7 +23,7 @@ void view_move_up(View *view) { } void view_move_down(View *view) { - if (view->y - view_max_y == view->canvas->num_rows) { + if (view->y + view_max_y > view->canvas->num_rows) { return; } view->y++; @@ -37,7 +37,7 @@ void view_move_left(View *view) { } void view_move_right(View *view) { - if (view->x + view_max_x == view->canvas->num_cols) { + if (view->x + view_max_x > view->canvas->num_cols) { return; } view->x++; diff --git a/src/view.h b/src/view.h index 76b58cb..1db0ddd 100644 --- a/src/view.h +++ b/src/view.h @@ -6,8 +6,8 @@ #include "canvas.h" #define STATUS_HEIGHT 2 // not including borders -#define view_max_x (COLS - 3) -#define view_max_y (LINES - 4 - STATUS_HEIGHT) +#define view_max_x (COLS - 2) +#define view_max_y (LINES - 3 - STATUS_HEIGHT) typedef struct { int x, y;