• Advertisement
  • Popular Tags

  • Popular Now

  • Advertisement
  • Similar Content

    • By Ty Typhoon
      I like to build my A - Team now.
       
      I need loyal people who can trust and believe in a dream.
      If you got time and no need for direct pay please contact me now.
       
      We cant pay now, you will recieve a lifetime percentage if the released game will give earnings. 
      If I get the best people together for a team, everything should be possible.
       
       
      What i need:
      - Programmer c++
      - Unity / Unreal - we must check whats possible, please share your experience with me.
      - Sculpter, 3D Artist
      - Animator
      - Marketing / Promotion 
       
       
      What i do:
      - Studio Owner
      - Director
      - Recruit exactly you
      - Sounddesign
      - Main theme composing
      - Vocals
      - Game design
      - Gun, swords, shields and weapon design
      - Character, plants and animal design
       
       
      Please dont ask about the Name of the Game, about Designs or Screenshots.
      The game will be defintitly affected about our and your skills if you join the team.
       
       
      Planned for the big Game:
      - 1st person shooter
      - online multiplayer
      - character manipulation
      - complete big open world with like lifetime actions and reactions
      - gunstore with many items to buy
      - many upgrades for players
      - specials like mini games
       
      So if you are interested in joining a team with a nearly complete game idea, contact me now and tell me what you can do.
       
      discord:
      joerg federmann composing#2898
       
       
    • By codelyoko373
      I wasn't sure if this would be the right place for a topic like this so sorry if it isn't.
      I'm currently working on a project for Uni using FreeGLUT to make a simple solar system simulation. I've got to the point where I've implemented all the planets and have used a Scene Graph to link them all together. The issue I'm having with now though is basically the planets and moons orbit correctly at their own orbit speeds.
      I'm not really experienced with using matrices for stuff like this so It's likely why I can't figure out how exactly to get it working. This is where I'm applying the transformation matrices, as well as pushing and popping them. This is within the Render function that every planet including the sun and moons will have and run.
      if (tag != "Sun") { glRotatef(orbitAngle, orbitRotation.X, orbitRotation.Y, orbitRotation.Z); } glPushMatrix(); glTranslatef(position.X, position.Y, position.Z); glRotatef(rotationAngle, rotation.X, rotation.Y, rotation.Z); glScalef(scale.X, scale.Y, scale.Z); glDrawElements(GL_TRIANGLES, mesh->indiceCount, GL_UNSIGNED_SHORT, mesh->indices); if (tag != "Sun") { glPopMatrix(); } The "If(tag != "Sun")" parts are my attempts are getting the planets to orbit correctly though it likely isn't the way I'm meant to be doing it. So I was wondering if someone would be able to help me? As I really don't have an idea on what I would do to get it working. Using the if statement is truthfully the closest I've got to it working but there are still weird effects like the planets orbiting faster then they should depending on the number of planets actually be updated/rendered.
    • By BenjaminBouchet
      Learning game development in Unreal Engine could be a daunting task for someone who don’t know where to start, and a cumbersome process if you don’t organize your progression correctly. One thing commonly known by experienced developers and by people unfamiliar with coding: mastering a development language is a long and difficult task.
      From blueprints to C++ in Unreal Engine
      If you want to learn fast, you need a good learning strategy. Unreal Engine contains a very powerful tool which you can use to learn C++ faster: its blueprint system. Blueprints are extremely easy to learn (and you may already have a good knowledge of them). Thus you can conveniently use them as a guide for writing code in C++. This is the reason why I am writing a tutorial series on how to make the transition from Unreal Engine blueprints to C++.
      Learn and practice C++
      Following this tutorial, you’ll acquire new concepts of C++ programming in every chapter. Then following chapters will give you reasons to reuse and practice those same concepts. There’s no better way to wire you brain.
      Link to the tutorial: [Tutorial] Learn C++ in Unreal Engine 4 by making a powerful camera
      Please do send me as much feedback as you want. I’ll be considering every constructive remarks and taking them into consideration. Your feedback will help me to improve and update the existing chapters and to make the next one better.

      View full story
    • By BenjaminBouchet
      Learning game development in Unreal Engine could be a daunting task for someone who don’t know where to start, and a cumbersome process if you don’t organize your progression correctly. One thing commonly known by experienced developers and by people unfamiliar with coding: mastering a development language is a long and difficult task.
      From blueprints to C++ in Unreal Engine
      If you want to learn fast, you need a good learning strategy. Unreal Engine contains a very powerful tool which you can use to learn C++ faster: its blueprint system. Blueprints are extremely easy to learn (and you may already have a good knowledge of them). Thus you can conveniently use them as a guide for writing code in C++. This is the reason why I am writing a tutorial series on how to make the transition from Unreal Engine blueprints to C++.
      Learn and practice C++
      Following this tutorial, you’ll acquire new concepts of C++ programming in every chapter. Then following chapters will give you reasons to reuse and practice those same concepts. There’s no better way to wire you brain.
      Link to the tutorial: [Tutorial] Learn C++ in Unreal Engine 4 by making a powerful camera
      Please do send me as much feedback as you want. I’ll be considering every constructive remarks and taking them into consideration. Your feedback will help me to improve and update the existing chapters and to make the next one better.
    • By mrDIMAS
      Hello everyone! I need to fill lua table with functions from script file like this:
      function init() end function update() end I need to create table on stack and fill it with this functions from specified file. How can I do this?
  • Advertisement
  • Advertisement

