Failing to create a window in GLFW C++ - 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);
}
} }

Related

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:

opengl window initialization issue

I'm trying to encapsulate GLFW in a class to make my code cleaner.
So in my class, I've made the basic window creation but when I execute the program, it always enter in the if statement where I check if the initialization went right or not and I don't know why.
This is what I have
#include "include/Window.h"
#include <iostream>
Window::Window(int x, int y) {
_window = glfwCreateWindow(x, y, "Heightmap Visualizer", NULL, NULL);
}
Window::~Window() {
glfwTerminate();
}
int Window::createWindow() {
if (!_window) {
std::cerr << "went here" << std::endl;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(_window);
run();
glfwTerminate();
return 0;
}
void Window::run() {
while (!glfwWindowShouldClose(_window)) {
glClear(GL_COLOR_BUFFER_BIT);
glfwSwapBuffers(_window);
glfwPollEvents();
}
}
This is the header file
#include <GLFW/glfw3.h>
class Window {
public:
Window(int x, int y);
~Window();
int createWindow();
void run();
private:
GLFWwindow* _window;
};
Make sure to glfwInit().
Also good luck in your jorney learning GL and C++!

std::cout << glGetString(GL_RENDER) << std::endl; Throws an Error but not GL_Renderer or GL_Verision and I can't figure out why?

First time asking a question on here so if it's not layed out correctly or something is missing I'll try to get everything else up.
I'm trying to learn OpenGl because I'm interested in developing games of my own and I would rather create my own engine than one already out there.
I'm using GLEW also and I know it inits because the code doesn't output an Error
I spend alittle while googling the error code and the OpenGL but none of the problems really match what I'm getting.
I also tried the GLEW_Experimental = true but that did not change anything.
Code: main
#include "src/graphics/window.h"
int main()
{
using namespace FutureGamingEngine;
using namespace graphics;
Window window("Test", 1080, 720);
glClearColor(0, 255, 0, 1.0f); //Red, Green, Blue, No Idea
std::cout << glGetString(GL_VERSION) << std::endl;
std::cout << glGetString(GL_RENDERER) << std::endl;
std::cout << glGetString(GL_RENDER) << std::endl;
while (!window.closed())
{
window.clear();
glBegin(GL_QUADS);
glVertex2f(-0.5f, -0.5f);
glVertex2f(-0.5f, 0.5f);
glVertex2f(0.5f, 0.5f);
glVertex2f(0.5f,-0.5f);
glEnd();
window.update();
}
//system("PAUSE");
return 0;
}
Window:
#include "window.h"
namespace FutureGamingEngine
{
namespace graphics
{
void windowResize(GLFWwindow *window, int width, int height);
Window::Window(const char *title, int width, int height)
{
c_title = title;
c_height = height;
c_width = width;
if (!init())
{
glfwTerminate();
}
}
Window::~Window()
{
glfwTerminate();
}
bool Window::init()
{
glewExperimental = true;
if (!glfwInit())
{
std::cout << "Error Code: 0" << std::endl;
return false;
}
//Create a windowed mode and it's OpenGl Content
c_window = glfwCreateWindow(c_width, c_height, c_title, NULL, NULL);
//If we fail to make the window Terminate OpenGL
if (!c_window)
{
glfwTerminate();
return false;
}
//std::cout << "Open GL: " << glGetString(GL_VERSION) << std::endl;
glfwMakeContextCurrent(c_window);
glfwSetWindowSizeCallback(c_window, windowResize);
if (glewInit() != GLEW_OK)
{
std::cout << "Error Code: 1" << std::endl;
return false;
}
return true;
}
bool Window::closed() const
{
return glfwWindowShouldClose(c_window);
}
void Window::update()
{
//poll for and process events
glfwPollEvents();
glfwGetFramebufferSize(c_window, &c_width, &c_height);
//swap front and backbuffers
glfwSwapBuffers(c_window);
}
void Window::clear() const
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}
void windowResize(GLFWwindow *window, int width, int height)
{
glViewport(0, 0, width, height);
}
}
}
The Header for window:
#pragma once
#define GLEW_STATIC
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <iostream>
namespace FutureGamingEngine
{
namespace graphics
{
class Window
{
private:
const char *c_title;
int c_width, c_height;
GLFWwindow *c_window;
bool c_closed;
public:
Window(const char *a_name, int a_width, int a_height);
~Window();
bool closed() const;
void update();
void clear() const;
inline int getHeight() const { return c_height; };
inline int getWidth() const { return c_width; };
private:
bool init();
};
}
}
I'm expecting it to tell me the Render Version. What I'm actually getting is Error Produced on std::cout << glGetString(GL_RENDER) << std::endl; :
Exception thrown at 0x7C50F6E0 (ucrtbased.dll) in FutureGamingEngine-Core.exe: 0xC0000005: Access violation reading location 0x00000000.
(https://i.imgur.com/uKu3hxX.png)
and the doesn't get to rendering the Triangle like it did prior to me asking for Gl_render
GL_RENDERER is valid enum, GL_RENDER is not. See OpenGL doc
Thank you Ripi2! I apperently did not look hard enough :D

Breaking up the SDL code

I am doing first examples from the "SDL Game Development Black and White" book.
The point where I have problems is in example where I should break "HelloSDL" code in one header( .h) and two cpp( .cpp) files.
Here they are:
main.cpp
#include "Game.h"
#include <stdio.h>
#include <SDL2/SDL.h>
Game* g_game = 0;
int main(int argc, char* args[])
{
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();
void Game::handleEvents()
{
SDL_Event event;
if(SDL_PollEvent(&event))
{
switch (event.type)
{
case SDL_QUIT:
m_bRunning = false;
break;
default;
break;
}
}
}
return 0;
}
Game.h
#ifndef GAME_H_INCLUDED
#define GAME_H_INCLUDED
#include <SDL2/SDL.h>
class Game
{
public:
Game();
~Game();
void init();
void render();
void update();
void handleEvents();
void clean();
bool running() { return m_bRunning; }
private:
SDL_Window* m_pWindow;
SDL_Renderer* m_pRenderer;
bool m_bRunning;
};
#endif // GAME_H_INCLUDED
Game.cpp
#include <iostream>
#include "Game.h"
//#include <stdio.h>
#include <SDL2/SDL.h>
bool Game::init(const char* title, int xpos, int ypos, int width, int height, int flags)
{
if(SDL_Init(SDL_INIT_EVERYTHING) == 0)
{
std::cout << "SDL init success\n";
m_pWindow = SDL_CreateWindow(title, xpos, ypos, width, height, flags);
if(m_pWindow != 0)
{
std::cout << "window creation success\n";
m_pRenderer = SDL_CreateRenderer(m_pWindow, -1, 0);
if(m_pRenderer != 0)
{
std::cout << "renderer creation success\n";
SDL_SetRenderDrawColor(m_pRenderer, 255 255, 255, 255);
}
else
{
std::cout << "renderer init fail\n";
return false;
}
}
else
{
std::cout << "window init fail\n";
return false;
}
}
else
{
std::cout << "SDL init fail\n";
return false;
}
std::cout << "init success\n";
m_bRunning = true;
return true;
}
void Game::render()
{
SDL_RenderClear(m_pRenderer);
SDL_RenderPresent(m_pRenderer);
}
SDL_Quit()
+{
std::cout << "cleaning game\n";
SDL_DestroyWindow(m_pWindow);
SDL_DestroyRenderer(m_pRenderer);
SDL_Quit();
}
void Game::clean()
{
std::cout << "cleaning game\n";
SDL_DestroyWindow(m_pWindow);
SDL_DestroyRenderer(m_pRenderer);
SDL_Quit();
}
After compiling it in CodeBlocks(16.11) I get next error messages:
Game.cpp|6|error: prototype for 'bool Game::init(const char*, int, int, int, int, int)' does not match any in class 'Game'
Game.h|12|error: candidate is: void Game::init()
Firstly, I got much more errors but I managed to solve it.
There is still two more errors to solve.
I suppose that is something wromg in Game.h file, but I cannot find it.
Thank you in advance!
The init() declared in the game.h returns void, but the definition you provide returns bool.
It seems that you may be coming from C background. In C++ omitting function signature at function declaration is not permitted. Basically by declaring void init(); you declare void Game::init(void) function, not bool Game::init(const char* title, int xpos, int ypos, int width, int height, int flags). So one must carefully check that function declaration does exactly match function definition. Another thing is that in C++ one should perform object initialization at constructor and cleanup at destructor.

GLEW _First was nullptr

So I have been working on a game through C++ with GLFW and GLEW and everything was going fine until I implemented GLEW then I get an error that I have never encountered before and I don't have the foggiest on what to do, I have googled it and done some research but to no avail. I have noticed that in the stack frame section it says something to do with this line of code in my main.cpp
std::cout << glGetString(GL_VERSION) << std::endl;
Also it says something to do with memory. I'll leave the rest of my code down below and if there's any info you need just ask and I will try my best to provide it
So I just discovered if i take out
std::cout << glGetString(GL_VERSION) << std::endl;
then it works however the window isn't created.
Where do I go from here?
Any idea?
#include "src\graphics\window.h"
int main() {
using namespace benji;
using namespace graphics;
Window window("Benji Engine", 1280, 720);
glClearColor(0.2f, 0.3f, 0.8f, 1.0f);
std::cout << glGetString(GL_VERSION) << std::endl;``
while (!window.closed()) {
std::cout << window.getWidth() << ", " << window.getHeight() << std::endl;
window.clear();
glBegin(GL_TRIANGLES);
glVertex2f(-0.5f, -0.5f);
glVertex2f(0.0f, 0.5f);
glVertex2f(0.5f, -0.5f);
glEnd();
window.update();
}
return 0;
}
main.h
#pragma once
class main
{
public:
main();
~main();
};
window.cpp
#include "window.h"
namespace benji { namespace graphics {
void windowResize(GLFWwindow *window, int width, int height);
Window::Window(const char *title, int width, int height) {
m_Title = title;
m_Width = width;
m_Height = height;
if (!init()) {
glfwTerminate();
}
}
Window::~Window() {
glfwTerminate();
}
bool Window::init() {
if (!glfwInit()) {
std::cout << "Failed to initialize GLFW!" << std::endl;
return false;
}
m_Window = glfwCreateWindow(m_Width, m_Height, m_Title, NULL, NULL);
if (!m_Window) {
std::cout << "Failed to create GLFW window!" << std::endl;
return false;
}
glfwMakeContextCurrent(m_Window);
glfwSetWindowSizeCallback(m_Window, windowResize);
if (glewInit != GLEW_OK) {
std::cout << "GLEW FAILED!" << std::endl;
return false;
}
return true;
}
void Window::clear() const {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}
void Window::update(){
glfwPollEvents();
glfwSwapBuffers(m_Window);
}
bool Window::closed() const {
return glfwWindowShouldClose(m_Window) == 1;
}
void windowResize(GLFWwindow *window, int width, int height) {
glViewport(0, 0, width, height);
}
}}
window.h
#pragma once
#include <iostream>
#include <GL\glew.h>
#include <GLFW\glfw3.h>
namespace benji {
namespace graphics {
class Window {
private:
const char *m_Title;
int m_Width, m_Height;
GLFWwindow *m_Window;
bool m_Closed;
public:
Window(const char *title, int width, int height);
~Window();
bool closed() const;
void update();
void clear() const;
int getWidth() const {
return m_Width;
}
int getHeight() const { return m_Height; }
private:
bool init();
};
}
}
In Window.cpp:
if (glewInit != GLEW_OK) {
std::cout << "GLEW FAILED!" << std::endl;
return false;
}
glewInit() is a function, not a variable. You need to call it as a function. I'm surprised this even compiled.
All other OpenGL functions that come from after version 1.1 will throw errors to the effect of ACCESS_VIOLATION READING ADDRESS 0x00000000 or some similar error, because if glewInit() is not properly called, none of the function macros provided by GLEW will point to valid function pointers.