Rename object
This commit is contained in:
parent
d3e9475130
commit
946934ce09
53
main.c
53
main.c
@ -17,6 +17,8 @@
|
|||||||
#define min(A, B) (A < B ? A : B)
|
#define min(A, B) (A < B ? A : B)
|
||||||
#define max(A, B) (A > B ? A : B)
|
#define max(A, B) (A > B ? A : B)
|
||||||
|
|
||||||
|
#define OBJECT_COMMON struct {Position position; Scale scale;}
|
||||||
|
|
||||||
typedef struct Position Position;
|
typedef struct Position Position;
|
||||||
struct Position {
|
struct Position {
|
||||||
f32 x;
|
f32 x;
|
||||||
@ -35,16 +37,19 @@ struct Velocity {
|
|||||||
f32 y;
|
f32 y;
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef struct Object Object;
|
typedef struct Wanderer Wanderer;
|
||||||
struct Object {
|
struct Wanderer {
|
||||||
Position position;
|
OBJECT_COMMON;
|
||||||
Scale scale;
|
|
||||||
Velocity velocity;
|
Velocity velocity;
|
||||||
};
|
};
|
||||||
|
|
||||||
void init_object(Object *object, XOR256State *state);
|
typedef struct SlowZone SlowZone;
|
||||||
void update_object(Object *object);
|
struct SlowZone {
|
||||||
void move_object(Object *object);
|
OBJECT_COMMON;
|
||||||
|
};
|
||||||
|
|
||||||
|
void init_wanderer(Wanderer *wanderer, XOR256State *state);
|
||||||
|
void move_wanderer(Wanderer *wanderer);
|
||||||
f32 get_random_float(XOR256State *state);
|
f32 get_random_float(XOR256State *state);
|
||||||
|
|
||||||
int main(void) {
|
int main(void) {
|
||||||
@ -53,18 +58,18 @@ int main(void) {
|
|||||||
|
|
||||||
Allocator arena = wapp_mem_arena_allocator_init(MB(10));
|
Allocator arena = wapp_mem_arena_allocator_init(MB(10));
|
||||||
XOR256State state = wapp_prng_xorshift_init_state();
|
XOR256State state = wapp_prng_xorshift_init_state();
|
||||||
Object *objects = wapp_mem_allocator_alloc(&arena, sizeof(Object) * OBJECT_COUNT);
|
Wanderer *objects = wapp_mem_allocator_alloc(&arena, sizeof(Wanderer) * OBJECT_COUNT);
|
||||||
assert(objects != NULL);
|
assert(objects != NULL);
|
||||||
|
|
||||||
for (u64 i = 0; i < OBJECT_COUNT; ++i) {
|
for (u64 i = 0; i < OBJECT_COUNT; ++i) {
|
||||||
Object *object = &(objects[i]);
|
Wanderer *object = &(objects[i]);
|
||||||
init_object(object, &state);
|
init_wanderer(object, &state);
|
||||||
}
|
}
|
||||||
|
|
||||||
while (!WindowShouldClose()) {
|
while (!WindowShouldClose()) {
|
||||||
for (u64 i = 0; i < OBJECT_COUNT; ++i) {
|
for (u64 i = 0; i < OBJECT_COUNT; ++i) {
|
||||||
Object *object = &(objects[i]);
|
Wanderer *object = &(objects[i]);
|
||||||
move_object(object);
|
move_wanderer(object);
|
||||||
}
|
}
|
||||||
|
|
||||||
BeginDrawing();
|
BeginDrawing();
|
||||||
@ -72,7 +77,7 @@ int main(void) {
|
|||||||
ClearBackground(BG_COLOR);
|
ClearBackground(BG_COLOR);
|
||||||
|
|
||||||
for (u64 i = 0; i < OBJECT_COUNT; ++i) {
|
for (u64 i = 0; i < OBJECT_COUNT; ++i) {
|
||||||
Object *object = &(objects[i]);
|
Wanderer *object = &(objects[i]);
|
||||||
DrawRectangle(
|
DrawRectangle(
|
||||||
(i32)object->position.x,
|
(i32)object->position.x,
|
||||||
(i32)object->position.y,
|
(i32)object->position.y,
|
||||||
@ -92,33 +97,33 @@ int main(void) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void init_object(Object *object, XOR256State *state) {
|
void init_wanderer(Wanderer *wanderer, XOR256State *state) {
|
||||||
object->position = (Position){
|
wanderer->position = (Position){
|
||||||
.x = wapp_prng_xorshift_256(state) % WIDTH,
|
.x = wapp_prng_xorshift_256(state) % WIDTH,
|
||||||
.y = wapp_prng_xorshift_256(state) % HEIGHT,
|
.y = wapp_prng_xorshift_256(state) % HEIGHT,
|
||||||
};
|
};
|
||||||
|
|
||||||
f32 scale = (f32)((wapp_prng_xorshift_256(state) % (MAX_OBJ_DIM + 1 - MIN_OBJ_DIM)) + MIN_OBJ_DIM);
|
f32 scale = (f32)((wapp_prng_xorshift_256(state) % (MAX_OBJ_DIM + 1 - MIN_OBJ_DIM)) + MIN_OBJ_DIM);
|
||||||
object->scale = (Scale){
|
wanderer->scale = (Scale){
|
||||||
.width = scale,
|
.width = scale,
|
||||||
.height = scale,
|
.height = scale,
|
||||||
};
|
};
|
||||||
|
|
||||||
object->velocity = (Velocity){
|
wanderer->velocity = (Velocity){
|
||||||
.x = get_random_float(state),
|
.x = get_random_float(state),
|
||||||
// .y = get_random_float(state),
|
// .y = get_random_float(state),
|
||||||
.y = 0.0f,
|
.y = 0.0f,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
void move_object(Object *object) {
|
void move_wanderer(Wanderer *wanderer) {
|
||||||
object->position.x += object->velocity.x;
|
wanderer->position.x += wanderer->velocity.x;
|
||||||
object->position.y += object->velocity.y;
|
wanderer->position.y += wanderer->velocity.y;
|
||||||
|
|
||||||
if (object->position.x > WIDTH ||
|
if (wanderer->position.x > WIDTH ||
|
||||||
object->position.x + object->scale.width < 0 ||
|
wanderer->position.x + wanderer->scale.width < 0 ||
|
||||||
object->position.y > HEIGHT ||
|
wanderer->position.y > HEIGHT ||
|
||||||
object->position.y + object->scale.height < 0
|
wanderer->position.y + wanderer->scale.height < 0
|
||||||
) {
|
) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user