initial commit

This commit is contained in:
Alexandre 2024-12-30 12:02:08 +01:00
commit 01d87ab3b6
65 changed files with 1134 additions and 0 deletions

31
Makefile Normal file
View File

@ -0,0 +1,31 @@
CC = gcc
FLAGS = -Wall -Wextra -Wpedantic -g
LFLAGS = -lSDL2 -lSDL2_image -lm -lncurses
all: bin/back
test: bin/back
bin/back
bin/back: obj/main.o obj/generation.o obj/display.o obj/base.o obj/hash.o
mkdir -p bin
$(CC) $(FLAGS) $^ $(LFLAGS) -o $@
obj/%.o: src/%.c
@mkdir -p obj
$(CC) -o $@ -c $(FLAGS) $<
obj/main.o: src/main.c
obj/generation.o: src/generation.c
obj/display.o: src/display.c
obj/base.o: src/base.c
obj/hash.o: src/hash.c
.PHONY: clean mrproper
clean:
rm -rf obj/
mrproper: clean
rm -rf bin/

BIN
bin/back Executable file

Binary file not shown.

BIN
obj/base.o Normal file

Binary file not shown.

BIN
obj/display.o Normal file

Binary file not shown.

BIN
obj/generation.o Normal file

Binary file not shown.

BIN
obj/hash.o Normal file

Binary file not shown.

BIN
obj/main.o Normal file

Binary file not shown.

BIN
res/digits/big-sign-sum.bmp Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 123 KiB

BIN
res/digits/digit-0.bmp Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 31 KiB

BIN
res/digits/digit-1.bmp Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 31 KiB

BIN
res/digits/digit-2.bmp Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 31 KiB

BIN
res/digits/digit-3.bmp Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 31 KiB

BIN
res/digits/digit-4.bmp Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 31 KiB

BIN
res/digits/digit-5.bmp Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 31 KiB

BIN
res/digits/digit-6.bmp Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 31 KiB

BIN
res/digits/digit-7.bmp Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 31 KiB

BIN
res/digits/digit-8.bmp Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 31 KiB

BIN
res/digits/digit-9.bmp Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 31 KiB

BIN
res/digits/sign-minus.bmp Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 31 KiB

BIN
res/digits/sign-plus.bmp Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 31 KiB

BIN
res/digits/sign-prod.bmp Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 31 KiB

BIN
res/digits/sum-k.bmp Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 31 KiB

BIN
res/letters/letter-a.bmp Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 31 KiB

BIN
res/letters/letter-b.bmp Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 31 KiB

BIN
res/letters/letter-c.bmp Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 31 KiB

BIN
res/letters/letter-d.bmp Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 31 KiB

BIN
res/letters/letter-e.bmp Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 31 KiB

BIN
res/letters/letter-f.bmp Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 31 KiB

BIN
res/letters/letter-g.bmp Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 31 KiB

BIN
res/letters/letter-h.bmp Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 31 KiB

BIN
res/letters/letter-i.bmp Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 31 KiB

BIN
res/letters/letter-j.bmp Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 31 KiB

BIN
res/letters/letter-k.bmp Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 31 KiB

BIN
res/letters/letter-l.bmp Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 31 KiB

BIN
res/letters/letter-m.bmp Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 31 KiB

BIN
res/letters/letter-n.bmp Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 31 KiB

BIN
res/letters/letter-o.bmp Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 31 KiB

BIN
res/letters/letter-p.bmp Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 31 KiB

BIN
res/letters/letter-q.bmp Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 31 KiB

BIN
res/letters/letter-r.bmp Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 31 KiB

BIN
res/letters/letter-s.bmp Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 31 KiB

BIN
res/letters/letter-t.bmp Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 31 KiB

BIN
res/letters/letter-u.bmp Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 31 KiB

BIN
res/letters/letter-v.bmp Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 31 KiB

BIN
res/letters/letter-w.bmp Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 31 KiB

BIN
res/letters/letter-x.bmp Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 31 KiB

BIN
res/letters/letter-y.bmp Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 31 KiB

