diff --git a/CMakeLists.txt b/CMakeLists.txt index 3b0aa59568932eaccc2dc9ece83d8d67649e533d..745b72a2e4ecc30a0a6c97876847a5a08cd604fe 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 9679dd6e63519ad49faa5e67fe14ff9903cf4065..718b0318b4dc15bdd5de18924c9a29fd815fccfc 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 501186a27e8b28d9234b4eaf3bb31b27b03e9d7e..44b092cc69c9bdd31e343b45ed8d4d7dfd5f33f1 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 f30f661875ff40a52522a45cd75fccbae38f9506..dafee86e0c9eca4397ec7235754e3fa781dee92d 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 b7b4fb53ebe26092ad93193ced0a2cb9fea75d29..e91e9cf6a9b9a33a9b01da30718de243adb933e3 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 e1cb5121a33a3dd3ea73c805cdb502b57a2b5bef..f39047592f024ce8eb3980af693fb129141bff50 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(); }