I’m building a game engine, and I made a custom interface to the OS (it’s barebones for now, but I plan to expand it) that I can reimplement for each platform to keep things cross-platform. My current implementation is quick & dirty using SFML middleware to save time during testing (I eventually plan to port to Windows/D3D or GTK/Vulkan). However, I’m getting the error “Failed to Create Window’s Context.” I looked online for people with the same problem, but it almost always arose from multithreading. However, I haven’t done any multithreading optimizations yet, so my code has no threads. I have no usage whatsoever of std::async
/std::thread
(or equivalents). Instead, I have a basic window class in my header os.hpp
, and a simple SFML implementation in os.cpp
.
I’m not too familiar with SFML, as it’s been a long time since I’ve used it–so I’ve probably made a rookie mistake. However, I’ve not been able to catch it and I’ve been stuck on this for hours. I just want to get something quick and dirty working so I can actually focus on the meat of the engine.
To make the code more readable and concise, I took the os::Window
class and interspersed the header and the implementation (of course, the implementation & the header are seperate, hence the weird design–since SFML & osWindow aren’t available in the header, so that I can change implementation details down the line).
struct Window
{
Window(Shader const& shader) : shader(shader)
{
data = new osWindow;
osWindow* ptr = (osWindow*)data;
// Shader just stores file paths for the vertex/pixel shader
ptr->shader.loadFromFile(shader.vertex, shader.pixel);
ptr->window.create(sf::WindowHandle("Osmium"));
}
// Hide the Window
void hide(void) { ((osWindow*)data)->window.close(); }
void os::Window::show(void)
{
delete data;
data = new osWindow;
osWindow* ptr = (osWindow*)data;
ptr->shader.loadFromFile(shader.vertex, shader.pixel);
ptr->window.create(sf::WindowHandle("Osmium"));
}
void loop(std::function<void(Window&)> proc)
{
bool running = true;
while (running)
{
sf::RenderWindow& window = ((osWindow*)data)->window;
sf::Event event;
while (ptr->window.pollEvent(event))
if (event.type == sf::Event::Closed)
running = false;
proc(*this);
// Logic to draw the entire window with the shader -- Will be expanded later
}
}
template<class T>
void setUniform(std::string const& uniform, T const& value) { ((osWindow*)data)->shader.setUniform(uniform, value); }
~Window() { delete data; }
private:
Shader const& shader;
bool displayed = false;
void* data = nullptr;
};
I’ve tried a lot of stuff. I thought at first that it was the shaders, but after looking at SFML/Window.cpp
, I found that the error is printed when the setActive
function fails, so it was before the shaders were being used. Additionally, when testing by printing out the frame number to stdout
every frame in proc
, I found that the error was being printed multiple times a frame. I can’t tell where in the window class the error is, though, and printing at various points has raised more questions that it’s answered.
At this point, I’m very sure it is not proc
, vertex.glsl
(my vertex shader), pixel.glsl
(the corresponding pixel shader), or the Shader
struct (which is very simple). I’m 90% sure it’s somewhere in this Window
struct.
Spencer Rosas-Gunn is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.