Thanks FonzTech. I checked and yes, “std::cout << glGenBuffers << std::endl;” is printing a 0! But I’m initializing glew already in my first class, MainGame.cpp. I added the “SDL_GL_MakeCurrent(_window, _glContext);” line though, to no avail.
So somehow I have to make my Sprite.cpp file see the GL context created in my MainGame.cpp file. The context and window get created just fine in MainGame. So out of desperation I passed the context and window to Sprite and added this to Sprite.cpp:
std::cout << SDL_GL_MakeCurrent(window, context) << std::endl;
SDL_GL_MakeCurrent returns 0. So that’s good. glGenBuffers still crashes when I don’t do the cout you suggested though.
You mentioned something about using correct SDL flags. Can you elaborate please?
Here’s my renderer and OpenGL info:
Renderer: AMD Radeon HD 7800 Series
OpenGL: Version 4.4
OpenGL Driver version: 16.150.211.0 (pressing the “check for updated drivers” link in GLView leads to a server timeout page… every time.)
EDIT: I might as well just throw my whole code in there for you guys to see:
MainGame.cpp
#include "MainGame.h"
#include <iostream>
MainGame::MainGame()
{
_window = 0;
_SCREEN_WIDTH = 1024;
_SCREEN_HEIGHT = 768;
_gameState = PLAY;
};
MainGame::~MainGame()
{
};
void fatalError(std::string errorString)
{
std::cout << errorString << std::endl;
std::cout << "Enter any key to quit..." << std::endl;
int tmp;
std::cin >> tmp;
// SDL_QUIT;
}
void MainGame::run()
{
MainGame::initSystems();
MainGame::gameLoop();
};
void MainGame::initSystems()
{ //initialize SDL
SDL_Init(SDL_INIT_EVERYTHING);
_window = SDL_CreateWindow("Game Engine", SDL_WINDOWPOS_CENTERED
,SDL_WINDOWPOS_CENTERED, _SCREEN_WIDTH
, _SCREEN_HEIGHT, SDL_WINDOW_OPENGL);
if (_window == 0)
fatalError("SDL Window could not be opened.");
SDL_GLContext _glContext = SDL_GL_CreateContext(_window);
SDL_GL_MakeCurrent(_window, _glContext);
if(_glContext == 0)
fatalError("GL Context could not be created.");
glewExperimental = GL_TRUE;
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
GLenum glewError = glewInit();
if (glewError != GLEW_OK)
fatalError("Could not initialize glew.");
else
std::cout << "initialized glew
";
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
glClearColor(0.0f, 0.0f, 1.0f, 0.0f);
_sprite.init(-1, -1, 1, 1, _window, _glContext);
};
void MainGame::processInput()
{
SDL_Event evnt;
while (SDL_PollEvent(&evnt) == true)
{
switch(evnt.type)
{
case SDL_QUIT:
_gameState = EXIT;
case SDL_MOUSEMOTION:
std::cout << evnt.motion.x << " " << evnt.motion.y << "
";
}
}
};
void MainGame::drawGame()
{
glClearDepth(1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}
void MainGame::gameLoop()
{
while(_gameState != EXIT)
{
drawGame();
processInput();
SDL_GL_SwapWindow(_window);
}
};
Sprite.cpp:
#include "Sprite.h"
Sprite::Sprite()
{
_vboID = 0;
}
Sprite::~Sprite()
{
if(_vboID != 0)
glDeleteBuffers(1, &_vboID);
}
void Sprite::init(float x, float y, float w, float h, SDL_Window *window, SDL_GLContext context) //, SDL_GLContext *context, SDL_Window* window)
{
_x = x;
_y = y;
_w = w;
_h = h;
//TODO: Why doesn't this class see the GL context? Research scope.
std::cout << "MakeCurrent result: " << SDL_GL_MakeCurrent(window, context) << std::endl;
// std::cout << "Worked. Using version: " << SDL_GL_CONTEXT_MAJOR_VERSION << "." << SDL_GL_CONTEXT_MINOR_VERSION << std::endl;
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
if (_vboID == 0)
{
// std::cout << glGenBuffers << std::endl;
glGenBuffers(1, &_vboID);
std::cout << "gen'd buffers!
";
}
std::cout <<"got past that";
float vertData[12]; //12-- 6 (x,y) coordinates
vertData[0] = x + w;
vertData[1] = y + h;
vertData[2] = x;
vertData[3] = y + h;
vertData[4] = x;
vertData[5] = y;
vertData[6] = x;
vertData[7] = y;
vertData[8] = x + w;
vertData[9] = y;
vertData[10] = x + w;
vertData[11] = y + h;
glBindBuffer(GL_ARRAY_BUFFER, _vboID);
//STATIC_DRAW tells the GPU we're only going to draw something once.
glBufferData(GL_ARRAY_BUFFER, sizeof(vertData), vertData, GL_STATIC_DRAW);
//You can free that G-RAM
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
void Sprite::draw()
{
glBindBuffer(GL_ARRAY_BUFFER, _vboID);
//give only basic position, sending only 1 vertex attrib array
glEnableVertexAttribArray(0);
//tells OpenGl where the data is, where to start, size, etc.
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, 0);
//triangles, squares, etc. determines how GPU draws and fills in vertices
//You can put 6 below because you told OpenGL that each vertex is 2 elements above. So It knows 12 elements in the array is 6 points
glDrawArrays(GL_TRIANGLES, 0, 6);
glDisableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}