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