Skip to content

Instantly share code, notes, and snippets.

@AlexVestin
Created March 23, 2021 12:39
Show Gist options
  • Save AlexVestin/39584ee4c9d47f134cb004ae13f1ca6e to your computer and use it in GitHub Desktop.
Save AlexVestin/39584ee4c9d47f134cb004ae13f1ca6e to your computer and use it in GitHub Desktop.
#include <iostream>
#include <vector>
#define EGL_EGL_PROTOTYPES 1
#define GL_GL_PROTOTYPES 1
#include "EGL/egl.h"
#include "EGL/eglext.h"
#include "GLES2/gl2.h"
#include "GLES3/gl3.h"
int screenWidth = 1280;
int screenHeight = 720;
void checkEGLError() {
int err = eglGetError();
const char* errorString;
switch(err) {
case EGL_NOT_INITIALIZED: errorString = "EGL_NOT_INITIALIZED"; break;
case EGL_BAD_ACCESS: errorString = "EGL_BAD_ACCESS";break;
case EGL_BAD_ALLOC: errorString = "EGL_BAD_ALLOC";break;
case EGL_BAD_ATTRIBUTE: errorString = "EGL_BAD_ATTRIBUTE"; break;
case EGL_BAD_CONTEXT: errorString = "EGL_BAD_CONTEXT"; break;
case EGL_BAD_CONFIG: errorString = "EGL_BAD_CONFIG"; break;
case EGL_BAD_CURRENT_SURFACE: errorString = "EGL_BAD_CURRENT_SURFACE"; break;
case EGL_BAD_DISPLAY: errorString = "EGL_BAD_DISPLAY"; break;
case EGL_BAD_SURFACE: errorString = "EGL_BAD_SURFACE"; break;
case EGL_BAD_MATCH: errorString = "EGL_BAD_MATCH"; break;
case EGL_BAD_PARAMETER: errorString = "EGL_BAD_PARAMETER"; break;
case EGL_BAD_NATIVE_PIXMAP: errorString = "EGL_BAD_NATIVE_PIXMAP"; break;
case EGL_BAD_NATIVE_WINDOW: errorString = "EGL_BAD_NATIVE_WINDOW"; break;
case EGL_CONTEXT_LOST: errorString = "EGL_CONTEXT_LOST"; break;
}
std::cout << "EGL error: " << errorString << std::endl;
}
int initEGL() {
// Set up EGL
EGLContext context;
EGLDisplay display;
EGLConfig config;
EGLSurface surface;
// display = eglGetPlatformDisplay(EGL_PLATFORM_ANGLE_ANGLE, nullptr, &display_attributes[0]);
display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
if (display == EGL_NO_DISPLAY) {
checkEGLError();
exit(1);
}
EGLint major;
EGLint minor;
if (!eglInitialize(display, &major, &minor)) {
checkEGLError();
exit(1);
}
EGLint attrib_list[] = {EGL_SURFACE_TYPE, EGL_PBUFFER_BIT,
EGL_RED_SIZE, 8,
EGL_GREEN_SIZE, 8,
EGL_BLUE_SIZE, 8,
EGL_ALPHA_SIZE, 8,
EGL_DEPTH_SIZE, 24,
EGL_STENCIL_SIZE, 8,
EGL_NONE};
EGLint num_config;
if (!eglChooseConfig(display, attrib_list, &config, 1, &num_config)) {
checkEGLError();
exit(1);
}
eglBindAPI(EGL_OPENGL_ES_API);
if (eglGetError() != EGL_SUCCESS) {
checkEGLError();
exit(1);
}
EGLint config_renderable_type;
if (!eglGetConfigAttrib(display, config, EGL_RENDERABLE_TYPE,
&config_renderable_type)) {
checkEGLError();
exit(1);
}
EGLint major_version = 3;
EGLint minor_version = 0;
if ((config_renderable_type & EGL_OPENGL_ES3_BIT) == 0 &&
major_version >= 3) {
major_version = 2;
minor_version = 0;
std::cout << "using 2.0 version" << std::endl;
}
std::vector<EGLint> context_attributes;
context_attributes.push_back(EGL_CONTEXT_MAJOR_VERSION);
context_attributes.push_back(major_version);
context_attributes.push_back(EGL_CONTEXT_MINOR_VERSION);
context_attributes.push_back(minor_version);
context_attributes.push_back(EGL_NONE);
context = eglCreateContext(display, config, EGL_NO_CONTEXT,
context_attributes.data());
if (context == EGL_NO_CONTEXT) {
checkEGLError();
exit(1);
}
EGLint surface_attribs[] = {EGL_WIDTH, (EGLint)screenWidth,
EGL_HEIGHT, (EGLint)screenHeight,
EGL_NONE};
surface = eglCreatePbufferSurface(display, config, surface_attribs);
if (surface == EGL_NO_SURFACE) {
checkEGLError();
exit(1);
}
if (!eglMakeCurrent(display, surface, surface, context)) {
checkEGLError();
exit(1);
}
glClearColo(1.0, 1.0, 0.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
return 0;
}
int main(int argc, char *argv[]) {
if(initEGL() == -1) {
std::cout << "ERROR EGL" << std::endl;
return -1;
}
return EXIT_SUCCESS;
}
/*
INCLUDE=
LIB=
LINKER_FLAGS=-lGLESv2 -lEGL
GCC=g++ -std=c++11 -Wall
FILES=main.cc
all:
$(GCC) $(INCLUDE) $(LIB) $(LINKER_FLAGS) $(FILES) -o main.out
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment