Initial commit (engine done)

This commit is contained in:
Alexandre 2024-08-22 21:57:20 +02:00
commit 5562be9370
71 changed files with 1579 additions and 0 deletions

11
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,11 @@
{
"files.associations": {
"structure.h": "c",
"generation.h": "c",
"display.h": "c",
"sdl_image.h": "c",
"sdl.h": "c",
"base.h": "c",
"limits": "c"
}
}

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 obj/move.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
obj/move.o: src/move.c
.PHONY: clean mrproper
clean:
rm -rf obj/
mrproper: clean
rm -rf bin/

7
README.md Normal file
View File

@ -0,0 +1,7 @@
** intended to be a backrooms generator **
** with configurable room pool **
| COMMANDS |
> zqsd : move
> TAB : exit
> p : zoom out
> m : zoom in

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
obj/move.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

422
src/base.c Normal file
View File

@ -0,0 +1,422 @@
#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"
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 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 arr_mem(array arr, int elt) {
for(int i = 0; i < arr.len; i++) {
if(arr.arr[i] == elt) {
return true;
}
};
return false;
}
// ------------------------------------------------------------------------------------------------ //
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);
}

44
src/base.h Normal file
View File

@ -0,0 +1,44 @@
#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 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 arr_mem(array arr, int elt);
void import_digits(SDL_Renderer* renderer);
void import_letters(SDL_Renderer* renderer);
void free_digits(imgs dgts);
#endif

213
src/display.c Normal file
View File

@ -0,0 +1,213 @@
#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 = 1+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;
}
}
void drawChunkToRenderer(SDL_Renderer* renderer, chunk ch, int xmin, int xmax, int ymin, int ymax) {
if(ch.draw_id != draw_par) {
ch.draw_id = draw_par ;
for(int i = 0; i < 8; i++) {
uint8_t cur = ch.chdata.lines[i] ;
for(int j = 0; j < 8; j++) {
int cux = 50*(8*ch.chx + i);
int cuy = 50*(8*ch.chy + j);
int cux2 = 50*(8*ch.chx + i+1);
int cuy2 = 50*(8*ch.chy + j+1);
if(true) {
//if(cux - 50/zoom > xmin || cux - 50/zoom < xmax || cuy > ymin || cuy < ymax) {
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)) ;
placeRectToRenderer(renderer, nxmin, nymin, nxmin2 - nxmin, nymin2 - nymin, 255, 255, 255, SDL_ALPHA_OPAQUE);
};
cur = cur / 2 ;
}
}
}
}
}
void drawHashToRenderer(SDL_Renderer* renderer, int chx, int chy, int xmin, int xmax, int ymin, int ymax, int dist) {
//printf("[DEBUG]%d / %d\n", dist, render_distance);
if(dist < render_distance) {
//printf("[DEBUG] drawing chunk (%d, %d)\n", chx, chy);
chunk* todraw = gridGet(map, chx, chy) ;
if(todraw == NULL) {
fprintf(stderr, "NO\n");
exit(1);
};
drawChunkToRenderer(renderer, *todraw, xmin, xmax, ymin, ymax);
//printf("[DEBUG] drawn chunk (%d, %d)\n", chx, chy);
if(!gridMem(map, chx+1, chy)) {
//printf("[DEBUG] (%d, %d) from (%d, %d)\n", chx+1, chy, chx, chy);
//select_config(*gridGet(map, chx, chy), EAST);
generate_chunk_all(chx+1, chy);
//printf("[DEBUG] done\n");
};
if(!gridMem(map, chx-1, chy)) {
//printf("[DEBUG] (%d, %d) from (%d, %d)\n", chx-1, chy, chx, chy);
//select_config(*gridGet(map, chx, chy), WEST);
generate_chunk_all(chx-1, chy);
//printf("[DEBUG] done\n");
};
if(!gridMem(map, chx, chy+1)) {
//printf("[DEBUG] (%d, %d) from (%d, %d)\n", chx, chy+1, chx, chy);
//select_config(*gridGet(map, chx, chy), SOUTH);
generate_chunk_all(chx, chy+1);
//printf("[DEBUG] done\n");
};
if(!gridMem(map, chx, chy-1)) {
//printf("[DEBUG] (%d, %d) from (%d, %d)\n", chx, chy-1, chx, chy);
//select_config(*gridGet(map, chx, chy), NORTH);
generate_chunk_all(chx, chy-1);
//printf("[DEBUG] done\n");
};
//printf("[DEBUG] recursive (%d, %d)\n", chx, chy);
drawHashToRenderer(renderer, chx+1, chy, xmin, xmax, ymin, ymax, dist+1);
drawHashToRenderer(renderer, chx-1, chy, xmin, xmax, ymin, ymax, dist+1);
drawHashToRenderer(renderer, chx, chy+1, xmin, xmax, ymin, ymax, dist+1);
drawHashToRenderer(renderer, chx, chy-1, xmin, xmax, ymin, ymax, dist+1);
}
}
void drawMapToRenderer(SDL_Renderer* renderer, int xmin, int xmax, int ymin, int ymax) {
drawHashToRenderer(renderer, player_cx, player_cy, xmin, xmax, ymin, ymax, 0);
placeRectToRenderer(renderer, __width__ /2 - 5, __height__ /2 + 5, 10, 10, 255, 255, 32, SDL_ALPHA_OPAQUE);
}

