Files
player/Project/Src/MyWin/MyWinCore/mywin_timer.c
2025-06-27 00:32:57 +08:00

101 lines
1.7 KiB
C
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "mywin_inc.h"
//清零定时器
int WIN_TimerReload (int timerId)
{
WIN_GetWinStruct()->softTimer[timerId-1].time_ms=0;
return 0;
}
//设置定时器周期
int WIN_SetTimerCycle (int timerId,int cycle)
{
WIN_GetWinStruct()->softTimer[timerId-1].cycle_ms=cycle;
return 0;
}
//添加定时器,成功返回id失败返回0
int WIN_CreatTimer (WIN_WindowStruct *win,u32 ms)
{
for (int i=0;i<WIN_SOFTTIMER_MAXNUM;i++)
{
if (WIN_GetWinStruct()->softTimer[i].win==0)
{
WIN_GetWinStruct()->softTimer[i].win=win;
WIN_GetWinStruct()->softTimer[i].cycle_ms=ms;
WIN_GetWinStruct()->softTimer[i].time_ms=0;
return i+1;
}
}
return 0;
}
//删除指定定时器失败返回-1成功返回0
int WIN_DeleteTimer (int id)
{
if (id>=WIN_SOFTTIMER_MAXNUM) return -1;
if (id<=0) return -1;
WIN_GetWinStruct()->softTimer[id-1].win=0;
return 0;
}
//删除窗口创建的所有定时器
int WIN_WinDeleteTimer (WIN_WindowStruct *win)
{
for (int i=0;i<WIN_SOFTTIMER_MAXNUM;i++)
{
if (WIN_GetWinStruct()->softTimer[i].win==win)
{
WIN_GetWinStruct()->softTimer[i].win=0;
}
}
return 0;
}
//定时器工作
void WIN_TimerWork (void)
{
WIN_MsgStruct msg={0};
u32 timePast=WIN_GetTimePast ();
for (int i=0;i<WIN_SOFTTIMER_MAXNUM;i++)
{
if (WIN_GetWinStruct()->softTimer[i].win)
{
WIN_GetWinStruct()->softTimer[i].time_ms+=timePast;
if (WIN_GetWinStruct()->softTimer[i].time_ms>=WIN_GetWinStruct()->softTimer[i].cycle_ms)
{
WIN_GetWinStruct()->softTimer[i].time_ms=0;
msg.msg=WIN_MSG_TIMER;
msg.data.v=i+1; //填充定时器id
WIN_SendMsg (0,WIN_GetWinStruct()->softTimer[i].win,&msg);
}
}
}
}