#include #include #include #include #include #include #include #include #include #include #include #include #include "hash.h" #include "structure.h" #include "base.h" imgs digits ; imgs letters ; int ln_baseN(int n, int b) { int r = 0; while(n != 0) { n = n / b; r++; } return r; } int pw(int x, int n) { if (n<0) return 0; if (n==0) return 1; if (n%2==0) return pw(x*x, n/2); return x*pw(x*x, n/2); } double pwf(double x, int n) { if (n<0) return 0.0; if (n==0) return 1.0; if (n%2==0) return pwf(x*x, n/2); return x*pwf(x*x, n/2); } int abs(int n) { if(n > 0) { return n; } else { return (-n); } } int min(int a, int b) { if(a > b) { return b; }; return a; } int max(int a, int b) { if(a < b) { return b; }; return a; } double mind(double a, double b) { if(a > b) { return b; }; return a; } double maxd(double a, double b) { if(a < b) { return b; }; return a; } double absf(double n) { if(n > 0.0f) { return n; } else { return (-n); } } int convex_seg(int x1, int x2, double theta) { return (int)(((1.0f - theta) * x1 + theta * x2)); } bool is_an_integer(char c) { return ((int)c >= 48 && (int)c <= 57); } double to_double(int n) { return (double)n ; } int to_int(double n) { return (int)n ; } int line_count(char* filename) { FILE* ptr = fopen(filename, "r"); char c = 'd'; int n = 0 ; while(c != EOF) { if(c == '\n') { n += 1; }; c = fgetc(ptr); }; fclose(ptr); return (n+1); } int str_to_int(char* s) { int res = 0 ; int i = 0 ; while(s[i] != '\0' && is_an_integer(s[i])) { res *= 10 ; res += (int)s[i] - 48 ; i++; }; return res; } bool str_equal(char* s1, char* s2) { if(s1[0] == '\0' || s2[0] == '\0') { return (s1[0] == '\0' && s2[0] == '\0'); } return (s1[0] == s2[0] && str_equal(&s1[1], &s2[1])); } // ------------------------------------------------------------------------------------------------ // cube_0 create_cube_0(double x, double y, double z, double w, double h, double d, double hz_a, double vt_a, int r, int g, int b) { cube_0* cb = malloc(sizeof(cube_0)) ; cb->red = r ; cb->green = g ; cb->blue = b ; cb->x = x ; cb->y = y ; cb->z = z ; cb->w = w ; cb->h = h ; cb->d = d ; cb->hz_angle = hz_a ; cb->vt_angle = vt_a ; return *cb ; } cube create_cube(double x, double y, double z, double w, double h, double d, double hz_a, double vt_a, int r, int g, int b) { cube cb = malloc(sizeof(cube_0)); *cb = create_cube_0(x, y, z, w, h, d, hz_a, vt_a, r, g, b) ; return cb; } void free_cube(cube c) { free(c) ; } teleporter create_teleporter( double x, double y, double z, double w, double h, double d, double hz_a, double vt_a, int r, int g, int b, int chx_dest, int chy_dest, double x_dest, double y_dest, double z_dest ) { teleporter* tp = malloc(sizeof(teleporter)) ; tp->dest_chx = chx_dest ; tp->dest_chy = chy_dest ; tp->dest_x = x_dest ; tp->dest_y = y_dest ; tp->dest_z = z_dest ; tp->hitbox = create_cube_0(x, y, z, w, h, d, hz_a, vt_a, r, g, b); return *tp ; } // ------------------------------------------------------------------------------------------------ // double convex_pt(double a, double b, double theta) { return (a+(b-a)*theta) ; } double distance_pt_pt_3d(double x0, double y0, double z0, double x1, double y1, double z1) { return sqrt((x1 - x0)*(x1 - x0)+(y1 - y0)*(y1 - y0)+(z1 - z0)*(z1 - z0)) ; } double distance_pt_seg_3d(double x, double y, double z, double sx, double sy, double sz, double ex, double ey, double ez) { double theta = -( ((ex - sx) * (sx - x) + (ey - sy) * (sy - y) + (ez - sz) * (sz - z)) / ((ex - sx) * (ex - sx) + (ey - sy) * (ey - sy) + (ez - sz) * (ez - sz)) ); if(theta >= 0.0 && theta <= 1.0) { return (distance_pt_pt_3d(x, y, z, convex_pt(sx, ex, theta), convex_pt(sy, ey, theta), convex_pt(sz, ez, theta))) ; } else if (theta < 0.0) { return (distance_pt_pt_3d(x, y, z, sx, sy, sz)) ; } else { return (distance_pt_pt_3d(x, y, z, ex, ey, ez)) ; } } double distance_pt_cube_axis(double coord, double begin, double end) { if(coord < begin) { return (begin-coord) ; } else if(coord > end) { return (coord-end) ; } else { return 0.0 ; } } double distance_pt_cube_axis_max(double coord, double begin, double end) { if(coord < (begin+end)/2) { return absf(end-coord) ; } else { return absf(begin-coord) ; } } double distance_pt_cube_aligned_3d(double x0, double y0, double z0, double cx, double cy, double cz, double cw, double ch, double cd) { return (distance_pt_cube_axis(x0, cx, cx+cw)+distance_pt_cube_axis(y0, cy, cy+ch)+distance_pt_cube_axis(z0, cz, cz+cd)) ; } double distance_pt_cube_aligned_3d_max(double x0, double y0, double z0, double cx, double cy, double cz, double cw, double ch, double cd) { return (distance_pt_cube_axis_max(x0, cx, cx+cw)+distance_pt_cube_axis_max(y0, cy, cy+ch)+distance_pt_cube_axis_max(z0, cz, cz+cd)) ; } double distance_pt_cube_0_3d(double x0, double y0, double z0, cube_0 c) { // places the origin at the center of the cube double x = x0 - (c.x + c.w/2.0) ; double y = y0 - (c.y + c.h/2.0) ; double z = z0 - (c.z + c.d/2.0) ; // rotate the point : y then x double xry = x*cos(c.hz_angle) + z*sin(c.hz_angle) ; double yry = y ; double zry = z*cos(c.hz_angle) - x*sin(c.hz_angle) ; double xrx = xry ; double yrx = yry*cos(c.vt_angle) - zry*sin(c.vt_angle) ; double zrx = zry*cos(c.vt_angle) + yry*sin(c.vt_angle) ; // now the cube and pt are aligned, and (0, 0, 0) is at the cube's (bary)center return distance_pt_cube_aligned_3d(xrx, yrx, zrx, -c.w/2.0, -c.h/2.0, -c.d/2.0, c.w, c.h, c.d) ; } double distance_pt_cube_0_3d_max(double x0, double y0, double z0, cube_0 c) { // places the origin at the center of the cube double x = x0 - (c.x + c.w/2.0) ; double y = y0 - (c.y + c.h/2.0) ; double z = z0 - (c.z + c.d/2.0) ; // rotate the point : y then x double xry = x*cos(c.hz_angle) + z*sin(c.hz_angle) ; double yry = y ; double zry = z*cos(c.hz_angle) - x*sin(c.hz_angle) ; double xrx = xry ; double yrx = yry*cos(c.vt_angle) - zry*sin(c.vt_angle) ; double zrx = zry*cos(c.vt_angle) + yry*sin(c.vt_angle) ; // now the cube and pt are aligned, and (0, 0, 0) is at the cube's (bary)center return distance_pt_cube_aligned_3d_max(xrx, yrx, zrx, -c.w/2.0, -c.h/2.0, -c.d/2.0, c.w, c.h, c.d) ; } double distance_pt_cube_aligned_3d_weighted(double x0, double y0, double z0, double cx, double cy, double cz, double cw, double ch, double cd, double mx, double my, double mz) { return (mx*distance_pt_cube_axis(x0, cx, cx+cw)+my*distance_pt_cube_axis(y0, cy, cy+ch)+mz*distance_pt_cube_axis(z0, cz, cz+cd)) ; } double distance_pt_cube_0_3d_weighted(double x0, double y0, double z0, double mx, double my, double mz, cube_0 c) { // places the origin at the center of the cube double x = x0 - (c.x + c.w/2.0) ; double y = y0 - (c.y + c.h/2.0) ; double z = z0 - (c.z + c.d/2.0) ; // rotate the point : y then x double xry = x*cos(c.hz_angle) + z*sin(c.hz_angle) ; double yry = y ; double zry = z*cos(c.hz_angle) - x*sin(c.hz_angle) ; double xrx = xry ; double yrx = yry*cos(c.vt_angle) - zry*sin(c.vt_angle) ; double zrx = zry*cos(c.vt_angle) + yry*sin(c.vt_angle) ; // now the cube and pt are aligned, and (0, 0, 0) is at the cube's (bary)center return distance_pt_cube_aligned_3d_weighted(xrx, yrx, zrx, -c.w/2.0, -c.h/2.0, -c.d/2.0, c.w, c.h, c.d, mx, my, mz) ; } double distance_pt_cube_3d(double x0, double y0, double z0, cube cb) { return distance_pt_cube_0_3d(x0, y0, z0, *cb) ; } // ---------------- // // intersects = !((a.max < b.min) || (b.max < a.min)) double distance_seg_seg_1d(double s0, double e0, double s1, double e1) { double theta_s0 = -(((e1 - s1) * (s1 - s0)) / ((e1 - s1) * (e1 - s1))); double theta_e0 = -(((e1 - s1) * (s1 - e0)) / ((e1 - s1) * (e1 - s1))); if( (theta_s0 >= 0.0 && theta_s0 <= 1.0) || // s0 is in [s1, e1] (theta_e0 >= 0 && theta_e0 <= 1.0) || // s1 is in [s1, e1] (theta_s0 < 0.0 && theta_e0 > 1.0) || // inclusion (theta_e0 < 0.0 && theta_s0 > 1.0)) // inclusion { return 0.0; } else { double dist0 = mind(absf(theta_s0), absf(1.0 - theta_s0)) ; double dist1 = mind(absf(theta_e0), absf(1.0 - theta_e0)) ; // get the closest theta to [0, 1] return mind(dist1, dist0)*absf(e1 - s1); } } bool intersects_seg_seg_1d(double s0, double e0, double s1, double e1) { int amin = mind(s0, e0); int amax = maxd(s0, e0); int bmin = mind(s1, e1); int bmax = maxd(s1, e1); return !((amax < bmin) || (bmax < amin)); } double distance_seg_seg_2d( double s0x, double s0y, double e0x, double e0y, double s1x, double s1y, double e1x, double e1y ) { // basically ||.||_1 return ( distance_seg_seg_1d(s0x, e0x, s1x, e1x) + distance_seg_seg_1d(s0y, e0y, s1y, e1y) ); } bool intersects_seg_seg_2d( double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4 ) { double t = ((x1 - x3)*(y3 - y4) - (y1 - y3)*(x3 - x4))/ ((x1 - x2)*(y3 - y4) - (y1 - y2)*(x3 - x4)); double u = -( ((x1 - x2)*(y1 - y3) - (y1 - y2)*(x1 - x3))/ ((x1 - x2)*(y3 - y4) - (y1 - y2)*(x3 - x4))); return ((0.0 <= t && t <= 1.0) && (0.0 <= u && u <= 1.0)); } double distance_seg_seg_3d( double s0x, double s0y, double s0z, double e0x, double e0y, double e0z, double s1x, double s1y, double s1z, double e1x, double e1y, double e1z ) { // same return ( distance_seg_seg_1d(s0x, e0x, s1x, e1x) + distance_seg_seg_1d(s0y, e0y, s1y, e1y) + distance_seg_seg_1d(s0z, e0z, s1z, e1z) ); } double distance_poly_poly_2d(pt_2d* t1, int len_1, pt_2d* t2, int len_2) { double res = 10000.0 ; if(len_1 == 0 || len_2 == 0) { return 727.27 ; // arbitrary } for(int k1 = 0; k1 < len_1; k1++) { for(int k2 = 0; k2 < len_2; k2++) { res = mind(res, distance_seg_seg_2d( t1[k1].x, t1[k1].y, t1[(k1+1)%len_1].x, t1[(k1+1)%len_1].y, t2[k2].x, t2[k2].y, t2[(k2+1)%len_2].x, t2[(k2+1)%len_2].y )); } } return res; } bool intersects_poly_poly_2d(pt_2d* t1, int len_1, pt_2d* t2, int len_2) { if(len_1 == 0 || len_2 == 0) { return false ; // arbitrary } for(int k1 = 0; k1 < len_1; k1++) { for(int k2 = 0; k2 < len_2; k2++) { if(intersects_seg_seg_2d( t1[k1].x, t1[k1].y, t1[(k1+1)%len_1].x, t1[(k1+1)%len_1].y, t2[k2].x, t2[k2].y, t2[(k2+1)%len_2].x, t2[(k2+1)%len_2].y )) { return true; } } } return false; } // ------------------------------------------------------------------------------------------------ // void remove_entity(entity** arr, int* memlen, int* len, int index) { if(*len > 0) { (*arr)[index] = (*arr)[*len -1]; *len -= 1; } } void add_entity(entity** arr, int* memlen, int* len, entity ent) { if(*memlen == *len) { entity* newarr = malloc(sizeof(entity)*2*(*memlen)); for(int k = 0; k < *len; k++) { newarr[k] = (*arr)[k] ; } free(*arr); *arr = newarr ; *memlen *= 2; } (*arr)[*len] = ent ; *len += 1; } // ------------------------------------------------------------------------------------------------ // void import_digits(SDL_Renderer* renderer) { imgs res; res.arr = malloc(sizeof(SDL_Texture*)*11); SDL_Texture* texture; SDL_Surface* img; // -------------------------------------------------------- // img = SDL_LoadBMP("./res/digits/digit-0.bmp"); texture = SDL_CreateTextureFromSurface(renderer, img); SDL_FreeSurface(img); res.arr[0] = texture; // -------------------------------------------------------- // img = SDL_LoadBMP("./res/digits/digit-1.bmp"); texture = SDL_CreateTextureFromSurface(renderer, img); SDL_FreeSurface(img); res.arr[1] = texture; // -------------------------------------------------------- // img = SDL_LoadBMP("./res/digits/digit-2.bmp"); texture = SDL_CreateTextureFromSurface(renderer, img); SDL_FreeSurface(img); res.arr[2] = texture; // -------------------------------------------------------- // img = SDL_LoadBMP("./res/digits/digit-3.bmp"); texture = SDL_CreateTextureFromSurface(renderer, img); SDL_FreeSurface(img); res.arr[3] = texture; // -------------------------------------------------------- // img = SDL_LoadBMP("./res/digits/digit-4.bmp"); texture = SDL_CreateTextureFromSurface(renderer, img); SDL_FreeSurface(img); res.arr[4] = texture; // -------------------------------------------------------- // img = SDL_LoadBMP("./res/digits/digit-5.bmp"); texture = SDL_CreateTextureFromSurface(renderer, img); SDL_FreeSurface(img); res.arr[5] = texture; // -------------------------------------------------------- // img = SDL_LoadBMP("./res/digits/digit-6.bmp"); texture = SDL_CreateTextureFromSurface(renderer, img); SDL_FreeSurface(img); res.arr[6] = texture; // -------------------------------------------------------- // img = SDL_LoadBMP("./res/digits/digit-7.bmp"); texture = SDL_CreateTextureFromSurface(renderer, img); SDL_FreeSurface(img); res.arr[7] = texture; // -------------------------------------------------------- // img = SDL_LoadBMP("./res/digits/digit-8.bmp"); texture = SDL_CreateTextureFromSurface(renderer, img); SDL_FreeSurface(img); res.arr[8] = texture; // -------------------------------------------------------- // img = SDL_LoadBMP("./res/digits/digit-9.bmp"); texture = SDL_CreateTextureFromSurface(renderer, img); SDL_FreeSurface(img); res.arr[9] = texture; // -------------------------------------------------------- // img = SDL_LoadBMP("./res/digits/sign-minus.bmp"); texture = SDL_CreateTextureFromSurface(renderer, img); SDL_FreeSurface(img); res.arr[10] = texture; res.len = 11 ; digits = res; } void import_letters(SDL_Renderer* renderer) { imgs res; res.arr = malloc(sizeof(SDL_Texture*)*26); SDL_Texture* texture; SDL_Surface* img; int cc = 0 ; // -------------------------------------------------------- // img = SDL_LoadBMP("./res/letters/letter-a.bmp"); texture = SDL_CreateTextureFromSurface(renderer, img); SDL_FreeSurface(img); res.arr[cc] = texture; cc += 1 ; // -------------------------------------------------------- // img = SDL_LoadBMP("./res/letters/letter-b.bmp"); texture = SDL_CreateTextureFromSurface(renderer, img); SDL_FreeSurface(img); res.arr[cc] = texture; cc += 1 ; // -------------------------------------------------------- // img = SDL_LoadBMP("./res/letters/letter-c.bmp"); texture = SDL_CreateTextureFromSurface(renderer, img); SDL_FreeSurface(img); res.arr[cc] = texture; cc += 1 ; // -------------------------------------------------------- // img = SDL_LoadBMP("./res/letters/letter-d.bmp"); texture = SDL_CreateTextureFromSurface(renderer, img); SDL_FreeSurface(img); res.arr[cc] = texture; cc += 1 ; // -------------------------------------------------------- // img = SDL_LoadBMP("./res/letters/letter-e.bmp"); texture = SDL_CreateTextureFromSurface(renderer, img); SDL_FreeSurface(img); res.arr[cc] = texture; cc += 1 ; // -------------------------------------------------------- // img = SDL_LoadBMP("./res/letters/letter-f.bmp"); texture = SDL_CreateTextureFromSurface(renderer, img); SDL_FreeSurface(img); res.arr[cc] = texture; cc += 1 ; // -------------------------------------------------------- // img = SDL_LoadBMP("./res/letters/letter-g.bmp"); texture = SDL_CreateTextureFromSurface(renderer, img); SDL_FreeSurface(img); res.arr[cc] = texture; cc += 1 ; // -------------------------------------------------------- // img = SDL_LoadBMP("./res/letters/letter-h.bmp"); texture = SDL_CreateTextureFromSurface(renderer, img); SDL_FreeSurface(img); res.arr[cc] = texture; cc += 1 ; // -------------------------------------------------------- // img = SDL_LoadBMP("./res/letters/letter-i.bmp"); texture = SDL_CreateTextureFromSurface(renderer, img); SDL_FreeSurface(img); res.arr[cc] = texture; cc += 1 ; // -------------------------------------------------------- // img = SDL_LoadBMP("./res/letters/letter-j.bmp"); texture = SDL_CreateTextureFromSurface(renderer, img); SDL_FreeSurface(img); res.arr[cc] = texture; cc += 1 ; // -------------------------------------------------------- // img = SDL_LoadBMP("./res/letters/letter-k.bmp"); texture = SDL_CreateTextureFromSurface(renderer, img); SDL_FreeSurface(img); res.arr[cc] = texture; cc += 1 ; // -------------------------------------------------------- // img = SDL_LoadBMP("./res/letters/letter-l.bmp"); texture = SDL_CreateTextureFromSurface(renderer, img); SDL_FreeSurface(img); res.arr[cc] = texture; cc += 1 ; // -------------------------------------------------------- // img = SDL_LoadBMP("./res/letters/letter-m.bmp"); texture = SDL_CreateTextureFromSurface(renderer, img); SDL_FreeSurface(img); res.arr[cc] = texture; cc += 1 ; // -------------------------------------------------------- // img = SDL_LoadBMP("./res/letters/letter-n.bmp"); texture = SDL_CreateTextureFromSurface(renderer, img); SDL_FreeSurface(img); res.arr[cc] = texture; cc += 1 ; // -------------------------------------------------------- // img = SDL_LoadBMP("./res/letters/letter-o.bmp"); texture = SDL_CreateTextureFromSurface(renderer, img); SDL_FreeSurface(img); res.arr[cc] = texture; cc += 1 ; // -------------------------------------------------------- // img = SDL_LoadBMP("./res/letters/letter-p.bmp"); texture = SDL_CreateTextureFromSurface(renderer, img); SDL_FreeSurface(img); res.arr[cc] = texture; cc += 1 ; // -------------------------------------------------------- // img = SDL_LoadBMP("./res/letters/letter-q.bmp"); texture = SDL_CreateTextureFromSurface(renderer, img); SDL_FreeSurface(img); res.arr[cc] = texture; cc += 1 ; // -------------------------------------------------------- // img = SDL_LoadBMP("./res/letters/letter-r.bmp"); texture = SDL_CreateTextureFromSurface(renderer, img); SDL_FreeSurface(img); res.arr[cc] = texture; cc += 1 ; // -------------------------------------------------------- // img = SDL_LoadBMP("./res/letters/letter-s.bmp"); texture = SDL_CreateTextureFromSurface(renderer, img); SDL_FreeSurface(img); res.arr[cc] = texture; cc += 1 ; // -------------------------------------------------------- // img = SDL_LoadBMP("./res/letters/letter-t.bmp"); texture = SDL_CreateTextureFromSurface(renderer, img); SDL_FreeSurface(img); res.arr[cc] = texture; cc += 1 ; // -------------------------------------------------------- // img = SDL_LoadBMP("./res/letters/letter-u.bmp"); texture = SDL_CreateTextureFromSurface(renderer, img); SDL_FreeSurface(img); res.arr[cc] = texture; cc += 1 ; // -------------------------------------------------------- // img = SDL_LoadBMP("./res/letters/letter-v.bmp"); texture = SDL_CreateTextureFromSurface(renderer, img); SDL_FreeSurface(img); res.arr[cc] = texture; cc += 1 ; // -------------------------------------------------------- // img = SDL_LoadBMP("./res/letters/letter-w.bmp"); texture = SDL_CreateTextureFromSurface(renderer, img); SDL_FreeSurface(img); res.arr[cc] = texture; cc += 1 ; // -------------------------------------------------------- // img = SDL_LoadBMP("./res/letters/letter-x.bmp"); texture = SDL_CreateTextureFromSurface(renderer, img); SDL_FreeSurface(img); res.arr[cc] = texture; cc += 1 ; // -------------------------------------------------------- // img = SDL_LoadBMP("./res/letters/letter-y.bmp"); texture = SDL_CreateTextureFromSurface(renderer, img); SDL_FreeSurface(img); res.arr[cc] = texture; cc += 1 ; // -------------------------------------------------------- // img = SDL_LoadBMP("./res/letters/letter-z.bmp"); texture = SDL_CreateTextureFromSurface(renderer, img); SDL_FreeSurface(img); res.arr[cc] = texture; res.len = 26 ; letters = res; } void free_digits(imgs dgts) { for(int i = 0; i < dgts.len; i++) { SDL_DestroyTexture(dgts.arr[i]); } free(dgts.arr); }