30
src/display.h Normal file
View File

@ -0,0 +1,30 @@
#ifndef BACK_DISPLAY_H
#define BACK_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);
void drawChunkToRenderer(SDL_Renderer* renderer, chunk ch, int xmin, int xmax, int ymin, int ymax);
void drawHashToRenderer(SDL_Renderer* renderer, int chx, int chy, int xmin, int xmax, int ymin, int ymax, int dist);
void drawMapToRenderer(SDL_Renderer* renderer, int xmin, int xmax, int ymin, int ymax);
#endif

227
src/generation.c Normal file
View File

@ -0,0 +1,227 @@
#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"
// ------------------------------------------------------------------------ //
Grid map ;
template* configs ;
array** matches ;
int n_configs = 2;
int render_distance = 3 ;
int player_x = 4;
int player_y = 4;
int player_cx = 0;
int player_cy = 0;
int zoom = 250 ;
int draw_par = 1;
int __width__ = 1200 ;
int __height__ = 800 ;
imgs digits ;
imgs letters ;
// ------------------------------------------------------------------------ //
void one_debug() {
matches = malloc(sizeof(array*)*4);
matches[0] = malloc(sizeof(array));
matches[1] = malloc(sizeof(array));
matches[2] = malloc(sizeof(array));
matches[3] = malloc(sizeof(array));
matches[0][0].arr = malloc(sizeof(int));
matches[0][0].arr[0] = 0;
matches[0][0].len = 1;
matches[1][0].arr = malloc(sizeof(int));
matches[1][0].arr[0] = 0;
matches[1][0].len = 1;
matches[2][0].arr = malloc(sizeof(int));
matches[2][0].arr[0] = 0;
matches[2][0].len = 1;
matches[3][0].arr = malloc(sizeof(int));
matches[3][0].arr[0] = 0;
matches[3][0].len = 1;
configs = malloc(sizeof(template));
configs[0].lines = malloc(sizeof(uint8_t)*8);
configs[0].lines[0] = 255;
configs[0].lines[1] = 129;
configs[0].lines[2] = 129;
configs[0].lines[3] = 129;
configs[0].lines[4] = 129;
configs[0].lines[5] = 129;
configs[0].lines[6] = 129;
configs[0].lines[7] = 255;
configs[0].id = 0;
}
// ------------------------------------------------------------------------ //
bool unpack_coord(uint8_t* lines, int cx, int cy) {
return (bool)((lines[cy]/pw(2, cx))%2);
}
bool is_compatible(chunk ch, cardinal dir, int config_id) {
return arr_mem(matches[(int)dir][ch.chdata.id], config_id);
}
void generate_chunk(int x, int y, int config_id) {
chunk* new = malloc(sizeof(chunk)) ;
new->chx = x ;
new->chy = y ;
new->draw_id = draw_par - 2;
new->chdata = configs[config_id];
//printf(" inserting\n");
gridInsert(map, x, y, new);
}
void select_config(chunk ch, cardinal dir) {
//printf(" in\n");
int i = 1 + (rand()%n_configs);
int idx = 0 ;
//printf("init : %d\n", i);
while(i > 0) {
//printf(" current : %d | %d\n", i, idx);
if(is_compatible(ch, dir, idx)) {
i -= 1;
if(i != 0) {
idx = (idx+1)%n_configs;
};
//printf(" pass\n");
} else {
idx = (idx+1)%n_configs;
//printf(" smash\n");
}
};
//printf("chosen : %d\n\n", idx);
if(dir == NORTH) {
generate_chunk(ch.chx, ch.chy - 1, idx);
} else if(dir == SOUTH) {
generate_chunk(ch.chx, ch.chy + 1, idx);
} else if(dir == EAST) {
generate_chunk(ch.chx + 1, ch.chy, idx);
} else {
generate_chunk(ch.chx - 1, ch.chy, idx);
};
//printf(" out\n");
}
bool is_compat_one(int cx, int cy, cardinal dir, int idx) {
if(!gridMem(map, cx, cy)) {
return true;
};
return is_compatible(*gridGet(map, cx, cy), dir, idx);
}
void generate_chunk_all(int cx, int cy) {
int i = 1 + (rand()%n_configs);
int idx = 0 ;
while(i > 0) {
if(is_compat_one(cx-1, cy, EAST, idx) && is_compat_one(cx+1, cy, WEST, idx) && is_compat_one(cx, cy-1, SOUTH, idx) && is_compat_one(cx, cy+1, NORTH, idx)) {
i -= 1;
if(i != 0) {
idx = (idx+1)%n_configs;
}
} else {
idx = (idx+1)%n_configs;
}
};
generate_chunk(cx, cy, idx);
}
void initialize(SDL_Renderer* renderer) {
map = newGrid();
import_letters(renderer);
import_digits(renderer);
generate_chunk(0, 0, rand()%n_configs);
}
void destroy() {
freeGrid(map);
free_digits(digits);
free_digits(letters);
}
void parse_one(FILE* ptr, FILE* ptr2, int index) {
for(int i = 0; i < 8; i++) {
for(int j = 0; j < 8; j++) {
int xres = get_integer_plus_align(ptr, ptr2);
configs[index].lines[i] *= 2 ;
configs[index].lines[i] += xres ;
}
};
for(int i = 0; i < 4; i++) {
int len = count_char_in_line(ptr2, ',');
matches[i][index].len = len ;
matches[i][index].arr = malloc(sizeof(int)*len);
int wasted = fgetc(ptr); // always a '['
for(int j = 0; j <len; j++) {
int read = get_integer(ptr);
matches[i][index].arr[j] = read ;
};
terminate_line(ptr);
};
terminate_line(ptr);
terminate_line(ptr2);
}
void parse_configs(char* filename, int n_conf) {
// Initializing everything //
n_configs = n_conf ;
matches = malloc(sizeof(array*)*4);
// 4 cardinal directions
matches[0] = malloc(sizeof(array)*n_conf);
matches[1] = malloc(sizeof(array)*n_conf);
matches[2] = malloc(sizeof(array)*n_conf);
matches[3] = malloc(sizeof(array)*n_conf);
// compatibilities, 1 for each template
configs = malloc(sizeof(template)*n_conf);
// data for each template
for(int i = 0; i < n_conf; i++) {
configs[i].id = i;
configs[i].lines = malloc(sizeof(uint8_t)*8);
for(int j = 0; j < 8; j++) {
configs[i].lines[j] = 0;
}
}
// Now for the fun part //
FILE* ptr = fopen(filename, "r");
FILE* ptr2 = fopen(filename, "r");
for(int k = 0; k < n_conf; k++) {
parse_one(ptr, ptr2, k);
}
}

