~yerinalexey/public-inbox

This thread contains a patchset. You're looking at the original emails, but you may wish to use the patch review UI. Review patch
1

[PATCH sdl2-breakout] Add keybinding to pause program

Details
Message ID
<20220105215032.4085191-1-mail@smlavine.com>
DKIM signature
pass
Download raw message
Patch: +26 -5
The SDL_Delay call was moved to the top of the loop so that it occurs
even while paused.
---
 include/state.h |  1 +
 src/main.c      | 30 +++++++++++++++++++++++++-----
 2 files changed, 26 insertions(+), 5 deletions(-)

diff --git a/include/state.h b/include/state.h
index 6d142dd..4f9f5ec 100644
--- a/include/state.h
+++ b/include/state.h
@@ -13,6 +13,7 @@ enum block_state {

struct state {
	bool running;
	bool paused;
	SDL_Renderer *renderer;

	int hearts;
diff --git a/src/main.c b/src/main.c
index 8340ef0..81cea48 100644
--- a/src/main.c
+++ b/src/main.c
@@ -26,6 +26,7 @@ main()

	struct state st = {
		.running = true,
		.paused = false,
		.renderer = SDL_CreateRenderer(win, -1, 0),
		.hearts = 5,
		.score = 0,
@@ -56,23 +57,43 @@ main()

	while (st.running) {
		SDL_Event event;
		SDL_Delay(1000 / 60);
		while (SDL_PollEvent(&event)) {
			/* Exiting and pausing/unpausing need to occur whether
			 * or not the game is paused.
			 */
			switch (event.type) {
			case SDL_MOUSEMOTION:
				paddle_move(&st.paddle, event.motion.x);
				break;
			case SDL_KEYUP:
				switch (event.key.keysym.sym) {
				case SDLK_q:
				case SDLK_ESCAPE:
					st.running = false;
					break;
				case SDLK_ESCAPE:
					st.paused = !st.paused;
					break;
				}
				break;
			case SDL_QUIT:
				st.running = false;
				break;
			}

			if (st.paused || !st.running) {
				break;
			}

			switch (event.type) {
			case SDL_MOUSEMOTION:
				paddle_move(&st.paddle, event.motion.x);
				break;
			}
		}

		if (st.paused) {
			continue;
		}
		if (!st.running) {
			break;
		}

		ball_update(&st);
@@ -119,7 +140,6 @@ main()
		}

		SDL_RenderPresent(st.renderer);
		SDL_Delay(1000 / 60);
	}

	for (size_t row = 0; row < 10; row++) {
-- 
2.34.1
Details
Message ID
<CGYHAHIX1S2B.3KN7MEMDBE16Z@desktop>
In-Reply-To
<20220105215032.4085191-1-mail@smlavine.com> (view parent)
DKIM signature
pass
Download raw message
Thanks, applied!

I also added a little visual effect when the game is paused.

To git.sr.ht:~yerinalexey/sdl2-breakout
   029cc14..107d442  master -> master
Reply to thread Export thread (mbox)