summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJuan Manuel Tomas <jtomas1815@gmail.com>2022-01-25 20:00:31 -0300
committerJuan Manuel Tomas <jtomas1815@gmail.com>2022-01-25 20:00:31 -0300
commite8f7ca58aa7b0554def69ee0a1700e6cb3af1809 (patch)
treec079871e7496ff2c0f45136040ba1852bed00f95
parentbcc20de802c40506205ec0a7388262e94cf07668 (diff)
downloadcannons-e8f7ca58aa7b0554def69ee0a1700e6cb3af1809.tar.gz
cannons-e8f7ca58aa7b0554def69ee0a1700e6cb3af1809.zip
Add basic projectile
-rw-r--r--main.c39
1 files changed, 37 insertions, 2 deletions
diff --git a/main.c b/main.c
index d08090f..8a4fd34 100644
--- a/main.c
+++ b/main.c
@@ -75,6 +75,13 @@ void GameMain(SDL_Renderer *context) {
float player_vy = 0;
float player_ay = -9.8;
+ 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;
+
float old_t = 0.0;
int exit = 0;
@@ -87,15 +94,19 @@ 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;
}
}
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, WINDOW_H - mouse_y, 1.0, size);
+ PaintMask(mask, WINDOW_W, WINDOW_H, mouse_x, mouse_y, 1.0, size);
} else if (mouse_state & SDL_BUTTON_RMASK) {
- PaintMask(mask, WINDOW_W, WINDOW_H, mouse_x, WINDOW_H - mouse_y, 0.0, size);
+ PaintMask(mask, WINDOW_W, WINDOW_H, mouse_x, mouse_y, 0.0, size);
}
const Uint8 *keys = SDL_GetKeyboardState(0);
@@ -125,6 +136,20 @@ void GameMain(SDL_Renderer *context) {
}
}
+ 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;
+ } else {
+ projectile_alive = 0;
+ projectile_vy = 100;
+ PaintMask(mask, WINDOW_W, WINDOW_H, projectile_x, projectile_y, 0.0, 40);
+ }
+ }
+
void *pixels;
int pitch;
SDL_LockTexture(mask_texture, 0, &pixels, &pitch);
@@ -138,7 +163,9 @@ void GameMain(SDL_Renderer *context) {
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);
@@ -146,9 +173,17 @@ void GameMain(SDL_Renderer *context) {
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);
+
+ 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_RenderFillRect(context, &projectile_rect);
+ }
+
SDL_RenderPresent(context);
SDL_Delay(1);
}