Bonjour à tous,
Je viens de créer une début de jeu en réalité augmentée, avec la SFML et OpenCV.
L'ensemble tourne bien, mais j'aimerai savoir comment je peux améliorer mon code (Template, Fonctions, Classes), pour aller plus vite et ne pas utiliser inutilement de la mémoire.
Je vous joins le code:
///--HEADER C++--///
#include <string>
#include <sstream>
#include <iostream>
///--HEADER SFML--///
#include <SFML/Graphics.hpp>
#include <SFML/Graphics/Text.hpp>
///--HEADER OPENCV2--///
#include <opencv2/core/core.hpp>
#include <opencv2/video/video.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
///--FONCTION FLOAT -> STRING--///
std::string float2string(float f)
{
std:: ostringstream os;
os << f;
return os.str();
}
int main()
{
int L(640);
int H(480);
int Ep(2);
float Radius(5.0f);
int stepColor(0);
sf::RenderWindow window(sf::VideoMode(L,H), "Flight Mode");
cv::Mat frameRGB, frameRGBA;
cv::VideoCapture cap(0);
if(!cap.isOpened())
{
return 0;
}
///--RECTANGLE CIBLE--///
sf::RectangleShape cible(sf::Vector2f(50,50));
cible.setPosition((L/2)+25,(H/2)+25);
cible.setFillColor(sf::Color(0,0,0));
///--RETICULE CENTRAL--//
sf::RectangleShape recH(sf::Vector2f(50,1));
recH.setPosition(295,H/2);
recH.setFillColor(sf::Color(0,0,0));
sf::RectangleShape recW(sf::Vector2f(50,1));
recW.setPosition(L/2,215);
recW.setFillColor(sf::Color(0,0,0));
recW.rotate(90);
///--BARRE GAUCHE--///
sf::RectangleShape barG(sf::Vector2f(300,Ep));
barG.setPosition(200,90);
barG.setFillColor(sf::Color(0,255,0));
barG.rotate(90);
///--BARRE DROITE--///
sf::RectangleShape barD(sf::Vector2f(300,Ep));
barD.setPosition(440,90);
barD.setFillColor(sf::Color(0,255,0));
barD.rotate(90);
bool move = false;
float zone(10.0f);
int turbo(6);
sf::Image image;
sf::Texture texture;
sf::Event event;
sf::Sprite sprite;
sf::Clock tickClock;
sf::Time timeSinceLastUpdate = sf::Time::Zero;
sf::Time TimePerFrame = sf::seconds(1.f / 60.f);
sf::Time duration = sf::Time::Zero;
sf::Font font;
sf::Text myText;
sf::Text coordX;
sf::Text coordY;
sf::Text coordZ;
sf::Text coordPovX;
sf::Text coordPovY;
sf::Text coordText;
///--CHARGEMENT DE LA FONTE--///
font.loadFromFile("Font/arial.ttf");
while (window.isOpen())
{
cap >> frameRGB;
if(frameRGB.empty())
{
break;
}
///--OPENCV RECTANGLE--///
cv::rectangle(frameRGB,cvPoint(300,220),cvPoint(340,260),CV_RGB(255,0,0),1,1);
///--AFFICHAGE TEXTE--///
myText.setFont(font);
myText.setString("Engaging target...");
myText.setCharacterSize(12);
myText.setColor(sf::Color::Green);
myText.setStyle(sf::Text::Italic);
myText.setPosition(10,5);
cv::cvtColor(frameRGB, frameRGBA, cv::COLOR_BGR2RGBA);
image.create(frameRGBA.cols, frameRGBA.rows, frameRGBA.ptr());
///--POSITION JOYSTICK--///
float x = sf::Joystick::getAxisPosition(0, sf::Joystick::X);
float y = sf::Joystick::getAxisPosition(0, sf::Joystick::Y);
float z = sf::Joystick::getAxisPosition(0, sf::Joystick::Z);
float povx = sf::Joystick::getAxisPosition(0, sf::Joystick::PovX);
float povy = sf::Joystick::getAxisPosition(0, sf::Joystick::PovY);
///--CONVERTIR FLOAT EN STRING--///
std::string coordX = float2string(x);
std::string coordY = float2string(y);
std::string coordZ = float2string(z);
std::string coordPovX = float2string(povx);
std::string coordPovY = float2string(povy);
///--AFFICHE STRING DANS FENETRE--///
std::ostringstream os;
os << "X: " << coordX << std::endl << "Y: " << coordY << std::endl << "Z: " << coordZ << std::endl
<< std::endl << "Pov X: " << coordPovX << std::endl << "Pov Y: " << coordPovY << std::endl;
///--ROTATION CIBLE--///
cible.setRotation(180);
cible.rotate(x);
///--PARAMETRES D'AFFICHAGE DU STRING--///
coordText.setString(os.str());
coordText.setCharacterSize(12);
coordText.setFont(font);
coordText.setColor(sf::Color::Green);
coordText.setPosition(10,25);
///--AFFICHE COORDONNEES JOYSTICK SI MOUVT--///
if (event.type == sf::Event::JoystickMoved)
{
move = true;
std::cout << "X axis: " << x << std::endl;
std::cout << "Y axis: " << y << std::endl;
std::cout << "Z axis: " << z << std::endl;
}
else
{
move = false;
}
timeSinceLastUpdate += tickClock.restart();
///--SI LE BOUTON 0 JOYSTICK EST PRESSE--///
if (sf::Joystick::isButtonPressed(0,0))
{
cible.setFillColor(sf::Color(255,0,0));
}
///--SI LE BOUTON 1 JOYSTICK EST PRESSE--///
if (sf::Joystick::isButtonPressed(0,1))
{
int stepColor(15);
cible.setFillColor(sf::Color(0,255,0));
}
///--CHANGE COULEUR--///
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
{
if (stepColor < 255)
{
stepColor += 5 ;
}
//cible.setFillColor(sf::Color(255,0,0,stepColor));
recH.setFillColor(sf::Color(0,255,0,stepColor));
recW.setFillColor(sf::Color(0,255,0,stepColor));
barD.setFillColor(sf::Color(0,255,0,stepColor));
barG.setFillColor(sf::Color(0,255,0,stepColor));
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
{
if (stepColor > 0)
{
stepColor -= 5 ;
}
//cible.setFillColor(sf::Color(255,0,0,stepColor));
recH.setFillColor(sf::Color(0,255,0,stepColor));
recW.setFillColor(sf::Color(0,255,0,stepColor));
barD.setFillColor(sf::Color(0,255,0,stepColor));
barG.setFillColor(sf::Color(0,255,0,stepColor));
}
while (timeSinceLastUpdate > TimePerFrame)
{
int stepColor(15);
timeSinceLastUpdate -= TimePerFrame;
/*
if (x > zone || x < -zone || y > zone || y < -zone)
{
recH.move(turbo*x*TimePerFrame.asSeconds(), 0);
recW.move(0, turbo*y*TimePerFrame.asSeconds());
cible.move(turbo*x*TimePerFrame.asSeconds(), turbo*y*TimePerFrame.asSeconds());
}
*/
}
if (!texture.loadFromImage(image))
{
break;
}
sprite.setTexture(texture);
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
///--AFFICHE LES FORMES--///
window.draw(sprite);
///--AFFICHE LE RETICULE--///
window.draw(recW);
window.draw(recH);
window.draw(barG);
window.draw(barD);
///--AFFICHE CIBLE--///
//window.draw(cible);
///--AFFICHE LES TEXTES--///
window.draw(myText);
window.draw(coordText);
///--AFFICHE L'ENSEMBLE--///
window.display();
window.clear();
}
return 0;
}
Qu'en pensez-vous ?
Peut-on l'améliorer ?
Merci pour votre aide.
Amitiés,
Chris