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

Author Topic: 3 Questions I have.  (Read 1961 times)

0 Members and 1 Guest are viewing this topic.

Moonkis

  • Jr. Member
  • **
  • Posts: 58
    • View Profile
3 Questions I have.
« on: February 03, 2013, 10:24:37 am »
I know the title is rather unspecific but It's hard when your questions are not related to each other, instead of creating 3 topics I'd rather gather them here.


Dragging GUI Console Window

I'v been going at this for a while and I haven't been able to receive the effect I wanted. Inside my game I have this Console that is basically a square picture. Now I want to be able to drag that image across the screen using the top as a draggable zone ( you know like how a normal window works ).

Problem with this is that the only solution I have come across SNAPS the window top left corner to the mouse location. I'v been trying to calculate an offset for where the place the dragged window when I'm moving my mouse but it results in the window not being draggabele at all.

I'm checking if the mouse left button is down-

Transparent SFML Window
Is there a way to achieve this? Like those patches where they have an irregular shaped image that is not a square and they keep the rest of the rendering window totally transparent so you can only see the irregular image. Kinda of like Perfect Worlds patcher window.

Handling resources
This one might be a bit longer but maybe you can point me in an direction? I wounder how to efficiently store my audio, graphics ect within the game so that all that needs have access to it. Right now I'm going with a singleton, but my main problem here is I don't know how I should write the code that determines wheither an graphical resources should be unloaded or keept in memory, like for example if you switch between "States/Screens" I want to keep the resources that are loaded the most so that I don't do unload the imidiatly load them again.


I know this was kinda a mouth full but you know, I'v always wondered.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: 3 Questions I have.
« Reply #1 on: February 03, 2013, 11:14:42 am »
Quote
instead of creating 3 topics I'd rather gather them here
I think it's a very bad strategy, mixing unrelated discussions together never helped to make them more readable ;)

Quote
Dragging GUI Console Window
Obviously, it's your algorithm or implementation which is wrong. We can't help you improve it if you don't show your code.

Quote
Transparent SFML Window
This is not directly possible with SFML, you need to write OS-specific code.

Quote
Handling resources
I know other people here that will have much better answers than me on this topic :)
Laurent Gomila - SFML developer

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: 3 Questions I have.
« Reply #2 on: February 03, 2013, 12:26:04 pm »
Handling resources
This one might be a bit longer but maybe you can point me in an direction? I wounder how to efficiently store my audio, graphics ect within the game so that all that needs have access to it. Right now I'm going with a singleton, but my main problem here is I don't know how I should write the code that determines wheither an graphical resources should be unloaded or keept in memory, like for example if you switch between "States/Screens" I want to keep the resources that are loaded the most so that I don't do unload the imidiatly load them again.
With idiomatic C++, you use RAII to manage resources. That is, you have classes that store resources and are responsible for their lifetime. You should not use singletons and global variables, they bring a lot of problems. A good design mostly makes them unnecessary.

For example, you can store your textures in an associative STL container like std::map. The key type is some kind of ID to refer to the texture, the mapped type is the texture itself or a std::unique_ptr<sf::Texture>. In the class that manages your resources, you initially load the resources. As long as they are used, your object stays alive. If it is destroyed, all resources are automatically freed -- if you make it right, you need not define a destructor.

This is for the case where you have relatively deterministic lifetimes: You acquire assets at initialization time and release them at destruction. If you have dynamic requirements like resources that should be loaded on-demand, you can try the Thor.Resources module I have written (tutorial and API doc). But if you don't actually need the dynamic semantics, it tends to overcomplicate things ;)
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Moonkis

  • Jr. Member
  • **
  • Posts: 58
    • View Profile
Re: 3 Questions I have.
« Reply #3 on: February 03, 2013, 06:24:29 pm »
#include "TinyTerminal.hpp"

void TinyTerminal::start()
{
        sf::RenderWindow window( sf::VideoMode( 512, 256, 32 ), "TinyTerminal", sf::Style::None );
        window.setFramerateLimit(30);
        sf::Event e;

        sf::Texture txtSkin;
        sf::Sprite sprSkin;
        txtSkin.loadFromFile("TinyTerminal.png");
        sprSkin.setTexture( txtSkin );

        int lastMX = 0;
        int lastMY = 0;

        bool dragging = false;
        while (window.isOpen())
        {
                while (window.pollEvent(e))
                {

                }
                if( sf::Mouse::isButtonPressed( sf::Mouse::Button::Left ) )
                {
                        if( sf::Mouse::getPosition(window).x > 0 && sf::Mouse::getPosition(window).x < 512 )
                        {
                                if( sf::Mouse::getPosition(window).y > 0 && sf::Mouse::getPosition(window).y < 12 )
                                {
                                        dragging = true;
                                        lastMX = window.getPosition().x - sf::Mouse::getPosition().x;
                                        lastMY = window.getPosition().y - sf::Mouse::getPosition().y;
                                        window.setMouseCursorVisible(false);
                                }
                        }
                }
                else
                {
                        dragging = false;
                        lastMX = 0;
                        lastMY = 0;
                        window.setMouseCursorVisible(true);
                }

                if( dragging )
                {
                        /* Offset */
                        window.setPosition( sf::Vector2i( sf::Mouse::getPosition().x + lastMX, sf::Mouse::getPosition().y + lastMY ) );
                }

                /* Parse some sweet commands here. */

                /* Draw some sweet sprites here. */
                window.clear(sf::Color::Black);
                window.draw( sprSkin );
                window.draw( output );
                window.display();
        }
}

This works, a lot better BUT there are some response issues when dragging the mouse along the x-axel without changing the y-axel.
« Last Edit: February 03, 2013, 07:14:52 pm by Moonkis »

Weeve

  • Jr. Member
  • **
  • Posts: 87
  • C++ Programmer (Intermediate), 3D Artist (Skilled)
    • View Profile
    • Email
Re: 3 Questions I have.
« Reply #4 on: February 16, 2013, 07:41:55 pm »
If I am understanding what you want, Why not do this?

        if( sf::Mouse::isButtonPressed( sf::Mouse::Button::Left ) )
        {
            if( sf::Mouse::getPosition(window).x > 0 && sf::Mouse::getPosition(window).x < 512 )
            {
                   dragging = true;
                   lastMX = window.getPosition().x - sf::Mouse::getPosition().x;
                   window.setMouseCursorVisible(false);
             }
             if( sf::Mouse::getPosition(window).y > 0 && sf::Mouse::getPosition(window).y < 12 )
             {
                    dragging = true;
                    lastMY = window.getPosition().y - sf::Mouse::getPosition().y;
                    window.setMouseCursorVisible(false);
                }
            }
        }
 
Long live rapid project development! -- Kestrel3D Game-Engine nearing completion

 

anything