sprites and ttf work

This commit is contained in:
2021-12-05 22:24:10 -05:00
parent 3ba9364e75
commit 26b341ee63
17 changed files with 244 additions and 57 deletions

View File

@@ -4,7 +4,9 @@ project(rla_iipp) # Defines the project name.
include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake) # Includes the contents of the conanbuildinfo.cmake file. include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake) # Includes the contents of the conanbuildinfo.cmake file.
conan_basic_setup() # Prepares the CMakeList.txt for Conan. conan_basic_setup() # Prepares the CMakeList.txt for Conan.
set(CMAKE_CXX_STANDARD 20) # c++ 20
# $source_files is a space-delimited list of filenames. # $source_files is a space-delimited list of filenames.
add_executable(rla_iipp src/main.cpp src/engine/engine.hpp src/engine/game/game.hpp src/engine/rendering/renderer.hpp src/game/visualizer.hpp src/game/visualizer.cpp src/engine/rendering/sdl/sdlrenderer.cpp src/engine/rendering/sdl/sdlrenderer.hpp src/engine/rendering/sdl/sdlrenderer.cpp src/engine/rendering/sdl/sdlrenderer.hpp src/engine/engine.cpp src/engine/engine.hpp src/engine/rendering/sdl/sdltexturemanager.cpp src/engine/rendering/sdl/sdltexturemanager.hpp) # Specifies the executable to build. add_executable(rla_iipp src/main.cpp src/engine/engine.hpp src/engine/game/game.hpp src/engine/rendering/renderer.hpp src/game/visualizer.hpp src/game/visualizer.cpp src/engine/rendering/sdl/sdlrenderer.cpp src/engine/rendering/sdl/sdlrenderer.hpp src/engine/rendering/sdl/sdlrenderer.cpp src/engine/rendering/sdl/sdlrenderer.hpp src/engine/engine.cpp src/engine/engine.hpp src/engine/rendering/sdl/sdltexturemanager.cpp src/engine/rendering/sdl/sdltexturemanager.hpp src/engine/resources/resourcemanager.hpp src/engine/rendering/sdl/sdlfontmanager.cpp src/engine/rendering/sdl/sdlfontmanager.hpp) # Specifies the executable to build.
target_link_libraries(rla_iipp ${CONAN_LIBS}) # Specifies what libraries to link, using Conan. target_link_libraries(rla_iipp ${CONAN_LIBS}) # Specifies what libraries to link, using Conan.
file(COPY assets DESTINATION ${CMAKE_BINARY_DIR}/bin) file(COPY assets DESTINATION ${CMAKE_BINARY_DIR}/bin)

Binary file not shown.

BIN
assets/fonts/lazy.ttf Executable file

Binary file not shown.

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.4 MiB

View File

@@ -2,6 +2,6 @@
sdl/2.0.16 sdl/2.0.16
sdl_ttf/2.0.15 sdl_ttf/2.0.15
sdl_image/2.0.5 sdl_image/2.0.5
lua/5.4.3 fmt/8.0.1
[generators] [generators]
cmake cmake

View File

