Jump to content
  • Advertisement
Sign in to follow this  
toro

How to call game parts?

This topic is 3063 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, I'm in the prototyping phase for a game engine and I have the following situation: The game engine will be divided in several components/tasks that will be executed in a sequential order: Input -> Physics -> Render -> Audio -> Resources -> Scripts My approach to this work-flow was to design a kernel class that will call all the tasks found in an array based on the following conditions: - the task must be executed if is marked as mandatory, - a non-mandatory task can be skipped if there is not enough time in the current kernel cycle.
    ui8   indx;
    ui32  ret = NO_ERROR;

    /* save start reference period */
    pf.pf_ref_start();
    
    /* execute all tasks from the list */
	KernelCycleState kernel_cycle_state = GREEN_FLAG;
	ui32 time_consumed = 0;
	for (indx = 0; indx < task_count; indx++)
    {
		// increment task counter
		task[indx]->task_counter++;

		// init execute_flag 
		bool execute_flag = false;

		// check if the task should be executed in this cycle
		if (task[indx]->task_counter == task[indx]->task_period)
		{
			// reset task counter
			task[indx]->task_counter = 0;

			// check if the task is mandatory
			if (task[indx]->task_is_mandatory)
			{
				execute_flag = true;
			}
			else
			{
				// if the task is not mandatory, check the kernel cycle state
				if (kernel_cycle_state == GREEN_FLAG)
				{
					execute_flag = true;
				}
			}
		}

		// the task must be executed
		if (execute_flag)
		{
			ui32 task_start = Timer::getTime();
			task[indx]->time_allocated = time_cycle - time_consumed;	// set allocated time for task
			ret = task[indx]->execute();								// execute task
			ui32 task_end = Timer::getTime();

			task[indx]->time_consumed = task_end - task_start;			// set consumed time for task
			time_consumed += task[indx]->time_consumed;
			
			// compute the kernel cycle flag (Red or Green)
			if (time_consumed > time_cycle)								
			{
				kernel_cycle_state = RED_FLAG;
			}
			else
			{
				kernel_cycle_state = GREEN_FLAG;
			}

			// in case of error
			if (ret != TASK_SUCCESS) break;
		}
    }

    /* save end reference period */
    pf.pf_ref_end();

My questions are: 1) Is this a sound approach? 2) Could someone describe a real world example for this mechanism? Thank you,

Share this post


Link to post
Share on other sites
Advertisement
Yes, that will work, but I imagine you'll want a more complex implementation than that. What you really want is a priority queue weighted by how much CPU time the task has gotten recently times whatever fixed priority you've given your processes. If you don't do that, when some of your mandatory processes get busy, the non-mandatory processes will *never* get any CPU (which I doubt you'll want). A weighted priority queue ensures that they'll get at least *some* CPU.

A good real-world example is kernel threading.

What sort of platform or API are you developing for? For example, with pthreads, you can just let the kernel handle prioritization and process switching for you. Which is probably the best approach.

Share this post


Link to post
Share on other sites
Thank you for the quick answer. I will have to look in both of your recommendations: adding priorities and pthreads.
The prototype is not made for a specific API. The integer types were just typedef to other names and the intended platform is Win32.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!