⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 scheduler.cpp

📁 VIGASOCO (VIdeo GAmes SOurce COde) Windows port (v0.01)
💻 CPP
字号:
// Scheduler.cpp
//
/////////////////////////////////////////////////////////////////////////////

#include "Scheduler.h"

/////////////////////////////////////////////////////////////////////////////
// initialization and cleanup
/////////////////////////////////////////////////////////////////////////////

Scheduler::Scheduler()
{
	// inits internal timers
	for (int i = 0; i < 4; i++){
        internalTimer[i] = 0;
	}
}

Scheduler::~Scheduler()
{
	TasksList::iterator i;

	// deletes pending tasks
	for (i = tasksList.begin(); i != tasksList.end(); i++){
		delete (*i);
	}
}

/////////////////////////////////////////////////////////////////////////////
// internal timers and scheduling
/////////////////////////////////////////////////////////////////////////////

// updates the timers
void Scheduler::updateInternalTimers()
{
	static int timerLimits[4][2] = {
		{ 0x06, 0xa0 },		// hundredths
		{ 0x0a, 0x60 },		// seconds
		{ 0x0a, 0x60 },		// minutes
		{ 0x0a, 0xa0 }		// hours
	};

	// initially only one timer change
	timerChangesLastFrame = 1;

	int i = 0;

	do {
		// increment counter
		internalTimer[i]++;

		// if the LSB of the counter has reached the limit
		if ((internalTimer[i] & 0x0f) == timerLimits[i][0]){
			// increment timer changes
			timerChangesLastFrame++;

			// update counter
			internalTimer[i] = (internalTimer[i] + 0x10) & 0xf0;

			// if the counter has reached the limit
			if (internalTimer[i] == timerLimits[i][1]){
				// increment timer changes
				timerChangesLastFrame++;

				// reset counter
				internalTimer[i] = 0x00;

				// goes to the next counter
				i++;
			} else break;
		} else break;
	} while (i < 4);
}

// executes scheduled tasks
void Scheduler::executeScheduledTasks()
{
	// for all scheduled tasks
	TasksList::iterator i = tasksList.begin();

	while (i != tasksList.end()){
		Task *task = (*i);

		// if a timeUnit has expired
		if (timerChangesLastFrame > task->timeUnits){
			// decrement task remaining units and execute if applicable
			task->remainingUnits--;

			if (task->remainingUnits == 0){
				task->execute();

				i = tasksList.erase(i);
				delete task;
			} else {
				i++;
			}
		} else {
			i++;
		}
	}
}

// adds a task to the task list
void Scheduler::addScheduledTask(Task *t)
{
	tasksList.push_back(t);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -