glewinfo_tail.c 8.21 KB
Newer Older
mem's avatar
mem committed
1
2
3
4
5
6
}

#endif /* _WIN32 */

/* ------------------------------------------------------------------------ */

ikits's avatar
ikits committed
7
#if defined(_WIN32) || !defined(__APPLE__) || defined(GLEW_APPLE_GLX)
ikits's avatar
ikits committed
8
int main (int argc, char** argv)
ikits's avatar
ikits committed
9
10
11
#else
int main (void)
#endif
mem's avatar
mem committed
12
13
{
  GLuint err;
ikits's avatar
ikits committed
14
15

#if defined(_WIN32) || !defined(__APPLE__) || defined(GLEW_APPLE_GLX)
ikits's avatar
ikits committed
16
17
18
19
20
  char* display = NULL;
  int visual = -1;

  if (glewParseArgs(argc-1, argv+1, &display, &visual))
  {
ikits's avatar
ikits committed
21
#if defined(_WIN32)
ikits's avatar
ikits committed
22
23
24
25
26
27
    fprintf(stderr, "Usage: glewinfo [-pf <id>]\n");
#else
    fprintf(stderr, "Usage: glewinfo [-display <display>] [-visual <id>]\n");
#endif
    return 1;
  }
ikits's avatar
ikits committed
28
29
#endif

ikits's avatar
ikits committed
30
31
32
33
34
#if defined(_WIN32)
  if (GL_TRUE == glewCreateContext(&visual))
#elif defined(__APPLE__) && !defined(GLEW_APPLE_GLX)
  if (GL_TRUE == glewCreateContext())
#else
ikits's avatar
ikits committed
35
  if (GL_TRUE == glewCreateContext(display, &visual))
ikits's avatar
ikits committed
36
#endif
ikits's avatar
ikits committed
37
38
39
40
41
  {
    fprintf(stderr, "Error: glewCreateContext failed\n");
    glewDestroyContext();
    return 1;
  }
mem's avatar
mem committed
42
  glewExperimental = GL_TRUE;
ikits's avatar
ikits committed
43
#ifdef GLEW_MX
ikits's avatar
ikits committed
44
45
46
  err = glewContextInit(glewGetContext());
#ifdef _WIN32
  err = err || wglewContextInit(wglewGetContext());
ikits's avatar
ikits committed
47
#elif !defined(__APPLE__) || defined(GLEW_APPLE_GLX)
ikits's avatar
ikits committed
48
49
50
  err = err || glxewContextInit(glxewGetContext());
#endif

ikits's avatar
ikits committed
51
#else
mem's avatar
mem committed
52
  err = glewInit();
ikits's avatar
ikits committed
53
#endif
mem's avatar
mem committed
54
55
56
  if (GLEW_OK != err)
  {
    fprintf(stderr, "Error [main]: glewInit failed: %s\n", glewGetErrorString(err));
ikits's avatar
ikits committed
57
    glewDestroyContext();
mem's avatar
mem committed
58
59
    return 1;
  }
ikits's avatar
ikits committed
60
#if defined(_WIN32)
61
62
63
64
#if defined(_MSC_VER) && (_MSC_VER >= 1400)
  if (fopen_s(&f, "glewinfo.txt", "w") != 0)
    f = stdout;
#else
mem's avatar
mem committed
65
  f = fopen("glewinfo.txt", "w");
66
#endif
mem's avatar
mem committed
67
  if (f == NULL) f = stdout;
68
69
70
#else
  f = stdout;
#endif
mem's avatar
mem committed
71
72
73
  fprintf(f, "---------------------------\n");
  fprintf(f, "    GLEW Extension Info\n");
  fprintf(f, "---------------------------\n\n");
ikits's avatar
ikits committed
74
  fprintf(f, "GLEW version %s\n", glewGetString(GLEW_VERSION));
ikits's avatar
ikits committed
75
#if defined(_WIN32)
ikits's avatar
ikits committed
76
  fprintf(f, "Reporting capabilities of pixelformat %d\n", visual);
ikits's avatar
ikits committed
77
#elif !defined(__APPLE__) || defined(GLEW_APPLE_GLX)
ikits's avatar
ikits committed
78
  fprintf(f, "Reporting capabilities of display %s, visual 0x%x\n", 
ikits's avatar
ikits committed
79
    display == NULL ? getenv("DISPLAY") : display, visual);
ikits's avatar
ikits committed
80
#endif
mem's avatar
mem committed
81
82
83
84
  fprintf(f, "Running on a %s from %s\n", 
	  glGetString(GL_RENDERER), glGetString(GL_VENDOR));
  fprintf(f, "OpenGL version %s is supported\n", glGetString(GL_VERSION));
  glewInfo();
ikits's avatar
ikits committed
85
#if defined(_WIN32)
mem's avatar
mem committed
86
  wglewInfo();
ikits's avatar
ikits committed
87
#else
mem's avatar
mem committed
88
89
90
  glxewInfo();
#endif
  if (f != stdout) fclose(f);
ikits's avatar
ikits committed
91
  glewDestroyContext();
mem's avatar
mem committed
92
93
  return 0;
}
ikits's avatar
ikits committed
94
95
96

