static int UpdateLogicalSize(SDL_Renderer *renderer) { int w, h; float want_aspect; float real_aspect; float scale; SDL_Rect viewport; if (SDL_GetRendererOutputSize(renderer, &w, &h) < 0) { return -1; } want_aspect = (float)renderer->logical_w / renderer->logical_h; real_aspect = (float)w / h; /* Clear the scale because we're setting viewport in output coordinates */ SDL_RenderSetScale(renderer, 1.0f, 1.0f); if (SDL_fabs(want_aspect-real_aspect) < 0.0001) { /* The aspect ratios are the same, just scale appropriately */ scale = (float)w / renderer->logical_w; SDL_RenderSetViewport(renderer, NULL); } else if (want_aspect > real_aspect) { /* We want a wider aspect ratio than is available - letterbox it */ scale = (float)w / renderer->logical_w; viewport.x = 0; viewport.w = w; viewport.h = (int)SDL_ceil(renderer->logical_h * scale); viewport.y = (h - viewport.h) / 2; SDL_RenderSetViewport(renderer, &viewport); } else { /* We want a narrower aspect ratio than is available - use side-bars */ scale = (float)h / renderer->logical_h; viewport.y = 0; viewport.h = h; viewport.w = (int)SDL_ceil(renderer->logical_w * scale); viewport.x = (w - viewport.w) / 2; SDL_RenderSetViewport(renderer, &viewport); } /* Set the new scale */ SDL_RenderSetScale(renderer, scale, scale); return 0; }
static float bestDollarDifference(SDL_FloatPoint* points,SDL_FloatPoint* templ) { /*------------BEGIN DOLLAR BLACKBOX------------------ -TRANSLATED DIRECTLY FROM PSUDEO-CODE AVAILABLE AT- -"http://depts.washington.edu/aimgroup/proj/dollar/" */ double ta = -M_PI/4; double tb = M_PI/4; double dt = M_PI/90; float x1 = (float)(PHI*ta + (1-PHI)*tb); float f1 = dollarDifference(points,templ,x1); float x2 = (float)((1-PHI)*ta + PHI*tb); float f2 = dollarDifference(points,templ,x2); while (SDL_fabs(ta-tb) > dt) { if (f1 < f2) { tb = x2; x2 = x1; f2 = f1; x1 = (float)(PHI*ta + (1-PHI)*tb); f1 = dollarDifference(points,templ,x1); } else { ta = x1; x1 = x2; f1 = f2; x2 = (float)((1-PHI)*ta + PHI*tb); f2 = dollarDifference(points,templ,x2); } } /* if (f1 <= f2) printf("Min angle (x1): %f\n",x1); else if (f1 > f2) printf("Min angle (x2): %f\n",x2); */ return SDL_min(f1,f2); }