2013-11-19 02:38:44 +04:00
|
|
|
/*********************************************************************************************
|
|
|
|
*
|
2013-11-23 16:30:54 +04:00
|
|
|
* raylib.core
|
2013-11-19 02:38:44 +04:00
|
|
|
*
|
2013-11-23 16:30:54 +04:00
|
|
|
* Basic functions to manage Windows, OpenGL context and Input
|
|
|
|
*
|
|
|
|
* Uses external lib:
|
|
|
|
* GLFW3 - Window, context and Input management (static lib version)
|
2013-11-19 02:38:44 +04:00
|
|
|
*
|
2013-11-23 16:30:54 +04:00
|
|
|
* Copyright (c) 2013 Ramon Santamaria (Ray San - raysan@raysanweb.com)
|
|
|
|
*
|
|
|
|
* This software is provided "as-is", without any express or implied warranty. In no event
|
|
|
|
* will the authors be held liable for any damages arising from the use of this software.
|
2013-11-19 02:38:44 +04:00
|
|
|
*
|
2013-11-23 16:30:54 +04:00
|
|
|
* Permission is granted to anyone to use this software for any purpose, including commercial
|
|
|
|
* applications, and to alter it and redistribute it freely, subject to the following restrictions:
|
2013-11-19 02:38:44 +04:00
|
|
|
*
|
2013-11-23 16:30:54 +04:00
|
|
|
* 1. The origin of this software must not be misrepresented; you must not claim that you
|
|
|
|
* wrote the original software. If you use this software in a product, an acknowledgment
|
|
|
|
* in the product documentation would be appreciated but is not required.
|
2013-11-19 02:38:44 +04:00
|
|
|
*
|
2013-11-23 16:30:54 +04:00
|
|
|
* 2. Altered source versions must be plainly marked as such, and must not be misrepresented
|
|
|
|
* as being the original software.
|
2013-11-19 02:38:44 +04:00
|
|
|
*
|
2013-11-23 16:30:54 +04:00
|
|
|
* 3. This notice may not be removed or altered from any source distribution.
|
2013-11-19 02:38:44 +04:00
|
|
|
*
|
|
|
|
**********************************************************************************************/
|
|
|
|
|
|
|
|
#include "raylib.h"
|
|
|
|
|
2013-11-23 16:30:54 +04:00
|
|
|
#include <GLFW/glfw3.h> // GLFW3 lib: Windows, OpenGL context and Input management
|
|
|
|
//#include <GL/gl.h> // OpenGL functions (GLFW3 already includes gl.h)
|
|
|
|
#include <stdio.h> // Standard input / output lib
|
2013-12-19 15:08:06 +04:00
|
|
|
#include <stdlib.h> // Declares malloc() and free() for memory management, rand()
|
|
|
|
#include <time.h> // Useful to initialize random seed
|
2014-03-16 23:59:02 +04:00
|
|
|
#include <math.h> // Math related functions, tan() used to set perspective
|
2013-11-23 16:30:54 +04:00
|
|
|
#include "vector3.h" // Basic Vector3 functions
|
2014-01-23 15:36:18 +04:00
|
|
|
#include "utils.h" // WritePNG() function
|
2013-11-19 02:38:44 +04:00
|
|
|
|
2013-11-23 16:30:54 +04:00
|
|
|
//#define GLFW_DLL // Using GLFW DLL on Windows -> No, we use static version!
|
2013-11-19 02:38:44 +04:00
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
// Defines and Macros
|
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
// Nop...
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
// Types and Structures Definition
|
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
typedef Color pixel;
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
// Global Variables Definition
|
|
|
|
//----------------------------------------------------------------------------------
|
2013-11-23 16:30:54 +04:00
|
|
|
static GLFWwindow* window; // Main window
|
|
|
|
static bool fullscreen; // Fullscreen mode track
|
2013-11-19 02:38:44 +04:00
|
|
|
|
2013-11-23 16:30:54 +04:00
|
|
|
static double currentTime, previousTime; // Used to track timmings
|
|
|
|
static double updateTime, drawTime; // Time measures for update and draw
|
|
|
|
static double frameTime; // Time measure for one frame
|
|
|
|
static double targetTime = 0; // Desired time for one frame, if 0 not applied
|
2013-11-19 02:38:44 +04:00
|
|
|
|
2013-11-23 16:30:54 +04:00
|
|
|
static int windowWidth, windowHeight; // Required to switch between windowed/fullscren mode (F11)
|
2013-12-19 15:08:06 +04:00
|
|
|
static const char *windowTitle; // Required to switch between windowed/fullscren mode (F11)
|
|
|
|
static int exitKey = GLFW_KEY_ESCAPE;
|
|
|
|
|
|
|
|
static bool customCursor = false; // Tracks if custom cursor has been set
|
|
|
|
static bool cursorOnScreen = false; // Tracks if cursor is inside client area
|
|
|
|
static Texture2D cursor; // Cursor texture
|
2013-11-19 02:38:44 +04:00
|
|
|
|
2013-11-28 22:59:56 +04:00
|
|
|
static char previousKeyState[512] = { 0 }; // Required to check if key pressed/released once
|
|
|
|
static char currentKeyState[512] = { 0 }; // Required to check if key pressed/released once
|
|
|
|
|
|
|
|
static char previousMouseState[3] = { 0 }; // Required to check if mouse btn pressed/released once
|
|
|
|
static char currentMouseState[3] = { 0 }; // Required to check if mouse btn pressed/released once
|
|
|
|
|
|
|
|
static char previousGamepadState[32] = {0}; // Required to check if gamepad btn pressed/released once
|
|
|
|
static char currentGamepadState[32] = {0}; // Required to check if gamepad btn pressed/released once
|
|
|
|
|
2014-01-29 00:21:29 +04:00
|
|
|
static int previousMouseWheelY = 0;
|
|
|
|
static int currentMouseWheelY = 0;
|
|
|
|
|
2013-11-19 02:38:44 +04:00
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
// Other Modules Functions Declaration (required by core)
|
|
|
|
//----------------------------------------------------------------------------------
|
2013-11-23 16:30:54 +04:00
|
|
|
extern void LoadDefaultFont(); // [Module: text] Loads default font on InitWindow()
|
|
|
|
extern void UnloadDefaultFont(); // [Module: text] Unloads default font from GPU memory
|
2013-11-19 02:38:44 +04:00
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
// Module specific Functions Declaration
|
|
|
|
//----------------------------------------------------------------------------------
|
2013-11-23 16:30:54 +04:00
|
|
|
static void InitGraphicsDevice(); // Initialize Graphics Device (OpenGL stuff)
|
|
|
|
static void ErrorCallback(int error, const char *description); // GLFW3 Error Callback, runs on GLFW3 error
|
|
|
|
static void KeyCallback(GLFWwindow* window, int key, int scancode, int action, int mods); // GLFW3 Keyboard Callback, runs on key pressed
|
2014-01-29 00:21:29 +04:00
|
|
|
static void ScrollCallback(GLFWwindow* window, double xoffset, double yoffset); // GLFW3 Srolling Callback, runs on mouse wheel
|
2013-12-19 15:08:06 +04:00
|
|
|
static void CursorEnterCallback(GLFWwindow* window, int enter); // GLFW3 Cursor Enter Callback, cursor enters client area
|
2013-11-23 16:30:54 +04:00
|
|
|
static void WindowSizeCallback(GLFWwindow* window, int width, int height); // GLFW3 WindowSize Callback, runs when window is resized
|
|
|
|
static void CameraLookAt(Vector3 position, Vector3 target, Vector3 up); // Setup camera view (updates MODELVIEW matrix)
|
|
|
|
static void TakeScreenshot(); // Takes a bitmap (BMP) screenshot and saves it in the same folder as executable
|
2013-11-19 02:38:44 +04:00
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
// Module Functions Definition - Window and OpenGL Context Functions
|
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// Initialize Window and Graphics Context (OpenGL)
|
2013-12-19 15:08:06 +04:00
|
|
|
void InitWindow(int width, int height, const char *title)
|
|
|
|
{
|
|
|
|
InitWindowEx(width, height, title, true, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize Window and Graphics Context (OpenGL) with extended parameters
|
|
|
|
void InitWindowEx(int width, int height, const char* title, bool resizable, const char *cursorImage)
|
2013-11-19 02:38:44 +04:00
|
|
|
{
|
|
|
|
glfwSetErrorCallback(ErrorCallback);
|
2013-11-23 16:30:54 +04:00
|
|
|
|
2013-11-19 02:38:44 +04:00
|
|
|
if (!glfwInit()) exit(1);
|
2013-11-23 16:30:54 +04:00
|
|
|
|
2013-12-19 15:08:06 +04:00
|
|
|
//glfwDefaultWindowHints() // Set default windows hints
|
|
|
|
//glfwWindowHint(GLFW_SAMPLES, 4); // If called before windows creation, enables multisampling x4 (MSAA), default is 0
|
|
|
|
if (!resizable) glfwWindowHint(GLFW_RESIZABLE, GL_FALSE); // Avoid window being resizable
|
2013-11-23 16:30:54 +04:00
|
|
|
|
2013-11-19 02:38:44 +04:00
|
|
|
window = glfwCreateWindow(width, height, title, NULL, NULL);
|
2013-11-23 16:30:54 +04:00
|
|
|
|
|
|
|
windowWidth = width;
|
|
|
|
windowHeight = height;
|
|
|
|
windowTitle = title;
|
|
|
|
|
2013-11-19 02:38:44 +04:00
|
|
|
if (!window)
|
|
|
|
{
|
|
|
|
glfwTerminate();
|
|
|
|
exit(1);
|
|
|
|
}
|
2013-11-23 16:30:54 +04:00
|
|
|
|
|
|
|
glfwSetWindowSizeCallback(window, WindowSizeCallback);
|
2013-12-19 15:08:06 +04:00
|
|
|
glfwSetCursorEnterCallback(window, CursorEnterCallback);
|
2013-11-23 16:30:54 +04:00
|
|
|
|
|
|
|
glfwMakeContextCurrent(window);
|
|
|
|
glfwSetKeyCallback(window, KeyCallback);
|
2014-01-29 00:21:29 +04:00
|
|
|
glfwSetScrollCallback(window, ScrollCallback);
|
2013-11-23 16:30:54 +04:00
|
|
|
glfwSwapInterval(0); // Disables GPU v-sync (if set), so frames are not limited to screen refresh rate (60Hz -> 60 FPS)
|
|
|
|
// If not set, swap interval uses GPU v-sync configuration
|
|
|
|
// Framerate can be setup using SetTargetFPS()
|
|
|
|
InitGraphicsDevice();
|
|
|
|
|
|
|
|
previousTime = glfwGetTime();
|
|
|
|
|
|
|
|
LoadDefaultFont();
|
2013-12-19 15:08:06 +04:00
|
|
|
|
2014-03-16 23:59:02 +04:00
|
|
|
if (cursorImage != NULL) SetCustomCursor(cursorImage);
|
2013-12-19 15:08:06 +04:00
|
|
|
|
|
|
|
srand(time(NULL)); // Initialize random seed
|
2013-11-19 02:38:44 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Close Window and Terminate Context
|
|
|
|
void CloseWindow()
|
|
|
|
{
|
2013-11-23 16:30:54 +04:00
|
|
|
UnloadDefaultFont();
|
2013-11-19 02:38:44 +04:00
|
|
|
|
|
|
|
glfwDestroyWindow(window);
|
2013-11-23 16:30:54 +04:00
|
|
|
glfwTerminate();
|
2013-11-19 02:38:44 +04:00
|
|
|
}
|
|
|
|
|
2013-12-19 15:08:06 +04:00
|
|
|
// Set a custom cursor icon/image
|
|
|
|
void SetCustomCursor(const char *cursorImage)
|
|
|
|
{
|
|
|
|
if (customCursor) UnloadTexture(cursor);
|
|
|
|
|
|
|
|
cursor = LoadTexture(cursorImage);
|
|
|
|
|
|
|
|
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_HIDDEN);
|
|
|
|
customCursor = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set a custom key to exit program
|
|
|
|
// NOTE: default exitKey is ESCAPE
|
|
|
|
void SetExitKey(int key)
|
|
|
|
{
|
|
|
|
exitKey = key;
|
|
|
|
}
|
|
|
|
|
2013-11-19 02:38:44 +04:00
|
|
|
// Detect if KEY_ESCAPE pressed or Close icon pressed
|
|
|
|
bool WindowShouldClose()
|
|
|
|
{
|
2013-11-23 16:30:54 +04:00
|
|
|
return (glfwWindowShouldClose(window));
|
2013-11-19 02:38:44 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Fullscreen toggle (by default F11)
|
|
|
|
void ToggleFullscreen()
|
|
|
|
{
|
2013-11-23 16:30:54 +04:00
|
|
|
if (glfwGetKey(window, GLFW_KEY_F11))
|
|
|
|
{
|
|
|
|
fullscreen = !fullscreen; // Toggle fullscreen flag
|
|
|
|
|
2014-03-16 23:59:02 +04:00
|
|
|
UnloadDefaultFont();
|
|
|
|
|
2013-11-23 16:30:54 +04:00
|
|
|
glfwDestroyWindow(window); // Destroy the current window (we will recreate it!)
|
2014-01-29 00:21:29 +04:00
|
|
|
|
|
|
|
// TODO: WARNING! All loaded resources are lost, we loose Context!
|
2013-11-23 16:30:54 +04:00
|
|
|
|
|
|
|
// NOTE: Window aspect ratio is always windowWidth / windowHeight
|
|
|
|
if (fullscreen) window = glfwCreateWindow(windowWidth, windowHeight, windowTitle, glfwGetPrimaryMonitor(), NULL); // Fullscreen mode
|
|
|
|
else window = glfwCreateWindow(windowWidth, windowHeight, windowTitle, NULL, NULL);
|
|
|
|
|
|
|
|
if (!window)
|
|
|
|
{
|
|
|
|
glfwTerminate();
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
glfwMakeContextCurrent(window);
|
|
|
|
glfwSetKeyCallback(window, KeyCallback);
|
|
|
|
|
|
|
|
InitGraphicsDevice();
|
2014-03-16 23:59:02 +04:00
|
|
|
|
|
|
|
LoadDefaultFont();
|
2013-11-23 16:30:54 +04:00
|
|
|
}
|
2013-11-19 02:38:44 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Sets Background Color
|
|
|
|
void ClearBackground(Color color)
|
|
|
|
{
|
2013-11-23 16:30:54 +04:00
|
|
|
// Color values clamp to 0.0f(0) and 1.0f(255)
|
|
|
|
float r = (float)color.r / 255;
|
|
|
|
float g = (float)color.g / 255;
|
|
|
|
float b = (float)color.b / 255;
|
|
|
|
float a = (float)color.a / 255;
|
|
|
|
|
|
|
|
glClearColor(r, g, b, a);
|
2013-11-19 02:38:44 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Setup drawing canvas to start drawing
|
|
|
|
void BeginDrawing()
|
|
|
|
{
|
2013-11-23 16:30:54 +04:00
|
|
|
currentTime = glfwGetTime(); // glfwGetTime() returns a 'double' containing the number of elapsed seconds since glfwInit() was called
|
|
|
|
updateTime = currentTime - previousTime;
|
|
|
|
previousTime = currentTime;
|
2013-11-19 02:38:44 +04:00
|
|
|
|
2013-11-23 16:30:54 +04:00
|
|
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear used buffers, Depth Buffer is used for 3D
|
|
|
|
|
|
|
|
glLoadIdentity(); // Reset current matrix (MODELVIEW)
|
|
|
|
|
|
|
|
glTranslatef(0.375, 0.375, 0); // HACK to have 2D pixel-perfect drawing on OpenGL
|
2013-11-19 02:38:44 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// End canvas drawing and Swap Buffers (Double Buffering)
|
|
|
|
void EndDrawing()
|
|
|
|
{
|
2013-12-19 15:08:06 +04:00
|
|
|
if (customCursor && cursorOnScreen) DrawTexture(cursor, GetMouseX(), GetMouseY(), WHITE);
|
|
|
|
|
2013-11-23 16:30:54 +04:00
|
|
|
glfwSwapBuffers(window); // Swap back and front buffers
|
|
|
|
glfwPollEvents(); // Register keyboard/mouse events
|
|
|
|
|
|
|
|
currentTime = glfwGetTime();
|
|
|
|
drawTime = currentTime - previousTime;
|
|
|
|
previousTime = currentTime;
|
|
|
|
|
|
|
|
frameTime = updateTime + drawTime;
|
|
|
|
|
|
|
|
double extraTime = 0;
|
|
|
|
|
|
|
|
while (frameTime < targetTime)
|
|
|
|
{
|
|
|
|
// Implement a delay
|
|
|
|
currentTime = glfwGetTime();
|
|
|
|
extraTime = currentTime - previousTime;
|
|
|
|
previousTime = currentTime;
|
|
|
|
frameTime += extraTime;
|
|
|
|
}
|
2013-11-19 02:38:44 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Initializes 3D mode for drawing (Camera setup)
|
|
|
|
void Begin3dMode(Camera camera)
|
|
|
|
{
|
2013-11-23 16:30:54 +04:00
|
|
|
glMatrixMode(GL_PROJECTION); // Switch to projection matrix
|
|
|
|
|
|
|
|
glPushMatrix(); // Save previous matrix, which contains the settings for the 2d ortho projection
|
|
|
|
glLoadIdentity(); // Reset current matrix (PROJECTION)
|
|
|
|
|
2014-03-16 23:59:02 +04:00
|
|
|
// Setup perspective projection
|
|
|
|
float aspect = (GLfloat)windowWidth/(GLfloat)windowHeight;
|
|
|
|
double top = 0.1f*tan(45.0f*PI / 360.0);
|
|
|
|
double right = top*aspect;
|
2013-11-23 16:30:54 +04:00
|
|
|
|
2014-03-16 23:59:02 +04:00
|
|
|
glFrustum(-right, right, -top, top, 0.1f, 100.0f);
|
|
|
|
|
2013-11-23 16:30:54 +04:00
|
|
|
glMatrixMode(GL_MODELVIEW); // Switch back to modelview matrix
|
|
|
|
glLoadIdentity(); // Reset current matrix (MODELVIEW)
|
|
|
|
|
|
|
|
CameraLookAt(camera.position, camera.target, camera.up); // Setup Camera view
|
2013-11-19 02:38:44 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Ends 3D mode and returns to default 2D orthographic mode
|
|
|
|
void End3dMode()
|
|
|
|
{
|
2013-11-23 16:30:54 +04:00
|
|
|
glMatrixMode(GL_PROJECTION); // Switch to projection matrix
|
|
|
|
glPopMatrix(); // Restore previous matrix (PROJECTION) from matrix stack
|
|
|
|
|
|
|
|
glMatrixMode(GL_MODELVIEW); // Get back to modelview matrix
|
|
|
|
glLoadIdentity(); // Reset current matrix (MODELVIEW)
|
|
|
|
|
|
|
|
glTranslatef(0.375, 0.375, 0); // HACK to ensure pixel-perfect drawing on OpenGL (after exiting 3D mode)
|
2013-11-19 02:38:44 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set target FPS for the game
|
|
|
|
void SetTargetFPS(int fps)
|
|
|
|
{
|
2013-11-23 16:30:54 +04:00
|
|
|
targetTime = 1 / (float)fps;
|
|
|
|
|
|
|
|
printf("TargetTime per Frame: %f seconds\n", (float)targetTime);
|
2013-11-19 02:38:44 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Returns current FPS
|
|
|
|
float GetFPS()
|
|
|
|
{
|
2013-11-23 16:30:54 +04:00
|
|
|
return (1/(float)frameTime);
|
2013-11-19 02:38:44 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Returns time in seconds for one frame
|
|
|
|
float GetFrameTime()
|
|
|
|
{
|
2013-11-23 16:30:54 +04:00
|
|
|
// As we are operating quite a lot with frameTime, it could be no stable
|
|
|
|
// so we round it before before passing around to be used
|
|
|
|
// NOTE: There are still problems with high framerates (>500fps)
|
|
|
|
double roundedFrameTime = round(frameTime*10000) / 10000;
|
|
|
|
|
|
|
|
return (float)roundedFrameTime; // Time in seconds to run a frame
|
2013-11-19 02:38:44 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Returns a Color struct from hexadecimal value
|
|
|
|
Color GetColor(int hexValue)
|
|
|
|
{
|
2013-11-23 16:30:54 +04:00
|
|
|
Color color;
|
2013-11-19 02:38:44 +04:00
|
|
|
|
2013-11-23 16:30:54 +04:00
|
|
|
color.r = (unsigned char)(hexValue >> 24) & 0xFF;
|
|
|
|
color.g = (unsigned char)(hexValue >> 16) & 0xFF;
|
|
|
|
color.b = (unsigned char)(hexValue >> 8) & 0xFF;
|
|
|
|
color.a = (unsigned char)hexValue & 0xFF;
|
|
|
|
|
|
|
|
return color;
|
2013-11-19 02:38:44 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Returns hexadecimal value for a Color
|
|
|
|
int GetHexValue(Color color)
|
|
|
|
{
|
2013-11-23 16:30:54 +04:00
|
|
|
return ((color.a << 24) + (color.r << 16) + (color.g << 8) + color.b);
|
2013-11-19 02:38:44 +04:00
|
|
|
}
|
|
|
|
|
2013-12-19 15:08:06 +04:00
|
|
|
// Returns a random value between min and max (both included)
|
|
|
|
int GetRandomValue(int min, int max)
|
|
|
|
{
|
2014-01-07 19:53:57 +04:00
|
|
|
if (min > max)
|
|
|
|
{
|
|
|
|
int tmp = max;
|
|
|
|
max = min;
|
|
|
|
min = tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (rand()%(abs(max-min)+1) + min);
|
2013-12-19 15:08:06 +04:00
|
|
|
}
|
|
|
|
|
2014-01-23 15:36:18 +04:00
|
|
|
// Fades color by a percentadge
|
|
|
|
Color Fade(Color color, float alpha)
|
|
|
|
{
|
|
|
|
if (alpha < 0.0) alpha = 0.0;
|
|
|
|
else if (alpha > 1.0) alpha = 1.0;
|
|
|
|
|
|
|
|
return (Color){color.r, color.g, color.b, color.a*alpha};
|
|
|
|
}
|
|
|
|
|
2013-11-19 02:38:44 +04:00
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
// Module Functions Definition - Input (Keyboard, Mouse, Gamepad) Functions
|
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
|
2013-11-28 22:59:56 +04:00
|
|
|
// Detect if a key has been pressed once
|
2013-11-19 02:38:44 +04:00
|
|
|
bool IsKeyPressed(int key)
|
2013-11-28 22:59:56 +04:00
|
|
|
{
|
|
|
|
bool ret = false;
|
|
|
|
|
|
|
|
currentKeyState[key] = IsKeyDown(key);
|
|
|
|
|
|
|
|
if (currentKeyState[key] != previousKeyState[key])
|
|
|
|
{
|
|
|
|
if (currentKeyState[key]) ret = true;
|
|
|
|
previousKeyState[key] = currentKeyState[key];
|
|
|
|
}
|
|
|
|
else ret = false;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Detect if a key is being pressed (key held down)
|
|
|
|
bool IsKeyDown(int key)
|
2013-11-19 02:38:44 +04:00
|
|
|
{
|
2013-11-23 16:30:54 +04:00
|
|
|
if (glfwGetKey(window, key) == GLFW_PRESS) return true;
|
|
|
|
else return false;
|
2013-11-19 02:38:44 +04:00
|
|
|
}
|
|
|
|
|
2013-11-28 22:59:56 +04:00
|
|
|
// Detect if a key has been released once
|
2013-11-19 02:38:44 +04:00
|
|
|
bool IsKeyReleased(int key)
|
2013-11-28 22:59:56 +04:00
|
|
|
{
|
|
|
|
bool ret = false;
|
|
|
|
|
|
|
|
currentKeyState[key] = IsKeyUp(key);
|
|
|
|
|
|
|
|
if (currentKeyState[key] != previousKeyState[key])
|
|
|
|
{
|
|
|
|
if (currentKeyState[key]) ret = true;
|
|
|
|
previousKeyState[key] = currentKeyState[key];
|
|
|
|
}
|
|
|
|
else ret = false;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Detect if a key is NOT being pressed (key not held down)
|
|
|
|
bool IsKeyUp(int key)
|
2013-11-19 02:38:44 +04:00
|
|
|
{
|
2013-11-23 16:30:54 +04:00
|
|
|
if (glfwGetKey(window, key) == GLFW_RELEASE) return true;
|
|
|
|
else return false;
|
2013-11-19 02:38:44 +04:00
|
|
|
}
|
|
|
|
|
2013-11-28 22:59:56 +04:00
|
|
|
// Detect if a mouse button has been pressed once
|
2013-11-19 02:38:44 +04:00
|
|
|
bool IsMouseButtonPressed(int button)
|
2013-11-28 22:59:56 +04:00
|
|
|
{
|
|
|
|
bool ret = false;
|
|
|
|
|
|
|
|
currentMouseState[button] = IsMouseButtonDown(button);
|
|
|
|
|
|
|
|
if (currentMouseState[button] != previousMouseState[button])
|
|
|
|
{
|
|
|
|
if (currentMouseState[button]) ret = true;
|
|
|
|
previousMouseState[button] = currentMouseState[button];
|
|
|
|
}
|
|
|
|
else ret = false;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Detect if a mouse button is being pressed
|
|
|
|
bool IsMouseButtonDown(int button)
|
2013-11-19 02:38:44 +04:00
|
|
|
{
|
2013-11-23 16:30:54 +04:00
|
|
|
if (glfwGetMouseButton(window, button) == GLFW_PRESS) return true;
|
|
|
|
else return false;
|
2013-11-19 02:38:44 +04:00
|
|
|
}
|
|
|
|
|
2013-11-28 22:59:56 +04:00
|
|
|
// Detect if a mouse button has been released once
|
2013-11-19 02:38:44 +04:00
|
|
|
bool IsMouseButtonReleased(int button)
|
2013-11-28 22:59:56 +04:00
|
|
|
{
|
|
|
|
bool ret = false;
|
|
|
|
|
|
|
|
currentMouseState[button] = IsMouseButtonUp(button);
|
|
|
|
|
|
|
|
if (currentMouseState[button] != previousMouseState[button])
|
|
|
|
{
|
|
|
|
if (currentMouseState[button]) ret = true;
|
|
|
|
previousMouseState[button] = currentMouseState[button];
|
|
|
|
}
|
|
|
|
else ret = false;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Detect if a mouse button is NOT being pressed
|
|
|
|
bool IsMouseButtonUp(int button)
|
2013-11-19 02:38:44 +04:00
|
|
|
{
|
2013-11-23 16:30:54 +04:00
|
|
|
if (glfwGetMouseButton(window, button) == GLFW_RELEASE) return true;
|
|
|
|
else return false;
|
2013-11-19 02:38:44 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Returns mouse position X
|
|
|
|
int GetMouseX()
|
|
|
|
{
|
2013-11-23 16:30:54 +04:00
|
|
|
double mouseX;
|
|
|
|
double mouseY;
|
|
|
|
|
|
|
|
glfwGetCursorPos(window, &mouseX, &mouseY);
|
2013-11-19 02:38:44 +04:00
|
|
|
|
2013-11-23 16:30:54 +04:00
|
|
|
return (int)mouseX;
|
2013-11-19 02:38:44 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Returns mouse position Y
|
|
|
|
int GetMouseY()
|
|
|
|
{
|
2013-11-23 16:30:54 +04:00
|
|
|
double mouseX;
|
|
|
|
double mouseY;
|
|
|
|
|
|
|
|
glfwGetCursorPos(window, &mouseX, &mouseY);
|
2013-11-19 02:38:44 +04:00
|
|
|
|
2013-11-23 16:30:54 +04:00
|
|
|
return (int)mouseY;
|
2013-11-19 02:38:44 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Returns mouse position XY
|
|
|
|
Vector2 GetMousePosition()
|
|
|
|
{
|
2013-11-23 16:30:54 +04:00
|
|
|
double mouseX;
|
|
|
|
double mouseY;
|
|
|
|
|
|
|
|
glfwGetCursorPos(window, &mouseX, &mouseY);
|
|
|
|
|
|
|
|
Vector2 position = { (float)mouseX, (float)mouseY };
|
2013-11-19 02:38:44 +04:00
|
|
|
|
2013-11-23 16:30:54 +04:00
|
|
|
return position;
|
2013-11-19 02:38:44 +04:00
|
|
|
}
|
|
|
|
|
2014-01-29 00:21:29 +04:00
|
|
|
// Returns mouse wheel movement Y
|
|
|
|
int GetMouseWheelMove()
|
|
|
|
{
|
|
|
|
previousMouseWheelY = currentMouseWheelY;
|
|
|
|
|
|
|
|
currentMouseWheelY = 0;
|
|
|
|
|
|
|
|
return previousMouseWheelY;
|
|
|
|
}
|
|
|
|
|
2013-11-19 02:38:44 +04:00
|
|
|
// Detect if a gamepad is available
|
|
|
|
bool IsGamepadAvailable(int gamepad)
|
|
|
|
{
|
2013-11-23 16:30:54 +04:00
|
|
|
int result = glfwJoystickPresent(gamepad);
|
|
|
|
|
|
|
|
if (result == 1) return true;
|
|
|
|
else return false;
|
2013-11-19 02:38:44 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Return axis movement vector for a gamepad
|
|
|
|
Vector2 GetGamepadMovement(int gamepad)
|
|
|
|
{
|
2013-11-23 16:30:54 +04:00
|
|
|
Vector2 vec = { 0, 0 };
|
|
|
|
|
|
|
|
const float *axes;
|
2013-11-19 02:38:44 +04:00
|
|
|
int axisCount;
|
2013-11-23 16:30:54 +04:00
|
|
|
|
|
|
|
axes = glfwGetJoystickAxes(gamepad, &axisCount);
|
|
|
|
|
|
|
|
if (axisCount >= 2)
|
|
|
|
{
|
|
|
|
vec.x = axes[0]; // Left joystick X
|
|
|
|
vec.y = axes[1]; // Left joystick Y
|
|
|
|
|
|
|
|
//vec.x = axes[2]; // Right joystick X
|
|
|
|
//vec.x = axes[3]; // Right joystick Y
|
|
|
|
}
|
|
|
|
|
|
|
|
return vec;
|
2013-11-19 02:38:44 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Detect if a gamepad button is being pressed
|
|
|
|
bool IsGamepadButtonPressed(int gamepad, int button)
|
2013-11-28 22:59:56 +04:00
|
|
|
{
|
|
|
|
bool ret = false;
|
|
|
|
|
|
|
|
currentGamepadState[button] = IsGamepadButtonDown(gamepad, button);
|
|
|
|
|
|
|
|
if (currentGamepadState[button] != previousGamepadState[button])
|
|
|
|
{
|
|
|
|
if (currentGamepadState[button]) ret = true;
|
|
|
|
previousGamepadState[button] = currentGamepadState[button];
|
|
|
|
}
|
|
|
|
else ret = false;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsGamepadButtonDown(int gamepad, int button)
|
2013-11-19 02:38:44 +04:00
|
|
|
{
|
2013-11-23 16:30:54 +04:00
|
|
|
const unsigned char* buttons;
|
|
|
|
int buttonsCount;
|
|
|
|
|
|
|
|
buttons = glfwGetJoystickButtons(gamepad, &buttonsCount);
|
|
|
|
|
2014-03-16 23:59:02 +04:00
|
|
|
if ((buttons != NULL) && (buttons[button] == GLFW_PRESS))
|
2013-11-23 16:30:54 +04:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else return false;
|
2013-11-19 02:38:44 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Detect if a gamepad button is NOT being pressed
|
|
|
|
bool IsGamepadButtonReleased(int gamepad, int button)
|
2013-11-28 22:59:56 +04:00
|
|
|
{
|
|
|
|
bool ret = false;
|
|
|
|
|
|
|
|
currentGamepadState[button] = IsGamepadButtonUp(gamepad, button);
|
|
|
|
|
|
|
|
if (currentGamepadState[button] != previousGamepadState[button])
|
|
|
|
{
|
|
|
|
if (currentGamepadState[button]) ret = true;
|
|
|
|
previousGamepadState[button] = currentGamepadState[button];
|
|
|
|
}
|
|
|
|
else ret = false;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsGamepadButtonUp(int gamepad, int button)
|
2013-11-19 02:38:44 +04:00
|
|
|
{
|
2013-11-23 16:30:54 +04:00
|
|
|
const unsigned char* buttons;
|
|
|
|
int buttonsCount;
|
|
|
|
|
|
|
|
buttons = glfwGetJoystickButtons(gamepad, &buttonsCount);
|
|
|
|
|
2014-03-16 23:59:02 +04:00
|
|
|
if ((buttons != NULL) && (buttons[button] == GLFW_RELEASE))
|
2013-11-23 16:30:54 +04:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else return false;
|
2013-11-19 02:38:44 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
// Module specific Functions Definition
|
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// GLFW3 Error Callback, runs on GLFW3 error
|
|
|
|
static void ErrorCallback(int error, const char *description)
|
|
|
|
{
|
2013-12-19 15:08:06 +04:00
|
|
|
printf(description);
|
|
|
|
//fprintf(stderr, description);
|
2013-11-19 02:38:44 +04:00
|
|
|
}
|
|
|
|
|
2014-01-29 00:21:29 +04:00
|
|
|
// GLFW3 Srolling Callback, runs on mouse wheel
|
|
|
|
static void ScrollCallback(GLFWwindow* window, double xoffset, double yoffset)
|
|
|
|
{
|
|
|
|
currentMouseWheelY = (int)yoffset;
|
|
|
|
}
|
|
|
|
|
2013-11-19 02:38:44 +04:00
|
|
|
// GLFW3 Keyboard Callback, runs on key pressed
|
|
|
|
static void KeyCallback(GLFWwindow* window, int key, int scancode, int action, int mods)
|
|
|
|
{
|
2013-12-19 15:08:06 +04:00
|
|
|
if (key == exitKey && action == GLFW_PRESS)
|
2013-11-23 16:30:54 +04:00
|
|
|
{
|
|
|
|
glfwSetWindowShouldClose(window, GL_TRUE);
|
|
|
|
|
|
|
|
// NOTE: Before closing window, while loop must be left!
|
|
|
|
}
|
|
|
|
else if (key == GLFW_KEY_F11 && action == GLFW_PRESS)
|
|
|
|
{
|
|
|
|
ToggleFullscreen();
|
|
|
|
}
|
|
|
|
else if (key == GLFW_KEY_F12 && action == GLFW_PRESS)
|
|
|
|
{
|
|
|
|
TakeScreenshot();
|
|
|
|
}
|
2013-11-19 02:38:44 +04:00
|
|
|
}
|
|
|
|
|
2013-12-19 15:08:06 +04:00
|
|
|
static void CursorEnterCallback(GLFWwindow* window, int enter)
|
|
|
|
{
|
|
|
|
if (enter == GL_TRUE) cursorOnScreen = true;
|
|
|
|
else cursorOnScreen = false;
|
|
|
|
}
|
|
|
|
|
2013-11-19 02:38:44 +04:00
|
|
|
// GLFW3 WindowSize Callback, runs when window is resized
|
|
|
|
static void WindowSizeCallback(GLFWwindow* window, int width, int height)
|
|
|
|
{
|
2013-11-23 16:30:54 +04:00
|
|
|
InitGraphicsDevice(); // If window is resized, graphics device is re-initialized
|
|
|
|
// NOTE: Aspect ratio does not change, so, image can be deformed
|
2013-11-19 02:38:44 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize Graphics Device (OpenGL stuff)
|
|
|
|
static void InitGraphicsDevice()
|
|
|
|
{
|
2013-11-23 16:30:54 +04:00
|
|
|
int fbWidth, fbHeight;
|
|
|
|
|
|
|
|
glfwGetFramebufferSize(window, &fbWidth, &fbHeight); // Get framebuffer size of current window
|
|
|
|
|
|
|
|
glViewport(0, 0, fbWidth, fbHeight); // Set viewport width and height
|
|
|
|
|
|
|
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear used buffers, depth buffer is used for 3D
|
|
|
|
glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // Set background color (black)
|
|
|
|
glClearDepth(1.0f); // Clear depth buffer
|
|
|
|
|
|
|
|
glEnable(GL_DEPTH_TEST); // Enables depth testing (required for 3D)
|
|
|
|
glDepthFunc(GL_LEQUAL); // Type of depth testing to apply
|
|
|
|
|
|
|
|
glEnable(GL_BLEND); // Enable color blending (required to work with transparencies)
|
|
|
|
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); // Color blending function (how colors are mixed)
|
|
|
|
|
|
|
|
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Improve quality of color and texture coordinate interpolation (Deprecated in OGL 3.0)
|
|
|
|
// Other options: GL_FASTEST, GL_DONT_CARE (default)
|
|
|
|
|
|
|
|
glMatrixMode(GL_PROJECTION); // Switch to PROJECTION matrix
|
|
|
|
glLoadIdentity(); // Reset current matrix (PROJECTION)
|
|
|
|
glOrtho(0, fbWidth, fbHeight, 0, 0, 1); // Config orthographic mode: top-left corner --> (0,0)
|
|
|
|
glMatrixMode(GL_MODELVIEW); // Switch back to MODELVIEW matrix
|
|
|
|
glLoadIdentity(); // Reset current matrix (MODELVIEW)
|
|
|
|
|
|
|
|
// TODO: Review all shapes/models are drawn CCW and enable backface culling
|
|
|
|
|
|
|
|
//glEnable(GL_CULL_FACE); // Enable backface culling (Disabled by default)
|
|
|
|
//glCullFace(GL_BACK); // Cull the Back face (default)
|
|
|
|
//glFrontFace(GL_CCW); // Front face are defined counter clockwise (default)
|
|
|
|
|
|
|
|
glShadeModel(GL_SMOOTH); // Smooth shading between vertex (vertex colors interpolation)
|
|
|
|
// Possible options: GL_SMOOTH (Color interpolation) or GL_FLAT (no interpolation)
|
2013-11-19 02:38:44 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Setup camera view (updates MODELVIEW matrix)
|
|
|
|
static void CameraLookAt(Vector3 position, Vector3 target, Vector3 up)
|
|
|
|
{
|
2013-11-23 16:30:54 +04:00
|
|
|
float rotMatrix[16]; // Matrix to store camera rotation
|
2013-11-19 02:38:44 +04:00
|
|
|
|
2013-11-23 16:30:54 +04:00
|
|
|
Vector3 rotX, rotY, rotZ; // Vectors to calculate camera rotations X, Y, Z (Euler)
|
2013-11-19 02:38:44 +04:00
|
|
|
|
|
|
|
// Construct rotation matrix from vectors
|
2013-11-23 16:30:54 +04:00
|
|
|
rotZ = VectorSubtract(position, target);
|
|
|
|
VectorNormalize(&rotZ);
|
|
|
|
rotY = up; // Y rotation vector
|
|
|
|
rotX = VectorCrossProduct(rotY, rotZ); // X rotation vector = Y cross Z
|
|
|
|
rotY = VectorCrossProduct(rotZ, rotX); // Recompute Y rotation = Z cross X
|
|
|
|
VectorNormalize(&rotX); // X rotation vector normalization
|
|
|
|
VectorNormalize(&rotY); // Y rotation vector normalization
|
2013-11-19 02:38:44 +04:00
|
|
|
|
|
|
|
rotMatrix[0] = rotX.x;
|
2013-11-23 16:30:54 +04:00
|
|
|
rotMatrix[1] = rotY.x;
|
|
|
|
rotMatrix[2] = rotZ.x;
|
|
|
|
rotMatrix[3] = 0.0f;
|
2013-11-19 02:38:44 +04:00
|
|
|
rotMatrix[4] = rotX.y;
|
2013-11-23 16:30:54 +04:00
|
|
|
rotMatrix[5] = rotY.y;
|
2013-11-19 02:38:44 +04:00
|
|
|
rotMatrix[6] = rotZ.y;
|
2013-11-23 16:30:54 +04:00
|
|
|
rotMatrix[7] = 0.0f;
|
2013-11-19 02:38:44 +04:00
|
|
|
rotMatrix[8] = rotX.z;
|
|
|
|
rotMatrix[9] = rotY.z;
|
|
|
|
rotMatrix[10] = rotZ.z;
|
|
|
|
rotMatrix[11] = 0.0f;
|
|
|
|
rotMatrix[12] = 0.0f;
|
|
|
|
rotMatrix[13] = 0.0f;
|
|
|
|
rotMatrix[14] = 0.0f;
|
|
|
|
rotMatrix[15] = 1.0f;
|
|
|
|
|
2013-11-23 16:30:54 +04:00
|
|
|
glMultMatrixf(rotMatrix); // Multiply MODELVIEW matrix by rotation matrix
|
2013-11-19 02:38:44 +04:00
|
|
|
|
2013-11-23 16:30:54 +04:00
|
|
|
glTranslatef(-position.x, -position.y, -position.z); // Translate eye to position
|
2013-11-19 02:38:44 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Takes a bitmap (BMP) screenshot and saves it in the same folder as executable
|
|
|
|
static void TakeScreenshot()
|
|
|
|
{
|
2013-11-23 16:30:54 +04:00
|
|
|
static int shotNum = 0; // Screenshot number, increments every screenshot take during program execution
|
|
|
|
|
|
|
|
char buffer[20]; // Buffer to store file name
|
|
|
|
int fbWidth, fbHeight;
|
|
|
|
|
2014-01-23 15:36:18 +04:00
|
|
|
unsigned char *imgData; // Pixel image data array
|
2013-11-19 02:38:44 +04:00
|
|
|
|
2013-11-23 16:30:54 +04:00
|
|
|
glfwGetFramebufferSize(window, &fbWidth, &fbHeight); // Get framebuffer size of current window
|
2013-11-19 02:38:44 +04:00
|
|
|
|
2014-01-23 15:36:18 +04:00
|
|
|
imgData = (unsigned char *)malloc(fbWidth * fbHeight * sizeof(unsigned char) * 4);
|
2013-11-19 02:38:44 +04:00
|
|
|
|
2013-11-23 16:30:54 +04:00
|
|
|
// NOTE: glReadPixels returns image flipped vertically -> (0,0) is the bottom left corner of the framebuffer
|
2014-01-23 15:36:18 +04:00
|
|
|
glReadPixels(0, 0, fbWidth, fbHeight, GL_RGBA, GL_UNSIGNED_BYTE, imgData);
|
|
|
|
|
|
|
|
// TODO: Flip image vertically!
|
|
|
|
|
|
|
|
unsigned char *imgDataFlip = (unsigned char *)malloc(fbWidth * fbHeight * sizeof(unsigned char) * 4);
|
|
|
|
|
|
|
|
for (int y = fbHeight-1; y >= 0; y--)
|
|
|
|
{
|
|
|
|
for (int x = 0; x < (fbWidth*4); x++)
|
|
|
|
{
|
|
|
|
imgDataFlip[x + (fbHeight - y - 1)*fbWidth*4] = imgData[x + (y*fbWidth*4)];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
free(imgData);
|
2013-11-23 16:30:54 +04:00
|
|
|
|
2014-01-23 15:36:18 +04:00
|
|
|
sprintf(buffer, "screenshot%03i.png", shotNum);
|
2013-11-19 02:38:44 +04:00
|
|
|
|
2013-11-23 16:30:54 +04:00
|
|
|
// NOTE: BMP directly stores data flipped vertically
|
2014-01-23 15:36:18 +04:00
|
|
|
//WriteBitmap(buffer, imgDataPixel, fbWidth, fbHeight); // Writes pixel data array into a bitmap (BMP) file
|
|
|
|
WritePNG(buffer, imgDataFlip, fbWidth, fbHeight);
|
2013-11-23 16:30:54 +04:00
|
|
|
|
2014-01-23 15:36:18 +04:00
|
|
|
free(imgDataFlip);
|
2013-11-23 16:30:54 +04:00
|
|
|
|
|
|
|
shotNum++;
|
2013-12-19 15:08:06 +04:00
|
|
|
}
|