60 FPS game loop using std::chrono

Recommended Posts

Hi everyone, out of curiosity yesterday I was trying out chrono header with the goal of creating a 60fps loop, the code below is what I have so far and I wanted your opinion about it, if it is the proper way to do it, how it is usually done or how I could improve on it .

Thanks :)

#include <iostream>
#include <chrono>
#include <cstdint>
using namespace std;
using namespace chrono;

using frame = duration<int32_t, ratio<1, 60>>;
using ms = duration<float, milli>;

int main()
{
	time_point<steady_clock> fpsTimer(steady_clock::now());
	frame FPS{};

	while (true)
	{
		FPS = duration_cast<frame>(steady_clock::now() - fpsTimer);
		if (FPS.count() >= 1)
		{
			fpsTimer = steady_clock::now();
			cout << "LastFrame: " << duration_cast<ms>(FPS).count() << "ms  |  FPS: " << FPS.count() * 60 << endl;
		}
	}
	return 0;
}

output:

LastFrame: 16.6667ms  |  FPS: 60
LastFrame: 16.6667ms  |  FPS: 60
LastFrame: 16.6667ms  |  FPS: 60
LastFrame: 16.6667ms  |  FPS: 60
LastFrame: 16.6667ms  |  FPS: 60
LastFrame: 16.6667ms  |  FPS: 60
LastFrame: 16.6667ms  |  FPS: 60
LastFrame: 16.6667ms  |  FPS: 60
LastFrame: 16.6667ms  |  FPS: 60
LastFrame: 16.6667ms  |  FPS: 60
LastFrame: 16.6667ms  |  FPS: 60
LastFrame: 16.6667ms  |  FPS: 60

Edited by MarcusAseth

Share this post


Link to post
Share on other sites
Advertisement

The 60 frames per second really only applies to game consoles where the TV monitor is running at 60 frames per second because that's what the old television standards used.

If you're on a monitor running at 75 frames per second, 60 is probably not your ideal target.

There are many good articles about the typical game loop, here are two often recommended articles:

Clicky - DeWiTTERS

Clicky - Gaffer

Share this post


Link to post
Share on other sites
On 7/16/2017 at 10:52 PM, MarcusAseth said:

I'll go check them out, thanks frob :)

