Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
snake
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
leo.muff
snake
Commits
ac35ec78
Commit
ac35ec78
authored
2 years ago
by
leo.muff
Browse files
Options
Downloads
Patches
Plain Diff
v1
parent
09da191d
Branches
main
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
main.c
+41
-13
41 additions, 13 deletions
main.c
snake.c
+15
-0
15 additions, 0 deletions
snake.c
snake.h
+1
-0
1 addition, 0 deletions
snake.h
with
57 additions
and
13 deletions
main.c
+
41
−
13
View file @
ac35ec78
...
...
@@ -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
5
00
#define SCREEN_H
5
00
#define SCREEN_W
2
00
#define SCREEN_H
2
00
/* 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
);
...
...
This diff is collapsed.
Click to expand it.
snake.c
+
15
−
0
View file @
ac35ec78
...
...
@@ -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é
...
...
This diff is collapsed.
Click to expand it.
snake.h
+
1
−
0
View file @
ac35ec78
...
...
@@ -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
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment