gfx and 2-2 and multi objects

This commit is contained in:
2026-02-08 20:01:32 -06:00
parent ce57691f96
commit e06ab8f76b
12 changed files with 483 additions and 8 deletions

View File

@@ -1,3 +1,10 @@
# The Nature Of Code Vectors # The Nature Of Code Vectors
Me following [The Nature Of Code](https://www.youtube.com/playlist?list=PLRqwX-V7Uu6ZV4yEcW3uDwOgGXKUUsPOM) vectors 1.1 with SDL2 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

View File

@@ -2,6 +2,7 @@
#define global_objects #define global_objects
#include <math.h> #include <math.h>
#include <stdlib.h>
#define flaot float #define flaot float
@@ -25,6 +26,11 @@ void point_mul(Point *A, Point *B){
A->y = A->y * B->y; 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) { float magnitude(flaot x, flaot y) {
return sqrt((pow(x,2) + pow(y,2))); return sqrt((pow(x,2) + pow(y,2)));
} }
@@ -38,4 +44,23 @@ void point_set_mag(Point *point, float mag){
point->y = ry; 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 #endif

71
lib/init-multi.c Executable file
View File

@@ -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;
}

15
lib/init-multi.h Executable file
View File

@@ -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

View File

@@ -1,5 +1,6 @@
#include <stdio.h> #include <stdio.h>
#include <stdbool.h> #include <stdbool.h>
#include <time.h>
#include <init.h> #include <init.h>
#include <SDL2/SDL.h> #include <SDL2/SDL.h>
@@ -23,12 +24,15 @@ int main(int argc, char *argv[])
} }
srand(time(NULL));
OBJECT obj = init_stuffs(startw,starth); OBJECT obj = init_stuffs(startw,starth);
STUFFS stuff = { STUFFS stuff = {
startw, startw,
starth, starth,
false, false,
0,
0,
&obj, &obj,
}; };
@@ -47,9 +51,11 @@ int main(int argc, char *argv[])
} else if(e.type==SDL_MOUSEBUTTONUP){ } else if(e.type==SDL_MOUSEBUTTONUP){
stuff.mousedown=false; stuff.mousedown=false;
} }
stuff.mx=e.button.x;
stuff.my=e.button.y;
} }
draw(renderer,window,&stuff); draw(renderer,window,&stuff);
SDL_Delay(30); SDL_Delay(10);
} }
SDL_DestroyRenderer(renderer); SDL_DestroyRenderer(renderer);

View File

@@ -8,6 +8,8 @@ typedef struct{
int width; int width;
int height; int height;
bool mousedown; bool mousedown;
int mx;
int my;
OBJECT *obj; OBJECT *obj;
} STUFFS; } STUFFS;
#endif #endif

5
make
View File

@@ -1,7 +1,7 @@
#!/bin/sh #!/bin/sh
DEBUG="" DEBUG=""
LIB="-Ilib/" LIB="-Ilib/ -lSDL2_gfx"
while getopts "d" opt; do while getopts "d" opt; do
case "$opt" in case "$opt" in
@@ -13,14 +13,15 @@ done
shift $((OPTIND-1)) shift $((OPTIND-1))
[ "${1:-}" = "--" ] && shift [ "${1:-}" = "--" ] && shift
echo making shit
if [[ "$1" ]]; then if [[ "$1" ]]; then
echo making src/$1
file="src/$1" file="src/$1"
clang -o bin/$(basename $file | sed 's/.c$//') $file $LIB `sdl2-config --cflags --libs` -lm $DEBUG clang -o bin/$(basename $file | sed 's/.c$//') $file $LIB `sdl2-config --cflags --libs` -lm $DEBUG
else else
for file in src/*; do for file in src/*; do
if [ -f "$file" ]; then if [ -f "$file" ]; then
if [[ "$file" != ".clangd" ]]; then if [[ "$file" != ".clangd" ]]; then
echo making $file
clang -o bin/$(basename $file | sed 's/.c$//') $file $LIB `sdl2-config --cflags --libs` -lm $DEBUG clang -o bin/$(basename $file | sed 's/.c$//') $file $LIB `sdl2-config --cflags --libs` -lm $DEBUG
fi fi
fi fi

View File

@@ -1,4 +1,3 @@
#include <stdlib.h>
#include <stdbool.h> #include <stdbool.h>
#include <global_objects.c> #include <global_objects.c>
@@ -13,10 +12,6 @@ typedef struct {
#include <init.h> #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){ void constructor(int w, int h, OBJECT *obj){
obj->pos.x = (float)w/2; obj->pos.x = (float)w/2;
obj->pos.y = (float)h/2; obj->pos.y = (float)h/2;

63
src/1-5-unit-vec.c Executable file
View File

@@ -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>

80
src/1-6-mover.c Executable file
View File

@@ -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>

103
src/2-1-gravity-wind.c Executable file
View File

@@ -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>

107
src/2-2-mass-accel.c Executable file
View File

@@ -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>