From 9843d558fd056c890c460b9a06f153102a6b77e9 Mon Sep 17 00:00:00 2001 From: alfrh02 <alfred.n.hall@gmail.com> Date: Thu, 12 Dec 2024 16:32:25 +0000 Subject: [PATCH] a0.6_0 Added 'wall' tool; renamed run.sh to build.sh which can be ran with to run the subsequent binary file; organised mouse & keyboard inputs and UI rendering into their own functions --- CMakeLists.txt | 2 +- README.md | 4 +- run.sh => build.sh | 5 +- src/game.cpp | 114 ++++++++++++++++++++++++++++++++++----------- src/game.h | 22 ++++++++- src/main.cpp | 3 ++ 6 files changed, 118 insertions(+), 32 deletions(-) rename run.sh => build.sh (59%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 3b0aa59..745b72a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,7 +5,7 @@ set(CMAKE_CXX_STANDARD_REQUIRED NO) set(CMAKE_CXX_EXTENSIONS OFF) # major, minor, patch, and _STAGE_ (0 = alpha, 1 = beta, 2 = gamma/release) -project(radiance_cascades VERSION 0.5.2.0) +project(radiance_cascades VERSION 0.6.0.0) configure_file(config.h.in config.h) diff --git a/README.md b/README.md index 9679dd6..718b031 100644 --- a/README.md +++ b/README.md @@ -33,8 +33,8 @@ sudo pacman -S raylib Compile via CMake: ```bash -# either run the convenience build-n-run script -./run.sh +# either run the convenience build script (with the `-r` flag to run the binary after compilation) +./build.sh -r # or build it manually mkdir build diff --git a/run.sh b/build.sh similarity index 59% rename from run.sh rename to build.sh index 501186a..44b092c 100755 --- a/run.sh +++ b/build.sh @@ -8,4 +8,7 @@ pushd build cmake .. make popd -./build/radiance_cascades + +while getopts "r:" arg; do + ./build/radiance_cascades +done diff --git a/src/game.cpp b/src/game.cpp index f30f661..dafee86 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -4,6 +4,7 @@ void Game::setup() { boxPosition = { (float)SCREEN_WIDTH/2, (float)SCREEN_HEIGHT/2 }; boxSize = 50; debug = false; + tool = BRUSH; shader = LoadShader(0, TextFormat("res/shaders/rainbow.frag", GLSL_VERSION)); @@ -15,38 +16,13 @@ void Game::setup() { } void Game::update() { - if (IsKeyPressed(KEY_F3)) debug = !debug; - if (IsKeyPressed(KEY_C)) { - canvas = GenImageColor(SCREEN_WIDTH, SCREEN_HEIGHT, BACKGROUND_COLOR); - UnloadTexture(canvasTex); - canvasTex = LoadTextureFromImage(canvas); - } - -//--- - - if (IsKeyDown(KEY_W)) boxPosition.y -= 80.0f * GetFrameTime(); - if (IsKeyDown(KEY_A)) boxPosition.x -= 80.0f * GetFrameTime(); - if (IsKeyDown(KEY_S)) boxPosition.y += 80.0f * GetFrameTime(); - if (IsKeyDown(KEY_D)) boxPosition.x += 80.0f * GetFrameTime(); - boxSize = 50 + (sin(GetTime() * 2) * 2); time = GetTime(); SetShaderValue(shader, GetShaderLocation(shader, "uTime"), &time, SHADER_UNIFORM_FLOAT); - - if (IsMouseButtonDown(0)) { - ImageDraw(&canvas, - brush, - (Rectangle){ 0, 0, canvas.width, canvas.height }, - (Rectangle){ (float)GetMouseX() - brush.width/2*BRUSH_SCALE, (float)GetMouseY() - brush.height/2*BRUSH_SCALE, brush.width*BRUSH_SCALE, brush.height*BRUSH_SCALE }, - BLACK); - UnloadTexture(canvasTex); - canvasTex = LoadTextureFromImage(canvas); - } } void Game::render() { ClearBackground(PINK); - DrawTexture(canvasTex, 0, 0, WHITE); BeginShaderMode(shader); @@ -56,14 +32,98 @@ void Game::render() { MAROON); EndShaderMode(); - DrawTextureEx(brushTex, + for (Rectangle* r : walls) { + DrawRectanglePro(*r, (Vector2){ 0, 0 }, 0, BLACK); + } + + if (tool == BRUSH) { + DrawTextureEx(brushTex, (Vector2){ (float)(GetMouseX() - brush.width/2*BRUSH_SCALE), (float)(GetMouseY() - brush.height/2*BRUSH_SCALE) }, 0.0, BRUSH_SCALE, BLACK); + } else { + DrawRectanglePro(boxToolInfo.rect, + (Vector2){ 1, 1 }, + 0, + BLACK); + } +} + +void Game::renderUI() { + std::string toolstr = ""; + if (tool == BRUSH) { + toolstr = "Brush"; + } else if (tool == BOX) { + toolstr = "Box"; + } + DrawText(TextFormat("%s", toolstr.c_str()), 0, 0, 1, BLACK); if (debug) { - DrawText(TextFormat("%i FPS", GetFPS()), 0, 0, 1, BLACK); + DrawText(TextFormat("%i FPS", GetFPS()), 0, 8, 1, BLACK); + } +} + +void Game::processKeyboardInput() { + if (IsKeyPressed(KEY_F3)) debug = !debug; + if (IsKeyPressed(KEY_C)) { + // clear canvas + std::cout << "Clearing canvas." << std::endl; + canvas = GenImageColor(SCREEN_WIDTH, SCREEN_HEIGHT, BACKGROUND_COLOR); + UnloadTexture(canvasTex); + canvasTex = LoadTextureFromImage(canvas); + + // clear walls + std::cout << "Removing " << walls.size() << " walls." << std::endl; + // for (int i = walls.size(); i > 0; i--) { + for (int i = 0; i < walls.size(); i++) { + delete walls[i]; + walls.erase(walls.begin() + i); + i--; + } + } + + if (IsKeyPressed(KEY_ONE)) tool = BRUSH; + if (IsKeyPressed(KEY_TWO)) tool = BOX; + + if (IsKeyDown(KEY_W)) boxPosition.y -= 80.0f * GetFrameTime(); + if (IsKeyDown(KEY_A)) boxPosition.x -= 80.0f * GetFrameTime(); + if (IsKeyDown(KEY_S)) boxPosition.y += 80.0f * GetFrameTime(); + if (IsKeyDown(KEY_D)) boxPosition.x += 80.0f * GetFrameTime(); +} + +void Game::processMouseInput() { + // switch case doesnt work? + if (tool == BRUSH) { + if (IsMouseButtonDown(0)) { + ImageDraw(&canvas, + brush, + (Rectangle){ 0, 0, (float)canvas.width, (float)canvas.height }, + (Rectangle){ GetMouseX() - brush.width/2*BRUSH_SCALE, GetMouseY() - brush.height/2*BRUSH_SCALE, brush.width*BRUSH_SCALE, brush.height*BRUSH_SCALE }, + BLACK); + UnloadTexture(canvasTex); + canvasTex = LoadTextureFromImage(canvas); + } + } + if (tool == BOX) { + if (IsMouseButtonPressed(0)) { + boxToolInfo.rect.x = GetMouseX(); + boxToolInfo.rect.y = GetMouseY(); + } + if (IsMouseButtonDown(0)) { + boxToolInfo.rect.width = (GetMouseX() - boxToolInfo.rect.x); + boxToolInfo.rect.height = (GetMouseY() - boxToolInfo.rect.y); + } + if (IsMouseButtonReleased(0)) { + Rectangle* r = new Rectangle; + r->x = boxToolInfo.rect.x; + r->y = boxToolInfo.rect.y; + r->width = boxToolInfo.rect.width; + r->height = boxToolInfo.rect.height; + walls.push_back(r); + boxToolInfo.rect.width = 0; + boxToolInfo.rect.height = 0; + } } } diff --git a/src/game.h b/src/game.h index b7b4fb5..e91e9cf 100644 --- a/src/game.h +++ b/src/game.h @@ -5,6 +5,12 @@ #include "config.h" #include <math.h> #include <iostream> +#include <vector> + +enum Tool { + BRUSH, + BOX +}; class Game { public: @@ -12,17 +18,31 @@ class Game { void update(); void render(); + // functions purely for organisation + void renderUI(); + void processKeyboardInput(); + void processMouseInput(); + private: Vector2 boxPosition; float boxSize; + Tool tool; Shader shader; + bool debug; float time; - bool debug; Image brush; Texture2D brushTex; Image canvas; Texture2D canvasTex; + + struct { + Rectangle rect; + ushort x; + ushort y; + } boxToolInfo; + + std::vector<Rectangle*> walls; }; #endif /* GAME_H */ diff --git a/src/main.cpp b/src/main.cpp index e1cb512..f390475 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -14,9 +14,12 @@ int main() { game.setup(); while (!WindowShouldClose()) { + game.processKeyboardInput(); + game.processMouseInput(); game.update(); BeginDrawing(); game.render(); + game.renderUI(); EndDrawing(); } -- GitLab