source: trunk/src/eglapp.cpp @ 735

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

core: get rid of now useless <cstdio> includes.

  • Property svn:keywords set to Id
File size: 4.4 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_1X
102        EGL_OPENGL_ES_BIT,
103#elif defined HAVE_GLES_2X
104        EGL_OPENGL_ES2_BIT,
105#endif
106        EGL_NONE
107    };
108
109    EGLConfig ecfg;
110    EGLint num_config;
111    if (!eglChooseConfig(data->egl_dpy, attr, &ecfg, 1, &num_config))
112    {
113        Log::Error("cannot choose EGL config (%i)\n", eglGetError());
114        exit(EXIT_FAILURE);
115    }
116
117    if (num_config != 1)
118    {
119        Log::Error("cannot choose between %i EGL configs\n", num_config);
120        exit(EXIT_FAILURE);
121    }
122
123    data->egl_surf = eglCreateWindowSurface(data->egl_dpy, ecfg,
124                                            data->win, NULL);
125    if (data->egl_surf == EGL_NO_SURFACE)
126    {
127        Log::Error("cannot create EGL surface (%i)\n", eglGetError());
128        exit(EXIT_FAILURE);
129    }
130
131    EGLint ctxattr[] =
132    {
133#if defined HAVE_GLES_1X
134        EGL_CONTEXT_CLIENT_VERSION, 1,
135#elif defined HAVE_GLES_2X
136        EGL_CONTEXT_CLIENT_VERSION, 2,
137#endif
138        EGL_NONE
139    };
140    data->egl_ctx = eglCreateContext(data->egl_dpy, ecfg,
141                                     EGL_NO_CONTEXT, ctxattr);
142    if (data->egl_ctx == EGL_NO_CONTEXT)
143    {
144        Log::Error("cannot create EGL context (%i)\n", eglGetError());
145        exit(EXIT_FAILURE);
146    }
147
148    eglMakeCurrent(data->egl_dpy, data->egl_surf,
149                   data->egl_surf, data->egl_ctx);
150
151    XWindowAttributes gwa;
152    XGetWindowAttributes(data->dpy, data->win, &gwa);
153
154    /* Initialise everything */
155    Ticker::Setup(fps);
156    Video::Setup(gwa.width, gwa.height);
157    Audio::Setup(2);
158#endif
159}
160
161void EglApp::Run()
162{
163    while (!Ticker::Finished())
164    {
165        /* Tick the game */
166        Ticker::TickGame();
167
168        /* Tick the renderer, show the frame and clamp to desired framerate. */
169        Ticker::TickDraw();
170#if defined USE_EGL
171        eglSwapBuffers(data->egl_dpy, data->egl_surf);
172#endif
173
174        Ticker::ClampFps();
175    }
176}
177
178EglApp::~EglApp()
179{
180#if defined USE_EGL
181    eglDestroyContext(data->egl_dpy, data->egl_ctx);
182    eglDestroySurface(data->egl_dpy, data->egl_surf);
183    eglTerminate(data->egl_dpy);
184    XDestroyWindow(data->dpy, data->win);
185    XCloseDisplay(data->dpy);
186#endif
187
188    delete data;
189}
190
191} /* namespace lol */
192
Note: See TracBrowser for help on using the repository browser.