抛出了一个奇怪的异常:在SFML中获取精灵图像的高度
Strange exception thrown: getting the height of a sprites image in SFML
嘿,我已经找到了一个异常的根,它在这行代码中:
>= obj2->GetPosition().y + (obj2->GetCenter().y - obj2->GetImage()->GetHeight()))
但我不知道是什么导致的…奇怪的部分在函数中,它需要两个精灵指针,函数GetImage()->GetHeight()适用于对象1,但不适用于对象2…
请帮忙!我不知道为什么会发生这种事!感谢
所有代码:(搜索我之前发布的代码行)
#include<SFML/Graphics.hpp>
#include<SFML/System.hpp>
#include<cmath>
#include<vector>
# define M_PI 3.14159265358979323846
enum{xXx= 0, yYy= 1};
enum{ScreenWidth=800, ScreenHeight=600};
sf::RenderWindow Window;
template<typename T/*, typename T2*/>
bool CheckCollision(T* obj1, T* obj2){
bool xColl=false,yColl=false;
// check if any part of object 1 is between object 2's top and bottom
if(obj1->GetPosition().y + (obj1->GetCenter().y - obj1->GetImage()->GetHeight())
<= obj2->GetPosition().y + (obj2->GetImage()->GetHeight() - obj2->GetCenter().y)
&& obj1->GetPosition().y + (obj1->GetImage()->GetHeight() - obj1->GetCenter().y)
>= obj2->GetPosition().y + (obj2->GetCenter().y - obj2->GetImage()->GetHeight()))
yColl= true;
// check if any part of object 1 is between object 2's left and right
if(obj1->GetPosition().x + (obj1->GetCenter().x - obj1->GetImage()->GetWidth())
<= obj2->GetPosition().x + (obj2->GetImage()->GetWidth() - obj2->GetCenter().x)
&& obj1->GetPosition().x + (obj1->GetImage()->GetWidth() - obj1->GetCenter().x)
>= obj2->GetPosition().x + (obj2->GetCenter().x - obj2->GetImage()->GetWidth()))
xColl= true;
if(xColl==true && yColl==true) return true;
else return false;
};
template<typename T>
void CalculateMove(T Time, T Speed, T Angle, T& buffX, T& buffY)
{ //Make the degrees positive
if(Angle<0) Angle= 360+Angle;
//determine what quadrant of circle we're in
unsigned int Quadrant= 1;
if(Angle>=90) Quadrant= 2;
if(Angle>=180) Quadrant= 3;
if(Angle>=270) Quadrant= 4;
//anything above 90 would be impossible triangle
Angle= (float)(Angle-(int)Angle)+(float)((int)Angle%90);
// calculates x and y based on angle and Hypotenuse
if((int)Angle!=0){
if(Quadrant==2 || Quadrant==4) Angle=90-Angle; //The unit circle triangle is flipped otherwise, causing x and y to be switched
buffY= sin(Angle / 180 * M_PI)/ (1.f/(Speed*Time));
buffX= sin((180-Angle-90)/ 180 * M_PI)/ (1.f/(Speed*Time));}
else{// Movement is a straight line on X or Y axis
if(Quadrant==1 || Quadrant==3) buffX= Speed*Time;
if(Quadrant==2 || Quadrant==4) buffY= Speed*Time;}
//Quadrant Factor (positive or negative movement on the axis)
switch(Quadrant){
case 1: break;
case 2: buffX=-buffX; break;
case 3: buffX=-buffX; buffY=-buffY; break;
case 4: buffY=-buffY; break;}
};
template<typename T>
struct MoveData{
float X,Y;
T* Object;
MoveData(float x, float y, T* object):X(x), Y(y), Object(object){};
};
///////////////////////////////////////// Mysprite ////////////////////////////////
class mySprite : public sf::Sprite
{
private:
float velocity;
float angle;
public:
// all the values needed by the base class sprite();
mySprite(
const sf::Image& Img,
const sf::Vector2f& Position = sf::Vector2f(0, 0),
const sf::Vector2f& Scale = sf::Vector2f(1, 1),
float Rotation = 0.f,
const float Angle= 0.f,
const float Velocity= 0.f,
const sf::Color& Col = sf::Color(255, 255, 255, 255)):
Sprite(Img, Position, Scale, Rotation, Col){
angle= Angle;
velocity= Velocity;};
float Velocity(){return velocity;};
void SetVelocity(float newVelocity){velocity=newVelocity;};
float Angle(){return angle;};
void SetAngle(float newAngle){angle=(float)(newAngle-(int)newAngle)+(float)((int)newAngle%360);};
void Update(){
float frameTime= Window.GetFrameTime();
float X=0,Y=0;
//SetRotation(angle);
CalculateMove(frameTime,velocity,angle,X,Y);
Move(X,-Y);
};
void Accelerate(float PPS){velocity+=PPS;};
void Turn(float degrees){
angle=(float)((angle+degrees)-(int)(angle+degrees))+(float)((int)(angle+degrees)%360);};
void Reflect(float CollAngle){
SetRotation(-GetRotation());
angle=360-angle;
//TODO: factor in the collision angle
};
};
class MoveBuff
{
private:
std::vector<MoveData<mySprite>> buff;
public:
void AddMove(mySprite obj, float X, float Y){
MoveData<mySprite> data(X, Y, &obj);
buff.push_back(data);};
void TestColl(){
for(unsigned int Object= 0; Object< buff.size(); Object++){
for(unsigned int CollCheck=Object+1; CollCheck<=buff.size()-1; CollCheck++){
if(CheckCollision(buff[Object].Object, buff[CollCheck].Object)){
buff[Object].Object->Reflect(0);
buff[Object].Object->Reflect(0);}}}
};
void Move(){
for(unsigned int I= 0; I<buff.size(); I++)
buff[I].Object->Move(buff[I].X, buff[I].Y);
};
};
int main()
{
Window.Create(sf::VideoMode(ScreenWidth, ScreenHeight), "Pong! by Griffin Howlett", sf::Style::Resize | sf::Style::Close);
sf::Image img;
img.Create(30,50,sf::Color(255,0,0));
mySprite box(img, sf::Vector2f(400,0), sf::Vector2f(1,1), 0, 270, 100); //TODO: test collision with different speeds
box.SetCenter(15,25);
sf::Image img2;
img2.Create(56.5,80,sf::Color(255,0,0));
mySprite box2(img2, sf::Vector2f(400,400), sf::Vector2f(1,1), 0, 90, 100);
box2.SetCenter(25,40);
Window.Display();
for(;;){
Window.Clear();
MoveBuff MoveBuff;
float frameTime= Window.GetFrameTime();
float X=0,Y=0;
CalculateMove(frameTime,box.Velocity(),box.Angle(),X,Y);
MoveBuff.AddMove(box,X,Y);
CalculateMove(frameTime,box2.Velocity(),box2.Angle(),X,Y);
MoveBuff.AddMove(box2,X,Y);
MoveBuff.TestColl();
MoveBuff.Move();
Window.Draw(box);
Window.Draw(box2);
Window.Display();
}
}
MoveData(float x, float y, T* object):X(x), Y(y), Object(object){}; // Constructor
// ^^^^^^^^^^^^
// Just making a reference to the argument. Shallow copy.
void AddMove(mySprite obj, float X, float Y){
MoveData<mySprite> data(X, Y, &obj);
buff.push_back(data);
}// => lifetime of the obj ends at this point.
构造函数正在进行浅层复制。在初始值设定项列表中,您只是在引用参数。但是obj
的寿命在呼叫MoveBuff::AddMove
之后结束。因此,MoveData::Object
处于悬空状态。而取消引用是导致您异常的原因。相反,做一个深度复制。
void TestColl()
{
for(unsigned int Object= 0; Object< buff.size(); Object++){
for(unsigned int CollCheck=Object+1; CollCheck<=buff.size()-1; CollCheck++){
if(CheckCollision(buff[Object].Object, buff[CollCheck].Object)){
// ^^^^^^^^^ This the problem. buff[Object].Object is dangling.
相关文章:
- 处理多个异常集合的C++方法
- 我在c++代码中生成了一个运行时#3异常
- 孤立代码块在结构中引发异常
- C++中的赋值发生,尽管右侧出现异常
- 从构造函数抛出异常时如何克服内存泄漏
- 异常属于C++中的线程还是进程
- 当类定义不可见时捕获异常
- SFML纹理像播放器
- 如何使用AngelScript注册SFML Vector2运算符
- 如何修复sfml c++代码编译错误
- 引发异常:读取访问冲突**dynamicArray**为0x1118235.发生
- SFML library: http request
- 为什么异常不退出程序?
- 落砂模拟碰撞检测C++和SFML
- 为什么我应该在异常处理中使用std::cerr而不是std::cout
- 如何修复链表类实现的未处理异常0xDDDDDDDD
- 在使用SFML时,我会得到记忆或出于界限异常,这是什么问题
- SFML(32位VS12)-SFML.exe中0x701ADEF8(msvcr110.dll)处未处理的异常:0xC00
- SFML 2.1 "setFont"未处理的异常
- 抛出了一个奇怪的异常:在SFML中获取精灵图像的高度