Created
June 7, 2023 00:55
-
-
Save Introscopia/e32720497e3c962874e3a3bd406cfa5e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdlib.h> | |
#include <stdio.h> | |
#include <SDL.h> | |
#define min(a,b) (((a)<(b))?(a):(b)) | |
#define max(a,b) (((a)>(b))?(a):(b)) | |
typedef int bool; | |
typedef struct{ | |
int len, size; | |
SDL_Rect *rcts; | |
SDL_Rect original; | |
} rectBlob; | |
void rectBlob_init( rectBlob *rB, int x, int y, int w, int h ){ | |
rB->len = 1; | |
rB->size = 4; | |
rB->original = (SDL_Rect){x,y,w,h}; | |
rB->rcts = malloc( rB->size * sizeof(SDL_Rect) ); | |
rB->rcts[0] = rB->original; | |
} | |
void rectBlob_append( rectBlob *rB, int x, int y, int w, int h ){ | |
if( rB->len >= rB->size ){ | |
rB->size *= 2; | |
//printf("(resizing to %d) ", rB->size ); | |
rB->rcts = realloc( rB->rcts, rB->size * sizeof(SDL_Rect) ); | |
} | |
//printf("A%d ", rB->len ); | |
//if( w < 0 ) printf("negativa!!! "); | |
rB->rcts[ rB->len ] = (SDL_Rect){x,y,w,h}; | |
rB->len += 1; | |
} | |
int rect_area( SDL_Rect *r ){ | |
return r->w * r->h; | |
} | |
void clip_rectBlob( rectBlob *rB, SDL_Rect faca ){ | |
int faca_r = faca.x + faca.w; | |
int faca_b = faca.y + faca.h; | |
int len_so_far = rB->len; | |
//printf("clipping %d rcts:", len_so_far ); | |
for ( int i = 0; i < len_so_far; ++i ){ | |
SDL_Rect *B = rB->rcts + i; | |
if( rect_area( rB->rcts + i ) <= 0 ) continue; | |
int rB_rcts_i_r = rB->rcts[i].x + rB->rcts[i].w; | |
int rB_rcts_i_b = rB->rcts[i].y + rB->rcts[i].h; | |
if( faca.x >= rB_rcts_i_r || | |
faca.y >= rB_rcts_i_b || | |
faca_r <= rB->rcts[i].x || | |
faca_b <= rB->rcts[i].y ){ | |
continue; | |
} | |
bool top_in = faca.y > rB->rcts[i].y && faca.y < rB_rcts_i_b; | |
bool bot_in = faca_b > rB->rcts[i].y && faca_b < rB_rcts_i_b; | |
bool lef_in = faca.x > rB->rcts[i].x && faca.x < rB_rcts_i_r; | |
bool rig_in = faca_r > rB->rcts[i].x && faca_r < rB_rcts_i_r; | |
int total = top_in + bot_in + lef_in + rig_in; | |
//printf("\n%d: %d%d%d%d = %d. ", i, top_in, bot_in, lef_in, rig_in, total ); | |
switch( total ){ | |
case 0: | |
//if( faca.x < rB->rcts[i].x && faca_r > rB_rcts_i_r && | |
// faca.y < rB->rcts[i].y && faca_b > rB_rcts_i_b ){ | |
//FULL CLIP | |
rB->rcts[i].w = 0; | |
break; | |
case 1: | |
if( top_in ){ | |
rB->rcts[i].h = faca.y - rB->rcts[i].y; | |
} | |
else if( bot_in ){ | |
rB->rcts[i].y = faca_b; | |
rB->rcts[i].h = rB_rcts_i_b - faca_b; | |
} | |
else if( lef_in ){ | |
rB->rcts[i].w = faca.x - rB->rcts[i].x; | |
} | |
else if( rig_in ){ | |
rB->rcts[i].x = faca_r; | |
rB->rcts[i].w = rB_rcts_i_r - faca_r; | |
} | |
break; | |
case 2: | |
if( rig_in && bot_in ){//top left corner clipped | |
rectBlob_append( rB, rB->rcts[i].x, faca_b, faca_r - rB->rcts[i].x, rB_rcts_i_b - faca_b ); | |
rB->rcts[i].x = faca_r; | |
rB->rcts[i].w = rB_rcts_i_r - faca_r; | |
} | |
else if( lef_in && bot_in ){//top right corner clipped | |
rectBlob_append( rB, faca.x, faca_b, rB_rcts_i_r - faca.x, rB_rcts_i_b - faca_b ); | |
rB->rcts[i].w = faca.x - rB->rcts[i].x; | |
} | |
else if( lef_in && top_in ){//bottom right corner clipped | |
rectBlob_append( rB, faca.x, rB->rcts[i].y, rB_rcts_i_r - faca.x, faca.y - rB->rcts[i].y ); | |
rB->rcts[i].w = faca.x - rB->rcts[i].x; | |
} | |
else if( rig_in && top_in ){// bottom left corner clipped | |
rectBlob_append( rB, rB->rcts[i].x, rB->rcts[i].y, faca_r - rB->rcts[i].x, faca.y - rB->rcts[i].y ); | |
rB->rcts[i].x = faca_r; | |
rB->rcts[i].w = rB_rcts_i_r - faca_r; | |
} | |
else if( lef_in && rig_in ){// vertical slice | |
rectBlob_append( rB, faca_r, rB->rcts[i].y, rB_rcts_i_r - faca_r, rB->rcts[i].h ); | |
rB->rcts[i].w = faca.x - rB->rcts[i].x; | |
} | |
else if( top_in && bot_in ){// horizontal slice | |
rectBlob_append( rB, rB->rcts[i].x, faca_b, rB->rcts[i].w, rB_rcts_i_b - faca_b ); | |
rB->rcts[i].h = faca.y - rB->rcts[i].y; | |
} | |
break; | |
case 3: | |
if( rig_in && bot_in && top_in ){// Left bite | |
rectBlob_append( rB, rB->rcts[i].x, rB->rcts[i].y, faca_r - rB->rcts[i].x, faca.y - rB->rcts[i].y ); | |
rectBlob_append( rB, rB->rcts[i].x, faca_b, faca_r - rB->rcts[i].x, rB_rcts_i_b - faca_b ); | |
rB->rcts[i].x = faca_r; | |
rB->rcts[i].w = rB_rcts_i_r - faca_r; | |
} | |
else if( lef_in && bot_in && rig_in ){//top bite | |
rectBlob_append( rB, faca.x, faca_b, faca.w, rB_rcts_i_b - faca_b ); | |
rectBlob_append( rB, faca_r, rB->rcts[i].y, rB_rcts_i_r - faca_r, rB->rcts[i].h ); | |
rB->rcts[i].w = faca.x - rB->rcts[i].x; | |
} | |
else if( lef_in && top_in && bot_in ){//right bite | |
rectBlob_append( rB, faca.x, rB->rcts[i].y, rB_rcts_i_r - faca.x, faca.y - rB->rcts[i].y ); | |
rectBlob_append( rB, faca.x, faca_b, rB_rcts_i_r - faca.x, rB_rcts_i_b - faca_b ); | |
rB->rcts[i].w = faca.x - rB->rcts[i].x; | |
} | |
else if( rig_in && top_in && lef_in ){//bottom bite | |
rectBlob_append( rB, faca.x, rB->rcts[i].y, faca.w, faca.y - rB->rcts[i].y ); | |
rectBlob_append( rB, faca_r, rB->rcts[i].y, rB_rcts_i_r - faca_r, rB->rcts[i].h ); | |
rB->rcts[i].w = faca.x - rB->rcts[i].x; | |
} | |
break; | |
case 4: | |
// HOLE | |
rectBlob_append( rB, faca.x, rB->rcts[i].y, faca.w, faca.y - rB->rcts[i].y ); | |
rectBlob_append( rB, faca.x, faca_b, faca.w, rB_rcts_i_b - faca_b ); | |
rectBlob_append( rB, faca_r, rB->rcts[i].y, rB_rcts_i_r - faca_r, rB->rcts[i].h ); | |
rB->rcts[i].w = faca.x - rB->rcts[i].x; | |
break; | |
} | |
//if( rB->rcts[i].w < 0 ) printf("Largura Negativa!\n"); | |
} | |
//printf("clip done: len:%d\n\n", rB->len ); | |
} | |
int main(int argc, char *argv[]){ | |
SDL_Window *window; | |
SDL_Renderer *rend; | |
int width = 1; | |
int height = 1; | |
bool loop = 1; | |
int mouseX = 0, mouseY = 0; | |
if (SDL_Init(SDL_INIT_VIDEO) < 0) { | |
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s", SDL_GetError()); | |
return 3; | |
} | |
window = SDL_CreateWindow( "rectClipper", 0, 0, 1, 1, SDL_WINDOW_MAXIMIZED | SDL_WINDOW_RESIZABLE ); | |
rend = SDL_CreateRenderer( window, 0, 0 ); | |
SDL_SetRenderDrawBlendMode( rend, SDL_BLENDMODE_BLEND ); | |
SDL_GetWindowSize( window, &width, &height ); | |
//printf("%d x %d\n", width, height ); | |
rectBlob rB; | |
rectBlob_init( &rB, 0.25*width, 0.25*height, width/2, height/2 ); | |
int ax = -1, ay = 0; | |
bool show_outlines = 0; | |
while ( loop ) {//============================================================================================================ | |
SDL_Event event; | |
while( SDL_PollEvent(&event) ){ | |
switch (event.type) { | |
case SDL_QUIT: | |
loop = 0; | |
break; | |
case SDL_KEYUP: | |
if( event.key.keysym.sym == ' ' ){ | |
show_outlines = !show_outlines; | |
} | |
else{ | |
for (int i = 0; i < rB.len; ++i ){ | |
printf( "%d: %d, %d, %d, %d\n", i, rB.rcts[i].x, rB.rcts[i].y, rB.rcts[i].w, rB.rcts[i].h ); | |
} | |
} | |
puts(""); | |
break; | |
case SDL_MOUSEMOTION: | |
mouseX = event.motion.x; | |
mouseY = event.motion.y; | |
break; | |
case SDL_MOUSEBUTTONDOWN: | |
ax = event.button.x; | |
ay = event.button.y; | |
break; | |
case SDL_MOUSEBUTTONUP:; | |
SDL_Rect clip = (SDL_Rect){ min(mouseX,ax), min(mouseY,ay), abs(mouseX-ax), abs(mouseY-ay) }; | |
clip_rectBlob( &rB, clip ); | |
ax = -1; | |
break; | |
} | |
} | |
SDL_SetRenderDrawColor( rend, 0, 0, 0, 255 ); | |
SDL_RenderClear( rend ); | |
SDL_SetRenderDrawColor( rend, 80, 80, 80, 255 ); | |
SDL_RenderDrawRect( rend, &(rB.original) ); | |
for (int i = 0; i < rB.len; ++i ){ | |
if( rect_area( rB.rcts + i ) <= 0 ) continue; | |
SDL_SetRenderDrawColor( rend, 255, 255, 255, 40 ); | |
SDL_RenderFillRect( rend, rB.rcts + i ); | |
if( show_outlines ){ | |
SDL_SetRenderDrawColor( rend, 255, 255, 255, 255 ); | |
SDL_RenderDrawRect( rend, rB.rcts + i ); | |
} | |
} | |
if( ax >= 0 ){ | |
SDL_SetRenderDrawColor( rend, 255, 0, 0, 255 ); | |
SDL_RenderDrawRect( rend, &(SDL_Rect){ min(mouseX,ax), min(mouseY,ay), abs(mouseX-ax), abs(mouseY-ay) } ); | |
} | |
SDL_RenderPresent(rend); | |
//SDL_framerateDelay( 16 ); | |
} | |
exit:; | |
free( rB.rcts ); | |
SDL_DestroyRenderer(rend); | |
SDL_DestroyWindow(window); | |
SDL_Quit(); | |
return 0; | |
} |
villares
commented
Jun 7, 2023
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment