aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoriceyrazor <iceyrazor@mailfence.com>2026-02-08 20:01:32 -0600
committericeyrazor <iceyrazor@mailfence.com>2026-02-08 20:01:32 -0600
commite06ab8f76be21b866207a708f1336a5c1a53f149 (patch)
treebb0e53dd6b0fd3906ab53e0b292e56c735a7cbec
parentce57691f9631e5a08a82221634898b5ce58b7a5e (diff)
gfx and 2-2 and multi objects
-rw-r--r--README.md7
-rw-r--r--lib/global_objects.c25
-rwxr-xr-xlib/init-multi.c71
-rwxr-xr-xlib/init-multi.h15
-rwxr-xr-xlib/init.c8
-rwxr-xr-xlib/init.h2
-rwxr-xr-xmake5
-rwxr-xr-xsrc/1-3-rand-vec.c5
-rwxr-xr-xsrc/1-5-unit-vec.c63
-rwxr-xr-xsrc/1-6-mover.c80
-rwxr-xr-xsrc/2-1-gravity-wind.c103
-rwxr-xr-xsrc/2-2-mass-accel.c107
12 files changed, 483 insertions, 8 deletions
diff --git a/README.md b/README.md
index 8658cb3..bf04a72 100644
--- a/README.md
+++ b/README.md
@@ -1,3 +1,10 @@
# The Nature Of Code Vectors
Me following [The Nature Of Code](https://www.youtube.com/playlist?list=PLRqwX-V7Uu6ZV4yEcW3uDwOgGXKUUsPOM) vectors 1.1 with SDL2
+
+## requirements
+
+- clang
+- shell
+- SDL2
+- SDL2_gfx
diff --git a/lib/global_objects.c b/lib/global_objects.c
index 74abb86..fe56c0b 100644
--- a/lib/global_objects.c
+++ b/lib/global_objects.c
@@ -2,6 +2,7 @@
#define global_objects
#include <math.h>
+#include <stdlib.h>
#define flaot float
@@ -25,6 +26,11 @@ void point_mul(Point *A, Point *B){
A->y = A->y * B->y;
}
+void point_div(Point *A, Point *B){
+ A->x = A->x / B->x;
+ A->y = A->y / B->y;
+}
+
float magnitude(flaot x, flaot y) {
return sqrt((pow(x,2) + pow(y,2)));
}
@@ -38,4 +44,23 @@ void point_set_mag(Point *point, float mag){
point->y = ry;
}
+void point_limit(Point *point, float mag){
+ flaot getmag = magnitude(point->x, point->y);
+ flaot rx, ry;
+ if (getmag > mag){
+ rx = (point->x / getmag) * mag;
+ ry = (point->y / getmag) * mag;
+ } else {
+ rx = point->x;
+ ry = point->y;
+ }
+
+ point->x = rx;
+ point->y = ry;
+}
+
+float RandomFloat(float min, float max){
+ return ((max - min) * ((float)rand() / (double)RAND_MAX)) + min;
+}
+
#endif
diff --git a/lib/init-multi.c b/lib/init-multi.c
new file mode 100755
index 0000000..054ef9e
--- /dev/null
+++ b/lib/init-multi.c
@@ -0,0 +1,71 @@
+#include <stdio.h>
+#include <stdbool.h>
+#include <time.h>
+#include <init-multi.h>
+
+#include <SDL2/SDL.h>
+#include <SDL2/SDL_video.h>
+
+int main(int argc, char *argv[])
+{
+ if (SDL_InitSubSystem(SDL_INIT_VIDEO) < 0)
+ SDL_Log("SDL fails to initialize! %s\n", SDL_GetError());
+
+ int startw=400;
+ int starth=400;
+
+ SDL_Window *window = SDL_CreateWindow(PROJECT_NAME, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, startw, starth, SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL);
+ if (!window) {
+ printf("Failed to create window: %s\n", SDL_GetError());
+ }
+ SDL_Renderer *renderer = SDL_CreateRenderer(window,-1,SDL_RENDERER_SOFTWARE);
+ if (!renderer) {
+ printf("Failed to create renderer: %s\n", SDL_GetError());
+ }
+
+
+ srand(time(NULL));
+ OBJECT obj_storage[obj_n];
+
+ init_stuffs(startw,starth,obj_storage);
+
+ STUFFS stuff = {
+ startw,
+ starth,
+ false,
+ 0,
+ 0,
+ };
+
+ for (int i = 0; i < obj_n; i++)
+ stuff.obj[i] = &obj_storage[i];
+
+ bool quit = false;
+ SDL_Event e;
+ while (!quit) {
+ while (SDL_PollEvent(&e)) {
+ if (e.type == SDL_QUIT) {
+ quit = true;
+ }
+ else if(e.type == SDL_MOUSEBUTTONDOWN){
+ if(e.button.button==SDL_BUTTON_LEFT){
+ stuff.mousedown=true;
+ } else if(e.button.button==SDL_BUTTON_RIGHT){
+ }
+ } else if(e.type==SDL_MOUSEBUTTONUP){
+ stuff.mousedown=false;
+ }
+ stuff.mx=e.button.x;
+ stuff.my=e.button.y;
+ }
+ draw(renderer,window,&stuff);
+ SDL_Delay(10);
+ }
+
+ SDL_DestroyRenderer(renderer);
+ SDL_DestroyWindow(window);
+ SDL_QuitSubSystem(SDL_INIT_VIDEO);
+ SDL_Quit();
+
+ return 0;
+}
diff --git a/lib/init-multi.h b/lib/init-multi.h
new file mode 100755
index 0000000..34376e1
--- /dev/null
+++ b/lib/init-multi.h
@@ -0,0 +1,15 @@
+#ifndef inithell
+#define inithell
+
+#include <stdio.h>
+#include <stdbool.h>
+
+typedef struct{
+ int width;
+ int height;
+ bool mousedown;
+ int mx;
+ int my;
+ OBJECT *obj[obj_n];
+} STUFFS;
+#endif
diff --git a/lib/init.c b/lib/init.c
index 3880c6d..6adb878 100755
--- a/lib/init.c
+++ b/lib/init.c
@@ -1,5 +1,6 @@
#include <stdio.h>
#include <stdbool.h>
+#include <time.h>
#include <init.h>
#include <SDL2/SDL.h>
@@ -23,12 +24,15 @@ int main(int argc, char *argv[])
}
+ srand(time(NULL));
OBJECT obj = init_stuffs(startw,starth);
STUFFS stuff = {
startw,
starth,
false,
+ 0,
+ 0,
&obj,
};
@@ -47,9 +51,11 @@ int main(int argc, char *argv[])
} else if(e.type==SDL_MOUSEBUTTONUP){
stuff.mousedown=false;
}
+ stuff.mx=e.button.x;
+ stuff.my=e.button.y;
}
draw(renderer,window,&stuff);
- SDL_Delay(30);
+ SDL_Delay(10);
}
SDL_DestroyRenderer(renderer);
diff --git a/lib/init.h b/lib/init.h
index e957476..a4d9736 100755
--- a/lib/init.h
+++ b/lib/init.h
@@ -8,6 +8,8 @@ typedef struct{
int width;
int height;
bool mousedown;
+ int mx;
+ int my;
OBJECT *obj;
} STUFFS;
#endif
diff --git a/make b/make
index 72f08f9..2c3d3b9 100755
--- a/make
+++ b/make
@@ -1,7 +1,7 @@
#!/bin/sh
DEBUG=""
-LIB="-Ilib/"
+LIB="-Ilib/ -lSDL2_gfx"
while getopts "d" opt; do
case "$opt" in
@@ -13,14 +13,15 @@ done
shift $((OPTIND-1))
[ "${1:-}" = "--" ] && shift
-echo making shit
if [[ "$1" ]]; then
+ echo making src/$1
file="src/$1"
clang -o bin/$(basename $file | sed 's/.c$//') $file $LIB `sdl2-config --cflags --libs` -lm $DEBUG
else
for file in src/*; do
if [ -f "$file" ]; then
if [[ "$file" != ".clangd" ]]; then
+ echo making $file
clang -o bin/$(basename $file | sed 's/.c$//') $file $LIB `sdl2-config --cflags --libs` -lm $DEBUG
fi
fi
diff --git a/src/1-3-rand-vec.c b/src/1-3-rand-vec.c
index f592fdc..2880f6b 100755
--- a/src/1-3-rand-vec.c
+++ b/src/1-3-rand-vec.c
@@ -1,4 +1,3 @@
-#include <stdlib.h>
#include <stdbool.h>
#include <global_objects.c>
@@ -13,10 +12,6 @@ typedef struct {
#include <init.h>
-float RandomFloat(float min, float max){
- return ((max - min) * ((float)rand() / RAND_MAX)) + min;
-}
-
void constructor(int w, int h, OBJECT *obj){
obj->pos.x = (float)w/2;
obj->pos.y = (float)h/2;
diff --git a/src/1-5-unit-vec.c b/src/1-5-unit-vec.c
new file mode 100755
index 0000000..84710eb
--- /dev/null
+++ b/src/1-5-unit-vec.c
@@ -0,0 +1,63 @@
+#include <stdlib.h>
+#include <stdbool.h>
+#include <global_objects.c>
+
+#include <SDL2/SDL.h>
+#include <SDL2/SDL_video.h>
+
+#define PROJECT_NAME "vecotrs-main"
+
+typedef struct {
+ Point pos;
+} OBJECT;
+
+#include <init.h>
+
+void constructor(int w, int h, OBJECT *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 normalize(Point *p){
+ point_set_mag(p, 1);
+}
+
+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 mouse = {stuff->mx, stuff->my};
+
+ Point v = {
+ mouse.x - pos.x,
+ mouse.y - pos.y,
+ };
+
+ //float mag = magnitude(v.x, v.y);
+ //point_div(&v,&(Point){mag,mag});
+
+ //normalize(&v);
+
+ //point_mul(&v,&(Point){50,50});
+
+ point_set_mag(&v, 50);
+
+ SDL_SetRenderDrawColor(renderer,150,50,255,255);
+ SDL_RenderDrawLine(renderer,stuff->width/2,stuff->height/2,v.x + (float)stuff->width/2,v.y + (float)stuff->height/2);
+
+ SDL_RenderPresent(renderer);
+}
+
+void mousePressed(STUFFS *stuff){
+
+}
+
+#include <init.c>
diff --git a/src/1-6-mover.c b/src/1-6-mover.c
new file mode 100755
index 0000000..c7ff186
--- /dev/null
+++ b/src/1-6-mover.c
@@ -0,0 +1,80 @@
+#include <stdlib.h>
+#include <stdbool.h>
+#include <global_objects.c>
+
+#include <SDL2/SDL.h>
+#include <SDL2/SDL_video.h>
+#include <time.h>
+
+#define PROJECT_NAME "vecotrs-main"
+
+typedef struct {
+ Point pos;
+ Point vel;
+ Point acc;
+} OBJECT;
+
+#include <init.h>
+
+void constructor(int w, int h, OBJECT *obj){
+ obj->pos.x = (float)w/2;
+ obj->pos.y = (float)h/2;
+ obj->vel.x = RandomFloat(-1, 1);
+ obj->vel.y = RandomFloat(-1, 1);
+ point_mul(&obj->vel, &(Point){3,3});
+ obj->acc.x = RandomFloat(-1, 1);
+ obj->acc.y = RandomFloat(-1, 1);
+ point_set_mag(&obj->acc, 0.01);
+}
+
+OBJECT init_stuffs(int w, int h){
+ srand(time(NULL));
+ OBJECT obj;
+ constructor(w,h,&obj);
+
+ return obj;
+}
+
+Point vec_sub(Point A, Point B){
+ point_sub(&A, &B);
+ return A;
+}
+
+void update(STUFFS *stuff){
+ Point mouse = {
+ stuff->mx,
+ stuff->my,
+ };
+
+ stuff->obj->acc = vec_sub(mouse, stuff->obj->pos);
+ point_set_mag(&stuff->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(&stuff->obj->pos, &stuff->obj->vel);
+}
+
+void show(SDL_Renderer* renderer,STUFFS *stuff){
+ 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});
+}
+
+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);
+
+ 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
new file mode 100755
index 0000000..7a54b05
--- /dev/null
+++ b/src/2-1-gravity-wind.c
@@ -0,0 +1,103 @@
+#include <stdlib.h>
+#include <stdbool.h>
+#include <global_objects.c>
+
+#include <SDL2/SDL.h>
+#include <SDL2/SDL_video.h>
+#include <time.h>
+
+#define PROJECT_NAME "vecotrs-main"
+
+typedef struct {
+ Point pos;
+ Point vel;
+ Point acc;
+ float r;
+} OBJECT;
+
+#include <init.h>
+
+void constructor(int w, int h, OBJECT *obj){
+ obj->pos.x = (float)w/2;
+ obj->pos.y = (float)h/2;
+ obj->vel = (Point){0,0};
+ obj->acc = (Point){0,0};
+ obj->r = 40;
+}
+
+OBJECT init_stuffs(int w, int h){
+ srand(time(NULL));
+ OBJECT obj;
+ constructor(w,h,&obj);
+
+ return obj;
+}
+
+Point vec_sub(Point A, Point B){
+ point_sub(&A, &B);
+ return A;
+}
+
+void applyForce(OBJECT *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;
+ }
+
+ 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;
+ }
+}
+
+void update(STUFFS *stuff){
+ //Point mouse = {
+ // stuff->mx,
+ // stuff->my,
+ //};
+
+ //stuff->obj->acc = vec_sub(mouse, stuff->obj->pos);
+ //point_set_mag(&stuff->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};
+}
+
+void show(SDL_Renderer* renderer,STUFFS *stuff){
+ 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});
+}
+
+void draw(SDL_Renderer* renderer,SDL_Window* window, STUFFS *stuff){
+ SDL_SetRenderDrawColor(renderer,0,0,0,255);
+ SDL_RenderClear(renderer);
+
+ if(stuff->mousedown == true){
+ Point wind = {0.1, 0};
+ applyForce(stuff->obj, wind);
+ }
+
+ Point gravity = {0, 0.2};
+ applyForce(stuff->obj, gravity);
+
+
+ update(stuff);
+ edges(stuff);
+ show(renderer,stuff);
+
+ 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
new file mode 100755
index 0000000..dc79d3e
--- /dev/null
+++ b/src/2-2-mass-accel.c
@@ -0,0 +1,107 @@
+#include <stdbool.h>
+#include <global_objects.c>
+
+#include <SDL2/SDL.h>
+#include <SDL2/SDL_video.h>
+#include <SDL2/SDL2_gfxPrimitives.h>
+
+#define PROJECT_NAME "vecotrs-main"
+
+typedef struct OBJECT {
+ Point pos;
+ Point vel;
+ Point acc;
+ float r;
+ float mass;
+} OBJECT;
+
+#define obj_n 2
+
+#include <init-multi.h>
+
+void constructor(int w, int h, OBJECT *obj, float x, float y, float mass){
+ obj->pos.x = (float)w/2 + x;
+ obj->pos.y = (float)h/2 + y;
+ obj->vel = (Point){0,0};
+ obj->acc = (Point){0,0};
+ obj->mass = mass;
+ obj->r = sqrt(obj->mass) * 10;
+}
+
+void init_stuffs(int w, int h, OBJECT *objs){
+ constructor(w, h, &objs[0], 100, 100, 4);
+ constructor(w, h, &objs[1], -100, 100, 10);
+}
+
+Point vec_sub(Point A, Point B){
+ point_sub(&A, &B);
+ return A;
+}
+
+void applyForce(OBJECT *obj, Point force){
+ //no need to make a copy. tis not a pointer
+ point_div(&force, &(Point){obj->mass,obj->mass});
+ point_add(&obj->acc, &force);
+}
+
+void edges(STUFFS *stuff, OBJECT *obj) {
+ if (obj->pos.y >= stuff->height - obj->r){
+ obj->pos.y = stuff->height - obj->r;
+ obj->vel.y = obj->vel.y * -1;
+ }
+
+ if (obj->pos.x >= stuff->width - obj->r){
+ obj->pos.x = stuff->width - obj->r;
+ obj->vel.x = obj->vel.x * -1;
+ } else if (obj->pos.x <= obj->r){
+ obj->pos.x = obj->r;
+ obj->vel.x = obj->vel.x * -1;
+ }
+}
+
+void update(STUFFS *stuff, OBJECT *obj){
+ point_add(&obj->vel,&obj->acc);
+ point_add(&obj->pos, &obj->vel);
+ obj->acc = (Point){0,0};
+}
+
+void show(SDL_Renderer* renderer,STUFFS *stuff,OBJECT *obj){
+ SDL_SetRenderDrawColor(renderer,150,50,255,10);
+
+ Sint16 x = (Sint16)obj->pos.x;
+ Sint16 y = (Sint16)obj->pos.y;
+
+ filledCircleRGBA(renderer, x, y, obj->r, 150, 50, 255, 255);
+}
+
+void draw(SDL_Renderer* renderer,SDL_Window* window, STUFFS *stuff){
+ SDL_SetRenderDrawColor(renderer,0,0,0,255);
+ SDL_RenderClear(renderer);
+
+ for (int i=0; i<obj_n; i++){
+ Point gravity = {0, 0.2};
+ Point weight = {0};
+
+ weight.x = gravity.x * stuff->obj[i]->mass;
+ weight.y = gravity.y * stuff->obj[i]->mass;
+ if(stuff->mousedown == true){
+ Point wind = {0.1, 0};
+ applyForce(stuff->obj[i], wind);
+ }
+
+ applyForce(stuff->obj[i], weight);
+
+
+ update(stuff,stuff->obj[i]);
+ edges(stuff,stuff->obj[i]);
+ show(renderer,stuff,stuff->obj[i]);
+ }
+
+ SDL_RenderPresent(renderer);
+}
+
+void mousePressed(STUFFS *stuff){
+
+}
+
+#include <init-multi.c>