summaryrefslogtreecommitdiff
path: root/main.c
diff options
context:
space:
mode:
authorJuan Manuel Tomas <jtomas1815@gmail.com>2022-01-16 20:05:17 -0300
committerJuan Manuel Tomas <jtomas1815@gmail.com>2022-01-16 20:05:17 -0300
commit53ab36dc7b21f6bb5ebed3130fb026b0779122fe (patch)
treec4da7abb5861a3394878f60a6deacf1c1337239e /main.c
parent09b065e85d7199f7b2082066fbf9ce66648592b7 (diff)
downloadcannons-53ab36dc7b21f6bb5ebed3130fb026b0779122fe.tar.gz
cannons-53ab36dc7b21f6bb5ebed3130fb026b0779122fe.zip
Fix inconsistencies with terrain and player
Diffstat (limited to 'main.c')
-rw-r--r--main.c87
1 files changed, 40 insertions, 47 deletions
diff --git a/main.c b/main.c
index da37ce4..ebca8cf 100644
--- a/main.c
+++ b/main.c
@@ -1,3 +1,4 @@
+#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <SDL2/SDL.h>
@@ -5,13 +6,15 @@
#define WINDOW_W 1280
#define WINDOW_H 720
-void renderMaskedTexture(SDL_Renderer *context, SDL_Texture *result, SDL_Texture *mask, SDL_Texture *diffuse) {
- SDL_SetRenderTarget(context, result);
- SDL_SetRenderDrawColor(context, 0, 0, 0, 0);
- SDL_RenderClear(context);
- SDL_RenderCopy(context, mask, 0, 0);
- SDL_RenderCopy(context, diffuse, 0, 0);
- SDL_SetRenderTarget(context, 0);
+void editMask(float *mask, float x, float y, float color, float size) {
+ for (int j = y - size / 2; j < y + size / 2; j++) {
+ for (int i = x - size / 2; i < x + size / 2; i++) {
+ if (i > 0 && i < WINDOW_W && j > 0 && j < WINDOW_H) {
+ size_t index = i + j * WINDOW_W;
+ mask[index] = color;
+ }
+ }
+ }
}
SDL_Texture *createTiledTexture(SDL_Renderer *context, const char *filename) {
@@ -61,24 +64,18 @@ int main() {
SDL_SetTextureBlendMode(terrain, SDL_BLENDMODE_BLEND);
// Terrain generation
+ float *mask = calloc(WINDOW_W * WINDOW_H, sizeof(float));
+ for (int i = 500 * WINDOW_W; i < WINDOW_W * WINDOW_H; i++) {
+ mask[i] = 1.0;
+ }
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;
- }
- for (int i = mask_surface->w * 500; i < mask_surface->w * mask_surface->h; i++) {
- ((Uint32 *)mask_surface->pixels)[i] = 0xffffffff;
- }
- 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_y = 500;
float player_vy = 0;
- float player_ay = 1;
+ float player_ay = -9.8;
// Timer variables
float old_t = 0.0;
@@ -94,34 +91,11 @@ int main() {
}
int mouse_x, mouse_y;
Uint32 mouse_state = SDL_GetMouseState(&mouse_x, &mouse_y);
+ float size = 100;
if (mouse_state & SDL_BUTTON_LMASK) {
- SDL_LockSurface(mask_surface);
- unsigned size = 100;
- for (int j = mouse_y - size / 2; j < mouse_y + size / 2; j++) {
- for (int i = mouse_x - size / 2; i < mouse_x + size / 2; i++) {
- size_t index = i + j * WINDOW_W;
- ((Uint32 *)mask_surface->pixels)[index] = 0xffffffff;
- }
- }
- SDL_UnlockSurface(mask_surface);
- SDL_DestroyTexture(mask_texture);
- mask_texture = SDL_CreateTextureFromSurface(context, mask_surface);
-
- renderMaskedTexture(context, terrain, mask_texture, foreground);
+ editMask(mask, mouse_x, mouse_y, 1.0, size);
} else if (mouse_state & SDL_BUTTON_RMASK) {
- SDL_LockSurface(mask_surface);
- unsigned size = 100;
- for (int j = mouse_y - size / 2; j < mouse_y + size / 2; j++) {
- for (int i = mouse_x - size / 2; i < mouse_x + size / 2; i++) {
- size_t index = i + j * WINDOW_W;
- ((Uint32 *)mask_surface->pixels)[index] = 0x00000000;
- }
- }
- SDL_UnlockSurface(mask_surface);
- SDL_DestroyTexture(mask_texture);
- mask_texture = SDL_CreateTextureFromSurface(context, mask_surface);
-
- renderMaskedTexture(context, terrain, mask_texture, foreground);
+ editMask(mask, mouse_x, mouse_y, 0.0, size);
}
// Main loop
@@ -129,7 +103,21 @@ int main() {
SDL_RenderClear(context);
SDL_RenderCopy(context, background, 0, 0);
+
+ SDL_LockSurface(mask_surface);
+ for (int i = 0; i < mask_surface->w * mask_surface->h; i++) {
+ ((Uint32 *)mask_surface->pixels)[i] = mask[i] ? 0xffffffff : 0x00000000;
+ }
+ SDL_UnlockSurface(mask_surface);
+ SDL_Texture *mask_texture = SDL_CreateTextureFromSurface(context, mask_surface);
+ 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_DestroyTexture(mask_texture);
// Time delta calculation
double new_t = (double) SDL_GetTicks64() / 1000.0;
@@ -137,8 +125,13 @@ int main() {
old_t = new_t;
// Player logic
- player_y += player_vy * dt;
- player_vy += player_ay * dt;
+ if (mask[(int)player_x + (WINDOW_H - (int)player_y) * WINDOW_W] == 0.0) {
+ player_y += player_vy * dt;
+ player_vy += player_ay * dt;
+ } else {
+ player_y += 1;
+ player_vy = 0;
+ }
// Draw player
SDL_SetRenderDrawColor(context, 255, 0, 0, 255);