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 --- .gitignore | 2 ++ Makefile | 20 ++++++++++++ README.md | 2 ++ src/main.c | 106 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 130 insertions(+) create mode 100644 .gitignore create mode 100644 Makefile create mode 100644 README.md create mode 100644 src/main.c diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f18d57b --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +*.exe +main diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..5d398f2 --- /dev/null +++ b/Makefile @@ -0,0 +1,20 @@ +.PHONY: build clean run + +CFLAGS := -Wall + +ifeq ($(OS),Windows_NT) + CC := gcc + CFLAGS += -lmingw32 -lSDL2main -lSDL2 \ + -IG:\dev\mingw\include\SDL2 +else + CFLAGS += -Wall $(shell pkg-config sdl2 --cflags --libs) +endif + +build: + $(CC) src/main.c -o main $(CFLAGS) + +clean: + rm ./game; + +run: + ./game; diff --git a/README.md b/README.md new file mode 100644 index 0000000..0f4e4bc --- /dev/null +++ b/README.md @@ -0,0 +1,2 @@ +Game +==== 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 --- .gitignore | 4 +++- Makefile | 24 +++++++++++++++++++----- src/controller.h | 21 +++++++++++++++++++++ src/main.c | 11 +++-------- 4 files changed, 46 insertions(+), 14 deletions(-) create mode 100644 src/controller.h diff --git a/.gitignore b/.gitignore index f18d57b..ff853ac 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ *.exe -main +*.o +/build +blast_attack diff --git a/Makefile b/Makefile index 5d398f2..68d2425 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,9 @@ -.PHONY: build clean run +TARGET ?= blast_attack +SOURCE_DIR ?= ./src +BUILD_DIR ?= ./build CFLAGS := -Wall +OBJS := $(BUILD_DIR)/main.o ifeq ($(OS),Windows_NT) CC := gcc @@ -10,11 +13,22 @@ else CFLAGS += -Wall $(shell pkg-config sdl2 --cflags --libs) endif -build: - $(CC) src/main.c -o main $(CFLAGS) +.PHONY: build clean run $(TARGET) + +all: $(TARGET) + +$(TARGET): $(BUILD_DIR) $(OBJS) + $(CC) $(OBJS) -o $(TARGET) $(CFLAGS) + +$(BUILD_DIR): + @mkdir -p $@ + +$(BUILD_DIR)/%.o: $(SOURCE_DIR)/%.c + $(CC) $(CFLAGS) -c $< -o $@ clean: - rm ./game; + @rm -rf $(OBJS) + @rm $(TARGET) run: - ./game; + ./$(TARGET) 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 --- Makefile | 3 ++- src/controller.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ src/controller.h | 4 ++++ src/main.c | 38 +------------------------------------- 4 files changed, 53 insertions(+), 38 deletions(-) create mode 100644 src/controller.c diff --git a/Makefile b/Makefile index 68d2425..6c615f3 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,8 @@ SOURCE_DIR ?= ./src BUILD_DIR ?= ./build CFLAGS := -Wall -OBJS := $(BUILD_DIR)/main.o +OBJS := $(BUILD_DIR)/main.o \ + $(BUILD_DIR)/controller.o ifeq ($(OS),Windows_NT) CC := gcc 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 1f9826faefa2b1a1bddac3f3413a6d6055ee49bf Mon Sep 17 00:00:00 2001 From: Johnny Richard Date: Sat, 17 Apr 2021 01:27:27 +0200 Subject: Add microunit test framework Signed-off-by: Johnny Richard --- .gitignore | 1 + Makefile | 42 +++++++++++++++++------- README.md | 22 +++++++++++-- test/controller-test.c | 89 ++++++++++++++++++++++++++++++++++++++++++++++++++ third_party/munit | 1 + 5 files changed, 142 insertions(+), 13 deletions(-) create mode 100644 test/controller-test.c create mode 160000 third_party/munit diff --git a/.gitignore b/.gitignore index ff853ac..1f44161 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ *.exe *.o +/lib /build blast_attack diff --git a/Makefile b/Makefile index 6c615f3..2433900 100644 --- a/Makefile +++ b/Makefile @@ -1,20 +1,25 @@ -TARGET ?= blast_attack -SOURCE_DIR ?= ./src -BUILD_DIR ?= ./build - -CFLAGS := -Wall -OBJS := $(BUILD_DIR)/main.o \ - $(BUILD_DIR)/controller.o +TARGET ?= blast_attack +SOURCE_DIR ?= ./src +TEST_DIR ?= ./test +LIB_DIR ?= ./lib +BUILD_DIR ?= ./build + +CFLAGS := -Wall +CFLAGS_TEST := -Isrc -Ithird_party -Llib -lmunit +SRCS := $(wildcard $(SOURCE_DIR)/*.c) +SRCS_TEST := $(wildcard $(TEST_DIR)/*.c) +OBJS := $(patsubst $(SOURCE_DIR)/%.c, $(BUILD_DIR)/%.o, $(SRCS)) +TESTS := $(patsubst $(TEST_DIR)/%-test.c, $(TEST_DIR)/%, $(SRCS_TEST)) ifeq ($(OS),Windows_NT) CC := gcc CFLAGS += -lmingw32 -lSDL2main -lSDL2 \ -IG:\dev\mingw\include\SDL2 else - CFLAGS += -Wall $(shell pkg-config sdl2 --cflags --libs) + CFLAGS += $(shell pkg-config sdl2 --cflags --libs) endif -.PHONY: build clean run $(TARGET) +.PHONY: build clean run test $(TARGET) all: $(TARGET) @@ -24,12 +29,27 @@ $(TARGET): $(BUILD_DIR) $(OBJS) $(BUILD_DIR): @mkdir -p $@ +$(LIB_DIR): + @mkdir -p $@ + $(BUILD_DIR)/%.o: $(SOURCE_DIR)/%.c $(CC) $(CFLAGS) -c $< -o $@ +$(TEST_DIR)/%-test: $(BUILD_DIR)/%.o $(TEST_DIR)/%-test.c + $(CC) $(CFLAGS) $(CFLAGS_TEST) $? -o $@ + +$(TEST_DIR)/%: $(TEST_DIR)/%-test + ./$@-test + +test: lib/libmunit.so $(BUILD_DIR) $(TESTS) + +lib/libmunit.so: $(LIB_DIR) third_party/munit/munit.c + $(CC) -Wall -Werror -fpic -c third_party/munit/munit.c -shared -o lib/libmunit.so + clean: - @rm -rf $(OBJS) + @rm -rf $(BUILD_DIR) + @rm -rf $(LIB_DIR) @rm $(TARGET) -run: +run: all ./$(TARGET) diff --git a/README.md b/README.md index 0f4e4bc..99cb855 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,20 @@ -Game -==== +Black Attack +------------ + +## Dependencies + +- make +- SDL2 +- pkg-config + +## Run + +```shell +$ make run +``` + +## Tests + +```shell +$ make test +``` diff --git a/test/controller-test.c b/test/controller-test.c new file mode 100644 index 0000000..fb13000 --- /dev/null +++ b/test/controller-test.c @@ -0,0 +1,89 @@ +#define MUNIT_ENABLE_ASSERT_ALIASES +#include "controller.h" +#include + +static SDL_Event +an_event(SDL_EventType type, SDL_KeyCode keycode) { + SDL_Event event; + event.type = type; + event.key.keysym.sym = keycode; + return event; +} + +static MunitResult +press_and_release_up(const MunitParameter params[], void* fixture) +{ + controller_t ctrl = {0}; + + SDL_Event event = an_event(SDL_KEYDOWN, SDLK_UP); + controller_update(&ctrl, &event); + assert_true(ctrl.up); + + event = an_event(SDL_KEYUP, SDLK_UP); + controller_update(&ctrl, &event); + assert_false(ctrl.up); + return MUNIT_OK; +} + +static MunitResult +press_and_release_down(const MunitParameter params[], void* fixture) +{ + controller_t ctrl = {0}; + + SDL_Event event = an_event(SDL_KEYDOWN, SDLK_DOWN); + controller_update(&ctrl, &event); + assert_true(ctrl.down); + + event = an_event(SDL_KEYUP, SDLK_DOWN); + controller_update(&ctrl, &event); + assert_false(ctrl.down); + return MUNIT_OK; +} + +static MunitResult +press_and_release_right(const MunitParameter params[], void* fixture) +{ + controller_t ctrl = {0}; + + SDL_Event event = an_event(SDL_KEYDOWN, SDLK_RIGHT); + controller_update(&ctrl, &event); + assert_true(ctrl.right); + + event = an_event(SDL_KEYUP, SDLK_RIGHT); + controller_update(&ctrl, &event); + assert_false(ctrl.right); + return MUNIT_OK; +} + +static MunitResult +press_and_release_left(const MunitParameter params[], void* fixture) +{ + controller_t ctrl = {0}; + + SDL_Event event = an_event(SDL_KEYDOWN, SDLK_LEFT); + controller_update(&ctrl, &event); + assert_true(ctrl.left); + + event = an_event(SDL_KEYUP, SDLK_LEFT); + controller_update(&ctrl, &event); + assert_false(ctrl.left); + return MUNIT_OK; +} + +MunitTest tests[] = { + { "/press_and_release_up", press_and_release_up, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL }, + { "/press_and_release_down", press_and_release_down, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL }, + { "/press_and_release_right", press_and_release_right, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL }, + { "/press_and_release_left", press_and_release_left, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL }, + { NULL, NULL, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL } +}; + +static const MunitSuite suite = { + "/controller", tests, NULL, 1, MUNIT_TEST_OPTION_NONE +}; + +int +main(int argc, char *argv[]) +{ + return munit_suite_main(&suite, NULL, argc, argv); +} diff --git a/third_party/munit b/third_party/munit new file mode 160000 index 0000000..fbbdf14 --- /dev/null +++ b/third_party/munit @@ -0,0 +1 @@ +Subproject commit fbbdf1467eb0d04a6ee465def2e529e4c87f2118 -- cgit v1.2.3 From c7c8d6bed07ff7112d4d8f137ec1a5e1f8092167 Mon Sep 17 00:00:00 2001 From: Johnny Richard Date: Tue, 20 Apr 2021 02:21:59 +0200 Subject: Add submodule munit version v0.2.0 Signed-off-by: Johnny Richard --- .gitmodules | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 .gitmodules diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..a1d1dca --- /dev/null +++ b/.gitmodules @@ -0,0 +1,4 @@ +[submodule "third_party/munit"] + path = third_party/munit + url = git@github.com:nemequ/munit.git + branch = v0.2.0 -- cgit v1.2.3 From 655fd7c2b14fb3429d90d9d494cdb696666fa237 Mon Sep 17 00:00:00 2001 From: Johnny Richard Date: Tue, 20 Apr 2021 02:26:53 +0200 Subject: Add download instruction to README file Signed-off-by: Johnny Richard --- README.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/README.md b/README.md index 99cb855..536b8a0 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,12 @@ Black Attack ------------ +## Download + +```shell +$ git clone --recursive git@github.com:johnnyrichard/blast-attack.git +``` + ## Dependencies - make -- cgit v1.2.3 From 41091489ac8bcdab2b0b9f53ad62913caf8565f6 Mon Sep 17 00:00:00 2001 From: Johnny Richard Date: Tue, 20 Apr 2021 02:27:55 +0200 Subject: Fix README.md title Signed-off-by: Johnny Richard --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 536b8a0..8aa2a48 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ Black Attack ------------- +============ ## Download -- cgit v1.2.3 From c367a177b600311f8b22cb9f274a834a8ea4c25e Mon Sep 17 00:00:00 2001 From: Johnny Richard Date: Sun, 24 Oct 2021 23:49:57 +0200 Subject: docs: Fix poject name on README.md Signed-off-by: Johnny Richard --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 8aa2a48..795f49a 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -Black Attack +Blast Attack ============ ## Download -- 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 --- LICENSE | 28 ++++++++++++++++++++++++++++ src/controller.c | 34 ++++++++++++++++++++++++++++++++-- src/controller.h | 30 ++++++++++++++++++++++++++++++ src/main.c | 44 +++++++++++++++++++++++++++++++++++++------- test/controller-test.c | 30 ++++++++++++++++++++++++++++++ 5 files changed, 157 insertions(+), 9 deletions(-) create mode 100644 LICENSE diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..5540c06 --- /dev/null +++ b/LICENSE @@ -0,0 +1,28 @@ +Copyright (c) 2021, Blast Attack Authors +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. + 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; } diff --git a/test/controller-test.c b/test/controller-test.c index fb13000..d6c1455 100644 --- a/test/controller-test.c +++ b/test/controller-test.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. + */ + #define MUNIT_ENABLE_ASSERT_ALIASES #include "controller.h" #include -- cgit v1.2.3 From cc7ca20d7e76bdbfef97bd4fa1bf136871f03e7d Mon Sep 17 00:00:00 2001 From: Johnny Richard Date: Thu, 28 Oct 2021 02:59:06 +0200 Subject: docs: Add AUTHORS files Signed-off-by: Johnny Richard --- AUTHORS | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 AUTHORS diff --git a/AUTHORS b/AUTHORS new file mode 100644 index 0000000..58c0bb0 --- /dev/null +++ b/AUTHORS @@ -0,0 +1,2 @@ +Fabio Maciel +Johnny Richard -- cgit v1.2.3 From de155a9a7d5d2e04d0d2a841e62a97430fd428b0 Mon Sep 17 00:00:00 2001 From: Johnny Richard Date: Fri, 5 Nov 2021 19:39:33 +0100 Subject: docs: Rename repository url on README file Signed-off-by: Johnny Richard --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 795f49a..de76286 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ Blast Attack ## Download ```shell -$ git clone --recursive git@github.com:johnnyrichard/blast-attack.git +$ git clone --recursive git@git.sr.ht:~johnnyrichard/blast-attack ``` ## Dependencies -- 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(-) 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 --- Makefile | 2 +- src/controller.c | 72 ++++++++++++++++++++++++++++++++------------------ src/controller.h | 22 ++++++++------- src/main.c | 12 ++++----- test/controller-test.c | 16 +++++------ 5 files changed, 73 insertions(+), 51 deletions(-) diff --git a/Makefile b/Makefile index 2433900..2a46725 100644 --- a/Makefile +++ b/Makefile @@ -44,7 +44,7 @@ $(TEST_DIR)/%: $(TEST_DIR)/%-test test: lib/libmunit.so $(BUILD_DIR) $(TESTS) lib/libmunit.so: $(LIB_DIR) third_party/munit/munit.c - $(CC) -Wall -Werror -fpic -c third_party/munit/munit.c -shared -o lib/libmunit.so + $(CC) -Wall -fpic -c third_party/munit/munit.c -shared -o lib/libmunit.so clean: @rm -rf $(BUILD_DIR) 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, diff --git a/test/controller-test.c b/test/controller-test.c index d6c1455..c271756 100644 --- a/test/controller-test.c +++ b/test/controller-test.c @@ -47,11 +47,11 @@ press_and_release_up(const MunitParameter params[], void* fixture) SDL_Event event = an_event(SDL_KEYDOWN, SDLK_UP); controller_update(&ctrl, &event); - assert_true(ctrl.up); + assert_true(controller_is_up_pressed(&ctrl)); event = an_event(SDL_KEYUP, SDLK_UP); controller_update(&ctrl, &event); - assert_false(ctrl.up); + assert_false(controller_is_up_pressed(&ctrl)); return MUNIT_OK; } @@ -62,11 +62,11 @@ press_and_release_down(const MunitParameter params[], void* fixture) SDL_Event event = an_event(SDL_KEYDOWN, SDLK_DOWN); controller_update(&ctrl, &event); - assert_true(ctrl.down); + assert_true(controller_is_down_pressed(&ctrl)); event = an_event(SDL_KEYUP, SDLK_DOWN); controller_update(&ctrl, &event); - assert_false(ctrl.down); + assert_false(controller_is_down_pressed(&ctrl)); return MUNIT_OK; } @@ -77,11 +77,11 @@ press_and_release_right(const MunitParameter params[], void* fixture) SDL_Event event = an_event(SDL_KEYDOWN, SDLK_RIGHT); controller_update(&ctrl, &event); - assert_true(ctrl.right); + assert_true(controller_is_right_pressed(&ctrl)); event = an_event(SDL_KEYUP, SDLK_RIGHT); controller_update(&ctrl, &event); - assert_false(ctrl.right); + assert_false(controller_is_right_pressed(&ctrl)); return MUNIT_OK; } @@ -92,11 +92,11 @@ press_and_release_left(const MunitParameter params[], void* fixture) SDL_Event event = an_event(SDL_KEYDOWN, SDLK_LEFT); controller_update(&ctrl, &event); - assert_true(ctrl.left); + assert_true(controller_is_left_pressed(&ctrl)); event = an_event(SDL_KEYUP, SDLK_LEFT); controller_update(&ctrl, &event); - assert_false(ctrl.left); + assert_false(controller_is_left_pressed(&ctrl)); return MUNIT_OK; } -- 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(-) 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(-) 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(-) 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 --- Makefile | 4 ++-- src/main.c | 41 ++++++++++++++++++++++++----------------- 2 files changed, 26 insertions(+), 19 deletions(-) diff --git a/Makefile b/Makefile index 2a46725..208cc9f 100644 --- a/Makefile +++ b/Makefile @@ -4,8 +4,8 @@ TEST_DIR ?= ./test LIB_DIR ?= ./lib BUILD_DIR ?= ./build -CFLAGS := -Wall -CFLAGS_TEST := -Isrc -Ithird_party -Llib -lmunit +CFLAGS := -Wall -lm +CFLAGS_TEST := -Isrc -Ithird_party -Llib -lmunit SRCS := $(wildcard $(SOURCE_DIR)/*.c) SRCS_TEST := $(wildcard $(TEST_DIR)/*.c) OBJS := $(patsubst $(SOURCE_DIR)/%.c, $(BUILD_DIR)/%.o, $(SRCS)) 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(-) 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(-) 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 --- .gitignore | 3 +- src/main.c | 28 ++++--------------- src/player.c | 91 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/player.h | 55 ++++++++++++++++++++++++++++++++++++ 4 files changed, 154 insertions(+), 23 deletions(-) create mode 100644 src/player.c create mode 100644 src/player.h diff --git a/.gitignore b/.gitignore index 1f44161..449f08f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ *.exe *.o -/lib /build +/lib blast_attack +tags 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 From 394ffe17766082c6895208f3fc5710a53cb4f01f Mon Sep 17 00:00:00 2001 From: Johnny Richard Date: Wed, 20 Apr 2022 03:36:16 +0200 Subject: TODO: Add TODO list --- TODO | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 TODO diff --git a/TODO b/TODO new file mode 100644 index 0000000..819999e --- /dev/null +++ b/TODO @@ -0,0 +1,3 @@ +- [ ] Create a second player + - [ ] Make controller more flexible to accept different buttons +- [ ] Create enemies -- cgit v1.2.3