@@ -6,17 +6,16 @@
#include <cstdio> #include <cstdio>
#include <chrono> #include <chrono>
#include <thread> #include <thread>
#include <lua.hpp> #include <exception>
using namespace std::chrono_literals; using namespace std::chrono_literals;
void Engine::start_loop() { void Engine::start_loop() {
const auto one_second_milli = std::chrono::milliseconds(1s); const auto one_second_milli = std::chrono::milliseconds(1s);
if(game == nullptr) { if(game == nullptr) {
printf("game is null\n"); throw MissingGameException();
return;
} }
if(renderer == nullptr) { if(renderer == nullptr) {
printf("renderer is null\n"); throw MissingRendererException();
return;
} }
RendererParams renderParams = game->get_renderer_params(); RendererParams renderParams = game->get_renderer_params();
renderer->initialize(renderParams); renderer->initialize(renderParams);
@@ -49,10 +48,18 @@ Engine::render_fps(std::chrono::time_point<std::chrono::system_clock> end,
double frames = 1; double frames = 1;
std::chrono::duration<double> dur = end - lastframe; std::chrono::duration<double> dur = end - lastframe;
if(SHOW_FPS) { if(SHOW_FPS) {
printf("fps: %f\n", frames/dur.count()); //printf("fps: %f\n", frames/dur.count());
this->renderer->draw_text(DEFAULT_FONT, fmt::format("{:.2f}", frames/dur.count()), 0, 0);
} }
return end; return end;
} }
Engine::~Engine() = default; Engine::~Engine() = default;
const char *MissingGameException::what() const noexcept {
return MISSING_GAME.c_str();
}
const char *MissingRendererException::what() const noexcept {
return MISSING_RENDERER.c_str();
}

View File

@@ -4,12 +4,21 @@
#ifndef RLA_IIPP_ENGINE_HPP #ifndef RLA_IIPP_ENGINE_HPP
#define RLA_IIPP_ENGINE_HPP #define RLA_IIPP_ENGINE_HPP
#define FMT_HEADER_ONLY
#include <memory> #include <memory>
#include <chrono> #include <chrono>
#include "game/game.hpp" #include "game/game.hpp"
#include <fmt/format.h>
#include "rendering/renderer.hpp" #include "rendering/renderer.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 bool SHOW_FPS = true;
static const std::string RESOURCE_DIR = "assets"; static const std::string RESOURCE_DIR = "assets";
static const TextRenderDetails DEFAULT_FONT = TextRenderDetails(
"fonts/Consolas-Regular.ttf",
12,
COLOR_WHITE
);
class Engine { class Engine {
public: public:
Engine(std::unique_ptr<Game> game, std::unique_ptr<Renderer> renderer); Engine(std::unique_ptr<Game> game, std::unique_ptr<Renderer> renderer);
@@ -23,5 +32,15 @@ private:
std::unique_ptr<Renderer> renderer = nullptr; std::unique_ptr<Renderer> renderer = nullptr;
}; };
class MissingGameException : public std::exception {
public:
const char *what() const _GLIBCXX_TXN_SAFE_DYN _GLIBCXX_NOTHROW override;
};
class MissingRendererException : public std::exception {
public:
const char *what() const _GLIBCXX_TXN_SAFE_DYN _GLIBCXX_NOTHROW override;
};
#endif //RLA_IIPP_ENGINE_HPP #endif //RLA_IIPP_ENGINE_HPP

View File

@@ -5,6 +5,18 @@
#ifndef RLA_IIPP_RENDERER_HPP #ifndef RLA_IIPP_RENDERER_HPP
#define RLA_IIPP_RENDERER_HPP #define RLA_IIPP_RENDERER_HPP
#include <string> #include <string>
#include <memory>
struct SpriteSheet {
std::string path;
int sprite_width;
int sprite_height;
};
struct Sprite {
SpriteSheet *sprite_sheet;
int index;
};
struct Color { struct Color {
public: public:
@@ -15,11 +27,22 @@ public:
Color(unsigned char r, unsigned char g, unsigned char b, unsigned char a) : r(r), g(g), b(b), a(a) {} 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 TextRenderDetails {
std::string font_path;
int size;
Color color;
TextRenderDetails(const std::string &fontPath, int size, const Color &color) : font_path(
fontPath), size(size), color(color) {}
};
struct RendererParams { struct RendererParams {
std::string title; std::string title;
unsigned int width; int width;
unsigned int height; int height;
}; };
class Renderer { class Renderer {
@@ -27,7 +50,8 @@ public:
virtual void initialize(RendererParams params)=0; 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::string text,int x, int y)=0; virtual void draw_text(TextRenderDetails details, std::string text,int x, int y)=0;
virtual void draw_sprite(Sprite sprite, int x, int y)=0;
}; };
#endif //RLA_IIPP_RENDERER_HPP #endif //RLA_IIPP_RENDERER_HPP

View File

@@ -0,0 +1,19 @@
//
// Created by m on 12/5/21.
//
#include "sdlfontmanager.hpp"
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::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;
}

View File

@@ -0,0 +1,28 @@
//
// Created by m on 12/5/21.
//
#ifndef RLA_IIPP_SDLFONTMANAGER_HPP
#define RLA_IIPP_SDLFONTMANAGER_HPP
#include <string>
#include <utility>
#include <SDL_ttf.h>
#include "../../resources/resourcemanager.hpp"
struct SdlFontArgs {
int size;
std::string font_path;
SdlFontArgs(int size, std::string fontPath) : size(size), font_path(std::move(fontPath)) {}
};
class SdlFontManager : public ResourceManager<TTF_Font, std::string, SdlFontArgs> {
std::string get_key(SdlFontArgs args) override;
TTF_Font *load_resource(SdlFontArgs args) override;
};
#endif //RLA_IIPP_SDLFONTMANAGER_HPP

View File

@@ -11,7 +11,11 @@ void SdlRenderer::initialize(RendererParams params) {
} }
auto img_flags = IMG_INIT_PNG; auto img_flags = IMG_INIT_PNG;
if(!(IMG_Init(img_flags) & img_flags)) { if(!(IMG_Init(img_flags) & img_flags)) {
printf("Failed to init image loading: %s", IMG_GetError()); 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); 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); SDL_Window* sdl_window = SDL_CreateWindow(params.title.c_str(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, params.width, params.height, SDL_WINDOW_SHOWN);
@@ -24,7 +28,9 @@ void SdlRenderer::initialize(RendererParams params) {
SDL_Renderer* sdl_renderer = SDL_CreateRenderer(this->window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC); SDL_Renderer* sdl_renderer = SDL_CreateRenderer(this->window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
this->renderer = sdl_renderer; this->renderer = sdl_renderer;
this->texture_manager = std::make_unique<SdlTextureManager>(); 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);
} }
@@ -32,7 +38,7 @@ void SdlRenderer::initialize(RendererParams params) {
void SdlRenderer::flush() { void SdlRenderer::flush() {
SDL_RenderPresent(this->renderer); SDL_RenderPresent(this->renderer);
SDL_SetRenderDrawColor(this->renderer, 0, 0, 0, 255); SDL_SetRenderDrawColor(this->renderer, COLOR_BLACK.r, COLOR_BLACK.g, COLOR_BLACK.b, COLOR_BLACK.a);
SDL_RenderClear(this->renderer); SDL_RenderClear(this->renderer);
} }
@@ -41,8 +47,10 @@ void SdlRenderer::draw_point(int x, int y, Color color) {
SDL_RenderDrawPoint(this->renderer, x, y); SDL_RenderDrawPoint(this->renderer, x, y);
} }
void SdlRenderer::draw_text(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));
SDL_Texture *texture = this->texture_manager->load_text_as_texture(text, font, details.color);
render_texture(x, y, texture, nullptr);
} }
SdlRenderer::~SdlRenderer() { SdlRenderer::~SdlRenderer() {
@@ -54,5 +62,37 @@ SdlRenderer::~SdlRenderer() {
// exit subsystems // exit subsystems
IMG_Quit(); IMG_Quit();
TTF_Quit();
SDL_Quit(); SDL_Quit();
} }
void SdlRenderer::render_texture(int x, int y, SDL_Texture *texture, SDL_Rect *src) {
int w, h;
if(src == nullptr) {
SDL_QueryTexture(texture, 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);
}
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);
int w, h;
SDL_QueryTexture(texture, 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;
auto source = SDL_Rect{src_x, src_y, sprite_sheet->sprite_width, sprite_sheet->sprite_height};
render_texture(x, y, texture, &source);
}

View File

@@ -6,27 +6,29 @@
#define RLA_IIPP_SDLRENDERER_HPP #define RLA_IIPP_SDLRENDERER_HPP
#include "../renderer.hpp" #include "../renderer.hpp"
#include "sdltexturemanager.hpp" #include "sdltexturemanager.hpp"
#include "sdlfontmanager.hpp"
#include <SDL.h> #include <SDL.h>
#include <SDL_ttf.h> #include <SDL_ttf.h>
#include <SDL_image.h> #include <SDL_image.h>
class SdlRenderer : public Renderer { class SdlRenderer : public Renderer {
void initialize(RendererParams params) override;
void flush() override;
void draw_point(int x, int y, Color color) override;
public: public:
~SdlRenderer(); ~SdlRenderer();
void draw_text(TextRenderDetails details, std::string text, int x, int y) override;
void initialize(RendererParams params) override;
void flush() override;
void draw_point(int x, int y, Color color) override;
public: void draw_sprite(Sprite sprite, int x, int y) override;
void draw_text(std::string text, int x, int y) override;
private: private:
RendererParams renderer_params;
SDL_Window* window = nullptr; SDL_Window* window = nullptr;
SDL_Renderer* renderer = nullptr; SDL_Renderer* renderer = nullptr;
std::unique_ptr<SdlTextureManager> texture_manager = nullptr; 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);
}; };

