Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Problem using sf::Rect  (Read 9669 times)

0 Members and 1 Guest are viewing this topic.

MastrRanger

  • Newbie
  • *
  • Posts: 6
    • View Profile
Problem using sf::Rect
« on: January 08, 2013, 12:38:15 am »
Hey, I'm attempting to use the sf::Rect class to check for intersections between two sprites. The problem appears when I actually call the intersects method for the class in that I get this error:

c:\sfml\include\sfml\graphics\rect.inl(104): error C2589: '(' : illegal token on right side of '::'
1>          c:\sfml\include\sfml\graphics\rect.inl(102) : while compiling class template member function 'bool sf::Rect<T>::intersects(const sf::Rect<T> &,sf::Rect<T> &) const'
1>          with
1>          [
1>              T=float
1>          ]
1>          c:\sfml\include\sfml\graphics\rect.inl(95) : see reference to function template instantiation 'bool sf::Rect<T>::intersects(const sf::Rect<T> &,sf::Rect<T> &) const' being compiled
1>          with
1>          [
1>              T=float
1>          ]
1>          c:\sfml\include\sfml\graphics\rect.inl(28) : while compiling class template member function 'sf::Rect<T>::Rect(void)'
1>          with
1>          [
1>              T=float
1>          ]
1>          c:\users\username\documents\visual studio 2012\projects\brickbreak\brickbreak\ball.cpp(6) : see reference to function template instantiation 'sf::Rect<T>::Rect(void)' being compiled
1>          with
1>          [
1>              T=float
1>          ]
1>c:\sfml\include\sfml\graphics\rect.inl(104): error C2059: syntax error : '::'
1>c:\sfml\include\sfml\graphics\rect.inl(105): error C2589: '(' : illegal token on right side of '::'
1>c:\sfml\include\sfml\graphics\rect.inl(105): error C2059: syntax error : '::'
1>c:\sfml\include\sfml\graphics\rect.inl(106): error C2589: '(' : illegal token on right side of '::'
1>c:\sfml\include\sfml\graphics\rect.inl(106): error C2059: syntax error : '::'
1>c:\sfml\include\sfml\graphics\rect.inl(107): error C2589: '(' : illegal token on right side of '::'
1>c:\sfml\include\sfml\graphics\rect.inl(107): error C2059: syntax error : '::'

Here's the code to create the rectangles:
bRect = sprite.getGlobalBounds();
pRect = sprite.getGlobalBounds();

And here's the actual call to the intersects method:
if(bRect.intersects(player.pRect) == true)
        {
                std::cout << "Hello";
        }

Anybody able to help me out on this one?

Qix

  • Full Member
  • ***
  • Posts: 139
  • I am Qix!
    • View Profile
    • Natoga Technologies
Re: Problem using sf::Rect
« Reply #1 on: January 08, 2013, 12:40:51 am »
What code is on the line c:\users\username\documents\visual studio 2012\projects\brickbreak\brickbreak\ball.cpp : 6? Copy and paste exactly.

Also, without stealing Laurent's inevitable thunder, I will say that a complete code sample would be nice. We can't help you much with the "examples" you've given us.
~ Qix
Creator of Rippl Studio
Code: [Select]
<danharibo> iostream: I don't do enough drugs to think that's a good idea.

MastrRanger

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Problem using sf::Rect
« Reply #2 on: January 08, 2013, 12:48:04 am »
Guess I'll just paste the whole file then:

#include "stdafx.h"
#include "Ball.h"
//#include "BrickBreak.h"

Ball::Ball(sf::VideoMode *video)
{
        Player * player = new Player(video);

        position = new sf::Vector2f();
       
        if(!texture.loadFromFile("ball.png"))
                std::cout << "ERROR";

        sprite.setTexture(texture);

        sprite_size = texture.getSize();

        screen_size.x = video->width;
        screen_size.y = video->height;

        position->x = player->position->x + player->sprite_size.x / 2;
        position->y = player->position->y - sprite_size.y;

        sprite.setPosition(unsigned int(position->x),unsigned int(position->y));

        is_moving = false;

        moveX = new float(1.0f);
        moveY = new float(-1.0f);

        speed = new float(1.0f);

}


Ball::~Ball(void)
{
}

void Ball::Update(sf::VideoMode &video,Player &player)
{
        if(is_moving == true)
        {
                Collision(moveX,moveY,player);
               
        }
        else
        {
                sprite.setPosition(player.position->x + sprite_size.x / 2, player.position->y - sprite_size.y);
        }

        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Space))
        {
                is_moving = true;
        }
        bRect = sprite.getGlobalBounds();
}

void Ball::Draw(sf::RenderWindow *window)
{
        window->draw(sprite);
}

