commit ebe94f56119f7fcb2c7cbc76a113f7dd04a5fcd2 Author: attilavs2 Date: Sat Aug 31 23:10:08 2024 +0200 feat : initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3eee43e --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +build/ +raylib/ +*.amd64 +*.exe diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..8f053e0 --- /dev/null +++ b/Makefile @@ -0,0 +1,34 @@ +OUTNAME = ttower + +CFLAGS = -O0 -g -Wall -Wextra -I.raylib/include +LDFLAGS = -L./raylib/lib -lraylib -lm + +CC = gcc +OUTPUT = "${OUTNAME}.amd64" + +ifeq "${TARGET_IS_WIN}" "true" + +CC = x86_64-w64-mingw32-gcc +OUTPUT = "${OUTNAME}.exe" + +endif + +BUILD_DIR = build +SRC_DIR = src + +$(BUILD_DIR)/%.o : $(SRC_DIR)/%.c + ${CC} -c ${CFLAGS} -o $@ $< ${LDFLAGS} + + +OBJS = $(patsubst $(SRC_DIR)/%.c,$(BUILD_DIR)/%.o,$(wildcard $(SRC_DIR)/*.c)) + +all: $(OBJS) + ${CC} ${CFLAGS} -o ${OUTPUT} ${OBJS} ${LDFLAGS} + +test: all + ./${OUTPUT} + +clean: + rm -rf ${BUILD_DIR}/* + +.PHONY: all builddir test clean diff --git a/plane.png b/plane.png new file mode 100644 index 0000000..5784458 Binary files /dev/null and b/plane.png differ diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..666516b --- /dev/null +++ b/src/main.c @@ -0,0 +1,195 @@ +#include +#include +#include +#include +#include + +#include "raylib.h" + +#define min(x, minn) ((x) < (minn) ? (minn):(x)) + +#define max(x, maxn) ((x) > (maxn) ? (maxn):(x)) + +#define clamp(x, minn, maxn) (min(max((x),(maxn)),(minn))) + +#define PQUEUE_SIZE 256 + +typedef struct{ + + Vector2 pos; + float dist; + +} Plane; + +int lost = 0; + +int planen; + +Plane plane_queue[PQUEUE_SIZE]; + +void draw_planes(Texture2D *plane){ + int i; + if(!planen) + return; + for(i = 0; i < planen; i++){ + Plane *pln = &plane_queue[i]; + DrawTextureEx(*plane,pln->pos,0,pln->dist,WHITE); + } +} + +void spawn_plane(Vector2 pos){ + plane_queue[planen].pos = pos; + plane_queue[planen].dist = 0.1; + planen++; +} + +void pop_plane(){ + planen--; + memmove(plane_queue, &plane_queue[1], sizeof(Plane)*planen); +} + +void update_planes(int diff, int xpos){ + int i, j; + + //TODO : Redo + if(planen < 2){ + int nx = rand() % 600; + int ny = rand() % 400; + spawn_plane((Vector2){nx,ny}); + } + + double exp = 2-(0.0016666666*147); + double sign = (double)(abs(xpos)/(double)xpos); + + //TODO : Redo + for(i = 0; i < planen; i++){ + Plane *pln = &plane_queue[i]; + pln->dist += 0.003; + if(pln->dist >= 1){ + for(j = 0; j < 8; j++){ + double x = pow(sqrt(abs(xpos)),exp) * sign + 153; + exp -= 0.0016666666*60; + //DrawRectangle(x,147+j*60,50,60,BLACK); + //DrawRectangle(x+260,147+j*60,50,60,BLACK); + int coll = 0; + coll = pln->pos.x < x+104 && pln->pos.x+60 > x && + pln->pos.y > j*60 && pln->pos.y < j*60+60; + + coll = coll | (pln->pos.x < x+104+163 && pln->pos.x+60 > x+163 && + pln->pos.y > j*60 && pln->pos.y < j*60+60); + + if(coll){ + lost = 1; + } + } + pop_plane(); + } + else { + for(j = 0; j < 8; j++){ + double x = pow(sqrt(abs(xpos)),exp) * sign + 153; + exp -= 0.0016666666*60; + DrawRectangle(x,147+j*60,50,60,WHITE); + DrawRectangle(x+260,147+j*60,50,60,WHITE); + } + } + } +} + +void draw_towers(Texture2D *towers, int xpos){ + double x; + int y; + xpos = xpos == 0 ? 1:xpos; + int sign = (double)(abs(xpos)/(double)xpos); + xpos = sqrt(abs(xpos)); + double exp = 2; + + for(y = 0; y < 600; y++){ + x = pow(xpos,exp) * sign; + exp -= 0.0016666666; + DrawTextureRec(*towers,(Rectangle){.height=1,.width=800,.x=0,.y=y}, + (Vector2){round(x),y},WHITE); + } +} + +char scorebuf[100]; + +void lost_men(int *score){ + + lost = 0; + + while(!IsKeyDown(KEY_ENTER) && !WindowShouldClose()){ + BeginDrawing(); + DrawRectangle(200,150,400,300,WHITE); + + snprintf(scorebuf,100,"Votre score : %d", *score); + + DrawText(scorebuf, 250, 275, 30, BLACK); + + DrawText("Appuyez sur entrée", 250, 350, 30, BLACK); + + EndDrawing(); + } + + while(planen){ + pop_plane(); + } + + *score = 0; +} + +int main(){ + + int rand_seed = GetTime(); + rand_seed &= 0xFFFFFFFF; + + srand(rand_seed); + + InitWindow(800, 600, "ttower"); + + Texture2D towers = LoadTexture("towers.png"); + + Texture2D plane = LoadTexture("plane.png"); + + int xpos = 0; + + int score = 0; + + SetTargetFPS(60); + + planen = 0; + memset(plane_queue, 0, sizeof(Plane)*PQUEUE_SIZE); + + while(!WindowShouldClose()){ + BeginDrawing(); + + ClearBackground(BLUE); + + if(IsKeyDown(KEY_LEFT)) + xpos-=3; + if(IsKeyDown(KEY_RIGHT)) + xpos+=3; + + xpos = clamp(xpos, -200, 200); + + update_planes(0, xpos); + + draw_planes(&plane); + + draw_towers(&towers, xpos); + + snprintf(scorebuf, 100, "Score : %d", score); + + DrawText(scorebuf, 0,0,30,BLACK); + + if(lost) + lost_men(&score); + + score++; + + EndDrawing(); + } + + CloseWindow(); + + return 0; +} diff --git a/towers.png b/towers.png new file mode 100644 index 0000000..625365d Binary files /dev/null and b/towers.png differ