From 11c8b134d7de3d7b2249d74b26a87bc9d6acc27d Mon Sep 17 00:00:00 2001 From: ubq323 Date: Mon, 22 Apr 2024 00:34:20 +0100 Subject: rle --- .gitignore | 1 + Makefile | 3 ++ client/pictures.ha | 1 + drawing/drawing.ha | 1 - packet_reader/packet_reader.ha | 6 +++- packet_reader/rle.ha | 65 ++++++++++++++++++++++++++++++++++++++++++ server/main.ha | 2 +- 7 files changed, 76 insertions(+), 3 deletions(-) create mode 100644 packet_reader/rle.ha diff --git a/.gitignore b/.gitignore index 5321b16..de34bb1 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ *.ppm +*.png e/* diff --git a/Makefile b/Makefile index 411f03c..d25f2ba 100644 --- a/Makefile +++ b/Makefile @@ -4,6 +4,9 @@ nothing: client: hare run -lSDL2 client/ +build_client: + hare build -o garden -lSDL2 client/ + server: hare run server/ diff --git a/client/pictures.ha b/client/pictures.ha index f139b28..ce6c600 100644 --- a/client/pictures.ha +++ b/client/pictures.ha @@ -102,6 +102,7 @@ fn process_chunk_loadedness( const pic = pmgr.pictures[i]; if (!is_picture_visible(camera_pos, pic.world_pos)) { fmt::printfln("## unloading {},{}", pic.world_pos.0, pic.world_pos.1)!; + sdl2::SDL_FreeSurface(pmgr.pictures[i].surface); delete(pmgr.pictures[i]); }; }; diff --git a/drawing/drawing.ha b/drawing/drawing.ha index ae36501..729b441 100644 --- a/drawing/drawing.ha +++ b/drawing/drawing.ha @@ -113,7 +113,6 @@ export fn stroke(picture: *picture, c0: pos, c1: pos, r: i32, color: u32) void = circle(picture, c1, r, color); return; }; - fmt::printfln("count: {}",count)!; const sx = dx: f64 / (count): f64; const sy = dy: f64 / (count): f64; diff --git a/packet_reader/packet_reader.ha b/packet_reader/packet_reader.ha index 6d8c315..a1b5b8f 100644 --- a/packet_reader/packet_reader.ha +++ b/packet_reader/packet_reader.ha @@ -4,21 +4,25 @@ use endian; use drawing; use drawing::{pos,CHUNKSIZE}; -export def VERSION: u8 = 1; +export def VERSION: u8 = 2; export type error = !str; export type packet_reader = struct { buf: []u8, good: []u8, + wbuf: []u8, + wgood: []u8, }; export fn new() packet_reader = { let pr = packet_reader { buf = alloc([0...],512*512*4*2), // ehhh + wbuf = alloc([0...], 512*512*4*2), // ehhhh ... }; pr.good = pr.buf[0..0]; + pr.wgood = pr.wbuf[0..0]; return pr; }; diff --git a/packet_reader/rle.ha b/packet_reader/rle.ha new file mode 100644 index 0000000..af9f080 --- /dev/null +++ b/packet_reader/rle.ha @@ -0,0 +1,65 @@ + +use fmt; + +fn item_encode(count: int, val: u32) u32 = + ((count-1)<<24):u32 | (val&0xffffff); +fn item_decode(c: u32) (int, u32) = + ((((c&0xff000000)>>24)+1):int, c&0xffffff); + +// caller freeeee +export fn rle_encode(d: []u32) []u32 = { + let out: []u32 = []; + let start = 0z; + for (start < len(d)) { + let this = d[start]; + let ix = start + 1; + for (ix < len(d) && d[ix] == this) ix += 1; + const val = this; + let count = (ix - start): int; + fmt::printfln("#{:_06x} x{}",val, count)!; + for (count > 0) { + append(out, item_encode(count%256, val)); + count -= 256; + }; + start = ix; + }; + return out; +}; + +// ooooo caller freeeeeee meee +export fn rle_decode(d: []u32) []u32 = { + let out: []u32 = []; + for (const k .. d) { + const (count, val) = item_decode(k); + append(out, [val...], count: size); + }; + return out; +}; + +fn dotest(eu: []u32) void = { + const ee = rle_encode(eu); + defer free(ee); + // for (const k .. ee) { + // const (count, val) = item_decode(k); + // fmt::printfln("0x{:_08x} | #{:_06x} x{}", k, val, count)!; + // }; + + const dd = rle_decode(ee); + defer free(dd); + + assert(len(eu) == len(dd), "length mismatch??"); + let good = true; + for (let i = 0z; i < len(eu); i += 1) { + assert(eu[i] == dd[i], "value wrong"); + // fmt::printfln("{} vs {}",eu[i], dd[i])!; + // if (eu[i] != dd[i]) { good=false; fmt::println("uh oh")!; }; + }; + // if (good) fmt::printfln("good yay")!; + +}; + +@test fn euou() void = dotest([1,1,1,1,1,2,2,2,3,3,3,3,3,4,4,5,5,5,666,69,9,9]); +@test fn oeuoeu2() void = dotest([1,2,3,4,5]); +@test fn oeuoeu2() void = dotest([1,1,1]); +@test fn oeuoeu2() void = dotest([1,6]); +@test fn oeuoeu2() void = dotest([1]); diff --git a/server/main.ha b/server/main.ha index 41e6b6a..475d19a 100644 --- a/server/main.ha +++ b/server/main.ha @@ -69,7 +69,6 @@ export fn main() void = { for (state.running) { const timeout = time::diff(now, next); - fmt::printfln("running loop, timeout {} sec",timeout/time::SECOND)!; loop(&state, timeout); now = time::now(time::clock::MONOTONIC); @@ -289,6 +288,7 @@ fn unload_distant_chunks(state: *server_state) void = { fmt::printfln("unloading {},{}", state.pictures[i].world_pos.0, state.pictures[i].world_pos.1)!; + free(state.pictures[i].d); delete(state.pictures[i]); }; }; -- cgit v1.2.3