Hello everyone
I would like to know how do I create class with glew
If you know how do I fix Access Violation executing location?
I made DisplayManager for SDL2 + GLEW in C++
DisplayManager.h
#include "SDL2/SDL.h"
#include "gl/glew.h"
class DisplayManager
{
public:
static DisplayManager* Instance()
{
return s_instance = (s_instance != nullptr) ? s_instance : new DisplayManager();
}
bool Create(const char* title);
void Make();
bool IsRunning();
void HandleEvent();
void Prepare();
void Swap();
void Destroy();
private:
DisplayManager(){}
static DisplayManager* s_instance;
bool isRunning = true;
int WIDTH = 1280;
int HEIGHT = 820;
SDL_Window* Window;
SDL_GLContext Context;
GLenum error;
SDL_Event evt;
};
DisplayManager.cpp
#include "DisplayManager.h"
DisplayManager* DisplayManager::s_instance = nullptr;
bool DisplayManager::Create(const char* title)
{
bool success = true;
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_FORWARD_COMPATIBLE_FLAG);
if (SDL_Init(SDL_INIT_EVERYTHING))
{
printf("Error: Initializing SDL2 \n");
success = false;
}
else
{
Window = SDL_CreateWindow(title, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, WIDTH, HEIGHT, SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE);
if (!Window)
{
printf("Error: Creating Window %s \n", SDL_GetError());
success = false;
}
else
{
Context = SDL_GL_CreateContext(Window);
if (!Context)
{
printf("Error: Creating Context %s \n", SDL_GetError());
success = false;
}
else
{
glewExperimental = GL_TRUE;
glewInit();
SDL_GL_SetSwapInterval(1);
}
}
}
return success;
}
void DisplayManager::Make()
{
SDL_GL_MakeCurrent(Window, Context);
}
bool DisplayManager::IsRunning()
{
return isRunning;
}
void DisplayManager::HandleEvent()
{
while (SDL_PollEvent(&evt))
{
if (evt.type == SDL_QUIT)
{
isRunning = false;
}
if (evt.type == SDL_KEYDOWN)
{
if (evt.key.keysym.sym == SDLK_ESCAPE)
{
isRunning = false;
}
}
if (evt.type == SDL_WINDOWEVENT)
{
if (evt.window.event == SDL_WINDOWEVENT_SIZE_CHANGED)
{
WIDTH = evt.window.data1;
HEIGHT = evt.window.data2;
glViewport(0, 0, evt.window.data1, evt.window.data2);
}
}
}
}
void DisplayManager::Prepare()
{
glClearColor(1, 1 * 0.35, 0, 1);
glClear(GL_COLOR_BUFFER_BIT);
}
void DisplayManager::Swap()
{
SDL_GL_SwapWindow(Window);
}
void DisplayManager::Destroy()
{
SDL_GL_DeleteContext(Context);
SDL_DestroyWindow(Window);
SDL_Quit();
}
And I add VertexArrayObject class for glGenVertexArrays()
VertexArrayObject.h
#include <gl/glew.h>
class VertexArrayObject
{
public:
VertexArrayObject();
void Bind();
void Unbind();
void Delete();
private:
unsigned int vaoID;
};
VertexArrayObject.cpp
#include "VertexArrayObject.h"
VertexArrayObject::VertexArrayObject()
{
glGenVertexArrays(1, &vaoID);
}
void VertexArrayObject::Bind()
{
glBindVertexArray(vaoID);
}
void VertexArrayObject::Unbind()
{
glBindVertexArray(0);
}
void VertexArrayObject::Delete()
{
glDeleteVertexArrays(1, &vaoID);
}
and I write main.cpp
#include "DisplayManager.h";
#include "VertexArrayObject.h"
VertexArrayObject* vao;
// For Vertex Buffer Object - I will write later I know that.. I write later
// It is simple triangle
float vertices[] =
{
0.0, 0.5, 0.0, // top
0.5, -0.5, 0.9, // botton right
-0.5, -0.5, 0.0 // bottom left
};
int main(int argv, char* argc[])
{
if (!DisplayManager::Instance()->Create("Learning with SDL2 + GLEW - 02 Triangle"))
{
printf("Error: It could not initialize...");
}
else
{
// Making context with gl functions
vao = new VertexArrayObject();
vao->Bind();
while (DisplayManager::Instance()->IsRunning())
{
// Handle events
DisplayManager::Instance()->HandleEvent();
// Rendering
DisplayManager::Instance()->Prepare();
// Swapping
DisplayManager::Instance()->Swap();
}
}
// Destroying and closing
vao->Delete();
DisplayManager::Instance()->Destroy();
return 0;
}
This is not finish because I checked glGenVertexArrays() if it throws exception Access Violation.
Like I have learned LearningOpenGL already but I don’t understand why does exception throw? I thought Java programming is very simple like Loader.java by ThinMatrix he wrote:
example Loader.java
class Loader
{
.....
pzublic RawModel LoadToVAO(int vao_ID, int vertex_counts)
{
vao_ID = glGenVertexArray() ( Custom getter of glGenVertexArrays(1, &vao_id);
....
}
.....
}
But I made VertexArrayObject class but iot doesn’t work for me. What is wrong with glGenVertexArrays()
Sorry my bad English.
I hope who can fix Access Violation executing location?