My program loads the processor by 80%. Some time ago i was have same problem with gpu, i solved it by timer. Cpu loading was about 50-60 % and now - 80 %. What i did wrong? I can't solve it problem.
#include <fstream>
#include <SFML/Graphics.hpp>
#include <ctime>
using namespace std;
char * readFile(char * filePath, unsigned int &lengthBuffer, fstream &f) {
f.open(filePath, ios::in | ios::app | ios::binary);
f.seekg (0, f.end);
lengthBuffer = f.tellg();
f.seekg (0, f.beg);
char * fileBuffer = new char[lengthBuffer];
f.read(fileBuffer, lengthBuffer);
f.close();
return fileBuffer;
}
char * writeFile(char * fileBuffer, char * filePath, unsigned int &lengthBuffer, fstream &f, bool &fileCreated){
filePath[23] += 1;
f.open(filePath, ios::out | ios::app | ios::binary);
f.write(fileBuffer, lengthBuffer);
filePath[23] -= 1;
fileCreated = 1;
f.close();
return fileBuffer;
}
void removeFile(char * filePath, bool &fileCreated) {
filePath[23] += 1;
remove(filePath);
filePath[23] -= 1;
fileCreated = 0;
}
unsigned int mouse(unsigned int &funcSelector, bool &mouseLeft, sf::RenderWindow &window) {
mouseLeft = sf::Mouse::isButtonPressed(sf::Mouse::Left);
sf::Vector2i mouse = sf::Mouse::getPosition(window);
if (mouseLeft & mouse.y < 100) {
funcSelector = 1 + mouse.x/100;
}
return funcSelector;
}
int main(){
sf::RenderWindow window(sf::VideoMode(500, 400), "COT++", sf::Style::Titlebar);
sf::VertexArray points(sf::Points, 3000);
sf::Event event;
fstream f;
bool mouseLeft, fileCreated = 0;
unsigned int n = 0, funcSelector = 0, lengthBuffer = 0;
float start = 0, now = 0, x = 0.f, y = 1.f, pos = 0.f;
char * fileBuffer, filePath[30] = "c:/users/79994/desktop/a.exe";
while (x < 500.f){
points[n].position = sf::Vector2f(x + pos, y + pos);
points[n].color = sf::Color::Green;
x += 1.f;
n += 1;
if (x == 500.f & y < 100.f){
x = 0.f;
y += 100.f;
}
}
x = 100.f, y = 1.f;
while (x < 600.f){
points[n].position = sf::Vector2f(x + pos, y + pos);
points[n].color = sf::Color::Green;
y += 1.f;
n += 1;
if (y > 101.f){
x += 100.f;
y = 1.f;
}
}
while (window.isOpen())
{
while (window.pollEvent(event)) {
if((clock()-start) > 50){
start = clock();
switch(funcSelector){
case 5: window.close();
break;
case 1: if (lengthBuffer == 0){
fileBuffer = readFile(filePath, lengthBuffer, f);
}
break;
case 2: if (lengthBuffer > 0 & fileCreated == 0) {
writeFile(fileBuffer, filePath, lengthBuffer, f, fileCreated);
}
break;
case 3: removeFile(filePath, fileCreated);
break;
}
mouse(funcSelector, mouseLeft, window);
window.clear();
window.draw(points);
window.display();
}
}
}
return 0;
}
P.S. "It looks like your post is mostly code; please add some more details" - I think i described enough details.
It depends on what you want to do. For the simple example you've given, you can use waitEvent
instead of pollEvent
. I recommend checking if waitEvent
suits your needs, but it looks like it will.
However, if you insist on using pollEvent
, read on!
Your while(window.pollEvent(event))
loop is running at full speed 100% of the time, polling non-stop, even though it looks like your goal is to only "do work" every 50
clocks. (Clocks aren't consistently correlated to real units of measure except by CLOCKS_PER_SEC
, which is implementation defined... and std::clock
may not correlate to wall time... and CLOCKS_PER_SEC
might be hardcoded to 1 million when that isn't a meaningful value... but I digress. Your problem would exist no matter how you track time, for your code as written, though you might want some other timing mechanism.)
Here's a possible solution with minimal changes to your code. Replace:
while (window.pollEvent(event)) {
if((clock()-start) > 50){
with the following:
while (window.pollEvent(event)) {
const auto delta = clock() - start;
if (delta < 50) {
std::this_thread::sleep_for(std::chrono::microseconds(
static_cast<int>(1E6 * (50 - delta) / CLOCKS_PER_SEC)));
} else {
What this does, is instead of constantly call window.pollEvent(event)
, it'll call it, sleep for a bit if it needs to, then do some work.
There are some downsides to this approach, too, but it should get you started on how to think about the problem.
You'll need to #include <chrono>
as well (or, you can find some other way of sleeping for the almost-right duration, if you're not using C++11
or later).