-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathebinaryimageloader.cpp
40 lines (34 loc) · 1.12 KB
/
ebinaryimageloader.cpp
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
34
35
36
37
38
39
40
#include "ebinaryimageloader.h"
#include <fstream>
#include "binary.h"
#include "egamedir.h"
std::shared_ptr<eTexture> eBinaryImageLoader::load(SDL_Renderer* const r,
const std::string& path) {
const auto it = eBinaryDataMap.find(path);
if(it == eBinaryDataMap.end()) {
printf("Could not find '%s' image\n", path.c_str());
return nullptr;
}
const auto bp = eGameDir::binaryPath();
std::ifstream file(bp, std::ios::in | std::ios::binary);
if(!file) {
printf("Could not open '%s'\n", bp.c_str());
return nullptr;
}
const auto& bd = it->second;
const auto data = new char[bd.fSize];
file.seekg(bd.fPos);
file.read(data, bd.fSize);
file.close();
const auto rw = SDL_RWFromMem(data, bd.fSize);
const auto surf = IMG_Load_RW(rw, SDL_FALSE);
if(!surf) {
printf("Unable to load image %s! SDL_image Error: %s\n",
path.c_str(), IMG_GetError());
return nullptr;
}
delete[] data;
const auto tex = std::make_shared<eTexture>();
tex->load(r, surf);
return tex;
}