1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
|
#include <SFML/Graphics.hpp>
#include "Collision.h"
int main()
{
int width = 800;
int heigth = 600;
int depth = 32;
sf::Image Image;
if (!Image.LoadFromFile("YellowStripes.png"))
{
// Error...
}
sf::Sprite Sprite;
Sprite.SetSubRect(sf::IntRect(10, 10, 20, 20));
Sprite.SetPosition(30.f, 30.f);
Sprite.SetCenter(30, 30);
sf::Sprite Sprite2;
Sprite2.SetSubRect(sf::IntRect(10, 10, 20, 20));
Sprite2.SetPosition(400.f, 400.f);
Sprite2.SetCenter(30, 30);
// Create the main rendering window
sf::RenderWindow App(sf::VideoMode(800, 600, 32), "SFML Graphics");
// Create the main window
//sf::Window App(sf::VideoMode(width, heigth, depth), "SFML Window");
//recreate window in fullscreen
//App.Create(sf::VideoMode::GetMode(0), "SFML Window", sf::Style::Fullscreen);
// Disable vertical synchronization to get maximum framerate
App.UseVerticalSync(false);
// Create a clock for measuring time
sf::Clock Clock;
// Some dummy variables to simulate an object that moves
const float Speed = 50.f;
float Left = 0.f;
float Top = 0.f;
// Get a reference to the input manager associated to our window, and store it for later use
const sf::Input& Input = App.GetInput();
bool bang = false;
int mouseX = 0;
int mouseY = 0;
// Start main loop
while (App.IsOpened())
{
sf::Event Event;
// Process event
while (App.GetEvent(Event))
{
// Close window : exit
// Window closed
if (Event.Type == sf::Event::Closed)
App.Close(); //to actually close window...or just destroy the sf::Window
// Escape key : exit
// Escape key pressed
if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Escape))
App.Close(); //to actually close window...or just destroy the sf::Window
if (App.GetInput().IsMouseButtonDown(sf::Mouse::Left))
{
//mouseX = App.GetInput().GetMouseX();
//mouseY = App.GetInput().GetMouseY();
//App.Draw(line);
}
}
// Get elapsed time since last frame (we could as well use App.GetFrameTime())
float ElapsedTime = Clock.GetElapsedTime();
Clock.Reset();
Sprite.SetImage(Image);
Sprite2.SetImage(Image);
// Get the sprite's dimensions
float Width = Sprite.GetSize().x;
float Height = Sprite.GetSize().y;
// Move the sprite
if (App.GetInput().IsKeyDown(sf::Key::Left)) Sprite.Move(-200 * ElapsedTime, 0);
if (App.GetInput().IsKeyDown(sf::Key::Right)) Sprite.Move( 200 * ElapsedTime, 0);
if (App.GetInput().IsKeyDown(sf::Key::Up)) Sprite.Move(0, -200 * ElapsedTime);
if (App.GetInput().IsKeyDown(sf::Key::Down)) Sprite.Move(0, 200 * ElapsedTime);
// Rotate the sprite
if (App.GetInput().IsKeyDown(sf::Key::S)) Sprite.Rotate(-200 * ElapsedTime);
if (App.GetInput().IsKeyDown(sf::Key::A)) Sprite.Rotate( 200 * ElapsedTime);
// Clear the screen (fill it with black color)
//App.Clear();
App.Clear(sf::Color(255, 255, 0));
//Incomplete Test - Minimally Functional
if(Collision::BoundingBoxTest(Sprite, Sprite2))
{
if(Collision::PixelPerfectTest(Sprite, Sprite2))
{
//Contact
bang = true;
//Sprite.SetPosition(30.f, 30.f);
}
if(!Collision::PixelPerfectTest(Sprite, Sprite2))
{
//Contact
bang = false;
//Sprite.SetPosition(30.f, 30.f);
}
}
//Create or Destroy Sprite2
if (App.GetInput().IsKeyDown(sf::Key::B))
{
if(bang)
bang = false;
if(!bang)
bang = true;
}
if(App.GetInput().IsMouseButtonDown(sf::Mouse::Left))
{
float xcoord = Sprite.GetCenter().x;
float ycoord = Sprite.GetCenter().y;
float xcoord2 = Sprite2.GetCenter().x;
float ycoord2 = Sprite2.GetCenter().y;
App.Draw(sf::Shape::Line(xcoord, ycoord, 600, 400, 30.0, sf::Color::Blue));
}
if(bang == false)
{
App.Draw(Sprite2);
}
App.Draw(Sprite);
// Display window on screen
App.Display();
}
return EXIT_SUCCESS;
}
|