diff --git a/kernel/src/logger.rs b/kernel/src/logger.rs index fd9cc4a..d1bd6e1 100644 --- a/kernel/src/logger.rs +++ b/kernel/src/logger.rs @@ -36,13 +36,26 @@ impl log::Log for Logger { Level::Debug => "25", Level::Trace => "103", }; - let module = record.module_path().unwrap_or_default(); - let line = record.line().unwrap_or_default(); - crate::arch::log(format_args!( - "\x1b[38;5;{lvl_color}m{lvl}\x1b[0m [{module}:{line}]: {}\r\n", - record.args(), - )) - .expect("write to serial console"); + let module = record + .module_path() + .unwrap_or_default() + .rsplit_once(':') + .unwrap_or_default() + .1; + if module == "" { + crate::arch::log(format_args!( + "\x1b[38;5;{lvl_color}m{lvl}\x1b[0m: {}\r\n", + record.args(), + )) + .expect("write to serial console"); + } else { + let line = record.line().unwrap_or_default(); + crate::arch::log(format_args!( + "\x1b[38;5;{lvl_color}m{lvl}\x1b[0m [{module}:{line}]: {}\r\n", + record.args(), + )) + .expect("write to serial console"); + } } fn flush(&self) {} diff --git a/repbuild/src/dev.rs b/repbuild/src/dev.rs index d2a66e3..f93edd1 100644 --- a/repbuild/src/dev.rs +++ b/repbuild/src/dev.rs @@ -74,7 +74,6 @@ impl Package { &path, Options { fmt: true, - // optimize: true, ..Default::default() }, &mut bytes, @@ -83,7 +82,6 @@ impl Package { hblang::run_compiler( &path, Options { - // optimize: true, ..Default::default() }, &mut bytes, @@ -100,7 +98,6 @@ impl Package { &path, Options { dump_asm: true, - // optimize: true, ..Default::default() }, &mut bytes, diff --git a/sysdata/libraries/render/src/image.hb b/sysdata/libraries/render/src/image.hb deleted file mode 100644 index 249094a..0000000 --- a/sysdata/libraries/render/src/image.hb +++ /dev/null @@ -1,102 +0,0 @@ -.{Color, Surface, new_surface} := @use("./lib.hb"); -.{log, memory} := @use("../../stn/src/lib.hb") - -BitmapFileHeader := packed struct { - img_type: u16, - size: u32, - reserved_1: u16, - reserved_2: u16, - offset: u32, -} - -BitmapInfoHeader := packed struct { - size: u32, - width: i32, - height: i32, - planes: u16, - bits: u16, - compression: u32, - image_size: u32, - x_resolution: i32, - y_resolution: i32, - n_colours: u32, - important_colours: u32, -} - -BitmapColorHeader := packed struct { - red_mask: u32, - green_mask: u32, - blue_mask: u32, - alpha_mask: u32, - color_space_type: u32, - unused: u32, -} - -surface_from_bmp := fn(bmp: ^u8): ?Surface { - file_header := @as(^BitmapFileHeader, @bitcast(bmp)) - if file_header.img_type != 0x4D42 { - log.error("failed to load bmp image: not a bmp image, idiot\0") - return null - } - info_header := @as(^BitmapInfoHeader, @bitcast(bmp + @sizeof(BitmapFileHeader))) - bmp += file_header.offset - - px := info_header.width * info_header.height - ptr := @as(^Color, @bitcast(bmp)) - tmp := @as(Color, idk) - row := @as(i32, 0) - - loop if row == info_header.height / 2 break else { - col := @as(i32, 0) - loop if col == info_header.width break else { - top_index := row * info_header.width + col - bottom_index := (info_header.height - 1 - row) * info_header.width + col - - tmp = *(ptr + top_index); - *(ptr + top_index) = *(ptr + bottom_index); - *(ptr + bottom_index) = tmp - - col += 1 - } - row += 1 - } - - return .(@bitcast(bmp), info_header.width, info_header.height) -} - -new_surface_from_bmp := fn(bmp: ^u8): ?Surface { - file_header := @as(^BitmapFileHeader, @bitcast(bmp)) - if file_header.img_type != 0x4D42 { - log.error("failed to load bmp image: not a bmp image, idiot\0") - return null - } - info_header := @as(^BitmapInfoHeader, @bitcast(bmp + @sizeof(BitmapFileHeader))) - bmp += file_header.offset - - width := @as(uint, @intcast(info_header.width)) - height := @as(uint, @intcast(info_header.height)) - - surface := new_surface(width, height) - top_start_idx := surface.buf - bottom_start_idx := surface.buf + width * (height - 1) - rows_to_copy := height - top_cursor := @as(^Color, @bitcast(bmp)) - bottom_cursor := top_cursor + width * (height - 1) - - loop if rows_to_copy <= 1 break else { - @inline(memory.copy, Color, top_cursor, bottom_start_idx, @bitcast(width)) - @inline(memory.copy, Color, bottom_cursor, top_start_idx, @bitcast(width)) - - top_start_idx += surface.width - bottom_start_idx -= surface.width - top_cursor += width - bottom_cursor -= width - rows_to_copy -= 2 - } - - if rows_to_copy == 1 { - @inline(memory.copy, Color, top_cursor, top_start_idx, @bitcast(width)) - } - - return surface -} \ No newline at end of file diff --git a/sysdata/libraries/render/src/image/bmp.hb b/sysdata/libraries/render/src/image/bmp.hb new file mode 100644 index 0000000..690198b --- /dev/null +++ b/sysdata/libraries/render/src/image/bmp.hb @@ -0,0 +1,52 @@ +.{Color, Surface, new_surface, put_surface} := @use("../lib.hb"); +.{log, memory} := @use("../../../stn/src/lib.hb") + +BitmapFileHeader := packed struct { + magic: u16, + size: u32, + reserved_1: u16, + reserved_2: u16, + offset: u32, +} + +BitmapInfoHeader := packed struct { + size: u32, + width: i32, + height: i32, + planes: u16, + bits: u16, + compression: u32, + image_size: u32, + x_resolution: i32, + y_resolution: i32, + n_colours: u32, + important_colours: u32, +} + +BitmapColorHeader := packed struct { + red_mask: u32, + green_mask: u32, + blue_mask: u32, + alpha_mask: u32, + color_space_type: u32, + unused: u32, +} + +from := fn(bmp: ^u8): ?Surface { + file_header := @as(^BitmapFileHeader, @bitcast(bmp)) + info_header := @as(^BitmapInfoHeader, @bitcast(bmp + @sizeof(BitmapFileHeader))) + bmp += file_header.offset + + if file_header.magic != 0x4D42 | info_header.width == 0 | info_header.height == 0 { + log.error("Invalid BMP image.\0") + return null + } + + width := @as(uint, info_header.width) + height := @as(uint, info_header.height) + lhs := Surface.(@bitcast(bmp), width, height) + rhs := new_surface(width, height) + put_surface(rhs, lhs, .(0, 0), true) + + return rhs +} \ No newline at end of file diff --git a/sysdata/libraries/render/src/image/lib.hb b/sysdata/libraries/render/src/image/lib.hb new file mode 100644 index 0000000..5b8765e --- /dev/null +++ b/sysdata/libraries/render/src/image/lib.hb @@ -0,0 +1,34 @@ +.{log} := @use("../../../stn/src/lib.hb"); +.{Surface} := @use("../lib.hb") +bmp := @use("bmp.hb") +qoi := @use("qoi.hb") + +BMP := 0x4D42 +QOI := 0x66696F71 +// stand-in for null until bugfix +DOES_NOT_EXIST := 1 << 32 + +get_format := fn(file: ^u8): uint { + if *@as(^u16, @bitcast(file)) == BMP { + return BMP + } else if *@as(^u32, @bitcast(file)) == QOI { + return QOI + } else { + return DOES_NOT_EXIST + } +} + +from := fn(file: ^u8): ?Surface { + format := get_format(file) + + if format == DOES_NOT_EXIST { + log.error("Could not detect image format.\0") + return null + } else if format == BMP { + return bmp.from(file) + } else if format == QOI { + return qoi.from(file) + } + + return null +} \ No newline at end of file diff --git a/sysdata/libraries/render/src/image/qoi.hb b/sysdata/libraries/render/src/image/qoi.hb new file mode 100644 index 0000000..ca6e8c1 --- /dev/null +++ b/sysdata/libraries/render/src/image/qoi.hb @@ -0,0 +1,101 @@ +.{Color, Surface, new_surface} := @use("../lib.hb"); +.{log, memory} := @use("../../../stn/src/lib.hb") + +/* source: + https://github.com/phoboslab/qoi/blob/master/qoi.h */ + +QOI_SRGB := 0 +QOI_LINEAR := 1 +QOI_OP_INDEX := 0x0 +QOI_OP_DIFF := 0x40 +QOI_OP_LUMA := 0x80 +QOI_OP_RUN := 0xC0 +QOI_OP_RGB := 0xFE +QOI_OP_RGBA := 0xFF +QOI_MASK_2 := 0xC0 +QOI_COLOR_HASH := fn(c: Color): u8 { + return (c.r * 3 + c.g * 5 + c.b * 7 + c.a * 11) % 64 +} +QOI_MAGIC := 0x716F6966 +QOI_PIXELS_MAX := 400000000 + +QuiteOkayHeader := packed struct { + magic: u32, + width: u32, + height: u32, + channels: u8, + colorspace: u8, +} + +be_to_le := fn(big: u32): u32 { + return (big & 0xFF000000) >> 24 | (big & 0xFF0000) >> 8 | (big & 0xFF00) << 8 | (big & 0xFF) << 24 +} + +from := fn(qoi: ^u8): ?Surface { + header := @as(^QuiteOkayHeader, @bitcast(qoi)) + + qoi += @sizeof(QuiteOkayHeader) + + width := be_to_le(header.width) + height := be_to_le(header.height) + + if be_to_le(header.magic) != QOI_MAGIC | width == 0 | height == 0 | header.channels < 3 | header.channels > 4 { + log.error("Invalid QOI image.\0") + return null + } + + surface := new_surface(width, height) + index := @as([Color; 64], idk) + + run := 0 + px := Color.(0, 0, 0, 255) + px_pos := 0 + + total_pixels := width * height + + loop if px_pos >= total_pixels break else { + if run > 0 { + run -= 1 + } else { + b1 := *qoi + qoi += 1 + + if b1 == QOI_OP_RGB { + px.r = *qoi + px.g = *(qoi + 1) + px.b = *(qoi + 2) + qoi += 3 + } else if b1 == QOI_OP_RGBA { + px.r = *qoi + px.g = *(qoi + 1) + px.b = *(qoi + 2) + px.a = *(qoi + 3) + qoi += 4 + } else if (b1 & QOI_MASK_2) == QOI_OP_INDEX { + px = index[b1 & 0x3F] + } else if (b1 & QOI_MASK_2) == QOI_OP_DIFF { + px.r = px.r + (b1 >> 4 & 0x3) - 2 & 0xFF + px.g = px.g + (b1 >> 2 & 0x3) - 2 & 0xFF + px.b = px.b + (b1 & 0x3) - 2 & 0xFF + } else if (b1 & QOI_MASK_2) == QOI_OP_LUMA { + b2 := *qoi + vg := (b1 & 0x3F) - 32 + + px.r = px.r + vg - 8 + (b2 >> 4 & 0xF) & 0xFF + px.g = px.g + vg & 0xFF + px.b = px.b + vg - 8 + (b2 & 0xF) & 0xFF + qoi += 1 + } else if (b1 & QOI_MASK_2) == QOI_OP_RUN { + run = b1 & 0x3F + } + + index[@inline(QOI_COLOR_HASH, px)] = px + }; + + *(surface.buf + px_pos) = px + + px_pos += 1 + } + + return surface +} \ No newline at end of file diff --git a/sysdata/libraries/render/src/lib.hb b/sysdata/libraries/render/src/lib.hb index 87e86c3..465a004 100644 --- a/sysdata/libraries/render/src/lib.hb +++ b/sysdata/libraries/render/src/lib.hb @@ -1,5 +1,5 @@ software := @use("software.hb") -image := @use("image.hb") +image := @use("image/lib.hb") text := @use("text.hb") // default mode diff --git a/sysdata/libraries/stn/src/math.hb b/sysdata/libraries/stn/src/math.hb index 9108919..9d0910d 100644 --- a/sysdata/libraries/stn/src/math.hb +++ b/sysdata/libraries/stn/src/math.hb @@ -30,30 +30,37 @@ Vec2 := fn($Expr: type): type { return struct {x: Expr, y: Expr} } -SIN_TABLE := @as([int; 91], @bitcast(@embed("./assets/sin_table"))) +/* source: + https://github.com/baker-Xie/FastMath/blob/master/include/fast_math.h */ -sin := fn(theta: int, amplitude: uint): int { - if theta < 0 { - theta += (-theta / 360 + 1) * 360 - } else if theta >= 360 { - theta -= theta / 360 * 360 - } +// ! heavily broken, possibly due to compiler bug... or skill issues - quadrant := theta / 90 - index := theta % 90 +PI := 3.14159265358979323846 - if @as(u8, @intcast(quadrant)) == @as(u8, 1) { - index = 90 - index - } +SIN_TABLE := [f32].(0.0, 0.02454122852291229, 0.04906767432741801, 0.07356456359966743, 0.0980171403295606, 0.1224106751992162, 0.1467304744553617, 0.1709618887603012, 0.1950903220161282, 0.2191012401568698, 0.2429801799032639, 0.2667127574748984, 0.2902846772544623, 0.3136817403988915, 0.3368898533922201, 0.3598950365349881, 0.3826834323650898, 0.4052413140049899, 0.4275550934302821, 0.4496113296546065, 0.4713967368259976, 0.492898192229784, 0.5141027441932217, 0.5349976198870972, 0.5555702330196022, 0.5758081914178453, 0.5956993044924334, 0.6152315905806268, 0.6343932841636455, 0.6531728429537768, 0.6715589548470183, 0.6895405447370668, 0.7071067811865475, 0.7242470829514669, 0.7409511253549591, 0.7572088465064845, 0.773010453362737, 0.7883464276266062, 0.8032075314806448, 0.8175848131515837, 0.8314696123025452, 0.844853565249707, 0.8577286100002721, 0.8700869911087113, 0.8819212643483549, 0.8932243011955153, 0.9039892931234433, 0.9142097557035307, 0.9238795325112867, 0.9329927988347388, 0.9415440651830208, 0.9495281805930367, 0.9569403357322089, 0.9637760657954398, 0.970031253194544, 0.9757021300385286, 0.9807852804032304, 0.9852776423889412, 0.989176509964781, 0.99247953459871, 0.9951847266721968, 0.9972904566786902, 0.9987954562051724, 0.9996988186962042, 1.0, 0.9996988186962042, 0.9987954562051724, 0.9972904566786902, 0.9951847266721969, 0.99247953459871, 0.989176509964781, 0.9852776423889412, 0.9807852804032304, 0.9757021300385286, 0.970031253194544, 0.9637760657954398, 0.9569403357322089, 0.9495281805930367, 0.9415440651830208, 0.9329927988347388, 0.9238795325112867, 0.9142097557035307, 0.9039892931234434, 0.8932243011955152, 0.881921264348355, 0.8700869911087115, 0.8577286100002721, 0.8448535652497072, 0.8314696123025455, 0.8175848131515837, 0.8032075314806449, 0.7883464276266063, 0.7730104533627371, 0.7572088465064847, 0.740951125354959, 0.7242470829514669, 0.7071067811865476, 0.6895405447370671, 0.6715589548470186, 0.6531728429537766, 0.6343932841636455, 0.6152315905806269, 0.5956993044924335, 0.5758081914178454, 0.5555702330196022, 0.5349976198870972, 0.5141027441932218, 0.4928981922297841, 0.4713967368259979, 0.4496113296546069, 0.427555093430282, 0.4052413140049899, 0.3826834323650899, 0.3598950365349883, 0.3368898533922203, 0.3136817403988914, 0.2902846772544624, 0.2667127574748985, 0.2429801799032641, 0.21910124015687, 0.1950903220161286, 0.1709618887603012, 0.1467304744553618, 0.1224106751992163, 0.09801714032956083, 0.07356456359966773, 0.04906767432741797, 0.02454122852291233, 0.0, -0.02454122852291208, -0.04906767432741772, -0.0735645635996675, -0.09801714032956059, -0.1224106751992161, -0.1467304744553616, -0.170961888760301, -0.1950903220161284, -0.2191012401568698, -0.2429801799032638, -0.2667127574748983, -0.2902846772544621, -0.3136817403988912, -0.3368898533922201, -0.3598950365349881, -0.3826834323650897, -0.4052413140049897, -0.4275550934302818, -0.4496113296546067, -0.4713967368259976, -0.4928981922297839, -0.5141027441932216, -0.5349976198870969, -0.555570233019602, -0.5758081914178453, -0.5956993044924332, -0.6152315905806267, -0.6343932841636453, -0.6531728429537765, -0.6715589548470184, -0.6895405447370668, -0.7071067811865475, -0.7242470829514668, -0.7409511253549589, -0.7572088465064842, -0.7730104533627367, -0.7883464276266059, -0.8032075314806451, -0.8175848131515838, -0.8314696123025452, -0.844853565249707, -0.857728610000272, -0.8700869911087113, -0.8819212643483549, -0.8932243011955152, -0.9039892931234431, -0.9142097557035305, -0.9238795325112865, -0.932992798834739, -0.9415440651830208, -0.9495281805930367, -0.9569403357322088, -0.9637760657954398, -0.970031253194544, -0.9757021300385285, -0.9807852804032303, -0.9852776423889411, -0.9891765099647809, -0.9924795345987101, -0.9951847266721969, -0.9972904566786902, -0.9987954562051724, -0.9996988186962042, -1.0, -0.9996988186962042, -0.9987954562051724, -0.9972904566786902, -0.9951847266721969, -0.9924795345987101, -0.9891765099647809, -0.9852776423889412, -0.9807852804032304, -0.9757021300385286, -0.970031253194544, -0.96377606579544, -0.9569403357322089, -0.9495281805930368, -0.9415440651830209, -0.9329927988347391, -0.9238795325112866, -0.9142097557035306, -0.9039892931234433, -0.8932243011955153, -0.881921264348355, -0.8700869911087115, -0.8577286100002722, -0.8448535652497072, -0.8314696123025455, -0.817584813151584, -0.8032075314806453, -0.7883464276266061, -0.7730104533627369, -0.7572088465064846, -0.7409511253549591, -0.724247082951467, -0.7071067811865477, -0.6895405447370672, -0.6715589548470187, -0.6531728429537771, -0.6343932841636459, -0.6152315905806274, -0.5956993044924332, -0.5758081914178452, -0.5555702330196022, -0.5349976198870973, -0.5141027441932219, -0.4928981922297843, -0.4713967368259979, -0.449611329654607, -0.4275550934302825, -0.4052413140049904, -0.3826834323650904, -0.359895036534988, -0.33688985339222, -0.3136817403988915, -0.2902846772544625, -0.2667127574748986, -0.2429801799032642, -0.2191012401568702, -0.1950903220161287, -0.1709618887603018, -0.1467304744553624, -0.122410675199216, -0.09801714032956051, -0.07356456359966741, -0.04906767432741809, -0.02454122852291245) +TAN_TABLE := [f32].(0.0, 0.01227246237956628, 0.02454862210892544, 0.03683218099484564, 0.04912684976946725, 0.06143635258159376, 0.07376443152244928, 0.08611485119762791, 0.09849140335716425, 0.110897911595913, 0.1233382361367387, 0.1358162787093877, 0.1483359875383474, 0.1609013624534892, 0.1735164601378558, 0.1861853995275837, 0.198912367379658, 0.2117016240239833, 0.2245575093171293, 0.2374844488160702, 0.2504869601913055, 0.263569659899918, 0.2767372701404143, 0.2899946261126061, 0.3033466836073424, 0.3167985269526038, 0.330355377344334, 0.3440226015924263, 0.3578057213145241, 0.3717104226127435, 0.3857425662711212, 0.3999081985145372, 0.414213562373095, 0.4286651096994995, 0.4432695138908643, 0.4580336833706724, 0.4729647758913199, 0.4880702137228629, 0.5033576997992942, 0.5188352348999757, 0.5345111359507916, 0.5503940555372639, 0.566493002730344, 0.5828173653349761, 0.5993769336819237, 0.616181926094866, 0.6332430161775691, 0.6505713620801533, 0.6681786379192989, 0.6860770675448629, 0.7042794608650442, 0.7227992529642059, 0.7416505462720354, 0.7608481560702512, 0.7804076596539435, 0.8003454494993202, 0.8206787908286602, 0.8414258840072547, 0.8626059322567399, 0.8842392152253498, 0.906347169019147, 0.9289524733703675, 0.9520791467009252, 0.9757526499323765, 0.9999999999999999, 1.024849894150227, 1.05033284623986, 1.076481336415266, 1.103329975733476, 1.130915687498827, 1.159277907333434, 1.188458804282966, 1.218503525587976, 1.249460468133579, 1.281381580036554, 1.31432269635108, 1.34834391348672, 1.383510007652874, 1.419890903494092, 1.457562200087105, 1.496605762665489, 1.537110389861882, 1.579172567960209, 1.622897325693455, 1.668399205583507, 1.715803370795664, 1.765246870094191, 1.816880087892402, 1.870868411789389, 1.927394156630064, 1.986658792343365, 2.04888553303075, 2.11432235754864, 2.183245547884151, 2.255963851929159, 2.33282340310135, 2.414213562373095, 2.500573890994256, 2.592402517738071, 2.690266237279613, 2.794812772490477, 2.906785761665535, 3.027043204317773, 3.156580333940787, 3.296558208938321, 3.448339762033025, 3.613535681307428, 3.7940634000883, 3.992223783770083, 4.210802033502797, 4.453202224414411, 4.723629327882301, 5.027339492125846, 5.370990435003726, 5.763142005118804, 6.21498777108904, 6.741452405414988, 7.362887641324242, 8.107785803676903, 9.017302360424724, 10.15317038760884, 11.61239886143525, 13.55666924235242, 16.27700795993539, 20.35546762498714, 27.15017066569958, 40.73548387208334, 81.48324020654604, 1633123935319537.0, -81.48324020654685, -40.73548387208354, -27.15017066569967, -20.35546762498719, -16.27700795993542, -13.55666924235244, -11.61239886143527, -10.15317038760886, -9.017302360424734, -8.10778580367691, -7.362887641324249, -6.741452405414994, -6.214987771089044, -5.763142005118809, -5.37099043500373, -5.02733949212585, -4.723629327882303, -4.453202224414413, -4.2108020335028, -3.992223783770084, -3.794063400088302, -3.61353568130743, -3.448339762033026, -3.296558208938323, -3.156580333940789, -3.027043204317775, -2.906785761665536, -2.794812772490478, -2.690266237279614, -2.592402517738072, -2.500573890994257, -2.414213562373095, -2.332823403101351, -2.25596385192916, -2.183245547884153, -2.114322357548642, -2.048885533030752, -1.986658792343365, -1.927394156630064, -1.870868411789389, -1.816880087892402, -1.765246870094192, -1.715803370795664, -1.668399205583508, -1.622897325693455, -1.57917256796021, -1.537110389861883, -1.49660576266549, -1.457562200087105, -1.419890903494092, -1.383510007652874, -1.34834391348672, -1.31432269635108, -1.281381580036555, -1.249460468133579, -1.218503525587977, -1.188458804282967, -1.159277907333435, -1.130915687498827, -1.103329975733476, -1.076481336415266, -1.05033284623986, -1.024849894150228, -1.0, -0.9757526499323768, -0.9520791467009256, -0.9289524733703679, -0.9063471690191476, -0.8842392152253504, -0.8626059322567398, -0.8414258840072547, -0.8206787908286604, -0.8003454494993202, -0.7804076596539438, -0.7608481560702515, -0.7416505462720356, -0.7227992529642062, -0.7042794608650446, -0.6860770675448633, -0.6681786379192988, -0.6505713620801532, -0.6332430161775691, -0.6161819260948661, -0.5993769336819238, -0.5828173653349762, -0.5664930027303442, -0.5503940555372643, -0.5345111359507919, -0.5188352348999761, -0.5033576997992947, -0.4880702137228627, -0.4729647758913199, -0.4580336833706724, -0.4432695138908644, -0.4286651096994996, -0.4142135623730952, -0.3999081985145373, -0.3857425662711215, -0.3717104226127437, -0.3578057213145244, -0.3440226015924267, -0.3303553773443338, -0.3167985269526037, -0.3033466836073424, -0.2899946261126062, -0.2767372701404144, -0.2635696598999182, -0.2504869601913056, -0.2374844488160704, -0.2245575093171296, -0.2117016240239837, -0.1989123673796584, -0.1861853995275837, -0.1735164601378557, -0.1609013624534892, -0.1483359875383475, -0.1358162787093878, -0.1233382361367388, -0.1108979115959132, -0.09849140335716448, -0.08611485119762818, -0.0737644315224496, -0.06143635258159368, -0.04912684976946721, -0.03683218099484564, -0.02454862210892548, -0.01227246237956636) +TABLE_SIZE := @as(i32, 256) - value := SIN_TABLE[@bitcast(index)] - if quadrant >= 2 { - value = -value - } - - return (value * @bitcast(amplitude) + 5000) / 10000 +sin := fn(theta: f32): f32 { + si := @fti(theta * 0.5 * @itf(TABLE_SIZE) / PI) + d := theta - @itf(si) * 2.0 * PI / @itf(TABLE_SIZE) + ci := si + TABLE_SIZE / 4 & TABLE_SIZE - 1 + si &= TABLE_SIZE - 1 + return SIN_TABLE[si] + (SIN_TABLE[ci] - 0.5 * SIN_TABLE[si] * d) * d } -cos := fn(theta: int, amplitude: uint): int { - return @inline(sin, theta + 90, amplitude) +cos := fn(theta: f32): f32 { + ci := @fti(theta * 0.5 * @itf(TABLE_SIZE) / PI) + d := theta - @itf(ci) * 2.0 * PI / @itf(TABLE_SIZE) + si := ci + TABLE_SIZE / 4 & TABLE_SIZE - 1 + ci &= TABLE_SIZE - 1 + return SIN_TABLE[si] + (SIN_TABLE[ci] - 0.5 * SIN_TABLE[si] * d) * d +} + +tan := fn(theta: f32): f32 { + a := @fti(theta * @itf(TABLE_SIZE) / PI) + d := theta - @itf(a) * PI / @itf(TABLE_SIZE) + d = d + 1.0 / 3.0 * d * d * d + a &= TABLE_SIZE - 1 + return (TAN_TABLE[a] + d) / (1.0 - TAN_TABLE[a] * d) } \ No newline at end of file diff --git a/sysdata/libraries/stn/src/string.hb b/sysdata/libraries/stn/src/string.hb index 71a8bcb..41a5535 100644 --- a/sysdata/libraries/stn/src/string.hb +++ b/sysdata/libraries/stn/src/string.hb @@ -67,4 +67,18 @@ reverse := fn(s: ^u8): void { j -= 1 } return +} + +equals := fn(lhs: ^u8, rhs: ^u8): bool { + if lhs == rhs { + return true + } + i := 0 + loop if *(lhs + i) != *(rhs + i) { + return false + } else if *lhs == 0 { + return true + } else { + i += 1 + } } \ No newline at end of file diff --git a/sysdata/programs/render_example/src/examples/assets/mini.bmp b/sysdata/programs/render_example/src/examples/assets/mini.bmp index 7e0b4ce..49cfcd2 100644 Binary files a/sysdata/programs/render_example/src/examples/assets/mini.bmp and b/sysdata/programs/render_example/src/examples/assets/mini.bmp differ diff --git a/sysdata/programs/render_example/src/examples/assets/mini.qoi b/sysdata/programs/render_example/src/examples/assets/mini.qoi new file mode 100644 index 0000000..3e42d02 Binary files /dev/null and b/sysdata/programs/render_example/src/examples/assets/mini.qoi differ diff --git a/sysdata/programs/render_example/src/examples/image.hb b/sysdata/programs/render_example/src/examples/image.hb index 49dafd0..392480c 100644 --- a/sysdata/programs/render_example/src/examples/image.hb +++ b/sysdata/programs/render_example/src/examples/image.hb @@ -1,38 +1,29 @@ -.{Vec2} := @use("../../../../libraries/stn/src/lib.hb").math +.{log} := @use("../../../../libraries/stn/src/lib.hb") render := @use("../../../../libraries/render/src/lib.hb") /* expected result: - a cute image bounces around the screen */ + a cute qoi image and a cute bmp image */ -bmp_1 := @embed("./assets/able.bmp") -bmp_2 := @embed("./assets/mini.bmp") +qoi := @embed("./assets/mini.qoi") +bmp := @embed("./assets/mini.bmp") example := fn(): void { - // strictly we should be null checking here but i am lazy - images := [render.Surface].( - @unwrap(render.image.surface_from_bmp(@bitcast(&bmp_1))), - @unwrap(render.image.surface_from_bmp(@bitcast(&bmp_2))), - ) screen := render.init(true) - vel := Vec2(int).(1, 1) - pos := Vec2(uint).(100, 100) - n := 0 - loop { - image := images[n] - render.put_surface(screen, image, pos, false) - render.sync(screen) - render.clear(screen, render.black) + image_qoi := render.image.from(@bitcast(&qoi)) + image_bmp := render.image.from(@bitcast(&bmp)) - if pos.x == 0 | pos.x == screen.width - image.width { - vel.x = -vel.x - n = 1 - n - } - if pos.y == 0 | pos.y == screen.height - image.height { - vel.y = -vel.y - n = 1 - n - } - - pos += @bitcast(vel) + if image_qoi == null { + log.error("failed to load qoi image for whatever reason\0") + return } + if image_bmp == null { + log.error("failed to load bmp image for whatever reason\0") + return + } + + render.clear(screen, render.black) + render.put_surface(screen, image_bmp, .((screen.width - image_bmp.width * 3) / 2, (screen.height - image_bmp.height) / 2), false) + render.put_surface(screen, image_qoi, .((screen.width + image_qoi.width) / 2, (screen.height - image_qoi.height) / 2), false) + render.sync(screen) return } \ No newline at end of file diff --git a/sysdata/programs/render_example/src/main.hb b/sysdata/programs/render_example/src/main.hb index 77cb04c..06644f7 100644 --- a/sysdata/programs/render_example/src/main.hb +++ b/sysdata/programs/render_example/src/main.hb @@ -1 +1 @@ -.{example: main} := @use("./examples/text.hb") \ No newline at end of file +.{example: main} := @use("./examples/image.hb") \ No newline at end of file