我是初学者,我尝试使用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
搜索后,我认为这个问题来自于函数&#s; :: RenderWindow&#39;但再次初学者我不明白如何解决这个问题。
感谢您的帮助! :)
答案 0 :(得分:0)
问题是@Jepessen提到你的声明基本上有一个返回类型的sf :: RenderWindow,你的参数类型为Pointer sf :: RenderWindow:
sf::RenderWindow fenetre(sf::RenderWindow*);
然后在定义函数时,返回void,参数为Reference sf :: RenderWindow:
void fenetre(sf::RenderWindow &window)
{
// Code
}
要修复此问题,请将fenetre声明的返回类型设为void,因为您实际上根本没有返回值,并使您的参数引用sf :: RenderWindow:
// Declaration, on top of your program
void fenetre(sf::RenderWindow&);
// Definition, at the bottom
void fenetre(sf::RenderWindow &window)
{
// Code
}