Skip to content

Instantly share code, notes, and snippets.

View peterhellberg's full-sized avatar
💙
Coding Go

Peter Hellberg peterhellberg

💙
Coding Go
View GitHub Profile
@peterhellberg
peterhellberg / build.zig
Last active March 23, 2024 04:55
Example of using a current (as of 2023-12-01) version of Zig ⚡ to build a cart for the Gamercade fantasy console. https://gamercade.io
const std = @import("std");
pub fn build(b: *std.Build) void {
const exe = b.addExecutable(.{
.name = "cart",
.root_source_file = .{ .path = "src/main.zig" },
.target = .{ .cpu_arch = .wasm32, .os_tag = .wasi },
.optimize = .ReleaseSmall,
});
@peterhellberg
peterhellberg / .gitignore
Last active November 28, 2023 13:55
Compiling Zig to WASI, then calling it from Go using Wazero (https://ziglang.org, https://go.dev, https://wazero.io)
zig-cache
bin
@peterhellberg
peterhellberg / pp.zig
Created November 15, 2023 22:12
tic: Pixel position and color using the mouse
const tic = @import("tic");
fn pp(c: u8) void {
var m: tic.MouseData = .{};
tic.mouse(&m);
if (m.left) {
const x = m.x - 1;
const y = m.y - 1;
const tic = @import("tic80.zig");
const Rainbow = struct {
red: u8 = 255,
grn: u8 = 0,
blu: u8 = 0,
cyRG: bool = true,
cyGB: bool = false,
cyBR: bool = false,
const math = @import("std").math;
const tic = @import("tic80.zig");
export fn BDR(row: i32) void {
const urow: u8 = @intCast(row);
{ // skygradient
scanline(0x42, 0xFF -| urow, 0x99 +| urow);
}
const math = @import("std").math;
const tic = @import("tic80.zig");
fn drawCable(p1: [2]f32, p2: [2]f32, max_sag: f32, color: i32) [2]f32 {
const dx = p2[0] - p1[0];
const dy = p2[1] - p1[1];
const length = math.sqrt(dx * dx + dy * dy);
const sag = @max(length / max_sag, max_sag);
const midpoint: [2]f32 = .{
(p1[0] + p2[0]) / 2,
@peterhellberg
peterhellberg / uw8.zig
Created November 6, 2023 14:03
MicroW8 API for Zig ⚡
//
// MicroW8: https://exoticorn.github.io/microw8/docs/
// ┌───────────────────────────────────────────────────────────────────────────┐
// │ │
// │ Platform Constants │
// │ │
// └───────────────────────────────────────────────────────────────────────────┘
pub const SCREEN_WIDTH: u32 = 320;
@peterhellberg
peterhellberg / build.zig
Last active November 7, 2023 09:37
Updated build.zig to use with MicroW8 (requires Zig ⚡ 0.12.0-dev.1482+e74ced21b or later)
const std = @import("std");
pub fn build(b: *std.Build) void {
const exe = b.addExecutable(.{
.name = "cart",
.root_source_file = .{ .path = "src/main.zig" },
.target = .{
.cpu_arch = .wasm32,
.os_tag = .freestanding,
.cpu_features_add = std.Target.wasm.featureSet(&.{.nontrapping_fptoint}),
@peterhellberg
peterhellberg / minimal-htmx-todo.go
Last active December 28, 2023 18:48
A single Go file HTMX Todo list example, using no third party Go dependencies.
package main
import (
"context"
"crypto/rand"
"encoding/hex"
"fmt"
"html/template"
"net/http"
"os"
@peterhellberg
peterhellberg / pro-prisundersokning-graph.go
Created September 29, 2023 14:34
Using go-chart to generate a graph based on the PRO Prisundersökning data converted using https://gist.github.com/peterhellberg/ceed862a83c29a80b5bbd4631c24fb27
package main
import (
"encoding/json"
"fmt"
"os"
"strings"
_ "embed"