Skip to content
Snippets Groups Projects
Commit ac35ec78 authored by leo.muff's avatar leo.muff
Browse files

v1

parent 09da191d
No related branches found
No related tags found
No related merge requests found
......@@ -2,11 +2,12 @@
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <stdint.h>
#include "gfx/gfx.h"
#include "queue/queue_ptr_int.h"
#include "snake.h"
#define SCREEN_W 500
#define SCREEN_H 500
#define SCREEN_W 200
#define SCREEN_H 200
/* int main(){
tant que (jeu pas fini){
......@@ -40,9 +41,25 @@
}
}
int main(){
struct timespec start, finish;
int elapsed; // vars for time func
int get_input(int argc, char *argv[] , int *nb_food,int *time_food){
printf("%s, %s\n", argv[1], argv[2]);
if (argc != 3){
return 0;
}
else if ((atoi(argv[1]) <= 0) || (atoi(argv[2]) <= 0)){
return 0;
}
else {
*nb_food = strtol(argv[1], NULL, 10);
*time_food = strtol(argv[2], NULL, 10);
}
return 1;
}
int main(int argc, char *argv[]){
snake_t snake = create_snake(SCREEN_W, SCREEN_H);
board_t board = create_board(SCREEN_W, SCREEN_H);
......@@ -54,16 +71,22 @@
fprintf(stderr, "Graphics initialization failed!\n");
return EXIT_FAILURE;
}
SDL_SetWindowSize(ctxt->window, 1000 ,1000);
SDL_SetWindowPosition(ctxt->window, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED);
//SDL_RenderSetLogicalSize(ctxt->renderer,1000,1000);
game_status_t status;
int nb_food = 8;
int sec = 5;
double total_time;
uint32_t nb_food;
uint32_t sec;
get_input(argc, argv, &nb_food, &sec);
uint64_t total_time;
struct timespec start, finish;
uint32_t elapsed; // vars for time func
while(status != WON && status != LOST){
clock_gettime(CLOCK_MONOTONIC, &start);
gfx_present(ctxt);
......@@ -80,16 +103,21 @@
printf("%d\n", queue_count(snake.body));
printf("debut %d\n", snake.body.debut->coordinates.x); */
if (total_time >= sec){
if (total_time / 100000000 >= sec){
gen_food(nb_food, &board);
total_time = 0;
}
// Algorithme de mise à jour d'une frame
clock_gettime(CLOCK_MONOTONIC, &finish);
elapsed = (finish.tv_nsec - start.tv_nsec);
total_time += (finish.tv_sec - start.tv_sec);
usleep(elapsed / 100);
elapsed = (finish.tv_sec - start.tv_sec) + (finish.tv_nsec - start.tv_nsec);
if (elapsed > 3000000){
elapsed = 500000;
}
total_time += elapsed;
//printf("%d\n", elapsed);
usleep((elapsed / 1000) * 40);
}
gfx_destroy(ctxt);
queue_detruire(&snake.body);
......
......@@ -110,6 +110,21 @@ void gen_food(int max, board_t *board){
}
}
uint32_t* enlarge_pixels(board_t board, int factor) {
int old_size = board.size_x*board.size_y;
int new_size = old_size * factor;
uint32_t *new_pixels = (uint32_t *)malloc(new_size * sizeof(uint32_t));
for (int i = 0; i < old_size; i++) {
for (int j = 0; j < factor; j++) {
new_pixels[i * factor + j] = board.pixels[i];
}
}
// replace the original pixel array with the enlarged one
return new_pixels;
}
/*
avancer(){
// ajouter un elem avec coordonées + ptr sur celui d'avant , puis enlever le dernier elem, sauf si l'on a mangé
......
......@@ -42,5 +42,6 @@ game_status_t move(snake_t *snake, board_t *board);
void gen_food(int max, board_t *board);
uint32_t* enlarge_pixels(board_t board, int factor);
#endif
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment