Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Mastering SFML Game Development

You're reading from   Mastering SFML Game Development Inject new life and light into your old SFML projects by advancing to the next level.

Arrow left icon
Product type Paperback
Published in Jan 2017
Publisher Packt
ISBN-13 9781786469885
Length 442 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Raimondas Pupius Raimondas Pupius
Author Profile Icon Raimondas Pupius
Raimondas Pupius
Arrow right icon
View More author details
Toc

Table of Contents (11) Chapters Close

Preface 1. Under the Hood - Setting up the Backend FREE CHAPTER 2. Its Game Time! - Designing the Project 3. Make It Rain! - Building a Particle System 4. Have Thy Gear Ready - Building Game Tools 5. Filling the Tool Belt - a few More Gadgets 6. Adding Some Finishing Touches - Using Shaders 7. One Step Forward, One Level Down - OpenGL Basics 8. Let There Be Light - An Introduction to Advanced Lighting 9. The Speed of Dark - Lighting and Shadows 10. A Chapter You Shouldnt Skip - Final Optimizations

Use of graphical user interfaces

A friendly way of interfacing with applications in a day and age where computers are basically a necessity inside every household is a must. The entire subject of GUIs could fill multiple books by itself, so for the sake of keeping this simple, we are only going to scratch the surface of what we have to work with:

class GUI_Manager : public StateDependent{ 
  friend class GUI_Interface; 
public: 
  ... 
  bool AddInterface(const StateType& l_state, 
    const std::string& l_name); 
  bool AddInterface(const std::string& l_name); 
  GUI_Interface* GetInterface(const StateType& l_state, 
    const std::string& l_name); 
  GUI_Interface* GetInterface(const std::string& l_name); 
  bool RemoveInterface(const StateType& l_state, 
    const std::string& l_name); 
  bool RemoveInterface(const std::string& l_name); 
  bool LoadInterface(const StateType& l_state, 
    const std::string& l_interface, const std::string& l_name); 
  bool LoadInterface(const std::string& l_interface, 
    const std::string& l_name); 
  void ChangeState(const StateType& l_state); 
  void RemoveState(const StateType& l_state); 
  SharedContext* GetContext() const; 
  void DefocusAllInterfaces(); 
  void HandleClick(EventDetails* l_details); 
  void HandleRelease(EventDetails* l_details); 
  void HandleTextEntered(EventDetails* l_details); 
  void AddEvent(GUI_Event l_event); 
  bool PollEvent(GUI_Event& l_event); 
  void Update(float l_dT); 
  void Render(sf::RenderWindow* l_wind); 
  template<class T> 
  void RegisterElement(const GUI_ElementType& l_id){ ... } 
private: 
  ... 
}; 

Interface management, quite predictably, is also dependent on application states. The interfaces themselves are also assigned names, which is how they are loaded and stored. Mouse input, as well as text enter events, are both utilized in making the GUI system work, which is why this class actually uses the event manager and registers three call-backs with it. Not unlike other classes we have discussed, it also uses the factory method, in order to be able to dynamically create different types of elements that populate our interfaces.

Interfaces are described as groups of elements, like so:

Interface MainMenu MainMenu.style 0 0 Immovable NoTitle "Main menu" 
Element Label Title 100 0 MainMenuTitle.style "Main menu:" 
Element Label Play 0 32 MainMenuLabel.style "PLAY" 
Element Label Credits 0 68 MainMenuLabel.style "CREDITS" 
Element Label Quit 0 104 MainMenuLabel.style "EXIT" 

Each element also supports styles for the three different states it can be in: neutral, hovered, and clicked. A single style file describes what an element would look like under all of these conditions:

State Neutral 
Size 300 32 
BgColor 255 0 0 255 
TextColor 255 255 255 255 
TextSize 14 
Font Main 
TextPadding 150 16 
TextOriginCenter 
/State 
 
State Hover 
BgColor 255 100 0 255 
/State 
 
State Clicked 
BgColor 255 150 0 255 
/State 

The Neutral style serves as a base for the other two, which is why they only define attributes that are different from it. Using this model, interfaces of great complexity can be constructed and customized to do almost anything.

You have been reading a chapter from
Mastering SFML Game Development
Published in: Jan 2017
Publisher: Packt
ISBN-13: 9781786469885
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime