mirror of
https://github.com/Ed94/Odin.git
synced 2026-07-18 00:41:26 -07:00
using on struct/union fields
This commit is contained in:
+117
-25
@@ -1,26 +1,18 @@
|
||||
// Demo 001
|
||||
#load "basic.odin"
|
||||
#load "math.odin"
|
||||
#load "game.odin"
|
||||
|
||||
main :: proc() {
|
||||
// _ = hellope();
|
||||
// procedures();
|
||||
// variables();
|
||||
// constants();
|
||||
// types();
|
||||
// data_control();
|
||||
_ = hellope();
|
||||
procedures();
|
||||
variables();
|
||||
constants();
|
||||
types();
|
||||
data_control();
|
||||
using_fields();
|
||||
|
||||
|
||||
// run_game();
|
||||
|
||||
Thing :: type union {
|
||||
i: i32,
|
||||
f: f32,
|
||||
}
|
||||
t: Thing;
|
||||
t.f = 123;
|
||||
print_int_base(t.i as int, 16);
|
||||
print_nl();
|
||||
}
|
||||
|
||||
hellope :: proc() -> int {
|
||||
@@ -43,7 +35,7 @@ hellope :: proc() -> int {
|
||||
|
||||
apple, banana, carrot: bool;
|
||||
box, carboard: bool = true, false;
|
||||
hellope_value: int = hellope();
|
||||
hellope_value: int = hellope(); // The procedure is ran just before `main`
|
||||
|
||||
variables :: proc() {
|
||||
i: int; // initialized with zero value
|
||||
@@ -293,16 +285,16 @@ types :: proc() {
|
||||
}
|
||||
|
||||
BinaryNode :: type struct {
|
||||
left, right: ^BinaryNode, // same format as procedure argument
|
||||
data: rawptr,
|
||||
left, right: ^BinaryNode; // same format as procedure argument
|
||||
data: rawptr;
|
||||
}
|
||||
|
||||
AddProc :: type proc(a, b: int) -> int
|
||||
|
||||
Packed :: type struct #packed {
|
||||
a: u8,
|
||||
b: u16,
|
||||
c: u32,
|
||||
a: u8;
|
||||
b: u16;
|
||||
c: u32;
|
||||
}
|
||||
static_assert(size_of(Packed) == 7); // builtin procedure
|
||||
|
||||
@@ -383,6 +375,50 @@ types :: proc() {
|
||||
static_assert(Certain.TOMB == 8);
|
||||
}
|
||||
|
||||
{ // Untagged union
|
||||
BitHack :: type union {
|
||||
i: i32;
|
||||
f: f32;
|
||||
}
|
||||
b: BitHack;
|
||||
b.f = 123;
|
||||
print_int(b.i as int); print_nl();
|
||||
|
||||
|
||||
|
||||
// Manually tagged union
|
||||
|
||||
EntityKind :: type enum {
|
||||
Invalid,
|
||||
Constant,
|
||||
Variable,
|
||||
TypeName,
|
||||
Procedure,
|
||||
Builtin,
|
||||
Count,
|
||||
}
|
||||
|
||||
Entity :: type struct {
|
||||
kind: EntityKind;
|
||||
guid: u64;
|
||||
|
||||
// Other data
|
||||
|
||||
/*using*/
|
||||
data: union {
|
||||
constant: struct{};
|
||||
variable: struct{
|
||||
visited, is_field, used, anonymous: bool;
|
||||
};
|
||||
procedure: struct { used: bool };
|
||||
buitlin: struct { id: i32 };
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
// NOTE(bill): Tagged unions are not added yet but are planned
|
||||
}
|
||||
|
||||
|
||||
|
||||
{ // Compound Literals
|
||||
@@ -478,9 +514,9 @@ void main() {
|
||||
|
||||
{ // size, align, offset
|
||||
Thing :: type struct {
|
||||
a: u8,
|
||||
b: u16,
|
||||
c, d, e: u32,
|
||||
a: u8;
|
||||
b: u16;
|
||||
c, d, e: u32;
|
||||
}
|
||||
|
||||
s := size_of(Thing);
|
||||
@@ -619,3 +655,59 @@ data_control :: proc() {
|
||||
}
|
||||
|
||||
|
||||
using_fields :: proc() {
|
||||
{ // Everyday stuff
|
||||
Vec3 :: type struct { x, y, z: f32; }
|
||||
|
||||
Entity :: type struct {
|
||||
name: string;
|
||||
using pos: Vec3;
|
||||
vel: Vec3;
|
||||
}
|
||||
t: Entity;
|
||||
t.y = 456;
|
||||
print_f32(t.y); print_nl();
|
||||
print_f32(t.pos.y); print_nl();
|
||||
print_f32(t.vel.y); print_nl();
|
||||
|
||||
|
||||
Frog :: type struct { // Subtype (kind of)
|
||||
using entity: Entity;
|
||||
colour: u32;
|
||||
jump_height: f32;
|
||||
}
|
||||
|
||||
f: Frog;
|
||||
f.y = 1337;
|
||||
print_f32(f.y); print_nl();
|
||||
print_f32(f.pos.y); print_nl();
|
||||
print_f32(f.vel.y); print_nl();
|
||||
|
||||
|
||||
Buffalo :: type struct {
|
||||
using entity: Entity;
|
||||
speed: f32;
|
||||
noise_level: f32;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
{ // Crazy Shit
|
||||
Vec2 :: type union {
|
||||
using _xy: struct {x, y: f32};
|
||||
e: [2]f32;
|
||||
v: {2}f32;
|
||||
}
|
||||
|
||||
Entity :: type struct {
|
||||
using pos: ^Vec2;
|
||||
name: string;
|
||||
}
|
||||
t: Entity;
|
||||
t.pos = alloc(size_of(Vec2)) as ^Vec2; // TODO(bill): make an alloc type? i.e. new(Type)?
|
||||
t.x = 123;
|
||||
print_f32(t._xy.x); print_nl();
|
||||
print_f32(t.pos.x); print_nl();
|
||||
print_f32(t.pos._xy.x); print_nl();
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -3,7 +3,7 @@
|
||||
FileHandle :: type HANDLE;
|
||||
|
||||
File :: type struct {
|
||||
handle: FileHandle,
|
||||
handle: FileHandle;
|
||||
}
|
||||
|
||||
file_open :: proc(name: string) -> (File, bool) {
|
||||
|
||||
+8
-8
@@ -34,12 +34,12 @@ to_c_string :: proc(s: string) -> ^u8 {
|
||||
|
||||
|
||||
Window :: type struct {
|
||||
width, height: int,
|
||||
wc: WNDCLASSEXA,
|
||||
dc: HDC,
|
||||
hwnd: HWND,
|
||||
opengl_context, rc: HGLRC,
|
||||
c_title: ^u8,
|
||||
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) {
|
||||
@@ -123,8 +123,8 @@ display_window :: proc(w: ^Window) {
|
||||
|
||||
|
||||
Entity :: type struct {
|
||||
pos: Vec2,
|
||||
dim: Vec2,
|
||||
pos: Vec2;
|
||||
dim: Vec2;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -258,18 +258,18 @@ AllocatorProc :: type proc(allocator_data: rawptr, mode: AllocationMode,
|
||||
old_memory: rawptr, old_size: int, flags: u64) -> rawptr;
|
||||
|
||||
Allocator :: type struct {
|
||||
procedure: AllocatorProc,
|
||||
data: rawptr,
|
||||
procedure: AllocatorProc;
|
||||
data: rawptr;
|
||||
}
|
||||
|
||||
|
||||
Context :: type struct {
|
||||
thread_id: i32,
|
||||
thread_id: i32;
|
||||
|
||||
user_index: i32,
|
||||
user_data: rawptr,
|
||||
user_index: i32;
|
||||
user_data: rawptr;
|
||||
|
||||
allocator: Allocator,
|
||||
allocator: Allocator;
|
||||
}
|
||||
|
||||
#thread_local context: Context;
|
||||
|
||||
+18
-18
@@ -40,24 +40,24 @@ INVALID_HANDLE_VALUE :: (-1 as int) as HANDLE;
|
||||
WNDPROC :: type proc(hwnd: HWND, msg: u32, wparam: WPARAM, lparam: LPARAM) -> LRESULT
|
||||
|
||||
WNDCLASSEXA :: type struct {
|
||||
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,
|
||||
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 :: type struct {
|
||||
hwnd: HWND,
|
||||
message: u32,
|
||||
wparam: WPARAM,
|
||||
lparam: LPARAM,
|
||||
time: u32,
|
||||
pt: POINT,
|
||||
hwnd: HWND;
|
||||
message: u32;
|
||||
wparam: WPARAM;
|
||||
lparam: LPARAM;
|
||||
time: u32;
|
||||
pt: POINT;
|
||||
}
|
||||
|
||||
|
||||
@@ -189,7 +189,7 @@ wglCreateContextAttribsARBType :: type proc(hdc: HDC, hshareContext: rawptr, att
|
||||
PIXELFORMATDESCRIPTOR :: type struct {
|
||||
size,
|
||||
version,
|
||||
flags: u32,
|
||||
flags: u32;
|
||||
|
||||
pixel_type,
|
||||
color_bits,
|
||||
@@ -210,11 +210,11 @@ PIXELFORMATDESCRIPTOR :: type struct {
|
||||
stencil_bits,
|
||||
aux_buffers,
|
||||
layer_type,
|
||||
reserved: byte,
|
||||
reserved: byte;
|
||||
|
||||
layer_mask,
|
||||
visible_mask,
|
||||
damage_mask: u32,
|
||||
damage_mask: u32;
|
||||
}
|
||||
|
||||
GetDC :: proc(h: HANDLE) -> HDC #foreign
|
||||
|
||||
Reference in New Issue
Block a user