1b877906bSopenharmony_ci//========================================================================
2b877906bSopenharmony_ci// Event wait timeout test
3b877906bSopenharmony_ci// Copyright (c) Camilla Löwy <elmindreda@glfw.org>
4b877906bSopenharmony_ci//
5b877906bSopenharmony_ci// This software is provided 'as-is', without any express or implied
6b877906bSopenharmony_ci// warranty. In no event will the authors be held liable for any damages
7b877906bSopenharmony_ci// arising from the use of this software.
8b877906bSopenharmony_ci//
9b877906bSopenharmony_ci// Permission is granted to anyone to use this software for any purpose,
10b877906bSopenharmony_ci// including commercial applications, and to alter it and redistribute it
11b877906bSopenharmony_ci// freely, subject to the following restrictions:
12b877906bSopenharmony_ci//
13b877906bSopenharmony_ci// 1. The origin of this software must not be misrepresented; you must not
14b877906bSopenharmony_ci//    claim that you wrote the original software. If you use this software
15b877906bSopenharmony_ci//    in a product, an acknowledgment in the product documentation would
16b877906bSopenharmony_ci//    be appreciated but is not required.
17b877906bSopenharmony_ci//
18b877906bSopenharmony_ci// 2. Altered source versions must be plainly marked as such, and must not
19b877906bSopenharmony_ci//    be misrepresented as being the original software.
20b877906bSopenharmony_ci//
21b877906bSopenharmony_ci// 3. This notice may not be removed or altered from any source
22b877906bSopenharmony_ci//    distribution.
23b877906bSopenharmony_ci//
24b877906bSopenharmony_ci//========================================================================
25b877906bSopenharmony_ci//
26b877906bSopenharmony_ci// This test is intended to verify that waiting for events with timeout works
27b877906bSopenharmony_ci//
28b877906bSopenharmony_ci//========================================================================
29b877906bSopenharmony_ci
30b877906bSopenharmony_ci#define GLAD_GL_IMPLEMENTATION
31b877906bSopenharmony_ci#include <glad/gl.h>
32b877906bSopenharmony_ci#define GLFW_INCLUDE_NONE
33b877906bSopenharmony_ci#include <GLFW/glfw3.h>
34b877906bSopenharmony_ci
35b877906bSopenharmony_ci#include <time.h>
36b877906bSopenharmony_ci#include <math.h>
37b877906bSopenharmony_ci#include <stdio.h>
38b877906bSopenharmony_ci#include <stdlib.h>
39b877906bSopenharmony_ci
40b877906bSopenharmony_cistatic void error_callback(int error, const char* description)
41b877906bSopenharmony_ci{
42b877906bSopenharmony_ci    fprintf(stderr, "Error: %s\n", description);
43b877906bSopenharmony_ci}
44b877906bSopenharmony_ci
45b877906bSopenharmony_cistatic void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
46b877906bSopenharmony_ci{
47b877906bSopenharmony_ci    if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
48b877906bSopenharmony_ci        glfwSetWindowShouldClose(window, GLFW_TRUE);
49b877906bSopenharmony_ci}
50b877906bSopenharmony_ci
51b877906bSopenharmony_cistatic float nrand(void)
52b877906bSopenharmony_ci{
53b877906bSopenharmony_ci    return (float) rand() / (float) RAND_MAX;
54b877906bSopenharmony_ci}
55b877906bSopenharmony_ci
56b877906bSopenharmony_ciint main(void)
57b877906bSopenharmony_ci{
58b877906bSopenharmony_ci    GLFWwindow* window;
59b877906bSopenharmony_ci
60b877906bSopenharmony_ci    srand((unsigned int) time(NULL));
61b877906bSopenharmony_ci
62b877906bSopenharmony_ci    glfwSetErrorCallback(error_callback);
63b877906bSopenharmony_ci
64b877906bSopenharmony_ci    if (!glfwInit())
65b877906bSopenharmony_ci        exit(EXIT_FAILURE);
66b877906bSopenharmony_ci
67b877906bSopenharmony_ci    window = glfwCreateWindow(640, 480, "Event Wait Timeout Test", NULL, NULL);
68b877906bSopenharmony_ci    if (!window)
69b877906bSopenharmony_ci    {
70b877906bSopenharmony_ci        glfwTerminate();
71b877906bSopenharmony_ci        exit(EXIT_FAILURE);
72b877906bSopenharmony_ci    }
73b877906bSopenharmony_ci
74b877906bSopenharmony_ci    glfwMakeContextCurrent(window);
75b877906bSopenharmony_ci    gladLoadGL(glfwGetProcAddress);
76b877906bSopenharmony_ci    glfwSetKeyCallback(window, key_callback);
77b877906bSopenharmony_ci
78b877906bSopenharmony_ci    while (!glfwWindowShouldClose(window))
79b877906bSopenharmony_ci    {
80b877906bSopenharmony_ci        int width, height;
81b877906bSopenharmony_ci        float r = nrand(), g = nrand(), b = nrand();
82b877906bSopenharmony_ci        float l = (float) sqrt(r * r + g * g + b * b);
83b877906bSopenharmony_ci
84b877906bSopenharmony_ci        glfwGetFramebufferSize(window, &width, &height);
85b877906bSopenharmony_ci
86b877906bSopenharmony_ci        glViewport(0, 0, width, height);
87b877906bSopenharmony_ci        glClearColor(r / l, g / l, b / l, 1.f);
88b877906bSopenharmony_ci        glClear(GL_COLOR_BUFFER_BIT);
89b877906bSopenharmony_ci        glfwSwapBuffers(window);
90b877906bSopenharmony_ci
91b877906bSopenharmony_ci        glfwWaitEventsTimeout(1.0);
92b877906bSopenharmony_ci    }
93b877906bSopenharmony_ci
94b877906bSopenharmony_ci    glfwDestroyWindow(window);
95b877906bSopenharmony_ci
96b877906bSopenharmony_ci    glfwTerminate();
97b877906bSopenharmony_ci    exit(EXIT_SUCCESS);
98b877906bSopenharmony_ci}
99b877906bSopenharmony_ci
100