texture manager, going to pull out into a resource manager

This commit is contained in:
2021-12-04 09:09:59 -05:00
parent 7b2a0719b1
commit 3473e3dab7
12 changed files with 127 additions and 10 deletions

View File

@@ -4,10 +4,8 @@
#include "engine.hpp"
#include <cstdio>
#include <memory>
#include <chrono>
#include <thread>
#include <SDL.h>
using namespace std::chrono_literals;
void Engine::start_loop() {
const auto one_second_milli = std::chrono::milliseconds(1s);
@@ -55,3 +53,5 @@ Engine::render_fps(std::chrono::time_point<std::chrono::system_clock> end,
return end;
}
Engine::~Engine() = default;

View File

@@ -9,14 +9,18 @@
#include "game/game.hpp"
#include "rendering/renderer.hpp"
static const bool SHOW_FPS = true;
static const std::string RESOURCE_DIR = "assets";
class Engine {
public:
Engine(std::unique_ptr<Game> game, std::unique_ptr<Renderer> renderer);
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();
private:
std::unique_ptr<Game> game;
std::unique_ptr<Renderer> renderer;
std::unique_ptr<Game> game = nullptr;
std::unique_ptr<Renderer> renderer = nullptr;
};

View File

@@ -4,6 +4,7 @@
#ifndef RLA_IIPP_RENDERER_HPP
#define RLA_IIPP_RENDERER_HPP
#include <string>
struct Color {
public:
@@ -16,7 +17,7 @@ public:
};
struct RendererParams {
char* title;
std::string title;
unsigned int width;
unsigned int height;
};
@@ -26,6 +27,7 @@ public:
virtual void initialize(RendererParams params)=0;
virtual void flush() = 0;
virtual void draw_point(int x, int y, Color color) = 0;
virtual void draw_text(std::string text,int x, int y)=0;
};
#endif //RLA_IIPP_RENDERER_HPP

View File

@@ -7,15 +7,25 @@
void SdlRenderer::initialize(RendererParams params) {
if(SDL_Init(SDL_INIT_VIDEO) < 0) {
printf("SDL vide oinit failed: %s\n", SDL_GetError());
return;
}
SDL_Window* sdl_window = SDL_CreateWindow(params.title, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, params.width, params.height, SDL_WINDOW_SHOWN);
auto img_flags = IMG_INIT_PNG;
if(!(IMG_Init(img_flags) & img_flags)) {
printf("Failed to init image loading: %s", IMG_GetError());
return;
}
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());
return;
}
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->texture_manager = std::make_unique<SdlTextureManager>();
SDL_RenderClear(this->renderer);
}
}
@@ -30,3 +40,19 @@ 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);
}
void SdlRenderer::draw_text(std::string text, int x, int y) {
}
SdlRenderer::~SdlRenderer() {
// destroy components
SDL_DestroyRenderer(this->renderer);
this->renderer = nullptr;
SDL_DestroyWindow(this->window);
this->window = nullptr;
// exit subsystems
IMG_Quit();
SDL_Quit();
}

View File

@@ -5,7 +5,10 @@
#ifndef RLA_IIPP_SDLRENDERER_HPP
#define RLA_IIPP_SDLRENDERER_HPP
#include "../renderer.hpp"
#include "sdltexturemanager.hpp"
#include <SDL.h>
#include <SDL_ttf.h>
#include <SDL_image.h>
class SdlRenderer : public Renderer {
void initialize(RendererParams params) override;
@@ -13,9 +16,17 @@ class SdlRenderer : public Renderer {
void flush() override;
void draw_point(int x, int y, Color color) override;
public:
~SdlRenderer();
public:
void draw_text(std::string text, int x, int y) override;
private:
SDL_Window* window = nullptr;
SDL_Renderer* renderer = nullptr;
std::unique_ptr<SdlTextureManager> texture_manager = nullptr;
};

View File

@@ -0,0 +1,40 @@
//
// Created by m on 12/3/21.
//
#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* surface = IMG_Load(path.c_str());
if(surface == nullptr) {
printf("failed ot load image: %s\n", IMG_GetError());
}
return surface;
}
SdlTextureManager::~SdlTextureManager() {
std::for_each(this->texture_map.begin(), this->texture_map.end(), [](const std::pair<std::string, SDL_Texture *>& pair) {
SDL_DestroyTexture(pair.second);
});
}

View File

@@ -0,0 +1,27 @@
//
// Created by m on 12/3/21.
//
#ifndef RLA_IIPP_SDLTEXTUREMANAGER_HPP
#define RLA_IIPP_SDLTEXTUREMANAGER_HPP
#include <SDL.h>
#include <string>
#include <unordered_map>
#include "../../engine.hpp"
#include <SDL_image.h>
#include <algorithm>
class SdlTextureManager {
public:
SDL_Texture *load_texture(const std::string &path, SDL_Renderer *renderer);
SdlTextureManager();
virtual ~SdlTextureManager();
private:
std::unordered_map<std::string, SDL_Texture*> texture_map;
static SDL_Surface* load_surface(const std::string& path);
};
#endif //RLA_IIPP_SDLTEXTUREMANAGER_HPP

View File

@@ -17,7 +17,7 @@ bool Visualizer::update() {
}
RendererParams Visualizer::get_renderer_params() {
return {.title = TITLE, .width = WIN_WIDTH, .height = WIN_HEIGHT};
return {TITLE, WIN_WIDTH, WIN_HEIGHT};
}
unsigned int Visualizer::get_framerate() {
@@ -30,3 +30,5 @@ void Visualizer::render(Renderer *renderer) {
renderer->draw_point(this->x, y, color);
}
}
Visualizer::~Visualizer() = default;

View File

@@ -16,6 +16,9 @@ public:
unsigned int get_framerate() override;
~Visualizer();
private:
int x = 0;

View File

@@ -6,11 +6,10 @@
#include "game/visualizer.hpp"
int main() {
const char* TITLE = "Test";
std::unique_ptr<Renderer> renderer = std::make_unique<SdlRenderer>();
std::unique_ptr<Game> game = std::make_unique<Visualizer>();
Engine* engine = new Engine(std::move(game), std::move(renderer));
auto* engine = new Engine(std::move(game), std::move(renderer));
engine->start_loop();
return 0;