Skip to content
Snippets Groups Projects
Commit 01e0bc85 authored by Tomas Vybiral's avatar Tomas Vybiral
Browse files

added resources and their loading

parent eaf98db5
No related branches found
No related tags found
No related merge requests found
resources/images/cannon.png

2.47 KiB

resources/images/collision.png

1.83 KiB

resources/images/enemy1.png

1.94 KiB

resources/images/enemy2.png

1.92 KiB

resources/images/missile.png

1.74 KiB

......@@ -2,6 +2,8 @@ add_subdirectory(game)
 
add_executable(shooter main.cpp)
 
add_custom_command(TARGET shooter PRE_BUILD COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_SOURCE_DIR}/resources/ $<TARGET_FILE_DIR:shooter>)
target_link_libraries(
shooter
PRIVATE project_options
......
add_library(renderer
abstract_renderer.h abstract_renderer.cpp
sdl_renderer.h sdl_renderer.cpp)
sdl_renderer.h sdl_renderer.cpp
sdl_resources.h sdl_resources.cpp)
 
target_link_libraries(input
PUBLIC
......
......@@ -3,9 +3,12 @@
 
#include "../state/state.h"
 
/// Abstract class that represents generic renderer template that can be used by any framework.
class abstract_renderer{
public:
/// Virtual destructor to prevent memory leaks
virtual ~abstract_renderer() {}
/// Virtual draw method that is called by the game controller.
virtual void render_state(const state& current_state) = 0;
};
 
......
......@@ -4,7 +4,7 @@
 
#include <stdexcept>
 
sdl_renderer::sdl_renderer() {
sdl_renderer::sdl_renderer(): window(nullptr), renderer(nullptr), resources(nullptr) {
if (SDL_Init(SDL_INIT_VIDEO) != 0) {
throw std::runtime_error(SDL_GetError());
}
......@@ -21,8 +21,10 @@ sdl_renderer::sdl_renderer() {
throw std::runtime_error(SDL_GetError());
}
 
resources = new sdl_resources(renderer);
SDL_RenderClear(renderer);
SDL_SetRenderDrawColor(renderer, 100, 100, 100, 255);
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
SDL_Rect screen{0, 0, 640, 480};
SDL_RenderFillRect(renderer, &screen);
SDL_RenderPresent(renderer);
......@@ -33,6 +35,8 @@ sdl_renderer::~sdl_renderer() {
SDL_DestroyRenderer(renderer);
if (window != nullptr)
SDL_DestroyWindow(window);
if (resources != nullptr)
delete resources;
SDL_QuitSubSystem(SDL_INIT_VIDEO);
}
 
......
......@@ -4,6 +4,7 @@
#include <SDL2/SDL.h>
 
#include "abstract_renderer.h"
#include "sdl_resources.h"
 
/// Game renderer class. Using SDL2 to render game
class sdl_renderer: public abstract_renderer {
......@@ -16,9 +17,13 @@ public:
/// Renders passed state to created window
void render_state(const state& current_state) override;
protected:
/// SDL window struct
/// SDL window struct pointer
SDL_Window* window;
/// SDL renderer struct pointer
SDL_Renderer* renderer;
/// Collection of resources used to render.
sdl_resources* resources;
};
 
#endif//SDL_RENDERER_H
\ No newline at end of file
#include "sdl_resources.h"
#include <SDL2/SDL_image.h>
#include <stdexcept>
sdl_resources::sdl_resources(SDL_Renderer* renderer): renderer(renderer) {
if (! (IMG_Init(IMG_INIT_PNG) & IMG_INIT_PNG)) {
throw std::runtime_error(IMG_GetError());
}
shooter = load_texture("images/cannon.png");
enemy = load_texture("images/enemy1.png");
projectile = load_texture("images/missile.png");
}
sdl_resources::~sdl_resources() {
IMG_Quit();
}
SDL_Texture* sdl_resources::load_texture(const std::string& path_to_image) {
SDL_Surface * surface = nullptr;
SDL_Texture* texture = nullptr;
surface = IMG_Load(path_to_image.c_str());
if (surface == nullptr) {
throw std::runtime_error(IMG_GetError());
}
texture = SDL_CreateTextureFromSurface(renderer, surface);
SDL_FreeSurface(surface);
if (texture == nullptr) {
throw std::runtime_error(SDL_GetError());
}
return texture;
}
\ No newline at end of file
#ifndef SDL_RESOURCES_H
#define SDL_RESOURCES_H
#include <SDL2/SDL.h>
#include <string>
/// Resource class that load all the resources for game rendering.
class sdl_resources {
public:
/// Loads resources
explicit sdl_resources(SDL_Renderer* renderer);
/// Unloads resources
~sdl_resources();
/// Shooter texture
const SDL_Texture* shooter;
/// Projectile texture
const SDL_Texture* projectile;
/// Enemy texture
const SDL_Texture* enemy;
protected:
SDL_Renderer* renderer;
/// Loads texture by path defined by path
SDL_Texture* load_texture(const std::string& path_to_image);
};
#endif//SDL_RESOURCES_H
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment