source: trunk/src/eglapp.cpp @ 686

Last change on this file since 686 was 686, checked in by sam, 11 years ago

Put everything in the "lol" namespace. Better late than never.

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