This commit is contained in:
veclavtlica
2026-09-14 02:04:16 +03:00
parent c350d65592
commit 26f6ba56e9
16 changed files with 178 additions and 72 deletions
+2 -56
View File
@@ -7,9 +7,9 @@
#include <stdio.h>
#include <stdlib.h>
#include "../../Common/gem.h"
#include "../../Common/lzw.h"
#include "../../Common/mat.h"
#include "../draw.h"
#include "display.h"
bool quited = false;
@@ -121,62 +121,8 @@ extern int plr_display_x11_main(int argc, char *argv[]) {
case Expose:
frame += 2;
// Fill buffer with a simple color gradient
for (int y = 0; y < HEIGHT; y++) {
for (int x = 0; x < WIDTH; x++) {
int pixel_index = (y * WIDTH + x) * bytes_per_pixel;
// Assuming standard Little-Endian 32-bit BGRA format
pixel_buffer[pixel_index + 0] = (x + frame) % 256; // Blue
pixel_buffer[pixel_index + 1] = (y + frame) % 256; // Green
pixel_buffer[pixel_index + 2] = (x + y - frame) % 256; // Red
pixel_buffer[pixel_index + 3] = 0; // Alpha/Padding
}
}
com_mat_t proj = com_mat_perspective(640, 480, 1, 100 * COM_FIX_FRACUNIT,
COM_FIX_PIHALF);
com_mat_t view = com_mat_look_at(com_vec_from(0, 0, 0),
com_vec_from(0, COM_FIX_FRACUNIT, 0),
com_vec_from(-COM_FIX_FRACUNIT, 0, 0));
com_mat_t mvp = com_mat_reoder(com_mat_mul(proj, view));
com_vec_t v0 =
com_vec_from(5 * COM_FIX_FRACUNIT, 0, 2 * COM_FIX_FRACUNIT);
com_vec_t v1 = com_vec_from(5 * COM_FIX_FRACUNIT, 1 * COM_FIX_FRACUNIT,
4 * COM_FIX_FRACUNIT);
com_vec_t v2 = com_vec_from(5 * COM_FIX_FRACUNIT, 4 * COM_FIX_FRACUNIT,
4 * COM_FIX_FRACUNIT);
com_pnt_t uv0 = com_pnt_from(0 * COM_FIX_FRACUNIT, 0 * COM_FIX_FRACUNIT);
com_pnt_t uv1 = com_pnt_from(1 * COM_FIX_FRACUNIT, 0 * COM_FIX_FRACUNIT);
com_pnt_t uv2 = com_pnt_from(1 * COM_FIX_FRACUNIT, 1 * COM_FIX_FRACUNIT);
com_vec_t c0 = com_gem_vec_project_clip(mvp, v0);
com_vec_t c1 = com_gem_vec_project_clip(mvp, v1);
com_vec_t c2 = com_gem_vec_project_clip(mvp, v2);
com_pnt_t p0, p1, p2;
uint8_t cc0 = com_gem_clip_vis_project(c0, &p0);
uint8_t cc1 = com_gem_clip_vis_project(c1, &p1);
uint8_t cc2 = com_gem_clip_vis_project(c2, &p2);
com_pnt_print(p0);
com_pnt_print(p1);
com_pnt_print(p2);
uint8_t tex[32 * 32 * 3] = {0};
for (int y = 0; y < 32; ++y) {
for (int x = 0; x < 32; ++x) {
tex[(y * 32 + x) * 3 + 0] = (x / 2 + y / 2) % 2 == 0 ? 175 : 0;
tex[(y * 32 + x) * 3 + 1] = (x / 2 + y / 2) % 2 == 0 ? 0 : 0;
tex[(y * 32 + x) * 3 + 2] = (x / 2 + y / 2) % 2 == 0 ? 175 : 0;
}
}
com_gem_draw_triangle_textured(p0, p1, p2, (uint8_t *)pixel_buffer, uv0,
uv1, uv2, tex);
// com_gem_draw_triangle(p0, p1, p2, (uint8_t *)pixel_buffer);
plr_draw_screen((uint8_t *)pixel_buffer);
// Draw the complete image onto the window when exposed
XPutImage(display,
+1 -1
View File
@@ -1,4 +1,4 @@
#include "Display/display.h"
#include "../Display/display.h"
extern int main(int argc, char *argv[]) {
return plr_display_x11_main(argc, argv);
+15 -6
View File
@@ -1,20 +1,29 @@
CC=clang
CC = clang
# todo: Only link to libm on debug.
CFLAGS=-lX11 -Wall -std=c99 -g3 -O3 -msse2 -lm
CFLAGS = -Wall -std=c99 -g3 -O3
DEPS = ../Common/lzw.h ../Common/mat.h ../Common/vec.h ../Common/fix.h ../Common/def.h ../Common/bits.h \
../Common/gem.h \
../Common/Timer/timer.h \
./Display/display.h
OBJ = ../Common/lzw.o ../Common/fix.o ../Common/mat.o
LINUX = main.o Display/x11.o ../Common/Timer/unix.o
./Display/display.h \
./draw.h
SRC = ../Common/lzw.c ../Common/fix.c ../Common/mat.c ./draw.c
OBJ = ../Common/lzw.o ../Common/fix.o ../Common/mat.o ./draw.o
LINUX = ./Main/linux.o ./amalgam.o Display/x11.o ../Common/Timer/unix.o
WASM = ./amalgam.c
DOS = maindos.c Display/dos.c
%.o: %.c $(DEPS)
$(CC) -c -o $@ $< $(CFLAGS)
linux: $(LINUX) $(OBJ)
linux: CFLAGS += -msse2 -lm -lX11
linux: $(LINUX) | $(SRC)
$(CC) -o player $^ $(CFLAGS)
# Note: lld package might need to be downloaded.
wasm: CFLAGS += --target=wasm32 -nostdlib -Wl,--no-entry
wasm:
$(CC) -o player.wasm $(WASM) $(CFLAGS)
dos: $(DOS) $(DEPS)
wcl386 -bt=dos4g -ox -l=dos4g $(DOS)
+8
View File
@@ -0,0 +1,8 @@
/*
Amalgamation file, compiling all in single unit.
*/
#include "../Common/fix.c"
#include "../Common/lzw.c"
#include "../Common/mat.c"
#include "./draw.c"
+70
View File
@@ -0,0 +1,70 @@
#include "../Common/def.h"
#include "../Common/gem.h"
#include <stdint.h>
#if COM_DEF_TARGET == COM_DEF_TARGET_WASM
uint8_t plr_draw_buffer[640 * 480 * 4];
__attribute__((export_name("plr_draw_get_pixel_buffer"))) uint8_t *
plr_draw_get_pixel_buffer(void) {
return plr_draw_buffer;
}
#endif
__attribute__((export_name("plr_draw_screen"))) void
plr_draw_screen(uint8_t *pixel_buffer) {
for (int y = 0; y < 480; y++) {
for (int x = 0; x < 640; x++) {
int pixel_index = (y * 640 + x) * 4;
// Assuming standard Little-Endian 32-bit BGRA format
pixel_buffer[pixel_index + 0] = (x) % 256; // Blue
pixel_buffer[pixel_index + 1] = (y) % 256; // Green
pixel_buffer[pixel_index + 2] = (x + y) % 256; // Red
pixel_buffer[pixel_index + 3] = 255; // Alpha/Padding
}
}
com_mat_t proj =
com_mat_perspective(640, 480, 1, 100 * COM_FIX_FRACUNIT, COM_FIX_PIHALF);
com_mat_t view = com_mat_look_at(com_vec_from(0, 0, 0),
com_vec_from(0, COM_FIX_FRACUNIT, 0),
com_vec_from(-COM_FIX_FRACUNIT, 0, 0));
com_mat_t mvp = com_mat_reoder(com_mat_mul(proj, view));
com_vec_t v0 = com_vec_from(5 * COM_FIX_FRACUNIT, 0, 2 * COM_FIX_FRACUNIT);
com_vec_t v1 = com_vec_from(5 * COM_FIX_FRACUNIT, 1 * COM_FIX_FRACUNIT,
4 * COM_FIX_FRACUNIT);
com_vec_t v2 = com_vec_from(5 * COM_FIX_FRACUNIT, 4 * COM_FIX_FRACUNIT,
4 * COM_FIX_FRACUNIT);
com_pnt_t uv0 = com_pnt_from(0 * COM_FIX_FRACUNIT, 0 * COM_FIX_FRACUNIT);
com_pnt_t uv1 = com_pnt_from(1 * COM_FIX_FRACUNIT, 0 * COM_FIX_FRACUNIT);
com_pnt_t uv2 = com_pnt_from(1 * COM_FIX_FRACUNIT, 1 * COM_FIX_FRACUNIT);
com_vec_t c0 = com_gem_vec_project_clip(mvp, v0);
com_vec_t c1 = com_gem_vec_project_clip(mvp, v1);
com_vec_t c2 = com_gem_vec_project_clip(mvp, v2);
com_pnt_t p0, p1, p2;
uint8_t cc0 = com_gem_clip_vis_project(c0, &p0);
uint8_t cc1 = com_gem_clip_vis_project(c1, &p1);
uint8_t cc2 = com_gem_clip_vis_project(c2, &p2);
com_pnt_print(p0);
com_pnt_print(p1);
com_pnt_print(p2);
uint8_t tex[32 * 32 * 3] = {0};
for (int y = 0; y < 32; ++y) {
for (int x = 0; x < 32; ++x) {
tex[(y * 32 + x) * 3 + 0] = (x / 2 + y / 2) % 2 == 0 ? 175 : 0;
tex[(y * 32 + x) * 3 + 1] = (x / 2 + y / 2) % 2 == 0 ? 0 : 0;
tex[(y * 32 + x) * 3 + 2] = (x / 2 + y / 2) % 2 == 0 ? 175 : 0;
}
}
com_gem_draw_triangle_textured(p0, p1, p2, (uint8_t *)pixel_buffer, uv0, uv1,
uv2, tex);
}
+8
View File
@@ -0,0 +1,8 @@
#ifndef PLR_DRAW_H
#define PLR_DRAW_H
#include <stdint.h>
extern void plr_draw_screen(uint8_t *pixel_buffer);
#endif
+42
View File
@@ -0,0 +1,42 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Brightstone</title>
<style>
canvas {
border: 2px solid #333;
background-color: #f0f0f0;
}
</style>
</head>
<body>
<canvas id="canvas" width="640" height="480"></canvas>
<script>
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
async function main() {
const wasm = await WebAssembly.instantiateStreaming(fetch("player.wasm"));
const { memory, plr_draw_screen, plr_draw_get_pixel_buffer } = wasm.instance.exports;
const bufferPtr = plr_draw_get_pixel_buffer();
const wasmBuffer = new Uint8ClampedArray(
memory.buffer,
bufferPtr,
640 * 480 * 4
);
const imgData = new ImageData(wasmBuffer, 640, 480);
function renderLoop() {
plr_draw_screen(bufferPtr);
ctx.putImageData(imgData, 0, 0);
requestAnimationFrame(renderLoop);
}
renderLoop(wasm);
}
main();
</script>
</body>
</html>