3
« on: June 14, 2010, 06:27:26 am »
When I compile and run the below program and move my mouse around the screen a few times (it also helps a bit if you click around), the program closes. The weird thing is that I get a 0x0 "EXIT_SUCCESS" message when the program exits. It happens more often if you put more commands and event checks and stuff in. (The comments are for my own benefit; I'm not trying to make everybody look like a noob just because I am one)
#include <iostream>
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
int main()
{
//Creating variables that will define the size of the game window
int Xres = 1440, Yres = 900;
// Creates a RenderWindow type window
sf::RenderWindow App(sf::VideoMode(Xres, Yres, 32), "Awesomely Cool Window", sf::Style::Fullscreen);
App.UseVerticalSync(true);
//Creates a font
sf::Font MyFont;
//Creates a buffer and loads the sound file
sf::SoundBuffer Buffer;
if (!Buffer.LoadFromFile("files/Menu.ogg"))
return EXIT_FAILURE;
//Loads the font file
if (!MyFont.LoadFromFile("files/Achafexp.ttf", 200))
return EXIT_FAILURE;
//Creates a sound
sf::Sound Sound;
//Creates some text and and all the settings
sf::String Text("Hello", MyFont, 200);
Text.SetColor(sf::Color(0, 0, 0));
Text.SetRotation(0.f);
Text.SetScale(0.f, 0.f);
Text.Move(100.f, 200.f);
//Creates an image and loads the image file
sf::Image Image;
if (!Image.LoadFromFile("files/smiley.png"))
return EXIT_FAILURE;
// Creates a couple sprites using the image created above
sf::Sprite CoolSprite(Image);
sf::Sprite CoolSprite2(Image);
//All the settings for the sprites
CoolSprite.SetColor(sf::Color(255, 255, 255, 255));
CoolSprite.SetPosition(132.f, 42.f);
CoolSprite.SetCenter(32, 32);
CoolSprite2.SetColor(sf::Color(255, 0, 0, 255));
CoolSprite2.SetPosition(42.f, 42.f);
CoolSprite2.SetCenter(32, 32);
//Assignes the buffer made above to the sound made above, effectively assigning the sound file to the sound
Sound.SetBuffer(Buffer);
//Loops the sound
Sound.SetLoop(true);
//Plays the sound
Sound.Play();
// Starts the main loop thing
while (App.IsOpened())
{
//Makes the window do stuff when the user does stuff
sf::Event Event;
while (App.GetEvent(Event))
{
if ((Event.Type == sf::Event::Closed) || (Event.Key.Code == sf::Key::Escape) || (Event.Key.Code == sf::Key::F4 && Event.Key.Alt))
App.Close();
if (Event.Key.Code == sf::Key::F1)
{
sf::Image Screen = App.Capture();
Screen.SaveToFile("screenshot.jpg");
}
if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::P))
{
if (Sound.GetStatus() == sf::Sound::Playing)
Sound.Pause();
else
Sound.Play();
}
if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::O))
Sound.Stop();
}
//Gets the FPS
float ElapsedTime = App.GetFrameTime();
//Makes the sprites move and spin around when certain keys are pressed
if (App.GetInput().IsKeyDown(sf::Key::Left)) CoolSprite.Move(-500 * ElapsedTime ,0);
if (App.GetInput().IsKeyDown(sf::Key::Right)) CoolSprite.Move( 500 * ElapsedTime ,0);
if (App.GetInput().IsKeyDown(sf::Key::Up)) CoolSprite.Move(0, -500 * ElapsedTime);
if (App.GetInput().IsKeyDown(sf::Key::Down)) CoolSprite.Move(0, 500 * ElapsedTime);
if (App.GetInput().IsKeyDown(sf::Key::Add)) CoolSprite.Rotate(-200 * ElapsedTime);
if (App.GetInput().IsKeyDown(sf::Key::Subtract)) CoolSprite.Rotate(+200 * ElapsedTime);
if (App.GetInput().IsKeyDown(sf::Key::A)) CoolSprite2.Move(-500 * ElapsedTime ,0);
if (App.GetInput().IsKeyDown(sf::Key::D)) CoolSprite2.Move( 500 * ElapsedTime ,0);
if (App.GetInput().IsKeyDown(sf::Key::W)) CoolSprite2.Move(0, -500 * ElapsedTime);
if (App.GetInput().IsKeyDown(sf::Key::S)) CoolSprite2.Move(0, 500 * ElapsedTime);
if (App.GetInput().IsKeyDown(sf::Key::E)) CoolSprite2.Rotate(-200 * ElapsedTime);
if (App.GetInput().IsKeyDown(sf::Key::Q)) CoolSprite2.Rotate(+200 * ElapsedTime);
//Makes the sprites go back to their exact original positions when the spacebar is pressed
if (App.GetInput().IsKeyDown(sf::Key::Space))
{
CoolSprite2.SetPosition(42.f, 42.f);
CoolSprite.SetPosition(132.f, 42.f);
CoolSprite.SetRotation(0.f);
CoolSprite2.SetRotation(0.f);
}
//Finds the center of the screen from the resolution the user entered at the beginning of the program
int xcenter, ycenter;
xcenter = Xres / 2;
ycenter = Yres / 2;
//Set Rectangle lengths in a X1, Y1, X2, Y2, Color, [OutlineSize], [OutlineColor] format.
sf::Shape Rect = sf::Shape::Rectangle(200, 200, 400, 400, sf::Color::Green, 20, sf::Color::Red);
Rect.SetColor(sf::Color(255, 255, 255, 200));
Rect.SetCenter(300, 300);
Rect.SetPosition(xcenter, ycenter);
// Clear the screen with a color (R, G, B) format
App.Clear(sf::Color(255, 255, 225));
// Draws the sprites, rectangle, and text on the screen
App.Draw(CoolSprite);
App.Draw(CoolSprite2);
App.Draw(Rect);
App.Draw(Text);
// Opens the window
App.Display();
}
return EXIT_SUCCESS;
}