raylib/ports/core/core_window_should_close

Licence This example has its own licence, rather than Odin's: raylib/ports/LICENSE
These are ported Raylib examples, and as such, they are subject to Raylib's license. Copyright (c) 2013-2024 Ramon Santamaria (@raysan5)
The rest of the licence
This software is provided "as-is", without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software. Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions: 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. 3. This notice may not be removed or altered from any source distribution.
A screenshot of core_window_should_close

Code

core_window_should_close.odin ¶
81 linesSource

1/*******************************************************************************************
2*
3*   raylib [core] example - window should close
4*
5*   Example complexity rating: [★☆☆☆] 1/4
6*
7*   Example originally created with raylib 4.2, last time updated with raylib 4.2
8*
9*   Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
10*   BSD-like license that allows static linking with closed source software
11*
12*   Copyright (c) 2013-2025 Ramon Santamaria (@raysan5)
13*
14********************************************************************************************/
15
16package raylib_examples
17
18import rl "vendor:raylib"
19
20//------------------------------------------------------------------------------------
21// Program main entry point
22//------------------------------------------------------------------------------------
23main :: proc() {
24	// Initialization
25	//--------------------------------------------------------------------------------------
26	SCREEN_WIDTH :: 800
27	SCREEN_HEIGHT :: 450
28
29	rl.InitWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "raylib [core] example - window should close")
30
31	rl.SetExitKey(.KEY_NULL)       // Disable KEY_ESCAPE to close window, X-button still works
32
33	exit_window_requested: bool   // Flag to request window to exit
34	exit_window: bool    // Flag to set window to exit
35
36	rl.SetTargetFPS(60)           // Set our game to run at 60 frames-per-second
37	//--------------------------------------------------------------------------------------
38
39	// Main game loop
40	for !exit_window {
41		// Update
42		//----------------------------------------------------------------------------------
43		// Detect if X-button or KEY_ESCAPE have been pressed to close window
44		if rl.WindowShouldClose() || rl.IsKeyPressed(.ESCAPE) {
45			exit_window_requested = true
46		}
47
48		if exit_window_requested {
49			// A request for close window has been issued, we can save data before closing
50			// or just show a message asking for confirmation
51
52			if rl.IsKeyPressed(.Y) {
53				exit_window = true
54			} else if rl.IsKeyPressed(.N) {
55				exit_window_requested = false
56			}
57		}
58		//----------------------------------------------------------------------------------
59
60		// Draw
61		//----------------------------------------------------------------------------------
62		rl.BeginDrawing()
63
64			rl.ClearBackground(rl.RAYWHITE)
65
66			if exit_window_requested {
67				rl.DrawRectangle(0, 100, SCREEN_WIDTH, 200, rl.BLACK)
68				rl.DrawText("Are you sure you want to exit program? [Y/N]", 40, 180, 30, rl.WHITE)
69			} else {
70				rl.DrawText("Try to close the window to get confirmation message!", 120, 200, 20, rl.LIGHTGRAY)
71			}
72
73		rl.EndDrawing()
74		//----------------------------------------------------------------------------------
75	}
76
77	// De-Initialization
78	//--------------------------------------------------------------------------------------
79	rl.CloseWindow()        // Close window and OpenGL context
80	//--------------------------------------------------------------------------------------
81}

Declarations Used 15