BIN
res/letters/letter-z.bmp Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 31 KiB

BIN
res/letters/ranking-A.png Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 62 KiB

BIN
res/letters/ranking-B.png Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 63 KiB

BIN
res/letters/ranking-C.png Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 41 KiB

BIN
res/letters/ranking-D.png Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 49 KiB

BIN
res/letters/ranking-S.png Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 49 KiB

BIN
res/letters/ranking-X.png Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 61 KiB

BIN
res/letters/symbol-percent.bmp Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 31 KiB

516
src/base.c Normal file
View File

@ -0,0 +1,516 @@
#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);
}

60
src/base.h Normal file
View File

@ -0,0 +1,60 @@
#ifndef BACK_BASE_H
#define BACK_BASE_H
int ln_baseN(int n, int b);
int pw(int x, int n);
int abs(int n);
int min(int a, int b);
int max(int a, int b);
double absf(double n);
int convex_seg(int x1, int x2, double theta);
bool is_an_integer(char c);
double to_double(int n);
int to_int(double n);
double distance_pt(int x1, int x2, int y1, int y2);
int line_count(char* filename);
int str_to_int(char* s);
int get_integer(FILE* ptr);
int get_integer_plus_align(FILE* ptr, FILE* ptr2);
int count_char_in_line(FILE* ptr, char c);
void terminate_line(FILE* ptr);
bool str_equal(char* s1, char* s2);
void linked_add(linkedList* lst, int x, int y, char* flag);
void linked_removeCoord(linkedList* lst, int x, int y);
void linked_removeFlag(linkedList* lst, char* flag);
void linked_change(linkedList* lst, int x, int y, char* flag);
bool linked_mem(linkedList* lst, int x, int y, char** flag);
linkedList* linked_copy(linkedList* src);
void linkedPrint(linkedList* lst);
void import_digits(SDL_Renderer* renderer);
void import_letters(SDL_Renderer* renderer);
void free_digits(imgs dgts);
#endif

137
src/display.c Normal file
View File