24
src/generation.h Normal file
View File

@ -0,0 +1,24 @@
#ifndef BACK_GEN_H
#define BACK_GEN_H
void one_debug();
bool unpack_coord(uint8_t* lines, int cx, int cy);
bool is_compatible(chunk ch, cardinal dir, int config_id);
void generate_chunk(int x, int y, int config_id);
void select_config(chunk ch, cardinal dir);
bool is_compat_one(int cx, int cy, cardinal dir, int idx);
void generate_chunk_all(int cx, int cy);
void initialize(SDL_Renderer* renderer);
void destroy();
void parse_configs(char* filename, int n_conf);
#endif

192
src/hash.c Normal file
View File

@ -0,0 +1,192 @@
#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 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->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->hash == hash) {
list->elt = elt;
} else {
List nex = malloc(sizeof(struct List));
nex->hash = hash;
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 = list->next;
}
}
};
int hash = generate_hash(x, z);
subGridInsert(grid, hash, elt);
}
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->hash != hash) {
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->hash != hash) {
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) * 8);
grid->size = 0;
grid->capacity = 8;
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);
}

60
src/hash.h Normal file
View File

@ -0,0 +1,60 @@
// credit to Benoit //
#ifndef GRID_H
#define GRID_H
#include <stdbool.h>
typedef struct template {
uint8_t id ;
uint8_t* lines ; // len = 8
} template ;
typedef struct chunk {
int16_t chx ;
int16_t chy ;
uint8_t draw_id ;
template chdata ;
} chunk ;
//
typedef struct List {
int hash;
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

61
src/main.c Normal file
View File

@ -0,0 +1,61 @@
#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"
#include "move.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,
1200, 800, 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);
}
/* -------------------------------------------------------- */
//one_debug();
parse_configs("templates.txt", 2);
initialize(rend);
moveFunctionMaster(rend);
destroy();
/* -------------------------------------------------------- */
SDL_DestroyRenderer(rend);
SDL_DestroyWindow(win);
SDL_Quit();
return 0;
}