/* ------------------------------------------------------------------------ */

ikits's avatar
ikits committed
97
#if defined(_WIN32) || !defined(__APPLE__) || defined(GLEW_APPLE_GLX)
ikits's avatar
ikits committed
98
99
100
101
102
GLboolean glewParseArgs (int argc, char** argv, char** display, int* visual)
{
  int p = 0;
  while (p < argc)
  {
ikits's avatar
ikits committed
103
104
#if defined(_WIN32)
    if (!strcmp(argv[p], "-pf") || !strcmp(argv[p], "-pixelformat"))
ikits's avatar
ikits committed
105
106
    {
      if (++p >= argc) return GL_TRUE;
ikits's avatar
ikits committed
107
      *display = 0;
ikits's avatar
ikits committed
108
      *visual = strtol(argv[p++], NULL, 0);
ikits's avatar
ikits committed
109
110
111
    }
    else
      return GL_TRUE;
ikits's avatar
ikits committed
112
#else
ikits's avatar
ikits committed
113
114
115
116
117
    if (!strcmp(argv[p], "-display"))
    {
      if (++p >= argc) return GL_TRUE;
      *display = argv[p++];
    }
ikits's avatar
ikits committed
118
    else if (!strcmp(argv[p], "-visual"))
ikits's avatar
ikits committed
119
120
    {
      if (++p >= argc) return GL_TRUE;
ikits's avatar
ikits committed
121
      *visual = (int)strtol(argv[p++], NULL, 0);
ikits's avatar
ikits committed
122
123
124
125
126
127
128
    }
    else
      return GL_TRUE;
#endif
  }
  return GL_FALSE;
}
ikits's avatar
ikits committed
129
#endif
ikits's avatar
ikits committed
130
131
132

/* ------------------------------------------------------------------------ */

ikits's avatar
ikits committed
133
#if defined(_WIN32)
ikits's avatar
ikits committed
134

ikits's avatar
ikits committed
135
136
137
138
HWND wnd = NULL;
HDC dc = NULL;
HGLRC rc = NULL;

ikits's avatar
ikits committed
139
GLboolean glewCreateContext (int* pixelformat)
ikits's avatar
ikits committed
140
141
142
{
  WNDCLASS wc;
  PIXELFORMATDESCRIPTOR pfd;
ikits's avatar
ikits committed
143
  /* register window class */
ikits's avatar
ikits committed
144
145
146
147
148
  ZeroMemory(&wc, sizeof(WNDCLASS));
  wc.hInstance = GetModuleHandle(NULL);
  wc.lpfnWndProc = DefWindowProc;
  wc.lpszClassName = "GLEW";
  if (0 == RegisterClass(&wc)) return GL_TRUE;
ikits's avatar
ikits committed
149
  /* create window */
ikits's avatar
ikits committed
150
151
  wnd = CreateWindow("GLEW", "GLEW", 0, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, 
                     CW_USEDEFAULT, NULL, NULL, GetModuleHandle(NULL), NULL);
ikits's avatar
ikits committed
152
  if (NULL == wnd) return GL_TRUE;
ikits's avatar
ikits committed
153
  /* get the device context */
ikits's avatar
ikits committed
154
155
  dc = GetDC(wnd);
  if (NULL == dc) return GL_TRUE;
ikits's avatar
ikits committed
156
  /* find pixel format */
ikits's avatar
ikits committed
157
  ZeroMemory(&pfd, sizeof(PIXELFORMATDESCRIPTOR));
ikits's avatar
ikits committed
158
  if (*pixelformat == -1) /* find default */
ikits's avatar
ikits committed
159
160
161
162
  {
    pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);
    pfd.nVersion = 1;
    pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL;
ikits's avatar
ikits committed
163
164
    *pixelformat = ChoosePixelFormat(dc, &pfd);
    if (*pixelformat == 0) return GL_TRUE;
ikits's avatar
ikits committed
165
  }
