Skip to content
Snippets Groups Projects
sdl_resources.cpp 1.02 KiB
Newer Older
#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");
Tomas Vybiral's avatar
Tomas Vybiral committed
    enemy1 = load_texture("images/enemy1.png");
    enemy2 = load_texture("images/enemy2.png");
    enemy3 = load_texture("images/enemy3.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;
}