Skip to content

Commit

Permalink
Merge pull request #34 from olin/adam/fix_canvasbounds
Browse files Browse the repository at this point in the history
fix writing canvas past bounds
  • Loading branch information
labseven authored May 5, 2019
2 parents b08e0a2 + 1a0fa88 commit 70669f9
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 9 deletions.
7 changes: 6 additions & 1 deletion src/canvas.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* TODO: assert get/set positions are within canvas sizes?
*/
#include "canvas.h"
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Expand Down Expand Up @@ -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
*
Expand Down
12 changes: 10 additions & 2 deletions src/cursor.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand Down
26 changes: 24 additions & 2 deletions src/frontend.c
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
4 changes: 2 additions & 2 deletions src/view.c
Original file line number Diff line number Diff line change
Expand Up @@ -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++;
Expand All @@ -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++;
Expand Down
4 changes: 2 additions & 2 deletions src/view.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 70669f9

Please sign in to comment.