ikits's avatar
ikits committed
166
  /* set the pixel format for the dc */
ikits's avatar
ikits committed
167
  if (FALSE == SetPixelFormat(dc, *pixelformat, &pfd)) return GL_TRUE;
ikits's avatar
ikits committed
168
  /* create rendering context */
ikits's avatar
ikits committed
169
170
171
172
173
174
175
176
177
  rc = wglCreateContext(dc);
  if (NULL == rc) return GL_TRUE;
  if (FALSE == wglMakeCurrent(dc, rc)) return GL_TRUE;
  return GL_FALSE;
}

void glewDestroyContext ()
{
  if (NULL != rc) wglMakeCurrent(NULL, NULL);
nigels's avatar
nigels committed
178
  if (NULL != rc) wglDeleteContext(rc);
ikits's avatar
ikits committed
179
180
181
182
183
184
185
  if (NULL != wnd && NULL != dc) ReleaseDC(wnd, dc);
  if (NULL != wnd) DestroyWindow(wnd);
  UnregisterClass("GLEW", GetModuleHandle(NULL));
}

/* ------------------------------------------------------------------------ */

ikits's avatar
ikits committed
186
#elif defined(__APPLE__) && !defined(GLEW_APPLE_GLX)
ikits's avatar
ikits committed
187

188
189
#include <OpenGL/OpenGL.h>
#include <OpenGL/CGLTypes.h>
ikits's avatar
ikits committed
190

191
CGLContextObj ctx, octx;
ikits's avatar
ikits committed
192

ikits's avatar
ikits committed
193
GLboolean glewCreateContext ()
ikits's avatar
ikits committed
194
{
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
  const CGLPixelFormatAttribute attrib[4] =
  {
    kCGLPFAAccelerated,                                  /* No software rendering */
#if 0
    kCGLPFAOpenGLProfile,                                /* OSX 10.7 Lion onwards */
    (CGLPixelFormatAttribute) kCGLOGLPVersion_3_2_Core,  /* 3.2 Core Context      */
#endif
    0
  };
  CGLPixelFormatObj pf;
  GLint npix;
  CGLError error;

  error = CGLChoosePixelFormat(attrib, &pf, &npix);
  if (error) return GL_TRUE;
  error = CGLCreateContext(pf, NULL, &ctx);
  if (error) return GL_TRUE;
  CGLReleasePixelFormat(pf);
  octx = CGLGetCurrentContext();
  error = CGLSetCurrentContext(ctx);
  if (error) return GL_TRUE;
216
217
  /* Needed for Regal on the Mac */
  #if defined(GLEW_REGAL) && defined(__APPLE__)
218
  RegalMakeCurrent(ctx);
219
  #endif
ikits's avatar
ikits committed
220
  return GL_FALSE;
ikits's avatar
ikits committed
221
222
223
224
}

void glewDestroyContext ()
{
225
226
  CGLSetCurrentContext(octx);
  CGLReleaseContext(ctx);
ikits's avatar
ikits committed
227
}
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242

/* ------------------------------------------------------------------------ */

#elif defined(__HAIKU__)

GLboolean glewCreateContext ()
{
  /* TODO: Haiku: We need to call C++ code here */
  return GL_FALSE;
}

void glewDestroyContext ()
{
  /* TODO: Haiku: We need to call C++ code here */
}
ikits's avatar
ikits committed
243
244
245

