-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsprite.cpp
More file actions
33 lines (28 loc) · 1.14 KB
/
Copy pathsprite.cpp
File metadata and controls
33 lines (28 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include "sprite.h"
#include <SDL2/SDL_image.h>
#include <iostream>
#include <sstream>
constexpr char kAssetPath[] = "assets/";
Sprite::Sprite(std::string sprite_sheet_name, SDL_Rect location, float scale,
SDL_Renderer* renderer)
: sprite_sheet_name_(sprite_sheet_name),
source_location_(location),
scale_(scale) {
// Check if we've already opened this sprite sheet.
if (Sprite::sprite_textures_.find(sprite_sheet_name) ==
sprite_textures_.end()) {
std::stringstream file_path;
file_path << kAssetPath << sprite_sheet_name << ".png";
// If the file has never been opened, open it and cache the result.
SDL_Surface* surface = IMG_Load(file_path.str().c_str());
if (!surface) {
std::cout << "Couldn't load image " << sprite_sheet_name
<< ". Error: " << IMG_GetError();
}
// Convert the sprite sheet SDL_Surface to a SDL_Texture for caching.
SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, surface);
CacheTexture(texture);
}
}
std::unordered_map<std::string, SDL_Texture*> Sprite::sprite_textures_ =
std::unordered_map<std::string, SDL_Texture*>();