struct simple_soft_ctx *simple_soft_init(int w, int h, simple_soft_map_func map_func, int audio_rate, int audio_channels, julia_vis_pixel_format format) { struct simple_soft_ctx *ctx = malloc(sizeof(*ctx)); if(!ctx) return NULL; ctx->map_surf[0] = ctx->map_surf[1] = NULL; ctx->maxsrc = NULL; ctx->pal_ctx = NULL; ctx->pd = NULL; ctx->beat = NULL; ctx->cur_buf = ctx->prev_buf = ctx->fft_tmp = NULL; // force divisible by 16 ctx->im_w = w - w%16; ctx->im_h = h - h%16; simple_soft_change_map_func(ctx, map_func); ctx->m = 0; ctx->map_surf[0] = aligned_alloc(64, ctx->im_w * ctx->im_h * sizeof(uint16_t)); ctx->map_surf[1] = aligned_alloc(64, ctx->im_w * ctx->im_h * sizeof(uint16_t)); if(!ctx->map_surf[0] || !ctx->map_surf[1]) goto fail; memset(ctx->map_surf[0], 0, ctx->im_w * ctx->im_h * sizeof(uint16_t)); memset(ctx->map_surf[1], 0, ctx->im_w * ctx->im_h * sizeof(uint16_t)); ctx->pal_ctx = pal_ctx_pix_format_new(format); ctx->maxsrc_rate = 24; //TODO: add a property that can change this ctx->maxsrc = maxsrc_new(ctx->im_w, ctx->im_h); ctx->pd = new_point_data(ctx->rational_julia?4:2); if(!ctx->maxsrc || !ctx->pal_ctx || !ctx->pd) goto fail; ctx->last_beat_time = 0; ctx->lastpalstep = 0; ctx->maxfrms = 1; ctx->beats = 0; ctx->beat = beat_new(); if(!ctx->beat) goto fail; ctx->audio_bufp = 0; ctx->sample_count = 0; ctx->sample_rate = audio_rate; ctx->channels = audio_channels; int nr_samp = ctx->nr_samp = (audio_rate<50000)?1024:2048; ctx->fft_tmp = aligned_alloc(64, nr_samp*sizeof(float)); ctx->cur_buf = aligned_alloc(64, sizeof(float)*nr_samp/2); ctx->prev_buf = aligned_alloc(64, sizeof(float)*nr_samp/2); if(!ctx->fft_tmp || !ctx->cur_buf || !ctx->prev_buf) goto fail; memset(ctx->fft_tmp, 0, nr_samp*sizeof(float)); memset(ctx->cur_buf, 0, sizeof(float)*nr_samp/2); memset(ctx->prev_buf, 0, sizeof(float)*nr_samp/2); return ctx; fail: simple_soft_destroy(ctx); return NULL; }
int main(int argc, char **argv) { optproc(argc, argv, &opts); if(audio_init(&opts) < 0) exit(1); SDL_Surface *screen = sdl_setup(&opts, IM_SIZE); im_w = screen->w - screen->w%16; im_h = screen->h - screen->h%16; printf("running with %dx%d bufs\n", im_w, im_h); if(strcmp(opts.map_name, "rational") == 0) { map_func = soft_map_rational_interp; if(opts.quality >= 1) map_func = soft_map_rational; } else if(strcmp(opts.map_name, "butterfly") == 0) { // map_func = soft_map_butterfly_interp; // if(opts.quality >= 1) map_func = soft_map_butterfly; } else if(opts.quality >= 1) map_func = soft_map; maxsrc = maxsrc_new(im_w, im_h); struct pal_ctx *pal_ctx = pal_ctx_new(screen->format->BitsPerPixel == 8); uint16_t *map_surf[3]; void *map_surf_mem = aligned_alloc(64, 3 * im_w * im_h * sizeof(uint16_t)); for(int i=0; i<3; i++) map_surf[i] = map_surf_mem + i * im_w * im_h * sizeof(uint16_t); memset(map_surf_mem, 0, 3 * im_w * im_h * sizeof(uint16_t)); tribuf *map_tb = tribuf_new((void **)map_surf, 1); int beats = 0; int frmcnt = 0; int lastdrawn=0; SDL_Thread *map_thread = SDL_CreateThread((void *)&run_map_thread, map_tb); SDL_Delay(100); uint32_t frametimes[FPS_HIST_LEN]; for(int i=1; i<FPS_HIST_LEN; i++) frametimes[i] = 0; uint32_t totframetime = frametimes[0] = MIN(1000/opts.draw_rate, 1); Uint32 tick0 = SDL_GetTicks(); Uint32 lastpalstep, lastupdate, fps_oldtime; fps_oldtime = lastpalstep = lastupdate = tick0; SDL_Event event; while(SDL_PollEvent(&event) >= 0) { if(event.type == SDL_QUIT || (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_ESCAPE)) { break; } else if(tribuf_check_fresh(map_tb)) { Uint32 now = SDL_GetTicks(); if(now - lastpalstep >= 2048/256) { // want pallet switch to take ~2 seconds pal_ctx_step(pal_ctx, IMIN((now - lastpalstep)*256/2048, 64)); lastpalstep = now; } pallet_blit_SDL(screen, tribuf_get_read(map_tb), im_w, im_h, pal_ctx_get_active(pal_ctx)); tribuf_finish_read(map_tb); char buf[64]; sprintf(buf,"%6.1f FPS %6.1f UPS %6.1f", map_fps, FPS_HIST_LEN*1000.0f/totframetime, maxfrms*1000.0f/(now-tick0)); DrawText(screen, buf); SDL_Flip(screen); int newbeat = beat_get_count(); if(newbeat != beats) pal_ctx_start_switch(pal_ctx, newbeat); beats = newbeat; now = SDL_GetTicks(); int delay = (tick0 + frmcnt*1000/opts.draw_rate) - now; if(delay > 0) SDL_Delay(delay); totframetime -= frametimes[frmcnt%FPS_HIST_LEN]; totframetime += (frametimes[frmcnt%FPS_HIST_LEN] = now - fps_oldtime); fps_oldtime = now; frmcnt++; } else { SDL_Delay(1000/opts.draw_rate/2); // wait half a frame and hope we get a new buffer } } running = 0; int status; SDL_WaitThread(map_thread, &status); aligned_free(map_surf_mem); audio_shutdown(); SDL_Quit(); return 0; }