Skip to content

Commit 144be79

Browse files
committed
Merge pull request #59 from Zardoz89/develop
Fix OSX and extra stuff . Close issue #58
2 parents 2487f03 + 971ac85 commit 144be79

File tree

6 files changed

+135
-92
lines changed

6 files changed

+135
-92
lines changed

cmake/Platform.cmake

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ if (CMAKE_HOST_APPLE)
66
/usr/local
77
/opt/local
88
)
9+
INCLUDE_DIRECTORIES(/usr/include)
10+
INCLUDE_DIRECTORIES(/usr/local/include)
911
else (CMAKE_HOST_APPLE)
1012
# OS X is a Unix, but it's not a normal Unix as far as search paths go.
1113
if (CMAKE_HOST_UNIX)

tools/include/al_engine.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,12 @@
1616
#ifdef __APPLE__
1717
#include <OpenAL/al.h>
1818
#include <OpenAL/alc.h>
19+
#include <OpenAL/alure.h>
1920
#else
2021
#include <AL/al.h>
2122
#include <AL/alc.h>
23+
#include <AL/alure.h>
2224
#endif
23-
#include <AL/alure.h> // Check if in MacOS or Win needs other stuff here
2425

2526

2627
#include "types.hpp"

tools/include/gl_engine.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class GlEngine {
2727

2828
yaw = 0;
2929
pith = 0;
30-
zoom = 4.7;
30+
zoom = 4.7f;
3131

3232
frame_count = 0;
3333
t_acu = 0;

tools/include/os.hpp

Lines changed: 58 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -37,65 +37,38 @@ namespace OS {
3737

3838
~OS() { }
3939

40-
bool InitializeWindow(const int width, const int height, const std::string title, const unsigned int glMajor = 3, const unsigned int glMinor = 2) {
41-
assert(glMajor >= 4 || (glMajor == 3 && glMinor >= 2));
42-
std::string glcx_major = "1";
43-
std::string glcx_version;
40+
bool InitializeWindow(const int width, const int height, const std::string title) {
4441
glfwSetErrorCallback(ErrorCallback);
4542

43+
this->title = title;
4644
// Initialize the library.
4745
if (glfwInit() != GL_TRUE) {
46+
std::cerr << "Can't start GLFW\n";
4847
return false;
4948
}
50-
49+
#ifdef __APPLE__
50+
// Try to grab latest OpenGL version on OSX
51+
// Source : http://antongerdelan.net/opengl/hellotriangle.html
5152
glfwWindowHint(GLFW_CLIENT_API, GLFW_OPENGL_API);
52-
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, glMajor);
53-
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, glMinor);
53+
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
54+
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
5455
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
56+
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
57+
#endif
58+
glfwWindowHint (GLFW_SAMPLES, 4);
5559
// Create a windowed mode window and its OpenGL context.
5660
this->window = glfwCreateWindow(width, height, title.c_str(), NULL, NULL);
57-
if (this->window) {
58-
// Make the window's context current.
59-
glfwMakeContextCurrent(this->window);
60-
61-
glcx_version = (char*)glGetString(GL_VERSION);
62-
glcx_major = glcx_version.substr(0, glcx_version.find('.', 0));
61+
if (!this->window) {
62+
glfwTerminate();
63+
std::cerr << "Can't open a window with GLFW\n";
64+
return false;
6365
}
6466

65-
if (glcx_major == "1" || glcx_major == "2") {
66-
std::clog << "Trying again to geta valid OpenGL context\n";
67-
// we got a old version context or failed. So try again.
68-
if (this->window) {
69-
glfwMakeContextCurrent(nullptr);
70-
glfwDestroyWindow(this->window);
71-
}
72-
// Try again, enforcing Core profile
73-
glfwWindowHint(GLFW_CLIENT_API, GLFW_OPENGL_API);
74-
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, glMajor);
75-
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, glMinor);
76-
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
77-
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
78-
this->window = glfwCreateWindow(width, height, title.c_str(), NULL, NULL);
79-
80-
if (!this->window) {
81-
glfwTerminate();
82-
return false;
83-
}
84-
// attach the context
85-
glfwMakeContextCurrent(this->window);
86-
87-
// check the context version again
88-
glcx_version = (char*)glGetString(GL_VERSION);
89-
glcx_major = glcx_version.substr(0, glcx_version.find('.', 0));
90-
if(glcx_major == "1") {
91-
// still 1, higher versions probably not supported
92-
glfwTerminate();
93-
std::cerr << "Initializing OpenGL failed, unsupported version: " << glcx_version << '\n';
94-
std::cerr << "Press \"Enter\" to exit\n";
95-
std::cin.get();
96-
return false;
97-
}
98-
}
67+
// Make the window's context current.
68+
glfwMakeContextCurrent(this->window);
69+
70+
71+
9972
this->width = width;
10073
this->height = height;
10174

@@ -104,18 +77,26 @@ namespace OS {
10477
id cocoaWindow = glfwGetCocoaWindow(this->window);
10578
id cocoaGLView = ((id (*)(id, SEL)) objc_msgSend)(cocoaWindow, sel_getUid("contentView"));
10679
((void (*)(id, SEL, bool)) objc_msgSend)(cocoaGLView, sel_getUid("setWantsBestResolutionOpenGLSurface:"), false);
107-
10880
#else
10981
// setting glewExperimental fixes a glfw context problem
11082
// (tested on Ubuntu 13.04)
11183
glewExperimental = GL_TRUE;
11284
// Init GLEW.
11385
GLuint error = glewInit();
11486
if (error != GLEW_OK) {
115-
return false;
87+
std::cerr << "Can't initialize glew\n";
88+
glfwTerminate();
89+
return false;
11690
}
11791
#endif
11892

93+
std::string gl_version, gl_renderer;
94+
gl_version = (char*)glGetString(GL_VERSION);
95+
gl_renderer = (char*)glGetString(GL_RENDERER);
96+
97+
std::cout << "Renderer: " << gl_renderer << "\n";
98+
std::cout << "OpenGL Version: " << gl_version << "\n";
99+
119100
// Associate a pointer for this instance with this window.
120101
glfwSetWindowUserPointer(this->window, this);
121102

@@ -190,7 +171,9 @@ namespace OS {
190171
OS* os = static_cast<OS*>(glfwGetWindowUserPointer(window));
191172
if (os) {
192173
if (width != os->width || height != os->height) {
193-
glfwSetWindowSize(window, os->width, os->height);
174+
//glfwSetWindowSize(window, os->width, os->height);
175+
os->UpdateWindowSize(width, height);
176+
glViewport(0, 0, width, height);
194177
}
195178
}
196179
}
@@ -286,6 +269,30 @@ namespace OS {
286269
return false;
287270
}
288271

272+
/**
273+
* Display FPS on title
274+
*/
275+
void UpdateCounter () {
276+
static double previous_seconds = glfwGetTime ();
277+
static int frame_count;
278+
double current_seconds = glfwGetTime ();
279+
double elapsed_seconds = current_seconds - previous_seconds;
280+
if (elapsed_seconds > 0.25) {
281+
previous_seconds = current_seconds;
282+
double fps = (double)frame_count / elapsed_seconds;
283+
char tmp[128];
284+
#if defined(_MSC_VER)
285+
// VC++ C compiler support : C89 thanks microsoft !
286+
_snprintf (tmp, 128, "%s @ fps: %.2f", this->title.c_str(), fps);
287+
#else
288+
snprintf(tmp, 128, "%s @ fps: %.2f", this->title.c_str(), fps);
289+
#endif
290+
glfwSetWindowTitle (this->window, tmp);
291+
frame_count = 0;
292+
}
293+
frame_count++;
294+
}
295+
289296
private:
290297

291298
/**
@@ -323,6 +330,7 @@ namespace OS {
323330
double lastTime; // The time at the last call to GetDeltaTime().
324331
bool mouseLock; // If mouse lock is enabled causing the cursor to snap to mid-window each movement event.
325332

333+
std::string title;
326334
event::KeyboardInputSystem KeyboardEventSystem;
327335
};
328336

0 commit comments

Comments
 (0)