Renderers no longer have an init function and are expected to be prepared in their constructor (whatever it is)
We can do something similar for input processor (probably a good idea?) or go with the plan for game and use a template, engine can know about the renderers as long as it treats them as generic pointers, but shouldn't know about the game Game can either be a templated make_unique or we can just enforce a game class naming Or pass game in at start loop
This commit is contained in:
@@ -18,8 +18,6 @@ void Engine::start_loop() {
|
|||||||
if(renderer == nullptr) {
|
if(renderer == nullptr) {
|
||||||
throw MissingRendererException();
|
throw MissingRendererException();
|
||||||
}
|
}
|
||||||
auto render_params = RendererParams{this->config.window_title, this->config.window_width, this->config.window_height, this->config.default_font};
|
|
||||||
renderer->initialize(render_params);
|
|
||||||
renderer->flush();
|
renderer->flush();
|
||||||
auto game_params = GameInitArgs{this->config.target_fps, this->config.window_width, this->config.window_height};
|
auto game_params = GameInitArgs{this->config.target_fps, this->config.window_width, this->config.window_height};
|
||||||
game->initialize(game_params);
|
game->initialize(game_params);
|
||||||
@@ -86,8 +84,9 @@ void Engine::parse_config(const std::string& config_file_path) {
|
|||||||
|
|
||||||
void Engine::setup_renderer(RendererTypes rendering_engine) {
|
void Engine::setup_renderer(RendererTypes rendering_engine) {
|
||||||
std::unique_ptr<Renderer> rendering_engine_ptr;
|
std::unique_ptr<Renderer> rendering_engine_ptr;
|
||||||
|
auto render_params = RendererParams{this->config.window_title, this->config.window_width, this->config.window_height, this->config.default_font};
|
||||||
switch(rendering_engine) {
|
switch(rendering_engine) {
|
||||||
case SDL_HARDWARE: rendering_engine_ptr = std::make_unique<SdlRenderer>(); break;
|
case SDL_HARDWARE: rendering_engine_ptr = std::make_unique<SdlRenderer>(render_params); break;
|
||||||
default: throw RLEngineException("Invalid renderer specified");
|
default: throw RLEngineException("Invalid renderer specified");
|
||||||
}
|
}
|
||||||
this->renderer = std::move(rendering_engine_ptr);
|
this->renderer = std::move(rendering_engine_ptr);
|
||||||
|
|||||||
@@ -58,13 +58,12 @@ struct RendererParams {
|
|||||||
|
|
||||||
class Renderer {
|
class Renderer {
|
||||||
public:
|
public:
|
||||||
virtual void initialize(RendererParams params)=0;
|
|
||||||
virtual void flush() = 0;
|
virtual void flush() = 0;
|
||||||
virtual void draw_point(int x, int y, Color color) = 0;
|
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_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 void draw_sprite(Sprite sprite, int x, int y)=0;
|
||||||
|
|
||||||
virtual ~Renderer() = default;;
|
virtual ~Renderer() = default;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif //RLA_IIPP_RENDERER_HPP
|
#endif //RLA_IIPP_RENDERER_HPP
|
||||||
|
|||||||
@@ -2,40 +2,9 @@
|
|||||||
// Created by m on 12/3/21.
|
// Created by m on 12/3/21.
|
||||||
//
|
//
|
||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
|
#include <utility>
|
||||||
#include "sdlrenderer.hpp"
|
#include "sdlrenderer.hpp"
|
||||||
|
|
||||||
void SdlRenderer::initialize(RendererParams params) {
|
|
||||||
if(SDL_Init(SDL_INIT_VIDEO) < 0) {
|
|
||||||
printf("SDL vide oinit failed: %s\n", SDL_GetError());
|
|
||||||
exit(EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
auto img_flags = IMG_INIT_PNG;
|
|
||||||
if(!(IMG_Init(img_flags) & img_flags)) {
|
|
||||||
printf("Failed to init image loading: %s\n", IMG_GetError());
|
|
||||||
exit(EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
if(TTF_Init() < 0) {
|
|
||||||
printf("Failed to initialize font loading: %s\n", TTF_GetError());
|
|
||||||
exit(EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
SDL_Window* sdl_window = SDL_CreateWindow(params.title.c_str(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, params.width, params.height, SDL_WINDOW_SHOWN);
|
|
||||||
if(sdl_window == nullptr) {
|
|
||||||
printf("error making window: %s\n", SDL_GetError());
|
|
||||||
exit(EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
this->window = std::unique_ptr<SDL_Window, SDL_WindowDeleter>(sdl_window);
|
|
||||||
SDL_Renderer* sdl_renderer = SDL_CreateRenderer(this->window.get(), -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
|
|
||||||
this->renderer = std::shared_ptr<SDL_Renderer>(sdl_renderer, SDL_DestroyRenderer);
|
|
||||||
|
|
||||||
this->texture_manager = std::make_unique<SdlTextureManager>(this->renderer);
|
|
||||||
this->font_manager = std::make_unique<SdlFontManager>();
|
|
||||||
this->renderer_params = params;
|
|
||||||
|
|
||||||
SDL_RenderClear(this->renderer.get());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void SdlRenderer::flush() {
|
void SdlRenderer::flush() {
|
||||||
SDL_RenderPresent(this->renderer.get());
|
SDL_RenderPresent(this->renderer.get());
|
||||||
SDL_SetRenderDrawColor(this->renderer.get(), COLOR_BLACK.r, COLOR_BLACK.g, COLOR_BLACK.b, COLOR_BLACK.a);
|
SDL_SetRenderDrawColor(this->renderer.get(), COLOR_BLACK.r, COLOR_BLACK.g, COLOR_BLACK.b, COLOR_BLACK.a);
|
||||||
@@ -90,3 +59,34 @@ void SdlRenderer::draw_sprite(Sprite sprite, int x, int y) {
|
|||||||
auto source = SDL_Rect{src_x, src_y, sprite_sheet->sprite_width, sprite_sheet->sprite_height};
|
auto source = SDL_Rect{src_x, src_y, sprite_sheet->sprite_width, sprite_sheet->sprite_height};
|
||||||
render_texture(x, y, texture, &source);
|
render_texture(x, y, texture, &source);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
SdlRenderer::SdlRenderer(RendererParams renderer_params) : renderer_params(std::move(renderer_params)) {
|
||||||
|
if(SDL_Init(SDL_INIT_VIDEO) < 0) {
|
||||||
|
printf("SDL vide oinit failed: %s\n", SDL_GetError());
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
auto img_flags = IMG_INIT_PNG;
|
||||||
|
if(!(IMG_Init(img_flags) & img_flags)) {
|
||||||
|
printf("Failed to init image loading: %s\n", IMG_GetError());
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
if(TTF_Init() < 0) {
|
||||||
|
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);
|
||||||
|
if(sdl_window == nullptr) {
|
||||||
|
printf("error making window: %s\n", SDL_GetError());
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
this->window = std::unique_ptr<SDL_Window, SDL_WindowDeleter>(sdl_window);
|
||||||
|
SDL_Renderer* sdl_renderer = SDL_CreateRenderer(this->window.get(), -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
|
||||||
|
this->renderer = std::shared_ptr<SDL_Renderer>(sdl_renderer, SDL_DestroyRenderer);
|
||||||
|
|
||||||
|
this->texture_manager = std::make_unique<SdlTextureManager>(this->renderer);
|
||||||
|
this->font_manager = std::make_unique<SdlFontManager>();
|
||||||
|
|
||||||
|
SDL_RenderClear(this->renderer.get());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -18,8 +18,10 @@ struct SDL_WindowDeleter {
|
|||||||
class SdlRenderer : public Renderer {
|
class SdlRenderer : public Renderer {
|
||||||
public:
|
public:
|
||||||
~SdlRenderer() override;
|
~SdlRenderer() override;
|
||||||
|
|
||||||
|
SdlRenderer(RendererParams renderer_params);
|
||||||
|
|
||||||
void draw_text(std::optional<TextRenderDetails> details, std::string text, int x, int y) override;
|
void draw_text(std::optional<TextRenderDetails> details, std::string text, int x, int y) override;
|
||||||
void initialize(RendererParams params) override;
|
|
||||||
void flush() override;
|
void flush() override;
|
||||||
void draw_point(int x, int y, Color color) override;
|
void draw_point(int x, int y, Color color) override;
|
||||||
|
|
||||||
|
|||||||
@@ -7,8 +7,6 @@
|
|||||||
#include "engine/input/sdlinputprocessor.hpp"
|
#include "engine/input/sdlinputprocessor.hpp"
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
|
|
||||||
std::unique_ptr<Renderer> renderer = std::make_unique<SdlRenderer>();
|
|
||||||
std::unique_ptr<Game> game = std::make_unique<Visualizer>();
|
std::unique_ptr<Game> game = std::make_unique<Visualizer>();
|
||||||
std::unique_ptr<InputProcessor> input_processor = std::make_unique<SdlInputProcessor>();
|
std::unique_ptr<InputProcessor> input_processor = std::make_unique<SdlInputProcessor>();
|
||||||
auto engine = std::make_unique<Engine>(std::move(game), std::move(input_processor));
|
auto engine = std::make_unique<Engine>(std::move(game), std::move(input_processor));
|
||||||
|
|||||||
Reference in New Issue
Block a user