switched up some pointers to shared, added some more rendering example
This commit is contained in:
BIN
assets/sprites/map0.bmp
Normal file
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
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
BIN
assets/sprites/map2.bmp
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 5.4 MiB |
@@ -3,5 +3,7 @@ sdl/2.0.16
|
|||||||
sdl_ttf/2.0.15
|
sdl_ttf/2.0.15
|
||||||
sdl_image/2.0.5
|
sdl_image/2.0.5
|
||||||
fmt/8.0.1
|
fmt/8.0.1
|
||||||
|
tomlplusplus/2.5.0
|
||||||
|
sol2/3.2.3
|
||||||
[generators]
|
[generators]
|
||||||
cmake
|
cmake
|
||||||
@@ -25,7 +25,7 @@ public:
|
|||||||
Engine(std::unique_ptr<Game> game, std::unique_ptr<Renderer> renderer,
|
Engine(std::unique_ptr<Game> game, std::unique_ptr<Renderer> renderer,
|
||||||
std::unique_ptr<InputProcessor> input_processor);
|
std::unique_ptr<InputProcessor> input_processor);
|
||||||
void start_loop();
|
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();
|
virtual ~Engine();
|
||||||
|
|
||||||
@@ -33,6 +33,7 @@ private:
|
|||||||
std::unique_ptr<Game> game = nullptr;
|
std::unique_ptr<Game> game = nullptr;
|
||||||
std::unique_ptr<Renderer> renderer = nullptr;
|
std::unique_ptr<Renderer> renderer = nullptr;
|
||||||
std::unique_ptr<InputProcessor> input_processor = 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 {
|
class MissingGameException : public std::exception {
|
||||||
|
|||||||
@@ -290,6 +290,5 @@ InputProcessorKeycode SdlInputProcessor::get_keycode(SDL_Keycode keycode) {
|
|||||||
if(this->keycode_map.find(keycode) == this->keycode_map.end()) {
|
if(this->keycode_map.find(keycode) == this->keycode_map.end()) {
|
||||||
return KEY_UNKNOWN;
|
return KEY_UNKNOWN;
|
||||||
}
|
}
|
||||||
printf("Detected keypress for %i\n", this->keycode_map[keycode]);
|
|
||||||
return this->keycode_map[keycode];
|
return this->keycode_map[keycode];
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,12 +8,12 @@ std::string SdlFontManager::get_key(SdlFontArgs args) {
|
|||||||
return fmt::format("{}_{}", args.size, get_resource_path(args.font_path));
|
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);
|
std::string path = get_resource_path(args.font_path);
|
||||||
TTF_Font *font = TTF_OpenFont(path.c_str(), args.size);
|
TTF_Font *font = TTF_OpenFont(path.c_str(), args.size);
|
||||||
if(font == nullptr) {
|
if(font == nullptr) {
|
||||||
printf("Could not load font: %s\n", TTF_GetError());
|
printf("Could not load font: %s\n", TTF_GetError());
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
return font;
|
return std::shared_ptr<TTF_Font>(font, TTF_CloseFont);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,10 +18,10 @@ struct SdlFontArgs {
|
|||||||
SdlFontArgs(int size, std::string fontPath) : size(size), font_path(std::move(fontPath)) {}
|
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;
|
std::string get_key(SdlFontArgs args) override;
|
||||||
|
|
||||||
TTF_Font *load_resource(SdlFontArgs args) override;
|
std::shared_ptr<TTF_Font> load_resource(SdlFontArgs args) override;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -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) {
|
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));
|
std::shared_ptr<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<SDL_Texture> texture = this->texture_manager->load_text_as_texture(text, font, details.color);
|
||||||
render_texture(x, y, texture, nullptr);
|
render_texture(x, y, texture, nullptr);
|
||||||
SDL_DestroyTexture(texture);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
SdlRenderer::~SdlRenderer() {
|
SdlRenderer::~SdlRenderer() {
|
||||||
@@ -67,30 +66,24 @@ SdlRenderer::~SdlRenderer() {
|
|||||||
SDL_Quit();
|
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;
|
int w, h;
|
||||||
if(src == nullptr) {
|
if(src == nullptr) {
|
||||||
SDL_QueryTexture(texture, nullptr, nullptr, &w, &h);
|
SDL_QueryTexture(texture.get(), nullptr, nullptr, &w, &h);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
w = src->w;
|
w = src->w;
|
||||||
h = src->h;
|
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_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) {
|
void SdlRenderer::draw_sprite(Sprite sprite, int x, int y) {
|
||||||
SpriteSheet *sprite_sheet = sprite.sprite_sheet;
|
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;
|
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 l = (sprite_sheet->sprite_width * sprite.index) / w;
|
||||||
int src_y = l*sprite_sheet->sprite_height;
|
int src_y = l*sprite_sheet->sprite_height;
|
||||||
int src_x = (sprite_sheet->sprite_width * sprite.index) % w;
|
int src_x = (sprite_sheet->sprite_width * sprite.index) % w;
|
||||||
|
|||||||
@@ -28,7 +28,7 @@ private:
|
|||||||
std::unique_ptr<SdlTextureManager> texture_manager = nullptr;
|
std::unique_ptr<SdlTextureManager> texture_manager = nullptr;
|
||||||
std::unique_ptr<SdlFontManager> font_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);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -12,13 +12,7 @@ SDL_Surface *SdlTextureManager::load_surface(const std::string& path) {
|
|||||||
return surface;
|
return surface;
|
||||||
}
|
}
|
||||||
|
|
||||||
SdlTextureManager::~SdlTextureManager() {
|
std::shared_ptr<SDL_Texture> SdlTextureManager::load_resource(std::string args) {
|
||||||
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::string full_path = get_resource_path(args);
|
std::string full_path = get_resource_path(args);
|
||||||
SDL_Surface* surface = load_surface(full_path);
|
SDL_Surface* surface = load_surface(full_path);
|
||||||
if(surface == nullptr) {
|
if(surface == nullptr) {
|
||||||
@@ -26,19 +20,19 @@ SDL_Texture *SdlTextureManager::load_resource(std::string args) {
|
|||||||
}
|
}
|
||||||
SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, surface);
|
SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, surface);
|
||||||
SDL_FreeSurface(surface);
|
SDL_FreeSurface(surface);
|
||||||
return texture;
|
return std::shared_ptr<SDL_Texture>(texture, SDL_DestroyTexture);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string SdlTextureManager::get_key(std::string args) {
|
std::string SdlTextureManager::get_key(std::string args) {
|
||||||
return get_resource_path(args);
|
return get_resource_path(args);
|
||||||
}
|
}
|
||||||
|
|
||||||
SDL_Texture *SdlTextureManager::load_text_as_texture(const std::string& text, TTF_Font *font, Color color) {
|
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, text.c_str(), {color.r, color.g, color.b});
|
SDL_Surface *surface = TTF_RenderText_Solid(font.get(), text.c_str(), {color.r, color.g, color.b});
|
||||||
if(surface == nullptr) {
|
if(surface == nullptr) {
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
SDL_Texture *texture = SDL_CreateTextureFromSurface(this->renderer, surface);
|
SDL_Texture *texture = SDL_CreateTextureFromSurface(this->renderer, surface);
|
||||||
SDL_FreeSurface(surface);
|
SDL_FreeSurface(surface);
|
||||||
return texture;
|
return std::shared_ptr<SDL_Texture>(texture, SDL_DestroyTexture);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,15 +13,14 @@
|
|||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <SDL_ttf.h>
|
#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:
|
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;
|
std::string get_key(std::string args) override;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
explicit SdlTextureManager(SDL_Renderer *renderer) : renderer(renderer) {}
|
explicit SdlTextureManager(SDL_Renderer *renderer) : renderer(renderer) {}
|
||||||
SDL_Texture *load_text_as_texture(const std::string& text, TTF_Font *font, Color color);
|
std::shared_ptr<SDL_Texture> load_text_as_texture(const std::string& text, const std::shared_ptr<TTF_Font>& font, Color color);
|
||||||
~SdlTextureManager();
|
|
||||||
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|||||||
@@ -10,19 +10,19 @@
|
|||||||
template<typename T, typename K, typename A>
|
template<typename T, typename K, typename A>
|
||||||
class ResourceManager {
|
class ResourceManager {
|
||||||
public:
|
public:
|
||||||
virtual T* fetch_resource(A args) {
|
virtual T fetch_resource(A args) {
|
||||||
K key = get_key(args);
|
K key = get_key(args);
|
||||||
if(this->resource_map.find(key) == this->resource_map.end()) {
|
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;
|
this->resource_map[key] = resource;
|
||||||
}
|
}
|
||||||
return this->resource_map[key];
|
return this->resource_map[key];
|
||||||
}
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
std::unordered_map<K, T*> resource_map;
|
std::unordered_map<K, T> resource_map;
|
||||||
virtual K get_key(A args)=0;
|
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) {
|
std::string get_resource_path(std::string path) {
|
||||||
return fmt::format("{}/{}", RESOURCE_DIR, path);
|
return fmt::format("{}/{}", RESOURCE_DIR, path);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,9 +4,7 @@
|
|||||||
|
|
||||||
#include "visualizer.hpp"
|
#include "visualizer.hpp"
|
||||||
bool Visualizer::update(InputResult input) {
|
bool Visualizer::update(InputResult input) {
|
||||||
this->x += 1;
|
if(input.keycode == KEY_ESCAPE) {
|
||||||
if(this->x > WIN_WIDTH || input.keycode == KEY_ESCAPE) {
|
|
||||||
this->x = 0;
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if(input.keycode == KEY_RIGHT) {
|
if(input.keycode == KEY_RIGHT) {
|
||||||
@@ -25,13 +23,22 @@ unsigned int Visualizer::get_framerate() {
|
|||||||
|
|
||||||
void Visualizer::render(Renderer *renderer) {
|
void Visualizer::render(Renderer *renderer) {
|
||||||
Color color = COLOR_WHITE;
|
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();
|
RendererParams window_params = get_renderer_params();
|
||||||
renderer->draw_sprite(sprite, window_params.width/2, window_params.height/2);
|
SpriteSheet tiles = SpriteSheet{"sprites/map1.bmp", 48, 48};
|
||||||
for(int y=0;y<WIN_HEIGHT;y++) {
|
auto grass = Sprite{&tiles, 0};
|
||||||
renderer->draw_point(this->x, y, color);
|
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;
|
Visualizer::~Visualizer() = default;
|
||||||
|
|||||||
Reference in New Issue
Block a user