From ee1121ce969248d701fea7ffe79bc6f686610ce7 Mon Sep 17 00:00:00 2001 From: Fabio Maciel Date: Sat, 30 May 2020 22:05:06 +0200 Subject: Create simple red rect controlled by arrow keys Signed-off-by: Fabio Maciel Co-authored-by: Johnny Richard --- src/main.c | 106 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 src/main.c (limited to 'src') diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..022a888 --- /dev/null +++ b/src/main.c @@ -0,0 +1,106 @@ +#include +#include +#include +#include + +const int SCREEN_WIDTH = 640; +const int SCREEN_HEIGHT = 480; + +typedef struct Controller { + bool up; + bool down; + bool left; + bool right; +} Controller; + +int +main (int argc, + char *args[]) +{ + SDL_Window* window = NULL; + SDL_Surface* screenSurface = NULL; + + if (SDL_Init(SDL_INIT_VIDEO) < 0) { + fprintf(stderr, "SDL could not initialize! SDL_Error: %s\n", SDL_GetError()); + return EXIT_FAILURE; + } + + window = SDL_CreateWindow( + "SDL Tutorial", + SDL_WINDOWPOS_UNDEFINED, + SDL_WINDOWPOS_UNDEFINED, + SCREEN_WIDTH, SCREEN_HEIGHT, + SDL_WINDOW_SHOWN + ); + + if (window == NULL) { + fprintf(stderr, "Window could not be created! SDL_Error: %s\n", SDL_GetError()); + return EXIT_FAILURE; + } + + SDL_Rect rect = { .x = 0, .y = 0, .w = 20, .h = 20 }; + + bool quit = false; + Controller controller = {0}; + + while (!quit) { + screenSurface = SDL_GetWindowSurface(window); + SDL_FillRect(screenSurface, NULL, SDL_MapRGB(screenSurface->format, 0xFF, 0xFF, 0xFF)); + + SDL_Event event; + while (SDL_PollEvent(&event) != 0) { + if (event.type == SDL_QUIT) { + quit = true; + } + + if (event.type == SDL_KEYDOWN) { + switch (event.key.keysym.sym) { + case SDLK_UP: + controller.up = true; + break; + case SDLK_DOWN: + controller.down = true; + break; + case SDLK_LEFT: + controller.left = true; + break; + case SDLK_RIGHT: + controller.right = true; + break; + } + } + + if (event.type == SDL_KEYUP) { + switch (event.key.keysym.sym) { + case SDLK_UP: + controller.up = false; + break; + + case SDLK_DOWN: + controller.down = false; + break; + + case SDLK_LEFT: + controller.left = false; + break; + + case SDLK_RIGHT: + controller.right = false; + break; + } + } + } + + if (controller.up) rect.y -= 1; + if (controller.down) rect.y += 1; + if (controller.left) rect.x -= 1; + if (controller.right) rect.x += 1; + + SDL_FillRect(screenSurface, &rect, SDL_MapRGB(screenSurface->format, 0xFF, 0x0, 0x0)); + SDL_UpdateWindowSurface(window); + } + + SDL_DestroyWindow(window); + SDL_Quit(); + return EXIT_SUCCESS; +} -- cgit v1.2.3 From 9a285cc3bdb8d5870fea5f07235a3e44786a0317 Mon Sep 17 00:00:00 2001 From: Johnny Richard Date: Tue, 13 Apr 2021 02:23:22 +0200 Subject: Add controller to its own header file Signed-off-by: Johnny Richard --- src/controller.h | 21 +++++++++++++++++++++ src/main.c | 11 +++-------- 2 files changed, 24 insertions(+), 8 deletions(-) create mode 100644 src/controller.h (limited to 'src') diff --git a/src/controller.h b/src/controller.h new file mode 100644 index 0000000..bf22335 --- /dev/null +++ b/src/controller.h @@ -0,0 +1,21 @@ +#ifndef CONTROLLER_H +#define CONTROLLER_H + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +typedef struct controller_t { + bool up; + bool down; + bool left; + bool right; +} controller_t; + +#ifdef __cplusplus +} +#endif + +#endif /* CONTROLLER_H */ diff --git a/src/main.c b/src/main.c index 022a888..4f685b2 100644 --- a/src/main.c +++ b/src/main.c @@ -3,16 +3,11 @@ #include #include +#include "controller.h" + const int SCREEN_WIDTH = 640; const int SCREEN_HEIGHT = 480; -typedef struct Controller { - bool up; - bool down; - bool left; - bool right; -} Controller; - int main (int argc, char *args[]) @@ -41,7 +36,7 @@ main (int argc, SDL_Rect rect = { .x = 0, .y = 0, .w = 20, .h = 20 }; bool quit = false; - Controller controller = {0}; + controller_t controller = {0}; while (!quit) { screenSurface = SDL_GetWindowSurface(window); -- cgit v1.2.3 From d8336885a4c053544fe3491335da0f76afffdfb9 Mon Sep 17 00:00:00 2001 From: Johnny Richard Date: Tue, 13 Apr 2021 02:53:13 +0200 Subject: Move controller update to a new update function Signed-off-by: Johnny Richard --- src/controller.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ src/controller.h | 4 ++++ src/main.c | 38 +------------------------------------- 3 files changed, 51 insertions(+), 37 deletions(-) create mode 100644 src/controller.c (limited to 'src') diff --git a/src/controller.c b/src/controller.c new file mode 100644 index 0000000..5fb1e0b --- /dev/null +++ b/src/controller.c @@ -0,0 +1,46 @@ +#include +#include "controller.h" + + +void +controller_update(controller_t *self, + SDL_Event *event) +{ + if (event->type == SDL_KEYDOWN) { + switch (event->key.keysym.sym) { + case SDLK_UP: + self->up = true; + break; + case SDLK_DOWN: + self->down = true; + break; + case SDLK_LEFT: + self->left = true; + break; + case SDLK_RIGHT: + self->right = true; + break; + } + } + + if (event->type == SDL_KEYUP) { + switch (event->key.keysym.sym) { + case SDLK_UP: + self->up = false; + break; + + case SDLK_DOWN: + self->down = false; + break; + + case SDLK_LEFT: + self->left = false; + break; + + case SDLK_RIGHT: + self->right = false; + break; + } + } +} + diff --git a/src/controller.h b/src/controller.h index bf22335..a97fc7a 100644 --- a/src/controller.h +++ b/src/controller.h @@ -1,6 +1,7 @@ #ifndef CONTROLLER_H #define CONTROLLER_H +#include #include #ifdef __cplusplus @@ -14,6 +15,9 @@ typedef struct controller_t { bool right; } controller_t; +void controller_update(controller_t *self, + SDL_Event *event); + #ifdef __cplusplus } #endif diff --git a/src/main.c b/src/main.c index 4f685b2..19c4acf 100644 --- a/src/main.c +++ b/src/main.c @@ -47,43 +47,7 @@ main (int argc, if (event.type == SDL_QUIT) { quit = true; } - - if (event.type == SDL_KEYDOWN) { - switch (event.key.keysym.sym) { - case SDLK_UP: - controller.up = true; - break; - case SDLK_DOWN: - controller.down = true; - break; - case SDLK_LEFT: - controller.left = true; - break; - case SDLK_RIGHT: - controller.right = true; - break; - } - } - - if (event.type == SDL_KEYUP) { - switch (event.key.keysym.sym) { - case SDLK_UP: - controller.up = false; - break; - - case SDLK_DOWN: - controller.down = false; - break; - - case SDLK_LEFT: - controller.left = false; - break; - - case SDLK_RIGHT: - controller.right = false; - break; - } - } + controller_update(&controller, &event); } if (controller.up) rect.y -= 1; -- cgit v1.2.3 From 6c1f0750e637f3a67aada3846bf32f815809205c Mon Sep 17 00:00:00 2001 From: Johnny Richard Date: Thu, 28 Oct 2021 02:59:05 +0200 Subject: docs: Add BSD 3-Clause License Signed-off-by: Johnny Richard --- src/controller.c | 34 ++++++++++++++++++++++++++++++++-- src/controller.h | 30 ++++++++++++++++++++++++++++++ src/main.c | 44 +++++++++++++++++++++++++++++++++++++------- 3 files changed, 99 insertions(+), 9 deletions(-) (limited to 'src') diff --git a/src/controller.c b/src/controller.c index 5fb1e0b..09757c4 100644 --- a/src/controller.c +++ b/src/controller.c @@ -1,9 +1,39 @@ +/* + * Copyright (c) 2021, Johnny Richard + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + #include #include "controller.h" -void -controller_update(controller_t *self, +void +controller_update(controller_t *self, SDL_Event *event) { if (event->type == SDL_KEYDOWN) { diff --git a/src/controller.h b/src/controller.h index a97fc7a..736b9e6 100644 --- a/src/controller.h +++ b/src/controller.h @@ -1,3 +1,33 @@ +/* + * Copyright (c) 2021, Johnny Richard + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + #ifndef CONTROLLER_H #define CONTROLLER_H diff --git a/src/main.c b/src/main.c index 19c4acf..1e45e4c 100644 --- a/src/main.c +++ b/src/main.c @@ -1,3 +1,33 @@ +/* + * Copyright (c) 2021, Johnny Richard + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + #include #include #include @@ -8,7 +38,7 @@ const int SCREEN_WIDTH = 640; const int SCREEN_HEIGHT = 480; -int +int main (int argc, char *args[]) { @@ -21,10 +51,10 @@ main (int argc, } window = SDL_CreateWindow( - "SDL Tutorial", - SDL_WINDOWPOS_UNDEFINED, - SDL_WINDOWPOS_UNDEFINED, - SCREEN_WIDTH, SCREEN_HEIGHT, + "SDL Tutorial", + SDL_WINDOWPOS_UNDEFINED, + SDL_WINDOWPOS_UNDEFINED, + SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN ); @@ -32,7 +62,7 @@ main (int argc, fprintf(stderr, "Window could not be created! SDL_Error: %s\n", SDL_GetError()); return EXIT_FAILURE; } - + SDL_Rect rect = { .x = 0, .y = 0, .w = 20, .h = 20 }; bool quit = false; @@ -43,7 +73,7 @@ main (int argc, SDL_FillRect(screenSurface, NULL, SDL_MapRGB(screenSurface->format, 0xFF, 0xFF, 0xFF)); SDL_Event event; - while (SDL_PollEvent(&event) != 0) { + while (SDL_PollEvent(&event) != 0) { if (event.type == SDL_QUIT) { quit = true; } -- cgit v1.2.3 From f584871064596b52efe12c2f8258f0839f6eb0fa Mon Sep 17 00:00:00 2001 From: Johnny Richard Date: Fri, 5 Nov 2021 20:52:06 +0100 Subject: polish: Reformat main.c to not exceed 80 columns Signed-off-by: Johnny Richard --- src/main.c | 81 +++++++++++++++++++++++++++++++++++++------------------------- 1 file changed, 49 insertions(+), 32 deletions(-) (limited to 'src') diff --git a/src/main.c b/src/main.c index 1e45e4c..2c71864 100644 --- a/src/main.c +++ b/src/main.c @@ -1,32 +1,32 @@ /* - * Copyright (c) 2021, Johnny Richard - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, this - * list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * - * 3. Neither the name of the copyright holder nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ +* Copyright (c) 2021, Johnny Richard +* All rights reserved. +* +* Redistribution and use in source and binary forms, with or without +* modification, are permitted provided that the following conditions are met: +* +* 1. Redistributions of source code must retain the above copyright notice, this +* list of conditions and the following disclaimer. +* +* 2. Redistributions in binary form must reproduce the above copyright notice, +* this list of conditions and the following disclaimer in the documentation +* and/or other materials provided with the distribution. +* +* 3. Neither the name of the copyright holder nor the names of its +* contributors may be used to endorse or promote products derived from +* this software without specific prior written permission. +* +* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE +* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ #include #include @@ -46,7 +46,11 @@ main (int argc, SDL_Surface* screenSurface = NULL; if (SDL_Init(SDL_INIT_VIDEO) < 0) { - fprintf(stderr, "SDL could not initialize! SDL_Error: %s\n", SDL_GetError()); + fprintf( + stderr, + "SDL could not initialize! SDL_Error: %s\n", + SDL_GetError() + ); return EXIT_FAILURE; } @@ -59,7 +63,11 @@ main (int argc, ); if (window == NULL) { - fprintf(stderr, "Window could not be created! SDL_Error: %s\n", SDL_GetError()); + fprintf( + stderr, + "Window could not be created! SDL_Error: %s\n", + SDL_GetError() + ); return EXIT_FAILURE; } @@ -70,7 +78,11 @@ main (int argc, while (!quit) { screenSurface = SDL_GetWindowSurface(window); - SDL_FillRect(screenSurface, NULL, SDL_MapRGB(screenSurface->format, 0xFF, 0xFF, 0xFF)); + SDL_FillRect( + screenSurface, + NULL, + SDL_MapRGB(screenSurface->format, 0xFF, 0xFF, 0xFF) + ); SDL_Event event; while (SDL_PollEvent(&event) != 0) { @@ -85,7 +97,12 @@ main (int argc, if (controller.left) rect.x -= 1; if (controller.right) rect.x += 1; - SDL_FillRect(screenSurface, &rect, SDL_MapRGB(screenSurface->format, 0xFF, 0x0, 0x0)); + SDL_FillRect( + screenSurface, + &rect, + SDL_MapRGB(screenSurface->format, 0xFF, 0x0, 0x0) + ); + SDL_UpdateWindowSurface(window); } -- cgit v1.2.3 From e415b62a2e85f44e6ff285e08b16525605d2fc0f Mon Sep 17 00:00:00 2001 From: Johnny Richard Date: Sun, 14 Nov 2021 01:27:15 +0100 Subject: controller_t: Reduce memory usage using bitwise --- src/controller.c | 72 ++++++++++++++++++++++++++++++++++++-------------------- src/controller.h | 22 +++++++++-------- src/main.c | 12 +++++----- 3 files changed, 64 insertions(+), 42 deletions(-) (limited to 'src') diff --git a/src/controller.c b/src/controller.c index 09757c4..4e488e9 100644 --- a/src/controller.c +++ b/src/controller.c @@ -38,39 +38,59 @@ controller_update(controller_t *self, { if (event->type == SDL_KEYDOWN) { switch (event->key.keysym.sym) { - case SDLK_UP: - self->up = true; - break; - case SDLK_DOWN: - self->down = true; - break; - case SDLK_LEFT: - self->left = true; - break; - case SDLK_RIGHT: - self->right = true; - break; + case SDLK_UP: + self->pressed_buttons |= BTN_UP; + break; + case SDLK_DOWN: + self->pressed_buttons |= BTN_DOWN; + break; + case SDLK_LEFT: + self->pressed_buttons |= BTN_LEFT; + break; + case SDLK_RIGHT: + self->pressed_buttons |= BTN_RIGHT; + break; } } if (event->type == SDL_KEYUP) { switch (event->key.keysym.sym) { - case SDLK_UP: - self->up = false; - break; + case SDLK_UP: + self->pressed_buttons ^= BTN_UP; + break; + case SDLK_DOWN: + self->pressed_buttons ^= BTN_DOWN; + break; + case SDLK_LEFT: + self->pressed_buttons ^= BTN_LEFT; + break; + case SDLK_RIGHT: + self->pressed_buttons ^= BTN_RIGHT; + break; + } + } +} - case SDLK_DOWN: - self->down = false; - break; +bool +controller_is_up_pressed(controller_t *self) +{ + return self->pressed_buttons & BTN_UP; +} - case SDLK_LEFT: - self->left = false; - break; +bool +controller_is_down_pressed(controller_t *self) +{ + return self->pressed_buttons & BTN_DOWN; +} - case SDLK_RIGHT: - self->right = false; - break; - } - } +bool +controller_is_right_pressed(controller_t *self) +{ + return self->pressed_buttons & BTN_RIGHT; } +bool +controller_is_left_pressed(controller_t *self) +{ + return self->pressed_buttons & BTN_LEFT; +} diff --git a/src/controller.h b/src/controller.h index 736b9e6..ecc0389 100644 --- a/src/controller.h +++ b/src/controller.h @@ -33,23 +33,25 @@ #include #include +#include -#ifdef __cplusplus -extern "C" { -#endif +enum btn_t { + BTN_UP = 1 << 0, + BTN_DOWN = 1 << 1, + BTN_RIGHT = 1 << 2, + BTN_LEFT = 1 << 3 +}; typedef struct controller_t { - bool up; - bool down; - bool left; - bool right; + uint8_t pressed_buttons; } controller_t; void controller_update(controller_t *self, SDL_Event *event); -#ifdef __cplusplus -} -#endif +bool controller_is_up_pressed(controller_t *self); +bool controller_is_down_pressed(controller_t *self); +bool controller_is_right_pressed(controller_t *self); +bool controller_is_left_pressed(controller_t *self); #endif /* CONTROLLER_H */ diff --git a/src/main.c b/src/main.c index 2c71864..b8e723b 100644 --- a/src/main.c +++ b/src/main.c @@ -74,7 +74,7 @@ main (int argc, SDL_Rect rect = { .x = 0, .y = 0, .w = 20, .h = 20 }; bool quit = false; - controller_t controller = {0}; + controller_t ctrl = {0}; while (!quit) { screenSurface = SDL_GetWindowSurface(window); @@ -89,13 +89,13 @@ main (int argc, if (event.type == SDL_QUIT) { quit = true; } - controller_update(&controller, &event); + controller_update(&ctrl, &event); } - if (controller.up) rect.y -= 1; - if (controller.down) rect.y += 1; - if (controller.left) rect.x -= 1; - if (controller.right) rect.x += 1; + if (controller_is_up_pressed(&ctrl)) rect.y -= 1; + if (controller_is_down_pressed(&ctrl)) rect.y += 1; + if (controller_is_left_pressed(&ctrl)) rect.x -= 1; + if (controller_is_right_pressed(&ctrl)) rect.x += 1; SDL_FillRect( screenSurface, -- cgit v1.2.3 From 25b5544dcf7ee81915006fe940816368ef61c0de Mon Sep 17 00:00:00 2001 From: Johnny Richard Date: Sun, 14 Nov 2021 01:35:22 +0100 Subject: main.c: Set window title to Blast Attack --- src/main.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/main.c b/src/main.c index b8e723b..bd31bfb 100644 --- a/src/main.c +++ b/src/main.c @@ -55,7 +55,7 @@ main (int argc, } window = SDL_CreateWindow( - "SDL Tutorial", + "Blast Attack", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, -- cgit v1.2.3 From ad3b7b0047c3f1590cbd750c4f3952d3de5d9b5d Mon Sep 17 00:00:00 2001 From: Johnny Richard Date: Sun, 14 Nov 2021 01:36:25 +0100 Subject: main.c: Fix SDL initializaiton error message --- src/main.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/main.c b/src/main.c index bd31bfb..46b4dee 100644 --- a/src/main.c +++ b/src/main.c @@ -48,7 +48,7 @@ main (int argc, if (SDL_Init(SDL_INIT_VIDEO) < 0) { fprintf( stderr, - "SDL could not initialize! SDL_Error: %s\n", + "SDL could not be initialized! SDL_Error: %s\n", SDL_GetError() ); return EXIT_FAILURE; -- cgit v1.2.3 From 670d944f983a1063b6675b811a9d09910a1df2ec Mon Sep 17 00:00:00 2001 From: Johnny Richard Date: Sun, 14 Nov 2021 01:37:22 +0100 Subject: main.c: Rename surface variable --- src/main.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'src') diff --git a/src/main.c b/src/main.c index 46b4dee..38b948c 100644 --- a/src/main.c +++ b/src/main.c @@ -42,8 +42,8 @@ int main (int argc, char *args[]) { - SDL_Window* window = NULL; - SDL_Surface* screenSurface = NULL; + SDL_Window* window = NULL; + SDL_Surface* surface = NULL; if (SDL_Init(SDL_INIT_VIDEO) < 0) { fprintf( @@ -77,11 +77,11 @@ main (int argc, controller_t ctrl = {0}; while (!quit) { - screenSurface = SDL_GetWindowSurface(window); + surface = SDL_GetWindowSurface(window); SDL_FillRect( - screenSurface, + surface, NULL, - SDL_MapRGB(screenSurface->format, 0xFF, 0xFF, 0xFF) + SDL_MapRGB(surface->format, 0xFF, 0xFF, 0xFF) ); SDL_Event event; @@ -98,9 +98,9 @@ main (int argc, if (controller_is_right_pressed(&ctrl)) rect.x += 1; SDL_FillRect( - screenSurface, + surface, &rect, - SDL_MapRGB(screenSurface->format, 0xFF, 0x0, 0x0) + SDL_MapRGB(surface->format, 0xFF, 0x0, 0x0) ); SDL_UpdateWindowSurface(window); -- cgit v1.2.3 From 4c8eb3e8ce0616c377f8e31eeb658fb01807e4b5 Mon Sep 17 00:00:00 2001 From: Johnny Richard Date: Wed, 20 Apr 2022 01:59:30 +0200 Subject: main: Start using renderer and set 60 fps --- src/main.c | 41 ++++++++++++++++++++++++----------------- 1 file changed, 24 insertions(+), 17 deletions(-) (limited to 'src') diff --git a/src/main.c b/src/main.c index 38b948c..e1fc2e6 100644 --- a/src/main.c +++ b/src/main.c @@ -31,6 +31,7 @@ #include #include #include +#include #include #include "controller.h" @@ -43,7 +44,6 @@ main (int argc, char *args[]) { SDL_Window* window = NULL; - SDL_Surface* surface = NULL; if (SDL_Init(SDL_INIT_VIDEO) < 0) { fprintf( @@ -71,18 +71,23 @@ main (int argc, return EXIT_FAILURE; } + SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED); + + if (renderer == NULL) { + fprintf(stderr, "Could not create renderer: %s\n", SDL_GetError()); + return EXIT_FAILURE; + } + SDL_Rect rect = { .x = 0, .y = 0, .w = 20, .h = 20 }; bool quit = false; controller_t ctrl = {0}; while (!quit) { - surface = SDL_GetWindowSurface(window); - SDL_FillRect( - surface, - NULL, - SDL_MapRGB(surface->format, 0xFF, 0xFF, 0xFF) - ); + SDL_SetRenderDrawColor(renderer, 0, 0, 0, 1); + SDL_RenderClear(renderer); + + uint64_t start = SDL_GetPerformanceCounter(); SDL_Event event; while (SDL_PollEvent(&event) != 0) { @@ -92,20 +97,22 @@ main (int argc, controller_update(&ctrl, &event); } - if (controller_is_up_pressed(&ctrl)) rect.y -= 1; - if (controller_is_down_pressed(&ctrl)) rect.y += 1; - if (controller_is_left_pressed(&ctrl)) rect.x -= 1; - if (controller_is_right_pressed(&ctrl)) rect.x += 1; + if (controller_is_up_pressed(&ctrl)) rect.y -= 8; + if (controller_is_down_pressed(&ctrl)) rect.y += 8; + if (controller_is_left_pressed(&ctrl)) rect.x -= 8; + if (controller_is_right_pressed(&ctrl)) rect.x += 8; - SDL_FillRect( - surface, - &rect, - SDL_MapRGB(surface->format, 0xFF, 0x0, 0x0) - ); + SDL_SetRenderDrawColor(renderer, 0xFF, 0, 0, 1); + SDL_RenderFillRect(renderer, &rect); + SDL_RenderPresent(renderer); + + uint64_t end = SDL_GetPerformanceCounter(); - SDL_UpdateWindowSurface(window); + float elapsedMS = (end - start) / (float)SDL_GetPerformanceFrequency() * 1000.0f; + SDL_Delay(floor(16.666f - elapsedMS)); } + SDL_DestroyRenderer(renderer); SDL_DestroyWindow(window); SDL_Quit(); return EXIT_SUCCESS; -- cgit v1.2.3 From e1df1f684e3accc6b54b16c23c4f977e7fc11ab6 Mon Sep 17 00:00:00 2001 From: Johnny Richard Date: Wed, 20 Apr 2022 02:11:27 +0200 Subject: main: Define SDL_RenderSetLogicalSize --- src/main.c | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) (limited to 'src') diff --git a/src/main.c b/src/main.c index e1fc2e6..8b14bfa 100644 --- a/src/main.c +++ b/src/main.c @@ -43,7 +43,6 @@ int main (int argc, char *args[]) { - SDL_Window* window = NULL; if (SDL_Init(SDL_INIT_VIDEO) < 0) { fprintf( @@ -54,31 +53,31 @@ main (int argc, return EXIT_FAILURE; } - window = SDL_CreateWindow( + SDL_Window* window = SDL_CreateWindow( "Blast Attack", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, - SDL_WINDOW_SHOWN + SDL_WINDOW_RESIZABLE ); - if (window == NULL) { - fprintf( - stderr, - "Window could not be created! SDL_Error: %s\n", - SDL_GetError() - ); + fprintf(stderr, "Window could not be created! SDL_Error: %s\n", SDL_GetError()); return EXIT_FAILURE; } SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED); - if (renderer == NULL) { fprintf(stderr, "Could not create renderer: %s\n", SDL_GetError()); return EXIT_FAILURE; } + if (SDL_RenderSetLogicalSize(renderer, SCREEN_WIDTH, SCREEN_HEIGHT) < 0) { + fprintf(stderr, "Could not set logical size: %s\n", SDL_GetError()); + return EXIT_FAILURE; + } + SDL_Rect rect = { .x = 0, .y = 0, .w = 20, .h = 20 }; + SDL_Rect bg_rect = { .x = 0, .y = 0, .w = SCREEN_WIDTH, .h = SCREEN_HEIGHT }; bool quit = false; controller_t ctrl = {0}; @@ -89,6 +88,9 @@ main (int argc, uint64_t start = SDL_GetPerformanceCounter(); + SDL_SetRenderDrawColor(renderer, 0XCC, 0XCC, 0XCC, 1); + SDL_RenderFillRect(renderer, &bg_rect); + SDL_Event event; while (SDL_PollEvent(&event) != 0) { if (event.type == SDL_QUIT) { -- cgit v1.2.3 From 2f537114acc74a9e09443f69ab2508b292fdfa91 Mon Sep 17 00:00:00 2001 From: Johnny Richard Date: Wed, 20 Apr 2022 02:56:09 +0200 Subject: Add screen limit collision --- src/main.c | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/main.c b/src/main.c index 8b14bfa..70b283d 100644 --- a/src/main.c +++ b/src/main.c @@ -99,10 +99,25 @@ main (int argc, controller_update(&ctrl, &event); } - if (controller_is_up_pressed(&ctrl)) rect.y -= 8; - if (controller_is_down_pressed(&ctrl)) rect.y += 8; - if (controller_is_left_pressed(&ctrl)) rect.x -= 8; - if (controller_is_right_pressed(&ctrl)) rect.x += 8; + if (controller_is_up_pressed(&ctrl) && rect.y > 0) { + rect.y -= 8; + rect.y = rect.y < 0 ? 0 : rect.y; + } + + if (controller_is_down_pressed(&ctrl) && rect.y + 8 < SCREEN_HEIGHT) { + rect.y += 8; + rect.y = rect.y + rect.h > SCREEN_HEIGHT ? SCREEN_HEIGHT - rect.h : rect.y; + } + + if (controller_is_left_pressed(&ctrl) && rect.x > 0) { + rect.x -= 8; + rect.x = rect.x < 0 ? 0 : rect.x; + } + + if (controller_is_right_pressed(&ctrl) && rect.x + 8 < SCREEN_WIDTH) { + rect.x += 8; + rect.x = rect.x + rect.h > SCREEN_WIDTH ? SCREEN_WIDTH - rect.h : rect.x; + } SDL_SetRenderDrawColor(renderer, 0xFF, 0, 0, 1); SDL_RenderFillRect(renderer, &rect); -- cgit v1.2.3 From 9dadbc4409e2ce32eee80c7a8a6ee93e2524763b Mon Sep 17 00:00:00 2001 From: Johnny Richard Date: Wed, 20 Apr 2022 03:34:03 +0200 Subject: player: Introduce player object --- src/main.c | 28 ++++--------------- src/player.c | 91 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/player.h | 55 ++++++++++++++++++++++++++++++++++++ 3 files changed, 152 insertions(+), 22 deletions(-) create mode 100644 src/player.c create mode 100644 src/player.h (limited to 'src') diff --git a/src/main.c b/src/main.c index 70b283d..f9b89bf 100644 --- a/src/main.c +++ b/src/main.c @@ -35,6 +35,7 @@ #include #include "controller.h" +#include "player.h" const int SCREEN_WIDTH = 640; const int SCREEN_HEIGHT = 480; @@ -76,12 +77,14 @@ main (int argc, return EXIT_FAILURE; } - SDL_Rect rect = { .x = 0, .y = 0, .w = 20, .h = 20 }; SDL_Rect bg_rect = { .x = 0, .y = 0, .w = SCREEN_WIDTH, .h = SCREEN_HEIGHT }; bool quit = false; controller_t ctrl = {0}; + player_t player; + player_init(&player); + while (!quit) { SDL_SetRenderDrawColor(renderer, 0, 0, 0, 1); SDL_RenderClear(renderer); @@ -99,28 +102,9 @@ main (int argc, controller_update(&ctrl, &event); } - if (controller_is_up_pressed(&ctrl) && rect.y > 0) { - rect.y -= 8; - rect.y = rect.y < 0 ? 0 : rect.y; - } - - if (controller_is_down_pressed(&ctrl) && rect.y + 8 < SCREEN_HEIGHT) { - rect.y += 8; - rect.y = rect.y + rect.h > SCREEN_HEIGHT ? SCREEN_HEIGHT - rect.h : rect.y; - } - - if (controller_is_left_pressed(&ctrl) && rect.x > 0) { - rect.x -= 8; - rect.x = rect.x < 0 ? 0 : rect.x; - } - - if (controller_is_right_pressed(&ctrl) && rect.x + 8 < SCREEN_WIDTH) { - rect.x += 8; - rect.x = rect.x + rect.h > SCREEN_WIDTH ? SCREEN_WIDTH - rect.h : rect.x; - } + player_update(&player, &ctrl, SCREEN_WIDTH, SCREEN_HEIGHT); + player_draw(&player, renderer); - SDL_SetRenderDrawColor(renderer, 0xFF, 0, 0, 1); - SDL_RenderFillRect(renderer, &rect); SDL_RenderPresent(renderer); uint64_t end = SDL_GetPerformanceCounter(); diff --git a/src/player.c b/src/player.c new file mode 100644 index 0000000..000eb76 --- /dev/null +++ b/src/player.c @@ -0,0 +1,91 @@ +/* +* Copyright (c) 2021, Johnny Richard +* All rights reserved. +* +* Redistribution and use in source and binary forms, with or without +* modification, are permitted provided that the following conditions are met: +* +* 1. Redistributions of source code must retain the above copyright notice, this +* list of conditions and the following disclaimer. +* +* 2. Redistributions in binary form must reproduce the above copyright notice, +* this list of conditions and the following disclaimer in the documentation +* and/or other materials provided with the distribution. +* +* 3. Neither the name of the copyright holder nor the names of its +* contributors may be used to endorse or promote products derived from +* this software without specific prior written permission. +* +* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE +* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +#include "player.h" + +#include +#include + +void +player_init(player_t *player) +{ + assert(player && "player is required"); + player->x = 0; + player->y = 0; + player->width = 20; + player->height = 20; + player->speed = 8; +} + +void player_update(player_t *player, + controller_t *ctrl, + int screen_width, + int screen_height) +{ + if (controller_is_up_pressed(ctrl) && player->y > 0) { + player->y -= player->speed; + player->y = player->y < 0 ? 0 : player->y; + } + + if (controller_is_down_pressed(ctrl) && player->y < screen_height) { + player->y += player->speed; + player->y = player->y + player->height > screen_height + ? screen_height - player->height + : player->y; + } + + if (controller_is_left_pressed(ctrl) && player->x > 0) { + player->x -= player->speed; + player->x = player->x < 0 + ? 0 + : player->x; + } + + if (controller_is_right_pressed(ctrl) && player->x < screen_width) { + player->x += player->speed; + player->x = player->x + player->width > screen_width + ? screen_width - player->width + : player->x; + } +} + +void +player_draw(player_t *player, SDL_Renderer *renderer) +{ + SDL_Rect rect = { + .x = player->x, + .y = player->y, + .w = player->width, + .h = player->height + }; + + SDL_SetRenderDrawColor(renderer, 0, 0, 0xFF, 1); + SDL_RenderFillRect(renderer, &rect); +} diff --git a/src/player.h b/src/player.h new file mode 100644 index 0000000..1f211c7 --- /dev/null +++ b/src/player.h @@ -0,0 +1,55 @@ +/* +* Copyright (c) 2021, Johnny Richard +* All rights reserved. +* +* Redistribution and use in source and binary forms, with or without +* modification, are permitted provided that the following conditions are met: +* +* 1. Redistributions of source code must retain the above copyright notice, this +* list of conditions and the following disclaimer. +* +* 2. Redistributions in binary form must reproduce the above copyright notice, +* this list of conditions and the following disclaimer in the documentation +* and/or other materials provided with the distribution. +* +* 3. Neither the name of the copyright holder nor the names of its +* contributors may be used to endorse or promote products derived from +* this software without specific prior written permission. +* +* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE +* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ +#ifndef PLAYER_H +#define PLAYER_H + +#include "controller.h" + +#include + +typedef struct player { + uint16_t x; + uint16_t y; + uint16_t width; + uint16_t height; + uint16_t speed; +} player_t; + +void player_init(player_t *player); + +// FIXME: Find out a better way to provide screen w and h properties +void player_update(player_t *player, + controller_t *ctrl, + int screen_width, + int screen_height); + +void player_draw(player_t *player, SDL_Renderer *renderer); + +#endif /* PLAYER_H */ -- cgit v1.2.3