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

Author Topic: [SOLVED]Code Exits with -1, but Compiles Fine  (Read 1926 times)

0 Members and 1 Guest are viewing this topic.

DatLinuxGuy

  • Newbie
  • *
  • Posts: 6
    • View Profile
    • Email
[SOLVED]Code Exits with -1, but Compiles Fine
« on: February 21, 2014, 01:40:38 am »
I have decided that I would try to adapt a code snippet found on the SFML website to be stretched across multiple files but to no avail. Here is the snippet I was trying to adapt.
#include <SFML/Audio.hpp>
#include <SFML/Graphics.hpp>
int main()
{
    // Create the main window
    sf::RenderWindow window(sf::VideoMode(800, 600), "SFML window");
    // Load a sprite to display
    sf::Texture texture;
    if (!texture.loadFromFile("cute_image.jpg"))
        return EXIT_FAILURE;
    sf::Sprite sprite(texture);
    // Create a graphical text to display
    sf::Font font;
    if (!font.loadFromFile("arial.ttf"))
        return EXIT_FAILURE;
    sf::Text text("Hello SFML", font, 50);
    // Load a music to play
    sf::Music music;
    if (!music.openFromFile("nice_music.ogg"))
        return EXIT_FAILURE;
    // Play the music
    music.play();
    // Start the game loop
    while (window.isOpen())
    {
        // Process events
        sf::Event event;
        while (window.pollEvent(event))
        {
            // Close window : exit
            if (event.type == sf::Event::Closed)
                window.close();
        }
        // Clear screen
        window.clear();
        // Draw the sprite
        window.draw(sprite);
        // Draw the string
        window.draw(text);
        // Update the window
        window.display();
    }
    return EXIT_SUCCESS;
}

I was able to begin splitting up the code, and it complied fine, but when I run it, it returns -1. I started SFML yesterday, and I still haven't found a tutorial that shows how to use multiple files. Here is my code that compiles, but returns -1. I am attaching all my code. Please reply ASAP.
PS. If some one could show me how to adapt the snippet above so I could split it into multiple files and make it so it is object oriented, that would be much preferred. Please explain it though.

Thanks,
Brooks Rady
« Last Edit: February 21, 2014, 03:17:48 am by DatLinuxGuy »

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: Code Exits with -1, but Compiles Fine
« Reply #1 on: February 21, 2014, 02:02:52 am »
Quote
If some one could show me how to adapt the snippet above so I could split it into multiple files and make it so it is object oriented, that would be much preferred. Please explain it though.

Ummm, well there isn't much you can do to break that sample code into objects. Maybe you should look into getting a good C++ book and learning object oriented coding first. Trying to learn C++ and SFML at the same time is not recommending and is just going to give you a lot more issues.

And to be clear, "multiple files" has nothing to do with code being "object oriented".
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

DatLinuxGuy

  • Newbie
  • *
  • Posts: 6
    • View Profile
    • Email
Re: Code Exits with -1, but Compiles Fine
« Reply #2 on: February 21, 2014, 02:09:11 am »
I suppose I was looking for a way to split it into functions.  :-\ Such as an OnInit() function that would create the window. Things like that, I was trying to do so via multiple files AND classes. So I could use this as an outline for my real game later on. What broke it, or so I think, was when I was trying pass the window object to another file. I used an access function to do so and it compiled, but didn't work. If you could, It would be nice if you could look at my code if the other option doesn't work.

Thanks,
Brooks Rady

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: Code Exits with -1, but Compiles Fine
« Reply #3 on: February 21, 2014, 02:20:25 am »
The thing is, once you actually learn C++ (yes working with the console for several weeks just learning C++ without SFML can seem boring) you will understand how you should be breaking up your code into objects.

As for why the above code returns -1? Because I will say with 99% chance you actually don't have the three resources in your working directory.
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

DatLinuxGuy

  • Newbie
  • *
  • Posts: 6
    • View Profile
    • Email
Re: Code Exits with -1, but Compiles Fine
« Reply #4 on: February 21, 2014, 02:27:07 am »
Okay, I'll do more work on C++ alone, but, the above code is not mine. Mine is in the form of an attachment, no one has downloaded it yet, and it would be nice to know what is wrong with that first.

Thanks,
Brooks Rady

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: Code Exits with -1, but Compiles Fine
« Reply #5 on: February 21, 2014, 02:34:04 am »
Sorry, didn't see your attachment. Anyways you several things wrong with that code. You are trying use manual memory management and really doing it wrong. I suggest you read up on RAII also.

DuDi *DD = NULL;
    // Create the main window
    if (DD->OnInit() == false){
        return -1;
    }
////
sf::RenderWindow *MWindow;
MWindow->create(sf::VideoMode(1000, 500), "Dungeon Dimensions");

Calling functions on a null pointer doesn't work too well.

sf::RenderWindow *MWindow; // Not this
sf::RenderWindow MWindow; // This

DuDi *DD = NULL; // Not this at all
DuDi DD; // this

sf::RenderWindow *GetWindow();

Don't return pointers unless you must, return references or const references.
« Last Edit: February 21, 2014, 02:46:27 am by zsbzsb »
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

DatLinuxGuy

  • Newbie
  • *
  • Posts: 6
    • View Profile
    • Email
Re: Code Exits with -1, but Compiles Fine
« Reply #6 on: February 21, 2014, 03:17:19 am »
Thank you for all the help. :) I agree that I should probably read up on manual memory management as well as OOP in C++. I would not have ever suspected pointers to be my problem. I'll read up on C++.

Thanks,
Brooks Rady

 

anything