summaryrefslogtreecommitdiff
path: root/graphics.c
diff options
context:
space:
mode:
Diffstat (limited to 'graphics.c')
-rwxr-xr-xgraphics.c42
1 files changed, 31 insertions, 11 deletions
diff --git a/graphics.c b/graphics.c
index 95cff6b..670610b 100755
--- a/graphics.c
+++ b/graphics.c
@@ -248,6 +248,10 @@ enum {
CHAR_NUM_BITS_ = CHAR_NUM_BITS_X_ * CHAR_NUM_BITS_Y_,
};
+static i64 min2_i64_(i64 x, i64 y) {
+ return x < y ? x : y;
+}
+
static f32 max2_f32_(f32 a, f32 b) {
return a < b ? b : a;
}
@@ -344,11 +348,25 @@ void draw_pixels_to_buffer(Pixel_Buffer dst, Box area, Pixel_Buffer src) {
for (i64 j = j0; j < j1; ++j) {
i64 src_j = (i64) floor(((j - area.y + .5) * src.height) * h_inv);
if (src_j < 0 || src_j >= src.height) continue;
- i64 src_n = src_j * src.stride;
+ vec4_f32 *q = src.pixels + src_j * src.stride;
+ vec4_f32 *p = dst.pixels + j * dst.stride + i0;
for (i64 i = i0; i < i1; ++i) {
i64 src_i = (i64) floor(((i - area.x + .5) * src.width) * w_inv);
if (src_i < 0 || src_i >= src.width) continue;
- put_pixel_(dst, src.pixels[src_n + src_i], i, j);
+ vec4_f32 color = q[src_i];
+ if (color.w == 1.f)
+ *p = color;
+ else {
+ vec4_f32 dst_color = *p;
+ f32 dst_amount = 1. - color.w;
+ *p = (vec4_f32) {
+ .x = dst_color.x * dst_amount + color.x * color.w,
+ .y = dst_color.y * dst_amount + color.y * color.w,
+ .z = dst_color.z * dst_amount + color.z * color.w,
+ .w = max2_f32_(dst_color.w, color.w),
+ };
+ }
+ ++p;
}
}
}
@@ -1948,8 +1966,10 @@ static void scale_and_perform_graphics_request_(Graphics_Context context, Graphi
static Graphics_Context graphics_context_defaults_(Graphics_Context context) {
if (context.dst.pixels == NULL) {
+ i64 max_height = g_platform.num_pixels / g_platform.frame_width;
+
context.dst.width = g_platform.frame_width;
- context.dst.height = g_platform.frame_height;
+ context.dst.height = min2_i64_(max_height, g_platform.frame_height);
context.dst.stride = g_platform.frame_width;
context.dst.pixels = g_platform.pixels;
@@ -2076,19 +2096,19 @@ TEST("colors") {
REQUIRE_EQ((purple.z + 2e-7) * 100, 100);
}
-Pixel_Buffer pixels = {
+static Pixel_Buffer _test_pixels = {
.width = 1280,
.height = 720,
.stride = 1280,
.pixels = (vec4_f32[1280 * 720]) {0},
};
-vec4_f32 color = { 1., 1., 1., 1., };
+static vec4_f32 _test_color = { 1., 1., 1., 1., };
BENCHMARK("fill rectangle") {
BENCHMARK_BEGIN;
{
- fill_rectangle_to_buffer(pixels, color, (Box) { .x = 100, .y = 100, .width = 300, .height = 200, });
+ fill_rectangle_to_buffer(_test_pixels, _test_color, (Box) { .x = 100, .y = 100, .width = 300, .height = 200, });
}
BENCHMARK_END;
}
@@ -2096,7 +2116,7 @@ BENCHMARK("fill rectangle") {
BENCHMARK("fill triangle") {
BENCHMARK_BEGIN;
{
- fill_triangle_to_buffer(pixels, color, (vec2[3]) {
+ fill_triangle_to_buffer(_test_pixels, _test_color, (vec2[3]) {
{ 100, 100 },
{ 300, 100 },
{ 200, 250 },
@@ -2108,7 +2128,7 @@ BENCHMARK("fill triangle") {
BENCHMARK("fill quad") {
BENCHMARK_BEGIN;
{
- fill_quad_to_buffer(pixels, color, (vec2[4]) {
+ fill_quad_to_buffer(_test_pixels, _test_color, (vec2[4]) {
{ 100, 100 },
{ 300, 100 },
{ 300, 200 },
@@ -2121,7 +2141,7 @@ BENCHMARK("fill quad") {
BENCHMARK("fill ellipse") {
BENCHMARK_BEGIN;
{
- fill_ellipse_to_buffer(pixels, color, (Box) { .x = 80, .y = 80, .width = 340, .height = 240, });
+ fill_ellipse_to_buffer(_test_pixels, _test_color, (Box) { .x = 80, .y = 80, .width = 340, .height = 240, });
}
BENCHMARK_END;
}
@@ -2129,7 +2149,7 @@ BENCHMARK("fill ellipse") {
BENCHMARK("fill line") {
BENCHMARK_BEGIN;
{
- fill_line_to_buffer(pixels, color, (vec2[2]) {
+ fill_line_to_buffer(_test_pixels, _test_color, (vec2[2]) {
{ 100, 100 },
{ 300, 200 },
}, 40);
@@ -2141,7 +2161,7 @@ BENCHMARK("draw text area") {
BENCHMARK_BEGIN;
{
c32 text[] = { 'H', 'e', 'l', 'l', 'o', ',', ' ', 'S', 'a', 'i', 'l', 'o', 'r', '!', };
- draw_text_area_to_buffer(pixels, color, (Box) { .x = 100, .y = 100, .width = 300, .height = 200, }, (vec2) { 100, 200 }, sizeof text / sizeof *text, text);
+ draw_text_area_to_buffer(_test_pixels, _test_color, (Box) { .x = 100, .y = 100, .width = 300, .height = 200, }, (vec2) { 100, 200 }, sizeof text / sizeof *text, text);
}
BENCHMARK_END;
}