color keying for sprite sheets and properly keyed texture map params
This commit is contained in:
Binary file not shown.
|
Before Width: | Height: | Size: 5.4 MiB After Width: | Height: | Size: 5.4 MiB |
Binary file not shown.
|
Before Width: | Height: | Size: 5.4 MiB After Width: | Height: | Size: 5.4 MiB |
Binary file not shown.
|
Before Width: | Height: | Size: 5.4 MiB After Width: | Height: | Size: 5.4 MiB |
6
config/engine.toml
Normal file
6
config/engine.toml
Normal file
@@ -0,0 +1,6 @@
|
||||
[display]
|
||||
target_fps = 61
|
||||
show_fps = true
|
||||
window_width = 800
|
||||
window_height = 600
|
||||
window_title = "Roguelike Algorithm Visualizer"
|
||||
@@ -17,10 +17,12 @@ void Engine::start_loop() {
|
||||
if(renderer == nullptr) {
|
||||
throw MissingRendererException();
|
||||
}
|
||||
RendererParams renderParams = game->get_renderer_params();
|
||||
renderer->initialize(renderParams);
|
||||
auto render_params = RendererParams{this->config.window_title, this->config.window_width, this->config.window_height};
|
||||
renderer->initialize(render_params);
|
||||
renderer->flush();
|
||||
auto duration_per_frame = one_second_milli / game->get_framerate();
|
||||
auto game_params = GameInitArgs{this->config.target_fps, this->config.window_width, this->config.window_height};
|
||||
game->initialize(game_params);
|
||||
auto duration_per_frame = one_second_milli / this->config.target_fps;
|
||||
auto last_frame = std::chrono::high_resolution_clock::now();
|
||||
while(true) {
|
||||
auto starttime = std::chrono::high_resolution_clock::now();
|
||||
@@ -30,9 +32,9 @@ void Engine::start_loop() {
|
||||
break;
|
||||
}
|
||||
game->render(renderer.get());
|
||||
renderer->flush();
|
||||
auto endtime = std::chrono::high_resolution_clock::now();
|
||||
last_frame = render_fps(endtime, last_frame);
|
||||
renderer->flush(); // not technically correct to do this here, but is done so we can put the fps over the display
|
||||
|
||||
std::this_thread::sleep_for(duration_per_frame);
|
||||
}
|
||||
@@ -43,6 +45,16 @@ Engine::Engine(std::unique_ptr<Game> game, std::unique_ptr<Renderer> renderer,
|
||||
this->renderer = std::move(renderer);
|
||||
this->game = std::move(game);
|
||||
this->input_processor = std::move(input_processor);
|
||||
EngineConfig new_config = EngineConfig();
|
||||
load_config("engine.toml", [&new_config](const toml::table& table) {
|
||||
new_config.show_fps = table["display"]["show_fps"].value_or<bool>(DEFAULT_SHOW_FPS);
|
||||
new_config.window_width = table["display"]["window_width"].value_or(DEFAULT_WINDOW_WIDTH);
|
||||
new_config.window_height = table["display"]["window_height"].value_or(DEFAULT_WINDOW_HEIGHT);
|
||||
new_config.target_fps = table["display"]["target_fps"].value_or(DEFAULT_TARGET_FPS);
|
||||
new_config.window_title = table["display"]["window_title"].value_or(DEFAULT_WINDOW_TITLE);
|
||||
|
||||
});
|
||||
this->config = new_config;
|
||||
}
|
||||
|
||||
std::chrono::time_point<std::chrono::system_clock>
|
||||
@@ -50,19 +62,26 @@ Engine::render_fps(std::chrono::time_point<std::chrono::system_clock> end,
|
||||
std::chrono::time_point<std::chrono::system_clock> lastframe) {
|
||||
double frames = 1;
|
||||
std::chrono::duration<double> dur = end - lastframe;
|
||||
if(SHOW_FPS) {
|
||||
if(this->config.show_fps) {
|
||||
//printf("fps: %f\n", frames/dur.count());
|
||||
this->renderer->draw_text(DEFAULT_FONT, fmt::format("{:.2f}", frames/dur.count()), 0, 0);
|
||||
}
|
||||
return end;
|
||||
}
|
||||
|
||||
void Engine::load_config(const std::string &path, const std::function<void (toml::table)>& f) {
|
||||
std::string full_path = fmt::format("{}/{}", CONFIG_DIR, path);
|
||||
auto table = toml::parse_file(full_path);
|
||||
f(table);
|
||||
}
|
||||
|
||||
|
||||
Engine::~Engine() = default;
|
||||
|
||||
const char *MissingGameException::what() const noexcept {
|
||||
return MISSING_GAME.c_str();
|
||||
return MISSING_GAME_ERROR;
|
||||
}
|
||||
|
||||
const char *MissingRendererException::what() const noexcept {
|
||||
return MISSING_RENDERER.c_str();
|
||||
return MISSING_RENDERER_ERROR;
|
||||
}
|
||||
|
||||
@@ -9,27 +9,46 @@
|
||||
#include <chrono>
|
||||
#include "game/game.hpp"
|
||||
#include <fmt/format.h>
|
||||
#include <toml.hpp>
|
||||
#include <functional>
|
||||
#include "rendering/renderer.hpp"
|
||||
#include "input/inputprocessor.hpp"
|
||||
static const std::string MISSING_GAME = "Game pointer is null";
|
||||
static const std::string MISSING_RENDERER = "Renderer pointer is null";
|
||||
static const bool SHOW_FPS = true;
|
||||
static const std::string RESOURCE_DIR = "assets";
|
||||
#define MISSING_GAME_ERROR "Game pointer is null"
|
||||
#define MISSING_RENDERER_ERROR "Renderer pointer is null"
|
||||
#define RESOURCE_DIR "assets"
|
||||
#define CONFIG_DIR "config"
|
||||
#define DEFAULT_TARGET_FPS 61
|
||||
#define DEFAULT_WINDOW_WIDTH 800
|
||||
#define DEFAULT_WINDOW_HEIGHT 600
|
||||
#define DEFAULT_WINDOW_TITLE "RLEngine"
|
||||
#define DEFAULT_SHOW_FPS false
|
||||
|
||||
static const TextRenderDetails DEFAULT_FONT = TextRenderDetails(
|
||||
"fonts/Consolas-Regular.ttf",
|
||||
12,
|
||||
COLOR_WHITE
|
||||
);
|
||||
|
||||
struct EngineConfig {
|
||||
unsigned int target_fps;
|
||||
int window_width;
|
||||
int window_height;
|
||||
bool show_fps;
|
||||
std::string window_title;
|
||||
TextRenderDetails default_font;
|
||||
};
|
||||
|
||||
class Engine {
|
||||
public:
|
||||
Engine(std::unique_ptr<Game> game, std::unique_ptr<Renderer> renderer,
|
||||
std::unique_ptr<InputProcessor> input_processor);
|
||||
void start_loop();
|
||||
|
||||
static void load_config(const std::string& path, const std::function<void (toml::table)>& f);
|
||||
|
||||
virtual ~Engine();
|
||||
|
||||
private:
|
||||
EngineConfig config;
|
||||
std::unique_ptr<Game> game = nullptr;
|
||||
std::unique_ptr<Renderer> renderer = nullptr;
|
||||
std::unique_ptr<InputProcessor> input_processor = nullptr;
|
||||
|
||||
@@ -6,12 +6,18 @@
|
||||
#define RLA_IIPP_GAME_HPP
|
||||
#include "../rendering/renderer.hpp"
|
||||
#include "../input/inputprocessor.hpp"
|
||||
|
||||
struct GameInitArgs {
|
||||
unsigned int target_fps;
|
||||
int window_width;
|
||||
int window_height;
|
||||
};
|
||||
|
||||
class Game {
|
||||
public:
|
||||
virtual bool update(InputResult input) = 0;
|
||||
virtual void render(Renderer* renderer) = 0;
|
||||
virtual RendererParams get_renderer_params() = 0;
|
||||
virtual unsigned int get_framerate() = 0;
|
||||
virtual void initialize(GameInitArgs args)=0;
|
||||
};
|
||||
|
||||
#endif //RLA_IIPP_GAME_HPP
|
||||
|
||||
@@ -6,17 +6,8 @@
|
||||
#define RLA_IIPP_RENDERER_HPP
|
||||
#include <string>
|
||||
#include <memory>
|
||||
|
||||
struct SpriteSheet {
|
||||
std::string path;
|
||||
int sprite_width;
|
||||
int sprite_height;
|
||||
};
|
||||
|
||||
struct Sprite {
|
||||
SpriteSheet *sprite_sheet;
|
||||
int index;
|
||||
};
|
||||
#include <ostream>
|
||||
#include <optional>
|
||||
|
||||
struct Color {
|
||||
public:
|
||||
@@ -25,16 +16,32 @@ public:
|
||||
unsigned char b;
|
||||
unsigned char a;
|
||||
|
||||
Color(){}
|
||||
|
||||
Color(unsigned char r, unsigned char g, unsigned char b, unsigned char a) : r(r), g(g), b(b), a(a) {}
|
||||
};
|
||||
#define COLOR_BLACK Color(0, 0, 0, 255)
|
||||
#define COLOR_WHITE Color(255, 255, 255, 255)
|
||||
|
||||
struct SpriteSheet {
|
||||
std::string path;
|
||||
int sprite_width;
|
||||
int sprite_height;
|
||||
std::optional<Color> color_key;
|
||||
};
|
||||
|
||||
struct Sprite {
|
||||
SpriteSheet *sprite_sheet;
|
||||
int index;
|
||||
};
|
||||
|
||||
struct TextRenderDetails {
|
||||
std::string font_path;
|
||||
int size;
|
||||
Color color;
|
||||
|
||||
TextRenderDetails(){};
|
||||
|
||||
TextRenderDetails(const std::string &fontPath, int size, const Color &color) : font_path(
|
||||
fontPath), size(size), color(color) {}
|
||||
};
|
||||
|
||||
@@ -24,27 +24,27 @@ void SdlRenderer::initialize(RendererParams params) {
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
else {
|
||||
this->window = sdl_window;
|
||||
SDL_Renderer* sdl_renderer = SDL_CreateRenderer(this->window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
|
||||
this->renderer = sdl_renderer;
|
||||
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);
|
||||
SDL_RenderClear(this->renderer.get());
|
||||
}
|
||||
}
|
||||
|
||||
void SdlRenderer::flush() {
|
||||
SDL_RenderPresent(this->renderer);
|
||||
SDL_SetRenderDrawColor(this->renderer, COLOR_BLACK.r, COLOR_BLACK.g, COLOR_BLACK.b, COLOR_BLACK.a);
|
||||
SDL_RenderClear(this->renderer);
|
||||
SDL_RenderPresent(this->renderer.get());
|
||||
SDL_SetRenderDrawColor(this->renderer.get(), COLOR_BLACK.r, COLOR_BLACK.g, COLOR_BLACK.b, COLOR_BLACK.a);
|
||||
SDL_RenderClear(this->renderer.get());
|
||||
}
|
||||
|
||||
void SdlRenderer::draw_point(int x, int y, Color color) {
|
||||
SDL_SetRenderDrawColor(this->renderer, color.r, color.g, color.b, color.a);
|
||||
SDL_RenderDrawPoint(this->renderer, x, y);
|
||||
SDL_SetRenderDrawColor(this->renderer.get(), color.r, color.g, color.b, color.a);
|
||||
SDL_RenderDrawPoint(this->renderer.get(), x, y);
|
||||
}
|
||||
|
||||
void SdlRenderer::draw_text(TextRenderDetails details, std::string text, int x, int y) {
|
||||
@@ -54,12 +54,6 @@ void SdlRenderer::draw_text(TextRenderDetails details, std::string text, int x,
|
||||
}
|
||||
|
||||
SdlRenderer::~SdlRenderer() {
|
||||
// destroy components
|
||||
SDL_DestroyRenderer(this->renderer);
|
||||
this->renderer = nullptr;
|
||||
SDL_DestroyWindow(this->window);
|
||||
this->window = nullptr;
|
||||
|
||||
// exit subsystems
|
||||
IMG_Quit();
|
||||
TTF_Quit();
|
||||
@@ -76,12 +70,13 @@ void SdlRenderer::render_texture(int x, int y, const std::shared_ptr<SDL_Texture
|
||||
h = src->h;
|
||||
}
|
||||
SDL_Rect rect = {x,y,w,h};
|
||||
SDL_RenderCopy(this->renderer, texture.get(), src, &rect);
|
||||
SDL_RenderCopy(this->renderer.get(), texture.get(), src, &rect);
|
||||
}
|
||||
|
||||
void SdlRenderer::draw_sprite(Sprite sprite, int x, int y) {
|
||||
SpriteSheet *sprite_sheet = sprite.sprite_sheet;
|
||||
std::shared_ptr<SDL_Texture> texture = this->texture_manager->fetch_resource(sprite_sheet->path);
|
||||
TextureManagerFetchArgs args = TextureManagerFetchArgs {sprite_sheet->path, sprite_sheet->color_key};
|
||||
std::shared_ptr<SDL_Texture> texture = this->texture_manager->fetch_resource(args);
|
||||
int w, h;
|
||||
SDL_QueryTexture(texture.get(), nullptr, nullptr, &w, &h);
|
||||
int l = (sprite_sheet->sprite_width * sprite.index) / w;
|
||||
|
||||
@@ -11,6 +11,10 @@
|
||||
#include <SDL_ttf.h>
|
||||
#include <SDL_image.h>
|
||||
|
||||
struct SDL_WindowDeleter {
|
||||
void operator()(SDL_Window* window) { SDL_DestroyWindow(window);}
|
||||
};
|
||||
|
||||
class SdlRenderer : public Renderer {
|
||||
public:
|
||||
~SdlRenderer();
|
||||
@@ -23,8 +27,8 @@ public:
|
||||
|
||||
private:
|
||||
RendererParams renderer_params;
|
||||
SDL_Window* window = nullptr;
|
||||
SDL_Renderer* renderer = nullptr;
|
||||
std::unique_ptr<SDL_Window, SDL_WindowDeleter> window;
|
||||
std::shared_ptr<SDL_Renderer> renderer = nullptr;
|
||||
std::unique_ptr<SdlTextureManager> texture_manager = nullptr;
|
||||
std::unique_ptr<SdlFontManager> font_manager = nullptr;
|
||||
|
||||
|
||||
@@ -12,19 +12,28 @@ SDL_Surface *SdlTextureManager::load_surface(const std::string& path) {
|
||||
return surface;
|
||||
}
|
||||
|
||||
std::shared_ptr<SDL_Texture> SdlTextureManager::load_resource(std::string args) {
|
||||
std::string full_path = get_resource_path(args);
|
||||
std::shared_ptr<SDL_Texture> SdlTextureManager::load_resource(TextureManagerFetchArgs args) {
|
||||
std::string full_path = get_resource_path(args.path);
|
||||
SDL_Surface* surface = load_surface(full_path);
|
||||
if(surface == nullptr) {
|
||||
return nullptr;
|
||||
}
|
||||
SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, surface);
|
||||
if(args.color_key.has_value()) {
|
||||
Color color_key = args.color_key.value();
|
||||
SDL_SetColorKey(surface, SDL_TRUE, SDL_MapRGB(surface->format, color_key.r, color_key.g, color_key.b));
|
||||
}
|
||||
SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer.get(), surface);
|
||||
SDL_FreeSurface(surface);
|
||||
return std::shared_ptr<SDL_Texture>(texture, SDL_DestroyTexture);
|
||||
}
|
||||
|
||||
std::string SdlTextureManager::get_key(std::string args) {
|
||||
return get_resource_path(args);
|
||||
std::string SdlTextureManager::get_key(TextureManagerFetchArgs args) {
|
||||
std::string key;
|
||||
if(args.color_key.has_value()) {
|
||||
key += fmt::format("{}_{}_{}_{}_", args.color_key->r, args.color_key->g, args.color_key->b, args.color_key->a);
|
||||
}
|
||||
key += fmt::format("{}", args.path);
|
||||
return key;
|
||||
}
|
||||
|
||||
std::shared_ptr<SDL_Texture> SdlTextureManager::load_text_as_texture(const std::string& text, const std::shared_ptr<TTF_Font>& font, Color color) {
|
||||
@@ -32,7 +41,7 @@ std::shared_ptr<SDL_Texture> SdlTextureManager::load_text_as_texture(const std::
|
||||
if(surface == nullptr) {
|
||||
return nullptr;
|
||||
}
|
||||
SDL_Texture *texture = SDL_CreateTextureFromSurface(this->renderer, surface);
|
||||
SDL_Texture *texture = SDL_CreateTextureFromSurface(this->renderer.get(), surface);
|
||||
SDL_FreeSurface(surface);
|
||||
return std::shared_ptr<SDL_Texture>(texture, SDL_DestroyTexture);
|
||||
}
|
||||
|
||||
@@ -13,18 +13,24 @@
|
||||
#include <algorithm>
|
||||
#include <SDL_ttf.h>
|
||||
|
||||
class SdlTextureManager : public ResourceManager<std::shared_ptr<SDL_Texture>, std::string, std::string> {
|
||||
struct TextureManagerFetchArgs {
|
||||
std::string path;
|
||||
std::optional<Color> color_key;
|
||||
|
||||
};
|
||||
|
||||
class SdlTextureManager : public ResourceManager<std::shared_ptr<SDL_Texture>, std::string, TextureManagerFetchArgs> {
|
||||
protected:
|
||||
std::shared_ptr<SDL_Texture> load_resource(std::string args) override;
|
||||
std::string get_key(std::string args) override;
|
||||
std::shared_ptr<SDL_Texture> load_resource(TextureManagerFetchArgs args) override;
|
||||
std::string get_key(TextureManagerFetchArgs args) override;
|
||||
|
||||
public:
|
||||
explicit SdlTextureManager(SDL_Renderer *renderer) : renderer(renderer) {}
|
||||
explicit SdlTextureManager(std::shared_ptr<SDL_Renderer> renderer) : renderer(renderer) {}
|
||||
std::shared_ptr<SDL_Texture> load_text_as_texture(const std::string& text, const std::shared_ptr<TTF_Font>& font, Color color);
|
||||
|
||||
|
||||
private:
|
||||
SDL_Renderer *renderer = nullptr;
|
||||
std::shared_ptr<SDL_Renderer> renderer = nullptr;
|
||||
static SDL_Surface* load_surface(const std::string& path);
|
||||
};
|
||||
|
||||
|
||||
@@ -3,42 +3,55 @@
|
||||
//
|
||||
|
||||
#include "visualizer.hpp"
|
||||
#define TILE_WIDTH 48
|
||||
#define TILE_HEIGHT 48
|
||||
bool Visualizer::update(InputResult input) {
|
||||
if(input.keycode == KEY_ESCAPE) {
|
||||
return true;
|
||||
}
|
||||
if(input.keycode == KEY_RIGHT) {
|
||||
this->sprite_index += 1;
|
||||
switch(input.keycode) {
|
||||
case KEY_RIGHT: x += 1; break;
|
||||
case KEY_LEFT: x -= 1; break;
|
||||
case KEY_UP: y -= 1; break;
|
||||
case KEY_DOWN: y += 1; break;
|
||||
default: break;
|
||||
}
|
||||
|
||||
this->x = std::clamp(this->x, 0, (int)this->tile_map[0].size()-1);
|
||||
this->y = std::clamp(this->y, 0, (int)this->tile_map.size()-1);
|
||||
return false;
|
||||
}
|
||||
|
||||
RendererParams Visualizer::get_renderer_params() {
|
||||
return {TITLE, WIN_WIDTH, WIN_HEIGHT};
|
||||
}
|
||||
|
||||
unsigned int Visualizer::get_framerate() {
|
||||
return TARGET_FPS;
|
||||
}
|
||||
|
||||
void Visualizer::render(Renderer *renderer) {
|
||||
Color color = COLOR_WHITE;
|
||||
RendererParams window_params = get_renderer_params();
|
||||
SpriteSheet tiles = SpriteSheet{"sprites/map1.bmp", 48, 48};
|
||||
SpriteSheet tiles = SpriteSheet{"sprites/map1.bmp", TILE_WIDTH, TILE_HEIGHT};
|
||||
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);
|
||||
auto wall = Sprite{&tiles, 497};
|
||||
|
||||
for(int ty=0;ty<this->tile_map.size();ty++) {
|
||||
for(int tx=0;tx<tile_map[0].size();tx++) {
|
||||
if(tx == 0 || ty == 0 || ty == this->tile_map.size()-1 || tx == this->tile_map[0].size()-1) {
|
||||
renderer->draw_sprite(wall, tx*TILE_WIDTH, ty*TILE_HEIGHT);
|
||||
}
|
||||
else {
|
||||
renderer->draw_sprite(grass, tx*TILE_WIDTH, ty*TILE_HEIGHT);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
SpriteSheet characters = SpriteSheet{"sprites/character.bmp", 48, 48};
|
||||
SpriteSheet characters = SpriteSheet{"sprites/character.bmp", TILE_WIDTH, TILE_HEIGHT, COLOR_BLACK};
|
||||
auto character = Sprite{&characters, this->sprite_index};
|
||||
|
||||
renderer->draw_sprite(character, window_params.width / 2, window_params.height / 2);
|
||||
renderer->draw_sprite(character, x*TILE_WIDTH, y*TILE_HEIGHT);
|
||||
}
|
||||
|
||||
void Visualizer::initialize(GameInitArgs args) {
|
||||
this->window_width = args.window_width;
|
||||
this->window_height = args.window_height;
|
||||
int tilesx = (window_width / TILE_WIDTH)+1;
|
||||
int tilesy = (window_height / TILE_HEIGHT)+1;
|
||||
this->tile_map = std::vector<std::vector<char>>(tilesy, std::vector<char>(tilesx));
|
||||
}
|
||||
|
||||
Visualizer::~Visualizer() = default;
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
//
|
||||
#include "../engine/game/game.hpp"
|
||||
#include "../engine/rendering/renderer.hpp"
|
||||
#include <vector>
|
||||
#ifndef RLA_IIPP_VISUALIZER_HPP
|
||||
#define RLA_IIPP_VISUALIZER_HPP
|
||||
#define TITLE "test"
|
||||
@@ -16,15 +17,18 @@ public:
|
||||
|
||||
void render(Renderer* renderer) override;
|
||||
|
||||
RendererParams get_renderer_params() override;
|
||||
|
||||
unsigned int get_framerate() override;
|
||||
|
||||
~Visualizer();
|
||||
|
||||
void initialize(GameInitArgs args) override;
|
||||
|
||||
private:
|
||||
int window_width = 0;
|
||||
int window_height = 0;
|
||||
std::vector<std::vector<char>> tile_map;
|
||||
|
||||
int x = 0;
|
||||
int y=0;
|
||||
int sprite_index = 1;
|
||||
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user