basic paddle movement
authorMatthew Owens <matthew@owens.tech>
Thu, 1 Oct 2020 14:48:39 +0000 (15:48 +0100)
committerMatthew Owens <matthew@owens.tech>
Thu, 1 Oct 2020 14:48:39 +0000 (15:48 +0100)
src/main.c
src/main.o

index 0c0ce22..fb05761 100644 (file)
 #include <stdio.h>
 #include "clogs.h"
 #include "vector.h"
+#include <SDL2/SDL.h>
+#include <stdbool.h>
+#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;
 }
index afd1b90..667fbd2 100644 (file)
Binary files a/src/main.o and b/src/main.o differ