Merge pull request 'Removes the test alog, cleans up some renderer stuff, starts work on cellular automata, closes #14' (#15) from feature/rla-14_AlgorithmsUI into master

Reviewed-on: #15
This commit was merged in pull request #15.
This commit is contained in:
2023-07-23 14:58:44 +00:00
7 changed files with 32 additions and 24 deletions

View File

@@ -0,0 +1,11 @@
CellularAutomata = {}
function CellularAutomata:initialize(w, h)
end
function CellularAutomata:update()
return true
end
visualizer.algorithm_manager:register_algorithm("Cellular Automata", function (w, h) CellularAutomata:initialize() end, function () return CellularAutomata:update() end, 1)

View File

@@ -1,18 +0,0 @@
Test = {}
function Test.initialize(w, h)
visualizer.map:fill(false)
for mx = 0, w-1 do
for my = 0, h-1 do
if mx == 0 or mx == w-1 or my == 0 or my == h-1 then
visualizer.map:draw_wall(mx, my)
end
end
end
end
function Test.update()
end
visualizer.algorithm_manager:register_algorithm("Test", Test.initialize, Test.update, 1)

View File

@@ -62,6 +62,7 @@ public:
virtual void draw_point(int x, int y, Color color) = 0;
virtual void draw_text(std::optional<TextRenderDetails> details, std::string text,int x, int y)=0;
virtual void draw_sprite(Sprite sprite, int x, int y)=0;
virtual TextRenderDetails get_default_text_render_details()=0;
virtual ~Renderer() = default;
};

View File

@@ -74,7 +74,7 @@ SdlRenderer::SdlRenderer(RendererParams renderer_params) : renderer_params(std::
printf("Failed to initialize font loading: %s\n", TTF_GetError());
exit(EXIT_FAILURE);
}
SDL_Window* sdl_window = SDL_CreateWindow(renderer_params.title.c_str(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, renderer_params.width, renderer_params.height, SDL_WINDOW_SHOWN);
SDL_Window* sdl_window = SDL_CreateWindow(this->renderer_params.title.c_str(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, this->renderer_params.width, this->renderer_params.height, SDL_WINDOW_SHOWN);
if(sdl_window == nullptr) {
printf("error making window: %s\n", SDL_GetError());
exit(EXIT_FAILURE);

View File

@@ -18,15 +18,14 @@ struct SDL_WindowDeleter {
class SdlRenderer : public Renderer {
public:
~SdlRenderer() override;
SdlRenderer(RendererParams renderer_params);
explicit SdlRenderer(RendererParams renderer_params);
void draw_text(std::optional<TextRenderDetails> details, std::string text, int x, int y) override;
void flush() override;
void draw_point(int x, int y, Color color) override;
void draw_sprite(Sprite sprite, int x, int y) override;
TextRenderDetails get_default_text_render_details() override {
return this->renderer_params.default_font;
}
private:
RendererParams renderer_params;
std::unique_ptr<SDL_Window, SDL_WindowDeleter> window;

View File

@@ -25,6 +25,9 @@ public:
ranges::any_view<DungeonAlgorithm*, ranges::category::random_access | ranges::category::sized> get_algorithms();
void load_algorithm(DungeonAlgorithm* algorithm, int w, int h);
void tick_algorithm();
DungeonAlgorithm* get_currently_running_algorithm() {
return algorithm_done ? nullptr : loaded_algorithm;
}
explicit AlgorithmManager(unsigned int targetFps) : target_fps(targetFps) {}
AlgorithmManager() = default;
};

View File

@@ -52,6 +52,18 @@ void Visualizer::render(Renderer *renderer) {
renderer->draw_sprite(character, wx*tile_width, wy*tile_height);
}
}
// Draw keycode to algo mappings
auto default_font_details = renderer->get_default_text_render_details();
auto running_algorithm = this->algorithm_manager.get_currently_running_algorithm();
for(auto pairing : this->keycode_to_algorithm) {
auto index = pairing.first-InputProcessorKeycode::KEY_1;
auto draw_params = default_font_details;
if(pairing.second == running_algorithm) {
draw_params.color = COLOR_RED;
}
renderer->draw_text(draw_params, fmt::format("{}: {}", index+1, pairing.second->name), 0, this->window_height-((index+1)*default_font_details.size));
}
}
void Visualizer::initialize(GameInitArgs args) {