diff --git a/.vscode/settings.json b/.vscode/settings.json index f314253..eaa918d 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -2,6 +2,7 @@ "files.associations": { "ncurses.h": "c", "stdio.h": "c", - "math.h": "c" + "math.h": "c", + "sdl_image.h": "c" } } \ No newline at end of file diff --git a/bin/back b/bin/back index 1559060..b31b648 100755 Binary files a/bin/back and b/bin/back differ diff --git a/obj/base.o b/obj/base.o index b28fbe8..133f544 100644 Binary files a/obj/base.o and b/obj/base.o differ diff --git a/obj/display.o b/obj/display.o index f116f47..8160256 100644 Binary files a/obj/display.o and b/obj/display.o differ diff --git a/obj/hash.o b/obj/hash.o index 6d1374f..9bd57e8 100644 Binary files a/obj/hash.o and b/obj/hash.o differ diff --git a/obj/main.o b/obj/main.o index c5cdc7f..e18d15d 100644 Binary files a/obj/main.o and b/obj/main.o differ diff --git a/obj/move.o b/obj/move.o index 15d3f02..8276ed6 100644 Binary files a/obj/move.o and b/obj/move.o differ diff --git a/src/base.c b/src/base.c index 7d3fd3e..a040a1f 100644 --- a/src/base.c +++ b/src/base.c @@ -109,66 +109,6 @@ int str_to_int(char* s) { 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'); diff --git a/src/base.h b/src/base.h index b29af16..3adcaed 100644 --- a/src/base.h +++ b/src/base.h @@ -13,10 +13,6 @@ double to_double(int n); int to_int(double n); 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); cube_0 create_cube_0(double x, double y, double z, double w, double h, double d, int r, int g, int b); diff --git a/src/hash.c b/src/hash.c index ea62a13..1951cfc 100644 --- a/src/hash.c +++ b/src/hash.c @@ -11,9 +11,66 @@ #include #include +#include "structure.h" #include "hash.h" -// Cantor pairing function +// ------------------------------------------------------------------------------------------------------- // + +bool linkedList_mem(linkedList lst, int chx, int chy) { + if(lst == NULL) { + return false ; + } else if(lst->chx == chx && lst->chy == chy) { + return true ; + } + return linkedList_mem(lst->next, chx, chy) ; +} + +room linkedList_find(linkedList lst, int chx, int chy) { + if(lst == NULL) { + fprintf(stderr, "ERROR : (%d, %d) is not a valid key in hash\n", chx, chy); + exit(1); + } else if(lst->chx == chx && lst->chy == chy) { + return *(lst->area) ; + } + return linkedList_find(lst->next, chx, chy) ; +} + +room* linkedList_find_opt(linkedList lst, int chx, int chy) { + if(lst == NULL) { + return NULL ; + } else if(lst->chx == chx && lst->chy == chy) { + return (lst->area) ; + } + return linkedList_find_opt(lst->next, chx, chy) ; +} + +void linkedList_add(linkedList lst, int chx, int chy, room* area) { + if(lst == NULL) { + fprintf(stderr, "ERROR : cannot add to NULL\n"); + exit(1) ; + } else if(lst->next == NULL) { + cell* nex = malloc(sizeof(cell)); + nex->area = area ; + nex->chx = chx ; + nex->chy = chy ; + nex->next = NULL ; + lst->next = nex ; + } else { + linkedList_add(lst->next, chx, chy, area) ; + } +} + +void linkedList_free(linkedList lst) { + // frees rooms as well (require all created rooms to be there) + if(lst != NULL) { + linkedList_free(lst->next); + free(lst->area); + free(lst); + } +} + +// ------------------------------------------------------------------------------------------------------- // + int cantor(int x, int y) { return ((x + y) * (x + y + 1) / 2) + y; } @@ -32,165 +89,51 @@ int generate_hash(int x, int 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; - } +hashtbl hashtbl_generate(int initial_size) { + hashtbl h = malloc(sizeof(hashtbl_0)); + h->insertedElts = 0 ; + h->tabLength = initial_size ; + h->maxInserted = initial_size/2 ; + h->tab = malloc(sizeof(linkedList)*initial_size) ; + for(int k = 0; k < initial_size; k++) { + h->tab[k] = NULL ; } - free(grid->data); - free(grid); + return h ; } -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; +bool hashtbl_mem(hashtbl tbl, int chx, int chy) { + int id = generate_hash(chx, chy)%(tbl->tabLength) ; + return linkedList_mem(tbl->tab[id], chx, chy) ; +} + +room hashtbl_find(hashtbl tbl, int chx, int chy) { + int id = generate_hash(chx, chy)%(tbl->tabLength) ; + return linkedList_find(tbl->tab[id], chx, chy) ; +} + +room* hashtbl_find_opt(hashtbl tbl, int chx, int chy) { + int id = generate_hash(chx, chy)%(tbl->tabLength) ; + return linkedList_find_opt(tbl->tab[id], chx, chy) ; +} + +void hashtbl_add(hashtbl tbl, int chx, int chy, room* area) { + int id = generate_hash(chx, chy)%(tbl->tabLength) ; + tbl->insertedElts += 1 ; + if(tbl->tab[id] != NULL) { + linkedList_add(tbl->tab[id], chx, chy, area); } 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++; - } + linkedList nex = malloc(sizeof(cell)); + nex->area = area ; + nex->chx = chx ; + nex->chy = chy ; + nex->next = NULL ; + tbl->tab[id] = nex ; } } -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; +void hashtbl_free(hashtbl tbl) { + for(int k = 0; k < tbl->tabLength; k++) { + free(tbl->tab[k]); } - 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); -} + free(tbl); +} \ No newline at end of file diff --git a/src/hash.h b/src/hash.h index 453276b..c939aea 100644 --- a/src/hash.h +++ b/src/hash.h @@ -1,75 +1,26 @@ // credit to Benoit // -#ifndef GRID_H -#define GRID_H +// (int * int) -> room* + +#ifndef HASH_H +#define HASH_H +#include "structure.h" #include -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; +bool linkedList_mem(linkedList lst, int chx, int chy); +room linkedList_find(linkedList lst, int chx, int chy); +room* linkedList_find_opt(linkedList lst, int chx, int chy); +void linkedList_add(linkedList lst, int chx, int chy, room* area); +void linkedList_free(linkedList lst); +int cantor(int x, int y); 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); +hashtbl hashtbl_generate(int initial_size); +bool hashtbl_mem(hashtbl tbl, int chx, int chy); +room hashtbl_find(hashtbl tbl, int chx, int chy); +room* hashtbl_find_opt(hashtbl tbl, int chx, int chy); +void hashtbl_add(hashtbl tbl, int chx, int chy, room* area); +void hashtbl_free(hashtbl tbl); #endif \ No newline at end of file diff --git a/src/main.c b/src/main.c index 4874e5b..5b79a52 100644 --- a/src/main.c +++ b/src/main.c @@ -34,6 +34,7 @@ int main(int argc, char** argv) { 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)) { diff --git a/src/move.c b/src/move.c index 52c79b0..32c251e 100644 --- a/src/move.c +++ b/src/move.c @@ -67,16 +67,18 @@ void playerActions() { has_changed = true ; switch (event.key.keysym.sym) { case SDLK_z: - camz += speed*cos(rot_hz); - camx -= speed*sin(rot_hz); + camz += speed*cos(rot_hz)*cos(rot_vt); + camx -= speed*sin(rot_hz)*cos(rot_vt); + camy += speed*sin(rot_vt); break; case SDLK_q: camx -= speed*cos(rot_hz); camz -= speed*sin(rot_hz); break; case SDLK_s: - camz -= speed*cos(rot_hz); - camx += speed*sin(rot_hz); + camz -= speed*cos(rot_hz)*cos(rot_vt); + camx += speed*sin(rot_hz)*cos(rot_vt); + camy -= speed*sin(rot_vt); break; case SDLK_d: camx += speed*cos(rot_hz); diff --git a/src/structure.h b/src/structure.h index 2abb785..624d31c 100644 --- a/src/structure.h +++ b/src/structure.h @@ -20,6 +20,43 @@ struct cube_0 { typedef struct cube_0 cube_0 ; typedef cube_0* cube ; +typedef struct teleporter { + cube_0 hitbox ; + int dest_chx ; + int dest_chy ; + double dest_x ; + double dest_y ; +} teleporter ; + +struct room { + // (0, 0, 0) = bottom, left and down + int chunk_x ; + int chunk_y ; + cube* map ; +} ; +typedef struct room room ; + +struct cell { + int chx ; + int chy ; + room* area ; + struct cell* next ; +} ; + +typedef struct cell cell ; +typedef struct cell* linkedList ; + +struct hashtbl_0 { + int tabLength ; + int insertedElts ; + int maxInserted ; + linkedList* tab ; +} ; +typedef struct hashtbl_0 hashtbl_0 ; +typedef hashtbl_0* hashtbl ; + +// ------------------------------------------------ // + extern imgs digits ; extern imgs letters ; @@ -34,4 +71,6 @@ extern double tan_fov ; extern bool has_changed ; +extern hashtbl visited ; + #endif \ No newline at end of file