@ -0,0 +1,137 @@
#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"
#include "generation.h"
#include "display.h"
void updateRenderer(SDL_Renderer* renderer) {
//printf("E");
SDL_RenderPresent(renderer);
}
void resetRenderer(SDL_Renderer* renderer) {
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderClear(renderer);
}
void drawRectToRenderer(SDL_Renderer* renderer, SDL_Rect* rect, int R, int G, int B, int A) {
SDL_SetRenderDrawColor(renderer, R, G, B, A);
SDL_RenderFillRect(renderer, rect);
}
void placeRectToRenderer(SDL_Renderer* renderer, int X, int Y, int W, int H, int R, int G, int B, int A) {
SDL_Rect rect;
rect.x = X;
rect.y = Y;
rect.w = W;
rect.h = H;
SDL_SetRenderDrawColor(renderer, R, G, B, A);
SDL_RenderFillRect(renderer, &rect);
}
void drawLineWithThicc(SDL_Renderer* renderer, int width, int x1, int x2, int y1, int y2, int R, int G, int B, int A) {
// draws line with width (native SDL cannot do that)
double theta = 0.0;
double seglen = distance_pt(x1, x2, y1, y2);
while(theta < 1.0) {
placeRectToRenderer(renderer, convex_seg(x1, x2, theta)-width/2, convex_seg(y1, y2, theta)-width/2, width, width, R, G, B, A);
theta += 1 / seglen;
}
}
void drawLineWithThiccGradient(SDL_Renderer* renderer, int start_width, int end_width, int x1, int x2, int y1, int y2, int R, int G, int B, int A) {
// draws line with width ;
// width goes from start_width to end_with in a linear way
double theta = 0.0;
double seglen = distance_pt(x1, x2, y1, y2);
while(theta < 1.0) {
int width = convex_seg(start_width, end_width, theta) ;
placeRectToRenderer(renderer, convex_seg(x1, x2, theta)-width/2, convex_seg(y1, y2, theta)-width/2, width, width, R, G, B, A);
theta += 1 / seglen;
}
}
void drawDigitToRenderer(SDL_Renderer* renderer, imgs data, int digit, int X, int Y, int W, int H) {
if(digit == -727) {
SDL_Rect rect;
rect.x = X;
rect.y = Y;
rect.w = W;
rect.h = H;
SDL_Texture* texture = data.arr[10];
SDL_RenderCopy(renderer, texture, NULL, &rect);
} else if (!(0 <= digit && digit <= 9)) {
fprintf(stderr, "Illegal digit : '%d'.\n", digit);
exit(1);
} else {
SDL_Rect rect;
rect.x = X;
rect.y = Y;
rect.w = W;
rect.h = H;
SDL_Texture* texture = data.arr[digit];
SDL_RenderCopy(renderer, texture, NULL, &rect);
}
}
void drawCharToRenderer(SDL_Renderer* renderer, imgs data, char c, int X, int Y, int W, int H) {
if ((int)c >= 97 && (int)c <= 122) {
SDL_Rect rect;
rect.x = X;
rect.y = Y;
rect.w = W;
rect.h = H;
SDL_Texture* texture = data.arr[(int)c - 97];
SDL_RenderCopy(renderer, texture, NULL, &rect);
}
}
void drawNumberToRenderer(SDL_Renderer* renderer, imgs data, int n, int X, int Y, int W, int H, int Woffset) {
if(n == 0) {
drawDigitToRenderer(renderer, data, 0, X + W, Y, W, H);
} else if(n > 0) {
int toDraw = 0, remaining = n, nIter = ln_baseN(n, 10);
while(nIter != 0) {
toDraw = remaining%10;
remaining = remaining / 10;
drawDigitToRenderer(renderer, data, toDraw, X + (W-Woffset)*nIter, Y, W, H);
nIter--;
}
} else {
int toDraw = 0, remaining = -n, nIter = ln_baseN(-n, 10);
drawDigitToRenderer(renderer, data, -727, X, Y, W, H);
while(nIter != 0) {
toDraw = remaining%10;
remaining = remaining / 10;
drawDigitToRenderer(renderer, data, toDraw, X + (W-Woffset)*nIter, Y, W, H);
nIter--;
}
}
}
void drawStringToRenderer(SDL_Renderer* renderer, imgs data, char* s, int X, int Y, int W, int H) {
int k = 0 ;
while(s[k] != '\0') {
drawCharToRenderer(renderer, data, s[k], X + W*k, Y, W, H);
k += 1;
}
}

24
src/display.h Normal file
View File

@ -0,0 +1,24 @@
#ifndef DISPLAY_H
#define DISPLAY_H
void updateRenderer(SDL_Renderer* renderer);
void resetRenderer(SDL_Renderer* renderer);
void drawRectToRenderer(SDL_Renderer* renderer, SDL_Rect* rect, int R, int G, int B, int A);
void placeRectToRenderer(SDL_Renderer* renderer, int X, int Y, int W, int H, int R, int G, int B, int A);
void drawLineWithThicc(SDL_Renderer* renderer, int width, int x1, int x2, int y1, int y2, int R, int G, int B, int A);
void drawLineWithThiccGradient(SDL_Renderer* renderer, int start_width, int end_width, int x1, int x2, int y1, int y2, int R, int G, int B, int A);
void drawDigitToRenderer(SDL_Renderer* renderer, imgs data, int digit, int X, int Y, int W, int H);
void drawCharToRenderer(SDL_Renderer* renderer, imgs data, char c, int X, int Y, int W, int H);
void drawNumberToRenderer(SDL_Renderer* renderer, imgs data, int n, int X, int Y, int W, int H, int Woffset);
void drawStringToRenderer(SDL_Renderer* renderer, imgs data, char* s, int X, int Y, int W, int H);
#endif

19
src/generation.c Normal file
View File

