Compare commits

...

3 Commits

Author SHA1 Message Date
Alexandre a9f7f1ea69 some fixes to fillRect #2 2024-08-28 15:29:09 +02:00
Alexandre ac059b3f74 some fixes to fillRect 2024-08-28 15:28:59 +02:00
Alexandre 0a61e13d9d added top-down view (laggy) 2024-08-28 11:23:32 +02:00
9 changed files with 225 additions and 24 deletions

View File

@ -8,6 +8,7 @@
"base.h": "c",
"limits": "c",
"move.h": "c",
"stdbool.h": "c"
"stdbool.h": "c",
"sdl2_gfxprimitives.h": "c"
}
}

BIN
bin/back

Binary file not shown.

View File

@ -56,6 +56,27 @@ int max(int a, int b) {
return a;
}
double min_db(double a, double b) {
if(a > b) {
return b;
};
return a;
}
double max_db(double a, double b) {
if(a < b) {
return b;
};
return a;
}
int sign(int n) {
if(n < 0) {
return (-1);
}
return 1;
}
double absf(double n) {
if(n > 0.0f) {
return n;

View File

@ -11,6 +11,12 @@ int min(int a, int b);
int max(int a, int b);
double min_db(double a, double b);
double max_db(double a, double b);
int sign(int n);
double absf(double n);
int convex_seg(int x1, int x2, double theta);

View File

@ -136,8 +136,69 @@ void drawStringToRenderer(SDL_Renderer* renderer, imgs data, char* s, int X, int
}
}
void drawChunkToRenderer(SDL_Renderer* renderer, chunk* ch, int xmin, int xmax, int ymin, int ymax) {
if(ch->draw_id != draw_par) {
void rescale(int* xm, int* xM, int* ym, int* yM, double scale) {
*xm = (int)((*xm - __width__/2) * scale + __width__/2) ;
*xM = (int)((*xM - __width__/2) * scale + __width__/2) ;
*ym = (int)((*ym - __height__/2) * scale + __height__/2) ;
*yM = (int)((*yM - __height__/2) * scale + __height__/2) ;
}
bool isOnLeft(chunk* ch, int x, int y) {
return (x == 0 || unpack_coord(ch->chdata.lines, x-1, y));
}
bool isOnRight(chunk* ch, int x, int y) {
return (x == 7 || unpack_coord(ch->chdata.lines, x+1, y));
}
bool isOnTop(chunk* ch, int x, int y) {
return (y == 0 || unpack_coord(ch->chdata.lines, x, y-1));
}
bool isOnBottom(chunk* ch, int x, int y) {
return (y == 7 || unpack_coord(ch->chdata.lines, x, y+1));
}
bool isPtInPoly(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4, int xp, int yp) {
// (x2 - x1) * (yp - y1) - (xp - x1) * (y2 - y1) ;
int v1 = (x2 - x1) * (yp - y1) - (xp - x1) * (y2 - y1) ;
int v2 = (x3 - x2) * (yp - y2) - (xp - x2) * (y3 - y2) ;
int v3 = (x4 - x3) * (yp - y3) - (xp - x3) * (y4 - y3) ;
int v4 = (x1 - x4) * (yp - y4) - (xp - x4) * (y1 - y4) ;
return (sign(v1) == sign(v2) && sign(v2) == sign(v3) && sign(v3) == sign(v4));
}
static int total ;
void fillQuadPoly(SDL_Renderer* renderer, int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4) {
int ymin = min(min(y1, y2), min(y3, y4));
int ymax = max(max(y1, y2), max(y3, y4));
int xmin = min(min(x1, x2), min(x3, x4));
int xmax = max(max(x1, x2), max(x3, x4));
for(int h = ymin; h <= ymax; h++) {
for(int w = xmin; w <= xmax; w++) {
if(isPtInPoly(x1, y1, x2, y2, x3, y3, x4, y4, w, h)) {
SDL_RenderDrawPoint(renderer, w, h);
}
}
};
/*double step = max_db(distance_pt(x1, y1, x2, y2), distance_pt(x3, y3, x4, y4));
double theta = 0.0 ;
while(theta <= 1.0) {
total += 1 ;
drawLineWithThicc(renderer, 2, convex_seg(x1, x2, theta), convex_seg(x4, x3, theta), convex_seg(y1, y2, theta), convex_seg(y4, y3, theta), 255, 192, 255, SDL_ALPHA_OPAQUE);
theta += 1.0 / step;
}*/
/*total += (ymax-ymin)*(xmax-xmin) ;
printf("T = %d\n", total);*/
drawLineWithThicc(renderer, 2, x1, x2, y1, y2, 32, 32, 32, SDL_ALPHA_OPAQUE);
drawLineWithThicc(renderer, 2, x2, x3, y2, y3, 32, 32, 32, SDL_ALPHA_OPAQUE);
drawLineWithThicc(renderer, 2, x3, x4, y3, y4, 32, 32, 32, SDL_ALPHA_OPAQUE);
drawLineWithThicc(renderer, 2, x4, x1, y4, y1, 32, 32, 32, SDL_ALPHA_OPAQUE);
}
void drawChunkToRenderer(SDL_Renderer* renderer, chunk* ch, int xmin, int xmax, int ymin, int ymax, bool period) {
if(ch->draw_id != draw_par || !period) {
ch->draw_id = draw_par ;
for(int i = 0; i < 8; i++) {
uint8_t cur = ch->chdata.lines[i] ;
@ -150,12 +211,40 @@ void drawChunkToRenderer(SDL_Renderer* renderer, chunk* ch, int xmin, int xmax,
if(cur%2 == 1) {
int nxmin = to_int((to_double(cux) - to_double(xmin)) * to_double(__width__)) / (to_double(xmax) - to_double(xmin)) ;
int nymin = to_int((to_double(cuy) - to_double(ymin)) * to_double(__height__)) / (to_double(ymax) - to_double(ymin)) ;
int nxmin2 = to_int((to_double(cux2) - to_double(xmin)) * to_double(__width__)) / (to_double(xmax) - to_double(xmin)) ;
int nymin2 = to_int((to_double(cuy2) - to_double(ymin)) * to_double(__height__)) / (to_double(ymax) - to_double(ymin)) ;
if(abs(ch->chx + ch->chy)%2 == 0) {
placeRectToRenderer(renderer, nxmin, nymin, nxmin2 - nxmin, nymin2 - nymin, 255, 192, 192, SDL_ALPHA_OPAQUE);
int nxmax = to_int((to_double(cux2) - to_double(xmin)) * to_double(__width__)) / (to_double(xmax) - to_double(xmin)) ;
int nymax = to_int((to_double(cuy2) - to_double(ymin)) * to_double(__height__)) / (to_double(ymax) - to_double(ymin)) ;
uint8_t R = 255 - 63 * ((abs(ch->chx) + abs(ch->chy))%2 == 0) ;
uint8_t G = 255 ;
uint8_t B = 255 - 63 * ((abs(ch->chx) + abs(ch->chy))%2 == 1) ;
int sxmin = nxmin ;
int sxmax = nxmax ;
int symin = nymin ;
int symax = nymax ;
rescale(&nxmin, &nxmax, &nymin, &nymax, 1.5);
if(period) {
if(!isOnLeft(ch, i, j) && (8*ch->chy + i) >= (8*player_cy + player_y)) { // North
SDL_SetRenderDrawColor(renderer, R/2, G/2, B/2, SDL_ALPHA_OPAQUE);
fillQuadPoly(renderer, nxmin, nymin, sxmin, symin, sxmax, symin, nxmax, nymin);
}
if(!isOnBottom(ch, i, j) && (8*ch->chx + j) <= (8*player_cx + player_x)) { // West
SDL_SetRenderDrawColor(renderer, R/2, G/2, B/2, SDL_ALPHA_OPAQUE);
fillQuadPoly(renderer, nxmax, nymax, sxmax, symax, sxmax, symin, nxmax, nymin);
}
if(!isOnRight(ch, i, j) && (8*ch->chy + i) <= (8*player_cy + player_y)) { // South
SDL_SetRenderDrawColor(renderer, R/2, G/2, B/2, SDL_ALPHA_OPAQUE);
fillQuadPoly(renderer, nxmax, nymax, sxmax, symax, sxmin, symax, nxmin, nymax);
}
if(!isOnTop(ch, i, j) && (8*ch->chx + j) >= (8*player_cx + player_x)) { // East
SDL_SetRenderDrawColor(renderer, R/2, G/2, B/2, SDL_ALPHA_OPAQUE);
fillQuadPoly(renderer, nxmin, nymin, sxmin, symin, sxmin, symax, nxmin, nymax);
}
} else {
placeRectToRenderer(renderer, nxmin, nymin, nxmin2 - nxmin, nymin2 - nymin, 192, 192, 255, SDL_ALPHA_OPAQUE);
SDL_SetRenderDrawColor(renderer, R, G, B, SDL_ALPHA_OPAQUE);
placeRectToRenderer(renderer, nxmin, nymin, nxmax - nxmin, nymax - nymin, 32, 32, 32, SDL_ALPHA_OPAQUE);
placeRectToRenderer(renderer, nxmin+1, nymin+1, nxmax - nxmin -2, nymax - nymin -2, R, G, B, SDL_ALPHA_OPAQUE);
}
};
cur = cur / 2 ;
@ -172,7 +261,6 @@ void drawHashToRenderer(SDL_Renderer* renderer, int chx, int chy, int xmin, int
fprintf(stderr, "NO (%d, %d)\n", chx, chy);
exit(1);
};
drawChunkToRenderer(renderer, todraw, xmin, xmax, ymin, ymax);
if(distx != render_distance -1 && !gridMem(map, chx+1, chy)) {
generate_chunk_all(chx+1, chy);
@ -191,6 +279,8 @@ void drawHashToRenderer(SDL_Renderer* renderer, int chx, int chy, int xmin, int
drawHashToRenderer(renderer, chx-1, chy, xmin, xmax, ymin, ymax, distx+1, disty);
drawHashToRenderer(renderer, chx, chy+1, xmin, xmax, ymin, ymax, distx, disty+1);
drawHashToRenderer(renderer, chx, chy-1, xmin, xmax, ymin, ymax, distx, disty+1);
drawChunkToRenderer(renderer, todraw, xmin, xmax, ymin, ymax, false);
}
}
@ -198,3 +288,17 @@ void drawMapToRenderer(SDL_Renderer* renderer, int xmin, int xmax, int ymin, int
drawHashToRenderer(renderer, player_cx, player_cy, xmin, xmax, ymin, ymax, 0, 0);
placeRectToRenderer(renderer, __width__ /2 - 5*zoom/100, __height__ /2 - 5*zoom/100, 10*zoom/100, 10*zoom/100, 255, 255, 32, SDL_ALPHA_OPAQUE);
}
void drawMapToRendererV2(SDL_Renderer* renderer, int xmin, int xmax, int ymin, int ymax) {
for(int i = 0; i < (2*render_distance+1)*(2*render_distance+1); i++) {
if(!gridMem(map, player_cx+sorted_x[i], player_cy + sorted_y[i])) {
generate_chunk_all(player_cx+sorted_x[i], player_cy + sorted_y[i]);
}
chunk* todraw = gridGet(map, player_cx+sorted_x[i], player_cy + sorted_y[i]) ;
drawChunkToRenderer(renderer, todraw, xmin, xmax, ymin, ymax, true);
drawChunkToRenderer(renderer, todraw, xmin, xmax, ymin, ymax, false);
}
placeRectToRenderer(renderer, __width__ /2 - 5*zoom/100, __height__ /2 - 5*zoom/100, 10*zoom/100, 10*zoom/100, 255, 255, 32, SDL_ALPHA_OPAQUE);
}

View File

@ -21,10 +21,26 @@ void drawNumberToRenderer(SDL_Renderer* renderer, imgs data, int n, int X, int Y
void drawStringToRenderer(SDL_Renderer* renderer, imgs data, char* s, int X, int Y, int W, int H);
void drawChunkToRenderer(SDL_Renderer* renderer, chunk* ch, int xmin, int xmax, int ymin, int ymax);
void rescale(int* xm, int* xM, int* ym, int* yM, double scale);
bool isOnLeft(chunk* ch, int x, int y);
bool isOnRight(chunk* ch, int x, int y);
bool isOnTop(chunk* ch, int x, int y);
bool isOnBottom(chunk* ch, int x, int y);
bool isPtInPoly(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4, int xp, int yp);
void fillQuadPoly(SDL_Renderer* renderer, int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4);
void drawChunkToRenderer(SDL_Renderer* renderer, chunk* ch, int xmin, int xmax, int ymin, int ymax, bool period);
void drawHashToRenderer(SDL_Renderer* renderer, int chx, int chy, int xmin, int xmax, int ymin, int ymax, int distx, int disty);
void drawMapToRenderer(SDL_Renderer* renderer, int xmin, int xmax, int ymin, int ymax);
void drawMapToRendererV2(SDL_Renderer* renderer, int xmin, int xmax, int ymin, int ymax);
#endif

View File

@ -42,13 +42,17 @@ imgs digits ;
imgs letters ;
template full ;
template empty ;
int* sorted_x ;
int* sorted_y ;
// yes Valentin I learned to use .h the intended way xD
// ------------------------------------------------------------------------ //
bool unpack_coord(uint8_t* lines, int cx, int cy) {
return (bool)((lines[cy]/pw(2, cx))%2);
return (bool)((lines[cx]/pw(2, cy))%2);
}
template copy(template src) {
@ -91,6 +95,13 @@ void print_template(int config_id) {
printf("(%d)\n", configs[config_id].lines[i]);
};
printf("\n");
for(int i = 0; i < 8; i++) {
for(int j = 0; j < 8; j++) {
printf("%d ", unpack_coord(configs[config_id].lines, i, j));
};
printf("\n");
};
printf("\n");
}
void rotateTemplateClockWise(int config_id) {
@ -160,17 +171,6 @@ void signature(template t) {
printf("[WEST] %d\n\n", t.westsig);
}
bool is_compat_with_spins(int cx, int cy, int idx) {
for(int k = 0; k < 4; k++) {
if(is_compat_sig_north(cx, cy, idx) && is_compat_sig_east(cx, cy, idx) && is_compat_sig_south(cx, cy, idx) && is_compat_sig_west(cx, cy, idx)) {
return true ;
} else {
rotateTemplateClockWise(idx);
}
};
return false;
}
bool is_compat_with_spins_uno(int cx, int cy, int idx) {
if(is_compat_sig_north(cx, cy, idx) && is_compat_sig_east(cx, cy, idx) && is_compat_sig_south(cx, cy, idx) && is_compat_sig_west(cx, cy, idx)) {
return true ;
@ -227,11 +227,53 @@ void initialize(SDL_Renderer* renderer) {
import_digits(renderer);
full.lines = malloc(sizeof(uint8_t)*8);
full.eastsig = 255 ;
full.westsig = 255 ;
for(int i = 0; i < 8; i++) {
full.lines[i] = 255 ;
};
full.id = -1 ;
empty.lines = malloc(sizeof(uint8_t)*8);
empty.eastsig = 0 ;
empty.westsig = 0 ;
for(int i = 0; i < 8; i++) {
empty.lines[i] = 0 ;
};
empty.id = -2 ;
int xys = pw(2*render_distance+1, 2);
sorted_x = malloc(sizeof(int)*xys);
sorted_y = malloc(sizeof(int)*xys);
int i = 0 ;
for(int w = -render_distance; w <= render_distance; w++) {
for(int h = -render_distance; h <= render_distance; h++) {
sorted_x[i] = w ;
sorted_y[i] = h ;
i += 1;
}
}
for(int k = 0; k < xys-1; k++) {
for(int l = 0; l < xys-1; l++) {
if(abs(sorted_x[l]) + abs(sorted_y[l]) < abs(sorted_x[1+l]) + abs(sorted_y[1+l])) {
int tpx = sorted_x[l];
int tpy = sorted_y[l];
sorted_x[l] = sorted_x[l+1];
sorted_y[l] = sorted_y[l+1];
sorted_x[l+1] = tpx;
sorted_y[l+1] = tpy ;
}
}
}
for(int k = 0; k < xys; k++) {
printf("[%d, %d] -> (%d)\n", sorted_x[k], sorted_y[k], abs(sorted_x[k]) + abs(sorted_y[k]));
}
generate_chunk(0, 0, 2);
}
@ -242,6 +284,10 @@ void destroy() {
free_digits(letters);
free(full.lines);
free(empty.lines);
free(sorted_x);
free(sorted_y);
}
void parse_one(FILE* ptr, FILE* ptr2, int index) {

View File

@ -29,7 +29,7 @@ bool checkCollision() {
}
bool checkCollision2(int cx, int cy, int x, int y) {
return (unpack_coord(gridGet(map, cx, cy)->chdata.lines, x, y));
return (unpack_coord(gridGet(map, cx, cy)->chdata.lines, y, x));
}
void normalize(int* ch_coord, int* coord, double* ε) {
@ -177,6 +177,7 @@ void moveFunctionMaster(SDL_Renderer* renderer) {
moveControl(&halt);
resetRenderer(renderer);
drawMapToRenderer(renderer, -300 * 250/zoom + to_int(50 * (8*player_cx + player_x + εx)), 300 * 250/zoom + to_int(50 * (8*player_cx + player_x + εx)), -300 * 250/zoom + to_int(50 * (8*player_cy + player_y + εy)), 300 * 250/zoom + to_int(50 * (8*player_cy + player_y + εy)));
//drawMapToRendererV2(renderer, -300 * 250/zoom + to_int(50 * (8*player_cx + player_x + εx)), 300 * 250/zoom + to_int(50 * (8*player_cx + player_x + εx)), -300 * 250/zoom + to_int(50 * (8*player_cy + player_y + εy)), 300 * 250/zoom + to_int(50 * (8*player_cy + player_y + εy)));
drawNumberToRenderer(renderer, digits, player_cx, 10, 10, 50, 70, 0);
drawNumberToRenderer(renderer, digits, player_cy, 10, 80, 50, 70, 0);
drawNumberToRenderer(renderer, digits, player_x, __width__ / 3, 10, 50, 70, 0);
@ -184,7 +185,8 @@ void moveFunctionMaster(SDL_Renderer* renderer) {
drawNumberToRenderer(renderer, digits, to_int(εx*100), 2 * __width__ / 3, 10, 50, 70, 0);
drawNumberToRenderer(renderer, digits, to_int(εy*100), 2 * __width__ / 3, 80, 50, 70, 0);
updateRenderer(renderer);
//exit(1);
draw_par += 1 ;
usleep(20000);
//usleep(20000);
}
}

View File

@ -47,4 +47,9 @@ extern imgs letters ;
extern template full ;
extern template empty ;
extern int* sorted_x ;
extern int* sorted_y ;
#endif