From 07885a2b86c98ed650e836c2110aa192e936e811 Mon Sep 17 00:00:00 2001 From: Mitchell Hansen Date: Wed, 20 Jul 2016 22:17:19 -0700 Subject: [PATCH] Ported over the 'fix your timestep' game loop with the nifty C++11 chrono stuff --- cmake/.DS_Store => .DS_Store | Bin 6148 -> 6148 bytes .gitignore | 1 + src/.main.cpp.swp | Bin 12288 -> 0 bytes src/main.cpp | 73 +++++++++++++++++++++++++++-------- 4 files changed, 57 insertions(+), 17 deletions(-) rename cmake/.DS_Store => .DS_Store (97%) create mode 100644 .gitignore delete mode 100644 src/.main.cpp.swp diff --git a/cmake/.DS_Store b/.DS_Store similarity index 97% rename from cmake/.DS_Store rename to .DS_Store index 5008ddfcf53c02e82d7eee2e57c38e5672ef89f6..9a874b5768f336915163bb88cd434575b859f936 100644 GIT binary patch delta 72 zcmZoMXfc?eJ=s8n#ekWin4yRvIi)x`Cn-Nahmipan4mOB0Eh(`pz<3Nm$6UQ6JguT I&GC~T02$#9#Q*>R delta 51 vcmZoMXfc?e&B4IH0LBv&MFg0D92j6^U=Y}txQu;b0oP;$5thx|96$L1$0-Qd diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e43b0f9 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.DS_Store diff --git a/src/.main.cpp.swp b/src/.main.cpp.swp deleted file mode 100644 index 24e86fda14613ca3f7dd9f573575a78fdcce2709..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 12288 zcmeI2KW`H;6u@03G87POm5@xj+)2shN(SJXcA(HwDFH-9J0f%E=&U-nWjjO_6c$DX zm>BsKzzTc-_z)}%dF#NIxZk5Uk~V>MiI7J3&C#!q zYoxqL$UykKtrC)5fa2l(|MdC)H@yG<0elC(0bhYHz-yoYI>0U9DsUe73F|)tAAtA3 zJK!zw0w{p{Ko8gl?g1Sj>{;!*gx;`ZG=K)s02)98XaEhM0W^RH(7^v=;FQ#AmiwVq zG~FaHI1QItZ6s8oM@b?CC#OFe44UW z6MY)-XI%NjpBPTJwy5i8+1)UcWji6Zh@X$QFNNeZn$%{rlvUJtbvy~0Ev8k{Q21zW zop28fS5X|(kQIK_NfO8Xu8?v^OKqc&r23a-dzN!mowJ%07EXB(&Q7a!(MFo2{n`CT zTov3NRrct(&??!XD7fzFf=BH%ZBV$x4O&0g?RD$H7ZJ+S@V$Hlry%P><#JeX+6e)z XJ +#include #include +#include +#include +#include + +const int WINDOW_X = 600; +const int WINDOW_Y = 800; -int main(){ +float elap_time(){ + static std::chrono::time_point start; + static bool started = false; + if (!started){ + start = std::chrono::system_clock::now(); + started = true; + } + + std::chrono::time_point now = std::chrono::system_clock::now(); + std::chrono::duration elapsed_time = now - start; + return elapsed_time.count(); +} - sf::RenderWindow window(sf::VideoMode(300, 300), "SFML"); - sf::CircleShape shape(10.0f); - shape.setFillColor(sf::Color::Green); - - while (window.isOpen()) { - sf::Event event; - while (window.pollEvent(event)){ - if (event.type == sf::Event::Closed){ - window.close(); - } - } +int main() { + elap_time(); + std::mt19937 rng(time(NULL)); + std::uniform_int_distribution rgen(100, 400); - window.clear(); - window.draw(shape); - window.display(); + sf::RenderWindow window(sf::VideoMode(WINDOW_X, WINDOW_Y), "SFML"); + float step_size = 0.005f; + double frame_time = 0.0, elapsed_time = 0.0, delta_time = 0.0, accumulator_time = 0.0, current_time = 0.0; + + + while (window.isOpen()) { + + sf::Event event; + while (window.pollEvent(event)) { + if (event.type == sf::Event::Closed) + window.close(); } - return 0; + elapsed_time = elap_time(); + delta_time = elapsed_time - current_time; + current_time = elapsed_time; + if (delta_time > 0.02f) + delta_time = 0.02f; + accumulator_time += delta_time; + + while ((accumulator_time - step_size) >= step_size) { + accumulator_time -= step_size; + + // Update(step_size); + } + + window.clear(sf::Color::Black); + + window.display(); + + + + } + return 0; + }