Skip to content

Commit 100a308

Browse files
fixed scaling
1 parent 67f8177 commit 100a308

File tree

2 files changed

+87
-24
lines changed

2 files changed

+87
-24
lines changed

example/main.c

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Surface image;
1414
#define HEIGHT 600
1515

1616
static int x;
17+
static int y;
1718

1819
void init(void) {
1920
display = waphics_surface_new(pixels, WIDTH, HEIGHT);
@@ -22,16 +23,21 @@ void init(void) {
2223

2324
uint32_t *render(void) {
2425
waphics_fill_display(display, 0xFF000000);
25-
image = waphics_surface_scale(image, VECTOR2(32, 64));
26-
waphics_draw_image(display, VECTOR2(100, 100), image);
26+
Surface new = waphics_surface_scale(&image, VECTOR2(135+x, 64));
27+
waphics_draw_image(display, VECTOR2(x, y), new);
2728

29+
if (get_key(KEY_D)) {
30+
x++;
31+
}
2832
if (get_key(KEY_A)) {
2933
x--;
3034
}
31-
if (get_key(KEY_D)) {
32-
x++;
35+
if (get_key(KEY_S)) {
36+
y++;
37+
}
38+
if (get_key(KEY_W)) {
39+
y--;
3340
}
34-
35-
return display.pixels;
36-
}
3741

42+
return display.pixels;
43+
}

src/waphics.c

Lines changed: 74 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ typedef struct {
6262
} Circle;
6363

6464
typedef struct {
65-
int x, y;
65+
float x, y;
6666
} Vector2;
6767

6868
typedef struct {
@@ -74,7 +74,7 @@ Rectangle waphics_rectangle_new(int x, int y, int width, int height);
7474
Circle waphics_circle_new(int x, int y, int radius);
7575
Surface waphics_surface_new(uint32_t *pixels, unsigned int width, unsigned int height);
7676
Surface waphics_surface_from_file(const char *filename);
77-
Vector2 waphics_vector2_new(int x, int y);
77+
Vector2 waphics_vector2_new(float x, float y);
7878

7979
void waphics_fill_display(Surface display, uint32_t color);
8080
void waphics_draw_rect(Surface display, Rectangle rect, uint32_t color);
@@ -85,7 +85,7 @@ void waphics_draw_triangle_3(Surface display, Vector2 p1, Vector2 p2, Vector2 p3
8585
void waphics_draw_image(Surface display, Vector2 position, Surface image);
8686
void waphics_draw_image_alpha(Surface display, Rectangle rect,
8787
uint32_t scale, uint32_t *pixels, uint32_t alpha);
88-
Surface waphics_surface_scale(Surface surface, Vector2 size);
88+
Surface waphics_surface_scale(Surface *surface, Vector2 size);
8989
uint32_t waphics_rgba(uint8_t r, uint8_t g, uint8_t b, uint8_t a);
9090
uint32_t waphics_rgb(uint32_t r, uint32_t g, uint32_t b);
9191
int waphics_collide_rect(Rectangle *rect1, Rectangle *rect2);
@@ -125,7 +125,7 @@ Circle waphics_circle_new(int x, int y, int radius) {
125125
return circ;
126126
}
127127

128-
Vector2 waphics_vector2_new(int x, int y) {
128+
Vector2 waphics_vector2_new(float x, float y) {
129129
Vector2 vec;
130130
vec.x = x;
131131
vec.y = y;
@@ -349,8 +349,12 @@ float lerp(uint32_t v0, uint32_t v1, float t) {
349349
void waphics_draw_image(Surface display, Vector2 position, Surface image) {
350350
for (int _y = 0; _y < image.height; _y++) {
351351
for (int _x = 0; _x < image.width; _x++) {
352-
uint32_t pixel = image.pixels[_y * image.width + _x];
353-
display.pixels[(_y+position.y) * display.width + (_x + position.x)] = pixel;
352+
if (_y < image.height && _x < image.width) {
353+
uint32_t pixel = image.pixels[_y * image.width + _x];
354+
if (_y+(int)position.y <= display.height && _x + (int)position.x <= display.width) {
355+
display.pixels[(_y+(int)position.y) * display.width + (_x + (int)position.x)] = pixel;
356+
}
357+
}
354358
}
355359
}
356360
}
@@ -380,17 +384,70 @@ void waphics_draw_image(Surface display, Vector2 position, Surface image) {
380384
// }
381385
// }
382386

383-
Surface waphics_surface_scale(Surface surface, Vector2 size) {
384-
size_t scale_x = size.x / surface.width;
385-
size_t scale_y = size.y / surface.height;
386-
uint32_t pixels[surface.width * scale_x * surface.height * scale_y];
387-
Surface new = waphics_surface_new(pixels, size.x, size.y);
388-
printf("%ld %ld\n", scale_x, scale_y);
389-
for (size_t y = 0; y < surface.height * scale_y; y++) {
390-
for (size_t x = 0; x < surface.width * scale_x; x++) {
391-
uint32_t pixel = surface.pixels[(y/scale_y * surface.width + x/scale_x)];
392-
new.pixels[y * new.width + x] = pixel;
393-
}
387+
Surface waphics_surface_scale(Surface *surface, Vector2 size) {
388+
float size_x = size.x / surface->width;
389+
float size_y = size.y / surface->height;
390+
391+
uint32_t pixels[(int)(surface->width * size_x * surface->height * size_y)];
392+
393+
Surface new = waphics_surface_new(pixels, surface->width * size_x, surface->height * size_y);
394+
waphics_fill_display(new, 0xFFFFFFF);
395+
396+
Vector2 tri_1_p1 = VECTOR2(0, 0);
397+
Vector2 tri_1_p2 = VECTOR2(new.width, 0);
398+
Vector2 tri_1_p3 = VECTOR2(0, new.height);
399+
400+
Vector2 uv_1_p1 = VECTOR2(0, 1);
401+
Vector2 uv_1_p2 = VECTOR2(1, 1);
402+
Vector2 uv_1_p3 = VECTOR2(0, 0);
403+
404+
Vector2 tri_2_p1 = VECTOR2(new.width, 0);
405+
Vector2 tri_2_p2 = VECTOR2(new.width, new.height);
406+
Vector2 tri_2_p3 = VECTOR2(0, new.height);
407+
408+
Vector2 uv_2_p1 = VECTOR2(1, 1);
409+
Vector2 uv_2_p2 = VECTOR2(1, 0);
410+
Vector2 uv_2_p3 = VECTOR2(0, 0);
411+
412+
413+
for (int y = 0; y < new.height; y++) {
414+
for (int x = 0; x < new.width; x++) {
415+
float denom = ((tri_1_p2.y - tri_1_p3.y) * (tri_1_p1.x - tri_1_p3.x) + (tri_1_p3.x - tri_1_p2.x) * (tri_1_p1.y - tri_1_p3.y));
416+
float barya = ((tri_1_p2.y - tri_1_p3.y) * (x - tri_1_p3.x) + (tri_1_p3.x - tri_1_p2.x) * (y - tri_1_p3.y)) / denom;
417+
float baryb = ((tri_1_p3.y - tri_1_p1.y) * (x - tri_1_p3.x) + (tri_1_p1.x - tri_1_p3.x) * (y - tri_1_p3.y)) / denom;
418+
float baryc = 1 - barya - baryb;
419+
420+
if (barya >= 0 && baryb >= 0 && baryc >= 0) {
421+
float u = barya * uv_1_p1.x + baryb * uv_1_p2.x + baryc * uv_1_p3.x;
422+
float v = barya * uv_1_p1.y + baryb * uv_1_p2.y + baryc * uv_1_p3.y;
423+
424+
int px = (int)(u * surface->width);
425+
int py = (int)(surface->height - (v * surface->height));
426+
if (y <= new.height && x <= new.width) {
427+
new.pixels[y * new.width + x] = surface->pixels[py * surface->width + px];
428+
}
429+
}
430+
}
431+
}
432+
433+
for (int y = 0; y < new.height; y++) {
434+
for (int x = 0; x < new.width; x++) {
435+
float denom = ((tri_2_p2.y - tri_2_p3.y) * (tri_2_p1.x - tri_2_p3.x) + (tri_2_p3.x - tri_2_p2.x) * (tri_2_p1.y - tri_2_p3.y));
436+
float barya = ((tri_2_p2.y - tri_2_p3.y) * (x - tri_2_p3.x) + (tri_2_p3.x - tri_2_p2.x) * (y - tri_2_p3.y)) / denom;
437+
float baryb = ((tri_2_p3.y - tri_2_p1.y) * (x - tri_2_p3.x) + (tri_2_p1.x - tri_2_p3.x) * (y - tri_2_p3.y)) / denom;
438+
float baryc = 1 - barya - baryb;
439+
440+
if (barya >= 0 && baryb >= 0 && baryc >= 0) {
441+
float u = barya * uv_2_p1.x + baryb * uv_2_p2.x + baryc * uv_2_p3.x;
442+
float v = barya * uv_2_p1.y + baryb * uv_2_p2.y + baryc * uv_2_p3.y;
443+
444+
int px = (int)(u * surface->width);
445+
int py = (int)(surface->height - (v * surface->height));
446+
if (y <= new.height && x <= new.width) {
447+
new.pixels[y * new.width + x] = surface->pixels[py * surface->width + px];
448+
}
449+
}
450+
}
394451
}
395452

396453
return new;

0 commit comments

Comments
 (0)