summaryrefslogtreecommitdiff
path: root/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'main.c')
-rw-r--r--main.c114
1 files changed, 66 insertions, 48 deletions
diff --git a/main.c b/main.c
index 8a4fd34..9bcd467 100644
--- a/main.c
+++ b/main.c
@@ -1,12 +1,31 @@
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
+#include <math.h>
#include <SDL2/SDL.h>
+#define GRAVITY -9.8
+#define PI 3.141592
#define WINDOW_W 1280
#define WINDOW_H 720
#define WALK_VEL 20
+typedef struct {
+ float x;
+ float y;
+ float vy;
+ float angle;
+ float power;
+} Cannon;
+
+typedef struct {
+ float x;
+ float y;
+ float vy;
+ float vx;
+ int alive;
+} Projectile;
+
static inline double GetCurrentTimestamp() {
return (double) SDL_GetTicks64() / 1000.0;
}
@@ -70,17 +89,12 @@ void GameMain(SDL_Renderer *context) {
mask[i] = 1.0;
}
- float player_x = 100;
- float player_y = 500;
- float player_vy = 0;
- float player_ay = -9.8;
+ Cannon player = {0};
+ player.x = 100;
+ player.y = 500;
+ player.power = 100;
- float projectile_x = 100;
- float projectile_y = 500;
- float projectile_vy = 100;
- float projectile_vx = 5;
- float projectile_ay = -9.8;
- int projectile_alive = 0;
+ Projectile projectile = {0};
float old_t = 0.0;
@@ -94,59 +108,61 @@ void GameMain(SDL_Renderer *context) {
while (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT) {
exit = 1;
- } else if (event.type == SDL_KEYDOWN && event.key.state == SDL_PRESSED && event.key.keysym.sym == SDLK_SPACE && !projectile_alive) {
- projectile_alive = 1;
- projectile_x = player_x;
- projectile_y = player_y + 10;
+ } else if (event.type == SDL_KEYDOWN && event.key.state == SDL_PRESSED && event.key.keysym.sym == SDLK_SPACE && !projectile.alive) {
+ projectile.alive = 1;
+ projectile.x = player.x;
+ projectile.y = player.y;
+ projectile.vy = sin(player.angle) * player.power;
+ projectile.vx = cos(player.angle) * player.power;
}
}
int mouse_x, mouse_y;
Uint32 mouse_state = SDL_GetMouseState(&mouse_x, &mouse_y);
float size = 20;
if (mouse_state & SDL_BUTTON_LMASK) {
- PaintMask(mask, WINDOW_W, WINDOW_H, mouse_x, mouse_y, 1.0, size);
+ PaintMask(mask, WINDOW_W, WINDOW_H, mouse_x, WINDOW_H - mouse_y, 1.0, size);
} else if (mouse_state & SDL_BUTTON_RMASK) {
- PaintMask(mask, WINDOW_W, WINDOW_H, mouse_x, mouse_y, 0.0, size);
+ PaintMask(mask, WINDOW_W, WINDOW_H, mouse_x, WINDOW_H - mouse_y, 0.0, size);
}
const Uint8 *keys = SDL_GetKeyboardState(0);
- if (keys[SDL_SCANCODE_D]) {
- player_x += WALK_VEL * dt;
+ if (keys[SDL_SCANCODE_D] && player.x < WINDOW_W) {
+ player.x += WALK_VEL * dt;
}
- if (keys[SDL_SCANCODE_A]) {
- player_x -= WALK_VEL * dt;
+ if (keys[SDL_SCANCODE_A] && player.x >= 0) {
+ player.x -= WALK_VEL * dt;
}
-
- if (player_x < 0) {
- player_x += WALK_VEL * dt;
- } else if (player_x >= WINDOW_W) {
- player_x -= WALK_VEL * dt;
+ if (keys[SDL_SCANCODE_W] && player.angle < PI / 2) {
+ player.angle += PI / 180 * dt;
}
- if (player_y >= WINDOW_H) {
- player_y = WINDOW_H - 1;
- player_vy = 0;
+ if (keys[SDL_SCANCODE_S] && player.angle > 0) {
+ player.angle -= PI / 180 * dt;
}
- if (mask[(int)player_x + (int)player_y * WINDOW_W] == 0.0 &&
- player_y >= 0) {
- player_y += player_vy * dt;
- player_vy += player_ay * dt;
- while (mask[(int)player_x + (int)player_y * WINDOW_W] != 0.0) {
- player_y += 1;
- player_vy = 0;
+
+ if (player.y >= WINDOW_H) {
+ player.y = WINDOW_H - 1;
+ player.vy = 0;
+ }
+ if (mask[(int)player.x + (int)player.y * WINDOW_W] == 0.0 &&
+ player.y >= 0) {
+ player.y += player.vy * dt;
+ player.vy += GRAVITY * dt;
+ while (mask[(int)player.x + (int)player.y * WINDOW_W] != 0.0) {
+ player.y += 1;
+ player.vy = 0;
}
}
- if (projectile_alive) {
- if (projectile_y >= 0 && projectile_y < WINDOW_H &&
- projectile_x >= 0 && projectile_x < WINDOW_W &&
- mask[(int)projectile_x + (int)projectile_y * WINDOW_W] == 0.0) {
- projectile_x += projectile_vx * dt;
- projectile_y += projectile_vy * dt;
- projectile_vy += projectile_ay * dt;
+ if (projectile.alive) {
+ if (projectile.y >= 0 && projectile.y < WINDOW_H &&
+ projectile.x >= 0 && projectile.x < WINDOW_W &&
+ mask[(int)projectile.x + (int)projectile.y * WINDOW_W] == 0.0) {
+ projectile.x += projectile.vx * dt;
+ projectile.y += projectile.vy * dt;
+ projectile.vy += GRAVITY * dt;
} else {
- projectile_alive = 0;
- projectile_vy = 100;
- PaintMask(mask, WINDOW_W, WINDOW_H, projectile_x, projectile_y, 0.0, 40);
+ projectile.alive = 0;
+ PaintMask(mask, WINDOW_W, WINDOW_H, projectile.x, projectile.y, 0.0, 40);
}
}
@@ -175,12 +191,14 @@ void GameMain(SDL_Renderer *context) {
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_Rect player_rect = {player.x - 5, WINDOW_H - player.y - 10, 10, 10 };
SDL_RenderFillRect(context, &player_rect);
- if (projectile_alive) {
+ SDL_RenderDrawLine(context, player.x, WINDOW_H - player.y, player.x + cos(player.angle) * 100, WINDOW_H - player.y - sin(player.angle) * 100);
+
+ if (projectile.alive) {
SDL_SetRenderDrawColor(context, 0, 255, 0, 255);
- SDL_Rect projectile_rect = {projectile_x - 5, WINDOW_H - projectile_y - 10, 10, 10 };
+ SDL_Rect projectile_rect = {projectile.x - 5, WINDOW_H - projectile.y - 10, 10, 10 };
SDL_RenderFillRect(context, &projectile_rect);
}