Separating into lots of headers

This commit is contained in:
Pierre committed 2026-09-29 21:51:25 +02:00
1 parent b6265c6b61
commit 5ceb272a49
5 files changed
+79 -47

No files matched your search

+8
View File
@@ -0,0 +1,8 @@
#include "cphysics.h"
void gravity(universe_t* univ){
}
void magnetism(universe_t* univ){
}
+7
View File
@@ -0,0 +1,7 @@
#ifndef CPHYSICS_H_
#define CPHYSICS_H_
#include "objects.h"
void gravity(universe_t* univ);
void magnetism(universe_t* univ);
void mechanics(universe_t* univ);
#endif // CPHYSICS_H_
+6 -47
View File
@@ -1,18 +1,12 @@
#include <math.h> #include <math.h>
#include <time.h> #include <time.h>
#include <pthread.h> #include <pthread.h>
#include <unistd.h>
#include <raylib.h> #include <raylib.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include "objects.h"
static const Vector2 NULL_VEC2 = {0, 0};
typedef struct {
float mass;
float radius;
Vector2 position;
Vector2 speed;
} mass_body_t;
int center_x_text(const char *text, int width, int size) { int center_x_text(const char *text, int width, int size) {
return (width - MeasureText(text, size)) / 2; return (width - MeasureText(text, size)) / 2;
@@ -20,39 +14,6 @@ int center_x_text(const char *text, int width, int size) {
static inline int center_y_text(int height, int size) { return (height - size) / 2; } static inline int center_y_text(int height, int size) { return (height - size) / 2; }
Vector2 transform_to_center(Vector2 v) {
int width = GetRenderWidth();
int height = GetRenderHeight();
Vector2 result = {v.x + width / 2.0, v.y + height / 2.0};
return result;
}
Vector2 vec2_sum(const Vector2 *a, const Vector2 *b) {
Vector2 result = {a->x + b->x, a->y + b->y};
return result;
}
Vector2 vec2_diff(const Vector2 *a, const Vector2 *b) {
Vector2 result = {a->x - b->x, a->y - b->y};
return result;
}
float vec2_len(Vector2 v) { return sqrtf(v.x * v.x + v.y * v.y); }
Vector2 vec2_normalize(const Vector2 v) {
float len = vec2_len(v);
Vector2 result = {v.x / len, v.y / len};
return result;
}
Vector2 vec2_scale(const Vector2 *normal, const float len) {
Vector2 result = {len * normal->x, len * normal->y};
return result;
}
float vec2_dot(const Vector2 *a, const Vector2 *b) {
return a->x * b->x + a->y * b->y;
}
Vector2 body_collision(mass_body_t *a, mass_body_t *b) { Vector2 body_collision(mass_body_t *a, mass_body_t *b) {
Vector2 diff = vec2_diff(&a->position, &b->position); Vector2 diff = vec2_diff(&a->position, &b->position);
float len = vec2_len(diff); float len = vec2_len(diff);
@@ -119,22 +80,19 @@ bool vec2_is_null(Vector2 v) {
return false; return false;
} }
typedef struct universe {
unsigned int num_bodies;
mass_body_t *bodies;
} universe_t;
void *physics(void *args) { void *physics(void *args) {
universe_t* uni= (universe_t*)args; universe_t* uni= (universe_t*)args;
mass_body_t body_1; mass_body_t body_1;
mass_body_t body_2; mass_body_t body_2;
Vector2 collision; Vector2 collision;
struct timespec ot, ct;
clock_gettime(CLOCK_REALTIME,&ot);
while(true){ while(true){
clock_gettime(CLOCK_REALTIME,&ct);
for(unsigned int i=0;i<uni->num_bodies;i++){ for(unsigned int i=0;i<uni->num_bodies;i++){
body_1=uni->bodies[i]; body_1=uni->bodies[i];
printf("body %d pos is : %f %f \n speed is : %f %f\n",i,uni->bodies[i].position.x,uni->bodies[i].position.y,uni->bodies[i].speed.x,uni->bodies[i].speed.x);
body_1.position = vec2_sum(&body_1.position, &body_1.speed); body_1.position = vec2_sum(&body_1.position, &body_1.speed);
printf("body %d pos is : %f %f \n speed is : %f %f\n",i,uni->bodies[i].position.x,uni->bodies[i].position.y,uni->bodies[i].speed.x,uni->bodies[i].speed.x);
for(unsigned int y=0;y<uni->num_bodies;y++){ for(unsigned int y=0;y<uni->num_bodies;y++){
body_2=uni->bodies[y]; body_2=uni->bodies[y];
collision = body_collision(&body_1, &body_2); collision = body_collision(&body_1, &body_2);
@@ -149,6 +107,7 @@ void *physics(void *args) {
} }
uni->bodies[i]=body_1; uni->bodies[i]=body_1;
} }
ot=ct;
} }
} }
+16
View File
@@ -0,0 +1,16 @@
#ifndef OBJECTS_H_
#define OBJECTS_H_
#include "vec_util.h"
typedef struct {
float mass;
float radius;
Vector2 position;
Vector2 speed;
} mass_body_t;
typedef struct universe {
unsigned int num_bodies;
mass_body_t *bodies;
} universe_t;
#endif // OBJECTS_H_
+42
View File
@@ -0,0 +1,42 @@
#ifndef VEC_UTIL_H_
#define VEC_UTIL_H_
#include <raylib.h>
#include <math.h>
static const Vector2 NULL_VEC2 = {0, 0};
Vector2 transform_to_center(Vector2 v) {
int width = GetRenderWidth();
int height = GetRenderHeight();
Vector2 result = {v.x + width / 2.0, v.y + height / 2.0};
return result;
}
Vector2 vec2_sum(const Vector2 *a, const Vector2 *b) {
Vector2 result = {a->x + b->x, a->y + b->y};
return result;
}
Vector2 vec2_diff(const Vector2 *a, const Vector2 *b) {
Vector2 result = {a->x - b->x, a->y - b->y};
return result;
}
float vec2_len(Vector2 v) { return sqrtf(v.x * v.x + v.y * v.y); }
Vector2 vec2_normalize(const Vector2 v) {
float len = vec2_len(v);
Vector2 result = {v.x / len, v.y / len};
return result;
}
Vector2 vec2_scale(const Vector2 *normal, const float len) {
Vector2 result = {len * normal->x, len * normal->y};
return result;
}
float vec2_dot(const Vector2 *a, const Vector2 *b) {
return a->x * b->x + a->y * b->y;
}
#endif // VEC_UTIL_H_