Skip to content

Instantly share code, notes, and snippets.

@cbmeeks
Last active February 11, 2020 21:22
Show Gist options
  • Save cbmeeks/5587a11e7856baf819b7 to your computer and use it in GitHub Desktop.
Save cbmeeks/5587a11e7856baf819b7 to your computer and use it in GitHub Desktop.
Having trouble calling glewInit() on a Mac? Try this...
// Take a look at the other file below.
// Basically, since OpenGL is a core feature of OSX, you don't need to include GLEW.
/*
So, for example, you can put a conditional include like so:
#ifdef __APPLE__
#include <OpenGL/gl3.h> /// remove the "3" for OpenGL versions < 3
#include <OpenGL/gl3ext.h> /// ditto
#else
#include <GL/glew.h>
#endif
...
Next, instead of calling glewInit(), you can conditionally leave it out like:
#ifndef __APPLE__
GLenum status = glewInit();
if (status != GLEW_OK) {
std::cerr << "GLEW failed to initialize!" << std::endl;
}
#endif
Finally, in your g++ compiler options (or, whatever you use) put:
-framework OpenGL
If you choose to use SDL2, download the runtime binaries and put them in your /Library/Frameworks folder. Then add:
-framework SDL2
to your compiler options like before.
I hope this helps someone out because it took me a while to figure out why I couldn't build a simple C++ application using
GLEW
Be sure to let me know what you think!
[email protected]
*/
/*
* File: Display.cpp
* Author: cbmeeks
*
* Created on November 26, 2014, 8:58 PM
*/
#include "Display.h"
#include <iostream>
#ifdef __APPLE__
#include <OpenGL/gl3.h>
#include <OpenGL/gl3ext.h>
#else
#include <GL/glew.h>
#endif
Display::Display(int width, int height, const std::string& title) {
// Init SDL
SDL_Init(SDL_INIT_EVERYTHING);
// Set color information (8 bits per color and alpha)
SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
// Set other attributes
SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, 32);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
m_window = SDL_CreateWindow(
title.c_str(),
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
width,
height,
SDL_WINDOW_OPENGL);
m_glContext = SDL_GL_CreateContext(m_window);
#ifndef __APPLE__
// Setup GLEW
GLenum status = glewInit();
if (status != GLEW_OK) {
std::cerr << "GLEW failed to initialize!" << std::endl;
}
#endif
m_isClosed = false;
}
Display::~Display() {
SDL_GL_DeleteContext(m_glContext);
SDL_DestroyWindow(m_window);
SDL_Quit();
}
bool Display::IsClosed() {
return m_isClosed;
}
void Display::Update() {
SDL_GL_SwapWindow(m_window);
SDL_Event e;
while (SDL_PollEvent(&e)) {
if (e.type == SDL_QUIT)
m_isClosed = true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment