third-party/glfw/tests/empty.c
Go to the documentation of this file.
1 //========================================================================
2 // Empty event test
3 // Copyright (c) Camilla Löwy <elmindreda@glfw.org>
4 //
5 // This software is provided 'as-is', without any express or implied
6 // warranty. In no event will the authors be held liable for any damages
7 // arising from the use of this software.
8 //
9 // Permission is granted to anyone to use this software for any purpose,
10 // including commercial applications, and to alter it and redistribute it
11 // freely, subject to the following restrictions:
12 //
13 // 1. The origin of this software must not be misrepresented; you must not
14 // claim that you wrote the original software. If you use this software
15 // in a product, an acknowledgment in the product documentation would
16 // be appreciated but is not required.
17 //
18 // 2. Altered source versions must be plainly marked as such, and must not
19 // be misrepresented as being the original software.
20 //
21 // 3. This notice may not be removed or altered from any source
22 // distribution.
23 //
24 //========================================================================
25 //
26 // This test is intended to verify that posting of empty events works
27 //
28 //========================================================================
29 
30 #include "tinycthread.h"
31 
32 #include <glad/glad.h>
33 #include <GLFW/glfw3.h>
34 
35 #include <math.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 
39 static volatile int running = GLFW_TRUE;
40 
41 static void error_callback(int error, const char* description)
42 {
43  fprintf(stderr, "Error: %s\n", description);
44 }
45 
46 static int thread_main(void* data)
47 {
48  struct timespec time;
49 
50  while (running)
51  {
52  clock_gettime(CLOCK_REALTIME, &time);
53  time.tv_sec += 1;
54  thrd_sleep(&time, NULL);
55 
57  }
58 
59  return 0;
60 }
61 
62 static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
63 {
64  if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
66 }
67 
68 static float nrand(void)
69 {
70  return (float) rand() / (float) RAND_MAX;
71 }
72 
73 int main(void)
74 {
75  int result;
76  thrd_t thread;
78 
79  srand((unsigned int) time(NULL));
80 
82 
83  if (!glfwInit())
84  exit(EXIT_FAILURE);
85 
86  window = glfwCreateWindow(640, 480, "Empty Event Test", NULL, NULL);
87  if (!window)
88  {
89  glfwTerminate();
90  exit(EXIT_FAILURE);
91  }
92 
93  glfwMakeContextCurrent(window);
96 
97  if (thrd_create(&thread, thread_main, NULL) != thrd_success)
98  {
99  fprintf(stderr, "Failed to create secondary thread\n");
100 
101  glfwTerminate();
102  exit(EXIT_FAILURE);
103  }
104 
105  while (running)
106  {
107  int width, height;
108  float r = nrand(), g = nrand(), b = nrand();
109  float l = (float) sqrt(r * r + g * g + b * b);
110 
111  glfwGetFramebufferSize(window, &width, &height);
112 
113  glViewport(0, 0, width, height);
114  glClearColor(r / l, g / l, b / l, 1.f);
116  glfwSwapBuffers(window);
117 
118  glfwWaitEvents();
119 
120  if (glfwWindowShouldClose(window))
122  }
123 
124  glfwHideWindow(window);
125  thrd_join(thread, &result);
126  glfwDestroyWindow(window);
127 
128  glfwTerminate();
129  exit(EXIT_SUCCESS);
130 }
131 
GLboolean GLboolean GLboolean b
GLboolean GLboolean g
The header of the GLFW 3 API.
GLFWAPI GLFWglproc glfwGetProcAddress(const char *procname)
Returns the address of the specified function for the current context.
Definition: context.c:741
void *(* GLADloadproc)(const char *name)
GLFWAPI void glfwHideWindow(GLFWwindow *window)
Hides the specified window.
Definition: window.c:781
static GLFWwindow * window
Definition: joysticks.c:55
int thrd_create(thrd_t *thr, thrd_start_t func, void *arg)
Definition: tinycthread.c:361
GLFWAPI GLFWerrorfun glfwSetErrorCallback(GLFWerrorfun cbfun)
Sets the error callback.
Definition: init.c:309
GLFWAPI void glfwPostEmptyEvent(void)
Posts an empty event to the event queue.
Definition: window.c:1104
#define GLFW_FALSE
Zero.
Definition: glfw3.h:287
GLuint64 key
Definition: glext.h:8966
GLFWAPI int glfwInit(void)
Initializes the GLFW library.
Definition: init.c:198
GLdouble f
static void error_callback(int error, const char *description)
static volatile int running
#define GL_COLOR_BUFFER_BIT
GLdouble GLdouble r
#define GLFW_KEY_ESCAPE
Definition: glfw3.h:412
#define glClear
GLint GLsizei GLsizei height
GLFWAPI void glfwSwapBuffers(GLFWwindow *window)
Swaps the front and back buffers of the specified window.
Definition: context.c:641
GLFWAPI void glfwMakeContextCurrent(GLFWwindow *window)
Makes the context of the specified window current for the calling thread.
Definition: context.c:611
int thrd_join(thrd_t thr, int *res)
Definition: tinycthread.c:432
static void key_callback(GLFWwindow *window, int key, int scancode, int action, int mods)
action
Definition: enums.py:62
#define glViewport
GLAPI int gladLoadGLLoader(GLADloadproc)
Definition: glad/glad.c:1697
static const textual_icon exit
Definition: model-views.h:254
GLFWAPI void glfwSetWindowShouldClose(GLFWwindow *window, int value)
Sets the close flag of the specified window.
Definition: window.c:486
GLFWAPI void glfwWaitEvents(void)
Waits until events are queued and processes them.
Definition: window.c:1078
#define RAND_MAX
Definition: boing.c:110
GLFWAPI GLFWwindow * glfwCreateWindow(int width, int height, const char *title, GLFWmonitor *monitor, GLFWwindow *share)
Creates a window and its associated context.
Definition: window.c:151
#define GLFW_TRUE
One.
Definition: glfw3.h:279
static int thread_main(void *data)
GLFWAPI void glfwDestroyWindow(GLFWwindow *window)
Destroys the specified window and its context.
Definition: window.c:444
GLFWAPI void glfwGetFramebufferSize(GLFWwindow *window, int *width, int *height)
Retrieves the size of the framebuffer of the specified window.
Definition: window.c:647
GLFWAPI void glfwTerminate(void)
Terminates the GLFW library.
Definition: init.c:243
int main(void)
#define glClearColor
GLFWAPI GLFWkeyfun glfwSetKeyCallback(GLFWwindow *window, GLFWkeyfun cbfun)
Sets the key callback.
Definition: input.c:791
pthread_t thrd_t
Definition: tinycthread.h:317
#define NULL
Definition: tinycthread.c:47
static float nrand(void)
int thrd_sleep(const struct timespec *time_point, struct timespec *remaining)
Definition: tinycthread.c:465
#define GLFW_PRESS
The key or mouse button was pressed.
Definition: glfw3.h:304
GLuint64EXT * result
Definition: glext.h:10921
#define thrd_success
Definition: tinycthread.h:172
struct GLFWwindow GLFWwindow
Definition: parser.hpp:150
GLint GLsizei width
GLFWAPI int glfwWindowShouldClose(GLFWwindow *window)
Checks the close flag of the specified window.
Definition: window.c:477


librealsense2
Author(s): Sergey Dorodnicov , Doron Hirshberg , Mark Horn , Reagan Lopez , Itay Carpis
autogenerated on Mon May 3 2021 02:47:14