Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-06-02 08:58:15

0001 ////////////////////////////////////////////////////////////
0002 //
0003 // SFML - Simple and Fast Multimedia Library
0004 // Copyright (C) 2007-2023 Laurent Gomila (laurent@sfml-dev.org)
0005 //
0006 // This software is provided 'as-is', without any express or implied warranty.
0007 // In no event will the authors be held liable for any damages arising from the use of this software.
0008 //
0009 // Permission is granted to anyone to use this software for any purpose,
0010 // including commercial applications, and to alter it and redistribute it freely,
0011 // subject to the following restrictions:
0012 //
0013 // 1. The origin of this software must not be misrepresented;
0014 //    you must not claim that you wrote the original software.
0015 //    If you use this software in a product, an acknowledgment
0016 //    in the product documentation would be appreciated but is not required.
0017 //
0018 // 2. Altered source versions must be plainly marked as such,
0019 //    and must not be misrepresented as being the original software.
0020 //
0021 // 3. This notice may not be removed or altered from any source distribution.
0022 //
0023 ////////////////////////////////////////////////////////////
0024 
0025 #ifndef SFML_WINDOWBASE_HPP
0026 #define SFML_WINDOWBASE_HPP
0027 
0028 ////////////////////////////////////////////////////////////
0029 // Headers
0030 ////////////////////////////////////////////////////////////
0031 #include <SFML/Window/Cursor.hpp>
0032 #include <SFML/Window/Export.hpp>
0033 #include <SFML/Window/VideoMode.hpp>
0034 #include <SFML/Window/Vulkan.hpp>
0035 #include <SFML/Window/WindowHandle.hpp>
0036 #include <SFML/Window/WindowStyle.hpp>
0037 #include <SFML/System/Clock.hpp>
0038 #include <SFML/System/NonCopyable.hpp>
0039 #include <SFML/System/String.hpp>
0040 #include <SFML/System/Vector2.hpp>
0041 
0042 
0043 namespace sf
0044 {
0045 namespace priv
0046 {
0047     class WindowImpl;
0048 }
0049 
0050 class Event;
0051 
0052 ////////////////////////////////////////////////////////////
0053 /// \brief Window that serves as a base for other windows
0054 ///
0055 ////////////////////////////////////////////////////////////
0056 class SFML_WINDOW_API WindowBase : NonCopyable
0057 {
0058 public:
0059 
0060     ////////////////////////////////////////////////////////////
0061     /// \brief Default constructor
0062     ///
0063     /// This constructor doesn't actually create the window,
0064     /// use the other constructors or call create() to do so.
0065     ///
0066     ////////////////////////////////////////////////////////////
0067     WindowBase();
0068 
0069     ////////////////////////////////////////////////////////////
0070     /// \brief Construct a new window
0071     ///
0072     /// This constructor creates the window with the size and pixel
0073     /// depth defined in \a mode. An optional style can be passed to
0074     /// customize the look and behavior of the window (borders,
0075     /// title bar, resizable, closable, ...). If \a style contains
0076     /// Style::Fullscreen, then \a mode must be a valid video mode.
0077     ///
0078     /// \param mode  Video mode to use (defines the width, height and depth of the rendering area of the window)
0079     /// \param title Title of the window
0080     /// \param style %Window style, a bitwise OR combination of sf::Style enumerators
0081     ///
0082     ////////////////////////////////////////////////////////////
0083     WindowBase(VideoMode mode, const String& title, Uint32 style = Style::Default);
0084 
0085     ////////////////////////////////////////////////////////////
0086     /// \brief Construct the window from an existing control
0087     ///
0088     /// \param handle Platform-specific handle of the control
0089     ///
0090     ////////////////////////////////////////////////////////////
0091     explicit WindowBase(WindowHandle handle);
0092 
0093     ////////////////////////////////////////////////////////////
0094     /// \brief Destructor
0095     ///
0096     /// Closes the window and frees all the resources attached to it.
0097     ///
0098     ////////////////////////////////////////////////////////////
0099     virtual ~WindowBase();
0100 
0101     ////////////////////////////////////////////////////////////
0102     /// \brief Create (or recreate) the window
0103     ///
0104     /// If the window was already created, it closes it first.
0105     /// If \a style contains Style::Fullscreen, then \a mode
0106     /// must be a valid video mode.
0107     ///
0108     /// \param mode  Video mode to use (defines the width, height and depth of the rendering area of the window)
0109     /// \param title Title of the window
0110     /// \param style %Window style, a bitwise OR combination of sf::Style enumerators
0111     ///
0112     ////////////////////////////////////////////////////////////
0113     virtual void create(VideoMode mode, const String& title, Uint32 style = Style::Default);
0114 
0115     ////////////////////////////////////////////////////////////
0116     /// \brief Create (or recreate) the window from an existing control
0117     ///
0118     /// \param handle Platform-specific handle of the control
0119     ///
0120     ////////////////////////////////////////////////////////////
0121     virtual void create(WindowHandle handle);
0122 
0123     ////////////////////////////////////////////////////////////
0124     /// \brief Close the window and destroy all the attached resources
0125     ///
0126     /// After calling this function, the sf::Window instance remains
0127     /// valid and you can call create() to recreate the window.
0128     /// All other functions such as pollEvent() or display() will
0129     /// still work (i.e. you don't have to test isOpen() every time),
0130     /// and will have no effect on closed windows.
0131     ///
0132     ////////////////////////////////////////////////////////////
0133     virtual void close();
0134 
0135     ////////////////////////////////////////////////////////////
0136     /// \brief Tell whether or not the window is open
0137     ///
0138     /// This function returns whether or not the window exists.
0139     /// Note that a hidden window (setVisible(false)) is open
0140     /// (therefore this function would return true).
0141     ///
0142     /// \return True if the window is open, false if it has been closed
0143     ///
0144     ////////////////////////////////////////////////////////////
0145     bool isOpen() const;
0146 
0147     ////////////////////////////////////////////////////////////
0148     /// \brief Pop the event on top of the event queue, if any, and return it
0149     ///
0150     /// This function is not blocking: if there's no pending event then
0151     /// it will return false and leave \a event unmodified.
0152     /// Note that more than one event may be present in the event queue,
0153     /// thus you should always call this function in a loop
0154     /// to make sure that you process every pending event.
0155     /// \code
0156     /// sf::Event event;
0157     /// while (window.pollEvent(event))
0158     /// {
0159     ///    // process event...
0160     /// }
0161     /// \endcode
0162     ///
0163     /// \param event Event to be returned
0164     ///
0165     /// \return True if an event was returned, or false if the event queue was empty
0166     ///
0167     /// \see waitEvent
0168     ///
0169     ////////////////////////////////////////////////////////////
0170     bool pollEvent(Event& event);
0171 
0172     ////////////////////////////////////////////////////////////
0173     /// \brief Wait for an event and return it
0174     ///
0175     /// This function is blocking: if there's no pending event then
0176     /// it will wait until an event is received.
0177     /// After this function returns (and no error occurred),
0178     /// the \a event object is always valid and filled properly.
0179     /// This function is typically used when you have a thread that
0180     /// is dedicated to events handling: you want to make this thread
0181     /// sleep as long as no new event is received.
0182     /// \code
0183     /// sf::Event event;
0184     /// if (window.waitEvent(event))
0185     /// {
0186     ///    // process event...
0187     /// }
0188     /// \endcode
0189     ///
0190     /// \param event Event to be returned
0191     ///
0192     /// \return False if any error occurred
0193     ///
0194     /// \see pollEvent
0195     ///
0196     ////////////////////////////////////////////////////////////
0197     bool waitEvent(Event& event);
0198 
0199     ////////////////////////////////////////////////////////////
0200     /// \brief Get the position of the window
0201     ///
0202     /// \return Position of the window, in pixels
0203     ///
0204     /// \see setPosition
0205     ///
0206     ////////////////////////////////////////////////////////////
0207     Vector2i getPosition() const;
0208 
0209     ////////////////////////////////////////////////////////////
0210     /// \brief Change the position of the window on screen
0211     ///
0212     /// This function only works for top-level windows
0213     /// (i.e. it will be ignored for windows created from
0214     /// the handle of a child window/control).
0215     ///
0216     /// \param position New position, in pixels
0217     ///
0218     /// \see getPosition
0219     ///
0220     ////////////////////////////////////////////////////////////
0221     void setPosition(const Vector2i& position);
0222 
0223     ////////////////////////////////////////////////////////////
0224     /// \brief Get the size of the rendering region of the window
0225     ///
0226     /// The size doesn't include the titlebar and borders
0227     /// of the window.
0228     ///
0229     /// \return Size in pixels
0230     ///
0231     /// \see setSize
0232     ///
0233     ////////////////////////////////////////////////////////////
0234     Vector2u getSize() const;
0235 
0236     ////////////////////////////////////////////////////////////
0237     /// \brief Change the size of the rendering region of the window
0238     ///
0239     /// \param size New size, in pixels
0240     ///
0241     /// \see getSize
0242     ///
0243     ////////////////////////////////////////////////////////////
0244     void setSize(const Vector2u& size);
0245 
0246     ////////////////////////////////////////////////////////////
0247     /// \brief Change the title of the window
0248     ///
0249     /// \param title New title
0250     ///
0251     /// \see setIcon
0252     ///
0253     ////////////////////////////////////////////////////////////
0254     void setTitle(const String& title);
0255 
0256     ////////////////////////////////////////////////////////////
0257     /// \brief Change the window's icon
0258     ///
0259     /// \a pixels must be an array of \a width x \a height pixels
0260     /// in 32-bits RGBA format.
0261     ///
0262     /// The OS default icon is used by default.
0263     ///
0264     /// \param width  Icon's width, in pixels
0265     /// \param height Icon's height, in pixels
0266     /// \param pixels Pointer to the array of pixels in memory. The
0267     ///               pixels are copied, so you need not keep the
0268     ///               source alive after calling this function.
0269     ///
0270     /// \see setTitle
0271     ///
0272     ////////////////////////////////////////////////////////////
0273     void setIcon(unsigned int width, unsigned int height, const Uint8* pixels);
0274 
0275     ////////////////////////////////////////////////////////////
0276     /// \brief Show or hide the window
0277     ///
0278     /// The window is shown by default.
0279     ///
0280     /// \param visible True to show the window, false to hide it
0281     ///
0282     ////////////////////////////////////////////////////////////
0283     void setVisible(bool visible);
0284 
0285     ////////////////////////////////////////////////////////////
0286     /// \brief Show or hide the mouse cursor
0287     ///
0288     /// The mouse cursor is visible by default.
0289     ///
0290     /// \param visible True to show the mouse cursor, false to hide it
0291     ///
0292     ////////////////////////////////////////////////////////////
0293     void setMouseCursorVisible(bool visible);
0294 
0295     ////////////////////////////////////////////////////////////
0296     /// \brief Grab or release the mouse cursor
0297     ///
0298     /// If set, grabs the mouse cursor inside this window's client
0299     /// area so it may no longer be moved outside its bounds.
0300     /// Note that grabbing is only active while the window has
0301     /// focus.
0302     ///
0303     /// \param grabbed True to enable, false to disable
0304     ///
0305     ////////////////////////////////////////////////////////////
0306     void setMouseCursorGrabbed(bool grabbed);
0307 
0308     ////////////////////////////////////////////////////////////
0309     /// \brief Set the displayed cursor to a native system cursor
0310     ///
0311     /// Upon window creation, the arrow cursor is used by default.
0312     ///
0313     /// \warning The cursor must not be destroyed while in use by
0314     ///          the window.
0315     ///
0316     /// \warning Features related to Cursor are not supported on
0317     ///          iOS and Android.
0318     ///
0319     /// \param cursor Native system cursor type to display
0320     ///
0321     /// \see sf::Cursor::loadFromSystem
0322     /// \see sf::Cursor::loadFromPixels
0323     ///
0324     ////////////////////////////////////////////////////////////
0325     void setMouseCursor(const Cursor& cursor);
0326 
0327     ////////////////////////////////////////////////////////////
0328     /// \brief Enable or disable automatic key-repeat
0329     ///
0330     /// If key repeat is enabled, you will receive repeated
0331     /// KeyPressed events while keeping a key pressed. If it is disabled,
0332     /// you will only get a single event when the key is pressed.
0333     ///
0334     /// Key repeat is enabled by default.
0335     ///
0336     /// \param enabled True to enable, false to disable
0337     ///
0338     ////////////////////////////////////////////////////////////
0339     void setKeyRepeatEnabled(bool enabled);
0340 
0341     ////////////////////////////////////////////////////////////
0342     /// \brief Change the joystick threshold
0343     ///
0344     /// The joystick threshold is the value below which
0345     /// no JoystickMoved event will be generated.
0346     ///
0347     /// The threshold value is 0.1 by default.
0348     ///
0349     /// \param threshold New threshold, in the range [0, 100]
0350     ///
0351     ////////////////////////////////////////////////////////////
0352     void setJoystickThreshold(float threshold);
0353 
0354     ////////////////////////////////////////////////////////////
0355     /// \brief Request the current window to be made the active
0356     ///        foreground window
0357     ///
0358     /// At any given time, only one window may have the input focus
0359     /// to receive input events such as keystrokes or mouse events.
0360     /// If a window requests focus, it only hints to the operating
0361     /// system, that it would like to be focused. The operating system
0362     /// is free to deny the request.
0363     /// This is not to be confused with setActive().
0364     ///
0365     /// \see hasFocus
0366     ///
0367     ////////////////////////////////////////////////////////////
0368     void requestFocus();
0369 
0370     ////////////////////////////////////////////////////////////
0371     /// \brief Check whether the window has the input focus
0372     ///
0373     /// At any given time, only one window may have the input focus
0374     /// to receive input events such as keystrokes or most mouse
0375     /// events.
0376     ///
0377     /// \return True if window has focus, false otherwise
0378     /// \see requestFocus
0379     ///
0380     ////////////////////////////////////////////////////////////
0381     bool hasFocus() const;
0382 
0383     ////////////////////////////////////////////////////////////
0384     /// \brief Get the OS-specific handle of the window
0385     ///
0386     /// The type of the returned handle is sf::WindowHandle,
0387     /// which is a typedef to the handle type defined by the OS.
0388     /// You shouldn't need to use this function, unless you have
0389     /// very specific stuff to implement that SFML doesn't support,
0390     /// or implement a temporary workaround until a bug is fixed.
0391     ///
0392     /// \return System handle of the window
0393     ///
0394     ////////////////////////////////////////////////////////////
0395     WindowHandle getSystemHandle() const;
0396 
0397     ////////////////////////////////////////////////////////////
0398     /// \brief Create a Vulkan rendering surface
0399     ///
0400     /// \param instance  Vulkan instance
0401     /// \param surface   Created surface
0402     /// \param allocator Allocator to use
0403     ///
0404     /// \return True if surface creation was successful, false otherwise
0405     ///
0406     ////////////////////////////////////////////////////////////
0407     bool createVulkanSurface(const VkInstance& instance, VkSurfaceKHR& surface, const VkAllocationCallbacks* allocator = 0);
0408 
0409 protected:
0410 
0411     ////////////////////////////////////////////////////////////
0412     /// \brief Function called after the window has been created
0413     ///
0414     /// This function is called so that derived classes can
0415     /// perform their own specific initialization as soon as
0416     /// the window is created.
0417     ///
0418     ////////////////////////////////////////////////////////////
0419     virtual void onCreate();
0420 
0421     ////////////////////////////////////////////////////////////
0422     /// \brief Function called after the window has been resized
0423     ///
0424     /// This function is called so that derived classes can
0425     /// perform custom actions when the size of the window changes.
0426     ///
0427     ////////////////////////////////////////////////////////////
0428     virtual void onResize();
0429 
0430 private:
0431 
0432     friend class Window;
0433 
0434     ////////////////////////////////////////////////////////////
0435     /// \brief Processes an event before it is sent to the user
0436     ///
0437     /// This function is called every time an event is received
0438     /// from the internal window (through pollEvent or waitEvent).
0439     /// It filters out unwanted events, and performs whatever internal
0440     /// stuff the window needs before the event is returned to the
0441     /// user.
0442     ///
0443     /// \param event Event to filter
0444     ///
0445     ////////////////////////////////////////////////////////////
0446     bool filterEvent(const Event& event);
0447 
0448     ////////////////////////////////////////////////////////////
0449     /// \brief Perform some common internal initializations
0450     ///
0451     ////////////////////////////////////////////////////////////
0452     void initialize();
0453 
0454     ////////////////////////////////////////////////////////////
0455     /// \brief Get the fullscreen window
0456     ///
0457     /// \return The fullscreen window or NULL if there is none
0458     ///
0459     ////////////////////////////////////////////////////////////
0460     const WindowBase* getFullscreenWindow();
0461 
0462     ////////////////////////////////////////////////////////////
0463     /// \brief Set a window as the fullscreen window
0464     ///
0465     /// \param window Window to set as fullscreen window
0466     ///
0467     ////////////////////////////////////////////////////////////
0468     void setFullscreenWindow(const WindowBase* window);
0469 
0470     ////////////////////////////////////////////////////////////
0471     // Member data
0472     ////////////////////////////////////////////////////////////
0473     priv::WindowImpl* m_impl;           //!< Platform-specific implementation of the window
0474     Vector2u          m_size;           //!< Current size of the window
0475 };
0476 
0477 } // namespace sf
0478 
0479 
0480 #endif // SFML_WINDOWBASE_HPP
0481 
0482 
0483 ////////////////////////////////////////////////////////////
0484 /// \class sf::WindowBase
0485 /// \ingroup window
0486 ///
0487 /// sf::WindowBase serves as the base class for all Windows.
0488 ///
0489 /// A sf::WindowBase can create its own new window, or be embedded into
0490 /// an already existing control using the create(handle) function.
0491 ///
0492 /// The sf::WindowBase class provides a simple interface for manipulating
0493 /// the window: move, resize, show/hide, control mouse cursor, etc.
0494 /// It also provides event handling through its pollEvent() and waitEvent()
0495 /// functions.
0496 ///
0497 /// Usage example:
0498 /// \code
0499 /// // Declare and create a new window
0500 /// sf::WindowBase window(sf::VideoMode(800, 600), "SFML window");
0501 ///
0502 /// // The main loop - ends as soon as the window is closed
0503 /// while (window.isOpen())
0504 /// {
0505 ///    // Event processing
0506 ///    sf::Event event;
0507 ///    while (window.pollEvent(event))
0508 ///    {
0509 ///        // Request for closing the window
0510 ///        if (event.type == sf::Event::Closed)
0511 ///            window.close();
0512 ///    }
0513 ///
0514 ///    // Do things with the window here...
0515 /// }
0516 /// \endcode
0517 ///
0518 ////////////////////////////////////////////////////////////