diff --git a/userspace/glogin.c b/userspace/glogin.c index 77c03e38..2c0aa82f 100644 --- a/userspace/glogin.c +++ b/userspace/glogin.c @@ -8,6 +8,18 @@ sprite_t * sprites[128]; sprite_t alpha_tmp; +uint16_t win_width; +uint16_t win_height; + +int center_x(int x) { + return (win_width - x) / 2; +} + +int center_y(int y) { + return (win_height - y) / 2; +} + + void init_sprite(int i, char * filename, char * alpha) { sprites[i] = malloc(sizeof(sprite_t)); load_sprite(sprites[i], filename); @@ -36,27 +48,43 @@ uint32_t getBilinearFilteredPixelColor(sprite_t * tex, double u, double v) { v *= tex->height; int x = floor(u); int y = floor(v); - if (x >= tex->width) return rgb(0,0,0); - if (y >= tex->height) return rgb(0,0,0); + if (x >= tex->width) return 0; + if (y >= tex->height) return 0; double u_ratio = u - x; double v_ratio = v - y; double u_o = 1 - u_ratio; double v_o = 1 - v_ratio; + double r_ALP = 256; + if (tex->alpha) { + if (x == tex->width - 1 || y == tex->height - 1) return (SPRITE(tex,x,y) | 0xFF000000) & (0xFFFFFF + _RED(SMASKS(tex,x,y)) * 0x1000000); + r_ALP = (_RED(SMASKS(tex,x,y)) * u_o + _RED(SMASKS(tex,x+1,y)) * u_ratio) * v_o + (_RED(SMASKS(tex,x,y+1)) * u_o + _RED(SMASKS(tex,x+1,y+1)) * u_ratio) * v_ratio; + } if (x == tex->width - 1 || y == tex->height - 1) return SPRITE(tex,x,y); double r_RED = (_RED(SPRITE(tex,x,y)) * u_o + _RED(SPRITE(tex,x+1,y)) * u_ratio) * v_o + (_RED(SPRITE(tex,x,y+1)) * u_o + _RED(SPRITE(tex,x+1,y+1)) * u_ratio) * v_ratio; double r_BLU = (_BLU(SPRITE(tex,x,y)) * u_o + _BLU(SPRITE(tex,x+1,y)) * u_ratio) * v_o + (_BLU(SPRITE(tex,x,y+1)) * u_o + _BLU(SPRITE(tex,x+1,y+1)) * u_ratio) * v_ratio; double r_GRE = (_GRE(SPRITE(tex,x,y)) * u_o + _GRE(SPRITE(tex,x+1,y)) * u_ratio) * v_o + (_GRE(SPRITE(tex,x,y+1)) * u_o + _GRE(SPRITE(tex,x+1,y+1)) * u_ratio) * v_ratio; - return rgb(r_RED,r_GRE,r_BLU); + return rgb(r_RED,r_GRE,r_BLU) & (0xFFFFFF + (int)r_ALP * 0x1000000); } -void window_draw_sprite_scaled(window_t * window, sprite_t * sprite, uint16_t x, uint16_t y, uint16_t width, uint16_t height) { - int x_hi = min(width, (window->width - x)); - int y_hi = min(height, (window->height - y)); - - for (uint16_t _y = 0; _y < y_hi; ++_y) { - for (uint16_t _x = 0; _x < x_hi; ++_x) { - window_set_point(window, x + _x, y + _y, getBilinearFilteredPixelColor(sprite, (double)_x / (double)width, (double)_y/(double)height)); +void draw_sprite_scaled(sprite_t * sprite, uint16_t x, uint16_t y, uint16_t width, uint16_t height) { + int32_t _left = max(x, 0); + int32_t _top = max(y, 0); + int32_t _right = min(x + width, graphics_width - 1); + int32_t _bottom = min(y + height, graphics_height - 1); + for (uint16_t _y = 0; _y < height; ++_y) { + for (uint16_t _x = 0; _x < width; ++_x) { + if (x + _x < _left || x + _x > _right || y + _y < _top || y + _y > _bottom) + continue; + if (sprite->alpha) { + uint32_t n_color = getBilinearFilteredPixelColor(sprite, (double)_x / (double)width, (double)_y/(double)height); + uint32_t f_color = rgb(_ALP(n_color), 0, 0); + GFX(x + _x, y + _y) = alpha_blend(GFX(x + _x, y + _y), n_color, f_color); + } else { + if (SPRITE(sprite,_x,_y) != sprite->blank) { + GFX(x + _x, y + _y) = getBilinearFilteredPixelColor(sprite, (double)_x / (double)width, (double)_y/(double)height); + } + } } } } @@ -70,12 +98,16 @@ int main (int argc, char ** argv) { int width = atoi(argv[1]); int height = atoi(argv[2]); + win_width = width; + win_height = height; + setup_windowing(); /* Do something with a window */ window_t * wina = window_create(0,0, width, height); assert(wina); window_fill(wina, rgb(100,100,100)); + init_graphics_window_double_buffer(wina); #if 0 window_redraw_full(wina); #endif @@ -83,8 +115,16 @@ int main (int argc, char ** argv) { printf("Loading background...\n"); init_sprite(0, "/usr/share/login-background.bmp", NULL); printf("Background loaded.\n"); + init_sprite(1, "/usr/share/bs.bmp", "/usr/share/bs-alpha.bmp"); - window_draw_sprite_scaled(wina, sprites[0], 0, 0, width, height); + draw_sprite_scaled(sprites[0], 0, 0, width, height); + flip(); + + size_t buf_size = wina->width * wina->height * sizeof(uint32_t); + char * buf = malloc(buf_size); + memcpy(buf, wina->buffer, buf_size); + + uint32_t i = 0; while (1) { w_keyboard_t * kbd = poll_keyboard(); @@ -93,9 +133,14 @@ int main (int argc, char ** argv) { free(kbd); } - //window_draw_line(wina, rand() % width, rand() % width, rand() % height, rand() % height, rgb(rand() % 255,rand() % 255,rand() % 255)); - window_draw_sprite_scaled(wina, sprites[0], 0, 0, width, height); + double scale = 2.0 + 1.5 * sin((double)i * 0.02); + + /* Redraw the background by memcpy (super speedy) */ + memcpy(frame_mem, buf, buf_size); + draw_sprite_scaled(sprites[1], center_x(sprites[1]->width * scale), center_y(sprites[1]->height * scale), sprites[1]->width * scale, sprites[1]->height * scale); + flip(); window_redraw_full(wina); + ++i; } //window_destroy(window); // (will close on exit) diff --git a/userspace/lib/graphics.c b/userspace/lib/graphics.c index b59c0a01..1a7e47db 100644 --- a/userspace/lib/graphics.c +++ b/userspace/lib/graphics.c @@ -6,6 +6,7 @@ #include #include #include "graphics.h" +#include "window.h" DEFN_SYSCALL0(getgraphicsaddress, 11); DEFN_SYSCALL1(kbd_mode, 12, int); @@ -25,7 +26,6 @@ uint16_t graphics_depth __attribute__ ((aligned (16))) = 0; uint8_t * gfx_mem = 0; uint8_t * frame_mem; uint32_t gfx_size; -uint32_t flip_offset; void flip() { memcpy(gfx_mem, frame_mem, gfx_size); @@ -36,7 +36,6 @@ void init_graphics() { graphics_width = syscall_getgraphicswidth(); graphics_height = syscall_getgraphicsheight(); graphics_depth = syscall_getgraphicsdepth(); - flip_offset = GFX_H; gfx_size = GFX_B * GFX_H * GFX_W; gfx_mem = (void *)syscall_getgraphicsaddress(); frame_mem = gfx_mem; @@ -47,6 +46,20 @@ void init_graphics_double_buffer() { frame_mem = (void *)((uintptr_t)gfx_mem + sizeof(uint32_t) * GFX_W * GFX_H); } +void init_graphics_window(window_t * window) { + graphics_width = window->width; + graphics_height = window->height; + graphics_depth = 32; + gfx_size = GFX_B * GFX_H * GFX_W; + gfx_mem = (void *)window->buffer; + frame_mem = gfx_mem; +} + +void init_graphics_window_double_buffer(window_t * window) { + init_graphics_window(window); + frame_mem = malloc(sizeof(uint32_t) * GFX_W * GFX_H); +} + uint32_t rgb(uint8_t r, uint8_t g, uint8_t b) { return 0xFF000000 + (r * 0x10000) + (g * 0x100) + (b * 0x1); } diff --git a/userspace/lib/graphics.h b/userspace/lib/graphics.h index e0fd5633..e20cd469 100644 --- a/userspace/lib/graphics.h +++ b/userspace/lib/graphics.h @@ -16,6 +16,7 @@ #define _RED(color) ((color & 0x00FF0000) / 0x10000) #define _GRE(color) ((color & 0x0000FF00) / 0x100) #define _BLU(color) ((color & 0x000000FF) / 0x1) +#define _ALP(color) ((color & 0xFF000000) / 0x1000000) /* * Macros make verything easier. diff --git a/userspace/lib/window.c b/userspace/lib/window.c index 0f60a73a..11ed718b 100644 --- a/userspace/lib/window.c +++ b/userspace/lib/window.c @@ -205,8 +205,8 @@ void window_draw_sprite(window_t * window, sprite_t * sprite, uint16_t x, uint16 for (uint16_t _y = 0; _y < y_hi; ++_y) { for (uint16_t _x = 0; _x < x_hi; ++_x) { if (sprite->alpha) { - /* Technically, unsupported! */ - window_set_point(window, x + _x, y + _y, SPRITE(sprite, _x, _y)); + uint32_t color = ((uint32_t *)window->buffer)[DIRECT_OFFSET(x+_x,y+_y)]; + window_set_point(window, x + _x, y + _y, alpha_blend(color, SPRITE(sprite, _x, _y), SMASKS(sprite, _x, _y))); } else { if (SPRITE(sprite,_x,_y) != sprite->blank) { window_set_point(window, x + _x, y + _y, SPRITE(sprite, _x, _y)); diff --git a/userspace/lib/window.h b/userspace/lib/window.h index 65e2e77f..901559c6 100644 --- a/userspace/lib/window.h +++ b/userspace/lib/window.h @@ -169,4 +169,8 @@ void window_draw_sprite(window_t * window, sprite_t * sprite, uint16_t x, uint16 void window_fill(window_t *window, uint32_t color); +void init_graphics_window(window_t * window); +void init_graphics_window_double_buffer(window_t * window); + + #endif