A sort of blit.

This commit is contained in:
Simon Forman 2023-02-26 15:06:30 -08:00
parent d7db105bc8
commit 9ca4f81325
4 changed files with 62 additions and 30 deletions

View File

@ -22,8 +22,9 @@ for i, _ in enumerate(txt):
print(f'''\
int font_{font_name}_{pointsize}_width = {w};
int font_{font_name}_{pointsize}_height = {h};
u64 font_{font_name}_{pointsize}_number_of_characters = {len(txt)};
u64 font_{font_name}_{pointsize}_width = {w};
u64 font_{font_name}_{pointsize}_height = {h};
u32 font_{font_name}_{pointsize}_data[{len(txt)}][{w * h}];

View File

@ -1,6 +1,7 @@
int font_Inconsolata_22_width = 12;
int font_Inconsolata_22_height = 25;
u64 font_Inconsolata_22_number_of_characters = 94;
u64 font_Inconsolata_22_width = 12;
u64 font_Inconsolata_22_height = 25;
u32 font_Inconsolata_22_data[94][300];

View File

@ -1,3 +1,4 @@
#include <stddef.h>
void
draw_background(u32* buffer, size_t w, size_t h)
@ -12,3 +13,32 @@ draw_background(u32* buffer, size_t w, size_t h)
}
}
}
void
carefree_alpha_blend_blit(u32* dest, u32* source, size_t dest_stride, u64 dest_x, u64 dest_y, u64 w, u64 h)
{
u32* d = dest + dest_stride * dest_y + dest_x;
for (u64 x = 0; x < w; ++x) {
for (u64 y = 0; y < h; ++y) {
u32* pix_ptr = d + x + dest_stride * y;
u32* spix_ptr = source + x + w * y;
u32 pixel = *spix_ptr;
u8 alpha = pixel >> 24;
if (!alpha) { // no alpha
continue;
}
if (0xFF == alpha) {
*pix_ptr = pixel;
continue;
}
u32 dest_pixel = *pix_ptr;
u8 unalpha = 0xFF - alpha;
u8 red = (((dest_pixel >> 16) & 255) * unalpha + ((pixel >> 16) & 255) * alpha) / 0xff;
u8 green = (((dest_pixel >> 8) & 255) * unalpha + ((pixel >> 8) & 255) * alpha) / 0xff;
u8 blue = ((dest_pixel & 255) * unalpha + (pixel & 255) * alpha) / 0xff;
*pix_ptr = (alpha << 24) | (red << 16) | (green << 8) | blue;
}
}
}

View File

@ -1,3 +1,4 @@
#include <stdlib.h>
#include <uvm/syscalls.h>
#include <uvm/utils.h>
#include "/home/sforman/src/Joypy/implementations/uvm-ncc/font/font.h"
@ -10,34 +11,33 @@ size_t FRAME_WIDTH = 768;
size_t FRAME_HEIGHT = 512;
u32 frame_buffer[393216];
int wid;
int wid; // Window ID.
void
draw_char(u8 ch, u64 dest_x, u64 dest_y)
{
u32* dest = frame_buffer + FRAME_WIDTH * dest_y + dest_x;
u32* character_data = font_Inconsolata_22_data[ch];
for (size_t x = 0; x < font_Inconsolata_22_width; ++x) {
for (size_t y = 0; y < font_Inconsolata_22_height; ++y) {
u32* pix_ptr = dest + x + FRAME_WIDTH * y;
u32 pixel = character_data[x + font_Inconsolata_22_width * y];
u8 alpha = pixel >> 24;
if (!alpha) { // no alpha
continue;
}
if (0xFF == alpha) {
*pix_ptr = pixel;
continue;
}
u32 dest_pixel = *pix_ptr;
u8 unalpha = 0xFF - alpha;
u8 red = (((dest_pixel >> 16) & 255) * unalpha + ((pixel >> 16) & 255) * alpha) / 0xff;
u8 green = (((dest_pixel >> 8) & 255) * unalpha + ((pixel >> 8) & 255) * alpha) / 0xff;
u8 blue = ((dest_pixel & 255) * unalpha + (pixel & 255) * alpha) / 0xff;
*pix_ptr = (alpha << 24) | (red << 16) | (green << 8) | blue;
}
// Check the inputs.
// Trust that the type "u-" means we don't have to check lower bounds?
if (ch > font_Inconsolata_22_number_of_characters) {
// No error message or anything, just decline to draw.
return;
}
if (dest_x >= (FRAME_WIDTH - font_Inconsolata_22_width)) {
return;
}
if (dest_y >= (FRAME_HEIGHT - font_Inconsolata_22_height)) {
return;
}
carefree_alpha_blend_blit(
frame_buffer,
font_Inconsolata_22_data[ch],
FRAME_WIDTH,
dest_x,
dest_y,
font_Inconsolata_22_width,
font_Inconsolata_22_height
);
}
@ -49,7 +49,7 @@ draw_char(u8 ch, u64 dest_x, u64 dest_y)
void
mousemove(u64 window_id, u64 new_x, u64 new_y)
mousedown(u64 window_id, u64 new_x, u64 new_y)
{
u32* pix_ptr = frame_buffer + FRAME_WIDTH * new_y + new_x;
*pix_ptr = GREEN;
@ -61,9 +61,9 @@ void
main()
{
init_font_data();
wid = window_create(FRAME_WIDTH, FRAME_HEIGHT, "Bouncing Ball Example", 0);
wid = window_create(FRAME_WIDTH, FRAME_HEIGHT, "Xerblin", 0);
draw_background(frame_buffer, FRAME_WIDTH, FRAME_HEIGHT);
for (size_t ch = 0; ch < 94; ++ch) {
for (size_t ch = 0; ch < font_Inconsolata_22_number_of_characters; ++ch) {
draw_char(
ch,
128 + (ch % 26) * font_Inconsolata_22_width,
@ -72,6 +72,6 @@ main()
}
window_draw_frame(wid, frame_buffer);
//window_on_keydown(wid, keydown);
window_on_mousemove(wid, mousemove);
window_on_mousedown(wid, mousedown);
enable_event_loop();
}