commit e1bbf72e9fb81a2a9b3babda3cd8de069f6d54f1
parent b2c903946a4853d4f92408f0ad1695e085539559
Author: bsandro <brian.drosan@gmail.com>
Date: Fri, 18 Mar 2022 01:51:40 +0200
falling + bouncing on <space> key; basic game over support
Diffstat:
M | main.c | | | 52 | +++++++++++++++++++++++++++++++++++++++------------- |
1 file changed, 39 insertions(+), 13 deletions(-)
diff --git a/main.c b/main.c
@@ -11,20 +11,22 @@
#define WINDOW_FLAGS SDL_WINDOW_OPENGL | SDL_WINDOW_ALLOW_HIGHDPI
#endif
-#define GAME_WIN_WIDTH 640
-#define GAME_WIN_HEIGHT 480
+#define GAME_WIN_WIDTH 800
+#define GAME_WIN_HEIGHT 600
#define GAME_FPS 60
-#define GAME_SPEED 80 // pixels per second
struct sprite_t {
SDL_Texture *texture;
SDL_Rect rect;
- int angle;
+ int angle; // rotation in degrees
+ double velocity;
+ double accel;
};
struct game_t {
bool over;
SDL_Window *screen;
+ int scrW, scrH;
SDL_Renderer *renderer;
uint64_t last_frame;
struct sprite_t bg;
@@ -32,15 +34,31 @@ struct game_t {
};
void process_frame(uint64_t ts, uint64_t ftime, struct game_t *game) {
- int offset = round(ftime * GAME_SPEED / 1000.0f);
- //printf("offset: %d\n", offset);
+ game->ship.velocity += game->ship.accel * ftime;
+ game->ship.rect.y += (ftime * game->ship.velocity / 1000.0);
+
+ if (game->ship.rect.y >= game->scrH) {
+ game->over = true;
+ }
+
+ // top border
+ if (game->ship.rect.y < 50 && game->ship.velocity < 0) {
+ game->ship.velocity = 0;
+ }
- game->ship.rect.x += offset;
- game->ship.rect.y += offset;
game->ship.angle = ++game->ship.angle % 360;
SDL_RenderClear(game->renderer);
- SDL_RenderCopy(game->renderer, game->bg.texture, NULL, NULL);
+
+ // tile background to whole screen area
+ for (int bg_x = 0; bg_x < game->scrW; bg_x += game->bg.rect.w) {
+ for (int bg_y = 0; bg_y < game->scrH; bg_y += game->bg.rect.h) {
+ game->bg.rect.x = bg_x;
+ game->bg.rect.y = bg_y;
+ SDL_RenderCopy(game->renderer, game->bg.texture, NULL, &game->bg.rect);
+ }
+ }
+
SDL_RenderCopyEx(game->renderer, game->ship.texture, NULL, &game->ship.rect, game->ship.angle, NULL, SDL_FLIP_NONE);
SDL_RenderPresent(game->renderer);
@@ -73,16 +91,21 @@ int main(int argc, char *argv[]) {
game.ship.texture = SDL_CreateTextureFromSurface(game.renderer, knife_img);
assert(game.ship.texture != NULL);
+ SDL_GetClipRect(bg_img, &game.bg.rect);
+ SDL_GetClipRect(knife_img, &game.ship.rect);
+
SDL_FreeSurface(knife_img);
SDL_FreeSurface(bg_img);
- game.ship.rect.w = 56;
- game.ship.rect.h = 56;
- game.ship.angle = 0;
-
+ SDL_GetRendererOutputSize(game.renderer, &game.scrW, &game.scrH);
SDL_RenderPresent(game.renderer);
+ // initial values
game.last_frame = SDL_GetTicks64();
+ game.ship.rect.x = 150;
+ game.ship.rect.y = 50;
+ game.ship.accel = 0.85; // pixels per second^2 essentially
+
// main game loop
while (!game.over) {
while (SDL_PollEvent(&event)) {
@@ -94,6 +117,9 @@ int main(int argc, char *argv[]) {
if (event.key.keysym.sym == SDLK_q) { // quit if "q" button is pressed
game.over = true;
}
+ if (event.key.keysym.sym == SDLK_SPACE) {
+ game.ship.velocity = -600;
+ }
break;
}
}