Bläddra i källkod

Decoupled some modules: audio, camera, gestures

Added window drop files support
raysan5 10 år sedan
förälder
incheckning
c0d8ae7f4d
1 ändrade filer med 77 tillägg och 78 borttagningar
  1. 77 78
      src/core.c

+ 77 - 78
src/core.c

@@ -17,7 +17,7 @@
 *
 *   On PLATFORM_RPI, graphic device is managed by EGL and input system is coded in raw mode.
 *
-*   Copyright (c) 2014 Ramon Santamaria (Ray San - [email protected])
+*   Copyright (c) 2014 Ramon Santamaria (@raysan5)
 *
 *   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.
@@ -164,7 +164,7 @@ static bool customCursor = false;           // Tracks if custom cursor has been
 static bool cursorOnScreen = false;         // Tracks if cursor is inside client area
 static Texture2D cursor;                    // Cursor texture
 
-static Vector2 mousePosition;
+static Vector2 mousePosition;               // Mouse position on screen
 
 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
@@ -179,9 +179,12 @@ static int previousMouseWheelY = 0;         // Required to track mouse wheel var
 static int currentMouseWheelY = 0;          // Required to track mouse wheel variation
 
 static int exitKey = KEY_ESCAPE;            // Default exit key (ESC)
-static int lastKeyPressed = -1;
+static int lastKeyPressed = -1;             // Register last key pressed
 
-static bool cursorHidden;
+static bool cursorHidden;                   // Track if cursor is hidden
+
+static char **dropFilesPath;                // Store dropped files paths as strings
+static int dropFilesCount = 0;              // Count stored strings
 #endif
 
 static double currentTime, previousTime;    // Used to track timmings
@@ -189,8 +192,8 @@ static double updateTime, drawTime;         // Time measures for update and draw
 static double frameTime;                    // Time measure for one frame
 static double targetTime = 0.0;             // Desired time for one frame, if 0 not applied
 
-static char configFlags = 0;
-static bool showLogo = false;
+static char configFlags = 0;                // Configuration flags (bit  based)
+static bool showLogo = false;               // Track if showing logo at init is enabled
 
 //----------------------------------------------------------------------------------
 // Other Modules Functions Declaration (required by core)
@@ -198,19 +201,6 @@ static bool showLogo = false;
 extern void LoadDefaultFont(void);              // [Module: text] Loads default font on InitWindow()
 extern void UnloadDefaultFont(void);            // [Module: text] Unloads default font from GPU memory
 
-extern void UpdateMusicStream(void);            // [Module: audio] Updates buffers for music streaming
-
-extern Vector2 GetRawPosition(void);
-extern void ResetGestures(void);
-
-#if defined(PLATFORM_ANDROID)
-extern void InitAndroidGestures(struct android_app *app);
-#endif
-
-#if defined(PLATFORM_WEB)
-extern void InitWebGestures(void);              // [Module: gestures] Initializes emscripten gestures for web
-#endif
-
 //----------------------------------------------------------------------------------
 // Module specific Functions Declaration
 //----------------------------------------------------------------------------------
@@ -241,7 +231,11 @@ static void CharCallback(GLFWwindow *window, unsigned int key);
 static void ScrollCallback(GLFWwindow *window, double xoffset, double yoffset);            // GLFW3 Srolling Callback, runs on mouse wheel
 static void CursorEnterCallback(GLFWwindow *window, int enter);                            // GLFW3 Cursor Enter Callback, cursor enters client area
 static void WindowSizeCallback(GLFWwindow *window, int width, int height);                 // GLFW3 WindowSize Callback, runs when window is resized
-static void WindowIconifyCallback(GLFWwindow* window, int iconified);                      // GLFW3 WindowIconify Callback, runs when window is minimized/restored
+static void WindowIconifyCallback(GLFWwindow *window, int iconified);                      // GLFW3 WindowIconify Callback, runs when window is minimized/restored
+#endif
+
+#if defined(PLATFORM_DESKTOP)
+static void WindowDropCallback(GLFWwindow *window, int count, const char **paths);         // GLFW3 Window Drop Callback, runs when drop files into window
 #endif
 
 #if defined(PLATFORM_DESKTOP) || defined(PLATFORM_RPI)
@@ -284,10 +278,6 @@ void InitWindow(int width, int height, const char *title)
     InitGamepad();      // Gamepad init
 #endif
 
-#if defined(PLATFORM_WEB)
-    InitWebGestures();  // Init touch input events for web
-#endif
-
     mousePosition.x = screenWidth/2;
     mousePosition.y = screenHeight/2;
 
@@ -341,7 +331,7 @@ void InitWindow(int width, int height, struct android_app *state)
     //state->userData = &engine;
     app->onAppCmd = AndroidCommandCallback;
     
-    InitAndroidGestures(app);
+    //InitGesturesSystem(app);   // NOTE: Must be called by user
 
     InitAssetManager(app->activity->assetManager);
 
@@ -412,6 +402,16 @@ bool WindowShouldClose(void)
 #endif
 }
 
