From: Matthew Owens Date: Thu, 1 Oct 2020 14:48:39 +0000 (+0100) Subject: basic paddle movement X-Git-Url: https://git.owens.tech/git.owens.tech/git.owens.tech/git?a=commitdiff_plain;h=ba16549664b44b047cc604f8383131bed907aa9f;p=pong.git basic paddle movement --- diff --git a/src/main.c b/src/main.c index 0c0ce22..fb05761 100644 --- a/src/main.c +++ b/src/main.c @@ -1,10 +1,141 @@ #include #include "clogs.h" #include "vector.h" +#include +#include +#include "timestep.h" + +typedef struct Colour { + Uint8 r; + Uint8 g; + Uint8 b; + Uint8 a; +} Colour; + +typedef struct Box { + Colour c; + SDL_Rect r; +} Box; + +static SDL_Window *window = NULL; +static SDL_Renderer *renderer = NULL; +static const Vec2d windowSize = { 1920, 1080 }; +static bool quit = false; +static const float speed = 5.f; +static const Colour bgClr = { 0x1c, 0x1c, 0x1c, 0xff }; + +Box pad, ball; +Box *vec_bricks = NULL; + +static void paddle_init() +{ + pad.r.w = 192; + pad.r.h = 32; + + pad.r.x = (windowSize.x / 2) - (pad.r.w / 2); + pad.r.y = windowSize.y - ((windowSize.y / 8) - (pad.r.h / 2)); + + pad.c.r = pad.c.g = pad.c.b = 0x55; + pad.c.a = 0xff; + + COUT("pad.r. (%d, %d, %d, %d)", + pad.r.x, pad.r.y, pad.r.w, pad.r.h); +} + +static void paddle_render() +{ + SDL_SetRenderDrawColor(renderer, pad.c.r, pad.c.g, pad.c.b, pad.c.a); + SDL_RenderFillRect(renderer, &pad.r); +} + +static void paddle_update(const Uint8* keyStates, float dt) +{ + if(keyStates[SDL_SCANCODE_LEFT] || keyStates[SDL_SCANCODE_A]) { + pad.r.x -= speed * dt; + } + + if(keyStates[SDL_SCANCODE_RIGHT] || keyStates[SDL_SCANCODE_D]) { + pad.r.x += speed * 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 bool init() +{ + if(SDL_Init( + SDL_INIT_TIMER | SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_EVENTS + ) < 0) { + COUT_ERR("SDL init failed! SDL_Error: %s", SDL_GetError()); + return false; + } + + window = SDL_CreateWindow("pong", + SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, + windowSize.x, windowSize.y, + SDL_WINDOW_INPUT_FOCUS | SDL_WINDOW_BORDERLESS); + + if(window == NULL) { + COUT_ERR("Couldn't create window! SDL_Error: %s", SDL_GetError()); + return false; + } + + renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED); + + if(renderer == NULL) { + COUT_ERR("Couldn't create renderer! SDL_Error: %s", SDL_GetError()); + return false; + } + + ts_init(60.f); + paddle_init(); + return true; +} + +static void update() +{ + SDL_Event e; + int nPhysUpdates = 0; + const Uint8 *keyStates = SDL_GetKeyboardState(NULL); + + ts_update(); + + while(SDL_PollEvent(&e) != 0){ + if(e.type == SDL_QUIT) { quit = true; } + } + + while(ts_phys_required(nPhysUpdates)){ + paddle_update(keyStates, 1.f); + nPhysUpdates++; + } +} + +static void render() +{ + SDL_SetRenderDrawColor(renderer, bgClr.r, bgClr.g, bgClr.b, bgClr.a); + SDL_RenderClear(renderer); + paddle_render(); + SDL_RenderPresent(renderer); +} + +static void cleanup() +{ + SDL_DestroyRenderer(renderer); + SDL_DestroyWindow(window); + SDL_Quit(); +} int main() { - printf("hello, world\n"); - COUT("TEST"); + //TODO: run on primary display + if(!init()) { return 1; } + + while(!quit) { + update(); + render(); + } + + cleanup(); return 0; } diff --git a/src/main.o b/src/main.o index afd1b90..667fbd2 100644 Binary files a/src/main.o and b/src/main.o differ