texture manager, going to pull out into a resource manager
This commit is contained in:
@@ -5,6 +5,6 @@ include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake) # Includes the contents of th
|
||||
conan_basic_setup() # Prepares the CMakeList.txt for Conan.
|
||||
|
||||
# $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) # 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) # Specifies the executable to build.
|
||||
target_link_libraries(rla_iipp ${CONAN_LIBS}) # Specifies what libraries to link, using Conan.
|
||||
file(COPY assets DESTINATION ${CMAKE_BINARY_DIR}/bin)
|
||||
@@ -1,4 +1,7 @@
|
||||
[requires]
|
||||
sdl/2.0.16
|
||||
sdl_ttf/2.0.15
|
||||
sdl_image/2.0.5
|
||||
lua/5.4.3
|
||||
[generators]
|
||||
cmake
|
||||
@@ -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;
|
||||
|
||||
|
||||
@@ -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;
|
||||
};
|
||||
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
};
|
||||
|
||||
|
||||
|
||||
40
src/engine/rendering/sdl/sdltexturemanager.cpp
Normal file
40
src/engine/rendering/sdl/sdltexturemanager.cpp
Normal 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);
|
||||
});
|
||||
}
|
||||
27
src/engine/rendering/sdl/sdltexturemanager.hpp
Normal file
27
src/engine/rendering/sdl/sdltexturemanager.hpp
Normal 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
|
||||
@@ -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;
|
||||
|
||||
@@ -16,6 +16,9 @@ public:
|
||||
|
||||
unsigned int get_framerate() override;
|
||||
|
||||
~Visualizer();
|
||||
|
||||
|
||||
private:
|
||||
int x = 0;
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user