glfwCreateWindow() doesnt work out of DLL`s EntryPoint - c++

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);
}
}

Related

glfwGetFrameBufferSize crashes program with access violation

I've been tryna write a renderer using BGFX, and I've set up a method to get the window width, and height - from my window class that wraps GLFWwindow*;
Here is window.h:
#ifndef WINDOW_H
#define WINDOW_H
#include <types.h>
#include <string>
#define GLFW_INCLUDE_NONE
#include <GLFW/glfw3.h>
class RenderWindow {
public:
bool Init(u16 _width, u16 _height, std::string _title);
bool IsCloseRequested() const;
void* GetNativeWindow();
u16 GetHeight();
u16 GetWidth();
void Refresh() const;
GLFWwindow* GlfwWindowPointer = nullptr;
};
#endif
And here is window.cpp
#include "window.h"
#ifdef _WIN32
#define GLFW_EXPOSE_NATIVE_WIN32
#elif __APPLE__
#define GLFW_EXPOSE_NATIVE_COCOA
#endif
#include <GLFW/glfw3native.h>
#include <iostream>
#include <cstdlib>
bool RenderWindow::Init(u16 _width, u16 _height, std::string _title) {
glfwInit();
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
this->GlfwWindowPointer = glfwCreateWindow(_width, _height, _title.c_str(), nullptr, nullptr);
if(this->GlfwWindowPointer == nullptr) {
std::cout << "[OS] [Window] [FATAL] Window creation failed, exiting" << std::endl;
std::exit(10);
}
return true;
}
bool RenderWindow::IsCloseRequested() const {
return glfwWindowShouldClose(this->GlfwWindowPointer);
}
void RenderWindow::Refresh() const {
glfwPollEvents();
}
u16 RenderWindow::GetHeight() {
int w = 0, h = 0;
glfwGetFramebufferSize(&(*GlfwWindowPointer), &w, &h); // ERROR HAPPENS HERE
return h;
}
u16 RenderWindow::GetWidth() {
int w = 0, h = 0;
glfwGetFramebufferSize(&(*GlfwWindowPointer), &w, &h); // ERROR HAPPENS HERE TOO
return w;
}
void* RenderWindow::GetNativeWindow() {
#ifdef _WIN32
return glfwGetWin32Window(this->GlfwWindowPointer);
#elif __APPLE__
return glfwGetCocoaWindow(this->GlfwWindowPointer);
#endif
}
The thing is, whenever I call these methods - the program crashes in vs debugger with the message:
Exception thrown: read access violation.
window was 0xF242340000.
Of course, the address changes every time - but you get the point. Without calling these two functions, my window works effortlessly. I even got bgfx to clear the screen to purple. Please help me.

OpenGL can't create window object

I've bee trying to make a class for a window and I'm not sure why this error shows up.
I've been also trying to add the functions and everything else manually into the "main.cpp" file to check if openGl is working, and it does, but when I try to make a openGl object, it's not working.
Maybe the problem it's easy to spot, but I can't figure it out.(sorry I'm just trying to learn here :3 )
Here is my code:
The header file
#pragma once
#include <iostream>
#include <GLFW/glfw3.h>
class Window {
private:
const char* p_title;
int p_width;
int p_height;
GLFWwindow* window;
public:
Window(const char* title, int width, int height);
~Window();
bool close();
void update();
private:
void init();
};
The .cpp file
#include "window .h"
Window::Window(const char* title, int width, int height)
{
p_title = title;
p_width = width;
p_height = height;
init();
}
Window::~Window()
{
glfwTerminate();
}
bool Window::close()
{
return glfwWindowShouldClose(window);
}
void Window::update()
{
glfwPollEvents();
glfwSwapBuffers(window);
}
void Window::init()
{
window = glfwCreateWindow(p_width, p_height, p_title, NULL, NULL);
if (!window) {
std::cout << "The window could not be created\n";
glfwTerminate();
}
glfwMakeContextCurrent(window);
}
If it helps, here is the debugger:

"Cannot open include file: 'vulkan/vulkan.h': No such file or directory" when static library is build as part of a solution in VS 2019

I'm currently experimenting with the vulkan API and have trouble building my solution with VS 2019.
The setup is as follows:
I have a multi-project solution with 2 projects: A static library called *Engine* and an application called *Game*.
External libs: vulkan, GLFW and GLM.
Dependencies are configured almost the same as described in [Vulkan Tutorial][1] with slight adjustments for the static lib, exact setup can be found at the end of this post on screenshots.
My problem is this:
When I build the Engine by itself, everything is fine and no errors occur.
If I build the whole solution and only #include "Engine.h" or "util.h" in "Game.cpp" (source file with main method in Game) then it works as well and if I execute it a window opens and the debug output is correct as well.
But if I #include "Configuration.h" in "Game.cpp" the error as stated in the title occurs.
The only difference I notice between the files is that "Engine.h" and "util.h" do not include any vulkan code but rather their source file counterparts "Engine.cpp" and "util.cpp" do, while "Configuration.h" contains #include "vulkan/vulkan.h".
Does anyone have any idea what might be the problem and how to fix it?
Code
Engine.h
#pragma once
namespace re
{
class REngine
{
public:
void init();
};
}
Engine.cpp
#include "Engine.h"
#define GLFW_INCLUDE_VULKAN
#include <GLFW/glfw3.h>
#define GLM_FORCE_RADIANS
#define GLM_FORCE_DEPTH_ZERO_TO_ONE
#include <glm/vec4.hpp>
#include <glm/mat4x4.hpp>
//std
#include <iostream>
namespace re {
void REngine::init()
{
#ifdef _DEBUG
std::cout << "Realms Engine begin initializing." << std::endl;
#endif // _DEBUG
glfwInit();
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
GLFWwindow* window = glfwCreateWindow(800, 600, "Vulkan window", nullptr, nullptr);
uint32_t extensionCount = 0;
vkEnumerateInstanceExtensionProperties(nullptr, &extensionCount, nullptr);
std::cout << extensionCount << " extensions supported\n";
glm::mat4 matrix;
glm::vec4 vec;
auto test = matrix * vec;
while (!glfwWindowShouldClose(window)) {
glfwPollEvents();
}
glfwDestroyWindow(window);
glfwTerminate();
}
}
Configuration.h
#pragma once
#include "vulkan/vulkan.h"
namespace re {
class RConfiguration
{
private:
VkApplicationInfo m_appInfo;
const int m_height;
const int m_width;
public:
RConfiguration(
const char* name,
int height, int width,
int major, int minor, int patch
);
const int get_width() { return m_width; }
const int get_height() { return m_height; }
VkApplicationInfo get_app_info() { return m_appInfo; }
};
}
Configuration.cpp
#include "Configuration.h"
namespace re {
RConfiguration::RConfiguration(const char* name, int height, int width, int major, int minor, int patch): m_height(height), m_width(width)
{
m_appInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
m_appInfo.pApplicationName = name;
m_appInfo.applicationVersion = VK_MAKE_VERSION(major, minor, patch);
m_appInfo.pEngineName = "Realms Engine";
m_appInfo.engineVersion = VK_MAKE_VERSION(1, 0, 0);
m_appInfo.apiVersion = VK_API_VERSION_1_2;
}
}
Game.cpp
#include <iostream>
#include "Engine.h"
#include "util.h"
#include "Configuration.h"
int main()
{
std::cout << "Hello World!\n";
re::REngine engine;
auto version = re::util::make_version(1, 2, 1);
std::cout << version << std::endl;
engine.init();
}
re::RConfiguration make_config() {
return re::RConfiguration(
"Realms in the Sky",
600,
800,
1, 0, 0
);
}
Project Setup
Engine
Game

SDL render rect from a class

I'm trying to render a SDL_Rect in a class, I send the renderer to the "draw" function of my class, and then render it from there.
void Tray::draw(SDL_Renderer *renderer){
SDL_RenderFillRect(renderer, &bckRect);
}
When I put the same code on my main loop. All goes fine, but when I do this from my tray class, It compiles, but doesn't run.
Simplified code:
main.cpp
#include <iostream>
#include <SDL2/SDL.h>
// local includes
#include "editor.h"
using namespace std;
int main(int argc, const char * argv[]) {
Editor *editor = new Editor();
while (editor->running){
editor->render();
}
editor->clean();
return 0;
}
editor.h
#ifndef EDITOR_H
#define EDITOR_H
#include <iostream>
#include <SDL2/SDL.h>
#include "Gui-classes/tray.h"
#include "properties.h"
class Editor{
private:
// Window variables
SDL_Window *window;
SDL_Renderer *renderer;
Tray *tray;
public:
Editor();
~Editor();
void render();
};
#endif
editor.cpp
#include "editor.h"
Editor::Editor(){
props = new Properties();
if (SDL_Init(SDL_INIT_EVERYTHING) == 0) {
window = SDL_CreateWindow("Great Editor", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1000, 600, SDL_WINDOW_RESIZABLE);
running = true;
if (window) {
std::cout << "Window created!" << std::endl;
}
renderer = SDL_CreateRenderer(window, -1, 0);
}
}
void Editor::render(){
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
SDL_RenderClear(renderer);
SDL_SetRenderDrawColor(renderer, 12, 12, 55, 255);
tray->draw(renderer);
SDL_RenderPresent(renderer);
}
void Editor::clean(){
SDL_DestroyWindow(window);
SDL_DestroyRenderer(renderer);
SDL_Quit();
std::cout << "Window ended" << std::endl;
}
tray.h
#ifndef TRAY_H
#define TRAY_H
#include <iostream>
#include <SDL2/SDL.h>
class Tray{
private:
// Position variables
int x;
int y;
int w;
int h;
//Display variables
SDL_Color color;
SDL_Rect bckRect;
public:
Tray();
~Tray();
void draw(SDL_Renderer *renderer);
};
#endif
Tray.cpp
#include "tray.h"
Tray::Tray(){
bckRect.x = x;
bckRect.x = y;
bckRect.w = w;
bckRect.h = h;
}
Tray::~Tray(){
}
void Tray::draw(SDL_Renderer *renderer){
SDL_RenderFillRect(renderer, &bckRect);
//std::cout << "teeest" << std::endl;
}
Thanks for the help.

Failing to create a window in GLFW C++

Some background if you care/if it helps. I am pretty knew to programming and C++, I tried to learn it a couple times before but couldn't stay interested. As I couldn't think of an interesting project to do as I learn. Eventually I thought of making a game engine(dumb idea for a noob, but it will be a good learning experience and I'm interested in doing it.), so I searched for a series on making one to get an idea and found TheChernoProject's series and here I am.
I'm trying to make a make a window in GLFW and C++ but it keeps failing, I have no idea what to do.
Main.cpp
#include "src\graphics\window.h"
int main() {
using namespace tmge;
using namespace graphics;
Window window("Test", 1280, 720);
while (!window.windowClosed()) {
window.update();
}
return 0;
}
Window.h
#pragma once
#include <GLFW\glfw3.h>
#include <iostream>
namespace tmge { namespace graphics {
class Window {
private:
GLFWwindow *privateWindow;
int privateWidth, privateHeight;
const char *privateTitle;
bool privateWindowClosed();
public:
Window(const char *title, int width, int height);
~Window();
bool init();
void update() const;
bool windowClosed() const;
};
} }
Window.cpp
#include "window.h"
namespace tmge { namespace graphics {
Window::Window(const char *title, int width, int height) {
privateTitle = title;
privateWidth = width;
privateHeight = height;
if (!init()) {
glfwTerminate();
}
}
Window::~Window() {
glfwTerminate();
}
bool Window::init() {
if (!glfwInit) {
std::cout << "GLFW initialaztion failed" << std::endl;
return false;
}
privateWindow = glfwCreateWindow(privateWidth, privateHeight, privateTitle, NULL, NULL);
if (!privateWindow) {
std::cout << "Failed to create GLFW window" << std::endl;
return false;
}
glfwMakeContextCurrent(privateWindow);
return true;
}
bool Window::windowClosed() const{
return glfwWindowShouldClose(privateWindow);
}
void Window::update() const{
glfwPollEvents();
glfwSwapBuffers(privateWindow);
}
} }