From 09b065e85d7199f7b2082066fbf9ce66648592b7 Mon Sep 17 00:00:00 2001 From: Juan Manuel Tomas Date: Sun, 16 Jan 2022 18:08:09 -0300 Subject: Add basic player --- main.c | 35 ++++++++++++++++++++++++++++++----- 1 file changed, 30 insertions(+), 5 deletions(-) (limited to 'main.c') diff --git a/main.c b/main.c index 646d79b..da37ce4 100644 --- a/main.c +++ b/main.c @@ -54,15 +54,15 @@ int main() { SDL_Texture *foreground = createTiledTexture(context, "foreground.bmp"); SDL_SetTextureBlendMode(foreground, SDL_BLENDMODE_MOD); - SDL_Surface *mask_surface = SDL_CreateRGBSurfaceWithFormat(0, - WINDOW_W, WINDOW_H, 32, SDL_PIXELFORMAT_RGBA8888); - SDL_Texture *terrain = SDL_CreateTexture(context, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, WINDOW_W, WINDOW_H); SDL_SetTextureBlendMode(terrain, SDL_BLENDMODE_BLEND); + // Terrain generation + SDL_Surface *mask_surface = SDL_CreateRGBSurfaceWithFormat(0, + WINDOW_W, WINDOW_H, 32, SDL_PIXELFORMAT_RGBA8888); SDL_LockSurface(mask_surface); for (int i = 0; i < mask_surface->w * mask_surface->h; i++) { ((Uint32 *)mask_surface->pixels)[i] = 0x00000000; @@ -72,9 +72,18 @@ int main() { } SDL_UnlockSurface(mask_surface); SDL_Texture *mask_texture = SDL_CreateTextureFromSurface(context, mask_surface); - renderMaskedTexture(context, terrain, mask_texture, foreground); + // Player variables + float player_x = 100; + float player_y = 100; + float player_vy = 0; + float player_ay = 1; + + // Timer variables + float old_t = 0.0; + + // Event handling int exit = 0; while (!exit) { SDL_Event event = {0}; @@ -114,14 +123,30 @@ int main() { renderMaskedTexture(context, terrain, mask_texture, foreground); } + + // Main loop SDL_SetRenderDrawColor(context, 40, 40, 40, 255); SDL_RenderClear(context); SDL_RenderCopy(context, background, 0, 0); SDL_RenderCopy(context, terrain, 0, 0); + // Time delta calculation + double new_t = (double) SDL_GetTicks64() / 1000.0; + double dt = new_t - old_t; + old_t = new_t; + + // Player logic + player_y += player_vy * dt; + player_vy += player_ay * dt; + + // Draw player + 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(10); + SDL_Delay(1); } SDL_DestroyRenderer(context); -- cgit v1.2.3