summaryrefslogtreecommitdiff
path: root/src/core/texture.c
diff options
context:
space:
mode:
authorchai <chaifix@163.com>2019-12-23 23:59:02 +0800
committerchai <chaifix@163.com>2019-12-23 23:59:02 +0800
commitd49f3d3f73709a9a7c0bce53aa13ed28a2bd27cb (patch)
tree3fc2cca442106a56fc151c5faffbb24217ca83f5 /src/core/texture.c
parentec111247c614663d8231245a17c314b9b8b4a28c (diff)
*misc
Diffstat (limited to 'src/core/texture.c')
-rw-r--r--src/core/texture.c20
1 files changed, 15 insertions, 5 deletions
diff --git a/src/core/texture.c b/src/core/texture.c
index 33a00c5..bb33bae 100644
--- a/src/core/texture.c
+++ b/src/core/texture.c
@@ -41,17 +41,23 @@ Texture* texture_loadfromfile(const char* path) {
return NULL;
}
fclose(file);
+
int width, height;
Color* pixels = (Color*)stbi_load_from_memory((unsigned char*)buffer, size, &width, &height, NULL, STBI_rgb_alpha);
ssrM_free(buffer);
- if (!pixels) return NULL;
+ if (!pixels)
+ return NULL;
+
+ /*build texture*/
Texture* texture = ssrM_new(Texture);
texture->width = width; texture->height = height;
texture->pixels = ssrM_newvector(Color32, width*height);
texture->filter_mode = FILTERMODE_POINT;
texture->wrap_mode = WRAPMODE_CLAMP;
texture_abgr2c32(pixels, texture->pixels, width*height);
+
ssrM_free(pixels);
+
return texture;
}
@@ -65,6 +71,10 @@ Texture* texture_create(uint width, uint height) {
return tex;
}
+void texture_save(const char* file) {
+
+}
+
static Color32 sampling(Texture* tex, WrapMode wrap_mode, int x, int y) {
if (wrap_mode == WRAPMODE_CLAMP) { /*clamp*/
x = clamp(x, 0, tex->width - 1);
@@ -81,16 +91,16 @@ Color32 texture_sampling(Texture* tex, float x01, float y01) {
FilterMode filter_mode = tex->filter_mode;
WrapMode wrap_mode = tex->wrap_mode;
float x = x01 * tex->width, y = (1 - y01) * tex->height; /*map to texture coordinate*/
- int x_min = floor(x), y_min = floor(y);
- int x_max = ceil(x), y_max = ceil(y);
if (filter_mode == FILTERMODE_POINT) {
- int px = (x_max - x > x - x_min) ? x_min : x_max;
- int py = (y_max - y > y - y_min) ? y_min : y_max;
+ int px = round(x);
+ int py = round(y);
return sampling(tex, wrap_mode, px, py);
}
else if (filter_mode == FILTERMODE_BILINEAR) {
static Color32 tl, tr, bl, br, t, b, out;
+ int x_min = floor(x), y_min = floor(y);
+ int x_max = ceil(x), y_max = ceil(y);
tl = sampling(tex, wrap_mode, x_min, y_min);
tr = sampling(tex, wrap_mode, x_max, y_min);