#include <stdbool.h>
#include "timestep.h"
-typedef struct Vec2d {
+typedef struct Vec2di {
int x;
int y;
-} Vec2d;
+} Vec2di;
+
+typedef struct Vec2df {
+ float x;
+ float y;
+} Vec2df;
typedef struct Colour {
Uint8 r;
SDL_Rect r;
} Box;
+typedef struct Vbox {
+ Colour c;
+ SDL_Rect r;
+ Vec2df v;
+} Vbox;
+
static SDL_Window *window = NULL;
static SDL_Renderer *renderer = NULL;
-static const Vec2d windowSize = { 1920, 1080 };
+static const Vec2di windowSize = { 1920, 1080 };
static bool quit = false;
static const float speed = 20.f;
static const Colour bgClr = { 0x1c, 0x1c, 0x1c, 0xff };
-Box pad, ball;
+Vbox pad, ball;
Box *vec_bricks = NULL;
+Vec2df vline[2];
static void paddle_init()
{
pad.c.r = pad.c.g = pad.c.b = 0x55;
pad.c.a = 0xff;
- COUT("pad.r. (%d, %d, %d, %d)",
+ COUT("pad.r (%d, %d, %d, %d)",
pad.r.x, pad.r.y, pad.r.w, pad.r.h);
}
-static void paddle_render()
+static void ball_init()
+{
+ ball.r.h = ball.r.w = 32;
+ ball.r.x = (windowSize.x / 2) - (ball.r.w / 2);
+ ball.r.y = (windowSize.y / 2) - (ball.r.h / 2);
+
+ ball.c.r = 0xb3;
+ ball.c.g = ball.c.b = 0x50;
+ ball.c.a = 0xff;
+
+ ball.v.y = speed;
+}
+
+static void box_render(const Box *b)
+{
+ SDL_SetRenderDrawColor(renderer, b->c.r, b->c.g, b->c.b, b->c.a);
+ SDL_RenderFillRect(renderer, &b->r);
+}
+
+static void vbox_render(const Vbox *b)
{
- SDL_SetRenderDrawColor(renderer, pad.c.r, pad.c.g, pad.c.b, pad.c.a);
- SDL_RenderFillRect(renderer, &pad.r);
+ SDL_SetRenderDrawColor(renderer, b->c.r, b->c.g, b->c.b, b->c.a);
+ SDL_RenderFillRect(renderer, &b->r);
}
static void paddle_update(const Uint8* keyStates, float dt)
{
+ pad.v.x = 0;
+
if(keyStates[SDL_SCANCODE_LEFT] || keyStates[SDL_SCANCODE_A]) {
- pad.r.x -= speed * dt;
+ pad.v.x = -speed;
}
if(keyStates[SDL_SCANCODE_RIGHT] || keyStates[SDL_SCANCODE_D]) {
- pad.r.x += speed * dt;
+ pad.v.x = speed;
}
+
+ pad.r.x += pad.v.x * dt;
+
// screen bounds check
if(pad.r.x + pad.r.w > windowSize.x) { pad.r.x = windowSize.x - pad.r.w; }
if(pad.r.x < 0 ) { pad.r.x = 0; }
}
+static void ball_update(float dt)
+{
+ bool collisionResponded = false;
+ // applying velocity
+ ball.r.x += ball.v.x * dt;
+ ball.r.y += ball.v.y * dt;
+
+ vline[0].x = ball.r.x + ball.r.w / 2;
+ vline[0].y = ball.r.y + ball.r.h / 2;
+
+ // todo: fix
+ // terniary should be replaced with the size of the ball * normalised vel
+ vline[1].x = vline[0].x + ball.v.x + ( ball.v.x > 0 ? ball.r.w / 2 : 0);
+ 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;
+ ball.v.x *= -1;
+ }
+ if(ball.r.y + ball.r.h > windowSize.y && !collisionResponded){
+ ball.r.y = windowSize.y - ball.r.h;
+ ball.v.y *= -1;
+ }
+ if(ball.r.y < 0 ) { ball.r.y = 0; ball.v.y *= -1; }
+ if(ball.r.x < 0 ) { ball.r.x = 0; ball.v.x *= -1; }
+}
+
static bool init()
{
if(SDL_Init(
ts_init(60.f);
paddle_init();
+ ball_init();
return true;
}
ts_update();
while(ts_phys_required(nPhysUpdates)){
- paddle_update(keyStates, 1.f);
+ paddle_update(keyStates, ts_delta());
+ ball_update(ts_delta());
nPhysUpdates++;
}
}
{
SDL_SetRenderDrawColor(renderer, bgClr.r, bgClr.g, bgClr.b, bgClr.a);
SDL_RenderClear(renderer);
- paddle_render();
+ vbox_render(&pad);
+ vbox_render(&ball);
+ SDL_SetRenderDrawColor(renderer, 0x00, 0xff, 0x00, 0xff);
+ SDL_RenderDrawLine(renderer, vline[0].x, vline[0].y, vline[1].x, vline[1].y);
SDL_RenderPresent(renderer);
}