mirror of
https://github.com/Ed94/Odin.git
synced 2026-08-07 16:18:52 +00:00
Fix old_demos; Fix when bug; Fix enum .names
This commit is contained in:
@@ -1,14 +1,14 @@
|
||||
#import "atomic.odin";
|
||||
#import "hash.odin";
|
||||
#import "mem.odin";
|
||||
#import "opengl.odin";
|
||||
#import "strconv.odin";
|
||||
#import "sync.odin";
|
||||
#import win32 "sys/windows.odin";
|
||||
// import "core:atomic.odin";
|
||||
import "core:hash.odin";
|
||||
import "core:mem.odin";
|
||||
import "core:opengl.odin";
|
||||
import "core:strconv.odin";
|
||||
import "core:sync.odin";
|
||||
import win32 "core:sys/windows.odin";
|
||||
|
||||
#import "fmt.odin";
|
||||
#import "os.odin";
|
||||
#import "math.odin";
|
||||
import "core:fmt.odin";
|
||||
import "core:os.odin";
|
||||
import "core:math.odin";
|
||||
|
||||
|
||||
main :: proc() {
|
||||
@@ -55,7 +55,7 @@ when true {
|
||||
for i in 0..16 {
|
||||
}
|
||||
// Is similar to
|
||||
for _i := 0; _i < 16; _i++ { immutable i := _i;
|
||||
for i := 0; i < 16; i += 1 {
|
||||
}
|
||||
}
|
||||
|
||||
@@ -77,16 +77,15 @@ when true {
|
||||
}
|
||||
|
||||
{
|
||||
t := type_info(int);
|
||||
using Type_Info;
|
||||
match i in t {
|
||||
case Integer, Float:
|
||||
t := type_info_of(int);
|
||||
match i in t.variant {
|
||||
case Type_Info_Integer, Type_Info_Float:
|
||||
fmt.println("It's a number");
|
||||
}
|
||||
|
||||
|
||||
x: any = 123;
|
||||
foo match i in x {
|
||||
foo: match i in x {
|
||||
case int, f32:
|
||||
fmt.println("It's an int or f32");
|
||||
break foo;
|
||||
@@ -112,21 +111,20 @@ when true {
|
||||
|
||||
{
|
||||
// Slices now store a capacity
|
||||
buf: [256]byte;
|
||||
s: []byte;
|
||||
buf: [256]u8;
|
||||
s: []u8;
|
||||
s = buf[..0]; // == buf[0..0];
|
||||
fmt.println("count =", s.count);
|
||||
fmt.println("capacity =", s.capacity);
|
||||
append(s, 1, 2, 3);
|
||||
fmt.println("count =", len(s));
|
||||
fmt.println("capacity =", cap(s));
|
||||
append(&s, 1, 2, 3);
|
||||
fmt.println(s);
|
||||
|
||||
s = buf[1..2..3];
|
||||
fmt.println("count =", s.count);
|
||||
fmt.println("capacity =", s.capacity);
|
||||
fmt.println("count =", len(s));
|
||||
fmt.println("capacity =", cap(s));
|
||||
fmt.println(s);
|
||||
|
||||
clear(s); // Sets count to zero
|
||||
s.count = 0; // Equivalent
|
||||
clear(&s); // Sets count to zero
|
||||
}
|
||||
|
||||
{
|
||||
@@ -136,11 +134,11 @@ when true {
|
||||
flags: u32,
|
||||
}
|
||||
foo_array: [256]Foo;
|
||||
foo_as_bytes: []byte = slice_to_bytes(foo_array[..]);
|
||||
foo_as_bytes: []u8 = mem.slice_to_bytes(foo_array[..]);
|
||||
// Useful for things like
|
||||
// os.write(handle, foo_as_bytes);
|
||||
|
||||
foo_slice := slice_ptr(cast(^Foo)foo_as_bytes.data, foo_as_bytes.count/size_of(Foo), foo_as_bytes.capacity/size_of(Foo));
|
||||
foo_slice := mem.slice_ptr(cast(^Foo)&foo_as_bytes[0], len(foo_as_bytes)/size_of(Foo), cap(foo_as_bytes)/size_of(Foo));
|
||||
// Question: Should there be a bytes_to_slice procedure or is it clearer to do this even if it is error prone?
|
||||
// And if so what would the syntax be?
|
||||
// slice_transmute([]Foo, foo_as_bytes);
|
||||
@@ -161,33 +159,26 @@ when true {
|
||||
fmt.println(i);
|
||||
}
|
||||
|
||||
compile_assert(size_of([vector 7]bool) == size_of([7]bool));
|
||||
compile_assert(size_of([vector 7]i32) == size_of([7]i32));
|
||||
compile_assert(size_of([vector 7]bool) >= size_of([7]bool));
|
||||
compile_assert(size_of([vector 7]i32) >= size_of([7]i32));
|
||||
// align_of([vector 7]i32) != align_of([7]i32) // this may be the case
|
||||
}
|
||||
|
||||
{
|
||||
// fmt.* changes
|
||||
// bprint* returns `int` (bytes written)
|
||||
// sprint* returns `string` (bytes written as a string)
|
||||
// bprint* returns `string`
|
||||
|
||||
data: [256]byte;
|
||||
str := fmt.sprintf(data[..0], "Hellope %d %s %c", 123, "others", '!');
|
||||
data: [256]u8;
|
||||
str := fmt.bprintf(data[..], "Hellope %d %s %c", 123, "others", '!');
|
||||
fmt.println(str);
|
||||
|
||||
buf := data[..0];
|
||||
count := fmt.bprintf(^buf, "Hellope %d %s %c", 321, "y'all", '!');
|
||||
fmt.println(cast(string)buf[..count]);
|
||||
|
||||
// NOTE(bill): We may change this but because this is a library feature, I am not that bothered yet
|
||||
}
|
||||
|
||||
{
|
||||
x: [dynamic]f64;
|
||||
reserve(x, 16);
|
||||
reserve(&x, 16);
|
||||
defer free(x); // `free` is overloaded for numerous types
|
||||
// Number literals can have underscores in them for readability
|
||||
append(x, 2_000_000.500_000, 123, 5, 7); // variadic append
|
||||
append(&x, 2_000_000.500_000, 123, 5, 7); // variadic append
|
||||
|
||||
for p, i in x {
|
||||
if i > 0 { fmt.print(", "); }
|
||||
@@ -201,13 +192,13 @@ when true {
|
||||
x := [dynamic]f64{2_000_000.500_000, 3, 5, 7};
|
||||
defer free(x);
|
||||
fmt.println(x); // fmt.print* supports printing of dynamic types
|
||||
clear(x);
|
||||
clear(&x);
|
||||
fmt.println(x);
|
||||
}
|
||||
|
||||
{
|
||||
m: map[f32]int;
|
||||
reserve(m, 16);
|
||||
reserve(&m, 16);
|
||||
defer free(m);
|
||||
|
||||
m[1.0] = 1278;
|
||||
@@ -241,12 +232,12 @@ when true {
|
||||
assert(ok && c == 7654);
|
||||
fmt.println(m);
|
||||
|
||||
delete(m, "c"); // deletes entry with key "c"
|
||||
delete(&m, "c"); // deletes entry with key "c"
|
||||
_, found := m["c"];
|
||||
assert(!found);
|
||||
|
||||
fmt.println(m);
|
||||
clear(m);
|
||||
clear(&m);
|
||||
fmt.println(m);
|
||||
|
||||
// NOTE: Fixed size maps are planned but we have not yet implemented
|
||||
@@ -257,7 +248,21 @@ when true {
|
||||
Vector3 :: struct{x, y, z: f32};
|
||||
Quaternion :: struct{x, y, z, w: f32};
|
||||
|
||||
Entity :: union {
|
||||
// Variants
|
||||
Frog :: struct {
|
||||
ribbit_volume: f32,
|
||||
jump_height: f32,
|
||||
}
|
||||
Door :: struct {
|
||||
openness: f32,
|
||||
}
|
||||
Map :: struct {
|
||||
width, height: f32,
|
||||
place_positions: []Vector3,
|
||||
place_names: []string,
|
||||
}
|
||||
|
||||
Entity :: struct {
|
||||
// Common Fields
|
||||
id: u64,
|
||||
name: string,
|
||||
@@ -265,25 +270,13 @@ when true {
|
||||
orientation: Quaternion,
|
||||
flags: u32,
|
||||
|
||||
// Variants
|
||||
Frog{
|
||||
ribbit_volume: f32,
|
||||
jump_height: f32,
|
||||
},
|
||||
Door{
|
||||
openness: f32,
|
||||
},
|
||||
Map{
|
||||
width, height: f32,
|
||||
place_positions: []Vector3,
|
||||
place_names: []string,
|
||||
},
|
||||
variant: union { Frog, Door, Map },
|
||||
}
|
||||
|
||||
entity: Entity;
|
||||
entity.id = 1337;
|
||||
// implicit conversion from variant to base type
|
||||
entity = Entity.Frog{
|
||||
id = 1337,
|
||||
entity.variant = Frog{
|
||||
ribbit_volume = 0.5,
|
||||
jump_height = 2.1,
|
||||
/*other data */
|
||||
@@ -292,26 +285,25 @@ when true {
|
||||
entity.name = "Frank";
|
||||
entity.position = Vector3{1, 4, 9};
|
||||
|
||||
using Entity;
|
||||
match e in entity {
|
||||
match e in entity.variant {
|
||||
case Frog:
|
||||
fmt.println("Ribbit");
|
||||
case Door:
|
||||
fmt.println("Creak");
|
||||
case Map:
|
||||
fmt.println("Rustle");
|
||||
default:
|
||||
case:
|
||||
fmt.println("Just a normal entity");
|
||||
}
|
||||
|
||||
if frog, ok := union_cast(Frog)entity; ok {
|
||||
fmt.printf("The frog jumps %f feet high at %v\n", frog.jump_height, frog.position);
|
||||
if frog, ok := entity.variant.(Frog); ok {
|
||||
fmt.printf("The frog jumps %f feet high at %v\n", frog.jump_height, entity.position);
|
||||
}
|
||||
|
||||
// Panics if not the correct type
|
||||
frog: Frog;
|
||||
frog = union_cast(Frog)entity;
|
||||
frog, _ = union_cast(Frog)entity; // ignore error and force cast
|
||||
frog = entity.variant.(Frog);
|
||||
frog, _ = entity.variant.(Frog); // ignore error and force cast
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user