toaruos/userspace/wallpaper.c
2012-03-10 18:20:34 -06:00

138 lines
4.0 KiB
C

#include <stdlib.h>
#include <assert.h>
#include <math.h>
#include <ft2build.h>
#include FT_FREETYPE_H
#include FT_CACHE_H
#include "lib/window.h"
#include "lib/graphics.h"
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);
if (alpha) {
sprites[i]->alpha = 1;
load_sprite(&alpha_tmp, alpha);
sprites[i]->masks = alpha_tmp.bitmap;
} else {
sprites[i]->alpha = 0;
}
sprites[i]->blank = 0x0;
}
int32_t min(int32_t a, int32_t b) {
return (a < b) ? a : b;
}
int32_t max(int32_t a, int32_t b) {
return (a > b) ? a : b;
}
/* Bilinear filtering from Wikipedia */
uint32_t getBilinearFilteredPixelColor(sprite_t * tex, double u, double v) {
u *= tex->width;
v *= tex->height;
int x = floor(u);
int y = floor(v);
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) & (0xFFFFFF + (int)r_ALP * 0x1000000);
}
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 {
GFX(x + _x, y + _y) = getBilinearFilteredPixelColor(sprite, (double)_x / (double)width, (double)_y/(double)height);
}
}
}
}
int main (int argc, char ** argv) {
setup_windowing();
int width = wins_globals->server_width;
int height = wins_globals->server_height;
win_width = width;
win_height = height;
/* Do something with a window */
window_t * wina = window_create(0,0, width, height);
assert(wina);
window_fill(wina, rgb(100,100,100));
window_reorder (wina, 0);
init_graphics_window_double_buffer(wina);
window_redraw_full(wina);
#if 1
printf("Loading background...\n");
init_sprite(0, "/usr/share/login-background.bmp", NULL);
printf("Background loaded.\n");
draw_sprite_scaled(sprites[0], 0, 0, width, height);
#endif
#if 0
init_sprite(1, "/usr/share/bs.bmp", "/usr/share/bs-alpha.bmp");
draw_sprite_scaled(sprites[1], center_x(sprites[1]->width), center_y(sprites[1]->height), sprites[1]->width, sprites[1]->height);
#endif
flip();
window_redraw_full(wina);
while (1) {
syscall_yield();
//syscall_wait(wins_globals->server_pid);
}
#if 0
window_destroy(window); // (will close on exit)
teardown_windowing();
#endif
return 0;
}