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
+1
View File
@@ -6,4 +6,5 @@
*.sublime-*
Player/player
Player/player.wasm
Master/master
-1
View File
@@ -2,7 +2,6 @@
#define COM_BITS_H
#include "def.h"
#include <assert.h>
#include <stdint.h>
/* How many bits needed to represent a given number */
+13
View File
@@ -10,6 +10,19 @@
#define COM_DEF_COMPILE_MODERN 1
#endif
#define COM_DEF_TARGET_NATIVE 0
#define COM_DEF_TARGET_WASM 1
#if defined(__wasm__)
#define COM_DEF_NOSTD 1
#define COM_DEF_TARGET COM_DEF_TARGET_WASM
/* TODO: Move to std.h instead, forbid stdlib on any platform. */
#define assert(m_t) (m_t)
#define printf(...) ((void)0)
#else
#define COM_DEF_TARGET COM_DEF_TARGET_NATIVE
#endif
/* TODO: uppercase? */
#ifdef COM_DEF_COMPILE_MODERN
#define com_def_alignedas(v_as) _Alignas(v_as)
+7 -2
View File
@@ -3,14 +3,17 @@
https://github.com/howerj/q/blob/master/q.c
*/
#include "fix.h"
#include "Timer/timer.h"
#include "def.h"
#include "fix.h"
#include <assert.h>
#include <stdbool.h>
#include <stdint.h>
#ifndef COM_DEF_NOSTD
#include <assert.h>
#include <stdio.h>
#include <tgmath.h>
#endif
// 4-bit LUT (16 entries) for the normalized range [0.5, 2.0)
// It stores the initial guess scaled to Q16.16.
@@ -130,6 +133,7 @@ void com_fix_run_bench(void) {
}
void com_fix_run_tests(void) {
#ifndef COM_DEF_NOSTD
{
double max_sqrt_deviation = 0.0f;
com_fix_t sqrt_accumulator = 0;
@@ -196,4 +200,5 @@ void com_fix_run_tests(void) {
printf("max sincos deviations: %f %f\n", max_sin_deviation,
max_cos_deviation);
}
#endif
}
-1
View File
@@ -7,7 +7,6 @@
#define COM_FIX_H
#include <stdint.h>
#include <stdio.h>
#define COM_FIX_FRACBITS 16
#define COM_FIX_FRACUNIT (1 << COM_FIX_FRACBITS)
+1 -2
View File
@@ -50,7 +50,7 @@ static inline com_vec_t com_gem_vec_project_clip(com_mat_t a, com_vec_t b) {
* outside of view. Bit test against those per component. This is needed for
* determining new view-lying clipped triangles. */
static inline uint8_t com_gem_clip_vis_project(com_vec_t a, com_pnt_t *out) {
uint8_t mask;
uint8_t mask = 0;
if (a.s.x < -a.s.z || a.s.x > a.s.z)
mask ^= COM_GEM_VERTEX_CLIPPED_X;
if (a.s.y < -a.s.z || a.s.y > a.s.z)
@@ -77,7 +77,6 @@ static inline void com_gem_draw_triangle(com_pnt_t v0, com_pnt_t v1,
// If the point is inside or on all edges, draw the pixel
// (Assumes Clockwise vertex ordering)
if (ws.a[0] >= 0 && ws.a[1] >= 0 && ws.a[2] >= 0) {
printf("%i, %i\n", y, x);
out[(y * 640 + x) * 4 + 0] = 125;
out[(y * 640 + x) * 4 + 1] = 125;
out[(y * 640 + x) * 4 + 2] = 125;
+6 -2
View File
@@ -5,12 +5,16 @@
#include "lzw.h"
#include "bits.h"
#include <assert.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#ifndef COM_DEF_NOSTD
#include <assert.h>
#include <stdlib.h>
#else
#include "std.h"
#endif
/* References: */
/* https://www.w3.org/Graphics/GIF/spec-gif89a.txt */
-1
View File
@@ -20,7 +20,6 @@
#include "fix.h"
#include "vec.h"
#include <stdint.h>
#include <stdio.h>
typedef union {
com_def_alignedas(64) com_def_vector(com_fix_t, a, 4 * 4);
+4
View File
@@ -0,0 +1,4 @@
/* TODO: Actually implement. */
#define realloc(m_p, m_b) (NULL)
#define free(m_p) ((void)0)
+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);
+14 -5
View File
@@ -1,20 +1,29 @@
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>