s |
focus search bar ( enter to select, ▲ / ▼ to change selection) |
g e |
go to examples |
g m |
go to modules |
g o |
go to overview |
g r |
go to reference |
h |
toggle this help ( esc also exits) |
usingA keyword that lets you import other namespaces into your namespace 1
things :: enum {
FIRST,
SECOND
}
enum_test :: () {
printf("things.FIRST is: %d\n", things.FIRST);
printf("things.SECOND is: %d\n", things.SECOND);
using things.members;
printf("FIRST is: %d\n", FIRST);
printf("SECOND is: %d\n", SECOND);
}
Function parameters and nested namespaces can be declared with using:
// from: https://youtu.be/ZHqFrNyLlpA?t=1472
Vector3 :: struct {
x : float = 1;
y : float = 4;
z : float = 9;
}
print_position_test :: () {
Entity :: struct {
position : Vector3;
}
print_position_a :: (entity : ^Entity) {
printf("print_position_a: (%f, %f, %f)\n", entity.position.x, entity.position.y, entity.position.z);
}
print_position_b :: (entity : ^Entity) {
using entity;
printf("print_position_b: (%f, %f, %f)\n", position.x, position.y, position.z);
}
print_position_c :: (using entity : ^Entity) {
printf("print_position_c: (%f, %f, %f)\n", position.x, position.y, position.z);
}
print_position_d :: (entity : ^Entity) {
using entity.position;
printf("print_position_d: (%f, %f, %f)\n", x, y, z);
}
e : Entity;
print_position_a(^e);
print_position_b(^e);
print_position_c(^e);
print_position_d(^e);
}
using can import a namespace into a struct, and that namespace is addressable: 2
// from: https://youtu.be/ZHqFrNyLlpA?t=1835
Vector3 :: struct {
x : float = 1;
y : float = 4;
z : float = 9;
}
door_test :: () {
Entity :: struct {
position : Vector3;
}
Door :: struct {
using entity : Entity;
openness_current : float = 0;
openness_target : float = 0;
}
door : Door;
// since the struct has access to the entity namespace, we don't need to say: door.entity.position, just door.position
printf("door_test: door.position is (%f, %f, %f)\n", door.position.x, door.position.y, door.position.z);
// but, since entity is named, we can take a pointer to it if we want
e : ^Entity = ^door.entity;
printf("e.position.x is %f\n", e.position.x);
}
using can import a pointer to a namespace into a struct: 3
// from: https://youtu.be/ZHqFrNyLlpA?t=2182
Vector3 :: struct {
x : float = 1;
y : float = 4;
z : float = 9;
}
door_test_pointer :: () {
Entity :: struct {
position : Vector3;
}
Door :: struct {
using entity : ^ Entity; // default value is null
openness_current : float = 0;
openness_target : float = 0;
}
door : Door;
door.entity = new Entity; // must set pointer before dereferencing!
printf("door_test_pointer : door.position is (%f, %f, %f)\n", door.position.x, door.position.y, door.position.z);
print_position :: (using entity : ^ Entity) {
printf("door_test_pointer : position is (%f, %f, %f)\n", position.x, position.y, position.z);
}
print_position(^door);
}
By using pointers to structs, you can control the memory packing of the referenced structs: 4
// from: https://youtu.be/ZHqFrNyLlpA?t=2330
Vector3 :: struct {
x : float = 1;
y : float = 4;
z : float = 9;
}
door_test_pointer :: () {
Entity :: struct {
position : Vector3;
}
Door :: struct {
using entity : ^ Entity; // default value is null
openness_current : float = 0;
openness_target : float = 0;
}
MAX_ENTITIES :: 100;
num_entities : int = 0;
entities : [MAX_ENTITIES] Entity;
get_next_entity :: (entities : [] Entity, num_entities : ^ int) -> ^ Entity {
result := ^entities[*num_entities];
*num_entities += 1;
return result;
}
door : Door;
door.entity = get_next_entity(entities, ^num_entities);
printf("door_test_packed: position is (%f, %f, %f)\n", door.position.x, door.position.y, door.position.z);
}
Composition via using allows fine-grained control over data packing and visitation: 5
Entity_Hot :: Struct {
position : Vector3;
orientation : Quaternion;
scale : float = 1;
flags : u32;
}
Entity_Cold :: Struct {
label : ^ u8;
group : ^ Group;
// ...more
}
Entity :: struct {
using hot : ^ Entity_Hot;
using cold : ^ Entity_Cold;
}
Door :: struct {
using entity : Entity;
openness_current : float = 0;
}
using can import a function to be implicitly invoked when accessing members of a struct:
// from: https://www.youtube.com/watch?v=ZHqFrNyLlpA&t=4175
MAX_ENTITIES :: 100;
Entity :: struct {
position : Vector3;
}
entities : [MAX_ENTITIES] Entity;
door_test_function :: () {
Door :: struct {
using entity_proc :: (door : ^Door) -> ^Entity {
return ^entities[door.entity_index];
}
entity_index : u16;
openness_current : float;
openness_target : float;
}
num_entities : int = 0;
get_next_entity :: (num_entities : ^int) -> u16 {
result := cast(*num_entities, u16);
*num_entities += 1;
return result;
}
door : Door;
door.entity_index = get_next_entity(^num_entities);
printf("door_test_function: door.position is (%f, %f, %f)\n", door.position.x, door.position.y, door.position.z);
}
using is a keyword that lets you import other namespaces into your namespace.
“Data-Oriented Demo: SOA, composition.” YouTube, uploaded by Jonathan Blow, Jan 21, 2015, https://youtu.be/ZHqFrNyLlpA?t=1296 ⮌
Now that I’m using that, now I can just say FIRST, SECOND, and THIRD down here, without saying things.whatever. I’m just importing those into my namespace directly, into this function.
“Data-Oriented Demo: SOA, composition.” YouTube, uploaded by Jonathan Blow, Jan 21, 2015, https://youtu.be/ZHqFrNyLlpA?t=1392 ⮌
Now the difference is, in the previous function, the thing that we were using was all constants. They didn’t involve storage or anything, it was just a bunch of enums that expand to constants. But now we’re importing a live pointer. [..] We’re using entity, we don’t have to say entity.position.whatever, we’re just saying position.x, position.y, position.z. But what those mean is dereferencing this live pointer that we’re using, rather than constants in some constant namespace.
“Data-Oriented Demo: SOA, composition.” YouTube, uploaded by Jonathan Blow, Jan 21, 2015, https://youtu.be/ZHqFrNyLlpA?t=1522 ⮌
Now that we’ve got our entity in a different place in memory, instead of just new-ing on the heap, which is kind of dumb and doesn’t really help you, we’re going to store them in a more efficient way.
“Data-Oriented Demo: SOA, composition.” YouTube, uploaded by Jonathan Blow, Jan 21, 2015, https://youtu.be/ZHqFrNyLlpA?t=2330 ⮌
What I would like to do, is break an entity into a couple of parts. I want to break in into the part that I want to visit really a lot from tight loops all of the time and that needs to be just cranked through as fast as the cpu can. And then I want to split it into the other part [..] that doesn’t get used as often, they don’t really get used in tight loops, and they’re not as performance critical.
“Data-Oriented Demo: SOA, composition.” YouTube, uploaded by Jonathan Blow, Jan 21, 2015, https://youtu.be/ZHqFrNyLlpA?t=2605 ⮌
|
jailang
2019 pixeldroid
https://github.com/pixeldroid/jailang |
| programming pages theme v0.5.21 (https://github.com/pixeldroid/programming-pages) |
s |
focus search bar ( enter to select, ▲ / ▼ to change selection) |
g e |
go to examples |
g m |
go to modules |
g o |
go to overview |
g r |
go to reference |
h |
toggle this help ( esc also exits) |