-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
141 lines (112 loc) · 2.87 KB
/
main.cpp
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include <stdio.h>
#include <iostream>
#include "ansigame.h"
#include <unistd.h>
#include <sys/time.h>
#include <thread>
#include <SFML/Window/Keyboard.hpp>
//#include "icon2.xpm"
using namespace std;
int fps = 60; // change to 50 or 30 or 15 if you want
float w = 1000000/fps;
int waittime = int(w);
int _wait = waittime;
float cpu_pct = 0.0;
float fpscalc = 0.0;
struct timeval framestart, frameend;
bool ENABLE_DEBUG = true;
int frames = 0;
ANSIGame g;
char inputbuffer[16];
void gameloop()
{
g.tx_plot2(std::to_string(inputbuffer[0]).c_str(), BCYAN, BLACK, 30, 10);
frames++;
if(frames==fps) { frames = 0; }
}
void check_input()
{
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
{
cout << "LEFT";
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
{
cout << "RIGHT";
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
{
cout << "UP";
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
{
cout << "DOWN";
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Z))
{
cout << "(1)";
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::X))
{
cout << "(2)";
}
cout << " ";
}
#include "assets/icon2.xpm"
int init()
{
g.key_echo(false);
g.wait_for_resize();
g.clear_screen();
g.show_cursor(false);
g.tx_draw_xpm(sample_xpm, 10, 5);
return 0;
}
int debug()
{
cpu_pct = (float)((waittime-_wait)*100.0 / waittime);
if(cpu_pct>0)
{
g.tx_plot2(std::to_string(cpu_pct).c_str(), BRED, BLACK, 0, 0);
g.tx_plot2(" % CPU ", WHITE, BLACK, 5, 0);
}
gettimeofday(&frameend, NULL);
fpscalc = 1000000.0/((frameend.tv_usec - framestart.tv_usec));
framestart = frameend;
if(fpscalc>0)
{
g.tx_plot2(std::to_string(fpscalc).c_str(), BCYAN, BLACK, 20, 0);
g.tx_plot2(" FPS ", BCYAN, BLACK, 25, 0);
}
return 0;
}
int main()
{
init();
//thread input_monitor(check_input);
struct timeval tv1, tv2;
while(1) // TODO
{
// _wait is the expected FPS in us minus code exec time.
usleep(_wait);
//^ somehow replace this with getch?
// EXEC CODE MARK:
gettimeofday(&tv1, NULL);
//
// run the game loop (bulk of code here!)
gameloop();
g.draw();
check_input();
// EXEC CODE MARK:
gettimeofday(&tv2, NULL);
_wait = waittime - (tv2.tv_usec-tv1.tv_usec); // calc new wait time: this amounts to vblank.
if(_wait <= 0) { _wait = 1; }
else if(_wait >= waittime) { _wait = waittime; }
//
if(ENABLE_DEBUG == true) { debug(); }
// update screen all at once - don't make more changes after this point.
fflush(stdout);
}
//input_monitor.join();
return g.tx_quit();
}