flappychik

Silly SDL2 game
git clone git://bsandro.tech/flappychik
Log | Files | Refs

commit 9077f846501611f1de029fc430a3a6df72c2cbfa
parent 70b5024d4ab531a17bab3837403b112aabb989de
Author: bsandro <brian.drosan@gmail.com>
Date:   Fri, 25 Mar 2022 01:15:12 +0200

Scrolling background; drawing rectangles attempt.

Diffstat:
Mmain.c | 50++++++++++++++++++++++++++++++++++++++++++++++++--
1 file changed, 48 insertions(+), 2 deletions(-)

diff --git a/main.c b/main.c @@ -16,6 +16,10 @@ #define GAME_WIN_HEIGHT 600 #define GAME_FPS 60 #define GAME_FALL_ACCEL 0.85 // pixels per second^2 essentially +#define GAME_BG_SCROLL_VELOCITY 0.3 +#define GAME_SCROLL_VELOCITY 0.4 +#define GAME_COLUMN_WIDTH 80 +#define GAME_COLUMN_HEIGHT 200 enum game_state_t { GAME_STATE_INIT, GAME_STATE_RUNNING, GAME_STATE_OVER }; @@ -35,6 +39,9 @@ struct game_t { uint64_t last_frame; struct sprite_t bg; struct sprite_t ship; + int bg_x; + SDL_Rect *columns; + int columns_count; }; struct menu_t { @@ -47,8 +54,10 @@ struct menu_t { void draw_game(uint64_t ftime, struct game_t *game) { game->ship.velocity += game->ship.accel * ftime; game->ship.rect.y += (ftime * game->ship.velocity / 1000.0); + int bg_offset = ftime * GAME_BG_SCROLL_VELOCITY; // background position offset + game->bg_x = (game->bg_x - bg_offset) % game->bg.rect.w; // can be only %width max - if (game->ship.rect.y >= game->scrH) { + if (game->ship.rect.y >= game->scrH) { // falling beyond the playing area (screen) game->state = GAME_STATE_INIT; } @@ -62,7 +71,7 @@ void draw_game(uint64_t ftime, struct game_t *game) { SDL_RenderClear(game->renderer); // tile background to whole screen area - for (int bg_x = 0; bg_x < game->scrW; bg_x += game->bg.rect.w) { + for (int bg_x = game->bg_x; 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; @@ -70,6 +79,10 @@ void draw_game(uint64_t ftime, struct game_t *game) { } } + // columns + SDL_SetRenderDrawColor(game->renderer, 0, 154, 213, 0); + SDL_RenderFillRects(game->renderer, game->columns, game->columns_count); + SDL_RenderCopyEx(game->renderer, game->ship.texture, NULL, &game->ship.rect, game->ship.angle, NULL, SDL_FLIP_NONE); SDL_RenderPresent(game->renderer); @@ -77,6 +90,8 @@ void draw_game(uint64_t ftime, struct game_t *game) { } void draw_menu(struct menu_t *menu) { + SDL_SetRenderDrawColor(menu->game->renderer, 2, 40, 223, 0); + if (menu->surface == NULL) { SDL_Color color = { 255, 255, 255, 0 }; menu->surface = TTF_RenderText_Solid(menu->font, "Press <space> to play", color); @@ -96,6 +111,31 @@ void draw_menu(struct menu_t *menu) { SDL_RenderPresent(menu->game->renderer); } +void create_columns(SDL_Rect **rects, int count) { + assert(count > 0); + assert(*rects == NULL); + *rects = calloc(count, sizeof(SDL_Rect)); + assert(*rects != NULL); +} + +void init_columns(SDL_Rect **columns, int count, int hidpi_scale) { + assert(count > 0); + for (int i = 0; i < count; ++i) { + SDL_Rect *column = *columns + i; + column->w = GAME_COLUMN_WIDTH * hidpi_scale; + column->h = GAME_COLUMN_HEIGHT * hidpi_scale; + column->x = 200 * i; + column->y = 0; + } +} + +void destroy_columns(SDL_Rect **rects) { + assert(rects != NULL); + assert(*rects != NULL); + free(*rects); + *rects = NULL; +} + int main(int argc, char *argv[]) { (void)argc; (void)argv; @@ -143,6 +183,11 @@ int main(int argc, char *argv[]) { game.bg.rect.h *= hidpi_scale; game.ship.accel = GAME_FALL_ACCEL; + // "enemy" columns + game.columns_count = 2; // max columns on screen + create_columns(&game.columns, game.columns_count); + init_columns(&game.columns, game.columns_count, hidpi_scale); + // menu struct menu_t menu = {0}; menu.game = &game; @@ -188,6 +233,7 @@ int main(int argc, char *argv[]) { } // cleanup + destroy_columns(&game.columns); TTF_CloseFont(menu.font); SDL_DestroyTexture(game.bg.texture); SDL_DestroyTexture(game.ship.texture);