diff options
| author | iceyrazor <iceyrazor@mailfence.com> | 2026-02-24 05:49:52 -0600 |
|---|---|---|
| committer | iceyrazor <iceyrazor@mailfence.com> | 2026-02-24 05:49:52 -0600 |
| commit | b264506817f65660d57a7cdf24fcdef85a2f473e (patch) | |
| tree | bed95ad92dbc792a692647f55339496ffd39e9fb /src | |
| parent | cdd60c0036503df6abdabfa035a35bb5ea17e905 (diff) | |
added delta time
- updated readme requirements
- changed audio capture name
Diffstat (limited to 'src')
| -rwxr-xr-x | src/main.c | 17 | ||||
| -rwxr-xr-x | src/objects.c | 28 | ||||
| -rwxr-xr-x | src/objects.h | 1 | ||||
| -rw-r--r-- | src/pipe.c | 2 |
4 files changed, 32 insertions, 16 deletions
@@ -15,7 +15,6 @@ bool running=true; #define PROJECT_NAME "audio reactive" - int main(int argc, char *argv[]) { if (SDL_InitSubSystem(SDL_INIT_VIDEO) < 0) @@ -35,10 +34,10 @@ int main(int argc, char *argv[]) VOL_ARG volargs={ argc, argv, }; pthread_t vol_thread; - int thread_err=pthread_create(&vol_thread, NULL, volume_input, (void *)&volargs); + pthread_create(&vol_thread, NULL, volume_input, (void *)&volargs); pthread_t peak_reduc; - int thread_err2=pthread_create(&peak_reduc, NULL, avg_peak, NULL); + pthread_create(&peak_reduc, NULL, avg_peak, NULL); srand(time(NULL)); OBJECT_INER obj_storage[obj_n]; @@ -65,6 +64,7 @@ int main(int argc, char *argv[]) SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND); bool quit = false; + Uint32 lastUpdate = 0; SDL_Event e; while (!quit) { while (SDL_PollEvent(&e)) { @@ -88,9 +88,18 @@ int main(int argc, char *argv[]) stuff.avg_peak=mutVolume.avg_peak; mutVolume.locked=false; } + + Uint64 start = SDL_GetPerformanceCounter(); //SDL_Log("vol: %f avg: %f",stuff.volume,stuff.avg_peak); + Uint32 current=SDL_GetTicks(); + stuff.dt = (current - lastUpdate); draw(&stuff); - SDL_Delay(5); + lastUpdate=current; + + Uint64 end = SDL_GetPerformanceCounter(); + float elapsedMS = (end - start) / (float)SDL_GetPerformanceFrequency() * 1000.0f; + SDL_Delay(floor(16.666f - elapsedMS)); + //SDL_Delay(rand() % 25); } running=false; diff --git a/src/objects.c b/src/objects.c index 1107c1d..ccc9e25 100755 --- a/src/objects.c +++ b/src/objects.c @@ -31,15 +31,20 @@ void init_stuffs(int w, int h, OBJECT_INER *objs,STUFFS *stuff){ attractor_constructor(w,h,&stuff->obj.attractor,(float)w/2,(float)h/2,100); } -void applyForce(OBJECT_INER *obj, Point force){ +void applyForce(STUFFS *stuff, OBJECT_INER *obj, Point force){ point_div(&force, &(Point){obj->mass,obj->mass}); - point_add(&obj->acc, &force); + Point add_vec=force; + point_add(&obj->acc, &add_vec); } void update(STUFFS *stuff, OBJECT_INER *obj){ - point_add(&obj->vel,&obj->acc); + Point add_vec=obj->acc; + point_add(&obj->vel,&add_vec); point_limit(&obj->vel,10); - point_add(&obj->pos, &obj->vel); + + add_vec=obj->vel; + point_mul(&add_vec,&(Point){(float)stuff->dt/10,(float)stuff->dt/10}); + point_add(&obj->pos, &add_vec); obj->acc = (Point){0,0}; } @@ -74,7 +79,7 @@ float calcG(ATTRACTOR *attractor, OBJECT_INER *obj,STUFFS *stuff){ G=5; } - if(G!=G) G=1; + if(G!=G) G=5; return G; } @@ -85,10 +90,10 @@ void attract(ATTRACTOR *attractor, OBJECT_INER *obj,STUFFS *stuff){ dist = constrain(dist,100,1000); float G = calcG(attractor,obj,stuff); - float strength = G * (attractor->mass * obj->mass) / dist; + float strength = ((float)stuff->dt/10) * G * (attractor->mass * obj->mass) / dist; point_set_mag(&force, strength); - applyForce(obj,force); + applyForce(stuff,obj,force); } void atr_show(STUFFS *stuff,ATTRACTOR *attr){ @@ -96,8 +101,8 @@ void atr_show(STUFFS *stuff,ATTRACTOR *attr){ } void draw(STUFFS *stuff){ - SDL_SetRenderDrawColor(stuff->renderer,0,0,0,30); - //SDL_RenderClear(renderer); + SDL_SetRenderDrawColor(stuff->renderer,0,0,0,50); + //SDL_RenderClear(stuff->renderer); SDL_RenderFillRect(stuff->renderer, &(SDL_Rect){0,0,stuff->width,stuff->height}); static int frame_i1=0; @@ -107,16 +112,17 @@ void draw(STUFFS *stuff){ attract(&stuff->obj.attractor, stuff->obj.objs[i], stuff); float vol=stuff->volume; - if(vol==vol && frame_i1>200){ + if(vol==vol && frame_i1>100){ Point rand_p={RandomFloat(-1, 1),RandomFloat(-1, 1)}; point_set_mag(&rand_p, stuff->volume*100); + point_mul(&rand_p,&(Point){(float)stuff->dt/10,(float)stuff->dt/10}); point_add(&stuff->obj.objs[i]->vel,&rand_p); } } SDL_SetRenderDrawColor(stuff->renderer, 100,20,20,180); atr_show(stuff,&stuff->obj.attractor); - if(frame_i1>200) frame_i1=0; + if(frame_i1>100) frame_i1=0; frame_i1++; SDL_RenderPresent(stuff->renderer); } diff --git a/src/objects.h b/src/objects.h index d1faa02..ecbda75 100755 --- a/src/objects.h +++ b/src/objects.h @@ -47,6 +47,7 @@ typedef struct{ OBJECT obj; flaot volume; flaot avg_peak; + Uint32 dt; } STUFFS; #endif @@ -180,7 +180,7 @@ void *volume_input(void* threadarg){ data.stream = pw_stream_new_simple( pw_main_loop_get_loop(data.loop), - "audio-capture", + "audio_reactive", props, &stream_events, &data); |
