1
General / Is there a way to request that an OpenGL ES context is created?
« on: April 17, 2020, 03:01:51 pm »
The tittle says it all. Is there a way to specifically request an ES context to be created by SFML?
This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.
GLint compileStatus;
gGetShaderiv(fragmentShaderId, GL_COMPILE_STATUS, &compileStatus)
#include <GL/glew.h> //glew must be included befor gl.h (SFML/Window.hpp is including gl.h)
#include <SFML/Window.hpp>
#include <iostream>
#include <fstream>
#include <X11/Xlib.h>
#include <math.h>
using namespace std;
int resolutionX = 600;
int resolutionY = 600;
sf::Window *mainWindow;
void wait(float sec)
{
sf::Clock clock;
sf::Time time;
while (true)
{
time = clock.getElapsedTime();
if (time.asSeconds() >= sec) break;
}
}
std::string fileToString(const std::string &fileName)
{
std::ifstream fileStream(fileName.c_str(), std::ios::in | std::ios::binary | std::ios::ate);
if (fileStream.is_open())
{
std::ifstream::pos_type fileSize = fileStream.tellg();
fileStream.seekg(0, std::ios::beg);
char data[static_cast<int>(fileSize)];
fileStream.read(&data[0], fileSize);
return std::string(&data[0], fileSize);
}
else
{
return "";
}
}
void processEvent(sf::Event e)
{
switch(e.type)
{
case sf::Event::Closed:
{
mainWindow->close();
break;
}
case sf::Event::KeyPressed:
{
switch(e.key.code)
{
case sf::Keyboard::Escape:
{
mainWindow->close();
break;
}
}
break;
}
case sf::Event::Resized:
{
resolutionX = e.size.width;
resolutionY = e.size.height;
glViewport(0, 0, resolutionX, resolutionY);
break;
}
}
}
bool shaderIsReady = false;
GLuint shaderProgramId = 0;
GLuint vertexShaderId = 0;
GLuint fragmentShaderId = 0;
void threadLoadShader()
{
sf::Context threadContext3; //the context creation blocks the main thread... but thats ok because it only happens once at start
wait(2);
shaderProgramId = glCreateProgram();
vertexShaderId = glCreateShader(GL_VERTEX_SHADER);
const char *pCVertexString = fileToString("vertexShader.txt").c_str();
glShaderSource(vertexShaderId, 1, reinterpret_cast<const GLchar**>(&pCVertexString), NULL);
glCompileShader(vertexShaderId);
fragmentShaderId = glCreateShader(GL_FRAGMENT_SHADER);
const char *pCFragmentString = fileToString("fragmentShader.txt").c_str();
glShaderSource(fragmentShaderId, 1, reinterpret_cast<const GLchar**>(&pCFragmentString), NULL);
glCompileShader(fragmentShaderId);
glAttachShader(shaderProgramId, vertexShaderId);
glAttachShader(shaderProgramId, fragmentShaderId);
glLinkProgram(shaderProgramId);
shaderIsReady = true;
}
int main()
{
XInitThreads();
mainWindow = new sf::Window(sf::VideoMode(resolutionX, resolutionY), "SFML OpenGL async glsl load Test");
GLenum err = glewInit();
if (GLEW_OK != err)
{
cout << "GLEW Error:" << glewGetErrorString(err) << endl;
return -1;
}
sf::Thread thread(&threadLoadShader);
thread.launch();
sf::Event event;
sf::Clock clock;
float runtimeSeconds = 0;
float runtimeSecondsOld = 0;
float frameSeconds = 0;
while (true)
{
runtimeSecondsOld = runtimeSeconds;
runtimeSeconds = clock.getElapsedTime().asSeconds();
frameSeconds = runtimeSeconds - runtimeSecondsOld;
if (frameSeconds > 0.02f)
cout << "framelag: " << frameSeconds*1000 << "ms" << endl;
while (mainWindow->pollEvent(event))
{
processEvent(event);
}
if(!mainWindow->isOpen()) break;
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1.0f, 1.0f, -1.0f, 1.0f, -1.0f, 1.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
float rot = runtimeSeconds * 40.0f;
glRotatef(rot, 0.f, 0.f, 1.f);
glColor3f(0.8f, 0.8f, 0.8f);
glLineWidth(4);
glBegin(GL_LINES);
for(float f = -2; f < 2; f += 0.1f)
{
glVertex2f( f, -2.0f);
glVertex2f( f, 2.0f);
}
for(float f = -2; f < 2; f += 0.1f)
{
glVertex2f( -2.0f, f);
glVertex2f( 2.0f, f);
}
glEnd();
glLineWidth(1);
mainWindow->display();
}
thread.terminate();
return EXIT_SUCCESS;
}
#version 330
layout(location = 0) in vec2 position;
void main()
{
gl_Position = vec4(position.x, position.y, 0.0, 1.0);
}
#version 330
out vec4 outputColor;
void main()
{
outputColor = vec4(1.0f, 1.0f, 1.0f, 1.0f);
}