mirror of
https://github.com/Ed94/Odin.git
synced 2026-07-25 00:47:55 +00:00
Switchable array bounds checking
This commit is contained in:
@@ -0,0 +1,4 @@
|
||||
#load "runtime.odin"
|
||||
#load "win32.odin"
|
||||
#load "file.odin"
|
||||
#load "print.odin"
|
||||
@@ -0,0 +1,9 @@
|
||||
#load "basic.odin"
|
||||
|
||||
main :: proc() {
|
||||
str := "Hellope"
|
||||
|
||||
println(str, true, 6.28)
|
||||
|
||||
println([4]int{1, 2, 3, 4})
|
||||
}
|
||||
+106
@@ -0,0 +1,106 @@
|
||||
#load "win32.odin"
|
||||
|
||||
File :: type struct {
|
||||
Handle :: type HANDLE
|
||||
handle: Handle
|
||||
}
|
||||
|
||||
file_open :: proc(name: string) -> (File, bool) {
|
||||
buf: [300]byte
|
||||
_ = copy(buf[:], name as []byte)
|
||||
f := File{CreateFileA(^buf[0], FILE_GENERIC_READ, FILE_SHARE_READ, null, OPEN_EXISTING, 0, null)}
|
||||
success := f.handle != INVALID_HANDLE_VALUE as File.Handle
|
||||
|
||||
return f, success
|
||||
}
|
||||
|
||||
file_create :: proc(name: string) -> (File, bool) {
|
||||
buf: [300]byte
|
||||
_ = copy(buf[:], name as []byte)
|
||||
f := File{
|
||||
handle = CreateFileA(^buf[0], FILE_GENERIC_WRITE, FILE_SHARE_READ, null, CREATE_ALWAYS, 0, null),
|
||||
}
|
||||
success := f.handle != INVALID_HANDLE_VALUE as File.Handle
|
||||
return f, success
|
||||
}
|
||||
|
||||
|
||||
file_close :: proc(f: ^File) {
|
||||
CloseHandle(f.handle)
|
||||
}
|
||||
|
||||
file_write :: proc(f: ^File, buf: []byte) -> bool {
|
||||
bytes_written: i32
|
||||
return WriteFile(f.handle, ^buf[0], len(buf) as i32, ^bytes_written, null) != 0
|
||||
}
|
||||
|
||||
File_Standard :: type enum {
|
||||
INPUT,
|
||||
OUTPUT,
|
||||
ERROR,
|
||||
COUNT,
|
||||
}
|
||||
|
||||
__std_file_set := false;
|
||||
__std_files: [File_Standard.COUNT as int]File;
|
||||
|
||||
file_get_standard :: proc(std: File_Standard) -> ^File {
|
||||
// using File_Standard;
|
||||
if (!__std_file_set) {
|
||||
using File_Standard
|
||||
__std_files[INPUT] .handle = GetStdHandle(STD_INPUT_HANDLE)
|
||||
__std_files[OUTPUT].handle = GetStdHandle(STD_OUTPUT_HANDLE)
|
||||
__std_files[ERROR] .handle = GetStdHandle(STD_ERROR_HANDLE)
|
||||
__std_file_set = true
|
||||
}
|
||||
return ^__std_files[std]
|
||||
}
|
||||
|
||||
|
||||
read_entire_file :: proc(name: string) -> (string, bool) {
|
||||
buf: [300]byte
|
||||
_ = copy(buf[:], name as []byte)
|
||||
c_string := ^buf[0]
|
||||
|
||||
|
||||
f, file_ok := file_open(name)
|
||||
if !file_ok {
|
||||
return "", false
|
||||
}
|
||||
defer file_close(^f)
|
||||
|
||||
length: i64
|
||||
file_size_ok := GetFileSizeEx(f.handle as HANDLE, ^length) != 0
|
||||
if !file_size_ok {
|
||||
return "", false
|
||||
}
|
||||
|
||||
data := new_slice(u8, length)
|
||||
if ^data[0] == null {
|
||||
return "", false
|
||||
}
|
||||
|
||||
single_read_length: i32
|
||||
total_read: i64
|
||||
|
||||
for total_read < length {
|
||||
remaining := length - total_read
|
||||
to_read: u32
|
||||
MAX :: 0x7fffffff
|
||||
if remaining <= MAX {
|
||||
to_read = remaining as u32
|
||||
} else {
|
||||
to_read = MAX
|
||||
}
|
||||
|
||||
ReadFile(f.handle as HANDLE, ^data[total_read], to_read, ^single_read_length, null)
|
||||
if single_read_length <= 0 {
|
||||
delete(data)
|
||||
return "", false
|
||||
}
|
||||
|
||||
total_read += single_read_length as i64
|
||||
}
|
||||
|
||||
return data as string, true
|
||||
}
|
||||
+207
@@ -0,0 +1,207 @@
|
||||
#load "basic.odin"
|
||||
#load "opengl.odin"
|
||||
#load "math.odin"
|
||||
|
||||
TWO_HEARTS :: #rune "💕"
|
||||
|
||||
win32_perf_count_freq := GetQueryPerformanceFrequency()
|
||||
time_now :: proc() -> f64 {
|
||||
assert(win32_perf_count_freq != 0)
|
||||
|
||||
counter: i64
|
||||
_ = QueryPerformanceCounter(^counter)
|
||||
result := counter as f64 / win32_perf_count_freq as f64
|
||||
return result
|
||||
}
|
||||
win32_print_last_error :: proc() {
|
||||
err_code := GetLastError() as int
|
||||
if err_code != 0 {
|
||||
println("GetLastError:", err_code)
|
||||
}
|
||||
}
|
||||
|
||||
// Yuk!
|
||||
to_c_string :: proc(s: string) -> []u8 {
|
||||
c_str := new_slice(u8, len(s)+1)
|
||||
_ = copy(c_str, s as []byte)
|
||||
c_str[len(s)] = 0
|
||||
return c_str
|
||||
}
|
||||
|
||||
|
||||
Window :: struct {
|
||||
width, height: int
|
||||
wc: WNDCLASSEXA
|
||||
dc: HDC
|
||||
hwnd: HWND
|
||||
opengl_context, rc: HGLRC
|
||||
c_title: []u8
|
||||
}
|
||||
|
||||
make_window :: proc(title: string, msg, height: int, window_proc: WNDPROC) -> (Window, bool) {
|
||||
w: Window
|
||||
w.width, w.height = msg, height
|
||||
|
||||
class_name := "Win32-Odin-Window\x00"
|
||||
c_class_name := ^class_name[0]
|
||||
// w.c_title = to_c_string(title)
|
||||
w.c_title = "Title\x00" as []byte
|
||||
|
||||
instance := GetModuleHandleA(null)
|
||||
|
||||
w.wc = WNDCLASSEXA{
|
||||
size = size_of(WNDCLASSEXA) as u32,
|
||||
style = CS_VREDRAW | CS_HREDRAW,
|
||||
instance = instance as HINSTANCE,
|
||||
class_name = c_class_name,
|
||||
wnd_proc = window_proc,
|
||||
};
|
||||
|
||||
if RegisterClassExA(^w.wc) == 0 {
|
||||
win32_print_last_error( )
|
||||
return w, false
|
||||
}
|
||||
|
||||
w.hwnd = CreateWindowExA(0,
|
||||
c_class_name, ^w.c_title[0],
|
||||
WS_VISIBLE | WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX,
|
||||
CW_USEDEFAULT, CW_USEDEFAULT,
|
||||
w.width as i32, w.height as i32,
|
||||
null, null, instance, null)
|
||||
|
||||
if w.hwnd == null {
|
||||
win32_print_last_error()
|
||||
return w, false
|
||||
}
|
||||
|
||||
w.dc = GetDC(w.hwnd)
|
||||
|
||||
{
|
||||
pfd := PIXELFORMATDESCRIPTOR{
|
||||
size = size_of(PIXELFORMATDESCRIPTOR) as u32,
|
||||
version = 1,
|
||||
flags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER,
|
||||
pixel_type = PFD_TYPE_RGBA,
|
||||
color_bits = 32,
|
||||
alpha_bits = 8,
|
||||
depth_bits = 24,
|
||||
stencil_bits = 8,
|
||||
layer_type = PFD_MAIN_PLANE,
|
||||
}
|
||||
|
||||
SetPixelFormat(w.dc, ChoosePixelFormat(w.dc, ^pfd), null)
|
||||
w.opengl_context = wglCreateContext(w.dc)
|
||||
wglMakeCurrent(w.dc, w.opengl_context)
|
||||
|
||||
attribs := [8]i32{
|
||||
WGL_CONTEXT_MAJOR_VERSION_ARB, 2,
|
||||
WGL_CONTEXT_MINOR_VERSION_ARB, 1,
|
||||
WGL_CONTEXT_PROFILE_MASK_ARB, WGL_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB,
|
||||
0, // NOTE(bill): tells the proc that this is the end of attribs
|
||||
}
|
||||
|
||||
wgl_string := "wglCreateContextAttribsARB\x00"
|
||||
c_wgl_string := ^wgl_string[0]
|
||||
wglCreateContextAttribsARB := wglGetProcAddress(c_wgl_string) as wglCreateContextAttribsARBType
|
||||
w.rc = wglCreateContextAttribsARB(w.dc, 0, ^attribs[0])
|
||||
wglMakeCurrent(w.dc, w.rc)
|
||||
SwapBuffers(w.dc)
|
||||
}
|
||||
|
||||
return w, true
|
||||
}
|
||||
|
||||
destroy_window :: proc(w: ^Window) {
|
||||
delete(w.c_title)
|
||||
}
|
||||
|
||||
display_window :: proc(w: ^Window) {
|
||||
SwapBuffers(w.dc)
|
||||
}
|
||||
|
||||
|
||||
run_game :: proc() {
|
||||
win32_proc :: proc(hwnd: HWND, msg: u32, wparam: WPARAM, lparam: LPARAM) -> LRESULT #no_inline {
|
||||
if msg == WM_DESTROY || msg == WM_CLOSE || msg == WM_QUIT {
|
||||
ExitProcess(0)
|
||||
return 0
|
||||
}
|
||||
return DefWindowProcA(hwnd, msg, wparam, lparam)
|
||||
}
|
||||
|
||||
|
||||
window, window_success := make_window("Odin Language Demo", 854, 480, win32_proc)
|
||||
if !window_success {
|
||||
return
|
||||
}
|
||||
defer destroy_window(^window)
|
||||
|
||||
|
||||
prev_time := time_now()
|
||||
running := true
|
||||
|
||||
pos := Vec2{100, 100}
|
||||
|
||||
for running {
|
||||
curr_time := time_now()
|
||||
dt := (curr_time - prev_time) as f32
|
||||
prev_time = curr_time
|
||||
|
||||
msg: MSG
|
||||
for PeekMessageA(^msg, null, 0, 0, PM_REMOVE) > 0 {
|
||||
if msg.message == WM_QUIT {
|
||||
running = false
|
||||
}
|
||||
_ = TranslateMessage(^msg)
|
||||
_ = DispatchMessageA(^msg)
|
||||
}
|
||||
|
||||
if is_key_down(Key_Code.ESCAPE) {
|
||||
running = false
|
||||
}
|
||||
|
||||
{
|
||||
SPEED :: 500
|
||||
v: Vec2
|
||||
|
||||
if is_key_down(Key_Code.RIGHT) { v[0] += 1 }
|
||||
if is_key_down(Key_Code.LEFT) { v[0] -= 1 }
|
||||
if is_key_down(Key_Code.UP) { v[1] += 1 }
|
||||
if is_key_down(Key_Code.DOWN) { v[1] -= 1 }
|
||||
|
||||
v = vec2_norm0(v)
|
||||
|
||||
pos += v * Vec2{SPEED * dt}
|
||||
}
|
||||
|
||||
|
||||
glClearColor(0.5, 0.7, 1.0, 1.0)
|
||||
glClear(GL_COLOR_BUFFER_BIT)
|
||||
|
||||
glLoadIdentity()
|
||||
glOrtho(0, window.width as f64,
|
||||
0, window.height as f64, 0, 1)
|
||||
|
||||
draw_rect :: proc(x, y, w, h: f32) {
|
||||
glBegin(GL_TRIANGLES)
|
||||
|
||||
glColor3f(1, 0, 0); glVertex3f(x, y, 0)
|
||||
glColor3f(0, 1, 0); glVertex3f(x+w, y, 0)
|
||||
glColor3f(0, 0, 1); glVertex3f(x+w, y+h, 0)
|
||||
|
||||
glColor3f(0, 0, 1); glVertex3f(x+w, y+h, 0)
|
||||
glColor3f(1, 1, 0); glVertex3f(x, y+h, 0)
|
||||
glColor3f(1, 0, 0); glVertex3f(x, y, 0)
|
||||
|
||||
glEnd()
|
||||
}
|
||||
|
||||
draw_rect(pos[0], pos[1], 50, 50)
|
||||
|
||||
display_window(^window)
|
||||
ms_to_sleep := (16 - 1000*dt) as i32
|
||||
if ms_to_sleep > 0 {
|
||||
sleep_ms(ms_to_sleep)
|
||||
}
|
||||
}
|
||||
}
|
||||
+155
@@ -0,0 +1,155 @@
|
||||
MATH_TAU :: 6.28318530717958647692528676655900576
|
||||
MATH_PI :: 3.14159265358979323846264338327950288
|
||||
MATH_ONE_OVER_TAU :: 0.636619772367581343075535053490057448
|
||||
MATH_ONE_OVER_PI :: 0.159154943091895335768883763372514362
|
||||
|
||||
MATH_E :: 2.71828182845904523536
|
||||
MATH_SQRT_TWO :: 1.41421356237309504880168872420969808
|
||||
MATH_SQRT_THREE :: 1.73205080756887729352744634150587236
|
||||
MATH_SQRT_FIVE :: 2.23606797749978969640917366873127623
|
||||
|
||||
MATH_LOG_TWO :: 0.693147180559945309417232121458176568
|
||||
MATH_LOG_TEN :: 2.30258509299404568401799145468436421
|
||||
|
||||
MATH_EPSILON :: 1.19209290e-7
|
||||
|
||||
τ :: MATH_TAU
|
||||
π :: MATH_PI
|
||||
|
||||
Vec2 :: type {2}f32
|
||||
Vec3 :: type {3}f32
|
||||
Vec4 :: type {4}f32
|
||||
|
||||
Mat2 :: type {4}f32
|
||||
Mat3 :: type {9}f32
|
||||
Mat4 :: type {16}f32
|
||||
|
||||
|
||||
fsqrt :: proc(x: f32) -> f32 #foreign "llvm.sqrt.f32"
|
||||
fsin :: proc(x: f32) -> f32 #foreign "llvm.sin.f32"
|
||||
fcos :: proc(x: f32) -> f32 #foreign "llvm.cos.f32"
|
||||
flerp :: proc(a, b, t: f32) -> f32 { return a*(1-t) + b*t }
|
||||
fclamp :: proc(x, lower, upper: f32) -> f32 { return min(max(x, lower), upper) }
|
||||
fclamp01 :: proc(x: f32) -> f32 { return fclamp(x, 0, 1) }
|
||||
fsign :: proc(x: f32) -> f32 { if x >= 0 { return +1 } return -1 }
|
||||
|
||||
copy_sign :: proc(x, y: f32) -> f32 {
|
||||
ix := x transmute u32
|
||||
iy := y transmute u32
|
||||
ix &= 0x7fffffff
|
||||
ix |= iy & 0x80000000
|
||||
return ix transmute f32
|
||||
}
|
||||
|
||||
|
||||
round :: proc(x: f32) -> f32 {
|
||||
if x >= 0 {
|
||||
return floor(x + 0.5)
|
||||
}
|
||||
return ceil(x - 0.5)
|
||||
}
|
||||
floor :: proc(x: f32) -> f32 {
|
||||
if x >= 0 {
|
||||
return x as int as f32
|
||||
}
|
||||
return (x-0.5) as int as f32
|
||||
}
|
||||
ceil :: proc(x: f32) -> f32 {
|
||||
if x < 0 {
|
||||
return x as int as f32
|
||||
}
|
||||
return ((x as int)+1) as f32
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
remainder :: proc(x, y: f32) -> f32 {
|
||||
return x - round(x/y) * y
|
||||
}
|
||||
|
||||
fmod :: proc(x, y: f32) -> f32 {
|
||||
y = abs(y)
|
||||
result := remainder(abs(x), y)
|
||||
if fsign(result) < 0 {
|
||||
result += y
|
||||
}
|
||||
return copy_sign(result, x)
|
||||
}
|
||||
|
||||
|
||||
to_radians :: proc(degrees: f32) -> f32 { return degrees * MATH_TAU / 360 }
|
||||
to_degrees :: proc(radians: f32) -> f32 { return radians * 360 / MATH_TAU }
|
||||
|
||||
|
||||
|
||||
|
||||
dot2 :: proc(a, b: Vec2) -> f32 { c := a*b; return c[0] + c[1] }
|
||||
dot3 :: proc(a, b: Vec3) -> f32 { c := a*b; return c[0] + c[1] + c[2] }
|
||||
dot4 :: proc(a, b: Vec4) -> f32 { c := a*b; return c[0] + c[1] + c[2] + c[3] }
|
||||
|
||||
cross :: proc(x, y: Vec3) -> Vec3 {
|
||||
a := swizzle(x, 1, 2, 0) * swizzle(y, 2, 0, 1)
|
||||
b := swizzle(x, 2, 0, 1) * swizzle(y, 1, 2, 0)
|
||||
return a - b
|
||||
}
|
||||
|
||||
|
||||
vec2_mag :: proc(v: Vec2) -> f32 { return fsqrt(dot2(v, v)) }
|
||||
vec3_mag :: proc(v: Vec3) -> f32 { return fsqrt(dot3(v, v)) }
|
||||
vec4_mag :: proc(v: Vec4) -> f32 { return fsqrt(dot4(v, v)) }
|
||||
|
||||
vec2_norm :: proc(v: Vec2) -> Vec2 { return v / Vec2{vec2_mag(v)} }
|
||||
vec3_norm :: proc(v: Vec3) -> Vec3 { return v / Vec3{vec3_mag(v)} }
|
||||
vec4_norm :: proc(v: Vec4) -> Vec4 { return v / Vec4{vec4_mag(v)} }
|
||||
|
||||
vec2_norm0 :: proc(v: Vec2) -> Vec2 {
|
||||
m := vec2_mag(v)
|
||||
if m == 0 {
|
||||
return Vec2{0}
|
||||
}
|
||||
return v / Vec2{m}
|
||||
}
|
||||
|
||||
vec3_norm0 :: proc(v: Vec3) -> Vec3 {
|
||||
m := vec3_mag(v)
|
||||
if m == 0 {
|
||||
return Vec3{0}
|
||||
}
|
||||
return v / Vec3{m}
|
||||
}
|
||||
|
||||
vec4_norm0 :: proc(v: Vec4) -> Vec4 {
|
||||
m := vec4_mag(v)
|
||||
if m == 0 {
|
||||
return Vec4{0}
|
||||
}
|
||||
return v / Vec4{m}
|
||||
}
|
||||
|
||||
|
||||
F32_DIG :: 6
|
||||
F32_EPSILON :: 1.192092896e-07
|
||||
F32_GUARD :: 0
|
||||
F32_MANT_DIG :: 24
|
||||
F32_MAX :: 3.402823466e+38
|
||||
F32_MAX_10_EXP :: 38
|
||||
F32_MAX_EXP :: 128
|
||||
F32_MIN :: 1.175494351e-38
|
||||
F32_MIN_10_EXP :: -37
|
||||
F32_MIN_EXP :: -125
|
||||
F32_NORMALIZE :: 0
|
||||
F32_RADIX :: 2
|
||||
F32_ROUNDS :: 1
|
||||
|
||||
F64_DIG :: 15 // # of decimal digits of precision
|
||||
F64_EPSILON :: 2.2204460492503131e-016 // smallest such that 1.0+F64_EPSILON != 1.0
|
||||
F64_MANT_DIG :: 53 // # of bits in mantissa
|
||||
F64_MAX :: 1.7976931348623158e+308 // max value
|
||||
F64_MAX_10_EXP :: 308 // max decimal exponent
|
||||
F64_MAX_EXP :: 1024 // max binary exponent
|
||||
F64_MIN :: 2.2250738585072014e-308 // min positive value
|
||||
F64_MIN_10_EXP :: -307 // min decimal exponent
|
||||
F64_MIN_EXP :: -1021 // min binary exponent
|
||||
F64_RADIX :: 2 // exponent radix
|
||||
F64_ROUNDS :: 1 // addition rounding: near
|
||||
@@ -0,0 +1,900 @@
|
||||
// Demo 002
|
||||
#load "basic.odin"
|
||||
#load "math.odin"
|
||||
// #load "game.odin"
|
||||
|
||||
#thread_local tls_int: int
|
||||
|
||||
main :: proc() {
|
||||
// Forenotes
|
||||
|
||||
// Semicolons are now optional
|
||||
// Rule for when a semicolon is expected after a statement
|
||||
// - If the next token is not on the same line
|
||||
// - if the next token is a closing brace }
|
||||
// - Otherwise, a semicolon is needed
|
||||
//
|
||||
// Expections:
|
||||
// for, if, match
|
||||
// if x := thing(); x < 123 {}
|
||||
// for i := 0; i < 123; i++ {}
|
||||
|
||||
// Q: Should I use the new rule or go back to the old one without optional semicolons?
|
||||
|
||||
|
||||
// #thread_local - see runtime.odin and above at `tls_int`
|
||||
// #foreign_system_library - see win32.odin
|
||||
|
||||
// struct_compound_literals()
|
||||
// enumerations()
|
||||
// variadic_procedures()
|
||||
// new_builtins()
|
||||
// match_statement()
|
||||
// namespacing()
|
||||
// subtyping()
|
||||
// tagged_unions()
|
||||
}
|
||||
|
||||
struct_compound_literals :: proc() {
|
||||
Thing :: type struct {
|
||||
id: int
|
||||
x: f32
|
||||
name: string
|
||||
}
|
||||
{
|
||||
t1: Thing
|
||||
t1.id = 1
|
||||
|
||||
t3 := Thing{}
|
||||
t4 := Thing{1, 2, "Fred"}
|
||||
// t5 := Thing{1, 2}
|
||||
|
||||
t6 := Thing{
|
||||
name = "Tom",
|
||||
x = 23,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
enumerations :: proc() {
|
||||
{
|
||||
Fruit :: type enum {
|
||||
APPLE, // 0
|
||||
BANANA, // 1
|
||||
PEAR, // 2
|
||||
}
|
||||
|
||||
f := Fruit.APPLE
|
||||
// g12: int = Fruit.BANANA
|
||||
g: int = Fruit.BANANA as int
|
||||
// However, you can use enums are index values as _any_ integer allowed
|
||||
}
|
||||
{
|
||||
Fruit1 :: type enum int {
|
||||
APPLE,
|
||||
BANANA,
|
||||
PEAR,
|
||||
}
|
||||
|
||||
Fruit2 :: type enum u8 {
|
||||
APPLE,
|
||||
BANANA,
|
||||
PEAR,
|
||||
}
|
||||
|
||||
Fruit3 :: type enum u8 {
|
||||
APPLE = 1,
|
||||
BANANA, // 2
|
||||
PEAR = 5,
|
||||
TOMATO, // 6
|
||||
}
|
||||
}
|
||||
|
||||
// Q: remove the need for `type` if it's a record (struct/enum/raw_union/union)?
|
||||
}
|
||||
|
||||
variadic_procedures :: proc() {
|
||||
print_ints :: proc(args: ..int) {
|
||||
for i := 0; i < len(args); i++ {
|
||||
if i > 0 {
|
||||
print_string(", ")
|
||||
}
|
||||
print_int(args[i])
|
||||
}
|
||||
}
|
||||
|
||||
print_ints(); // nl()
|
||||
print_ints(1); nl()
|
||||
print_ints(1, 2, 3); nl()
|
||||
|
||||
print_prefix_f32s :: proc(prefix: string, args: ..f32) {
|
||||
print_string(prefix)
|
||||
print_string(": ")
|
||||
for i := 0; i < len(args); i++ {
|
||||
if i > 0 {
|
||||
print_string(", ")
|
||||
}
|
||||
print_f32(args[i])
|
||||
}
|
||||
}
|
||||
|
||||
print_prefix_f32s("a"); nl()
|
||||
print_prefix_f32s("b", 1); nl()
|
||||
print_prefix_f32s("c", 1, 2, 3); nl()
|
||||
|
||||
// Internally, the variadic procedures get allocated to an array on the stack,
|
||||
// and this array is passed a slice
|
||||
|
||||
// This is first step for a `print` procedure but I do not have an `any` type
|
||||
// yet as this requires a few other things first - i.e. introspection
|
||||
|
||||
// NOTE(bill): I haven't yet added the feature of expanding a slice or array into
|
||||
// a variadic a parameter but it's pretty trivial to add
|
||||
}
|
||||
|
||||
new_builtins :: proc() {
|
||||
{
|
||||
a := new(int)
|
||||
b := new_slice(int, 12)
|
||||
c := new_slice(int, 12, 16)
|
||||
|
||||
defer delete(a)
|
||||
defer delete(b)
|
||||
defer delete(c)
|
||||
|
||||
// NOTE(bill): These use the current context's allocator not the default allocator
|
||||
// see runtime.odin
|
||||
|
||||
// Q: Should this be `free` rather than `delete` and should I overload it for slices too?
|
||||
|
||||
{
|
||||
prev_context := context
|
||||
defer context = prev_context
|
||||
// Q: Should I add a `push_context` feature to the language?
|
||||
|
||||
context.allocator = __default_allocator()
|
||||
|
||||
a := new(int)
|
||||
defer delete(a)
|
||||
|
||||
// Do whatever
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
a: int = 123
|
||||
b: type_of_val(a) = 321
|
||||
|
||||
// NOTE(bill): This matches the current naming scheme
|
||||
// size_of
|
||||
// align_of
|
||||
// offset_of
|
||||
//
|
||||
// size_of_val
|
||||
// align_of_val
|
||||
// offset_of_val
|
||||
// type_of_val
|
||||
}
|
||||
|
||||
{
|
||||
// Compile time assert
|
||||
COND :: true
|
||||
compile_assert(COND)
|
||||
// compile_assert(!COND)
|
||||
|
||||
// Runtime assert
|
||||
x := true
|
||||
assert(x)
|
||||
// assert(!x)
|
||||
}
|
||||
|
||||
{
|
||||
x: ^u32 = null;
|
||||
y := ptr_offset(x, 100)
|
||||
z := ptr_sub(y, x)
|
||||
w := slice_ptr(x, 12)
|
||||
t := slice_ptr(x, 12, 16)
|
||||
|
||||
// NOTE(bill): These are here because I've removed:
|
||||
// pointer arithmetic
|
||||
// pointer indexing
|
||||
// pointer slicing
|
||||
|
||||
// Reason
|
||||
|
||||
a: [16]int
|
||||
a[1] = 1;
|
||||
b := ^a
|
||||
// Auto pointer deref
|
||||
// consistent with record members
|
||||
assert(b[1] == 1)
|
||||
|
||||
// Q: Should I add them back in at the cost of inconsitency?
|
||||
}
|
||||
|
||||
{
|
||||
a, b := -1, 2
|
||||
print_int(min(a, b)); nl()
|
||||
print_int(max(a, b)); nl()
|
||||
print_int(abs(a)); nl()
|
||||
|
||||
// These work at compile time too
|
||||
A :: -1
|
||||
B :: 2
|
||||
C :: min(A, B)
|
||||
D :: max(A, B)
|
||||
E :: abs(A)
|
||||
|
||||
print_int(C); nl()
|
||||
print_int(D); nl()
|
||||
print_int(E); nl()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
match_statement :: proc() {
|
||||
// NOTE(bill): `match` statements are similar to `switch` statements
|
||||
// in other languages but there are few differences
|
||||
|
||||
{
|
||||
match x := 5; x {
|
||||
case 1: // cases must be constant expression
|
||||
print_string("1!\n")
|
||||
// break by default
|
||||
|
||||
case 2:
|
||||
s := "2!\n"; // Each case has its own scope
|
||||
print_string(s)
|
||||
break // explicit break
|
||||
|
||||
case 3, 4: // multiple cases
|
||||
print_string("3 or 4!\n")
|
||||
|
||||
case 5:
|
||||
print_string("5!\n")
|
||||
fallthrough // explicit fallthrough
|
||||
|
||||
default:
|
||||
print_string("default!\n")
|
||||
}
|
||||
|
||||
|
||||
|
||||
match x := 1.5; x {
|
||||
case 1.5:
|
||||
print_string("1.5!\n")
|
||||
// break by default
|
||||
case MATH_TAU:
|
||||
print_string("τ!\n")
|
||||
default:
|
||||
print_string("default!\n")
|
||||
}
|
||||
|
||||
|
||||
|
||||
match x := "Hello"; x {
|
||||
case "Hello":
|
||||
print_string("greeting\n")
|
||||
// break by default
|
||||
case "Goodbye":
|
||||
print_string("farewell\n")
|
||||
default:
|
||||
print_string("???\n")
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
a := 53
|
||||
match {
|
||||
case a == 1:
|
||||
print_string("one\n")
|
||||
case a == 2:
|
||||
print_string("a couple\n")
|
||||
case a < 7, a == 7:
|
||||
print_string("a few\n")
|
||||
case a < 12: // intentional bug
|
||||
print_string("several\n")
|
||||
case a >= 12 && a < 100:
|
||||
print_string("dozens\n")
|
||||
case a >= 100 && a < 1000:
|
||||
print_string("hundreds\n")
|
||||
default:
|
||||
print_string("a fuck ton\n")
|
||||
}
|
||||
|
||||
// Identical to this
|
||||
|
||||
b := 53
|
||||
if b == 1 {
|
||||
print_string("one\n")
|
||||
} else if b == 2 {
|
||||
print_string("a couple\n")
|
||||
} else if b < 7 || b == 7 {
|
||||
print_string("a few\n")
|
||||
} else if b < 12 { // intentional bug
|
||||
print_string("several\n")
|
||||
} else if b >= 12 && b < 100 {
|
||||
print_string("dozens\n")
|
||||
} else if b >= 100 && b < 1000 {
|
||||
print_string("hundreds\n")
|
||||
} else {
|
||||
print_string("a fuck ton\n")
|
||||
}
|
||||
|
||||
// However, match statements allow for `break` and `fallthrough` unlike
|
||||
// an if statement
|
||||
}
|
||||
}
|
||||
|
||||
Vector3 :: type struct {
|
||||
x, y, z: f32
|
||||
}
|
||||
|
||||
print_floats :: proc(args: ..f32) {
|
||||
for i := 0; i < len(args); i++ {
|
||||
if i > 0 {
|
||||
print_string(", ")
|
||||
}
|
||||
print_f32(args[i])
|
||||
}
|
||||
print_nl()
|
||||
}
|
||||
|
||||
namespacing :: proc() {
|
||||
{
|
||||
Thing :: type struct {
|
||||
x: f32
|
||||
name: string
|
||||
}
|
||||
|
||||
a: Thing
|
||||
a.x = 3
|
||||
{
|
||||
Thing :: type struct {
|
||||
y: int
|
||||
test: bool
|
||||
}
|
||||
|
||||
b: Thing // Uses this scope's Thing
|
||||
b.test = true
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
Entity :: type struct {
|
||||
Guid :: type int
|
||||
Nested :: type struct {
|
||||
MyInt :: type int
|
||||
i: int
|
||||
}
|
||||
|
||||
CONSTANT :: 123
|
||||
|
||||
|
||||
guid: Guid
|
||||
name: string
|
||||
pos: Vector3
|
||||
vel: Vector3
|
||||
nested: Nested
|
||||
}
|
||||
|
||||
guid: Entity.Guid = Entity.CONSTANT
|
||||
i: Entity.Nested.MyInt
|
||||
|
||||
|
||||
|
||||
{
|
||||
using Entity
|
||||
guid: Guid = CONSTANT
|
||||
using Nested
|
||||
i: MyInt
|
||||
}
|
||||
|
||||
|
||||
{
|
||||
using Entity.Nested
|
||||
guid: Entity.Guid = Entity.CONSTANT
|
||||
i: MyInt
|
||||
}
|
||||
|
||||
|
||||
{
|
||||
e: Entity
|
||||
using e
|
||||
guid = 27832
|
||||
name = "Bob"
|
||||
|
||||
print_int(e.guid as int); nl()
|
||||
print_string(e.name); nl()
|
||||
}
|
||||
|
||||
{
|
||||
using e: Entity
|
||||
guid = 78456
|
||||
name = "Thing"
|
||||
|
||||
print_int(e.guid as int); nl()
|
||||
print_string(e.name); nl()
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
Entity :: type struct {
|
||||
Guid :: type int
|
||||
Nested :: type struct {
|
||||
MyInt :: type int
|
||||
i: int
|
||||
}
|
||||
|
||||
CONSTANT :: 123
|
||||
|
||||
|
||||
guid: Guid
|
||||
name: string
|
||||
using pos: Vector3
|
||||
vel: Vector3
|
||||
using nested: ^Nested
|
||||
}
|
||||
|
||||
e := Entity{nested = new(Entity.Nested)}
|
||||
e.x = 123
|
||||
e.i = Entity.CONSTANT
|
||||
}
|
||||
|
||||
|
||||
|
||||
{
|
||||
Entity :: type struct {
|
||||
position: Vector3
|
||||
}
|
||||
|
||||
print_pos_1 :: proc(entity: ^Entity) {
|
||||
print_string("print_pos_1: ")
|
||||
print_floats(entity.position.x, entity.position.y, entity.position.z)
|
||||
}
|
||||
|
||||
print_pos_2 :: proc(entity: ^Entity) {
|
||||
using entity
|
||||
print_string("print_pos_2: ")
|
||||
print_floats(position.x, position.y, position.z)
|
||||
}
|
||||
|
||||
print_pos_3 :: proc(using entity: ^Entity) {
|
||||
print_string("print_pos_3: ")
|
||||
print_floats(position.x, position.y, position.z)
|
||||
}
|
||||
|
||||
print_pos_4 :: proc(using entity: ^Entity) {
|
||||
using position
|
||||
print_string("print_pos_4: ")
|
||||
print_floats(x, y, z)
|
||||
}
|
||||
|
||||
e := Entity{position = Vector3{1, 2, 3}}
|
||||
print_pos_1(^e)
|
||||
print_pos_2(^e)
|
||||
print_pos_3(^e)
|
||||
print_pos_4(^e)
|
||||
|
||||
// This is similar to C++'s `this` pointer that is implicit and only available in methods
|
||||
}
|
||||
}
|
||||
|
||||
subtyping :: proc() {
|
||||
{
|
||||
// C way for subtyping/subclassing
|
||||
|
||||
Entity :: type struct {
|
||||
position: Vector3
|
||||
}
|
||||
|
||||
Frog :: type struct {
|
||||
entity: Entity
|
||||
jump_height: f32
|
||||
}
|
||||
|
||||
f: Frog
|
||||
f.entity.position = Vector3{1, 2, 3}
|
||||
|
||||
using f.entity
|
||||
position = Vector3{1, 2, 3}
|
||||
|
||||
}
|
||||
|
||||
{
|
||||
// C++ way for subtyping/subclassing
|
||||
|
||||
Entity :: type struct {
|
||||
position: Vector3
|
||||
}
|
||||
|
||||
Frog :: type struct {
|
||||
using entity: Entity
|
||||
jump_height: f32
|
||||
}
|
||||
|
||||
f: Frog
|
||||
f.position = Vector3{1, 2, 3}
|
||||
|
||||
|
||||
print_pos :: proc(using entity: Entity) {
|
||||
print_string("print_pos: ")
|
||||
print_floats(position.x, position.y, position.z)
|
||||
}
|
||||
|
||||
print_pos(f.entity)
|
||||
print_pos(f)
|
||||
|
||||
// Subtype Polymorphism
|
||||
}
|
||||
|
||||
{
|
||||
// More than C++ way for subtyping/subclassing
|
||||
|
||||
Entity :: type struct {
|
||||
position: Vector3
|
||||
}
|
||||
|
||||
Frog :: type struct {
|
||||
jump_height: f32
|
||||
using entity: ^Entity // Doesn't have to be first member!
|
||||
}
|
||||
|
||||
f: Frog
|
||||
f.entity = new(Entity)
|
||||
f.position = Vector3{1, 2, 3}
|
||||
|
||||
|
||||
print_pos :: proc(using entity: ^Entity) {
|
||||
print_string("print_pos: ")
|
||||
print_floats(position.x, position.y, position.z)
|
||||
}
|
||||
|
||||
print_pos(f.entity)
|
||||
print_pos(^f)
|
||||
print_pos(f)
|
||||
}
|
||||
|
||||
{
|
||||
// More efficient subtyping
|
||||
|
||||
Entity :: type struct {
|
||||
position: Vector3
|
||||
}
|
||||
|
||||
Frog :: type struct {
|
||||
jump_height: f32
|
||||
using entity: ^Entity
|
||||
}
|
||||
|
||||
MAX_ENTITES :: 64
|
||||
entities: [MAX_ENTITES]Entity
|
||||
entity_count := 0
|
||||
|
||||
next_entity :: proc(entities: []Entity, entity_count: ^int) -> ^Entity {
|
||||
e := ^entities[entity_count^]
|
||||
entity_count^++
|
||||
return e
|
||||
}
|
||||
|
||||
f: Frog
|
||||
f.entity = next_entity(entities[:], ^entity_count)
|
||||
f.position = Vector3{3, 4, 6}
|
||||
|
||||
using f.position
|
||||
print_floats(x, y, z)
|
||||
}
|
||||
|
||||
{
|
||||
// Down casting
|
||||
|
||||
Entity :: type struct {
|
||||
position: Vector3
|
||||
}
|
||||
|
||||
Frog :: type struct {
|
||||
jump_height: f32
|
||||
using entity: Entity
|
||||
}
|
||||
|
||||
f: Frog
|
||||
f.jump_height = 564
|
||||
e := ^f.entity
|
||||
|
||||
frog := e down_cast ^Frog
|
||||
print_string("down_cast: ")
|
||||
print_f32(frog.jump_height); nl()
|
||||
|
||||
// NOTE(bill): `down_cast` is unsafe and there are not check are compile time or run time
|
||||
// Q: Should I completely remove `down_cast` as I added it in about 30 minutes
|
||||
}
|
||||
|
||||
{
|
||||
// Multiple "inheritance"/subclassing
|
||||
|
||||
Entity :: type struct {
|
||||
position: Vector3
|
||||
}
|
||||
Climber :: type struct {
|
||||
speed: f32
|
||||
}
|
||||
|
||||
Frog :: type struct {
|
||||
using entity: Entity
|
||||
using climber: Climber
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
tagged_unions :: proc() {
|
||||
{
|
||||
EntityKind :: type enum {
|
||||
INVALID,
|
||||
FROG,
|
||||
GIRAFFE,
|
||||
HELICOPTER,
|
||||
}
|
||||
|
||||
Entity :: type struct {
|
||||
kind: EntityKind
|
||||
using data: raw_union {
|
||||
frog: struct {
|
||||
jump_height: f32
|
||||
colour: u32
|
||||
}
|
||||
giraffe: struct {
|
||||
neck_length: f32
|
||||
spot_count: int
|
||||
}
|
||||
helicopter: struct {
|
||||
blade_count: int
|
||||
weight: f32
|
||||
pilot_name: string
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
e: Entity
|
||||
e.kind = EntityKind.FROG
|
||||
e.frog.jump_height = 12
|
||||
|
||||
f: type_of_val(e.frog);
|
||||
|
||||
// But this is very unsafe and extremely cumbersome to write
|
||||
// In C++, I use macros to alleviate this but it's not a solution
|
||||
}
|
||||
|
||||
{
|
||||
Entity :: type union {
|
||||
Frog: struct {
|
||||
jump_height: f32
|
||||
colour: u32
|
||||
}
|
||||
Giraffe: struct {
|
||||
neck_length: f32
|
||||
spot_count: int
|
||||
}
|
||||
Helicopter: struct {
|
||||
blade_count: int
|
||||
weight: f32
|
||||
pilot_name: string
|
||||
}
|
||||
}
|
||||
|
||||
using Entity
|
||||
f1: Frog = Frog{12, 0xff9900}
|
||||
f2: Entity = Frog{12, 0xff9900} // Implicit cast
|
||||
f3 := Frog{12, 0xff9900} as Entity // Explicit cast
|
||||
|
||||
// f3.Frog.jump_height = 12 // There are "members" of a union
|
||||
|
||||
|
||||
|
||||
e, f, g, h: Entity
|
||||
f = Frog{12, 0xff9900}
|
||||
g = Giraffe{2.1, 23}
|
||||
h = Helicopter{4, 1000, "Frank"}
|
||||
|
||||
|
||||
|
||||
|
||||
// Requires a pointer to the union
|
||||
// `x` will be a pointer to type of the case
|
||||
|
||||
match type x : ^f {
|
||||
case Frog:
|
||||
print_string("Frog!\n")
|
||||
print_f32(x.jump_height); nl()
|
||||
x.jump_height = 3
|
||||
print_f32(x.jump_height); nl()
|
||||
case Giraffe:
|
||||
print_string("Giraffe!\n")
|
||||
case Helicopter:
|
||||
print_string("ROFLCOPTER!\n")
|
||||
default:
|
||||
print_string("invalid entity\n")
|
||||
}
|
||||
|
||||
|
||||
// Q: Allow for a non pointer version with takes a copy instead?
|
||||
// Or it takes the pointer the data and not a copy
|
||||
|
||||
|
||||
fp := ^f as ^Frog // Unsafe
|
||||
print_f32(fp.jump_height); nl()
|
||||
|
||||
|
||||
// Internals of a tagged union
|
||||
/*
|
||||
struct {
|
||||
data: [size_of_biggest_tag]u8
|
||||
tag_index: int
|
||||
}
|
||||
*/
|
||||
// This is to allow for pointer casting if needed
|
||||
|
||||
|
||||
// Advantage over subtyping version
|
||||
MAX_ENTITES :: 64
|
||||
entities: [MAX_ENTITES]Entity
|
||||
|
||||
entities[0] = Frog{}
|
||||
entities[1] = Helicopter{}
|
||||
// etc.
|
||||
}
|
||||
|
||||
|
||||
{
|
||||
// Transliteration of code from this actual compiler
|
||||
// Some stuff is missing
|
||||
Type :: type struct {}
|
||||
Scope :: type struct {}
|
||||
Token :: type struct {}
|
||||
AstNode :: type struct {}
|
||||
ExactValue :: type struct {}
|
||||
|
||||
EntityKind :: type enum {
|
||||
Invalid,
|
||||
Constant,
|
||||
Variable,
|
||||
UsingVariable,
|
||||
TypeName,
|
||||
Procedure,
|
||||
Builtin,
|
||||
Count,
|
||||
}
|
||||
|
||||
Entity :: type struct {
|
||||
Guid :: type i64
|
||||
|
||||
kind: EntityKind
|
||||
guid: Guid
|
||||
|
||||
scope: ^Scope
|
||||
token: Token
|
||||
type_: ^Type
|
||||
|
||||
using data: raw_union {
|
||||
Constant: struct {
|
||||
value: ExactValue
|
||||
}
|
||||
Variable: struct {
|
||||
visited: bool // Cycle detection
|
||||
used: bool // Variable is used
|
||||
is_field: bool // Is struct field
|
||||
anonymous: bool // Variable is an anonymous
|
||||
}
|
||||
UsingVariable: struct {
|
||||
}
|
||||
TypeName: struct {
|
||||
}
|
||||
Procedure: struct {
|
||||
used: bool
|
||||
}
|
||||
Builtin: struct {
|
||||
id: int
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Plus all the constructing procedures that go along with them!!!!
|
||||
// It's a nightmare
|
||||
}
|
||||
|
||||
{
|
||||
Type :: type struct {}
|
||||
Scope :: type struct {}
|
||||
Token :: type struct {}
|
||||
AstNode :: type struct {}
|
||||
ExactValue :: type struct {}
|
||||
|
||||
|
||||
Entity :: type union {
|
||||
Base :: type struct {
|
||||
Guid :: type i64
|
||||
guid: Guid
|
||||
|
||||
scope: ^Scope
|
||||
token: Token
|
||||
type_: ^Type
|
||||
}
|
||||
|
||||
|
||||
Constant: struct {
|
||||
using base: Base
|
||||
value: ExactValue
|
||||
}
|
||||
Variable: struct {
|
||||
using base: Base
|
||||
visited: bool // Cycle detection
|
||||
used: bool // Variable is used
|
||||
is_field: bool // Is struct field
|
||||
anonymous: bool // Variable is an anonymous
|
||||
}
|
||||
UsingVariable: struct {
|
||||
using base: Base
|
||||
}
|
||||
TypeName: struct {
|
||||
using base: Base
|
||||
}
|
||||
Procedure: struct {
|
||||
using base: Base
|
||||
used: bool
|
||||
}
|
||||
Builtin: struct {
|
||||
using base: Base
|
||||
id: int
|
||||
}
|
||||
}
|
||||
|
||||
using Entity
|
||||
|
||||
e: Entity
|
||||
|
||||
e = Variable{
|
||||
base = Base{},
|
||||
used = true,
|
||||
anonymous = false,
|
||||
}
|
||||
|
||||
|
||||
|
||||
// Q: Allow a "base" type to be added to a union?
|
||||
// Or even `using` on union to get the same properties?
|
||||
}
|
||||
|
||||
|
||||
{
|
||||
// `Raw` unions still have uses, especially for mathematic types
|
||||
|
||||
Vector2 :: type raw_union {
|
||||
using xy_: struct { x, y: f32 }
|
||||
e: [2]f32
|
||||
v: {2}f32
|
||||
}
|
||||
|
||||
Vector3 :: type raw_union {
|
||||
using xyz_: struct { x, y, z: f32 }
|
||||
xy: Vector2
|
||||
e: [3]f32
|
||||
v: {3}f32
|
||||
}
|
||||
|
||||
v2: Vector2
|
||||
v2.x = 1
|
||||
v2.e[0] = 1
|
||||
v2.v[0] = 1
|
||||
|
||||
v3: Vector3
|
||||
v3.x = 1
|
||||
v3.e[0] = 1
|
||||
v3.v[0] = 1
|
||||
v3.xy.x = 1
|
||||
}
|
||||
}
|
||||
|
||||
nl :: proc() { print_nl() }
|
||||
@@ -0,0 +1,412 @@
|
||||
#load "win32.odin"
|
||||
|
||||
assume :: proc(cond: bool) #foreign "llvm.assume"
|
||||
|
||||
__debug_trap :: proc() #foreign "llvm.debugtrap"
|
||||
__trap :: proc() #foreign "llvm.trap"
|
||||
read_cycle_counter :: proc() -> u64 #foreign "llvm.readcyclecounter"
|
||||
|
||||
bit_reverse16 :: proc(b: u16) -> u16 #foreign "llvm.bitreverse.i16"
|
||||
bit_reverse32 :: proc(b: u32) -> u32 #foreign "llvm.bitreverse.i32"
|
||||
bit_reverse64 :: proc(b: u64) -> u64 #foreign "llvm.bitreverse.i64"
|
||||
|
||||
byte_swap16 :: proc(b: u16) -> u16 #foreign "llvm.bswap.i16"
|
||||
byte_swap32 :: proc(b: u32) -> u32 #foreign "llvm.bswap.i32"
|
||||
byte_swap64 :: proc(b: u64) -> u64 #foreign "llvm.bswap.i64"
|
||||
|
||||
fmuladd_f32 :: proc(a, b, c: f32) -> f32 #foreign "llvm.fmuladd.f32"
|
||||
fmuladd_f64 :: proc(a, b, c: f64) -> f64 #foreign "llvm.fmuladd.f64"
|
||||
|
||||
// TODO(bill): make custom heap procedures
|
||||
heap_alloc :: proc(len: int) -> rawptr #foreign "malloc"
|
||||
heap_dealloc :: proc(ptr: rawptr) #foreign "free"
|
||||
|
||||
memory_zero :: proc(data: rawptr, len: int) {
|
||||
d := slice_ptr(data as ^byte, len)
|
||||
for i := 0; i < len; i++ {
|
||||
d[i] = 0
|
||||
}
|
||||
}
|
||||
|
||||
memory_compare :: proc(dst, src: rawptr, len: int) -> int {
|
||||
s1, s2: ^byte = dst, src
|
||||
for i := 0; i < len; i++ {
|
||||
a := ptr_offset(s1, i)^
|
||||
b := ptr_offset(s2, i)^
|
||||
if a != b {
|
||||
return (a - b) as int
|
||||
}
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
memory_copy :: proc(dst, src: rawptr, n: int) #inline {
|
||||
if dst == src {
|
||||
return
|
||||
}
|
||||
|
||||
v128b :: type {4}u32
|
||||
compile_assert(align_of(v128b) == 16)
|
||||
|
||||
d, s: ^byte = dst, src
|
||||
|
||||
for ; s as uint % 16 != 0 && n != 0; n-- {
|
||||
d^ = s^
|
||||
d, s = ptr_offset(d, 1), ptr_offset(s, 1)
|
||||
}
|
||||
|
||||
if d as uint % 16 == 0 {
|
||||
for ; n >= 16; d, s, n = ptr_offset(d, 16), ptr_offset(s, 16), n-16 {
|
||||
(d as ^v128b)^ = (s as ^v128b)^
|
||||
}
|
||||
|
||||
if n&8 != 0 {
|
||||
(d as ^u64)^ = (s as ^u64)^
|
||||
d, s = ptr_offset(d, 8), ptr_offset(s, 8)
|
||||
}
|
||||
if n&4 != 0 {
|
||||
(d as ^u32)^ = (s as ^u32)^;
|
||||
d, s = ptr_offset(d, 4), ptr_offset(s, 4)
|
||||
}
|
||||
if n&2 != 0 {
|
||||
(d as ^u16)^ = (s as ^u16)^
|
||||
d, s = ptr_offset(d, 2), ptr_offset(s, 2)
|
||||
}
|
||||
if n&1 != 0 {
|
||||
d^ = s^
|
||||
d, s = ptr_offset(d, 1), ptr_offset(s, 1)
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// IMPORTANT NOTE(bill): Little endian only
|
||||
LS :: proc(a, b: u32) -> u32 #inline { return a << b }
|
||||
RS :: proc(a, b: u32) -> u32 #inline { return a >> b }
|
||||
/* NOTE(bill): Big endian version
|
||||
LS :: proc(a, b: u32) -> u32 #inline { return a >> b; }
|
||||
RS :: proc(a, b: u32) -> u32 #inline { return a << b; }
|
||||
*/
|
||||
|
||||
w, x: u32
|
||||
|
||||
if d as uint % 4 == 1 {
|
||||
w = (s as ^u32)^
|
||||
d^ = s^; d = ptr_offset(d, 1); s = ptr_offset(s, 1)
|
||||
d^ = s^; d = ptr_offset(d, 1); s = ptr_offset(s, 1)
|
||||
d^ = s^; d = ptr_offset(d, 1); s = ptr_offset(s, 1)
|
||||
n -= 3
|
||||
|
||||
for n > 16 {
|
||||
d32 := d as ^u32
|
||||
s32 := ptr_offset(s, 1) as ^u32
|
||||
x = s32^; d32^ = LS(w, 24) | RS(x, 8)
|
||||
d32, s32 = ptr_offset(d32, 1), ptr_offset(s32, 1)
|
||||
w = s32^; d32^ = LS(x, 24) | RS(w, 8)
|
||||
d32, s32 = ptr_offset(d32, 1), ptr_offset(s32, 1)
|
||||
x = s32^; d32^ = LS(w, 24) | RS(x, 8)
|
||||
d32, s32 = ptr_offset(d32, 1), ptr_offset(s32, 1)
|
||||
w = s32^; d32^ = LS(x, 24) | RS(w, 8)
|
||||
d32, s32 = ptr_offset(d32, 1), ptr_offset(s32, 1)
|
||||
|
||||
d, s, n = ptr_offset(d, 16), ptr_offset(s, 16), n-16
|
||||
}
|
||||
|
||||
} else if d as uint % 4 == 2 {
|
||||
w = (s as ^u32)^
|
||||
d^ = s^; d = ptr_offset(d, 1); s = ptr_offset(s, 1)
|
||||
d^ = s^; d = ptr_offset(d, 1); s = ptr_offset(s, 1)
|
||||
n -= 2
|
||||
|
||||
for n > 17 {
|
||||
d32 := d as ^u32
|
||||
s32 := ptr_offset(s, 2) as ^u32
|
||||
x = s32^; d32^ = LS(w, 16) | RS(x, 16)
|
||||
d32, s32 = ptr_offset(d32, 1), ptr_offset(s32, 1)
|
||||
w = s32^; d32^ = LS(x, 16) | RS(w, 16)
|
||||
d32, s32 = ptr_offset(d32, 1), ptr_offset(s32, 1)
|
||||
x = s32^; d32^ = LS(w, 16) | RS(x, 16)
|
||||
d32, s32 = ptr_offset(d32, 1), ptr_offset(s32, 1)
|
||||
w = s32^; d32^ = LS(x, 16) | RS(w, 16)
|
||||
d32, s32 = ptr_offset(d32, 1), ptr_offset(s32, 1)
|
||||
|
||||
d, s, n = ptr_offset(d, 16), ptr_offset(s, 16), n-16
|
||||
}
|
||||
|
||||
} else if d as uint % 4 == 3 {
|
||||
w = (s as ^u32)^
|
||||
d^ = s^
|
||||
n -= 1
|
||||
|
||||
for n > 18 {
|
||||
d32 := d as ^u32
|
||||
s32 := ptr_offset(s, 3) as ^u32
|
||||
x = s32^; d32^ = LS(w, 8) | RS(x, 24)
|
||||
d32, s32 = ptr_offset(d32, 1), ptr_offset(s32, 1)
|
||||
w = s32^; d32^ = LS(x, 8) | RS(w, 24)
|
||||
d32, s32 = ptr_offset(d32, 1), ptr_offset(s32, 1)
|
||||
x = s32^; d32^ = LS(w, 8) | RS(x, 24)
|
||||
d32, s32 = ptr_offset(d32, 1), ptr_offset(s32, 1)
|
||||
w = s32^; d32^ = LS(x, 8) | RS(w, 24)
|
||||
d32, s32 = ptr_offset(d32, 1), ptr_offset(s32, 1)
|
||||
|
||||
d, s, n = ptr_offset(d, 16), ptr_offset(s, 16), n-16
|
||||
}
|
||||
}
|
||||
|
||||
if n&16 != 0 {
|
||||
(d as ^v128b)^ = (s as ^v128b)^
|
||||
d, s = ptr_offset(d, 16), ptr_offset(s, 16)
|
||||
}
|
||||
if n&8 != 0 {
|
||||
(d as ^u64)^ = (s as ^u64)^
|
||||
d, s = ptr_offset(d, 8), ptr_offset(s, 8)
|
||||
}
|
||||
if n&4 != 0 {
|
||||
(d as ^u32)^ = (s as ^u32)^;
|
||||
d, s = ptr_offset(d, 4), ptr_offset(s, 4)
|
||||
}
|
||||
if n&2 != 0 {
|
||||
(d as ^u16)^ = (s as ^u16)^
|
||||
d, s = ptr_offset(d, 2), ptr_offset(s, 2)
|
||||
}
|
||||
if n&1 != 0 {
|
||||
d^ = s^
|
||||
}
|
||||
}
|
||||
|
||||
memory_move :: proc(dst, src: rawptr, n: int) #inline {
|
||||
d, s: ^byte = dst, src
|
||||
if d == s {
|
||||
return
|
||||
}
|
||||
if d >= ptr_offset(s, n) || ptr_offset(d, n) <= s {
|
||||
memory_copy(d, s, n)
|
||||
return
|
||||
}
|
||||
|
||||
// TODO(bill): Vectorize the shit out of this
|
||||
if d < s {
|
||||
if s as int % size_of(int) == d as int % size_of(int) {
|
||||
for d as int % size_of(int) != 0 {
|
||||
if n == 0 {
|
||||
return
|
||||
}
|
||||
n--
|
||||
d^ = s^
|
||||
d, s = ptr_offset(d, 1), ptr_offset(s, 1)
|
||||
}
|
||||
di, si := d as ^int, s as ^int
|
||||
for n >= size_of(int) {
|
||||
di^ = si^
|
||||
di, si = ptr_offset(di, 1), ptr_offset(si, 1)
|
||||
n -= size_of(int)
|
||||
}
|
||||
}
|
||||
for ; n > 0; n-- {
|
||||
d^ = s^
|
||||
d, s = ptr_offset(d, 1), ptr_offset(s, 1)
|
||||
}
|
||||
} else {
|
||||
if s as int % size_of(int) == d as int % size_of(int) {
|
||||
for ptr_offset(d, n) as int % size_of(int) != 0 {
|
||||
if n == 0 {
|
||||
return
|
||||
}
|
||||
n--
|
||||
d^ = s^
|
||||
d, s = ptr_offset(d, 1), ptr_offset(s, 1)
|
||||
}
|
||||
for n >= size_of(int) {
|
||||
n -= size_of(int)
|
||||
di := ptr_offset(d, n) as ^int
|
||||
si := ptr_offset(s, n) as ^int
|
||||
di^ = si^
|
||||
}
|
||||
for ; n > 0; n-- {
|
||||
d^ = s^
|
||||
d, s = ptr_offset(d, 1), ptr_offset(s, 1)
|
||||
}
|
||||
}
|
||||
for n > 0 {
|
||||
n--
|
||||
dn := ptr_offset(d, n)
|
||||
sn := ptr_offset(s, n)
|
||||
dn^ = sn^
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
__string_eq :: proc(a, b: string) -> bool {
|
||||
if len(a) != len(b) {
|
||||
return false
|
||||
}
|
||||
if ^a[0] == ^b[0] {
|
||||
return true
|
||||
}
|
||||
return memory_compare(^a[0], ^b[0], len(a)) == 0
|
||||
}
|
||||
|
||||
__string_cmp :: proc(a, b : string) -> int {
|
||||
min_len := len(a)
|
||||
if len(b) < min_len {
|
||||
min_len = len(b)
|
||||
}
|
||||
for i := 0; i < min_len; i++ {
|
||||
x := a[i]
|
||||
y := b[i]
|
||||
if x < y {
|
||||
return -1
|
||||
} else if x > y {
|
||||
return +1
|
||||
}
|
||||
}
|
||||
|
||||
if len(a) < len(b) {
|
||||
return -1
|
||||
} else if len(a) > len(b) {
|
||||
return +1
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
__string_ne :: proc(a, b : string) -> bool #inline { return !__string_eq(a, b) }
|
||||
__string_lt :: proc(a, b : string) -> bool #inline { return __string_cmp(a, b) < 0 }
|
||||
__string_gt :: proc(a, b : string) -> bool #inline { return __string_cmp(a, b) > 0 }
|
||||
__string_le :: proc(a, b : string) -> bool #inline { return __string_cmp(a, b) <= 0 }
|
||||
__string_ge :: proc(a, b : string) -> bool #inline { return __string_cmp(a, b) >= 0 }
|
||||
|
||||
|
||||
|
||||
|
||||
Allocation_Mode :: type enum {
|
||||
ALLOC,
|
||||
DEALLOC,
|
||||
DEALLOC_ALL,
|
||||
RESIZE,
|
||||
}
|
||||
|
||||
|
||||
|
||||
Allocator_Proc :: type proc(allocator_data: rawptr, mode: Allocation_Mode,
|
||||
size, alignment: int,
|
||||
old_memory: rawptr, old_size: int, flags: u64) -> rawptr
|
||||
|
||||
Allocator :: type struct {
|
||||
procedure: Allocator_Proc;
|
||||
data: rawptr
|
||||
}
|
||||
|
||||
|
||||
Context :: type struct {
|
||||
thread_ptr: rawptr
|
||||
|
||||
user_data: rawptr
|
||||
user_index: int
|
||||
|
||||
allocator: Allocator
|
||||
}
|
||||
|
||||
#thread_local context: Context
|
||||
|
||||
DEFAULT_ALIGNMENT :: 2*size_of(int)
|
||||
|
||||
|
||||
__check_context :: proc() {
|
||||
if context.allocator.procedure == null {
|
||||
context.allocator = __default_allocator()
|
||||
}
|
||||
if context.thread_ptr == null {
|
||||
// TODO(bill):
|
||||
// context.thread_ptr = current_thread_pointer()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
alloc :: proc(size: int) -> rawptr #inline { return alloc_align(size, DEFAULT_ALIGNMENT) }
|
||||
|
||||
alloc_align :: proc(size, alignment: int) -> rawptr #inline {
|
||||
__check_context()
|
||||
a := context.allocator
|
||||
return a.procedure(a.data, Allocation_Mode.ALLOC, size, alignment, null, 0, 0)
|
||||
}
|
||||
|
||||
dealloc :: proc(ptr: rawptr) #inline {
|
||||
__check_context()
|
||||
a := context.allocator
|
||||
_ = a.procedure(a.data, Allocation_Mode.DEALLOC, 0, 0, ptr, 0, 0)
|
||||
}
|
||||
dealloc_all :: proc(ptr: rawptr) #inline {
|
||||
__check_context()
|
||||
a := context.allocator
|
||||
_ = a.procedure(a.data, Allocation_Mode.DEALLOC_ALL, 0, 0, ptr, 0, 0)
|
||||
}
|
||||
|
||||
|
||||
resize :: proc(ptr: rawptr, old_size, new_size: int) -> rawptr #inline { return resize_align(ptr, old_size, new_size, DEFAULT_ALIGNMENT) }
|
||||
resize_align :: proc(ptr: rawptr, old_size, new_size, alignment: int) -> rawptr #inline {
|
||||
__check_context()
|
||||
a := context.allocator
|
||||
return a.procedure(a.data, Allocation_Mode.RESIZE, new_size, alignment, ptr, old_size, 0)
|
||||
}
|
||||
|
||||
|
||||
|
||||
default_resize_align :: proc(old_memory: rawptr, old_size, new_size, alignment: int) -> rawptr {
|
||||
if old_memory == null {
|
||||
return alloc_align(new_size, alignment)
|
||||
}
|
||||
|
||||
if new_size == 0 {
|
||||
dealloc(old_memory)
|
||||
return null
|
||||
}
|
||||
|
||||
if new_size == old_size {
|
||||
return old_memory
|
||||
}
|
||||
|
||||
new_memory := alloc_align(new_size, alignment)
|
||||
if new_memory == null {
|
||||
return null
|
||||
}
|
||||
|
||||
memory_copy(new_memory, old_memory, min(old_size, new_size));
|
||||
dealloc(old_memory)
|
||||
return new_memory
|
||||
}
|
||||
|
||||
|
||||
__default_allocator_proc :: proc(allocator_data: rawptr, mode: Allocation_Mode,
|
||||
size, alignment: int,
|
||||
old_memory: rawptr, old_size: int, flags: u64) -> rawptr {
|
||||
using Allocation_Mode
|
||||
match mode {
|
||||
case ALLOC:
|
||||
return heap_alloc(size)
|
||||
case RESIZE:
|
||||
return default_resize_align(old_memory, old_size, size, alignment)
|
||||
case DEALLOC:
|
||||
heap_dealloc(old_memory)
|
||||
case DEALLOC_ALL:
|
||||
// NOTE(bill): Does nothing
|
||||
}
|
||||
|
||||
return null
|
||||
}
|
||||
|
||||
__default_allocator :: proc() -> Allocator {
|
||||
return Allocator{
|
||||
__default_allocator_proc,
|
||||
null,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
__assert :: proc(msg: string) {
|
||||
file_write(file_get_standard(File_Standard.ERROR), msg as []byte)
|
||||
// TODO(bill): Which is better?
|
||||
// __trap()
|
||||
__debug_trap()
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
#foreign_system_library "opengl32"
|
||||
|
||||
GL_ZERO :: 0x0000
|
||||
GL_ONE :: 0x0001
|
||||
GL_TRIANGLES :: 0x0004
|
||||
GL_BLEND :: 0x0be2
|
||||
GL_SRC_ALPHA :: 0x0302
|
||||
GL_ONE_MINUS_SRC_ALPHA :: 0x0303
|
||||
GL_TEXTURE_2D :: 0x0de1
|
||||
GL_RGBA8 :: 0x8058
|
||||
GL_UNSIGNED_BYTE :: 0x1401
|
||||
GL_BGRA_EXT :: 0x80e1
|
||||
GL_TEXTURE_MAX_LEVEL :: 0x813d
|
||||
GL_RGBA :: 0x1908
|
||||
|
||||
GL_NEAREST :: 0x2600
|
||||
GL_LINEAR :: 0x2601
|
||||
|
||||
GL_DEPTH_BUFFER_BIT :: 0x00000100
|
||||
GL_STENCIL_BUFFER_BIT :: 0x00000400
|
||||
GL_COLOR_BUFFER_BIT :: 0x00004000
|
||||
|
||||
GL_TEXTURE_MAX_ANISOTROPY_EXT :: 0x84fe
|
||||
|
||||
GL_TEXTURE_MAG_FILTER :: 0x2800
|
||||
GL_TEXTURE_MIN_FILTER :: 0x2801
|
||||
GL_TEXTURE_WRAP_S :: 0x2802
|
||||
GL_TEXTURE_WRAP_T :: 0x2803
|
||||
|
||||
glClear :: proc(mask: u32) #foreign
|
||||
glClearColor :: proc(r, g, b, a: f32) #foreign
|
||||
glBegin :: proc(mode: i32) #foreign
|
||||
glEnd :: proc() #foreign
|
||||
glColor3f :: proc(r, g, b: f32) #foreign
|
||||
glColor4f :: proc(r, g, b, a: f32) #foreign
|
||||
glVertex2f :: proc(x, y: f32) #foreign
|
||||
glVertex3f :: proc(x, y, z: f32) #foreign
|
||||
glTexCoord2f :: proc(u, v: f32) #foreign
|
||||
glLoadIdentity :: proc() #foreign
|
||||
glOrtho :: proc(left, right, bottom, top, near, far: f64) #foreign
|
||||
glBlendFunc :: proc(sfactor, dfactor: i32) #foreign
|
||||
glEnable :: proc(cap: i32) #foreign
|
||||
glDisable :: proc(cap: i32) #foreign
|
||||
glGenTextures :: proc(count: i32, result: ^u32) #foreign
|
||||
glTexParameteri :: proc(target, pname, param: i32) #foreign
|
||||
glTexParameterf :: proc(target: i32, pname: i32, param: f32) #foreign
|
||||
glBindTexture :: proc(target: i32, texture: u32) #foreign
|
||||
glTexImage2D :: proc(target, level, internal_format, width, height, border, format, _type: i32, pixels: rawptr) #foreign
|
||||
|
||||
+399
@@ -0,0 +1,399 @@
|
||||
#load "runtime.odin"
|
||||
#load "win32.odin"
|
||||
#load "file.odin"
|
||||
|
||||
print_string_to_buffer :: proc(buf: ^[]byte, s: string) {
|
||||
// NOTE(bill): This is quite a hack
|
||||
// TODO(bill): Should I allow the raw editing of a slice by exposing its
|
||||
// internal members?
|
||||
Raw_Bytes :: struct #ordered {
|
||||
data: ^byte
|
||||
len: int
|
||||
cap: int
|
||||
}
|
||||
|
||||
slice := buf as ^Raw_Bytes
|
||||
if slice.len < slice.cap {
|
||||
n := min(slice.cap-slice.len, len(s))
|
||||
offset := ptr_offset(slice.data, slice.len)
|
||||
memory_copy(offset, ^s[0], n)
|
||||
slice.len += n
|
||||
}
|
||||
}
|
||||
|
||||
byte_reverse :: proc(b: []byte) {
|
||||
n := len(b)
|
||||
for i := 0; i < n/2; i++ {
|
||||
b[i], b[n-1-i] = b[n-1-i], b[i]
|
||||
}
|
||||
}
|
||||
|
||||
encode_rune :: proc(r: rune) -> ([4]byte, int) {
|
||||
buf: [4]byte
|
||||
i := r as u32
|
||||
mask: byte : 0x3f
|
||||
if i <= 1<<7-1 {
|
||||
buf[0] = r as byte
|
||||
return buf, 1
|
||||
}
|
||||
if i <= 1<<11-1 {
|
||||
buf[0] = 0xc0 | (r>>6) as byte
|
||||
buf[1] = 0x80 | (r) as byte & mask
|
||||
return buf, 2
|
||||
}
|
||||
|
||||
// Invalid or Surrogate range
|
||||
if i > 0x0010ffff ||
|
||||
(i >= 0xd800 && i <= 0xdfff) {
|
||||
r = 0xfffd
|
||||
}
|
||||
|
||||
if i <= 1<<16-1 {
|
||||
buf[0] = 0xe0 | (r>>12) as byte
|
||||
buf[1] = 0x80 | (r>>6) as byte & mask
|
||||
buf[2] = 0x80 | (r) as byte & mask
|
||||
return buf, 3
|
||||
}
|
||||
|
||||
buf[0] = 0xf0 | (r>>18) as byte
|
||||
buf[1] = 0x80 | (r>>12) as byte & mask
|
||||
buf[2] = 0x80 | (r>>6) as byte & mask
|
||||
buf[3] = 0x80 | (r) as byte & mask
|
||||
return buf, 4
|
||||
}
|
||||
|
||||
print_rune_to_buffer :: proc(buf: ^[]byte, r: rune) {
|
||||
b, n := encode_rune(r)
|
||||
print_string_to_buffer(buf, b[:n] as string)
|
||||
}
|
||||
|
||||
print_space_to_buffer :: proc(buf: ^[]byte) { print_rune_to_buffer(buf, #rune " ") }
|
||||
print_nl_to_buffer :: proc(buf: ^[]byte) { print_rune_to_buffer(buf, #rune "\n") }
|
||||
|
||||
print_int_to_buffer :: proc(buf: ^[]byte, i: int) {
|
||||
print_int_base_to_buffer(buf, i, 10);
|
||||
}
|
||||
PRINT__NUM_TO_CHAR_TABLE :: "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz@$"
|
||||
print_int_base_to_buffer :: proc(buffer: ^[]byte, i, base: int) {
|
||||
|
||||
buf: [65]byte
|
||||
len := 0
|
||||
negative := false
|
||||
if i < 0 {
|
||||
negative = true
|
||||
i = -i
|
||||
}
|
||||
if i == 0 {
|
||||
buf[len] = #rune "0"
|
||||
len++
|
||||
}
|
||||
for i > 0 {
|
||||
buf[len] = PRINT__NUM_TO_CHAR_TABLE[i % base]
|
||||
len++
|
||||
i /= base
|
||||
}
|
||||
|
||||
if negative {
|
||||
buf[len] = #rune "-"
|
||||
len++
|
||||
}
|
||||
|
||||
byte_reverse(buf[:len])
|
||||
print_string_to_buffer(buffer, buf[:len] as string)
|
||||
}
|
||||
|
||||
print_uint_to_buffer :: proc(buffer: ^[]byte, i: uint) {
|
||||
print_uint_base_to_buffer(buffer, i, 10, 0, #rune " ")
|
||||
}
|
||||
print_uint_base_to_buffer :: proc(buffer: ^[]byte, i, base: uint, min_width: int, pad_char: byte) {
|
||||
buf: [65]byte
|
||||
len := 0
|
||||
if i == 0 {
|
||||
buf[len] = #rune "0"
|
||||
len++
|
||||
}
|
||||
for i > 0 {
|
||||
buf[len] = PRINT__NUM_TO_CHAR_TABLE[i % base]
|
||||
len++
|
||||
i /= base
|
||||
}
|
||||
for len < min_width {
|
||||
buf[len] = pad_char
|
||||
len++
|
||||
}
|
||||
|
||||
byte_reverse(buf[:len])
|
||||
print_string_to_buffer(buffer, buf[:len] as string)
|
||||
}
|
||||
|
||||
print_bool_to_buffer :: proc(buffer: ^[]byte, b : bool) {
|
||||
if b { print_string_to_buffer(buffer, "true") }
|
||||
else { print_string_to_buffer(buffer, "false") }
|
||||
}
|
||||
|
||||
print_pointer_to_buffer :: proc(buffer: ^[]byte, p: rawptr) #inline { print_uint_base_to_buffer(buffer, p as uint, 16, 0, #rune " ") }
|
||||
|
||||
print_f32_to_buffer :: proc(buffer: ^[]byte, f: f32) #inline { print__f64(buffer, f as f64, 7) }
|
||||
print_f64_to_buffer :: proc(buffer: ^[]byte, f: f64) #inline { print__f64(buffer, f, 10) }
|
||||
|
||||
print__f64 :: proc(buffer: ^[]byte, f: f64, decimal_places: int) {
|
||||
if f == 0 {
|
||||
print_rune_to_buffer(buffer, #rune "0")
|
||||
return
|
||||
}
|
||||
if f < 0 {
|
||||
print_rune_to_buffer(buffer, #rune "-")
|
||||
f = -f
|
||||
}
|
||||
|
||||
print_u64_to_buffer :: proc(buffer: ^[]byte, i: u64) {
|
||||
buf: [22]byte
|
||||
len := 0
|
||||
if i == 0 {
|
||||
buf[len] = #rune "0"
|
||||
len++
|
||||
}
|
||||
for i > 0 {
|
||||
buf[len] = PRINT__NUM_TO_CHAR_TABLE[i % 10]
|
||||
len++
|
||||
i /= 10
|
||||
}
|
||||
byte_reverse(buf[:len])
|
||||
print_string_to_buffer(buffer, buf[:len] as string)
|
||||
}
|
||||
|
||||
i := f as u64
|
||||
print_u64_to_buffer(buffer, i)
|
||||
f -= i as f64
|
||||
|
||||
print_rune_to_buffer(buffer, #rune ".")
|
||||
|
||||
mult := 10.0
|
||||
for decimal_places := 6; decimal_places >= 0; decimal_places-- {
|
||||
i = (f * mult) as u64
|
||||
print_u64_to_buffer(buffer, i as u64)
|
||||
f -= i as f64 / mult
|
||||
mult *= 10
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
print_any_to_buffer :: proc(buf: ^[]byte, arg: any) {
|
||||
using Type_Info
|
||||
match type info : arg.type_info {
|
||||
case Named:
|
||||
a: any
|
||||
a.type_info = info.base
|
||||
a.data = arg.data
|
||||
match type b : info.base {
|
||||
case Struct:
|
||||
print_string_to_buffer(buf, info.name)
|
||||
print_string_to_buffer(buf, "{")
|
||||
for i := 0; i < len(b.fields); i++ {
|
||||
f := b.fields[i];
|
||||
if i > 0 {
|
||||
print_string_to_buffer(buf, ", ")
|
||||
}
|
||||
print_any_to_buffer(buf, f.name)
|
||||
print_string_to_buffer(buf, " = ")
|
||||
v: any
|
||||
v.type_info = f.type_info
|
||||
v.data = ptr_offset(arg.data as ^byte, f.offset)
|
||||
print_any_to_buffer(buf, v)
|
||||
}
|
||||
print_string_to_buffer(buf, "}")
|
||||
|
||||
default:
|
||||
print_any_to_buffer(buf, a)
|
||||
}
|
||||
|
||||
case Integer:
|
||||
if info.signed {
|
||||
i: int = 0;
|
||||
if arg.data != null {
|
||||
match info.size {
|
||||
case 1: i = (arg.data as ^i8)^ as int
|
||||
case 2: i = (arg.data as ^i16)^ as int
|
||||
case 4: i = (arg.data as ^i32)^ as int
|
||||
case 8: i = (arg.data as ^i64)^ as int
|
||||
case 16: i = (arg.data as ^i128)^ as int
|
||||
}
|
||||
}
|
||||
print_int_to_buffer(buf, i)
|
||||
} else {
|
||||
i: uint = 0;
|
||||
if arg.data != null {
|
||||
match info.size {
|
||||
case 1: i = (arg.data as ^u8)^ as uint
|
||||
case 2: i = (arg.data as ^u16)^ as uint
|
||||
case 4: i = (arg.data as ^u32)^ as uint
|
||||
case 8: i = (arg.data as ^u64)^ as uint
|
||||
case 16: i = (arg.data as ^u128)^ as uint
|
||||
}
|
||||
}
|
||||
print_uint_to_buffer(buf, i)
|
||||
}
|
||||
|
||||
case Float:
|
||||
f: f64 = 0
|
||||
if arg.data != null {
|
||||
match info.size {
|
||||
case 4: f = (arg.data as ^f32)^ as f64
|
||||
case 8: f = (arg.data as ^f64)^ as f64
|
||||
}
|
||||
}
|
||||
print_f64_to_buffer(buf, f)
|
||||
|
||||
case String:
|
||||
s := ""
|
||||
if arg.data != null {
|
||||
s = (arg.data as ^string)^
|
||||
}
|
||||
print_string_to_buffer(buf, s)
|
||||
|
||||
case Boolean:
|
||||
v := false;
|
||||
if arg.data != null {
|
||||
v = (arg.data as ^bool)^
|
||||
}
|
||||
print_bool_to_buffer(buf, v)
|
||||
|
||||
case Pointer:
|
||||
v := null;
|
||||
if arg.data != null {
|
||||
v = (arg.data as ^rawptr)^
|
||||
}
|
||||
print_pointer_to_buffer(buf, v)
|
||||
|
||||
case Enum:
|
||||
v: any
|
||||
v.data = arg.data
|
||||
v.type_info = info.base
|
||||
print_any_to_buffer(buf, v)
|
||||
|
||||
|
||||
case Array:
|
||||
print_string_to_buffer(buf, "[")
|
||||
for i := 0; i < info.len; i++ {
|
||||
if i > 0 {
|
||||
print_string_to_buffer(buf, ", ")
|
||||
}
|
||||
|
||||
elem: any
|
||||
elem.data = (arg.data as int + i*info.elem_size) as rawptr
|
||||
elem.type_info = info.elem
|
||||
print_any_to_buffer(buf, elem)
|
||||
}
|
||||
print_string_to_buffer(buf, "]")
|
||||
|
||||
case Slice:
|
||||
slice := arg.data as ^struct { data: rawptr; len, cap: int }
|
||||
print_string_to_buffer(buf, "[")
|
||||
for i := 0; i < slice.len; i++ {
|
||||
if i > 0 {
|
||||
print_string_to_buffer(buf, ", ")
|
||||
}
|
||||
|
||||
elem: any
|
||||
elem.data = (slice.data as int + i*info.elem_size) as rawptr
|
||||
elem.type_info = info.elem
|
||||
print_any_to_buffer(buf, elem)
|
||||
}
|
||||
print_string_to_buffer(buf, "]")
|
||||
|
||||
case Vector:
|
||||
print_string_to_buffer(buf, "<")
|
||||
for i := 0; i < info.len; i++ {
|
||||
if i > 0 {
|
||||
print_string_to_buffer(buf, ", ")
|
||||
}
|
||||
|
||||
elem: any
|
||||
elem.data = (arg.data as int + i*info.elem_size) as rawptr
|
||||
elem.type_info = info.elem
|
||||
print_any_to_buffer(buf, elem)
|
||||
}
|
||||
print_string_to_buffer(buf, ">")
|
||||
|
||||
|
||||
case Struct:
|
||||
print_string_to_buffer(buf, "(struct ")
|
||||
for i := 0; i < len(info.fields); i++ {
|
||||
if i > 0 {
|
||||
print_string_to_buffer(buf, ", ")
|
||||
}
|
||||
print_any_to_buffer(buf, info.fields[i].name)
|
||||
}
|
||||
print_string_to_buffer(buf, ")")
|
||||
case Union: print_string_to_buffer(buf, "(union)")
|
||||
case Raw_Union: print_string_to_buffer(buf, "(raw_union)")
|
||||
case Procedure:
|
||||
print_string_to_buffer(buf, "(procedure 0x")
|
||||
print_pointer_to_buffer(buf, (arg.data as ^rawptr)^)
|
||||
print_string_to_buffer(buf, ")")
|
||||
default:
|
||||
print_string_to_buffer(buf, "")
|
||||
}
|
||||
}
|
||||
|
||||
type_info_is_string :: proc(info: ^Type_Info) -> bool {
|
||||
using Type_Info
|
||||
if info == null {
|
||||
return false
|
||||
}
|
||||
|
||||
for {
|
||||
match type i : info {
|
||||
case Named:
|
||||
info = i.base
|
||||
continue
|
||||
case String:
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
print_to_buffer :: proc(buf: ^[]byte, args: ..any) {
|
||||
prev_string := false
|
||||
for i := 0; i < len(args); i++ {
|
||||
arg := args[i]
|
||||
is_string := arg.data != null && type_info_is_string(arg.type_info)
|
||||
if i > 0 && !is_string && !prev_string {
|
||||
// Add space between two non-string arguments
|
||||
print_space_to_buffer(buf)
|
||||
}
|
||||
print_any_to_buffer(buf, arg)
|
||||
prev_string = is_string
|
||||
}
|
||||
}
|
||||
|
||||
println_to_buffer :: proc(buf: ^[]byte, args: ..any) {
|
||||
for i := 0; i < len(args); i++ {
|
||||
arg := args[i]
|
||||
if i > 0 {
|
||||
print_space_to_buffer(buf)
|
||||
}
|
||||
print_any_to_buffer(buf, arg)
|
||||
}
|
||||
print_nl_to_buffer(buf)
|
||||
}
|
||||
|
||||
|
||||
print :: proc(args: ..any) {
|
||||
data: [4096]byte
|
||||
buf := data[:0]
|
||||
print_to_buffer(^buf, ..args)
|
||||
file_write(file_get_standard(File_Standard.OUTPUT), buf)
|
||||
}
|
||||
|
||||
|
||||
println :: proc(args: ..any) {
|
||||
data: [4096]byte
|
||||
buf := data[:0]
|
||||
println_to_buffer(^buf, ..args)
|
||||
file_write(file_get_standard(File_Standard.OUTPUT), buf)
|
||||
}
|
||||
@@ -0,0 +1,297 @@
|
||||
#load "win32.odin"
|
||||
#load "print.odin"
|
||||
|
||||
// IMPORTANT NOTE(bill): Do not change the order of any of this data
|
||||
// The compiler relies upon this _exact_ order
|
||||
Type_Info :: union {
|
||||
Member :: struct #ordered {
|
||||
name: string // can be empty if tuple
|
||||
type_info: ^Type_Info
|
||||
offset: int // offsets are not used in tuples
|
||||
}
|
||||
Record :: struct #ordered {
|
||||
fields: []Member
|
||||
}
|
||||
|
||||
|
||||
Named: struct #ordered {
|
||||
name: string
|
||||
base: ^Type_Info
|
||||
}
|
||||
Integer: struct #ordered {
|
||||
size: int // in bytes
|
||||
signed: bool
|
||||
}
|
||||
Float: struct #ordered {
|
||||
size: int // in bytes
|
||||
}
|
||||
String: struct #ordered {}
|
||||
Boolean: struct #ordered {}
|
||||
Pointer: struct #ordered {
|
||||
elem: ^Type_Info
|
||||
}
|
||||
Procedure: struct #ordered {
|
||||
params: ^Type_Info // Type_Info.Tuple
|
||||
results: ^Type_Info // Type_Info.Tuple
|
||||
variadic: bool
|
||||
}
|
||||
Array: struct #ordered {
|
||||
elem: ^Type_Info
|
||||
elem_size: int
|
||||
len: int
|
||||
}
|
||||
Slice: struct #ordered {
|
||||
elem: ^Type_Info
|
||||
elem_size: int
|
||||
}
|
||||
Vector: struct #ordered {
|
||||
elem: ^Type_Info
|
||||
elem_size: int
|
||||
len: int
|
||||
}
|
||||
Tuple: Record
|
||||
Struct: Record
|
||||
Union: Record
|
||||
Raw_Union: Record
|
||||
Enum: struct #ordered {
|
||||
base: ^Type_Info
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
assume :: proc(cond: bool) #foreign "llvm.assume"
|
||||
|
||||
__debug_trap :: proc() #foreign "llvm.debugtrap"
|
||||
__trap :: proc() #foreign "llvm.trap"
|
||||
read_cycle_counter :: proc() -> u64 #foreign "llvm.readcyclecounter"
|
||||
|
||||
bit_reverse16 :: proc(b: u16) -> u16 #foreign "llvm.bitreverse.i16"
|
||||
bit_reverse32 :: proc(b: u32) -> u32 #foreign "llvm.bitreverse.i32"
|
||||
bit_reverse64 :: proc(b: u64) -> u64 #foreign "llvm.bitreverse.i64"
|
||||
|
||||
byte_swap16 :: proc(b: u16) -> u16 #foreign "llvm.bswap.i16"
|
||||
byte_swap32 :: proc(b: u32) -> u32 #foreign "llvm.bswap.i32"
|
||||
byte_swap64 :: proc(b: u64) -> u64 #foreign "llvm.bswap.i64"
|
||||
|
||||
fmuladd_f32 :: proc(a, b, c: f32) -> f32 #foreign "llvm.fmuladd.f32"
|
||||
fmuladd_f64 :: proc(a, b, c: f64) -> f64 #foreign "llvm.fmuladd.f64"
|
||||
|
||||
heap_alloc :: proc(len: int) -> rawptr {
|
||||
return HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, len)
|
||||
}
|
||||
|
||||
heap_dealloc :: proc(ptr: rawptr) {
|
||||
_ = HeapFree(GetProcessHeap(), 0, ptr)
|
||||
}
|
||||
|
||||
memory_zero :: proc(data: rawptr, len: int) {
|
||||
llvm_memset_64bit :: proc(dst: rawptr, val: byte, len: int, align: i32, is_volatile: bool) #foreign "llvm.memset.p0i8.i64"
|
||||
llvm_memset_64bit(data, 0, len, 1, false)
|
||||
}
|
||||
|
||||
memory_compare :: proc(dst, src: rawptr, len: int) -> int {
|
||||
// TODO(bill): make a faster `memory_compare`
|
||||
a := slice_ptr(dst as ^byte, len)
|
||||
b := slice_ptr(src as ^byte, len)
|
||||
for i := 0; i < len; i++ {
|
||||
if a[i] != b[i] {
|
||||
return (a[i] - b[i]) as int
|
||||
}
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
memory_copy :: proc(dst, src: rawptr, len: int) #inline {
|
||||
llvm_memmove_64bit :: proc(dst, src: rawptr, len: int, align: i32, is_volatile: bool) #foreign "llvm.memmove.p0i8.p0i8.i64"
|
||||
llvm_memmove_64bit(dst, src, len, 1, false)
|
||||
}
|
||||
|
||||
__string_eq :: proc(a, b: string) -> bool {
|
||||
if len(a) != len(b) {
|
||||
return false
|
||||
}
|
||||
if ^a[0] == ^b[0] {
|
||||
return true
|
||||
}
|
||||
return memory_compare(^a[0], ^b[0], len(a)) == 0
|
||||
}
|
||||
|
||||
__string_cmp :: proc(a, b : string) -> int {
|
||||
// Translation of http://mgronhol.github.io/fast-strcmp/
|
||||
n := min(len(a), len(b))
|
||||
|
||||
fast := n/size_of(int) + 1
|
||||
offset := (fast-1)*size_of(int)
|
||||
curr_block := 0
|
||||
if n <= size_of(int) {
|
||||
fast = 0
|
||||
}
|
||||
|
||||
la := slice_ptr(^a[0] as ^int, fast)
|
||||
lb := slice_ptr(^b[0] as ^int, fast)
|
||||
|
||||
for ; curr_block < fast; curr_block++ {
|
||||
if (la[curr_block] ~ lb[curr_block]) != 0 {
|
||||
for pos := curr_block*size_of(int); pos < n; pos++ {
|
||||
if (a[pos] ~ b[pos]) != 0 {
|
||||
return a[pos] as int - b[pos] as int
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
for ; offset < n; offset++ {
|
||||
if (a[offset] ~ b[offset]) != 0 {
|
||||
return a[offset] as int - b[offset] as int
|
||||
}
|
||||
}
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
__string_ne :: proc(a, b : string) -> bool #inline { return !__string_eq(a, b) }
|
||||
__string_lt :: proc(a, b : string) -> bool #inline { return __string_cmp(a, b) < 0 }
|
||||
__string_gt :: proc(a, b : string) -> bool #inline { return __string_cmp(a, b) > 0 }
|
||||
__string_le :: proc(a, b : string) -> bool #inline { return __string_cmp(a, b) <= 0 }
|
||||
__string_ge :: proc(a, b : string) -> bool #inline { return __string_cmp(a, b) >= 0 }
|
||||
|
||||
|
||||
|
||||
|
||||
Allocation_Mode :: enum {
|
||||
ALLOC,
|
||||
DEALLOC,
|
||||
DEALLOC_ALL,
|
||||
RESIZE,
|
||||
}
|
||||
|
||||
Allocator_Proc :: type proc(allocator_data: rawptr, mode: Allocation_Mode,
|
||||
size, alignment: int,
|
||||
old_memory: rawptr, old_size: int, flags: u64) -> rawptr
|
||||
|
||||
Allocator :: struct {
|
||||
procedure: Allocator_Proc;
|
||||
data: rawptr
|
||||
}
|
||||
|
||||
|
||||
Context :: struct {
|
||||
thread_ptr: rawptr
|
||||
|
||||
user_data: rawptr
|
||||
user_index: int
|
||||
|
||||
allocator: Allocator
|
||||
}
|
||||
|
||||
#thread_local context: Context
|
||||
|
||||
DEFAULT_ALIGNMENT :: 2*size_of(int)
|
||||
|
||||
|
||||
__check_context :: proc() {
|
||||
if context.allocator.procedure == null {
|
||||
context.allocator = __default_allocator()
|
||||
}
|
||||
if context.thread_ptr == null {
|
||||
// TODO(bill):
|
||||
// context.thread_ptr = current_thread_pointer()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
alloc :: proc(size: int) -> rawptr #inline { return alloc_align(size, DEFAULT_ALIGNMENT) }
|
||||
|
||||
alloc_align :: proc(size, alignment: int) -> rawptr #inline {
|
||||
__check_context()
|
||||
a := context.allocator
|
||||
return a.procedure(a.data, Allocation_Mode.ALLOC, size, alignment, null, 0, 0)
|
||||
}
|
||||
|
||||
dealloc :: proc(ptr: rawptr) #inline {
|
||||
__check_context()
|
||||
a := context.allocator
|
||||
_ = a.procedure(a.data, Allocation_Mode.DEALLOC, 0, 0, ptr, 0, 0)
|
||||
}
|
||||
dealloc_all :: proc(ptr: rawptr) #inline {
|
||||
__check_context()
|
||||
a := context.allocator
|
||||
_ = a.procedure(a.data, Allocation_Mode.DEALLOC_ALL, 0, 0, ptr, 0, 0)
|
||||
}
|
||||
|
||||
|
||||
resize :: proc(ptr: rawptr, old_size, new_size: int) -> rawptr #inline { return resize_align(ptr, old_size, new_size, DEFAULT_ALIGNMENT) }
|
||||
resize_align :: proc(ptr: rawptr, old_size, new_size, alignment: int) -> rawptr #inline {
|
||||
__check_context()
|
||||
a := context.allocator
|
||||
return a.procedure(a.data, Allocation_Mode.RESIZE, new_size, alignment, ptr, old_size, 0)
|
||||
}
|
||||
|
||||
|
||||
|
||||
default_resize_align :: proc(old_memory: rawptr, old_size, new_size, alignment: int) -> rawptr {
|
||||
if old_memory == null {
|
||||
return alloc_align(new_size, alignment)
|
||||
}
|
||||
|
||||
if new_size == 0 {
|
||||
dealloc(old_memory)
|
||||
return null
|
||||
}
|
||||
|
||||
if new_size == old_size {
|
||||
return old_memory
|
||||
}
|
||||
|
||||
new_memory := alloc_align(new_size, alignment)
|
||||
if new_memory == null {
|
||||
return null
|
||||
}
|
||||
|
||||
memory_copy(new_memory, old_memory, min(old_size, new_size));
|
||||
dealloc(old_memory)
|
||||
return new_memory
|
||||
}
|
||||
|
||||
|
||||
__default_allocator_proc :: proc(allocator_data: rawptr, mode: Allocation_Mode,
|
||||
size, alignment: int,
|
||||
old_memory: rawptr, old_size: int, flags: u64) -> rawptr {
|
||||
using Allocation_Mode
|
||||
match mode {
|
||||
case ALLOC:
|
||||
return heap_alloc(size)
|
||||
case RESIZE:
|
||||
return default_resize_align(old_memory, old_size, size, alignment)
|
||||
case DEALLOC:
|
||||
heap_dealloc(old_memory)
|
||||
return null
|
||||
case DEALLOC_ALL:
|
||||
// NOTE(bill): Does nothing
|
||||
}
|
||||
|
||||
return null
|
||||
}
|
||||
|
||||
__default_allocator :: proc() -> Allocator {
|
||||
return Allocator{
|
||||
__default_allocator_proc,
|
||||
null,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
__assert :: proc(msg: string) {
|
||||
file_write(file_get_standard(File_Standard.ERROR), msg as []byte)
|
||||
__debug_trap()
|
||||
}
|
||||
|
||||
__abc_error :: proc(file: string, line, column: int, index, len: int) {
|
||||
print(file, "(", line, ":", line, ") Index out of bounds: index: ", index, ", len: ", len, "\n")
|
||||
__debug_trap()
|
||||
}
|
||||
|
||||
+401
@@ -0,0 +1,401 @@
|
||||
#foreign_system_library "user32"
|
||||
#foreign_system_library "gdi32"
|
||||
|
||||
CS_VREDRAW :: 1
|
||||
CS_HREDRAW :: 2
|
||||
CW_USEDEFAULT :: 0x80000000
|
||||
|
||||
WS_OVERLAPPED :: 0
|
||||
WS_MAXIMIZEBOX :: 0x00010000
|
||||
WS_MINIMIZEBOX :: 0x00020000
|
||||
WS_THICKFRAME :: 0x00040000
|
||||
WS_SYSMENU :: 0x00080000
|
||||
WS_CAPTION :: 0x00C00000
|
||||
WS_VISIBLE :: 0x10000000
|
||||
WS_OVERLAPPEDWINDOW :: WS_OVERLAPPED|WS_CAPTION|WS_SYSMENU|WS_THICKFRAME|WS_MINIMIZEBOX|WS_MAXIMIZEBOX
|
||||
|
||||
WM_DESTROY :: 0x02
|
||||
WM_CLOSE :: 0x10
|
||||
WM_QUIT :: 0x12
|
||||
|
||||
PM_REMOVE :: 1
|
||||
|
||||
COLOR_BACKGROUND :: 1 as HBRUSH
|
||||
|
||||
|
||||
HANDLE :: type rawptr
|
||||
HWND :: type HANDLE
|
||||
HDC :: type HANDLE
|
||||
HINSTANCE :: type HANDLE
|
||||
HICON :: type HANDLE
|
||||
HCURSOR :: type HANDLE
|
||||
HMENU :: type HANDLE
|
||||
HBRUSH :: type HANDLE
|
||||
WPARAM :: type uint
|
||||
LPARAM :: type int
|
||||
LRESULT :: type int
|
||||
ATOM :: type i16
|
||||
BOOL :: type i32
|
||||
POINT :: type struct { x, y: i32 }
|
||||
|
||||
INVALID_HANDLE_VALUE :: (-1 as int) as HANDLE
|
||||
|
||||
WNDPROC :: type proc(hwnd: HWND, msg: u32, wparam: WPARAM, lparam: LPARAM) -> LRESULT
|
||||
|
||||
WNDCLASSEXA :: struct #ordered {
|
||||
size, style: u32
|
||||
wnd_proc: WNDPROC
|
||||
cls_extra, wnd_extra: i32
|
||||
instance: HINSTANCE
|
||||
icon: HICON
|
||||
cursor: HCURSOR
|
||||
background: HBRUSH
|
||||
menu_name, class_name: ^u8
|
||||
sm: HICON
|
||||
}
|
||||
|
||||
MSG :: struct #ordered {
|
||||
hwnd: HWND
|
||||
message: u32
|
||||
wparam: WPARAM
|
||||
lparam: LPARAM
|
||||
time: u32
|
||||
pt: POINT
|
||||
}
|
||||
|
||||
|
||||
|
||||
GetLastError :: proc() -> i32 #foreign
|
||||
ExitProcess :: proc(exit_code: u32) #foreign
|
||||
GetDesktopWindow :: proc() -> HWND #foreign
|
||||
GetCursorPos :: proc(p: ^POINT) -> i32 #foreign
|
||||
ScreenToClient :: proc(h: HWND, p: ^POINT) -> i32 #foreign
|
||||
|
||||
GetModuleHandleA :: proc(module_name: ^u8) -> HINSTANCE #foreign
|
||||
|
||||
QueryPerformanceFrequency :: proc(result: ^i64) -> i32 #foreign
|
||||
QueryPerformanceCounter :: proc(result: ^i64) -> i32 #foreign
|
||||
|
||||
sleep_ms :: proc(ms: i32) {
|
||||
Sleep :: proc(ms: i32) -> i32 #foreign
|
||||
Sleep(ms)
|
||||
}
|
||||
|
||||
OutputDebugStringA :: proc(c_str: ^u8) #foreign
|
||||
|
||||
|
||||
RegisterClassExA :: proc(wc: ^WNDCLASSEXA) -> ATOM #foreign
|
||||
CreateWindowExA :: proc(ex_style: u32,
|
||||
class_name, title: ^u8,
|
||||
style: u32,
|
||||
x, y: u32,
|
||||
w, h: i32,
|
||||
parent: HWND, menu: HMENU, instance: HINSTANCE,
|
||||
param: rawptr) -> HWND #foreign
|
||||
|
||||
ShowWindow :: proc(hwnd: HWND, cmd_show: i32) -> BOOL #foreign
|
||||
UpdateWindow :: proc(hwnd: HWND) -> BOOL #foreign
|
||||
PeekMessageA :: proc(msg: ^MSG, hwnd: HWND,
|
||||
msg_filter_min, msg_filter_max, remove_msg: u32) -> BOOL #foreign
|
||||
TranslateMessage :: proc(msg: ^MSG) -> BOOL #foreign
|
||||
DispatchMessageA :: proc(msg: ^MSG) -> LRESULT #foreign
|
||||
|
||||
DefWindowProcA :: proc(hwnd: HWND, msg: u32, wparam: WPARAM, lparam: LPARAM) -> LRESULT #foreign
|
||||
|
||||
|
||||
|
||||
GetQueryPerformanceFrequency :: proc() -> i64 {
|
||||
r: i64
|
||||
_ = QueryPerformanceFrequency(^r)
|
||||
return r
|
||||
}
|
||||
|
||||
GetCommandLineA :: proc() -> ^u8 #foreign
|
||||
|
||||
|
||||
|
||||
// File Stuff
|
||||
|
||||
CloseHandle :: proc(h: HANDLE) -> i32 #foreign
|
||||
GetStdHandle :: proc(h: i32) -> HANDLE #foreign
|
||||
CreateFileA :: proc(filename: ^u8, desired_access, share_mode: u32,
|
||||
security: rawptr,
|
||||
creation, flags_and_attribs: u32, template_file: HANDLE) -> HANDLE #foreign
|
||||
ReadFile :: proc(h: HANDLE, buf: rawptr, to_read: u32, bytes_read: ^i32, overlapped: rawptr) -> BOOL #foreign
|
||||
WriteFile :: proc(h: HANDLE, buf: rawptr, len: i32, written_result: ^i32, overlapped: rawptr) -> i32 #foreign
|
||||
|
||||
GetFileSizeEx :: proc(file_handle: HANDLE, file_size: ^i64) -> BOOL #foreign
|
||||
|
||||
FILE_SHARE_READ :: 0x00000001
|
||||
FILE_SHARE_WRITE :: 0x00000002
|
||||
FILE_SHARE_DELETE :: 0x00000004
|
||||
FILE_GENERIC_ALL :: 0x10000000
|
||||
FILE_GENERIC_EXECUTE :: 0x20000000
|
||||
FILE_GENERIC_WRITE :: 0x40000000
|
||||
FILE_GENERIC_READ :: 0x80000000
|
||||
|
||||
STD_INPUT_HANDLE :: -10
|
||||
STD_OUTPUT_HANDLE :: -11
|
||||
STD_ERROR_HANDLE :: -12
|
||||
|
||||
CREATE_NEW :: 1
|
||||
CREATE_ALWAYS :: 2
|
||||
OPEN_EXISTING :: 3
|
||||
OPEN_ALWAYS :: 4
|
||||
TRUNCATE_EXISTING :: 5
|
||||
|
||||
|
||||
HeapAlloc :: proc(h: HANDLE, flags: u32, bytes: int) -> rawptr #foreign
|
||||
HeapFree :: proc(h: HANDLE, flags: u32, memory: rawptr) -> BOOL #foreign
|
||||
GetProcessHeap :: proc() -> HANDLE #foreign
|
||||
|
||||
HEAP_ZERO_MEMORY :: 0x00000008
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// Windows OpenGL
|
||||
|
||||
PFD_TYPE_RGBA :: 0
|
||||
PFD_TYPE_COLORINDEX :: 1
|
||||
PFD_MAIN_PLANE :: 0
|
||||
PFD_OVERLAY_PLANE :: 1
|
||||
PFD_UNDERLAY_PLANE :: -1
|
||||
PFD_DOUBLEBUFFER :: 1
|
||||
PFD_STEREO :: 2
|
||||
PFD_DRAW_TO_WINDOW :: 4
|
||||
PFD_DRAW_TO_BITMAP :: 8
|
||||
PFD_SUPPORT_GDI :: 16
|
||||
PFD_SUPPORT_OPENGL :: 32
|
||||
PFD_GENERIC_FORMAT :: 64
|
||||
PFD_NEED_PALETTE :: 128
|
||||
PFD_NEED_SYSTEM_PALETTE :: 0x00000100
|
||||
PFD_SWAP_EXCHANGE :: 0x00000200
|
||||
PFD_SWAP_COPY :: 0x00000400
|
||||
PFD_SWAP_LAYER_BUFFERS :: 0x00000800
|
||||
PFD_GENERIC_ACCELERATED :: 0x00001000
|
||||
PFD_DEPTH_DONTCARE :: 0x20000000
|
||||
PFD_DOUBLEBUFFER_DONTCARE :: 0x40000000
|
||||
PFD_STEREO_DONTCARE :: 0x80000000
|
||||
|
||||
HGLRC :: type HANDLE
|
||||
PROC :: type proc()
|
||||
wglCreateContextAttribsARBType :: type proc(hdc: HDC, hshareContext: rawptr, attribList: ^i32) -> HGLRC
|
||||
|
||||
|
||||
PIXELFORMATDESCRIPTOR :: struct #ordered {
|
||||
size,
|
||||
version,
|
||||
flags: u32
|
||||
|
||||
pixel_type,
|
||||
color_bits,
|
||||
red_bits,
|
||||
red_shift,
|
||||
green_bits,
|
||||
green_shift,
|
||||
blue_bits,
|
||||
blue_shift,
|
||||
alpha_bits,
|
||||
alpha_shift,
|
||||
accum_bits,
|
||||
accum_red_bits,
|
||||
accum_green_bits,
|
||||
accum_blue_bits,
|
||||
accum_alpha_bits,
|
||||
depth_bits,
|
||||
stencil_bits,
|
||||
aux_buffers,
|
||||
layer_type,
|
||||
reserved: byte
|
||||
|
||||
layer_mask,
|
||||
visible_mask,
|
||||
damage_mask: u32
|
||||
}
|
||||
|
||||
GetDC :: proc(h: HANDLE) -> HDC #foreign
|
||||
SetPixelFormat :: proc(hdc: HDC, pixel_format: i32, pfd: ^PIXELFORMATDESCRIPTOR ) -> BOOL #foreign
|
||||
ChoosePixelFormat :: proc(hdc: HDC, pfd: ^PIXELFORMATDESCRIPTOR) -> i32 #foreign
|
||||
SwapBuffers :: proc(hdc: HDC) -> BOOL #foreign
|
||||
|
||||
|
||||
WGL_CONTEXT_MAJOR_VERSION_ARB :: 0x2091
|
||||
WGL_CONTEXT_MINOR_VERSION_ARB :: 0x2092
|
||||
WGL_CONTEXT_PROFILE_MASK_ARB :: 0x9126
|
||||
WGL_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB :: 0x0002
|
||||
|
||||
wglCreateContext :: proc(hdc: HDC) -> HGLRC #foreign
|
||||
wglMakeCurrent :: proc(hdc: HDC, hglrc: HGLRC) -> BOOL #foreign
|
||||
wglGetProcAddress :: proc(c_str: ^u8) -> PROC #foreign
|
||||
wglDeleteContext :: proc(hglrc: HGLRC) -> BOOL #foreign
|
||||
|
||||
|
||||
|
||||
GetAsyncKeyState :: proc(v_key: i32) -> i16 #foreign
|
||||
|
||||
is_key_down :: proc(key: Key_Code) -> bool {
|
||||
return GetAsyncKeyState(key as i32) < 0
|
||||
}
|
||||
|
||||
Key_Code :: enum i32 {
|
||||
LBUTTON = 0x01,
|
||||
RBUTTON = 0x02,
|
||||
CANCEL = 0x03,
|
||||
MBUTTON = 0x04,
|
||||
|
||||
BACK = 0x08,
|
||||
TAB = 0x09,
|
||||
|
||||
CLEAR = 0x0C,
|
||||
RETURN = 0x0D,
|
||||
|
||||
SHIFT = 0x10,
|
||||
CONTROL = 0x11,
|
||||
MENU = 0x12,
|
||||
PAUSE = 0x13,
|
||||
CAPITAL = 0x14,
|
||||
|
||||
KANA = 0x15,
|
||||
HANGEUL = 0x15,
|
||||
HANGUL = 0x15,
|
||||
JUNJA = 0x17,
|
||||
FINAL = 0x18,
|
||||
HANJA = 0x19,
|
||||
KANJI = 0x19,
|
||||
|
||||
ESCAPE = 0x1B,
|
||||
|
||||
CONVERT = 0x1C,
|
||||
NONCONVERT = 0x1D,
|
||||
ACCEPT = 0x1E,
|
||||
MODECHANGE = 0x1F,
|
||||
|
||||
SPACE = 0x20,
|
||||
PRIOR = 0x21,
|
||||
NEXT = 0x22,
|
||||
END = 0x23,
|
||||
HOME = 0x24,
|
||||
LEFT = 0x25,
|
||||
UP = 0x26,
|
||||
RIGHT = 0x27,
|
||||
DOWN = 0x28,
|
||||
SELECT = 0x29,
|
||||
PRINT = 0x2A,
|
||||
EXECUTE = 0x2B,
|
||||
SNAPSHOT = 0x2C,
|
||||
INSERT = 0x2D,
|
||||
DELETE = 0x2E,
|
||||
HELP = 0x2F,
|
||||
|
||||
NUM0 = #rune "0",
|
||||
NUM1 = #rune "1",
|
||||
NUM2 = #rune "2",
|
||||
NUM3 = #rune "3",
|
||||
NUM4 = #rune "4",
|
||||
NUM5 = #rune "5",
|
||||
NUM6 = #rune "6",
|
||||
NUM7 = #rune "7",
|
||||
NUM8 = #rune "8",
|
||||
NUM9 = #rune "9",
|
||||
|
||||
A = #rune "A",
|
||||
B = #rune "B",
|
||||
C = #rune "C",
|
||||
D = #rune "D",
|
||||
E = #rune "E",
|
||||
F = #rune "F",
|
||||
G = #rune "G",
|
||||
H = #rune "H",
|
||||
I = #rune "I",
|
||||
J = #rune "J",
|
||||
K = #rune "K",
|
||||
L = #rune "L",
|
||||
M = #rune "M",
|
||||
N = #rune "N",
|
||||
O = #rune "O",
|
||||
P = #rune "P",
|
||||
Q = #rune "Q",
|
||||
R = #rune "R",
|
||||
S = #rune "S",
|
||||
T = #rune "T",
|
||||
U = #rune "U",
|
||||
V = #rune "V",
|
||||
W = #rune "W",
|
||||
X = #rune "X",
|
||||
Y = #rune "Y",
|
||||
Z = #rune "Z",
|
||||
|
||||
LWIN = 0x5B,
|
||||
RWIN = 0x5C,
|
||||
APPS = 0x5D,
|
||||
|
||||
NUMPAD0 = 0x60,
|
||||
NUMPAD1 = 0x61,
|
||||
NUMPAD2 = 0x62,
|
||||
NUMPAD3 = 0x63,
|
||||
NUMPAD4 = 0x64,
|
||||
NUMPAD5 = 0x65,
|
||||
NUMPAD6 = 0x66,
|
||||
NUMPAD7 = 0x67,
|
||||
NUMPAD8 = 0x68,
|
||||
NUMPAD9 = 0x69,
|
||||
MULTIPLY = 0x6A,
|
||||
ADD = 0x6B,
|
||||
SEPARATOR = 0x6C,
|
||||
SUBTRACT = 0x6D,
|
||||
DECIMAL = 0x6E,
|
||||
DIVIDE = 0x6F,
|
||||
F1 = 0x70,
|
||||
F2 = 0x71,
|
||||
F3 = 0x72,
|
||||
F4 = 0x73,
|
||||
F5 = 0x74,
|
||||
F6 = 0x75,
|
||||
F7 = 0x76,
|
||||
F8 = 0x77,
|
||||
F9 = 0x78,
|
||||
F10 = 0x79,
|
||||
F11 = 0x7A,
|
||||
F12 = 0x7B,
|
||||
F13 = 0x7C,
|
||||
F14 = 0x7D,
|
||||
F15 = 0x7E,
|
||||
F16 = 0x7F,
|
||||
F17 = 0x80,
|
||||
F18 = 0x81,
|
||||
F19 = 0x82,
|
||||
F20 = 0x83,
|
||||
F21 = 0x84,
|
||||
F22 = 0x85,
|
||||
F23 = 0x86,
|
||||
F24 = 0x87,
|
||||
|
||||
NUMLOCK = 0x90,
|
||||
SCROLL = 0x91,
|
||||
|
||||
LSHIFT = 0xA0,
|
||||
RSHIFT = 0xA1,
|
||||
LCONTROL = 0xA2,
|
||||
RCONTROL = 0xA3,
|
||||
LMENU = 0xA4,
|
||||
RMENU = 0xA5,
|
||||
PROCESSKEY = 0xE5,
|
||||
ATTN = 0xF6,
|
||||
CRSEL = 0xF7,
|
||||
EXSEL = 0xF8,
|
||||
EREOF = 0xF9,
|
||||
PLAY = 0xFA,
|
||||
ZOOM = 0xFB,
|
||||
NONAME = 0xFC,
|
||||
PA1 = 0xFD,
|
||||
OEM_CLEAR = 0xFE,
|
||||
}
|
||||
Reference in New Issue
Block a user