diff options
author | Juan Manuel Tomas <jtomas1815@gmail.com> | 2022-01-23 00:40:36 -0300 |
---|---|---|
committer | Juan Manuel Tomas <jtomas1815@gmail.com> | 2022-01-23 00:40:36 -0300 |
commit | aadd4703f6b8381f1218f4bb9f6c2de25f340f2c (patch) | |
tree | e66e64fbe1d22a3497561e5fd0d0b6c78b12baa0 | |
parent | aca708aa91f9db51a69f27e3e855b24144bd26ed (diff) | |
download | cannons-aadd4703f6b8381f1218f4bb9f6c2de25f340f2c.tar.gz cannons-aadd4703f6b8381f1218f4bb9f6c2de25f340f2c.zip |
Refactor main loop
-rw-r--r-- | main.c | 123 | ||||
-rw-r--r-- | platform.c | 100 |
2 files changed, 108 insertions, 115 deletions
@@ -1,7 +1,37 @@ #include <assert.h> #include <stdio.h> #include <stdlib.h> -#include "platform.c" +#include <SDL2/SDL.h> + +#define WINDOW_W 1280 +#define WINDOW_H 720 + +static inline double GetCurrentTimestamp() { + return (double) SDL_GetTicks64() / 1000.0; +} + +SDL_Texture *CreateTiledTexture(SDL_Renderer *context, const char *filename, int width, int height) { + SDL_Surface *texture_surface = SDL_LoadBMP(filename); + SDL_Texture *texture_tile = SDL_CreateTextureFromSurface(context, texture_surface); + int tile_w = texture_surface->w; + int tile_h = texture_surface->h; + SDL_FreeSurface(texture_surface); + + SDL_Texture *texture = SDL_CreateTexture(context, + SDL_PIXELFORMAT_RGBA8888, + SDL_TEXTUREACCESS_TARGET, + width, height); + SDL_SetRenderTarget(context, texture); + for (int y = 0; y < height; y += tile_h) { + for (int x = 0; x < width; x += tile_w) { + SDL_Rect tile_rect = { x, y, tile_w, tile_h }; + SDL_RenderCopy(context, texture_tile, 0, &tile_rect); + } + } + SDL_DestroyTexture(texture_tile); + SDL_SetRenderTarget(context, 0); + return texture; +} void PaintMask(float *mask, float x, float y, float color, float size) { for (int j = y - size; j < y + size; j++) { @@ -17,25 +47,41 @@ void PaintMask(float *mask, float x, float y, float color, float size) { } } -void GameMain() { - // Terrain generation +void GameMain(SDL_Renderer *context) { + SDL_Texture *background = CreateTiledTexture(context, "background.bmp", WINDOW_W, WINDOW_H); + + SDL_Texture *foreground = CreateTiledTexture(context, "foreground.bmp", WINDOW_W, WINDOW_H); + SDL_SetTextureBlendMode(foreground, SDL_BLENDMODE_MOD); + + SDL_Texture *terrain = SDL_CreateTexture(context, + SDL_PIXELFORMAT_RGBA8888, + SDL_TEXTUREACCESS_TARGET, + WINDOW_W, WINDOW_H); + SDL_SetTextureBlendMode(terrain, SDL_BLENDMODE_BLEND); + + SDL_Texture *mask_texture = SDL_CreateTexture(context, + SDL_PIXELFORMAT_RGBA8888, + SDL_TEXTUREACCESS_STREAMING, + WINDOW_W, WINDOW_H); + float *mask = calloc(WINDOW_W * WINDOW_H, sizeof(float)); for (int i = 0; i < 100 * WINDOW_W; i++) { mask[i] = 1.0; } - // Player variables float player_x = 100; float player_y = 500; float player_vy = 0; float player_ay = -9.8; - // Timer variables float old_t = 0.0; - // Event handling int exit = 0; while (!exit) { + double new_t = GetCurrentTimestamp(); + double dt = (new_t - old_t) * 10; + old_t = new_t; + SDL_Event event; while (SDL_PollEvent(&event)) { if (event.type == SDL_QUIT) { @@ -51,26 +97,73 @@ void GameMain() { PaintMask(mask, mouse_x, WINDOW_H - mouse_y, 0.0, size); } - // Time delta calculation - double new_t = GetCurrentTimestamp(); - double dt = new_t - old_t; - old_t = new_t; + const Uint8 *keys = SDL_GetKeyboardState(0); + if (keys[SDL_SCANCODE_D]) { + player_x += 10 * dt; + } + if (keys[SDL_SCANCODE_A]) { + player_x -= 10 * dt; + } - // Player logic - if (mask[(int)player_x + (int)player_y * WINDOW_W] == 0.0) { + if (player_x < 0) { + player_x += 10 * dt; + } else if (player_x >= WINDOW_W) { + player_x -= 10 * dt; + } + if (mask[(int)player_x + (int)player_y * WINDOW_W] == 0.0 && + player_y >= 0 && player_y < WINDOW_H) { player_y += player_vy * dt; player_vy += player_ay * dt; } else { - player_y += 1; + player_y += 10 * dt; player_vy = 0; } - RenderScene(mask, player_x, player_y); + void *pixels; + int pitch; + SDL_LockTexture(mask_texture, 0, &pixels, &pitch); + Uint32 *p = (Uint32 *) pixels; + for (int j = 0; j < WINDOW_H; j++) { + for (int i = 0; i < WINDOW_W; i++) { + p[i + j * WINDOW_W] = mask[i + (WINDOW_H - j) * WINDOW_W] ? 0xffffffff : 0x00000000; + } + } + SDL_UnlockTexture(mask_texture); + + SDL_SetRenderDrawColor(context, 40, 40, 40, 255); + SDL_RenderClear(context); + SDL_RenderCopy(context, background, 0, 0); + SDL_SetRenderTarget(context, terrain); + SDL_SetRenderDrawColor(context, 0, 0, 0, 0); + SDL_RenderClear(context); + SDL_RenderCopy(context, mask_texture, 0, 0); + SDL_RenderCopy(context, foreground, 0, 0); + SDL_SetRenderTarget(context, 0); + SDL_RenderCopy(context, terrain, 0, 0); + SDL_SetRenderDrawColor(context, 255, 0, 0, 255); + SDL_Rect player_rect = {player_x - 5, WINDOW_H - player_y - 10, 10, 10 }; + SDL_RenderFillRect(context, &player_rect); + SDL_RenderPresent(context); SDL_Delay(1); } } int main() { - PlatformMain(GameMain); + SDL_Init(SDL_INIT_VIDEO); + + SDL_Window *window = SDL_CreateWindow("Cannons", + SDL_WINDOWPOS_UNDEFINED, + SDL_WINDOWPOS_UNDEFINED, + WINDOW_W, + WINDOW_H, + 0); + + SDL_Renderer *context = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED); + + GameMain(context); + + SDL_DestroyRenderer(context); + SDL_DestroyWindow(window); + SDL_Quit(); return 0; } diff --git a/platform.c b/platform.c deleted file mode 100644 index 24f5394..0000000 --- a/platform.c +++ /dev/null @@ -1,100 +0,0 @@ -#include <SDL2/SDL.h> - -#define WINDOW_W 1280 -#define WINDOW_H 720 - -SDL_Renderer *context = 0; -SDL_Texture *background = 0; -SDL_Texture *foreground = 0; -SDL_Texture *terrain = 0; -SDL_Texture *mask_texture = 0; - -double GetCurrentTimestamp() { - return (double) SDL_GetTicks64() / 1000.0; -} - -SDL_Texture *CreateTiledTexture(SDL_Renderer *context, const char *filename, int width, int height) { - SDL_Surface *texture_surface = SDL_LoadBMP(filename); - SDL_Texture *texture_tile = SDL_CreateTextureFromSurface(context, texture_surface); - int tile_w = texture_surface->w; - int tile_h = texture_surface->h; - SDL_FreeSurface(texture_surface); - - SDL_Texture *texture = SDL_CreateTexture(context, - SDL_PIXELFORMAT_RGBA8888, - SDL_TEXTUREACCESS_TARGET, - width, height); - SDL_SetRenderTarget(context, texture); - for (int y = 0; y < height; y += tile_h) { - for (int x = 0; x < width; x += tile_w) { - SDL_Rect tile_rect = { x, y, tile_w, tile_h }; - SDL_RenderCopy(context, texture_tile, 0, &tile_rect); - } - } - SDL_DestroyTexture(texture_tile); - SDL_SetRenderTarget(context, 0); - return texture; -} - -void PlatformMain(void (*GameMain)(void)) { - SDL_Init(SDL_INIT_VIDEO); - - SDL_Window *window = SDL_CreateWindow("Cannons", - SDL_WINDOWPOS_UNDEFINED, - SDL_WINDOWPOS_UNDEFINED, - WINDOW_W, - WINDOW_H, - 0); - - context = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED); - - background = CreateTiledTexture(context, "background.bmp", WINDOW_W, WINDOW_H); - - foreground = CreateTiledTexture(context, "foreground.bmp", WINDOW_W, WINDOW_H); - SDL_SetTextureBlendMode(foreground, SDL_BLENDMODE_MOD); - - terrain = SDL_CreateTexture(context, - SDL_PIXELFORMAT_RGBA8888, - SDL_TEXTUREACCESS_TARGET, - WINDOW_W, WINDOW_H); - SDL_SetTextureBlendMode(terrain, SDL_BLENDMODE_BLEND); - - mask_texture = SDL_CreateTexture(context, - SDL_PIXELFORMAT_RGBA8888, - SDL_TEXTUREACCESS_STREAMING, - WINDOW_W, WINDOW_H); - - GameMain(); - - SDL_DestroyRenderer(context); - SDL_DestroyWindow(window); - SDL_Quit(); -} - -void RenderScene(float *mask, int player_x, int player_y) { - void *pixels; - int pitch; - SDL_LockTexture(mask_texture, 0, &pixels, &pitch); - Uint32 *p = (Uint32 *) pixels; - for (int j = 0; j < WINDOW_H; j++) { - for (int i = 0; i < WINDOW_W; i++) { - p[i + j * WINDOW_W] = mask[i + (WINDOW_H - j) * WINDOW_W] ? 0xffffffff : 0x00000000; - } - } - SDL_UnlockTexture(mask_texture); - - SDL_SetRenderDrawColor(context, 40, 40, 40, 255); - SDL_RenderClear(context); - SDL_RenderCopy(context, background, 0, 0); - SDL_SetRenderTarget(context, terrain); - SDL_SetRenderDrawColor(context, 0, 0, 0, 0); - SDL_RenderClear(context); - SDL_RenderCopy(context, mask_texture, 0, 0); - SDL_RenderCopy(context, foreground, 0, 0); - SDL_SetRenderTarget(context, 0); - SDL_RenderCopy(context, terrain, 0, 0); - SDL_SetRenderDrawColor(context, 255, 0, 0, 255); - SDL_Rect player_rect = {player_x - 5, WINDOW_H - player_y - 10, 10, 10 }; - SDL_RenderFillRect(context, &player_rect); - SDL_RenderPresent(context); -} |