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 / meta_programming.md
Created September 29, 2026 11:12
Scrapped Meta Programming Chapter

Code generation: Meta programming without meta programming

Meta programming is writing code that generates more code, where that generated code becomes part of your program. Odin does not have any widespread support for meta programming built into the compiler. You can generate variations of procedures using parametric polymorphism. Other than that, there isn't much more in the way of compile-time code generation.

This is an explicit choice in the design of Odin. Meta programming makes it harder to read and debug programs. You very quickly get "dialects" of the language because people use meta programming in creative ways. Dialects were deemed to not be a good thing.

To achieve code generation, you can write a program that somehow outputs an Odin source file. You then run that generator before building your actual program. The generated code then becomes part of your program. Thanks to Odin being a simple language, this is actually feasible.

Let's look a

package ui_playground
import k2 "karl2d"
import "core:strings"
import "core:unicode/utf8"
COLOR_MAIN_BG :: k2.Color { 22, 22, 22, 255 }
COLOR_PANEL_BG :: k2.Color { 44, 44, 44, 255 }
COLOR_LINES :: k2.Color { 144, 144, 144, 255 }
COLOR_CONTROL_BG :: k2.Color { 44, 44, 44, 255 }
@karl-zylinski
karl-zylinski / odin_raylib_basic.odin
Last active June 25, 2025 15:50
For getting started with Odin + Raylib
/*
This is a minimal Odin + Raylib example.
Put this file in an empty folder, navigate to that folder using a teriminal
and then run this command:
odin run .
To see all the things available in Raylib, open `raylib.odin` and look around.
You'll find it here:
<odin_install_directory>/vendor/raylib/raylib.odin
@karl-zylinski
karl-zylinski / raylib_minimal.odin
Last active July 7, 2025 08:53
Minimal Odin + Raylib example.
// Minimal Odin + Raylib example that opens window and lets you control a
// rectangle using the arrow keys. Beginners can use this as a starting point.
//
// Copy this into a file called whatever_you_want.odin inside an empty folder.
// Use the command prompt to navigate to that folder and run:
// odin run .
// Note the period, with a space before it! This should run this minimal example.
package raylib_minimal
import rl "vendor:raylib"
@karl-zylinski
karl-zylinski / odin_book_table_of_contents.txt
Last active January 11, 2025 17:12
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 July 14, 2026 23:15
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 April 7, 2026 16:32
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 April 24, 2026 23:24
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