From: Matthew Owens Date: Fri, 2 Oct 2020 11:42:51 +0000 (+0100) Subject: display scaling X-Git-Url: https://git.owens.tech/rss.xml/rss.xml/git?a=commitdiff_plain;h=e9b5a087cc1efdf291ff126106989ec710976821;p=pong.git display scaling --- diff --git a/src/main.c b/src/main.c index f1ab276..d902d0b 100644 --- a/src/main.c +++ b/src/main.c @@ -35,19 +35,23 @@ typedef struct Vbox { static SDL_Window *window = NULL; static SDL_Renderer *renderer = NULL; -static Vec2di windowSize;// = { 1920, 1080 }; static bool quit = false; -static const float speed = 20.f; +static float speed = 20.f; static const Colour bgClr = { 0x1c, 0x1c, 0x1c, 0xff }; +// base window size to set pixel-values against, actual window size to scale to +static const Vec2di baseWindowSize = { 1920, 1080 }; +static Vec2di windowSize; +static Vec2df windowScale; + Vbox pad, ball; Box *vec_bricks = NULL; Vec2df vline[2]; static void paddle_init() { - pad.r.w = 192; - pad.r.h = 32; + pad.r.w = 192 * windowScale.x; + pad.r.h = 32 * windowScale.y; pad.r.x = (windowSize.x / 2) - (pad.r.w / 2); pad.r.y = windowSize.y - ((windowSize.y / 8) - (pad.r.h / 2)); @@ -62,6 +66,8 @@ static void paddle_init() static void ball_init() { ball.r.h = ball.r.w = 32; + ball.r.w *= windowScale.x; + ball.r.h *= windowScale.y; ball.r.x = (windowSize.x / 2) - (ball.r.w / 2); ball.r.y = (windowSize.y / 2) - (ball.r.h / 2); @@ -119,22 +125,6 @@ static void ball_update(float dt) vline[1].y = vline[0].y + ball.v.y + ( ball.v.y > 0 ? ball.r.h / 2 : 0); - // paddle bounds checks & responses - /* - if( ( - (ball.r.x > pad.r.x && ball.r.x < pad.r.x + pad.r.w ) || - (ball.r.x + ball.r.w > pad.r.x && ball.r.x + ball.r.w < pad.r.x + pad.r.w ) - ) && ( - (ball.r.y > pad.r.y && ball.r.y < pad.r.y + pad.r.h ) || - (ball.r.y + ball.r.h > pad.r.y && ball.r.y + ball.r.h < pad.r.y + pad.r.h ) - ) - ) - { - ball.v.y *= -1; - } - */ - - // screen bounds checks & responses if(ball.r.x + ball.r.w > windowSize.x && !collisionResponded){ ball.r.x = windowSize.x - ball.r.w; @@ -163,6 +153,9 @@ static bool init() windowSize.x = dm.w; windowSize.y = dm.h; + windowScale.x = windowSize.x / baseWindowSize.x; + windowScale.y = windowSize.y / baseWindowSize.y; + window = SDL_CreateWindow("pong", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, windowSize.x, windowSize.y,