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

Author Topic: App not running in Xcode  (Read 2716 times)

0 Members and 1 Guest are viewing this topic.

jaredcaruso

  • Newbie
  • *
  • Posts: 12
    • View Profile
App not running in Xcode
« on: April 24, 2016, 09:06:09 pm »
Whenever I try to build my project in Xcode, the app appears on the dock but it just bounces forever. I don't have any errors or anything. Also, the template worked and I could run that just fine.

Here is my code if it matters:
//
// Disclamer:
// ----------
//
// This code will work only if you selected window, graphics and audio.
//
// Note that the "Run Script" build phase will copy the required frameworks
// or dylibs to your application bundle so you can execute it on any OS X
// computer.
//
// Your resource files (images, sounds, fonts, ...) are also copied to your
// application bundle. To get the path to these resource, use the helper
// method resourcePath() from ResourcePath.hpp
//

#include <SFML/Audio.hpp>
#include <SFML/Graphics.hpp>

// Here is a small helper for you ! Have a look.
#include "ResourcePath.hpp"

void initShape(sf::RectangleShape& shape, sf::Vector2f const& pos, sf::Color const& color)
{
    shape.setFillColor(color);
    shape.setPosition(pos);
    shape.setOrigin(shape.getSize() * 0.5f);
}

int main()
{
   
    sf::RenderWindow window(sf::VideoMode(480, 380), "Bad Squares");
    window.setFramerateLimit(60); //set the frame limit to 60 FPS
   
    sf::Image icon;
    if (!icon.loadFromFile(resourcePath() + "icon.png")) {
        return EXIT_FAILURE;
    }
    window.setIcon(icon.getSize().x, icon.getSize().y, icon.getPixelsPtr());
   
    sf::Vector2f startPosition = sf::Vector2f(50, 50);
    sf::RectangleShape playerSquare(sf::Vector2f(25, 25));
    initShape(playerSquare, startPosition, sf::Color::Yellow);
    sf::RectangleShape targetSquare(sf::Vector2f(50, 50));
    initShape(targetSquare, sf::Vector2f(400, 50), sf::Color::Green);
    sf::RectangleShape badSquareOne(sf::Vector2f(50, 100));
    initShape(badSquareOne, sf::Vector2f(250, 50), sf::Color::Red);
    sf::RectangleShape badSquareTwo(sf::Vector2f(50, 200));
    initShape(badSquareTwo, sf::Vector2f(250, 250), sf::Color::Red);
    sf::RectangleShape changeToRedZoneOne(sf::Vector2f(425, 75));
    initShape(changeToRedZoneOne, sf::Vector2f(400, 50), sf::Color::Yellow);
    window.clear(sf::Color::Black);
   
    while (window.isOpen())
    {
        window.draw(targetSquare);
        window.draw(badSquareOne);
        window.draw(badSquareTwo);
        window.draw(playerSquare);
       
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Down))
        {
            window.clear(sf::Color::Black);
            playerSquare.move(0, 2);
        }
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Up))
        {
            window.clear(sf::Color::Black);
            playerSquare.move(0, -2);
        }
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Left))
        {
            window.clear(sf::Color::Black);
            playerSquare.move(-2, 0);
        }
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Right))
        {
            window.clear(sf::Color::Black);
            playerSquare.move(2, 0);
        }
       
        if (playerSquare.getGlobalBounds().intersects(targetSquare.getGlobalBounds()))
        {
            window.clear(sf::Color::Red);
        }
        if (playerSquare.getGlobalBounds().intersects(badSquareOne.getGlobalBounds()))
        {
            window.clear(sf::Color::Red);
        }
        if (playerSquare.getGlobalBounds().intersects(changeToRedZoneOne.getGlobalBounds()))
        {
            targetSquare.setFillColor(sf::Color::Red);
        }
       
        badSquareTwo.rotate(2);
        window.clear(sf::Color::Black);
        window.display();
    }
   
    return EXIT_SUCCESS;
}
 

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: App not running in Xcode
« Reply #1 on: April 24, 2016, 09:16:30 pm »
Why do you clear everything before you display?
The logical order is; clear, draw, display.

jaredcaruso

  • Newbie
  • *
  • Posts: 12
    • View Profile
Re: App not running in Xcode
« Reply #2 on: April 24, 2016, 09:22:42 pm »
I tried removing the window.clear in all the if statements but it still doesn't work :(

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
Re: App not running in Xcode
« Reply #3 on: April 24, 2016, 09:34:58 pm »
Event handling (not probing with `sf::Keyboard` and related) is mandatory. If you are still facing issues, start from the snippet available when you create your project, test it and modify it until it doesn't work anymore.
SFML / OS X developer

jaredcaruso

  • Newbie
  • *
  • Posts: 12
    • View Profile
Re: App not running in Xcode
« Reply #4 on: April 24, 2016, 09:50:47 pm »
The template runs fine until I add this:
       
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Down))
        {
            playerSquare.move(0, 2);
        }
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Up))
        {
            playerSquare.move(0, -2);
        }
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Left))
        {
            playerSquare.move(-2, 0);
        }
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Right))
        {
            playerSquare.move(2, 0);
        }
 

However, it runs just fine when I use Visual Studio/Visual C++. Is that just because of the compiler or something? And how would I fix the event handling?

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
Re: App not running in Xcode
« Reply #5 on: April 25, 2016, 09:30:31 am »
Give us a SSCCE please.
SFML / OS X developer