windows.c
Go to the documentation of this file.
1 //========================================================================
2 // Simple multi-window 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 creates four windows and clears each in a different color
27 //
28 //========================================================================
29 
30 #include <glad/glad.h>
31 #include <GLFW/glfw3.h>
32 
33 #include <stdio.h>
34 #include <stdlib.h>
35 
36 #include "getopt.h"
37 
38 static const char* titles[] =
39 {
40  "Red",
41  "Green",
42  "Blue",
43  "Yellow"
44 };
45 
46 static const struct
47 {
48  float r, g, b;
49 } colors[] =
50 {
51  { 0.95f, 0.32f, 0.11f },
52  { 0.50f, 0.80f, 0.16f },
53  { 0.f, 0.68f, 0.94f },
54  { 0.98f, 0.74f, 0.04f }
55 };
56 
57 static void usage(void)
58 {
59  printf("Usage: windows [-h] [-b] [-f] \n");
60  printf("Options:\n");
61  printf(" -b create decorated windows\n");
62  printf(" -f set focus on show off for all but first window\n");
63  printf(" -h show this help\n");
64 }
65 
66 static void error_callback(int error, const char* description)
67 {
68  fprintf(stderr, "Error: %s\n", description);
69 }
70 
71 static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
72 {
73  if (action != GLFW_PRESS)
74  return;
75 
76  switch (key)
77  {
78  case GLFW_KEY_SPACE:
79  {
80  int xpos, ypos;
81  glfwGetWindowPos(window, &xpos, &ypos);
82  glfwSetWindowPos(window, xpos, ypos);
83  break;
84  }
85 
86  case GLFW_KEY_ESCAPE:
88  break;
89  }
90 }
91 
92 int main(int argc, char** argv)
93 {
94  int i, ch;
95  int decorated = GLFW_FALSE;
96  int focusOnShow = GLFW_TRUE;
97  int running = GLFW_TRUE;
98  GLFWwindow* windows[4];
99 
100  while ((ch = getopt(argc, argv, "bfh")) != -1)
101  {
102  switch (ch)
103  {
104  case 'b':
105  decorated = GLFW_TRUE;
106  break;
107  case 'f':
108  focusOnShow = GLFW_FALSE;
109  break;
110  case 'h':
111  usage();
112  exit(EXIT_SUCCESS);
113  default:
114  usage();
115  exit(EXIT_FAILURE);
116  }
117  }
118 
120 
121  if (!glfwInit())
122  exit(EXIT_FAILURE);
123 
124  glfwWindowHint(GLFW_DECORATED, decorated);
126 
127  for (i = 0; i < 4; i++)
128  {
129  int left, top, right, bottom;
130  if (i)
131  glfwWindowHint(GLFW_FOCUS_ON_SHOW, focusOnShow);
132 
133  windows[i] = glfwCreateWindow(200, 200, titles[i], NULL, NULL);
134  if (!windows[i])
135  {
136  glfwTerminate();
137  exit(EXIT_FAILURE);
138  }
139 
140  glfwSetKeyCallback(windows[i], key_callback);
141 
142  glfwMakeContextCurrent(windows[i]);
144  glClearColor(colors[i].r, colors[i].g, colors[i].b, 1.f);
145 
146  glfwGetWindowFrameSize(windows[i], &left, &top, &right, &bottom);
147  glfwSetWindowPos(windows[i],
148  100 + (i & 1) * (200 + left + right),
149  100 + (i >> 1) * (200 + top + bottom));
150  }
151 
152  for (i = 0; i < 4; i++)
153  glfwShowWindow(windows[i]);
154 
155  while (running)
156  {
157  for (i = 0; i < 4; i++)
158  {
159  glfwMakeContextCurrent(windows[i]);
161  glfwSwapBuffers(windows[i]);
162 
163  if (glfwWindowShouldClose(windows[i]))
164  running = GLFW_FALSE;
165  }
166 
167  glfwWaitEvents();
168  }
169 
170  glfwTerminate();
171  exit(EXIT_SUCCESS);
172 }
173 
GLboolean GLboolean GLboolean b
GLboolean GLboolean g
GLdouble GLdouble GLdouble top
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
static const struct @58 colors[]
static void error_callback(int error, const char *description)
Definition: windows.c:66
void *(* GLADloadproc)(const char *name)
float g
Definition: windows.c:48
#define GLFW_KEY_SPACE
Definition: glfw3.h:360
GLFWAPI GLFWerrorfun glfwSetErrorCallback(GLFWerrorfun cbfun)
Sets the error callback.
Definition: init.c:309
#define GLFW_FALSE
Zero.
Definition: glfw3.h:287
int getopt(int argc, char *const argv[], const char *optstring)
Definition: getopt.c:52
GLuint64 key
Definition: glext.h:8966
float r
Definition: windows.c:48
GLFWAPI int glfwInit(void)
Initializes the GLFW library.
Definition: init.c:198
GLdouble f
static volatile int running
#define GL_COLOR_BUFFER_BIT
static void usage(void)
Definition: windows.c:57
GLdouble GLdouble r
#define GLFW_KEY_ESCAPE
Definition: glfw3.h:412
#define glClear
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
GLFWAPI void glfwGetWindowFrameSize(GLFWwindow *window, int *left, int *top, int *right, int *bottom)
Retrieves the size of the frame of the window.
Definition: window.c:661
GLint left
Definition: glext.h:1963
static void key_callback(GLFWwindow *window, int key, int scancode, int action, int mods)
Definition: windows.c:71
int main(int argc, char **argv)
Definition: windows.c:92
float b
Definition: windows.c:48
action
Definition: enums.py:62
GLAPI int gladLoadGLLoader(GLADloadproc)
Definition: glad/glad.c:1697
static const textual_icon exit
Definition: model-views.h:254
GLFWAPI void glfwSetWindowPos(GLFWwindow *window, int xpos, int ypos)
Sets the position of the client area of the specified window.
Definition: window.c:531
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
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
#define GLFW_FOCUS_ON_SHOW
Input focus on calling show window hint and attribute.
Definition: glfw3.h:827
GLFWAPI void glfwGetWindowPos(GLFWwindow *window, int *xpos, int *ypos)
Retrieves the position of the client area of the specified window.
Definition: window.c:517
GLdouble right
static double xpos
Definition: splitview.c:33
GLFWAPI void glfwShowWindow(GLFWwindow *window)
Makes the specified window visible.
Definition: window.c:755
GLFWAPI void glfwTerminate(void)
Terminates the GLFW library.
Definition: init.c:243
#define GLFW_VISIBLE
Window visibility window hint and attribute.
Definition: glfw3.h:780
#define glClearColor
GLFWAPI GLFWkeyfun glfwSetKeyCallback(GLFWwindow *window, GLFWkeyfun cbfun)
Sets the key callback.
Definition: input.c:791
#define NULL
Definition: tinycthread.c:47
int i
#define GLFW_DECORATED
Window decoration window hint and attribute.
Definition: glfw3.h:786
#define GLFW_PRESS
The key or mouse button was pressed.
Definition: glfw3.h:304
struct GLFWwindow GLFWwindow
GLFWAPI void glfwWindowHint(int hint, int value)
Sets the specified window hint to the desired value.
Definition: window.c:291
static const char * titles[]
Definition: windows.c:38
static double ypos
Definition: splitview.c:33
GLFWAPI int glfwWindowShouldClose(GLFWwindow *window)
Checks the close flag of the specified window.
Definition: window.c:477
GLdouble GLdouble bottom


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