-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomponent.cpp
More file actions
53 lines (42 loc) · 1.28 KB
/
component.cpp
File metadata and controls
53 lines (42 loc) · 1.28 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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include "component.h"
#include "stb_image.h"
SpriteRenderer* SpriteRenderer::SetSprite(std::string path, int x_, int y_) {
x = x_;
y = y_;
unsigned char* data = stbi_load(path.c_str(), &w, &h, nullptr, 4);
if (!data) return this;
pixels.assign(data, data + (w * h * 4));
stbi_image_free(data);
sw = 1.0f;
sh = 1.0f;
return this;
}
SpriteRenderer::SpriteRenderer(std::string path, int x_, int y_) {
x = x_;
y = y_;
unsigned char* data = stbi_load(path.c_str(), &w, &h, &c, 4);
this->pixels.assign(data, data + w * h * 4);
stbi_image_free(data);
}
SpriteRenderer* SpriteRenderer::SetSpriteScale(float sw_, float sh_){
this->sw = sw_;
this->sh = sh_;
return this;
}
static void ScaleImageNN(
const unsigned char* src, int srcW, int srcH,
unsigned char* dst, int dstW, int dstH)
{
for (int y = 0; y < dstH; y++) {
int sy = (y * srcH) / dstH;
for (int x = 0; x < dstW; x++) {
int sx = (x * srcW) / dstW;
const unsigned char* sp = src + 4 * (sy * srcW + sx);
unsigned char* dp = dst + 4 * (y * dstW + x);
dp[0] = sp[0];
dp[1] = sp[1];
dp[2] = sp[2];
dp[3] = sp[3];
}
}
}