View File

@@ -4,27 +4,6 @@
#include "sdltexturemanager.hpp" #include "sdltexturemanager.hpp"
SdlTextureManager::SdlTextureManager() {
this->texture_map = std::unordered_map<std::string, SDL_Texture*>();
}
SDL_Texture *SdlTextureManager::load_texture(const std::string &path, SDL_Renderer *renderer) {
std::string full_path = RESOURCE_DIR + "/" + path;
if(this->texture_map.find(full_path) == this->texture_map.end()) {
SDL_Surface* surface = load_surface(full_path);
if(surface == nullptr) {
return nullptr;
}
SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, surface);
if(texture == nullptr) {
printf("failed ot load image into texture: %s\n", IMG_GetError());
}
this->texture_map[path] = texture;
SDL_FreeSurface(surface);
}
return this->texture_map[path];
}
SDL_Surface *SdlTextureManager::load_surface(const std::string& path) { SDL_Surface *SdlTextureManager::load_surface(const std::string& path) {
SDL_Surface* surface = IMG_Load(path.c_str()); SDL_Surface* surface = IMG_Load(path.c_str());
if(surface == nullptr) { if(surface == nullptr) {
@@ -34,7 +13,32 @@ SDL_Surface *SdlTextureManager::load_surface(const std::string& path) {
} }
SdlTextureManager::~SdlTextureManager() { SdlTextureManager::~SdlTextureManager() {
std::for_each(this->texture_map.begin(), this->texture_map.end(), [](const std::pair<std::string, SDL_Texture *>& pair) { std::for_each(this->resource_map.begin(), this->resource_map.end(), [](const std::pair<std::string, SDL_Texture *>& pair) {
SDL_DestroyTexture(pair.second); SDL_DestroyTexture(pair.second);
}); });
} }
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) {
return nullptr;
}
SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, surface);
SDL_FreeSurface(surface);
return texture;
}
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});
if(surface == nullptr) {
return nullptr;
}
SDL_Texture *texture = SDL_CreateTextureFromSurface(this->renderer, surface);
SDL_FreeSurface(surface);
return texture;
}

