From 90b9831d6fb89feedeec63cb392c4535c5df60fe Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Sun, 24 Feb 2019 12:30:40 -0500 Subject: Started state machine The "--editor" flag can be passed to the program to start it in editor mode, which is currently nothing. The Renderer class was removed here, as each state basically needs to do its own rendering. However, refactoring to make this more elegant will probably occur in the future. --- CMakeLists.txt | 2 +- src/editor.cpp | 13 +++++++ src/editor.h | 17 +++++++++ src/main.cpp | 49 +++++++++++++++++++++---- src/renderer.cpp | 103 ----------------------------------------------------- src/renderer.h | 15 -------- src/simulation.cpp | 43 ++++++++++++++++++++++ src/simulation.h | 8 +++-- src/state.h | 19 ++++++++++ 9 files changed, 141 insertions(+), 128 deletions(-) create mode 100644 src/editor.cpp create mode 100644 src/editor.h delete mode 100644 src/renderer.cpp create mode 100644 src/state.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 6fabbf3..a1456a6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -21,9 +21,9 @@ include_directories( add_executable(dispatcher src/main.cpp - src/renderer.cpp src/simulation.cpp src/direction.cpp + src/editor.cpp ) set_property(TARGET dispatcher PROPERTY CXX_STANDARD 17) diff --git a/src/editor.cpp b/src/editor.cpp new file mode 100644 index 0000000..6e53ac3 --- /dev/null +++ b/src/editor.cpp @@ -0,0 +1,13 @@ +#include "editor.h" + +void Editor::tick( + double dt, + const Uint8* keystate) +{ + +} + +void Editor::render(SDL_Renderer* ren) +{ + +} diff --git a/src/editor.h b/src/editor.h new file mode 100644 index 0000000..aa137ec --- /dev/null +++ b/src/editor.h @@ -0,0 +1,17 @@ +#ifndef EDITOR_H_8BB54FE3 +#define EDITOR_H_8BB54FE3 + +#include "state.h" + +class Editor : public State { +public: + + void tick( + double dt, + const Uint8* keystate) override; + + void render(SDL_Renderer* ren) override; + +}; + +#endif /* end of include guard: EDITOR_H_8BB54FE3 */ diff --git a/src/main.cpp b/src/main.cpp index c0ba15f..7c65e81 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,17 +1,51 @@ #include "renderer.h" #include +#include "renderer.h" #include "simulation.h" #include "level.h" +#include "editor.h" -int main(int, char**) +int main(int argc, char** argv) { std::random_device randomEngine; std::mt19937 rng(randomEngine()); - Renderer renderer; + window_ptr win( + SDL_CreateWindow( + "dispatcher", + 100, + 100, + WINDOW_SIZE.w(), + WINDOW_SIZE.h(), + SDL_WINDOW_SHOWN)); + + if (!win) + { + throw sdl_error(); + } + + renderer_ptr ren( + SDL_CreateRenderer( + win.get(), + -1, + SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC)); + + if (!ren) + { + throw sdl_error(); + } + Level level; - Simulation sim(level); + + std::unique_ptr state; + + if (argc == 2 && !strcmp(argv[1], "--editor")) + { + state.reset(new Editor()); + } else { + state.reset(new Simulation(level)); + } bool quit = false; @@ -43,10 +77,13 @@ int main(int, char**) } } - const Uint8* state = SDL_GetKeyboardState(NULL); + const Uint8* keystate = SDL_GetKeyboardState(NULL); - sim.tick(frameTime / 1000.0, state); + state->tick(frameTime / 1000.0, keystate); - renderer.render(sim); + SDL_SetRenderTarget(ren.get(), nullptr); + SDL_RenderClear(ren.get()); + state->render(ren.get()); + SDL_RenderPresent(ren.get()); } } diff --git a/src/renderer.cpp b/src/renderer.cpp deleted file mode 100644 index 286a530..0000000 --- a/src/renderer.cpp +++ /dev/null @@ -1,103 +0,0 @@ -#include "renderer.h" -#include "simulation.h" -#include "consts.h" -#include "level.h" -#include "views.h" - -Renderer::Renderer() -{ - win_ = window_ptr( - SDL_CreateWindow( - "dispatcher", - 100, - 100, - WINDOW_SIZE.w(), - WINDOW_SIZE.h(), - SDL_WINDOW_SHOWN)); - - if (!win_) - { - throw sdl_error(); - } - - ren_ = renderer_ptr( - SDL_CreateRenderer( - win_.get(), - -1, - SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC)); - - if (!ren_) - { - throw sdl_error(); - } - - SDL_SetRenderDrawBlendMode(ren_.get(), SDL_BLENDMODE_MOD); - SDL_SetRenderDrawColor(ren_.get(), 255, 150, 255, 255); - SDL_RenderFillRect(ren_.get(), nullptr); -} - -void Renderer::render(const Simulation& sim) -{ - texture_ptr canvas( - SDL_CreateTexture( - ren_.get(), - SDL_PIXELFORMAT_RGBA8888, - SDL_TEXTUREACCESS_TARGET, - WINDOW_SIZE.w(), - WINDOW_SIZE.h())); - - if (!canvas) - { - throw sdl_error(); - } - - SDL_SetRenderTarget(ren_.get(), nullptr); - SDL_SetRenderDrawBlendMode(ren_.get(), SDL_BLENDMODE_NONE); - SDL_SetRenderDrawColor(ren_.get(), 255, 150, rand() % 255, 255); - SDL_RenderClear(ren_.get()); - - const Level& level = sim.getLevel(); - - for (size_t y = 0; y < level.getSize().h(); y++) - { - for (size_t x = 0; x < level.getSize().w(); x++) - { - int val = 255 - level.at(x, y) * 10; - - SDL_SetRenderDrawColor(ren_.get(), val, val, val, 255); - - SDL_Rect rect { - static_cast(x * TILE_SIZE.w()), - static_cast(y * TILE_SIZE.h()), - TILE_SIZE.w(), - TILE_SIZE.h() - }; - - SDL_RenderFillRect(ren_.get(), &rect); - } - } - - constexpr Layer renderOrder[] = { Layer::track, Layer::object }; - - ranges::for_each( - ranges::view::for_each( - renderOrder, - [&] (Layer layer) { - return sim.entityRange() | views::isOnLayer(layer); - }), - [&] (const Entity& entity) { - SDL_SetRenderDrawColor(ren_.get(), entity.colorVal, entity.colorVal, 65, 255); - - SDL_Rect rect { - static_cast(entity.pos.x()), - static_cast(entity.pos.y()), - static_cast(entity.size.w()), - static_cast(entity.size.h()) - }; - - SDL_RenderFillRect(ren_.get(), &rect); - }); - - //SDL_RenderCopy(ren_.get(), canvas.get(), nullptr, nullptr); - SDL_RenderPresent(ren_.get()); -} diff --git a/src/renderer.h b/src/renderer.h index dd3498f..ac20ec2 100644 --- a/src/renderer.h +++ b/src/renderer.h @@ -108,19 +108,4 @@ public: using texture_ptr = std::unique_ptr; -class Renderer { -public: - - Renderer(); - - void render(const Simulation& game); - -private: - - sdl_wrapper sdl_; - img_wrapper img_; - window_ptr win_; - renderer_ptr ren_; -}; - #endif /* end of include guard: RENDERER_H_6A58EC30 */ diff --git a/src/simulation.cpp b/src/simulation.cpp index a43a4e8..317e010 100644 --- a/src/simulation.cpp +++ b/src/simulation.cpp @@ -233,6 +233,49 @@ void Simulation::tick( } } +void Simulation::render(SDL_Renderer* ren) +{ + for (size_t y = 0; y < level_.getSize().h(); y++) + { + for (size_t x = 0; x < level_.getSize().w(); x++) + { + int val = 255 - level_.at(x, y) * 10; + + SDL_SetRenderDrawColor(ren, val, val, val, 255); + + SDL_Rect rect { + static_cast(x * TILE_SIZE.w()), + static_cast(y * TILE_SIZE.h()), + TILE_SIZE.w(), + TILE_SIZE.h() + }; + + SDL_RenderFillRect(ren, &rect); + } + } + + constexpr Layer renderOrder[] = { Layer::track, Layer::object }; + + ranges::for_each( + ranges::view::for_each( + renderOrder, + [&] (Layer layer) { + return entityRange() | views::isOnLayer(layer); + }), + [&] (const Entity& entity) { + SDL_SetRenderDrawColor(ren, entity.colorVal, entity.colorVal, 65, 255); + + SDL_Rect rect { + static_cast(entity.pos.x()), + static_cast(entity.pos.y()), + static_cast(entity.size.w()), + static_cast(entity.size.h()) + }; + + SDL_RenderFillRect(ren, &rect); + }); +} + id_type Simulation::emplaceEntity() { id_type nextId; diff --git a/src/simulation.h b/src/simulation.h index abff3da..1502a70 100644 --- a/src/simulation.h +++ b/src/simulation.h @@ -1,8 +1,8 @@ #ifndef SIMULATION_H_7BF6EEA4 #define SIMULATION_H_7BF6EEA4 +#include "state.h" #include "entity.h" -#include "renderer.h" #include "schedule.h" #include #include @@ -11,7 +11,7 @@ class Level; -class Simulation { +class Simulation : public State { public: // Constructor @@ -19,7 +19,9 @@ public: void tick( double dt, - const Uint8* keystate); + const Uint8* keystate) override; + + void render(SDL_Renderer* ren) override; id_type emplaceEntity(); diff --git a/src/state.h b/src/state.h new file mode 100644 index 0000000..3c3cfb4 --- /dev/null +++ b/src/state.h @@ -0,0 +1,19 @@ +#ifndef STATE_H_86BFEF59 +#define STATE_H_86BFEF59 + +#include "renderer.h" + +class State { +public: + + virtual ~State() = default; + + virtual void tick( + double dt, + const Uint8* keystate) = 0; + + virtual void render(SDL_Renderer* ren) = 0; + +}; + +#endif /* end of include guard: STATE_H_86BFEF59 */ -- cgit 1.4.1