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

Author Topic: i have a problem with the drag and drop event in my game  (Read 2006 times)

0 Members and 1 Guest are viewing this topic.

jess

  • Newbie
  • *
  • Posts: 1
    • View Profile
    • Email
i have a problem with the drag and drop event in my game
« on: February 02, 2019, 02:59:48 pm »
i would like to say that i am a complete biggner in c++ and sfml and making a simple  2 player mancal game where the players move the stones by dragging and dropping them by using thier mouse but i cant figure that part in my game could someone help me please
[c#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include <SFML/Window.hpp>
#include <iostream>
int main()
{

  sf::RenderWindow window3(sf::VideoMode(800,600),"Gebeta");
[code=cpp][ if (event.mouseButton.button == sf::Mouse::Left)
                    if ( sprite11.getGlobalBounds().
                                   contains(sf::Mouse::getPosition(window).x,
                                            sf::Mouse::getPosition(window).y))
                    {
                        // the problem is here
                        card.setOrigin(event.mouseButton.x, event.mouseButton.y);
                        selected = true;
                    }code]
  sf::Texture BackgroundTexture2;
  sf::Sprite background2;
  sf::Vector2u TextureSize;
  sf::Vector2u WindowSize;



  if(!BackgroundTexture2.loadFromFile("menupn.png"))
  {
    return -1;
  }
  else
  {
    TextureSize = BackgroundTexture2.getSize();
    WindowSize = window3.getSize();

    float ScaleX = (float) WindowSize.x / TextureSize.x;
    float ScaleY = (float) WindowSize.y / TextureSize.y;

    background2.setTexture(BackgroundTexture2);
    background2.setScale(ScaleX, ScaleY);
  }


  while(window3.isOpen())
  {
    sf::Event event;

    while(window3.pollEvent(event))
    {
        switch(event.type)
        {
            case sf::Event::Closed:
                window3.close();
        }
    }
/* sf::Event event1;
    while(window3.pollEvent(event1))
    {
       switch ( event1.type)
        {
      case sf::Event::MouseMoved:
            std::cout<<"x = " <<event1.mouseMove.x<<std::endl;
            std::cout<<"y = " <<event1.mouseMove.y<<std::endl;
            std::cout<<std::endl;
            break;
        }
    }

*/

        if( sf::Mouse::isButtonPressed(sf::Mouse::Left)
       && sf::Mouse::getPosition(window3).x >= 435
       && sf::Mouse::getPosition(window3).y >=226
       && sf::Mouse::getPosition(window3).x <= 485
       && sf::Mouse::getPosition(window3).y <= 260
       ){

          sf::RenderWindow window3(sf::VideoMode(800,600),"Gebeta");
  sf::Texture texture3;
  sf::Texture texture11;
  sf::Texture texture12;
  sf::Texture texture13;
  sf::Texture texture14;
  sf::Texture texture15;
  sf::Texture texture16;
  sf::Texture texture17;
  sf::Texture texture18;
  sf::Texture texture19;
  sf::Texture texture20;
  sf::Texture texture21;
  sf::Texture texture22;
  sf::Texture texture23;
  sf::Texture texture24;
  sf::Texture texture25;
  sf::Texture texture26;
  sf::Texture texture27;
  sf::Texture texture28;
  sf::Texture texture29;
  sf::Texture texture30;
  sf::Texture texture31;
  sf::Texture texture32;
  sf::Texture texture33;
  sf::Texture texture34;
  sf::Texture texture35;
  sf::Texture texture36;
  sf::Texture texture37;
  sf::Texture texture38;
  sf::Texture texture39;
  sf::Texture texture40;
  sf::Texture texture41;
  sf::Texture texture42;
  sf::Texture texture43;
  sf::Texture texture44;
  sf::Texture texture45;
  sf::Texture texture46;
  sf::Texture texture47;
  sf::Texture texture48;
  sf::Texture texture49;
  sf::Texture texture50;
  sf::Texture texture51;
  sf::Texture texture52;
  sf::Texture texture53;
  sf::Texture texture54;
  sf::Texture texture55;
  sf::Texture texture56;
  sf::Texture texture57;
  sf::Texture texture58;
  sf::Sprite sprite3;
  sf::Sprite sprite11;
  sf::Sprite sprite12;
  sf::Sprite sprite13;
  sf::Sprite sprite14;
  sf::Sprite sprite15;
  sf::Sprite sprite16;
  sf::Sprite sprite17;
  sf::Sprite sprite18;
  sf::Sprite sprite19;
  sf::Sprite sprite20;
  sf::Sprite sprite21;
  sf::Sprite sprite22;
  sf::Sprite sprite23;
  sf::Sprite sprite24;
  sf::Sprite sprite25;
  sf::Sprite sprite26;
  sf::Sprite sprite27;
  sf::Sprite sprite28;
  sf::Sprite sprite29;
  sf::Sprite sprite30;
  sf::Sprite sprite31;
  sf::Sprite sprite32;
  sf::Sprite sprite33;
  sf::Sprite sprite34;
  sf::Sprite sprite35;
  sf::Sprite sprite36;
  sf::Sprite sprite37;
  sf::Sprite sprite38;
  sf::Sprite sprite39;
  sf::Sprite sprite40;
  sf::Sprite sprite41;
  sf::Sprite sprite42;
  sf::Sprite sprite43;
  sf::Sprite sprite44;
  sf::Sprite sprite45;
  sf::Sprite sprite46;
  sf::Sprite sprite47;
  sf::Sprite sprite48;
  sf::Sprite sprite49;
  sf::Sprite sprite50;
  sf::Sprite sprite51;
  sf::Sprite sprite52;
  sf::Sprite sprite53;
  sf::Sprite sprite54;
  sf::Sprite sprite55;
  sf::Sprite sprite56;
  sf::Sprite sprite57;
  sf::Sprite sprite58;






 texture11.loadFromFile("1a.png");
sprite11.setTexture(texture11);
sprite11.setPosition(sf::Vector2f(300,330));

texture12.loadFromFile("1b.png");
sprite12.setTexture(texture12);
sprite12.setPosition(sf::Vector2f(310,330));

texture13.loadFromFile("1c.png");
sprite13.setTexture(texture13);
sprite13.setPosition(sf::Vector2f(30,350));

texture14.loadFromFile("1d.png");
sprite14.setTexture(texture14);
sprite14.setPosition(sf::Vector2f(40,350));

texture15.loadFromFile("2a.png");
sprite15.setTexture(texture15);
sprite15.setPosition(sf::Vector2f(45,310));

texture16.loadFromFile("2b.png");
sprite16.setTexture(texture16);
sprite16.setPosition(sf::Vector2f(65,310));

texture17.loadFromFile("2c.png");
sprite17.setTexture(texture17);
sprite17.setPosition(sf::Vector2f(45,350));

texture18.loadFromFile("2d.png");
sprite18.setTexture(texture18);
sprite18.setPosition(sf::Vector2f(65,350));

texture19.loadFromFile("3a.png");
sprite19.setTexture(texture19);
sprite19.setPosition(sf::Vector2f(70,310));

texture20.loadFromFile("3b.png");
sprite20.setTexture(texture20);
sprite20.setPosition(sf::Vector2f(90,310));

texture21.loadFromFile("3c.png");
sprite21.setTexture(texture21);
sprite21.setPosition(sf::Vector2f(70,350));

texture22.loadFromFile("3d.png");
sprite22.setTexture(texture22);
sprite22.setPosition(sf::Vector2f(90,350));

texture23.loadFromFile("4a.png");
sprite23.setTexture(texture23);
sprite23.setPosition(sf::Vector2f(110,310));

texture24.loadFromFile("4b.png");
sprite24.setTexture(texture24);
sprite24.setPosition(sf::Vector2f(140,310));

texture25.loadFromFile("4c.png");
sprite25.setTexture(texture25);
sprite25.setPosition(sf::Vector2f(110,350));

texture26.loadFromFile("4d.png");
sprite26.setTexture(texture26);
sprite26.setPosition(sf::Vector2f(140,350));

texture27.loadFromFile("5a.png");
sprite27.setTexture(texture27);
sprite27.setPosition(sf::Vector2f(150,310));

texture28.loadFromFile("5b.png");
sprite28.setTexture(texture28);
sprite28.setPosition(sf::Vector2f(34,310));

texture29.loadFromFile("5c.png");
sprite29.setTexture(texture29);
sprite29.setPosition(sf::Vector2f(390,350));

texture30.loadFromFile("5d.png");
sprite30.setTexture(texture30);
sprite30.setPosition(sf::Vector2f(390,350));

texture31.loadFromFile("6a.png");
sprite31.setTexture(texture31);
sprite31.setPosition(sf::Vector2f(390,350));

texture32.loadFromFile("6b.png");
sprite32.setTexture(texture32);
sprite32.setPosition(sf::Vector2f(390,350));

texture33.loadFromFile("6c.png");
sprite33.setTexture(texture33);
sprite33.setPosition(sf::Vector2f(390,350));

texture34.loadFromFile("6d.png");
sprite34.setTexture(texture34);
sprite34.setPosition(sf::Vector2f(390,350));

texture35.loadFromFile("7a.png");
sprite35.setTexture(texture35);
sprite35.setPosition(sf::Vector2f(390,350));

texture36.loadFromFile("7b.png");
sprite36.setTexture(texture36);
sprite36.setPosition(sf::Vector2f(390,350));

texture37.loadFromFile("7c.png");
sprite37.setTexture(texture37);
sprite37.setPosition(sf::Vector2f(390,350));

texture38.loadFromFile("7d.png");
sprite38.setTexture(texture38);
sprite38.setPosition(sf::Vector2f(390,350));

texture39.loadFromFile("8a.png");
sprite39.setTexture(texture39);
sprite39.setPosition(sf::Vector2f(390,350));

texture40.loadFromFile("8b.png");
sprite40.setTexture(texture40);
sprite40.setPosition(sf::Vector2f(390,350));

texture41.loadFromFile("8c.png");
sprite41.setTexture(texture41);
sprite41.setPosition(sf::Vector2f(390,350));

texture42.loadFromFile("8d.png");
sprite42.setTexture(texture42);
sprite42.setPosition(sf::Vector2f(390,350));

texture43.loadFromFile("9a.png");
sprite43.setTexture(texture43);
sprite43.setPosition(sf::Vector2f(390,350));

texture44.loadFromFile("9b.png");
sprite44.setTexture(texture44);
sprite44.setPosition(sf::Vector2f(390,350));

texture45.loadFromFile("9c.png");
sprite45.setTexture(texture45);
sprite45.setPosition(sf::Vector2f(390,350));

texture46.loadFromFile("9d.png");
sprite46.setTexture(texture46);
sprite46.setPosition(sf::Vector2f(390,350));

texture47.loadFromFile("10a.png");
sprite47.setTexture(texture47);
sprite47.setPosition(sf::Vector2f(390,350));

texture48.loadFromFile("10b.png");
sprite48.setTexture(texture48);
sprite48.setPosition(sf::Vector2f(390,350));

texture49.loadFromFile("10c.png");
sprite49.setTexture(texture49);
sprite49.setPosition(sf::Vector2f(390,350));

texture50.loadFromFile("10d.png");
sprite50.setTexture(texture50);
sprite50.setPosition(sf::Vector2f(390,350));

texture51.loadFromFile("11a.png");
sprite51.setTexture(texture51);
sprite51.setPosition(sf::Vector2f(390,350));

texture52.loadFromFile("11b.png");
sprite52.setTexture(texture52);
sprite52.setPosition(sf::Vector2f(390,350));

btexture53.loadFromFile("11c.png");
sprite53.setTexture(texture53);
sprite53.setPosition(sf::Vector2f(390,350));

texture54.loadFromFile("11d.png");
sprite54.setTexture(texture54);
sprite54.setPosition(sf::Vector2f(390,350));

texture55.loadFromFile("12a.png");
sprite55.setTexture(texture55);
sprite55.setPosition(sf::Vector2f(390,350));

texture56.loadFromFile("12b.png");
sprite56.setTexture(texture56);
sprite56.setPosition(sf::Vector2f(390,350));

texture57.loadFromFile("12c.png");
sprite57.setTexture(texture57);
sprite57.setPosition(sf::Vector2f(390,350));

texture58.loadFromFile("12d.png");
sprite58.setTexture(texture58);
sprite58.setPosition(sf::Vector2f(390,350));


  if(!texture3.loadFromFile("main.png"))
  {
    std::cout<< " failed to load image " <<std:: endl ;
  }
  else
  {
    TextureSize = texture3.getSize();
    WindowSize = window3.getSize();

    float ScaleX = (float) WindowSize.x / TextureSize.x;
    float ScaleY = (float) WindowSize.y / TextureSize.y;

    sprite3.setTexture(texture3);
    sprite3.setScale(ScaleX, ScaleY);

  }

  while(window3.isOpen())
  {
    sf::Event event;

    while(window3.pollEvent(event))
    {

        switch(event.type)
        {
            case sf::Event::Closed:
                window3.close();
        }

         if (event.type == sf::Event::KeyPressed)
                            {
                                switch (event.key.code)
                                {
                                case  sf::Keyboard::Escape : window3.close(); break;
                                case  sf::Keyboard::Space : window3.close(); break;
                                default : break;
                                }
                            }

    }





    window3.draw(sprite3);
    window3.draw(sprite11);
    window3.draw(sprite12);
    window3.draw(sprite13);
    window3.draw(sprite14);
    window3.draw(sprite15);
    window3.draw(sprite16);
    window3.draw(sprite17);
    window3.draw(sprite18);
    window3.draw(sprite19);
    window3.draw(sprite20);
    window3.draw(sprite21);
    window3.draw(sprite22);
    window3.draw(sprite23);
    window3.draw(sprite24);
    window3.draw(sprite25);
    window3.draw(sprite26);
    window3.draw(sprite27);
    window3.draw(sprite28);
    window3.draw(sprite29);
    window3.draw(sprite30);
    window3.draw(sprite31);
    window3.draw(sprite32);
    window3.draw(sprite33);
    window3.draw(sprite34);
    window3.draw(sprite35);
    window3.draw(sprite36);
    window3.draw(sprite37);
    window3.draw(sprite38);
    window3.draw(sprite39);
    window3.draw(sprite40);
    window3.draw(sprite41);
    window3.draw(sprite42);
    window3.draw(sprite43);
    window3.draw(sprite44);
    window3.draw(sprite45);
    window3.draw(sprite46);
    window3.draw(sprite47);
    window3.draw(sprite48);
    window3.draw(sprite49);
    window3.draw(sprite50);
    window3.draw(sprite51);
    window3.draw(sprite52);
    window3.draw(sprite53);
    window3.draw(sprite54);
    window3.draw(sprite55);
    window3.draw(sprite56);
    window3.draw(sprite57);
    window3.draw(sprite58);
    window3.display();
 }}

 if( sf::Mouse::isButtonPressed(sf::Mouse::Left)
       && sf::Mouse::getPosition(window3).x >= 435
       && sf::Mouse::getPosition(window3).y >=292
       && sf::Mouse::getPosition(window3).x <= 485
       && sf::Mouse::getPosition(window3).y <= 323
       ){

          sf::RenderWindow window3(sf::VideoMode(800,600),"Gebeta");
  sf::Texture texture5;
  sf::Sprite sprite5;
  sf::Vector2u TextureSize;
  sf::Vector2u WindowSize;

  if(!texture5.loadFromFile("n.png"))
  {
    std::cout<< " failed to load image " <<std:: endl ;
  }
  else
  {
    TextureSize = texture5.getSize();
    WindowSize = window3.getSize();

    float ScaleX = (float) WindowSize.x / TextureSize.x;
    float ScaleY = (float) WindowSize.y / TextureSize.y;

    sprite5.setTexture(texture5);
    sprite5.setScale(ScaleX, ScaleY);
  }

  while(window3.isOpen())
  {
    sf::Event event;

    while(window3.pollEvent(event))
    {

        switch(event.type)
        {
            case sf::Event::Closed:
                window3.close();
        }

         if (event.type == sf::Event::KeyPressed)
                            {
                                switch (event.key.code)
                                {
                                case  sf::Keyboard::Escape : window3.close(); break;
                                case  sf::Keyboard::Space : window3.close(); break;
                                default : break;
                                }
                            }

    }
    window3.draw(sprite5);
    window3.display();
  }
 }
//

if( sf::Mouse::isButtonPressed(sf::Mouse::Left)
       && sf::Mouse::getPosition(window3).x >= 434
       && sf::Mouse::getPosition(window3).y >=362
       && sf::Mouse::getPosition(window3).x <= 485
       && sf::Mouse::getPosition(window3).y <= 388
       ){
window3.close();

       }//





    window3.draw(background2);

    window3.display();
 }
}
 
« Last Edit: February 02, 2019, 03:10:50 pm by jess »

NGM88

  • Full Member
  • ***
  • Posts: 162
    • View Profile
Re: i have a problem with the drag and drop event in my game
« Reply #1 on: February 11, 2019, 07:46:38 am »
You need to learn how to properly structure an sfml program (update->events->draw) inside a loop first. I recommend reading the the first chapter of the book "SFML Game Development" and also reading the official tutorials on the website. In addition to learning how to make a proper game loop, you will also learn that you shouldn't use 58 textures to draw 58 sprites: use 1 texture atlas from which you can draw all your sprites.

catalinnic

  • Newbie
  • *
  • Posts: 19
    • View Profile
Re: i have a problem with the drag and drop event in my game
« Reply #2 on: February 19, 2019, 01:53:07 pm »
You know you can create a sprite and texture array using struct?