mirror of
https://github.com/Ed94/Odin.git
synced 2026-07-27 18:00:05 +00:00
print_(f32|f64)
This commit is contained in:
+58
-20
@@ -1,7 +1,5 @@
|
||||
#load "runtime.odin"
|
||||
|
||||
TWO_HEARTS :: '💕';
|
||||
|
||||
print_string :: proc(s: string) {
|
||||
for i := 0; i < len(s); i++ {
|
||||
putchar(s[i] as i32);
|
||||
@@ -15,10 +13,10 @@ byte_reverse :: proc(b: []byte) {
|
||||
}
|
||||
}
|
||||
|
||||
encode_rune :: proc(r : rune) -> ([4]byte, int) {
|
||||
buf : [4]byte;
|
||||
encode_rune :: proc(r: rune) -> ([4]byte, int) {
|
||||
buf: [4]byte;
|
||||
i := r as u32;
|
||||
mask : byte : 0x3f;
|
||||
mask: byte : 0x3f;
|
||||
if i <= 1<<7-1 {
|
||||
buf[0] = r as byte;
|
||||
return buf, 1;
|
||||
@@ -49,16 +47,16 @@ encode_rune :: proc(r : rune) -> ([4]byte, int) {
|
||||
return buf, 4;
|
||||
}
|
||||
|
||||
print_rune :: proc(r : rune) {
|
||||
print_rune :: proc(r: rune) {
|
||||
buf, n := encode_rune(r);
|
||||
str := buf[:n] as string;
|
||||
print_string(str);
|
||||
}
|
||||
|
||||
print_int :: proc(i : int) {
|
||||
print_int :: proc(i: int) {
|
||||
print_int_base(i, 10);
|
||||
}
|
||||
print_int_base :: proc(i, base : int) {
|
||||
print_int_base :: proc(i, base: int) {
|
||||
NUM_TO_CHAR_TABLE :: "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz@$";
|
||||
|
||||
buf: [65]byte;
|
||||
@@ -87,19 +85,14 @@ print_int_base :: proc(i, base : int) {
|
||||
print_string(buf[:len] as string);
|
||||
}
|
||||
|
||||
print_uint :: proc(i : uint) {
|
||||
print_uint_base(i, 10);
|
||||
print_uint :: proc(i: uint) {
|
||||
print__uint(i, 10, 0, ' ');
|
||||
}
|
||||
print_uint_base :: proc(i, base : uint) {
|
||||
print__uint :: proc(i, base: uint, min_width: int, pad_char: byte) {
|
||||
NUM_TO_CHAR_TABLE :: "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz@$";
|
||||
|
||||
buf: [65]byte;
|
||||
len := 0;
|
||||
negative := false;
|
||||
if i < 0 {
|
||||
negative = true;
|
||||
i = -i;
|
||||
}
|
||||
if i == 0 {
|
||||
buf[len] = '0';
|
||||
len++;
|
||||
@@ -109,9 +102,8 @@ print_uint_base :: proc(i, base : uint) {
|
||||
len++;
|
||||
i /= base;
|
||||
}
|
||||
|
||||
if negative {
|
||||
buf[len] = '-';
|
||||
for len < min_width {
|
||||
buf[len] = pad_char;
|
||||
len++;
|
||||
}
|
||||
|
||||
@@ -119,9 +111,55 @@ print_uint_base :: proc(i, base : uint) {
|
||||
print_string(buf[:len] as string);
|
||||
}
|
||||
|
||||
|
||||
print_bool :: proc(b : bool) {
|
||||
if b { print_string("true"); }
|
||||
else { print_string("false"); }
|
||||
}
|
||||
|
||||
print_pointer :: proc(p: rawptr) #inline { print__uint(p as uint, 16, 0, ' '); }
|
||||
|
||||
print_f32 :: proc(f: f32) #inline { print__f64(f as f64, 7); }
|
||||
print_f64 :: proc(f: f64) #inline { print__f64(f, 10); }
|
||||
|
||||
print__f64 :: proc(f: f64, decimal_places: int) {
|
||||
if f == 0 {
|
||||
print_rune('0');
|
||||
return;
|
||||
}
|
||||
if f < 0 {
|
||||
print_rune('-');
|
||||
f = -f;
|
||||
}
|
||||
|
||||
print_u64 :: proc(i: u64) {
|
||||
NUM_TO_CHAR_TABLE :: "0123456789";
|
||||
|
||||
buf: [22]byte;
|
||||
len := 0;
|
||||
if i == 0 {
|
||||
buf[len] = '0';
|
||||
len++;
|
||||
}
|
||||
for i > 0 {
|
||||
buf[len] = NUM_TO_CHAR_TABLE[i % 10];
|
||||
len++;
|
||||
i /= 10;
|
||||
}
|
||||
byte_reverse(buf[:len]);
|
||||
print_string(buf[:len] as string);
|
||||
}
|
||||
|
||||
i := f as u64;
|
||||
print_u64(i);
|
||||
f -= i as f64;
|
||||
|
||||
print_rune('.');
|
||||
|
||||
mult := 10.0;
|
||||
for decimal_places := 6; decimal_places >= 0; decimal_places-- {
|
||||
i = (f * mult) as u64;
|
||||
print_u64(i as u64);
|
||||
f -= i as f64 / mult;
|
||||
mult *= 10;
|
||||
}
|
||||
}
|
||||
|
||||
+567
-694
File diff suppressed because it is too large
Load Diff
+32
-34
@@ -1,9 +1,10 @@
|
||||
#load "basic.odin"
|
||||
#load "win32.odin"
|
||||
#load "opengl.odin"
|
||||
#load "stb_image.odin"
|
||||
#load "math.odin"
|
||||
|
||||
TWO_HEARTS :: '💕';
|
||||
|
||||
win32_perf_count_freq := GetQueryPerformanceFrequency();
|
||||
time_now :: proc() -> f64 {
|
||||
if win32_perf_count_freq == 0 {
|
||||
@@ -26,7 +27,7 @@ win32_print_last_error :: proc() {
|
||||
|
||||
// Yuk!
|
||||
to_c_string :: proc(s: string) -> ^u8 {
|
||||
c_str := heap_alloc(len(s)+1) as ^u8;
|
||||
c_str: ^u8 = heap_alloc(len(s)+1);
|
||||
mem_copy(c_str, ^s[0], len(s));
|
||||
c_str[len(s)] = 0;
|
||||
return c_str;
|
||||
@@ -38,20 +39,12 @@ type Window: struct {
|
||||
wc: WNDCLASSEXA,
|
||||
dc: HDC,
|
||||
hwnd: HWND,
|
||||
opengl_context: HGLRC,
|
||||
opengl_context: rawptr,
|
||||
rc: HGLRC,
|
||||
c_title: ^u8,
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
make_window :: proc(title: string, msg, height: int) -> (Window, bool) {
|
||||
make_window :: proc(title: string, msg, height: int, window_proc: WNDPROC) -> (Window, bool) {
|
||||
w: Window;
|
||||
w.width, w.height = msg, height;
|
||||
|
||||
@@ -66,7 +59,7 @@ make_window :: proc(title: string, msg, height: int) -> (Window, bool) {
|
||||
style = CS_VREDRAW | CS_HREDRAW,
|
||||
hInstance = instance as HINSTANCE,
|
||||
className = c_class_name,
|
||||
wndProc = win32_proc,
|
||||
wndProc = window_proc,
|
||||
};
|
||||
|
||||
if RegisterClassExA(^w.wc) == 0 {
|
||||
@@ -126,23 +119,6 @@ destroy_window :: proc(w: ^Window) {
|
||||
heap_free(w.c_title);
|
||||
}
|
||||
|
||||
update_window :: proc(w: ^Window) -> bool {
|
||||
msg: MSG;
|
||||
for {
|
||||
ok := PeekMessageA(^msg, null, 0, 0, PM_REMOVE) != 0;
|
||||
if !ok {
|
||||
break;
|
||||
}
|
||||
|
||||
if msg.message == WM_QUIT {
|
||||
return true;
|
||||
}
|
||||
_ = TranslateMessage(^msg);
|
||||
_ = DispatchMessageA(^msg);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
display_window :: proc(w: ^Window) {
|
||||
SwapBuffers(w.dc);
|
||||
}
|
||||
@@ -151,12 +127,23 @@ display_window :: proc(w: ^Window) {
|
||||
|
||||
|
||||
main :: proc() {
|
||||
window, window_success := make_window("Odin Language Demo", 854, 480);
|
||||
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);
|
||||
}
|
||||
print_f64(13.37);
|
||||
print_rune('\n');
|
||||
/*
|
||||
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;
|
||||
for running {
|
||||
@@ -164,8 +151,13 @@ main :: proc() {
|
||||
dt := (curr_time - prev_time) as f32;
|
||||
prev_time = curr_time;
|
||||
|
||||
if update_window(^window) {
|
||||
running = false;
|
||||
msg: MSG;
|
||||
for PeekMessageA(^msg, null, 0, 0, PM_REMOVE) > 0 {
|
||||
if msg.message == WM_QUIT {
|
||||
running = false;
|
||||
}
|
||||
_ = TranslateMessage(^msg);
|
||||
_ = DispatchMessageA(^msg);
|
||||
}
|
||||
|
||||
glClearColor(0.5, 0.7, 1.0, 1.0);
|
||||
@@ -176,6 +168,7 @@ main :: proc() {
|
||||
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);
|
||||
@@ -187,9 +180,14 @@ main :: proc() {
|
||||
glEnd();
|
||||
}
|
||||
|
||||
x, y : f32 = 100, 100;
|
||||
x, y : f32 = 100+50*sinf(curr_time as f32), 100;
|
||||
draw_rect(x, y, 50, 50);
|
||||
|
||||
display_window(^window);
|
||||
ms_to_sleep := (16 - 1000*dt) as i32;
|
||||
if ms_to_sleep > 0 {
|
||||
sleep_ms(ms_to_sleep);
|
||||
}
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
+5
-4
@@ -5,14 +5,15 @@ type Mat2: {4}f32
|
||||
|
||||
|
||||
|
||||
sqrt_f32 :: proc(x: f32) -> f32 #foreign "llvm.sqrt.f32"
|
||||
sqrt_f64 :: proc(x: f64) -> f64 #foreign "llvm.sqrt.f64"
|
||||
sqrtf :: proc(x: f32) -> f32 #foreign "llvm.sqrt.f32"
|
||||
sinf :: proc(x: f32) -> f32 #foreign "llvm.sin.f32"
|
||||
cosf :: proc(x: f32) -> f32 #foreign "llvm.cos.f32"
|
||||
|
||||
vec2_dot :: proc(a, b: Vec2) -> f32 { c := a*b; return c[0] + c[1]; }
|
||||
vec3_dot :: proc(a, b: Vec3) -> f32 { c := a*b; return c[0] + c[1] + c[2]; }
|
||||
|
||||
lerp :: proc(a, b, t: f32) -> f32 { return a*(1-t) + b*t; }
|
||||
|
||||
vec2_mag :: proc(a: Vec2) -> f32 { return sqrt_f32(vec2_dot(a, a)); }
|
||||
vec3_mag :: proc(a: Vec3) -> f32 { return sqrt_f32(vec3_dot(a, a)); }
|
||||
vec2_mag :: proc(a: Vec2) -> f32 { return sqrtf(vec2_dot(a, a)); }
|
||||
vec3_mag :: proc(a: Vec3) -> f32 { return sqrtf(vec3_dot(a, a)); }
|
||||
|
||||
|
||||
+12
-12
@@ -24,20 +24,20 @@ PM_REMOVE :: 1;
|
||||
COLOR_BACKGROUND: rawptr : 1; // NOTE(bill): cast to HBRUSH when needed
|
||||
|
||||
|
||||
type HANDLE: rawptr
|
||||
type HWND: HANDLE
|
||||
type HDC: HANDLE
|
||||
type HANDLE: rawptr
|
||||
type HWND: HANDLE
|
||||
type HDC: HANDLE
|
||||
type HINSTANCE: HANDLE
|
||||
type HICON: HANDLE
|
||||
type HCURSOR: HANDLE
|
||||
type HMENU: HANDLE
|
||||
type HBRUSH: HANDLE
|
||||
type WPARAM: uint
|
||||
type LPARAM: int
|
||||
type LRESULT: int
|
||||
type ATOM: i16
|
||||
type HICON: HANDLE
|
||||
type HCURSOR: HANDLE
|
||||
type HMENU: HANDLE
|
||||
type HBRUSH: HANDLE
|
||||
type WPARAM: uint
|
||||
type LPARAM: int
|
||||
type LRESULT: int
|
||||
type ATOM: i16
|
||||
type BOOL: i32
|
||||
type POINT: struct { x, y: i32 }
|
||||
type BOOL: i32
|
||||
|
||||
type WNDPROC: proc(hwnd: HWND, msg: u32, wparam: WPARAM, lparam: LPARAM) -> LRESULT
|
||||
|
||||
|
||||
Reference in New Issue
Block a user