@ -0,0 +1,19 @@
#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"
#include "generation.h"
// ------------------------------------------------------------------------ //

6
src/generation.h Normal file
View File

@ -0,0 +1,6 @@
#ifndef GEN_H
#define GEN_H
#endif

196
src/hash.c Normal file
View File

@ -0,0 +1,196 @@
#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"
// Cantor pairing function
int cantor(int x, int y) {
return ((x + y) * (x + y + 1) / 2) + y;
}
int generate_hash(int x, int y) {
if (x < 0) {
x = -2*x - 1;
} else {
x = 2 * x;
}
if (y < 0) {
y = -2*y - 1;
} else {
y = 2 * y;
}
return cantor(x, y);
}
void freeGrid(Grid grid) {
for (int i = 0; i < grid->capacity; i++) {
List list = grid->data[i];
while (list != NULL) {
List next = list->next;
free(list);
list = next;
}
}
free(grid->data);
free(grid);
}
void subGridInsert(Grid grid, int hash, chunk* elt, int x, int y) {
int index = hash % grid->capacity;
//printf(" inserting at index [%d]", index);
List* data = grid->data;
if (data[index] == NULL) {
//printf(" <-->\n");
List list = malloc(sizeof(struct List));
list->hash = hash;
list->x = x;
list->y = y;
list->elt = elt;
list->next = NULL;
grid->size++;
data[index] = list;
} else {
//printf(" <>\n");
List list = data[index];
while (list->next != NULL && list->hash != hash) {
list = list->next;
};
//printf(" <>\n");
if (list->x == x && list->y == y) {
list->elt = elt;
} else {
List nex = malloc(sizeof(struct List));
nex->hash = hash;
nex->x = x;
nex->y = y;
nex->elt = elt;
nex->next = NULL;
list->next = nex;
grid->size++;
}
}
}
void gridInsert(Grid grid, int x, int z, chunk* elt) {
if (grid->size >= grid->capacity * grid->loadFactor) {
grid->size = 0;
grid->capacity *= 2;
printf("resizing\n");
List* old_data = grid->data;
grid->data = malloc(sizeof(List) * grid->capacity);;
for (int i = 0; i < grid->capacity; i++) {
grid->data[i] = NULL;
};
for (int i = 0; i < grid->capacity/2; i++) {
List list = old_data[i];
while (list != NULL) {
subGridInsert(grid, list->hash, list->elt, list->x, list->y);
list = list->next;
}
}
};
int hash = generate_hash(x, z);
subGridInsert(grid, hash, elt, x, z);
}
chunk* gridGet(Grid grid, int x, int z) {
int hash = generate_hash(x, z);
int index = hash % grid->capacity;
List list = grid->data[index];
while (list != NULL && (list->x != x || list->y != z)) {
list = list->next;
}
if (list == NULL) {
return NULL;
}
return list->elt;
}
bool gridMem(Grid grid, int x, int z) {
int hash = generate_hash(x, z);
int index = hash % grid->capacity;
List list = grid->data[index];
while (list != NULL && (list->x != x || list->y != z)) {
list = list->next;
}
if (list == NULL) {
return false;
}
return true;
}
chunk* gridRemove(Grid grid, int x, int z) {
int hash = generate_hash(x, z);
int index = hash % grid->capacity;
List list = grid->data[index];
List prev = NULL;
while (list != NULL && list->hash != hash) {
prev = list;
list = list->next;
}
if (list == NULL) {
return NULL;
}
chunk* elt = list->elt;
if (prev == NULL) {
grid->data[index] = list->next;
} else {
prev->next = list->next;
}
free(list);
grid->size--;
return elt;
}
Grid newGrid() {
Grid grid = malloc(sizeof(struct Grid));
grid->data = malloc(sizeof(List) * 800);
grid->size = 0;
grid->capacity = 800;
grid->loadFactor = 0.75;
for (int i = 0; i < grid->capacity; i++) {
grid->data[i] = NULL;
}
grid->push = &gridInsert;
grid->pop = &gridRemove;
grid->get = &gridGet;
grid->mem = &gridMem;
return grid;
}
chunk** gridIter(Grid grid) {
chunk** array = malloc(sizeof(chunk*) * grid->size);
int index = 0;
for (int i = 0; i < grid->capacity; i++) {
List list = grid->data[i];
while (list != NULL) {
array[index++] = list->elt;
list = list->next;
}
}
return array;
}
ChunkCoord newChunkCoord(int x, int z) {
ChunkCoord coord = malloc(sizeof(struct ChunkCoord));
coord->x = x;
coord->z = z;
return coord;
}
void freeChunkCoord(ChunkCoord coord) {
free(coord);
}

