I want to create a share GLFW window but GLFW is not able to create the second window.
i am able to create a single window but not two windows
This is my code.
I want to use the second window in another thread so i can share their context.
#include "pch.h"
#include <iostream>
#include <gl\glew.h>
#include <glfw3.h>
int SCR_WIDTH = 1920;
int SCR_HEIGHT = 1080;
int main()
{
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
// glfw window creation
// --------------------
GLFWwindow* sharedWindow = NULL;
GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", 0, sharedWindow);
if (window == NULL)
{
std::cout << "Failed to create the first GLFW window" << std::endl;
glfwTerminate();
return -1;
}
if (sharedWindow == NULL)
{
std::cout << "Failed to create the second GLFW window" << std::endl;
// glfwTerminate();
// return -1;
}
while (true)
{
}
std::cout << "Hello World!\n";
}
share is an input parameter. See glfwCreateWindow.
Create the first window and call glfwCreateWindow a 2nd time for the 2nd window:
GLFWwindow* wnd = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", 0, nullptr);
GLFWwindow* wnd2 = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "window 2", 0, window);
The window can even be created in a separate thread, but note you've to ensure that the OpenGL context of the 1st window is not current when you create the 2nd window.
#include <thread>
#include <mutex>
#include <condition_variable>
GLFWwindow *wnd = nullptr;
bool wnd2created = false;
std::mutex mtx;
std::condition_variable cv;
void wnd2func( void )
{
GLFWwindow *wnd2 = glfwCreateWindow( 800, 600, "window 2", nullptr, wnd );
{
std::unique_lock<std::mutex> lck(mtx);
wnd2created = true;
cv.notify_one();
}
if (wnd2 == nullptr)
return;
glfwMakeContextCurrent(wnd2);
// [...]
}
int main()
{
// [...]
wnd = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", 0, nullptr);
if (wnd == nullptr)
return -1;
std::thread wnd2thread(wnd2func);
{
std::unique_lock<std::mutex> lck(mtx);
cv.wait(lck, []() -> bool { return wnd2created; });
}
glfwMakeContextCurrent(wnd);
// [...]
}
Related
I have .dll file with my Game Engine and .exe with game.
.exe uses .dll
Both of them use glfw_mt.lib.
In dll There is Window class (Window.cpp,Window.h) where i try to glfwCreateWindow(), but it returns NULL. HOWEVER when i try it in dlls main function (EntryPoint.h) glfwCreateWindow() works fine.
My Window abstractionm fails to create glfw window. I see no reason
EntryPoint.h in dll
#include "Core.h"
#include "Game.h"
#include "Window.h"
#include "glad/glad.h"
#include "GLFW/glfw3.h"
#ifdef NK_PLATFORM_WINDOWS
#define NIKSON_START Nikson::Game* CreateGame(){return
#define NIKSON_END }
extern NIKSON_API Nikson::Game* CreateGame();
static void callback(int error_code, const char* description) {
std::cout << "GLFW ERROR:" << error_code << " " << description << std::endl;
}
int main()
{
LOG("LOADED!");glfwSetErrorCallback(callback);
if (!glfwInit())
{
LOG("GLFW ERROR");
}
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
//GLFWwindow* win = glfwCreateWindow(1000, 1000, "Nikson", NULL, NULL);
// LOG(win)
//
///*if (win==nullptr) {
// LOG("FAILED TO CREATE WINDOW")
// return 0;
//}*/
//glfwMakeContextCurrent(win);
Nikson::Window* win = Nikson::CreateWindow("Nikson",1000,1000);
std::cin.get();
//Nikson::Game * game= CreateGame();
////Nikson::Window* win = new Nikson::Window();
//game->Run();
delete win;
}
#endif
Window.h
#pragma once
#include "Core.h"
#include "glad/glad.h"
#include "GLFW/glfw3.h"
namespace Nikson {
struct WindowProp {
std::string Title;
uint32_t Width;
uint32_t Height;
GLFWwindow* WindowPtr;
};
class NIKSON_API Window
{
public:
Window(GLFWwindow* window, const std::string& title = "Nikson",
uint32_t width = 1600,
uint32_t height = 900);
void Tick();
private:
WindowProp m_Properities;
};
NIKSON_API Nikson::Window* CreateWindow(const std::string& title = "Nikson",
uint32_t width = 1600,
uint32_t height = 900);
}
Window.cpp
#include "Window.h"
namespace Nikson
{
Window::Window(GLFWwindow* window, const std::string& title,
uint32_t width,
uint32_t height)
{
m_Properities.Title = title;
m_Properities.Width=width;
m_Properities.Height=height;
m_Properities.WindowPtr = window;
}
void Window::Tick() {
glfwPollEvents();
}
Nikson::Window* CreateWindow(const std::string& title,
uint32_t width,
uint32_t height)
{
GLFWwindow* win = glfwCreateWindow(1000,1000,"Nikson", NULL, NULL);
if (win == NULL) {
LOG(win)
}
/*if (win==nullptr) {
LOG("FAILED TO CREATE WINDOW")
return 0;
}*/
glfwMakeContextCurrent(win);
return new Nikson::Window(win, title, width, height);
}
}
I am writing a game engine in C++ and OpenGL and when I open my first test window i receive this issue:
Segmentation fault (core dumped)
There is no error when compiling, and it compiles just fine. The problem lies when I go to open the window.
HOW TO REPRODUCE:
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <iostream>
void framebuffer_size_callback(GLFWwindow* window, int width, int height);
void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{
glViewport(0, 0, width, height);
}
int main()
{
typedef void (*GL_GENBUFFERS) (GLsizei, GLuint*);
GL_GENBUFFERS glGenBuffers = (GL_GENBUFFERS) glfwGetProcAddress("glGenBuffers");
unsigned int buffer;
glGenBuffers(1, &buffer);
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
GLFWwindow* window = glfwCreateWindow(800, 600, "BloodBunny", NULL, NULL);
if (window == NULL)
{
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
std::cout << "Failed to initialize GLAD" << std::endl;
return -1;
}
while(!glfwWindowShouldClose(window))
{
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
glViewport(0,0,800,600);
glfwTerminate();
return 0;
}
glfwGetProcAddress() will only (potentially, depending on the name requested and the GL version of the current context) return usable, non-NULL function-pointers if a GL context is current.
With GLFW you make a window's context current via glfwMakeContextCurrent() so if you truly want to grab your own pointer to glGenBuffers() you need to move the glfwGetProcAddress() call to after a successful glfwMakeContextCurrent():
...
glfwMakeContextCurrent(window);
...
typedef void (*GL_GENBUFFERS) (GLsizei, GLuint*);
GL_GENBUFFERS glGenBuffers = (GL_GENBUFFERS) glfwGetProcAddress("glGenBuffers");
unsigned int buffer;
glGenBuffers(1, &buffer);
...
...though honestly there's little point since you're calling gladLoadGLLoader() right after glfwMakeContextCurrent() which will populate a perfectly usable glGenBuffers() pointer at global scope all on its own.
The program doesnt show the image in the window i have created , also i dont get any of the fail messages that i have set , which means the values are not null.
What is the problem?
Here is the code:
#include "SDL.h"
#include "SDL_image.h"
#include <iostream>
int main(int argc,char* argv[])
{
SDL_Window *window;
SDL_Renderer *renderer;
SDL_Event event;
bool quit = false;
SDL_Surface *tmpsur = NULL;
SDL_Texture *tex = NULL;
SDL_Init(SDL_INIT_EVERYTHING);
window = SDL_CreateWindow("First window", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 800, 600, SDL_WINDOW_SHOWN);
renderer = SDL_CreateRenderer(window, -1, 0);
tmpsur = IMG_Load("assets/player.png");
if (tmpsur == NULL)
{
std::cout << "fail" << std::endl;
}
tex = SDL_CreateTextureFromSurface(renderer,tmpsur);
if (tex == NULL)
{
std::cout << "fail 2" << std::endl;
}
SDL_FreeSurface(tmpsur);
SDL_RenderPresent(renderer);
while (!quit)
{
while (SDL_PollEvent(&event) != 0)
{
if(event.type == SDL_QUIT)
{
quit = true;
}
}
}
SDL_DestroyWindow(window);
SDL_DestroyRenderer(renderer);
SDL_Quit();
return 0;
}
You need to copy your texture onto the render target. Before presenting your renderer you need to call SDL_RenderCopy like this:
SDL_RenderCopy(renderer, text, nullptr, nullptr);
SDL_RenderPresent(renderer);
The nullptrs in the argument will make it copy the texture over all your target (the window).
For some reason my Window::callback is being called even after the mouse has left the window. I am unable to find a solution or even something that could help. Is it possible that GLFW updated how the mouse cursor callback operates? I wonder if it is an order of invocation problem?
Window
Window::Window(std::string title, int32_t width, int32_t height) {
// TODO: add support for monitor and share for GLFW
m_window = std::unique_ptr<GLFWwindow, GLFWdeleter>(glfwCreateWindow(width, height, title.c_str(), nullptr, nullptr));
glfwMakeContextCurrent(m_window.get());
glfwSetWindowUserPointer(m_window.get(), this);
glfwSetCursorPosCallback(m_window.get(), Window::callback);
}
void Window::mouse_callback(double xpos, double ypos) {
std::cout << "x: " << xpos << " y: " << ypos << std::endl;
}
void Window::callback(GLFWwindow* window, double xpos, double ypos)
{
auto win = static_cast<Window*>(glfwGetWindowUserPointer(window));
win->mouse_callback(xpos, ypos);
}
Engine
void startup() const
{
if (glfwInit() == 0)
{
LOG(kError, "GLFW init failed!");
exit(-1);
}
}
void Engine::run() {
if (m_main_window_registered)
{
glewExperimental = static_cast<GLboolean>(true);
if (glewInit() != GLEW_OK)
{
std::cout << "Failed to initialize glew" << std::endl;
return;
}
}
while(glfwWindowShouldClose(m_main_window->window()) == 0) {
glClear(GL_COLOR_BUFFER_BIT);
glfwSwapBuffers(m_main_window->window());
glfwPollEvents();
}
}
main.cpp
int main()
{
g_engine.startup();
glfwWindowHint(GLFW_SAMPLES, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
auto window = std::make_unique<Window>("Hello World!", 640, 480);
//window->make_current();
g_engine.registerWindow(std::move(window));
g_engine.run();
glfwTerminate();
return 0;
}
I have figured out what the problem (or better put) issue is. On Windows the callback performs as expected where once the mouse leaves the windows area the callback stops firing. For OSX the window never loses focus and therefore the cursor callback is always being called. To fix the issue you simply have to test the coordinates to ensure that the mouse is in fact inside the window.
I was following the SDL Game development book and I cant even get the first file to work right. Upon the application starting it renders the window and then after a few seconds Linux says the game is not responding and ask me to force quit. This repeats every few seconds if I click wait. One thing I have noticed is that SDL_PollEvent never returns true.I am not sure why things are not working. Here is my code.
Main.cpp
#ifdef __cplusplus
#include <cstdlib>
#else
#include <stdlib.h>
#endif
#include "Game.h"
// our Game object
Game* g_game = 0;
int main(int argc, char* argv[]) {
g_game = new Game();
g_game->init("Chapter 1", 100, 100, 640, 480, 0);
while(g_game->running()) {
g_game->handleEvents();
//g_game->update();
g_game->render();
}
g_game->clean();
return 0;
}
Game.h
#ifndef __Game__
#define __Game__
#if !WINDOWS
#include <SDL2/SDL.h>
#else
#include <SDL.h>
#endif
class Game {
public:
Game() {}
~Game() {}
// simply set the running variable to true
bool init(const char* title, int xpos, int ypos, int width, int
height, bool fullscreen);
void render();
void update();
void handleEvents();
void clean();
// a function to access the private running variable
bool running() {
return m_bRunning;
}
private:
SDL_Window* m_pWindow;
SDL_Renderer* m_pRenderer;
bool m_bRunning;
};
#endif /* defined(__Game__) */
Game.cpp
#include "Game.h"
#include <iostream>
bool Game::init(const char* title, int xpos, int ypos, int width,
int height, bool fullscreen) {
// attempt to initialize SDL
if(SDL_Init(SDL_INIT_EVERYTHING) == 0) {
std::cout << "SDL init success\n";
// init the window
int flags = 0;
if(fullscreen) {
flags = SDL_WINDOW_FULLSCREEN;
}
m_pWindow = SDL_CreateWindow(title, xpos, ypos,
width, height, flags);
if(m_pWindow != 0) { // window init success
std::cout << "window creation success\n";
m_pRenderer = SDL_CreateRenderer(m_pWindow, -1, 0);
if(m_pRenderer != 0) { // renderer init success
std::cout << "renderer creation success\n";
SDL_SetRenderDrawColor(m_pRenderer,
255,0,255,255);
} else {
std::cout << "renderer init fail\n";
return false; // renderer init fail
}
} else {
std::cout << "window init fail\n";
return false; // window init fail
}
} else {
std::cout << "SDL init fail\n";
return false; // SDL init fail
}
std::cout << "init success\n";
m_bRunning = true; // everything inited successfully, start the main loop
return true;
}
void Game::render() {
SDL_RenderClear(m_pRenderer); // clear the renderer to the draw color
SDL_RenderPresent(m_pRenderer); // draw to the screen
}
void Game::clean() {
std::cout << "cleaning game\n";
SDL_DestroyWindow(m_pWindow);
SDL_DestroyRenderer(m_pRenderer);
SDL_Quit();
}
void Game::handleEvents() {
SDL_Event event;
while(SDL_PollEvent(&event)) {
std::cout << "Checking Events";
switch(event.type) {
case SDL_QUIT:
std::cout << "Quiting";
m_bRunning = false;
break;
default:
break;
}
}
}
Edit:
so I made a minamal version of the code and now i get a new error. The error says "Segmentation Fault (core dumped)". Based on running it to a specific line in debug the error seems to appear at the line that says "SDL_Texture *tex = SDL_CreateTextureFromSurface(ren, bmp);" I am not sure what the error is though
Here is the minial code:
#ifdef __cplusplus
#include <cstdlib>
#else
#include <stdlib.h>
#endif
#include <iostream>
#include <SDL2/SDL.h>
int main(int argc, char** argv) {
if(SDL_Init(SDL_INIT_EVERYTHING) != 0) {
std::cout << "SDL_Init Error: " << SDL_GetError() << std::endl;
return 1;
}
SDL_Window *win = SDL_CreateWindow("Hello World!", 100, 100, 640, 480, SDL_WINDOW_SHOWN);
if(win == NULL) {
std::cout << "SDL_CreateWindow Error: " << SDL_GetError() << std::endl;
SDL_Quit();
return 1;
}
SDL_Renderer *ren = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
if(ren == NULL) {
SDL_DestroyWindow(win);
std::cout << "SDL_CreateRenderer Error: " << SDL_GetError() << std::endl;
SDL_Quit();
return 1;
}
std::string imagePath = "cb.bmp";
SDL_Surface *bmp = SDL_LoadBMP(imagePath.c_str());
if(bmp == NULL) {
SDL_DestroyRenderer(ren);
SDL_DestroyWindow(win);
std::cout << "SDL_LoadBMP Error: " << SDL_GetError() << std::endl;
SDL_Quit();
return 1;
}
SDL_Texture *tex = SDL_CreateTextureFromSurface(ren, bmp);
if(tex == NULL) {
SDL_DestroyRenderer(ren);
SDL_DestroyWindow(win);
std::cout << "SDL_CreateTextureFromSurface Error: " << SDL_GetError() << std::endl;
SDL_Quit();
return 1;
}
SDL_FreeSurface(bmp);
SDL_Event e;
bool quit = false;
while(!quit) {
while(SDL_PollEvent(&e)) {
//If user closes the window
if(e.type == SDL_QUIT) {
quit = true;
}
//If user presses any key
if(e.type == SDL_KEYDOWN) {
quit = true;
}
//If user clicks the mouse
if(e.type == SDL_MOUSEBUTTONDOWN) {
quit = true;
}
}
SDL_RenderClear(ren);
SDL_RenderPresent(ren);
}
SDL_DestroyTexture(tex);
SDL_DestroyRenderer(ren);
SDL_DestroyWindow(win);
SDL_Quit();
return 0;
}
I have compiled and run your minimal code successfully under Linux with SDL 2.0.4. I used a simple image as "cb.bmp".
Try the following :
compile like this : g++ -Wall -ggdb $(sdl2-config --cflags) -o program main.cpp -lSDL2
gdb ./program, then type 'run'
when it crashes, type 'bt' (for backtrace)
copy the output here
My guess is : either the "bmp" file format is not supported or the rendering driver is not supported. try to use :
SDL_CreateRenderer(win, -1, 0);