-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
64 lines (52 loc) · 1.57 KB
/
main.cpp
File metadata and controls
64 lines (52 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include "simulation.h"
#include "SDL_image.h"
#include <iostream>
#include <chrono>
#include <thread>
int main(int argc, char** argv)
{
//declare window and start running:
SDL_Window* window;
running = true;
//initialize SDL and check for errors along the way:
if (SDL_Init(SDL_INIT_EVERYTHING) == 0)
{
window = SDL_CreateWindow("ElementSim", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, WIDTH * PARTICLE_SIZE, HEIGHT * PARTICLE_SIZE, SDL_WINDOW_SHOWN);
if(!window)
return 0;
SDL_Surface* icon = IMG_Load("assets/icon.png");
SDL_SetWindowIcon(window, icon);
SDL_FreeSurface(icon);
}
else
return 0;
//initialize the simulation:
if (!init_simulation(window))
return 0;
//declare timestepping variables:
using clock = std::chrono::steady_clock;
const double frameDelay = 1000 / 60; //fps
clock::time_point start = clock::now();
clock::time_point last = clock::now();
//timestep until running = false:
while (running)
{
start = clock::now();
std::chrono::duration<double, std::milli> workTime = start - last;
//std::cout << workTime.count() << std::endl;
if (workTime.count() < frameDelay)
{
std::chrono::duration<double, std::milli> deltaMs(frameDelay - workTime.count());
auto deltaMsDuration = std::chrono::duration_cast<std::chrono::milliseconds>(deltaMs);
std::this_thread::sleep_for(std::chrono::milliseconds(deltaMsDuration.count())); //sleep to keep fps capped
}
last = clock::now();
handle_input();
run_simulation();
render();
}
//clean up before exiting:
close_simulation();
SDL_DestroyWindow(window);
return 0;
}