Skip to content
Snippets Groups Projects
timer.c 1.48 KiB
Newer Older
Kaan.Korucan's avatar
Kaan.Korucan committed
/*
// 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;
}