/* ------------------------------------------------------------------------ */

ikits's avatar
ikits committed
246
247
#else /* __UNIX || (__APPLE__ && GLEW_APPLE_GLX) */

ikits's avatar
ikits committed
248
249
Display* dpy = NULL;
XVisualInfo* vi = NULL;
ikits's avatar
ikits committed
250
XVisualInfo* vis = NULL;
ikits's avatar
ikits committed
251
GLXContext ctx = NULL;
ikits's avatar
ikits committed
252
253
Window wnd = 0;
Colormap cmap = 0;
ikits's avatar
ikits committed
254

ikits's avatar
ikits committed
255
GLboolean glewCreateContext (const char* display, int* visual)
ikits's avatar
ikits committed
256
{
ikits's avatar
ikits committed
257
  int attrib[] = { GLX_RGBA, GLX_DOUBLEBUFFER, None };
ikits's avatar
ikits committed
258
  int erb, evb;
ikits's avatar
ikits committed
259
  XSetWindowAttributes swa;
ikits's avatar
ikits committed
260
  /* open display */
ikits's avatar
ikits committed
261
  dpy = XOpenDisplay(display);
ikits's avatar
ikits committed
262
263
  if (NULL == dpy) return GL_TRUE;
  /* query for glx */
ikits's avatar
ikits committed
264
  if (!glXQueryExtension(dpy, &erb, &evb)) return GL_TRUE;
ikits's avatar
ikits committed
265
  /* choose visual */
ikits's avatar
ikits committed
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
  if (*visual == -1)
  {
    vi = glXChooseVisual(dpy, DefaultScreen(dpy), attrib);
    if (NULL == vi) return GL_TRUE;
    *visual = (int)XVisualIDFromVisual(vi->visual);
  }
  else
  {
    int n_vis, i;
    vis = XGetVisualInfo(dpy, 0, NULL, &n_vis);
    for (i=0; i<n_vis; i++)
    {
      if ((int)XVisualIDFromVisual(vis[i].visual) == *visual)
        vi = &vis[i];
    }
    if (vi == NULL) return GL_TRUE;
  }
ikits's avatar
ikits committed
283
284
285
286
  /* create context */
  ctx = glXCreateContext(dpy, vi, None, True);
  if (NULL == ctx) return GL_TRUE;
  /* create window */
ikits's avatar
ikits committed
287
  /*wnd = XCreateSimpleWindow(dpy, RootWindow(dpy, vi->screen), 0, 0, 1, 1, 1, 0, 0);*/
ikits's avatar
ikits committed
288
289
290
  cmap = XCreateColormap(dpy, RootWindow(dpy, vi->screen), vi->visual, AllocNone);
  swa.border_pixel = 0;
  swa.colormap = cmap;
ikits's avatar
ikits committed
291
292
293
  wnd = XCreateWindow(dpy, RootWindow(dpy, vi->screen), 
                      0, 0, 1, 1, 0, vi->depth, InputOutput, vi->visual, 
                      CWBorderPixel | CWColormap, &swa);
ikits's avatar
ikits committed
294
  /* make context current */
ikits's avatar
ikits committed
295
296
  if (!glXMakeCurrent(dpy, wnd, ctx)) return GL_TRUE;
  return GL_FALSE;
ikits's avatar
ikits committed
297
298
299
300
}

void glewDestroyContext ()
{
ikits's avatar
ikits committed
301
  if (NULL != dpy && NULL != ctx) glXDestroyContext(dpy, ctx);
ikits's avatar
ikits committed
302
  if (NULL != dpy && 0 != wnd) XDestroyWindow(dpy, wnd);
ikits's avatar
ikits committed
303
  if (NULL != dpy && 0 != cmap) XFreeColormap(dpy, cmap);
ikits's avatar
ikits committed
304
305
306
307
  if (NULL != vis)
    XFree(vis);
  else if (NULL != vi)
    XFree(vi);
ikits's avatar
ikits committed
308
  if (NULL != dpy) XCloseDisplay(dpy);
ikits's avatar
ikits committed
309
310
}

ikits's avatar
ikits committed
311
#endif /* __UNIX || (__APPLE__ && GLEW_APPLE_GLX) */