138
src/move.c Normal file
View File

@ -0,0 +1,138 @@
#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"
#include "move.h"
double speed = 0.5 ;
int precision = 12 ;
double epsilon_x = 0.0;
double epsilon_y = 0.0;
bool checkCollision() {
return unpack_coord(gridGet(map, player_cx, player_cy)->chdata.lines, player_x, player_y);
}
void increment_x(double dx) {
epsilon_x = epsilon_x + dx ;
if(epsilon_x >= 1.0) {
epsilon_x -= 1.0 ;
player_x += 1 ;
if(player_x >= 8) {
player_x -= 8 ;
player_cx += 1;
}
} else if(epsilon_x < 0.0) {
epsilon_x += 1.0 ;
player_x -= 1 ;
if(player_x < 0) {
player_x += 8 ;
player_cx -= 1;
}
}
}
void increment_y(double dy) {
epsilon_y += dy ;
if(epsilon_y >= 1.0) {
epsilon_y -= 1.0 ;
player_y += 1 ;
if(player_y >= 8) {
player_y -= 8 ;
player_cy += 1;
}
} else if(epsilon_y < 0.0) {
epsilon_y += 1.0 ;
player_y -= 1 ;
if(player_y < 0) {
player_y += 8 ;
player_cy -= 1;
}
}
}
bool moveRequest(double dx, double dy) {
increment_x(dx);
increment_y(dy);
if(checkCollision()) {
increment_x(-dx);
increment_y(-dy);
return false;
};
return true;
}
void moveControl(bool* halt) {
SDL_Event event;
int i = 0;
while(SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_QUIT:
break;
case SDL_KEYDOWN:
switch (event.key.keysym.sym) {
case SDLK_z:
while(i < precision && moveRequest(0.0, -speed/precision)) {
i += 1;
};
break;
case SDLK_q:
while(i < precision && moveRequest(-speed/precision, 0.0)) {
i += 1;
};
break;
case SDLK_s:
while(i < precision && moveRequest(0.0, speed/precision)) {
i += 1;
};
break;
case SDLK_d:
while(i < precision && moveRequest(speed/precision, 0.0)) {
i += 1;
};
break;
case SDLK_TAB:
*halt = true;
break;
case SDLK_m:
zoom = max(100, zoom-25);
break;
case SDLK_p:
zoom = min(2250, zoom+25);
break;
}
}
}
}
void moveFunctionMaster(SDL_Renderer* renderer) {
bool halt = false ;
while(!halt) {
moveControl(&halt);
resetRenderer(renderer);
drawMapToRenderer(renderer, -300 * 250/zoom + to_int(50 * (8*player_cx + player_x + epsilon_x)), 300 * 250/zoom + to_int(50 * (8*player_cx + player_x + epsilon_x)), -300 * 250/zoom + to_int(50 * (8*player_cy + player_y + epsilon_y)), 300 * 250/zoom + to_int(50 * (8*player_cy + player_y + epsilon_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);
drawNumberToRenderer(renderer, digits, player_y, __width__ / 3, 80, 50, 70, 0);
drawNumberToRenderer(renderer, digits, to_int(epsilon_x*100), 2 * __width__ / 3, 10, 50, 70, 0);
drawNumberToRenderer(renderer, digits, to_int(epsilon_y*100), 2 * __width__ / 3, 80, 50, 70, 0);
updateRenderer(renderer);
usleep(20000);
}
}

16
src/move.h Normal file
View File

@ -0,0 +1,16 @@
#ifndef BACK_MOVE_H
#define BACK_MOVE_H
bool checkCollision();
void increment_x(double dx);
void increment_y(double dy);
bool moveRequest(double dx, double dy);
void moveControl(bool* halt);
void moveFunctionMaster(SDL_Renderer* renderer);
#endif

68
src/structure.h Normal file
View File

@ -0,0 +1,68 @@
#ifndef BACK_CONSTS_H
#define BACK_CONSTS_H
typedef struct imgs {
int len;
SDL_Texture** arr;
} imgs ;
typedef struct array {
int* arr ;
int len ;
} array ;
typedef enum cardinal {NORTH, SOUTH, EAST, WEST} cardinal ;
extern Grid map ;
extern int zoom ;
extern int render_distance ;
extern double speed ;
extern int precision ;
extern double epsilon_x ;
extern double epsilon_y ;
// are in [0.0, 1.0[
extern int player_x ;
extern int player_y ;
// are in [|0, 8[|
extern int player_cx ;
extern int player_cy ;
// any integer
extern int draw_par ;
extern int __width__ ;
extern int __height__ ;
extern template* configs ;
extern array** matches ; // first length is always 4 (NESW)
extern int n_configs ; // 2nd length
extern imgs digits ;
extern imgs letters ;
/* example
[
[
[1, 3], [0], [3], [2] <-- this 1 means "config 1 can be placed above a chunk with config 0"
],
[
[1], [0], [3], [0, 2] <-- this (1st) 0 means "config 0 can be placed below a chunk with config 1"
],
[
[1], [], [], []
],
[
[1], [0], [], []
]
]
*/
// chunk is 8x8 and (0, 0) is at top left
#endif

35
templates.txt Normal file
View File

@ -0,0 +1,35 @@
1 1 1 0 0 1 1 1
1 0 0 0 0 0 0 1
1 0 0 0 0 0 0 1
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 1
1 0 0 0 0 0 0 1
1 1 1 0 0 1 1 1
[0,1,]
[0,1,]
[0,1,]
[0,1,]
1 1 1 0 0 1 1 1
1 0 0 0 0 0 0 1
1 1 1 0 0 1 0 1
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
1 0 1 0 0 1 1 1
1 0 0 0 0 0 0 1
1 1 1 0 0 1 1 1
[0,1,]
[0,1,]
[1,0,]
[1,0,]
$
format is :
eight lines with eight 0s and 1s in each
four lists separated with a \n containing compatibilities with other configs
WARNING : do NOT forget the commas (1 for each)(otherwise seg fault will await...)
order is NORTH SOUTH EAST WEST
separate two templates with \n\n