How to allow the user to close the Window

This how to shows how to listen to see if the user has closed the window. This demonstrates the core SwinGame input functions and procedures. 

Level

Beginner

Details

In most games you will want the game screen to continue to be displayed until the game ends, or the user closes the window. To achieve this in your game you need to start using SwinGame's input functionality.

At the core of SwinGame's input is the Process Events procedure. This procedure listens to user input, and records what has happened. You can then query SwinGame to check what the user has done.

If you want to check if the user has asked to close the window use the Window Close Requested function. This will return true when the user has tried to close the window.

Each time you call Process Events SwinGame will clear its current list of actions, and check what the user has perfumed since the last call to Process Events. Try to only call Process Events once in your main loop.

The code below shows a small program that demonstrates these input operations in action. The program will continue to show the game screen to the user until they ask to close the window. This is the standard sequence of operations you would expect to see in SwinGames. 

Source Code Widget

  • program HowToAllowTheUserToCloseTheWindow;
    uses SwinGame;
    
    procedure Main();
    
    begin
        OpenGraphicsWindow('How To Allow The User To Close The Window' ,800 ,600 );
        LoadDefaultColors();
        repeat
            ProcessEvents();
            ClearScreen(ColorWhite );
            DrawText('Close the window' ,ColorBlack ,0 ,0 );
            RefreshScreen();
        
        until WindowCloseRequested();
    end;
    begin
        Main();
    end.
    Download Now"
  • #include 
    #include "SwinGame.h"
    int main() 
    {
    
        open_graphics_window("How To Allow The User To Close The Window" ,800 ,600 );
        load_default_colors();
        do 
        {
            process_events();
            clear_screen(color_white );
            draw_text("Close the window" ,color_black ,0 ,0 );
            refresh_screen();
        }
        while ( ! (window_close_requested() ) );
        return 0;
    }
    
    Download Now"
  • Source Code Coming Soon

  • Source Code Coming Soon

  • Source Code Coming Soon


Possibilities