Skip to content
This repository has been archived by the owner on May 12, 2024. It is now read-only.

Commit

Permalink
Did some window shit and added detection if mouse is howering a windo…
Browse files Browse the repository at this point in the history
…w, hardcoded to id 0, i need to add window ids later on
  • Loading branch information
KevinAlavik committed Feb 27, 2024
1 parent fb5f057 commit b9e6353
Show file tree
Hide file tree
Showing 6 changed files with 140 additions and 22 deletions.
2 changes: 0 additions & 2 deletions src/filesystem/vfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
125 changes: 105 additions & 20 deletions src/system/wm/windows.c
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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];
Expand All @@ -50,33 +77,91 @@ 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() {
for (int i = 0; i < num_windows; i++) {
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--;
}
}
5 changes: 5 additions & 0 deletions src/system/wm/windows.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <math.h>
#include <printf.h>
#include <stdlib.h>
#include <system/drivers/mouse.h>
#include <tga.h>
#include <vga.h>

Expand Down Expand Up @@ -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__
20 changes: 20 additions & 0 deletions src/system/wm/wm.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
1 change: 1 addition & 0 deletions src/system/wm/wm.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <system/drivers/keyboard.h>
#include <system/drivers/mouse.h>
#include <system/memory/pmm.h>
#include <system/utilities/utilities.h>
#include <system/wm/windows.h>
#include <tga.h>
#include <transform.h>
Expand Down
9 changes: 9 additions & 0 deletions tools/how_to_draw_image.txt
Original file line number Diff line number Diff line change
@@ -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);
}

0 comments on commit b9e6353

Please sign in to comment.