+// Detect if window has been minimized (or lost focus)
+bool IsWindowMinimized(void)
+{
+#if defined(PLATFORM_DESKTOP) || defined(PLATFORM_WEB)
+    return windowMinimized;
+#else
+    return false;
+#endif
+}
+
 // Fullscreen toggle
 void ToggleFullscreen(void)
 {
@@ -497,14 +497,8 @@ void EndDrawing(void)
 
     SwapBuffers();                  // Copy back buffer to front buffer
 
-#if defined(PLATFORM_ANDROID) || defined(PLATFORM_WEB)
-    ResetGestures();
-#endif
-
     PollInputEvents();              // Poll user events
 
-    UpdateMusicStream();            // NOTE: Function checks if music is enabled
-
     currentTime = GetTime();
     drawTime = currentTime - previousTime;
     previousTime = currentTime;
@@ -643,6 +637,34 @@ void ShowLogo(void)
     showLogo = true;
 }
 
+// Check if a file have been dropped into window
+bool IsFileDropped(void)
+{
+    if (dropFilesCount > 0) return true;
+    else return false;
+}
+
+// Retrieve dropped files into window
+char **GetDroppedFiles(int *count)
+{
+    *count = dropFilesCount;
+    return dropFilesPath;
+}
+
+// Clear dropped files paths buffer
+void ClearDroppedFiles(void)
+{
+    if (dropFilesCount > 0)
+    {
+        for (int i = 0; i < dropFilesCount; i++) free(dropFilesPath[i]);
+        
+        free(dropFilesPath);
+        
+        dropFilesCount = 0;
+    }
+}
+
+// TODO: Gives the ray trace from mouse position
 Ray GetMouseRay(Vector2 mousePosition, Camera camera)
 {
     Ray ray;
@@ -933,40 +955,6 @@ bool IsGamepadButtonUp(int gamepad, int button)
 }
 #endif
 
-#if defined(PLATFORM_ANDROID) || defined(PLATFORM_WEB)
-// Returns touch position X
-int GetTouchX(void)
-{
-    return (int)GetRawPosition().x;
-}
-
-// Returns touch position Y
-int GetTouchY(void)
-{
-    return (int)GetRawPosition().y;
-}
-
-// Returns touch position XY
-Vector2 GetTouchPosition(void)
-{
-    Vector2 position = GetRawPosition();
-    
-    if ((screenWidth > displayWidth) || (screenHeight > displayHeight))
-    {
-        // TODO: Seems to work ok but... review!
-        position.x = position.x*((float)screenWidth / (float)(displayWidth - renderOffsetX)) - renderOffsetX/2;
-        position.y = position.y*((float)screenHeight / (float)(displayHeight - renderOffsetY)) - renderOffsetY/2;
-    }
-    else
-    {
-        position.x = position.x*((float)renderWidth / (float)displayWidth) - renderOffsetX/2;
-        position.y = position.y*((float)renderHeight / (float)displayHeight) - renderOffsetY/2;
-    }
-
-    return position;
-}
-#endif
-
 //----------------------------------------------------------------------------------
 // Module specific Functions Definition
 //----------------------------------------------------------------------------------
@@ -1074,6 +1062,9 @@ static void InitDisplay(int width, int height)
     glfwSetCharCallback(window, CharCallback);
     glfwSetScrollCallback(window, ScrollCallback);
     glfwSetWindowIconifyCallback(window, WindowIconifyCallback);
+#if defined(PLATFORM_DESKTOP)
+    glfwSetDropCallback(window, WindowDropCallback);
+#endif
 
     glfwMakeContextCurrent(window);
 
@@ -1317,20 +1308,28 @@ static void WindowSizeCallback(GLFWwindow *window, int width, int height)
 // GLFW3 WindowIconify Callback, runs when window is minimized/restored
 static void WindowIconifyCallback(GLFWwindow* window, int iconified)
 {
-    if (iconified)
-    {
-        // The window was iconified
-        PauseMusicStream();
+    if (iconified) windowMinimized = true;  // The window was iconified
+    else windowMinimized = false;           // The window was restored
+}
+#endif
 
-        windowMinimized = true;
-    }
-    else
+#if defined(PLATFORM_DESKTOP)
+// GLFW3 Window Drop Callback, runs when drop files into window
+// NOTE: Paths are stored in dinamic memory for further retrieval
+// Everytime new files are dropped, old ones are discarded
+static void WindowDropCallback(GLFWwindow *window, int count, const char **paths)
+{
+    ClearDroppedFiles();
+    
+    dropFilesPath = (char **)malloc(sizeof(char *)*count);
+    
+    for (int i = 0; i < count; i++)
     {
-        // The window was restored
-        ResumeMusicStream();
-
-        windowMinimized = false;
+        dropFilesPath[i] = (char *)malloc(sizeof(char)*256);     // Max path length set to 256 char
+        strcpy(dropFilesPath[i], paths[i]);
     }
+
+    dropFilesCount = count;
 }
 #endif
 
@@ -1379,7 +1378,7 @@ static void AndroidCommandCallback(struct android_app *app, int32_t cmd)
         case APP_CMD_GAINED_FOCUS:
         {
             TraceLog(INFO, "APP_CMD_GAINED_FOCUS");
-            ResumeMusicStream();
+            //ResumeMusicStream();
         } break;
         case APP_CMD_PAUSE:
         {
@@ -1389,7 +1388,7 @@ static void AndroidCommandCallback(struct android_app *app, int32_t cmd)
         {
             //DrawFrame();
             TraceLog(INFO, "APP_CMD_LOST_FOCUS");
-            PauseMusicStream();
+            //PauseMusicStream();
         } break;
         case APP_CMD_TERM_WINDOW:
         {