diff --git a/src/filesystem/vfs.c b/src/filesystem/vfs.c index a99f344..f7f2174 100644 --- a/src/filesystem/vfs.c +++ b/src/filesystem/vfs.c @@ -206,8 +206,6 @@ size_t vfs_get_file_size(VFS_t *vfs, uint64_t id, const char *filepath) { for (unsigned int i = 0; i < rd->files; i++) { if (strcmp(rd->content->files[i].name, filepath) == 0) { - dprintf("[\e[0;32mVFS\e[0m] File found: %s, Size: %u bytes\n", filepath, - rd->content->files[i].size); return rd->content->files[i].size; } } diff --git a/src/system/wm/windows.c b/src/system/wm/windows.c index 4593758..1040344 100644 --- a/src/system/wm/windows.c +++ b/src/system/wm/windows.c @@ -3,6 +3,11 @@ window_t windows[MAX_WINDOWS]; int num_windows = 0; +uint32_t w_old_pixels[MAX_WINDOWS][WIN_WIDTH][WIN_HEIGHT]; +int old_window_x[MAX_WINDOWS]; +int old_window_y[MAX_WINDOWS]; +int last_updated_window_index = -1; + void spawn_window(window_t *window) { if (num_windows < MAX_WINDOWS) { window->buffer = malloc(window->width * window->height * sizeof(uint32_t)); @@ -18,17 +23,39 @@ void spawn_window(window_t *window) { memcpy(window->old_buffer, window->buffer, window->width * window->height * sizeof(uint32_t)); + windows[num_windows] = *window; + num_windows++; + + // // TODO: Make this not hardcoded to window id 0? + // old_window_x[0] = window->x; + // old_window_y[0] = window->y; + window->initialized = false; + update_window(window); } else { dprintf("Maximum number of windows reached\n"); } } -void update_window(window_t *window) { +void render_window_gui(window_t *window) { int i, j; uint32_t pixel; + bool buffer_changed = false; - if (!window->initialized) { + for (i = 0; i < window->height; i++) { + for (j = 0; j < window->width; j++) { + if (window->buffer[i * window->width + j] != + window->old_buffer[i * window->width + j]) { + buffer_changed = true; + break; + } + } + if (buffer_changed) { + break; + } + } + + if (!window->initialized || buffer_changed) { for (i = 0; i < window->height; i++) { for (j = 0; j < window->width; j++) { pixel = window->buffer[i * window->width + j]; @@ -50,29 +77,47 @@ void update_window(window_t *window) { draw_tga_from_raw(window->x, window->y, img, size); } - window->initialized = 1; + memcpy(window->old_buffer, window->buffer, + window->width * window->height * sizeof(uint32_t)); + window->initialized = true; + } +} + +bool mouse_style_changed = false; + +void update_window(window_t *window) { + bool buffer_changed = false; + bool should_set_style = false; + + if ((int32_t)mouse_x >= window->x && + (int32_t)mouse_x < window->x + window->width && + (int32_t)mouse_y >= window->y && + (int32_t)mouse_y < window->y + window->height) { + if (!mouse_style_changed) { + should_set_style = true; + mouse_style_changed = true; + } } else { - for (i = 0; i < window->height; i++) { - for (j = 0; j < window->width; j++) { - pixel = window->buffer[i * window->width + j]; - uint8_t alpha = (pixel & ALPHA_MASK) >> ALPHA_SHIFT; - uint8_t red = (pixel & RED_MASK) >> RED_SHIFT; - uint8_t green = (pixel & GREEN_MASK) >> GREEN_SHIFT; - uint8_t blue = (pixel & BLUE_MASK) >> BLUE_SHIFT; - put_pixel_rgba(window->x + j, window->y + i, red, green, blue, alpha); - } + if (mouse_style_changed) { + should_set_style = true; + mouse_style_changed = false; } + } - char *img; - uint32_t size; - vfs_op_status status; - - status = driver_read(vfs, 0x00000000, DEFAULT_WIN_DEC, &img); - if (status == STATUS_OK) { - size = vfs_get_file_size(vfs, 0x00000000, DEFAULT_WIN_DEC); - draw_tga_from_raw(window->x, window->y, img, size); + if (should_set_style) { + if (mouse_style_changed) { + set_mouse_style("hand"); + } else { + set_mouse_style("normal"); } } + + if (old_window_x[0] != window->x || old_window_y[0] != window->y) { + remove_window_gui(window); + render_window_gui(window); + old_window_x[0] = window->x; + old_window_y[0] = window->y; + } } void update_all_windows() { @@ -80,3 +125,43 @@ void update_all_windows() { update_window(&windows[i]); } } + +void remove_window_gui(window_t *window) { + for (int i = 0; i < window->height; i++) { + for (int j = 0; j < window->width; j++) { + int pixel_x = old_window_x[0] + j; + int pixel_y = old_window_y[0] + i; + + if (pixel_x >= 0 && (uint32_t)pixel_x < framebuffer->width && + pixel_y >= 0 && (uint32_t)pixel_y < framebuffer->height) { + uint32_t old_pixel = w_old_pixels[0][i][j]; + uint32_t *framebuffer_address = + (uint32_t *)(framebuffer->address + + pixel_x * (framebuffer->bpp >> 3) + + pixel_y * framebuffer->pitch); + *framebuffer_address = old_pixel; + } + } + } +} + +void destroy_window(window_t *window) { + remove_window_gui(window); + free(window->buffer); + free(window->old_buffer); + + int index = -1; + for (int i = 0; i < num_windows; i++) { + if (&windows[i] == window) { + index = i; + break; + } + } + + if (index != -1) { + for (int i = index; i < num_windows - 1; i++) { + windows[i] = windows[i + 1]; + } + num_windows--; + } +} \ No newline at end of file diff --git a/src/system/wm/windows.h b/src/system/wm/windows.h index 4000cdf..9b06d64 100644 --- a/src/system/wm/windows.h +++ b/src/system/wm/windows.h @@ -6,6 +6,7 @@ #include #include #include +#include #include #include @@ -42,5 +43,9 @@ extern int num_windows; void spawn_window(window_t *window); void update_window(window_t *window); +void destory_window(window_t *window); +void remove_window_gui(window_t *window); +void render_window_gui(window_t *window); + void update_all_windows(); #endif // __WINDOWS_H__ diff --git a/src/system/wm/wm.c b/src/system/wm/wm.c index e1e0743..ec92d50 100644 --- a/src/system/wm/wm.c +++ b/src/system/wm/wm.c @@ -33,4 +33,24 @@ void init_wm() { test_window.height = WIN_HEIGHT; spawn_window(&test_window); + + int nstatus = nighterm_initialize( + NULL, (uint64_t)test_window.buffer, test_window.width, test_window.height, + test_window.width * 4, framebuffer->bpp, malloc); + + if (nstatus) { + dprintf( + "[\e[0;32mSystem\e[0m] Nightem failed to initialize, got code: %s\n", + get_nighterm_return_string(nstatus)); + hcf(); + } else { + dprintf("[\e[0;32mSystem\e[0m] Initialized Nighterm with code: %s\n", + get_nighterm_return_string(nstatus)); + } + + nighterm_set_bg_color(255, 255, 255); + nighterm_set_fg_color(0, 0, 0); + printf("Hello, World!\n"); + printf("Hello, World!\n"); + printf("Hello, World!\n"); } \ No newline at end of file diff --git a/src/system/wm/wm.h b/src/system/wm/wm.h index f4dfdf0..0d719e4 100644 --- a/src/system/wm/wm.h +++ b/src/system/wm/wm.h @@ -13,6 +13,7 @@ #include #include #include +#include #include #include #include diff --git a/tools/how_to_draw_image.txt b/tools/how_to_draw_image.txt new file mode 100644 index 0000000..cf95189 --- /dev/null +++ b/tools/how_to_draw_image.txt @@ -0,0 +1,9 @@ +char *img; +uint32_t size; +vfs_op_status status; + +status = driver_read(vfs, 0x00000000, DEFAULT_WIN_DEC, &img); +if (status == STATUS_OK) { + size = vfs_get_file_size(vfs, 0x00000000, DEFAULT_WIN_DEC); + draw_tga_from_raw(window->x, window->y, img, size); +} \ No newline at end of file