View File

@@ -8,18 +8,24 @@
#include <string> #include <string>
#include <unordered_map> #include <unordered_map>
#include "../../engine.hpp" #include "../../engine.hpp"
#include "../../resources/resourcemanager.hpp"
#include <SDL_image.h> #include <SDL_image.h>
#include <algorithm> #include <algorithm>
#include <SDL_ttf.h>
class SdlTextureManager : public ResourceManager<SDL_Texture, std::string, std::string> {
protected:
SDL_Texture *load_resource(std::string args) override;
std::string get_key(std::string args) override;
class SdlTextureManager {
public: public:
SDL_Texture *load_texture(const std::string &path, SDL_Renderer *renderer); explicit SdlTextureManager(SDL_Renderer *renderer) : renderer(renderer) {}
SdlTextureManager(); SDL_Texture *load_text_as_texture(const std::string& text, TTF_Font *font, Color color);
~SdlTextureManager();
virtual ~SdlTextureManager();
private: private:
std::unordered_map<std::string, SDL_Texture*> texture_map; SDL_Renderer *renderer = nullptr;
static SDL_Surface* load_surface(const std::string& path); static SDL_Surface* load_surface(const std::string& path);
}; };

View File

@@ -0,0 +1,32 @@
//
// Created by m on 12/4/21.
//
#ifndef RLA_IIPP_RESOURCEMANAGER_HPP
#define RLA_IIPP_RESOURCEMANAGER_HPP
#include <unordered_map>
#include "../engine.hpp"
template<typename T, typename K, typename A>
class ResourceManager {
public:
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);
this->resource_map[key] = resource;
}
return this->resource_map[key];
}
protected:
std::unordered_map<K, T*> resource_map;
virtual K get_key(A args)=0;
virtual T* load_resource(A args)=0;
std::string get_resource_path(std::string path) {
return fmt::format("{}/{}", RESOURCE_DIR, path);
}
};
#endif //RLA_IIPP_RESOURCEMANAGER_HPP

View File

@@ -3,10 +3,6 @@
// //
#include "visualizer.hpp" #include "visualizer.hpp"
#define TITLE "test"
#define WIN_WIDTH 800
#define WIN_HEIGHT 600
#define TARGET_FPS 60
bool Visualizer::update() { bool Visualizer::update() {
this->x += 1; this->x += 1;
if(this->x > WIN_WIDTH) { if(this->x > WIN_WIDTH) {
@@ -25,7 +21,11 @@ unsigned int Visualizer::get_framerate() {
} }
void Visualizer::render(Renderer *renderer) { void Visualizer::render(Renderer *renderer) {
Color color = Color(255, 255, 255, 255); Color color = COLOR_WHITE;
SpriteSheet sheet = SpriteSheet{"sprites/character.bmp", 48, 48};
auto sprite = Sprite{&sheet, 1};
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++) { for(int y=0;y<WIN_HEIGHT;y++) {
renderer->draw_point(this->x, y, color); renderer->draw_point(this->x, y, color);
} }

View File

@@ -2,9 +2,13 @@
// Created by m on 12/3/21. // Created by m on 12/3/21.
// //
#include "../engine/game/game.hpp" #include "../engine/game/game.hpp"
#include "../engine/rendering/renderer.hpp"
#ifndef RLA_IIPP_VISUALIZER_HPP #ifndef RLA_IIPP_VISUALIZER_HPP
#define RLA_IIPP_VISUALIZER_HPP #define RLA_IIPP_VISUALIZER_HPP
#define TITLE "test"
#define WIN_WIDTH 800
#define WIN_HEIGHT 600
#define TARGET_FPS 61
class Visualizer : public Game { class Visualizer : public Game {
public: public: