Browse Source

Merge pull request #27 from Anaskorichi999/master

fix windows.h raylib.h collisions
Alexey Kutepov 2 years ago
parent
commit
c17c0e6942

+ 0 - 4
nob.c

@@ -187,7 +187,6 @@ bool build_musializer(Config config)
                         nob_cmd_append(&cmd, "-o", "./build/libplug.so");
                         nob_cmd_append(&cmd, "-o", "./build/libplug.so");
                         nob_cmd_append(&cmd,
                         nob_cmd_append(&cmd,
                             "./src/plug.c",
                             "./src/plug.c",
-                            "./src/separate_translation_unit_for_miniaudio.c",
                             "./src/ffmpeg_linux.c");
                             "./src/ffmpeg_linux.c");
                         nob_cmd_append(&cmd,
                         nob_cmd_append(&cmd,
                             nob_temp_sprintf("-L./build/raylib/%s", NOB_ARRAY_GET(target_names, config.target)),
                             nob_temp_sprintf("-L./build/raylib/%s", NOB_ARRAY_GET(target_names, config.target)),
@@ -224,7 +223,6 @@ bool build_musializer(Config config)
                     nob_cmd_append(&cmd, "-o", "./build/musializer");
                     nob_cmd_append(&cmd, "-o", "./build/musializer");
                     nob_cmd_append(&cmd,
                     nob_cmd_append(&cmd,
                         "./src/plug.c",
                         "./src/plug.c",
-                        "./src/separate_translation_unit_for_miniaudio.c",
                         "./src/ffmpeg_linux.c",
                         "./src/ffmpeg_linux.c",
                         "./src/musializer.c");
                         "./src/musializer.c");
                     nob_cmd_append(&cmd,
                     nob_cmd_append(&cmd,
@@ -254,7 +252,6 @@ bool build_musializer(Config config)
                     nob_cmd_append(&cmd, "-o", "./build/musializer");
                     nob_cmd_append(&cmd, "-o", "./build/musializer");
                     nob_cmd_append(&cmd,
                     nob_cmd_append(&cmd,
                         "./src/plug.c",
                         "./src/plug.c",
-                        "./src/separate_translation_unit_for_miniaudio.c",
                         "./src/ffmpeg_windows.c",
                         "./src/ffmpeg_windows.c",
                         "./src/musializer.c",
                         "./src/musializer.c",
                         "./build/musializer.res"
                         "./build/musializer.res"
@@ -280,7 +277,6 @@ bool build_musializer(Config config)
                     nob_cmd_append(&cmd,
                     nob_cmd_append(&cmd,
                         "./src/musializer.c",
                         "./src/musializer.c",
                         "./src/plug.c",
                         "./src/plug.c",
-                        "./src/separate_translation_unit_for_miniaudio.c",
                         "./src/ffmpeg_windows.c"
                         "./src/ffmpeg_windows.c"
                         // TODO: building resource file is not implemented for TARGET_WIN64_MSVC
                         // TODO: building resource file is not implemented for TARGET_WIN64_MSVC
                         //"./build/musializer.res"
                         //"./build/musializer.res"

+ 5 - 1
src/ffmpeg_windows.c

@@ -4,9 +4,13 @@
 #include <stdbool.h>
 #include <stdbool.h>
 
 
 #define WIN32_LEAN_AND_MEAN
 #define WIN32_LEAN_AND_MEAN
+#define _WINUSER_
+#define _WINGDI_
+#define _IMM_
+#define _WINCON_
 #include <windows.h>
 #include <windows.h>
 
 
-#include "raylib_log.h"
+#include <raylib.h>
 
 
 typedef struct {
 typedef struct {
     HANDLE hProcess;
     HANDLE hProcess;

+ 0 - 1
src/musializer.c

@@ -13,7 +13,6 @@
 #endif // _WIN32
 #endif // _WIN32
 
 
 #include "./hotreload.h"
 #include "./hotreload.h"
-#include "./separate_translation_unit_for_miniaudio.h"
 
 
 int main(void)
 int main(void)
 {
 {

+ 26 - 6
src/plug.c

@@ -7,7 +7,8 @@
 
 
 #include "plug.h"
 #include "plug.h"
 #include "ffmpeg.h"
 #include "ffmpeg.h"
-#include "separate_translation_unit_for_miniaudio.h"
+#define _WINDOWS_
+#include "miniaudio.h"
 
 
 #include <raylib.h>
 #include <raylib.h>
 #include <rlgl.h>
 #include <rlgl.h>
@@ -68,7 +69,7 @@ typedef struct {
 
 
     // Microphone
     // Microphone
     bool capturing;
     bool capturing;
-    void *microphone;
+    ma_device *microphone;
 } Plug;
 } Plug;
 
 
 Plug *p = NULL;
 Plug *p = NULL;
@@ -273,6 +274,12 @@ void callback(void *bufferData, unsigned int frames)
         fft_push(fs[i][0]);
         fft_push(fs[i][0]);
     }
     }
 }
 }
+void ma_callback(ma_device *pDevice, void *pOutput, const void *pInput,ma_uint32 frameCount)
+{
+    callback((void*)pInput,frameCount);
+    (void)pOutput;
+    (void)pDevice;
+}
 
 
 void plug_init(void)
 void plug_init(void)
 {
 {
@@ -326,7 +333,7 @@ void plug_update(void)
         if (p->capturing) {
         if (p->capturing) {
             if (p->microphone != NULL) {
             if (p->microphone != NULL) {
                 if (IsKeyPressed(KEY_ESCAPE) || IsKeyPressed(KEY_M)) {
                 if (IsKeyPressed(KEY_ESCAPE) || IsKeyPressed(KEY_M)) {
-                    uninit_capture_device(p->microphone);
+                    ma_device_uninit(p->microphone);
                     p->microphone = NULL;
                     p->microphone = NULL;
                     p->capturing = false;
                     p->capturing = false;
                 }
                 }
@@ -383,10 +390,23 @@ void plug_update(void)
 
 
             if (IsKeyPressed(KEY_M)) {
             if (IsKeyPressed(KEY_M)) {
                 // TODO: let the user choose their mic
                 // TODO: let the user choose their mic
-                p->microphone = init_default_capture_device(callback);
+                ma_device_config deviceConfig = ma_device_config_init(ma_device_type_capture);
+                deviceConfig.capture.format = ma_format_f32;
+                deviceConfig.capture.channels = 2;
+                deviceConfig.sampleRate = 44100;
+                deviceConfig.dataCallback = ma_callback;
+                deviceConfig.pUserData = NULL;
+                p->microphone = malloc(sizeof(ma_device));
+                assert(p->microphone != NULL && "Buy MORE RAM lol!!");
+                ma_result result = ma_device_init(NULL, &deviceConfig, p->microphone);
+                if (result != MA_SUCCESS) {
+                    TraceLog(LOG_ERROR,"MINIAUDIO: Failed to initialize capture device: %s",ma_result_description(result));
+                }
                 if (p->microphone != NULL) {
                 if (p->microphone != NULL) {
-                    if (!start_capture_device(p->microphone)) {
-                        uninit_capture_device(p->microphone);
+                    ma_result result = ma_device_start(p->microphone);
+                    if (result != MA_SUCCESS) {
+                        TraceLog(LOG_ERROR, "MINIAUDIO: Failed to start device: %s",ma_result_description(result));
+                        ma_device_uninit(p->microphone);
                         p->microphone = NULL;
                         p->microphone = NULL;
                     }
                     }
                 }
                 }

+ 0 - 40
src/raylib_log.h

@@ -1,40 +0,0 @@
-// NOTE: In case you can't include the entire raylib.h (for instance when you also want to include windows.h)
-// but still need its logging capabilities, just include this raylib_log.h file.
-// TODO: maybe we should implement our own logging interface that let's you just plug raylib's logging?
-
-#ifndef RAYLIB_LOG_H_
-#define RAYLIB_LOG_H_
-
-// Function specifiers in case library is build/used as a shared library (Windows)
-// NOTE: Microsoft specifiers to tell compiler that symbols are imported/exported from a .dll
-#if defined(_WIN32)
-    #if defined(BUILD_LIBTYPE_SHARED)
-        #if defined(__TINYC__)
-            #define __declspec(x) __attribute__((x))
-        #endif
-        #define RLAPI __declspec(dllexport)     // We are building the library as a Win32 shared library (.dll)
-    #elif defined(USE_LIBTYPE_SHARED)
-        #define RLAPI __declspec(dllimport)     // We are using the library as a Win32 shared library (.dll)
-    #endif
-#endif
-
-#ifndef RLAPI
-    #define RLAPI       // Functions defined as 'extern' by default (implicit specifiers)
-#endif
-
-// Trace log level
-// NOTE: Organized by priority level
-typedef enum {
-    LOG_ALL = 0,        // Display all logs
-    LOG_TRACE,          // Trace logging, intended for internal use only
-    LOG_DEBUG,          // Debug logging, used for internal debugging, it should be disabled on release builds
-    LOG_INFO,           // Info logging, used for program execution info
-    LOG_WARNING,        // Warning logging, used on recoverable failures
-    LOG_ERROR,          // Error logging, used on unrecoverable failures
-    LOG_FATAL,          // Fatal logging, used to abort program: exit(EXIT_FAILURE)
-    LOG_NONE            // Disable logging
-} TraceLogLevel;
-
-RLAPI void TraceLog(int logLevel, const char *text, ...);
-
-#endif // RAYLIB_LOG_H_

+ 0 - 52
src/separate_translation_unit_for_miniaudio.c

@@ -1,52 +0,0 @@
-#include <assert.h>
-#include <stdlib.h>
-#include <stdio.h>
-
-#include "separate_translation_unit_for_miniaudio.h"
-
-#include "miniaudio.h"
-#include "raylib_log.h"
-
-void data_callback(ma_device* pDevice, void* pOutput, const void* pInput, ma_uint32 frameCount)
-{
-    capture_device_callback2_t *user_callback = (capture_device_callback2_t*)pDevice->pUserData;
-    assert(user_callback != NULL);
-    user_callback((void*)pInput, frameCount);
-    (void)pOutput;
-}
-
-Capture_Device *init_default_capture_device(capture_device_callback2_t *capture_device_callback)
-{
-    ma_device_config deviceConfig = ma_device_config_init(ma_device_type_capture);
-    deviceConfig.capture.format   = ma_format_f32;
-    deviceConfig.capture.channels = 2;
-    deviceConfig.sampleRate       = 44100;
-    deviceConfig.dataCallback     = data_callback;
-    deviceConfig.pUserData        = capture_device_callback;
-
-    ma_device *device = malloc(sizeof(ma_device));
-    assert(device != NULL && "Buy MORE RAM lol!!");
-    ma_result result = ma_device_init(NULL, &deviceConfig, device);
-    if (result != MA_SUCCESS) {
-        TraceLog(LOG_ERROR, "MINIAUDIO: Failed to initialize capture device: %s", ma_result_description(result));
-        return NULL;
-    }
-
-    return device;
-}
-
-void uninit_capture_device(Capture_Device *device)
-{
-    ma_device_uninit(device);
-}
-
-bool start_capture_device(Capture_Device *device)
-{
-    ma_result result = ma_device_start(device);
-    if (result != MA_SUCCESS) {
-        TraceLog(LOG_ERROR, "MINIAUDIO: Failed to start device: %s", ma_result_description(result));
-        return false;
-    }
-
-    return true;
-}

+ 0 - 18
src/separate_translation_unit_for_miniaudio.h

@@ -1,18 +0,0 @@
-// Since miniaudio.h includes windows.h on Windows this separate translation
-// unit was specifically created to avoid collisions between symbols from windows.h
-// and raylib.h. The interface of this unit should not depend on anything
-// from miniaudio.h.
-
-#ifndef SEPARATE_TRANSLATION_UNIT_FOR_MINIAUDIO_H_
-#define SEPARATE_TRANSLATION_UNIT_FOR_MINIAUDIO_H_
-
-#include <stdbool.h>
-
-typedef void (capture_device_callback2_t)(void *bufferData, unsigned int frames);
-typedef void Capture_Device;
-
-Capture_Device *init_default_capture_device(capture_device_callback2_t *capture_device_callback);
-void uninit_capture_device(Capture_Device *device);
-bool start_capture_device(Capture_Device *device);
-
-#endif  // SEPARATE_TRANSLATION_UNIT_FOR_MINIAUDIO_H_