source: trunk/src/eglapp.cpp @ 745

Last change on this file since 745 was 745, checked in by sam, 10 years ago

video: enforce shader-enabled GL implementations, getting rid of a lot
of code.

  • Property svn:keywords set to Id
File size: 4.2 KB
Line 
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#if defined USE_EGL
16#   include <X11/Xlib.h>
17#   include <X11/Xatom.h>
18#   include <X11/Xutil.h>
19#   include <EGL/egl.h>
20#endif
21
22#include "core.h"
23#include "lolgl.h"
24#include "eglapp.h"
25
26namespace lol
27{
28
29/*
30 * EGL App implementation class
31 */
32
33class EglAppData
34{
35    friend class EglApp;
36
37private:
38#if defined USE_EGL
39    Display *dpy;
40    Window win;
41    EGLDisplay egl_dpy;
42    EGLContext egl_ctx;
43    EGLSurface egl_surf;
44#endif
45};
46
47/*
48 * Public EglApp class
49 */
50
51EglApp::EglApp(char const *title, vec2i res, float fps) :
52    data(new EglAppData())
53{
54#if defined USE_EGL
55    data->dpy = XOpenDisplay(NULL);
56    if (data->dpy == NULL)
57    {
58        Log::Error("cannot connect to X server\n");
59        exit(EXIT_FAILURE);
60    }
61
62    Window root = DefaultRootWindow(data->dpy);
63
64    XSetWindowAttributes swa;
65    swa.event_mask = ExposureMask | PointerMotionMask | KeyPressMask;
66
67    data->win = XCreateWindow(data->dpy, root, 0, 0, res.x, res.y, 0,
68                              CopyFromParent, InputOutput,
69                              CopyFromParent, CWEventMask, &swa);
70
71    XSetWindowAttributes xattr;
72
73    xattr.override_redirect = False;
74    XChangeWindowAttributes(data->dpy, data->win, CWOverrideRedirect, &xattr);
75
76    XWMHints hints;
77    hints.flags = InputHint;
78    hints.input = True;
79    XSetWMHints(data->dpy, data->win, &hints);
80
81    XMapWindow(data->dpy, data->win);
82    XStoreName(data->dpy, data->win, title);
83
84    data->egl_dpy = eglGetDisplay((EGLNativeDisplayType)data->dpy);
85    if (data->egl_dpy == EGL_NO_DISPLAY)
86    {
87        Log::Error("cannot get EGL display\n");
88        exit(EXIT_FAILURE);
89    }
90
91    if (!eglInitialize(data->egl_dpy, NULL, NULL))
92    {
93        Log::Error("cannot initialize EGL\n");
94        exit(EXIT_FAILURE);
95    }
96
97    EGLint attr[] =
98    {
99        EGL_BUFFER_SIZE, 16,
100        EGL_RENDERABLE_TYPE,
101#if defined HAVE_GLES_2X
102        EGL_OPENGL_ES2_BIT,
103#endif
104        EGL_NONE
105    };
106
107    EGLConfig ecfg;
108    EGLint num_config;
109    if (!eglChooseConfig(data->egl_dpy, attr, &ecfg, 1, &num_config))
110    {
111        Log::Error("cannot choose EGL config (%i)\n", eglGetError());
112        exit(EXIT_FAILURE);
113    }
114
115    if (num_config != 1)
116    {
117        Log::Error("cannot choose between %i EGL configs\n", num_config);
118        exit(EXIT_FAILURE);
119    }
120
121    data->egl_surf = eglCreateWindowSurface(data->egl_dpy, ecfg,
122                                            data->win, NULL);
123    if (data->egl_surf == EGL_NO_SURFACE)
124    {
125        Log::Error("cannot create EGL surface (%i)\n", eglGetError());
126        exit(EXIT_FAILURE);
127    }
128
129    EGLint ctxattr[] =
130    {
131#if defined HAVE_GLES_2X
132        EGL_CONTEXT_CLIENT_VERSION, 2,
133#endif
134        EGL_NONE
135    };
136    data->egl_ctx = eglCreateContext(data->egl_dpy, ecfg,
137                                     EGL_NO_CONTEXT, ctxattr);
138    if (data->egl_ctx == EGL_NO_CONTEXT)
139    {
140        Log::Error("cannot create EGL context (%i)\n", eglGetError());
141        exit(EXIT_FAILURE);
142    }
143
144    eglMakeCurrent(data->egl_dpy, data->egl_surf,
145                   data->egl_surf, data->egl_ctx);
146
147    XWindowAttributes gwa;
148    XGetWindowAttributes(data->dpy, data->win, &gwa);
149
150    /* Initialise everything */
151    Ticker::Setup(fps);
152    Video::Setup(gwa.width, gwa.height);
153    Audio::Setup(2);
154#endif
155}
156
157void EglApp::Run()
158{
159    while (!Ticker::Finished())
160    {
161        /* Tick the game */
162        Ticker::TickGame();
163
164        /* Tick the renderer, show the frame and clamp to desired framerate. */
165        Ticker::TickDraw();
166#if defined USE_EGL
167        eglSwapBuffers(data->egl_dpy, data->egl_surf);
168#endif
169
170        Ticker::ClampFps();
171    }
172}
173
174EglApp::~EglApp()
175{
176#if defined USE_EGL
177    eglDestroyContext(data->egl_dpy, data->egl_ctx);
178    eglDestroySurface(data->egl_dpy, data->egl_surf);
179    eglTerminate(data->egl_dpy);
180    XDestroyWindow(data->dpy, data->win);
181    XCloseDisplay(data->dpy);
182#endif
183
184    delete data;
185}
186
187} /* namespace lol */
188
Note: See TracBrowser for help on using the repository browser.