From 5af4bc0bd1e6e5f76a4ebe68e31b7e94af7a60e9 Mon Sep 17 00:00:00 2001 From: Baptiste Wicht Date: Sat, 24 Sep 2016 20:08:50 +0200 Subject: [PATCH] Do not draw outside of the buffer --- programs/odin/src/main.cpp | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/programs/odin/src/main.cpp b/programs/odin/src/main.cpp index eb6c8b4c..86277d87 100644 --- a/programs/odin/src/main.cpp +++ b/programs/odin/src/main.cpp @@ -46,13 +46,15 @@ void fill_buffer(uint32_t color) { } void draw_pixel(size_t x, size_t y, uint32_t color) { - z_buffer[x + y * y_shift] = color; + if(x < width && y < height){ + z_buffer[x + y * y_shift] = color; + } } void draw_hline(size_t x, size_t y, size_t w, uint32_t color) { auto where = x + y * y_shift; - for (size_t i = 0; i < w; ++i) { + for (size_t i = 0; i < w && x + i < width; ++i) { z_buffer[where + i] = color; } } @@ -60,7 +62,7 @@ void draw_hline(size_t x, size_t y, size_t w, uint32_t color) { void draw_vline(size_t x, size_t y, size_t h, uint32_t color) { auto where = x + y * y_shift; - for (size_t j = 0; j < h; ++j) { + for (size_t j = 0; j < h && y + j < height; ++j) { z_buffer[where] = color; where += y_shift; } @@ -69,8 +71,8 @@ void draw_vline(size_t x, size_t y, size_t h, uint32_t color) { void draw_rect(size_t x, size_t y, size_t w, size_t h, uint32_t color) { auto where = x + y * y_shift; - for (size_t j = 0; j < h; ++j) { - for (size_t i = 0; i < w; ++i) { + for (size_t j = 0; j < h && y + j < height; ++j) { + for (size_t i = 0; i < w && x + i < width; ++i) { z_buffer[where + i] = color; }