I'd like to know how to make a game loop process.As it is asynchronous, one of them ends first, so what should I do then?
An object to be drawn by calculation is calculated and drawn in the next frame.In the meantime, the calculation thread calculates the position of the next frame and draws it based on the next frame.
While drawing, another thread performs the calculation, so
For example, if the calculation is completed first, the drawing will draw based on the calculated value, so extreme talk will occur, and the calculation will be drawn in that position with reference to two values during one drawing.
1. I want to know what to do until the drawing is finished, for example, when the calculation is finished earlier than the drawing in the processing in which the drawing and the calculation are respectively operated by an independent threads.What should I do in the opposite case?提示Please refer to the presented image
2. If you want to create a program such as movement, you should multiply the delta time. Is this delta time for drawing or delta time for calculation?
Calculation and drawing loops
Implemented in std::thread
.
utilization libraries:opengl,glew,glfw,glm
OS:ubuntu
Language: C++
#include<iostream>
# include <thread>
# include <future>
# include <memory>
# include <ctime>
# include <unistd.h>
# include <random>
std::random_device rnd;// Generate non-deterministic random number generator
std::mt19937mt(rnd()); // 32-bit version of Mersenne Twister, argument is initial seed value
void thread_Update()
{
std::uniform_int_distribution<>land(0,10);//[0,99] uniform random number in the range
for (inti=0; i<10;i++)
{
std::cout<<"thread_A"<<std::endl;
std::this_thread::sleep_for(std::chrono::milliseconds(land(mt)*100));
}
}
void thread_Render()
{
std::uniform_int_distribution<>land(0,10);//[0,99] uniform random number in the range
for (inti=0; i<10;i++)
{
std::cout<<"thread_B"<<std::endl;
std::this_thread::sleep_for(std::chrono::milliseconds(land(mt)*100));
}
}
int main()
{
try
{
std::thread t1(thread_Render);
std::threadt2(thread_Update);
std::cout<<"end"<<std::endl;
std::this_thread::sleep_for (std::chrono::milliseconds(5000));
t1.join();
t2.join();
}
catch(std::exception&e)
{
std::cout<<e.what()<<std::endl;
}
return 0;
}
Question 1 can be asked according to the specifications.
Question 2 has nothing to do with Question 1, so please ask a different question.
© 2024 OneMinuteCode. All rights reserved.