switched up some pointers to shared, added some more rendering example

This commit is contained in:
2021-12-06 23:38:36 -05:00
parent 994a128c14
commit ea172110ea
14 changed files with 43 additions and 48 deletions

BIN
assets/sprites/map0.bmp Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.4 MiB

BIN
assets/sprites/map1.bmp Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.4 MiB

BIN
assets/sprites/map2.bmp Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.4 MiB

View File

@@ -3,5 +3,7 @@ sdl/2.0.16
sdl_ttf/2.0.15
sdl_image/2.0.5
fmt/8.0.1
tomlplusplus/2.5.0
sol2/3.2.3
[generators]
cmake

View File

@@ -25,7 +25,7 @@ public:
Engine(std::unique_ptr<Game> game, std::unique_ptr<Renderer> renderer,
std::unique_ptr<InputProcessor> input_processor);
void start_loop();
std::chrono::time_point<std::chrono::system_clock> render_fps(std::chrono::time_point<std::chrono::system_clock> end, std::chrono::time_point<std::chrono::system_clock> lastframe);
virtual ~Engine();
@@ -33,6 +33,7 @@ private:
std::unique_ptr<Game> game = nullptr;
std::unique_ptr<Renderer> renderer = nullptr;
std::unique_ptr<InputProcessor> input_processor = nullptr;
std::chrono::time_point<std::chrono::system_clock> render_fps(std::chrono::time_point<std::chrono::system_clock> end, std::chrono::time_point<std::chrono::system_clock> lastframe);
};
class MissingGameException : public std::exception {

View File

@@ -290,6 +290,5 @@ InputProcessorKeycode SdlInputProcessor::get_keycode(SDL_Keycode keycode) {
if(this->keycode_map.find(keycode) == this->keycode_map.end()) {
return KEY_UNKNOWN;
}
printf("Detected keypress for %i\n", this->keycode_map[keycode]);
return this->keycode_map[keycode];
}

View File

@@ -8,12 +8,12 @@ std::string SdlFontManager::get_key(SdlFontArgs args) {
return fmt::format("{}_{}", args.size, get_resource_path(args.font_path));
}
TTF_Font *SdlFontManager::load_resource(SdlFontArgs args) {
std::shared_ptr<TTF_Font> SdlFontManager::load_resource(SdlFontArgs args) {
std::string path = get_resource_path(args.font_path);
TTF_Font *font = TTF_OpenFont(path.c_str(), args.size);
if(font == nullptr) {
printf("Could not load font: %s\n", TTF_GetError());
return nullptr;
}
return font;
return std::shared_ptr<TTF_Font>(font, TTF_CloseFont);
}

View File

@@ -18,10 +18,10 @@ struct SdlFontArgs {
SdlFontArgs(int size, std::string fontPath) : size(size), font_path(std::move(fontPath)) {}
};
class SdlFontManager : public ResourceManager<TTF_Font, std::string, SdlFontArgs> {
class SdlFontManager : public ResourceManager<std::shared_ptr<TTF_Font>, std::string, SdlFontArgs> {
std::string get_key(SdlFontArgs args) override;
TTF_Font *load_resource(SdlFontArgs args) override;
std::shared_ptr<TTF_Font> load_resource(SdlFontArgs args) override;
};

View File

@@ -48,10 +48,9 @@ void SdlRenderer::draw_point(int x, int y, Color color) {
}
void SdlRenderer::draw_text(TextRenderDetails details, std::string text, int x, int y) {
TTF_Font *font = this->font_manager->fetch_resource(SdlFontArgs(details.size, details.font_path));
SDL_Texture *texture = this->texture_manager->load_text_as_texture(text, font, details.color);
std::shared_ptr<TTF_Font> font = this->font_manager->fetch_resource(SdlFontArgs(details.size, details.font_path));
std::shared_ptr<SDL_Texture> texture = this->texture_manager->load_text_as_texture(text, font, details.color);
render_texture(x, y, texture, nullptr);
SDL_DestroyTexture(texture);
}
SdlRenderer::~SdlRenderer() {
@@ -67,30 +66,24 @@ SdlRenderer::~SdlRenderer() {
SDL_Quit();
}
void SdlRenderer::render_texture(int x, int y, SDL_Texture *texture, SDL_Rect *src) {
void SdlRenderer::render_texture(int x, int y, const std::shared_ptr<SDL_Texture>& texture, SDL_Rect *src) {
int w, h;
if(src == nullptr) {
SDL_QueryTexture(texture, nullptr, nullptr, &w, &h);
SDL_QueryTexture(texture.get(), nullptr, nullptr, &w, &h);
}
else {
w = src->w;
h = src->h;
}
if(x+w >= renderer_params.width) {
x = renderer_params.width - w;
}
if(y+h >= renderer_params.height) {
y = renderer_params.height - h;
}
SDL_Rect rect = {x,y,w,h};
SDL_RenderCopy(this->renderer, texture, src, &rect);
SDL_RenderCopy(this->renderer, texture.get(), src, &rect);
}
void SdlRenderer::draw_sprite(Sprite sprite, int x, int y) {
SpriteSheet *sprite_sheet = sprite.sprite_sheet;
SDL_Texture *texture = this->texture_manager->fetch_resource(sprite_sheet->path);
std::shared_ptr<SDL_Texture> texture = this->texture_manager->fetch_resource(sprite_sheet->path);
int w, h;
SDL_QueryTexture(texture, nullptr, nullptr, &w, &h);
SDL_QueryTexture(texture.get(), nullptr, nullptr, &w, &h);
int l = (sprite_sheet->sprite_width * sprite.index) / w;
int src_y = l*sprite_sheet->sprite_height;
int src_x = (sprite_sheet->sprite_width * sprite.index) % w;

View File

@@ -28,7 +28,7 @@ private:
std::unique_ptr<SdlTextureManager> texture_manager = nullptr;
std::unique_ptr<SdlFontManager> font_manager = nullptr;
void render_texture(int x, int y, SDL_Texture *texture, SDL_Rect *src);
void render_texture(int x, int y, const std::shared_ptr<SDL_Texture>& texture, SDL_Rect *src);
};

View File

@@ -12,13 +12,7 @@ SDL_Surface *SdlTextureManager::load_surface(const std::string& path) {
return surface;
}
SdlTextureManager::~SdlTextureManager() {
std::for_each(this->resource_map.begin(), this->resource_map.end(), [](const std::pair<std::string, SDL_Texture *>& pair) {
SDL_DestroyTexture(pair.second);
});
}
SDL_Texture *SdlTextureManager::load_resource(std::string args) {
std::shared_ptr<SDL_Texture> SdlTextureManager::load_resource(std::string args) {
std::string full_path = get_resource_path(args);
SDL_Surface* surface = load_surface(full_path);
if(surface == nullptr) {
@@ -26,19 +20,19 @@ SDL_Texture *SdlTextureManager::load_resource(std::string args) {
}
SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, surface);
SDL_FreeSurface(surface);
return texture;
return std::shared_ptr<SDL_Texture>(texture, SDL_DestroyTexture);
}
std::string SdlTextureManager::get_key(std::string args) {
return get_resource_path(args);
}
SDL_Texture *SdlTextureManager::load_text_as_texture(const std::string& text, TTF_Font *font, Color color) {
SDL_Surface *surface = TTF_RenderText_Solid(font, text.c_str(), {color.r, color.g, color.b});
std::shared_ptr<SDL_Texture> SdlTextureManager::load_text_as_texture(const std::string& text, const std::shared_ptr<TTF_Font>& font, Color color) {
SDL_Surface *surface = TTF_RenderText_Solid(font.get(), text.c_str(), {color.r, color.g, color.b});
if(surface == nullptr) {
return nullptr;
}
SDL_Texture *texture = SDL_CreateTextureFromSurface(this->renderer, surface);
SDL_FreeSurface(surface);
return texture;
return std::shared_ptr<SDL_Texture>(texture, SDL_DestroyTexture);
}

View File

@@ -13,15 +13,14 @@
#include <algorithm>
#include <SDL_ttf.h>
class SdlTextureManager : public ResourceManager<SDL_Texture, std::string, std::string> {
class SdlTextureManager : public ResourceManager<std::shared_ptr<SDL_Texture>, std::string, std::string> {
protected:
SDL_Texture *load_resource(std::string args) override;
std::shared_ptr<SDL_Texture> load_resource(std::string args) override;
std::string get_key(std::string args) override;
public:
explicit SdlTextureManager(SDL_Renderer *renderer) : renderer(renderer) {}
SDL_Texture *load_text_as_texture(const std::string& text, TTF_Font *font, Color color);
~SdlTextureManager();
std::shared_ptr<SDL_Texture> load_text_as_texture(const std::string& text, const std::shared_ptr<TTF_Font>& font, Color color);
private:

View File

@@ -10,19 +10,19 @@
template<typename T, typename K, typename A>
class ResourceManager {
public:
virtual T* fetch_resource(A args) {
virtual T fetch_resource(A args) {
K key = get_key(args);
if(this->resource_map.find(key) == this->resource_map.end()) {
T* resource = this->load_resource(args);
T resource = this->load_resource(args);
this->resource_map[key] = resource;
}
return this->resource_map[key];
}
protected:
std::unordered_map<K, T*> resource_map;
std::unordered_map<K, T> resource_map;
virtual K get_key(A args)=0;
virtual T* load_resource(A args)=0;
virtual T load_resource(A args)=0;
std::string get_resource_path(std::string path) {
return fmt::format("{}/{}", RESOURCE_DIR, path);
}

View File

@@ -4,9 +4,7 @@
#include "visualizer.hpp"
bool Visualizer::update(InputResult input) {
this->x += 1;
if(this->x > WIN_WIDTH || input.keycode == KEY_ESCAPE) {
this->x = 0;
if(input.keycode == KEY_ESCAPE) {
return true;
}
if(input.keycode == KEY_RIGHT) {
@@ -25,13 +23,22 @@ unsigned int Visualizer::get_framerate() {
void Visualizer::render(Renderer *renderer) {
Color color = COLOR_WHITE;
SpriteSheet sheet = SpriteSheet{"sprites/character.bmp", 48, 48};
auto sprite = Sprite{&sheet, this->sprite_index};
RendererParams window_params = get_renderer_params();
renderer->draw_sprite(sprite, window_params.width/2, window_params.height/2);
for(int y=0;y<WIN_HEIGHT;y++) {
renderer->draw_point(this->x, y, color);
SpriteSheet tiles = SpriteSheet{"sprites/map1.bmp", 48, 48};
auto grass = Sprite{&tiles, 0};
int tilesx = (window_params.width / 48)+1;
int tilesy = (window_params.height / 48)+1;
for(int x=0;x<tilesx;x++) {
for(int y=0;y<tilesy;y++) {
renderer->draw_sprite(grass, x*48, y*48);
}
}
SpriteSheet characters = SpriteSheet{"sprites/character.bmp", 48, 48};
auto character = Sprite{&characters, this->sprite_index};
renderer->draw_sprite(character, window_params.width / 2, window_params.height / 2);
}
Visualizer::~Visualizer() = default;