summary refs log tree commit diff
path: root/src/main.c
diff options
context:
space:
mode:
authorzlago2024-10-06 19:54:24 +0200
committerzlago2024-10-06 21:23:44 +0200
commitd005a7756f6d15f67a7033a53bae23d00a58af69 (patch)
treedb6abbabdbfd1ca7d3c32756042623052c43a0ce /src/main.c
parenta887634f473b9e665c54f223e24080a1492c58fe (diff)
particles and projectiles
Diffstat (limited to 'src/main.c')
-rw-r--r--src/main.c66
1 files changed, 55 insertions, 11 deletions
diff --git a/src/main.c b/src/main.c
index c48deef..d5f6504 100644
--- a/src/main.c
+++ b/src/main.c
@@ -3,6 +3,7 @@
 
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
 
 #include "input.h"
 #include "loader.h"
@@ -22,11 +23,13 @@ SDL_Renderer *renderer = NULL;
 enum game_state game_state = STATE_PLAYING;
 char *game_next_level;
 
+static void *particle_tex = NULL;
+
 #define WINDOW_WIDTH 160
 #define WINDOW_HEIGHT 90
 #define INIT_SUBSYSTEMS SDL_INIT_VIDEO
 
-unsigned input_now = 0;
+unsigned input_pressed = 0, input_held = 0;
 SDL_Scancode keybinds[] = {
 	SDL_SCANCODE_UP,
 	SDL_SCANCODE_DOWN,
@@ -64,6 +67,8 @@ void entities_free(struct entities *entities) {
 int entities_load(struct entities *entities, char *data, size_t size, size_t input_bytes) {
 	entities->enemies = 0;
 	entities->warps = 0;
+	entities->projectiles = 0;
+	entities->particles = 0;
 	data += input_bytes;
 	size -= input_bytes;
 	while (size) {
@@ -138,6 +143,31 @@ int game_update(void) {
 		entities.warp[i].update(entities.warp + i);
 	}
 	
+	for (int i = 0, e = 0; i < 64 && e < entities.projectiles; i++) {
+		if (entities.projectile[i].state) {
+			e++;
+			if (entities.projectile[i].update(entities.projectile + i)) {
+				entities.projectiles--;
+				if (entities.projectile + i != entities.projectile + entities.projectiles) {
+					memcpy(entities.projectile + i, entities.projectile + entities.projectiles, sizeof (struct projectile));
+				}
+				continue;
+			}
+		}
+	}
+
+	for (int i = 0; i < entities.particles; i++) {
+		if (entities.particle[i].hp-- == 0) {
+			entities.particles--;
+			if (entities.particle + i != entities.particle + entities.particles) {
+				memcpy(entities.particle + i, entities.particle + entities.particles, sizeof (struct particle));
+			}
+			continue;
+		}
+		entities.particle[i].x += (entities.particle[i].velocity.x += entities.particle[i].acceleration.x);
+		entities.particle[i].y += (entities.particle[i].velocity.y += entities.particle[i].acceleration.y);
+	}
+	
 	return 0;
 }
 
@@ -151,8 +181,17 @@ void game_render(SDL_Texture *framebuffer, int x, int y) {
 			entities.enemy[i].draw(entities.enemy + i, x, y);
 		}
 	}
+	for (int i = 0, e = 0; i < 64 && e < entities.projectiles; i++) {
+		if (entities.projectile[i].state) {
+			e++;
+			entities.projectile[i].draw(entities.projectile + i, x, y);
+		}
+	}
+	for (int i = 0; i < entities.particles; i++) {
+		struct particle *self = entities.particle + i;
+		SDL_RenderCopy(renderer, particle_tex, &self->rect, &(SDL_Rect) {from_fixed(self->x) - x, from_fixed(self->y) - y, self->rect.w, self->rect.h});
+	}
 	tilemap_foreground(tilemap, x, y, WINDOW_WIDTH, WINDOW_HEIGHT);
-	//SDL_RenderCopy(renderer, res_get_texture("meow").data, &(SDL_Rect) {0, 0, 128, 90}, &(SDL_Rect) {0, 0, 128, 90});
 }
 
 void game_render_flush(SDL_Texture *framebuffer) {
@@ -174,8 +213,8 @@ int main(int const argc, char *const *const argv) {
 		{"border",     2, NULL, 'b'},
 		{NULL,         0, NULL,  0 },
 	};
-	int opt, li;
-	unsigned scale = 0, flags = 0, error = 0;
+	int opt, li, scale = 0;
+	unsigned flags = 0, error = 0;
 	while ((opt = getopt_long(argc, argv, "h", opts, &li)) != -1) {
 		switch (opt) {
 			case 'h':
@@ -199,9 +238,9 @@ int main(int const argc, char *const *const argv) {
 			
 			case 'f':
 				if (optarg == NULL || strcmp(optarg, "true") == 0) {
-					flags |= SDL_WINDOW_FULLSCREEN;
+					flags |= SDL_WINDOW_FULLSCREEN_DESKTOP;
 				} else if (strcmp(optarg, "false") == 0) {
-					flags &= ~SDL_WINDOW_FULLSCREEN;
+					flags &= ~SDL_WINDOW_FULLSCREEN_DESKTOP;
 				} else {
 					fprintf(stderr, "%s=%s -- expected 'true' or 'false'\n", opts[li].name, optarg);
 					error = 1;
@@ -243,7 +282,7 @@ int main(int const argc, char *const *const argv) {
 		return EXIT_FAILURE;
 	}
 	SDL_StopTextInput();
-	if (scale == 0) {
+	if (scale <= 0) {
 		SDL_DisplayMode dm;
 		if (SDL_GetDesktopDisplayMode(0, &dm) != 0) {
 			SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "couldnt get desktop size", SDL_GetError(), NULL);
@@ -299,6 +338,8 @@ int main(int const argc, char *const *const argv) {
 				optind++;
 			}
 		}
+		
+		particle_tex = res_get_texture("particles").data;
 
 		struct blob blob = res_get_map("untitled");
 		next_tilemap = tilemap_load(blob.data, blob.size);
@@ -339,6 +380,7 @@ int main(int const argc, char *const *const argv) {
 	player_property(next_entities.player, "y", "64");
 	memcpy(&entities, &next_entities, sizeof (entities));
 	while (1) {
+		input_pressed = input_held;
 		SDL_Event evt;
 		while (SDL_PollEvent(&evt)) {
 			switch (evt.type) {
@@ -355,16 +397,16 @@ int main(int const argc, char *const *const argv) {
 						goto end;
 					}
 					
-					//static_assert(INPUT_LENGTH <= sizeof(input_now) * CHAR_BIT); // if this trips up, scope creep happened
+					//static_assert(INPUT_LENGTH <= sizeof(input_held) * CHAR_BIT); // if this trips up, scope creep happened
 					for (unsigned key = 0, bit = 1; key < INPUT_LENGTH; key++, bit <<= 1) {
 						if (evt.key.keysym.scancode == keybinds[key]) {
 							if (evt.key.state == SDL_PRESSED)
-								input_now |= bit;
+								input_held |= bit;
 							else
-								input_now &= ~bit;
+								input_held &= ~bit;
 						}
 					}
-					//fprintf(stderr, "input: %0*b\n", INPUT_LENGTH, input_now);
+					//fprintf(stderr, "input: %0*b\n", INPUT_LENGTH, input_held);
 					break;
 				case SDL_MOUSEMOTION: // the cursor moves
 					//fprintf(stderr, "mouse at %4i %4i\n", evt.motion.x, evt.motion.y);
@@ -434,6 +476,8 @@ int main(int const argc, char *const *const argv) {
 					fprintf(stderr, "unknown event type %i\n", evt.type);
 			}
 		}
+		input_pressed = ~input_pressed & input_held;
+		//fprintf(stderr, "input: %0*b\n", INPUT_LENGTH, input_pressed);
 
 		switch (game_state) {
 			case STATE_PLAYING: