wcore: don't parse/validate the exact major/minor version
[mesa-waffle.git] / examples / simple-x11-egl.c
blobc08fc04e237888d35f0af3d40eb1f20652790279
1 // Copyright 2012, 2014 Intel Corporation
2 //
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are met:
7 //
8 // - Redistributions of source code must retain the above copyright notice, this
9 // list of conditions and the following disclaimer.
11 // - Redistributions in binary form must reproduce the above copyright notice,
12 // this list of conditions and the following disclaimer in the documentation
13 // and/or other materials provided with the distribution.
15 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
19 // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23 // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 /// @file
27 ///
28 /// This little example program renders to an X11 window with EGL and an OpenGL
29 /// ES2 context. The program does not include any OpenGL headers and does not
30 /// need to link to libGL. It declares the needed OpenGL symbols itself and
31 /// obtains the OpenGL functions with waffle_dl_sym().
32 ///
33 /// This example is too simple; it does not perform proper error checking. For
34 /// a complete example with error checking, see gl_basic.c.
36 #define WAFFLE_API_VERSION 0x0106
37 #define WAFFLE_API_EXPERIMENTAL
39 #include <stdlib.h>
40 #include <unistd.h>
42 #include <waffle.h>
44 // ---------------------------------------------
45 // OpenGL symbols
46 // ---------------------------------------------
48 typedef float GLclampf;
49 typedef unsigned int GLbitfield;
51 enum {
52 GL_COLOR_BUFFER_BIT = 0x00004000,
55 #ifndef _WIN32
56 #define APIENTRY
57 #else
58 #ifndef APIENTRY
59 #define APIENTRY __stdcall
60 #endif
61 #endif
63 static void (APIENTRY *glClearColor)(GLclampf red, GLclampf green, GLclampf blue, GLclampf alpha);
64 static void (APIENTRY *glClear)(GLbitfield mask);
66 // ---------------------------------------------
68 int
69 main()
71 struct waffle_display *dpy;
72 struct waffle_config *config;
73 struct waffle_window *window;
74 struct waffle_context *ctx;
76 const int32_t init_attrs[] = {
77 WAFFLE_PLATFORM, WAFFLE_PLATFORM_X11_EGL,
81 const int32_t config_attrs[] = {
82 WAFFLE_CONTEXT_API, WAFFLE_CONTEXT_OPENGL_ES2,
84 WAFFLE_RED_SIZE, 8,
85 WAFFLE_BLUE_SIZE, 8,
86 WAFFLE_GREEN_SIZE, 8,
91 const int32_t window_width = 320;
92 const int32_t window_height = 240;
94 waffle_init(init_attrs);
95 dpy = waffle_display_connect(NULL);
97 // Exit if OpenGL ES2 is unsupported.
98 if (!waffle_display_supports_context_api(dpy, WAFFLE_CONTEXT_OPENGL_ES2)
99 || !waffle_dl_can_open(WAFFLE_DL_OPENGL_ES2))
101 exit(EXIT_FAILURE);
104 // Get OpenGL functions.
105 glClearColor = waffle_dl_sym(WAFFLE_DL_OPENGL_ES2, "glClearColor");
106 glClear = waffle_dl_sym(WAFFLE_DL_OPENGL_ES2, "glClear");
108 config = waffle_config_choose(dpy, config_attrs);
109 window = waffle_window_create(config, window_width, window_height);
110 ctx = waffle_context_create(config, NULL);
112 waffle_window_show(window);
113 waffle_make_current(dpy, window, ctx);
115 // Draw red.
116 glClearColor(1.0, 0.0, 0.0, 1.0);
117 glClear(GL_COLOR_BUFFER_BIT);
118 waffle_window_swap_buffers(window);
119 sleep(1); // Sleep so that user can see window.
121 // Draw green.
122 glClearColor(0.0, 1.0, 0.0, 1.0);
123 glClear(GL_COLOR_BUFFER_BIT);
124 waffle_window_swap_buffers(window);
125 sleep(1);
127 // Draw blue.
128 glClearColor(0.0, 0.0, 1.0, 1.0);
129 glClear(GL_COLOR_BUFFER_BIT);
130 waffle_window_swap_buffers(window);
131 sleep(1);
133 // Clean up.
134 waffle_make_current(dpy, NULL, NULL);
135 waffle_window_destroy(window);
136 waffle_context_destroy(ctx);
137 waffle_config_destroy(config);
138 waffle_display_disconnect(dpy);
140 waffle_teardown();
142 return 0;