-
Notifications
You must be signed in to change notification settings - Fork 0
/
LibdragonImage.cpp
197 lines (160 loc) · 5.01 KB
/
LibdragonImage.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#include "LibdragonImage.h"
#include <fstream>
#include "json.hpp"
#include "imgui/imgui.h"
#include "imgui/imgui_custom.h"
std::string get_libdragon_image_type_name(LibdragonImageType type) {
switch (type) {
case IMAGE_UNKNOWN:
return "unknown";
case IMAGE_PNG:
return "png";
case IMAGE_BMP:
return "bmp";
case IMAGE_JPG:
return "jpg";
case IMAGE_JPEG:
return "jpeg";
case IMAGE_PCX:
return "pcx";
case IMAGE_TGA:
return "tga";
}
return "none";
}
std::string get_libdragon_image_type_extension(LibdragonImageType type) {
switch (type) {
case IMAGE_UNKNOWN:
return ".unknown";
case IMAGE_PNG:
return ".png";
case IMAGE_BMP:
return ".bmp";
case IMAGE_JPG:
return ".jpg";
case IMAGE_JPEG:
return ".jpeg";
case IMAGE_PCX:
return ".pcx";
case IMAGE_TGA:
return ".tga";
}
return ".none";
}
LibdragonImage::LibdragonImage()
: dfs_folder("/"),
h_slices(1),
v_slices(1),
width(0),
height(0),
display_width(0),
display_height(0),
type(IMAGE_PNG),
loaded_image(nullptr) {
}
LibdragonImage::~LibdragonImage() {
if (loaded_image) {
SDL_DestroyTexture(loaded_image);
loaded_image = nullptr;
}
if (loaded_image_overlay) {
SDL_DestroyTexture(loaded_image_overlay);
loaded_image_overlay = nullptr;
}
}
void LibdragonImage::SaveToDisk(const std::string &project_directory) {
nlohmann::json json = {
{"name", name}, {"image_path", image_path}, {"dfs_folder", dfs_folder},
{"h_slices", h_slices}, {"v_slices", v_slices}, {"type", type},
};
std::string directory = project_directory + "/.ngine/sprites/";
if (!std::filesystem::exists(directory))
std::filesystem::create_directories(directory);
std::string filepath = directory + name + ".sprite.json";
std::ofstream filestream(filepath);
filestream << json.dump(4) << std::endl;
filestream.close();
}
void LibdragonImage::LoadFromDisk(const std::string &filepath) {
nlohmann::json json;
std::ifstream filestream(filepath);
filestream >> json;
filestream.close();
name = json["name"];
image_path = json["image_path"];
dfs_folder = json["dfs_folder"];
h_slices = json["h_slices"];
v_slices = json["v_slices"];
if (!json["type"].is_null())
type = json["type"];
std::replace(dfs_folder.begin(), dfs_folder.end(), '\\', '/');
}
void LibdragonImage::DeleteFromDisk(const std::string &project_directory) const {
std::string json_filepath = project_directory + "/.ngine/sprites/" + name + ".sprite.json";
std::filesystem::remove(json_filepath);
std::string image_filepath = project_directory + "/" + image_path;
std::filesystem::remove(image_filepath);
}
void LibdragonImage::LoadImage(const std::string &project_directory, SDL_Renderer *renderer) {
std::string path(project_directory + "/" + image_path);
loaded_image = IMG_LoadTexture(renderer, path.c_str());
int w, h;
SDL_QueryTexture(loaded_image, nullptr, nullptr, &w, &h);
width = w;
height = h;
loaded_image_overlay = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA32,
SDL_TEXTUREACCESS_TARGET, w * 2, h * 2);
SDL_SetTextureBlendMode(loaded_image_overlay, SDL_BLENDMODE_BLEND);
const float max_size = 130.f;
if (w > h) {
h = (int)(((float)h / (float)w) * max_size);
w = (int)max_size;
} else {
w = (int)(((float)w / (float)h) * max_size);
h = (int)max_size;
}
display_width = w;
display_height = h;
RecreateOverlay(renderer, h_slices, v_slices);
}
void LibdragonImage::RecreateOverlay(SDL_Renderer *renderer, int overlay_h_slices, int overlay_v_slices) {
SDL_SetRenderTarget(renderer, loaded_image_overlay);
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0);
SDL_RenderClear(renderer);
SDL_SetRenderDrawColor(renderer, 0, 255, 0, 255);
int total_h_slices = overlay_h_slices - 1;
if (total_h_slices > 0) {
float steps = ((float)width * 2) / (float)overlay_h_slices;
for (int h = 1; h <= total_h_slices; ++h) {
SDL_RenderDrawLine(renderer, steps * h, 0, steps * h, (height * 2));
}
}
int total_v_slices = overlay_v_slices - 1;
if (total_v_slices > 0) {
float steps = ((float)height * 2) / (float)overlay_v_slices;
for (int v = 1; v <= total_v_slices; ++v) {
SDL_RenderDrawLine(renderer, 0, steps * v, (width * 2), steps * v);
}
}
SDL_Rect rect = {0, 0, width * 2, height * 2};
SDL_RenderDrawRect(renderer, &rect);
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
SDL_SetRenderTarget(renderer, nullptr);
}
void LibdragonImage::DrawTooltip() const {
std::stringstream tooltip;
tooltip << "Path: " << image_path << "\nDFS_Path: " << dfs_folder << name
<< ".sprite\nSize: " << width << "x" << height << "\nSlices: " << h_slices << "x"
<< v_slices << "\n";
ImGui::BeginTooltip();
render_badge("sprite", ImVec4(.4f, .8f, .4f, 0.7f));
ImGui::SameLine();
render_badge(get_libdragon_image_type_name(type).c_str(), ImVec4(.4f, .8f, .4f, 0.7f));
ImGui::SameLine();
ImGui::Text("%s", name.c_str());
ImGui::Separator();
ImGui::Text("%s", tooltip.str().c_str());
ImGui::Separator();
ImGui::Image((ImTextureID)(intptr_t)loaded_image, ImVec2((float)width, (float)height));
ImGui::EndTooltip();
}