#define _USE_MATH_DEFINES // for C++
#include <cmath>
#include <sfml.h>//incudeds all sfml libs, and set sfml libs as static link
//#include <stdio.h>
#include <sstream>
#include <conio.h>
//#include <iostream>
#include <sstream>
#include <windows.h>
//two versions
//SFML 1.6 in Visual Studio Express 2008
//SFML 2.4.0 in Visual Studio Express 2013
#define USING_SFML_240//comment-out to use sfml 1.6, change properties below...
//#if defined USING_SFML_240
//#else//USING SFML 1.6
//#endif//USING_SFML_240
//properties...
//C:\Programming\C++Projects\SFML-2.4.0\include
//C:\Programming\C++Projects\SFML-2.4.0\lib
//E:\Programming\C++Projects\SFML-1.6\include
//E:\Programming\C++Projects\SFML-1.6\lib
void main(void)
{
unsigned int WindowX = 800;
unsigned int WindowY = 600;
std::stringstream string;
string << "start string";
sf::RenderWindow App(sf::VideoMode(WindowX, WindowY, 32), "Speed Test - SFML 1.6 VS 2008");//changed to SFML 2.4.0 below, if selected
sf::Font font;
sf::Image image;
sf::Sprite sprite;
#if defined USING_SFML_240
App.setTitle("Speed Test - SFML 2.4.0 VS 2013");
font.loadFromFile("courbd.ttf");
sf::Text text("Init", font);
text.setColor(sf::Color::Red);
text.setString(string.str());
text.setCharacterSize(30);
image.create(App.getSize().x,App.getSize().y,sf::Color(0, 50, 50));
sf::Texture texture;
texture.create(WindowX, WindowY);
sprite.setTexture(texture);
#else//USING SFML 1.6
font.LoadFromFile("courbd.ttf");
sf::String text("Init", font);
text.SetColor(sf::Color::Red);
text.SetSize(30);
image.Create(App.GetWidth(),App.GetHeight(),sf::Color(0, 50, 50));
sprite.SetImage(image);
//sprite.SetBlendMode(sf::Blend::None);
sf::View& DefaultView = App.GetDefaultView();
#endif//USING_SFML_240
sf::Color color = sf::Color::Black;
sf::Clock clock;
float LoopTime = 0.0;
//sf::Clock DrawTimer;
float DisplayTime = 0.0;
float PollTime = 0.0;
bool FetchPollTime = true;
float SolvePixelTime = 0.0;
bool FetchSolvePixelTime = true;
float SetPixelTime = 0.0;
bool FetchSetPixelTime = true;
float MathLoopTime = 0.0;
float OldMathLoopTime = 0.0;
unsigned int PixelX = 0;
unsigned int PixelY = 0;
const unsigned int BrightColor = 128;
unsigned TestSquareX = 300;
unsigned TestSquareY = 300;
unsigned TestSquareSize = 15;
sf::Color TestSquareColor(192, 192, 192);
double FrameRate = 60;
float FrameTime = 1 / (float)FrameRate;//as seconds
const unsigned int DivFrameRate = 5;//slows text rate
unsigned int TextCount = 0;
unsigned int ShiftRegister = 0xffffffff;
unsigned int Cycles = 0;
unsigned int FilteredCycles = 0;
const unsigned int ShiftCoef = 9;//ShiftCoef * 2 + Cycles bit-width MUST fit within 32-bits
const unsigned int MultCoef = 1 << (ShiftCoef-1);
const double CornerFreq = 0.5;
const double Coef = pow(M_E, -M_2_PI * (CornerFreq/FrameRate));
const unsigned int BackCoef = (unsigned int)(Coef * (double)MultCoef);
const unsigned int ForwardCoef = MultCoef - BackCoef;
#if defined USING_SFML_240
App.clear(sf::Color::Magenta);
////App.draw(sprite);
App.draw(text);
App.display();
Sleep(500);
//text.setString("Test");
#else//USING SFML 1.6
App.Clear(sf::Color::Magenta);
////App.draw(sprite);
App.Draw(text);
App.Display();
Sleep(500);
//text.setString("Test");
#endif//USING_SFML_240
//main loop
char key_char = 0;
while(key_char != 27)//exit program
{
//poll events
sf::Event Event;
#if defined USING_SFML_240
if(FetchPollTime)
while (App.pollEvent(Event))
{
//exit
if((Event.type == sf::Event::Closed) || (Event.type == sf::Event::KeyPressed && Event.key.code == sf::Keyboard::Escape))key_char = 27;
//gain focus
if(Event.type == sf::Event::GainedFocus)
{
App.display();
}
//resize
if(Event.type == sf::Event::Resized)
{
WindowX = App.getSize().x;
WindowY = App.getSize().y;
image.create(WindowX, WindowY, sf::Color(0, 50, 25));
texture.create(WindowX, WindowY);
texture.update(image);
sprite.setTextureRect(sf::IntRect(0, 0, WindowX, WindowY));
//sprite.setTexture(texture);
App.setView(sf::View(sf::FloatRect(0.0f, 0.0f, App.getSize().x, App.getSize().y)));
}
}
if(FetchPollTime)
{
PollTime = clock.getElapsedTime().asSeconds() - DisplayTime;
FetchPollTime = false;
}
#else
if(FetchPollTime)
while (App.GetEvent(Event))
{
//exit
if(Event.Type == sf::Event::Closed || (sf::Event::KeyPressed && Event.Key.Code == sf::Key::Escape))key_char = 27;
//gain focus
if(Event.Type == sf::Event::GainedFocus)
{
App.Display();
}
//resize
if(Event.Type == sf::Event::Resized)
{
WindowX = App.GetWidth();
WindowY = App.GetHeight();
image.Create(WindowX, WindowY, sf::Color(0, 50, 25));
sprite.SetImage(image);
sprite.SetSubRect(sf::IntRect(0, 0, WindowX, WindowY));
//sprite.setTexture(texture);
DefaultView.SetFromRect(sf::FloatRect(0, 0, (float)App.GetWidth(), (float)App.GetHeight()));
}
}
if(FetchPollTime)
{
PollTime = clock.GetElapsedTime() - DisplayTime;
FetchPollTime = false;
}
#endif//USING_SFML_240
//32-bit linear feedback shift register, next pseudo-random number
bool NextState = false;
if((ShiftRegister & (1 << (1-1))) != 0)NextState = true;
if((ShiftRegister & (1 << (2-1))) != 0)NextState = !NextState;
if((ShiftRegister & (1 << (21-1))) != 0)NextState = !NextState;
if((ShiftRegister & (1 << (32-1))) != 0)NextState = !NextState;
ShiftRegister *= 2;
if(NextState)ShiftRegister += 1;
//psuedo-random color, 1st 3 Shift Register bits
if((ShiftRegister & (1 << (1-1))) != 0)color.r = BrightColor; else color.r = 0;
if((ShiftRegister & (1 << (2-1))) != 0)color.g = BrightColor; else color.g = 0;
if((ShiftRegister & (1 << (3-1))) != 0)color.b = BrightColor; else color.b = 0;
//psuedo-random pixel position
PixelX = ShiftRegister / (1 << 3) % WindowX;
PixelY = ShiftRegister / (1 << 14) % WindowY;
if(PixelX >= TestSquareX && PixelX < TestSquareX + TestSquareSize
&& PixelY >= TestSquareY && PixelY < TestSquareY + TestSquareSize)
color = TestSquareColor;
Cycles++;
//change image
#if defined USING_SFML_240
if(FetchSolvePixelTime)
{
SolvePixelTime = clock.getElapsedTime().asSeconds() - DisplayTime - PollTime;
FetchSolvePixelTime = false;
}
image.setPixel(PixelX, PixelY, color);
if(FetchSetPixelTime)
{
SetPixelTime = clock.getElapsedTime().asSeconds() - DisplayTime - PollTime - SolvePixelTime;
FetchSetPixelTime = false;
}
//frame-rate passed
LoopTime = clock.getElapsedTime().asSeconds();//read it once
if(LoopTime >= sf::seconds(FrameTime).asSeconds())// || index >= width * heigth - 1)
{
//reset time
clock.restart();
OldMathLoopTime = 0.0;
#else//USING SFML 1.6
if(FetchSolvePixelTime)
{
SolvePixelTime = clock.GetElapsedTime() - DisplayTime - PollTime;
FetchSolvePixelTime = false;
}
image.SetPixel(PixelX, PixelY, color);
if(FetchSetPixelTime)
{
SetPixelTime = clock.GetElapsedTime() - DisplayTime - PollTime - SolvePixelTime;
FetchSetPixelTime = false;
}
//frame-rate passed
LoopTime = clock.GetElapsedTime();//read it once
if(LoopTime >= FrameTime)
{
//reset time
clock.Reset();
OldMathLoopTime = 0.0;
#endif//USING_SFML_240
//filter cycles
FilteredCycles = Cycles * (ForwardCoef) + FilteredCycles * BackCoef / MultCoef;
//read and reset cycles
if(TextCount >= DivFrameRate)
{
TextCount = 0;
string.str("????");
string << "\n Cycles => " << Cycles << " Filtered => " << FilteredCycles / MultCoef
<< "\n Display Time => " << DisplayTime
<< "\n Poll Time => " << PollTime
<< "\n Solve Pixel Time => " << SolvePixelTime
<< "\n Set Pixel Time => " << SetPixelTime
<< "\n Math Loop Time => " << MathLoopTime;
}
else TextCount++;
FetchPollTime = true;
FetchSolvePixelTime = true;
FetchSetPixelTime = true;
Cycles = 0;
#if defined USING_SFML_240
//display image
App.clear(sf::Color::Green);
//App.clear();
texture.update(image);
//sprite.setTextureRect(sf::IntRect(0, 0, WindowX, WindowY));
App.draw(sprite);
text.setString(string.str());
App.draw(text);
App.display();
DisplayTime = clock.getElapsedTime().asSeconds();
}
//else
//{
// MathLoopTime = MathLoopClock.getElapsedTime().asSeconds();
// MathLoopClock.restart();
//}
#else//USING SFML 1.6
//display image
App.Clear(sf::Color::Green);
//App.clear();
sprite.SetImage(image);
App.Draw(sprite);
text.SetText(string.str());
App.Draw(text);
App.Display();
DisplayTime = clock.GetElapsedTime();
}
#endif//USING_SFML_240
else
{
if(OldMathLoopTime != 0.0)//ignore loop after display
{
MathLoopTime = LoopTime - OldMathLoopTime;
}
OldMathLoopTime = LoopTime;
}
}
}