diff options
| author | Juan Manuel Tomas <jtomas1815@gmail.com> | 2022-01-26 02:03:53 -0300 | 
|---|---|---|
| committer | Juan Manuel Tomas <jtomas1815@gmail.com> | 2022-01-26 02:03:53 -0300 | 
| commit | 7408ca5a4e7765a006533c3fbc13510058366639 (patch) | |
| tree | 884af8412a324504a63b00aa9d0e6741117915a6 | |
| parent | a8ff0d19d27697c931315da9d59d623dd71ceae7 (diff) | |
| download | cannons-7408ca5a4e7765a006533c3fbc13510058366639.tar.gz cannons-7408ca5a4e7765a006533c3fbc13510058366639.zip | |
Fix simulation time
| -rw-r--r-- | main.c | 116 | 
1 files changed, 59 insertions, 57 deletions
| @@ -125,77 +125,79 @@ void GameMain(SDL_Renderer *context) {  				projectile.vx = cos(player.angle) * player.power;  			}  		} -		int simulation_frames = 10; -		while (simulation_frames > 0) { -			simulation_frames--; +  		double new_t = GetCurrentTimestamp();  		double dt = (new_t - old_t);  		old_t = new_t; -		int mouse_x, mouse_y; -		Uint32 mouse_state = SDL_GetMouseState(&mouse_x, &mouse_y); -		float size = 20; -		if (mouse_state & SDL_BUTTON_LMASK) { -			PaintTerrain(&terrain, mouse_x, WINDOW_H - mouse_y, 1.0, size); -		} else if (mouse_state & SDL_BUTTON_RMASK) { -			PaintTerrain(&terrain, mouse_x, WINDOW_H - mouse_y, 0.0, size); -		} +		int times_to_simulate = 10; +		while (times_to_simulate > 0) { +			times_to_simulate--; + +			int mouse_x, mouse_y; +			Uint32 mouse_state = SDL_GetMouseState(&mouse_x, &mouse_y); +			float size = 20; +			if (mouse_state & SDL_BUTTON_LMASK) { +				PaintTerrain(&terrain, mouse_x, WINDOW_H - mouse_y, 1.0, size); +			} else if (mouse_state & SDL_BUTTON_RMASK) { +				PaintTerrain(&terrain, mouse_x, WINDOW_H - mouse_y, 0.0, size); +			} -		const Uint8 *keys = SDL_GetKeyboardState(0); -		if (keys[SDL_SCANCODE_D] && player.x < WINDOW_W) { -			player.x += WALK_VEL * dt; -		} -		if (keys[SDL_SCANCODE_A] && player.x >= 0) { -			player.x -= WALK_VEL * dt; -		} -		if (keys[SDL_SCANCODE_W] && player.angle < PI / 2) { -			player.angle += PI / 180 * dt; -		} -		if (keys[SDL_SCANCODE_S] && player.angle > 0) { +			const Uint8 *keys = SDL_GetKeyboardState(0); +			if (keys[SDL_SCANCODE_D] && player.x < WINDOW_W) { +				player.x += WALK_VEL * dt; +			} +			if (keys[SDL_SCANCODE_A] && player.x >= 0) { +				player.x -= WALK_VEL * dt; +			} +			if (keys[SDL_SCANCODE_W] && player.angle < PI / 2) { +				player.angle += PI / 180 * dt; +			} +			if (keys[SDL_SCANCODE_S] && player.angle > 0) { -			player.angle -= PI / 180 * dt; -		} +				player.angle -= PI / 180 * dt; +			} -		if (player.y >= WINDOW_H) { -			player.y  = WINDOW_H - 1; -			player.vy = 0; -		} -		if (terrain.mask[(int)player.x + (int)player.y * WINDOW_W] == 0.0 && -				player.y >= 0) { -			player.y += player.vy * dt; -			player.vy += GRAVITY * dt; -			while (terrain.mask[(int)player.x + (int)player.y * WINDOW_W] != 0.0) { -				player.y += 1; +			if (player.y >= WINDOW_H) { +				player.y  = WINDOW_H - 1;  				player.vy = 0;  			} -		} +			if (terrain.mask[(int)player.x + (int)player.y * WINDOW_W] == 0.0 && +					player.y >= 0) { +				player.y += player.vy * dt; +				player.vy += GRAVITY * dt; +				while (terrain.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 && -					terrain.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; -				PaintTerrain(&terrain, projectile.x, projectile.y, 0.0, 40); +			if (projectile.alive) { +				if (projectile.y >= 0 && projectile.y < WINDOW_H && +						projectile.x >= 0 && projectile.x < WINDOW_W && +						terrain.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; +					PaintTerrain(&terrain, projectile.x, projectile.y, 0.0, 40); +				}  			} -		} -		if (terrain.updated) { -			void *pixels; -			int pitch; -			SDL_LockTexture(mask_texture, 0, &pixels, &pitch); -			Uint32 *p = (Uint32 *) pixels; -			for (int j = 0; j < WINDOW_H; j++) { -				for (int i = 0; i < WINDOW_W; i++) { -					p[i + j * WINDOW_W] = terrain.mask[i + (WINDOW_H - j) * WINDOW_W] ? 0xffffffff : 0x00000000; +			if (terrain.updated) { +				void *pixels; +				int pitch; +				SDL_LockTexture(mask_texture, 0, &pixels, &pitch); +				Uint32 *p = (Uint32 *) pixels; +				for (int j = 0; j < WINDOW_H; j++) { +					for (int i = 0; i < WINDOW_W; i++) { +						p[i + j * WINDOW_W] = terrain.mask[i + (WINDOW_H - j) * WINDOW_W] ? 0xffffffff : 0x00000000; +					}  				} +				SDL_UnlockTexture(mask_texture); +				terrain.updated = 0;  			} -			SDL_UnlockTexture(mask_texture); -			terrain.updated = 0; -		}  		}  		SDL_SetRenderDrawColor(context, 40, 40, 40, 255); | 
