1 | // |
---|
2 | // Lol Engine |
---|
3 | // |
---|
4 | // Copyright: (c) 2010-2011 Sam Hocevar <sam@hocevar.net> |
---|
5 | // This program is free software; you can redistribute it and/or |
---|
6 | // modify it under the terms of the Do What The Fuck You Want To |
---|
7 | // Public License, Version 2, as published by Sam Hocevar. See |
---|
8 | // http://sam.zoy.org/projects/COPYING.WTFPL for more details. |
---|
9 | // |
---|
10 | |
---|
11 | #if defined HAVE_CONFIG_H |
---|
12 | # include "config.h" |
---|
13 | #endif |
---|
14 | |
---|
15 | #include <cstdio> |
---|
16 | |
---|
17 | #if defined USE_EGL |
---|
18 | # include <X11/Xlib.h> |
---|
19 | # include <X11/Xatom.h> |
---|
20 | # include <X11/Xutil.h> |
---|
21 | # include <EGL/egl.h> |
---|
22 | #endif |
---|
23 | |
---|
24 | #include "core.h" |
---|
25 | #include "lolgl.h" |
---|
26 | #include "eglapp.h" |
---|
27 | |
---|
28 | namespace lol |
---|
29 | { |
---|
30 | |
---|
31 | /* |
---|
32 | * EGL App implementation class |
---|
33 | */ |
---|
34 | |
---|
35 | class EglAppData |
---|
36 | { |
---|
37 | friend class EglApp; |
---|
38 | |
---|
39 | private: |
---|
40 | #if defined USE_EGL |
---|
41 | Display *dpy; |
---|
42 | Window win; |
---|
43 | EGLDisplay egl_dpy; |
---|
44 | EGLContext egl_ctx; |
---|
45 | EGLSurface egl_surf; |
---|
46 | #endif |
---|
47 | }; |
---|
48 | |
---|
49 | /* |
---|
50 | * Public EglApp class |
---|
51 | */ |
---|
52 | |
---|
53 | EglApp::EglApp(char const *title, vec2i res, float fps) : |
---|
54 | data(new EglAppData()) |
---|
55 | { |
---|
56 | #if defined USE_EGL |
---|
57 | data->dpy = XOpenDisplay(NULL); |
---|
58 | if (data->dpy == NULL) |
---|
59 | { |
---|
60 | fprintf(stderr, "Cannot connect to X server\n"); |
---|
61 | exit(EXIT_FAILURE); |
---|
62 | } |
---|
63 | |
---|
64 | Window root = DefaultRootWindow(data->dpy); |
---|
65 | |
---|
66 | XSetWindowAttributes swa; |
---|
67 | swa.event_mask = ExposureMask | PointerMotionMask | KeyPressMask; |
---|
68 | |
---|
69 | data->win = XCreateWindow(data->dpy, root, 0, 0, res.x, res.y, 0, |
---|
70 | CopyFromParent, InputOutput, |
---|
71 | CopyFromParent, CWEventMask, &swa); |
---|
72 | |
---|
73 | XSetWindowAttributes xattr; |
---|
74 | |
---|
75 | xattr.override_redirect = False; |
---|
76 | XChangeWindowAttributes(data->dpy, data->win, CWOverrideRedirect, &xattr); |
---|
77 | |
---|
78 | XWMHints hints; |
---|
79 | hints.flags = InputHint; |
---|
80 | hints.input = True; |
---|
81 | XSetWMHints(data->dpy, data->win, &hints); |
---|
82 | |
---|
83 | XMapWindow(data->dpy, data->win); |
---|
84 | XStoreName(data->dpy, data->win, title); |
---|
85 | |
---|
86 | data->egl_dpy = eglGetDisplay((EGLNativeDisplayType)data->dpy); |
---|
87 | if (data->egl_dpy == EGL_NO_DISPLAY) |
---|
88 | { |
---|
89 | fprintf(stderr, "Cannot get EGL display\n"); |
---|
90 | exit(EXIT_FAILURE); |
---|
91 | } |
---|
92 | |
---|
93 | if (!eglInitialize(data->egl_dpy, NULL, NULL)) |
---|
94 | { |
---|
95 | fprintf(stderr, "Cannot initialize EGL\n"); |
---|
96 | exit(EXIT_FAILURE); |
---|
97 | } |
---|
98 | |
---|
99 | EGLint attr[] = |
---|
100 | { |
---|
101 | EGL_BUFFER_SIZE, 16, |
---|
102 | EGL_RENDERABLE_TYPE, |
---|
103 | #if defined HAVE_GLES_1X |
---|
104 | EGL_OPENGL_ES_BIT, |
---|
105 | #elif defined HAVE_GLES_2X |
---|
106 | EGL_OPENGL_ES2_BIT, |
---|
107 | #endif |
---|
108 | EGL_NONE |
---|
109 | }; |
---|
110 | |
---|
111 | EGLConfig ecfg; |
---|
112 | EGLint num_config; |
---|
113 | if (!eglChooseConfig(data->egl_dpy, attr, &ecfg, 1, &num_config)) |
---|
114 | { |
---|
115 | fprintf(stderr, "Cannot choose EGL config (%i)\n", eglGetError()); |
---|
116 | exit(EXIT_FAILURE); |
---|
117 | } |
---|
118 | |
---|
119 | if (num_config != 1) |
---|
120 | { |
---|
121 | fprintf(stderr, "Cannot choose between %i EGL configs\n", num_config); |
---|
122 | exit(EXIT_FAILURE); |
---|
123 | } |
---|
124 | |
---|
125 | data->egl_surf = eglCreateWindowSurface(data->egl_dpy, ecfg, |
---|
126 | data->win, NULL); |
---|
127 | if (data->egl_surf == EGL_NO_SURFACE) |
---|
128 | { |
---|
129 | fprintf(stderr, "Cannot create EGL surface (%i)\n", eglGetError()); |
---|
130 | exit(EXIT_FAILURE); |
---|
131 | } |
---|
132 | |
---|
133 | EGLint ctxattr[] = |
---|
134 | { |
---|
135 | #if defined HAVE_GLES_1X |
---|
136 | EGL_CONTEXT_CLIENT_VERSION, 1, |
---|
137 | #elif defined HAVE_GLES_2X |
---|
138 | EGL_CONTEXT_CLIENT_VERSION, 2, |
---|
139 | #endif |
---|
140 | EGL_NONE |
---|
141 | }; |
---|
142 | data->egl_ctx = eglCreateContext(data->egl_dpy, ecfg, |
---|
143 | EGL_NO_CONTEXT, ctxattr); |
---|
144 | if (data->egl_ctx == EGL_NO_CONTEXT) |
---|
145 | { |
---|
146 | fprintf(stderr, "Cannot create EGL context (%i)\n", eglGetError()); |
---|
147 | exit(EXIT_FAILURE); |
---|
148 | } |
---|
149 | |
---|
150 | eglMakeCurrent(data->egl_dpy, data->egl_surf, |
---|
151 | data->egl_surf, data->egl_ctx); |
---|
152 | |
---|
153 | XWindowAttributes gwa; |
---|
154 | XGetWindowAttributes(data->dpy, data->win, &gwa); |
---|
155 | |
---|
156 | /* Initialise everything */ |
---|
157 | Ticker::Setup(fps); |
---|
158 | Video::Setup(gwa.width, gwa.height); |
---|
159 | Audio::Setup(2); |
---|
160 | #endif |
---|
161 | } |
---|
162 | |
---|
163 | void EglApp::Run() |
---|
164 | { |
---|
165 | while (!Ticker::Finished()) |
---|
166 | { |
---|
167 | /* Tick the game */ |
---|
168 | Ticker::TickGame(); |
---|
169 | |
---|
170 | /* Tick the renderer, show the frame and clamp to desired framerate. */ |
---|
171 | Ticker::TickDraw(); |
---|
172 | #if defined USE_EGL |
---|
173 | eglSwapBuffers(data->egl_dpy, data->egl_surf); |
---|
174 | #endif |
---|
175 | |
---|
176 | Ticker::ClampFps(); |
---|
177 | } |
---|
178 | } |
---|
179 | |
---|
180 | EglApp::~EglApp() |
---|
181 | { |
---|
182 | #if defined USE_EGL |
---|
183 | eglDestroyContext(data->egl_dpy, data->egl_ctx); |
---|
184 | eglDestroySurface(data->egl_dpy, data->egl_surf); |
---|
185 | eglTerminate(data->egl_dpy); |
---|
186 | XDestroyWindow(data->dpy, data->win); |
---|
187 | XCloseDisplay(data->dpy); |
---|
188 | #endif |
---|
189 | |
---|
190 | delete data; |
---|
191 | } |
---|
192 | |
---|
193 | } /* namespace lol */ |
---|
194 | |
---|