From ab36fbf24aa7fd89bfe0414276b0a962812ff62c Mon Sep 17 00:00:00 2001 From: Ray Date: Tue, 2 Mar 2021 12:45:23 +0100 Subject: [PATCH] Reviewed defines, try to avoid elif statements --- src/core.c | 59 ++++++++++++++++++++++++------------------- src/physac.h | 6 +++-- src/rlgl.h | 70 +++++++++++++++++++++++++++++++--------------------- 3 files changed, 80 insertions(+), 55 deletions(-) diff --git a/src/core.c b/src/core.c index 40962138..d5f58841 100644 --- a/src/core.c +++ b/src/core.c @@ -1445,18 +1445,20 @@ void *GetWindowHandle(void) #if defined(PLATFORM_DESKTOP) && defined(_WIN32) // NOTE: Returned handle is: void *HWND (windows.h) return glfwGetWin32Window(CORE.Window.handle); -#elif defined(__linux__) +#endif +#if defined(__linux__) // NOTE: Returned handle is: unsigned long Window (X.h) // typedef unsigned long XID; // typedef XID Window; //unsigned long id = (unsigned long)glfwGetX11Window(window); return NULL; // TODO: Find a way to return value... cast to void *? -#elif defined(__APPLE__) +#endif +#if defined(__APPLE__) // NOTE: Returned handle is: (objc_object *) return NULL; // TODO: return (void *)glfwGetCocoaWindow(window); -#else - return NULL; #endif + + return NULL; } // Get number of monitors @@ -2717,6 +2719,7 @@ bool SaveStorageValue(unsigned int position, int value) int LoadStorageValue(unsigned int position) { int value = 0; + #if defined(SUPPORT_DATA_STORAGE) char path[512] = { 0 }; #if defined(PLATFORM_ANDROID) @@ -2768,9 +2771,11 @@ void OpenURL(const char *url) char *cmd = (char *)RL_CALLOC(strlen(url) + 10, sizeof(char)); #if defined(_WIN32) sprintf(cmd, "explorer %s", url); - #elif defined(__linux__) + #endif + #if defined(__linux__) || defined(__FreeBSD__) sprintf(cmd, "xdg-open '%s'", url); // Alternatives: firefox, x-www-browser - #elif defined(__APPLE__) + #endif + #if defined(__APPLE__) sprintf(cmd, "open '%s'", url); #endif system(cmd); @@ -2904,13 +2909,12 @@ const char *GetGamepadName(int gamepad) #if defined(PLATFORM_DESKTOP) if (CORE.Input.Gamepad.ready[gamepad]) return glfwGetJoystickName(gamepad); else return NULL; -#elif defined(PLATFORM_RPI) || defined(PLATFORM_DRM) - if (CORE.Input.Gamepad.ready[gamepad]) ioctl(CORE.Input.Gamepad.streamId[gamepad], JSIOCGNAME(64), &CORE.Input.Gamepad.name); - - return CORE.Input.Gamepad.name; -#else - return NULL; #endif +#if defined(PLATFORM_RPI) || defined(PLATFORM_DRM) + if (CORE.Input.Gamepad.ready[gamepad]) ioctl(CORE.Input.Gamepad.streamId[gamepad], JSIOCGNAME(64), &CORE.Input.Gamepad.name); + return CORE.Input.Gamepad.name; +#endif + return NULL; } // Return gamepad axis count @@ -3112,11 +3116,12 @@ float GetMouseWheelMove(void) { #if defined(PLATFORM_ANDROID) return 0.0f; -#elif defined(PLATFORM_WEB) - return CORE.Input.Mouse.previousWheelMove/100.0f; -#else - return CORE.Input.Mouse.previousWheelMove; #endif +#if defined(PLATFORM_WEB) + return CORE.Input.Mouse.previousWheelMove/100.0f; +#endif + + return CORE.Input.Mouse.previousWheelMove; } // Returns mouse cursor @@ -3166,11 +3171,16 @@ Vector2 GetTouchPosition(int index) { Vector2 position = { -1.0f, -1.0f }; -#if defined(PLATFORM_ANDROID) || defined(PLATFORM_WEB) || defined(PLATFORM_RPI) || defined(PLATFORM_DRM) || defined(PLATFORM_UWP) +#if defined(PLATFORM_DESKTOP) + // TODO: GLFW does not support multi-touch input just yet + // https://www.codeproject.com/Articles/668404/Programming-for-Multi-Touch + // https://docs.microsoft.com/en-us/windows/win32/wintouch/getting-started-with-multi-touch-messages + if (index == 0) position = GetMousePosition(); +#endif +#if defined(PLATFORM_ANDROID) if (index < MAX_TOUCH_POINTS) position = CORE.Input.Touch.position[index]; else TRACELOG(LOG_WARNING, "INPUT: Required touch point out of range (Max touch points: %i)", MAX_TOUCH_POINTS); - #if defined(PLATFORM_ANDROID) if ((CORE.Window.screen.width > CORE.Window.display.width) || (CORE.Window.screen.height > CORE.Window.display.height)) { position.x = position.x*((float)CORE.Window.screen.width/(float)(CORE.Window.display.width - CORE.Window.renderOffset.x)) - CORE.Window.renderOffset.x/2; @@ -3181,13 +3191,12 @@ Vector2 GetTouchPosition(int index) position.x = position.x*((float)CORE.Window.render.width/(float)CORE.Window.display.width) - CORE.Window.renderOffset.x/2; position.y = position.y*((float)CORE.Window.render.height/(float)CORE.Window.display.height) - CORE.Window.renderOffset.y/2; } - #endif - -#elif defined(PLATFORM_DESKTOP) - // TODO: GLFW is not supporting multi-touch input just yet - // https://www.codeproject.com/Articles/668404/Programming-for-Multi-Touch - // https://docs.microsoft.com/en-us/windows/win32/wintouch/getting-started-with-multi-touch-messages - if (index == 0) position = GetMousePosition(); +#endif +#if defined(PLATFORM_WEB) || defined(PLATFORM_RPI) || defined(PLATFORM_DRM) || defined(PLATFORM_UWP) + if (index < MAX_TOUCH_POINTS) position = CORE.Input.Touch.position[index]; + else TRACELOG(LOG_WARNING, "INPUT: Required touch point out of range (Max touch points: %i)", MAX_TOUCH_POINTS); + + // TODO: Touch position scaling required? #endif return position; diff --git a/src/physac.h b/src/physac.h index fde0b90e..4b419bba 100644 --- a/src/physac.h +++ b/src/physac.h @@ -258,13 +258,15 @@ PHYSACDEF Vector2 GetPhysicsShapeVertex(PhysicsBody body, int vertex); // Functions required to query time on Windows int __stdcall QueryPerformanceCounter(unsigned long long int *lpPerformanceCount); int __stdcall QueryPerformanceFrequency(unsigned long long int *lpFrequency); - #elif defined(__linux__) + #endif + #if defined(__linux__) || defined(__FreeBSD__) #if _POSIX_C_SOURCE < 199309L #undef _POSIX_C_SOURCE #define _POSIX_C_SOURCE 199309L // Required for CLOCK_MONOTONIC if compiled with c99 without gnu ext. #endif #include // Required for: timespec - #elif defined(__APPLE__) // macOS also defines __MACH__ + #endif + #if defined(__APPLE__) // macOS also defines __MACH__ #include // Required for: mach_absolute_time() #endif #endif diff --git a/src/rlgl.h b/src/rlgl.h index 70b0471e..703821f0 100644 --- a/src/rlgl.h +++ b/src/rlgl.h @@ -136,7 +136,8 @@ // This is the maximum amount of elements (quads) per batch // NOTE: Be careful with text, every letter maps to a quad #define DEFAULT_BATCH_BUFFER_ELEMENTS 8192 - #elif defined(GRAPHICS_API_OPENGL_ES2) + #endif + #if defined(GRAPHICS_API_OPENGL_ES2) // We reduce memory sizes for embedded systems (RPI and HTML5) // NOTE: On HTML5 (emscripten) this is allocated on heap, // by default it's only 16MB!...just take care... @@ -811,7 +812,8 @@ typedef struct VertexBuffer { unsigned char *colors; // Vertex colors (RGBA - 4 components per vertex) (shader-location = 3) #if defined(GRAPHICS_API_OPENGL_11) || defined(GRAPHICS_API_OPENGL_33) unsigned int *indices; // Vertex indices (in case vertex data comes indexed) (6 indices per quad) -#elif defined(GRAPHICS_API_OPENGL_ES2) +#endif +#if defined(GRAPHICS_API_OPENGL_ES2) unsigned short *indices; // Vertex indices (in case vertex data comes indexed) (6 indices per quad) #endif unsigned int vaoId; // OpenGL Vertex Array Object id @@ -970,7 +972,6 @@ static Color *GenNextMipmap(Color *srcData, int srcWidth, int srcHeight); //---------------------------------------------------------------------------------- #if defined(GRAPHICS_API_OPENGL_11) - // Fallback to OpenGL 1.1 function calls //--------------------------------------- void rlMatrixMode(int mode) @@ -1001,9 +1002,8 @@ void rlTranslatef(float x, float y, float z) { glTranslatef(x, y, z); } void rlRotatef(float angleDeg, float x, float y, float z) { glRotatef(angleDeg, x, y, z); } void rlScalef(float x, float y, float z) { glScalef(x, y, z); } void rlMultMatrixf(float *matf) { glMultMatrixf(matf); } - -#elif defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) - +#endif +#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) // Choose the current matrix to be transformed void rlMatrixMode(int mode) { @@ -1124,7 +1124,6 @@ void rlViewport(int x, int y, int width, int height) // Module Functions Definition - Vertex level operations //---------------------------------------------------------------------------------- #if defined(GRAPHICS_API_OPENGL_11) - // Fallback to OpenGL 1.1 function calls //--------------------------------------- void rlBegin(int mode) @@ -1147,9 +1146,8 @@ void rlNormal3f(float x, float y, float z) { glNormal3f(x, y, z); } void rlColor4ub(unsigned char r, unsigned char g, unsigned char b, unsigned char a) { glColor4ub(r, g, b, a); } void rlColor3f(float x, float y, float z) { glColor3f(x, y, z); } void rlColor4f(float x, float y, float z, float w) { glColor4f(x, y, z, w); } - -#elif defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) - +#endif +#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) // Initialize drawing mode (how to organize vertex) void rlBegin(int mode) { @@ -1898,15 +1896,18 @@ int rlGetVersion(void) { #if defined(GRAPHICS_API_OPENGL_11) return OPENGL_11; -#elif defined(GRAPHICS_API_OPENGL_21) +#endif +#if defined(GRAPHICS_API_OPENGL_21) #if defined(__APPLE__) return OPENGL_33; // NOTE: Force OpenGL 3.3 on OSX #else return OPENGL_21; #endif -#elif defined(GRAPHICS_API_OPENGL_33) +#endif +#if defined(GRAPHICS_API_OPENGL_33) return OPENGL_33; -#elif defined(GRAPHICS_API_OPENGL_ES2) +#endif +#if defined(GRAPHICS_API_OPENGL_ES2) return OPENGL_ES_20; #endif } @@ -1951,7 +1952,8 @@ void rlLoadExtensions(void *loader) #if defined(GRAPHICS_API_OPENGL_21) if (GLAD_GL_VERSION_2_1) TRACELOG(LOG_INFO, "GL: OpenGL 2.1 profile supported"); - #elif defined(GRAPHICS_API_OPENGL_33) + #endif + #if defined(GRAPHICS_API_OPENGL_33) if (GLAD_GL_VERSION_3_3) TRACELOG(LOG_INFO, "GL: OpenGL 3.3 Core profile supported"); else TRACELOG(LOG_ERROR, "GL: OpenGL 3.3 Core profile not supported"); #endif @@ -2051,7 +2053,8 @@ unsigned int rlLoadTexture(void *data, int width, int height, int format, int mi { #if defined(GRAPHICS_API_OPENGL_21) GLint swizzleMask[] = { GL_RED, GL_RED, GL_RED, GL_ALPHA }; - #elif defined(GRAPHICS_API_OPENGL_33) + #endif + #if defined(GRAPHICS_API_OPENGL_33) GLint swizzleMask[] = { GL_RED, GL_RED, GL_RED, GL_GREEN }; #endif glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_RGBA, swizzleMask); @@ -2218,7 +2221,8 @@ unsigned int rlLoadTextureCubemap(void *data, int size, int format) { #if defined(GRAPHICS_API_OPENGL_21) GLint swizzleMask[] = { GL_RED, GL_RED, GL_RED, GL_ALPHA }; -#elif defined(GRAPHICS_API_OPENGL_33) +#endif +#if defined(GRAPHICS_API_OPENGL_33) GLint swizzleMask[] = { GL_RED, GL_RED, GL_RED, GL_GREEN }; #endif glTexParameteriv(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_SWIZZLE_RGBA, swizzleMask); @@ -2284,7 +2288,8 @@ void rlGetGlTextureFormats(int format, unsigned int *glInternalFormat, unsigned case UNCOMPRESSED_R32G32B32: if (RLGL.ExtSupported.texFloat32) *glInternalFormat = GL_RGB; *glFormat = GL_RGB; *glType = GL_FLOAT; break; // NOTE: Requires extension OES_texture_float case UNCOMPRESSED_R32G32B32A32: if (RLGL.ExtSupported.texFloat32) *glInternalFormat = GL_RGBA; *glFormat = GL_RGBA; *glType = GL_FLOAT; break; // NOTE: Requires extension OES_texture_float #endif - #elif defined(GRAPHICS_API_OPENGL_33) + #endif + #if defined(GRAPHICS_API_OPENGL_33) case UNCOMPRESSED_GRAYSCALE: *glInternalFormat = GL_R8; *glFormat = GL_RED; *glType = GL_UNSIGNED_BYTE; break; case UNCOMPRESSED_GRAY_ALPHA: *glInternalFormat = GL_RG8; *glFormat = GL_RG; *glType = GL_UNSIGNED_BYTE; break; case UNCOMPRESSED_R5G6B5: *glInternalFormat = GL_RGB565; *glFormat = GL_RGB; *glType = GL_UNSIGNED_SHORT_5_6_5; break; @@ -2296,7 +2301,7 @@ void rlGetGlTextureFormats(int format, unsigned int *glInternalFormat, unsigned case UNCOMPRESSED_R32G32B32: if (RLGL.ExtSupported.texFloat32) *glInternalFormat = GL_RGB32F; *glFormat = GL_RGB; *glType = GL_FLOAT; break; case UNCOMPRESSED_R32G32B32A32: if (RLGL.ExtSupported.texFloat32) *glInternalFormat = GL_RGBA32F; *glFormat = GL_RGBA; *glType = GL_FLOAT; break; #endif - #if !defined(GRAPHICS_API_OPENGL_11) + #if !defined(GRAPHICS_API_OPENGL_11) case COMPRESSED_DXT1_RGB: if (RLGL.ExtSupported.texCompDXT) *glInternalFormat = GL_COMPRESSED_RGB_S3TC_DXT1_EXT; break; case COMPRESSED_DXT1_RGBA: if (RLGL.ExtSupported.texCompDXT) *glInternalFormat = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT; break; case COMPRESSED_DXT3_RGBA: if (RLGL.ExtSupported.texCompDXT) *glInternalFormat = GL_COMPRESSED_RGBA_S3TC_DXT3_EXT; break; @@ -2308,7 +2313,7 @@ void rlGetGlTextureFormats(int format, unsigned int *glInternalFormat, unsigned case COMPRESSED_PVRT_RGBA: if (RLGL.ExtSupported.texCompPVRT) *glInternalFormat = GL_COMPRESSED_RGBA_PVRTC_4BPPV1_IMG; break; // NOTE: Requires PowerVR GPU case COMPRESSED_ASTC_4x4_RGBA: if (RLGL.ExtSupported.texCompASTC) *glInternalFormat = GL_COMPRESSED_RGBA_ASTC_4x4_KHR; break; // NOTE: Requires OpenGL ES 3.1 or OpenGL 4.3 case COMPRESSED_ASTC_8x8_RGBA: if (RLGL.ExtSupported.texCompASTC) *glInternalFormat = GL_COMPRESSED_RGBA_ASTC_8x8_KHR; break; // NOTE: Requires OpenGL ES 3.1 or OpenGL 4.3 - #endif + #endif default: TRACELOG(LOG_WARNING, "TEXTURE: Current format not supported (%i)", format); break; } } @@ -2456,7 +2461,8 @@ void rlGenerateMipmaps(Texture2D *texture) } else TRACELOG(LOG_WARNING, "TEXTURE: [ID %i] Failed to generate mipmaps for provided texture format", texture->id); } -#elif defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) +#endif +#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) if ((texIsPOT) || (RLGL.ExtSupported.texNPOT)) { //glHint(GL_GENERATE_MIPMAP_HINT, GL_DONT_CARE); // Hint for mipmaps generation algorythm: GL_FASTEST, GL_NICEST, GL_DONT_CARE @@ -4140,7 +4146,8 @@ static Shader LoadShaderDefault(void) const char *defaultVShaderStr = #if defined(GRAPHICS_API_OPENGL_21) "#version 120 \n" -#elif defined(GRAPHICS_API_OPENGL_ES2) +#endif +#if defined(GRAPHICS_API_OPENGL_ES2) "#version 100 \n" #endif #if defined(GRAPHICS_API_OPENGL_ES2) || defined(GRAPHICS_API_OPENGL_21) @@ -4149,7 +4156,8 @@ static Shader LoadShaderDefault(void) "attribute vec4 vertexColor; \n" "varying vec2 fragTexCoord; \n" "varying vec4 fragColor; \n" -#elif defined(GRAPHICS_API_OPENGL_33) +#endif +#if defined(GRAPHICS_API_OPENGL_33) "#version 330 \n" "in vec3 vertexPosition; \n" "in vec2 vertexTexCoord; \n" @@ -4169,14 +4177,16 @@ static Shader LoadShaderDefault(void) const char *defaultFShaderStr = #if defined(GRAPHICS_API_OPENGL_21) "#version 120 \n" -#elif defined(GRAPHICS_API_OPENGL_ES2) +#endif +#if defined(GRAPHICS_API_OPENGL_ES2) "#version 100 \n" "precision mediump float; \n" // precision required for OpenGL ES2 (WebGL) #endif #if defined(GRAPHICS_API_OPENGL_ES2) || defined(GRAPHICS_API_OPENGL_21) "varying vec2 fragTexCoord; \n" "varying vec4 fragColor; \n" -#elif defined(GRAPHICS_API_OPENGL_33) +#endif +#if defined(GRAPHICS_API_OPENGL_33) "#version 330 \n" "in vec2 fragTexCoord; \n" "in vec4 fragColor; \n" @@ -4189,7 +4199,8 @@ static Shader LoadShaderDefault(void) #if defined(GRAPHICS_API_OPENGL_ES2) || defined(GRAPHICS_API_OPENGL_21) " vec4 texelColor = texture2D(texture0, fragTexCoord); \n" // NOTE: texture2D() is deprecated on OpenGL 3.3 and ES 3.0 " gl_FragColor = texelColor*colDiffuse*fragColor; \n" -#elif defined(GRAPHICS_API_OPENGL_33) +#endif +#if defined(GRAPHICS_API_OPENGL_33) " vec4 texelColor = texture(texture0, fragTexCoord); \n" " finalColor = texelColor*colDiffuse*fragColor; \n" #endif @@ -4289,7 +4300,8 @@ static RenderBatch LoadRenderBatch(int numBuffers, int bufferElements) batch.vertexBuffer[i].colors = (unsigned char *)RL_MALLOC(bufferElements*4*4*sizeof(unsigned char)); // 4 float by color, 4 colors by quad #if defined(GRAPHICS_API_OPENGL_33) batch.vertexBuffer[i].indices = (unsigned int *)RL_MALLOC(bufferElements*6*sizeof(unsigned int)); // 6 int by quad (indices) -#elif defined(GRAPHICS_API_OPENGL_ES2) +#endif +#if defined(GRAPHICS_API_OPENGL_ES2) batch.vertexBuffer[i].indices = (unsigned short *)RL_MALLOC(bufferElements*6*sizeof(unsigned short)); // 6 int by quad (indices) #endif @@ -4358,7 +4370,8 @@ static RenderBatch LoadRenderBatch(int numBuffers, int bufferElements) glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, batch.vertexBuffer[i].vboId[3]); #if defined(GRAPHICS_API_OPENGL_33) glBufferData(GL_ELEMENT_ARRAY_BUFFER, bufferElements*6*sizeof(int), batch.vertexBuffer[i].indices, GL_STATIC_DRAW); -#elif defined(GRAPHICS_API_OPENGL_ES2) +#endif +#if defined(GRAPHICS_API_OPENGL_ES2) glBufferData(GL_ELEMENT_ARRAY_BUFFER, bufferElements*6*sizeof(short), batch.vertexBuffer[i].indices, GL_STATIC_DRAW); #endif } @@ -4519,7 +4532,8 @@ static void DrawRenderBatch(RenderBatch *batch) // NOTE: The final parameter tells the GPU the offset in bytes from the // start of the index buffer to the location of the first index to process glDrawElements(GL_TRIANGLES, batch->draws[i].vertexCount/4*6, GL_UNSIGNED_INT, (GLvoid *)(vertexOffset/4*6*sizeof(GLuint))); -#elif defined(GRAPHICS_API_OPENGL_ES2) +#endif +#if defined(GRAPHICS_API_OPENGL_ES2) glDrawElements(GL_TRIANGLES, batch->draws[i].vertexCount/4*6, GL_UNSIGNED_SHORT, (GLvoid *)(vertexOffset/4*6*sizeof(GLushort))); #endif }