delay add for update and time add in game
This commit is contained in:
66
display.c
66
display.c
@@ -26,9 +26,10 @@ void screenDisplay (unsigned short int **tab, int size)
|
|||||||
|
|
||||||
void screenDisplayGameSDL (unsigned short int **tab, dis *display_user)
|
void screenDisplayGameSDL (unsigned short int **tab, dis *display_user)
|
||||||
{
|
{
|
||||||
unsigned int display_game = display_user->size_window - display_user->size_menu;
|
unsigned int display_game
|
||||||
|
= display_user->size_window - display_user->size_menu;
|
||||||
int size = display_game / display_user->size_box;
|
int size = display_game / display_user->size_box;
|
||||||
unsigned int marge = display_user->size_menu /2;
|
unsigned int marge = display_user->size_menu / 2;
|
||||||
unsigned int i, j;
|
unsigned int i, j;
|
||||||
|
|
||||||
for (i = 0; i < display_user->size_box; ++i)
|
for (i = 0; i < display_user->size_box; ++i)
|
||||||
@@ -37,7 +38,7 @@ void screenDisplayGameSDL (unsigned short int **tab, dis *display_user)
|
|||||||
{
|
{
|
||||||
SDL_Surface *img;
|
SDL_Surface *img;
|
||||||
SDL_Texture *texture;
|
SDL_Texture *texture;
|
||||||
vect pos = { i * size + marge, j * size};
|
vect pos = { i * size + marge, j * size };
|
||||||
switch (tab[j][i])
|
switch (tab[j][i])
|
||||||
{
|
{
|
||||||
case EMPTY:
|
case EMPTY:
|
||||||
@@ -88,15 +89,15 @@ int getMaxSize (dis display_user)
|
|||||||
result = display.h;
|
result = display.h;
|
||||||
}
|
}
|
||||||
SDL_Quit ();
|
SDL_Quit ();
|
||||||
int minus_mod = (result-50) % display_user.size_box;
|
int minus_mod = (result - 50) % display_user.size_box;
|
||||||
//printf("DIS :%d, size : %d ,mod : %d\n",result, size_box,minus_mod);
|
// printf("DIS :%d, size : %d ,mod : %d\n",result, size_box,minus_mod);
|
||||||
return (result - 50 - minus_mod); // margin
|
return (result - 50 - minus_mod); // margin
|
||||||
}
|
}
|
||||||
|
|
||||||
void initSDL (dis *display_user)
|
void initSDL (dis *display_user)
|
||||||
{
|
{
|
||||||
SDL_Init (SDL_INIT_VIDEO);
|
SDL_Init (SDL_INIT_VIDEO);
|
||||||
TTF_Init();
|
TTF_Init ();
|
||||||
display_user->window
|
display_user->window
|
||||||
= SDL_CreateWindow ("Sokoman", SDL_WINDOWPOS_CENTERED,
|
= SDL_CreateWindow ("Sokoman", SDL_WINDOWPOS_CENTERED,
|
||||||
SDL_WINDOWPOS_CENTERED, display_user->size_window,
|
SDL_WINDOWPOS_CENTERED, display_user->size_window,
|
||||||
@@ -125,37 +126,46 @@ void displayImage (SDL_Renderer *renderer, SDL_Texture *texture, vect pos,
|
|||||||
SDL_RenderCopy (renderer, texture, NULL, &rect);
|
SDL_RenderCopy (renderer, texture, NULL, &rect);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void displayTextSDL (dis *display_user, char *text, vect coor, vect size,
|
||||||
void displayTextSDL(dis *display_user,char *text, vect coor, vect size, int font_size)
|
int font_size)
|
||||||
{
|
{
|
||||||
TTF_Font* Sans = TTF_OpenFont("Roboto-Regular.ttf", font_size);
|
TTF_Font *Sans = TTF_OpenFont ("Roboto-Regular.ttf", font_size);
|
||||||
system("pwd");
|
SDL_Color white = { 255, 255, 255, 255 };
|
||||||
printf("%p\n", Sans);
|
|
||||||
SDL_Color white = {255,0,0,255};
|
|
||||||
int text_width, text_height;
|
int text_width, text_height;
|
||||||
|
|
||||||
|
SDL_Rect background;
|
||||||
|
SDL_Surface *surface_text = TTF_RenderText_Solid (Sans, text, white);
|
||||||
|
|
||||||
SDL_Surface* surface_text = TTF_RenderText_Solid(Sans, text, white);
|
TTF_SizeText (Sans, text, &text_height, &text_width);
|
||||||
|
SDL_Rect message_rect;
|
||||||
SDL_Texture* message = SDL_CreateTextureFromSurface(display_user->renderer, surface_text);
|
|
||||||
|
|
||||||
TTF_SizeText(Sans, text, &text_height, &text_width);
|
|
||||||
SDL_Rect message_rect ;
|
|
||||||
message_rect.x = coor.x;
|
message_rect.x = coor.x;
|
||||||
message_rect.y = coor.y;
|
message_rect.y = coor.y;
|
||||||
message_rect.w = text_height;
|
message_rect.w = text_height;
|
||||||
message_rect.h = text_width;
|
message_rect.h = text_width;
|
||||||
|
background.x = coor.x;
|
||||||
|
background.y = coor.y;
|
||||||
|
background.w = text_height;
|
||||||
|
background.h = text_width;
|
||||||
|
|
||||||
if (message_rect.w > size.y) message_rect.w = size.y;
|
if (message_rect.w > size.y)
|
||||||
if (message_rect.h > size.x) message_rect.h = size.x;
|
{
|
||||||
|
message_rect.w = size.y;
|
||||||
|
background.w = size.y;
|
||||||
|
}
|
||||||
|
if (message_rect.h > size.x)
|
||||||
|
{
|
||||||
|
message_rect.h = size.x;
|
||||||
|
background.h = size.x;
|
||||||
|
}
|
||||||
|
|
||||||
printf("%d, %d", message_rect.w, message_rect.h);
|
SDL_Texture *message
|
||||||
SDL_RenderCopy(display_user-> renderer, message, NULL, &message_rect);
|
= SDL_CreateTextureFromSurface (display_user->renderer, surface_text);
|
||||||
SDL_RenderPresent(display_user->renderer);
|
SDL_SetRenderDrawColor(display_user->renderer, 0, 0, 0, 255);
|
||||||
puts("Fa it");
|
SDL_RenderFillRect(display_user->renderer, &background);
|
||||||
|
|
||||||
SDL_FreeSurface(surface_text);
|
SDL_RenderCopy (display_user->renderer, message, NULL, &message_rect);
|
||||||
SDL_DestroyTexture(message);
|
SDL_RenderPresent (display_user->renderer);
|
||||||
|
|
||||||
|
SDL_FreeSurface (surface_text);
|
||||||
|
SDL_DestroyTexture (message);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
25
function.c
25
function.c
@@ -5,9 +5,11 @@
|
|||||||
#include <SDL2/SDL_keycode.h>
|
#include <SDL2/SDL_keycode.h>
|
||||||
#include <SDL2/SDL_render.h>
|
#include <SDL2/SDL_render.h>
|
||||||
#include <SDL2/SDL_scancode.h>
|
#include <SDL2/SDL_scancode.h>
|
||||||
|
#include <SDL2/SDL_timer.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <time.h>
|
||||||
|
|
||||||
unsigned short int **creatArea2D (const unsigned int N)
|
unsigned short int **creatArea2D (const unsigned int N)
|
||||||
{
|
{
|
||||||
@@ -186,6 +188,11 @@ void move (unsigned short int **tab, vect *playerPos, vect direction)
|
|||||||
void inGameLoop (unsigned short int **tab2d, int N, vect *playerPos,
|
void inGameLoop (unsigned short int **tab2d, int N, vect *playerPos,
|
||||||
vect *targets, int nbr_targets, dis *display_user)
|
vect *targets, int nbr_targets, dis *display_user)
|
||||||
{
|
{
|
||||||
|
vect size_time = {display_user->size_menu , display_user->size_window / 3};
|
||||||
|
vect coor_time = {0, display_user->size_window - display_user->size_menu};
|
||||||
|
time_t time_start = time(NULL) ;
|
||||||
|
time_t current_time = time(NULL);
|
||||||
|
time_t delay = 0;
|
||||||
char last = SDL_SCANCODE_1;
|
char last = SDL_SCANCODE_1;
|
||||||
vect direction = { 0, 0 };
|
vect direction = { 0, 0 };
|
||||||
char input;
|
char input;
|
||||||
@@ -241,6 +248,14 @@ void inGameLoop (unsigned short int **tab2d, int N, vect *playerPos,
|
|||||||
screenDisplayGameSDL (tab2d, display_user);
|
screenDisplayGameSDL (tab2d, display_user);
|
||||||
}
|
}
|
||||||
last = SDL_SCANCODE_F21;
|
last = SDL_SCANCODE_F21;
|
||||||
|
current_time = time(NULL);
|
||||||
|
if (current_time > delay)
|
||||||
|
{
|
||||||
|
delay = current_time;
|
||||||
|
displayTextSDL(display_user, timeToText(time(NULL) - time_start), coor_time, size_time, 50);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -393,3 +408,13 @@ bool blockBox (unsigned short int **tab2d, vect box_coor)
|
|||||||
perror ("error in blockBox");
|
perror ("error in blockBox");
|
||||||
exit (0);
|
exit (0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
char *timeToText(time_t time)
|
||||||
|
{
|
||||||
|
char* result = calloc(20, sizeof(char));
|
||||||
|
result[0] = '\0';
|
||||||
|
unsigned int min = time / 60;
|
||||||
|
unsigned int sec = time % 60;
|
||||||
|
snprintf(result, 20,"Time : %02d:%02d",min, sec);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|||||||
@@ -48,5 +48,6 @@ bool blockBox (unsigned short int **tab2d, vect box_coor);
|
|||||||
vect plusVect (vect one, vect two);
|
vect plusVect (vect one, vect two);
|
||||||
void inGameLoop (unsigned short int **tab2d, int N, vect *playerPos,
|
void inGameLoop (unsigned short int **tab2d, int N, vect *playerPos,
|
||||||
vect *targets, int nbr_targets, dis *display_user);
|
vect *targets, int nbr_targets, dis *display_user);
|
||||||
|
char *timeToText(time_t time);
|
||||||
|
|
||||||
#endif // FONCTION_H
|
#endif // FONCTION_H
|
||||||
|
|||||||
8
main.c
8
main.c
@@ -5,7 +5,6 @@
|
|||||||
#include <SDL2/SDL_image.h>
|
#include <SDL2/SDL_image.h>
|
||||||
#include <SDL2/SDL_render.h>
|
#include <SDL2/SDL_render.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <time.h>
|
|
||||||
|
|
||||||
#define SIZE_PLAY 19
|
#define SIZE_PLAY 19
|
||||||
#define SIZE_MENU 200;
|
#define SIZE_MENU 200;
|
||||||
@@ -24,21 +23,14 @@ int main ()
|
|||||||
score playerScore;
|
score playerScore;
|
||||||
unsigned short int **tab2d = creatArea2D (SIZE_PLAY);
|
unsigned short int **tab2d = creatArea2D (SIZE_PLAY);
|
||||||
|
|
||||||
playerScore.before = time (NULL);
|
|
||||||
|
|
||||||
targets
|
targets
|
||||||
= fileToTab2D ("test.txt", tab2d, SIZE_PLAY, playerPos, &nbr_targets);
|
= fileToTab2D ("test.txt", tab2d, SIZE_PLAY, playerPos, &nbr_targets);
|
||||||
screenDisplayGameSDL (tab2d, &display_user);
|
screenDisplayGameSDL (tab2d, &display_user);
|
||||||
vect size = {display_user.size_menu , display_user.size_window / 3};
|
|
||||||
vect pos = {0, display_user.size_window - display_user.size_menu};
|
|
||||||
displayTextSDL(&display_user, "Time 20:4345999", pos, size, 69);
|
|
||||||
screenDisplay (tab2d, SIZE_PLAY);
|
screenDisplay (tab2d, SIZE_PLAY);
|
||||||
inGameLoop (tab2d, SIZE_PLAY, playerPos, targets, nbr_targets,
|
inGameLoop (tab2d, SIZE_PLAY, playerPos, targets, nbr_targets,
|
||||||
&display_user);
|
&display_user);
|
||||||
|
|
||||||
playerScore.after = time (NULL);
|
|
||||||
printf ("%ld\n", playerScore.after - playerScore.before);
|
|
||||||
|
|
||||||
SDL_DestroyWindow (display_user.window);
|
SDL_DestroyWindow (display_user.window);
|
||||||
SDL_DestroyRenderer (display_user.renderer);
|
SDL_DestroyRenderer (display_user.renderer);
|
||||||
free2D (tab2d, SIZE_PLAY);
|
free2D (tab2d, SIZE_PLAY);
|
||||||
|
|||||||
Reference in New Issue
Block a user