void Ball::Collision(float * moveX, float * moveY,Player &player)
{
        float pos_move = 1.0f;
        float neg_move = -1.0f;
        position->x = sprite.getPosition().x;
        position->y = sprite.getPosition().y;
        sprite.setPosition(position->x + (*moveX * *speed),position->y + (*moveY * *speed));
        if(position->x <= 0)
        {
                *moveX = pos_move;
                //*speed += 0.05f;
        }
        if(position->x >= screen_size.x + 32)
        {
                *moveX = neg_move;
                //*speed += 0.05f;
        }
        if(position->y <= 0)
        {
                *moveY = pos_move;
                //*speed += 0.05f;
        }
        if(position->y >= screen_size.y - 8)
        {
                *moveY = neg_move;
                //*speed += 0.05f;
        }
        if(bRect.intersects(player.pRect) == true)
        {
                std::cout << "Hello";
        }
}

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Problem using sf::Rect
« Reply #3 on: January 08, 2013, 01:00:55 am »
Erorr may or may not be in Ball.h or in front of the computer, writing bad C++ code.
Quote
Also, without stealing Laurent's inevitable thunder
That's nothing, when Nexus sees the c-tor and that:
Ball::~Ball(void)
{
}
apocalypse will happen.
Back to C++ gamedev with SFML in May 2023

MastrRanger

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Problem using sf::Rect
« Reply #4 on: January 08, 2013, 01:06:19 am »
It is probably me as I'm still trying to get the hang of things and I have no doubt I'm writing awful code. I'll post the header file as well in the mean time.

#ifndef BALL_H
#define BALL_H

#pragma once

#include "Player.h"
class Ball
{
        sf::Vector2f * position;
        sf::Texture texture;
        sf::Sprite sprite;
        sf::Vector2u sprite_size;
        sf::Vector2f screen_size;
        bool is_moving;
        float * moveX;
        float * moveY;
        float * speed;

public:
        Ball(sf::VideoMode *video);
        ~Ball(void);
        void Update(sf::VideoMode&,Player&);
        void Draw(sf::RenderWindow *window);
        void Collision(float *, float *,Player&);

        sf::FloatRect bRect;
};

#endif

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Problem using sf::Rect
« Reply #5 on: January 08, 2013, 01:27:19 am »
I'm sure Nexus will swing by later to tell you why manual memory managment is bad.
Does just that work?
int main()
{
sf::FloatRect rect,rect2;
rect.intersects(rect2);
}
Back to C++ gamedev with SFML in May 2023

MastrRanger

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Problem using sf::Rect
« Reply #6 on: January 08, 2013, 01:38:53 am »
It does not seem to work, no.
Everything works fine just up until the intersect method is called.
« Last Edit: January 08, 2013, 01:42:27 am by MastrRanger »

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Problem using sf::Rect
« Reply #7 on: January 08, 2013, 01:50:17 am »
Seems like you're missing std::min and max from <algorithm>, try them out.
Back to C++ gamedev with SFML in May 2023

MastrRanger

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Problem using sf::Rect
« Reply #8 on: January 08, 2013, 02:00:26 am »
Nope, min and max seem to work outside of that method.

binary1248

  • SFML Team
  • Hero Member
  • *****
  • Posts: 1405
  • I am awesome.
    • View Profile
    • The server that really shouldn't be running
Re: Problem using sf::Rect
« Reply #9 on: January 08, 2013, 06:33:58 am »
In both header and cpp you don't include any SFML headers before any win32 related headers. The problem is that the win32 headers for some reason define their own version of min and max that are not templated and interfere with the implementation from the STL. Microsoft invented a really cool solution which consists of defining NOMINMAX everytime you intend to use the STL versions of min and max (so std::min and std::max) although they are superior in every way. This definition has to be done before the first win32 header is included into a translation unit. Your compiler isn't finding them because the win32 headers either didn't define them or even worse undefined them before defining its own.

I have no idea what lurks inside your stdafx.h or how you use it, but using precompiled headers with SFML isn't worth it if you ask me. Simply make sure that you include an SFML header right at the start of stdafx.h or just pull all headers out of stdafx.h and into their respective header files, add an SFML header there and it should work. All SFML headers will automatically include SFML/Config.hpp which coincidently happens to define NOMINMAX (who would have thought? ;)).
« Last Edit: January 08, 2013, 06:35:30 am by binary1248 »
SFGUI # SFNUL # GLS # Wyrm <- Why do I waste my time on such a useless project? Because I am awesome (first meaning).

MastrRanger

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Problem using sf::Rect
« Reply #10 on: January 08, 2013, 04:05:26 pm »
Thanks for the help, binary. Everything seems to be working now that I've moved the sfml headers to the top of the stdafx.h.

 

anything