Skip to content

Commit

Permalink
Add function to check for rectangle intresection and get them
Browse files Browse the repository at this point in the history
  • Loading branch information
dreamos82 committed Jul 21, 2024
1 parent 96a06d5 commit 000ddc0
Show file tree
Hide file tree
Showing 9 changed files with 148 additions and 12 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ tests:
${TOOLCHAIN} ${TESTFLAGS} tests/test_vfs.c tests/test_common.c src/fs/vfs.c src/drivers/fs/ustar.c -o tests/test_vfs.o
${TOOLCHAIN} ${TESTFLAGS} tests/test_utils.c src/kernel/mem/vmm_util.c -o tests/test_utils.o
./tests/test_mem.o && ./tests/test_kheap.o && ./tests/test_number_conversion.o && ./tests/test_vm.o && ./tests/test_vfs.o && ./tests/test_utils.o
${TOOLCHAIN} ${TESTFLAGS} tests/test_window.c tests/test_common.c src/kernel/graphics/window.c -o tests/test_window.o && ./tests/test_window.o

todolist:
@echo "List of todos and fixme in sources: "
Expand Down
1 change: 1 addition & 0 deletions build/Common.mk
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ TESTFLAGS := -std=gnu99 \
-I src/include/fs \
-I src/include/drivers/fs \
-I src/include/kernel \
-I src/include/kernel/graphics \
-I src/include/kernel/arch/x86_64 \
-I src/include/kernel/arch/common/mem \
-I src/include/sys \
Expand Down
2 changes: 1 addition & 1 deletion src/include/kernel/framebuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,6 @@ void _fb_printStrAndNumberAt(const char*, uint64_t, size_t, size_t, uint32_t, ui
void get_framebuffer_mode(uint32_t* pixels_w, uint32_t* pixels_h, uint32_t* chars_w, uint32_t* chars_h);
void draw_logo(uint32_t start_x, uint32_t start_y);

void _fb_scrollLine_tmp(uint32_t x_origin, uint32_t y_origin, uint32_t window_width, uint32_t window_height, uint32_t line_height, uint32_t number_of_lines_to_scroll);
void _fb_scrollLine(_fb_window_t *scrolling_window, uint32_t line_height, uint32_t number_of_lines_to_scroll, _fb_window_t *area_to_pin);
//void _fb_scroll(_fb_window_t *scrolling_window, uint32_t line_height, uint32_t number_of_lines_to_scroll, _fb_window_t *area_to_pin);
#endif
9 changes: 9 additions & 0 deletions src/include/kernel/graphics/shapes.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#ifndef __SHAPES_H__
#define __SHAPES_H__

#include <stdint.h>
#include <window.h>

void _fb_draw_rectangle(_fb_window_t main_window, _fb_window_t rectangle, uint32_t color);

#endif
1 change: 1 addition & 0 deletions src/include/kernel/graphics/window.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@ typedef struct _fb_window_t {


bool _fb_intersect_window(uint32_t x, uint32_t y, _fb_window_t *area_to_interesect);
uint8_t _fb_get_rectangles(_fb_window_t *rectangles, _fb_window_t* main_window, _fb_window_t *area_to_intersect);

#endif
16 changes: 13 additions & 3 deletions src/kernel/framebuffer/framebuffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ void _fb_printStr( const char *string, uint32_t fg, uint32_t bg ) {
_fb_printStrAt(string, 0, cur_fb_line, fg, bg);
cur_fb_line++;
if ( cur_fb_line >= framebuffer_data.number_of_lines ) {
pretty_log(Verbose, "Exceeding number of lines, calling scroll_function");
//pretty_log(Verbose, "Exceeding number of lines, calling _fb_scrollLine");
_fb_scrollLine(&framebuffer_main_window, _psf_get_height(psf_font_version), 1, &framebuffer_logo_area);
cur_fb_line--;
}
Expand All @@ -174,7 +174,7 @@ void _fb_printStrAndNumber(const char *string, uint64_t number, uint32_t fg, uin
_fb_printStrAndNumberAt(string, number, 0, cur_fb_line, fg, bg);
cur_fb_line++;
if ( cur_fb_line >= framebuffer_data.number_of_lines ) {
pretty_log(Verbose, "Exceeding number of lines, calling scroll_function");
//pretty_log(Verbose, "Exceeding number of lines, calling _fb_scrollLine");
_fb_scrollLine(&framebuffer_main_window, _psf_get_height(psf_font_version), 1, &framebuffer_logo_area);
cur_fb_line--;
}
Expand All @@ -184,9 +184,19 @@ void _fb_printStrAt( const char *string, size_t cx, size_t cy, uint32_t fg, uint
while (*string != '\0'){
if (*string == '\n'){
cx=0;
cy++;
//cy++;
cur_fb_line = cy;
if ( cur_fb_line+1 >= framebuffer_data.number_of_lines ) {
//pretty_log(Verbose, "Exceeding number of lines, calling _fb_scrollLine");
_fb_scrollLine(&framebuffer_main_window, _psf_get_height(psf_font_version), 1, &framebuffer_logo_area);
} else {
cy++;
cur_fb_line = cy;
}
} else if (*string == '\t'){
for ( int i = 0; i++; i < 4 ) {
_fb_putchar(' ', cx+i, cy, fg, bg);
}
cx += 4;
} else {
_fb_putchar(*string, cx, cy, fg, bg);
Expand Down
13 changes: 13 additions & 0 deletions src/kernel/graphics/shapes.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include <framebuffer.h>
#include <shapes.h>

void _fb_draw_rectangle(_fb_window_t main_window, _fb_window_t rectangle, uint32_t color) {
//0xEB72F9
uint32_t rectangle_w = rectangle.x_orig + rectangle.width;
uint32_t rectangle_h = rectangle.y_orig + rectangle.height;
for(uint32_t i = rectangle.y_orig; i < rectangle_h; i++) {
for(uint32_t j = rectangle.x_orig; j < rectangle_w; j++) {
_fb_put_pixel(j, i, color);
}
}
}
73 changes: 65 additions & 8 deletions src/kernel/graphics/window.c
Original file line number Diff line number Diff line change
@@ -1,23 +1,80 @@
#include <logging.h>
#include <window.h>


/**
* This function return true if x and y are within the area of the window defined by area_to_interesect
* This function return true if x and y are within the area of the window defined by area_to_intersect
*
*
* @param x x coord of the point to check
* @param pixels_h y cood of the point to check
* @param area_to_interesect the window area we want to check the intersection
* @return true if the point (x,y) is within the rectangle specified by area_to_interesect
* @param area_to_intersect the window area we want to check the intersection
* @return true if the point (x,y) is within the rectangle specified by area_to_intersect
*/
bool _fb_intersect_window(uint32_t x, uint32_t y, _fb_window_t *area_to_interesect) {
if ( x > area_to_interesect->x_orig + area_to_interesect->width)
bool _fb_intersect_window(uint32_t x, uint32_t y, _fb_window_t *area_to_intersect) {
if ( x > area_to_intersect->x_orig + area_to_intersect->width)
return false;
if ( x < area_to_interesect->x_orig)
if ( x < area_to_intersect->x_orig)
return false;
if (y > area_to_interesect->y_orig + area_to_interesect->height)
if (y > area_to_intersect->y_orig + area_to_intersect->height)
return false;
if (y < area_to_interesect->y_orig)
if (y < area_to_intersect->y_orig)
return false;
return true;
}

/**
* This function return true if x and y are within the area of the window defined by area_to_intersect
*
*
* @param x x coord of the point to check
* @param pixels_h y cood of the point to check
* @param area_to_intersect the window area we want to check the intersection
* @return true if the point (x,y) is within the rectangle specified by area_to_intersect
*/
uint8_t _fb_get_rectangles(_fb_window_t *rectangles, _fb_window_t* main_window, _fb_window_t *area_to_intersect) {
//_fb_window_t rectangles[4];
uint8_t cur_rectangle = 0;
if(area_to_intersect == NULL) {
return 0;
}
// We can have up to 4 rectangles surrounding an area.
// How many depends on where the area.
// The window and the area have the left side with same origin
pretty_logf(Verbose, "Main window: x %d, y %d, w %d, h %d", main_window->x_orig, main_window->y_orig, main_window->width, main_window->height);
pretty_logf(Verbose, "Area to intersect: x %d, y %d, w %d, h %d", area_to_intersect->x_orig, area_to_intersect->y_orig, area_to_intersect->width, area_to_intersect->height);
if (main_window->x_orig < area_to_intersect->x_orig) {
rectangles[cur_rectangle].x_orig = main_window->x_orig;
rectangles[cur_rectangle].y_orig = main_window->y_orig;
rectangles[cur_rectangle].width = area_to_intersect->x_orig - main_window->x_orig;
rectangles[cur_rectangle].height = main_window->height;
pretty_logf(Verbose, "1 rectangle: %d, x_orig: %d, y_orig: %d, widht: %d, height: %d", cur_rectangle, rectangles[cur_rectangle].x_orig, rectangles[cur_rectangle].y_orig, rectangles[cur_rectangle].width, rectangles[cur_rectangle].height);
cur_rectangle++;
}
if (area_to_intersect->x_orig + area_to_intersect->width < main_window->x_orig + main_window->width) {
rectangles[cur_rectangle].x_orig = area_to_intersect->x_orig + area_to_intersect->width;
rectangles[cur_rectangle].y_orig = main_window->y_orig;
rectangles[cur_rectangle].width = (main_window->x_orig + main_window->width) - area_to_intersect->x_orig;
rectangles[cur_rectangle].height = main_window->height;
pretty_logf(Verbose, "2 rectangle: %d, x_orig: %d, y_orig: %d, widht: %d, height: %d", cur_rectangle, rectangles[cur_rectangle].x_orig, rectangles[cur_rectangle].y_orig, rectangles[cur_rectangle].width, rectangles[cur_rectangle].height);
cur_rectangle++;
}
if ( main_window->y_orig < area_to_intersect->y_orig) {
rectangles[cur_rectangle].x_orig = area_to_intersect->x_orig;
rectangles[cur_rectangle].width = area_to_intersect->width;
rectangles[cur_rectangle].y_orig = main_window->y_orig;
rectangles[cur_rectangle].height = area_to_intersect->y_orig - main_window->y_orig;
pretty_logf(Verbose, "3 rectangle: %d, x_orig: %d, y_orig: %d, widht: %d, height: %d", cur_rectangle, rectangles[cur_rectangle].x_orig, rectangles[cur_rectangle].y_orig, rectangles[cur_rectangle].width, rectangles[cur_rectangle].height);
cur_rectangle++;

}
if ( (area_to_intersect->y_orig + area_to_intersect->height) < (main_window->y_orig + main_window->height) ) {
rectangles[cur_rectangle].x_orig = area_to_intersect->x_orig;
rectangles[cur_rectangle].width = area_to_intersect->width;
rectangles[cur_rectangle].y_orig = (area_to_intersect->y_orig + area_to_intersect->height);
rectangles[cur_rectangle].height = (main_window->y_orig + main_window->height) - (area_to_intersect->y_orig + area_to_intersect->height);
pretty_logf(Verbose, "4 rectangle: %d, x_orig: %d, y_orig: %d, widht: %d, height: %d", cur_rectangle, rectangles[cur_rectangle].x_orig, rectangles[cur_rectangle].y_orig, rectangles[cur_rectangle].width, rectangles[cur_rectangle].height);
cur_rectangle++;
}
return cur_rectangle;
}
44 changes: 44 additions & 0 deletions tests/test_window.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#include <assert.h>
#include <test_common.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <window.h>

void test_get_rectangles();

int main() {
test_get_rectangles();
}

void test_get_rectangles() {
_fb_window_t rectangles[4];
_fb_window_t main_window = {0,0, 1280, 800};
/*main_window.x_orig = 0;
main_window.y_orig = 0;
main_window.width = 1280;
main_window.height = 800;*/
_fb_window_t area_to_intersect = {1030, 0, 250, 250};
printf("Testing _fb_get_rectangles function\n");
printf("\t[%s]: main_window values: x: %d - y: %d - w: %d - h: %d\n", __FUNCTION__, main_window.x_orig, main_window.y_orig, main_window.width, main_window.height);
uint8_t n_squares = _fb_get_rectangles(rectangles, &main_window, NULL);
printf("\t[%s] n_squares should be 0 : %d\n", __FUNCTION__, n_squares);
assert(n_squares == 0);
n_squares = _fb_get_rectangles(rectangles, &main_window, &area_to_intersect);
printf("\t[%s] n_squares should be 2 : %d\n", __FUNCTION__, n_squares);
assert(n_squares == 2);
_fb_window_t area_to_intersect_2 = {500, 300, 200, 120};
n_squares = _fb_get_rectangles(rectangles, &main_window, &area_to_intersect_2);
printf("\t[%s]: area_to_intersect values: x: %d - y: %d - w: %d - h: %d\n", __FUNCTION__, area_to_intersect_2.x_orig, area_to_intersect_2.y_orig, area_to_intersect_2.width, area_to_intersect_2.height);
printf("\t[%s]: n_squares should be 4: %d\n", __FUNCTION__, n_squares);
for(int i = 0; i < n_squares; i++) {
printf("\t[%s]: x_orig: %d - y_orig: %d - width: %d - height: %d\n", __FUNCTION__, rectangles[i].x_orig, rectangles[i].y_orig, rectangles[i].width, rectangles[i].height);
}
_fb_window_t area_to_intersect_3 = {0, 0, 200, 120};
n_squares = _fb_get_rectangles(rectangles, &main_window, &area_to_intersect_3);
printf("\t[%s]: area_to_intersect values: x: %d - y: %d - w: %d - h: %d\n", __FUNCTION__, area_to_intersect_3.x_orig, area_to_intersect_3.y_orig, area_to_intersect_3.width, area_to_intersect_3.height);
printf("\t[%s]: n_squares should be 4: %d\n", __FUNCTION__, n_squares);
for(int i = 0; i < n_squares; i++) {
printf("\t[%s]: x_orig: %d - y_orig: %d - width: %d - height: %d\n", __FUNCTION__, rectangles[i].x_orig, rectangles[i].y_orig, rectangles[i].width, rectangles[i].height);
}
}

0 comments on commit 000ddc0

Please sign in to comment.