我是初学者,我尝试使用 SFML 库,但我的代码有问题
#pragma region include
#include <SFML/Graphics.hpp>
#include <iostream>
#include <string>
#include <cstdlib>
#pragma endregion include
sf::RenderWindow fenetre(sf::RenderWindow*);
sf::RectangleShape rectDraw(sf::RectangleShape*);
sf::CircleShape cercleDraw(sf::CircleShape*);
void CentreCercle(sf::CircleShape *cercle, int*, int*);
void fermetureFenetre();
void main()
{
#pragma region variables
sf::RenderWindow window;
fenetre(&window);
sf::RectangleShape rect;
rectDraw(&rect);;
sf::CircleShape cercle;
cercleDraw(&cercle);
int xCentreCercle;
int yCentreCercle;
CentreCercle(&cercle, &xCentreCercle, &yCentreCercle);
float speed = 1;
sf::Vector2i positionSouris;
#pragma endregion variables
while(window.isOpen())
{
#pragma region fenOpen
sf::Event event;
while(window.pollEvent(event))
{
if(event.type == sf::Event::Closed)
window.close();
}
#pragma endregion fenOpen
#pragma region KeyboardRectangle
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
rect.move(0, -speed);
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
rect.move(0, speed);
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
rect.move(-speed, 0);
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
rect.move(speed, 0);
#pragma endregion KeyboardRectangle
#pragma region KeyboardCercle
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Z))
cercle.move(0, -speed);
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::S))
cercle.move(0, speed);
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Q))
cercle.move(-speed, 0);
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::D))
cercle.move(speed, 0);
#pragma endregion KeyboardCercle
#pragma region MouseCercle
if(sf::Mouse::isButtonPressed(sf::Mouse::Left))
{
bool boucle = true;
sf::Vector2f position = cercle.getPosition();
xCentreCercle = cercle.getPosition().x + cercle.getRadius();
yCentreCercle = cercle.getPosition().y+ cercle.getRadius();
float rayon = cercle.getRadius();
positionSouris = sf::Mouse::getPosition(window);
int xSouris = positionSouris.x;
int ySouris = positionSouris.y;
float resultat = (xCentreCercle-xSouris)*(xCentreCercle-xSouris)+(yCentreCercle-ySouris)*(yCentreCercle-ySouris);
if(sqrt(resultat)<= rayon)
{
int diffX;
int diffY;
diffX = xSouris-cercle.getPosition().x;
diffY = ySouris-cercle.getPosition().y;
while(boucle == true)
{
positionSouris = sf::Mouse::getPosition(window);
xSouris = positionSouris.x;
ySouris = positionSouris.y;
cercle.setPosition(xSouris - diffX, ySouris - diffY);
window.draw(rect);
window.draw(cercle);
window.display();
window.clear();
if (sf::Mouse::isButtonPressed(sf::Mouse::Left) == false)
boucle = false;
}
}
}
if(sf::Mouse::isButtonPressed(sf::Mouse::Right) && sf::Keyboard::isKeyPressed(sf::Keyboard::C))
{
positionSouris = sf::Mouse::getPosition(window);
int xSouris = positionSouris.x;
int ySouris = positionSouris.y;
cercle.setPosition(xSouris, ySouris);
}
#pragma endregion MouseCercle
#pragma region Mouserectangle
if(sf::Mouse::isButtonPressed(sf::Mouse::Left))
{
bool boucle = true;
sf::Vector2f position = rect.getPosition();
sf::Vector2f taille = rect.getSize();
int xMax = position.x + taille.x;
int xMin = position.x;
int yMax = position.y + taille.y;
int yMin = position.y;
positionSouris = sf::Mouse::getPosition(window);
int xSouris = positionSouris.x;
int ySouris = positionSouris.y;
if(xSouris > xMin && xSouris < xMax && ySouris > yMin && ySouris < yMax)
{
int diffX;
int diffY;
diffX = xSouris-xMin;
diffY = ySouris-yMin;
while(boucle == true)
{
positionSouris = sf::Mouse::getPosition(window);
xSouris = positionSouris.x;
ySouris = positionSouris.y;
rect.setPosition(xSouris - diffX, ySouris - diffY);
window.draw(rect);
window.draw(cercle);
window.display();
window.clear();
if (sf::Mouse::isButtonPressed(sf::Mouse::Left) == false)
boucle = false;
}
}
}
if(sf::Mouse::isButtonPressed(sf::Mouse::Right)&& sf::Keyboard::isKeyPressed(sf::Keyboard::R))
{
positionSouris = sf::Mouse::getPosition(window);
int xSouris = positionSouris.x;
int ySouris = positionSouris.y;
rect.setPosition(xSouris, ySouris);
}
#pragma endregion MouseRectangle
window.draw(rect);
window.draw(cercle);
window.display();
window.clear();
}
}
void fenetre(sf::RenderWindow &window)
{
window.create(sf::VideoMode(800,600),"Tuto SFML");
window.setPosition(sf::Vector2i(50,50));
window.setFramerateLimit(60);
}
void fermetureFenetre(sf::RenderWindow &window)
{
sf::Event event;
while(window.pollEvent(event))
{
if(event.type == sf::Event::Closed)
window.close();
}
}
void rectDraw(sf::RectangleShape &rect)
{
rect.setSize(sf::Vector2f(200, 100));
rect.setPosition(10, 10);
rect.setFillColor(sf::Color(250, 100, 150));
}
void cercleDraw(sf::CircleShape &cercle)
{
cercle.setFillColor(sf::Color(100,250,50));
cercle.setRadius(50);
cercle.setPosition(0, 0);
}
void CentreCercle(sf::CircleShape &cercle, int &xCentreCercle, int
&yCentreCercle)
{
xCentreCercle = cercle.getPosition().x + cercle.getRadius();
yCentreCercle = cercle.getPosition().y+ cercle.getRadius();
}
这个程序只是用来用键盘或鼠标移动矩形或圆形。当所有程序都在主函数中时,我没有任何问题,但我想在将来训练时使用子例程和类,但出现以下错误! error C2248
经过搜索,我认为问题来自函数“sf::RenderWindow”,但又是初学者,我不明白如何解决这个问题。
感谢帮助! :)
问题是@Jepessen 提到的,你的声明基本上有一个返回类型 sf::RenderWindow,而你的参数是 Pointer sf::RenderWindow 类型:
sf::RenderWindow fenetre(sf::RenderWindow*);
然后当你定义函数时,你返回void,你的参数是一个Reference sf::RenderWindow:
void fenetre(sf::RenderWindow &window)
{
// Code
}
要解决此问题,请使 fenetre 声明的返回类型无效,因为您实际上根本没有返回值,并使您的参数引用 sf::RenderWindow:
// Declaration, on top of your program
void fenetre(sf::RenderWindow&);
// Definition, at the bottom
void fenetre(sf::RenderWindow &window)
{
// Code
}
我是一名优秀的程序员,十分优秀!