@@ -62,7 +62,7 @@ typedef struct {
62
62
} Circle ;
63
63
64
64
typedef struct {
65
- int x , y ;
65
+ float x , y ;
66
66
} Vector2 ;
67
67
68
68
typedef struct {
@@ -74,7 +74,7 @@ Rectangle waphics_rectangle_new(int x, int y, int width, int height);
74
74
Circle waphics_circle_new (int x , int y , int radius );
75
75
Surface waphics_surface_new (uint32_t * pixels , unsigned int width , unsigned int height );
76
76
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 );
78
78
79
79
void waphics_fill_display (Surface display , uint32_t color );
80
80
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
85
85
void waphics_draw_image (Surface display , Vector2 position , Surface image );
86
86
void waphics_draw_image_alpha (Surface display , Rectangle rect ,
87
87
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 );
89
89
uint32_t waphics_rgba (uint8_t r , uint8_t g , uint8_t b , uint8_t a );
90
90
uint32_t waphics_rgb (uint32_t r , uint32_t g , uint32_t b );
91
91
int waphics_collide_rect (Rectangle * rect1 , Rectangle * rect2 );
@@ -125,7 +125,7 @@ Circle waphics_circle_new(int x, int y, int radius) {
125
125
return circ ;
126
126
}
127
127
128
- Vector2 waphics_vector2_new (int x , int y ) {
128
+ Vector2 waphics_vector2_new (float x , float y ) {
129
129
Vector2 vec ;
130
130
vec .x = x ;
131
131
vec .y = y ;
@@ -349,8 +349,12 @@ float lerp(uint32_t v0, uint32_t v1, float t) {
349
349
void waphics_draw_image (Surface display , Vector2 position , Surface image ) {
350
350
for (int _y = 0 ; _y < image .height ; _y ++ ) {
351
351
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
+ }
354
358
}
355
359
}
356
360
}
@@ -380,17 +384,70 @@ void waphics_draw_image(Surface display, Vector2 position, Surface image) {
380
384
// }
381
385
// }
382
386
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
+ }
394
451
}
395
452
396
453
return new ;
0 commit comments