diff --git a/GAME/include/timer.h b/GAME/include/timer.h new file mode 100644 index 0000000000000000000000000000000000000000..4fe1835d0f55afc4533a7224639fdfc899acee49 --- /dev/null +++ b/GAME/include/timer.h @@ -0,0 +1,17 @@ +/* +// Created by Kaan on 9.04.2021. +*/ + +#ifndef INSAGAME_TIMER_H +#define INSAGAME_TIMER_H +#include <stdio.h> +#include <stdlib.h> +#include <SDL2/SDL.h> +#include <SDL2/SDL_image.h> +#include <time.h> +#include <SDL2/SDL_ttf.h> + +void init_timer(); +SDL_Texture * query_timer(SDL_Renderer *renderer, TTF_Font* Sans, SDL_Rect* timer_rect, int* close_request, SDL_Window * window); + +#endif /*INSAGAME_TIMER_H*/ diff --git a/GAME/ressource/OpenSans-Semibold.ttf b/GAME/ressource/OpenSans-Semibold.ttf new file mode 100644 index 0000000000000000000000000000000000000000..1a7679e3949fb045f152f456bc4adad31e8b9f55 Binary files /dev/null and b/GAME/ressource/OpenSans-Semibold.ttf differ diff --git a/GAME/src/timer.c b/GAME/src/timer.c new file mode 100644 index 0000000000000000000000000000000000000000..aa5f55f25fc3a0c4aca5de43fb7f4ef0cc0042b0 --- /dev/null +++ b/GAME/src/timer.c @@ -0,0 +1,56 @@ +/* +// Created by Kaan on 9.04.2021. +*/ + +#include <stdio.h> +#include <stdlib.h> +#include <SDL2/SDL.h> +#include <time.h> +#include <SDL2/SDL_ttf.h> +#include "timer.h" + +clock_t begin; +int time_spent; +int seconde=30; +int time_remaining; +char timerString[10]; + +void init_timer(){ + TTF_Init(); + begin=clock(); +} + + +SDL_Texture * query_timer(SDL_Renderer *renderer, TTF_Font* Sans, SDL_Rect* timer_rect, int* close_request, SDL_Window * window){ + SDL_Color white = {255, 255, 255}; // text's color + time_spent = (clock() - begin) / CLOCKS_PER_SEC; + time_remaining=seconde-time_spent; + itoa(time_remaining, timerString, 10); + Sans = TTF_OpenFont("../ressource/OpenSans-Semibold.ttf", 8); //font style and sets a size + TTF_SetFontStyle(Sans, 2); + SDL_Surface* tim = TTF_RenderText_Solid(Sans, timerString , white); + SDL_Texture* texture_timer = SDL_CreateTextureFromSurface(renderer, tim); + SDL_FreeSurface(tim); + + // draw the image to the window + if (!texture_timer) + { + printf("error creating texture: %s\n", SDL_GetError()); + SDL_DestroyRenderer(renderer); + SDL_DestroyWindow(window); + SDL_Quit(); + return NULL; + } + + timer_rect->x = 20; //controls the rect's x coordinate + timer_rect->y = 20; // controls the rect's y coordinte + timer_rect->w = 50; // controls the width of the rect + timer_rect->h = 50; // controls the height of the rect + + if(time_remaining==0){ + *close_request=1; + } + + return texture_timer; +} +