#include <SFML/System.hpp>
#include <SFML/Graphics.hpp>
#include <iostream>
int main()
{
///////////////////////////
// I N I T
///////////////////////////
//Variabler
//Spelaren
int aktivtRum = 3;
//Världen
int antalRum = 4;
int antalLankarPerRum = 2;
std::string rumFil[antalRum][2]; //[0]:rum [1]:mask
int lank[antalRum][antalLankarPerRum][4]; //[0]:mål-rum [1]:rött [2]:grönt [3]:blått
//Katalog för grafikfiler
std::string graphLocation = "graphics/";
//rum0
rumFil[0][0] = "pos0Hangar.jpg";
rumFil[0][1] = "pos0HangarMask.jpg";
lank[0][0][0] = 1; lank[0][0][1] = 254; lank[0][0][2] = 0; lank[0][0][3] = 2;
lank[0][1][0] = 3; lank[0][1][1] = 1; lank[0][1][2] = 0; lank[0][1][3] = 254;
//rum1
rumFil[1][0] = "pos1Hangar.jpg";
rumFil[1][1] = "pos1HangarMask.jpg";
lank[1][1][0] = 0; lank[1][0][1] = 254; lank[1][0][2] = 0; lank[1][0][3] = 2;
lank[1][0][0] = 2; lank[1][1][1] = 1; lank[1][1][2] = 0; lank[1][1][3] = 254;
//rum2
rumFil[2][0] = "pos2Hangar.jpg";
rumFil[2][1] = "pos2HangarMask.jpg";
lank[2][0][0] = 1; lank[2][0][1] = 254; lank[2][0][2] = 0; lank[2][0][3] = 2;
lank[2][1][0] = 3; lank[2][1][1] = 1; lank[2][1][2] = 0; lank[2][1][3] = 254;
//rum3
rumFil[3][0] = "pos3Hangar.jpg";
rumFil[3][1] = "pos3HangarMask.jpg";
lank[3][0][0] = 0; lank[3][0][1] = 254; lank[3][0][2] = 0; lank[3][0][3] = 2;
lank[3][1][0] = 2; lank[3][1][1] = 1; lank[3][1][2] = 0; lank[3][1][3] = 254;
//Rotationskänslighet
int rotationTrigger = 50;
float rotationSpeed = 500;
//Skapa Fönstret
sf::RenderWindow App(sf::VideoMode(600, 400, 32), "Click!");
//Skapa bild som ska skrivas ut
sf::Image rum;
if (!rum.LoadFromFile(graphLocation + rumFil[aktivtRum][0]))
{
// Error...
}
//Skapa maskbild som ska ligga osynlig över bilden
sf::Image mask;
if (!mask.LoadFromFile(graphLocation + rumFil[aktivtRum][1]))
{
// Error...
}
//Skapa bild till pekaren
sf::Image pointerImage;
if (!pointerImage.LoadFromFile("graphics/pointer.png"))
{
// Error...
}
//Skapa bildens sprite nr 1
sf::Sprite Sprite;
Sprite.SetImage(rum);
Sprite.SetColor(sf::Color(255, 255, 255));
Sprite.SetPosition(0.f, 0.f);
http://Sprite.SetScaleX(1.625); //Skapa bildens sprite nr 2
sf::Sprite Sprite2;
Sprite2.SetImage(rum);
Sprite2.SetColor(sf::Color(255, 255, 255));
Sprite2.SetPosition(Sprite.GetSize().x, 0.f);
http://Sprite2.SetScaleX(1.625); //Skapa maskens Sprite nr 1
sf::Sprite MaskSprite;
MaskSprite.SetImage(mask);
MaskSprite.SetColor(sf::Color(255, 255, 255));
MaskSprite.SetPosition(0.f, 0.f);
//Skapa maskens Sprite nr 2
sf::Sprite MaskSprite2;
MaskSprite2.SetImage(mask);
MaskSprite2.SetColor(sf::Color(255, 255, 255));
MaskSprite2.SetPosition(MaskSprite.GetSize().x, 0.f);
//Pekarens Sprite
sf::Sprite Pointer;
Pointer.SetImage(pointerImage);
Pointer.SetColor(sf::Color(255, 255, 255));
Pointer.SetPosition(10,10);
Pointer.SetSubRect(sf::IntRect(0,0,30,30));
//Ta bort den ursprungliga muspekaren
App.ShowMouseCursor(0);
///////////////////////////
// G A M E L O O P
///////////////////////////
while (App.IsOpened())
{
//EVENT - HANTERING
////////////////////
//Engångsklick
sf::Event event;
while (App.GetEvent(event))
{
// Close window : exit
if (event.Type == sf::Event::Closed)
App.Close();
//Escape stänger av spelet
if ((event.Type == sf::Event::KeyPressed) && (event.Key.Code == sf::Key::Escape))
App.Close();
//Kolla om man klickar på färgade ytor av masken och byt bild
if (event.Type == sf::Event::MouseButtonPressed){
//Mask1
if ((App.GetInput().GetMouseX()-MaskSprite.GetPosition().x)>0 && (App.GetInput().GetMouseX()-MaskSprite.GetPosition().x)<MaskSprite.GetSize().x){
//Länk 1
if (MaskSprite.GetPixel(App.GetInput().GetMouseX()-MaskSprite.GetPosition().x, App.GetInput().GetMouseY()) == sf::Color(lank[aktivtRum][0][1],lank[aktivtRum][0][2],lank[aktivtRum][0][3])){
aktivtRum = lank[aktivtRum][0][0];
if (!rum.LoadFromFile(graphLocation + rumFil[aktivtRum][0])){
// Error...
}
if (!mask.LoadFromFile(graphLocation + rumFil[aktivtRum][1])){
// Error...
}
}
//Länk 2
else if (MaskSprite.GetPixel(App.GetInput().GetMouseX()-MaskSprite.GetPosition().x, App.GetInput().GetMouseY()) == sf::Color(lank[aktivtRum][1][1],lank[aktivtRum][1][2],lank[aktivtRum][1][3])){
aktivtRum = lank[aktivtRum][1][0];
if (!rum.LoadFromFile(graphLocation + rumFil[aktivtRum][0])){
// Error...
}
if (!mask.LoadFromFile(graphLocation + rumFil[aktivtRum][1])){
// Error...
}
}
}
//Mask2
else if ((App.GetInput().GetMouseX()-MaskSprite2.GetPosition().x)>0 && (App.GetInput().GetMouseX()-MaskSprite2.GetPosition().x)<MaskSprite2.GetSize().x){
if (MaskSprite2.GetPixel(App.GetInput().GetMouseX()-MaskSprite2.GetPosition().x, App.GetInput().GetMouseY()) == sf::Color(lank[aktivtRum][0][1],lank[aktivtRum][0][2],lank[aktivtRum][0][3])){
aktivtRum = lank[aktivtRum][0][0];
if (!rum.LoadFromFile(graphLocation + rumFil[aktivtRum][0])){
// Error...
}
if (!mask.LoadFromFile(graphLocation + rumFil[aktivtRum][1])){
// Error...
}
}else if (MaskSprite2.GetPixel(App.GetInput().GetMouseX()-MaskSprite2.GetPosition().x, App.GetInput().GetMouseY()) == sf::Color(lank[aktivtRum][1][1],lank[aktivtRum][1][2],lank[aktivtRum][1][3])){
aktivtRum = lank[aktivtRum][1][0];
if (!rum.LoadFromFile(graphLocation + rumFil[aktivtRum][0])){
// Error...
}
if (!mask.LoadFromFile(graphLocation + rumFil[aktivtRum][1])){
// Error...
}
}//if (MaskSprite2.GetPixel...
}//if ((App.GetInput().GetMouseX...
}//if (event.Type == sf::Event...
}//while (App.GetEv..
//Collisioner
//Om musen nuddar kanten så ska bilden ska rotera
if (App.GetInput().GetMouseX()< rotationTrigger){
Sprite.Move(rotationSpeed * App.GetFrameTime(),0);
Sprite2.Move(rotationSpeed * App.GetFrameTime(),0);
MaskSprite.Move(rotationSpeed * App.GetFrameTime(),0);
MaskSprite2.Move(rotationSpeed * App.GetFrameTime(),0);
}else if (App.GetInput().GetMouseX()> (App.GetWidth()-rotationTrigger)){
Sprite.Move(-rotationSpeed * App.GetFrameTime(),0);
Sprite2.Move(-rotationSpeed * App.GetFrameTime(),0);
MaskSprite.Move(-rotationSpeed * App.GetFrameTime(),0);
MaskSprite2.Move(-rotationSpeed * App.GetFrameTime(),0);
}
//Om musen nuddar en färgad yta på maskbilden så ska den byta bild
if ((App.GetInput().GetMouseX()-MaskSprite.GetPosition().x)>0 && (App.GetInput().GetMouseX()-MaskSprite.GetPosition().x)<MaskSprite.GetSize().x){
if (MaskSprite.GetPixel(App.GetInput().GetMouseX()-MaskSprite.GetPosition().x, App.GetInput().GetMouseY()) == sf::Color(lank[aktivtRum][0][1],lank[aktivtRum][0][2],lank[aktivtRum][0][3])){
Pointer.SetSubRect(sf::IntRect(30,0,60,30));
}else if (MaskSprite.GetPixel(App.GetInput().GetMouseX()-MaskSprite.GetPosition().x, App.GetInput().GetMouseY()) == sf::Color(lank[aktivtRum][1][1],lank[aktivtRum][1][2],lank[aktivtRum][1][3])){
Pointer.SetSubRect(sf::IntRect(30,0,60,30));
}else{
Pointer.SetSubRect(sf::IntRect(0,0,30,30));
}
}else if ((App.GetInput().GetMouseX()-MaskSprite2.GetPosition().x)>0 && (App.GetInput().GetMouseX()-MaskSprite2.GetPosition().x)<MaskSprite2.GetSize().x){
if (MaskSprite2.GetPixel(App.GetInput().GetMouseX()-MaskSprite2.GetPosition().x, App.GetInput().GetMouseY()) == sf::Color(lank[aktivtRum][0][1],lank[aktivtRum][0][2],lank[aktivtRum][0][3])){
Pointer.SetSubRect(sf::IntRect(30,0,60,30));
}else if (MaskSprite2.GetPixel(App.GetInput().GetMouseX()-MaskSprite2.GetPosition().x, App.GetInput().GetMouseY()) == sf::Color(lank[aktivtRum][1][1],lank[aktivtRum][1][2],lank[aktivtRum][1][3])){
Pointer.SetSubRect(sf::IntRect(30,0,60,30));
}else{
Pointer.SetSubRect(sf::IntRect(0,0,30,30));
}
}
//Flytta rotations-sprite ifall den hamnar utanför skärmen
//Sprite 1
if(Sprite.GetPosition().x< -Sprite.GetSize().x){
Sprite.SetX(Sprite2.GetPosition().x+Sprite2.GetSize().x-1);
MaskSprite.SetX(MaskSprite2.GetPosition().x+MaskSprite2.GetSize().x);
}else if(Sprite.GetPosition().x > Sprite.GetSize().x){
Sprite.SetX(Sprite2.GetPosition().x-Sprite2.GetSize().x+1);
MaskSprite.SetX(MaskSprite2.GetPosition().x-MaskSprite2.GetSize().x);
}
//Sprite 2
if(Sprite2.GetPosition().x< -Sprite2.GetSize().x){
Sprite2.SetX(Sprite.GetPosition().x+Sprite.GetSize().x-1);
MaskSprite2.SetX(MaskSprite.GetPosition().x+MaskSprite.GetSize().x);
}else if(Sprite2.GetPosition().x > Sprite2.GetSize().x){
Sprite2.SetX(Sprite.GetPosition().x-Sprite.GetSize().x+1);
MaskSprite2.SetX(MaskSprite.GetPosition().x-MaskSprite.GetSize().x);
}
//GÖR UPPDATERINGAR
///////////////////////
//Placera muspekarbilden på rätt position
Pointer.SetPosition(App.GetInput().GetMouseX(), App.GetInput().GetMouseY());
//SKRIV UT
///////////////////
App.Draw(Sprite);
App.Draw(Sprite2);
App.Draw(Pointer);
App.Display();
}
///////////////////////////
// E N D
///////////////////////////
return EXIT_SUCCESS;
}