diff options
| author | iceyrazor <iceyrazor@mailfence.com> | 2026-03-20 02:09:25 -0500 |
|---|---|---|
| committer | iceyrazor <iceyrazor@mailfence.com> | 2026-03-20 02:09:25 -0500 |
| commit | c5648c6959e9ab63406ab3907ae189da6fbead0e (patch) | |
| tree | 2003e86c41ad0eedd5fd8af49cc025ceaf703c74 /src | |
| parent | d52cdf58241e476f64be98d2d8b1b03825d7edfd (diff) | |
removed init.c and moved init-multi.c to init.c
Diffstat (limited to 'src')
| -rw-r--r--[-rwxr-xr-x] | src/1-1-walker.c | 28 | ||||
| -rw-r--r--[-rwxr-xr-x] | src/1-2-vec-math.c | 30 | ||||
| -rw-r--r--[-rwxr-xr-x] | src/1-3-rand-vec.c | 28 | ||||
| -rw-r--r--[-rwxr-xr-x] | src/1-5-unit-vec.c | 22 | ||||
| -rw-r--r--[-rwxr-xr-x] | src/1-6-mover.c | 40 | ||||
| -rw-r--r--[-rwxr-xr-x] | src/2-1-gravity-wind.c | 68 | ||||
| -rw-r--r--[-rwxr-xr-x] | src/2-2-mass-accel.c | 10 | ||||
| -rw-r--r--[-rwxr-xr-x] | src/2-3-friction.c | 6 | ||||
| -rw-r--r--[-rwxr-xr-x] | src/2-4-drag.c | 6 | ||||
| -rw-r--r--[-rwxr-xr-x] | src/2-5-grav-attraction.c | 10 |
10 files changed, 114 insertions, 134 deletions
diff --git a/src/1-1-walker.c b/src/1-1-walker.c index ba8f696..5b3d0e1 100755..100644 --- a/src/1-1-walker.c +++ b/src/1-1-walker.c @@ -5,32 +5,34 @@ #include <SDL2/SDL_video.h> #define PROJECT_NAME "vectors-walker" +#define obj_n 1 -typedef struct { +typedef struct OBJECT_INER { float x; float y; +} OBJECT_INER; + +typedef struct OBJECT { + OBJECT_INER *objs[obj_n]; } OBJECT; #include <init.h> -void constructor(int w, int h, OBJECT *obj){ +void constructor(int w, int h, OBJECT_INER *obj){ obj->x = (float)w/2; obj->y = (float)h/2; } -OBJECT init_stuffs(int w, int h){ - OBJECT obj; - constructor(w,h,&obj); - - return obj; +void init_stuffs(int w, int h, OBJECT_INER *objs, STUFFS *stuff){ + constructor(w,h,&objs[0]); } -void update(OBJECT *obj){ +void update(OBJECT_INER *obj){ obj->x = obj->x + (rand() % 2 - 0.5); obj->y = obj->y + (rand() % 2 - 0.5); } -void show(SDL_Renderer* renderer,OBJECT *obj){ +void show(SDL_Renderer* renderer,OBJECT_INER *obj){ SDL_SetRenderDrawColor(renderer,150,50,255,255); SDL_RenderDrawPoint(renderer,obj->x,obj->y); } @@ -39,15 +41,11 @@ void draw(SDL_Renderer* renderer,SDL_Window* window, STUFFS *stuff){ //SDL_SetRenderDrawColor(renderer,0,0,0,255); //SDL_RenderClear(renderer); - update(stuff->obj); - show(renderer,stuff->obj); + update(stuff->obj.objs[0]); + show(renderer,stuff->obj.objs[0]); SDL_RenderPresent(renderer); //SDL_UpdateWindowSurface(window); } -void mousePressed(STUFFS *stuff){ - -} - #include <init.c> diff --git a/src/1-2-vec-math.c b/src/1-2-vec-math.c index 201bbc0..b4e2c1e 100755..100644 --- a/src/1-2-vec-math.c +++ b/src/1-2-vec-math.c @@ -1,37 +1,39 @@ #include <stdbool.h> -#include <global_objects.c> +#include <vectorlib.c> #include <SDL2/SDL.h> #include <SDL2/SDL_video.h> #define PROJECT_NAME "vectors-vec math" +#define obj_n 1 -typedef struct { +typedef struct OBJECT_INER { Point pos; Point vel; +} OBJECT_INER; + +typedef struct OBJECT { + OBJECT_INER *objs[obj_n]; } OBJECT; #include <init.h> -void constructor(int w, int h, OBJECT *obj){ +void constructor(int w, int h, OBJECT_INER *obj){ obj->pos.x = (float)w/2; obj->pos.y = (float)h/2; obj->vel.x = 1; obj->vel.y = -1; } -OBJECT init_stuffs(int w, int h){ - OBJECT obj; - constructor(w,h,&obj); - - return obj; +void init_stuffs(int w, int h, OBJECT_INER *objs, STUFFS *stuff){ + constructor(w,h,&objs[0]); } -void update(OBJECT *obj){ +void update(OBJECT_INER *obj){ point_add(&obj->pos, &obj->vel); } -void show(SDL_Renderer* renderer,OBJECT *obj){ +void show(SDL_Renderer* renderer,OBJECT_INER *obj){ SDL_SetRenderDrawColor(renderer,150,50,255,255); SDL_RenderFillRect(renderer,&(SDL_Rect){.x = obj->pos.x - 5, .y=obj->pos.y - 5, .w = 10, .h = 10}); } @@ -40,14 +42,10 @@ void draw(SDL_Renderer* renderer,SDL_Window* window, STUFFS *stuff){ SDL_SetRenderDrawColor(renderer,0,0,0,255); SDL_RenderClear(renderer); - update(stuff->obj); - show(renderer,stuff->obj); + update(stuff->obj.objs[0]); + show(renderer,stuff->obj.objs[0]); SDL_RenderPresent(renderer); } -void mousePressed(STUFFS *stuff){ - -} - #include <init.c> diff --git a/src/1-3-rand-vec.c b/src/1-3-rand-vec.c index a5289d5..992a81a 100755..100644 --- a/src/1-3-rand-vec.c +++ b/src/1-3-rand-vec.c @@ -1,27 +1,29 @@ #include <stdbool.h> -#include <global_objects.c> +#include <vectorlib.c> #include <SDL2/SDL.h> #include <SDL2/SDL_video.h> #define PROJECT_NAME "vectors-rand vec" +#define obj_n 1 -typedef struct { +typedef struct OBJECT_INER { Point pos; +} OBJECT_INER; + +typedef struct OBJECT { + OBJECT_INER *objs[obj_n]; } OBJECT; #include <init.h> -void constructor(int w, int h, OBJECT *obj){ +void constructor(int w, int h, OBJECT_INER *obj){ obj->pos.x = (float)w/2; obj->pos.y = (float)h/2; } -OBJECT init_stuffs(int w, int h){ - OBJECT obj; - constructor(w,h,&obj); - - return obj; +void init_stuffs(int w, int h, OBJECT_INER *objs, STUFFS *stuff){ + constructor(w,h,&objs[0]); } void update(STUFFS *stuff){ @@ -31,14 +33,14 @@ void update(STUFFS *stuff){ float random_mul = RandomFloat(50, 100); point_mul(&rand_point,&(Point){.x = random_mul, .y = random_mul}); - stuff->obj->pos.x = rand_point.x + (float)stuff->width / 2;; - stuff->obj->pos.y = rand_point.y + (float)stuff->height / 2; + stuff->obj.objs[0]->pos.x = rand_point.x + (float)stuff->width / 2;; + stuff->obj.objs[0]->pos.y = rand_point.y + (float)stuff->height / 2; } void show(SDL_Renderer* renderer,STUFFS *stuff){ SDL_SetRenderDrawColor(renderer,150,50,255,255); //SDL_RenderFillRect(renderer,&(SDL_Rect){.x = obj->pos.x - 5, .y=obj->pos.y - 5, .w = 10, .h = 10}); - SDL_RenderDrawLine(renderer,stuff->width/2,stuff->height/2,stuff->obj->pos.x,stuff->obj->pos.y); + SDL_RenderDrawLine(renderer,stuff->width/2,stuff->height/2,stuff->obj.objs[0]->pos.x,stuff->obj.objs[0]->pos.y); } void draw(SDL_Renderer* renderer,SDL_Window* window, STUFFS *stuff){ @@ -51,8 +53,4 @@ void draw(SDL_Renderer* renderer,SDL_Window* window, STUFFS *stuff){ SDL_RenderPresent(renderer); } -void mousePressed(STUFFS *stuff){ - -} - #include <init.c> diff --git a/src/1-5-unit-vec.c b/src/1-5-unit-vec.c index b888b13..1f3acbb 100755..100644 --- a/src/1-5-unit-vec.c +++ b/src/1-5-unit-vec.c @@ -1,27 +1,29 @@ #include <stdbool.h> -#include <global_objects.c> +#include <vectorlib.c> #include <SDL2/SDL.h> #include <SDL2/SDL_video.h> #define PROJECT_NAME "vectors-unit vec" +#define obj_n 1 typedef struct { Point pos; +} OBJECT_INER; + +typedef struct OBJECT { + OBJECT_INER *objs[obj_n]; } OBJECT; #include <init.h> -void constructor(int w, int h, OBJECT *obj){ +void constructor(int w, int h, OBJECT_INER *obj){ obj->pos.x = (float)w/2; obj->pos.y = (float)h/2; } -OBJECT init_stuffs(int w, int h){ - OBJECT obj; - constructor(w,h,&obj); - - return obj; +void init_stuffs(int w, int h, OBJECT_INER *objs, STUFFS *stuff){ + constructor(w,h,&objs[0]); } void normalize(Point *p){ @@ -32,7 +34,7 @@ void draw(SDL_Renderer* renderer,SDL_Window* window, STUFFS *stuff){ SDL_SetRenderDrawColor(renderer,0,0,0,255); SDL_RenderClear(renderer); - Point pos = { 200,200 }; + Point pos = { 300,300 }; Point mouse = {stuff->mx, stuff->my}; Point v = { @@ -55,8 +57,4 @@ void draw(SDL_Renderer* renderer,SDL_Window* window, STUFFS *stuff){ SDL_RenderPresent(renderer); } -void mousePressed(STUFFS *stuff){ - -} - #include <init.c> diff --git a/src/1-6-mover.c b/src/1-6-mover.c index d4535d5..a59fa91 100755..100644 --- a/src/1-6-mover.c +++ b/src/1-6-mover.c @@ -1,22 +1,27 @@ #include <stdlib.h> #include <stdbool.h> -#include <global_objects.c> +#include <vectorlib.c> #include <SDL2/SDL.h> #include <SDL2/SDL_video.h> #include <time.h> #define PROJECT_NAME "vectors-mover" +#define obj_n 1 typedef struct { Point pos; Point vel; Point acc; +} OBJECT_INER; + +typedef struct OBJECT { + OBJECT_INER *objs[obj_n]; } OBJECT; #include <init.h> -void constructor(int w, int h, OBJECT *obj){ +void constructor(int w, int h, OBJECT_INER *obj){ obj->pos.x = (float)w/2; obj->pos.y = (float)h/2; obj->vel.x = RandomFloat(-1, 1); @@ -27,12 +32,9 @@ void constructor(int w, int h, OBJECT *obj){ point_set_mag(&obj->acc, 0.01); } -OBJECT init_stuffs(int w, int h){ +void init_stuffs(int w, int h, OBJECT_INER *objs, STUFFS *stuff){ srand(time(NULL)); - OBJECT obj; - constructor(w,h,&obj); - - return obj; + constructor(w,h,&objs[0]); } Point vec_sub(Point A, Point B){ @@ -40,41 +42,37 @@ Point vec_sub(Point A, Point B){ return A; } -void update(STUFFS *stuff){ +void update(STUFFS *stuff, OBJECT_INER *obj){ Point mouse = { stuff->mx, stuff->my, }; - stuff->obj->acc = vec_sub(mouse, stuff->obj->pos); - point_set_mag(&stuff->obj->acc, 1); + obj->acc = vec_sub(mouse, obj->pos); + point_set_mag(&obj->acc, 1); //stuff->obj->acc.x = RandomFloat(-1, 1); //stuff->obj->acc.y = RandomFloat(-1, 1); - point_add(&stuff->obj->vel,&stuff->obj->acc); - point_limit(&stuff->obj->vel, 5); + point_add(&obj->vel,&obj->acc); + point_limit(&obj->vel, 5); - point_add(&stuff->obj->pos, &stuff->obj->vel); + point_add(&obj->pos, &obj->vel); } -void show(SDL_Renderer* renderer,STUFFS *stuff){ +void show(SDL_Renderer* renderer,OBJECT_INER *obj){ SDL_SetRenderDrawColor(renderer,150,50,255,255); - SDL_RenderFillRect(renderer,&(SDL_Rect){.x = stuff->obj->pos.x - 5, .y=stuff->obj->pos.y - 5, .w = 10, .h = 10}); + SDL_RenderFillRect(renderer,&(SDL_Rect){.x = obj->pos.x - 5, .y=obj->pos.y - 5, .w = 10, .h = 10}); } void draw(SDL_Renderer* renderer,SDL_Window* window, STUFFS *stuff){ SDL_SetRenderDrawColor(renderer,0,0,0,255); SDL_RenderClear(renderer); - update(stuff); - show(renderer,stuff); + update(stuff, stuff->obj.objs[0]); + show(renderer,stuff->obj.objs[0]); SDL_RenderPresent(renderer); } -void mousePressed(STUFFS *stuff){ - -} - #include <init.c> diff --git a/src/2-1-gravity-wind.c b/src/2-1-gravity-wind.c index a5a2bb2..8e3be27 100755..100644 --- a/src/2-1-gravity-wind.c +++ b/src/2-1-gravity-wind.c @@ -1,23 +1,28 @@ #include <stdlib.h> #include <stdbool.h> -#include <global_objects.c> +#include <vectorlib.c> #include <SDL2/SDL.h> #include <SDL2/SDL_video.h> #include <time.h> #define PROJECT_NAME "vectors-gravity wind" +#define obj_n 1 typedef struct { Point pos; Point vel; Point acc; float r; +} OBJECT_INER; + +typedef struct OBJECT { + OBJECT_INER *objs[obj_n]; } OBJECT; #include <init.h> -void constructor(int w, int h, OBJECT *obj){ +void constructor(int w, int h, OBJECT_INER *obj){ obj->pos.x = (float)w/2; obj->pos.y = (float)h/2; obj->vel = (Point){0,0}; @@ -25,12 +30,9 @@ void constructor(int w, int h, OBJECT *obj){ obj->r = 40; } -OBJECT init_stuffs(int w, int h){ +void init_stuffs(int w, int h, OBJECT_INER *objs, STUFFS *stuff){ srand(time(NULL)); - OBJECT obj; - constructor(w,h,&obj); - - return obj; + constructor(w,h,&objs[0]); } Point vec_sub(Point A, Point B){ @@ -38,42 +40,42 @@ Point vec_sub(Point A, Point B){ return A; } -void applyForce(OBJECT *obj, Point force){ +void applyForce(OBJECT_INER *obj, Point force){ point_add(&obj->acc, &force); } -void edges(STUFFS *stuff) { - if (stuff->obj->pos.y >= stuff->height - stuff->obj->r / 2){ - stuff->obj->pos.y = stuff->height - stuff->obj->r / 2; - stuff->obj->vel.y = stuff->obj->vel.y * -1; +void edges(STUFFS *stuff, OBJECT_INER *obj) { + if (obj->pos.y >= stuff->height - obj->r / 2){ + obj->pos.y = stuff->height - obj->r / 2; + obj->vel.y = obj->vel.y * -1; } - if (stuff->obj->pos.x >= stuff->width - stuff->obj->r / 2){ - stuff->obj->pos.x = stuff->width - stuff->obj->r / 2; - stuff->obj->vel.x = stuff->obj->vel.x * -1; - } else if (stuff->obj->pos.x <= stuff->obj->r / 2){ - stuff->obj->pos.x = stuff->obj->r / 2; - stuff->obj->vel.x = stuff->obj->vel.x * -1; + if (obj->pos.x >= stuff->width - obj->r / 2){ + obj->pos.x = stuff->width - obj->r / 2; + obj->vel.x = obj->vel.x * -1; + } else if (obj->pos.x <= obj->r / 2){ + obj->pos.x = obj->r / 2; + obj->vel.x = obj->vel.x * -1; } } -void update(STUFFS *stuff){ +void update(STUFFS *stuff, OBJECT_INER *obj){ //Point mouse = { // stuff->mx, // stuff->my, //}; - //stuff->obj->acc = vec_sub(mouse, stuff->obj->pos); - //point_set_mag(&stuff->obj->acc, 0.1); + //obj->acc = vec_sub(mouse, obj->pos); + //point_set_mag(&obj->acc, 0.1); - point_add(&stuff->obj->vel,&stuff->obj->acc); - point_add(&stuff->obj->pos, &stuff->obj->vel); - stuff->obj->acc = (Point){0,0}; + point_add(&obj->vel,&obj->acc); + point_add(&obj->pos, &obj->vel); + obj->acc = (Point){0,0}; } -void show(SDL_Renderer* renderer,STUFFS *stuff){ +void show(SDL_Renderer* renderer,OBJECT_INER *obj){ SDL_SetRenderDrawColor(renderer,150,50,255,255); - SDL_RenderFillRect(renderer,&(SDL_Rect){.x = stuff->obj->pos.x - (stuff->obj->r / 2), .y=stuff->obj->pos.y - (stuff->obj->r / 2), .w = stuff->obj->r, .h = stuff->obj->r}); + SDL_RenderFillRect(renderer,&(SDL_Rect){.x = obj->pos.x - (obj->r / 2), .y=obj->pos.y - (obj->r / 2), .w = obj->r, .h = obj->r}); } void draw(SDL_Renderer* renderer,SDL_Window* window, STUFFS *stuff){ @@ -82,22 +84,18 @@ void draw(SDL_Renderer* renderer,SDL_Window* window, STUFFS *stuff){ if(stuff->mousedown == true){ Point wind = {0.1, 0}; - applyForce(stuff->obj, wind); + applyForce(stuff->obj.objs[0], wind); } Point gravity = {0, 0.2}; - applyForce(stuff->obj, gravity); + applyForce(stuff->obj.objs[0], gravity); - update(stuff); - edges(stuff); - show(renderer,stuff); + update(stuff,stuff->obj.objs[0]); + edges(stuff,stuff->obj.objs[0]); + show(renderer,stuff->obj.objs[0]); SDL_RenderPresent(renderer); } -void mousePressed(STUFFS *stuff){ - -} - #include <init.c> diff --git a/src/2-2-mass-accel.c b/src/2-2-mass-accel.c index 48b1784..1281433 100755..100644 --- a/src/2-2-mass-accel.c +++ b/src/2-2-mass-accel.c @@ -1,5 +1,5 @@ #include <stdbool.h> -#include <global_objects.c> +#include <vectorlib.c> #include <SDL2/SDL.h> #include <SDL2/SDL_video.h> @@ -20,7 +20,7 @@ typedef struct OBJECT { OBJECT_INER *objs[obj_n]; } OBJECT; -#include <init-multi.h> +#include <init.h> void constructor(int w, int h, OBJECT_INER *obj, float x, float y, float mass){ obj->pos.x = (float)w/2 + x; @@ -103,8 +103,4 @@ void draw(SDL_Renderer* renderer,SDL_Window* window, STUFFS *stuff){ SDL_RenderPresent(renderer); } -void mousePressed(STUFFS *stuff){ - -} - -#include <init-multi.c> +#include <init.c> diff --git a/src/2-3-friction.c b/src/2-3-friction.c index d78fc8e..974b19e 100755..100644 --- a/src/2-3-friction.c +++ b/src/2-3-friction.c @@ -1,5 +1,5 @@ #include <stdbool.h> -#include <global_objects.c> +#include <vectorlib.c> #include <SDL2/SDL.h> #include <SDL2/SDL_video.h> @@ -22,7 +22,7 @@ typedef struct OBJECT { float mu=0.1; -#include <init-multi.h> +#include <init.h> void constructor(int w, int h, OBJECT_INER *obj, float x, float y, float mass){ obj->pos.x = x; @@ -130,4 +130,4 @@ void mousePressed(STUFFS *stuff){ } -#include <init-multi.c> +#include <init.c> diff --git a/src/2-4-drag.c b/src/2-4-drag.c index 503af42..1bcbe17 100755..100644 --- a/src/2-4-drag.c +++ b/src/2-4-drag.c @@ -1,6 +1,6 @@ #include <SDL2/SDL_render.h> #include <stdbool.h> -#include <global_objects.c> +#include <vectorlib.c> #include <SDL2/SDL.h> #include <SDL2/SDL_video.h> @@ -23,7 +23,7 @@ typedef struct OBJECT { float DragC = 0.2; -#include <init-multi.h> +#include <init.h> void constructor(int w, int h, OBJECT_INER *obj, float x, float y, float mass){ obj->pos.x = x; @@ -132,7 +132,7 @@ void mousePressed(STUFFS *stuff){ } -#include <init-multi.c> +#include <init.c> /* Exercises diff --git a/src/2-5-grav-attraction.c b/src/2-5-grav-attraction.c index 89ee28a..e4e0313 100755..100644 --- a/src/2-5-grav-attraction.c +++ b/src/2-5-grav-attraction.c @@ -1,6 +1,6 @@ #include <SDL2/SDL_render.h> #include <stdbool.h> -#include <global_objects.c> +#include <vectorlib.c> #include <SDL2/SDL.h> #include <SDL2/SDL_video.h> @@ -28,7 +28,7 @@ typedef struct OBJECT { OBJECT_INER *objs[obj_n]; } OBJECT; -#include <init-multi.h> +#include <init.h> void constructor(int w, int h, OBJECT_INER *obj, float x, float y, float mass){ obj->pos.x = x; @@ -107,8 +107,4 @@ void draw(SDL_Renderer* renderer,SDL_Window* window, STUFFS *stuff){ SDL_RenderPresent(renderer); } -void mousePressed(STUFFS *stuff){ - -} - -#include <init-multi.c> +#include <init.c> |
