Skip to content

Instantly share code, notes, and snippets.

View karl-zylinski's full-sized avatar

Karl Zylinski karl-zylinski

View GitHub Profile
@karl-zylinski
karl-zylinski / odin_book_table_of_contents.txt
Last active November 22, 2024 18:14
Upcoming Odin book table of contents
1: Introduction
1.1: Why learn Odin?
1.2: Overview
1.3: Installing the Odin compiler
1.4: If you get stuck
1.5: Let's go!
2: Hellope! A tiny program
2.1: Compile and run the code
2.2: Line-by-line
@karl-zylinski
karl-zylinski / peek_at_next_rune.odin
Last active October 1, 2024 20:41
Example of how to look at next rune when iterating a string
package runefun
import fmt "core:fmt"
import "core:unicode/utf8"
main :: proc() {
str := "小猫咪"
// i is the byte index
for r, i in str {
@karl-zylinski
karl-zylinski / open_win32_window.odin
Last active September 3, 2024 22:25
Open a window using Windows API in Odin
package open_win32_window
import win "core:sys/windows"
main :: proc() {
instance := win.HINSTANCE(win.GetModuleHandleW(nil))
assert(instance != nil, "Failed to fetch current instance")
class_name := win.L("Windows Window")
cls := win.WNDCLASSW {
// extra things I add to hot reload script to make auto reload work
odin build file_version_builder
IF %ERRORLEVEL% NEQ 0 exit /b 1
file_version_builder.exe
IF %ERRORLEVEL% NEQ 0 exit /b 1
@karl-zylinski
karl-zylinski / rect_cut.odin
Last active September 12, 2024 23:28
For splitting up rectangles into smaller rectangles easily. Great for IMGUI layouting.
Rect :: struct {
x: f32,
y: f32,
width: f32,
height: f32,
}
cut_rect_top :: proc(r: ^Rect, y: f32, m: f32) -> Rect {
res := r^
res.y += m
@karl-zylinski
karl-zylinski / refresh_padded_tileset.odin
Created June 28, 2024 14:30
Takes a tileset and generates a new one with 1 px padding around all the tiles. The pixel at the border of the tile is copied to that border. This fixes subpixel camera interpolation glitches.
refresh_padded_tileset :: proc() {
if g_mem.tileset_padded.id != 0 {
rl.UnloadTexture(g_mem.tileset_padded)
g_mem.tileset_padded = {}
}
ts_source := load_image(.Tileset)
defer rl.UnloadImage(ts_source)
tileset_height := ts_source.height / TileHeight
{
"folders":
[
{
"path": ".",
},
],
"build_systems":
[
{
package game
import rl "vendor:raylib"
import "core:mem"
import "core:fmt"
import "core:encoding/json"
import "core:os"
Animation_Name :: enum {
Idle,
// A small test program of me trying Odin + Box2D + Raylib
// Made during this stream: https://www.youtube.com/watch?v=LYW7jdwEnaI
// I used these bindings https://github.com/cr1sth0fer/odin-box2d download them and put them in a sub-folder "box2d" next to this file.
// Then you can build using `odin run .`
package game
import b2 "box2d"
import rl "vendor:raylib"
@karl-zylinski
karl-zylinski / load_ttf_from_memory_premultiply_alpha.odin
Created April 15, 2024 11:28
Loading a TTF in Odin and multiply alpha into the gray component of the gray+alpha atlas
load_ttf_from_memory :: proc(file_data: []byte, font_size: int) -> rl.Font {
font := rl.Font {
baseSize = i32(font_size),
glyphCount = 95,
}
font.glyphs = rl.LoadFontData(&file_data[0], i32(len(file_data)), font.baseSize, {}, font.glyphCount, .DEFAULT)
if font.glyphs != nil {
font.glyphPadding = 4