//
// Created by sasha on 01.12.2025.
//

#include "timer.h"

Timer::Timer(IWidget *parent) : IWidget(parent,0,0,0,0) {
    timer_run = false;
}

void Timer::draw() {

}

void Timer::iterate() {
    if (!timer_run) return;
    Uint64 currentTime = SDL_GetTicks();
    basket_time+=(currentTime-lastTicks);
    lastTicks = currentTime;

    if (basket_time>interval){
        basket_time = basket_time-interval*(basket_time/interval);
        onTime();
    }
}

void Timer::start(Uint64 interval_msec, std::function<void()> func) {
    basket_time=0;
    interval = interval_msec;
    onTime = func;
    lastTicks = SDL_GetTicks();
    timer_run = true;
}

void Timer::stop() {
    timer_run = false;
}

void Timer::start() {
    basket_time=0;
    lastTicks = SDL_GetTicks();
    timer_run = true;
}
