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

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - GNRlova23

Pages: [1]
1
General / How can I create a drag and drop function?
« on: March 09, 2013, 11:02:47 am »
So yeah, what I want is to draw a rectangle, or any shape for that matter, and then have the user be able to drag that object and drop it off wherever he feels like. So far the only thing I've really managed to handle is setting the position of the shape whenever the user presses the left mouse button, the mouse doesn't even have to be over the rectangle, and even if it is, the rectangle will automatically adjust so that the mouse is holding it by the middle.

Now I understand why these problems exist, I just need to know how to have the program detect when the mouse is hovering over the rectangle and to have the origin of the rectangle set to where ever the mouse is (when the mouse is hovering over the rectangle). At least, that's what I think needs to be done. I'm gonna leave the code of the entire program here, if anybody would be so kind as to help me out, I'd very greatly appreciate it. :)

#include <SFML/Graphics.hpp>
#include <iostream>
#include <SFML/Audio.hpp>

//Glboal variables, functions, classes

//C++ program entry point
int main()
{
        //Creating the window
        sf::RenderWindow window(sf::VideoMode(1280, 800), "SFML Game");

        //Settign the framerate limit to 60 FPS
        window.setFramerateLimit(60);

        window.setKeyRepeatEnabled(false);

        //Variable that keeps the game loop running
        bool play = true;

        //Event object holding all events
        sf::Event event;

        //States for button/events
        bool mouseClicked = false;
        bool mouseInsideRect = false;

        //Variables
        int mouseX = 0;
        int mouseY = 0;

        //Images

        //Render shapes
        //Rectangle Shape
        sf::RectangleShape rect;
        rect.setSize(sf::Vector2f(200, 200));
        rect.setPosition(640, 400);
        rect.setFillColor(sf::Color::White);
        rect.setOrigin(100, 100);

        //Font

        //Text


        //Game loop
        while (play == true)
        {
                //EVENTS
                while (window.pollEvent(event))
                {
                        //LMB Pressed
                        if (event.type == sf::Event::MouseButtonPressed && event.mouseButton.button == sf::Mouse::Left)
                        {
                                mouseClicked = true;
                        }

                        //LMB released
                        if (event.type == sf::Event::MouseButtonReleased && event.mouseButton.button == sf::Mouse::Left)
                        {
                                mouseClicked = false;
                        }

                        //Mouse Moved in window
                        if (event.type == sf::Event::MouseMoved)
                        {
                                mouseX = event.mouseMove.x;
                                mouseY = event.mouseMove.y;
                        }

                        //Event type is window closed
                        if (event.type == sf::Event::Closed)
                        {
                                //Set play to false in order to stop the game loop
                                play = false;
                        }
                }

                //LOGIC

                if (mouseClicked == true)
                {
                        rect.setPosition(mouseX, mouseY);
                }

                //RENDERING
                window.clear();

                window.draw(rect);

                window.display();
        }
        ///////////

        //Clean up and close the window
        window.close();

        //Close the program
        return 0;
}

2
Graphics / Re: Can I create an array list for shapes?
« on: March 07, 2013, 12:17:46 am »
I think you're absolutely right. Thanks for directing me to the right website too. I've actually just started programming just out of pure interest, using free online tutorials. But I definately will check out these books. Thanks once again  :D

3
Graphics / Can I create an array list for shapes?
« on: March 07, 2013, 12:00:24 am »
I'm still new to C++ and SFML, and I'm trying my best to experiment as much as possible. So yeah, my question is: Is there a way I can create an array (Or array list, I'm not sure of the difference) of shapes?

Pages: [1]