I would strongly recommend you do not lock render frames in your time step, but you render your input and logic at a certain amount of ticks per second, and render graphics as fast as possible with the remaining time until the next frame is due. You will have to use interpolation to smooth out movement however. This will make sure you're getting smooth graphics at any FPS.

I'm using a similar loop like DeWitters with interpolation. Works like a charm. Also, stay away from variable rates for logic and input, it can do some wonky things with collision among other things. I always prefer Fixed Time Steps with Variable Render Rates using Interpolation.

Share this post


Link to post
Share on other sites

derived from, fix your timestep, I am using this:

mPlaying = true;

int lag = 0;
std::chrono::time_point<std::chrono::steady_clock> endTime = std::chrono::steady_clock::now();

long longestTime = 0;
long frameCount = 0;
long tickCount = 0;
long fpsElapsedTime = 0;

while (mPlaying) {

	std::chrono::time_point<std::chrono::steady_clock> startTime = std::chrono::steady_clock::now();

	std::chrono::milliseconds elapsedTime(std::chrono::duration_cast<std::chrono::milliseconds>(startTime - endTime));
	endTime = startTime;

	lag += static_cast<int>(elapsedTime.count());

	if (elapsedTime.count() == 0) { //the sim loop will not be run if lag was not increased prior ( previous frame normally )
		Sleep(5); 
	}

	const int fps = 60;
	const int lengthOfFrame = 1000 / fps;

	MSG msg;
	while (PeekMessageA(&msg, NULL, 0, 0, PM_REMOVE)) {
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}

	//game-sim loop, is independent of rendering
	std::chrono::time_point<std::chrono::steady_clock> oldTime = std::chrono::steady_clock::now();
	while (lag >= lengthOfFrame) {
		//update the player and ship
		if (!getInput()) break;

		game.step();					

		//finish tick
		lag -= lengthOfFrame;
		tickCount++;	//keep track of how many ticks weve done this second
	}

	//render the frame
	Graphics::get().getRoot()->renderOneFrame();
	frameCount++;	//keep trrack of how many frames weve done this second

	//handle per-second stats
	fpsElapsedTime += static_cast<long>(elapsedTime.count());
	if (fpsElapsedTime >= 1000) {

		std::stringstream sstr;
		sstr << frameCount << ", "
					 << tickCount;

		mCefGui->writeJavascript("setStatus", sstr.str().c_str(), url);
		fpsElapsedTime = 0;
		frameCount = 0;
		tickCount = 0;
	}
}
	

 

Edited by h8CplusplusGuru

Share this post


Link to post
Share on other sites

Thanks for the feedback guys, I am putting this on hold because I'm swamped in math (trying to watch ALL khan academy videos in order, 600 videos in), but when the times come I'm definetly putting my full attention into this :P

Share this post


Link to post
Share on other sites
10 hours ago, MarcusAseth said:

Thanks for the feedback guys, I am putting this on hold because I'm swamped in math (trying to watch ALL khan academy videos in order, 600 videos in), but when the times come I'm definetly putting my full attention into this :P

If you need help later on, just send me a PM.

@h8CplusplusGuru not sure if your loop is just an example to run and show, but you should never 'sleep' when waiting, it's not a good practice. You cannot guarantee when sleep will end, only that sleep will start and stay as such for the minimum time you set. Don't use it to regulate frames.

Share this post


Link to post
Share on other sites

It really only matters when debugging the loop, such that elapsedTime may == 0. In actual app that is unlikely, and the sleep is not ran. It actually makes sense to have sleep(5) if you were to troll the code more effectively. 

Share this post


Link to post
Share on other sites
1 minute ago, h8CplusplusGuru said:

It really only matters when debugging the loop, such that elapsedTime may == 0. In actual app that is unlikely, and the sleep is not ran. It actually makes sense to have sleep(5) if you were to troll the code more effectively. 

No problem. I just need to state this because new programmers are using sleep to regulate Frames, and it's a horrible practice.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now


  • Advertisement