Source

dbsdev / sources / opengl2_samples / test7_simplerenderer / glfw_utils.cpp

Full commit

#include "glfw_utils.h"

#include "GLCamera.h"


// ///////// //
// CALLBACKS //
// ///////// //

void GLFWCALL windowResize( int w, int h );
void GLFWCALL mousePos(int x, int y);
void GLFWCALL mouseButton( int button, int action );
void GLFWCALL mouseWheel( int pos );
void GLFWCALL keyCallback(int key, int state);
void GLFWCALL charCallback(int key, int state);



// masked global variables
namespace 
{
    // clipping volume (left, right, bottom, top, near, far)
    float clipping[] = {-100.0, 100.0, -100.0, 100.0, -100.0, 100.0};

    ContextData context;

    GLCamera *camera=NULL;
}


// ////////////// //
// INITIALIZATION //
// ////////////// //

/// initialize glfw session
bool glfw_Init(int width, int height, int mode)
{
    // Initialize GLFW
    if( !glfwInit() )
        return false;

    // Open an OpenGL window
    if( !glfwOpenWindow( width,height, 8,8,8,8,24,8, mode ) )
    {
        glfwTerminate(); 
        return false;
    }
    // // init glew extension wrangler
    // GLenum err = glewInit();
    // if (GLEW_OK != err)
    // {
    //     // Problem: glewInit failed, something is seriously wrong. 
    //     printf("GLEW Error: %s\n", glewGetErrorString(err));
    //     glfwTerminate(); 
    //     return false;
    // }
    // printf("Status: Using GLEW %s\n", glewGetString(GLEW_VERSION));

    // set callback functions
    glfwSetWindowSizeCallback(windowResize);
    glfwSetMousePosCallback(mousePos);
    glfwSetMouseButtonCallback(mouseButton);
    glfwSetMouseWheelCallback(mouseWheel);
    glfwSetKeyCallback(keyCallback);
    glfwSetCharCallback(charCallback);

    glfwSwapInterval(0);  // vsync

    // update global variables
    context.width = width;
    context.height = height; 

    context.dragging = false;

    return true;
}


/// terminate the glfw session
void glfw_terminate( void )
{
    glfwTerminate();
}


/// set the current camera
void glfw_setCamera( GLCamera *cam )
{
    camera = cam;

    if (camera)
    {
        camera->aspectRatio = float(context.width)/context.height;
        camera->updateProjectionMatrix();
    }

}


// access context data
ContextData& glfw_getContextData( void )
{
    return context;
}

// ///////// //
// CALLBACKS //
// ///////// //

void GLFWCALL windowResize( int w, int h )
{
    // Prevent a divide by zero
    if(h == 0)
        h = 1;
		
    // Set Viewport to window dimensions
    glViewport(0, 0, w, h);

    if (camera)
    {
        camera->aspectRatio = float(w)/h;
        camera->updateProjectionMatrix();
    }
	
    // update global variables
	context.width = w;
	context.height = h;    

}


void GLFWCALL mousePos(int x, int y)
{
    context.mouse_posx = x;
    context.mouse_posy = context.height-y;

    context.dragging = (context.mouse_button==MOUSE_BUTTON_LEFT);
}


void GLFWCALL mouseButton( int button, int action )
{
    if (action==GLFW_PRESS)
    {
        context.mouse_button = MOUSE_BUTTON_LEFT;
        if (button==GLFW_MOUSE_BUTTON_MIDDLE)
            context.mouse_button = MOUSE_BUTTON_CENTER;
        if (button==GLFW_MOUSE_BUTTON_RIGHT)
            context.mouse_button = MOUSE_BUTTON_RIGHT;
    }
    else
    {
        context.mouse_button = 0;
        context.dragging = false;
    }
}

void GLFWCALL mouseWheel( int pos )
{
    static int lastval = pos;
    context.mouse_wheel = lastval-pos;
    lastval = pos;
}

void GLFWCALL keyCallback(int key, int state)
{
    // standard keys
    if(state==GLFW_PRESS)
    {    
        if (key == GLFW_KEY_BACKSPACE)
            context.current_key = (char)KEY_BACKSPACE;
        else if (key == GLFW_KEY_DEL)
            context.current_key = (char)KEY_DELETE;
        else if (key == GLFW_KEY_TAB)
            context.current_key = (char)KEY_TAB;
        else if (key == GLFW_KEY_ENTER)
            context.current_key = (char)KEY_ENTER;
    }
}

void GLFWCALL charCallback(int key, int state)
{
    // standard keys
    if(state==GLFW_PRESS)
    {    
        if (key >= 32 && key < 127)
            context.current_key = (char)key;
    }
    else
    {
        context.current_key = (char)0;
    }
}