516 lines
14 KiB
C
516 lines
14 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <assert.h>
|
|
#include <math.h>
|
|
#include <stdbool.h>
|
|
#include <ncurses.h>
|
|
#include <unistd.h>
|
|
#include <termios.h>
|
|
#include <limits.h>
|
|
#include <time.h>
|
|
#include <SDL2/SDL.h>
|
|
#include <SDL2/SDL_image.h>
|
|
|
|
#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);
|
|
}
|
|
|
|
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 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 ;
|
|
}
|
|
|
|
double distance_pt(int x1, int x2, int y1, int y2) {
|
|
return sqrt(to_double(pw(x2 - x1, 2) + pw(y2 - y1, 2)));
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
int get_integer(FILE* ptr) {
|
|
char c = fgetc(ptr);
|
|
|
|
int res = 0 ;
|
|
int sign = 1;
|
|
|
|
if(c == '-') {
|
|
sign = -1;
|
|
c = fgetc(ptr);
|
|
};
|
|
|
|
while(is_an_integer(c)) {
|
|
res = 10*res + (int)c - 48;
|
|
c = fgetc(ptr);
|
|
};
|
|
return (res*sign);
|
|
}
|
|
|
|
int get_integer_plus_align(FILE* ptr, FILE* ptr2) {
|
|
char c = fgetc(ptr);
|
|
char c2 = fgetc(ptr2);
|
|
|
|
int res = 0 ;
|
|
int sign = 1;
|
|
|
|
if(c == '-') {
|
|
sign = -1;
|
|
c = fgetc(ptr);
|
|
c2 = fgetc(ptr2);
|
|
};
|
|
|
|
while(is_an_integer(c)) {
|
|
res = 10*res + (int)c - 48;
|
|
c = fgetc(ptr);
|
|
c2 = fgetc(ptr2);
|
|
};
|
|
return (res*sign);
|
|
}
|
|
|
|
int count_char_in_line(FILE* ptr, char target) {
|
|
int res = 0 ;
|
|
char c = fgetc(ptr);
|
|
|
|
while(c != EOF && c != '\n') {
|
|
if(c == target) {
|
|
res += 1;
|
|
}
|
|
c = fgetc(ptr);
|
|
};
|
|
|
|
return res;
|
|
}
|
|
|
|
void terminate_line(FILE* ptr) {
|
|
char c = fgetc(ptr);
|
|
while(c != '\n' && c != EOF) {
|
|
c = fgetc(ptr);
|
|
}
|
|
}
|
|
|
|
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]));
|
|
}
|
|
|
|
// ------------------------------------------------------------------------------------------------ //
|
|
|
|
void linked_add(linkedList* lst, int x, int y, char* flag) {
|
|
if(lst == NULL) {
|
|
fprintf(stderr, "ERROR : linked list has not been initialized\n");
|
|
exit(1);
|
|
} else if(lst->next == NULL) {
|
|
lst->next = malloc(sizeof(linkedList));
|
|
lst->next->coord = x + 16*y ;
|
|
lst->next->flag = flag ;
|
|
lst->next->next = NULL ;
|
|
} else {
|
|
linked_add(lst->next, x, y, flag);
|
|
}
|
|
}
|
|
|
|
void linked_removeCoord(linkedList* lst, int x, int y) {
|
|
if(lst != NULL) {
|
|
if(lst->coord == x + 16*y) {
|
|
linkedList* temp = lst->next ;
|
|
free(lst) ;
|
|
lst = temp ;
|
|
} else {
|
|
linked_removeCoord(lst->next, x, y);
|
|
}
|
|
}
|
|
}
|
|
|
|
void linked_removeFlag(linkedList* lst, char* flag) {
|
|
if(lst != NULL) {
|
|
if(lst->flag == flag) {
|
|
linkedList* temp = lst->next ;
|
|
free(lst) ;
|
|
lst = temp ;
|
|
} else {
|
|
linked_removeFlag(lst->next, flag);
|
|
}
|
|
}
|
|
}
|
|
|
|
void linked_change(linkedList* lst, int x, int y, char* flag) {
|
|
linked_removeCoord(lst, x, y);
|
|
linked_add(lst, x, y, flag);
|
|
}
|
|
|
|
bool linked_mem(linkedList* lst, int x, int y, char** flag) {
|
|
if(lst == NULL) {
|
|
return false;
|
|
}
|
|
if(lst->coord == x + 16*y) {
|
|
*flag = lst->flag;
|
|
return true ;
|
|
}
|
|
return linked_mem(lst->next, x, y, flag);
|
|
}
|
|
|
|
linkedList* linked_copy(linkedList* src) {
|
|
linkedList* new = malloc(sizeof(linkedList)) ;
|
|
new->flag = "E" ;
|
|
new->coord = 0 ;
|
|
new->next = NULL ;
|
|
|
|
//printf("in\n");
|
|
linkedList* curSrc = src->next ;
|
|
//printf("out\n");
|
|
|
|
while(curSrc != NULL) {
|
|
//printf("cp\n");
|
|
linked_add(new, curSrc->coord%8, curSrc->coord/16, curSrc->flag);
|
|
curSrc = curSrc->next;
|
|
}
|
|
|
|
return new ;
|
|
}
|
|
|
|
void linkedPrint(linkedList* lst) {
|
|
if(lst != NULL) {
|
|
printf("[(%d, %d), %s] ", lst->coord%8, lst->coord/16, lst->flag);
|
|
linkedPrint(lst->next);
|
|
}
|
|
}
|
|
|
|
// ------------------------------------------------------------------------------------------------ //
|
|
|
|
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);
|
|
} |