75
src/hash.h Normal file
View File

@ -0,0 +1,75 @@
// credit to Benoit //
#ifndef GRID_H
#define GRID_H
#include <stdbool.h>
typedef struct linkedList {
uint8_t coord ; // coord%8 = x ; coord/16 = y
char* flag ;
struct linkedList* next ;
} linkedList ;
typedef struct template {
uint8_t id ;
uint8_t* lines ; // len = 8
bool checkCompat ;
uint8_t eastsig ;
uint8_t westsig ;
linkedList* meta ;
} template ;
typedef struct chunk {
int16_t chx ;
int16_t chy ;
int draw_id ;
template chdata ;
} chunk ;
//
typedef struct List {
int hash;
int x;
int y;
chunk* elt;
struct List* next;
} *List;
typedef struct Grid {
List* data;
int capacity;
int size;
double loadFactor;
void (*push)(struct Grid *grid, int x, int z, chunk* elt);
chunk* (*pop)(struct Grid *grid, int x, int z);
chunk* (*get)(struct Grid *grid, int x, int z);
bool (*mem)(struct Grid *grid, int x, int z);
} *Grid;
int generate_hash(int x, int y);
Grid newGrid();
void freeGrid(Grid grid);
void gridInsert(Grid grid, int x, int z, chunk* elt);
chunk* gridGet(Grid grid, int x, int z);
bool gridMem(Grid grid, int x, int z);
chunk* gridRemove(Grid grid, int x, int z);
chunk** gridIter(Grid grid);
typedef struct ChunkCoord {
int x;
int z;
} *ChunkCoord;
ChunkCoord newChunkCoord(int x, int z);
void freeChunkCoord(ChunkCoord coord);
#endif

58
src/main.c Normal file
View File

@ -0,0 +1,58 @@
#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"
#include "display.h"
#include "generation.h"
int main(int argc, char** argv) {
srand(time(NULL));
//-------------------------------------------------------------------------------//
if (SDL_Init(SDL_INIT_EVERYTHING) != 0) {
printf("error initializing SDL: %s\n", SDL_GetError());
}
SDL_Window* win = SDL_CreateWindow("Game",
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
1000, 1000, 0);
Uint32 render_flags = SDL_RENDERER_ACCELERATED;
SDL_Renderer* rend = SDL_CreateRenderer(win, -1, render_flags);
SDL_SetRenderDrawBlendMode(rend, SDL_BLENDMODE_BLEND);
//-------------------------------------------------------------------------------//
if(SDL_Init(SDL_INIT_AUDIO)) {
fprintf(stderr, "cannot initialize audio");
exit(1);
}
/* -------------------------------------------------------- */
/* -------------------------------------------------------- */
SDL_DestroyRenderer(rend);
SDL_DestroyWindow(win);
SDL_Quit();
/* -------------------------------------------------------- */
printf("Done\n") ;
return 0;
}

12
src/structure.h Normal file
View File

@ -0,0 +1,12 @@
#ifndef BACK_CONSTS_H
#define BACK_CONSTS_H
typedef struct imgs {
int len;
SDL_Texture** arr;
} imgs ;
extern imgs digits ;
extern imgs letters ;
#endif