From 8b7ca8b670a0f338fef85125311522833b945bf7 Mon Sep 17 00:00:00 2001 From: raysan5 Date: Sun, 27 Mar 2016 18:32:36 +0200 Subject: Review comments --- src/raylib.h | 20 ++++++++++---------- src/textures.c | 19 ++++++++++--------- 2 files changed, 20 insertions(+), 19 deletions(-) (limited to 'src') diff --git a/src/raylib.h b/src/raylib.h index 1782fef3..2b65df9e 100644 --- a/src/raylib.h +++ b/src/raylib.h @@ -1,6 +1,6 @@ /********************************************************************************************** * -* raylib 1.4.0 (www.raylib.com) +* raylib 1.5.0 (www.raylib.com) * * A simple and easy-to-use library to learn videogames programming * @@ -345,8 +345,8 @@ typedef struct Camera { // Camera2D type, defines a 2d camera typedef struct Camera2D { - Vector2 position; // Camera position - Vector2 origin; // Camera origin (for rotation and zoom) + Vector2 offset; // Camera offset (displacement from target) + Vector2 target; // Camera target (for rotation and zoom) float rotation; // Camera rotation in degrees float zoom; // Camera zoom (scaling), should be 1.0f by default } Camera2D; @@ -375,7 +375,7 @@ typedef struct Mesh { // Shader type (generic shader) typedef struct Shader { - unsigned int id; // Shader program id + unsigned int id; // Shader program id // Variable attributes int vertexLoc; // Vertex attribute location point (vertex shader) @@ -411,9 +411,9 @@ typedef struct Material { // 3d Model type // TODO: Replace shader/testure by material typedef struct Model { - Mesh mesh; - Matrix transform; - Material material; + Mesh mesh; // Vertex data buffers (RAM and VRAM) + Matrix transform; // Local transform matrix + Material material; // Shader and textures data } Model; // Ray type (useful for raycast) @@ -432,8 +432,8 @@ typedef struct Sound { typedef struct Wave { void *data; // Buffer data pointer unsigned int dataSize; // Data size in bytes - unsigned int sampleRate; - short bitsPerSample; + unsigned int sampleRate; // Samples per second to be played + short bitsPerSample; // Sample size in bits short channels; } Wave; @@ -484,7 +484,7 @@ typedef enum { TOUCH_UP, TOUCH_DOWN, TOUCH_MOVE } TouchAction; // Gesture events // NOTE: MAX_TOUCH_POINTS fixed to 2 -typedef struct { +typedef struct GestureEvent { int touchAction; int pointCount; int pointerId[MAX_TOUCH_POINTS]; diff --git a/src/textures.c b/src/textures.c index e649df57..9d0d13b6 100644 --- a/src/textures.c +++ b/src/textures.c @@ -29,21 +29,21 @@ #include "raylib.h" -#include // Declares malloc() and free() for memory management -#include // Required for strcmp(), strrchr(), strncmp() +#include // Declares malloc() and free() for memory management +#include // Required for strcmp(), strrchr(), strncmp() -#include "rlgl.h" // raylib OpenGL abstraction layer to OpenGL 1.1, 3.3 or ES2 - // Required: rlglLoadTexture() rlDeleteTextures(), - // rlglGenerateMipmaps(), some funcs for DrawTexturePro() +#include "rlgl.h" // raylib OpenGL abstraction layer to OpenGL 1.1, 3.3 or ES2 + // Required: rlglLoadTexture() rlDeleteTextures(), + // rlglGenerateMipmaps(), some funcs for DrawTexturePro() -#include "utils.h" // rRES data decompression utility function - // NOTE: Includes Android fopen function map +#include "utils.h" // rRES data decompression utility function + // NOTE: Includes Android fopen function map #define STB_IMAGE_IMPLEMENTATION -#include "stb_image.h" // Used to read image data (multiple formats support) +#include "stb_image.h" // Used to read image data (multiple formats support) #define STB_IMAGE_RESIZE_IMPLEMENTATION -#include "stb_image_resize.h" +#include "stb_image_resize.h" // Used on image scaling function: ImageResize() //---------------------------------------------------------------------------------- // Defines and Macros @@ -130,6 +130,7 @@ Image LoadImage(const char *fileName) } // Load image data from Color array data (RGBA - 32bit) +// NOTE: Creates a copy of pixels data array Image LoadImageEx(Color *pixels, int width, int height) { Image image; -- cgit v1.2.3 From 956a6e6f7713a19e746497efb9e909ba88be9c3d Mon Sep 17 00:00:00 2001 From: raysan5 Date: Sun, 27 Mar 2016 18:33:30 +0200 Subject: Corrected bug and comments on model unloading --- src/models.c | 5 ++--- src/rlgl.c | 18 +++++++++--------- 2 files changed, 11 insertions(+), 12 deletions(-) (limited to 'src') diff --git a/src/models.c b/src/models.c index a1590424..52c68f9b 100644 --- a/src/models.c +++ b/src/models.c @@ -608,6 +608,8 @@ void UnloadModel(Model model) //if (model.mesh.texcoords2 != NULL) free(model.mesh.texcoords2); // Not used //if (model.mesh.tangents != NULL) free(model.mesh.tangents); // Not used + TraceLog(INFO, "Unloaded model data from RAM (CPU)"); + rlDeleteBuffers(model.mesh.vboId[0]); // vertex rlDeleteBuffers(model.mesh.vboId[1]); // texcoords rlDeleteBuffers(model.mesh.vboId[2]); // normals @@ -616,9 +618,6 @@ void UnloadModel(Model model) //rlDeleteBuffers(model.mesh.vboId[5]); // colors (NOT USED) rlDeleteVertexArrays(model.mesh.vaoId); - - if (model.mesh.vaoId > 0) TraceLog(INFO, "[VAO ID %i] Unloaded model data from VRAM (GPU)", model.mesh.vaoId); - else TraceLog(INFO, "[VBO ID %i][VBO ID %i][VBO ID %i] Unloaded model data from VRAM (GPU)", model.mesh.vboId[0], model.mesh.vboId[1], model.mesh.vboId[2]); } // Link a texture to a model diff --git a/src/rlgl.c b/src/rlgl.c index fc14a0af..39a34095 100644 --- a/src/rlgl.c +++ b/src/rlgl.c @@ -823,7 +823,11 @@ void rlDeleteShader(unsigned int id) void rlDeleteVertexArrays(unsigned int id) { #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) - if (vaoSupported) glDeleteVertexArrays(1, &id); + if (vaoSupported) + { + glDeleteVertexArrays(1, &id); + TraceLog(INFO, "[VAO ID %i] Unloaded model data from VRAM (GPU)", id); + } #endif } @@ -832,6 +836,8 @@ void rlDeleteBuffers(unsigned int id) { #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) glDeleteBuffers(1, &id); + + if (!vaoSupported) TraceLog(INFO, "[VBO ID %i] Unloaded model vertex data from VRAM (GPU)", id); #endif } @@ -1139,7 +1145,7 @@ FBO rlglLoadFBO(int width, int height) GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER); - if (status != GL_FRAMEBUFFER_COMPLETE) + if (status != GL_FRAMEBUFFER_COMPLETE) { TraceLog(WARNING, "Framebuffer object could not be created..."); @@ -1238,12 +1244,6 @@ void rlglClose(void) rlglUnloadFBO(postproFbo); // Unload postpro quad model data -#if defined(GRAPHICS_API_OPENGL_11) - free(postproQuad.mesh.vertices); - free(postproQuad.mesh.texcoords); - free(postproQuad.mesh.normals); -#endif - rlDeleteBuffers(postproQuad.mesh.vboId[0]); rlDeleteBuffers(postproQuad.mesh.vboId[1]); rlDeleteBuffers(postproQuad.mesh.vboId[2]); @@ -1907,7 +1907,7 @@ void rlglGenerateMipmaps(Texture2D texture) TraceLog(WARNING, "[TEX ID %i] Mipmaps generated manually on CPU side", texture.id); // NOTE: Once mipmaps have been generated and data has been uploaded to GPU VRAM, we can discard RAM data - free(data): + free(data); #elif defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) glGenerateMipmap(GL_TEXTURE_2D); // Generate mipmaps automatically -- cgit v1.2.3 From a3f16c84594a4811219892e444350d7d3441a6ae Mon Sep 17 00:00:00 2001 From: raysan5 Date: Sun, 27 Mar 2016 18:33:54 +0200 Subject: Improved 2d camera system -IN PROGRESS- --- src/core.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/core.c b/src/core.c index c05de93b..2ab39ab0 100644 --- a/src/core.c +++ b/src/core.c @@ -560,14 +560,14 @@ void BeginDrawingEx(Camera2D camera) { BeginDrawing(); - // TODO: Consider origin offset on position, rotation, scaling - + // Camera rotation and scaling is always relative to target + Matrix matOrigin = MatrixTranslate(-camera.target.x, -camera.target.y, 0.0f); Matrix matRotation = MatrixRotate((Vector3){ 0.0f, 0.0f, 1.0f }, camera.rotation*DEG2RAD); Matrix matScale = MatrixScale(camera.zoom, camera.zoom, 1.0f); - Matrix matTranslation = MatrixTranslate(camera.position.x, camera.position.y, 0.0f); - Matrix matOrigin = MatrixTranslate(-camera.origin.x, -camera.origin.y, 0.0f); + + Matrix matTranslation = MatrixTranslate(camera.offset.x + camera.target.x, camera.offset.y + camera.target.y, 0.0f); - Matrix matTransform = MatrixMultiply(MatrixMultiply(matScale, matRotation), matTranslation); + Matrix matTransform = MatrixMultiply(MatrixMultiply(matOrigin, MatrixMultiply(matScale, matRotation)), matTranslation); rlMultMatrixf(MatrixToFloat(matTransform)); } -- cgit v1.2.3 From 136408d8b8b096f23953aba3d81b3fbeccdc2680 Mon Sep 17 00:00:00 2001 From: raysan5 Date: Sun, 27 Mar 2016 19:42:57 +0200 Subject: Corrected bug on bounding box if mesh is not loaded properly it breaks the game! --- src/models.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/models.c b/src/models.c index 52c68f9b..a515dd86 100644 --- a/src/models.c +++ b/src/models.c @@ -1328,13 +1328,19 @@ bool CheckCollisionRayBox(Ray ray, BoundingBox box) BoundingBox CalculateBoundingBox(Mesh mesh) { // Get min and max vertex to construct bounds (AABB) - Vector3 minVertex = (Vector3){ mesh.vertices[0], mesh.vertices[1], mesh.vertices[2] }; - Vector3 maxVertex = (Vector3){ mesh.vertices[0], mesh.vertices[1], mesh.vertices[2] }; + Vector3 minVertex = { 0 }; + Vector3 maxVertex = { 0 }; - for (int i = 1; i < mesh.vertexCount; i++) + if (mesh.vertices != NULL) { - minVertex = VectorMin(minVertex, (Vector3){ mesh.vertices[i*3], mesh.vertices[i*3 + 1], mesh.vertices[i*3 + 2] }); - maxVertex = VectorMax(maxVertex, (Vector3){ mesh.vertices[i*3], mesh.vertices[i*3 + 1], mesh.vertices[i*3 + 2] }); + minVertex = (Vector3){ mesh.vertices[0], mesh.vertices[1], mesh.vertices[2] }; + maxVertex = (Vector3){ mesh.vertices[0], mesh.vertices[1], mesh.vertices[2] }; + + for (int i = 1; i < mesh.vertexCount; i++) + { + minVertex = VectorMin(minVertex, (Vector3){ mesh.vertices[i*3], mesh.vertices[i*3 + 1], mesh.vertices[i*3 + 2] }); + maxVertex = VectorMax(maxVertex, (Vector3){ mesh.vertices[i*3], mesh.vertices[i*3 + 1], mesh.vertices[i*3 + 2] }); + } } // Create the bounding box -- cgit v1.2.3 From 1136d4222f81524c10b2319b325fcf1282bc6ec1 Mon Sep 17 00:00:00 2001 From: raysan5 Date: Mon, 28 Mar 2016 01:18:40 +0200 Subject: Setting up for raylib 1.5.0 --- src/core.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/core.c b/src/core.c index 2ab39ab0..5150389c 100644 --- a/src/core.c +++ b/src/core.c @@ -304,7 +304,7 @@ static EM_BOOL EmscriptenInputCallback(int eventType, const EmscriptenTouchEvent // Initialize Window and Graphics Context (OpenGL) void InitWindow(int width, int height, const char *title) { - TraceLog(INFO, "Initializing raylib (v1.4.0)"); + TraceLog(INFO, "Initializing raylib (v1.5.0)"); // Store window title (could be useful...) windowTitle = title; @@ -360,7 +360,7 @@ void InitWindow(int width, int height, const char *title) // Android activity initialization void InitWindow(int width, int height, struct android_app *state) { - TraceLog(INFO, "Initializing raylib (v1.4.0)"); + TraceLog(INFO, "Initializing raylib (v1.5.0)"); app_dummy(); -- cgit v1.2.3 From 66b096d97848eb096a043781f1f305e7189f2a00 Mon Sep 17 00:00:00 2001 From: raysan5 Date: Wed, 30 Mar 2016 20:09:16 +0200 Subject: Added support for render to texture (use RenderTexture2D) Now it's possible to render to texture, old postprocessing system will be removed on next raylib version. --- src/core.c | 22 ++++++++++- src/raylib.h | 11 ++++++ src/rlgl.c | 118 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++- src/rlgl.h | 11 ++++++ src/textures.c | 19 ++++++++++ 5 files changed, 179 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/core.c b/src/core.c index 5150389c..5a794376 100644 --- a/src/core.c +++ b/src/core.c @@ -545,7 +545,7 @@ void BeginDrawing(void) if (IsPosproShaderEnabled()) rlEnablePostproFBO(); - rlClearScreenBuffers(); + rlClearScreenBuffers(); // Clear current framebuffers rlLoadIdentity(); // Reset current matrix (MODELVIEW) @@ -656,6 +656,26 @@ void End3dMode(void) rlDisableDepthTest(); // Disable DEPTH_TEST for 2D } +// Initializes render texture for drawing +void BeginTextureMode(RenderTexture2D target) +{ + rlglDraw(); // Draw Buffers (Only OpenGL 3+ and ES2) + + rlEnableRenderTexture(target.id); + + rlClearScreenBuffers(); // Clear render texture buffers + + rlLoadIdentity(); // Reset current matrix (MODELVIEW) +} + +// Ends drawing to render texture +void EndTextureMode(void) +{ + rlglDraw(); // Draw Buffers (Only OpenGL 3+ and ES2) + + rlDisableRenderTexture(); +} + // Set target FPS for the game void SetTargetFPS(int fps) { diff --git a/src/raylib.h b/src/raylib.h index 2b65df9e..c8f2c2a2 100644 --- a/src/raylib.h +++ b/src/raylib.h @@ -324,6 +324,13 @@ typedef struct Texture2D { int format; // Data format (TextureFormat) } Texture2D; +// RenderTexture2D type, for texture rendering +typedef struct RenderTexture2D { + unsigned int id; // Render texture (fbo) id + Texture2D texture; // Color buffer attachment texture + Texture2D depth; // Depth buffer attachment texture +} RenderTexture2D; + // SpriteFont type, includes texture and charSet array data typedef struct SpriteFont { Texture2D texture; // Font texture @@ -565,6 +572,8 @@ void EndDrawing(void); // End canvas drawin void Begin3dMode(Camera camera); // Initializes 3D mode for drawing (Camera setup) void End3dMode(void); // Ends 3D mode and returns to default 2D orthographic mode +void BeginTextureMode(RenderTexture2D target); // Initializes render texture for drawing +void EndTextureMode(void); // Ends drawing to render texture Ray GetMouseRay(Vector2 mousePosition, Camera camera); // Returns a ray trace from mouse position Vector2 WorldToScreen(Vector3 position, Camera camera); // Returns the screen space position from a 3d world space position @@ -714,8 +723,10 @@ Texture2D LoadTexture(const char *fileName); Texture2D LoadTextureEx(void *data, int width, int height, int textureFormat); // Load a texture from raw data into GPU memory Texture2D LoadTextureFromRES(const char *rresName, int resId); // Load an image as texture from rRES file (raylib Resource) Texture2D LoadTextureFromImage(Image image); // Load a texture from image data +RenderTexture2D LoadRenderTexture(int width, int height); // Load a texture to be used for rendering void UnloadImage(Image image); // Unload image from CPU memory (RAM) void UnloadTexture(Texture2D texture); // Unload texture from GPU memory +void UnloadRenderTexture(RenderTexture2D target); // Unload render texture from GPU memory Color *GetImageData(Image image); // Get pixel data from image as a Color struct array Image GetTextureData(Texture2D texture); // Get pixel data from GPU texture and return an Image void ImageToPOT(Image *image, Color fillColor); // Convert image to POT (power-of-two) diff --git a/src/rlgl.c b/src/rlgl.c index 39a34095..2153221e 100644 --- a/src/rlgl.c +++ b/src/rlgl.c @@ -785,6 +785,20 @@ void rlDisableTexture(void) #endif } +void rlEnableRenderTexture(unsigned int id) +{ +#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) + glBindFramebuffer(GL_FRAMEBUFFER, id); +#endif +} + +void rlDisableRenderTexture(void) +{ +#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) + glBindFramebuffer(GL_FRAMEBUFFER, 0); +#endif +} + // Enable depth test void rlEnableDepthTest(void) { @@ -803,6 +817,16 @@ void rlDeleteTextures(unsigned int id) glDeleteTextures(1, &id); } +// Unload render texture from GPU memory +void rlDeleteRenderTextures(RenderTexture2D target) +{ +#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) + glDeleteFramebuffers(1, &target.id); + glDeleteTextures(1, &target.texture.id); + glDeleteTextures(1, &target.depth.id); +#endif +} + // Enable rendering to postprocessing FBO void rlEnablePostproFBO() { @@ -1163,7 +1187,7 @@ FBO rlglLoadFBO(int width, int height) glDeleteTextures(1, &fbo.colorTextureId); glDeleteTextures(1, &fbo.depthTextureId); } - else TraceLog(INFO, "[FBO ID %i] Framebuffer object created successfully", fbo); + else TraceLog(INFO, "[FBO ID %i] Framebuffer object created successfully", fbo.id); glBindFramebuffer(GL_FRAMEBUFFER, 0); #endif @@ -1833,6 +1857,98 @@ unsigned int rlglLoadTexture(void *data, int width, int height, int textureForma return id; } +// Load a texture to be used for rendering (fbo with color and depth attachments) +RenderTexture2D rlglLoadRenderTexture(int width, int height) +{ + RenderTexture2D target; + + target.id = 0; + + target.texture.id = 0; + target.texture.width = width; + target.texture.height = height; + target.texture.format = UNCOMPRESSED_R8G8B8; + target.texture.mipmaps = 1; + + target.depth.id = 0; + target.depth.width = width; + target.depth.height = height; + target.depth.format = 19; //DEPTH_COMPONENT_16BIT + target.depth.mipmaps = 1; + +#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) + // Create the texture that will serve as the color attachment for the framebuffer + glGenTextures(1, &target.texture.id); + glBindTexture(GL_TEXTURE_2D, target.texture.id); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL); + glBindTexture(GL_TEXTURE_2D, 0); + +#define USE_DEPTH_RENDERBUFFER +#if defined(USE_DEPTH_RENDERBUFFER) + // Create the renderbuffer that will serve as the depth attachment for the framebuffer. + glGenRenderbuffers(1, &target.depth.id); + glBindRenderbuffer(GL_RENDERBUFFER, target.depth.id); + glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT24, width, height); + +#elif defined(USE_DEPTH_TEXTURE) + // NOTE: We can also use a texture for depth buffer (GL_ARB_depth_texture/GL_OES_depth_texture extension required) + // A renderbuffer is simpler than a texture and could offer better performance on embedded devices + glGenTextures(1, &target.depth.id); + glBindTexture(GL_TEXTURE_2D, target.depth.id); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, width, height, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, NULL); + glBindTexture(GL_TEXTURE_2D, 0); +#endif + + // Create the framebuffer object + glGenFramebuffers(1, &target.id); + glBindFramebuffer(GL_FRAMEBUFFER, target.id); + + // Attach color texture and depth renderbuffer to FBO + glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, target.texture.id, 0); +#if defined(USE_DEPTH_RENDERBUFFER) + glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, target.depth.id); +#elif defined(USE_DEPTH_TEXTURE) + glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, target.depth.id, 0); +#endif + + GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER); + + if (status != GL_FRAMEBUFFER_COMPLETE) + { + TraceLog(WARNING, "Framebuffer object could not be created..."); + + switch(status) + { + case GL_FRAMEBUFFER_UNSUPPORTED: TraceLog(WARNING, "Framebuffer is unsupported"); break; + case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT: TraceLog(WARNING, "Framebuffer incomplete attachment"); break; +#if defined(GRAPHICS_API_OPENGL_ES2) + case GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS: TraceLog(WARNING, "Framebuffer incomplete dimensions"); break; +#endif + case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT: TraceLog(WARNING, "Framebuffer incomplete missing attachment"); break; + default: break; + } + + glDeleteTextures(1, &target.texture.id); + glDeleteTextures(1, &target.depth.id); + glDeleteFramebuffers(1, &target.id); + } + else TraceLog(INFO, "[FBO ID %i] Framebuffer object created successfully", target.id); + + glBindFramebuffer(GL_FRAMEBUFFER, 0); +#endif + + return target; +} + +// Update already loaded texture in GPU with new data void rlglUpdateTexture(unsigned int id, int width, int height, int format, void *data) { glBindTexture(GL_TEXTURE_2D, id); diff --git a/src/rlgl.h b/src/rlgl.h index 1a5260eb..679cb590 100644 --- a/src/rlgl.h +++ b/src/rlgl.h @@ -183,6 +183,13 @@ typedef enum { OPENGL_11 = 1, OPENGL_33, OPENGL_ES_20 } GlVersion; int format; // Data format (TextureFormat) } Texture2D; + // RenderTexture2D type, for texture rendering + typedef struct RenderTexture2D { + unsigned int id; // Render texture (fbo) id + Texture2D texture; // Color buffer attachment texture + Texture2D depth; // Depth buffer attachment texture + } RenderTexture2D; + // Material type typedef struct Material { Shader shader; @@ -248,9 +255,12 @@ void rlColor4f(float x, float y, float z, float w); // Define one vertex (color) //------------------------------------------------------------------------------------ void rlEnableTexture(unsigned int id); // Enable texture usage void rlDisableTexture(void); // Disable texture usage +void rlEnableRenderTexture(unsigned int id); // Enable render texture (fbo) +void rlDisableRenderTexture(void); // Disable render texture (fbo), return to default framebuffer void rlEnableDepthTest(void); // Enable depth test void rlDisableDepthTest(void); // Disable depth test void rlDeleteTextures(unsigned int id); // Delete OpenGL texture from GPU +void rlDeleteRenderTextures(RenderTexture2D target); // Delete render textures (fbo) from GPU void rlDeleteShader(unsigned int id); // Delete OpenGL shader program from GPU void rlDeleteVertexArrays(unsigned int id); // Unload vertex data (VAO) from GPU memory void rlDeleteBuffers(unsigned int id); // Unload vertex data (VBO) from GPU memory @@ -268,6 +278,7 @@ void rlglDraw(void); // Draw VAO/VBO void rlglInitGraphics(int offsetX, int offsetY, int width, int height); // Initialize Graphics (OpenGL stuff) unsigned int rlglLoadTexture(void *data, int width, int height, int textureFormat, int mipmapCount); // Load texture in GPU +RenderTexture2D rlglLoadRenderTexture(int width, int height); // Load a texture to be used for rendering (fbo with color and depth attachments) void rlglUpdateTexture(unsigned int id, int width, int height, int format, void *data); // Update GPU texture with new data void rlglGenerateMipmaps(Texture2D texture); // Generate mipmap data for selected texture diff --git a/src/textures.c b/src/textures.c index 9d0d13b6..67264afb 100644 --- a/src/textures.c +++ b/src/textures.c @@ -389,6 +389,14 @@ Texture2D LoadTextureFromImage(Image image) return texture; } +// Load a texture to be used for rendering +RenderTexture2D LoadRenderTexture(int width, int height) +{ + RenderTexture2D target = rlglLoadRenderTexture(width, height); + + return target; +} + // Unload image from CPU memory (RAM) void UnloadImage(Image image) { @@ -409,6 +417,17 @@ void UnloadTexture(Texture2D texture) } } +// Unload render texture from GPU memory +void UnloadRenderTexture(RenderTexture2D target) +{ + if (target.id != 0) + { + rlDeleteRenderTextures(target); + + TraceLog(INFO, "[FBO ID %i] Unloaded render texture data from VRAM (GPU)", target.id); + } +} + // Get pixel data from image in the form of Color struct array Color *GetImageData(Image image) { -- cgit v1.2.3 From 06a8d7eb06e1573d5cce991ca39f6cb1067ea6fb Mon Sep 17 00:00:00 2001 From: raysan5 Date: Fri, 1 Apr 2016 10:39:33 +0200 Subject: Remove old postprocessing system --- src/core.c | 9 +-- src/raylib.h | 2 - src/rlgl.c | 244 ++--------------------------------------------------------- src/rlgl.h | 6 -- 4 files changed, 9 insertions(+), 252 deletions(-) (limited to 'src') diff --git a/src/core.c b/src/core.c index 5a794376..ae6f4cad 100644 --- a/src/core.c +++ b/src/core.c @@ -543,12 +543,8 @@ void BeginDrawing(void) updateTime = currentTime - previousTime; previousTime = currentTime; - if (IsPosproShaderEnabled()) rlEnablePostproFBO(); - rlClearScreenBuffers(); // Clear current framebuffers - rlLoadIdentity(); // Reset current matrix (MODELVIEW) - rlMultMatrixf(MatrixToFloat(downscaleView)); // If downscale required, apply it here //rlTranslatef(0.375, 0.375, 0); // HACK to have 2D pixel-perfect drawing on OpenGL 1.1 @@ -578,7 +574,7 @@ void BeginDrawingPro(int blendMode, Shader shader, Matrix transform) BeginDrawing(); SetBlendMode(blendMode); - SetPostproShader(shader); + SetCustomShader(shader); rlMultMatrixf(MatrixToFloat(transform)); } @@ -588,12 +584,11 @@ void EndDrawing(void) { rlglDraw(); // Draw Buffers (Only OpenGL 3+ and ES2) - if (IsPosproShaderEnabled()) rlglDrawPostpro(); // Draw postprocessing effect (shader) - SwapBuffers(); // Copy back buffer to front buffer PollInputEvents(); // Poll user events + // Frame time control system currentTime = GetTime(); drawTime = currentTime - previousTime; previousTime = currentTime; diff --git a/src/raylib.h b/src/raylib.h index c8f2c2a2..22fcb4ac 100644 --- a/src/raylib.h +++ b/src/raylib.h @@ -829,11 +829,9 @@ Vector3 ResolveCollisionCubicmap(Image cubicmap, Vector3 mapPosition, Vector3 *p Shader LoadShader(char *vsFileName, char *fsFileName); // Load a custom shader and bind default locations unsigned int LoadShaderProgram(char *vShaderStr, char *fShaderStr); // Load custom shaders strings and return program id void UnloadShader(Shader shader); // Unload a custom shader from memory -void SetPostproShader(Shader shader); // Set fullscreen postproduction shader void SetCustomShader(Shader shader); // Set custom shader to be used in batch draw void SetDefaultShader(void); // Set default shader to be used in batch draw void SetModelShader(Model *model, Shader shader); // Link a shader to a model -bool IsPosproShaderEnabled(void); // Check if postprocessing shader is enabled int GetShaderLocation(Shader shader, const char *uniformName); // Get shader uniform location void SetShaderValue(Shader shader, int uniformLoc, float *value, int size); // Set shader uniform value (float) diff --git a/src/rlgl.c b/src/rlgl.c index 2153221e..809077e3 100644 --- a/src/rlgl.c +++ b/src/rlgl.c @@ -176,24 +176,6 @@ typedef struct { // TODO: Store draw state -> blending mode, shader } DrawCall; -// pixel type (same as Color type) -// NOTE: Used exclusively in mipmap generation functions -typedef struct { - unsigned char r; - unsigned char g; - unsigned char b; - unsigned char a; -} pixel; - -// Framebuffer Object type -typedef struct { - GLuint id; - int width; - int height; - GLuint colorTextureId; - GLuint depthTextureId; -} FBO; - #if defined(RLGL_STANDALONE) typedef enum { INFO = 0, ERROR, WARNING, DEBUG, OTHER } TraceLogType; #endif @@ -248,13 +230,6 @@ static bool texCompETC1Supported = false; // ETC1 texture compression support static bool texCompETC2Supported = false; // ETC2/EAC texture compression support static bool texCompPVRTSupported = false; // PVR texture compression support static bool texCompASTCSupported = false; // ASTC texture compression support - -// Framebuffer object and texture -static FBO postproFbo; -static Model postproQuad; - -// Shaders related variables -static bool enabledPostpro = false; #endif // Compressed textures support flags @@ -269,9 +244,6 @@ static PFNGLDELETEVERTEXARRAYSOESPROC glDeleteVertexArrays; //static PFNGLISVERTEXARRAYOESPROC glIsVertexArray; // NOTE: Fails in WebGL, omitted #endif -// Save screen size data (render size), required for postpro quad -static int screenWidth, screenHeight; - static int blendMode = 0; // White texture useful for plain color polys (required by shader) @@ -290,14 +262,11 @@ static void UpdateBuffers(void); static char *TextFileRead(char *fn); static void LoadCompressedTexture(unsigned char *data, int width, int height, int mipmapCount, int compressedFormat); - -FBO rlglLoadFBO(int width, int height); -void rlglUnloadFBO(FBO fbo); #endif #if defined(GRAPHICS_API_OPENGL_11) static int GenerateMipmaps(unsigned char *data, int baseWidth, int baseHeight); -static pixel *GenNextMipmap(pixel *srcData, int srcWidth, int srcHeight); +static Color *GenNextMipmap(Color *srcData, int srcWidth, int srcHeight); #endif #if defined(RLGL_STANDALONE) @@ -827,14 +796,6 @@ void rlDeleteRenderTextures(RenderTexture2D target) #endif } -// Enable rendering to postprocessing FBO -void rlEnablePostproFBO() -{ -#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) - glBindFramebuffer(GL_FRAMEBUFFER, postproFbo.id); -#endif -} - // Unload shader from GPU memory void rlDeleteShader(unsigned int id) { @@ -1088,125 +1049,6 @@ void rlglInit(void) #endif } -// Init postpro system -// NOTE: Uses global variables screenWidth and screenHeight -// Modifies global variables: postproFbo, postproQuad -void rlglInitPostpro(void) -{ -#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) - postproFbo = rlglLoadFBO(screenWidth, screenHeight); - - if (postproFbo.id > 0) - { - // Create a simple quad model to render fbo texture - Mesh quad; - - quad.vertexCount = 6; - - float w = (float)postproFbo.width; - float h = (float)postproFbo.height; - - float quadPositions[6*3] = { w, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, h, 0.0f, 0.0f, h, 0.0f, w, h, 0.0f, w, 0.0f, 0.0f }; - float quadTexcoords[6*2] = { 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f }; - float quadNormals[6*3] = { 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f }; - unsigned char quadColors[6*4] = { 255 }; - - quad.vertices = quadPositions; - quad.texcoords = quadTexcoords; - quad.normals = quadNormals; - quad.colors = quadColors; - - postproQuad = rlglLoadModel(quad); - - // NOTE: postproFbo.colorTextureId must be assigned to postproQuad model shader - } -#endif -} - -// Load a framebuffer object -FBO rlglLoadFBO(int width, int height) -{ - FBO fbo; - fbo.id = 0; - fbo.width = width; - fbo.height = height; - -#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) - // Create the texture that will serve as the color attachment for the framebuffer - glGenTextures(1, &fbo.colorTextureId); - glBindTexture(GL_TEXTURE_2D, fbo.colorTextureId); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL); - glBindTexture(GL_TEXTURE_2D, 0); - - // Create the renderbuffer that will serve as the depth attachment for the framebuffer. - glGenRenderbuffers(1, &fbo.depthTextureId); - glBindRenderbuffer(GL_RENDERBUFFER, fbo.depthTextureId); - glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, width, height); - - // NOTE: We can also use a texture for depth buffer (GL_ARB_depth_texture/GL_OES_depth_texture extensions) - // A renderbuffer is simpler than a texture and could offer better performance on embedded devices -/* - glGenTextures(1, &fbo.depthTextureId); - glBindTexture(GL_TEXTURE_2D, fbo.depthTextureId); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); - glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, width, height, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, NULL); - glBindTexture(GL_TEXTURE_2D, 0); -*/ - // Create the framebuffer object - glGenFramebuffers(1, &fbo.id); - glBindFramebuffer(GL_FRAMEBUFFER, fbo.id); - - // Attach color texture and depth renderbuffer to FBO - glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, fbo.colorTextureId, 0); - glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, fbo.depthTextureId); - - GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER); - - if (status != GL_FRAMEBUFFER_COMPLETE) - { - TraceLog(WARNING, "Framebuffer object could not be created..."); - - switch(status) - { - case GL_FRAMEBUFFER_UNSUPPORTED: TraceLog(WARNING, "Framebuffer is unsupported"); break; - case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT: TraceLog(WARNING, "Framebuffer incomplete attachment"); break; -#if defined(GRAPHICS_API_OPENGL_ES2) - case GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS: TraceLog(WARNING, "Framebuffer incomplete dimensions"); break; -#endif - case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT: TraceLog(WARNING, "Framebuffer incomplete missing attachment"); break; - default: break; - } - - glDeleteTextures(1, &fbo.colorTextureId); - glDeleteTextures(1, &fbo.depthTextureId); - } - else TraceLog(INFO, "[FBO ID %i] Framebuffer object created successfully", fbo.id); - - glBindFramebuffer(GL_FRAMEBUFFER, 0); -#endif - - return fbo; -} - -// Unload framebuffer object -void rlglUnloadFBO(FBO fbo) -{ -#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) - glDeleteFramebuffers(1, &fbo.id); - glDeleteTextures(1, &fbo.colorTextureId); - glDeleteTextures(1, &fbo.depthTextureId); - - TraceLog(INFO, "[FBO ID %i] Unloaded framebuffer object successfully", fbo.id); -#endif -} - // Vertex Buffer Object deinitialization (memory free) void rlglClose(void) { @@ -1263,20 +1105,6 @@ void rlglClose(void) glDeleteTextures(1, &whiteTexture); TraceLog(INFO, "[TEX ID %i] Unloaded texture data (base white texture) from VRAM", whiteTexture); - if (postproFbo.id != 0) - { - rlglUnloadFBO(postproFbo); - - // Unload postpro quad model data - rlDeleteBuffers(postproQuad.mesh.vboId[0]); - rlDeleteBuffers(postproQuad.mesh.vboId[1]); - rlDeleteBuffers(postproQuad.mesh.vboId[2]); - - rlDeleteVertexArrays(postproQuad.mesh.vaoId); - - TraceLog(INFO, "Unloaded postprocessing data"); - } - free(draws); #endif } @@ -1443,16 +1271,6 @@ void rlglDraw(void) #endif } -// Draw with postprocessing shader -void rlglDrawPostpro(void) -{ -#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) - glBindFramebuffer(GL_FRAMEBUFFER, 0); - - rlglDrawModel(postproQuad, (Vector3){ 0.0f, 0.0f, 0.0f }, (Vector3){ 0.0f, 0.0f, 0.0f }, 0.0f, (Vector3){1.0f, 1.0f, 1.0f}, (Color){ 255, 255, 255, 255 }, false); -#endif -} - // Draw a 3d model // NOTE: Model transform can come within model struct void rlglDrawModel(Model model, Vector3 position, Vector3 rotationAxis, float rotationAngle, Vector3 scale, Color color, bool wires) @@ -1606,12 +1424,7 @@ void rlglDrawModel(Model model, Vector3 position, Vector3 rotationAxis, float ro // Initialize Graphics Device (OpenGL stuff) // NOTE: Stores global variables screenWidth and screenHeight void rlglInitGraphics(int offsetX, int offsetY, int width, int height) -{ - // Save screen size data (global vars), required on postpro quad - // NOTE: Size represents render size, it could differ from screen size! - screenWidth = width; - screenHeight = height; - +{ // NOTE: Required! viewport must be recalculated if screen resized! glViewport(offsetX/2, offsetY/2, width - offsetX, height - offsetY); // Set viewport width and height @@ -2458,44 +2271,11 @@ void SetCustomShader(Shader shader) #endif } -// Set postprocessing shader -void SetPostproShader(Shader shader) -{ -#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) - if (!enabledPostpro) - { - enabledPostpro = true; - rlglInitPostpro(); // Lazy initialization on postprocessing usage - } - - SetModelShader(&postproQuad, shader); - - Texture2D texture; - texture.id = postproFbo.colorTextureId; - texture.width = postproFbo.width; - texture.height = postproFbo.height; - - postproQuad.material.texDiffuse = texture; - - //TraceLog(DEBUG, "Postproquad texture id: %i", postproQuad.texture.id); - //TraceLog(DEBUG, "Postproquad shader diffuse map id: %i", postproQuad.shader.texDiffuseId); - //TraceLog(DEBUG, "Shader diffuse map id: %i", shader.texDiffuseId); -#elif defined(GRAPHICS_API_OPENGL_11) - TraceLog(WARNING, "Shaders not supported on OpenGL 1.1"); -#endif -} - // Set default shader to be used in batch draw void SetDefaultShader(void) { #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) SetCustomShader(defaultShader); - - if (enabledPostpro) - { - SetPostproShader(defaultShader); - enabledPostpro = false; - } #endif } @@ -2529,16 +2309,6 @@ void SetModelShader(Model *model, Shader shader) #endif } -// Check if postprocessing is enabled (used in module: core) -bool IsPosproShaderEnabled(void) -{ -#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) - return enabledPostpro; -#elif defined(GRAPHICS_API_OPENGL_11) - return false; -#endif -} - // Get shader uniform location int GetShaderLocation(Shader shader, const char *uniformName) { @@ -3120,8 +2890,8 @@ static int GenerateMipmaps(unsigned char *data, int baseWidth, int baseHeight) // Generate mipmaps // NOTE: Every mipmap data is stored after data - pixel *image = (pixel *)malloc(width*height*sizeof(pixel)); - pixel *mipmap = NULL; + Color *image = (Color *)malloc(width*height*sizeof(Color)); + Color *mipmap = NULL; int offset = 0; int j = 0; @@ -3169,15 +2939,15 @@ static int GenerateMipmaps(unsigned char *data, int baseWidth, int baseHeight) } // Manual mipmap generation (basic scaling algorithm) -static pixel *GenNextMipmap(pixel *srcData, int srcWidth, int srcHeight) +static Color *GenNextMipmap(Color *srcData, int srcWidth, int srcHeight) { int x2, y2; - pixel prow, pcol; + Color prow, pcol; int width = srcWidth/2; int height = srcHeight/2; - pixel *mipmap = (pixel *)malloc(width*height*sizeof(pixel)); + Color *mipmap = (Color *)malloc(width*height*sizeof(Color)); // Scaling algorithm works perfectly (box-filter) for (int y = 0; y < height; y++) diff --git a/src/rlgl.h b/src/rlgl.h index 679cb590..714961e1 100644 --- a/src/rlgl.h +++ b/src/rlgl.h @@ -267,7 +267,6 @@ void rlDeleteBuffers(unsigned int id); // Unload vertex data (VBO) from void rlClearColor(byte r, byte g, byte b, byte a); // Clear color buffer with color void rlClearScreenBuffers(void); // Clear used screen buffers (color and depth) int rlGetVersion(void); // Returns current OpenGL version -void rlEnablePostproFBO(void); // Enable rendering to postprocessing FBO //------------------------------------------------------------------------------------ // Functions Declaration - rlgl functionality @@ -285,9 +284,6 @@ void rlglGenerateMipmaps(Texture2D texture); // Gene // NOTE: There is a set of shader related functions that are available to end user, // to avoid creating function wrappers through core module, they have been directly declared in raylib.h -void rlglInitPostpro(void); // Initialize postprocessing system -void rlglDrawPostpro(void); // Draw with postprocessing shader - Model rlglLoadModel(Mesh mesh); // Upload vertex data into GPU and provided VAO/VBO ids void rlglDrawModel(Model model, Vector3 position, Vector3 rotationAxis, float rotationAngle, Vector3 scale, Color color, bool wires); @@ -309,11 +305,9 @@ void PrintModelviewMatrix(void); // DEBUG: Print modelview matrix Shader LoadShader(char *vsFileName, char *fsFileName); // Load a custom shader and bind default locations unsigned int LoadShaderProgram(char *vShaderStr, char *fShaderStr); // Load custom shader strings and return program id void UnloadShader(Shader shader); // Unload a custom shader from memory -void SetPostproShader(Shader shader); // Set fullscreen postproduction shader void SetCustomShader(Shader shader); // Set custom shader to be used in batch draw void SetDefaultShader(void); // Set default shader to be used in batch draw void SetModelShader(Model *model, Shader shader); // Link a shader to a model -bool IsPosproShaderEnabled(void); // Check if postprocessing shader is enabled int GetShaderLocation(Shader shader, const char *uniformName); // Get shader uniform location void SetShaderValue(Shader shader, int uniformLoc, float *value, int size); // Set shader uniform value (float) -- cgit v1.2.3 From f2f4079411472e835263a7a59ed58dba380ecca6 Mon Sep 17 00:00:00 2001 From: LelixSuper Date: Sun, 3 Apr 2016 16:05:23 +0200 Subject: Remove recipes of GLEW from Makefile (not used any more) --- src/Makefile | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) (limited to 'src') diff --git a/src/Makefile b/src/Makefile index cab2ced0..8aeb00d7 100644 --- a/src/Makefile +++ b/src/Makefile @@ -84,8 +84,6 @@ else # external libraries headers # GLFW3 INCLUDES += -I../external/glfw3/include -# GLEW - INCLUDES += -I../external/glew/include # OpenAL Soft INCLUDES += -I../external/openal_soft/include endif @@ -143,7 +141,7 @@ models.o: models.c # compile audio module audio.o: audio.c $(CC) -c audio.c $(CFLAGS) $(INCLUDES) -D$(PLATFORM) - + # compile stb_vorbis library stb_vorbis.o: stb_vorbis.c $(CC) -c stb_vorbis.c -O1 $(INCLUDES) -D$(PLATFORM) -- cgit v1.2.3 From a04a7b6ea53ed291a742baf764bd604d9fc7b70e Mon Sep 17 00:00:00 2001 From: LelixSuper Date: Sun, 3 Apr 2016 16:07:44 +0200 Subject: Fix cleaning recipies for GNU/Linux --- src/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/Makefile b/src/Makefile index 8aeb00d7..76dbfc67 100644 --- a/src/Makefile +++ b/src/Makefile @@ -165,7 +165,7 @@ ifeq ($(PLATFORM),PLATFORM_DESKTOP) rm -f *.o libraylib.a else ifeq ($(PLATFORM_OS),LINUX) - find -type f -executable | xargs file -i | grep -E 'x-object|x-archive|x-sharedlib|x-executable' | rev | cut -d ':' -f 2- | rev | xargs rm -f + rm -f *.o libraylib.a else del *.o libraylib.a endif -- cgit v1.2.3 From a66c8531d69569b0c5173c71a8ed28565c1b5214 Mon Sep 17 00:00:00 2001 From: raysan5 Date: Sun, 3 Apr 2016 18:31:42 +0200 Subject: Some code simplifications --- src/models.c | 1 + src/rlgl.c | 84 +++++++++++++++++++----------------------------------------- 2 files changed, 27 insertions(+), 58 deletions(-) (limited to 'src') diff --git a/src/models.c b/src/models.c index a515dd86..0bb2b8d6 100644 --- a/src/models.c +++ b/src/models.c @@ -627,6 +627,7 @@ void SetModelTexture(Model *model, Texture2D texture) else model->material.texDiffuse = texture; } +// Generate a mesh from heightmap static Mesh GenMeshHeightmap(Image heightmap, Vector3 size) { #define GRAY_VALUE(c) ((c.r+c.g+c.b)/3) diff --git a/src/rlgl.c b/src/rlgl.c index 809077e3..cf9fe17b 100644 --- a/src/rlgl.c +++ b/src/rlgl.c @@ -256,6 +256,7 @@ unsigned int whiteTexture; #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) static Shader LoadDefaultShader(void); static Shader LoadSimpleShader(void); +static void GetShaderDefaultLocations(Shader *shader); static void InitializeBuffers(void); static void InitializeBuffersGPU(void); static void UpdateBuffers(void); @@ -1369,22 +1370,25 @@ void rlglDrawModel(Model model, Vector3 position, Vector3 rotationAxis, float ro } else { - // Bind model VBOs data + // Bind model VBO data: vertex position glBindBuffer(GL_ARRAY_BUFFER, model.mesh.vboId[0]); glVertexAttribPointer(model.material.shader.vertexLoc, 3, GL_FLOAT, 0, 0, 0); glEnableVertexAttribArray(model.material.shader.vertexLoc); + // Bind model VBO data: vertex texcoords glBindBuffer(GL_ARRAY_BUFFER, model.mesh.vboId[1]); glVertexAttribPointer(model.material.shader.texcoordLoc, 2, GL_FLOAT, 0, 0, 0); glEnableVertexAttribArray(model.material.shader.texcoordLoc); - // Add normals support + // Bind model VBO data: vertex normals (if available) if (model.material.shader.normalLoc != -1) { glBindBuffer(GL_ARRAY_BUFFER, model.mesh.vboId[2]); glVertexAttribPointer(model.material.shader.normalLoc, 3, GL_FLOAT, 0, 0, 0); glEnableVertexAttribArray(model.material.shader.normalLoc); } + + // TODO: Bind model VBO data: colors, tangents, texcoords2 (if available) } // Draw call! @@ -2094,9 +2098,7 @@ void *rlglReadTexturePixels(Texture2D texture) // Load a custom shader and bind default locations Shader LoadShader(char *vsFileName, char *fsFileName) { - Shader shader; - - shader.id = 0; // Default value in case of loading failure + Shader shader = { 0 }; #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) // Shaders loading from external text file @@ -2107,28 +2109,7 @@ Shader LoadShader(char *vsFileName, char *fsFileName) { shader.id = LoadShaderProgram(vShaderStr, fShaderStr); - if (shader.id != 0) - { - TraceLog(INFO, "[SHDR ID %i] Custom shader loaded successfully", shader.id); - - // Get handles to GLSL input attibute locations - //------------------------------------------------------------------- - shader.vertexLoc = glGetAttribLocation(shader.id, "vertexPosition"); - shader.texcoordLoc = glGetAttribLocation(shader.id, "vertexTexCoord"); - shader.normalLoc = glGetAttribLocation(shader.id, "vertexNormal"); - // NOTE: custom shader does not use colorLoc - shader.colorLoc = -1; - - // Get handles to GLSL uniform locations (vertex shader) - shader.mvpLoc = glGetUniformLocation(shader.id, "mvpMatrix"); - - // Get handles to GLSL uniform locations (fragment shader) - shader.tintColorLoc = glGetUniformLocation(shader.id, "fragTintColor"); - shader.mapDiffuseLoc = glGetUniformLocation(shader.id, "texture0"); - shader.mapNormalLoc = -1; // It can be set later - shader.mapSpecularLoc = -1; // It can be set later - //-------------------------------------------------------------------- - } + if (shader.id != 0) GetShaderDefaultLocations(&shader); else { TraceLog(WARNING, "Custom shader could not be loaded"); @@ -2497,23 +2478,7 @@ static Shader LoadDefaultShader(void) if (shader.id != 0) TraceLog(INFO, "[SHDR ID %i] Default shader loaded successfully", shader.id); else TraceLog(WARNING, "[SHDR ID %i] Default shader could not be loaded", shader.id); - // Get handles to GLSL input attibute locations - //------------------------------------------------------------------- - shader.vertexLoc = glGetAttribLocation(shader.id, "vertexPosition"); - shader.texcoordLoc = glGetAttribLocation(shader.id, "vertexTexCoord"); - shader.colorLoc = glGetAttribLocation(shader.id, "vertexColor"); - // NOTE: default shader does not use normalLoc - shader.normalLoc = -1; - - // Get handles to GLSL uniform locations (vertex shader) - shader.mvpLoc = glGetUniformLocation(shader.id, "mvpMatrix"); - - // Get handles to GLSL uniform locations (fragment shader) - shader.tintColorLoc = -1; - shader.mapDiffuseLoc = glGetUniformLocation(shader.id, "texture0"); - shader.mapNormalLoc = -1; // It can be set later - shader.mapSpecularLoc = -1; // It can be set later - //-------------------------------------------------------------------- + GetShaderDefaultLocations(&shader); return shader; } @@ -2573,25 +2538,28 @@ static Shader LoadSimpleShader(void) if (shader.id != 0) TraceLog(INFO, "[SHDR ID %i] Simple shader loaded successfully", shader.id); else TraceLog(WARNING, "[SHDR ID %i] Simple shader could not be loaded", shader.id); + GetShaderDefaultLocations(&shader); + + return shader; +} + +// Get location handlers to for shader attributes and uniforms +static void GetShaderDefaultLocations(Shader *shader) +{ // Get handles to GLSL input attibute locations - //------------------------------------------------------------------- - shader.vertexLoc = glGetAttribLocation(shader.id, "vertexPosition"); - shader.texcoordLoc = glGetAttribLocation(shader.id, "vertexTexCoord"); - shader.normalLoc = glGetAttribLocation(shader.id, "vertexNormal"); - // NOTE: simple shader does not use colorLoc - shader.colorLoc = -1; + shader->vertexLoc = glGetAttribLocation(shader->id, "vertexPosition"); + shader->texcoordLoc = glGetAttribLocation(shader->id, "vertexTexCoord"); + shader->normalLoc = glGetAttribLocation(shader->id, "vertexNormal"); + shader->colorLoc = glGetAttribLocation(shader->id, "vertexColor"); // -1 if not found // Get handles to GLSL uniform locations (vertex shader) - shader.mvpLoc = glGetUniformLocation(shader.id, "mvpMatrix"); + shader->mvpLoc = glGetUniformLocation(shader->id, "mvpMatrix"); // Get handles to GLSL uniform locations (fragment shader) - shader.tintColorLoc = glGetUniformLocation(shader.id, "fragTintColor"); - shader.mapDiffuseLoc = glGetUniformLocation(shader.id, "texture0"); - shader.mapNormalLoc = -1; // It can be set later - shader.mapSpecularLoc = -1; // It can be set later - //-------------------------------------------------------------------- - - return shader; + shader->tintColorLoc = glGetUniformLocation(shader->id, "fragTintColor"); + shader->mapDiffuseLoc = glGetUniformLocation(shader->id, "texture0"); + shader->mapNormalLoc = glGetUniformLocation(shader->id, "texture1"); // -1 if not found + shader->mapSpecularLoc = glGetUniformLocation(shader->id, "texture2"); // -1 if not found } // Read text file -- cgit v1.2.3 From b6cec214bd8cd557a92edeb6a53311eb192b56d4 Mon Sep 17 00:00:00 2001 From: Ray Date: Sun, 3 Apr 2016 20:14:07 +0200 Subject: Unified internal shader to only one Only defaultShader required, set default value for vertex color attribute if not enabled and fragColor uniform --- src/rlgl.c | 121 +++++++++++++++++-------------------------------------------- 1 file changed, 33 insertions(+), 88 deletions(-) (limited to 'src') diff --git a/src/rlgl.c b/src/rlgl.c index cf9fe17b..e08847b9 100644 --- a/src/rlgl.c +++ b/src/rlgl.c @@ -202,7 +202,7 @@ static VertexPositionColorBuffer triangles; // No texture support static VertexPositionColorTextureIndexBuffer quads; // Shader Programs -static Shader defaultShader, simpleShader; +static Shader defaultShader; static Shader currentShader; // By default, defaultShader // Vertex Array Objects (VAO) @@ -255,8 +255,7 @@ unsigned int whiteTexture; //---------------------------------------------------------------------------------- #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) static Shader LoadDefaultShader(void); -static Shader LoadSimpleShader(void); -static void GetShaderDefaultLocations(Shader *shader); +static void LoadDefaultShaderLocations(Shader *shader); static void InitializeBuffers(void); static void InitializeBuffersGPU(void); static void UpdateBuffers(void); @@ -1021,9 +1020,8 @@ void rlglInit(void) if (whiteTexture != 0) TraceLog(INFO, "[TEX ID %i] Base white texture loaded successfully", whiteTexture); else TraceLog(WARNING, "Base white texture could not be loaded"); - // Init default Shader (Custom for GL 3.3 and ES2) + // Init default Shader (customized for GL 3.3 and ES2) defaultShader = LoadDefaultShader(); - simpleShader = LoadSimpleShader(); //customShader = LoadShader("custom.vs", "custom.fs"); // Works ok currentShader = defaultShader; @@ -1083,12 +1081,11 @@ void rlglClose(void) glDeleteVertexArrays(1, &vaoQuads); } - //glDetachShader(defaultShaderProgram, v); - //glDetachShader(defaultShaderProgram, f); - //glDeleteShader(v); - //glDeleteShader(f); + //glDetachShader(defaultShaderProgram, vertexShader); + //glDetachShader(defaultShaderProgram, fragmentShader); + //glDeleteShader(vertexShader); // Already deleted on shader compilation + //glDeleteShader(fragmentShader); // Already deleted on sahder compilation glDeleteProgram(defaultShader.id); - glDeleteProgram(simpleShader.id); // Free vertex arrays memory free(lines.vertices); @@ -1124,6 +1121,7 @@ void rlglDraw(void) glUniformMatrix4fv(currentShader.mvpLoc, 1, false, MatrixToFloat(matMVP)); glUniform1i(currentShader.mapDiffuseLoc, 0); + glUniform4f(currentShader.tintColorLoc, 1.0f, 1.0f, 1.0f, 1.0f); } // NOTE: We draw in this order: lines, triangles, quads @@ -1866,6 +1864,8 @@ Model rlglLoadModel(Mesh mesh) model.mesh.vboId[1] = 0; // Vertex texcoords VBO model.mesh.vboId[2] = 0; // Vertex normals VBO + // TODO: Consider attributes: color, texcoords2, tangents (if available) + model.transform = MatrixIdentity(); #if defined(GRAPHICS_API_OPENGL_11) @@ -1873,7 +1873,7 @@ Model rlglLoadModel(Mesh mesh) model.material.shader.id = 0; // No shader used #elif defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) - model.material.shader = simpleShader; // Default model shader + model.material.shader = defaultShader; // Default model shader model.material.texDiffuse.id = whiteTexture; // Default whiteTexture model.material.texDiffuse.width = 1; // Default whiteTexture width @@ -1896,7 +1896,7 @@ Model rlglLoadModel(Mesh mesh) } // Create buffers for our vertex data (positions, texcoords, normals) - glGenBuffers(3, vertexBuffer); + glGenBuffers(4, vertexBuffer); // Enable vertex attributes: position glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer[0]); @@ -1915,7 +1915,10 @@ Model rlglLoadModel(Mesh mesh) glBufferData(GL_ARRAY_BUFFER, sizeof(float)*3*mesh.vertexCount, mesh.normals, GL_STATIC_DRAW); glVertexAttribPointer(model.material.shader.normalLoc, 3, GL_FLOAT, 0, 0, 0); glEnableVertexAttribArray(model.material.shader.normalLoc); - + + glVertexAttrib4f(model.material.shader.colorLoc, 1.0f, 1.0f, 1.0f, 1.0f); // Color vertex attribute set to default: WHITE + glDisableVertexAttribArray(model.material.shader.colorLoc); + model.mesh.vboId[0] = vertexBuffer[0]; // Vertex position VBO model.mesh.vboId[1] = vertexBuffer[1]; // Texcoords VBO model.mesh.vboId[2] = vertexBuffer[2]; // Normals VBO @@ -2068,7 +2071,7 @@ void *rlglReadTexturePixels(Texture2D texture) Model quad; //quad.mesh = GenMeshQuad(width, height); quad.transform = MatrixIdentity(); - quad.shader = simpleShader; + quad.shader = defaultShader; DrawModel(quad, (Vector3){ 0.0f, 0.0f, 0.0f }, 1.0f, WHITE); @@ -2109,11 +2112,12 @@ Shader LoadShader(char *vsFileName, char *fsFileName) { shader.id = LoadShaderProgram(vShaderStr, fShaderStr); - if (shader.id != 0) GetShaderDefaultLocations(&shader); + // After shader loading, we try to load default location names + if (shader.id != 0) LoadDefaultShaderLocations(&shader); else { TraceLog(WARNING, "Custom shader could not be loaded"); - shader = simpleShader; + shader = defaultShader; } // Shader strings must be freed @@ -2123,7 +2127,7 @@ Shader LoadShader(char *vsFileName, char *fsFileName) else { TraceLog(WARNING, "Custom shader could not be loaded"); - shader = simpleShader; + shader = defaultShader; } #endif @@ -2432,20 +2436,20 @@ static Shader LoadDefaultShader(void) "in vec2 vertexTexCoord; \n" "in vec4 vertexColor; \n" "out vec2 fragTexCoord; \n" - "out vec4 fragTintColor; \n" + "out vec4 fragColor; \n" #elif defined(GRAPHICS_API_OPENGL_ES2) char vShaderStr[] = "#version 100 \n" "attribute vec3 vertexPosition; \n" "attribute vec2 vertexTexCoord; \n" "attribute vec4 vertexColor; \n" "varying vec2 fragTexCoord; \n" - "varying vec4 fragTintColor; \n" + "varying vec4 fragColor; \n" #endif "uniform mat4 mvpMatrix; \n" "void main() \n" "{ \n" " fragTexCoord = vertexTexCoord; \n" - " fragTintColor = vertexColor; \n" + " fragColor = vertexColor; \n" " gl_Position = mvpMatrix*vec4(vertexPosition, 1.0); \n" "} \n"; @@ -2453,23 +2457,24 @@ static Shader LoadDefaultShader(void) #if defined(GRAPHICS_API_OPENGL_33) char fShaderStr[] = "#version 330 \n" "in vec2 fragTexCoord; \n" - "in vec4 fragTintColor; \n" - "out vec4 fragColor; \n" + "in vec4 fragColor; \n" + "out vec4 finalColor; \n" #elif defined(GRAPHICS_API_OPENGL_ES2) char fShaderStr[] = "#version 100 \n" "precision mediump float; \n" // precision required for OpenGL ES2 (WebGL) "varying vec2 fragTexCoord; \n" - "varying vec4 fragTintColor; \n" + "varying vec4 fragColor; \n" #endif "uniform sampler2D texture0; \n" + "uniform vec4 fragTintColor; \n" "void main() \n" "{ \n" #if defined(GRAPHICS_API_OPENGL_33) - " vec4 texelColor = texture(texture0, fragTexCoord); \n" - " fragColor = texelColor*fragTintColor; \n" + " vec4 texelColor = texture(texture0, fragTexCoord); \n" + " finalColor = texelColor*fragTintColor*fragColor; \n" #elif defined(GRAPHICS_API_OPENGL_ES2) " vec4 texelColor = texture2D(texture0, fragTexCoord); \n" // NOTE: texture2D() is deprecated on OpenGL 3.3 and ES 3.0 - " gl_FragColor = texelColor*fragTintColor; \n" + " gl_FragColor = texelColor*fragTintColor*fragColor; \n" #endif "} \n"; @@ -2478,73 +2483,13 @@ static Shader LoadDefaultShader(void) if (shader.id != 0) TraceLog(INFO, "[SHDR ID %i] Default shader loaded successfully", shader.id); else TraceLog(WARNING, "[SHDR ID %i] Default shader could not be loaded", shader.id); - GetShaderDefaultLocations(&shader); - - return shader; -} - -// Load Simple Shader (Vertex and Fragment) -// NOTE: This shader program is used to render models -static Shader LoadSimpleShader(void) -{ - Shader shader; - - // Vertex shader directly defined, no external file required -#if defined(GRAPHICS_API_OPENGL_33) - char vShaderStr[] = "#version 330 \n" - "in vec3 vertexPosition; \n" - "in vec2 vertexTexCoord; \n" - "in vec3 vertexNormal; \n" - "out vec2 fragTexCoord; \n" -#elif defined(GRAPHICS_API_OPENGL_ES2) - char vShaderStr[] = "#version 100 \n" - "attribute vec3 vertexPosition; \n" - "attribute vec2 vertexTexCoord; \n" - "attribute vec3 vertexNormal; \n" - "varying vec2 fragTexCoord; \n" -#endif - "uniform mat4 mvpMatrix; \n" - "void main() \n" - "{ \n" - " fragTexCoord = vertexTexCoord; \n" - " gl_Position = mvpMatrix*vec4(vertexPosition, 1.0); \n" - "} \n"; - - // Fragment shader directly defined, no external file required -#if defined(GRAPHICS_API_OPENGL_33) - char fShaderStr[] = "#version 330 \n" - "in vec2 fragTexCoord; \n" - "out vec4 fragColor; \n" -#elif defined(GRAPHICS_API_OPENGL_ES2) - char fShaderStr[] = "#version 100 \n" - "precision mediump float; \n" // precision required for OpenGL ES2 (WebGL) - "varying vec2 fragTexCoord; \n" -#endif - "uniform sampler2D texture0; \n" - "uniform vec4 fragTintColor; \n" - "void main() \n" - "{ \n" -#if defined(GRAPHICS_API_OPENGL_33) - " vec4 texelColor = texture(texture0, fragTexCoord); \n" - " fragColor = texelColor*fragTintColor; \n" -#elif defined(GRAPHICS_API_OPENGL_ES2) - " vec4 texelColor = texture2D(texture0, fragTexCoord); \n" - " gl_FragColor = texelColor*fragTintColor; \n" -#endif - "} \n"; - - shader.id = LoadShaderProgram(vShaderStr, fShaderStr); - - if (shader.id != 0) TraceLog(INFO, "[SHDR ID %i] Simple shader loaded successfully", shader.id); - else TraceLog(WARNING, "[SHDR ID %i] Simple shader could not be loaded", shader.id); - - GetShaderDefaultLocations(&shader); + LoadDefaultShaderLocations(&shader); return shader; } // Get location handlers to for shader attributes and uniforms -static void GetShaderDefaultLocations(Shader *shader) +static void LoadDefaultShaderLocations(Shader *shader) { // Get handles to GLSL input attibute locations shader->vertexLoc = glGetAttribLocation(shader->id, "vertexPosition"); -- cgit v1.2.3 From 0133917bf93fd3efd9580db6ff31126c58bf38f4 Mon Sep 17 00:00:00 2001 From: raysan5 Date: Mon, 4 Apr 2016 01:15:43 +0200 Subject: Correct detail --- src/rlgl.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/rlgl.c b/src/rlgl.c index e08847b9..b2a36351 100644 --- a/src/rlgl.c +++ b/src/rlgl.c @@ -1896,7 +1896,7 @@ Model rlglLoadModel(Mesh mesh) } // Create buffers for our vertex data (positions, texcoords, normals) - glGenBuffers(4, vertexBuffer); + glGenBuffers(3, vertexBuffer); // Enable vertex attributes: position glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer[0]); -- cgit v1.2.3 From 93616157862ab9557c42494e23fc96d9d4de21d1 Mon Sep 17 00:00:00 2001 From: LelixSuper Date: Wed, 6 Apr 2016 13:21:29 +0200 Subject: Fix Makefile files I've added .PHONY targets and fixed "clean" recipe. --- src/Makefile | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) (limited to 'src') diff --git a/src/Makefile b/src/Makefile index 76dbfc67..12f4609b 100644 --- a/src/Makefile +++ b/src/Makefile @@ -21,6 +21,8 @@ # #************************************************************************************************** +.PHONY: all clean + # define raylib platform to compile for # possible platforms: PLATFORM_DESKTOP PLATFORM_RPI PLATFORM_WEB PLATFORM ?= PLATFORM_DESKTOP @@ -97,9 +99,9 @@ else endif -# typing 'make' will invoke the first target entry in the file, -# in this case, the 'default' target entry is raylib -default: raylib +# typing 'make' will invoke the default target entry called 'all', +# in this case, the 'default' target entry is basic_game +all: raylib # compile raylib library raylib: $(OBJS) @@ -161,21 +163,21 @@ gestures.o: gestures.c # clean everything clean: ifeq ($(PLATFORM),PLATFORM_DESKTOP) - ifeq ($(PLATFORM_OS),OSX) - rm -f *.o libraylib.a + ifeq ($(PLATFORM_OS),WINDOWS) + del *.o libraylib.a else - ifeq ($(PLATFORM_OS),LINUX) rm -f *.o libraylib.a - else - del *.o libraylib.a endif +endif +ifeq ($(PLATFORM),PLATFORM_WEB) + ifeq ($(PLATFORM_OS),WINDOWS) + del *.o libraylib.bc + else + rm -f *.o libraylib.bc endif endif ifeq ($(PLATFORM),PLATFORM_RPI) rm -f *.o libraylib.a -endif -ifeq ($(PLATFORM),PLATFORM_WEB) - del *.o libraylib.bc endif @echo Cleaning done -- cgit v1.2.3 From 3b67a4cfba7d3b5dca805b1d1718fd2c9db64e99 Mon Sep 17 00:00:00 2001 From: raysan5 Date: Wed, 6 Apr 2016 17:45:25 +0200 Subject: Update stb libs to latest version --- src/stb_image.h | 263 ++++++++++++++++++++++++++++++++++++++----------- src/stb_image_resize.h | 110 ++++++++++----------- src/stb_image_write.h | 14 ++- src/stb_rect_pack.h | 26 +++-- src/stb_truetype.h | 52 ++++++---- src/stb_vorbis.c | 112 +++++---------------- src/stb_vorbis.h | 20 ++-- 7 files changed, 349 insertions(+), 248 deletions(-) (limited to 'src') diff --git a/src/stb_image.h b/src/stb_image.h index c28b5286..ce87646d 100644 --- a/src/stb_image.h +++ b/src/stb_image.h @@ -1,4 +1,4 @@ -/* stb_image - v2.09 - public domain image loader - http://nothings.org/stb_image.h +/* stb_image - v2.12 - public domain image loader - http://nothings.org/stb_image.h no warranty implied; use at your own risk Do this: @@ -146,6 +146,12 @@ Latest revision history: + 2.12 (2016-04-02) fix typo in 2.11 PSD fix that caused crashes + 2.11 (2016-04-02) 16-bit PNGS; enable SSE2 in non-gcc x64 + RGB-format JPEG; remove white matting in PSD; + allocate large structures on the stack; + correct channel count for PNG & BMP + 2.10 (2016-01-22) avoid warning introduced in 2.09 2.09 (2016-01-16) 16-bit TGA; comments in PNM files; STBI_REALLOC_SIZED 2.08 (2015-09-13) fix to 2.07 cleanup, reading RGB PSD as RGBA 2.07 (2015-09-13) partial animated GIF support @@ -166,10 +172,6 @@ STBI_MALLOC,STBI_REALLOC,STBI_FREE STBI_NO_*, STBI_ONLY_* GIF bugfix - 1.48 (2014-12-14) fix incorrectly-named assert() - 1.47 (2014-12-14) 1/2/4-bit PNG support (both grayscale and paletted) - optimize PNG - fix bug in interlaced PNG with user-specified channel count See end of file for full revision history. @@ -200,17 +202,17 @@ Janez Zemva John Bartholomew Michal Cichon svdijk@github Jonathan Blow Ken Hamada Tero Hanninen Baldur Karlsson Laurent Gomila Cort Stratton Sergio Gonzalez romigrou@github - Aruelien Pocheville Thibault Reuille Cass Everitt - Ryamond Barbiero Paul Du Bois Engin Manap + Aruelien Pocheville Thibault Reuille Cass Everitt Matthew Gregan + Ryamond Barbiero Paul Du Bois Engin Manap snagar@github + Michaelangel007@github Oriol Ferrer Mesia socks-the-fox Blazej Dariusz Roszkowski - Michaelangel007@github LICENSE -This software is in the public domain. Where that dedication is not -recognized, you are granted a perpetual, irrevocable license to copy, -distribute, and modify this file as you see fit. +This software is dual-licensed to the public domain and under the following +license: you are granted a perpetual, irrevocable license to copy, modify, +publish, and distribute this file as you see fit. */ @@ -679,7 +681,7 @@ typedef unsigned char validate_uint32[sizeof(stbi__uint32)==4 ? 1 : -1]; #define STBI_NO_SIMD #endif -#if !defined(STBI_NO_SIMD) && defined(STBI__X86_TARGET) +#if !defined(STBI_NO_SIMD) && (defined(STBI__X86_TARGET) || defined(STBI__X64_TARGET)) #define STBI_SSE2 #include @@ -1513,6 +1515,7 @@ typedef struct int succ_high; int succ_low; int eob_run; + int rgb; int scan_n, order[4]; int restart_interval, todo; @@ -2724,11 +2727,17 @@ static int stbi__process_frame_header(stbi__jpeg *z, int scan) if (Lf != 8+3*s->img_n) return stbi__err("bad SOF len","Corrupt JPEG"); + z->rgb = 0; for (i=0; i < s->img_n; ++i) { + static unsigned char rgb[3] = { 'R', 'G', 'B' }; z->img_comp[i].id = stbi__get8(s); if (z->img_comp[i].id != i+1) // JFIF requires - if (z->img_comp[i].id != i) // some version of jpegtran outputs non-JFIF-compliant files! - return stbi__err("bad component ID","Corrupt JPEG"); + if (z->img_comp[i].id != i) { // some version of jpegtran outputs non-JFIF-compliant files! + // somethings output this (see http://fileformats.archiveteam.org/wiki/JPEG#Color_format) + if (z->img_comp[i].id != rgb[i]) + return stbi__err("bad component ID","Corrupt JPEG"); + ++z->rgb; + } q = stbi__get8(s); z->img_comp[i].h = (q >> 4); if (!z->img_comp[i].h || z->img_comp[i].h > 4) return stbi__err("bad H","Corrupt JPEG"); z->img_comp[i].v = q & 15; if (!z->img_comp[i].v || z->img_comp[i].v > 4) return stbi__err("bad V","Corrupt JPEG"); @@ -3390,7 +3399,17 @@ static stbi_uc *load_jpeg_image(stbi__jpeg *z, int *out_x, int *out_y, int *comp if (n >= 3) { stbi_uc *y = coutput[0]; if (z->s->img_n == 3) { - z->YCbCr_to_RGB_kernel(out, y, coutput[1], coutput[2], z->s->img_x, n); + if (z->rgb == 3) { + for (i=0; i < z->s->img_x; ++i) { + out[0] = y[i]; + out[1] = coutput[1][i]; + out[2] = coutput[2][i]; + out[3] = 255; + out += n; + } + } else { + z->YCbCr_to_RGB_kernel(out, y, coutput[1], coutput[2], z->s->img_x, n); + } } else for (i=0; i < z->s->img_x; ++i) { out[0] = out[1] = out[2] = y[i]; @@ -3415,10 +3434,13 @@ static stbi_uc *load_jpeg_image(stbi__jpeg *z, int *out_x, int *out_y, int *comp static unsigned char *stbi__jpeg_load(stbi__context *s, int *x, int *y, int *comp, int req_comp) { - stbi__jpeg j; - j.s = s; - stbi__setup_jpeg(&j); - return load_jpeg_image(&j, x,y,comp,req_comp); + unsigned char* result; + stbi__jpeg* j = (stbi__jpeg*) stbi__malloc(sizeof(stbi__jpeg)); + j->s = s; + stbi__setup_jpeg(j); + result = load_jpeg_image(j, x,y,comp,req_comp); + STBI_FREE(j); + return result; } static int stbi__jpeg_test(stbi__context *s) @@ -3446,9 +3468,12 @@ static int stbi__jpeg_info_raw(stbi__jpeg *j, int *x, int *y, int *comp) static int stbi__jpeg_info(stbi__context *s, int *x, int *y, int *comp) { - stbi__jpeg j; - j.s = s; - return stbi__jpeg_info_raw(&j, x, y, comp); + int result; + stbi__jpeg* j = (stbi__jpeg*) (stbi__malloc(sizeof(stbi__jpeg))); + j->s = s; + result = stbi__jpeg_info_raw(j, x, y, comp); + STBI_FREE(j); + return result; } #endif @@ -3629,6 +3654,7 @@ static int stbi__zexpand(stbi__zbuf *z, char *zout, int n) // need to make room while (cur + n > limit) limit *= 2; q = (char *) STBI_REALLOC_SIZED(z->zout_start, old_limit, limit); + STBI_NOTUSED(old_limit); if (q == NULL) return stbi__err("outofmem", "Out of memory"); z->zout_start = q; z->zout = q + cur; @@ -3738,7 +3764,7 @@ static int stbi__compute_huffman_codes(stbi__zbuf *a) return 1; } -static int stbi__parse_uncomperssed_block(stbi__zbuf *a) +static int stbi__parse_uncompressed_block(stbi__zbuf *a) { stbi_uc header[4]; int len,nlen,k; @@ -3804,7 +3830,7 @@ static int stbi__parse_zlib(stbi__zbuf *a, int parse_header) final = stbi__zreceive(a,1); type = stbi__zreceive(a,2); if (type == 0) { - if (!stbi__parse_uncomperssed_block(a)) return 0; + if (!stbi__parse_uncompressed_block(a)) return 0; } else if (type == 3) { return 0; } else { @@ -3946,6 +3972,7 @@ typedef struct { stbi__context *s; stbi_uc *idata, *expanded, *out; + int depth; } stbi__png; @@ -3985,14 +4012,19 @@ static stbi_uc stbi__depth_scale_table[9] = { 0, 0xff, 0x55, 0, 0x11, 0,0,0, 0x0 // create the png data from post-deflated data static int stbi__create_png_image_raw(stbi__png *a, stbi_uc *raw, stbi__uint32 raw_len, int out_n, stbi__uint32 x, stbi__uint32 y, int depth, int color) { + int bytes = (depth == 16? 2 : 1); stbi__context *s = a->s; - stbi__uint32 i,j,stride = x*out_n; + stbi__uint32 i,j,stride = x*out_n*bytes; stbi__uint32 img_len, img_width_bytes; int k; int img_n = s->img_n; // copy it into a local for later + int output_bytes = out_n*bytes; + int filter_bytes = img_n*bytes; + int width = x; + STBI_ASSERT(out_n == s->img_n || out_n == s->img_n+1); - a->out = (stbi_uc *) stbi__malloc(x * y * out_n); // extra bytes to write off the end into + a->out = (stbi_uc *) stbi__malloc(x * y * output_bytes); // extra bytes to write off the end into if (!a->out) return stbi__err("outofmem", "Out of memory"); img_width_bytes = (((img_n * x * depth) + 7) >> 3); @@ -4007,8 +4039,7 @@ static int stbi__create_png_image_raw(stbi__png *a, stbi_uc *raw, stbi__uint32 r stbi_uc *cur = a->out + stride*j; stbi_uc *prior = cur - stride; int filter = *raw++; - int filter_bytes = img_n; - int width = x; + if (filter > 4) return stbi__err("invalid filter","Corrupt PNG"); @@ -4041,6 +4072,14 @@ static int stbi__create_png_image_raw(stbi__png *a, stbi_uc *raw, stbi__uint32 r raw += img_n; cur += out_n; prior += out_n; + } else if (depth == 16) { + if (img_n != out_n) { + cur[filter_bytes] = 255; // first pixel top byte + cur[filter_bytes+1] = 255; // first pixel bottom byte + } + raw += filter_bytes; + cur += output_bytes; + prior += output_bytes; } else { raw += 1; cur += 1; @@ -4049,7 +4088,7 @@ static int stbi__create_png_image_raw(stbi__png *a, stbi_uc *raw, stbi__uint32 r // this is a little gross, so that we don't switch per-pixel or per-component if (depth < 8 || img_n == out_n) { - int nk = (width - 1)*img_n; + int nk = (width - 1)*filter_bytes; #define CASE(f) \ case f: \ for (k=0; k < nk; ++k) @@ -4069,18 +4108,27 @@ static int stbi__create_png_image_raw(stbi__png *a, stbi_uc *raw, stbi__uint32 r STBI_ASSERT(img_n+1 == out_n); #define CASE(f) \ case f: \ - for (i=x-1; i >= 1; --i, cur[img_n]=255,raw+=img_n,cur+=out_n,prior+=out_n) \ - for (k=0; k < img_n; ++k) + for (i=x-1; i >= 1; --i, cur[filter_bytes]=255,raw+=filter_bytes,cur+=output_bytes,prior+=output_bytes) \ + for (k=0; k < filter_bytes; ++k) switch (filter) { CASE(STBI__F_none) cur[k] = raw[k]; break; - CASE(STBI__F_sub) cur[k] = STBI__BYTECAST(raw[k] + cur[k-out_n]); break; + CASE(STBI__F_sub) cur[k] = STBI__BYTECAST(raw[k] + cur[k- output_bytes]); break; CASE(STBI__F_up) cur[k] = STBI__BYTECAST(raw[k] + prior[k]); break; - CASE(STBI__F_avg) cur[k] = STBI__BYTECAST(raw[k] + ((prior[k] + cur[k-out_n])>>1)); break; - CASE(STBI__F_paeth) cur[k] = STBI__BYTECAST(raw[k] + stbi__paeth(cur[k-out_n],prior[k],prior[k-out_n])); break; - CASE(STBI__F_avg_first) cur[k] = STBI__BYTECAST(raw[k] + (cur[k-out_n] >> 1)); break; - CASE(STBI__F_paeth_first) cur[k] = STBI__BYTECAST(raw[k] + stbi__paeth(cur[k-out_n],0,0)); break; + CASE(STBI__F_avg) cur[k] = STBI__BYTECAST(raw[k] + ((prior[k] + cur[k- output_bytes])>>1)); break; + CASE(STBI__F_paeth) cur[k] = STBI__BYTECAST(raw[k] + stbi__paeth(cur[k- output_bytes],prior[k],prior[k- output_bytes])); break; + CASE(STBI__F_avg_first) cur[k] = STBI__BYTECAST(raw[k] + (cur[k- output_bytes] >> 1)); break; + CASE(STBI__F_paeth_first) cur[k] = STBI__BYTECAST(raw[k] + stbi__paeth(cur[k- output_bytes],0,0)); break; } #undef CASE + + // the loop above sets the high byte of the pixels' alpha, but for + // 16 bit png files we also need the low byte set. we'll do that here. + if (depth == 16) { + cur = a->out + stride*j; // start at the beginning of the row again + for (i=0; i < x; ++i,cur+=output_bytes) { + cur[filter_bytes+1] = 255; + } + } } } @@ -4156,6 +4204,17 @@ static int stbi__create_png_image_raw(stbi__png *a, stbi_uc *raw, stbi__uint32 r } } } + } else if (depth == 16) { + // force the image data from big-endian to platform-native. + // this is done in a separate pass due to the decoding relying + // on the data being untouched, but could probably be done + // per-line during decode if care is taken. + stbi_uc *cur = a->out; + stbi__uint16 *cur16 = (stbi__uint16*)cur; + + for(i=0; i < x*y*out_n; ++i,cur16++,cur+=2) { + *cur16 = (cur[0] << 8) | cur[1]; + } } return 1; @@ -4228,6 +4287,31 @@ static int stbi__compute_transparency(stbi__png *z, stbi_uc tc[3], int out_n) return 1; } +static int stbi__compute_transparency16(stbi__png *z, stbi__uint16 tc[3], int out_n) +{ + stbi__context *s = z->s; + stbi__uint32 i, pixel_count = s->img_x * s->img_y; + stbi__uint16 *p = (stbi__uint16*) z->out; + + // compute color-based transparency, assuming we've + // already got 65535 as the alpha value in the output + STBI_ASSERT(out_n == 2 || out_n == 4); + + if (out_n == 2) { + for (i = 0; i < pixel_count; ++i) { + p[1] = (p[0] == tc[0] ? 0 : 65535); + p += 2; + } + } else { + for (i = 0; i < pixel_count; ++i) { + if (p[0] == tc[0] && p[1] == tc[1] && p[2] == tc[2]) + p[3] = 0; + p += 4; + } + } + return 1; +} + static int stbi__expand_png_palette(stbi__png *a, stbi_uc *palette, int len, int pal_img_n) { stbi__uint32 i, pixel_count = a->s->img_x * a->s->img_y; @@ -4265,6 +4349,26 @@ static int stbi__expand_png_palette(stbi__png *a, stbi_uc *palette, int len, int return 1; } +static int stbi__reduce_png(stbi__png *p) +{ + int i; + int img_len = p->s->img_x * p->s->img_y * p->s->img_out_n; + stbi_uc *reduced; + stbi__uint16 *orig = (stbi__uint16*)p->out; + + if (p->depth != 16) return 1; // don't need to do anything if not 16-bit data + + reduced = (stbi_uc *)stbi__malloc(img_len); + if (p == NULL) return stbi__err("outofmem", "Out of memory"); + + for (i = 0; i < img_len; ++i) reduced[i] = (stbi_uc)((orig[i] >> 8) & 0xFF); // top half of each byte is a decent approx of 16->8 bit scaling + + p->out = reduced; + STBI_FREE(orig); + + return 1; +} + static int stbi__unpremultiply_on_load = 0; static int stbi__de_iphone_flag = 0; @@ -4326,8 +4430,9 @@ static int stbi__parse_png_file(stbi__png *z, int scan, int req_comp) { stbi_uc palette[1024], pal_img_n=0; stbi_uc has_trans=0, tc[3]; + stbi__uint16 tc16[3]; stbi__uint32 ioff=0, idata_limit=0, i, pal_len=0; - int first=1,k,interlace=0, color=0, depth=0, is_iphone=0; + int first=1,k,interlace=0, color=0, is_iphone=0; stbi__context *s = z->s; z->expanded = NULL; @@ -4352,8 +4457,9 @@ static int stbi__parse_png_file(stbi__png *z, int scan, int req_comp) if (c.length != 13) return stbi__err("bad IHDR len","Corrupt PNG"); s->img_x = stbi__get32be(s); if (s->img_x > (1 << 24)) return stbi__err("too large","Very large image (corrupt?)"); s->img_y = stbi__get32be(s); if (s->img_y > (1 << 24)) return stbi__err("too large","Very large image (corrupt?)"); - depth = stbi__get8(s); if (depth != 1 && depth != 2 && depth != 4 && depth != 8) return stbi__err("1/2/4/8-bit only","PNG not supported: 1/2/4/8-bit only"); + z->depth = stbi__get8(s); if (z->depth != 1 && z->depth != 2 && z->depth != 4 && z->depth != 8 && z->depth != 16) return stbi__err("1/2/4/8/16-bit only","PNG not supported: 1/2/4/8/16-bit only"); color = stbi__get8(s); if (color > 6) return stbi__err("bad ctype","Corrupt PNG"); + if (color == 3 && z->depth == 16) return stbi__err("bad ctype","Corrupt PNG"); if (color == 3) pal_img_n = 3; else if (color & 1) return stbi__err("bad ctype","Corrupt PNG"); comp = stbi__get8(s); if (comp) return stbi__err("bad comp method","Corrupt PNG"); filter= stbi__get8(s); if (filter) return stbi__err("bad filter method","Corrupt PNG"); @@ -4401,8 +4507,11 @@ static int stbi__parse_png_file(stbi__png *z, int scan, int req_comp) if (!(s->img_n & 1)) return stbi__err("tRNS with alpha","Corrupt PNG"); if (c.length != (stbi__uint32) s->img_n*2) return stbi__err("bad tRNS len","Corrupt PNG"); has_trans = 1; - for (k=0; k < s->img_n; ++k) - tc[k] = (stbi_uc) (stbi__get16be(s) & 255) * stbi__depth_scale_table[depth]; // non 8-bit images will be larger + if (z->depth == 16) { + for (k = 0; k < s->img_n; ++k) tc16[k] = stbi__get16be(s); // copy the values as-is + } else { + for (k = 0; k < s->img_n; ++k) tc[k] = (stbi_uc)(stbi__get16be(s) & 255) * stbi__depth_scale_table[z->depth]; // non 8-bit images will be larger + } } break; } @@ -4418,6 +4527,7 @@ static int stbi__parse_png_file(stbi__png *z, int scan, int req_comp) if (idata_limit == 0) idata_limit = c.length > 4096 ? c.length : 4096; while (ioff + c.length > idata_limit) idata_limit *= 2; + STBI_NOTUSED(idata_limit_old); p = (stbi_uc *) STBI_REALLOC_SIZED(z->idata, idata_limit_old, idata_limit); if (p == NULL) return stbi__err("outofmem", "Out of memory"); z->idata = p; } @@ -4432,7 +4542,7 @@ static int stbi__parse_png_file(stbi__png *z, int scan, int req_comp) if (scan != STBI__SCAN_load) return 1; if (z->idata == NULL) return stbi__err("no IDAT","Corrupt PNG"); // initial guess for decoded data size to avoid unnecessary reallocs - bpl = (s->img_x * depth + 7) / 8; // bytes per line, per component + bpl = (s->img_x * z->depth + 7) / 8; // bytes per line, per component raw_len = bpl * s->img_y * s->img_n /* pixels */ + s->img_y /* filter mode per row */; z->expanded = (stbi_uc *) stbi_zlib_decode_malloc_guesssize_headerflag((char *) z->idata, ioff, raw_len, (int *) &raw_len, !is_iphone); if (z->expanded == NULL) return 0; // zlib should set error @@ -4441,9 +4551,14 @@ static int stbi__parse_png_file(stbi__png *z, int scan, int req_comp) s->img_out_n = s->img_n+1; else s->img_out_n = s->img_n; - if (!stbi__create_png_image(z, z->expanded, raw_len, s->img_out_n, depth, color, interlace)) return 0; - if (has_trans) - if (!stbi__compute_transparency(z, tc, s->img_out_n)) return 0; + if (!stbi__create_png_image(z, z->expanded, raw_len, s->img_out_n, z->depth, color, interlace)) return 0; + if (has_trans) { + if (z->depth == 16) { + if (!stbi__compute_transparency16(z, tc16, s->img_out_n)) return 0; + } else { + if (!stbi__compute_transparency(z, tc, s->img_out_n)) return 0; + } + } if (is_iphone && stbi__de_iphone_flag && s->img_out_n > 2) stbi__de_iphone(z); if (pal_img_n) { @@ -4485,6 +4600,11 @@ static unsigned char *stbi__do_png(stbi__png *p, int *x, int *y, int *n, int req unsigned char *result=NULL; if (req_comp < 0 || req_comp > 4) return stbi__errpuc("bad req_comp", "Internal error"); if (stbi__parse_png_file(p, STBI__SCAN_load, req_comp)) { + if (p->depth == 16) { + if (!stbi__reduce_png(p)) { + return result; + } + } result = p->out; p->out = NULL; if (req_comp && req_comp != p->s->img_out_n) { @@ -4494,7 +4614,7 @@ static unsigned char *stbi__do_png(stbi__png *p, int *x, int *y, int *n, int req } *x = p->s->img_x; *y = p->s->img_y; - if (n) *n = p->s->img_out_n; + if (n) *n = p->s->img_n; } STBI_FREE(p->out); p->out = NULL; STBI_FREE(p->expanded); p->expanded = NULL; @@ -4619,6 +4739,7 @@ static void *stbi__bmp_parse_header(stbi__context *s, stbi__bmp_data *info) stbi__get16le(s); // discard reserved info->offset = stbi__get32le(s); info->hsz = hsz = stbi__get32le(s); + info->mr = info->mg = info->mb = info->ma = 0; if (hsz != 12 && hsz != 40 && hsz != 56 && hsz != 108 && hsz != 124) return stbi__errpuc("unknown BMP", "BMP type not supported: unknown"); if (hsz == 12) { @@ -4647,7 +4768,6 @@ static void *stbi__bmp_parse_header(stbi__context *s, stbi__bmp_data *info) stbi__get32le(s); } if (info->bpp == 16 || info->bpp == 32) { - info->mr = info->mg = info->mb = 0; if (compress == 0) { if (info->bpp == 32) { info->mr = 0xffu << 16; @@ -5342,6 +5462,21 @@ static stbi_uc *stbi__psd_load(stbi__context *s, int *x, int *y, int *comp, int } } + if (channelCount >= 4) { + for (i=0; i < w*h; ++i) { + unsigned char *pixel = out + 4*i; + if (pixel[3] != 0 && pixel[3] != 255) { + // remove weird white matte from PSD + float a = pixel[3] / 255.0f; + float ra = 1.0f / a; + float inv_a = 255.0f * (1 - ra); + pixel[0] = (unsigned char) (pixel[0]*ra + inv_a); + pixel[1] = (unsigned char) (pixel[1]*ra + inv_a); + pixel[2] = (unsigned char) (pixel[2]*ra + inv_a); + } + } + } + if (req_comp && req_comp != 4) { out = stbi__convert_format(out, 4, req_comp, w, h); if (out == NULL) return out; // stbi__convert_format frees input on failure @@ -5654,13 +5789,15 @@ static int stbi__gif_header(stbi__context *s, stbi__gif *g, int *comp, int is_in static int stbi__gif_info_raw(stbi__context *s, int *x, int *y, int *comp) { - stbi__gif g; - if (!stbi__gif_header(s, &g, comp, 1)) { + stbi__gif* g = (stbi__gif*) stbi__malloc(sizeof(stbi__gif)); + if (!stbi__gif_header(s, g, comp, 1)) { + STBI_FREE(g); stbi__rewind( s ); return 0; } - if (x) *x = g.w; - if (y) *y = g.h; + if (x) *x = g->w; + if (y) *y = g->h; + STBI_FREE(g); return 1; } @@ -5913,20 +6050,20 @@ static stbi_uc *stbi__gif_load_next(stbi__context *s, stbi__gif *g, int *comp, i static stbi_uc *stbi__gif_load(stbi__context *s, int *x, int *y, int *comp, int req_comp) { stbi_uc *u = 0; - stbi__gif g; - memset(&g, 0, sizeof(g)); + stbi__gif* g = (stbi__gif*) stbi__malloc(sizeof(stbi__gif)); + memset(g, 0, sizeof(*g)); - u = stbi__gif_load_next(s, &g, comp, req_comp); + u = stbi__gif_load_next(s, g, comp, req_comp); if (u == (stbi_uc *) s) u = 0; // end of animated gif marker if (u) { - *x = g.w; - *y = g.h; + *x = g->w; + *y = g->h; if (req_comp && req_comp != 4) - u = stbi__convert_format(u, 4, req_comp, g.w, g.h); + u = stbi__convert_format(u, 4, req_comp, g->w, g->h); } - else if (g.out) - STBI_FREE(g.out); - + else if (g->out) + STBI_FREE(g->out); + STBI_FREE(g); return u; } @@ -6464,6 +6601,14 @@ STBIDEF int stbi_info_from_callbacks(stbi_io_callbacks const *c, void *user, int /* revision history: + 2.12 (2016-04-02) fix typo in 2.11 PSD fix that caused crashes + 2.11 (2016-04-02) allocate large structures on the stack + remove white matting for transparent PSD + fix reported channel count for PNG & BMP + re-enable SSE2 in non-gcc 64-bit + support RGB-formatted JPEG + read 16-bit PNGs (only as 8-bit) + 2.10 (2016-01-22) avoid warning introduced in 2.09 by STBI_REALLOC_SIZED 2.09 (2016-01-16) allow comments in PNM files 16-bit-per-pixel TGA (not bit-per-component) info() for TGA could break due to .hdr handling diff --git a/src/stb_image_resize.h b/src/stb_image_resize.h index 4ce7ddbf..4cabe540 100644 --- a/src/stb_image_resize.h +++ b/src/stb_image_resize.h @@ -1,4 +1,4 @@ -/* stb_image_resize - v0.90 - public domain image resizing +/* stb_image_resize - v0.91 - public domain image resizing by Jorge L Rodriguez (@VinoBS) - 2014 http://github.com/nothings/stb @@ -156,13 +156,14 @@ Sean Barrett: API design, optimizations REVISIONS + 0.91 (2016-04-02) fix warnings; fix handling of subpixel regions 0.90 (2014-09-17) first released version LICENSE - This software is in the public domain. Where that dedication is not - recognized, you are granted a perpetual, irrevocable license to copy, - distribute, and modify this file as you see fit. + This software is dual-licensed to the public domain and under the following + license: you are granted a perpetual, irrevocable license to copy, modify, + publish, and distribute this file as you see fit. TODO Don't decode all of the image data when only processing a partial tile @@ -383,15 +384,6 @@ STBIRDEF int stbir_resize_region( const void *input_pixels , int input_w , int #define STBIR_ASSERT(x) assert(x) #endif -#ifdef STBIR_DEBUG -#define STBIR__DEBUG_ASSERT STBIR_ASSERT -#else -#define STBIR__DEBUG_ASSERT -#endif - -// If you hit this it means I haven't done it yet. -#define STBIR__UNIMPLEMENTED(x) STBIR_ASSERT(!(x)) - // For memset #include @@ -758,7 +750,7 @@ static float stbir__filter_trapezoid(float x, float scale) { float halfscale = scale / 2; float t = 0.5f + halfscale; - STBIR__DEBUG_ASSERT(scale <= 1); + STBIR_ASSERT(scale <= 1); x = (float)fabs(x); @@ -776,7 +768,7 @@ static float stbir__filter_trapezoid(float x, float scale) static float stbir__support_trapezoid(float scale) { - STBIR__DEBUG_ASSERT(scale <= 1); + STBIR_ASSERT(scale <= 1); return 0.5f + scale / 2; } @@ -990,7 +982,7 @@ static int stbir__edge_wrap_slow(stbir_edge edge, int n, int max) return n; // NOTREACHED default: - STBIR__UNIMPLEMENTED("Unimplemented edge type"); + STBIR_ASSERT(!"Unimplemented edge type"); return 0; } } @@ -1039,12 +1031,12 @@ static void stbir__calculate_coefficients_upsample(stbir__info* stbir_info, stbi float total_filter = 0; float filter_scale; - STBIR__DEBUG_ASSERT(in_last_pixel - in_first_pixel <= (int)ceil(stbir__filter_info_table[filter].support(1/scale) * 2)); // Taken directly from stbir__get_coefficient_width() which we can't call because we don't know if we're horizontal or vertical. + STBIR_ASSERT(in_last_pixel - in_first_pixel <= (int)ceil(stbir__filter_info_table[filter].support(1/scale) * 2)); // Taken directly from stbir__get_coefficient_width() which we can't call because we don't know if we're horizontal or vertical. contributor->n0 = in_first_pixel; contributor->n1 = in_last_pixel; - STBIR__DEBUG_ASSERT(contributor->n1 >= contributor->n0); + STBIR_ASSERT(contributor->n1 >= contributor->n0); for (i = 0; i <= in_last_pixel - in_first_pixel; i++) { @@ -1062,10 +1054,10 @@ static void stbir__calculate_coefficients_upsample(stbir__info* stbir_info, stbi total_filter += coefficient_group[i]; } - STBIR__DEBUG_ASSERT(stbir__filter_info_table[filter].kernel((float)(in_last_pixel + 1) + 0.5f - in_center_of_out, 1/scale) == 0); + STBIR_ASSERT(stbir__filter_info_table[filter].kernel((float)(in_last_pixel + 1) + 0.5f - in_center_of_out, 1/scale) == 0); - STBIR__DEBUG_ASSERT(total_filter > 0.9); - STBIR__DEBUG_ASSERT(total_filter < 1.1f); // Make sure it's not way off. + STBIR_ASSERT(total_filter > 0.9); + STBIR_ASSERT(total_filter < 1.1f); // Make sure it's not way off. // Make sure the sum of all coefficients is 1. filter_scale = 1 / total_filter; @@ -1087,12 +1079,12 @@ static void stbir__calculate_coefficients_downsample(stbir__info* stbir_info, st { int i; - STBIR__DEBUG_ASSERT(out_last_pixel - out_first_pixel <= (int)ceil(stbir__filter_info_table[filter].support(scale_ratio) * 2)); // Taken directly from stbir__get_coefficient_width() which we can't call because we don't know if we're horizontal or vertical. + STBIR_ASSERT(out_last_pixel - out_first_pixel <= (int)ceil(stbir__filter_info_table[filter].support(scale_ratio) * 2)); // Taken directly from stbir__get_coefficient_width() which we can't call because we don't know if we're horizontal or vertical. contributor->n0 = out_first_pixel; contributor->n1 = out_last_pixel; - STBIR__DEBUG_ASSERT(contributor->n1 >= contributor->n0); + STBIR_ASSERT(contributor->n1 >= contributor->n0); for (i = 0; i <= out_last_pixel - out_first_pixel; i++) { @@ -1101,7 +1093,7 @@ static void stbir__calculate_coefficients_downsample(stbir__info* stbir_info, st coefficient_group[i] = stbir__filter_info_table[filter].kernel(x, scale_ratio) * scale_ratio; } - STBIR__DEBUG_ASSERT(stbir__filter_info_table[filter].kernel((float)(out_last_pixel + 1) + 0.5f - out_center_of_in, scale_ratio) == 0); + STBIR_ASSERT(stbir__filter_info_table[filter].kernel((float)(out_last_pixel + 1) + 0.5f - out_center_of_in, scale_ratio) == 0); for (i = out_last_pixel - out_first_pixel; i >= 0; i--) { @@ -1136,8 +1128,8 @@ static void stbir__normalize_downsample_coefficients(stbir__info* stbir_info, st break; } - STBIR__DEBUG_ASSERT(total > 0.9f); - STBIR__DEBUG_ASSERT(total < 1.1f); + STBIR_ASSERT(total > 0.9f); + STBIR_ASSERT(total < 1.1f); scale = 1 / total; @@ -1364,7 +1356,7 @@ static void stbir__decode_scanline(stbir__info* stbir_info, int n) break; default: - STBIR__UNIMPLEMENTED("Unknown type/colorspace/channels combination."); + STBIR_ASSERT(!"Unknown type/colorspace/channels combination."); break; } @@ -1425,7 +1417,7 @@ static float* stbir__add_empty_ring_buffer_entry(stbir__info* stbir_info, int n) else { ring_buffer_index = (stbir_info->ring_buffer_begin_index + (stbir_info->ring_buffer_last_scanline - stbir_info->ring_buffer_first_scanline) + 1) % stbir_info->vertical_filter_pixel_width; - STBIR__DEBUG_ASSERT(ring_buffer_index != stbir_info->ring_buffer_begin_index); + STBIR_ASSERT(ring_buffer_index != stbir_info->ring_buffer_begin_index); } ring_buffer = stbir__get_ring_buffer_entry(stbir_info->ring_buffer, ring_buffer_index, stbir_info->ring_buffer_length_bytes / sizeof(float)); @@ -1457,11 +1449,11 @@ static void stbir__resample_horizontal_upsample(stbir__info* stbir_info, int n, int coefficient_group = coefficient_width * x; int coefficient_counter = 0; - STBIR__DEBUG_ASSERT(n1 >= n0); - STBIR__DEBUG_ASSERT(n0 >= -stbir_info->horizontal_filter_pixel_margin); - STBIR__DEBUG_ASSERT(n1 >= -stbir_info->horizontal_filter_pixel_margin); - STBIR__DEBUG_ASSERT(n0 < stbir_info->input_w + stbir_info->horizontal_filter_pixel_margin); - STBIR__DEBUG_ASSERT(n1 < stbir_info->input_w + stbir_info->horizontal_filter_pixel_margin); + STBIR_ASSERT(n1 >= n0); + STBIR_ASSERT(n0 >= -stbir_info->horizontal_filter_pixel_margin); + STBIR_ASSERT(n1 >= -stbir_info->horizontal_filter_pixel_margin); + STBIR_ASSERT(n0 < stbir_info->input_w + stbir_info->horizontal_filter_pixel_margin); + STBIR_ASSERT(n1 < stbir_info->input_w + stbir_info->horizontal_filter_pixel_margin); switch (channels) { case 1: @@ -1469,7 +1461,7 @@ static void stbir__resample_horizontal_upsample(stbir__info* stbir_info, int n, { int in_pixel_index = k * 1; float coefficient = horizontal_coefficients[coefficient_group + coefficient_counter++]; - STBIR__DEBUG_ASSERT(coefficient != 0); + STBIR_ASSERT(coefficient != 0); output_buffer[out_pixel_index + 0] += decode_buffer[in_pixel_index + 0] * coefficient; } break; @@ -1478,7 +1470,7 @@ static void stbir__resample_horizontal_upsample(stbir__info* stbir_info, int n, { int in_pixel_index = k * 2; float coefficient = horizontal_coefficients[coefficient_group + coefficient_counter++]; - STBIR__DEBUG_ASSERT(coefficient != 0); + STBIR_ASSERT(coefficient != 0); output_buffer[out_pixel_index + 0] += decode_buffer[in_pixel_index + 0] * coefficient; output_buffer[out_pixel_index + 1] += decode_buffer[in_pixel_index + 1] * coefficient; } @@ -1488,7 +1480,7 @@ static void stbir__resample_horizontal_upsample(stbir__info* stbir_info, int n, { int in_pixel_index = k * 3; float coefficient = horizontal_coefficients[coefficient_group + coefficient_counter++]; - STBIR__DEBUG_ASSERT(coefficient != 0); + STBIR_ASSERT(coefficient != 0); output_buffer[out_pixel_index + 0] += decode_buffer[in_pixel_index + 0] * coefficient; output_buffer[out_pixel_index + 1] += decode_buffer[in_pixel_index + 1] * coefficient; output_buffer[out_pixel_index + 2] += decode_buffer[in_pixel_index + 2] * coefficient; @@ -1499,7 +1491,7 @@ static void stbir__resample_horizontal_upsample(stbir__info* stbir_info, int n, { int in_pixel_index = k * 4; float coefficient = horizontal_coefficients[coefficient_group + coefficient_counter++]; - STBIR__DEBUG_ASSERT(coefficient != 0); + STBIR_ASSERT(coefficient != 0); output_buffer[out_pixel_index + 0] += decode_buffer[in_pixel_index + 0] * coefficient; output_buffer[out_pixel_index + 1] += decode_buffer[in_pixel_index + 1] * coefficient; output_buffer[out_pixel_index + 2] += decode_buffer[in_pixel_index + 2] * coefficient; @@ -1512,7 +1504,7 @@ static void stbir__resample_horizontal_upsample(stbir__info* stbir_info, int n, int in_pixel_index = k * channels; float coefficient = horizontal_coefficients[coefficient_group + coefficient_counter++]; int c; - STBIR__DEBUG_ASSERT(coefficient != 0); + STBIR_ASSERT(coefficient != 0); for (c = 0; c < channels; c++) output_buffer[out_pixel_index + c] += decode_buffer[in_pixel_index + c] * coefficient; } @@ -1535,7 +1527,7 @@ static void stbir__resample_horizontal_downsample(stbir__info* stbir_info, int n int filter_pixel_margin = stbir_info->horizontal_filter_pixel_margin; int max_x = input_w + filter_pixel_margin * 2; - STBIR__DEBUG_ASSERT(!stbir__use_width_upsampling(stbir_info)); + STBIR_ASSERT(!stbir__use_width_upsampling(stbir_info)); switch (channels) { case 1: @@ -1553,7 +1545,7 @@ static void stbir__resample_horizontal_downsample(stbir__info* stbir_info, int n { int out_pixel_index = k * 1; float coefficient = horizontal_coefficients[coefficient_group + k - n0]; - STBIR__DEBUG_ASSERT(coefficient != 0); + STBIR_ASSERT(coefficient != 0); output_buffer[out_pixel_index + 0] += decode_buffer[in_pixel_index + 0] * coefficient; } } @@ -1574,7 +1566,7 @@ static void stbir__resample_horizontal_downsample(stbir__info* stbir_info, int n { int out_pixel_index = k * 2; float coefficient = horizontal_coefficients[coefficient_group + k - n0]; - STBIR__DEBUG_ASSERT(coefficient != 0); + STBIR_ASSERT(coefficient != 0); output_buffer[out_pixel_index + 0] += decode_buffer[in_pixel_index + 0] * coefficient; output_buffer[out_pixel_index + 1] += decode_buffer[in_pixel_index + 1] * coefficient; } @@ -1596,7 +1588,7 @@ static void stbir__resample_horizontal_downsample(stbir__info* stbir_info, int n { int out_pixel_index = k * 3; float coefficient = horizontal_coefficients[coefficient_group + k - n0]; - STBIR__DEBUG_ASSERT(coefficient != 0); + STBIR_ASSERT(coefficient != 0); output_buffer[out_pixel_index + 0] += decode_buffer[in_pixel_index + 0] * coefficient; output_buffer[out_pixel_index + 1] += decode_buffer[in_pixel_index + 1] * coefficient; output_buffer[out_pixel_index + 2] += decode_buffer[in_pixel_index + 2] * coefficient; @@ -1619,7 +1611,7 @@ static void stbir__resample_horizontal_downsample(stbir__info* stbir_info, int n { int out_pixel_index = k * 4; float coefficient = horizontal_coefficients[coefficient_group + k - n0]; - STBIR__DEBUG_ASSERT(coefficient != 0); + STBIR_ASSERT(coefficient != 0); output_buffer[out_pixel_index + 0] += decode_buffer[in_pixel_index + 0] * coefficient; output_buffer[out_pixel_index + 1] += decode_buffer[in_pixel_index + 1] * coefficient; output_buffer[out_pixel_index + 2] += decode_buffer[in_pixel_index + 2] * coefficient; @@ -1644,7 +1636,7 @@ static void stbir__resample_horizontal_downsample(stbir__info* stbir_info, int n int c; int out_pixel_index = k * channels; float coefficient = horizontal_coefficients[coefficient_group + k - n0]; - STBIR__DEBUG_ASSERT(coefficient != 0); + STBIR_ASSERT(coefficient != 0); for (c = 0; c < channels; c++) output_buffer[out_pixel_index + c] += decode_buffer[in_pixel_index + c] * coefficient; } @@ -1856,7 +1848,7 @@ static void stbir__encode_scanline(stbir__info* stbir_info, int num_pixels, void break; default: - STBIR__UNIMPLEMENTED("Unknown type/colorspace/channels combination."); + STBIR_ASSERT(!"Unknown type/colorspace/channels combination."); break; } } @@ -1893,7 +1885,7 @@ static void stbir__resample_vertical_upsample(stbir__info* stbir_info, int n, in output_row_start = n * stbir_info->output_stride_bytes; - STBIR__DEBUG_ASSERT(stbir__use_height_upsampling(stbir_info)); + STBIR_ASSERT(stbir__use_height_upsampling(stbir_info)); memset(encode_buffer, 0, output_w * sizeof(float) * channels); @@ -2003,7 +1995,7 @@ static void stbir__resample_vertical_downsample(stbir__info* stbir_info, int n, n0 = vertical_contributors[contributor].n0; n1 = vertical_contributors[contributor].n1; - STBIR__DEBUG_ASSERT(!stbir__use_height_upsampling(stbir_info)); + STBIR_ASSERT(!stbir__use_height_upsampling(stbir_info)); for (k = n0; k <= n1; k++) { @@ -2068,7 +2060,7 @@ static void stbir__buffer_loop_upsample(stbir__info* stbir_info) float scale_ratio = stbir_info->vertical_scale; float out_scanlines_radius = stbir__filter_info_table[stbir_info->vertical_filter].support(1/scale_ratio) * scale_ratio; - STBIR__DEBUG_ASSERT(stbir__use_height_upsampling(stbir_info)); + STBIR_ASSERT(stbir__use_height_upsampling(stbir_info)); for (y = 0; y < stbir_info->output_h; y++) { @@ -2077,7 +2069,7 @@ static void stbir__buffer_loop_upsample(stbir__info* stbir_info) stbir__calculate_sample_range_upsample(y, out_scanlines_radius, scale_ratio, stbir_info->vertical_shift, &in_first_scanline, &in_last_scanline, &in_center_of_out); - STBIR__DEBUG_ASSERT(in_last_scanline - in_first_scanline <= stbir_info->vertical_filter_pixel_width); + STBIR_ASSERT(in_last_scanline - in_first_scanline <= stbir_info->vertical_filter_pixel_width); if (stbir_info->ring_buffer_begin_index >= 0) { @@ -2169,7 +2161,7 @@ static void stbir__buffer_loop_downsample(stbir__info* stbir_info) int pixel_margin = stbir_info->vertical_filter_pixel_margin; int max_y = stbir_info->input_h + pixel_margin; - STBIR__DEBUG_ASSERT(!stbir__use_height_upsampling(stbir_info)); + STBIR_ASSERT(!stbir__use_height_upsampling(stbir_info)); for (y = -pixel_margin; y < max_y; y++) { @@ -2178,7 +2170,7 @@ static void stbir__buffer_loop_downsample(stbir__info* stbir_info) stbir__calculate_sample_range_downsample(y, in_pixels_radius, scale_ratio, stbir_info->vertical_shift, &out_first_scanline, &out_last_scanline, &out_center_of_in); - STBIR__DEBUG_ASSERT(out_last_scanline - out_first_scanline <= stbir_info->vertical_filter_pixel_width); + STBIR_ASSERT(out_last_scanline - out_first_scanline <= stbir_info->vertical_filter_pixel_width); if (out_last_scanline < 0 || out_first_scanline >= output_h) continue; @@ -2229,8 +2221,8 @@ static void stbir__calculate_transform(stbir__info *info, float s0, float t0, fl info->horizontal_scale = ((float)info->output_w / info->input_w) / (s1 - s0); info->vertical_scale = ((float)info->output_h / info->input_h) / (t1 - t0); - info->horizontal_shift = s0 * info->input_w / (s1 - s0); - info->vertical_shift = t0 * info->input_h / (t1 - t0); + info->horizontal_shift = s0 * info->output_w / (s1 - s0); + info->vertical_shift = t0 * info->output_h / (t1 - t0); } } @@ -2380,7 +2372,7 @@ static int stbir__resize_allocated(stbir__info *info, info->ring_buffer = STBIR__NEXT_MEMPTR(info->decode_buffer, float); info->encode_buffer = STBIR__NEXT_MEMPTR(info->ring_buffer, float); - STBIR__DEBUG_ASSERT((size_t)STBIR__NEXT_MEMPTR(info->encode_buffer, unsigned char) == (size_t)tempmem + tempmem_size_in_bytes); + STBIR_ASSERT((size_t)STBIR__NEXT_MEMPTR(info->encode_buffer, unsigned char) == (size_t)tempmem + tempmem_size_in_bytes); } else { @@ -2388,7 +2380,7 @@ static int stbir__resize_allocated(stbir__info *info, info->ring_buffer = STBIR__NEXT_MEMPTR(info->horizontal_buffer, float); info->encode_buffer = NULL; - STBIR__DEBUG_ASSERT((size_t)STBIR__NEXT_MEMPTR(info->ring_buffer, unsigned char) == (size_t)tempmem + tempmem_size_in_bytes); + STBIR_ASSERT((size_t)STBIR__NEXT_MEMPTR(info->ring_buffer, unsigned char) == (size_t)tempmem + tempmem_size_in_bytes); } #undef STBIR__NEXT_MEMPTR @@ -2409,10 +2401,10 @@ static int stbir__resize_allocated(stbir__info *info, STBIR_PROGRESS_REPORT(1); #ifdef STBIR_DEBUG_OVERWRITE_TEST - STBIR__DEBUG_ASSERT(memcmp(overwrite_output_before_pre, &((unsigned char*)output_data)[-OVERWRITE_ARRAY_SIZE], OVERWRITE_ARRAY_SIZE) == 0); - STBIR__DEBUG_ASSERT(memcmp(overwrite_output_after_pre, &((unsigned char*)output_data)[begin_forbidden], OVERWRITE_ARRAY_SIZE) == 0); - STBIR__DEBUG_ASSERT(memcmp(overwrite_tempmem_before_pre, &((unsigned char*)tempmem)[-OVERWRITE_ARRAY_SIZE], OVERWRITE_ARRAY_SIZE) == 0); - STBIR__DEBUG_ASSERT(memcmp(overwrite_tempmem_after_pre, &((unsigned char*)tempmem)[tempmem_size_in_bytes], OVERWRITE_ARRAY_SIZE) == 0); + STBIR_ASSERT(memcmp(overwrite_output_before_pre, &((unsigned char*)output_data)[-OVERWRITE_ARRAY_SIZE], OVERWRITE_ARRAY_SIZE) == 0); + STBIR_ASSERT(memcmp(overwrite_output_after_pre, &((unsigned char*)output_data)[begin_forbidden], OVERWRITE_ARRAY_SIZE) == 0); + STBIR_ASSERT(memcmp(overwrite_tempmem_before_pre, &((unsigned char*)tempmem)[-OVERWRITE_ARRAY_SIZE], OVERWRITE_ARRAY_SIZE) == 0); + STBIR_ASSERT(memcmp(overwrite_tempmem_after_pre, &((unsigned char*)tempmem)[tempmem_size_in_bytes], OVERWRITE_ARRAY_SIZE) == 0); #endif return 1; diff --git a/src/stb_image_write.h b/src/stb_image_write.h index 98fa4103..4319c0de 100644 --- a/src/stb_image_write.h +++ b/src/stb_image_write.h @@ -1,4 +1,4 @@ -/* stb_image_write - v1.01 - public domain - http://nothings.org/stb/stb_image_write.h +/* stb_image_write - v1.02 - public domain - http://nothings.org/stb/stb_image_write.h writes out PNG/BMP/TGA images to C stdio - Sean Barrett 2010-2015 no warranty implied; use at your own risk @@ -102,12 +102,13 @@ CREDITS: Sergio Gonzalez Jonas Karlsson Filip Wasil + Thatcher Ulrich LICENSE -This software is in the public domain. Where that dedication is not -recognized, you are granted a perpetual, irrevocable license to copy, -distribute, and modify this file as you see fit. +This software is dual-licensed to the public domain and under the following +license: you are granted a perpetual, irrevocable license to copy, modify, +publish, and distribute this file as you see fit. */ @@ -736,7 +737,7 @@ unsigned char * stbi_zlib_compress(unsigned char *data, int data_len, int *out_l unsigned int bitbuf=0; int i,j, bitcount=0; unsigned char *out = NULL; - unsigned char **hash_table[stbiw__ZHASH]; // 64KB on the stack! + unsigned char ***hash_table = (unsigned char***) STBIW_MALLOC(stbiw__ZHASH * sizeof(char**)); if (quality < 5) quality = 5; stbiw__sbpush(out, 0x78); // DEFLATE 32K window @@ -808,6 +809,7 @@ unsigned char * stbi_zlib_compress(unsigned char *data, int data_len, int *out_l for (i=0; i < stbiw__ZHASH; ++i) (void) stbiw__sbfree(hash_table[i]); + STBIW_FREE(hash_table); { // compute adler32 on input @@ -1015,6 +1017,8 @@ STBIWDEF int stbi_write_png_to_func(stbi_write_func *func, void *context, int x, #endif // STB_IMAGE_WRITE_IMPLEMENTATION /* Revision history + 1.02 (2016-04-02) + avoid allocating large structures on the stack 1.01 (2016-01-16) STBIW_REALLOC_SIZED: support allocators with no realloc support avoid race-condition in crc initialization diff --git a/src/stb_rect_pack.h b/src/stb_rect_pack.h index 63a5b159..bd1cfc60 100644 --- a/src/stb_rect_pack.h +++ b/src/stb_rect_pack.h @@ -1,4 +1,4 @@ -// stb_rect_pack.h - v0.06 - public domain - rectangle packing +// stb_rect_pack.h - v0.08 - public domain - rectangle packing // Sean Barrett 2014 // // Useful for e.g. packing rectangular textures into an atlas. @@ -28,14 +28,22 @@ // Minor features // Martins Mozeiko // Bugfixes / warning fixes -// [your name could be here] +// Jeremy Jaussaud // // Version history: // +// 0.08 (2015-09-13) really fix bug with empty rects (w=0 or h=0) +// 0.07 (2015-09-13) fix bug with empty rects (w=0 or h=0) // 0.06 (2015-04-15) added STBRP_SORT to allow replacing qsort // 0.05: added STBRP_ASSERT to allow replacing assert // 0.04: fixed minor bug in STBRP_LARGE_RECTS support // 0.01: initial release +// +// LICENSE +// +// This software is dual-licensed to the public domain and under the following +// license: you are granted a perpetual, irrevocable license to copy, modify, +// publish, and distribute this file as you see fit. ////////////////////////////////////////////////////////////////////////////// // @@ -541,12 +549,16 @@ STBRP_DEF void stbrp_pack_rects(stbrp_context *context, stbrp_rect *rects, int n STBRP_SORT(rects, num_rects, sizeof(rects[0]), rect_height_compare); for (i=0; i < num_rects; ++i) { - stbrp__findresult fr = stbrp__skyline_pack_rectangle(context, rects[i].w, rects[i].h); - if (fr.prev_link) { - rects[i].x = (stbrp_coord) fr.x; - rects[i].y = (stbrp_coord) fr.y; + if (rects[i].w == 0 || rects[i].h == 0) { + rects[i].x = rects[i].y = 0; // empty rect needs no space } else { - rects[i].x = rects[i].y = STBRP__MAXVAL; + stbrp__findresult fr = stbrp__skyline_pack_rectangle(context, rects[i].w, rects[i].h); + if (fr.prev_link) { + rects[i].x = (stbrp_coord) fr.x; + rects[i].y = (stbrp_coord) fr.y; + } else { + rects[i].x = rects[i].y = STBRP__MAXVAL; + } } } diff --git a/src/stb_truetype.h b/src/stb_truetype.h index bfb1841f..d360d609 100644 --- a/src/stb_truetype.h +++ b/src/stb_truetype.h @@ -1,4 +1,4 @@ -// stb_truetype.h - v1.09 - public domain +// stb_truetype.h - v1.11 - public domain // authored from 2009-2015 by Sean Barrett / RAD Game Tools // // This library processes TrueType files: @@ -21,6 +21,10 @@ // Mikko Mononen: compound shape support, more cmap formats // Tor Andersson: kerning, subpixel rendering // +// Misc other: +// Ryan Gordon +// Simon Glass +// // Bug/warning reports/fixes: // "Zer" on mollyrocket (with fix) // Cass Everitt @@ -45,11 +49,10 @@ // Thomas Fields // Derek Vinyard // -// Misc other: -// Ryan Gordon -// // VERSION HISTORY // +// 1.11 (2016-04-02) fix unused-variable warning +// 1.10 (2016-04-02) user-defined fabs(); rare memory leak; remove duplicate typedef // 1.09 (2016-01-16) warning fix; avoid crash on outofmem; use allocation userdata properly // 1.08 (2015-09-13) document stbtt_Rasterize(); fixes for vertical & horizontal edges // 1.07 (2015-08-01) allow PackFontRanges to accept arrays of sparse codepoints; @@ -68,9 +71,9 @@ // // LICENSE // -// This software is in the public domain. Where that dedication is not -// recognized, you are granted a perpetual, irrevocable license to copy, -// distribute, and modify this file as you see fit. +// This software is dual-licensed to the public domain and under the following +// license: you are granted a perpetual, irrevocable license to copy, modify, +// publish, and distribute this file as you see fit. // // USAGE // @@ -406,6 +409,11 @@ int main(int arg, char **argv) #define STBTT_sqrt(x) sqrt(x) #endif + #ifndef STBTT_fabs + #include + #define STBTT_fabs(x) fabs(x) + #endif + // #define your own functions "STBTT_malloc" / "STBTT_free" to avoid malloc.h #ifndef STBTT_malloc #include @@ -629,7 +637,7 @@ STBTT_DEF int stbtt_GetFontOffsetForIndex(const unsigned char *data, int index); // The following structure is defined publically so you can declare one on // the stack or as a global or etc, but you should treat it as opaque. -typedef struct stbtt_fontinfo +struct stbtt_fontinfo { void * userdata; unsigned char * data; // pointer to .ttf file @@ -640,7 +648,7 @@ typedef struct stbtt_fontinfo int loca,head,glyf,hhea,hmtx,kern; // table locations as offset from start of .ttf int index_map; // a cmap mapping for our chosen character encoding int indexToLocFormat; // format needed to map from glyph index to glyph -} stbtt_fontinfo; +}; STBTT_DEF int stbtt_InitFont(stbtt_fontinfo *info, const unsigned char *data, int offset); // Given an offset into the file that defines a font, this function builds @@ -942,6 +950,12 @@ typedef int stbtt__test_oversample_pow2[(STBTT_MAX_OVERSAMPLE & (STBTT_MAX_OVERS #define STBTT_RASTERIZER_VERSION 2 #endif +#ifdef _MSC_VER +#define STBTT__NOTUSED(v) (void)(v) +#else +#define STBTT__NOTUSED(v) (void)sizeof(v) +#endif + ////////////////////////////////////////////////////////////////////////// // // accessors to parse data from file @@ -1993,7 +2007,7 @@ static void stbtt__fill_active_edges_new(float *scanline, float *scanline_fill, } y_crossing += dy * (x2 - (x1+1)); - STBTT_assert(fabs(area) <= 1.01f); + STBTT_assert(STBTT_fabs(area) <= 1.01f); scanline[x2] += area + sign * (1-((x2-x2)+(x_bottom-x2))/2) * (sy1-y_crossing); @@ -2071,6 +2085,8 @@ static void stbtt__rasterize_sorted_edges(stbtt__bitmap *result, stbtt__edge *e, int y,j=0, i; float scanline_data[129], *scanline, *scanline2; + STBTT__NOTUSED(vsubsample); + if (result->w > 64) scanline = (float *) STBTT_malloc((result->w*2+1) * sizeof(float), userdata); else @@ -2129,7 +2145,7 @@ static void stbtt__rasterize_sorted_edges(stbtt__bitmap *result, stbtt__edge *e, int m; sum += scanline2[i]; k = scanline[i] + sum; - k = (float) fabs(k)*255 + 0.5f; + k = (float) STBTT_fabs(k)*255 + 0.5f; m = (int) k; if (m > 255) m = 255; result->pixels[j*result->stride + i] = (unsigned char) m; @@ -2430,7 +2446,10 @@ STBTT_DEF unsigned char *stbtt_GetGlyphBitmapSubpixel(const stbtt_fontinfo *info if (scale_x == 0) scale_x = scale_y; if (scale_y == 0) { - if (scale_x == 0) return NULL; + if (scale_x == 0) { + STBTT_free(vertices, info->userdata); + return NULL; + } scale_y = scale_x; } @@ -2586,11 +2605,6 @@ STBTT_DEF void stbtt_GetBakedQuad(stbtt_bakedchar *chardata, int pw, int ph, int // #ifndef STB_RECT_PACK_VERSION -#ifdef _MSC_VER -#define STBTT__NOTUSED(v) (void)(v) -#else -#define STBTT__NOTUSED(v) (void)sizeof(v) -#endif typedef int stbrp_coord; @@ -3205,6 +3219,10 @@ STBTT_DEF int stbtt_FindMatchingFont(const unsigned char *font_collection, const // FULL VERSION HISTORY // +// 1.11 (2016-04-02) fix unused-variable warning +// 1.10 (2016-04-02) allow user-defined fabs() replacement +// fix memory leak if fontsize=0.0 +// fix warning from duplicate typedef // 1.09 (2016-01-16) warning fix; avoid crash on outofmem; use alloc userdata for PackFontRanges // 1.08 (2015-09-13) document stbtt_Rasterize(); fixes for vertical & horizontal edges // 1.07 (2015-08-01) allow PackFontRanges to accept arrays of sparse codepoints; diff --git a/src/stb_vorbis.c b/src/stb_vorbis.c index 0510edc7..07d79318 100644 --- a/src/stb_vorbis.c +++ b/src/stb_vorbis.c @@ -168,6 +168,9 @@ #include #if !(defined(__APPLE__) || defined(MACOSX) || defined(macintosh) || defined(Macintosh)) #include +#if defined(__linux__) || defined(__linux) || defined(__EMSCRIPTEN__) +#include +#endif #endif #else // STB_VORBIS_NO_CRT #define NULL 0 @@ -1484,85 +1487,6 @@ static int codebook_decode_deinterleave_repeat(vorb *f, Codebook *c, float **out return TRUE; } -#ifndef STB_VORBIS_DIVIDES_IN_CODEBOOK -static int codebook_decode_deinterleave_repeat_2(vorb *f, Codebook *c, float **outputs, int *c_inter_p, int *p_inter_p, int len, int total_decode) -{ - int c_inter = *c_inter_p; - int p_inter = *p_inter_p; - int i,z, effective = c->dimensions; - - // type 0 is only legal in a scalar context - if (c->lookup_type == 0) return error(f, VORBIS_invalid_stream); - - while (total_decode > 0) { - float last = CODEBOOK_ELEMENT_BASE(c); - DECODE_VQ(z,f,c); - - if (z < 0) { - if (!f->bytes_in_seg) - if (f->last_seg) return FALSE; - return error(f, VORBIS_invalid_stream); - } - - // if this will take us off the end of the buffers, stop short! - // we check by computing the length of the virtual interleaved - // buffer (len*ch), our current offset within it (p_inter*ch)+(c_inter), - // and the length we'll be using (effective) - if (c_inter + p_inter*2 + effective > len * 2) { - effective = len*2 - (p_inter*2 - c_inter); - } - - { - z *= c->dimensions; - if (c->sequence_p) { - // haven't optimized this case because I don't have any examples - for (i=0; i < effective; ++i) { - float val = CODEBOOK_ELEMENT_FAST(c,z+i) + last; - if (outputs[c_inter]) - outputs[c_inter][p_inter] += val; - if (++c_inter == 2) { c_inter = 0; ++p_inter; } - last = val; - } - } else { - i=0; - if (c_inter == 1 && i < effective) { - float val = CODEBOOK_ELEMENT_FAST(c,z+i) + last; - if (outputs[c_inter]) - outputs[c_inter][p_inter] += val; - c_inter = 0; ++p_inter; - ++i; - } - { - float *z0 = outputs[0]; - float *z1 = outputs[1]; - for (; i+1 < effective;) { - float v0 = CODEBOOK_ELEMENT_FAST(c,z+i) + last; - float v1 = CODEBOOK_ELEMENT_FAST(c,z+i+1) + last; - if (z0) - z0[p_inter] += v0; - if (z1) - z1[p_inter] += v1; - ++p_inter; - i += 2; - } - } - if (i < effective) { - float val = CODEBOOK_ELEMENT_FAST(c,z+i) + last; - if (outputs[c_inter]) - outputs[c_inter][p_inter] += val; - if (++c_inter == 2) { c_inter = 0; ++p_inter; } - } - } - } - - total_decode -= effective; - } - *c_inter_p = c_inter; - *p_inter_p = p_inter; - return TRUE; -} -#endif - static int predict_point(int x, int x0, int x1, int y0, int y1) { int dy = y1 - y0; @@ -1941,6 +1865,11 @@ static void decode_residue(vorb *f, float *residue_buffers[], int ch, int n, int } done: CHECK(f); + #ifndef STB_VORBIS_DIVIDES_IN_RESIDUE + temp_free(f,part_classdata); + #else + temp_free(f,classifications); + #endif temp_alloc_restore(f,temp_alloc_point); } @@ -2586,6 +2515,7 @@ static void inverse_mdct(float *buffer, int n, vorb *f, int blocktype) } } + temp_free(f,buf2); temp_alloc_restore(f,save_point); } @@ -3499,7 +3429,6 @@ static int start_decoder(vorb *f) } } } - setup_temp_free(f, mults,sizeof(mults[0])*c->lookup_values); c->lookup_type = 2; } else @@ -3515,11 +3444,11 @@ static int start_decoder(vorb *f) if (c->sequence_p) last = val; } - setup_temp_free(f, mults,sizeof(mults[0])*c->lookup_values); } #ifndef STB_VORBIS_DIVIDES_IN_CODEBOOK skip:; #endif + setup_temp_free(f, mults, sizeof(mults[0])*c->lookup_values); CHECK(f); } @@ -4045,7 +3974,7 @@ int stb_vorbis_decode_frame_pushdata( while (get8_packet(f) != EOP) if (f->eof) break; *samples = 0; - return f->stream - data; + return (int) (f->stream - data); } if (error == VORBIS_continued_packet_flag_invalid) { if (f->previous_length == 0) { @@ -4055,7 +3984,7 @@ int stb_vorbis_decode_frame_pushdata( while (get8_packet(f) != EOP) if (f->eof) break; *samples = 0; - return f->stream - data; + return (int) (f->stream - data); } } // if we get an error while parsing, what to do? @@ -4075,7 +4004,7 @@ int stb_vorbis_decode_frame_pushdata( if (channels) *channels = f->channels; *samples = len; *output = f->outputs; - return f->stream - data; + return (int) (f->stream - data); } stb_vorbis *stb_vorbis_open_pushdata( @@ -4098,7 +4027,7 @@ stb_vorbis *stb_vorbis_open_pushdata( f = vorbis_alloc(&p); if (f) { *f = p; - *data_used = f->stream - data; + *data_used = (int) (f->stream - data); *error = 0; return f; } else { @@ -4113,9 +4042,9 @@ unsigned int stb_vorbis_get_file_offset(stb_vorbis *f) #ifndef STB_VORBIS_NO_PUSHDATA_API if (f->push_mode) return 0; #endif - if (USE_MEMORY(f)) return f->stream - f->stream_start; + if (USE_MEMORY(f)) return (unsigned int) (f->stream - f->stream_start); #ifndef STB_VORBIS_NO_STDIO - return ftell(f->f) - f->f_start; + return (unsigned int) (ftell(f->f) - f->f_start); #endif } @@ -4611,7 +4540,7 @@ stb_vorbis * stb_vorbis_open_file_section(FILE *file, int close_on_free, int *er stb_vorbis *f, p; vorbis_init(&p, alloc); p.f = file; - p.f_start = ftell(file); + p.f_start = (uint32) ftell(file); p.stream_len = length; p.close_on_free = close_on_free; if (start_decoder(&p)) { @@ -4630,9 +4559,9 @@ stb_vorbis * stb_vorbis_open_file_section(FILE *file, int close_on_free, int *er stb_vorbis * stb_vorbis_open_file(FILE *file, int close_on_free, int *error, const stb_vorbis_alloc *alloc) { unsigned int len, start; - start = ftell(file); + start = (unsigned int) ftell(file); fseek(file, 0, SEEK_END); - len = ftell(file) - start; + len = (unsigned int) (ftell(file) - start); fseek(file, start, SEEK_SET); return stb_vorbis_open_file_section(file, close_on_free, error, alloc, len); } @@ -5027,6 +4956,9 @@ int stb_vorbis_get_samples_float(stb_vorbis *f, int channels, float **buffer, in #endif // STB_VORBIS_NO_PULLDATA_API /* Version history + 1.09 - 2016/04/04 - back out 'avoid discarding last frame' fix from previous version + 1.08 - 2016/04/02 - fixed multiple warnings; fix setup memory leaks; + avoid discarding last frame of audio data 1.07 - 2015/01/16 - fixed some warnings, fix mingw, const-correct API some more crash fixes when out of memory or with corrupt files 1.06 - 2015/08/31 - full, correct support for seeking API (Dougall Johnson) diff --git a/src/stb_vorbis.h b/src/stb_vorbis.h index bd318972..624ce4bc 100644 --- a/src/stb_vorbis.h +++ b/src/stb_vorbis.h @@ -1,4 +1,4 @@ -// Ogg Vorbis audio decoder - v1.07 - public domain +// Ogg Vorbis audio decoder - v1.09 - public domain // http://nothings.org/stb_vorbis/ // // Original version written by Sean Barrett in 2007. @@ -9,9 +9,9 @@ // // LICENSE // -// This software is in the public domain. Where that dedication is not -// recognized, you are granted a perpetual, irrevocable license to copy, -// distribute, and modify this file as you see fit. +// This software is dual-licensed to the public domain and under the following +// license: you are granted a perpetual, irrevocable license to copy, modify, +// publish, and distribute this file as you see fit. // // No warranty for any purpose is expressed or implied by the author (nor // by RAD Game Tools). Report bugs and send enhancements to the author. @@ -31,11 +31,14 @@ // Terje Mathisen Niklas Frykholm Andy Hill // Casey Muratori John Bolton Gargaj // Laurent Gomila Marc LeBlanc Ronny Chevalier -// Bernhard Wodo Evan Balster "alxprd"@github +// Bernhard Wodo Evan Balster alxprd@github // Tom Beaumont Ingo Leitgeb Nicolas Guillemot -// Phillip Bennefall Rohit +// Phillip Bennefall Rohit Thiago Goulart +// manxorist@github saga musix // // Partial history: +// 1.09 - 2016/04/04 - back out 'truncation of last frame' fix from previous version +// 1.08 - 2016/04/02 - warnings; setup memory leaks; truncation of last frame // 1.07 - 2015/01/16 - fixes for crashes on invalid files; warning fixes; const // 1.06 - 2015/08/31 - full, correct support for seeking API (Dougall Johnson) // some crash fixes when out of memory or with corrupt files @@ -72,11 +75,6 @@ #include "utils.h" // Android fopen function map #endif -// RaySan: Added for Linux -#ifdef __linux - #include -#endif - #ifdef __cplusplus extern "C" { #endif -- cgit v1.2.3 From aa22d979834eeddb849f45bba7c0f467b575e6db Mon Sep 17 00:00:00 2001 From: raysan5 Date: Thu, 7 Apr 2016 13:31:53 +0200 Subject: Simplified texture flip and added comments --- src/rlgl.c | 10 +++++++--- src/textures.c | 4 ++++ 2 files changed, 11 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/rlgl.c b/src/rlgl.c index b2a36351..f0acf124 100644 --- a/src/rlgl.c +++ b/src/rlgl.c @@ -1897,6 +1897,9 @@ Model rlglLoadModel(Mesh mesh) // Create buffers for our vertex data (positions, texcoords, normals) glGenBuffers(3, vertexBuffer); + + // NOTE: Default shader is assigned to model, so vbo buffers are properly linked to vertex attribs + // If model shader is changed, vbo buffers must be re-assigned to new location points (previously loaded) // Enable vertex attributes: position glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer[0]); @@ -2489,13 +2492,14 @@ static Shader LoadDefaultShader(void) } // Get location handlers to for shader attributes and uniforms +// NOTE: If any location is not found, loc point becomes -1 static void LoadDefaultShaderLocations(Shader *shader) { // Get handles to GLSL input attibute locations shader->vertexLoc = glGetAttribLocation(shader->id, "vertexPosition"); shader->texcoordLoc = glGetAttribLocation(shader->id, "vertexTexCoord"); shader->normalLoc = glGetAttribLocation(shader->id, "vertexNormal"); - shader->colorLoc = glGetAttribLocation(shader->id, "vertexColor"); // -1 if not found + shader->colorLoc = glGetAttribLocation(shader->id, "vertexColor"); // Get handles to GLSL uniform locations (vertex shader) shader->mvpLoc = glGetUniformLocation(shader->id, "mvpMatrix"); @@ -2503,8 +2507,8 @@ static void LoadDefaultShaderLocations(Shader *shader) // Get handles to GLSL uniform locations (fragment shader) shader->tintColorLoc = glGetUniformLocation(shader->id, "fragTintColor"); shader->mapDiffuseLoc = glGetUniformLocation(shader->id, "texture0"); - shader->mapNormalLoc = glGetUniformLocation(shader->id, "texture1"); // -1 if not found - shader->mapSpecularLoc = glGetUniformLocation(shader->id, "texture2"); // -1 if not found + shader->mapNormalLoc = glGetUniformLocation(shader->id, "texture1"); + shader->mapSpecularLoc = glGetUniformLocation(shader->id, "texture2"); } // Read text file diff --git a/src/textures.c b/src/textures.c index 67264afb..79047ab7 100644 --- a/src/textures.c +++ b/src/textures.c @@ -1385,6 +1385,10 @@ void DrawTextureEx(Texture2D texture, Vector2 position, float rotation, float sc void DrawTextureRec(Texture2D texture, Rectangle sourceRec, Vector2 position, Color tint) { Rectangle destRec = { (int)position.x, (int)position.y, abs(sourceRec.width), abs(sourceRec.height) }; + + if (sourceRec.width < 0) sourceRec.x -= sourceRec.width; + if (sourceRec.height < 0) sourceRec.y -= sourceRec.height; + Vector2 origin = { 0, 0 }; DrawTexturePro(texture, sourceRec, destRec, origin, 0.0f, tint); -- cgit v1.2.3 From c1e49d2b13c75da2c532079d454fad32f9ceccd4 Mon Sep 17 00:00:00 2001 From: raysan5 Date: Fri, 8 Apr 2016 12:00:29 +0200 Subject: Removed function I decided it is redundant and could be confusing (when mixed with 3D drawing). It's not KISS. --- src/core.c | 11 ----------- src/raylib.h | 1 - 2 files changed, 12 deletions(-) (limited to 'src') diff --git a/src/core.c b/src/core.c index ae6f4cad..e01f3969 100644 --- a/src/core.c +++ b/src/core.c @@ -568,17 +568,6 @@ void BeginDrawingEx(Camera2D camera) rlMultMatrixf(MatrixToFloat(matTransform)); } -// Setup drawing canvas with pro parameters -void BeginDrawingPro(int blendMode, Shader shader, Matrix transform) -{ - BeginDrawing(); - - SetBlendMode(blendMode); - SetCustomShader(shader); - - rlMultMatrixf(MatrixToFloat(transform)); -} - // End canvas drawing and Swap Buffers (Double Buffering) void EndDrawing(void) { diff --git a/src/raylib.h b/src/raylib.h index 22fcb4ac..52a5e73c 100644 --- a/src/raylib.h +++ b/src/raylib.h @@ -567,7 +567,6 @@ int GetScreenHeight(void); // Get current scree void ClearBackground(Color color); // Sets Background Color void BeginDrawing(void); // Setup drawing canvas to start drawing void BeginDrawingEx(Camera2D camera); // Setup drawing canvas with 2d camera -void BeginDrawingPro(int blendMode, Shader shader, Matrix transform); // Setup drawing canvas with pro parameters void EndDrawing(void); // End canvas drawing and Swap Buffers (Double Buffering) void Begin3dMode(Camera camera); // Initializes 3D mode for drawing (Camera setup) -- cgit v1.2.3 From 284eaf157692469e061782fbf3753e22ee5eb861 Mon Sep 17 00:00:00 2001 From: raysan5 Date: Sun, 10 Apr 2016 19:38:57 +0200 Subject: Use Depth Texture on OpenGL 3.3 --- src/rlgl.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/rlgl.c b/src/rlgl.c index f0acf124..b659577a 100644 --- a/src/rlgl.c +++ b/src/rlgl.c @@ -1688,7 +1688,7 @@ RenderTexture2D rlglLoadRenderTexture(int width, int height) target.depth.id = 0; target.depth.width = width; target.depth.height = height; - target.depth.format = 19; //DEPTH_COMPONENT_16BIT + target.depth.format = 19; //DEPTH_COMPONENT_24BIT target.depth.mipmaps = 1; #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) @@ -1701,14 +1701,18 @@ RenderTexture2D rlglLoadRenderTexture(int width, int height) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL); glBindTexture(GL_TEXTURE_2D, 0); - -#define USE_DEPTH_RENDERBUFFER + +#if defined(GRAPHICS_API_OPENGL_33) + #define USE_DEPTH_TEXTURE +#else + #define USE_DEPTH_RENDERBUFFER +#endif + #if defined(USE_DEPTH_RENDERBUFFER) // Create the renderbuffer that will serve as the depth attachment for the framebuffer. glGenRenderbuffers(1, &target.depth.id); glBindRenderbuffer(GL_RENDERBUFFER, target.depth.id); glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT24, width, height); - #elif defined(USE_DEPTH_TEXTURE) // NOTE: We can also use a texture for depth buffer (GL_ARB_depth_texture/GL_OES_depth_texture extension required) // A renderbuffer is simpler than a texture and could offer better performance on embedded devices @@ -1718,7 +1722,7 @@ RenderTexture2D rlglLoadRenderTexture(int width, int height) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); - glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, width, height, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, NULL); + glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT24, width, height, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, NULL); glBindTexture(GL_TEXTURE_2D, 0); #endif -- cgit v1.2.3 From 6b5e18e6bfe9940987cc38dcf40e2e903fe3b235 Mon Sep 17 00:00:00 2001 From: raysan5 Date: Sun, 17 Apr 2016 11:19:32 +0200 Subject: Make mouse inputs available on Android for... ... easy code porting, transalating them to touches and gestures internally. Removed function SetCustomCursor(), it can be managed by the user. --- src/core.c | 245 ++++++++++++++++++++++++++++++++--------------------------- src/raylib.h | 35 ++++----- 2 files changed, 148 insertions(+), 132 deletions(-) (limited to 'src') diff --git a/src/core.c b/src/core.c index e01f3969..294936fb 100644 --- a/src/core.c +++ b/src/core.c @@ -195,26 +195,19 @@ static int renderOffsetY = 0; // Offset Y from render area (must b static bool fullscreen = false; // Fullscreen mode (useful only for PLATFORM_DESKTOP) static Matrix downscaleView; // Matrix to downscale view (in case screen size bigger than display size) -static Vector2 touchPosition[MAX_TOUCH_POINTS]; // Touch position on screen - #if defined(PLATFORM_DESKTOP) || defined(PLATFORM_RPI) || defined(PLATFORM_WEB) static const char *windowTitle; // Window text title... - -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 - -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 -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 +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 int previousMouseWheelY = 0; // Required to track mouse wheel variation static int currentMouseWheelY = 0; // Required to track mouse wheel variation @@ -224,6 +217,9 @@ static int lastKeyPressed = -1; // Register last key pressed static bool cursorHidden; // Track if cursor is hidden #endif +static Vector2 mousePosition; // Mouse position on screen +static Vector2 touchPosition[MAX_TOUCH_POINTS]; // Touch position on screen + #if defined(PLATFORM_DESKTOP) static char **dropFilesPath; // Store dropped files paths as strings static int dropFilesCount = 0; // Count stored strings @@ -494,29 +490,6 @@ void ToggleFullscreen(void) #endif } -#if defined(PLATFORM_DESKTOP) || defined(PLATFORM_RPI) || defined(PLATFORM_WEB) -// Set a custom cursor icon/image -void SetCustomCursor(const char *cursorImage) -{ - if (customCursor) UnloadTexture(cursor); - - cursor = LoadTexture(cursorImage); - -#if defined(PLATFORM_DESKTOP) - // NOTE: emscripten not implemented - glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_HIDDEN); -#endif - customCursor = true; -} - -// Set a custom key to exit program -// NOTE: default exitKey is ESCAPE -void SetExitKey(int key) -{ - exitKey = key; -} -#endif - // Get current screen width int GetScreenWidth(void) { @@ -1032,78 +1005,11 @@ int GetKeyPressed(void) return lastKeyPressed; } -// Detect if a mouse button has been pressed once -bool IsMouseButtonPressed(int button) -{ - bool pressed = false; - - if ((currentMouseState[button] != previousMouseState[button]) && (currentMouseState[button] == 1)) pressed = true; - else pressed = false; - - return pressed; -} - -// Detect if a mouse button is being pressed -bool IsMouseButtonDown(int button) -{ - if (GetMouseButtonStatus(button) == 1) return true; - else return false; -} - -// Detect if a mouse button has been released once -bool IsMouseButtonReleased(int button) -{ - bool released = false; - - if ((currentMouseState[button] != previousMouseState[button]) && (currentMouseState[button] == 0)) released = true; - else released = false; - - return released; -} - -// Detect if a mouse button is NOT being pressed -bool IsMouseButtonUp(int button) -{ - if (GetMouseButtonStatus(button) == 0) return true; - else return false; -} - -// Returns mouse position X -int GetMouseX(void) -{ - return (int)mousePosition.x; -} - -// Returns mouse position Y -int GetMouseY(void) -{ - return (int)mousePosition.y; -} - -// Returns mouse position XY -Vector2 GetMousePosition(void) -{ - return mousePosition; -} - -// Set mouse position XY -void SetMousePosition(Vector2 position) -{ - mousePosition = position; -#if defined(PLATFORM_DESKTOP) || defined(PLATFORM_WEB) - // NOTE: emscripten not implemented - glfwSetCursorPos(window, position.x, position.y); -#endif -} - -// Returns mouse wheel movement Y -int GetMouseWheelMove(void) +// Set a custom key to exit program +// NOTE: default exitKey is ESCAPE +void SetExitKey(int key) { -#if defined(PLATFORM_WEB) - return previousMouseWheelY/100; -#else - return previousMouseWheelY; -#endif + exitKey = key; } // Hide mouse cursor @@ -1280,6 +1186,111 @@ bool IsGamepadButtonUp(int gamepad, int button) } #endif //defined(PLATFORM_DESKTOP) || defined(PLATFORM_RPI) || defined(PLATFORM_WEB) + +// Detect if a mouse button has been pressed once +bool IsMouseButtonPressed(int button) +{ + bool pressed = false; + +#if defined(PLATFORM_ANDROID) + if (IsGestureDetected() && (GetGestureType() == GESTURE_TAP)) pressed = true; +#else + if ((currentMouseState[button] != previousMouseState[button]) && (currentMouseState[button] == 1)) pressed = true; +#endif + + return pressed; +} + +// Detect if a mouse button is being pressed +bool IsMouseButtonDown(int button) +{ + bool down = false; + +#if defined(PLATFORM_ANDROID) + if (IsGestureDetected() && (GetGestureType() == GESTURE_HOLD)) down = true; +#else + if (GetMouseButtonStatus(button) == 1) down = true; +#endif + + return down; +} + +// Detect if a mouse button has been released once +bool IsMouseButtonReleased(int button) +{ + bool released = false; + +#if !defined(PLATFORM_ANDROID) + if ((currentMouseState[button] != previousMouseState[button]) && (currentMouseState[button] == 0)) released = true; +#endif + + return released; +} + +// Detect if a mouse button is NOT being pressed +bool IsMouseButtonUp(int button) +{ + bool up = false; + +#if !defined(PLATFORM_ANDROID) + if (GetMouseButtonStatus(button) == 0) up = true; +#endif + + return up; +} + +// Returns mouse position X +int GetMouseX(void) +{ +#if defined(PLATFORM_ANDROID) + return (int)touchPosition[0].x; +#else + return (int)mousePosition.x; +#endif +} + +// Returns mouse position Y +int GetMouseY(void) +{ +#if defined(PLATFORM_ANDROID) + return (int)touchPosition[0].x; +#else + return (int)mousePosition.y; +#endif +} + +// Returns mouse position XY +Vector2 GetMousePosition(void) +{ +#if defined(PLATFORM_ANDROID) + return GetTouchPosition(0); +#else + return mousePosition; +#endif +} + +// Set mouse position XY +void SetMousePosition(Vector2 position) +{ + mousePosition = position; +#if defined(PLATFORM_DESKTOP) || defined(PLATFORM_WEB) + // NOTE: emscripten not implemented + glfwSetCursorPos(window, position.x, position.y); +#endif +} + +// Returns mouse wheel movement Y +int GetMouseWheelMove(void) +{ +#if defined(PLATFORM_ANDROID) + return 0; +#elif defined(PLATFORM_WEB) + return previousMouseWheelY/100; +#else + return previousMouseWheelY; +#endif +} + // Returns touch position X int GetTouchX(void) { @@ -1376,7 +1387,7 @@ static void InitDisplay(int width, int height) // Downscale matrix is required in case desired screen area is bigger than display area downscaleView = MatrixIdentity(); - + #if defined(PLATFORM_DESKTOP) || defined(PLATFORM_WEB) glfwSetErrorCallback(ErrorCallback); @@ -1393,10 +1404,12 @@ static void InitDisplay(int width, int height) // Screen size security check if (screenWidth <= 0) screenWidth = displayWidth; if (screenHeight <= 0) screenHeight = displayHeight; -#elif defined(PLATFORM_WEB) +#endif // defined(PLATFORM_DESKTOP) + +#if defined(PLATFORM_WEB) displayWidth = screenWidth; displayHeight = screenHeight; -#endif +#endif // defined(PLATFORM_WEB) glfwDefaultWindowHints(); // Set default windows hints @@ -1447,7 +1460,7 @@ static void InitDisplay(int width, int height) // TODO: Check modes[i]->width; // TODO: Check modes[i]->height; } - + window = glfwCreateWindow(screenWidth, screenHeight, windowTitle, glfwGetPrimaryMonitor(), NULL); } else @@ -1543,8 +1556,9 @@ static void InitDisplay(int width, int height) } //glfwGetFramebufferSize(window, &renderWidth, &renderHeight); // Get framebuffer size of current window +#endif // defined(PLATFORM_DESKTOP) || defined(PLATFORM_WEB) -#elif defined(PLATFORM_ANDROID) || defined(PLATFORM_RPI) +#if defined(PLATFORM_ANDROID) || defined(PLATFORM_RPI) fullscreen = true; // Screen size security check @@ -1629,8 +1643,9 @@ static void InitDisplay(int width, int height) //ANativeWindow_setBuffersGeometry(app->window, 0, 0, displayFormat); // Force use of native display size surface = eglCreateWindowSurface(display, config, app->window, NULL); +#endif // defined(PLATFORM_ANDROID) -#elif defined(PLATFORM_RPI) +#if defined(PLATFORM_RPI) graphics_get_display_size(0, &displayWidth, &displayHeight); // At this point we need to manage render size vs screen size @@ -1668,7 +1683,7 @@ static void InitDisplay(int width, int height) surface = eglCreateWindowSurface(display, config, &nativeWindow, NULL); //--------------------------------------------------------------------------------- -#endif +#endif // defined(PLATFORM_RPI) // There must be at least one frame displayed before the buffers are swapped //eglSwapInterval(display, 1); @@ -1688,13 +1703,17 @@ static void InitDisplay(int width, int height) TraceLog(INFO, "Screen size: %i x %i", screenWidth, screenHeight); TraceLog(INFO, "Viewport offsets: %i, %i", renderOffsetX, renderOffsetY); } -#endif +#endif // defined(PLATFORM_ANDROID) || defined(PLATFORM_RPI) } // Initialize OpenGL graphics static void InitGraphics(void) { rlglInit(); // Init rlgl + +#if defined(PLATFORM_OCULUS) + //rlglInitOculus(); // Init rlgl for Oculus Rift (required textures) +#endif rlglInitGraphics(renderOffsetX, renderOffsetY, renderWidth, renderHeight); // Init graphics (OpenGL stuff) @@ -2153,7 +2172,7 @@ static bool GetMouseButtonStatus(int button) #if defined(PLATFORM_DESKTOP) || defined(PLATFORM_WEB) return glfwGetMouseButton(window, button); #elif defined(PLATFORM_ANDROID) - // TODO: Check for virtual keyboard + // TODO: Check for virtual mouse return false; #elif defined(PLATFORM_RPI) // NOTE: Mouse buttons states are filled in PollInputEvents() diff --git a/src/raylib.h b/src/raylib.h index 52a5e73c..2fe29cc8 100644 --- a/src/raylib.h +++ b/src/raylib.h @@ -557,13 +557,15 @@ void CloseWindow(void); // Close Window and bool WindowShouldClose(void); // Detect if KEY_ESCAPE pressed or Close icon pressed bool IsWindowMinimized(void); // Detect if window has been minimized (or lost focus) void ToggleFullscreen(void); // Fullscreen toggle (only PLATFORM_DESKTOP) -#if defined(PLATFORM_DESKTOP) || defined(PLATFORM_RPI) -void SetCustomCursor(const char *cursorImage); // Set a custom cursor icon/image -void SetExitKey(int key); // Set a custom key to exit program (default is ESC) -#endif int GetScreenWidth(void); // Get current screen width int GetScreenHeight(void); // Get current screen height +void ShowCursor(void); // Shows cursor +void HideCursor(void); // Hides cursor +bool IsCursorHidden(void); // Returns true if cursor is not visible +void EnableCursor(void); // Enables cursor +void DisableCursor(void); // Disables cursor + void ClearBackground(Color color); // Sets Background Color void BeginDrawing(void); // Setup drawing canvas to start drawing void BeginDrawingEx(Camera2D camera); // Setup drawing canvas with 2d camera @@ -610,6 +612,15 @@ bool IsKeyDown(int key); // Detect if a key is be bool IsKeyReleased(int key); // Detect if a key has been released once bool IsKeyUp(int key); // Detect if a key is NOT being pressed int GetKeyPressed(void); // Get latest key pressed +void SetExitKey(int key); // Set a custom key to exit program (default is ESC) + +bool IsGamepadAvailable(int gamepad); // Detect if a gamepad is available +float GetGamepadAxisMovement(int gamepad, int axis); // Return axis movement value for a gamepad axis +bool IsGamepadButtonPressed(int gamepad, int button); // Detect if a gamepad button has been pressed once +bool IsGamepadButtonDown(int gamepad, int button); // Detect if a gamepad button is being pressed +bool IsGamepadButtonReleased(int gamepad, int button); // Detect if a gamepad button has been released once +bool IsGamepadButtonUp(int gamepad, int button); // Detect if a gamepad button is NOT being pressed +#endif bool IsMouseButtonPressed(int button); // Detect if a mouse button has been pressed once bool IsMouseButtonDown(int button); // Detect if a mouse button is being pressed @@ -621,20 +632,6 @@ Vector2 GetMousePosition(void); // Returns mouse positio void SetMousePosition(Vector2 position); // Set mouse position XY int GetMouseWheelMove(void); // Returns mouse wheel movement Y -void ShowCursor(void); // Shows cursor -void HideCursor(void); // Hides cursor -void EnableCursor(void); // Enables cursor -void DisableCursor(void); // Disables cursor -bool IsCursorHidden(void); // Returns true if cursor is not visible - -bool IsGamepadAvailable(int gamepad); // Detect if a gamepad is available -float GetGamepadAxisMovement(int gamepad, int axis); // Return axis movement value for a gamepad axis -bool IsGamepadButtonPressed(int gamepad, int button); // Detect if a gamepad button has been pressed once -bool IsGamepadButtonDown(int gamepad, int button); // Detect if a gamepad button is being pressed -bool IsGamepadButtonReleased(int gamepad, int button); // Detect if a gamepad button has been released once -bool IsGamepadButtonUp(int gamepad, int button); // Detect if a gamepad button is NOT being pressed -#endif - int GetTouchX(void); // Returns touch position X for touch point 0 (relative to screen size) int GetTouchY(void); // Returns touch position Y for touch point 0 (relative to screen size) Vector2 GetTouchPosition(int index); // Returns touch position XY for a touch point index (relative to screen size) @@ -649,7 +646,7 @@ bool IsButtonReleased(int button); // Detect if an android // Gestures and Touch Handling Functions (Module: gestures) //------------------------------------------------------------------------------------ void ProcessGestureEvent(GestureEvent event); // Process gesture event and translate it into gestures -void UpdateGestures(void); // Update gestures detected (must be called every frame) +void UpdateGestures(void); // Update gestures detected (called automatically in PollInputEvents()) bool IsGestureDetected(void); // Check if a gesture have been detected int GetGestureType(void); // Get latest detected gesture void SetGesturesEnabled(unsigned int gestureFlags); // Enable a set of gestures using flags -- cgit v1.2.3 From 2e5d898443767e027f0e1deab7ed3060085b62fd Mon Sep 17 00:00:00 2001 From: raysan5 Date: Sun, 17 Apr 2016 11:25:04 +0200 Subject: Corrected bug with old FBO struct --- src/rlgl.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/rlgl.c b/src/rlgl.c index b659577a..06efd777 100644 --- a/src/rlgl.c +++ b/src/rlgl.c @@ -1712,7 +1712,7 @@ RenderTexture2D rlglLoadRenderTexture(int width, int height) // Create the renderbuffer that will serve as the depth attachment for the framebuffer. glGenRenderbuffers(1, &target.depth.id); glBindRenderbuffer(GL_RENDERBUFFER, target.depth.id); - glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT24, width, height); + glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, width, height); // GL_DEPTH_COMPONENT24 not supported on Android #elif defined(USE_DEPTH_TEXTURE) // NOTE: We can also use a texture for depth buffer (GL_ARB_depth_texture/GL_OES_depth_texture extension required) // A renderbuffer is simpler than a texture and could offer better performance on embedded devices @@ -2032,8 +2032,9 @@ void *rlglReadTexturePixels(Texture2D texture) #endif #if defined(GRAPHICS_API_OPENGL_ES2) - FBO fbo = rlglLoadFBO(texture.width, texture.height); - + + RenderTexture2D fbo = rlglLoadRenderTexture(texture.width, texture.height); + // NOTE: Two possible Options: // 1 - Bind texture to color fbo attachment and glReadPixels() // 2 - Create an fbo, activate it, render quad with texture, glReadPixels() @@ -2054,7 +2055,7 @@ void *rlglReadTexturePixels(Texture2D texture) glReadPixels(0, 0, texture.width, texture.height, GL_RGBA, GL_UNSIGNED_BYTE, pixels); // Re-attach internal FBO color texture before deleting it - glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, fbo.colorTextureId, 0); + glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, fbo.texture.id, 0); glBindFramebuffer(GL_FRAMEBUFFER, 0); @@ -2093,7 +2094,8 @@ void *rlglReadTexturePixels(Texture2D texture) #endif // GET_TEXTURE_FBO_OPTION // Clean up temporal fbo - rlglUnloadFBO(fbo); + rlDeleteRenderTextures(fbo); + #endif return pixels; -- cgit v1.2.3 From 17eefed08fa052b39c9ee4c4cb486794a8551c92 Mon Sep 17 00:00:00 2001 From: raysan5 Date: Sun, 17 Apr 2016 11:36:40 +0200 Subject: Improved gestures system --- src/core.c | 4 ++-- src/gestures.c | 6 +++--- src/gestures.h | 4 ++-- src/raylib.h | 4 ++-- 4 files changed, 9 insertions(+), 9 deletions(-) (limited to 'src') diff --git a/src/core.c b/src/core.c index 294936fb..7b1d5960 100644 --- a/src/core.c +++ b/src/core.c @@ -1193,7 +1193,7 @@ bool IsMouseButtonPressed(int button) bool pressed = false; #if defined(PLATFORM_ANDROID) - if (IsGestureDetected() && (GetGestureType() == GESTURE_TAP)) pressed = true; + if (IsGestureDetected(GESTURE_TAP)) pressed = true; #else if ((currentMouseState[button] != previousMouseState[button]) && (currentMouseState[button] == 1)) pressed = true; #endif @@ -1207,7 +1207,7 @@ bool IsMouseButtonDown(int button) bool down = false; #if defined(PLATFORM_ANDROID) - if (IsGestureDetected() && (GetGestureType() == GESTURE_HOLD)) down = true; + if (IsGestureDetected(GESTURE_HOLD)) down = true; #else if (GetMouseButtonStatus(button) == 1) down = true; #endif diff --git a/src/gestures.c b/src/gestures.c index b0a80084..d72aaf4e 100644 --- a/src/gestures.c +++ b/src/gestures.c @@ -292,14 +292,14 @@ void UpdateGestures(void) } // Check if a gesture have been detected -bool IsGestureDetected(void) +bool IsGestureDetected(int gesture) { - if ((enabledGestures & currentGesture) != GESTURE_NONE) return true; + if ((enabledGestures & currentGesture) == gesture) return true; else return false; } // Check gesture type -int GetGestureType(void) +int GetGestureDetected(void) { // Get current gesture only if enabled return (enabledGestures & currentGesture); diff --git a/src/gestures.h b/src/gestures.h index 5468eb54..f2bdaba4 100644 --- a/src/gestures.h +++ b/src/gestures.h @@ -92,8 +92,8 @@ extern "C" { // Prevents name mangling of functions //---------------------------------------------------------------------------------- void ProcessGestureEvent(GestureEvent event); // Process gesture event and translate it into gestures void UpdateGestures(void); // Update gestures detected (must be called every frame) -bool IsGestureDetected(void); // Check if a gesture have been detected -int GetGestureType(void); // Get latest detected gesture +bool IsGestureDetected(int gesture); // Check if a gesture have been detected +int GetGestureDetected(void); // Get latest detected gesture void SetGesturesEnabled(unsigned int gestureFlags); // Enable a set of gestures using flags int GetTouchPointsCount(void); // Get touch points count diff --git a/src/raylib.h b/src/raylib.h index 2fe29cc8..0c80b957 100644 --- a/src/raylib.h +++ b/src/raylib.h @@ -647,8 +647,8 @@ bool IsButtonReleased(int button); // Detect if an android //------------------------------------------------------------------------------------ void ProcessGestureEvent(GestureEvent event); // Process gesture event and translate it into gestures void UpdateGestures(void); // Update gestures detected (called automatically in PollInputEvents()) -bool IsGestureDetected(void); // Check if a gesture have been detected -int GetGestureType(void); // Get latest detected gesture +bool IsGestureDetected(int gesture); // Check if a gesture have been detected +int GetGestureDetected(void); // Get latest detected gesture void SetGesturesEnabled(unsigned int gestureFlags); // Enable a set of gestures using flags int GetTouchPointsCount(void); // Get touch points count -- cgit v1.2.3 From 9639b14e1bbfb9065d788dd6341527c93ad55e88 Mon Sep 17 00:00:00 2001 From: raysan5 Date: Sun, 17 Apr 2016 14:48:20 +0200 Subject: Reduce PCM buffer size for Android to avoid stalls --- src/audio.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/audio.c b/src/audio.c index 260f6778..75383385 100644 --- a/src/audio.c +++ b/src/audio.c @@ -57,8 +57,8 @@ //---------------------------------------------------------------------------------- #define MUSIC_STREAM_BUFFERS 2 -#if defined(PLATFORM_RPI) - // NOTE: On RPI should be lower to avoid frame-stalls +#if defined(PLATFORM_RPI) || defined(PLATFORM_ANDROID) + // NOTE: On RPI and Android should be lower to avoid frame-stalls #define MUSIC_BUFFER_SIZE 4096*2 // PCM data buffer (short) - 16Kb (RPI) #else // NOTE: On HTML5 (emscripten) this is allocated on heap, by default it's only 16MB!...just take care... -- cgit v1.2.3 From ec2cbaa5eb5c4bca059c5592932746369e1e293d Mon Sep 17 00:00:00 2001 From: Joshua Reisenauer Date: Sun, 24 Apr 2016 15:25:48 -0700 Subject: Added proto version of jar_xm This is an early draft, needs lots of work. Still need to figure out way to calculate total length of song. This is hard because xm tracks stream out zeros when done, only position in track can be found. Position does not give any direct value of how much more time is left. I think that by setting the loop count to 1 and seeking until the end I can total up the number of samples and come up with a length. --- src/audio.c | 81 +- src/jar_xm.h | 2647 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 2712 insertions(+), 16 deletions(-) create mode 100644 src/jar_xm.h (limited to 'src') diff --git a/src/audio.c b/src/audio.c index 75383385..9baa2222 100644 --- a/src/audio.c +++ b/src/audio.c @@ -42,6 +42,8 @@ #include // Required for strcmp() #include // Used for .WAV loading +#include "jar_xm.h" // For playing .xm files + #if defined(AUDIO_STANDALONE) #include // Used for functions with variable number of parameters (TraceLog()) #else @@ -73,6 +75,7 @@ // NOTE: Anything longer than ~10 seconds should be streamed... typedef struct Music { stb_vorbis *stream; + jar_xm_context_t *chipctx; // Stores jar_xm context ALuint buffers[MUSIC_STREAM_BUFFERS]; ALuint source; @@ -82,7 +85,7 @@ typedef struct Music { int sampleRate; int totalSamplesLeft; bool loop; - + bool chipTune; // True if chiptune is loaded } Music; #if defined(AUDIO_STANDALONE) @@ -564,6 +567,22 @@ void PlayMusicStream(char *fileName) currentMusic.totalSamplesLeft = stb_vorbis_stream_length_in_samples(currentMusic.stream) * currentMusic.channels; } } + else if (strcmp(GetExtension(fileName),"xm") == 0) + { + currentMusic.chipTune = true; + currentMusic.channels = 2; + currentMusic.sampleRate = 48000; + currentMusic.loop = true; + + // only stereo/float is supported for xm + if(info.channels == 2 && !jar_xm_create_context_from_file(¤tMusic.chipctx, currentMusic.sampleRate, fileName)) + { + currentMusic.format = AL_FORMAT_STEREO_FLOAT32; + jar_xm_set_max_loop_count(currentMusic.chipctx, 0); //infinite number of loops + //currentMusic.totalSamplesLeft = ; // Unsure of how to calculate this + musicEnabled = true; + } + } else TraceLog(WARNING, "[%s] Music extension not recognized, it can't be loaded", fileName); } @@ -572,14 +591,19 @@ void StopMusicStream(void) { if (musicEnabled) { - alSourceStop(currentMusic.source); - - EmptyMusicStream(); // Empty music buffers - - alDeleteSources(1, ¤tMusic.source); - alDeleteBuffers(2, currentMusic.buffers); - - stb_vorbis_close(currentMusic.stream); + alSourceStop(currentMusic.source); + EmptyMusicStream(); // Empty music buffers + alDeleteSources(1, ¤tMusic.source); + alDeleteBuffers(2, currentMusic.buffers); + + if (currentMusic.chipTune) + { + jar_xm_free_context(currentMusic.chipctx); + } + else + { + stb_vorbis_close(currentMusic.stream); + } } musicEnabled = false; @@ -633,7 +657,15 @@ void SetMusicVolume(float volume) // Get current music time length (in seconds) float GetMusicTimeLength(void) { - float totalSeconds = stb_vorbis_stream_length_in_seconds(currentMusic.stream); + float totalSeconds; + if (currentMusic.chipTune) + { + //totalSeconds = (float)samples; // Need to figure out how toget this + } + else + { + totalSeconds = stb_vorbis_stream_length_in_seconds(currentMusic.stream); + } return totalSeconds; } @@ -641,11 +673,20 @@ float GetMusicTimeLength(void) // Get current music time played (in seconds) float GetMusicTimePlayed(void) { - int totalSamples = stb_vorbis_stream_length_in_samples(currentMusic.stream) * currentMusic.channels; - - int samplesPlayed = totalSamples - currentMusic.totalSamplesLeft; - - float secondsPlayed = (float)samplesPlayed / (currentMusic.sampleRate * currentMusic.channels); + float secondsPlayed; + if (currentMusic.chipTune) + { + uint64_t* samples; + jar_xm_get_position(currentMusic.chipctx, NULL, NULL, NULL, samples); // Unsure if this is the desired value + secondsPlayed = (float)samples; + } + else + { + int totalSamples = stb_vorbis_stream_length_in_samples(currentMusic.stream) * currentMusic.channels; + int samplesPlayed = totalSamples - currentMusic.totalSamplesLeft; + secondsPlayed = (float)samplesPlayed / (currentMusic.sampleRate * currentMusic.channels); + } + return secondsPlayed; } @@ -668,7 +709,15 @@ static bool BufferMusicStream(ALuint buffer) { while (size < MUSIC_BUFFER_SIZE) { - streamedBytes = stb_vorbis_get_samples_short_interleaved(currentMusic.stream, currentMusic.channels, pcm + size, MUSIC_BUFFER_SIZE - size); + if (currentMusic.chipTune) + { + jar_xm_generate_samples(currentMusic.chipctx, pcm + size, (MUSIC_BUFFER_SIZE - size)/2); + streamedBytes = (MUSIC_BUFFER_SIZE - size)/2; // There is no end of stream for xmfiles, once the end is reached zeros are generated for non looped chiptunes. + } + else + { + streamedBytes = stb_vorbis_get_samples_short_interleaved(currentMusic.stream, currentMusic.channels, pcm + size, MUSIC_BUFFER_SIZE - size); + } if (streamedBytes > 0) size += (streamedBytes*currentMusic.channels); else break; diff --git a/src/jar_xm.h b/src/jar_xm.h new file mode 100644 index 00000000..2f102cf8 --- /dev/null +++ b/src/jar_xm.h @@ -0,0 +1,2647 @@ +// jar_xm.h - v0.01 - public domain - Joshua Reisenauer, MAR 2016 +// +// HISTORY: +// +// v0.01 2016-02-22 Setup +// +// +// USAGE: +// +// In ONE source file, put: +// +// #define JAR_XM_IMPLEMENTATION +// #include "jar_xm.h" +// +// Other source files should just include jar_xm.h +// +// SAMPLE CODE: +// +// jar_xm_context_t *musicptr; +// float musicBuffer[48000 / 60]; +// int intro_load(void) +// { +// jar_xm_create_context_from_file(&musicptr, 48000, "Song.XM"); +// return 1; +// } +// int intro_unload(void) +// { +// jar_xm_free_context(musicptr); +// return 1; +// } +// int intro_tick(long counter) +// { +// jar_xm_generate_samples(musicptr, musicBuffer, (48000 / 60) / 2); +// if(IsKeyDown(KEY_ENTER)) +// return 1; +// return 0; +// } +// +// +// LISCENSE - FOR LIBXM: +// +// Author: Romain "Artefact2" Dalmaso +// Contributor: Dan Spencer +// Repackaged into jar_xm.h By: Joshua Adam Reisenauer +// This program is free software. It comes without any warranty, to the +// extent permitted by applicable law. You can redistribute it and/or +// modify it under the terms of the Do What The Fuck You Want To Public +// License, Version 2, as published by Sam Hocevar. See +// http://sam.zoy.org/wtfpl/COPYING for more details. + +#ifndef INCLUDE_JAR_XM_H +#define INCLUDE_JAR_XM_H + +#define JAR_XM_DEBUG 0 +#define JAR_XM_LINEAR_INTERPOLATION 1 // speed increase with decrease in quality +#define JAR_XM_DEFENSIVE 1 +#define JAR_XM_RAMPING 1 + +#include +#include +#include +#include +#include +#include + +//------------------------------------------------------------------------------- +#ifdef __cplusplus +extern "C" { +#endif + +struct jar_xm_context_s; +typedef struct jar_xm_context_s jar_xm_context_t; + +/** Create a XM context. + * + * @param moddata the contents of the module + * @param rate play rate in Hz, recommended value of 48000 + * + * @returns 0 on success + * @returns 1 if module data is not sane + * @returns 2 if memory allocation failed + * @returns 3 unable to open input file + * @returns 4 fseek() failed + * @returns 5 fread() failed + * @returns 6 unkown error + * + * @deprecated This function is unsafe! + * @see jar_xm_create_context_safe() + */ +int jar_xm_create_context_from_file(jar_xm_context_t** ctx, uint32_t rate, const char* filename); + +/** Create a XM context. + * + * @param moddata the contents of the module + * @param rate play rate in Hz, recommended value of 48000 + * + * @returns 0 on success + * @returns 1 if module data is not sane + * @returns 2 if memory allocation failed + * + * @deprecated This function is unsafe! + * @see jar_xm_create_context_safe() + */ +int jar_xm_create_context(jar_xm_context_t**, const char* moddata, uint32_t rate); + +/** Create a XM context. + * + * @param moddata the contents of the module + * @param moddata_length the length of the contents of the module, in bytes + * @param rate play rate in Hz, recommended value of 48000 + * + * @returns 0 on success + * @returns 1 if module data is not sane + * @returns 2 if memory allocation failed + */ +int jar_xm_create_context_safe(jar_xm_context_t**, const char* moddata, size_t moddata_length, uint32_t rate); + +/** Free a XM context created by jar_xm_create_context(). */ +void jar_xm_free_context(jar_xm_context_t*); + +/** Play the module and put the sound samples in an output buffer. + * + * @param output buffer of 2*numsamples elements + * @param numsamples number of samples to generate + */ +void jar_xm_generate_samples(jar_xm_context_t*, float* output, size_t numsamples); + +/** Play the module, resample from 32 bit to 16 bit, and put the sound samples in an output buffer. + * + * @param output buffer of 2*numsamples elements + * @param numsamples number of samples to generate + */ +void jar_xm_generate_samples_16bit(jar_xm_context_t* ctx, short* output, size_t numsamples) +{ + float* musicBuffer = malloc((2*numsamples)*sizeof(float)); + short* musicBuffer2 = malloc((2*numsamples)*sizeof(short)); + + jar_xm_generate_samples(ctx, musicBuffer, numsamples); + + int x; + for(x=0;x<2*numsamples;x++) + musicBuffer2[x] = musicBuffer[x] * SHRT_MAX; + + memcpy(output, musicBuffer2, (2*numsamples)*sizeof(short)); + free(musicBuffer); + free(musicBuffer2); +} + +/** Play the module, resample from 32 bit to 8 bit, and put the sound samples in an output buffer. + * + * @param output buffer of 2*numsamples elements + * @param numsamples number of samples to generate + */ +void jar_xm_generate_samples_8bit(jar_xm_context_t* ctx, char* output, size_t numsamples) +{ + float* musicBuffer = malloc((2*numsamples)*sizeof(float)); + char* musicBuffer2 = malloc((2*numsamples)*sizeof(char)); + + jar_xm_generate_samples(ctx, musicBuffer, numsamples); + + int x; + for(x=0;x<2*numsamples;x++) + musicBuffer2[x] = musicBuffer[x] * CHAR_MAX; + + memcpy(output, musicBuffer2, (2*numsamples)*sizeof(char)); + free(musicBuffer); + free(musicBuffer2); +} + + + +/** Set the maximum number of times a module can loop. After the + * specified number of loops, calls to jar_xm_generate_samples will only + * generate silence. You can control the current number of loops with + * jar_xm_get_loop_count(). + * + * @param loopcnt maximum number of loops. Use 0 to loop + * indefinitely. */ +void jar_xm_set_max_loop_count(jar_xm_context_t*, uint8_t loopcnt); + +/** Get the loop count of the currently playing module. This value is + * 0 when the module is still playing, 1 when the module has looped + * once, etc. */ +uint8_t jar_xm_get_loop_count(jar_xm_context_t*); + + + +/** Mute or unmute a channel. + * + * @note Channel numbers go from 1 to jar_xm_get_number_of_channels(...). + * + * @return whether the channel was muted. + */ +bool jar_xm_mute_channel(jar_xm_context_t*, uint16_t, bool); + +/** Mute or unmute an instrument. + * + * @note Instrument numbers go from 1 to + * jar_xm_get_number_of_instruments(...). + * + * @return whether the instrument was muted. + */ +bool jar_xm_mute_instrument(jar_xm_context_t*, uint16_t, bool); + + + +/** Get the module name as a NUL-terminated string. */ +const char* jar_xm_get_module_name(jar_xm_context_t*); + +/** Get the tracker name as a NUL-terminated string. */ +const char* jar_xm_get_tracker_name(jar_xm_context_t*); + + + +/** Get the number of channels. */ +uint16_t jar_xm_get_number_of_channels(jar_xm_context_t*); + +/** Get the module length (in patterns). */ +uint16_t jar_xm_get_module_length(jar_xm_context_t*); + +/** Get the number of patterns. */ +uint16_t jar_xm_get_number_of_patterns(jar_xm_context_t*); + +/** Get the number of rows of a pattern. + * + * @note Pattern numbers go from 0 to + * jar_xm_get_number_of_patterns(...)-1. + */ +uint16_t jar_xm_get_number_of_rows(jar_xm_context_t*, uint16_t); + +/** Get the number of instruments. */ +uint16_t jar_xm_get_number_of_instruments(jar_xm_context_t*); + +/** Get the number of samples of an instrument. + * + * @note Instrument numbers go from 1 to + * jar_xm_get_number_of_instruments(...). + */ +uint16_t jar_xm_get_number_of_samples(jar_xm_context_t*, uint16_t); + + + +/** Get the current module speed. + * + * @param bpm will receive the current BPM + * @param tempo will receive the current tempo (ticks per line) + */ +void jar_xm_get_playing_speed(jar_xm_context_t*, uint16_t* bpm, uint16_t* tempo); + +/** Get the current position in the module being played. + * + * @param pattern_index if not NULL, will receive the current pattern + * index in the POT (pattern order table) + * + * @param pattern if not NULL, will receive the current pattern number + * + * @param row if not NULL, will receive the current row + * + * @param samples if not NULL, will receive the total number of + * generated samples (divide by sample rate to get seconds of + * generated audio) + */ +void jar_xm_get_position(jar_xm_context_t*, uint8_t* pattern_index, uint8_t* pattern, uint8_t* row, uint64_t* samples); + +/** Get the latest time (in number of generated samples) when a + * particular instrument was triggered in any channel. + * + * @note Instrument numbers go from 1 to + * jar_xm_get_number_of_instruments(...). + */ +uint64_t jar_xm_get_latest_trigger_of_instrument(jar_xm_context_t*, uint16_t); + +/** Get the latest time (in number of generated samples) when a + * particular sample was triggered in any channel. + * + * @note Instrument numbers go from 1 to + * jar_xm_get_number_of_instruments(...). + * + * @note Sample numbers go from 0 to + * jar_xm_get_nubmer_of_samples(...,instr)-1. + */ +uint64_t jar_xm_get_latest_trigger_of_sample(jar_xm_context_t*, uint16_t instr, uint16_t sample); + +/** Get the latest time (in number of generated samples) when any + * instrument was triggered in a given channel. + * + * @note Channel numbers go from 1 to jar_xm_get_number_of_channels(...). + */ +uint64_t jar_xm_get_latest_trigger_of_channel(jar_xm_context_t*, uint16_t); + +#ifdef __cplusplus +} +#endif +//------------------------------------------------------------------------------- + + + + + + +//Function Definitions----------------------------------------------------------- +#ifdef JAR_XM_IMPLEMENTATION + +#include +#include + +#if JAR_XM_DEBUG +#include +#define DEBUG(fmt, ...) do { \ + fprintf(stderr, "%s(): " fmt "\n", __func__, __VA_ARGS__); \ + fflush(stderr); \ + } while(0) +#else +#define DEBUG(...) +#endif + +#if jar_xm_BIG_ENDIAN +#error "Big endian platforms are not yet supported, sorry" +/* Make sure the compiler stops, even if #error is ignored */ +extern int __fail[-1]; +#endif + +/* ----- XM constants ----- */ + +#define SAMPLE_NAME_LENGTH 22 +#define INSTRUMENT_NAME_LENGTH 22 +#define MODULE_NAME_LENGTH 20 +#define TRACKER_NAME_LENGTH 20 +#define PATTERN_ORDER_TABLE_LENGTH 256 +#define NUM_NOTES 96 +#define NUM_ENVELOPE_POINTS 12 +#define MAX_NUM_ROWS 256 + +#if JAR_XM_RAMPING +#define jar_xm_SAMPLE_RAMPING_POINTS 0x20 +#endif + +/* ----- Data types ----- */ + +enum jar_xm_waveform_type_e { + jar_xm_SINE_WAVEFORM = 0, + jar_xm_RAMP_DOWN_WAVEFORM = 1, + jar_xm_SQUARE_WAVEFORM = 2, + jar_xm_RANDOM_WAVEFORM = 3, + jar_xm_RAMP_UP_WAVEFORM = 4, +}; +typedef enum jar_xm_waveform_type_e jar_xm_waveform_type_t; + +enum jar_xm_loop_type_e { + jar_xm_NO_LOOP, + jar_xm_FORWARD_LOOP, + jar_xm_PING_PONG_LOOP, +}; +typedef enum jar_xm_loop_type_e jar_xm_loop_type_t; + +enum jar_xm_frequency_type_e { + jar_xm_LINEAR_FREQUENCIES, + jar_xm_AMIGA_FREQUENCIES, +}; +typedef enum jar_xm_frequency_type_e jar_xm_frequency_type_t; + +struct jar_xm_envelope_point_s { + uint16_t frame; + uint16_t value; +}; +typedef struct jar_xm_envelope_point_s jar_xm_envelope_point_t; + +struct jar_xm_envelope_s { + jar_xm_envelope_point_t points[NUM_ENVELOPE_POINTS]; + uint8_t num_points; + uint8_t sustain_point; + uint8_t loop_start_point; + uint8_t loop_end_point; + bool enabled; + bool sustain_enabled; + bool loop_enabled; +}; +typedef struct jar_xm_envelope_s jar_xm_envelope_t; + +struct jar_xm_sample_s { + char name[SAMPLE_NAME_LENGTH + 1]; + int8_t bits; /* Either 8 or 16 */ + + uint32_t length; + uint32_t loop_start; + uint32_t loop_length; + uint32_t loop_end; + float volume; + int8_t finetune; + jar_xm_loop_type_t loop_type; + float panning; + int8_t relative_note; + uint64_t latest_trigger; + + float* data; + }; + typedef struct jar_xm_sample_s jar_xm_sample_t; + + struct jar_xm_instrument_s { + char name[INSTRUMENT_NAME_LENGTH + 1]; + uint16_t num_samples; + uint8_t sample_of_notes[NUM_NOTES]; + jar_xm_envelope_t volume_envelope; + jar_xm_envelope_t panning_envelope; + jar_xm_waveform_type_t vibrato_type; + uint8_t vibrato_sweep; + uint8_t vibrato_depth; + uint8_t vibrato_rate; + uint16_t volume_fadeout; + uint64_t latest_trigger; + bool muted; + + jar_xm_sample_t* samples; + }; + typedef struct jar_xm_instrument_s jar_xm_instrument_t; + + struct jar_xm_pattern_slot_s { + uint8_t note; /* 1-96, 97 = Key Off note */ + uint8_t instrument; /* 1-128 */ + uint8_t volume_column; + uint8_t effect_type; + uint8_t effect_param; + }; + typedef struct jar_xm_pattern_slot_s jar_xm_pattern_slot_t; + + struct jar_xm_pattern_s { + uint16_t num_rows; + jar_xm_pattern_slot_t* slots; /* Array of size num_rows * num_channels */ + }; + typedef struct jar_xm_pattern_s jar_xm_pattern_t; + + struct jar_xm_module_s { + char name[MODULE_NAME_LENGTH + 1]; + char trackername[TRACKER_NAME_LENGTH + 1]; + uint16_t length; + uint16_t restart_position; + uint16_t num_channels; + uint16_t num_patterns; + uint16_t num_instruments; + jar_xm_frequency_type_t frequency_type; + uint8_t pattern_table[PATTERN_ORDER_TABLE_LENGTH]; + + jar_xm_pattern_t* patterns; + jar_xm_instrument_t* instruments; /* Instrument 1 has index 0, + * instrument 2 has index 1, etc. */ + }; + typedef struct jar_xm_module_s jar_xm_module_t; + + struct jar_xm_channel_context_s { + float note; + float orig_note; /* The original note before effect modifications, as read in the pattern. */ + jar_xm_instrument_t* instrument; /* Could be NULL */ + jar_xm_sample_t* sample; /* Could be NULL */ + jar_xm_pattern_slot_t* current; + + float sample_position; + float period; + float frequency; + float step; + bool ping; /* For ping-pong samples: true is -->, false is <-- */ + + float volume; /* Ideally between 0 (muted) and 1 (loudest) */ + float panning; /* Between 0 (left) and 1 (right); 0.5 is centered */ + + uint16_t autovibrato_ticks; + + bool sustained; + float fadeout_volume; + float volume_envelope_volume; + float panning_envelope_panning; + uint16_t volume_envelope_frame_count; + uint16_t panning_envelope_frame_count; + + float autovibrato_note_offset; + + bool arp_in_progress; + uint8_t arp_note_offset; + uint8_t volume_slide_param; + uint8_t fine_volume_slide_param; + uint8_t global_volume_slide_param; + uint8_t panning_slide_param; + uint8_t portamento_up_param; + uint8_t portamento_down_param; + uint8_t fine_portamento_up_param; + uint8_t fine_portamento_down_param; + uint8_t extra_fine_portamento_up_param; + uint8_t extra_fine_portamento_down_param; + uint8_t tone_portamento_param; + float tone_portamento_target_period; + uint8_t multi_retrig_param; + uint8_t note_delay_param; + uint8_t pattern_loop_origin; /* Where to restart a E6y loop */ + uint8_t pattern_loop_count; /* How many loop passes have been done */ + bool vibrato_in_progress; + jar_xm_waveform_type_t vibrato_waveform; + bool vibrato_waveform_retrigger; /* True if a new note retriggers the waveform */ + uint8_t vibrato_param; + uint16_t vibrato_ticks; /* Position in the waveform */ + float vibrato_note_offset; + jar_xm_waveform_type_t tremolo_waveform; + bool tremolo_waveform_retrigger; + uint8_t tremolo_param; + uint8_t tremolo_ticks; + float tremolo_volume; + uint8_t tremor_param; + bool tremor_on; + + uint64_t latest_trigger; + bool muted; + +#if JAR_XM_RAMPING + /* These values are updated at the end of each tick, to save + * a couple of float operations on every generated sample. */ + float target_panning; + float target_volume; + + unsigned long frame_count; + float end_of_previous_sample[jar_xm_SAMPLE_RAMPING_POINTS]; +#endif + + float actual_panning; + float actual_volume; + }; + typedef struct jar_xm_channel_context_s jar_xm_channel_context_t; + + struct jar_xm_context_s { + void* allocated_memory; + jar_xm_module_t module; + uint32_t rate; + + uint16_t tempo; + uint16_t bpm; + float global_volume; + float amplification; + +#if JAR_XM_RAMPING + /* How much is a channel final volume allowed to change per + * sample; this is used to avoid abrubt volume changes which + * manifest as "clicks" in the generated sound. */ + float volume_ramp; + float panning_ramp; /* Same for panning. */ +#endif + + uint8_t current_table_index; + uint8_t current_row; + uint16_t current_tick; /* Can go below 255, with high tempo and a pattern delay */ + float remaining_samples_in_tick; + uint64_t generated_samples; + + bool position_jump; + bool pattern_break; + uint8_t jump_dest; + uint8_t jump_row; + + /* Extra ticks to be played before going to the next row - + * Used for EEy effect */ + uint16_t extra_ticks; + + uint8_t* row_loop_count; /* Array of size MAX_NUM_ROWS * module_length */ + uint8_t loop_count; + uint8_t max_loop_count; + + jar_xm_channel_context_t* channels; +}; + +/* ----- Internal API ----- */ + +#if JAR_XM_DEFENSIVE + +/** Check the module data for errors/inconsistencies. + * + * @returns 0 if everything looks OK. Module should be safe to load. + */ +int jar_xm_check_sanity_preload(const char*, size_t); + +/** Check a loaded module for errors/inconsistencies. + * + * @returns 0 if everything looks OK. + */ +int jar_xm_check_sanity_postload(jar_xm_context_t*); + +#endif + +/** Get the number of bytes needed to store the module data in a + * dynamically allocated blank context. + * + * Things that are dynamically allocated: + * - sample data + * - sample structures in instruments + * - pattern data + * - row loop count arrays + * - pattern structures in module + * - instrument structures in module + * - channel contexts + * - context structure itself + + * @returns 0 if everything looks OK. + */ +size_t jar_xm_get_memory_needed_for_context(const char*, size_t); + +/** Populate the context from module data. + * + * @returns pointer to the memory pool + */ +char* jar_xm_load_module(jar_xm_context_t*, const char*, size_t, char*); + +int jar_xm_create_context(jar_xm_context_t** ctxp, const char* moddata, uint32_t rate) { + return jar_xm_create_context_safe(ctxp, moddata, SIZE_MAX, rate); +} + +int jar_xm_create_context_safe(jar_xm_context_t** ctxp, const char* moddata, size_t moddata_length, uint32_t rate) { +#if JAR_XM_DEFENSIVE + int ret; +#endif + size_t bytes_needed; + char* mempool; + jar_xm_context_t* ctx; + +#if JAR_XM_DEFENSIVE + if((ret = jar_xm_check_sanity_preload(moddata, moddata_length))) { + DEBUG("jar_xm_check_sanity_preload() returned %i, module is not safe to load", ret); + return 1; + } +#endif + + bytes_needed = jar_xm_get_memory_needed_for_context(moddata, moddata_length); + mempool = malloc(bytes_needed); + if(mempool == NULL && bytes_needed > 0) { + /* malloc() failed, trouble ahead */ + DEBUG("call to malloc() failed, returned %p", (void*)mempool); + return 2; + } + + /* Initialize most of the fields to 0, 0.f, NULL or false depending on type */ + memset(mempool, 0, bytes_needed); + + ctx = (*ctxp = (jar_xm_context_t*)mempool); + ctx->allocated_memory = mempool; /* Keep original pointer for free() */ + mempool += sizeof(jar_xm_context_t); + + ctx->rate = rate; + mempool = jar_xm_load_module(ctx, moddata, moddata_length, mempool); + + ctx->channels = (jar_xm_channel_context_t*)mempool; + mempool += ctx->module.num_channels * sizeof(jar_xm_channel_context_t); + + ctx->global_volume = 1.f; + ctx->amplification = .25f; /* XXX: some bad modules may still clip. Find out something better. */ + +#if JAR_XM_RAMPING + ctx->volume_ramp = (1.f / 128.f); + ctx->panning_ramp = (1.f / 128.f); +#endif + + for(uint8_t i = 0; i < ctx->module.num_channels; ++i) { + jar_xm_channel_context_t* ch = ctx->channels + i; + + ch->ping = true; + ch->vibrato_waveform = jar_xm_SINE_WAVEFORM; + ch->vibrato_waveform_retrigger = true; + ch->tremolo_waveform = jar_xm_SINE_WAVEFORM; + ch->tremolo_waveform_retrigger = true; + + ch->volume = ch->volume_envelope_volume = ch->fadeout_volume = 1.0f; + ch->panning = ch->panning_envelope_panning = .5f; + ch->actual_volume = .0f; + ch->actual_panning = .5f; + } + + ctx->row_loop_count = (uint8_t*)mempool; + mempool += MAX_NUM_ROWS * sizeof(uint8_t); + +#if JAR_XM_DEFENSIVE + if((ret = jar_xm_check_sanity_postload(ctx))) { + DEBUG("jar_xm_check_sanity_postload() returned %i, module is not safe to play", ret); + jar_xm_free_context(ctx); + return 1; + } +#endif + + return 0; +} + +void jar_xm_free_context(jar_xm_context_t* context) { + free(context->allocated_memory); +} + +void jar_xm_set_max_loop_count(jar_xm_context_t* context, uint8_t loopcnt) { + context->max_loop_count = loopcnt; +} + +uint8_t jar_xm_get_loop_count(jar_xm_context_t* context) { + return context->loop_count; +} + + + +bool jar_xm_mute_channel(jar_xm_context_t* ctx, uint16_t channel, bool mute) { + bool old = ctx->channels[channel - 1].muted; + ctx->channels[channel - 1].muted = mute; + return old; +} + +bool jar_xm_mute_instrument(jar_xm_context_t* ctx, uint16_t instr, bool mute) { + bool old = ctx->module.instruments[instr - 1].muted; + ctx->module.instruments[instr - 1].muted = mute; + return old; +} + + + +const char* jar_xm_get_module_name(jar_xm_context_t* ctx) { + return ctx->module.name; +} + +const char* jar_xm_get_tracker_name(jar_xm_context_t* ctx) { + return ctx->module.trackername; +} + + + +uint16_t jar_xm_get_number_of_channels(jar_xm_context_t* ctx) { + return ctx->module.num_channels; +} + +uint16_t jar_xm_get_module_length(jar_xm_context_t* ctx) { + return ctx->module.length; +} + +uint16_t jar_xm_get_number_of_patterns(jar_xm_context_t* ctx) { + return ctx->module.num_patterns; +} + +uint16_t jar_xm_get_number_of_rows(jar_xm_context_t* ctx, uint16_t pattern) { + return ctx->module.patterns[pattern].num_rows; +} + +uint16_t jar_xm_get_number_of_instruments(jar_xm_context_t* ctx) { + return ctx->module.num_instruments; +} + +uint16_t jar_xm_get_number_of_samples(jar_xm_context_t* ctx, uint16_t instrument) { + return ctx->module.instruments[instrument - 1].num_samples; +} + + + +void jar_xm_get_playing_speed(jar_xm_context_t* ctx, uint16_t* bpm, uint16_t* tempo) { + if(bpm) *bpm = ctx->bpm; + if(tempo) *tempo = ctx->tempo; +} + +void jar_xm_get_position(jar_xm_context_t* ctx, uint8_t* pattern_index, uint8_t* pattern, uint8_t* row, uint64_t* samples) { + if(pattern_index) *pattern_index = ctx->current_table_index; + if(pattern) *pattern = ctx->module.pattern_table[ctx->current_table_index]; + if(row) *row = ctx->current_row; + if(samples) *samples = ctx->generated_samples; +} + +uint64_t jar_xm_get_latest_trigger_of_instrument(jar_xm_context_t* ctx, uint16_t instr) { + return ctx->module.instruments[instr - 1].latest_trigger; +} + +uint64_t jar_xm_get_latest_trigger_of_sample(jar_xm_context_t* ctx, uint16_t instr, uint16_t sample) { + return ctx->module.instruments[instr - 1].samples[sample].latest_trigger; +} + +uint64_t jar_xm_get_latest_trigger_of_channel(jar_xm_context_t* ctx, uint16_t chn) { + return ctx->channels[chn - 1].latest_trigger; +} + +/* .xm files are little-endian. (XXX: Are they really?) */ + +/* Bounded reader macros. + * If we attempt to read the buffer out-of-bounds, pretend that the buffer is + * infinitely padded with zeroes. + */ +#define READ_U8(offset) (((offset) < moddata_length) ? (*(uint8_t*)(moddata + (offset))) : 0) +#define READ_U16(offset) ((uint16_t)READ_U8(offset) | ((uint16_t)READ_U8((offset) + 1) << 8)) +#define READ_U32(offset) ((uint32_t)READ_U16(offset) | ((uint32_t)READ_U16((offset) + 2) << 16)) +#define READ_MEMCPY(ptr, offset, length) memcpy_pad(ptr, length, moddata, moddata_length, offset) + +static inline void memcpy_pad(void* dst, size_t dst_len, const void* src, size_t src_len, size_t offset) { + uint8_t* dst_c = dst; + const uint8_t* src_c = src; + + /* how many bytes can be copied without overrunning `src` */ + size_t copy_bytes = (src_len >= offset) ? (src_len - offset) : 0; + copy_bytes = copy_bytes > dst_len ? dst_len : copy_bytes; + + memcpy(dst_c, src_c + offset, copy_bytes); + /* padded bytes */ + memset(dst_c + copy_bytes, 0, dst_len - copy_bytes); +} + +#if JAR_XM_DEFENSIVE + +int jar_xm_check_sanity_preload(const char* module, size_t module_length) { + if(module_length < 60) { + return 4; + } + + if(memcmp("Extended Module: ", module, 17) != 0) { + return 1; + } + + if(module[37] != 0x1A) { + return 2; + } + + if(module[59] != 0x01 || module[58] != 0x04) { + /* Not XM 1.04 */ + return 3; + } + + return 0; +} + +int jar_xm_check_sanity_postload(jar_xm_context_t* ctx) { + /* @todo: plenty of stuff to do here… */ + + /* Check the POT */ + for(uint8_t i = 0; i < ctx->module.length; ++i) { + if(ctx->module.pattern_table[i] >= ctx->module.num_patterns) { + if(i+1 == ctx->module.length && ctx->module.length > 1) { + /* Cheap fix */ + --ctx->module.length; + DEBUG("trimming invalid POT at pos %X", i); + } else { + DEBUG("module has invalid POT, pos %X references nonexistent pattern %X", + i, + ctx->module.pattern_table[i]); + return 1; + } + } + } + + return 0; +} + +#endif + +size_t jar_xm_get_memory_needed_for_context(const char* moddata, size_t moddata_length) { + size_t memory_needed = 0; + size_t offset = 60; /* Skip the first header */ + uint16_t num_channels; + uint16_t num_patterns; + uint16_t num_instruments; + + /* Read the module header */ + + num_channels = READ_U16(offset + 8); + num_channels = READ_U16(offset + 8); + + num_patterns = READ_U16(offset + 10); + memory_needed += num_patterns * sizeof(jar_xm_pattern_t); + + num_instruments = READ_U16(offset + 12); + memory_needed += num_instruments * sizeof(jar_xm_instrument_t); + + memory_needed += MAX_NUM_ROWS * READ_U16(offset + 4) * sizeof(uint8_t); /* Module length */ + + /* Header size */ + offset += READ_U32(offset); + + /* Read pattern headers */ + for(uint16_t i = 0; i < num_patterns; ++i) { + uint16_t num_rows; + + num_rows = READ_U16(offset + 5); + memory_needed += num_rows * num_channels * sizeof(jar_xm_pattern_slot_t); + + /* Pattern header length + packed pattern data size */ + offset += READ_U32(offset) + READ_U16(offset + 7); + } + + /* Read instrument headers */ + for(uint16_t i = 0; i < num_instruments; ++i) { + uint16_t num_samples; + uint32_t sample_header_size = 0; + uint32_t sample_size_aggregate = 0; + + num_samples = READ_U16(offset + 27); + memory_needed += num_samples * sizeof(jar_xm_sample_t); + + if(num_samples > 0) { + sample_header_size = READ_U32(offset + 29); + } + + /* Instrument header size */ + offset += READ_U32(offset); + + for(uint16_t j = 0; j < num_samples; ++j) { + uint32_t sample_size; + uint8_t flags; + + sample_size = READ_U32(offset); + flags = READ_U8(offset + 14); + sample_size_aggregate += sample_size; + + if(flags & (1 << 4)) { + /* 16 bit sample */ + memory_needed += sample_size * (sizeof(float) >> 1); + } else { + /* 8 bit sample */ + memory_needed += sample_size * sizeof(float); + } + + offset += sample_header_size; + } + + offset += sample_size_aggregate; + } + + memory_needed += num_channels * sizeof(jar_xm_channel_context_t); + memory_needed += sizeof(jar_xm_context_t); + + return memory_needed; +} + +char* jar_xm_load_module(jar_xm_context_t* ctx, const char* moddata, size_t moddata_length, char* mempool) { + size_t offset = 0; + jar_xm_module_t* mod = &(ctx->module); + + /* Read XM header */ + READ_MEMCPY(mod->name, offset + 17, MODULE_NAME_LENGTH); + READ_MEMCPY(mod->trackername, offset + 38, TRACKER_NAME_LENGTH); + offset += 60; + + /* Read module header */ + uint32_t header_size = READ_U32(offset); + + mod->length = READ_U16(offset + 4); + mod->restart_position = READ_U16(offset + 6); + mod->num_channels = READ_U16(offset + 8); + mod->num_patterns = READ_U16(offset + 10); + mod->num_instruments = READ_U16(offset + 12); + + mod->patterns = (jar_xm_pattern_t*)mempool; + mempool += mod->num_patterns * sizeof(jar_xm_pattern_t); + + mod->instruments = (jar_xm_instrument_t*)mempool; + mempool += mod->num_instruments * sizeof(jar_xm_instrument_t); + + uint16_t flags = READ_U32(offset + 14); + mod->frequency_type = (flags & (1 << 0)) ? jar_xm_LINEAR_FREQUENCIES : jar_xm_AMIGA_FREQUENCIES; + + ctx->tempo = READ_U16(offset + 16); + ctx->bpm = READ_U16(offset + 18); + + READ_MEMCPY(mod->pattern_table, offset + 20, PATTERN_ORDER_TABLE_LENGTH); + offset += header_size; + + /* Read patterns */ + for(uint16_t i = 0; i < mod->num_patterns; ++i) { + uint16_t packed_patterndata_size = READ_U16(offset + 7); + jar_xm_pattern_t* pat = mod->patterns + i; + + pat->num_rows = READ_U16(offset + 5); + + pat->slots = (jar_xm_pattern_slot_t*)mempool; + mempool += mod->num_channels * pat->num_rows * sizeof(jar_xm_pattern_slot_t); + + /* Pattern header length */ + offset += READ_U32(offset); + + if(packed_patterndata_size == 0) { + /* No pattern data is present */ + memset(pat->slots, 0, sizeof(jar_xm_pattern_slot_t) * pat->num_rows * mod->num_channels); + } else { + /* This isn't your typical for loop */ + for(uint16_t j = 0, k = 0; j < packed_patterndata_size; ++k) { + uint8_t note = READ_U8(offset + j); + jar_xm_pattern_slot_t* slot = pat->slots + k; + + if(note & (1 << 7)) { + /* MSB is set, this is a compressed packet */ + ++j; + + if(note & (1 << 0)) { + /* Note follows */ + slot->note = READ_U8(offset + j); + ++j; + } else { + slot->note = 0; + } + + if(note & (1 << 1)) { + /* Instrument follows */ + slot->instrument = READ_U8(offset + j); + ++j; + } else { + slot->instrument = 0; + } + + if(note & (1 << 2)) { + /* Volume column follows */ + slot->volume_column = READ_U8(offset + j); + ++j; + } else { + slot->volume_column = 0; + } + + if(note & (1 << 3)) { + /* Effect follows */ + slot->effect_type = READ_U8(offset + j); + ++j; + } else { + slot->effect_type = 0; + } + + if(note & (1 << 4)) { + /* Effect parameter follows */ + slot->effect_param = READ_U8(offset + j); + ++j; + } else { + slot->effect_param = 0; + } + } else { + /* Uncompressed packet */ + slot->note = note; + slot->instrument = READ_U8(offset + j + 1); + slot->volume_column = READ_U8(offset + j + 2); + slot->effect_type = READ_U8(offset + j + 3); + slot->effect_param = READ_U8(offset + j + 4); + j += 5; + } + } + } + + offset += packed_patterndata_size; + } + + /* Read instruments */ + for(uint16_t i = 0; i < ctx->module.num_instruments; ++i) { + uint32_t sample_header_size = 0; + jar_xm_instrument_t* instr = mod->instruments + i; + + READ_MEMCPY(instr->name, offset + 4, INSTRUMENT_NAME_LENGTH); + instr->num_samples = READ_U16(offset + 27); + + if(instr->num_samples > 0) { + /* Read extra header properties */ + sample_header_size = READ_U32(offset + 29); + READ_MEMCPY(instr->sample_of_notes, offset + 33, NUM_NOTES); + + instr->volume_envelope.num_points = READ_U8(offset + 225); + instr->panning_envelope.num_points = READ_U8(offset + 226); + + for(uint8_t j = 0; j < instr->volume_envelope.num_points; ++j) { + instr->volume_envelope.points[j].frame = READ_U16(offset + 129 + 4 * j); + instr->volume_envelope.points[j].value = READ_U16(offset + 129 + 4 * j + 2); + } + + for(uint8_t j = 0; j < instr->panning_envelope.num_points; ++j) { + instr->panning_envelope.points[j].frame = READ_U16(offset + 177 + 4 * j); + instr->panning_envelope.points[j].value = READ_U16(offset + 177 + 4 * j + 2); + } + + instr->volume_envelope.sustain_point = READ_U8(offset + 227); + instr->volume_envelope.loop_start_point = READ_U8(offset + 228); + instr->volume_envelope.loop_end_point = READ_U8(offset + 229); + + instr->panning_envelope.sustain_point = READ_U8(offset + 230); + instr->panning_envelope.loop_start_point = READ_U8(offset + 231); + instr->panning_envelope.loop_end_point = READ_U8(offset + 232); + + uint8_t flags = READ_U8(offset + 233); + instr->volume_envelope.enabled = flags & (1 << 0); + instr->volume_envelope.sustain_enabled = flags & (1 << 1); + instr->volume_envelope.loop_enabled = flags & (1 << 2); + + flags = READ_U8(offset + 234); + instr->panning_envelope.enabled = flags & (1 << 0); + instr->panning_envelope.sustain_enabled = flags & (1 << 1); + instr->panning_envelope.loop_enabled = flags & (1 << 2); + + instr->vibrato_type = READ_U8(offset + 235); + if(instr->vibrato_type == 2) { + instr->vibrato_type = 1; + } else if(instr->vibrato_type == 1) { + instr->vibrato_type = 2; + } + instr->vibrato_sweep = READ_U8(offset + 236); + instr->vibrato_depth = READ_U8(offset + 237); + instr->vibrato_rate = READ_U8(offset + 238); + instr->volume_fadeout = READ_U16(offset + 239); + + instr->samples = (jar_xm_sample_t*)mempool; + mempool += instr->num_samples * sizeof(jar_xm_sample_t); + } else { + instr->samples = NULL; + } + + /* Instrument header size */ + offset += READ_U32(offset); + + for(uint16_t j = 0; j < instr->num_samples; ++j) { + /* Read sample header */ + jar_xm_sample_t* sample = instr->samples + j; + + sample->length = READ_U32(offset); + sample->loop_start = READ_U32(offset + 4); + sample->loop_length = READ_U32(offset + 8); + sample->loop_end = sample->loop_start + sample->loop_length; + sample->volume = (float)READ_U8(offset + 12) / (float)0x40; + sample->finetune = (int8_t)READ_U8(offset + 13); + + uint8_t flags = READ_U8(offset + 14); + if((flags & 3) == 0) { + sample->loop_type = jar_xm_NO_LOOP; + } else if((flags & 3) == 1) { + sample->loop_type = jar_xm_FORWARD_LOOP; + } else { + sample->loop_type = jar_xm_PING_PONG_LOOP; + } + + sample->bits = (flags & (1 << 4)) ? 16 : 8; + + sample->panning = (float)READ_U8(offset + 15) / (float)0xFF; + sample->relative_note = (int8_t)READ_U8(offset + 16); + READ_MEMCPY(sample->name, 18, SAMPLE_NAME_LENGTH); + sample->data = (float*)mempool; + + if(sample->bits == 16) { + /* 16 bit sample */ + mempool += sample->length * (sizeof(float) >> 1); + sample->loop_start >>= 1; + sample->loop_length >>= 1; + sample->loop_end >>= 1; + sample->length >>= 1; + } else { + /* 8 bit sample */ + mempool += sample->length * sizeof(float); + } + + offset += sample_header_size; + } + + for(uint16_t j = 0; j < instr->num_samples; ++j) { + /* Read sample data */ + jar_xm_sample_t* sample = instr->samples + j; + uint32_t length = sample->length; + + if(sample->bits == 16) { + int16_t v = 0; + for(uint32_t k = 0; k < length; ++k) { + v = v + (int16_t)READ_U16(offset + (k << 1)); + sample->data[k] = (float)v / (float)(1 << 15); + } + offset += sample->length << 1; + } else { + int8_t v = 0; + for(uint32_t k = 0; k < length; ++k) { + v = v + (int8_t)READ_U8(offset + k); + sample->data[k] = (float)v / (float)(1 << 7); + } + offset += sample->length; + } + } + } + + return mempool; +} + +//------------------------------------------------------------------------------- +//THE FOLLOWING IS FOR PLAYING +//------------------------------------------------------------------------------- + +/* ----- Static functions ----- */ + +static float jar_xm_waveform(jar_xm_waveform_type_t, uint8_t); +static void jar_xm_autovibrato(jar_xm_context_t*, jar_xm_channel_context_t*); +static void jar_xm_vibrato(jar_xm_context_t*, jar_xm_channel_context_t*, uint8_t, uint16_t); +static void jar_xm_tremolo(jar_xm_context_t*, jar_xm_channel_context_t*, uint8_t, uint16_t); +static void jar_xm_arpeggio(jar_xm_context_t*, jar_xm_channel_context_t*, uint8_t, uint16_t); +static void jar_xm_tone_portamento(jar_xm_context_t*, jar_xm_channel_context_t*); +static void jar_xm_pitch_slide(jar_xm_context_t*, jar_xm_channel_context_t*, float); +static void jar_xm_panning_slide(jar_xm_channel_context_t*, uint8_t); +static void jar_xm_volume_slide(jar_xm_channel_context_t*, uint8_t); + +static float jar_xm_envelope_lerp(jar_xm_envelope_point_t*, jar_xm_envelope_point_t*, uint16_t); +static void jar_xm_envelope_tick(jar_xm_channel_context_t*, jar_xm_envelope_t*, uint16_t*, float*); +static void jar_xm_envelopes(jar_xm_channel_context_t*); + +static float jar_xm_linear_period(float); +static float jar_xm_linear_frequency(float); +static float jar_xm_amiga_period(float); +static float jar_xm_amiga_frequency(float); +static float jar_xm_period(jar_xm_context_t*, float); +static float jar_xm_frequency(jar_xm_context_t*, float, float); +static void jar_xm_update_frequency(jar_xm_context_t*, jar_xm_channel_context_t*); + +static void jar_xm_handle_note_and_instrument(jar_xm_context_t*, jar_xm_channel_context_t*, jar_xm_pattern_slot_t*); +static void jar_xm_trigger_note(jar_xm_context_t*, jar_xm_channel_context_t*, unsigned int flags); +static void jar_xm_cut_note(jar_xm_channel_context_t*); +static void jar_xm_key_off(jar_xm_channel_context_t*); + +static void jar_xm_post_pattern_change(jar_xm_context_t*); +static void jar_xm_row(jar_xm_context_t*); +static void jar_xm_tick(jar_xm_context_t*); + +static float jar_xm_next_of_sample(jar_xm_channel_context_t*); +static void jar_xm_sample(jar_xm_context_t*, float*, float*); + +/* ----- Other oddities ----- */ + +#define jar_xm_TRIGGER_KEEP_VOLUME (1 << 0) +#define jar_xm_TRIGGER_KEEP_PERIOD (1 << 1) +#define jar_xm_TRIGGER_KEEP_SAMPLE_POSITION (1 << 2) + +static const uint16_t amiga_frequencies[] = { + 1712, 1616, 1525, 1440, /* C-2, C#2, D-2, D#2 */ + 1357, 1281, 1209, 1141, /* E-2, F-2, F#2, G-2 */ + 1077, 1017, 961, 907, /* G#2, A-2, A#2, B-2 */ + 856, /* C-3 */ +}; + +static const float multi_retrig_add[] = { + 0.f, -1.f, -2.f, -4.f, /* 0, 1, 2, 3 */ + -8.f, -16.f, 0.f, 0.f, /* 4, 5, 6, 7 */ + 0.f, 1.f, 2.f, 4.f, /* 8, 9, A, B */ + 8.f, 16.f, 0.f, 0.f /* C, D, E, F */ +}; + +static const float multi_retrig_multiply[] = { + 1.f, 1.f, 1.f, 1.f, /* 0, 1, 2, 3 */ + 1.f, 1.f, .6666667f, .5f, /* 4, 5, 6, 7 */ + 1.f, 1.f, 1.f, 1.f, /* 8, 9, A, B */ + 1.f, 1.f, 1.5f, 2.f /* C, D, E, F */ +}; + +#define jar_xm_CLAMP_UP1F(vol, limit) do { \ + if((vol) > (limit)) (vol) = (limit); \ + } while(0) +#define jar_xm_CLAMP_UP(vol) jar_xm_CLAMP_UP1F((vol), 1.f) + +#define jar_xm_CLAMP_DOWN1F(vol, limit) do { \ + if((vol) < (limit)) (vol) = (limit); \ + } while(0) +#define jar_xm_CLAMP_DOWN(vol) jar_xm_CLAMP_DOWN1F((vol), .0f) + +#define jar_xm_CLAMP2F(vol, up, down) do { \ + if((vol) > (up)) (vol) = (up); \ + else if((vol) < (down)) (vol) = (down); \ + } while(0) +#define jar_xm_CLAMP(vol) jar_xm_CLAMP2F((vol), 1.f, .0f) + +#define jar_xm_SLIDE_TOWARDS(val, goal, incr) do { \ + if((val) > (goal)) { \ + (val) -= (incr); \ + jar_xm_CLAMP_DOWN1F((val), (goal)); \ + } else if((val) < (goal)) { \ + (val) += (incr); \ + jar_xm_CLAMP_UP1F((val), (goal)); \ + } \ + } while(0) + +#define jar_xm_LERP(u, v, t) ((u) + (t) * ((v) - (u))) +#define jar_xm_INVERSE_LERP(u, v, lerp) (((lerp) - (u)) / ((v) - (u))) + +#define HAS_TONE_PORTAMENTO(s) ((s)->effect_type == 3 \ + || (s)->effect_type == 5 \ + || ((s)->volume_column >> 4) == 0xF) +#define HAS_ARPEGGIO(s) ((s)->effect_type == 0 \ + && (s)->effect_param != 0) +#define HAS_VIBRATO(s) ((s)->effect_type == 4 \ + || (s)->effect_param == 6 \ + || ((s)->volume_column >> 4) == 0xB) +#define NOTE_IS_VALID(n) ((n) > 0 && (n) < 97) + +/* ----- Function definitions ----- */ + +static float jar_xm_waveform(jar_xm_waveform_type_t waveform, uint8_t step) { + static unsigned int next_rand = 24492; + step %= 0x40; + + switch(waveform) { + + case jar_xm_SINE_WAVEFORM: + /* Why not use a table? For saving space, and because there's + * very very little actual performance gain. */ + return -sinf(2.f * 3.141592f * (float)step / (float)0x40); + + case jar_xm_RAMP_DOWN_WAVEFORM: + /* Ramp down: 1.0f when step = 0; -1.0f when step = 0x40 */ + return (float)(0x20 - step) / 0x20; + + case jar_xm_SQUARE_WAVEFORM: + /* Square with a 50% duty */ + return (step >= 0x20) ? 1.f : -1.f; + + case jar_xm_RANDOM_WAVEFORM: + /* Use the POSIX.1-2001 example, just to be deterministic + * across different machines */ + next_rand = next_rand * 1103515245 + 12345; + return (float)((next_rand >> 16) & 0x7FFF) / (float)0x4000 - 1.f; + + case jar_xm_RAMP_UP_WAVEFORM: + /* Ramp up: -1.f when step = 0; 1.f when step = 0x40 */ + return (float)(step - 0x20) / 0x20; + + default: + break; + + } + + return .0f; +} + +static void jar_xm_autovibrato(jar_xm_context_t* ctx, jar_xm_channel_context_t* ch) { + if(ch->instrument == NULL || ch->instrument->vibrato_depth == 0) return; + jar_xm_instrument_t* instr = ch->instrument; + float sweep = 1.f; + + if(ch->autovibrato_ticks < instr->vibrato_sweep) { + /* No idea if this is correct, but it sounds close enough… */ + sweep = jar_xm_LERP(0.f, 1.f, (float)ch->autovibrato_ticks / (float)instr->vibrato_sweep); + } + + unsigned int step = ((ch->autovibrato_ticks++) * instr->vibrato_rate) >> 2; + ch->autovibrato_note_offset = .25f * jar_xm_waveform(instr->vibrato_type, step) + * (float)instr->vibrato_depth / (float)0xF * sweep; + jar_xm_update_frequency(ctx, ch); +} + +static void jar_xm_vibrato(jar_xm_context_t* ctx, jar_xm_channel_context_t* ch, uint8_t param, uint16_t pos) { + unsigned int step = pos * (param >> 4); + ch->vibrato_note_offset = + 2.f + * jar_xm_waveform(ch->vibrato_waveform, step) + * (float)(param & 0x0F) / (float)0xF; + jar_xm_update_frequency(ctx, ch); +} + +static void jar_xm_tremolo(jar_xm_context_t* ctx, jar_xm_channel_context_t* ch, uint8_t param, uint16_t pos) { + unsigned int step = pos * (param >> 4); + /* Not so sure about this, it sounds correct by ear compared with + * MilkyTracker, but it could come from other bugs */ + ch->tremolo_volume = -1.f * jar_xm_waveform(ch->tremolo_waveform, step) + * (float)(param & 0x0F) / (float)0xF; +} + +static void jar_xm_arpeggio(jar_xm_context_t* ctx, jar_xm_channel_context_t* ch, uint8_t param, uint16_t tick) { + switch(tick % 3) { + case 0: + ch->arp_in_progress = false; + ch->arp_note_offset = 0; + break; + case 2: + ch->arp_in_progress = true; + ch->arp_note_offset = param >> 4; + break; + case 1: + ch->arp_in_progress = true; + ch->arp_note_offset = param & 0x0F; + break; + } + + jar_xm_update_frequency(ctx, ch); +} + +static void jar_xm_tone_portamento(jar_xm_context_t* ctx, jar_xm_channel_context_t* ch) { + /* 3xx called without a note, wait until we get an actual + * target note. */ + if(ch->tone_portamento_target_period == 0.f) return; + + if(ch->period != ch->tone_portamento_target_period) { + jar_xm_SLIDE_TOWARDS(ch->period, + ch->tone_portamento_target_period, + (ctx->module.frequency_type == jar_xm_LINEAR_FREQUENCIES ? + 4.f : 1.f) * ch->tone_portamento_param + ); + jar_xm_update_frequency(ctx, ch); + } +} + +static void jar_xm_pitch_slide(jar_xm_context_t* ctx, jar_xm_channel_context_t* ch, float period_offset) { + /* Don't ask about the 4.f coefficient. I found mention of it + * nowhere. Found by ear™. */ + if(ctx->module.frequency_type == jar_xm_LINEAR_FREQUENCIES) { + period_offset *= 4.f; + } + + ch->period += period_offset; + jar_xm_CLAMP_DOWN(ch->period); + /* XXX: upper bound of period ? */ + + jar_xm_update_frequency(ctx, ch); +} + +static void jar_xm_panning_slide(jar_xm_channel_context_t* ch, uint8_t rawval) { + float f; + + if((rawval & 0xF0) && (rawval & 0x0F)) { + /* Illegal state */ + return; + } + + if(rawval & 0xF0) { + /* Slide right */ + f = (float)(rawval >> 4) / (float)0xFF; + ch->panning += f; + jar_xm_CLAMP_UP(ch->panning); + } else { + /* Slide left */ + f = (float)(rawval & 0x0F) / (float)0xFF; + ch->panning -= f; + jar_xm_CLAMP_DOWN(ch->panning); + } +} + +static void jar_xm_volume_slide(jar_xm_channel_context_t* ch, uint8_t rawval) { + float f; + + if((rawval & 0xF0) && (rawval & 0x0F)) { + /* Illegal state */ + return; + } + + if(rawval & 0xF0) { + /* Slide up */ + f = (float)(rawval >> 4) / (float)0x40; + ch->volume += f; + jar_xm_CLAMP_UP(ch->volume); + } else { + /* Slide down */ + f = (float)(rawval & 0x0F) / (float)0x40; + ch->volume -= f; + jar_xm_CLAMP_DOWN(ch->volume); + } +} + +static float jar_xm_envelope_lerp(jar_xm_envelope_point_t* restrict a, jar_xm_envelope_point_t* restrict b, uint16_t pos) { + /* Linear interpolation between two envelope points */ + if(pos <= a->frame) return a->value; + else if(pos >= b->frame) return b->value; + else { + float p = (float)(pos - a->frame) / (float)(b->frame - a->frame); + return a->value * (1 - p) + b->value * p; + } +} + +static void jar_xm_post_pattern_change(jar_xm_context_t* ctx) { + /* Loop if necessary */ + if(ctx->current_table_index >= ctx->module.length) { + ctx->current_table_index = ctx->module.restart_position; + } +} + +static float jar_xm_linear_period(float note) { + return 7680.f - note * 64.f; +} + +static float jar_xm_linear_frequency(float period) { + return 8363.f * powf(2.f, (4608.f - period) / 768.f); +} + +static float jar_xm_amiga_period(float note) { + unsigned int intnote = note; + uint8_t a = intnote % 12; + int8_t octave = note / 12.f - 2; + uint16_t p1 = amiga_frequencies[a], p2 = amiga_frequencies[a + 1]; + + if(octave > 0) { + p1 >>= octave; + p2 >>= octave; + } else if(octave < 0) { + p1 <<= (-octave); + p2 <<= (-octave); + } + + return jar_xm_LERP(p1, p2, note - intnote); +} + +static float jar_xm_amiga_frequency(float period) { + if(period == .0f) return .0f; + + /* This is the PAL value. No reason to choose this one over the + * NTSC value. */ + return 7093789.2f / (period * 2.f); +} + +static float jar_xm_period(jar_xm_context_t* ctx, float note) { + switch(ctx->module.frequency_type) { + case jar_xm_LINEAR_FREQUENCIES: + return jar_xm_linear_period(note); + case jar_xm_AMIGA_FREQUENCIES: + return jar_xm_amiga_period(note); + } + return .0f; +} + +static float jar_xm_frequency(jar_xm_context_t* ctx, float period, float note_offset) { + uint8_t a; + int8_t octave; + float note; + uint16_t p1, p2; + + switch(ctx->module.frequency_type) { + + case jar_xm_LINEAR_FREQUENCIES: + return jar_xm_linear_frequency(period - 64.f * note_offset); + + case jar_xm_AMIGA_FREQUENCIES: + if(note_offset == 0) { + /* A chance to escape from insanity */ + return jar_xm_amiga_frequency(period); + } + + /* FIXME: this is very crappy at best */ + a = octave = 0; + + /* Find the octave of the current period */ + if(period > amiga_frequencies[0]) { + --octave; + while(period > (amiga_frequencies[0] << (-octave))) --octave; + } else if(period < amiga_frequencies[12]) { + ++octave; + while(period < (amiga_frequencies[12] >> octave)) ++octave; + } + + /* Find the smallest note closest to the current period */ + for(uint8_t i = 0; i < 12; ++i) { + p1 = amiga_frequencies[i], p2 = amiga_frequencies[i + 1]; + + if(octave > 0) { + p1 >>= octave; + p2 >>= octave; + } else if(octave < 0) { + p1 <<= (-octave); + p2 <<= (-octave); + } + + if(p2 <= period && period <= p1) { + a = i; + break; + } + } + + if(JAR_XM_DEBUG && (p1 < period || p2 > period)) { + DEBUG("%i <= %f <= %i should hold but doesn't, this is a bug", p2, period, p1); + } + + note = 12.f * (octave + 2) + a + jar_xm_INVERSE_LERP(p1, p2, period); + + return jar_xm_amiga_frequency(jar_xm_amiga_period(note + note_offset)); + + } + + return .0f; +} + +static void jar_xm_update_frequency(jar_xm_context_t* ctx, jar_xm_channel_context_t* ch) { + ch->frequency = jar_xm_frequency( + ctx, ch->period, + (ch->arp_note_offset > 0 ? ch->arp_note_offset : ( + ch->vibrato_note_offset + ch->autovibrato_note_offset + )) + ); + ch->step = ch->frequency / ctx->rate; +} + +static void jar_xm_handle_note_and_instrument(jar_xm_context_t* ctx, jar_xm_channel_context_t* ch, + jar_xm_pattern_slot_t* s) { + if(s->instrument > 0) { + if(HAS_TONE_PORTAMENTO(ch->current) && ch->instrument != NULL && ch->sample != NULL) { + /* Tone portamento in effect, unclear stuff happens */ + jar_xm_trigger_note(ctx, ch, jar_xm_TRIGGER_KEEP_PERIOD | jar_xm_TRIGGER_KEEP_SAMPLE_POSITION); + } else if(s->instrument > ctx->module.num_instruments) { + /* Invalid instrument, Cut current note */ + jar_xm_cut_note(ch); + ch->instrument = NULL; + ch->sample = NULL; + } else { + ch->instrument = ctx->module.instruments + (s->instrument - 1); + if(s->note == 0 && ch->sample != NULL) { + /* Ghost instrument, trigger note */ + /* Sample position is kept, but envelopes are reset */ + jar_xm_trigger_note(ctx, ch, jar_xm_TRIGGER_KEEP_SAMPLE_POSITION); + } + } + } + + if(NOTE_IS_VALID(s->note)) { + /* Yes, the real note number is s->note -1. Try finding + * THAT in any of the specs! :-) */ + + jar_xm_instrument_t* instr = ch->instrument; + + if(HAS_TONE_PORTAMENTO(ch->current) && instr != NULL && ch->sample != NULL) { + /* Tone portamento in effect */ + ch->note = s->note + ch->sample->relative_note + ch->sample->finetune / 128.f - 1.f; + ch->tone_portamento_target_period = jar_xm_period(ctx, ch->note); + } else if(instr == NULL || ch->instrument->num_samples == 0) { + /* Bad instrument */ + jar_xm_cut_note(ch); + } else { + if(instr->sample_of_notes[s->note - 1] < instr->num_samples) { +#if JAR_XM_RAMPING + for(unsigned int z = 0; z < jar_xm_SAMPLE_RAMPING_POINTS; ++z) { + ch->end_of_previous_sample[z] = jar_xm_next_of_sample(ch); + } + ch->frame_count = 0; +#endif + ch->sample = instr->samples + instr->sample_of_notes[s->note - 1]; + ch->orig_note = ch->note = s->note + ch->sample->relative_note + + ch->sample->finetune / 128.f - 1.f; + if(s->instrument > 0) { + jar_xm_trigger_note(ctx, ch, 0); + } else { + /* Ghost note: keep old volume */ + jar_xm_trigger_note(ctx, ch, jar_xm_TRIGGER_KEEP_VOLUME); + } + } else { + /* Bad sample */ + jar_xm_cut_note(ch); + } + } + } else if(s->note == 97) { + /* Key Off */ + jar_xm_key_off(ch); + } + + switch(s->volume_column >> 4) { + + case 0x5: + if(s->volume_column > 0x50) break; + case 0x1: + case 0x2: + case 0x3: + case 0x4: + /* Set volume */ + ch->volume = (float)(s->volume_column - 0x10) / (float)0x40; + break; + + case 0x8: /* Fine volume slide down */ + jar_xm_volume_slide(ch, s->volume_column & 0x0F); + break; + + case 0x9: /* Fine volume slide up */ + jar_xm_volume_slide(ch, s->volume_column << 4); + break; + + case 0xA: /* Set vibrato speed */ + ch->vibrato_param = (ch->vibrato_param & 0x0F) | ((s->volume_column & 0x0F) << 4); + break; + + case 0xC: /* Set panning */ + ch->panning = (float)( + ((s->volume_column & 0x0F) << 4) | (s->volume_column & 0x0F) + ) / (float)0xFF; + break; + + case 0xF: /* Tone portamento */ + if(s->volume_column & 0x0F) { + ch->tone_portamento_param = ((s->volume_column & 0x0F) << 4) + | (s->volume_column & 0x0F); + } + break; + + default: + break; + + } + + switch(s->effect_type) { + + case 1: /* 1xx: Portamento up */ + if(s->effect_param > 0) { + ch->portamento_up_param = s->effect_param; + } + break; + + case 2: /* 2xx: Portamento down */ + if(s->effect_param > 0) { + ch->portamento_down_param = s->effect_param; + } + break; + + case 3: /* 3xx: Tone portamento */ + if(s->effect_param > 0) { + ch->tone_portamento_param = s->effect_param; + } + break; + + case 4: /* 4xy: Vibrato */ + if(s->effect_param & 0x0F) { + /* Set vibrato depth */ + ch->vibrato_param = (ch->vibrato_param & 0xF0) | (s->effect_param & 0x0F); + } + if(s->effect_param >> 4) { + /* Set vibrato speed */ + ch->vibrato_param = (s->effect_param & 0xF0) | (ch->vibrato_param & 0x0F); + } + break; + + case 5: /* 5xy: Tone portamento + Volume slide */ + if(s->effect_param > 0) { + ch->volume_slide_param = s->effect_param; + } + break; + + case 6: /* 6xy: Vibrato + Volume slide */ + if(s->effect_param > 0) { + ch->volume_slide_param = s->effect_param; + } + break; + + case 7: /* 7xy: Tremolo */ + if(s->effect_param & 0x0F) { + /* Set tremolo depth */ + ch->tremolo_param = (ch->tremolo_param & 0xF0) | (s->effect_param & 0x0F); + } + if(s->effect_param >> 4) { + /* Set tremolo speed */ + ch->tremolo_param = (s->effect_param & 0xF0) | (ch->tremolo_param & 0x0F); + } + break; + + case 8: /* 8xx: Set panning */ + ch->panning = (float)s->effect_param / (float)0xFF; + break; + + case 9: /* 9xx: Sample offset */ + if(ch->sample != NULL && NOTE_IS_VALID(s->note)) { + uint32_t final_offset = s->effect_param << (ch->sample->bits == 16 ? 7 : 8); + if(final_offset >= ch->sample->length) { + /* Pretend the sample dosen't loop and is done playing */ + ch->sample_position = -1; + break; + } + ch->sample_position = final_offset; + } + break; + + case 0xA: /* Axy: Volume slide */ + if(s->effect_param > 0) { + ch->volume_slide_param = s->effect_param; + } + break; + + case 0xB: /* Bxx: Position jump */ + if(s->effect_param < ctx->module.length) { + ctx->position_jump = true; + ctx->jump_dest = s->effect_param; + } + break; + + case 0xC: /* Cxx: Set volume */ + ch->volume = (float)((s->effect_param > 0x40) + ? 0x40 : s->effect_param) / (float)0x40; + break; + + case 0xD: /* Dxx: Pattern break */ + /* Jump after playing this line */ + ctx->pattern_break = true; + ctx->jump_row = (s->effect_param >> 4) * 10 + (s->effect_param & 0x0F); + break; + + case 0xE: /* EXy: Extended command */ + switch(s->effect_param >> 4) { + + case 1: /* E1y: Fine portamento up */ + if(s->effect_param & 0x0F) { + ch->fine_portamento_up_param = s->effect_param & 0x0F; + } + jar_xm_pitch_slide(ctx, ch, -ch->fine_portamento_up_param); + break; + + case 2: /* E2y: Fine portamento down */ + if(s->effect_param & 0x0F) { + ch->fine_portamento_down_param = s->effect_param & 0x0F; + } + jar_xm_pitch_slide(ctx, ch, ch->fine_portamento_down_param); + break; + + case 4: /* E4y: Set vibrato control */ + ch->vibrato_waveform = s->effect_param & 3; + ch->vibrato_waveform_retrigger = !((s->effect_param >> 2) & 1); + break; + + case 5: /* E5y: Set finetune */ + if(NOTE_IS_VALID(ch->current->note) && ch->sample != NULL) { + ch->note = ch->current->note + ch->sample->relative_note + + (float)(((s->effect_param & 0x0F) - 8) << 4) / 128.f - 1.f; + ch->period = jar_xm_period(ctx, ch->note); + jar_xm_update_frequency(ctx, ch); + } + break; + + case 6: /* E6y: Pattern loop */ + if(s->effect_param & 0x0F) { + if((s->effect_param & 0x0F) == ch->pattern_loop_count) { + /* Loop is over */ + ch->pattern_loop_count = 0; + break; + } + + /* Jump to the beginning of the loop */ + ch->pattern_loop_count++; + ctx->position_jump = true; + ctx->jump_row = ch->pattern_loop_origin; + ctx->jump_dest = ctx->current_table_index; + } else { + /* Set loop start point */ + ch->pattern_loop_origin = ctx->current_row; + /* Replicate FT2 E60 bug */ + ctx->jump_row = ch->pattern_loop_origin; + } + break; + + case 7: /* E7y: Set tremolo control */ + ch->tremolo_waveform = s->effect_param & 3; + ch->tremolo_waveform_retrigger = !((s->effect_param >> 2) & 1); + break; + + case 0xA: /* EAy: Fine volume slide up */ + if(s->effect_param & 0x0F) { + ch->fine_volume_slide_param = s->effect_param & 0x0F; + } + jar_xm_volume_slide(ch, ch->fine_volume_slide_param << 4); + break; + + case 0xB: /* EBy: Fine volume slide down */ + if(s->effect_param & 0x0F) { + ch->fine_volume_slide_param = s->effect_param & 0x0F; + } + jar_xm_volume_slide(ch, ch->fine_volume_slide_param); + break; + + case 0xD: /* EDy: Note delay */ + /* XXX: figure this out better. EDx triggers + * the note even when there no note and no + * instrument. But ED0 acts like like a ghost + * note, EDx (x ≠ 0) does not. */ + if(s->note == 0 && s->instrument == 0) { + unsigned int flags = jar_xm_TRIGGER_KEEP_VOLUME; + + if(ch->current->effect_param & 0x0F) { + ch->note = ch->orig_note; + jar_xm_trigger_note(ctx, ch, flags); + } else { + jar_xm_trigger_note( + ctx, ch, + flags + | jar_xm_TRIGGER_KEEP_PERIOD + | jar_xm_TRIGGER_KEEP_SAMPLE_POSITION + ); + } + } + break; + + case 0xE: /* EEy: Pattern delay */ + ctx->extra_ticks = (ch->current->effect_param & 0x0F) * ctx->tempo; + break; + + default: + break; + + } + break; + + case 0xF: /* Fxx: Set tempo/BPM */ + if(s->effect_param > 0) { + if(s->effect_param <= 0x1F) { + ctx->tempo = s->effect_param; + } else { + ctx->bpm = s->effect_param; + } + } + break; + + case 16: /* Gxx: Set global volume */ + ctx->global_volume = (float)((s->effect_param > 0x40) + ? 0x40 : s->effect_param) / (float)0x40; + break; + + case 17: /* Hxy: Global volume slide */ + if(s->effect_param > 0) { + ch->global_volume_slide_param = s->effect_param; + } + break; + + case 21: /* Lxx: Set envelope position */ + ch->volume_envelope_frame_count = s->effect_param; + ch->panning_envelope_frame_count = s->effect_param; + break; + + case 25: /* Pxy: Panning slide */ + if(s->effect_param > 0) { + ch->panning_slide_param = s->effect_param; + } + break; + + case 27: /* Rxy: Multi retrig note */ + if(s->effect_param > 0) { + if((s->effect_param >> 4) == 0) { + /* Keep previous x value */ + ch->multi_retrig_param = (ch->multi_retrig_param & 0xF0) | (s->effect_param & 0x0F); + } else { + ch->multi_retrig_param = s->effect_param; + } + } + break; + + case 29: /* Txy: Tremor */ + if(s->effect_param > 0) { + /* Tremor x and y params do not appear to be separately + * kept in memory, unlike Rxy */ + ch->tremor_param = s->effect_param; + } + break; + + case 33: /* Xxy: Extra stuff */ + switch(s->effect_param >> 4) { + + case 1: /* X1y: Extra fine portamento up */ + if(s->effect_param & 0x0F) { + ch->extra_fine_portamento_up_param = s->effect_param & 0x0F; + } + jar_xm_pitch_slide(ctx, ch, -1.0f * ch->extra_fine_portamento_up_param); + break; + + case 2: /* X2y: Extra fine portamento down */ + if(s->effect_param & 0x0F) { + ch->extra_fine_portamento_down_param = s->effect_param & 0x0F; + } + jar_xm_pitch_slide(ctx, ch, ch->extra_fine_portamento_down_param); + break; + + default: + break; + + } + break; + + default: + break; + + } +} + +static void jar_xm_trigger_note(jar_xm_context_t* ctx, jar_xm_channel_context_t* ch, unsigned int flags) { + if(!(flags & jar_xm_TRIGGER_KEEP_SAMPLE_POSITION)) { + ch->sample_position = 0.f; + ch->ping = true; + } + + if(ch->sample != NULL) { + if(!(flags & jar_xm_TRIGGER_KEEP_VOLUME)) { + ch->volume = ch->sample->volume; + } + + ch->panning = ch->sample->panning; + } + + ch->sustained = true; + ch->fadeout_volume = ch->volume_envelope_volume = 1.0f; + ch->panning_envelope_panning = .5f; + ch->volume_envelope_frame_count = ch->panning_envelope_frame_count = 0; + ch->vibrato_note_offset = 0.f; + ch->tremolo_volume = 0.f; + ch->tremor_on = false; + + ch->autovibrato_ticks = 0; + + if(ch->vibrato_waveform_retrigger) { + ch->vibrato_ticks = 0; /* XXX: should the waveform itself also + * be reset to sine? */ + } + if(ch->tremolo_waveform_retrigger) { + ch->tremolo_ticks = 0; + } + + if(!(flags & jar_xm_TRIGGER_KEEP_PERIOD)) { + ch->period = jar_xm_period(ctx, ch->note); + jar_xm_update_frequency(ctx, ch); + } + + ch->latest_trigger = ctx->generated_samples; + if(ch->instrument != NULL) { + ch->instrument->latest_trigger = ctx->generated_samples; + } + if(ch->sample != NULL) { + ch->sample->latest_trigger = ctx->generated_samples; + } +} + +static void jar_xm_cut_note(jar_xm_channel_context_t* ch) { + /* NB: this is not the same as Key Off */ + ch->volume = .0f; +} + +static void jar_xm_key_off(jar_xm_channel_context_t* ch) { + /* Key Off */ + ch->sustained = false; + + /* If no volume envelope is used, also cut the note */ + if(ch->instrument == NULL || !ch->instrument->volume_envelope.enabled) { + jar_xm_cut_note(ch); + } +} + +static void jar_xm_row(jar_xm_context_t* ctx) { + if(ctx->position_jump) { + ctx->current_table_index = ctx->jump_dest; + ctx->current_row = ctx->jump_row; + ctx->position_jump = false; + ctx->pattern_break = false; + ctx->jump_row = 0; + jar_xm_post_pattern_change(ctx); + } else if(ctx->pattern_break) { + ctx->current_table_index++; + ctx->current_row = ctx->jump_row; + ctx->pattern_break = false; + ctx->jump_row = 0; + jar_xm_post_pattern_change(ctx); + } + + jar_xm_pattern_t* cur = ctx->module.patterns + ctx->module.pattern_table[ctx->current_table_index]; + bool in_a_loop = false; + + /* Read notes… */ + for(uint8_t i = 0; i < ctx->module.num_channels; ++i) { + jar_xm_pattern_slot_t* s = cur->slots + ctx->current_row * ctx->module.num_channels + i; + jar_xm_channel_context_t* ch = ctx->channels + i; + + ch->current = s; + + if(s->effect_type != 0xE || s->effect_param >> 4 != 0xD) { + jar_xm_handle_note_and_instrument(ctx, ch, s); + } else { + ch->note_delay_param = s->effect_param & 0x0F; + } + + if(!in_a_loop && ch->pattern_loop_count > 0) { + in_a_loop = true; + } + } + + if(!in_a_loop) { + /* No E6y loop is in effect (or we are in the first pass) */ + ctx->loop_count = (ctx->row_loop_count[MAX_NUM_ROWS * ctx->current_table_index + ctx->current_row]++); + } + + ctx->current_row++; /* Since this is an uint8, this line can + * increment from 255 to 0, in which case it + * is still necessary to go the next + * pattern. */ + if(!ctx->position_jump && !ctx->pattern_break && + (ctx->current_row >= cur->num_rows || ctx->current_row == 0)) { + ctx->current_table_index++; + ctx->current_row = ctx->jump_row; /* This will be 0 most of + * the time, except when E60 + * is used */ + ctx->jump_row = 0; + jar_xm_post_pattern_change(ctx); + } +} + +static void jar_xm_envelope_tick(jar_xm_channel_context_t* ch, + jar_xm_envelope_t* env, + uint16_t* counter, + float* outval) { + if(env->num_points < 2) { + /* Don't really know what to do… */ + if(env->num_points == 1) { + /* XXX I am pulling this out of my ass */ + *outval = (float)env->points[0].value / (float)0x40; + if(*outval > 1) { + *outval = 1; + } + } + + return; + } else { + uint8_t j; + + if(env->loop_enabled) { + uint16_t loop_start = env->points[env->loop_start_point].frame; + uint16_t loop_end = env->points[env->loop_end_point].frame; + uint16_t loop_length = loop_end - loop_start; + + if(*counter >= loop_end) { + *counter -= loop_length; + } + } + + for(j = 0; j < (env->num_points - 2); ++j) { + if(env->points[j].frame <= *counter && + env->points[j+1].frame >= *counter) { + break; + } + } + + *outval = jar_xm_envelope_lerp(env->points + j, env->points + j + 1, *counter) / (float)0x40; + + /* Make sure it is safe to increment frame count */ + if(!ch->sustained || !env->sustain_enabled || + *counter != env->points[env->sustain_point].frame) { + (*counter)++; + } + } +} + +static void jar_xm_envelopes(jar_xm_channel_context_t* ch) { + if(ch->instrument != NULL) { + if(ch->instrument->volume_envelope.enabled) { + if(!ch->sustained) { + ch->fadeout_volume -= (float)ch->instrument->volume_fadeout / 65536.f; + jar_xm_CLAMP_DOWN(ch->fadeout_volume); + } + + jar_xm_envelope_tick(ch, + &(ch->instrument->volume_envelope), + &(ch->volume_envelope_frame_count), + &(ch->volume_envelope_volume)); + } + + if(ch->instrument->panning_envelope.enabled) { + jar_xm_envelope_tick(ch, + &(ch->instrument->panning_envelope), + &(ch->panning_envelope_frame_count), + &(ch->panning_envelope_panning)); + } + } +} + +static void jar_xm_tick(jar_xm_context_t* ctx) { + if(ctx->current_tick == 0) { + jar_xm_row(ctx); + } + + for(uint8_t i = 0; i < ctx->module.num_channels; ++i) { + jar_xm_channel_context_t* ch = ctx->channels + i; + + jar_xm_envelopes(ch); + jar_xm_autovibrato(ctx, ch); + + if(ch->arp_in_progress && !HAS_ARPEGGIO(ch->current)) { + ch->arp_in_progress = false; + ch->arp_note_offset = 0; + jar_xm_update_frequency(ctx, ch); + } + if(ch->vibrato_in_progress && !HAS_VIBRATO(ch->current)) { + ch->vibrato_in_progress = false; + ch->vibrato_note_offset = 0.f; + jar_xm_update_frequency(ctx, ch); + } + + switch(ch->current->volume_column >> 4) { + + case 0x6: /* Volume slide down */ + if(ctx->current_tick == 0) break; + jar_xm_volume_slide(ch, ch->current->volume_column & 0x0F); + break; + + case 0x7: /* Volume slide up */ + if(ctx->current_tick == 0) break; + jar_xm_volume_slide(ch, ch->current->volume_column << 4); + break; + + case 0xB: /* Vibrato */ + if(ctx->current_tick == 0) break; + ch->vibrato_in_progress = false; + jar_xm_vibrato(ctx, ch, ch->vibrato_param, ch->vibrato_ticks++); + break; + + case 0xD: /* Panning slide left */ + if(ctx->current_tick == 0) break; + jar_xm_panning_slide(ch, ch->current->volume_column & 0x0F); + break; + + case 0xE: /* Panning slide right */ + if(ctx->current_tick == 0) break; + jar_xm_panning_slide(ch, ch->current->volume_column << 4); + break; + + case 0xF: /* Tone portamento */ + if(ctx->current_tick == 0) break; + jar_xm_tone_portamento(ctx, ch); + break; + + default: + break; + + } + + switch(ch->current->effect_type) { + + case 0: /* 0xy: Arpeggio */ + if(ch->current->effect_param > 0) { + char arp_offset = ctx->tempo % 3; + switch(arp_offset) { + case 2: /* 0 -> x -> 0 -> y -> x -> … */ + if(ctx->current_tick == 1) { + ch->arp_in_progress = true; + ch->arp_note_offset = ch->current->effect_param >> 4; + jar_xm_update_frequency(ctx, ch); + break; + } + /* No break here, this is intended */ + case 1: /* 0 -> 0 -> y -> x -> … */ + if(ctx->current_tick == 0) { + ch->arp_in_progress = false; + ch->arp_note_offset = 0; + jar_xm_update_frequency(ctx, ch); + break; + } + /* No break here, this is intended */ + case 0: /* 0 -> y -> x -> … */ + jar_xm_arpeggio(ctx, ch, ch->current->effect_param, ctx->current_tick - arp_offset); + default: + break; + } + } + break; + + case 1: /* 1xx: Portamento up */ + if(ctx->current_tick == 0) break; + jar_xm_pitch_slide(ctx, ch, -ch->portamento_up_param); + break; + + case 2: /* 2xx: Portamento down */ + if(ctx->current_tick == 0) break; + jar_xm_pitch_slide(ctx, ch, ch->portamento_down_param); + break; + + case 3: /* 3xx: Tone portamento */ + if(ctx->current_tick == 0) break; + jar_xm_tone_portamento(ctx, ch); + break; + + case 4: /* 4xy: Vibrato */ + if(ctx->current_tick == 0) break; + ch->vibrato_in_progress = true; + jar_xm_vibrato(ctx, ch, ch->vibrato_param, ch->vibrato_ticks++); + break; + + case 5: /* 5xy: Tone portamento + Volume slide */ + if(ctx->current_tick == 0) break; + jar_xm_tone_portamento(ctx, ch); + jar_xm_volume_slide(ch, ch->volume_slide_param); + break; + + case 6: /* 6xy: Vibrato + Volume slide */ + if(ctx->current_tick == 0) break; + ch->vibrato_in_progress = true; + jar_xm_vibrato(ctx, ch, ch->vibrato_param, ch->vibrato_ticks++); + jar_xm_volume_slide(ch, ch->volume_slide_param); + break; + + case 7: /* 7xy: Tremolo */ + if(ctx->current_tick == 0) break; + jar_xm_tremolo(ctx, ch, ch->tremolo_param, ch->tremolo_ticks++); + break; + + case 0xA: /* Axy: Volume slide */ + if(ctx->current_tick == 0) break; + jar_xm_volume_slide(ch, ch->volume_slide_param); + break; + + case 0xE: /* EXy: Extended command */ + switch(ch->current->effect_param >> 4) { + + case 0x9: /* E9y: Retrigger note */ + if(ctx->current_tick != 0 && ch->current->effect_param & 0x0F) { + if(!(ctx->current_tick % (ch->current->effect_param & 0x0F))) { + jar_xm_trigger_note(ctx, ch, 0); + jar_xm_envelopes(ch); + } + } + break; + + case 0xC: /* ECy: Note cut */ + if((ch->current->effect_param & 0x0F) == ctx->current_tick) { + jar_xm_cut_note(ch); + } + break; + + case 0xD: /* EDy: Note delay */ + if(ch->note_delay_param == ctx->current_tick) { + jar_xm_handle_note_and_instrument(ctx, ch, ch->current); + jar_xm_envelopes(ch); + } + break; + + default: + break; + + } + break; + + case 17: /* Hxy: Global volume slide */ + if(ctx->current_tick == 0) break; + if((ch->global_volume_slide_param & 0xF0) && + (ch->global_volume_slide_param & 0x0F)) { + /* Illegal state */ + break; + } + if(ch->global_volume_slide_param & 0xF0) { + /* Global slide up */ + float f = (float)(ch->global_volume_slide_param >> 4) / (float)0x40; + ctx->global_volume += f; + jar_xm_CLAMP_UP(ctx->global_volume); + } else { + /* Global slide down */ + float f = (float)(ch->global_volume_slide_param & 0x0F) / (float)0x40; + ctx->global_volume -= f; + jar_xm_CLAMP_DOWN(ctx->global_volume); + } + break; + + case 20: /* Kxx: Key off */ + /* Most documentations will tell you the parameter has no + * use. Don't be fooled. */ + if(ctx->current_tick == ch->current->effect_param) { + jar_xm_key_off(ch); + } + break; + + case 25: /* Pxy: Panning slide */ + if(ctx->current_tick == 0) break; + jar_xm_panning_slide(ch, ch->panning_slide_param); + break; + + case 27: /* Rxy: Multi retrig note */ + if(ctx->current_tick == 0) break; + if(((ch->multi_retrig_param) & 0x0F) == 0) break; + if((ctx->current_tick % (ch->multi_retrig_param & 0x0F)) == 0) { + float v = ch->volume * multi_retrig_multiply[ch->multi_retrig_param >> 4] + + multi_retrig_add[ch->multi_retrig_param >> 4]; + jar_xm_CLAMP(v); + jar_xm_trigger_note(ctx, ch, 0); + ch->volume = v; + } + break; + + case 29: /* Txy: Tremor */ + if(ctx->current_tick == 0) break; + ch->tremor_on = ( + (ctx->current_tick - 1) % ((ch->tremor_param >> 4) + (ch->tremor_param & 0x0F) + 2) + > + (ch->tremor_param >> 4) + ); + break; + + default: + break; + + } + + float panning, volume; + + panning = ch->panning + + (ch->panning_envelope_panning - .5f) * (.5f - fabsf(ch->panning - .5f)) * 2.0f; + + if(ch->tremor_on) { + volume = .0f; + } else { + volume = ch->volume + ch->tremolo_volume; + jar_xm_CLAMP(volume); + volume *= ch->fadeout_volume * ch->volume_envelope_volume; + } + +#if JAR_XM_RAMPING + ch->target_panning = panning; + ch->target_volume = volume; +#else + ch->actual_panning = panning; + ch->actual_volume = volume; +#endif + } + + ctx->current_tick++; + if(ctx->current_tick >= ctx->tempo + ctx->extra_ticks) { + ctx->current_tick = 0; + ctx->extra_ticks = 0; + } + + /* FT2 manual says number of ticks / second = BPM * 0.4 */ + ctx->remaining_samples_in_tick += (float)ctx->rate / ((float)ctx->bpm * 0.4f); +} + +static float jar_xm_next_of_sample(jar_xm_channel_context_t* ch) { + if(ch->instrument == NULL || ch->sample == NULL || ch->sample_position < 0) { +#if JAR_XM_RAMPING + if(ch->frame_count < jar_xm_SAMPLE_RAMPING_POINTS) { + return jar_xm_LERP(ch->end_of_previous_sample[ch->frame_count], .0f, + (float)ch->frame_count / (float)jar_xm_SAMPLE_RAMPING_POINTS); + } +#endif + return .0f; + } + if(ch->sample->length == 0) { + return .0f; + } + + float u, v, t; + uint32_t a, b; + a = (uint32_t)ch->sample_position; /* This cast is fine, + * sample_position will not + * go above integer + * ranges */ + if(JAR_XM_LINEAR_INTERPOLATION) { + b = a + 1; + t = ch->sample_position - a; /* Cheaper than fmodf(., 1.f) */ + } + u = ch->sample->data[a]; + + switch(ch->sample->loop_type) { + + case jar_xm_NO_LOOP: + if(JAR_XM_LINEAR_INTERPOLATION) { + v = (b < ch->sample->length) ? ch->sample->data[b] : .0f; + } + ch->sample_position += ch->step; + if(ch->sample_position >= ch->sample->length) { + ch->sample_position = -1; + } + break; + + case jar_xm_FORWARD_LOOP: + if(JAR_XM_LINEAR_INTERPOLATION) { + v = ch->sample->data[ + (b == ch->sample->loop_end) ? ch->sample->loop_start : b + ]; + } + ch->sample_position += ch->step; + while(ch->sample_position >= ch->sample->loop_end) { + ch->sample_position -= ch->sample->loop_length; + } + break; + + case jar_xm_PING_PONG_LOOP: + if(ch->ping) { + ch->sample_position += ch->step; + } else { + ch->sample_position -= ch->step; + } + /* XXX: this may not work for very tight ping-pong loops + * (ie switches direction more than once per sample */ + if(ch->ping) { + if(JAR_XM_LINEAR_INTERPOLATION) { + v = (b >= ch->sample->loop_end) ? ch->sample->data[a] : ch->sample->data[b]; + } + if(ch->sample_position >= ch->sample->loop_end) { + ch->ping = false; + ch->sample_position = (ch->sample->loop_end << 1) - ch->sample_position; + } + /* sanity checking */ + if(ch->sample_position >= ch->sample->length) { + ch->ping = false; + ch->sample_position -= ch->sample->length - 1; + } + } else { + if(JAR_XM_LINEAR_INTERPOLATION) { + v = u; + u = (b == 1 || b - 2 <= ch->sample->loop_start) ? ch->sample->data[a] : ch->sample->data[b - 2]; + } + if(ch->sample_position <= ch->sample->loop_start) { + ch->ping = true; + ch->sample_position = (ch->sample->loop_start << 1) - ch->sample_position; + } + /* sanity checking */ + if(ch->sample_position <= .0f) { + ch->ping = true; + ch->sample_position = .0f; + } + } + break; + + default: + v = .0f; + break; + } + + float endval = JAR_XM_LINEAR_INTERPOLATION ? jar_xm_LERP(u, v, t) : u; + +#if JAR_XM_RAMPING + if(ch->frame_count < jar_xm_SAMPLE_RAMPING_POINTS) { + /* Smoothly transition between old and new sample. */ + return jar_xm_LERP(ch->end_of_previous_sample[ch->frame_count], endval, + (float)ch->frame_count / (float)jar_xm_SAMPLE_RAMPING_POINTS); + } +#endif + + return endval; +} + +static void jar_xm_sample(jar_xm_context_t* ctx, float* left, float* right) { + if(ctx->remaining_samples_in_tick <= 0) { + jar_xm_tick(ctx); + } + ctx->remaining_samples_in_tick--; + + *left = 0.f; + *right = 0.f; + + if(ctx->max_loop_count > 0 && ctx->loop_count >= ctx->max_loop_count) { + return; + } + + for(uint8_t i = 0; i < ctx->module.num_channels; ++i) { + jar_xm_channel_context_t* ch = ctx->channels + i; + + if(ch->instrument == NULL || ch->sample == NULL || ch->sample_position < 0) { + continue; + } + + const float fval = jar_xm_next_of_sample(ch); + + if(!ch->muted && !ch->instrument->muted) { + *left += fval * ch->actual_volume * (1.f - ch->actual_panning); + *right += fval * ch->actual_volume * ch->actual_panning; + } + +#if JAR_XM_RAMPING + ch->frame_count++; + jar_xm_SLIDE_TOWARDS(ch->actual_volume, ch->target_volume, ctx->volume_ramp); + jar_xm_SLIDE_TOWARDS(ch->actual_panning, ch->target_panning, ctx->panning_ramp); +#endif + } + + const float fgvol = ctx->global_volume * ctx->amplification; + *left *= fgvol; + *right *= fgvol; + +#if JAR_XM_DEBUG + if(fabs(*left) > 1 || fabs(*right) > 1) { + DEBUG("clipping frame: %f %f, this is a bad module or a libxm bug", *left, *right); + } +#endif +} + +void jar_xm_generate_samples(jar_xm_context_t* ctx, float* output, size_t numsamples) { + if(ctx && output) { + ctx->generated_samples += numsamples; + for(size_t i = 0; i < numsamples; i++) { + jar_xm_sample(ctx, output + (2 * i), output + (2 * i + 1)); + } + } +} + + + + + + + +//-------------------------------------------- +//FILE LOADER - TODO - NEEDS TO BE CLEANED UP +//-------------------------------------------- + + + +#undef DEBUG +#define DEBUG(...) do { \ + fprintf(stderr, __VA_ARGS__); \ + fflush(stderr); \ + } while(0) + +#define DEBUG_ERR(...) do { \ + fprintf(stderr, __VA_ARGS__); \ + fflush(stderr); \ + } while(0) + +#define FATAL(...) do { \ + fprintf(stderr, __VA_ARGS__); \ + fflush(stderr); \ + exit(1); \ + } while(0) + +#define FATAL_ERR(...) do { \ + fprintf(stderr, __VA_ARGS__); \ + fflush(stderr); \ + exit(1); \ + } while(0) + + +int jar_xm_create_context_from_file(jar_xm_context_t** ctx, uint32_t rate, const char* filename) { + FILE* xmf; + int size; + + xmf = fopen(filename, "rb"); + if(xmf == NULL) { + DEBUG_ERR("Could not open input file"); + *ctx = NULL; + return 3; + } + + fseek(xmf, 0, SEEK_END); + size = ftell(xmf); + rewind(xmf); + if(size == -1) { + fclose(xmf); + DEBUG_ERR("fseek() failed"); + *ctx = NULL; + return 4; + } + + char* data = malloc(size + 1); + if(fread(data, 1, size, xmf) < size) { + fclose(xmf); + DEBUG_ERR("fread() failed"); + *ctx = NULL; + return 5; + } + + fclose(xmf); + + switch(jar_xm_create_context_safe(ctx, data, size, rate)) { + case 0: + break; + + case 1: + DEBUG("could not create context: module is not sane\n"); + *ctx = NULL; + return 1; + break; + + case 2: + FATAL("could not create context: malloc failed\n"); + return 2; + break; + + default: + FATAL("could not create context: unknown error\n"); + return 6; + break; + + } + + return 0; +} + + + + +#endif//end of JAR_XM_IMPLEMENTATION +//------------------------------------------------------------------------------- + + + + +#endif//end of INCLUDE_JAR_XM_H -- cgit v1.2.3 From cb05c51911fdc885c8e91e51cbaa2ab32e114c7b Mon Sep 17 00:00:00 2001 From: Joshua Reisenauer Date: Sun, 24 Apr 2016 18:18:18 -0700 Subject: tabs to spaces fix --- src/audio.c | 124 ++++++++++++++++++++++++++++++------------------------------ 1 file changed, 62 insertions(+), 62 deletions(-) (limited to 'src') diff --git a/src/audio.c b/src/audio.c index 9baa2222..e5bd6819 100644 --- a/src/audio.c +++ b/src/audio.c @@ -75,7 +75,7 @@ // NOTE: Anything longer than ~10 seconds should be streamed... typedef struct Music { stb_vorbis *stream; - jar_xm_context_t *chipctx; // Stores jar_xm context + jar_xm_context_t *chipctx; // Stores jar_xm context ALuint buffers[MUSIC_STREAM_BUFFERS]; ALuint source; @@ -85,7 +85,7 @@ typedef struct Music { int sampleRate; int totalSamplesLeft; bool loop; - bool chipTune; // True if chiptune is loaded + bool chipTune; // True if chiptune is loaded } Music; #if defined(AUDIO_STANDALONE) @@ -567,22 +567,22 @@ void PlayMusicStream(char *fileName) currentMusic.totalSamplesLeft = stb_vorbis_stream_length_in_samples(currentMusic.stream) * currentMusic.channels; } } - else if (strcmp(GetExtension(fileName),"xm") == 0) - { - currentMusic.chipTune = true; - currentMusic.channels = 2; - currentMusic.sampleRate = 48000; - currentMusic.loop = true; - - // only stereo/float is supported for xm - if(info.channels == 2 && !jar_xm_create_context_from_file(¤tMusic.chipctx, currentMusic.sampleRate, fileName)) - { - currentMusic.format = AL_FORMAT_STEREO_FLOAT32; - jar_xm_set_max_loop_count(currentMusic.chipctx, 0); //infinite number of loops - //currentMusic.totalSamplesLeft = ; // Unsure of how to calculate this - musicEnabled = true; - } - } + else if (strcmp(GetExtension(fileName),"xm") == 0) + { + currentMusic.chipTune = true; + currentMusic.channels = 2; + currentMusic.sampleRate = 48000; + currentMusic.loop = true; + + // only stereo/float is supported for xm + if(info.channels == 2 && !jar_xm_create_context_from_file(¤tMusic.chipctx, currentMusic.sampleRate, fileName)) + { + currentMusic.format = AL_FORMAT_STEREO_FLOAT32; + jar_xm_set_max_loop_count(currentMusic.chipctx, 0); //infinite number of loops + //currentMusic.totalSamplesLeft = ; // Unsure of how to calculate this + musicEnabled = true; + } + } else TraceLog(WARNING, "[%s] Music extension not recognized, it can't be loaded", fileName); } @@ -591,19 +591,19 @@ void StopMusicStream(void) { if (musicEnabled) { - alSourceStop(currentMusic.source); - EmptyMusicStream(); // Empty music buffers - alDeleteSources(1, ¤tMusic.source); - alDeleteBuffers(2, currentMusic.buffers); - - if (currentMusic.chipTune) - { - jar_xm_free_context(currentMusic.chipctx); - } - else - { - stb_vorbis_close(currentMusic.stream); - } + alSourceStop(currentMusic.source); + EmptyMusicStream(); // Empty music buffers + alDeleteSources(1, ¤tMusic.source); + alDeleteBuffers(2, currentMusic.buffers); + + if (currentMusic.chipTune) + { + jar_xm_free_context(currentMusic.chipctx); + } + else + { + stb_vorbis_close(currentMusic.stream); + } } musicEnabled = false; @@ -657,15 +657,15 @@ void SetMusicVolume(float volume) // Get current music time length (in seconds) float GetMusicTimeLength(void) { - float totalSeconds; - if (currentMusic.chipTune) - { - //totalSeconds = (float)samples; // Need to figure out how toget this - } - else - { - totalSeconds = stb_vorbis_stream_length_in_seconds(currentMusic.stream); - } + float totalSeconds; + if (currentMusic.chipTune) + { + //totalSeconds = (float)samples; // Need to figure out how toget this + } + else + { + totalSeconds = stb_vorbis_stream_length_in_seconds(currentMusic.stream); + } return totalSeconds; } @@ -673,19 +673,19 @@ float GetMusicTimeLength(void) // Get current music time played (in seconds) float GetMusicTimePlayed(void) { - float secondsPlayed; - if (currentMusic.chipTune) - { - uint64_t* samples; - jar_xm_get_position(currentMusic.chipctx, NULL, NULL, NULL, samples); // Unsure if this is the desired value - secondsPlayed = (float)samples; - } - else - { - int totalSamples = stb_vorbis_stream_length_in_samples(currentMusic.stream) * currentMusic.channels; - int samplesPlayed = totalSamples - currentMusic.totalSamplesLeft; - secondsPlayed = (float)samplesPlayed / (currentMusic.sampleRate * currentMusic.channels); - } + float secondsPlayed; + if (currentMusic.chipTune) + { + uint64_t* samples; + jar_xm_get_position(currentMusic.chipctx, NULL, NULL, NULL, samples); // Unsure if this is the desired value + secondsPlayed = (float)samples; + } + else + { + int totalSamples = stb_vorbis_stream_length_in_samples(currentMusic.stream) * currentMusic.channels; + int samplesPlayed = totalSamples - currentMusic.totalSamplesLeft; + secondsPlayed = (float)samplesPlayed / (currentMusic.sampleRate * currentMusic.channels); + } return secondsPlayed; @@ -709,15 +709,15 @@ static bool BufferMusicStream(ALuint buffer) { while (size < MUSIC_BUFFER_SIZE) { - if (currentMusic.chipTune) - { - jar_xm_generate_samples(currentMusic.chipctx, pcm + size, (MUSIC_BUFFER_SIZE - size)/2); - streamedBytes = (MUSIC_BUFFER_SIZE - size)/2; // There is no end of stream for xmfiles, once the end is reached zeros are generated for non looped chiptunes. - } - else - { - streamedBytes = stb_vorbis_get_samples_short_interleaved(currentMusic.stream, currentMusic.channels, pcm + size, MUSIC_BUFFER_SIZE - size); - } + if (currentMusic.chipTune) + { + jar_xm_generate_samples(currentMusic.chipctx, pcm + size, (MUSIC_BUFFER_SIZE - size)/2); + streamedBytes = (MUSIC_BUFFER_SIZE - size)/2; // There is no end of stream for xmfiles, once the end is reached zeros are generated for non looped chiptunes. + } + else + { + streamedBytes = stb_vorbis_get_samples_short_interleaved(currentMusic.stream, currentMusic.channels, pcm + size, MUSIC_BUFFER_SIZE - size); + } if (streamedBytes > 0) size += (streamedBytes*currentMusic.channels); else break; -- cgit v1.2.3 From 1c370f5a179ab956d90d80e5a3b566ab14027557 Mon Sep 17 00:00:00 2001 From: Joshua Reisenauer Date: Sun, 24 Apr 2016 22:00:40 -0700 Subject: cleaned up calculations --- src/audio.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/audio.c b/src/audio.c index e5bd6819..b8657336 100644 --- a/src/audio.c +++ b/src/audio.c @@ -676,9 +676,9 @@ float GetMusicTimePlayed(void) float secondsPlayed; if (currentMusic.chipTune) { - uint64_t* samples; - jar_xm_get_position(currentMusic.chipctx, NULL, NULL, NULL, samples); // Unsure if this is the desired value - secondsPlayed = (float)samples; + uint64_t samples; + jar_xm_get_position(currentMusic.chipctx, NULL, NULL, NULL, &samples); // Unsure if this is the desired value + secondsPlayed = (float)samples / (currentMusic.sampleRate * currentMusic.channels); } else { @@ -711,7 +711,7 @@ static bool BufferMusicStream(ALuint buffer) { if (currentMusic.chipTune) { - jar_xm_generate_samples(currentMusic.chipctx, pcm + size, (MUSIC_BUFFER_SIZE - size)/2); + jar_xm_generate_samples(currentMusic.chipctx, pcm + size, (MUSIC_BUFFER_SIZE - size) / 2); streamedBytes = (MUSIC_BUFFER_SIZE - size)/2; // There is no end of stream for xmfiles, once the end is reached zeros are generated for non looped chiptunes. } else -- cgit v1.2.3 From 89a84a621bb5bd3a6f17ebc788e143c2874cd55b Mon Sep 17 00:00:00 2001 From: Joshua Reisenauer Date: Sun, 24 Apr 2016 22:04:31 -0700 Subject: implement --- src/audio.c | 1 + 1 file changed, 1 insertion(+) (limited to 'src') diff --git a/src/audio.c b/src/audio.c index b8657336..93b83599 100644 --- a/src/audio.c +++ b/src/audio.c @@ -42,6 +42,7 @@ #include // Required for strcmp() #include // Used for .WAV loading +#define JAR_XM_IMPLEMENTATION #include "jar_xm.h" // For playing .xm files #if defined(AUDIO_STANDALONE) -- cgit v1.2.3 From 62087d21cc9c3ab166fd0e4e54401c907374a46a Mon Sep 17 00:00:00 2001 From: Joshua Reisenauer Date: Sun, 24 Apr 2016 23:44:49 -0700 Subject: updated jar_xm --- src/jar_xm.h | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/jar_xm.h b/src/jar_xm.h index 2f102cf8..4fda948b 100644 --- a/src/jar_xm.h +++ b/src/jar_xm.h @@ -288,6 +288,13 @@ uint64_t jar_xm_get_latest_trigger_of_sample(jar_xm_context_t*, uint16_t instr, */ uint64_t jar_xm_get_latest_trigger_of_channel(jar_xm_context_t*, uint16_t); +/** Get the number of remaining samples. Divide by 2 to get the number of individual LR data samples. + * + * @note This is the remaining number of samples before the loop starts module again, or halts if on last pass. + * @note This function is very slow and should only be run once, if at all. + */ +uint64_t jar_xm_get_remaining_samples(jar_xm_context_t*); + #ifdef __cplusplus } #endif @@ -2543,7 +2550,22 @@ void jar_xm_generate_samples(jar_xm_context_t* ctx, float* output, size_t numsam } } - +uint64_t jar_xm_get_remaining_samples(jar_xm_context_t* ctx) +{ + uint64_t total = 0; + uint8_t currentLoopCount = jar_xm_get_loop_count(ctx); + jar_xm_set_max_loop_count(ctx, 0); + + while(jar_xm_get_loop_count(ctx) == currentLoopCount) + { + total += ctx->remaining_samples_in_tick; + ctx->remaining_samples_in_tick = 0; + jar_xm_tick(ctx); + } + + ctx->loop_count = currentLoopCount; + return total; +} -- cgit v1.2.3 From f12754b01f107913b722c80f0d957bdcdfb68c81 Mon Sep 17 00:00:00 2001 From: Joshua Reisenauer Date: Mon, 25 Apr 2016 18:40:19 -0700 Subject: quick fix Boolean errors --- src/audio.c | 20 ++++++++++---------- src/jar_xm.h | 3 ++- src/raylib.h | 3 ++- 3 files changed, 14 insertions(+), 12 deletions(-) (limited to 'src') diff --git a/src/audio.c b/src/audio.c index 93b83599..5953cd40 100644 --- a/src/audio.c +++ b/src/audio.c @@ -42,9 +42,6 @@ #include // Required for strcmp() #include // Used for .WAV loading -#define JAR_XM_IMPLEMENTATION -#include "jar_xm.h" // For playing .xm files - #if defined(AUDIO_STANDALONE) #include // Used for functions with variable number of parameters (TraceLog()) #else @@ -53,7 +50,10 @@ #endif //#define STB_VORBIS_HEADER_ONLY -#include "stb_vorbis.h" // OGG loading functions +#include "stb_vorbis.h" // OGG loading functions + +#define JAR_XM_IMPLEMENTATION +#include "jar_xm.h" // For playing .xm files //---------------------------------------------------------------------------------- // Defines and Macros @@ -576,11 +576,11 @@ void PlayMusicStream(char *fileName) currentMusic.loop = true; // only stereo/float is supported for xm - if(info.channels == 2 && !jar_xm_create_context_from_file(¤tMusic.chipctx, currentMusic.sampleRate, fileName)) + if(!jar_xm_create_context_from_file(¤tMusic.chipctx, currentMusic.sampleRate, fileName)) { - currentMusic.format = AL_FORMAT_STEREO_FLOAT32; - jar_xm_set_max_loop_count(currentMusic.chipctx, 0); //infinite number of loops - //currentMusic.totalSamplesLeft = ; // Unsure of how to calculate this + currentMusic.format = AL_FORMAT_STEREO16; // AL_FORMAT_STEREO_FLOAT32; + jar_xm_set_max_loop_count(currentMusic.chipctx, 0); // infinite number of loops + currentMusic.totalSamplesLeft = jar_xm_get_remaining_samples(currentMusic.chipctx); musicEnabled = true; } } @@ -712,8 +712,8 @@ static bool BufferMusicStream(ALuint buffer) { if (currentMusic.chipTune) { - jar_xm_generate_samples(currentMusic.chipctx, pcm + size, (MUSIC_BUFFER_SIZE - size) / 2); - streamedBytes = (MUSIC_BUFFER_SIZE - size)/2; // There is no end of stream for xmfiles, once the end is reached zeros are generated for non looped chiptunes. + jar_xm_generate_samples_16bit(currentMusic.chipctx, pcm + size, (MUSIC_BUFFER_SIZE - size) / 2); + streamedBytes = (MUSIC_BUFFER_SIZE - size) * 2; // There is no end of stream for xmfiles, once the end is reached zeros are generated for non looped chiptunes. } else { diff --git a/src/jar_xm.h b/src/jar_xm.h index 4fda948b..062b88da 100644 --- a/src/jar_xm.h +++ b/src/jar_xm.h @@ -59,10 +59,11 @@ #include #include #include -#include #include #include + + //------------------------------------------------------------------------------- #ifdef __cplusplus extern "C" { diff --git a/src/raylib.h b/src/raylib.h index 0c80b957..de157087 100644 --- a/src/raylib.h +++ b/src/raylib.h @@ -261,7 +261,8 @@ //---------------------------------------------------------------------------------- #ifndef __cplusplus // Boolean type -typedef enum { false, true } bool; +#include +//typedef enum { false, true } bool; #endif // byte type -- cgit v1.2.3 From 04d9deac92dc9dc041ee31b0d9535d0d535d4040 Mon Sep 17 00:00:00 2001 From: Joshua Reisenauer Date: Mon, 25 Apr 2016 20:05:03 -0700 Subject: setting up openal --- src/audio.c | 41 +++++++++++++++++++++++++++++++---------- 1 file changed, 31 insertions(+), 10 deletions(-) (limited to 'src') diff --git a/src/audio.c b/src/audio.c index 5953cd40..b46c2c20 100644 --- a/src/audio.c +++ b/src/audio.c @@ -85,6 +85,7 @@ typedef struct Music { int channels; int sampleRate; int totalSamplesLeft; + float totalLengthSeconds; bool loop; bool chipTune; // True if chiptune is loaded } Music; @@ -575,13 +576,26 @@ void PlayMusicStream(char *fileName) currentMusic.sampleRate = 48000; currentMusic.loop = true; - // only stereo/float is supported for xm + // only stereo is supported for xm if(!jar_xm_create_context_from_file(¤tMusic.chipctx, currentMusic.sampleRate, fileName)) { currentMusic.format = AL_FORMAT_STEREO16; // AL_FORMAT_STEREO_FLOAT32; jar_xm_set_max_loop_count(currentMusic.chipctx, 0); // infinite number of loops currentMusic.totalSamplesLeft = jar_xm_get_remaining_samples(currentMusic.chipctx); + currentMusic.totalLengthSeconds = currentMusic.totalSamplesLeft / (currentMusic.sampleRate * currentMusic.channels); musicEnabled = true; + + // Set up OpenAL + alGenSources(1, ¤tMusic.source); + alSourcef(currentMusic.source, AL_PITCH, 1); + alSourcef(currentMusic.source, AL_GAIN, 1); + alSource3f(currentMusic.source, AL_POSITION, 0, 0, 0); + alSource3f(currentMusic.source, AL_VELOCITY, 0, 0, 0); + alGenBuffers(2, currentMusic.buffers); + BufferMusicStream(currentMusic.buffers[0]); + BufferMusicStream(currentMusic.buffers[1]); + alSourceQueueBuffers(currentMusic.source, 2, currentMusic.buffers); + alSourcePlay(currentMusic.source); } } else TraceLog(WARNING, "[%s] Music extension not recognized, it can't be loaded", fileName); @@ -661,7 +675,7 @@ float GetMusicTimeLength(void) float totalSeconds; if (currentMusic.chipTune) { - //totalSeconds = (float)samples; // Need to figure out how toget this + totalSeconds = currentMusic.totalLengthSeconds; // Not sure if this is the correct value } else { @@ -678,8 +692,8 @@ float GetMusicTimePlayed(void) if (currentMusic.chipTune) { uint64_t samples; - jar_xm_get_position(currentMusic.chipctx, NULL, NULL, NULL, &samples); // Unsure if this is the desired value - secondsPlayed = (float)samples / (currentMusic.sampleRate * currentMusic.channels); + jar_xm_get_position(currentMusic.chipctx, NULL, NULL, NULL, &samples); + secondsPlayed = (float)samples / (currentMusic.sampleRate * currentMusic.channels); // Not sure if this is the correct value } else { @@ -710,10 +724,11 @@ static bool BufferMusicStream(ALuint buffer) { while (size < MUSIC_BUFFER_SIZE) { - if (currentMusic.chipTune) + if (currentMusic.chipTune) // There is no end of stream for xmfiles, once the end is reached zeros are generated for non looped chiptunes. { - jar_xm_generate_samples_16bit(currentMusic.chipctx, pcm + size, (MUSIC_BUFFER_SIZE - size) / 2); - streamedBytes = (MUSIC_BUFFER_SIZE - size) * 2; // There is no end of stream for xmfiles, once the end is reached zeros are generated for non looped chiptunes. + int readlen = (MUSIC_BUFFER_SIZE - size) / 2; + jar_xm_generate_samples_16bit(currentMusic.chipctx, pcm + size, readlen); // reads 2*readlen shorts and moves them to buffer+size memory location + streamedBytes = readlen * 4; // Not sure if this is what it needs } else { @@ -781,9 +796,15 @@ void UpdateMusicStream(void) // If no more data to stream, restart music (if loop) if ((!active) && (currentMusic.loop)) { - stb_vorbis_seek_start(currentMusic.stream); - currentMusic.totalSamplesLeft = stb_vorbis_stream_length_in_samples(currentMusic.stream)*currentMusic.channels; - + if(currentMusic.chipTune) + { + currentMusic.totalSamplesLeft = jar_xm_get_remaining_samples(currentMusic.chipctx); + } + else + { + stb_vorbis_seek_start(currentMusic.stream); + currentMusic.totalSamplesLeft = stb_vorbis_stream_length_in_samples(currentMusic.stream)*currentMusic.channels; + } active = BufferMusicStream(buffer); } -- cgit v1.2.3 From 3104d3d6cd3c29b6e240198628110b21882a8fb0 Mon Sep 17 00:00:00 2001 From: Joshua Reisenauer Date: Mon, 25 Apr 2016 22:18:49 -0700 Subject: small fix for streaming There is still an issue where audio will cut off after a brief moment --- src/audio.c | 37 ++++++++++++++++++++----------------- 1 file changed, 20 insertions(+), 17 deletions(-) (limited to 'src') diff --git a/src/audio.c b/src/audio.c index b46c2c20..9472cef6 100644 --- a/src/audio.c +++ b/src/audio.c @@ -567,10 +567,15 @@ void PlayMusicStream(char *fileName) // NOTE: Regularly, we must check if a buffer has been processed and refill it: UpdateMusicStream() currentMusic.totalSamplesLeft = stb_vorbis_stream_length_in_samples(currentMusic.stream) * currentMusic.channels; + currentMusic.totalLengthSeconds = stb_vorbis_stream_length_in_seconds(currentMusic.stream); } } else if (strcmp(GetExtension(fileName),"xm") == 0) { + // Stop current music, clean buffers, unload current stream + StopMusicStream(); + + // new song settings for xm chiptune currentMusic.chipTune = true; currentMusic.channels = 2; currentMusic.sampleRate = 48000; @@ -714,39 +719,37 @@ float GetMusicTimePlayed(void) static bool BufferMusicStream(ALuint buffer) { short pcm[MUSIC_BUFFER_SIZE]; - + int size = 0; // Total size of data steamed (in bytes) - int streamedBytes = 0; // Bytes of data obtained in one samples get - + int streamedBytes = 0; // samples of data obtained, channels are not included in calculation bool active = true; // We can get more data from stream (not finished) if (musicEnabled) { - while (size < MUSIC_BUFFER_SIZE) + if (currentMusic.chipTune) // There is no end of stream for xmfiles, once the end is reached zeros are generated for non looped chiptunes. { - if (currentMusic.chipTune) // There is no end of stream for xmfiles, once the end is reached zeros are generated for non looped chiptunes. - { - int readlen = (MUSIC_BUFFER_SIZE - size) / 2; - jar_xm_generate_samples_16bit(currentMusic.chipctx, pcm + size, readlen); // reads 2*readlen shorts and moves them to buffer+size memory location - streamedBytes = readlen * 4; // Not sure if this is what it needs - } - else + int readlen = MUSIC_BUFFER_SIZE / 2; + jar_xm_generate_samples_16bit(currentMusic.chipctx, pcm, readlen); // reads 2*readlen shorts and moves them to buffer+size memory location + size += readlen * currentMusic.channels; // Not sure if this is what it needs + } + else + { + while (size < MUSIC_BUFFER_SIZE) { streamedBytes = stb_vorbis_get_samples_short_interleaved(currentMusic.stream, currentMusic.channels, pcm + size, MUSIC_BUFFER_SIZE - size); + if (streamedBytes > 0) size += (streamedBytes*currentMusic.channels); + else break; } - - if (streamedBytes > 0) size += (streamedBytes*currentMusic.channels); - else break; } - - //TraceLog(DEBUG, "Streaming music data to buffer. Bytes streamed: %i", size); + TraceLog(DEBUG, "Streaming music data to buffer. Bytes streamed: %i", size); } if (size > 0) { alBufferData(buffer, currentMusic.format, pcm, size*sizeof(short), currentMusic.sampleRate); - currentMusic.totalSamplesLeft -= size; + + if(currentMusic.totalSamplesLeft <= 0) active = false; // end if no more samples left } else { -- cgit v1.2.3 From 299ae7a4bdcddc31281b1e2d0cd2df476755fb79 Mon Sep 17 00:00:00 2001 From: Joshua Reisenauer Date: Tue, 26 Apr 2016 16:50:07 -0700 Subject: new trace logs and optimizations --- src/audio.c | 14 ++++++++++---- src/jar_xm.h | 30 +++++++++++++----------------- 2 files changed, 23 insertions(+), 21 deletions(-) (limited to 'src') diff --git a/src/audio.c b/src/audio.c index 9472cef6..2b8c6d48 100644 --- a/src/audio.c +++ b/src/audio.c @@ -584,12 +584,15 @@ void PlayMusicStream(char *fileName) // only stereo is supported for xm if(!jar_xm_create_context_from_file(¤tMusic.chipctx, currentMusic.sampleRate, fileName)) { - currentMusic.format = AL_FORMAT_STEREO16; // AL_FORMAT_STEREO_FLOAT32; + currentMusic.format = AL_FORMAT_STEREO16; jar_xm_set_max_loop_count(currentMusic.chipctx, 0); // infinite number of loops currentMusic.totalSamplesLeft = jar_xm_get_remaining_samples(currentMusic.chipctx); - currentMusic.totalLengthSeconds = currentMusic.totalSamplesLeft / (currentMusic.sampleRate * currentMusic.channels); + currentMusic.totalLengthSeconds = ((float)currentMusic.totalSamplesLeft) / ((float)currentMusic.sampleRate); musicEnabled = true; + TraceLog(INFO, "[%s] XM number of samples: %i", fileName, currentMusic.totalSamplesLeft); + TraceLog(INFO, "[%s] XM track length: %11.6f sec", fileName, currentMusic.totalLengthSeconds); + // Set up OpenAL alGenSources(1, ¤tMusic.source); alSourcef(currentMusic.source, AL_PITCH, 1); @@ -601,7 +604,10 @@ void PlayMusicStream(char *fileName) BufferMusicStream(currentMusic.buffers[1]); alSourceQueueBuffers(currentMusic.source, 2, currentMusic.buffers); alSourcePlay(currentMusic.source); + + // NOTE: Regularly, we must check if a buffer has been processed and refill it: UpdateMusicStream() } + else TraceLog(WARNING, "[%s] XM file could not be opened", fileName); } else TraceLog(WARNING, "[%s] Music extension not recognized, it can't be loaded", fileName); } @@ -680,7 +686,7 @@ float GetMusicTimeLength(void) float totalSeconds; if (currentMusic.chipTune) { - totalSeconds = currentMusic.totalLengthSeconds; // Not sure if this is the correct value + totalSeconds = currentMusic.totalLengthSeconds; } else { @@ -801,7 +807,7 @@ void UpdateMusicStream(void) { if(currentMusic.chipTune) { - currentMusic.totalSamplesLeft = jar_xm_get_remaining_samples(currentMusic.chipctx); + currentMusic.totalSamplesLeft = currentMusic.totalLengthSeconds * currentMusic.sampleRate; } else { diff --git a/src/jar_xm.h b/src/jar_xm.h index 062b88da..f9ddb511 100644 --- a/src/jar_xm.h +++ b/src/jar_xm.h @@ -121,51 +121,47 @@ void jar_xm_free_context(jar_xm_context_t*); /** Play the module and put the sound samples in an output buffer. * - * @param output buffer of 2*numsamples elements + * @param output buffer of 2*numsamples elements (A left and right value for each sample) * @param numsamples number of samples to generate */ void jar_xm_generate_samples(jar_xm_context_t*, float* output, size_t numsamples); /** Play the module, resample from 32 bit to 16 bit, and put the sound samples in an output buffer. * - * @param output buffer of 2*numsamples elements + * @param output buffer of 2*numsamples elements (A left and right value for each sample) * @param numsamples number of samples to generate */ void jar_xm_generate_samples_16bit(jar_xm_context_t* ctx, short* output, size_t numsamples) { float* musicBuffer = malloc((2*numsamples)*sizeof(float)); - short* musicBuffer2 = malloc((2*numsamples)*sizeof(short)); - jar_xm_generate_samples(ctx, musicBuffer, numsamples); - int x; - for(x=0;x<2*numsamples;x++) - musicBuffer2[x] = musicBuffer[x] * SHRT_MAX; + if(output){ + int x; + for(x=0;x<2*numsamples;x++) + output[x] = musicBuffer[x] * SHRT_MAX; + } - memcpy(output, musicBuffer2, (2*numsamples)*sizeof(short)); free(musicBuffer); - free(musicBuffer2); } /** Play the module, resample from 32 bit to 8 bit, and put the sound samples in an output buffer. * - * @param output buffer of 2*numsamples elements + * @param output buffer of 2*numsamples elements (A left and right value for each sample) * @param numsamples number of samples to generate */ void jar_xm_generate_samples_8bit(jar_xm_context_t* ctx, char* output, size_t numsamples) { float* musicBuffer = malloc((2*numsamples)*sizeof(float)); - char* musicBuffer2 = malloc((2*numsamples)*sizeof(char)); - jar_xm_generate_samples(ctx, musicBuffer, numsamples); - int x; - for(x=0;x<2*numsamples;x++) - musicBuffer2[x] = musicBuffer[x] * CHAR_MAX; + if(output){ + int x; + for(x=0;x<2*numsamples;x++) + output[x] = musicBuffer[x] * CHAR_MAX; + } - memcpy(output, musicBuffer2, (2*numsamples)*sizeof(char)); free(musicBuffer); - free(musicBuffer2); } -- cgit v1.2.3 From f707c1ca468ef3243808ec87a0b6a891f6c6a130 Mon Sep 17 00:00:00 2001 From: Joshua Reisenauer Date: Wed, 27 Apr 2016 00:02:11 -0700 Subject: this should work --- src/audio.c | 2 +- src/raylib.h | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/audio.c b/src/audio.c index 2b8c6d48..0be257d9 100644 --- a/src/audio.c +++ b/src/audio.c @@ -820,7 +820,7 @@ void UpdateMusicStream(void) // Add refilled buffer to queue again... don't let the music stop! alSourceQueueBuffers(currentMusic.source, 1, &buffer); - if (alGetError() != AL_NO_ERROR) TraceLog(WARNING, "Ogg playing, error buffering data..."); + if (alGetError() != AL_NO_ERROR) TraceLog(WARNING, "Error buffering data..."); processed--; } diff --git a/src/raylib.h b/src/raylib.h index de157087..699fbbdb 100644 --- a/src/raylib.h +++ b/src/raylib.h @@ -261,8 +261,9 @@ //---------------------------------------------------------------------------------- #ifndef __cplusplus // Boolean type -#include -//typedef enum { false, true } bool; + #ifndef true + typedef enum { false, true } bool; + #endif #endif // byte type -- cgit v1.2.3 From 91f1f324c0ee3bd57ed778b39fd54d97330dba32 Mon Sep 17 00:00:00 2001 From: Joshua Reisenauer Date: Fri, 29 Apr 2016 23:00:12 -0700 Subject: First stage of audio API update Look over changes and give feedback please. --- src/audio.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++-- src/audio.h | 25 +++++++++++++++++++++++-- src/raylib.h | 19 +++++++++++++++++++ 3 files changed, 92 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/audio.c b/src/audio.c index 0be257d9..ededf4ae 100644 --- a/src/audio.c +++ b/src/audio.c @@ -97,9 +97,10 @@ typedef enum { INFO = 0, ERROR, WARNING, DEBUG, OTHER } TraceLogType; //---------------------------------------------------------------------------------- // Global Variables Definition //---------------------------------------------------------------------------------- +static bool mixChannelsActive_g[4]; // What mix channels are currently active static bool musicEnabled = false; -static Music currentMusic; // Current music loaded - // NOTE: Only one music file playing at a time +static Music currentMusic; // Current music loaded + // NOTE: Only one music file playing at a time //---------------------------------------------------------------------------------- // Module specific Functions Declaration @@ -164,6 +165,53 @@ void CloseAudioDevice(void) alcCloseDevice(device); } +// True if call to InitAudioDevice() was successful and CloseAudioDevice() has not been called yet +bool AudioDeviceReady(void) +{ + ALCcontext *context = alcGetCurrentContext(); + if (context == NULL) return false; + else{ + ALCdevice *device = alcGetContextsDevice(context); + if (device == NULL) return false; + else return true; + } +} + +//---------------------------------------------------------------------------------- +// Module Functions Definition - Custom audio output +//---------------------------------------------------------------------------------- + +// Audio contexts are for outputing custom audio waveforms, This will shut down any other sound sources currently playing +// The mix_t is what mix channel you want to operate on, mixA->mixD are the ones available. Each mix channel can only be used one at a time. +// exmple usage is InitAudioContext(48000, 16, mixA, stereo); +AudioContext* InitAudioContext(unsigned short sampleRate, unsigned char bitsPerSample, mix_t mixChannel, channel_t channels) +{ + if(!AudioDeviceReady()) InitAudioDevice(); + else StopMusicStream(); + + if(!mixChannelsActive_g[mixChannel]){ + AudioContext *ac = malloc(sizeof(AudioContext)); + ac->sampleRate = sampleRate; + ac->bitsPerSample = bitsPerSample; + ac->mixChannel = mixChannel; + ac->channels = channels; + mixChannelsActive_g[mixChannel] = true; + return ac; + } + return NULL; +} + +// Frees buffer in audio context +void CloseAudioContext(AudioContext *ctx) +{ + if(ctx){ + mixChannelsActive_g[ctx->mixChannel] = false; + free(ctx); + } +} + + + //---------------------------------------------------------------------------------- // Module Functions Definition - Sounds loading and playing (.WAV) //---------------------------------------------------------------------------------- diff --git a/src/audio.h b/src/audio.h index ed156532..401edc3e 100644 --- a/src/audio.h +++ b/src/audio.h @@ -41,8 +41,12 @@ //---------------------------------------------------------------------------------- #ifndef __cplusplus // Boolean type -typedef enum { false, true } bool; + #ifndef true + typedef enum { false, true } bool; + #endif #endif +typedef enum { silence, mono, stereo } channel_t; +typedef enum { mixA, mixB, mixC, mixD } mix_t; // Used for mixing/muxing up to four diferent audio streams // Sound source type typedef struct Sound { @@ -59,6 +63,16 @@ typedef struct Wave { short channels; } Wave; +// Audio Context, used to create custom audio streams that are not bound to a sound file. There can be +// no more than 4 concurrent audio contexts in use. This is due to each active context being tied to +// a dedicated mix channel. +typedef struct AudioContext { + unsigned short sampleRate; // default is 48000 + unsigned char bitsPerSample; // 16 is default + mix_t mixChannel; // 0-3 or mixA-mixD, each mix channel can receive up to one dedicated audio stream + channel_t channels; // 1=mono, 2=stereo +} AudioContext; + #ifdef __cplusplus extern "C" { // Prevents name mangling of functions #endif @@ -73,6 +87,13 @@ extern "C" { // Prevents name mangling of functions //---------------------------------------------------------------------------------- void InitAudioDevice(void); // Initialize audio device and context void CloseAudioDevice(void); // Close the audio device and context (and music stream) +bool AudioDeviceReady(void); // True if call to InitAudioDevice() was successful and CloseAudioDevice() has not been called yet + +// Audio contexts are for outputing custom audio waveforms, This will shut down any other sound sources currently playing +// The mix_t is what mix channel you want to operate on, mixA->mixD are the ones available. Each mix channel can only be used one at a time. +// exmple usage is InitAudioContext(48000, 16, mixA, stereo); +AudioContext* InitAudioContext(unsigned short sampleRate, unsigned char bitsPerSample, mix_t mixChannel, channel_t channels); +void CloseAudioContext(AudioContext *ctx); // Frees audio context Sound LoadSound(char *fileName); // Load sound to memory Sound LoadSoundFromWave(Wave wave); // Load sound to memory from wave data @@ -92,7 +113,7 @@ void PauseMusicStream(void); // Pause music p void ResumeMusicStream(void); // Resume playing paused music bool MusicIsPlaying(void); // Check if music is playing void SetMusicVolume(float volume); // Set volume for music (1.0 is max level) -float GetMusicTimeLength(void); // Get current music time length (in seconds) +float GetMusicTimeLength(void); // Get music time length (in seconds) float GetMusicTimePlayed(void); // Get current music time played (in seconds) #ifdef __cplusplus diff --git a/src/raylib.h b/src/raylib.h index 699fbbdb..1721c009 100644 --- a/src/raylib.h +++ b/src/raylib.h @@ -265,6 +265,8 @@ typedef enum { false, true } bool; #endif #endif +typedef enum { silence, mono, stereo } channel_t; +typedef enum { mixA, mixB, mixC, mixD } mix_t; // Used for mixing/muxing up to four diferent audio streams // byte type typedef unsigned char byte; @@ -446,6 +448,16 @@ typedef struct Wave { short channels; } Wave; +// Audio Context, used to create custom audio streams that are not bound to a sound file. There can be +// no more than 4 concurrent audio contexts in use. This is due to each active context being tied to +// a dedicated mix channel. +typedef struct AudioContext { + unsigned short sampleRate; // default is 48000 + unsigned char bitsPerSample; // 16 is default + mix_t mixChannel; // 0-3 or mixA-mixD, each mix channel can receive up to one dedicated audio stream + channel_t channels; // 1=mono, 2=stereo +} AudioContext; + // Texture formats // NOTE: Support depends on OpenGL version and platform typedef enum { @@ -863,6 +875,13 @@ void DrawPhysicObjectInfo(PhysicObject *pObj, Vector2 position, int fontSize); //------------------------------------------------------------------------------------ void InitAudioDevice(void); // Initialize audio device and context void CloseAudioDevice(void); // Close the audio device and context (and music stream) +bool AudioDeviceReady(void); // True if call to InitAudioDevice() was successful and CloseAudioDevice() has not been called yet + +// Audio contexts are for outputing custom audio waveforms, This will shut down any other sound sources currently playing +// The mix_t is what mix channel you want to operate on, mixA->mixD are the ones available. Each mix channel can only be used one at a time. +// exmple usage is InitAudioContext(48000, 16, mixA, stereo); +AudioContext* InitAudioContext(unsigned short sampleRate, unsigned char bitsPerSample, mix_t mixChannel, channel_t channels); +void CloseAudioContext(AudioContext *ctx); // Frees audio context Sound LoadSound(char *fileName); // Load sound to memory Sound LoadSoundFromWave(Wave wave); // Load sound to memory from wave data -- cgit v1.2.3 From 5f1e8b827822ded6d24d71f75e1f1d0d301dbb2d Mon Sep 17 00:00:00 2001 From: Joshua Reisenauer Date: Fri, 29 Apr 2016 23:43:21 -0700 Subject: hide struct from user Hiding the struct from user should protect from accidentally modifying the mix channel. This could cause serious errors down the road. --- src/audio.c | 37 +++++++++++++++++++++++++++---------- src/audio.h | 12 ++++-------- src/raylib.h | 12 ++++-------- 3 files changed, 35 insertions(+), 26 deletions(-) (limited to 'src') diff --git a/src/audio.c b/src/audio.c index ededf4ae..2c0ed3de 100644 --- a/src/audio.c +++ b/src/audio.c @@ -90,6 +90,16 @@ typedef struct Music { bool chipTune; // True if chiptune is loaded } Music; +// Audio Context, used to create custom audio streams that are not bound to a sound file. There can be +// no more than 4 concurrent audio contexts in use. This is due to each active context being tied to +// a dedicated mix channel. +typedef struct AudioContext_t { + unsigned short sampleRate; // default is 48000 + unsigned char bitsPerSample; // 16 is default + mix_t mixChannel; // 0-3 or mixA-mixD, each mix channel can receive up to one dedicated audio stream + channel_t channels; // 1=mono, 2=stereo +} AudioContext_t; + #if defined(AUDIO_STANDALONE) typedef enum { INFO = 0, ERROR, WARNING, DEBUG, OTHER } TraceLogType; #endif @@ -97,10 +107,10 @@ typedef enum { INFO = 0, ERROR, WARNING, DEBUG, OTHER } TraceLogType; //---------------------------------------------------------------------------------- // Global Variables Definition //---------------------------------------------------------------------------------- -static bool mixChannelsActive_g[4]; // What mix channels are currently active +static AudioContext_t* mixChannelsActive_g[4]; // What mix channels are currently active static bool musicEnabled = false; -static Music currentMusic; // Current music loaded - // NOTE: Only one music file playing at a time +static Music currentMusic; // Current music loaded + // NOTE: Only one music file playing at a time //---------------------------------------------------------------------------------- // Module specific Functions Declaration @@ -184,32 +194,39 @@ bool AudioDeviceReady(void) // Audio contexts are for outputing custom audio waveforms, This will shut down any other sound sources currently playing // The mix_t is what mix channel you want to operate on, mixA->mixD are the ones available. Each mix channel can only be used one at a time. // exmple usage is InitAudioContext(48000, 16, mixA, stereo); -AudioContext* InitAudioContext(unsigned short sampleRate, unsigned char bitsPerSample, mix_t mixChannel, channel_t channels) +AudioContext InitAudioContext(unsigned short sampleRate, unsigned char bitsPerSample, mix_t mixChannel, channel_t channels) { if(!AudioDeviceReady()) InitAudioDevice(); else StopMusicStream(); if(!mixChannelsActive_g[mixChannel]){ - AudioContext *ac = malloc(sizeof(AudioContext)); + AudioContext_t *ac = malloc(sizeof(AudioContext_t)); ac->sampleRate = sampleRate; ac->bitsPerSample = bitsPerSample; ac->mixChannel = mixChannel; ac->channels = channels; - mixChannelsActive_g[mixChannel] = true; + mixChannelsActive_g[mixChannel] = ac; return ac; } return NULL; } // Frees buffer in audio context -void CloseAudioContext(AudioContext *ctx) +void CloseAudioContext(AudioContext ctx) { - if(ctx){ - mixChannelsActive_g[ctx->mixChannel] = false; - free(ctx); + AudioContext_t *context = (AudioContext_t*)ctx; + if(context){ + mixChannelsActive_g[context->mixChannel] = NULL; + free(context); } } +// Pushes more audio data into context mix channel, if none are ever pushed then zeros are fed in +void UpdateAudioContext(AudioContext ctx, void *data, unsigned short *dataLength) +{ + ; +} + //---------------------------------------------------------------------------------- diff --git a/src/audio.h b/src/audio.h index 401edc3e..cadf2bc1 100644 --- a/src/audio.h +++ b/src/audio.h @@ -66,12 +66,7 @@ typedef struct Wave { // Audio Context, used to create custom audio streams that are not bound to a sound file. There can be // no more than 4 concurrent audio contexts in use. This is due to each active context being tied to // a dedicated mix channel. -typedef struct AudioContext { - unsigned short sampleRate; // default is 48000 - unsigned char bitsPerSample; // 16 is default - mix_t mixChannel; // 0-3 or mixA-mixD, each mix channel can receive up to one dedicated audio stream - channel_t channels; // 1=mono, 2=stereo -} AudioContext; +typedef void* AudioContext; #ifdef __cplusplus extern "C" { // Prevents name mangling of functions @@ -92,8 +87,9 @@ bool AudioDeviceReady(void); // True if call // Audio contexts are for outputing custom audio waveforms, This will shut down any other sound sources currently playing // The mix_t is what mix channel you want to operate on, mixA->mixD are the ones available. Each mix channel can only be used one at a time. // exmple usage is InitAudioContext(48000, 16, mixA, stereo); -AudioContext* InitAudioContext(unsigned short sampleRate, unsigned char bitsPerSample, mix_t mixChannel, channel_t channels); -void CloseAudioContext(AudioContext *ctx); // Frees audio context +AudioContext InitAudioContext(unsigned short sampleRate, unsigned char bitsPerSample, mix_t mixChannel, channel_t channels); +void CloseAudioContext(AudioContext ctx); // Frees audio context +void UpdateAudioContext(AudioContext ctx, void *data, unsigned short *dataLength); // Pushes more audio data into context mix channel, if none are ever pushed then zeros are fed in Sound LoadSound(char *fileName); // Load sound to memory Sound LoadSoundFromWave(Wave wave); // Load sound to memory from wave data diff --git a/src/raylib.h b/src/raylib.h index 1721c009..5b7b34fd 100644 --- a/src/raylib.h +++ b/src/raylib.h @@ -451,12 +451,7 @@ typedef struct Wave { // Audio Context, used to create custom audio streams that are not bound to a sound file. There can be // no more than 4 concurrent audio contexts in use. This is due to each active context being tied to // a dedicated mix channel. -typedef struct AudioContext { - unsigned short sampleRate; // default is 48000 - unsigned char bitsPerSample; // 16 is default - mix_t mixChannel; // 0-3 or mixA-mixD, each mix channel can receive up to one dedicated audio stream - channel_t channels; // 1=mono, 2=stereo -} AudioContext; +typedef void* AudioContext; // Texture formats // NOTE: Support depends on OpenGL version and platform @@ -880,8 +875,9 @@ bool AudioDeviceReady(void); // True if call // Audio contexts are for outputing custom audio waveforms, This will shut down any other sound sources currently playing // The mix_t is what mix channel you want to operate on, mixA->mixD are the ones available. Each mix channel can only be used one at a time. // exmple usage is InitAudioContext(48000, 16, mixA, stereo); -AudioContext* InitAudioContext(unsigned short sampleRate, unsigned char bitsPerSample, mix_t mixChannel, channel_t channels); -void CloseAudioContext(AudioContext *ctx); // Frees audio context +AudioContext InitAudioContext(unsigned short sampleRate, unsigned char bitsPerSample, mix_t mixChannel, channel_t channels); +void CloseAudioContext(AudioContext ctx); // Frees audio context +void UpdateAudioContext(AudioContext ctx, void *data, unsigned short *dataLength); // Pushes more audio data into context mix channel, if none are ever pushed then zeros are fed in Sound LoadSound(char *fileName); // Load sound to memory Sound LoadSoundFromWave(Wave wave); // Load sound to memory from wave data -- cgit v1.2.3 From a1038f61b60ad21c936af783facc91fe01607be3 Mon Sep 17 00:00:00 2001 From: Joshua Reisenauer Date: Sat, 30 Apr 2016 15:41:46 -0700 Subject: BPS type added to ensure consistency --- src/audio.c | 42 ++++++++++++++++++++++++++++++++++++++---- src/audio.h | 7 ++++--- src/raylib.h | 7 ++++--- 3 files changed, 46 insertions(+), 10 deletions(-) (limited to 'src') diff --git a/src/audio.c b/src/audio.c index 2c0ed3de..9d1aa014 100644 --- a/src/audio.c +++ b/src/audio.c @@ -95,9 +95,12 @@ typedef struct Music { // a dedicated mix channel. typedef struct AudioContext_t { unsigned short sampleRate; // default is 48000 - unsigned char bitsPerSample; // 16 is default + BPS bitsPerSample; // 16 is default mix_t mixChannel; // 0-3 or mixA-mixD, each mix channel can receive up to one dedicated audio stream channel_t channels; // 1=mono, 2=stereo + ALenum alFormat; // openAL format specifier + ALuint alSource; // openAL source + ALuint alBuffer[2]; // openAL sample buffer } AudioContext_t; #if defined(AUDIO_STANDALONE) @@ -193,8 +196,8 @@ bool AudioDeviceReady(void) // Audio contexts are for outputing custom audio waveforms, This will shut down any other sound sources currently playing // The mix_t is what mix channel you want to operate on, mixA->mixD are the ones available. Each mix channel can only be used one at a time. -// exmple usage is InitAudioContext(48000, 16, mixA, stereo); -AudioContext InitAudioContext(unsigned short sampleRate, unsigned char bitsPerSample, mix_t mixChannel, channel_t channels) +// exmple usage is InitAudioContext(48000, sixteenBPS, mixA, stereo); +AudioContext InitAudioContext(unsigned short sampleRate, BPS bitsPerSample, mix_t mixChannel, channel_t channels) { if(!AudioDeviceReady()) InitAudioDevice(); else StopMusicStream(); @@ -206,6 +209,30 @@ AudioContext InitAudioContext(unsigned short sampleRate, unsigned char bitsPerSa ac->mixChannel = mixChannel; ac->channels = channels; mixChannelsActive_g[mixChannel] = ac; + + // setup openAL format + if (channels == mono) + { + if (bitsPerSample == eightBPS ) ac->alFormat = AL_FORMAT_MONO8; + else if (bitsPerSample == sixteenBPS) ac->alFormat = AL_FORMAT_MONO16; + } + else if (channels == stereo) + { + if (bitsPerSample == eightBPS ) ac->alFormat = AL_FORMAT_STEREO8; + else if (bitsPerSample == sixteenBPS) ac->alFormat = AL_FORMAT_STEREO16; + } + + // Create an audio source + alGenSources(1, &ac->alSource); + alSourcef(ac->alSource, AL_PITCH, 1); + alSourcef(ac->alSource, AL_GAIN, 1); + alSource3f(ac->alSource, AL_POSITION, 0, 0, 0); + alSource3f(ac->alSource, AL_VELOCITY, 0, 0, 0); + + // Create Buffer + alGenBuffers(2, &ac->alBuffer); + + return ac; } return NULL; @@ -216,15 +243,22 @@ void CloseAudioContext(AudioContext ctx) { AudioContext_t *context = (AudioContext_t*)ctx; if(context){ + alDeleteSources(1, &context->alSource); + alDeleteBuffers(2, &context->alBuffer); mixChannelsActive_g[context->mixChannel] = NULL; free(context); + ctx = NULL; } } // Pushes more audio data into context mix channel, if none are ever pushed then zeros are fed in void UpdateAudioContext(AudioContext ctx, void *data, unsigned short *dataLength) { - ; + AudioContext_t *context = (AudioContext_t*)ctx; + if(!musicEnabled && context && mixChannelsActive_g[context->mixChannel] == context) + { + ; + } } diff --git a/src/audio.h b/src/audio.h index cadf2bc1..28fe4b3c 100644 --- a/src/audio.h +++ b/src/audio.h @@ -45,8 +45,9 @@ typedef enum { false, true } bool; #endif #endif -typedef enum { silence, mono, stereo } channel_t; -typedef enum { mixA, mixB, mixC, mixD } mix_t; // Used for mixing/muxing up to four diferent audio streams +typedef enum { silence, mono, stereo } channel_t; // number of audio sources per sample +typedef enum { mixA, mixB, mixC, mixD } mix_t; // Used for mixing/muxing up to four diferent audio samples +typedef enum { eightBPS, sixteenBPS } BPS; // Either 8 or 16 bit quality samples // Sound source type typedef struct Sound { @@ -87,7 +88,7 @@ bool AudioDeviceReady(void); // True if call // Audio contexts are for outputing custom audio waveforms, This will shut down any other sound sources currently playing // The mix_t is what mix channel you want to operate on, mixA->mixD are the ones available. Each mix channel can only be used one at a time. // exmple usage is InitAudioContext(48000, 16, mixA, stereo); -AudioContext InitAudioContext(unsigned short sampleRate, unsigned char bitsPerSample, mix_t mixChannel, channel_t channels); +AudioContext InitAudioContext(unsigned short sampleRate, BPS bitsPerSample, mix_t mixChannel, channel_t channels); void CloseAudioContext(AudioContext ctx); // Frees audio context void UpdateAudioContext(AudioContext ctx, void *data, unsigned short *dataLength); // Pushes more audio data into context mix channel, if none are ever pushed then zeros are fed in diff --git a/src/raylib.h b/src/raylib.h index 5b7b34fd..83f58a51 100644 --- a/src/raylib.h +++ b/src/raylib.h @@ -265,8 +265,9 @@ typedef enum { false, true } bool; #endif #endif -typedef enum { silence, mono, stereo } channel_t; -typedef enum { mixA, mixB, mixC, mixD } mix_t; // Used for mixing/muxing up to four diferent audio streams +typedef enum { silence, mono, stereo } channel_t; // number of audio sources per sample +typedef enum { mixA, mixB, mixC, mixD } mix_t; // Used for mixing/muxing up to four diferent audio samples +typedef enum { eightBPS, sixteenBPS } BPS; // Either 8 or 16 bit quality samples // byte type typedef unsigned char byte; @@ -875,7 +876,7 @@ bool AudioDeviceReady(void); // True if call // Audio contexts are for outputing custom audio waveforms, This will shut down any other sound sources currently playing // The mix_t is what mix channel you want to operate on, mixA->mixD are the ones available. Each mix channel can only be used one at a time. // exmple usage is InitAudioContext(48000, 16, mixA, stereo); -AudioContext InitAudioContext(unsigned short sampleRate, unsigned char bitsPerSample, mix_t mixChannel, channel_t channels); +AudioContext InitAudioContext(unsigned short sampleRate, BPS bitsPerSample, mix_t mixChannel, channel_t channels); void CloseAudioContext(AudioContext ctx); // Frees audio context void UpdateAudioContext(AudioContext ctx, void *data, unsigned short *dataLength); // Pushes more audio data into context mix channel, if none are ever pushed then zeros are fed in -- cgit v1.2.3 From 34e5fcf47e99deecc9954fd848130c61119e2e93 Mon Sep 17 00:00:00 2001 From: Joshua Reisenauer Date: Sat, 30 Apr 2016 16:05:43 -0700 Subject: removed enums --- src/audio.c | 36 +++++++++++++++++++----------------- src/audio.h | 13 +++++-------- src/raylib.h | 13 +++++-------- 3 files changed, 29 insertions(+), 33 deletions(-) (limited to 'src') diff --git a/src/audio.c b/src/audio.c index 9d1aa014..8a8ca4ef 100644 --- a/src/audio.c +++ b/src/audio.c @@ -59,6 +59,7 @@ // Defines and Macros //---------------------------------------------------------------------------------- #define MUSIC_STREAM_BUFFERS 2 +#define MAX_AUDIO_CONTEXTS 4 #if defined(PLATFORM_RPI) || defined(PLATFORM_ANDROID) // NOTE: On RPI and Android should be lower to avoid frame-stalls @@ -95,9 +96,9 @@ typedef struct Music { // a dedicated mix channel. typedef struct AudioContext_t { unsigned short sampleRate; // default is 48000 - BPS bitsPerSample; // 16 is default - mix_t mixChannel; // 0-3 or mixA-mixD, each mix channel can receive up to one dedicated audio stream - channel_t channels; // 1=mono, 2=stereo + unsigned char bitsPerSample; // 16 is default + unsigned char mixChannel; // 0-3 or mixA-mixD, each mix channel can receive up to one dedicated audio stream + unsigned char channels; // 1=mono, 2=stereo ALenum alFormat; // openAL format specifier ALuint alSource; // openAL source ALuint alBuffer[2]; // openAL sample buffer @@ -110,10 +111,10 @@ typedef enum { INFO = 0, ERROR, WARNING, DEBUG, OTHER } TraceLogType; //---------------------------------------------------------------------------------- // Global Variables Definition //---------------------------------------------------------------------------------- -static AudioContext_t* mixChannelsActive_g[4]; // What mix channels are currently active +static AudioContext_t* mixChannelsActive_g[MAX_AUDIO_CONTEXTS]; // What mix channels are currently active static bool musicEnabled = false; -static Music currentMusic; // Current music loaded - // NOTE: Only one music file playing at a time +static Music currentMusic; // Current music loaded + // NOTE: Only one music file playing at a time //---------------------------------------------------------------------------------- // Module specific Functions Declaration @@ -179,7 +180,7 @@ void CloseAudioDevice(void) } // True if call to InitAudioDevice() was successful and CloseAudioDevice() has not been called yet -bool AudioDeviceReady(void) +bool IsAudioDeviceReady(void) { ALCcontext *context = alcGetCurrentContext(); if (context == NULL) return false; @@ -195,11 +196,12 @@ bool AudioDeviceReady(void) //---------------------------------------------------------------------------------- // Audio contexts are for outputing custom audio waveforms, This will shut down any other sound sources currently playing -// The mix_t is what mix channel you want to operate on, mixA->mixD are the ones available. Each mix channel can only be used one at a time. -// exmple usage is InitAudioContext(48000, sixteenBPS, mixA, stereo); -AudioContext InitAudioContext(unsigned short sampleRate, BPS bitsPerSample, mix_t mixChannel, channel_t channels) +// The mixChannel is what mix channel you want to operate on, 0-3 are the ones available. Each mix channel can only be used one at a time. +// exmple usage is InitAudioContext(48000, 16, 0, 2); // stereo, mixchannel 1, 16bit, 48khz +AudioContext InitAudioContext(unsigned short sampleRate, unsigned char bitsPerSample, unsigned char mixChannel, unsigned char channels) { - if(!AudioDeviceReady()) InitAudioDevice(); + if(mixChannel > MAX_AUDIO_CONTEXTS) return NULL; + if(!IsAudioDeviceReady()) InitAudioDevice(); else StopMusicStream(); if(!mixChannelsActive_g[mixChannel]){ @@ -211,15 +213,15 @@ AudioContext InitAudioContext(unsigned short sampleRate, BPS bitsPerSample, mix_ mixChannelsActive_g[mixChannel] = ac; // setup openAL format - if (channels == mono) + if (channels == 1) { - if (bitsPerSample == eightBPS ) ac->alFormat = AL_FORMAT_MONO8; - else if (bitsPerSample == sixteenBPS) ac->alFormat = AL_FORMAT_MONO16; + if (bitsPerSample == 8 ) ac->alFormat = AL_FORMAT_MONO8; + else if (bitsPerSample == 16) ac->alFormat = AL_FORMAT_MONO16; } - else if (channels == stereo) + else if (channels == 2) { - if (bitsPerSample == eightBPS ) ac->alFormat = AL_FORMAT_STEREO8; - else if (bitsPerSample == sixteenBPS) ac->alFormat = AL_FORMAT_STEREO16; + if (bitsPerSample == 8 ) ac->alFormat = AL_FORMAT_STEREO8; + else if (bitsPerSample == 16) ac->alFormat = AL_FORMAT_STEREO16; } // Create an audio source diff --git a/src/audio.h b/src/audio.h index 28fe4b3c..9c681044 100644 --- a/src/audio.h +++ b/src/audio.h @@ -45,9 +45,6 @@ typedef enum { false, true } bool; #endif #endif -typedef enum { silence, mono, stereo } channel_t; // number of audio sources per sample -typedef enum { mixA, mixB, mixC, mixD } mix_t; // Used for mixing/muxing up to four diferent audio samples -typedef enum { eightBPS, sixteenBPS } BPS; // Either 8 or 16 bit quality samples // Sound source type typedef struct Sound { @@ -83,13 +80,13 @@ extern "C" { // Prevents name mangling of functions //---------------------------------------------------------------------------------- void InitAudioDevice(void); // Initialize audio device and context void CloseAudioDevice(void); // Close the audio device and context (and music stream) -bool AudioDeviceReady(void); // True if call to InitAudioDevice() was successful and CloseAudioDevice() has not been called yet +bool IsAudioDeviceReady(void); // True if call to InitAudioDevice() was successful and CloseAudioDevice() has not been called yet // Audio contexts are for outputing custom audio waveforms, This will shut down any other sound sources currently playing -// The mix_t is what mix channel you want to operate on, mixA->mixD are the ones available. Each mix channel can only be used one at a time. -// exmple usage is InitAudioContext(48000, 16, mixA, stereo); -AudioContext InitAudioContext(unsigned short sampleRate, BPS bitsPerSample, mix_t mixChannel, channel_t channels); -void CloseAudioContext(AudioContext ctx); // Frees audio context +// The mixChannel is what mix channel you want to operate on, 0-3 are the ones available. Each mix channel can only be used one at a time. +// exmple usage is InitAudioContext(48000, 16, 0, 2); // stereo, mixchannel 1, 16bit, 48khz +AudioContext InitAudioContext(unsigned short sampleRate, unsigned char bitsPerSample, unsigned char mixChannel, unsigned char channels); +void CloseAudioContext(AudioContext ctx); // Frees audio context void UpdateAudioContext(AudioContext ctx, void *data, unsigned short *dataLength); // Pushes more audio data into context mix channel, if none are ever pushed then zeros are fed in Sound LoadSound(char *fileName); // Load sound to memory diff --git a/src/raylib.h b/src/raylib.h index 83f58a51..8390d176 100644 --- a/src/raylib.h +++ b/src/raylib.h @@ -265,9 +265,6 @@ typedef enum { false, true } bool; #endif #endif -typedef enum { silence, mono, stereo } channel_t; // number of audio sources per sample -typedef enum { mixA, mixB, mixC, mixD } mix_t; // Used for mixing/muxing up to four diferent audio samples -typedef enum { eightBPS, sixteenBPS } BPS; // Either 8 or 16 bit quality samples // byte type typedef unsigned char byte; @@ -871,13 +868,13 @@ void DrawPhysicObjectInfo(PhysicObject *pObj, Vector2 position, int fontSize); //------------------------------------------------------------------------------------ void InitAudioDevice(void); // Initialize audio device and context void CloseAudioDevice(void); // Close the audio device and context (and music stream) -bool AudioDeviceReady(void); // True if call to InitAudioDevice() was successful and CloseAudioDevice() has not been called yet +bool IsAudioDeviceReady(void); // True if call to InitAudioDevice() was successful and CloseAudioDevice() has not been called yet // Audio contexts are for outputing custom audio waveforms, This will shut down any other sound sources currently playing -// The mix_t is what mix channel you want to operate on, mixA->mixD are the ones available. Each mix channel can only be used one at a time. -// exmple usage is InitAudioContext(48000, 16, mixA, stereo); -AudioContext InitAudioContext(unsigned short sampleRate, BPS bitsPerSample, mix_t mixChannel, channel_t channels); -void CloseAudioContext(AudioContext ctx); // Frees audio context +// The mixChannel is what mix channel you want to operate on, 0-3 are the ones available. Each mix channel can only be used one at a time. +// exmple usage is InitAudioContext(48000, 16, 0, 2); // stereo, mixchannel 1, 16bit, 48khz +AudioContext InitAudioContext(unsigned short sampleRate, unsigned char bitsPerSample, unsigned char mixChannel, unsigned char channels); +void CloseAudioContext(AudioContext ctx); // Frees audio context void UpdateAudioContext(AudioContext ctx, void *data, unsigned short *dataLength); // Pushes more audio data into context mix channel, if none are ever pushed then zeros are fed in Sound LoadSound(char *fileName); // Load sound to memory -- cgit v1.2.3 From 1fb874cdc5be59c510b46fd8b1a10b458ad3fb45 Mon Sep 17 00:00:00 2001 From: Ray Date: Sun, 1 May 2016 01:09:48 +0200 Subject: Check for WebGL/Webkit extensions Improve DXT-ETC1 support on HTML5 --- src/rlgl.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/rlgl.c b/src/rlgl.c index 06efd777..5b181a86 100644 --- a/src/rlgl.c +++ b/src/rlgl.c @@ -969,10 +969,12 @@ void rlglInit(void) // DDS texture compression support if ((strcmp(extList[i], (const char *)"GL_EXT_texture_compression_s3tc") == 0) || + (strcmp(extList[i], (const char *)"GL_WEBGL_compressed_texture_s3tc") == 0) || (strcmp(extList[i], (const char *)"GL_WEBKIT_WEBGL_compressed_texture_s3tc") == 0)) texCompDXTSupported = true; // ETC1 texture compression support - if (strcmp(extList[i], (const char *)"GL_OES_compressed_ETC1_RGB8_texture") == 0) texCompETC1Supported = true; + if ((strcmp(extList[i], (const char *)"GL_OES_compressed_ETC1_RGB8_texture") == 0) || + (strcmp(extList[i], (const char *)"GL_WEBGL_compressed_texture_etc1") == 0)) texCompETC1Supported = true; // ETC2/EAC texture compression support if (strcmp(extList[i], (const char *)"GL_ARB_ES3_compatibility") == 0) texCompETC2Supported = true; -- cgit v1.2.3 From 0e6d1cb272435c670a991169f413c98516dce15d Mon Sep 17 00:00:00 2001 From: Ray Date: Mon, 2 May 2016 00:16:32 +0200 Subject: Working on materials system... --- src/raylib.h | 21 +++++++++------------ src/rlgl.c | 14 +++++--------- 2 files changed, 14 insertions(+), 21 deletions(-) (limited to 'src') diff --git a/src/raylib.h b/src/raylib.h index 8390d176..8af13cb4 100644 --- a/src/raylib.h +++ b/src/raylib.h @@ -355,7 +355,7 @@ typedef struct Camera { // Camera2D type, defines a 2d camera typedef struct Camera2D { Vector2 offset; // Camera offset (displacement from target) - Vector2 target; // Camera target (for rotation and zoom) + Vector2 target; // Camera target (rotation and zoom origin) float rotation; // Camera rotation in degrees float zoom; // Camera zoom (scaling), should be 1.0f by default } Camera2D; @@ -386,16 +386,17 @@ typedef struct Mesh { typedef struct Shader { unsigned int id; // Shader program id - // Variable attributes + // Variable attributes locations int vertexLoc; // Vertex attribute location point (vertex shader) int texcoordLoc; // Texcoord attribute location point (vertex shader) int normalLoc; // Normal attribute location point (vertex shader) int colorLoc; // Color attibute location point (vertex shader) - // Uniforms + // Uniform locations int mvpLoc; // ModelView-Projection matrix uniform location point (vertex shader) int tintColorLoc; // Color uniform location point (fragment shader) + // Texture map locations int mapDiffuseLoc; // Diffuse map texture uniform location point (fragment shader) int mapNormalLoc; // Normal map texture uniform location point (fragment shader) int mapSpecularLoc; // Specular map texture uniform location point (fragment shader) @@ -832,18 +833,14 @@ Vector3 ResolveCollisionCubicmap(Image cubicmap, Vector3 mapPosition, Vector3 *p Shader LoadShader(char *vsFileName, char *fsFileName); // Load a custom shader and bind default locations unsigned int LoadShaderProgram(char *vShaderStr, char *fShaderStr); // Load custom shaders strings and return program id void UnloadShader(Shader shader); // Unload a custom shader from memory -void SetCustomShader(Shader shader); // Set custom shader to be used in batch draw void SetDefaultShader(void); // Set default shader to be used in batch draw +void SetCustomShader(Shader shader); // Set custom shader to be used in batch draw void SetModelShader(Model *model, Shader shader); // Link a shader to a model -int GetShaderLocation(Shader shader, const char *uniformName); // Get shader uniform location -void SetShaderValue(Shader shader, int uniformLoc, float *value, int size); // Set shader uniform value (float) -void SetShaderValuei(Shader shader, int uniformLoc, int *value, int size); // Set shader uniform value (int) -void SetShaderValueMatrix(Shader shader, int uniformLoc, Matrix mat); // Set shader uniform value (matrix 4x4) -//void SetShaderMapDiffuse(Shader *shader, Texture2D texture); // Default diffuse shader map texture assignment -//void SetShaderMapNormal(Shader *shader, const char *uniformName, Texture2D texture); // Normal map texture shader assignment -//void SetShaderMapSpecular(Shader *shader, const char *uniformName, Texture2D texture); // Specular map texture shader assignment -//void SetShaderMap(Shader *shader, int mapLocation, Texture2D texture, int textureUnit); // TODO: Generic shader map assignment +int GetShaderLocation(Shader shader, const char *uniformName); // Get shader uniform location +void SetShaderValue(Shader shader, int uniformLoc, float *value, int size); // Set shader uniform value (float) +void SetShaderValuei(Shader shader, int uniformLoc, int *value, int size); // Set shader uniform value (int) +void SetShaderValueMatrix(Shader shader, int uniformLoc, Matrix mat); // Set shader uniform value (matrix 4x4) void SetBlendMode(int mode); // Set blending mode (alpha, additive, multiplied) diff --git a/src/rlgl.c b/src/rlgl.c index 5b181a86..b1b020ec 100644 --- a/src/rlgl.c +++ b/src/rlgl.c @@ -1350,14 +1350,14 @@ void rlglDrawModel(Model model, Vector3 position, Vector3 rotationAxis, float ro glBindTexture(GL_TEXTURE_2D, model.material.texDiffuse.id); glUniform1i(model.material.shader.mapDiffuseLoc, 0); // Texture fits in active texture unit 0 - if (model.material.texNormal.id != 0) + if ((model.material.texNormal.id != 0) && (model.material.shader.mapNormalLoc != -1)) { glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, model.material.texNormal.id); glUniform1i(model.material.shader.mapNormalLoc, 1); // Texture fits in active texture unit 1 } - if (model.material.texSpecular.id != 0) + if ((model.material.texSpecular.id != 0) && (model.material.shader.mapSpecularLoc != -1)) { glActiveTexture(GL_TEXTURE2); glBindTexture(GL_TEXTURE_2D, model.material.texSpecular.id); @@ -2125,17 +2125,13 @@ Shader LoadShader(char *vsFileName, char *fsFileName) // After shader loading, we try to load default location names if (shader.id != 0) LoadDefaultShaderLocations(&shader); - else - { - TraceLog(WARNING, "Custom shader could not be loaded"); - shader = defaultShader; - } // Shader strings must be freed free(vShaderStr); free(fShaderStr); } - else + + if (shader.id == 0) { TraceLog(WARNING, "Custom shader could not be loaded"); shader = defaultShader; @@ -2494,7 +2490,7 @@ static Shader LoadDefaultShader(void) if (shader.id != 0) TraceLog(INFO, "[SHDR ID %i] Default shader loaded successfully", shader.id); else TraceLog(WARNING, "[SHDR ID %i] Default shader could not be loaded", shader.id); - LoadDefaultShaderLocations(&shader); + if (shader.id != 0) LoadDefaultShaderLocations(&shader); return shader; } -- cgit v1.2.3 From fa98289ddb5fc190a100249c667d5e9814f4d864 Mon Sep 17 00:00:00 2001 From: Ray Date: Mon, 2 May 2016 00:37:33 +0200 Subject: Added 2D camera mode functions Removed BeginDrawingEx() Added Begin2dMode() and End2dMode() --- src/core.c | 44 +++++++++++++++++++++++++++----------------- src/raylib.h | 3 ++- 2 files changed, 29 insertions(+), 18 deletions(-) (limited to 'src') diff --git a/src/core.c b/src/core.c index 7b1d5960..dd904692 100644 --- a/src/core.c +++ b/src/core.c @@ -524,23 +524,6 @@ void BeginDrawing(void) // NOTE: Not required with OpenGL 3.3+ } -// Setup drawing canvas with 2d camera -void BeginDrawingEx(Camera2D camera) -{ - BeginDrawing(); - - // Camera rotation and scaling is always relative to target - Matrix matOrigin = MatrixTranslate(-camera.target.x, -camera.target.y, 0.0f); - Matrix matRotation = MatrixRotate((Vector3){ 0.0f, 0.0f, 1.0f }, camera.rotation*DEG2RAD); - Matrix matScale = MatrixScale(camera.zoom, camera.zoom, 1.0f); - - Matrix matTranslation = MatrixTranslate(camera.offset.x + camera.target.x, camera.offset.y + camera.target.y, 0.0f); - - Matrix matTransform = MatrixMultiply(MatrixMultiply(matOrigin, MatrixMultiply(matScale, matRotation)), matTranslation); - - rlMultMatrixf(MatrixToFloat(matTransform)); -} - // End canvas drawing and Swap Buffers (Double Buffering) void EndDrawing(void) { @@ -569,6 +552,33 @@ void EndDrawing(void) } } +// Initialize 2D mode with custom camera +void Begin2dMode(Camera2D camera) +{ + rlglDraw(); // Draw Buffers (Only OpenGL 3+ and ES2) + + rlLoadIdentity(); // Reset current matrix (MODELVIEW) + + // Camera rotation and scaling is always relative to target + Matrix matOrigin = MatrixTranslate(-camera.target.x, -camera.target.y, 0.0f); + Matrix matRotation = MatrixRotate((Vector3){ 0.0f, 0.0f, 1.0f }, camera.rotation*DEG2RAD); + Matrix matScale = MatrixScale(camera.zoom, camera.zoom, 1.0f); + + Matrix matTranslation = MatrixTranslate(camera.offset.x + camera.target.x, camera.offset.y + camera.target.y, 0.0f); + + Matrix matTransform = MatrixMultiply(MatrixMultiply(matOrigin, MatrixMultiply(matScale, matRotation)), matTranslation); + + rlMultMatrixf(MatrixToFloat(matTransform)); +} + +// Ends 2D mode custom camera usage +void End2dMode(void) +{ + rlglDraw(); // Draw Buffers (Only OpenGL 3+ and ES2) + + rlLoadIdentity(); // Reset current matrix (MODELVIEW) +} + // Initializes 3D mode for drawing (Camera setup) void Begin3dMode(Camera camera) { diff --git a/src/raylib.h b/src/raylib.h index 8af13cb4..337b9813 100644 --- a/src/raylib.h +++ b/src/raylib.h @@ -576,9 +576,10 @@ void DisableCursor(void); // Disables cursor void ClearBackground(Color color); // Sets Background Color void BeginDrawing(void); // Setup drawing canvas to start drawing -void BeginDrawingEx(Camera2D camera); // Setup drawing canvas with 2d camera void EndDrawing(void); // End canvas drawing and Swap Buffers (Double Buffering) +void Begin2dMode(Camera2D camera); // Initialize 2D mode with custom camera +void End2dMode(void); // Ends 2D mode custom camera usage void Begin3dMode(Camera camera); // Initializes 3D mode for drawing (Camera setup) void End3dMode(void); // Ends 3D mode and returns to default 2D orthographic mode void BeginTextureMode(RenderTexture2D target); // Initializes render texture for drawing -- cgit v1.2.3 From 17732fa9c493bffb8e05cb5db45d998d2b151cb9 Mon Sep 17 00:00:00 2001 From: Ray Date: Mon, 2 May 2016 00:38:01 +0200 Subject: Corrected warning with array --- src/audio.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/audio.c b/src/audio.c index 8a8ca4ef..39d1ee8b 100644 --- a/src/audio.c +++ b/src/audio.c @@ -232,7 +232,7 @@ AudioContext InitAudioContext(unsigned short sampleRate, unsigned char bitsPerSa alSource3f(ac->alSource, AL_VELOCITY, 0, 0, 0); // Create Buffer - alGenBuffers(2, &ac->alBuffer); + alGenBuffers(2, ac->alBuffer); return ac; @@ -246,7 +246,7 @@ void CloseAudioContext(AudioContext ctx) AudioContext_t *context = (AudioContext_t*)ctx; if(context){ alDeleteSources(1, &context->alSource); - alDeleteBuffers(2, &context->alBuffer); + alDeleteBuffers(2, context->alBuffer); mixChannelsActive_g[context->mixChannel] = NULL; free(context); ctx = NULL; -- cgit v1.2.3 From a2a3d3aeb60dd117b02fc8c7c85ec428175a5f83 Mon Sep 17 00:00:00 2001 From: Joshua Reisenauer Date: Sun, 1 May 2016 18:53:40 -0700 Subject: new silence generator --- src/audio.c | 74 ++++++++++++++++++++++++++++++++++++++++++------------------ src/audio.h | 7 +++--- src/raylib.h | 7 +++--- 3 files changed, 60 insertions(+), 28 deletions(-) (limited to 'src') diff --git a/src/audio.c b/src/audio.c index 8a8ca4ef..7100a6c1 100644 --- a/src/audio.c +++ b/src/audio.c @@ -37,6 +37,7 @@ #include "AL/al.h" // OpenAL basic header #include "AL/alc.h" // OpenAL context header (like OpenGL, OpenAL requires a context to work) +#include "AL/alext.h" // extensions for other format types #include // Declares malloc() and free() for memory management #include // Required for strcmp() @@ -93,12 +94,10 @@ typedef struct Music { // Audio Context, used to create custom audio streams that are not bound to a sound file. There can be // no more than 4 concurrent audio contexts in use. This is due to each active context being tied to -// a dedicated mix channel. +// a dedicated mix channel. All audio is 32bit floating point in stereo. typedef struct AudioContext_t { unsigned short sampleRate; // default is 48000 - unsigned char bitsPerSample; // 16 is default unsigned char mixChannel; // 0-3 or mixA-mixD, each mix channel can receive up to one dedicated audio stream - unsigned char channels; // 1=mono, 2=stereo ALenum alFormat; // openAL format specifier ALuint alSource; // openAL source ALuint alBuffer[2]; // openAL sample buffer @@ -126,6 +125,9 @@ static void UnloadWave(Wave wave); // Unload wave data static bool BufferMusicStream(ALuint buffer); // Fill music buffers with data static void EmptyMusicStream(void); // Empty music buffers +// fill buffer with zeros +static void FillAlBufferWithSilence(AudioContext_t *ac, ALuint buffer); + #if defined(AUDIO_STANDALONE) const char *GetExtension(const char *fileName); // Get the extension for a filename void TraceLog(int msgType, const char *text, ...); // Outputs a trace log message (INFO, ERROR, WARNING) @@ -197,8 +199,9 @@ bool IsAudioDeviceReady(void) // Audio contexts are for outputing custom audio waveforms, This will shut down any other sound sources currently playing // The mixChannel is what mix channel you want to operate on, 0-3 are the ones available. Each mix channel can only be used one at a time. -// exmple usage is InitAudioContext(48000, 16, 0, 2); // stereo, mixchannel 1, 16bit, 48khz -AudioContext InitAudioContext(unsigned short sampleRate, unsigned char bitsPerSample, unsigned char mixChannel, unsigned char channels) +// exmple usage is InitAudioContext(48000, 0); // mixchannel 1, 48khz +// all samples are floating point stereo by default +AudioContext InitAudioContext(unsigned short sampleRate, unsigned char mixChannel) { if(mixChannel > MAX_AUDIO_CONTEXTS) return NULL; if(!IsAudioDeviceReady()) InitAudioDevice(); @@ -207,22 +210,11 @@ AudioContext InitAudioContext(unsigned short sampleRate, unsigned char bitsPerSa if(!mixChannelsActive_g[mixChannel]){ AudioContext_t *ac = malloc(sizeof(AudioContext_t)); ac->sampleRate = sampleRate; - ac->bitsPerSample = bitsPerSample; ac->mixChannel = mixChannel; - ac->channels = channels; mixChannelsActive_g[mixChannel] = ac; // setup openAL format - if (channels == 1) - { - if (bitsPerSample == 8 ) ac->alFormat = AL_FORMAT_MONO8; - else if (bitsPerSample == 16) ac->alFormat = AL_FORMAT_MONO16; - } - else if (channels == 2) - { - if (bitsPerSample == 8 ) ac->alFormat = AL_FORMAT_STEREO8; - else if (bitsPerSample == 16) ac->alFormat = AL_FORMAT_STEREO16; - } + ac->alFormat = AL_FORMAT_STEREO_FLOAT32; // Create an audio source alGenSources(1, &ac->alSource); @@ -232,7 +224,14 @@ AudioContext InitAudioContext(unsigned short sampleRate, unsigned char bitsPerSa alSource3f(ac->alSource, AL_VELOCITY, 0, 0, 0); // Create Buffer - alGenBuffers(2, &ac->alBuffer); + alGenBuffers(2, ac->alBuffer); + + //fill buffers + FillAlBufferWithSilence(ac, ac->alBuffer[0]); + FillAlBufferWithSilence(ac, ac->alBuffer[1]); + alSourceQueueBuffers(ac->alSource, 2, ac->alBuffer); + alSourcei(ac->alSource, AL_LOOPING, AL_FALSE); // this could cause errors + alSourcePlay(ac->alSource); return ac; @@ -245,8 +244,20 @@ void CloseAudioContext(AudioContext ctx) { AudioContext_t *context = (AudioContext_t*)ctx; if(context){ + alSourceStop(context->alSource); + + //flush out all queued buffers + ALuint buffer = 0; + int queued = 0; + alGetSourcei(context->alSource, AL_BUFFERS_QUEUED, &queued); + while (queued > 0) + { + alSourceUnqueueBuffers(context->alSource, 1, &buffer); + queued--; + } + alDeleteSources(1, &context->alSource); - alDeleteBuffers(2, &context->alBuffer); + alDeleteBuffers(2, context->alBuffer); mixChannelsActive_g[context->mixChannel] = NULL; free(context); ctx = NULL; @@ -254,15 +265,34 @@ void CloseAudioContext(AudioContext ctx) } // Pushes more audio data into context mix channel, if none are ever pushed then zeros are fed in -void UpdateAudioContext(AudioContext ctx, void *data, unsigned short *dataLength) +// Call "UpdateAudioContext(ctx, NULL, 0)" every game tick if you want to pause the audio +void UpdateAudioContext(AudioContext ctx, float *data, unsigned short dataLength) { AudioContext_t *context = (AudioContext_t*)ctx; - if(!musicEnabled && context && mixChannelsActive_g[context->mixChannel] == context) + if (context && mixChannelsActive_g[context->mixChannel] == context) { - ; + ALint processed = 0; + ALuint buffer = 0; + alGetSourcei(context->alSource, AL_BUFFERS_PROCESSED, &processed); // Get the number of already processed buffers (if any) + + if (!data || !dataLength)// play silence + while (processed > 0) + { + alSourceUnqueueBuffers(context->alSource, 1, &buffer); + FillAlBufferWithSilence(context, buffer); + alSourceQueueBuffers(context->alSource, 1, &buffer); + processed--; + } } } +// fill buffer with zeros +static void FillAlBufferWithSilence(AudioContext_t *ac, ALuint buffer) +{ + float pcm[MUSIC_BUFFER_SIZE] = {0.f}; + alBufferData(buffer, ac->alFormat, pcm, MUSIC_BUFFER_SIZE*sizeof(float), ac->sampleRate); +} + //---------------------------------------------------------------------------------- diff --git a/src/audio.h b/src/audio.h index 9c681044..9037a843 100644 --- a/src/audio.h +++ b/src/audio.h @@ -84,10 +84,11 @@ bool IsAudioDeviceReady(void); // True if call // Audio contexts are for outputing custom audio waveforms, This will shut down any other sound sources currently playing // The mixChannel is what mix channel you want to operate on, 0-3 are the ones available. Each mix channel can only be used one at a time. -// exmple usage is InitAudioContext(48000, 16, 0, 2); // stereo, mixchannel 1, 16bit, 48khz -AudioContext InitAudioContext(unsigned short sampleRate, unsigned char bitsPerSample, unsigned char mixChannel, unsigned char channels); +// exmple usage is InitAudioContext(48000, 0); // mixchannel 1, 48khz +// all samples are floating point stereo by default +AudioContext InitAudioContext(unsigned short sampleRate, unsigned char mixChannel); void CloseAudioContext(AudioContext ctx); // Frees audio context -void UpdateAudioContext(AudioContext ctx, void *data, unsigned short *dataLength); // Pushes more audio data into context mix channel, if none are ever pushed then zeros are fed in +void UpdateAudioContext(AudioContext ctx, float *data, unsigned short dataLength); // Pushes more audio data into context mix channel, if NULL is passed to data then zeros are played Sound LoadSound(char *fileName); // Load sound to memory Sound LoadSoundFromWave(Wave wave); // Load sound to memory from wave data diff --git a/src/raylib.h b/src/raylib.h index 8390d176..f4ea875e 100644 --- a/src/raylib.h +++ b/src/raylib.h @@ -872,10 +872,11 @@ bool IsAudioDeviceReady(void); // True if call // Audio contexts are for outputing custom audio waveforms, This will shut down any other sound sources currently playing // The mixChannel is what mix channel you want to operate on, 0-3 are the ones available. Each mix channel can only be used one at a time. -// exmple usage is InitAudioContext(48000, 16, 0, 2); // stereo, mixchannel 1, 16bit, 48khz -AudioContext InitAudioContext(unsigned short sampleRate, unsigned char bitsPerSample, unsigned char mixChannel, unsigned char channels); +// exmple usage is InitAudioContext(48000, 0); // mixchannel 1, 48khz +// all samples are floating point stereo by default +AudioContext InitAudioContext(unsigned short sampleRate, unsigned char mixChannel); void CloseAudioContext(AudioContext ctx); // Frees audio context -void UpdateAudioContext(AudioContext ctx, void *data, unsigned short *dataLength); // Pushes more audio data into context mix channel, if none are ever pushed then zeros are fed in +void UpdateAudioContext(AudioContext ctx, float *data, unsigned short dataLength); // Pushes more audio data into context mix channel, if NULL is passed to data then zeros are played Sound LoadSound(char *fileName); // Load sound to memory Sound LoadSoundFromWave(Wave wave); // Load sound to memory from wave data -- cgit v1.2.3 From 790bc7280685d714ddce3e1ee0afa11cee0c5e06 Mon Sep 17 00:00:00 2001 From: Joshua Reisenauer Date: Sun, 1 May 2016 23:07:02 -0700 Subject: bool return for failed update --- src/audio.c | 9 +++++++-- src/audio.h | 2 +- src/raylib.h | 2 +- 3 files changed, 9 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/audio.c b/src/audio.c index 7100a6c1..7b42b089 100644 --- a/src/audio.c +++ b/src/audio.c @@ -256,6 +256,7 @@ void CloseAudioContext(AudioContext ctx) queued--; } + //delete source and buffers alDeleteSources(1, &context->alSource); alDeleteBuffers(2, context->alBuffer); mixChannelsActive_g[context->mixChannel] = NULL; @@ -266,7 +267,8 @@ void CloseAudioContext(AudioContext ctx) // Pushes more audio data into context mix channel, if none are ever pushed then zeros are fed in // Call "UpdateAudioContext(ctx, NULL, 0)" every game tick if you want to pause the audio -void UpdateAudioContext(AudioContext ctx, float *data, unsigned short dataLength) +// Returns true if data was pushed onto queue, otherwise if queue is full then no data is added and false is returned +bool UpdateAudioContext(AudioContext ctx, float *data, unsigned short dataLength) { AudioContext_t *context = (AudioContext_t*)ctx; if (context && mixChannelsActive_g[context->mixChannel] == context) @@ -274,7 +276,9 @@ void UpdateAudioContext(AudioContext ctx, float *data, unsigned short dataLength ALint processed = 0; ALuint buffer = 0; alGetSourcei(context->alSource, AL_BUFFERS_PROCESSED, &processed); // Get the number of already processed buffers (if any) - + + if(!processed) return false;//nothing to process, queue is still full + if (!data || !dataLength)// play silence while (processed > 0) { @@ -283,6 +287,7 @@ void UpdateAudioContext(AudioContext ctx, float *data, unsigned short dataLength alSourceQueueBuffers(context->alSource, 1, &buffer); processed--; } + return true; } } diff --git a/src/audio.h b/src/audio.h index 9037a843..4a198c59 100644 --- a/src/audio.h +++ b/src/audio.h @@ -88,7 +88,7 @@ bool IsAudioDeviceReady(void); // True if call // all samples are floating point stereo by default AudioContext InitAudioContext(unsigned short sampleRate, unsigned char mixChannel); void CloseAudioContext(AudioContext ctx); // Frees audio context -void UpdateAudioContext(AudioContext ctx, float *data, unsigned short dataLength); // Pushes more audio data into context mix channel, if NULL is passed to data then zeros are played +bool UpdateAudioContext(AudioContext ctx, float *data, unsigned short dataLength); // Pushes more audio data into context mix channel, if NULL is passed to data then zeros are played Sound LoadSound(char *fileName); // Load sound to memory Sound LoadSoundFromWave(Wave wave); // Load sound to memory from wave data diff --git a/src/raylib.h b/src/raylib.h index ade581d3..9c5a8258 100644 --- a/src/raylib.h +++ b/src/raylib.h @@ -874,7 +874,7 @@ bool IsAudioDeviceReady(void); // True if call // all samples are floating point stereo by default AudioContext InitAudioContext(unsigned short sampleRate, unsigned char mixChannel); void CloseAudioContext(AudioContext ctx); // Frees audio context -void UpdateAudioContext(AudioContext ctx, float *data, unsigned short dataLength); // Pushes more audio data into context mix channel, if NULL is passed to data then zeros are played +bool UpdateAudioContext(AudioContext ctx, float *data, unsigned short dataLength); // Pushes more audio data into context mix channel, if NULL is passed to data then zeros are played Sound LoadSound(char *fileName); // Load sound to memory Sound LoadSoundFromWave(Wave wave); // Load sound to memory from wave data -- cgit v1.2.3 From 9ef0240e99e7e6a626fdb8fee4f8a81eea21f3e2 Mon Sep 17 00:00:00 2001 From: Joshua Reisenauer Date: Mon, 2 May 2016 01:24:24 -0700 Subject: resamples added Ease of use considered in api and channels are more convenient as unsigned char type. --- src/audio.c | 49 +++++++++++++++++++++++++++++++++++++++++++------ src/audio.h | 6 +++--- src/raylib.h | 6 +++--- 3 files changed, 49 insertions(+), 12 deletions(-) (limited to 'src') diff --git a/src/audio.c b/src/audio.c index 7b42b089..d935b6c7 100644 --- a/src/audio.c +++ b/src/audio.c @@ -97,6 +97,7 @@ typedef struct Music { // a dedicated mix channel. All audio is 32bit floating point in stereo. typedef struct AudioContext_t { unsigned short sampleRate; // default is 48000 + unsigned char channels; // 1=mono,2=stereo unsigned char mixChannel; // 0-3 or mixA-mixD, each mix channel can receive up to one dedicated audio stream ALenum alFormat; // openAL format specifier ALuint alSource; // openAL source @@ -125,8 +126,9 @@ static void UnloadWave(Wave wave); // Unload wave data static bool BufferMusicStream(ALuint buffer); // Fill music buffers with data static void EmptyMusicStream(void); // Empty music buffers -// fill buffer with zeros -static void FillAlBufferWithSilence(AudioContext_t *ac, ALuint buffer); +static void FillAlBufferWithSilence(AudioContext_t *ac, ALuint buffer);// fill buffer with zeros +static void ResampleShortToFloat(short *shorts, float *floats, unsigned short len); // pass two arrays of the same legnth in +static void ResampleByteToFloat(char *chars, float *floats, unsigned short len); // pass two arrays of same length in #if defined(AUDIO_STANDALONE) const char *GetExtension(const char *fileName); // Get the extension for a filename @@ -199,9 +201,9 @@ bool IsAudioDeviceReady(void) // Audio contexts are for outputing custom audio waveforms, This will shut down any other sound sources currently playing // The mixChannel is what mix channel you want to operate on, 0-3 are the ones available. Each mix channel can only be used one at a time. -// exmple usage is InitAudioContext(48000, 0); // mixchannel 1, 48khz -// all samples are floating point stereo by default -AudioContext InitAudioContext(unsigned short sampleRate, unsigned char mixChannel) +// exmple usage is InitAudioContext(48000, 0, 2); // mixchannel 1, 48khz, stereo +// all samples are floating point by default +AudioContext InitAudioContext(unsigned short sampleRate, unsigned char mixChannel, unsigned char channels) { if(mixChannel > MAX_AUDIO_CONTEXTS) return NULL; if(!IsAudioDeviceReady()) InitAudioDevice(); @@ -210,11 +212,15 @@ AudioContext InitAudioContext(unsigned short sampleRate, unsigned char mixChanne if(!mixChannelsActive_g[mixChannel]){ AudioContext_t *ac = malloc(sizeof(AudioContext_t)); ac->sampleRate = sampleRate; + ac->channels = channels; ac->mixChannel = mixChannel; mixChannelsActive_g[mixChannel] = ac; // setup openAL format - ac->alFormat = AL_FORMAT_STEREO_FLOAT32; + if(channels == 1) + ac->alFormat = AL_FORMAT_MONO_FLOAT32; + else + ac->alFormat = AL_FORMAT_STEREO_FLOAT32; // Create an audio source alGenSources(1, &ac->alSource); @@ -289,6 +295,7 @@ bool UpdateAudioContext(AudioContext ctx, float *data, unsigned short dataLength } return true; } + return false; } // fill buffer with zeros @@ -298,6 +305,36 @@ static void FillAlBufferWithSilence(AudioContext_t *ac, ALuint buffer) alBufferData(buffer, ac->alFormat, pcm, MUSIC_BUFFER_SIZE*sizeof(float), ac->sampleRate); } +// example usage: +// short sh[3] = {1,2,3};float fl[3]; +// ResampleShortToFloat(sh,fl,3); +static void ResampleShortToFloat(short *shorts, float *floats, unsigned short len) +{ + int x; + for(x=0;x Date: Mon, 2 May 2016 14:11:42 +0200 Subject: Removed debug functions --- src/rlgl.c | 12 ------------ 1 file changed, 12 deletions(-) (limited to 'src') diff --git a/src/rlgl.c b/src/rlgl.c index b1b020ec..e021e726 100644 --- a/src/rlgl.c +++ b/src/rlgl.c @@ -2377,18 +2377,6 @@ void SetBlendMode(int mode) } } -#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) -void PrintProjectionMatrix(void) -{ - PrintMatrix(projection); -} - -void PrintModelviewMatrix(void) -{ - PrintMatrix(modelview); -} -#endif - //---------------------------------------------------------------------------------- // Module specific Functions Definition //---------------------------------------------------------------------------------- -- cgit v1.2.3 From f2152aa3915be867907c569667828d10d2f1bdd6 Mon Sep 17 00:00:00 2001 From: Ray Date: Mon, 2 May 2016 14:11:57 +0200 Subject: Reorganize functions --- src/core.c | 853 ++++++++++++++++++++++++++++++------------------------------- 1 file changed, 426 insertions(+), 427 deletions(-) (limited to 'src') diff --git a/src/core.c b/src/core.c index dd904692..b27712a7 100644 --- a/src/core.c +++ b/src/core.c @@ -244,23 +244,16 @@ extern void UnloadDefaultFont(void); // [Module: text] Unloads defaul //---------------------------------------------------------------------------------- static void InitDisplay(int width, int height); // Initialize display device and framebuffer static void InitGraphics(void); // Initialize OpenGL graphics +static void SetupFramebufferSize(int displayWidth, int displayHeight); static void InitTimer(void); // Initialize timer static double GetTime(void); // Returns time since InitTimer() was run static bool GetKeyStatus(int key); // Returns if a key has been pressed static bool GetMouseButtonStatus(int button); // Returns if a mouse button has been pressed -static void SwapBuffers(void); // Copy back buffer to front buffers static void PollInputEvents(void); // Register user events +static void SwapBuffers(void); // Copy back buffer to front buffers static void LogoAnimation(void); // Plays raylib logo appearing animation -static void SetupFramebufferSize(int displayWidth, int displayHeight); - -#if defined(PLATFORM_RPI) -static void InitKeyboard(void); // Init raw keyboard system (standard input reading) -static void ProcessKeyboard(void); // Process keyboard events -static void RestoreKeyboard(void); // Restore keyboard system -static void InitMouse(void); // Mouse initialization (including mouse thread) -static void *MouseThread(void *arg); // Mouse reading thread -static void InitGamepad(void); // Init raw gamepad input -static void *GamepadThread(void *arg); // Mouse reading thread +#if defined(PLATFORM_DESKTOP) || defined(PLATFORM_RPI) +static void TakeScreenshot(void); // Takes a screenshot and saves it in the same folder as executable #endif #if defined(PLATFORM_DESKTOP) || defined(PLATFORM_WEB) @@ -279,10 +272,6 @@ static void WindowIconifyCallback(GLFWwindow *window, int iconified); 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) -static void TakeScreenshot(void); // Takes a screenshot and saves it in the same folder as executable -#endif - #if defined(PLATFORM_ANDROID) static void AndroidCommandCallback(struct android_app *app, int32_t cmd); // Process Android activity lifecycle commands static int32_t AndroidInputCallback(struct android_app *app, AInputEvent *event); // Process Android inputs @@ -293,6 +282,16 @@ static EM_BOOL EmscriptenFullscreenChangeCallback(int eventType, const Emscripte static EM_BOOL EmscriptenInputCallback(int eventType, const EmscriptenTouchEvent *touchEvent, void *userData); #endif +#if defined(PLATFORM_RPI) +static void InitKeyboard(void); // Init raw keyboard system (standard input reading) +static void ProcessKeyboard(void); // Process keyboard events +static void RestoreKeyboard(void); // Restore keyboard system +static void InitMouse(void); // Mouse initialization (including mouse thread) +static void *MouseThread(void *arg); // Mouse reading thread +static void InitGamepad(void); // Init raw gamepad input +static void *GamepadThread(void *arg); // Mouse reading thread +#endif + //---------------------------------------------------------------------------------- // Module Functions Definition - Window and OpenGL Context Functions //---------------------------------------------------------------------------------- @@ -1734,143 +1733,376 @@ static void InitGraphics(void) #endif } -#if defined(PLATFORM_DESKTOP) || defined(PLATFORM_WEB) -// GLFW3 Error Callback, runs on GLFW3 error -static void ErrorCallback(int error, const char *description) +// Compute framebuffer size relative to screen size and display size +// NOTE: Global variables renderWidth/renderHeight can be modified +static void SetupFramebufferSize(int displayWidth, int displayHeight) { - TraceLog(WARNING, "[GLFW3 Error] Code: %i Decription: %s", error, description); -} + // TODO: SetupFramebufferSize() does not consider properly display video modes. + // It setups a renderWidth/renderHeight with black bars that could not match a valid video mode, + // and so, framebuffer is not scaled properly to some monitors. + + // Calculate renderWidth and renderHeight, we have the display size (input params) and the desired screen size (global var) + if ((screenWidth > displayWidth) || (screenHeight > displayHeight)) + { + TraceLog(WARNING, "DOWNSCALING: Required screen size (%ix%i) is bigger than display size (%ix%i)", screenWidth, screenHeight, displayWidth, displayHeight); -// GLFW3 Srolling Callback, runs on mouse wheel -static void ScrollCallback(GLFWwindow *window, double xoffset, double yoffset) -{ - currentMouseWheelY = (int)yoffset; -} + // Downscaling to fit display with border-bars + float widthRatio = (float)displayWidth/(float)screenWidth; + float heightRatio = (float)displayHeight/(float)screenHeight; -// GLFW3 Keyboard Callback, runs on key pressed -static void KeyCallback(GLFWwindow *window, int key, int scancode, int action, int mods) -{ - if (key == exitKey && action == GLFW_PRESS) - { - glfwSetWindowShouldClose(window, GL_TRUE); + if (widthRatio <= heightRatio) + { + renderWidth = displayWidth; + renderHeight = (int)round((float)screenHeight*widthRatio); + renderOffsetX = 0; + renderOffsetY = (displayHeight - renderHeight); + } + else + { + renderWidth = (int)round((float)screenWidth*heightRatio); + renderHeight = displayHeight; + renderOffsetX = (displayWidth - renderWidth); + renderOffsetY = 0; + } - // NOTE: Before closing window, while loop must be left! + // NOTE: downscale matrix required! + float scaleRatio = (float)renderWidth/(float)screenWidth; + + downscaleView = MatrixScale(scaleRatio, scaleRatio, scaleRatio); + + // NOTE: We render to full display resolution! + // We just need to calculate above parameters for downscale matrix and offsets + renderWidth = displayWidth; + renderHeight = displayHeight; + + TraceLog(WARNING, "Downscale matrix generated, content will be rendered at: %i x %i", renderWidth, renderHeight); } -#if defined(PLATFORM_DESKTOP) - else if (key == GLFW_KEY_F12 && action == GLFW_PRESS) + else if ((screenWidth < displayWidth) || (screenHeight < displayHeight)) { - TakeScreenshot(); + // Required screen size is smaller than display size + TraceLog(INFO, "UPSCALING: Required screen size: %i x %i -> Display size: %i x %i", screenWidth, screenHeight, displayWidth, displayHeight); + + // Upscaling to fit display with border-bars + float displayRatio = (float)displayWidth/(float)displayHeight; + float screenRatio = (float)screenWidth/(float)screenHeight; + + if (displayRatio <= screenRatio) + { + renderWidth = screenWidth; + renderHeight = (int)round((float)screenWidth/displayRatio); + renderOffsetX = 0; + renderOffsetY = (renderHeight - screenHeight); + } + else + { + renderWidth = (int)round((float)screenHeight*displayRatio); + renderHeight = screenHeight; + renderOffsetX = (renderWidth - screenWidth); + renderOffsetY = 0; + } } -#endif - else + else // screen == display { - currentKeyState[key] = action; - if (action == GLFW_PRESS) lastKeyPressed = key; + renderWidth = screenWidth; + renderHeight = screenHeight; + renderOffsetX = 0; + renderOffsetY = 0; } } -// GLFW3 Mouse Button Callback, runs on mouse button pressed -static void MouseButtonCallback(GLFWwindow *window, int button, int action, int mods) +// Initialize hi-resolution timer +static void InitTimer(void) { - currentMouseState[button] = action; - -#define ENABLE_MOUSE_GESTURES -#if defined(ENABLE_MOUSE_GESTURES) - // Process mouse events as touches to be able to use mouse-gestures - GestureEvent gestureEvent; - - // Register touch actions - if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON)) gestureEvent.touchAction = TOUCH_DOWN; - else if (IsMouseButtonReleased(MOUSE_LEFT_BUTTON)) gestureEvent.touchAction = TOUCH_UP; - - // NOTE: TOUCH_MOVE event is registered in MouseCursorPosCallback() - - // Assign a pointer ID - gestureEvent.pointerId[0] = 0; - - // Register touch points count - gestureEvent.pointCount = 1; - - // Register touch points position, only one point registered - gestureEvent.position[0] = GetMousePosition(); - - // Normalize gestureEvent.position[0] for screenWidth and screenHeight - gestureEvent.position[0].x /= (float)GetScreenWidth(); - gestureEvent.position[0].y /= (float)GetScreenHeight(); - - // Gesture data is sent to gestures system for processing - ProcessGestureEvent(gestureEvent); + srand(time(NULL)); // Initialize random seed + +#if defined(PLATFORM_ANDROID) || defined(PLATFORM_RPI) + struct timespec now; + + if (clock_gettime(CLOCK_MONOTONIC, &now) == 0) // Success + { + baseTime = (uint64_t)now.tv_sec*1000000000LLU + (uint64_t)now.tv_nsec; + } + else TraceLog(WARNING, "No hi-resolution timer available"); #endif + + previousTime = GetTime(); // Get time as double } -// GLFW3 Cursor Position Callback, runs on mouse move -static void MouseCursorPosCallback(GLFWwindow *window, double x, double y) +// Get current time measure (in seconds) since InitTimer() +static double GetTime(void) { -#define ENABLE_MOUSE_GESTURES -#if defined(ENABLE_MOUSE_GESTURES) - // Process mouse events as touches to be able to use mouse-gestures - GestureEvent gestureEvent; - - gestureEvent.touchAction = TOUCH_MOVE; - - // Assign a pointer ID - gestureEvent.pointerId[0] = 0; - - // Register touch points count - gestureEvent.pointCount = 1; - - // Register touch points position, only one point registered - gestureEvent.position[0] = (Vector2){ (float)x, (float)y }; - - touchPosition[0] = gestureEvent.position[0]; - - // Normalize gestureEvent.position[0] for screenWidth and screenHeight - gestureEvent.position[0].x /= (float)GetScreenWidth(); - gestureEvent.position[0].y /= (float)GetScreenHeight(); +#if defined(PLATFORM_DESKTOP) || defined(PLATFORM_WEB) + return glfwGetTime(); +#elif defined(PLATFORM_ANDROID) || defined(PLATFORM_RPI) + struct timespec ts; + clock_gettime(CLOCK_MONOTONIC, &ts); + uint64_t time = (uint64_t)ts.tv_sec*1000000000LLU + (uint64_t)ts.tv_nsec; - // Gesture data is sent to gestures system for processing - ProcessGestureEvent(gestureEvent); + return (double)(time - baseTime)*1e-9; #endif } -// GLFW3 Char Key Callback, runs on key pressed (get char value) -static void CharCallback(GLFWwindow *window, unsigned int key) +// Get one key state +static bool GetKeyStatus(int key) { - lastKeyPressed = key; - - //TraceLog(INFO, "Char Callback Key pressed: %i\n", key); +#if defined(PLATFORM_DESKTOP) || defined(PLATFORM_WEB) + return glfwGetKey(window, key); +#elif defined(PLATFORM_ANDROID) + // TODO: Check for virtual keyboard + return false; +#elif defined(PLATFORM_RPI) + // NOTE: Keys states are filled in PollInputEvents() + if (key < 0 || key > 511) return false; + else return currentKeyState[key]; +#endif } -// GLFW3 CursorEnter Callback, when cursor enters the window -static void CursorEnterCallback(GLFWwindow *window, int enter) +// Get one mouse button state +static bool GetMouseButtonStatus(int button) { - if (enter == true) cursorOnScreen = true; - else cursorOnScreen = false; +#if defined(PLATFORM_DESKTOP) || defined(PLATFORM_WEB) + return glfwGetMouseButton(window, button); +#elif defined(PLATFORM_ANDROID) + // TODO: Check for virtual mouse + return false; +#elif defined(PLATFORM_RPI) + // NOTE: Mouse buttons states are filled in PollInputEvents() + return currentMouseState[button]; +#endif } -// GLFW3 WindowSize Callback, runs when window is resized -// NOTE: Window resizing not allowed by default -static void WindowSizeCallback(GLFWwindow *window, int width, int height) +// Poll (store) all input events +static void PollInputEvents(void) { - // If window is resized, graphics device is re-initialized (but only ortho mode) - rlglInitGraphics(0, 0, width, height); - - // Window size must be updated to be used on 3D mode to get new aspect ratio (Begin3dMode()) - screenWidth = width; - screenHeight = height; - renderWidth = width; - renderHeight = height; + // NOTE: Gestures update must be called every frame to reset gestures correctly + // because ProcessGestureEvent() is just called on an event, not every frame + UpdateGestures(); - // NOTE: Postprocessing texture is not scaled to new size +#if defined(PLATFORM_DESKTOP) || defined(PLATFORM_WEB) + // Mouse input polling + double mouseX; + double mouseY; - // Background must be also re-cleared - ClearBackground(RAYWHITE); -} + glfwGetCursorPos(window, &mouseX, &mouseY); -// GLFW3 WindowIconify Callback, runs when window is minimized/restored -static void WindowIconifyCallback(GLFWwindow* window, int iconified) -{ - if (iconified) windowMinimized = true; // The window was iconified + mousePosition.x = (float)mouseX; + mousePosition.y = (float)mouseY; + + // Keyboard input polling (automatically managed by GLFW3 through callback) + lastKeyPressed = -1; + + // Register previous keys states + for (int i = 0; i < 512; i++) previousKeyState[i] = currentKeyState[i]; + + // Register previous mouse states + for (int i = 0; i < 3; i++) previousMouseState[i] = currentMouseState[i]; + + previousMouseWheelY = currentMouseWheelY; + currentMouseWheelY = 0; + + glfwPollEvents(); // Register keyboard/mouse events... and window events! +#elif defined(PLATFORM_ANDROID) + + // Register previous keys states + for (int i = 0; i < 128; i++) previousButtonState[i] = currentButtonState[i]; + + // Poll Events (registered events) + // NOTE: Activity is paused if not enabled (appEnabled) + while ((ident = ALooper_pollAll(appEnabled ? 0 : -1, NULL, &events,(void**)&source)) >= 0) + { + // Process this event + if (source != NULL) source->process(app, source); + + // NOTE: Never close window, native activity is controlled by the system! + if (app->destroyRequested != 0) + { + //TraceLog(INFO, "Closing Window..."); + //windowShouldClose = true; + //ANativeActivity_finish(app->activity); + } + } +#elif defined(PLATFORM_RPI) + + // NOTE: Mouse input events polling is done asynchonously in another pthread - MouseThread() + + // NOTE: Keyboard reading could be done using input_event(s) reading or just read from stdin, + // we use method 2 (stdin) but maybe in a future we should change to method 1... + ProcessKeyboard(); + + // NOTE: Gamepad (Joystick) input events polling is done asynchonously in another pthread - GamepadThread() + +#endif +} + +// Copy back buffer to front buffers +static void SwapBuffers(void) +{ +#if defined(PLATFORM_DESKTOP) || defined(PLATFORM_WEB) + glfwSwapBuffers(window); +#elif defined(PLATFORM_ANDROID) || defined(PLATFORM_RPI) + eglSwapBuffers(display, surface); +#endif +} + +#if defined(PLATFORM_DESKTOP) || defined(PLATFORM_RPI) +// Takes a screenshot and saves it in the same folder as executable +static void TakeScreenshot(void) +{ + static int shotNum = 0; // Screenshot number, increments every screenshot take during program execution + char buffer[20]; // Buffer to store file name + + unsigned char *imgData = rlglReadScreenPixels(renderWidth, renderHeight); + + sprintf(buffer, "screenshot%03i.png", shotNum); + + // Save image as PNG + WritePNG(buffer, imgData, renderWidth, renderHeight, 4); + + free(imgData); + + shotNum++; + + TraceLog(INFO, "[%s] Screenshot taken!", buffer); +} +#endif + +#if defined(PLATFORM_DESKTOP) || defined(PLATFORM_WEB) +// GLFW3 Error Callback, runs on GLFW3 error +static void ErrorCallback(int error, const char *description) +{ + TraceLog(WARNING, "[GLFW3 Error] Code: %i Decription: %s", error, description); +} + +// GLFW3 Srolling Callback, runs on mouse wheel +static void ScrollCallback(GLFWwindow *window, double xoffset, double yoffset) +{ + currentMouseWheelY = (int)yoffset; +} + +// GLFW3 Keyboard Callback, runs on key pressed +static void KeyCallback(GLFWwindow *window, int key, int scancode, int action, int mods) +{ + if (key == exitKey && action == GLFW_PRESS) + { + glfwSetWindowShouldClose(window, GL_TRUE); + + // NOTE: Before closing window, while loop must be left! + } +#if defined(PLATFORM_DESKTOP) + else if (key == GLFW_KEY_F12 && action == GLFW_PRESS) + { + TakeScreenshot(); + } +#endif + else + { + currentKeyState[key] = action; + if (action == GLFW_PRESS) lastKeyPressed = key; + } +} + +// GLFW3 Mouse Button Callback, runs on mouse button pressed +static void MouseButtonCallback(GLFWwindow *window, int button, int action, int mods) +{ + currentMouseState[button] = action; + +#define ENABLE_MOUSE_GESTURES +#if defined(ENABLE_MOUSE_GESTURES) + // Process mouse events as touches to be able to use mouse-gestures + GestureEvent gestureEvent; + + // Register touch actions + if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON)) gestureEvent.touchAction = TOUCH_DOWN; + else if (IsMouseButtonReleased(MOUSE_LEFT_BUTTON)) gestureEvent.touchAction = TOUCH_UP; + + // NOTE: TOUCH_MOVE event is registered in MouseCursorPosCallback() + + // Assign a pointer ID + gestureEvent.pointerId[0] = 0; + + // Register touch points count + gestureEvent.pointCount = 1; + + // Register touch points position, only one point registered + gestureEvent.position[0] = GetMousePosition(); + + // Normalize gestureEvent.position[0] for screenWidth and screenHeight + gestureEvent.position[0].x /= (float)GetScreenWidth(); + gestureEvent.position[0].y /= (float)GetScreenHeight(); + + // Gesture data is sent to gestures system for processing + ProcessGestureEvent(gestureEvent); +#endif +} + +// GLFW3 Cursor Position Callback, runs on mouse move +static void MouseCursorPosCallback(GLFWwindow *window, double x, double y) +{ +#define ENABLE_MOUSE_GESTURES +#if defined(ENABLE_MOUSE_GESTURES) + // Process mouse events as touches to be able to use mouse-gestures + GestureEvent gestureEvent; + + gestureEvent.touchAction = TOUCH_MOVE; + + // Assign a pointer ID + gestureEvent.pointerId[0] = 0; + + // Register touch points count + gestureEvent.pointCount = 1; + + // Register touch points position, only one point registered + gestureEvent.position[0] = (Vector2){ (float)x, (float)y }; + + touchPosition[0] = gestureEvent.position[0]; + + // Normalize gestureEvent.position[0] for screenWidth and screenHeight + gestureEvent.position[0].x /= (float)GetScreenWidth(); + gestureEvent.position[0].y /= (float)GetScreenHeight(); + + // Gesture data is sent to gestures system for processing + ProcessGestureEvent(gestureEvent); +#endif +} + +// GLFW3 Char Key Callback, runs on key pressed (get char value) +static void CharCallback(GLFWwindow *window, unsigned int key) +{ + lastKeyPressed = key; + + //TraceLog(INFO, "Char Callback Key pressed: %i\n", key); +} + +// GLFW3 CursorEnter Callback, when cursor enters the window +static void CursorEnterCallback(GLFWwindow *window, int enter) +{ + if (enter == true) cursorOnScreen = true; + else cursorOnScreen = false; +} + +// GLFW3 WindowSize Callback, runs when window is resized +// NOTE: Window resizing not allowed by default +static void WindowSizeCallback(GLFWwindow *window, int width, int height) +{ + // If window is resized, graphics device is re-initialized (but only ortho mode) + rlglInitGraphics(0, 0, width, height); + + // Window size must be updated to be used on 3D mode to get new aspect ratio (Begin3dMode()) + screenWidth = width; + screenHeight = height; + renderWidth = width; + renderHeight = height; + + // NOTE: Postprocessing texture is not scaled to new size + + // Background must be also re-cleared + ClearBackground(RAYWHITE); +} + +// GLFW3 WindowIconify Callback, runs when window is minimized/restored +static void WindowIconifyCallback(GLFWwindow* window, int iconified) +{ + if (iconified) windowMinimized = true; // The window was iconified else windowMinimized = false; // The window was restored } #endif @@ -2107,151 +2339,95 @@ static int32_t AndroidInputCallback(struct android_app *app, AInputEvent *event) } #endif -#if defined(PLATFORM_DESKTOP) || defined(PLATFORM_RPI) -// Takes a screenshot and saves it in the same folder as executable -static void TakeScreenshot(void) +#if defined(PLATFORM_WEB) +static EM_BOOL EmscriptenFullscreenChangeCallback(int eventType, const EmscriptenFullscreenChangeEvent *e, void *userData) { - static int shotNum = 0; // Screenshot number, increments every screenshot take during program execution - char buffer[20]; // Buffer to store file name - - unsigned char *imgData = rlglReadScreenPixels(renderWidth, renderHeight); - - sprintf(buffer, "screenshot%03i.png", shotNum); + //isFullscreen: int e->isFullscreen + //fullscreenEnabled: int e->fullscreenEnabled + //fs element nodeName: (char *) e->nodeName + //fs element id: (char *) e->id + //Current element size: (int) e->elementWidth, (int) e->elementHeight + //Screen size:(int) e->screenWidth, (int) e->screenHeight - // Save image as PNG - WritePNG(buffer, imgData, renderWidth, renderHeight, 4); - - free(imgData); - - shotNum++; - - TraceLog(INFO, "[%s] Screenshot taken!", buffer); -} -#endif - -// Initialize hi-resolution timer -static void InitTimer(void) -{ - srand(time(NULL)); // Initialize random seed - -#if defined(PLATFORM_ANDROID) || defined(PLATFORM_RPI) - struct timespec now; - - if (clock_gettime(CLOCK_MONOTONIC, &now) == 0) // Success + if (e->isFullscreen) { - baseTime = (uint64_t)now.tv_sec*1000000000LLU + (uint64_t)now.tv_nsec; + TraceLog(INFO, "Canvas scaled to fullscreen. ElementSize: (%ix%i), ScreenSize(%ix%i)", e->elementWidth, e->elementHeight, e->screenWidth, e->screenHeight); + } + else + { + TraceLog(INFO, "Canvas scaled to windowed. ElementSize: (%ix%i), ScreenSize(%ix%i)", e->elementWidth, e->elementHeight, e->screenWidth, e->screenHeight); } - else TraceLog(WARNING, "No hi-resolution timer available"); -#endif - - previousTime = GetTime(); // Get time as double -} - -// Get current time measure (in seconds) since InitTimer() -static double GetTime(void) -{ -#if defined(PLATFORM_DESKTOP) || defined(PLATFORM_WEB) - return glfwGetTime(); -#elif defined(PLATFORM_ANDROID) || defined(PLATFORM_RPI) - struct timespec ts; - clock_gettime(CLOCK_MONOTONIC, &ts); - uint64_t time = (uint64_t)ts.tv_sec*1000000000LLU + (uint64_t)ts.tv_nsec; - - return (double)(time - baseTime)*1e-9; -#endif -} - -// Get one key state -static bool GetKeyStatus(int key) -{ -#if defined(PLATFORM_DESKTOP) || defined(PLATFORM_WEB) - return glfwGetKey(window, key); -#elif defined(PLATFORM_ANDROID) - // TODO: Check for virtual keyboard - return false; -#elif defined(PLATFORM_RPI) - // NOTE: Keys states are filled in PollInputEvents() - if (key < 0 || key > 511) return false; - else return currentKeyState[key]; -#endif -} - -// Get one mouse button state -static bool GetMouseButtonStatus(int button) -{ -#if defined(PLATFORM_DESKTOP) || defined(PLATFORM_WEB) - return glfwGetMouseButton(window, button); -#elif defined(PLATFORM_ANDROID) - // TODO: Check for virtual mouse - return false; -#elif defined(PLATFORM_RPI) - // NOTE: Mouse buttons states are filled in PollInputEvents() - return currentMouseState[button]; -#endif -} - -// Poll (store) all input events -static void PollInputEvents(void) -{ - // NOTE: Gestures update must be called every frame to reset gestures correctly - // because ProcessGestureEvent() is just called on an event, not every frame - UpdateGestures(); -#if defined(PLATFORM_DESKTOP) || defined(PLATFORM_WEB) - // Mouse input polling - double mouseX; - double mouseY; - - glfwGetCursorPos(window, &mouseX, &mouseY); - - mousePosition.x = (float)mouseX; - mousePosition.y = (float)mouseY; - - // Keyboard input polling (automatically managed by GLFW3 through callback) - lastKeyPressed = -1; - - // Register previous keys states - for (int i = 0; i < 512; i++) previousKeyState[i] = currentKeyState[i]; + // TODO: Depending on scaling factor (screen vs element), calculate factor to scale mouse/touch input - // Register previous mouse states - for (int i = 0; i < 3; i++) previousMouseState[i] = currentMouseState[i]; + return 0; +} - previousMouseWheelY = currentMouseWheelY; - currentMouseWheelY = 0; +// Web: Get input events +static EM_BOOL EmscriptenInputCallback(int eventType, const EmscriptenTouchEvent *touchEvent, void *userData) +{ + /* + for (int i = 0; i < touchEvent->numTouches; i++) + { + long x, y, id; - glfwPollEvents(); // Register keyboard/mouse events... and window events! -#elif defined(PLATFORM_ANDROID) + if (!touchEvent->touches[i].isChanged) continue; - // Register previous keys states - for (int i = 0; i < 128; i++) previousButtonState[i] = currentButtonState[i]; + id = touchEvent->touches[i].identifier; + x = touchEvent->touches[i].canvasX; + y = touchEvent->touches[i].canvasY; + } + + printf("%s, numTouches: %d %s%s%s%s\n", emscripten_event_type_to_string(eventType), event->numTouches, + event->ctrlKey ? " CTRL" : "", event->shiftKey ? " SHIFT" : "", event->altKey ? " ALT" : "", event->metaKey ? " META" : ""); - // Poll Events (registered events) - // NOTE: Activity is paused if not enabled (appEnabled) - while ((ident = ALooper_pollAll(appEnabled ? 0 : -1, NULL, &events,(void**)&source)) >= 0) + for(int i = 0; i < event->numTouches; ++i) { - // Process this event - if (source != NULL) source->process(app, source); - - // NOTE: Never close window, native activity is controlled by the system! - if (app->destroyRequested != 0) - { - //TraceLog(INFO, "Closing Window..."); - //windowShouldClose = true; - //ANativeActivity_finish(app->activity); - } + const EmscriptenTouchPoint *t = &event->touches[i]; + + printf(" %ld: screen: (%ld,%ld), client: (%ld,%ld), page: (%ld,%ld), isChanged: %d, onTarget: %d, canvas: (%ld, %ld)\n", + t->identifier, t->screenX, t->screenY, t->clientX, t->clientY, t->pageX, t->pageY, t->isChanged, t->onTarget, t->canvasX, t->canvasY); } -#elif defined(PLATFORM_RPI) + */ + + GestureEvent gestureEvent; - // NOTE: Mouse input events polling is done asynchonously in another pthread - MouseThread() + // Register touch actions + if (eventType == EMSCRIPTEN_EVENT_TOUCHSTART) gestureEvent.touchAction = TOUCH_DOWN; + else if (eventType == EMSCRIPTEN_EVENT_TOUCHEND) gestureEvent.touchAction = TOUCH_UP; + else if (eventType == EMSCRIPTEN_EVENT_TOUCHMOVE) gestureEvent.touchAction = TOUCH_MOVE; + + // Register touch points count + gestureEvent.pointCount = touchEvent->numTouches; + + // Register touch points id + gestureEvent.pointerId[0] = touchEvent->touches[0].identifier; + gestureEvent.pointerId[1] = touchEvent->touches[1].identifier; + + // Register touch points position + // NOTE: Only two points registered + // TODO: Touch data should be scaled accordingly! + //gestureEvent.position[0] = (Vector2){ touchEvent->touches[0].canvasX, touchEvent->touches[0].canvasY }; + //gestureEvent.position[1] = (Vector2){ touchEvent->touches[1].canvasX, touchEvent->touches[1].canvasY }; + gestureEvent.position[0] = (Vector2){ touchEvent->touches[0].targetX, touchEvent->touches[0].targetY }; + gestureEvent.position[1] = (Vector2){ touchEvent->touches[1].targetX, touchEvent->touches[1].targetY }; - // NOTE: Keyboard reading could be done using input_event(s) reading or just read from stdin, - // we use method 2 (stdin) but maybe in a future we should change to method 1... - ProcessKeyboard(); + touchPosition[0] = gestureEvent.position[0]; + touchPosition[1] = gestureEvent.position[1]; + + // Normalize gestureEvent.position[x] for screenWidth and screenHeight + gestureEvent.position[0].x /= (float)GetScreenWidth(); + gestureEvent.position[0].y /= (float)GetScreenHeight(); + + gestureEvent.position[1].x /= (float)GetScreenWidth(); + gestureEvent.position[1].y /= (float)GetScreenHeight(); - // NOTE: Gamepad (Joystick) input events polling is done asynchonously in another pthread - GamepadThread() + // Gesture data is sent to gestures system for processing + ProcessGestureEvent(gestureEvent); // Process obtained gestures data -#endif + return 1; } +#endif #if defined(PLATFORM_RPI) // Initialize Keyboard system (using standard input) @@ -2571,183 +2747,6 @@ static void *GamepadThread(void *arg) } #endif -// Copy back buffer to front buffers -static void SwapBuffers(void) -{ -#if defined(PLATFORM_DESKTOP) || defined(PLATFORM_WEB) - glfwSwapBuffers(window); -#elif defined(PLATFORM_ANDROID) || defined(PLATFORM_RPI) - eglSwapBuffers(display, surface); -#endif -} - -// Compute framebuffer size relative to screen size and display size -// NOTE: Global variables renderWidth/renderHeight can be modified -static void SetupFramebufferSize(int displayWidth, int displayHeight) -{ - // TODO: SetupFramebufferSize() does not consider properly display video modes. - // It setups a renderWidth/renderHeight with black bars that could not match a valid video mode, - // and so, framebuffer is not scaled properly to some monitors. - - // Calculate renderWidth and renderHeight, we have the display size (input params) and the desired screen size (global var) - if ((screenWidth > displayWidth) || (screenHeight > displayHeight)) - { - TraceLog(WARNING, "DOWNSCALING: Required screen size (%ix%i) is bigger than display size (%ix%i)", screenWidth, screenHeight, displayWidth, displayHeight); - - // Downscaling to fit display with border-bars - float widthRatio = (float)displayWidth/(float)screenWidth; - float heightRatio = (float)displayHeight/(float)screenHeight; - - if (widthRatio <= heightRatio) - { - renderWidth = displayWidth; - renderHeight = (int)round((float)screenHeight*widthRatio); - renderOffsetX = 0; - renderOffsetY = (displayHeight - renderHeight); - } - else - { - renderWidth = (int)round((float)screenWidth*heightRatio); - renderHeight = displayHeight; - renderOffsetX = (displayWidth - renderWidth); - renderOffsetY = 0; - } - - // NOTE: downscale matrix required! - float scaleRatio = (float)renderWidth/(float)screenWidth; - - downscaleView = MatrixScale(scaleRatio, scaleRatio, scaleRatio); - - // NOTE: We render to full display resolution! - // We just need to calculate above parameters for downscale matrix and offsets - renderWidth = displayWidth; - renderHeight = displayHeight; - - TraceLog(WARNING, "Downscale matrix generated, content will be rendered at: %i x %i", renderWidth, renderHeight); - } - else if ((screenWidth < displayWidth) || (screenHeight < displayHeight)) - { - // Required screen size is smaller than display size - TraceLog(INFO, "UPSCALING: Required screen size: %i x %i -> Display size: %i x %i", screenWidth, screenHeight, displayWidth, displayHeight); - - // Upscaling to fit display with border-bars - float displayRatio = (float)displayWidth/(float)displayHeight; - float screenRatio = (float)screenWidth/(float)screenHeight; - - if (displayRatio <= screenRatio) - { - renderWidth = screenWidth; - renderHeight = (int)round((float)screenWidth/displayRatio); - renderOffsetX = 0; - renderOffsetY = (renderHeight - screenHeight); - } - else - { - renderWidth = (int)round((float)screenHeight*displayRatio); - renderHeight = screenHeight; - renderOffsetX = (renderWidth - screenWidth); - renderOffsetY = 0; - } - } - else // screen == display - { - renderWidth = screenWidth; - renderHeight = screenHeight; - renderOffsetX = 0; - renderOffsetY = 0; - } -} - -#if defined(PLATFORM_WEB) -static EM_BOOL EmscriptenFullscreenChangeCallback(int eventType, const EmscriptenFullscreenChangeEvent *e, void *userData) -{ - //isFullscreen: int e->isFullscreen - //fullscreenEnabled: int e->fullscreenEnabled - //fs element nodeName: (char *) e->nodeName - //fs element id: (char *) e->id - //Current element size: (int) e->elementWidth, (int) e->elementHeight - //Screen size:(int) e->screenWidth, (int) e->screenHeight - - if (e->isFullscreen) - { - TraceLog(INFO, "Canvas scaled to fullscreen. ElementSize: (%ix%i), ScreenSize(%ix%i)", e->elementWidth, e->elementHeight, e->screenWidth, e->screenHeight); - } - else - { - TraceLog(INFO, "Canvas scaled to windowed. ElementSize: (%ix%i), ScreenSize(%ix%i)", e->elementWidth, e->elementHeight, e->screenWidth, e->screenHeight); - } - - // TODO: Depending on scaling factor (screen vs element), calculate factor to scale mouse/touch input - - return 0; -} - -// Web: Get input events -static EM_BOOL EmscriptenInputCallback(int eventType, const EmscriptenTouchEvent *touchEvent, void *userData) -{ - /* - for (int i = 0; i < touchEvent->numTouches; i++) - { - long x, y, id; - - if (!touchEvent->touches[i].isChanged) continue; - - id = touchEvent->touches[i].identifier; - x = touchEvent->touches[i].canvasX; - y = touchEvent->touches[i].canvasY; - } - - printf("%s, numTouches: %d %s%s%s%s\n", emscripten_event_type_to_string(eventType), event->numTouches, - event->ctrlKey ? " CTRL" : "", event->shiftKey ? " SHIFT" : "", event->altKey ? " ALT" : "", event->metaKey ? " META" : ""); - - for(int i = 0; i < event->numTouches; ++i) - { - const EmscriptenTouchPoint *t = &event->touches[i]; - - printf(" %ld: screen: (%ld,%ld), client: (%ld,%ld), page: (%ld,%ld), isChanged: %d, onTarget: %d, canvas: (%ld, %ld)\n", - t->identifier, t->screenX, t->screenY, t->clientX, t->clientY, t->pageX, t->pageY, t->isChanged, t->onTarget, t->canvasX, t->canvasY); - } - */ - - GestureEvent gestureEvent; - - // Register touch actions - if (eventType == EMSCRIPTEN_EVENT_TOUCHSTART) gestureEvent.touchAction = TOUCH_DOWN; - else if (eventType == EMSCRIPTEN_EVENT_TOUCHEND) gestureEvent.touchAction = TOUCH_UP; - else if (eventType == EMSCRIPTEN_EVENT_TOUCHMOVE) gestureEvent.touchAction = TOUCH_MOVE; - - // Register touch points count - gestureEvent.pointCount = touchEvent->numTouches; - - // Register touch points id - gestureEvent.pointerId[0] = touchEvent->touches[0].identifier; - gestureEvent.pointerId[1] = touchEvent->touches[1].identifier; - - // Register touch points position - // NOTE: Only two points registered - // TODO: Touch data should be scaled accordingly! - //gestureEvent.position[0] = (Vector2){ touchEvent->touches[0].canvasX, touchEvent->touches[0].canvasY }; - //gestureEvent.position[1] = (Vector2){ touchEvent->touches[1].canvasX, touchEvent->touches[1].canvasY }; - gestureEvent.position[0] = (Vector2){ touchEvent->touches[0].targetX, touchEvent->touches[0].targetY }; - gestureEvent.position[1] = (Vector2){ touchEvent->touches[1].targetX, touchEvent->touches[1].targetY }; - - touchPosition[0] = gestureEvent.position[0]; - touchPosition[1] = gestureEvent.position[1]; - - // Normalize gestureEvent.position[x] for screenWidth and screenHeight - gestureEvent.position[0].x /= (float)GetScreenWidth(); - gestureEvent.position[0].y /= (float)GetScreenHeight(); - - gestureEvent.position[1].x /= (float)GetScreenWidth(); - gestureEvent.position[1].y /= (float)GetScreenHeight(); - - // Gesture data is sent to gestures system for processing - ProcessGestureEvent(gestureEvent); // Process obtained gestures data - - return 1; -} -#endif - // Plays raylib logo appearing animation static void LogoAnimation(void) { -- cgit v1.2.3 From 4636e3367cdf3d2e1adab3a08667c34631f94837 Mon Sep 17 00:00:00 2001 From: Joshua Reisenauer Date: Mon, 2 May 2016 14:37:00 -0700 Subject: number remaining buffer transfer for updateAudioContext updateAudioContext is almost done --- src/audio.c | 39 +++++++++++++++++++++++++++++++-------- src/audio.h | 2 +- src/raylib.h | 2 +- 3 files changed, 33 insertions(+), 10 deletions(-) (limited to 'src') diff --git a/src/audio.c b/src/audio.c index d935b6c7..ff4f2858 100644 --- a/src/audio.c +++ b/src/audio.c @@ -273,17 +273,20 @@ void CloseAudioContext(AudioContext ctx) // Pushes more audio data into context mix channel, if none are ever pushed then zeros are fed in // Call "UpdateAudioContext(ctx, NULL, 0)" every game tick if you want to pause the audio -// Returns true if data was pushed onto queue, otherwise if queue is full then no data is added and false is returned -bool UpdateAudioContext(AudioContext ctx, float *data, unsigned short dataLength) +// Returns number of floats that where processed +unsigned short UpdateAudioContext(AudioContext ctx, float *data, unsigned short dataLength) { + unsigned short numberProcessed = 0; + unsigned short numberRemaining = dataLength; AudioContext_t *context = (AudioContext_t*)ctx; + if (context && mixChannelsActive_g[context->mixChannel] == context) { ALint processed = 0; ALuint buffer = 0; alGetSourcei(context->alSource, AL_BUFFERS_PROCESSED, &processed); // Get the number of already processed buffers (if any) - if(!processed) return false;//nothing to process, queue is still full + if(!processed) return 0;//nothing to process, queue is still full if (!data || !dataLength)// play silence while (processed > 0) @@ -292,17 +295,37 @@ bool UpdateAudioContext(AudioContext ctx, float *data, unsigned short dataLength FillAlBufferWithSilence(context, buffer); alSourceQueueBuffers(context->alSource, 1, &buffer); processed--; + numberProcessed+=MUSIC_BUFFER_SIZE; + } + if(numberRemaining)// buffer data stream in increments of MUSIC_BUFFER_SIZE + while (processed > 0) + { + alSourceUnqueueBuffers(context->alSource, 1, &buffer); + if(numberRemaining >= MUSIC_BUFFER_SIZE) + { + float pcm[MUSIC_BUFFER_SIZE]; + memcpy(pcm, &data[numberProcessed], MUSIC_BUFFER_SIZE); + alBufferData(buffer, context->alFormat, pcm, MUSIC_BUFFER_SIZE*sizeof(float), context->sampleRate); + alSourceQueueBuffers(context->alSource, 1, &buffer); + numberProcessed+=MUSIC_BUFFER_SIZE; + numberRemaining-=MUSIC_BUFFER_SIZE; + } + else // less than MUSIC_BUFFER_SIZE number of samples left to buffer, the remaining data is padded with zeros + { + float pcm[MUSIC_BUFFER_SIZE] = {0.f}; // pad with zeros + } + + processed--; } - return true; } - return false; + return numberProcessed; } // fill buffer with zeros -static void FillAlBufferWithSilence(AudioContext_t *ac, ALuint buffer) +static void FillAlBufferWithSilence(AudioContext_t *context, ALuint buffer) { float pcm[MUSIC_BUFFER_SIZE] = {0.f}; - alBufferData(buffer, ac->alFormat, pcm, MUSIC_BUFFER_SIZE*sizeof(float), ac->sampleRate); + alBufferData(buffer, context->alFormat, pcm, MUSIC_BUFFER_SIZE*sizeof(float), context->sampleRate); } // example usage: @@ -322,7 +345,7 @@ static void ResampleShortToFloat(short *shorts, float *floats, unsigned short le // example usage: // char ch[3] = {1,2,3};float fl[3]; -// ResampleShortToFloat(ch,fl,3); +// ResampleByteToFloat(ch,fl,3); static void ResampleByteToFloat(char *chars, float *floats, unsigned short len) { int x; diff --git a/src/audio.h b/src/audio.h index d723ef1b..2b36b3f0 100644 --- a/src/audio.h +++ b/src/audio.h @@ -88,7 +88,7 @@ bool IsAudioDeviceReady(void); // True if call // all samples are floating point by default AudioContext InitAudioContext(unsigned short sampleRate, unsigned char mixChannel, unsigned char channels); void CloseAudioContext(AudioContext ctx); // Frees audio context -bool UpdateAudioContext(AudioContext ctx, float *data, unsigned short dataLength); // Pushes more audio data into context mix channel, if NULL is passed to data then zeros are played +unsigned short UpdateAudioContext(AudioContext ctx, float *data, unsigned short dataLength); // Pushes more audio data into context mix channel, if NULL is passed to data then zeros are played Sound LoadSound(char *fileName); // Load sound to memory Sound LoadSoundFromWave(Wave wave); // Load sound to memory from wave data diff --git a/src/raylib.h b/src/raylib.h index 3fa20116..5c727b61 100644 --- a/src/raylib.h +++ b/src/raylib.h @@ -874,7 +874,7 @@ bool IsAudioDeviceReady(void); // True if call // all samples are floating point by default AudioContext InitAudioContext(unsigned short sampleRate, unsigned char mixChannel, unsigned char channels); void CloseAudioContext(AudioContext ctx); // Frees audio context -bool UpdateAudioContext(AudioContext ctx, float *data, unsigned short dataLength); // Pushes more audio data into context mix channel, if NULL is passed to data then zeros are played +unsigned short UpdateAudioContext(AudioContext ctx, float *data, unsigned short dataLength); // Pushes more audio data into context mix channel, if NULL is passed to data then zeros are played Sound LoadSound(char *fileName); // Load sound to memory Sound LoadSoundFromWave(Wave wave); // Load sound to memory from wave data -- cgit v1.2.3 From 9d09ada33b26704a7f5d285d2f0d115e41df41bf Mon Sep 17 00:00:00 2001 From: Joshua Reisenauer Date: Mon, 2 May 2016 21:59:55 -0700 Subject: new boolean floatingPoint option Now floating point is either on or off. This simplifies the use of 16bit vs float. --- src/audio.c | 132 +++++++++++++++++++++++++++++++++++++---------------------- src/audio.h | 7 ++-- src/raylib.h | 7 ++-- 3 files changed, 88 insertions(+), 58 deletions(-) (limited to 'src') diff --git a/src/audio.c b/src/audio.c index ff4f2858..dc063796 100644 --- a/src/audio.c +++ b/src/audio.c @@ -59,15 +59,17 @@ //---------------------------------------------------------------------------------- // Defines and Macros //---------------------------------------------------------------------------------- -#define MUSIC_STREAM_BUFFERS 2 -#define MAX_AUDIO_CONTEXTS 4 +#define MAX_STREAM_BUFFERS 2 +#define MAX_AUDIO_CONTEXTS 4 // Number of open AL sources #if defined(PLATFORM_RPI) || defined(PLATFORM_ANDROID) // NOTE: On RPI and Android should be lower to avoid frame-stalls - #define MUSIC_BUFFER_SIZE 4096*2 // PCM data buffer (short) - 16Kb (RPI) + #define MUSIC_BUFFER_SIZE_SHORT 4096*2 // PCM data buffer (short) - 16Kb (RPI) + #define MUSIC_BUFFER_SIZE_FLOAT 4096 // PCM data buffer (float) - 16Kb (RPI) #else // NOTE: On HTML5 (emscripten) this is allocated on heap, by default it's only 16MB!...just take care... - #define MUSIC_BUFFER_SIZE 4096*8 // PCM data buffer (short) - 64Kb + #define MUSIC_BUFFER_SIZE_SHORT 4096*8 // PCM data buffer (short) - 64Kb + #define MUSIC_BUFFER_SIZE_FLOAT 4096*4 // PCM data buffer (float) - 64Kb #endif //---------------------------------------------------------------------------------- @@ -80,7 +82,7 @@ typedef struct Music { stb_vorbis *stream; jar_xm_context_t *chipctx; // Stores jar_xm context - ALuint buffers[MUSIC_STREAM_BUFFERS]; + ALuint buffers[MAX_STREAM_BUFFERS]; ALuint source; ALenum format; @@ -96,12 +98,13 @@ typedef struct Music { // no more than 4 concurrent audio contexts in use. This is due to each active context being tied to // a dedicated mix channel. All audio is 32bit floating point in stereo. typedef struct AudioContext_t { - unsigned short sampleRate; // default is 48000 - unsigned char channels; // 1=mono,2=stereo - unsigned char mixChannel; // 0-3 or mixA-mixD, each mix channel can receive up to one dedicated audio stream - ALenum alFormat; // openAL format specifier - ALuint alSource; // openAL source - ALuint alBuffer[2]; // openAL sample buffer + unsigned short sampleRate; // default is 48000 + unsigned char channels; // 1=mono,2=stereo + unsigned char mixChannel; // 0-3 or mixA-mixD, each mix channel can receive up to one dedicated audio stream + bool floatingPoint; // if false then the short datatype is used instead + ALenum alFormat; // openAL format specifier + ALuint alSource; // openAL source + ALuint alBuffer[MAX_STREAM_BUFFERS]; // openAL sample buffer } AudioContext_t; #if defined(AUDIO_STANDALONE) @@ -126,7 +129,7 @@ static void UnloadWave(Wave wave); // Unload wave data static bool BufferMusicStream(ALuint buffer); // Fill music buffers with data static void EmptyMusicStream(void); // Empty music buffers -static void FillAlBufferWithSilence(AudioContext_t *ac, ALuint buffer);// fill buffer with zeros +static unsigned short FillAlBufferWithSilence(AudioContext_t *context, ALuint buffer);// fill buffer with zeros, returns number processed static void ResampleShortToFloat(short *shorts, float *floats, unsigned short len); // pass two arrays of the same legnth in static void ResampleByteToFloat(char *chars, float *floats, unsigned short len); // pass two arrays of same length in @@ -201,11 +204,10 @@ bool IsAudioDeviceReady(void) // Audio contexts are for outputing custom audio waveforms, This will shut down any other sound sources currently playing // The mixChannel is what mix channel you want to operate on, 0-3 are the ones available. Each mix channel can only be used one at a time. -// exmple usage is InitAudioContext(48000, 0, 2); // mixchannel 1, 48khz, stereo -// all samples are floating point by default -AudioContext InitAudioContext(unsigned short sampleRate, unsigned char mixChannel, unsigned char channels) +// exmple usage is InitAudioContext(48000, 0, 2, true); // mixchannel 1, 48khz, stereo, floating point +AudioContext InitAudioContext(unsigned short sampleRate, unsigned char mixChannel, unsigned char channels, bool floatingPoint) { - if(mixChannel > MAX_AUDIO_CONTEXTS) return NULL; + if(mixChannel >= MAX_AUDIO_CONTEXTS) return NULL; if(!IsAudioDeviceReady()) InitAudioDevice(); else StopMusicStream(); @@ -214,13 +216,24 @@ AudioContext InitAudioContext(unsigned short sampleRate, unsigned char mixChanne ac->sampleRate = sampleRate; ac->channels = channels; ac->mixChannel = mixChannel; + ac->floatingPoint = floatingPoint; mixChannelsActive_g[mixChannel] = ac; // setup openAL format if(channels == 1) - ac->alFormat = AL_FORMAT_MONO_FLOAT32; - else - ac->alFormat = AL_FORMAT_STEREO_FLOAT32; + { + if(floatingPoint) + ac->alFormat = AL_FORMAT_MONO_FLOAT32; + else + ac->alFormat = AL_FORMAT_MONO16; + } + else if(channels == 2) + { + if(floatingPoint) + ac->alFormat = AL_FORMAT_STEREO_FLOAT32; + else + ac->alFormat = AL_FORMAT_STEREO16; + } // Create an audio source alGenSources(1, &ac->alSource); @@ -230,16 +243,17 @@ AudioContext InitAudioContext(unsigned short sampleRate, unsigned char mixChanne alSource3f(ac->alSource, AL_VELOCITY, 0, 0, 0); // Create Buffer - alGenBuffers(2, ac->alBuffer); + alGenBuffers(MAX_STREAM_BUFFERS, ac->alBuffer); //fill buffers - FillAlBufferWithSilence(ac, ac->alBuffer[0]); - FillAlBufferWithSilence(ac, ac->alBuffer[1]); - alSourceQueueBuffers(ac->alSource, 2, ac->alBuffer); + int x; + for(x=0;xalBuffer[x]); + + alSourceQueueBuffers(ac->alSource, MAX_STREAM_BUFFERS, ac->alBuffer); alSourcei(ac->alSource, AL_LOOPING, AL_FALSE); // this could cause errors alSourcePlay(ac->alSource); - return ac; } return NULL; @@ -264,20 +278,22 @@ void CloseAudioContext(AudioContext ctx) //delete source and buffers alDeleteSources(1, &context->alSource); - alDeleteBuffers(2, context->alBuffer); + alDeleteBuffers(MAX_STREAM_BUFFERS, context->alBuffer); mixChannelsActive_g[context->mixChannel] = NULL; free(context); ctx = NULL; } } -// Pushes more audio data into context mix channel, if none are ever pushed then zeros are fed in -// Call "UpdateAudioContext(ctx, NULL, 0)" every game tick if you want to pause the audio -// Returns number of floats that where processed -unsigned short UpdateAudioContext(AudioContext ctx, float *data, unsigned short dataLength) +// Pushes more audio data into context mix channel, if none are ever pushed then zeros are fed in. +// Call "UpdateAudioContext(ctx, NULL, 0)" every game tick if you want to pause the audio. +// @Returns number of samples that where processed. +// All data streams should be of a length that is evenly divisible by MUSIC_BUFFER_SIZE, +// otherwise the remaining data will not be pushed. +unsigned short UpdateAudioContext(AudioContext ctx, void *data, unsigned short numberElements) { unsigned short numberProcessed = 0; - unsigned short numberRemaining = dataLength; + unsigned short numberRemaining = numberElements; AudioContext_t *context = (AudioContext_t*)ctx; if (context && mixChannelsActive_g[context->mixChannel] == context) @@ -288,44 +304,60 @@ unsigned short UpdateAudioContext(AudioContext ctx, float *data, unsigned short if(!processed) return 0;//nothing to process, queue is still full - if (!data || !dataLength)// play silence + if (!data || !numberElements)// play silence + { while (processed > 0) { alSourceUnqueueBuffers(context->alSource, 1, &buffer); - FillAlBufferWithSilence(context, buffer); + numberProcessed += FillAlBufferWithSilence(context, buffer); alSourceQueueBuffers(context->alSource, 1, &buffer); processed--; - numberProcessed+=MUSIC_BUFFER_SIZE; } + } if(numberRemaining)// buffer data stream in increments of MUSIC_BUFFER_SIZE + { while (processed > 0) { - alSourceUnqueueBuffers(context->alSource, 1, &buffer); - if(numberRemaining >= MUSIC_BUFFER_SIZE) + if(context->floatingPoint && numberRemaining >= MUSIC_BUFFER_SIZE_FLOAT) // process float buffers { - float pcm[MUSIC_BUFFER_SIZE]; - memcpy(pcm, &data[numberProcessed], MUSIC_BUFFER_SIZE); - alBufferData(buffer, context->alFormat, pcm, MUSIC_BUFFER_SIZE*sizeof(float), context->sampleRate); + float *ptr = (float*)data; + alSourceUnqueueBuffers(context->alSource, 1, &buffer); + alBufferData(buffer, context->alFormat, &ptr[numberProcessed], MUSIC_BUFFER_SIZE_FLOAT*sizeof(float), context->sampleRate); alSourceQueueBuffers(context->alSource, 1, &buffer); - numberProcessed+=MUSIC_BUFFER_SIZE; - numberRemaining-=MUSIC_BUFFER_SIZE; + numberProcessed+=MUSIC_BUFFER_SIZE_FLOAT; + numberRemaining-=MUSIC_BUFFER_SIZE_FLOAT; } - else // less than MUSIC_BUFFER_SIZE number of samples left to buffer, the remaining data is padded with zeros + else if(!context->floatingPoint && numberRemaining >= MUSIC_BUFFER_SIZE_SHORT) // process short buffers { - float pcm[MUSIC_BUFFER_SIZE] = {0.f}; // pad with zeros + short *ptr = (short*)data; + alSourceUnqueueBuffers(context->alSource, 1, &buffer); + alBufferData(buffer, context->alFormat, &ptr[numberProcessed], MUSIC_BUFFER_SIZE_SHORT*sizeof(short), context->sampleRate); + alSourceQueueBuffers(context->alSource, 1, &buffer); + numberProcessed+=MUSIC_BUFFER_SIZE_SHORT; + numberRemaining-=MUSIC_BUFFER_SIZE_SHORT; } processed--; } + } } return numberProcessed; } -// fill buffer with zeros -static void FillAlBufferWithSilence(AudioContext_t *context, ALuint buffer) +// fill buffer with zeros, returns number processed +static unsigned short FillAlBufferWithSilence(AudioContext_t *context, ALuint buffer) { - float pcm[MUSIC_BUFFER_SIZE] = {0.f}; - alBufferData(buffer, context->alFormat, pcm, MUSIC_BUFFER_SIZE*sizeof(float), context->sampleRate); + if(context->floatingPoint){ + float pcm[MUSIC_BUFFER_SIZE_FLOAT] = {0.f}; + alBufferData(buffer, context->alFormat, pcm, MUSIC_BUFFER_SIZE_FLOAT*sizeof(float), context->sampleRate); + return MUSIC_BUFFER_SIZE_FLOAT; + } + else + { + short pcm[MUSIC_BUFFER_SIZE_SHORT] = {0}; + alBufferData(buffer, context->alFormat, pcm, MUSIC_BUFFER_SIZE_SHORT*sizeof(short), context->sampleRate); + return MUSIC_BUFFER_SIZE_SHORT; + } } // example usage: @@ -920,7 +952,7 @@ float GetMusicTimePlayed(void) // Fill music buffers with new data from music stream static bool BufferMusicStream(ALuint buffer) { - short pcm[MUSIC_BUFFER_SIZE]; + short pcm[MUSIC_BUFFER_SIZE_SHORT]; int size = 0; // Total size of data steamed (in bytes) int streamedBytes = 0; // samples of data obtained, channels are not included in calculation @@ -930,15 +962,15 @@ static bool BufferMusicStream(ALuint buffer) { if (currentMusic.chipTune) // There is no end of stream for xmfiles, once the end is reached zeros are generated for non looped chiptunes. { - int readlen = MUSIC_BUFFER_SIZE / 2; + int readlen = MUSIC_BUFFER_SIZE_SHORT / 2; jar_xm_generate_samples_16bit(currentMusic.chipctx, pcm, readlen); // reads 2*readlen shorts and moves them to buffer+size memory location size += readlen * currentMusic.channels; // Not sure if this is what it needs } else { - while (size < MUSIC_BUFFER_SIZE) + while (size < MUSIC_BUFFER_SIZE_SHORT) { - streamedBytes = stb_vorbis_get_samples_short_interleaved(currentMusic.stream, currentMusic.channels, pcm + size, MUSIC_BUFFER_SIZE - size); + streamedBytes = stb_vorbis_get_samples_short_interleaved(currentMusic.stream, currentMusic.channels, pcm + size, MUSIC_BUFFER_SIZE_SHORT - size); if (streamedBytes > 0) size += (streamedBytes*currentMusic.channels); else break; } diff --git a/src/audio.h b/src/audio.h index 2b36b3f0..9d6fa5d0 100644 --- a/src/audio.h +++ b/src/audio.h @@ -84,11 +84,10 @@ bool IsAudioDeviceReady(void); // True if call // Audio contexts are for outputing custom audio waveforms, This will shut down any other sound sources currently playing // The mixChannel is what mix channel you want to operate on, 0-3 are the ones available. Each mix channel can only be used one at a time. -// exmple usage is InitAudioContext(48000, 0, 2); // mixchannel 1, 48khz, stereo -// all samples are floating point by default -AudioContext InitAudioContext(unsigned short sampleRate, unsigned char mixChannel, unsigned char channels); +// exmple usage is InitAudioContext(48000, 0, 2, true); // mixchannel 1, 48khz, stereo, floating point +AudioContext InitAudioContext(unsigned short sampleRate, unsigned char mixChannel, unsigned char channels, bool floatingPoint); void CloseAudioContext(AudioContext ctx); // Frees audio context -unsigned short UpdateAudioContext(AudioContext ctx, float *data, unsigned short dataLength); // Pushes more audio data into context mix channel, if NULL is passed to data then zeros are played +unsigned short UpdateAudioContext(AudioContext ctx, void *data, unsigned short numberElements); // Pushes more audio data into context mix channel, if NULL is passed to data then zeros are played Sound LoadSound(char *fileName); // Load sound to memory Sound LoadSoundFromWave(Wave wave); // Load sound to memory from wave data diff --git a/src/raylib.h b/src/raylib.h index 5c727b61..c9ba5b38 100644 --- a/src/raylib.h +++ b/src/raylib.h @@ -870,11 +870,10 @@ bool IsAudioDeviceReady(void); // True if call // Audio contexts are for outputing custom audio waveforms, This will shut down any other sound sources currently playing // The mixChannel is what mix channel you want to operate on, 0-3 are the ones available. Each mix channel can only be used one at a time. -// exmple usage is InitAudioContext(48000, 0, 2); // mixchannel 1, 48khz, stereo -// all samples are floating point by default -AudioContext InitAudioContext(unsigned short sampleRate, unsigned char mixChannel, unsigned char channels); +// exmple usage is InitAudioContext(48000, 0, 2, true); // mixchannel 1, 48khz, stereo, floating point +AudioContext InitAudioContext(unsigned short sampleRate, unsigned char mixChannel, unsigned char channels, bool floatingPoint); void CloseAudioContext(AudioContext ctx); // Frees audio context -unsigned short UpdateAudioContext(AudioContext ctx, float *data, unsigned short dataLength); // Pushes more audio data into context mix channel, if NULL is passed to data then zeros are played +unsigned short UpdateAudioContext(AudioContext ctx, void *data, unsigned short numberElements); // Pushes more audio data into context mix channel, if NULL is passed to data then zeros are played Sound LoadSound(char *fileName); // Load sound to memory Sound LoadSoundFromWave(Wave wave); // Load sound to memory from wave data -- cgit v1.2.3 From d6feeb14ffc11e54a82f51dbbe899f720d6997e8 Mon Sep 17 00:00:00 2001 From: Joshua Reisenauer Date: Tue, 3 May 2016 02:52:45 -0700 Subject: pause on no data --- src/audio.c | 31 +++++++++++++++---------------- src/easings.h | 17 +++++++++++++++++ 2 files changed, 32 insertions(+), 16 deletions(-) (limited to 'src') diff --git a/src/audio.c b/src/audio.c index dc063796..a46aa00a 100644 --- a/src/audio.c +++ b/src/audio.c @@ -114,11 +114,10 @@ typedef enum { INFO = 0, ERROR, WARNING, DEBUG, OTHER } TraceLogType; //---------------------------------------------------------------------------------- // Global Variables Definition //---------------------------------------------------------------------------------- -static AudioContext_t* mixChannelsActive_g[MAX_AUDIO_CONTEXTS]; // What mix channels are currently active +static AudioContext_t* mixChannelsActive_g[MAX_AUDIO_CONTEXTS]; // What mix channels are currently active static bool musicEnabled = false; static Music currentMusic; // Current music loaded // NOTE: Only one music file playing at a time - //---------------------------------------------------------------------------------- // Module specific Functions Declaration //---------------------------------------------------------------------------------- @@ -286,34 +285,34 @@ void CloseAudioContext(AudioContext ctx) } // Pushes more audio data into context mix channel, if none are ever pushed then zeros are fed in. -// Call "UpdateAudioContext(ctx, NULL, 0)" every game tick if you want to pause the audio. +// Call "UpdateAudioContext(ctx, NULL, 0)" if you want to pause the audio. // @Returns number of samples that where processed. // All data streams should be of a length that is evenly divisible by MUSIC_BUFFER_SIZE, // otherwise the remaining data will not be pushed. unsigned short UpdateAudioContext(AudioContext ctx, void *data, unsigned short numberElements) { - unsigned short numberProcessed = 0; - unsigned short numberRemaining = numberElements; AudioContext_t *context = (AudioContext_t*)ctx; + if(context && context->channels == 2 && numberElements % 2 != 0) return 0; // when there is two channels there must be an even number of samples + + if (!data || !numberElements) alSourcePause(context->alSource); // pauses audio until data is given + else{ // restart audio otherwise + ALint state; + alGetSourcei(context->alSource, AL_SOURCE_STATE, &state); + if (state != AL_PLAYING) alSourcePlay(context->alSource); + } + if (context && mixChannelsActive_g[context->mixChannel] == context) { ALint processed = 0; ALuint buffer = 0; - alGetSourcei(context->alSource, AL_BUFFERS_PROCESSED, &processed); // Get the number of already processed buffers (if any) + unsigned short numberProcessed = 0; + unsigned short numberRemaining = numberElements; + + alGetSourcei(context->alSource, AL_BUFFERS_PROCESSED, &processed); // Get the number of already processed buffers (if any) if(!processed) return 0;//nothing to process, queue is still full - if (!data || !numberElements)// play silence - { - while (processed > 0) - { - alSourceUnqueueBuffers(context->alSource, 1, &buffer); - numberProcessed += FillAlBufferWithSilence(context, buffer); - alSourceQueueBuffers(context->alSource, 1, &buffer); - processed--; - } - } if(numberRemaining)// buffer data stream in increments of MUSIC_BUFFER_SIZE { while (processed > 0) diff --git a/src/easings.h b/src/easings.h index a198be4d..e1e5465a 100644 --- a/src/easings.h +++ b/src/easings.h @@ -7,6 +7,23 @@ * This header uses: * #define EASINGS_STATIC_INLINE // Inlines all functions code, so it runs faster. * // This requires lots of memory on system. +* How to use: +* The four inputs t,b,c,d are defined as follows: +* t = current time in milliseconds +* b = starting position in only one dimension [X || Y || Z] your choice +* c = the total change in value of b that needs to occur +* d = total time it should take to complete +* +* Example: +* float speed = 1.f; +* float currentTime = 0.f; +* float currentPos[2] = {0,0}; +* float newPos[2] = {1,1}; +* float tempPosition[2] = currentPos;//x,y positions +* while(currentPos[0] < newPos[0]) +* currentPos[0] = EaseSineIn(currentTime, tempPosition[0], tempPosition[0]-newPos[0], speed); +* currentPos[1] = EaseSineIn(currentTime, tempPosition[1], tempPosition[1]-newPos[0], speed); +* currentTime += diffTime(); * * A port of Robert Penner's easing equations to C (http://robertpenner.com/easing/) * -- cgit v1.2.3 From e94acf86f8125edfb6534a45c86493be298fea68 Mon Sep 17 00:00:00 2001 From: raysan5 Date: Tue, 3 May 2016 17:54:50 +0200 Subject: Reorganized internal funcs --- src/text.c | 98 ++++++++++++++++++++++++++------------------------------------ 1 file changed, 41 insertions(+), 57 deletions(-) (limited to 'src') diff --git a/src/text.c b/src/text.c index f89d5e4e..7bb06f44 100644 --- a/src/text.c +++ b/src/text.c @@ -69,8 +69,7 @@ static SpriteFont defaultFont; // Default font provided by raylib //---------------------------------------------------------------------------------- // Module specific Functions Declaration //---------------------------------------------------------------------------------- -static bool PixelIsMagenta(Color p); // Check if a pixel is magenta -static int ParseImageData(Image image, int **charValues, Rectangle **charSet); // Parse image pixel data to obtain characters data +static SpriteFont LoadImageFont(Image image, Color key, int firstChar); // Load a Image font file (XNA style) static SpriteFont LoadRBMF(const char *fileName); // Load a rBMF font file (raylib BitMap Font) static SpriteFont LoadBMFont(const char *fileName); // Load a BMFont file (AngelCode font file) static SpriteFont LoadTTF(const char *fileName, int fontSize); // Generate a sprite font image from TTF data (font size required) @@ -210,7 +209,7 @@ extern void LoadDefaultFont(void) if (testPosX >= defaultFont.texture.width) { currentLine++; - currentPosX = 2 * charsDivisor + charsWidth[i]; + currentPosX = 2*charsDivisor + charsWidth[i]; testPosX = currentPosX; defaultFont.charRecs[i].x = charsDivisor; @@ -246,7 +245,7 @@ SpriteFont GetDefaultFont() // Load a SpriteFont image into GPU memory SpriteFont LoadSpriteFont(const char *fileName) { - SpriteFont spriteFont; + SpriteFont spriteFont = { 0 }; // Check file extension if (strcmp(GetExtension(fileName),"rbmf") == 0) spriteFont = LoadRBMF(fileName); @@ -255,36 +254,7 @@ SpriteFont LoadSpriteFont(const char *fileName) else { Image image = LoadImage(fileName); - - if (image.data != NULL) - { - // Process bitmap font pixel data to get characters measures - // spriteFont chars data is filled inside the function and memory is allocated! - int numChars = ParseImageData(image, &spriteFont.charValues, &spriteFont.charRecs); - - TraceLog(DEBUG, "[%s] SpriteFont data parsed correctly", fileName); - TraceLog(DEBUG, "[%s] SpriteFont num chars detected: %i", fileName, numChars); - - spriteFont.numChars = numChars; - spriteFont.texture = LoadTextureFromImage(image); // Convert loaded image to OpenGL texture - spriteFont.size = spriteFont.charRecs[0].height; - - spriteFont.charOffsets = (Vector2 *)malloc(spriteFont.numChars*sizeof(Vector2)); - spriteFont.charAdvanceX = (int *)malloc(spriteFont.numChars*sizeof(int)); - - for (int i = 0; i < spriteFont.numChars; i++) - { - // NOTE: On image based fonts (XNA style), character offsets and xAdvance are not required (set to 0) - spriteFont.charOffsets[i] = (Vector2){ 0.0f, 0.0f }; - spriteFont.charAdvanceX[i] = 0; - } - } - else - { - TraceLog(WARNING, "[%s] SpriteFont could not be loaded, using default font", fileName); - spriteFont = GetDefaultFont(); - } - + if (image.data != NULL) spriteFont = LoadImageFont(image, MAGENTA, 32); UnloadImage(image); } @@ -309,7 +279,7 @@ void UnloadSpriteFont(SpriteFont spriteFont) free(spriteFont.charOffsets); free(spriteFont.charAdvanceX); - TraceLog(INFO, "Unloaded sprite font data"); + TraceLog(DEBUG, "Unloaded sprite font data"); } } @@ -517,15 +487,11 @@ void DrawFPS(int posX, int posY) // Module specific Functions Definition //---------------------------------------------------------------------------------- -// Check if a pixel is magenta -static bool PixelIsMagenta(Color p) -{ - return ((p.r == 255) && (p.g == 0) && (p.b == 255) && (p.a == 255)); -} - -// Parse image pixel data to obtain characters data (measures) -static int ParseImageData(Image image, int **charValues, Rectangle **charRecs) +// Load a Image font file (XNA style) +static SpriteFont LoadImageFont(Image image, Color key, int firstChar) { + #define COLOR_EQUAL(col1, col2) ((col1.r == col2.r)&&(col1.g == col2.g)&&(col1.b == col2.b)&&(col1.a == col2.a)) + int charSpacing = 0; int lineSpacing = 0; @@ -539,13 +505,14 @@ static int ParseImageData(Image image, int **charValues, Rectangle **charRecs) Color *pixels = GetImageData(image); + // Parse image data to get charSpacing and lineSpacing for(y = 0; y < image.height; y++) { for(x = 0; x < image.width; x++) { - if (!PixelIsMagenta(pixels[y*image.width + x])) break; + if (!COLOR_EQUAL(pixels[y*image.width + x], key)) break; } - if (!PixelIsMagenta(pixels[y*image.width + x])) break; + if (!COLOR_EQUAL(pixels[y*image.width + x], key)) break; } charSpacing = x; @@ -554,7 +521,7 @@ static int ParseImageData(Image image, int **charValues, Rectangle **charRecs) int charHeight = 0; int j = 0; - while(!PixelIsMagenta(pixels[(lineSpacing + j)*image.width + charSpacing])) j++; + while(!COLOR_EQUAL(pixels[(lineSpacing + j)*image.width + charSpacing], key)) j++; charHeight = j; @@ -563,12 +530,13 @@ static int ParseImageData(Image image, int **charValues, Rectangle **charRecs) int lineToRead = 0; int xPosToRead = charSpacing; + // Parse image data to get rectangle sizes while((lineSpacing + lineToRead * (charHeight + lineSpacing)) < image.height) { while((xPosToRead < image.width) && - !PixelIsMagenta((pixels[(lineSpacing + (charHeight+lineSpacing)*lineToRead)*image.width + xPosToRead]))) + !COLOR_EQUAL((pixels[(lineSpacing + (charHeight+lineSpacing)*lineToRead)*image.width + xPosToRead]), key)) { - tempCharValues[index] = FONT_FIRST_CHAR + index; + tempCharValues[index] = firstChar + index; tempCharRecs[index].x = xPosToRead; tempCharRecs[index].y = lineSpacing + lineToRead * (charHeight + lineSpacing); @@ -576,7 +544,7 @@ static int ParseImageData(Image image, int **charValues, Rectangle **charRecs) int charWidth = 0; - while(!PixelIsMagenta(pixels[(lineSpacing + (charHeight+lineSpacing)*lineToRead)*image.width + xPosToRead + charWidth])) charWidth++; + while(!COLOR_EQUAL(pixels[(lineSpacing + (charHeight+lineSpacing)*lineToRead)*image.width + xPosToRead + charWidth], key)) charWidth++; tempCharRecs[index].width = charWidth; @@ -590,20 +558,35 @@ static int ParseImageData(Image image, int **charValues, Rectangle **charRecs) } free(pixels); + + TraceLog(DEBUG, "SpriteFont data parsed correctly from image"); + + // Create spritefont with all data parsed from image + SpriteFont spriteFont = { 0 }; + + spriteFont.texture = LoadTextureFromImage(image); // Convert loaded image to OpenGL texture + spriteFont.numChars = index; // We got tempCharValues and tempCharsRecs populated with chars data - // Now we move temp data to sized charValues and charRecs arrays (passed as parameter to the function) - // NOTE: This memory should be freed! - (*charRecs) = (Rectangle *)malloc(index*sizeof(Rectangle)); - (*charValues) = (int *)malloc(index*sizeof(int)); + // Now we move temp data to sized charValues and charRecs arrays + spriteFont.charRecs = (Rectangle *)malloc(spriteFont.numChars*sizeof(Rectangle)); + spriteFont.charValues = (int *)malloc(spriteFont.numChars*sizeof(int)); + spriteFont.charOffsets = (Vector2 *)malloc(spriteFont.numChars*sizeof(Vector2)); + spriteFont.charAdvanceX = (int *)malloc(spriteFont.numChars*sizeof(int)); - for (int i = 0; i < index; i++) + for (int i = 0; i < spriteFont.numChars; i++) { - (*charValues)[i] = tempCharValues[i]; - (*charRecs)[i] = tempCharRecs[i]; + spriteFont.charValues[i] = tempCharValues[i]; + spriteFont.charRecs[i] = tempCharRecs[i]; + + // NOTE: On image based fonts (XNA style), character offsets and xAdvance are not required (set to 0) + spriteFont.charOffsets[i] = (Vector2){ 0.0f, 0.0f }; + spriteFont.charAdvanceX[i] = 0; } + + spriteFont.size = spriteFont.charRecs[0].height; - return index; + return spriteFont; } // Load a rBMF font file (raylib BitMap Font) @@ -687,6 +670,7 @@ static SpriteFont LoadRBMF(const char *fileName) TraceLog(DEBUG, "[%s] Image reconstructed correctly, now converting it to texture", fileName); + // Create spritefont with all data read from rbmf file spriteFont.texture = LoadTextureFromImage(image); UnloadImage(image); // Unload image data -- cgit v1.2.3 From 5f73850fa675530d6933d85a6d80684106beff69 Mon Sep 17 00:00:00 2001 From: raysan5 Date: Tue, 3 May 2016 18:04:21 +0200 Subject: Renamed functions for consistency --- src/audio.c | 4 ++-- src/audio.h | 4 ++-- src/raylib.h | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/audio.c b/src/audio.c index 39d1ee8b..09c91785 100644 --- a/src/audio.c +++ b/src/audio.c @@ -585,7 +585,7 @@ void StopSound(Sound sound) } // Check if a sound is playing -bool SoundIsPlaying(Sound sound) +bool IsSoundPlaying(Sound sound) { bool playing = false; ALint state; @@ -764,7 +764,7 @@ void ResumeMusicStream(void) } // Check if music is playing -bool MusicIsPlaying(void) +bool IsMusicPlaying(void) { bool playing = false; ALint state; diff --git a/src/audio.h b/src/audio.h index 9c681044..73f60ab1 100644 --- a/src/audio.h +++ b/src/audio.h @@ -96,7 +96,7 @@ void UnloadSound(Sound sound); // Unload sound void PlaySound(Sound sound); // Play a sound void PauseSound(Sound sound); // Pause a sound void StopSound(Sound sound); // Stop playing a sound -bool SoundIsPlaying(Sound sound); // Check if a sound is currently playing +bool IsSoundPlaying(Sound sound); // Check if a sound is currently playing void SetSoundVolume(Sound sound, float volume); // Set volume for a sound (1.0 is max level) void SetSoundPitch(Sound sound, float pitch); // Set pitch for a sound (1.0 is base level) @@ -105,7 +105,7 @@ void UpdateMusicStream(void); // Updates buffe void StopMusicStream(void); // Stop music playing (close stream) void PauseMusicStream(void); // Pause music playing void ResumeMusicStream(void); // Resume playing paused music -bool MusicIsPlaying(void); // Check if music is playing +bool IsMusicPlaying(void); // Check if music is playing void SetMusicVolume(float volume); // Set volume for music (1.0 is max level) float GetMusicTimeLength(void); // Get music time length (in seconds) float GetMusicTimePlayed(void); // Get current music time played (in seconds) diff --git a/src/raylib.h b/src/raylib.h index 337b9813..bc98181b 100644 --- a/src/raylib.h +++ b/src/raylib.h @@ -882,7 +882,7 @@ void UnloadSound(Sound sound); // Unload sound void PlaySound(Sound sound); // Play a sound void PauseSound(Sound sound); // Pause a sound void StopSound(Sound sound); // Stop playing a sound -bool SoundIsPlaying(Sound sound); // Check if a sound is currently playing +bool IsSoundPlaying(Sound sound); // Check if a sound is currently playing void SetSoundVolume(Sound sound, float volume); // Set volume for a sound (1.0 is max level) void SetSoundPitch(Sound sound, float pitch); // Set pitch for a sound (1.0 is base level) @@ -891,7 +891,7 @@ void UpdateMusicStream(void); // Updates buffe void StopMusicStream(void); // Stop music playing (close stream) void PauseMusicStream(void); // Pause music playing void ResumeMusicStream(void); // Resume playing paused music -bool MusicIsPlaying(void); // Check if music is playing +bool IsMusicPlaying(void); // Check if music is playing void SetMusicVolume(float volume); // Set volume for music (1.0 is max level) float GetMusicTimeLength(void); // Get current music time length (in seconds) float GetMusicTimePlayed(void); // Get current music time played (in seconds) -- cgit v1.2.3 From 8301980ba8f917dd445b346332c48ec3b2447511 Mon Sep 17 00:00:00 2001 From: raysan5 Date: Tue, 3 May 2016 19:20:25 +0200 Subject: Clean up and consistency review - Renamed some functions for consistency (default buffers) - Removed mystrdup() function (implemented inline) - Renamed TextFileRead() to ReadTextFile() --- src/rlgl.c | 299 +++++++++++++++++++++++++++++++------------------------------ src/rlgl.h | 16 +--- 2 files changed, 158 insertions(+), 157 deletions(-) (limited to 'src') diff --git a/src/rlgl.c b/src/rlgl.c index e021e726..9112e47e 100644 --- a/src/rlgl.c +++ b/src/rlgl.c @@ -196,23 +196,20 @@ static DrawMode currentDrawMode; static float currentDepth = -1.0f; -// Vertex arrays for lines, triangles and quads +// Default vertex buffers for lines, triangles and quads static VertexPositionColorBuffer lines; // No texture support static VertexPositionColorBuffer triangles; // No texture support static VertexPositionColorTextureIndexBuffer quads; -// Shader Programs -static Shader defaultShader; -static Shader currentShader; // By default, defaultShader - -// Vertex Array Objects (VAO) +// Default vertex buffers VAOs (if supported) static GLuint vaoLines, vaoTriangles, vaoQuads; -// Vertex Buffer Objects (VBO) -static GLuint linesBuffer[2]; -static GLuint trianglesBuffer[2]; -static GLuint quadsBuffer[4]; +// Default vertex buffers VBOs +static GLuint linesBuffer[2]; // Lines buffers (position, color) +static GLuint trianglesBuffer[2]; // Triangles buffers (position, color) +static GLuint quadsBuffer[4]; // Quads buffers (position, texcoord, color, index) +// Default buffers draw calls static DrawCall *draws; static int drawsCounter; @@ -221,11 +218,14 @@ static Vector3 *tempBuffer; static int tempBufferCount = 0; static bool useTempBuffer = false; +// Shader Programs +static Shader defaultShader; +static Shader currentShader; // By default, defaultShader + // Flags for supported extensions static bool vaoSupported = false; // VAO support (OpenGL ES2 could not support VAO extension) // Compressed textures support flags -//static bool texCompDXTSupported = false; // DDS texture compression support static bool texCompETC1Supported = false; // ETC1 texture compression support static bool texCompETC2Supported = false; // ETC2/EAC texture compression support static bool texCompPVRTSupported = false; // PVR texture compression support @@ -233,8 +233,8 @@ static bool texCompASTCSupported = false; // ASTC texture compression support #endif // Compressed textures support flags -static bool texCompDXTSupported = false; // DDS texture compression support -static bool npotSupported = false; // NPOT textures full support +static bool texCompDXTSupported = false; // DDS texture compression support +static bool npotSupported = false; // NPOT textures full support #if defined(GRAPHICS_API_OPENGL_ES2) // NOTE: VAO functionality is exposed through extensions (OES) @@ -254,14 +254,17 @@ unsigned int whiteTexture; // Module specific Functions Declaration //---------------------------------------------------------------------------------- #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) +static void LoadCompressedTexture(unsigned char *data, int width, int height, int mipmapCount, int compressedFormat); + static Shader LoadDefaultShader(void); static void LoadDefaultShaderLocations(Shader *shader); -static void InitializeBuffers(void); -static void InitializeBuffersGPU(void); -static void UpdateBuffers(void); -static char *TextFileRead(char *fn); +static void UnloadDefaultShader(void); -static void LoadCompressedTexture(unsigned char *data, int width, int height, int mipmapCount, int compressedFormat); +static void LoadDefaultBuffers(void); +static void UpdateDefaultBuffers(void); +static void UnloadDefaultBuffers(void); + +static char *ReadTextFile(const char *fileName); #endif #if defined(GRAPHICS_API_OPENGL_11) @@ -274,20 +277,6 @@ static void TraceLog(int msgType, const char *text, ...); float *MatrixToFloat(Matrix mat); // Converts Matrix to float array #endif -#if defined(GRAPHICS_API_OPENGL_ES2) -// NOTE: strdup() functions replacement (not C99, POSIX function, not available on emscripten) -// Duplicates a string, returning an identical malloc'd string -char *mystrdup(const char *str) -{ - size_t len = strlen(str) + 1; - void *newstr = malloc(len); - - if (newstr == NULL) return NULL; - - return (char *)memcpy(newstr, str, len); -} -#endif - //---------------------------------------------------------------------------------- // Module Functions Definition - Matrix operations //---------------------------------------------------------------------------------- @@ -919,13 +908,18 @@ void rlglInit(void) // NOTE: We have to duplicate string because glGetString() returns a const value // If not duplicated, it fails in some systems (Raspberry Pi) - char *extensionsDup = mystrdup(extensions); + // Equivalent to function: char *strdup(const char *str) + char *extensionsDup; + size_t len = strlen(extensions) + 1; + void *newstr = malloc(len); + if (newstr == NULL) extensionsDup = NULL; + extensionsDup = (char *)memcpy(newstr, extensions, len); // NOTE: String could be splitted using strtok() function (string.h) // NOTE: strtok() modifies the received string, it can not be const char *extList[512]; // Allocate 512 strings pointers (2 KB) - + extList[numExt] = strtok(extensionsDup, " "); while (extList[numExt] != NULL) @@ -1024,12 +1018,9 @@ void rlglInit(void) // Init default Shader (customized for GL 3.3 and ES2) defaultShader = LoadDefaultShader(); - //customShader = LoadShader("custom.vs", "custom.fs"); // Works ok - currentShader = defaultShader; - InitializeBuffers(); // Init vertex arrays - InitializeBuffersGPU(); // Init VBO and VAO + LoadDefaultBuffers(); // Initialize default vertex arrays buffers (lines, triangles, quads) // Init temp vertex buffer, used when transformation required (translate, rotate, scale) tempBuffer = (Vector3 *)malloc(sizeof(Vector3)*TEMP_VERTEX_BUFFER_SIZE); @@ -1054,54 +1045,10 @@ void rlglInit(void) void rlglClose(void) { #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) - // Unbind everything - if (vaoSupported) glBindVertexArray(0); - glDisableVertexAttribArray(0); - glDisableVertexAttribArray(1); - glDisableVertexAttribArray(2); - glDisableVertexAttribArray(3); - glBindBuffer(GL_ARRAY_BUFFER, 0); - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); - - glUseProgram(0); - - // Delete VBOs - glDeleteBuffers(1, &linesBuffer[0]); - glDeleteBuffers(1, &linesBuffer[1]); - glDeleteBuffers(1, &trianglesBuffer[0]); - glDeleteBuffers(1, &trianglesBuffer[1]); - glDeleteBuffers(1, &quadsBuffer[0]); - glDeleteBuffers(1, &quadsBuffer[1]); - glDeleteBuffers(1, &quadsBuffer[2]); - glDeleteBuffers(1, &quadsBuffer[3]); - - if (vaoSupported) - { - // Delete VAOs - glDeleteVertexArrays(1, &vaoLines); - glDeleteVertexArrays(1, &vaoTriangles); - glDeleteVertexArrays(1, &vaoQuads); - } - - //glDetachShader(defaultShaderProgram, vertexShader); - //glDetachShader(defaultShaderProgram, fragmentShader); - //glDeleteShader(vertexShader); // Already deleted on shader compilation - //glDeleteShader(fragmentShader); // Already deleted on sahder compilation - glDeleteProgram(defaultShader.id); - - // Free vertex arrays memory - free(lines.vertices); - free(lines.colors); - - free(triangles.vertices); - free(triangles.colors); - - free(quads.vertices); - free(quads.texcoords); - free(quads.colors); - free(quads.indices); - - // Free GPU texture + UnloadDefaultShader(); + UnloadDefaultBuffers(); + + // Delete default white texture glDeleteTextures(1, &whiteTexture); TraceLog(INFO, "[TEX ID %i] Unloaded texture data (base white texture) from VRAM", whiteTexture); @@ -1113,7 +1060,7 @@ void rlglClose(void) void rlglDraw(void) { #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) - UpdateBuffers(); + UpdateDefaultBuffers(); if ((lines.vCounter > 0) || (triangles.vCounter > 0) || (quads.vCounter > 0)) { @@ -1846,7 +1793,9 @@ void rlglGenerateMipmaps(Texture2D texture) // NOTE: Once mipmaps have been generated and data has been uploaded to GPU VRAM, we can discard RAM data free(data); -#elif defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) +#endif + +#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) glGenerateMipmap(GL_TEXTURE_2D); // Generate mipmaps automatically TraceLog(INFO, "[TEX ID %i] Mipmaps generated automatically", texture.id); @@ -2116,8 +2065,8 @@ Shader LoadShader(char *vsFileName, char *fsFileName) #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) // Shaders loading from external text file - char *vShaderStr = TextFileRead(vsFileName); - char *fShaderStr = TextFileRead(fsFileName); + char *vShaderStr = ReadTextFile(vsFileName); + char *fShaderStr = ReadTextFile(fsFileName); if ((vShaderStr != NULL) && (fShaderStr != NULL)) { @@ -2418,7 +2367,7 @@ static void LoadCompressedTexture(unsigned char *data, int width, int height, in } } -// Load Shader (Vertex and Fragment) +// Load default shader (Vertex and Fragment) // NOTE: This shader program is used for batch buffers (lines, triangles, quads) static Shader LoadDefaultShader(void) { @@ -2503,43 +2452,24 @@ static void LoadDefaultShaderLocations(Shader *shader) shader->mapSpecularLoc = glGetUniformLocation(shader->id, "texture2"); } -// Read text file -// NOTE: text chars array should be freed manually -static char *TextFileRead(char *fileName) +// Unload default shader +static void UnloadDefaultShader(void) { - FILE *textFile; - char *text = NULL; - - int count = 0; - - if (fileName != NULL) - { - textFile = fopen(fileName,"rt"); - - if (textFile != NULL) - { - fseek(textFile, 0, SEEK_END); - count = ftell(textFile); - rewind(textFile); - - if (count > 0) - { - text = (char *)malloc(sizeof(char)*(count + 1)); - count = fread(text, sizeof(char), count, textFile); - text[count] = '\0'; - } - - fclose(textFile); - } - else TraceLog(WARNING, "[%s] Text file could not be opened", fileName); - } + glUseProgram(0); - return text; + //glDetachShader(defaultShaderProgram, vertexShader); + //glDetachShader(defaultShaderProgram, fragmentShader); + //glDeleteShader(vertexShader); // Already deleted on shader compilation + //glDeleteShader(fragmentShader); // Already deleted on sahder compilation + glDeleteProgram(defaultShader.id); } -// Allocate and initialize float array buffers to store vertex data (lines, triangles, quads) -static void InitializeBuffers(void) +// Load default internal buffers (lines, triangles, quads) +static void LoadDefaultBuffers(void) { + // [CPU] Allocate and initialize float array buffers to store vertex data (lines, triangles, quads) + //-------------------------------------------------------------------------------------------- + // Initialize lines arrays (vertex position and color data) lines.vertices = (float *)malloc(sizeof(float)*3*2*MAX_LINES_BATCH); // 3 float by vertex, 2 vertex by line lines.colors = (unsigned char *)malloc(sizeof(unsigned char)*4*2*MAX_LINES_BATCH); // 4 float by color, 2 colors by line @@ -2593,13 +2523,14 @@ static void InitializeBuffers(void) quads.tcCounter = 0; quads.cCounter = 0; - TraceLog(INFO, "CPU buffers (lines, triangles, quads) initialized successfully"); -} - -// Initialize Vertex Array Objects (Contain VBO) -// NOTE: lines, triangles and quads buffers use currentShader -static void InitializeBuffersGPU(void) -{ + TraceLog(INFO, "Default buffers initialized successfully in CPU (lines, triangles, quads)"); + //-------------------------------------------------------------------------------------------- + + // [GPU] Upload vertex data and initialize VAOs/VBOs (lines, triangles, quads) + // NOTE: Default buffers are linked to use currentShader (defaultShader) + //-------------------------------------------------------------------------------------------- + + // Upload and link lines vertex buffers if (vaoSupported) { // Initialize Lines VAO @@ -2622,10 +2553,10 @@ static void InitializeBuffersGPU(void) glEnableVertexAttribArray(currentShader.colorLoc); glVertexAttribPointer(currentShader.colorLoc, 4, GL_UNSIGNED_BYTE, GL_TRUE, 0, 0); - if (vaoSupported) TraceLog(INFO, "[VAO ID %i] Lines VAO initialized successfully", vaoLines); - else TraceLog(INFO, "[VBO ID %i][VBO ID %i] Lines VBOs initialized successfully", linesBuffer[0], linesBuffer[1]); - //-------------------------------------------------------------- + if (vaoSupported) TraceLog(INFO, "[VAO ID %i] Default buffers (lines) VAO initialized successfully", vaoLines); + else TraceLog(INFO, "[VBO ID %i][VBO ID %i] Default buffers (lines) VBOs initialized successfully", linesBuffer[0], linesBuffer[1]); + // Upload and link triangles vertex buffers if (vaoSupported) { // Initialize Triangles VAO @@ -2647,10 +2578,10 @@ static void InitializeBuffersGPU(void) glEnableVertexAttribArray(currentShader.colorLoc); glVertexAttribPointer(currentShader.colorLoc, 4, GL_UNSIGNED_BYTE, GL_TRUE, 0, 0); - if (vaoSupported) TraceLog(INFO, "[VAO ID %i] Triangles VAO initialized successfully", vaoTriangles); - else TraceLog(INFO, "[VBO ID %i][VBO ID %i] Triangles VBOs initialized successfully", trianglesBuffer[0], trianglesBuffer[1]); - //-------------------------------------------------------------- + if (vaoSupported) TraceLog(INFO, "[VAO ID %i] Default buffers (triangles) VAO initialized successfully", vaoTriangles); + else TraceLog(INFO, "[VBO ID %i][VBO ID %i] Default buffers (triangles) VBOs initialized successfully", trianglesBuffer[0], trianglesBuffer[1]); + // Upload and link quads vertex buffers if (vaoSupported) { // Initialize Quads VAO @@ -2685,18 +2616,20 @@ static void InitializeBuffersGPU(void) glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(short)*6*MAX_QUADS_BATCH, quads.indices, GL_STATIC_DRAW); #endif - if (vaoSupported) TraceLog(INFO, "[VAO ID %i] Quads VAO initialized successfully", vaoQuads); - else TraceLog(INFO, "[VBO ID %i][VBO ID %i][VBO ID %i][VBO ID %i] Quads VBOs initialized successfully", quadsBuffer[0], quadsBuffer[1], quadsBuffer[2], quadsBuffer[3]); + if (vaoSupported) TraceLog(INFO, "[VAO ID %i] Default buffers (quads) VAO initialized successfully", vaoQuads); + else TraceLog(INFO, "[VBO ID %i][VBO ID %i][VBO ID %i][VBO ID %i] Default buffers (quads) VBOs initialized successfully", quadsBuffer[0], quadsBuffer[1], quadsBuffer[2], quadsBuffer[3]); // Unbind the current VAO if (vaoSupported) glBindVertexArray(0); + //-------------------------------------------------------------------------------------------- } -// Update VBOs with vertex array data +// Update default buffers (VAOs/VBOs) with vertex array data // NOTE: If there is not vertex data, buffers doesn't need to be updated (vertexCount > 0) -// TODO: If no data changed on the CPU arrays --> No need to update GPU arrays (change flag required) -static void UpdateBuffers(void) +// TODO: If no data changed on the CPU arrays --> No need to re-update GPU arrays (change flag required) +static void UpdateDefaultBuffers(void) { + // Update lines vertex buffers if (lines.vCounter > 0) { // Activate Lines VAO @@ -2712,8 +2645,8 @@ static void UpdateBuffers(void) //glBufferData(GL_ARRAY_BUFFER, sizeof(float)*4*2*MAX_LINES_BATCH, lines.colors, GL_DYNAMIC_DRAW); glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(unsigned char)*4*lines.cCounter, lines.colors); } - //-------------------------------------------------------------- + // Update triangles vertex buffers if (triangles.vCounter > 0) { // Activate Triangles VAO @@ -2729,8 +2662,8 @@ static void UpdateBuffers(void) //glBufferData(GL_ARRAY_BUFFER, sizeof(float)*4*3*MAX_TRIANGLES_BATCH, triangles.colors, GL_DYNAMIC_DRAW); glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(unsigned char)*4*triangles.cCounter, triangles.colors); } - //-------------------------------------------------------------- + // Update quads vertex buffers if (quads.vCounter > 0) { // Activate Quads VAO @@ -2752,7 +2685,7 @@ static void UpdateBuffers(void) glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(unsigned char)*4*quads.vCounter, quads.colors); // Another option would be using buffer mapping... - //triangles.vertices = glMapBuffer(GL_ARRAY_BUFFER, GL_READ_WRITE); + //quads.vertices = glMapBuffer(GL_ARRAY_BUFFER, GL_READ_WRITE); // Now we can modify vertices //glUnmapBuffer(GL_ARRAY_BUFFER); } @@ -2761,6 +2694,83 @@ static void UpdateBuffers(void) // Unbind the current VAO if (vaoSupported) glBindVertexArray(0); } + +// Unload default buffers vertex data from CPU and GPU +static void UnloadDefaultBuffers(void) +{ + // Unbind everything + if (vaoSupported) glBindVertexArray(0); + glDisableVertexAttribArray(0); + glDisableVertexAttribArray(1); + glDisableVertexAttribArray(2); + glDisableVertexAttribArray(3); + glBindBuffer(GL_ARRAY_BUFFER, 0); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); + + // Delete VBOs from GPU (VRAM) + glDeleteBuffers(1, &linesBuffer[0]); + glDeleteBuffers(1, &linesBuffer[1]); + glDeleteBuffers(1, &trianglesBuffer[0]); + glDeleteBuffers(1, &trianglesBuffer[1]); + glDeleteBuffers(1, &quadsBuffer[0]); + glDeleteBuffers(1, &quadsBuffer[1]); + glDeleteBuffers(1, &quadsBuffer[2]); + glDeleteBuffers(1, &quadsBuffer[3]); + + if (vaoSupported) + { + // Delete VAOs from GPU (VRAM) + glDeleteVertexArrays(1, &vaoLines); + glDeleteVertexArrays(1, &vaoTriangles); + glDeleteVertexArrays(1, &vaoQuads); + } + + // Free vertex arrays memory from CPU (RAM) + free(lines.vertices); + free(lines.colors); + + free(triangles.vertices); + free(triangles.colors); + + free(quads.vertices); + free(quads.texcoords); + free(quads.colors); + free(quads.indices); +} + +// Read text data from file +// NOTE: text chars array should be freed manually +static char *ReadTextFile(const char *fileName) +{ + FILE *textFile; + char *text = NULL; + + int count = 0; + + if (fileName != NULL) + { + textFile = fopen(fileName,"rt"); + + if (textFile != NULL) + { + fseek(textFile, 0, SEEK_END); + count = ftell(textFile); + rewind(textFile); + + if (count > 0) + { + text = (char *)malloc(sizeof(char)*(count + 1)); + count = fread(text, sizeof(char), count, textFile); + text[count] = '\0'; + } + + fclose(textFile); + } + else TraceLog(WARNING, "[%s] Text file could not be opened", fileName); + } + + return text; +} #endif //defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) #if defined(GRAPHICS_API_OPENGL_11) @@ -2891,7 +2901,6 @@ static Color *GenNextMipmap(Color *srcData, int srcWidth, int srcHeight) #endif #if defined(RLGL_STANDALONE) - // Output a trace log message // NOTE: Expected msgType: (0)Info, (1)Error, (2)Warning static void TraceLog(int msgType, const char *text, ...) diff --git a/src/rlgl.h b/src/rlgl.h index 714961e1..cd8e6d1d 100644 --- a/src/rlgl.h +++ b/src/rlgl.h @@ -292,11 +292,6 @@ Vector3 rlglUnproject(Vector3 source, Matrix proj, Matrix view); // Get world unsigned char *rlglReadScreenPixels(int width, int height); // Read screen pixel data (color buffer) void *rlglReadTexturePixels(Texture2D texture); // Read texture pixel data -#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) -void PrintProjectionMatrix(void); // DEBUG: Print projection matrix -void PrintModelviewMatrix(void); // DEBUG: Print modelview matrix -#endif - #if defined(RLGL_STANDALONE) //------------------------------------------------------------------------------------ // Shaders System Functions (Module: rlgl) @@ -309,13 +304,10 @@ void SetCustomShader(Shader shader); // Set custo void SetDefaultShader(void); // Set default shader to be used in batch draw void SetModelShader(Model *model, Shader shader); // Link a shader to a model -int GetShaderLocation(Shader shader, const char *uniformName); // Get shader uniform location -void SetShaderValue(Shader shader, int uniformLoc, float *value, int size); // Set shader uniform value (float) -void SetShaderValuei(Shader shader, int uniformLoc, int *value, int size); // Set shader uniform value (int) -void SetShaderMapDiffuse(Shader *shader, Texture2D texture); // Default diffuse shader map texture assignment -void SetShaderMapNormal(Shader *shader, const char *uniformName, Texture2D texture); // Normal map texture shader assignment -void SetShaderMapSpecular(Shader *shader, const char *uniformName, Texture2D texture); // Specular map texture shader assignment -void SetShaderMap(Shader *shader, int mapLocation, Texture2D texture, int textureUnit); // TODO: Generic shader map assignment +int GetShaderLocation(Shader shader, const char *uniformName); // Get shader uniform location +void SetShaderValue(Shader shader, int uniformLoc, float *value, int size); // Set shader uniform value (float) +void SetShaderValuei(Shader shader, int uniformLoc, int *value, int size); // Set shader uniform value (int) +void SetShaderValueMatrix(Shader shader, int uniformLoc, Matrix mat); // Set shader uniform value (matrix 4x4) void SetBlendMode(int mode); // Set blending mode (alpha, additive, multiplied) #endif -- cgit v1.2.3 From fd67e31f630476980eb09e49177958703db5b3d3 Mon Sep 17 00:00:00 2001 From: raysan5 Date: Tue, 3 May 2016 19:27:06 +0200 Subject: Renamed function for consistency --- src/core.c | 4 ++-- src/raylib.h | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/core.c b/src/core.c index b27712a7..669010f9 100644 --- a/src/core.c +++ b/src/core.c @@ -905,7 +905,7 @@ Ray GetMouseRay(Vector2 mousePosition, Camera camera) Vector3 farPoint = rlglUnproject((Vector3){ deviceCoords.x, deviceCoords.y, 1.0f }, matProj, matView); #else // OPTION 2: Compute unprojection directly here - + // Calculate unproject matrix (multiply projection matrix and view matrix) and invert it Matrix matProjView = MatrixMultiply(matProj, matView); MatrixInvert(&matProjView); @@ -935,7 +935,7 @@ Ray GetMouseRay(Vector2 mousePosition, Camera camera) } // Returns the screen space position from a 3d world space position -Vector2 WorldToScreen(Vector3 position, Camera camera) +Vector2 GetWorldToScreen(Vector3 position, Camera camera) { // Calculate projection matrix (from perspective instead of frustum Matrix matProj = MatrixPerspective(camera.fovy, (double)GetScreenWidth()/(double)GetScreenHeight(), 0.01, 1000.0); diff --git a/src/raylib.h b/src/raylib.h index bc98181b..8af7d2fb 100644 --- a/src/raylib.h +++ b/src/raylib.h @@ -586,7 +586,7 @@ void BeginTextureMode(RenderTexture2D target); // Initializes rende void EndTextureMode(void); // Ends drawing to render texture Ray GetMouseRay(Vector2 mousePosition, Camera camera); // Returns a ray trace from mouse position -Vector2 WorldToScreen(Vector3 position, Camera camera); // Returns the screen space position from a 3d world space position +Vector2 GetWorldToScreen(Vector3 position, Camera camera); // Returns the screen space position from a 3d world space position Matrix GetCameraMatrix(Camera camera); // Returns camera transform matrix (view matrix) void SetTargetFPS(int fps); // Set target FPS (maximum) -- cgit v1.2.3 From ec72a8868e61507a3dfdc83bfe30c7110fc3051f Mon Sep 17 00:00:00 2001 From: raysan5 Date: Sat, 7 May 2016 18:04:22 +0200 Subject: Comment tweak --- src/core.c | 1 + 1 file changed, 1 insertion(+) (limited to 'src') diff --git a/src/core.c b/src/core.c index 669010f9..54d42f83 100644 --- a/src/core.c +++ b/src/core.c @@ -1448,6 +1448,7 @@ static void InitDisplay(int width, int height) glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); // Profiles Hint: Only 3.3 and above! // Other values: GLFW_OPENGL_ANY_PROFILE, GLFW_OPENGL_COMPAT_PROFILE glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_FALSE); // Fordward Compatibility Hint: Only 3.3 and above! + //glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, GL_TRUE); } if (fullscreen) -- cgit v1.2.3 From 7ab008878afa202b4f2e579567be7a7d87242661 Mon Sep 17 00:00:00 2001 From: raysan5 Date: Sat, 7 May 2016 18:07:15 +0200 Subject: Library redesign to accomodate materials system --- src/models.c | 164 ++++++++-- src/raylib.h | 31 +- src/rlgl.c | 989 +++++++++++++++++++++++++++++------------------------------ src/rlgl.h | 15 +- 4 files changed, 641 insertions(+), 558 deletions(-) (limited to 'src') diff --git a/src/models.c b/src/models.c index 0bb2b8d6..9b139120 100644 --- a/src/models.c +++ b/src/models.c @@ -55,7 +55,9 @@ extern unsigned int whiteTexture; //---------------------------------------------------------------------------------- // Module specific Functions Declaration //---------------------------------------------------------------------------------- -static Mesh LoadOBJ(const char *fileName); +static Mesh LoadOBJ(const char *fileName); // Load OBJ mesh data +static Material LoadMTL(const char *fileName); // Load MTL material data + static Mesh GenMeshHeightmap(Image image, Vector3 size); static Mesh GenMeshCubicmap(Image cubicmap, Vector3 cubeSize); @@ -542,24 +544,19 @@ void DrawGizmo(Vector3 position) Model LoadModel(const char *fileName) { Model model = { 0 }; - Mesh mesh = { 0 }; - // NOTE: Initialize default data for model in case loading fails, maybe a cube? + // TODO: Initialize default data for model in case loading fails, maybe a cube? - if (strcmp(GetExtension(fileName),"obj") == 0) mesh = LoadOBJ(fileName); + if (strcmp(GetExtension(fileName),"obj") == 0) model.mesh = LoadOBJ(fileName); else TraceLog(WARNING, "[%s] Model extension not recognized, it can't be loaded", fileName); - // NOTE: At this point we have all vertex, texcoord, normal data for the model in mesh struct - - if (mesh.vertexCount == 0) TraceLog(WARNING, "Model could not be loaded"); + if (model.mesh.vertexCount == 0) TraceLog(WARNING, "Model could not be loaded"); else { - // NOTE: model properties (transform, texture, shader) are initialized inside rlglLoadModel() - model = rlglLoadModel(mesh); // Upload vertex data to GPU - - // NOTE: Now that vertex data is uploaded to GPU VRAM, we can free arrays from CPU RAM - // We don't need CPU vertex data on OpenGL 3.3 or ES2... for static meshes... - // ...but we could keep CPU vertex data in case we need to update the mesh + rlglLoadMesh(&model.mesh); // Upload vertex data to GPU + + model.transform = MatrixIdentity(); + model.material = LoadDefaultMaterial(); } return model; @@ -568,12 +565,12 @@ Model LoadModel(const char *fileName) // Load a 3d model (from vertex data) Model LoadModelEx(Mesh data) { - Model model; + Model model = { 0 }; - // NOTE: model properties (transform, texture, shader) are initialized inside rlglLoadModel() - model = rlglLoadModel(data); // Upload vertex data to GPU + rlglLoadMesh(&data); // Upload vertex data to GPU - // NOTE: Vertex data is managed externally, must be deallocated manually + model.transform = MatrixIdentity(); + model.material = LoadDefaultMaterial(); return model; } @@ -582,8 +579,14 @@ Model LoadModelEx(Mesh data) // NOTE: model map size is defined in generic units Model LoadHeightmap(Image heightmap, Vector3 size) { - Mesh mesh = GenMeshHeightmap(heightmap, size); - Model model = rlglLoadModel(mesh); + Model model = { 0 }; + + model.mesh = GenMeshHeightmap(heightmap, size); + + rlglLoadMesh(&model.mesh); + + model.transform = MatrixIdentity(); + model.material = LoadDefaultMaterial(); return model; } @@ -591,8 +594,14 @@ Model LoadHeightmap(Image heightmap, Vector3 size) // Load a map image as a 3d model (cubes based) Model LoadCubicmap(Image cubicmap) { - Mesh mesh = GenMeshCubicmap(cubicmap, (Vector3){ 1.0, 1.0, 1.5f }); - Model model = rlglLoadModel(mesh); + Model model = { 0 }; + + model.mesh = GenMeshCubicmap(cubicmap, (Vector3){ 1.0, 1.0, 1.5f }); + + rlglLoadMesh(&model.mesh); + + model.transform = MatrixIdentity(); + model.material = LoadDefaultMaterial(); return model; } @@ -613,13 +622,44 @@ void UnloadModel(Model model) rlDeleteBuffers(model.mesh.vboId[0]); // vertex rlDeleteBuffers(model.mesh.vboId[1]); // texcoords rlDeleteBuffers(model.mesh.vboId[2]); // normals - //rlDeleteBuffers(model.mesh.vboId[3]); // texcoords2 (NOT USED) + //rlDeleteBuffers(model.mesh.vboId[3]); // colors (NOT USED) //rlDeleteBuffers(model.mesh.vboId[4]); // tangents (NOT USED) - //rlDeleteBuffers(model.mesh.vboId[5]); // colors (NOT USED) + //rlDeleteBuffers(model.mesh.vboId[5]); // texcoords2 (NOT USED) rlDeleteVertexArrays(model.mesh.vaoId); } +// Load material data (from file) +Material LoadMaterial(const char *fileName) +{ + Material material = { 0 }; + + if (strcmp(GetExtension(fileName),"mtl") == 0) material = LoadMTL(fileName); + else TraceLog(WARNING, "[%s] Material extension not recognized, it can't be loaded", fileName); + + return material; +} + +// Load default material (uses default models shader) +Material LoadDefaultMaterial(void) +{ + Material material = { 0 }; + + material.shader = GetDefaultShader(); + material.texDiffuse = GetDefaultTexture(); // White texture (1x1 pixel) + //material.texNormal; // NOTE: By default, not set + //material.texSpecular; // NOTE: By default, not set + + material.colDiffuse = WHITE; // Diffuse color + material.colAmbient = WHITE; // Ambient color + material.colSpecular = WHITE; // Specular color + + material.glossiness = 100.0f; // Glossiness level + material.normalDepth = 1.0f; // Normal map depth + + return material; +} + // Link a texture to a model void SetModelTexture(Model *model, Texture2D texture) { @@ -1100,31 +1140,59 @@ void DrawModel(Model model, Vector3 position, float scale, Color tint) { Vector3 vScale = { scale, scale, scale }; Vector3 rotationAxis = { 0.0f, 0.0f, 0.0f }; - + DrawModelEx(model, position, rotationAxis, 0.0f, vScale, tint); } // Draw a model with extended parameters void DrawModelEx(Model model, Vector3 position, Vector3 rotationAxis, float rotationAngle, Vector3 scale, Color tint) { - // NOTE: Rotation must be provided in degrees, it's converted to radians inside rlglDrawModel() - rlglDrawModel(model, position, rotationAxis, rotationAngle, scale, tint, false); + // Calculate transformation matrix from function parameters + // Get transform matrix (rotation -> scale -> translation) + Matrix matRotation = MatrixRotate(rotationAxis, rotationAngle*DEG2RAD); + Matrix matScale = MatrixScale(scale.x, scale.y, scale.z); + Matrix matTranslation = MatrixTranslate(position.x, position.y, position.z); + + // Combine model transformation matrix (model.transform) with matrix generated by function parameters (matTransform) + //Matrix matModel = MatrixMultiply(model.transform, matTransform); // Transform to world-space coordinates + + model.transform = MatrixMultiply(MatrixMultiply(matScale, matRotation), matTranslation); + model.material.colDiffuse = tint; + + rlglDrawEx(model.mesh, model.material, model.transform, false); } // Draw a model wires (with texture if set) -void DrawModelWires(Model model, Vector3 position, float scale, Color color) +void DrawModelWires(Model model, Vector3 position, float scale, Color tint) { Vector3 vScale = { scale, scale, scale }; Vector3 rotationAxis = { 0.0f, 0.0f, 0.0f }; - rlglDrawModel(model, position, rotationAxis, 0.0f, vScale, color, true); + // Calculate transformation matrix from function parameters + // Get transform matrix (rotation -> scale -> translation) + Matrix matRotation = MatrixRotate(rotationAxis, 0.0f); + Matrix matScale = MatrixScale(vScale.x, vScale.y, vScale.z); + Matrix matTranslation = MatrixTranslate(position.x, position.y, position.z); + + model.transform = MatrixMultiply(MatrixMultiply(matScale, matRotation), matTranslation); + model.material.colDiffuse = tint; + + rlglDrawEx(model.mesh, model.material, model.transform, true); } // Draw a model wires (with texture if set) with extended parameters void DrawModelWiresEx(Model model, Vector3 position, Vector3 rotationAxis, float rotationAngle, Vector3 scale, Color tint) { - // NOTE: Rotation must be provided in degrees, it's converted to radians inside rlglDrawModel() - rlglDrawModel(model, position, rotationAxis, rotationAngle, scale, tint, true); + // Calculate transformation matrix from function parameters + // Get transform matrix (rotation -> scale -> translation) + Matrix matRotation = MatrixRotate(rotationAxis, rotationAngle*DEG2RAD); + Matrix matScale = MatrixScale(scale.x, scale.y, scale.z); + Matrix matTranslation = MatrixTranslate(position.x, position.y, position.z); + + model.transform = MatrixMultiply(MatrixMultiply(matScale, matRotation), matTranslation); + model.material.colDiffuse = tint; + + rlglDrawEx(model.mesh, model.material, model.transform, true); } // Draw a billboard @@ -1856,3 +1924,39 @@ static Mesh LoadOBJ(const char *fileName) return mesh; } + +// Load MTL material data +static Material LoadMTL(const char *fileName) +{ + Material material = { 0 }; + + // TODO: Load mtl file + + char dataType; + char comments[200]; + + FILE *mtlFile; + + mtlFile = fopen(fileName, "rt"); + + if (mtlFile == NULL) + { + TraceLog(WARNING, "[%s] MTL file could not be opened", fileName); + return material; + } + + // First reading pass: Get numVertex, numNormals, numTexCoords, numTriangles + // NOTE: vertex, texcoords and normals could be optimized (to be used indexed on faces definition) + // NOTE: faces MUST be defined as TRIANGLES (3 vertex per face) + while(!feof(mtlFile)) + { + fscanf(mtlFile, "%c", &dataType); + } + + fclose(mtlFile); + + // NOTE: At this point we have all material data + TraceLog(INFO, "[%s] Material loaded successfully", fileName); + + return material; +} diff --git a/src/raylib.h b/src/raylib.h index 8af7d2fb..c88a60f4 100644 --- a/src/raylib.h +++ b/src/raylib.h @@ -387,10 +387,10 @@ typedef struct Shader { unsigned int id; // Shader program id // Variable attributes locations - int vertexLoc; // Vertex attribute location point (vertex shader) - int texcoordLoc; // Texcoord attribute location point (vertex shader) - int normalLoc; // Normal attribute location point (vertex shader) - int colorLoc; // Color attibute location point (vertex shader) + int vertexLoc; // Vertex attribute location point (default-location = 0) + int texcoordLoc; // Texcoord attribute location point (default-location = 1) + int normalLoc; // Normal attribute location point (default-location = 2) + int colorLoc; // Color attibute location point (default-location = 3) // Uniform locations int mvpLoc; // ModelView-Projection matrix uniform location point (vertex shader) @@ -801,17 +801,20 @@ void DrawGizmo(Vector3 position); //------------------------------------------------------------------------------------ // Model 3d Loading and Drawing Functions (Module: models) //------------------------------------------------------------------------------------ -Model LoadModel(const char *fileName); // Load a 3d model (.OBJ) -Model LoadModelEx(Mesh data); // Load a 3d model (from mesh data) -//Model LoadModelFromRES(const char *rresName, int resId); // TODO: Load a 3d model from rRES file (raylib Resource) -Model LoadHeightmap(Image heightmap, Vector3 size); // Load a heightmap image as a 3d model -Model LoadCubicmap(Image cubicmap); // Load a map image as a 3d model (cubes based) -void UnloadModel(Model model); // Unload 3d model from memory -void SetModelTexture(Model *model, Texture2D texture); // Link a texture to a model +Model LoadModel(const char *fileName); // Load a 3d model (.OBJ) +Model LoadModelEx(Mesh data); // Load a 3d model (from mesh data) +//Model LoadModelFromRES(const char *rresName, int resId); // TODO: Load a 3d model from rRES file (raylib Resource) +Model LoadHeightmap(Image heightmap, Vector3 size); // Load a heightmap image as a 3d model +Model LoadCubicmap(Image cubicmap); // Load a map image as a 3d model (cubes based) +void UnloadModel(Model model); // Unload 3d model from memory +void SetModelTexture(Model *model, Texture2D texture); // Link a texture to a model + +Material LoadMaterial(const char *fileName); // Load material data (from file) +Material LoadDefaultMaterial(void); // Load default material (uses default models shader) void DrawModel(Model model, Vector3 position, float scale, Color tint); // Draw a model (with texture if set) void DrawModelEx(Model model, Vector3 position, Vector3 rotationAxis, float rotationAngle, Vector3 scale, Color tint); // Draw a model with extended parameters -void DrawModelWires(Model model, Vector3 position, float scale, Color color); // Draw a model wires (with texture if set) +void DrawModelWires(Model model, Vector3 position, float scale, Color tint); // Draw a model wires (with texture if set) void DrawModelWiresEx(Model model, Vector3 position, Vector3 rotationAxis, float rotationAngle, Vector3 scale, Color tint); // Draw a model wires (with texture if set) with extended parameters void DrawBoundingBox(BoundingBox box, Color color); // Draw bounding box (wires) @@ -832,11 +835,11 @@ Vector3 ResolveCollisionCubicmap(Image cubicmap, Vector3 mapPosition, Vector3 *p // NOTE: This functions are useless when using OpenGL 1.1 //------------------------------------------------------------------------------------ Shader LoadShader(char *vsFileName, char *fsFileName); // Load a custom shader and bind default locations -unsigned int LoadShaderProgram(char *vShaderStr, char *fShaderStr); // Load custom shaders strings and return program id void UnloadShader(Shader shader); // Unload a custom shader from memory void SetDefaultShader(void); // Set default shader to be used in batch draw void SetCustomShader(Shader shader); // Set custom shader to be used in batch draw -void SetModelShader(Model *model, Shader shader); // Link a shader to a model +Shader GetDefaultShader(void); // Get default shader +Texture2D GetDefaultTexture(void); // Get default texture int GetShaderLocation(Shader shader, const char *uniformName); // Get shader uniform location void SetShaderValue(Shader shader, int uniformLoc, float *value, int size); // Set shader uniform value (float) diff --git a/src/rlgl.c b/src/rlgl.c index 9112e47e..02649e30 100644 --- a/src/rlgl.c +++ b/src/rlgl.c @@ -255,14 +255,16 @@ unsigned int whiteTexture; //---------------------------------------------------------------------------------- #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) static void LoadCompressedTexture(unsigned char *data, int width, int height, int mipmapCount, int compressedFormat); +static unsigned int LoadShaderProgram(char *vShaderStr, char *fShaderStr); // Load custom shader strings and return program id -static Shader LoadDefaultShader(void); -static void LoadDefaultShaderLocations(Shader *shader); -static void UnloadDefaultShader(void); +static Shader LoadDefaultShader(void); // Load default shader (just vertex positioning and texture coloring) +static void LoadDefaultShaderLocations(Shader *shader); // Bind default shader locations (attributes and uniforms) +static void UnloadDefaultShader(void); // Unload default shader -static void LoadDefaultBuffers(void); -static void UpdateDefaultBuffers(void); -static void UnloadDefaultBuffers(void); +static void LoadDefaultBuffers(void); // Load default internal buffers (lines, triangles, quads) +static void UpdateDefaultBuffers(void); // Update default internal buffers (VAOs/VBOs) with vertex data +static void DrawDefaultBuffers(void); // Draw default internal buffers vertex data +static void UnloadDefaultBuffers(void); // Unload default internal buffers vertex data from CPU and GPU static char *ReadTextFile(const char *fileName); #endif @@ -1061,167 +1063,12 @@ void rlglDraw(void) { #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) UpdateDefaultBuffers(); - - if ((lines.vCounter > 0) || (triangles.vCounter > 0) || (quads.vCounter > 0)) - { - glUseProgram(currentShader.id); - - Matrix matMVP = MatrixMultiply(modelview, projection); // Create modelview-projection matrix - - glUniformMatrix4fv(currentShader.mvpLoc, 1, false, MatrixToFloat(matMVP)); - glUniform1i(currentShader.mapDiffuseLoc, 0); - glUniform4f(currentShader.tintColorLoc, 1.0f, 1.0f, 1.0f, 1.0f); - } - - // NOTE: We draw in this order: lines, triangles, quads - - if (lines.vCounter > 0) - { - glBindTexture(GL_TEXTURE_2D, whiteTexture); - - if (vaoSupported) - { - glBindVertexArray(vaoLines); - } - else - { - glBindBuffer(GL_ARRAY_BUFFER, linesBuffer[0]); - glVertexAttribPointer(currentShader.vertexLoc, 3, GL_FLOAT, 0, 0, 0); - glEnableVertexAttribArray(currentShader.vertexLoc); - - if (currentShader.colorLoc != -1) - { - glBindBuffer(GL_ARRAY_BUFFER, linesBuffer[1]); - glVertexAttribPointer(currentShader.colorLoc, 4, GL_UNSIGNED_BYTE, GL_TRUE, 0, 0); - glEnableVertexAttribArray(currentShader.colorLoc); - } - } - - glDrawArrays(GL_LINES, 0, lines.vCounter); - - if (!vaoSupported) glBindBuffer(GL_ARRAY_BUFFER, 0); - glBindTexture(GL_TEXTURE_2D, 0); - } - - if (triangles.vCounter > 0) - { - glBindTexture(GL_TEXTURE_2D, whiteTexture); - - if (vaoSupported) - { - glBindVertexArray(vaoTriangles); - } - else - { - glBindBuffer(GL_ARRAY_BUFFER, trianglesBuffer[0]); - glVertexAttribPointer(currentShader.vertexLoc, 3, GL_FLOAT, 0, 0, 0); - glEnableVertexAttribArray(currentShader.vertexLoc); - - if (currentShader.colorLoc != -1) - { - glBindBuffer(GL_ARRAY_BUFFER, trianglesBuffer[1]); - glVertexAttribPointer(currentShader.colorLoc, 4, GL_UNSIGNED_BYTE, GL_TRUE, 0, 0); - glEnableVertexAttribArray(currentShader.colorLoc); - } - } - - glDrawArrays(GL_TRIANGLES, 0, triangles.vCounter); - - if (!vaoSupported) glBindBuffer(GL_ARRAY_BUFFER, 0); - glBindTexture(GL_TEXTURE_2D, 0); - } - - if (quads.vCounter > 0) - { - int quadsCount = 0; - int numIndicesToProcess = 0; - int indicesOffset = 0; - - if (vaoSupported) - { - glBindVertexArray(vaoQuads); - } - else - { - // Enable vertex attributes - glBindBuffer(GL_ARRAY_BUFFER, quadsBuffer[0]); - glVertexAttribPointer(currentShader.vertexLoc, 3, GL_FLOAT, 0, 0, 0); - glEnableVertexAttribArray(currentShader.vertexLoc); - - glBindBuffer(GL_ARRAY_BUFFER, quadsBuffer[1]); - glVertexAttribPointer(currentShader.texcoordLoc, 2, GL_FLOAT, 0, 0, 0); - glEnableVertexAttribArray(currentShader.texcoordLoc); - - if (currentShader.colorLoc != -1) - { - glBindBuffer(GL_ARRAY_BUFFER, quadsBuffer[2]); - glVertexAttribPointer(currentShader.colorLoc, 4, GL_UNSIGNED_BYTE, GL_TRUE, 0, 0); - glEnableVertexAttribArray(currentShader.colorLoc); - } - - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, quadsBuffer[3]); - } - - //TraceLog(DEBUG, "Draws required per frame: %i", drawsCounter); - - for (int i = 0; i < drawsCounter; i++) - { - quadsCount = draws[i].vertexCount/4; - numIndicesToProcess = quadsCount*6; // Get number of Quads * 6 index by Quad - - //TraceLog(DEBUG, "Quads to render: %i - Vertex Count: %i", quadsCount, draws[i].vertexCount); - - glBindTexture(GL_TEXTURE_2D, draws[i].textureId); - - // 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 -#if defined(GRAPHICS_API_OPENGL_33) - glDrawElements(GL_TRIANGLES, numIndicesToProcess, GL_UNSIGNED_INT, (GLvoid*) (sizeof(GLuint) * indicesOffset)); -#elif defined(GRAPHICS_API_OPENGL_ES2) - glDrawElements(GL_TRIANGLES, numIndicesToProcess, GL_UNSIGNED_SHORT, (GLvoid*) (sizeof(GLushort) * indicesOffset)); -#endif - //GLenum err; - //if ((err = glGetError()) != GL_NO_ERROR) TraceLog(INFO, "OpenGL error: %i", (int)err); //GL_INVALID_ENUM! - - indicesOffset += draws[i].vertexCount/4*6; - } - - if (!vaoSupported) - { - glBindBuffer(GL_ARRAY_BUFFER, 0); - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); - } - - glBindTexture(GL_TEXTURE_2D, 0); // Unbind textures - } - - if (vaoSupported) glBindVertexArray(0); // Unbind VAO - - glUseProgram(0); // Unbind shader program - - // Reset draws counter - drawsCounter = 1; - draws[0].textureId = whiteTexture; - draws[0].vertexCount = 0; - - // Reset vertex counters for next frame - lines.vCounter = 0; - lines.cCounter = 0; - - triangles.vCounter = 0; - triangles.cCounter = 0; - - quads.vCounter = 0; - quads.tcCounter = 0; - quads.cCounter = 0; - - // Reset depth for next draw - currentDepth = -1.0f; + DrawDefaultBuffers(); #endif } -// Draw a 3d model -// NOTE: Model transform can come within model struct -void rlglDrawModel(Model model, Vector3 position, Vector3 rotationAxis, float rotationAngle, Vector3 scale, Color color, bool wires) +// Draw a 3d mesh with material and transform +void rlglDrawEx(Mesh mesh, Material material, Matrix transform, bool wires) { #if defined (GRAPHICS_API_OPENGL_11) || defined(GRAPHICS_API_OPENGL_33) // NOTE: glPolygonMode() not available on OpenGL ES @@ -1230,27 +1077,21 @@ void rlglDrawModel(Model model, Vector3 position, Vector3 rotationAxis, float ro #if defined(GRAPHICS_API_OPENGL_11) glEnable(GL_TEXTURE_2D); - glBindTexture(GL_TEXTURE_2D, model.material.texDiffuse.id); + glBindTexture(GL_TEXTURE_2D, material.texDiffuse.id); // NOTE: On OpenGL 1.1 we use Vertex Arrays to draw model glEnableClientState(GL_VERTEX_ARRAY); // Enable vertex array glEnableClientState(GL_TEXTURE_COORD_ARRAY); // Enable texture coords array glEnableClientState(GL_NORMAL_ARRAY); // Enable normals array - glVertexPointer(3, GL_FLOAT, 0, model.mesh.vertices); // Pointer to vertex coords array - glTexCoordPointer(2, GL_FLOAT, 0, model.mesh.texcoords); // Pointer to texture coords array - glNormalPointer(GL_FLOAT, 0, model.mesh.normals); // Pointer to normals array - //glColorPointer(4, GL_UNSIGNED_BYTE, 0, model.mesh.colors); // Pointer to colors array (NOT USED) + glVertexPointer(3, GL_FLOAT, 0, mesh.vertices); // Pointer to vertex coords array + glTexCoordPointer(2, GL_FLOAT, 0, mesh.texcoords); // Pointer to texture coords array + glNormalPointer(GL_FLOAT, 0, mesh.normals); // Pointer to normals array + //glColorPointer(4, GL_UNSIGNED_BYTE, 0, mesh.colors); // Pointer to colors array (NOT USED) - rlPushMatrix(); - rlTranslatef(position.x, position.y, position.z); - rlScalef(scale.x, scale.y, scale.z); - rlRotatef(rotationAngle, rotationAxis.x, rotationAxis.y, rotationAxis.z); - - rlColor4ub(color.r, color.g, color.b, color.a); - - glDrawArrays(GL_TRIANGLES, 0, model.mesh.vertexCount); - rlPopMatrix(); + rlMultMatrixf(MatrixToFloat(transform)); + + glDrawArrays(GL_TRIANGLES, 0, mesh.vertexCount); glDisableClientState(GL_VERTEX_ARRAY); // Disable vertex array glDisableClientState(GL_TEXTURE_COORD_ARRAY); // Disable texture coords array @@ -1261,97 +1102,83 @@ void rlglDrawModel(Model model, Vector3 position, Vector3 rotationAxis, float ro #endif #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) - glUseProgram(model.material.shader.id); + glUseProgram(material.shader.id); // At this point the modelview matrix just contains the view matrix (camera) // That's because Begin3dMode() sets it an no model-drawing function modifies it, all use rlPushMatrix() and rlPopMatrix() Matrix matView = modelview; // View matrix (camera) Matrix matProjection = projection; // Projection matrix (perspective) - - // Calculate transformation matrix from function parameters - // Get transform matrix (rotation -> scale -> translation) - Matrix matRotation = MatrixRotate(rotationAxis, rotationAngle*DEG2RAD); - Matrix matScale = MatrixScale(scale.x, scale.y, scale.z); - Matrix matTranslation = MatrixTranslate(position.x, position.y, position.z); - Matrix matTransform = MatrixMultiply(MatrixMultiply(matScale, matRotation), matTranslation); - - // Combine model internal transformation matrix (model.transform) with matrix generated by function parameters (matTransform) - Matrix matModel = MatrixMultiply(model.transform, matTransform); // Transform to world-space coordinates - + // Calculate model-view matrix combining matModel and matView - Matrix matModelView = MatrixMultiply(matModel, matView); // Transform to camera-space coordinates + Matrix matModelView = MatrixMultiply(transform, matView); // Transform to camera-space coordinates // Calculate model-view-projection matrix (MVP) Matrix matMVP = MatrixMultiply(matModelView, matProjection); // Transform to screen-space coordinates // Send combined model-view-projection matrix to shader - glUniformMatrix4fv(model.material.shader.mvpLoc, 1, false, MatrixToFloat(matMVP)); + glUniformMatrix4fv(material.shader.mvpLoc, 1, false, MatrixToFloat(matMVP)); - // Apply color tinting to model + // Apply color tinting (material.colDiffuse) // NOTE: Just update one uniform on fragment shader - float vColor[4] = { (float)color.r/255, (float)color.g/255, (float)color.b/255, (float)color.a/255 }; - glUniform4fv(model.material.shader.tintColorLoc, 1, vColor); + float vColor[4] = { (float)material.colDiffuse.r/255, (float)material.colDiffuse.g/255, (float)material.colDiffuse.b/255, (float)material.colDiffuse.a/255 }; + glUniform4fv(material.shader.tintColorLoc, 1, vColor); // Set shader textures (diffuse, normal, specular) glActiveTexture(GL_TEXTURE0); - glBindTexture(GL_TEXTURE_2D, model.material.texDiffuse.id); - glUniform1i(model.material.shader.mapDiffuseLoc, 0); // Texture fits in active texture unit 0 + glBindTexture(GL_TEXTURE_2D, material.texDiffuse.id); + glUniform1i(material.shader.mapDiffuseLoc, 0); // Texture fits in active texture unit 0 - if ((model.material.texNormal.id != 0) && (model.material.shader.mapNormalLoc != -1)) + if ((material.texNormal.id != 0) && (material.shader.mapNormalLoc != -1)) { glActiveTexture(GL_TEXTURE1); - glBindTexture(GL_TEXTURE_2D, model.material.texNormal.id); - glUniform1i(model.material.shader.mapNormalLoc, 1); // Texture fits in active texture unit 1 + glBindTexture(GL_TEXTURE_2D, material.texNormal.id); + glUniform1i(material.shader.mapNormalLoc, 1); // Texture fits in active texture unit 1 } - if ((model.material.texSpecular.id != 0) && (model.material.shader.mapSpecularLoc != -1)) + if ((material.texSpecular.id != 0) && (material.shader.mapSpecularLoc != -1)) { glActiveTexture(GL_TEXTURE2); - glBindTexture(GL_TEXTURE_2D, model.material.texSpecular.id); - glUniform1i(model.material.shader.mapSpecularLoc, 2); // Texture fits in active texture unit 2 + glBindTexture(GL_TEXTURE_2D, material.texSpecular.id); + glUniform1i(material.shader.mapSpecularLoc, 2); // Texture fits in active texture unit 2 } if (vaoSupported) { - glBindVertexArray(model.mesh.vaoId); + glBindVertexArray(mesh.vaoId); } else { - // Bind model VBO data: vertex position - glBindBuffer(GL_ARRAY_BUFFER, model.mesh.vboId[0]); - glVertexAttribPointer(model.material.shader.vertexLoc, 3, GL_FLOAT, 0, 0, 0); - glEnableVertexAttribArray(model.material.shader.vertexLoc); + // Bind mesh VBO data: vertex position (shader-location = 0) + glBindBuffer(GL_ARRAY_BUFFER, mesh.vboId[0]); + glVertexAttribPointer(material.shader.vertexLoc, 3, GL_FLOAT, 0, 0, 0); + glEnableVertexAttribArray(material.shader.vertexLoc); - // Bind model VBO data: vertex texcoords - glBindBuffer(GL_ARRAY_BUFFER, model.mesh.vboId[1]); - glVertexAttribPointer(model.material.shader.texcoordLoc, 2, GL_FLOAT, 0, 0, 0); - glEnableVertexAttribArray(model.material.shader.texcoordLoc); + // Bind mesh VBO data: vertex texcoords (shader-location = 1) + glBindBuffer(GL_ARRAY_BUFFER, mesh.vboId[1]); + glVertexAttribPointer(material.shader.texcoordLoc, 2, GL_FLOAT, 0, 0, 0); + glEnableVertexAttribArray(material.shader.texcoordLoc); - // Bind model VBO data: vertex normals (if available) - if (model.material.shader.normalLoc != -1) + // Bind mesh VBO data: vertex normals (shader-location = 2, if available) + if (material.shader.normalLoc != -1) { - glBindBuffer(GL_ARRAY_BUFFER, model.mesh.vboId[2]); - glVertexAttribPointer(model.material.shader.normalLoc, 3, GL_FLOAT, 0, 0, 0); - glEnableVertexAttribArray(model.material.shader.normalLoc); + glBindBuffer(GL_ARRAY_BUFFER, mesh.vboId[2]); + glVertexAttribPointer(material.shader.normalLoc, 3, GL_FLOAT, 0, 0, 0); + glEnableVertexAttribArray(material.shader.normalLoc); } - // TODO: Bind model VBO data: colors, tangents, texcoords2 (if available) + // TODO: Bind mesh VBO data: colors, tangents, texcoords2 (if available) } // Draw call! - glDrawArrays(GL_TRIANGLES, 0, model.mesh.vertexCount); - - //glDisableVertexAttribArray(model.shader.vertexLoc); - //glDisableVertexAttribArray(model.shader.texcoordLoc); - //if (model.shader.normalLoc != -1) glDisableVertexAttribArray(model.shader.normalLoc); + glDrawArrays(GL_TRIANGLES, 0, mesh.vertexCount); - if (model.material.texNormal.id != 0) + if (material.texNormal.id != 0) { glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, 0); } - if (model.material.texSpecular.id != 0) + if (material.texSpecular.id != 0) { glActiveTexture(GL_TEXTURE2); glBindTexture(GL_TEXTURE_2D, 0); @@ -1808,95 +1635,75 @@ void rlglGenerateMipmaps(Texture2D texture) glBindTexture(GL_TEXTURE_2D, 0); } -// Load vertex data into a VAO (if supported) and VBO -Model rlglLoadModel(Mesh mesh) +// Upload vertex data into a VAO (if supported) and VBO +void rlglLoadMesh(Mesh *mesh) { - Model model; - - model.mesh = mesh; - model.mesh.vaoId = 0; // Vertex Array Object - model.mesh.vboId[0] = 0; // Vertex positions VBO - model.mesh.vboId[1] = 0; // Vertex texcoords VBO - model.mesh.vboId[2] = 0; // Vertex normals VBO + mesh->vaoId = 0; // Vertex Array Object + mesh->vboId[0] = 0; // Vertex positions VBO + mesh->vboId[1] = 0; // Vertex texcoords VBO + mesh->vboId[2] = 0; // Vertex normals VBO + mesh->vboId[3] = 0; // Vertex color VBO + mesh->vboId[4] = 0; // Vertex tangent VBO + mesh->vboId[5] = 0; // Vertex texcoord2 VBO // TODO: Consider attributes: color, texcoords2, tangents (if available) - model.transform = MatrixIdentity(); - -#if defined(GRAPHICS_API_OPENGL_11) - model.material.texDiffuse.id = 0; // No texture required - model.material.shader.id = 0; // No shader used - -#elif defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) - model.material.shader = defaultShader; // Default model shader - - model.material.texDiffuse.id = whiteTexture; // Default whiteTexture - model.material.texDiffuse.width = 1; // Default whiteTexture width - model.material.texDiffuse.height = 1; // Default whiteTexture height - model.material.texDiffuse.format = UNCOMPRESSED_R8G8B8A8; // Default whiteTexture format - - model.material.texNormal.id = 0; // By default, no normal texture - model.material.texSpecular.id = 0; // By default, no specular texture - - // TODO: Fill default material properties (color, glossiness...) - - GLuint vaoModel = 0; // Vertex Array Objects (VAO) - GLuint vertexBuffer[3]; // Vertex Buffer Objects (VBO) +#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) + GLuint vaoId = 0; // Vertex Array Objects (VAO) + GLuint vboId[3]; // Vertex Buffer Objects (VBOs) if (vaoSupported) { // Initialize Quads VAO (Buffer A) - glGenVertexArrays(1, &vaoModel); - glBindVertexArray(vaoModel); + glGenVertexArrays(1, &vaoId); + glBindVertexArray(vaoId); } // Create buffers for our vertex data (positions, texcoords, normals) - glGenBuffers(3, vertexBuffer); - - // NOTE: Default shader is assigned to model, so vbo buffers are properly linked to vertex attribs - // If model shader is changed, vbo buffers must be re-assigned to new location points (previously loaded) - - // Enable vertex attributes: position - glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer[0]); - glBufferData(GL_ARRAY_BUFFER, sizeof(float)*3*mesh.vertexCount, mesh.vertices, GL_STATIC_DRAW); - glVertexAttribPointer(model.material.shader.vertexLoc, 3, GL_FLOAT, 0, 0, 0); - glEnableVertexAttribArray(model.material.shader.vertexLoc); - - // Enable vertex attributes: texcoords - glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer[1]); - glBufferData(GL_ARRAY_BUFFER, sizeof(float)*2*mesh.vertexCount, mesh.texcoords, GL_STATIC_DRAW); - glVertexAttribPointer(model.material.shader.texcoordLoc, 2, GL_FLOAT, 0, 0, 0); - glEnableVertexAttribArray(model.material.shader.texcoordLoc); - - // Enable vertex attributes: normals - glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer[2]); - glBufferData(GL_ARRAY_BUFFER, sizeof(float)*3*mesh.vertexCount, mesh.normals, GL_STATIC_DRAW); - glVertexAttribPointer(model.material.shader.normalLoc, 3, GL_FLOAT, 0, 0, 0); - glEnableVertexAttribArray(model.material.shader.normalLoc); - - glVertexAttrib4f(model.material.shader.colorLoc, 1.0f, 1.0f, 1.0f, 1.0f); // Color vertex attribute set to default: WHITE - glDisableVertexAttribArray(model.material.shader.colorLoc); + glGenBuffers(3, vboId); + + // NOTE: Attributes must be uploaded considering default locations points + + // Enable vertex attributes: position (shader-location = 0) + glBindBuffer(GL_ARRAY_BUFFER, vboId[0]); + glBufferData(GL_ARRAY_BUFFER, sizeof(float)*3*mesh->vertexCount, mesh->vertices, GL_STATIC_DRAW); + glVertexAttribPointer(0, 3, GL_FLOAT, 0, 0, 0); + glEnableVertexAttribArray(0); + + // Enable vertex attributes: texcoords (shader-location = 1) + glBindBuffer(GL_ARRAY_BUFFER, vboId[1]); + glBufferData(GL_ARRAY_BUFFER, sizeof(float)*2*mesh->vertexCount, mesh->texcoords, GL_STATIC_DRAW); + glVertexAttribPointer(1, 2, GL_FLOAT, 0, 0, 0); + glEnableVertexAttribArray(1); + + // Enable vertex attributes: normals (shader-location = 2) + glBindBuffer(GL_ARRAY_BUFFER, vboId[2]); + glBufferData(GL_ARRAY_BUFFER, sizeof(float)*3*mesh->vertexCount, mesh->normals, GL_STATIC_DRAW); + glVertexAttribPointer(2, 3, GL_FLOAT, 0, 0, 0); + glEnableVertexAttribArray(2); + + // Default color vertex attribute (shader-location = 3) + glVertexAttrib4f(3, 1.0f, 1.0f, 1.0f, 1.0f); // Color vertex attribute set to default: WHITE + glDisableVertexAttribArray(3); - model.mesh.vboId[0] = vertexBuffer[0]; // Vertex position VBO - model.mesh.vboId[1] = vertexBuffer[1]; // Texcoords VBO - model.mesh.vboId[2] = vertexBuffer[2]; // Normals VBO + mesh->vboId[0] = vboId[0]; // Vertex position VBO + mesh->vboId[1] = vboId[1]; // Texcoords VBO + mesh->vboId[2] = vboId[2]; // Normals VBO if (vaoSupported) { - if (vaoModel > 0) + if (vaoId > 0) { - model.mesh.vaoId = vaoModel; - TraceLog(INFO, "[VAO ID %i] Model uploaded successfully to VRAM (GPU)", vaoModel); + mesh->vaoId = vaoId; + TraceLog(INFO, "[VAO ID %i] Mesh uploaded successfully to VRAM (GPU)", mesh->vaoId); } - else TraceLog(WARNING, "Model could not be uploaded to VRAM (GPU)"); + else TraceLog(WARNING, "Mesh could not be uploaded to VRAM (GPU)"); } else { - TraceLog(INFO, "[VBO ID %i][VBO ID %i][VBO ID %i] Model uploaded successfully to VRAM (GPU)", model.mesh.vboId[0], model.mesh.vboId[1], model.mesh.vboId[2]); + TraceLog(INFO, "[VBO ID %i][VBO ID %i][VBO ID %i] Mesh uploaded successfully to VRAM (GPU)", mesh->vboId[0], mesh->vboId[1], mesh->vboId[2]); } #endif - - return model; } // Read screen pixel data (color buffer) @@ -2090,36 +1897,202 @@ Shader LoadShader(char *vsFileName, char *fsFileName) return shader; } -// Load custom shader strings and return program id -unsigned int LoadShaderProgram(char *vShaderStr, char *fShaderStr) +// Unload a custom shader from memory +void UnloadShader(Shader shader) +{ + if (shader.id != 0) + { + rlDeleteShader(shader.id); + TraceLog(INFO, "[SHDR ID %i] Unloaded shader program data", shader.id); + } +} + +// Set custom shader to be used on batch draw +void SetCustomShader(Shader shader) { - unsigned int program = 0; - #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) - GLuint vertexShader; - GLuint fragmentShader; + if (currentShader.id != shader.id) + { + rlglDraw(); + currentShader = shader; + } +#endif +} - vertexShader = glCreateShader(GL_VERTEX_SHADER); - fragmentShader = glCreateShader(GL_FRAGMENT_SHADER); +// Set default shader to be used in batch draw +void SetDefaultShader(void) +{ +#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) + SetCustomShader(defaultShader); +#endif +} - const char *pvs = vShaderStr; - const char *pfs = fShaderStr; +// Get default shader +Shader GetDefaultShader(void) +{ + return defaultShader; +} - glShaderSource(vertexShader, 1, &pvs, NULL); - glShaderSource(fragmentShader, 1, &pfs, NULL); +// Get shader uniform location +int GetShaderLocation(Shader shader, const char *uniformName) +{ + int location = -1; +#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) + location = glGetUniformLocation(shader.id, uniformName); + + if (location == -1) TraceLog(WARNING, "[SHDR ID %i] Shader location for %s could not be found", shader.id, uniformName); +#endif + return location; +} - GLint success = 0; +// Set shader uniform value (float) +void SetShaderValue(Shader shader, int uniformLoc, float *value, int size) +{ +#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) + glUseProgram(shader.id); - glCompileShader(vertexShader); + if (size == 1) glUniform1fv(uniformLoc, 1, value); // Shader uniform type: float + else if (size == 2) glUniform2fv(uniformLoc, 1, value); // Shader uniform type: vec2 + else if (size == 3) glUniform3fv(uniformLoc, 1, value); // Shader uniform type: vec3 + else if (size == 4) glUniform4fv(uniformLoc, 1, value); // Shader uniform type: vec4 + else TraceLog(WARNING, "Shader value float array size not supported"); + + glUseProgram(0); +#endif +} - glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success); +// Set shader uniform value (int) +void SetShaderValuei(Shader shader, int uniformLoc, int *value, int size) +{ +#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) + glUseProgram(shader.id); - if (success != GL_TRUE) - { - TraceLog(WARNING, "[VSHDR ID %i] Failed to compile vertex shader...", vertexShader); + if (size == 1) glUniform1iv(uniformLoc, 1, value); // Shader uniform type: int + else if (size == 2) glUniform2iv(uniformLoc, 1, value); // Shader uniform type: ivec2 + else if (size == 3) glUniform3iv(uniformLoc, 1, value); // Shader uniform type: ivec3 + else if (size == 4) glUniform4iv(uniformLoc, 1, value); // Shader uniform type: ivec4 + else TraceLog(WARNING, "Shader value int array size not supported"); + + glUseProgram(0); +#endif +} - int maxLength = 0; - int length; +// Set shader uniform value (matrix 4x4) +void SetShaderValueMatrix(Shader shader, int uniformLoc, Matrix mat) +{ +#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) + glUseProgram(shader.id); + + glUniformMatrix4fv(uniformLoc, 1, false, MatrixToFloat(mat)); + + glUseProgram(0); +#endif +} + +// Set blending mode (alpha, additive, multiplied) +// NOTE: Only 3 blending modes predefined +void SetBlendMode(int mode) +{ + if ((blendMode != mode) && (mode < 3)) + { + rlglDraw(); + + switch (mode) + { + case BLEND_ALPHA: glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); break; + case BLEND_ADDITIVE: glBlendFunc(GL_SRC_ALPHA, GL_ONE); break; // Alternative: glBlendFunc(GL_ONE, GL_ONE); + case BLEND_MULTIPLIED: glBlendFunc(GL_DST_COLOR, GL_ONE_MINUS_SRC_ALPHA); break; + default: break; + } + + blendMode = mode; + } +} + +//---------------------------------------------------------------------------------- +// Module specific Functions Definition +//---------------------------------------------------------------------------------- + +#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) +// Convert image data to OpenGL texture (returns OpenGL valid Id) +// NOTE: Expected compressed image data and POT image +static void LoadCompressedTexture(unsigned char *data, int width, int height, int mipmapCount, int compressedFormat) +{ + glPixelStorei(GL_UNPACK_ALIGNMENT, 1); + + int blockSize = 0; // Bytes every block + int offset = 0; + + if ((compressedFormat == GL_COMPRESSED_RGB_S3TC_DXT1_EXT) || + (compressedFormat == GL_COMPRESSED_RGBA_S3TC_DXT1_EXT) || +#if defined(GRAPHICS_API_OPENGL_ES2) + (compressedFormat == GL_ETC1_RGB8_OES) || +#endif + (compressedFormat == GL_COMPRESSED_RGB8_ETC2)) blockSize = 8; + else blockSize = 16; + + // Load the mipmap levels + for (int level = 0; level < mipmapCount && (width || height); level++) + { + unsigned int size = 0; + + size = ((width + 3)/4)*((height + 3)/4)*blockSize; + + glCompressedTexImage2D(GL_TEXTURE_2D, level, compressedFormat, width, height, 0, size, data + offset); + + offset += size; + width /= 2; + height /= 2; + + // Security check for NPOT textures + if (width < 1) width = 1; + if (height < 1) height = 1; + } +} + +Texture2D GetDefaultTexture(void) +{ + Texture2D texture; + + texture.id = whiteTexture; + texture.width = 1; + texture.height = 1; + texture.mipmaps = 1; + texture.format = UNCOMPRESSED_R8G8B8A8; + + return texture; +} + +// Load custom shader strings and return program id +static unsigned int LoadShaderProgram(char *vShaderStr, char *fShaderStr) +{ + unsigned int program = 0; + +#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) + GLuint vertexShader; + GLuint fragmentShader; + + vertexShader = glCreateShader(GL_VERTEX_SHADER); + fragmentShader = glCreateShader(GL_FRAGMENT_SHADER); + + const char *pvs = vShaderStr; + const char *pfs = fShaderStr; + + glShaderSource(vertexShader, 1, &pvs, NULL); + glShaderSource(fragmentShader, 1, &pfs, NULL); + + GLint success = 0; + + glCompileShader(vertexShader); + + glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success); + + if (success != GL_TRUE) + { + TraceLog(WARNING, "[VSHDR ID %i] Failed to compile vertex shader...", vertexShader); + + int maxLength = 0; + int length; glGetShaderiv(vertexShader, GL_INFO_LOG_LENGTH, &maxLength); @@ -2156,7 +2129,17 @@ unsigned int LoadShaderProgram(char *vShaderStr, char *fShaderStr) glAttachShader(program, vertexShader); glAttachShader(program, fragmentShader); - + + // NOTE: Default attribute shader locations must be binded before linking + glBindAttribLocation(program, 0, "vertexPosition"); + glBindAttribLocation(program, 1, "vertexTexCoord"); + glBindAttribLocation(program, 2, "vertexNormal"); + glBindAttribLocation(program, 3, "vertexColor"); + glBindAttribLocation(program, 4, "vertexTangent"); + glBindAttribLocation(program, 5, "vertexTexCoord2"); + + // NOTE: If some attrib name is no found on the shader, it locations becomes -1 + glLinkProgram(program); // NOTE: All uniform variables are intitialised to 0 when a program links @@ -2190,184 +2173,8 @@ unsigned int LoadShaderProgram(char *vShaderStr, char *fShaderStr) return program; } -// Unload a custom shader from memory -void UnloadShader(Shader shader) -{ - if (shader.id != 0) - { - rlDeleteShader(shader.id); - TraceLog(INFO, "[SHDR ID %i] Unloaded shader program data", shader.id); - } -} - -// Set custom shader to be used on batch draw -void SetCustomShader(Shader shader) -{ -#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) - if (currentShader.id != shader.id) - { - rlglDraw(); - currentShader = shader; - } -#endif -} - -// Set default shader to be used in batch draw -void SetDefaultShader(void) -{ -#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) - SetCustomShader(defaultShader); -#endif -} - -// Link shader to model -void SetModelShader(Model *model, Shader shader) -{ -#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) - model->material.shader = shader; - - if (vaoSupported) glBindVertexArray(model->mesh.vaoId); - - // Enable vertex attributes: position - glBindBuffer(GL_ARRAY_BUFFER, model->mesh.vboId[0]); - glEnableVertexAttribArray(shader.vertexLoc); - glVertexAttribPointer(shader.vertexLoc, 3, GL_FLOAT, 0, 0, 0); - - // Enable vertex attributes: texcoords - glBindBuffer(GL_ARRAY_BUFFER, model->mesh.vboId[1]); - glEnableVertexAttribArray(shader.texcoordLoc); - glVertexAttribPointer(shader.texcoordLoc, 2, GL_FLOAT, 0, 0, 0); - - // Enable vertex attributes: normals - glBindBuffer(GL_ARRAY_BUFFER, model->mesh.vboId[2]); - glEnableVertexAttribArray(shader.normalLoc); - glVertexAttribPointer(shader.normalLoc, 3, GL_FLOAT, 0, 0, 0); - - if (vaoSupported) glBindVertexArray(0); // Unbind VAO - -#elif (GRAPHICS_API_OPENGL_11) - TraceLog(WARNING, "Shaders not supported on OpenGL 1.1"); -#endif -} - -// Get shader uniform location -int GetShaderLocation(Shader shader, const char *uniformName) -{ - int location = -1; -#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) - location = glGetUniformLocation(shader.id, uniformName); - - if (location == -1) TraceLog(WARNING, "[SHDR ID %i] Shader location for %s could not be found", shader.id, uniformName); -#endif - return location; -} - -// Set shader uniform value (float) -void SetShaderValue(Shader shader, int uniformLoc, float *value, int size) -{ -#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) - glUseProgram(shader.id); - - if (size == 1) glUniform1fv(uniformLoc, 1, value); // Shader uniform type: float - else if (size == 2) glUniform2fv(uniformLoc, 1, value); // Shader uniform type: vec2 - else if (size == 3) glUniform3fv(uniformLoc, 1, value); // Shader uniform type: vec3 - else if (size == 4) glUniform4fv(uniformLoc, 1, value); // Shader uniform type: vec4 - else TraceLog(WARNING, "Shader value float array size not supported"); - - glUseProgram(0); -#endif -} - -// Set shader uniform value (int) -void SetShaderValuei(Shader shader, int uniformLoc, int *value, int size) -{ -#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) - glUseProgram(shader.id); - - if (size == 1) glUniform1iv(uniformLoc, 1, value); // Shader uniform type: int - else if (size == 2) glUniform2iv(uniformLoc, 1, value); // Shader uniform type: ivec2 - else if (size == 3) glUniform3iv(uniformLoc, 1, value); // Shader uniform type: ivec3 - else if (size == 4) glUniform4iv(uniformLoc, 1, value); // Shader uniform type: ivec4 - else TraceLog(WARNING, "Shader value int array size not supported"); - - glUseProgram(0); -#endif -} - -// Set shader uniform value (matrix 4x4) -void SetShaderValueMatrix(Shader shader, int uniformLoc, Matrix mat) -{ -#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) - glUseProgram(shader.id); - - glUniformMatrix4fv(uniformLoc, 1, false, MatrixToFloat(mat)); - - glUseProgram(0); -#endif -} - -// Set blending mode (alpha, additive, multiplied) -// NOTE: Only 3 blending modes predefined -void SetBlendMode(int mode) -{ - if ((blendMode != mode) && (mode < 3)) - { - rlglDraw(); - - switch (mode) - { - case BLEND_ALPHA: glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); break; - case BLEND_ADDITIVE: glBlendFunc(GL_SRC_ALPHA, GL_ONE); break; // Alternative: glBlendFunc(GL_ONE, GL_ONE); - case BLEND_MULTIPLIED: glBlendFunc(GL_DST_COLOR, GL_ONE_MINUS_SRC_ALPHA); break; - default: break; - } - - blendMode = mode; - } -} - -//---------------------------------------------------------------------------------- -// Module specific Functions Definition -//---------------------------------------------------------------------------------- - -#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) -// Convert image data to OpenGL texture (returns OpenGL valid Id) -// NOTE: Expected compressed image data and POT image -static void LoadCompressedTexture(unsigned char *data, int width, int height, int mipmapCount, int compressedFormat) -{ - glPixelStorei(GL_UNPACK_ALIGNMENT, 1); - - int blockSize = 0; // Bytes every block - int offset = 0; - - if ((compressedFormat == GL_COMPRESSED_RGB_S3TC_DXT1_EXT) || - (compressedFormat == GL_COMPRESSED_RGBA_S3TC_DXT1_EXT) || -#if defined(GRAPHICS_API_OPENGL_ES2) - (compressedFormat == GL_ETC1_RGB8_OES) || -#endif - (compressedFormat == GL_COMPRESSED_RGB8_ETC2)) blockSize = 8; - else blockSize = 16; - - // Load the mipmap levels - for (int level = 0; level < mipmapCount && (width || height); level++) - { - unsigned int size = 0; - - size = ((width + 3)/4)*((height + 3)/4)*blockSize; - - glCompressedTexImage2D(GL_TEXTURE_2D, level, compressedFormat, width, height, 0, size, data + offset); - offset += size; - width /= 2; - height /= 2; - - // Security check for NPOT textures - if (width < 1) width = 1; - if (height < 1) height = 1; - } -} - -// Load default shader (Vertex and Fragment) +// Load default shader (just vertex positioning and texture coloring) // NOTE: This shader program is used for batch buffers (lines, triangles, quads) static Shader LoadDefaultShader(void) { @@ -2436,6 +2243,12 @@ static Shader LoadDefaultShader(void) // NOTE: If any location is not found, loc point becomes -1 static void LoadDefaultShaderLocations(Shader *shader) { + // NOTE: Default shader attrib locations have been fixed before linking: + // vertex position location = 0 + // vertex texcoord location = 1 + // vertex normal location = 2 + // vertex color location = 3 + // Get handles to GLSL input attibute locations shader->vertexLoc = glGetAttribLocation(shader->id, "vertexPosition"); shader->texcoordLoc = glGetAttribLocation(shader->id, "vertexTexCoord"); @@ -2470,7 +2283,7 @@ static void LoadDefaultBuffers(void) // [CPU] Allocate and initialize float array buffers to store vertex data (lines, triangles, quads) //-------------------------------------------------------------------------------------------- - // Initialize lines arrays (vertex position and color data) + // Lines - Initialize arrays (vertex position and color data) lines.vertices = (float *)malloc(sizeof(float)*3*2*MAX_LINES_BATCH); // 3 float by vertex, 2 vertex by line lines.colors = (unsigned char *)malloc(sizeof(unsigned char)*4*2*MAX_LINES_BATCH); // 4 float by color, 2 colors by line @@ -2480,7 +2293,7 @@ static void LoadDefaultBuffers(void) lines.vCounter = 0; lines.cCounter = 0; - // Initialize triangles arrays (vertex position and color data) + // Triangles - Initialize arrays (vertex position and color data) triangles.vertices = (float *)malloc(sizeof(float)*3*3*MAX_TRIANGLES_BATCH); // 3 float by vertex, 3 vertex by triangle triangles.colors = (unsigned char *)malloc(sizeof(unsigned char)*4*3*MAX_TRIANGLES_BATCH); // 4 float by color, 3 colors by triangle @@ -2490,7 +2303,7 @@ static void LoadDefaultBuffers(void) triangles.vCounter = 0; triangles.cCounter = 0; - // Initialize quads arrays (vertex position, texcoord and color data... and indexes) + // Quads - Initialize arrays (vertex position, texcoord, color data and indexes) quads.vertices = (float *)malloc(sizeof(float)*3*4*MAX_QUADS_BATCH); // 3 float by vertex, 4 vertex by quad quads.texcoords = (float *)malloc(sizeof(float)*2*4*MAX_QUADS_BATCH); // 2 float by texcoord, 4 texcoord by quad quads.colors = (unsigned char *)malloc(sizeof(unsigned char)*4*4*MAX_QUADS_BATCH); // 4 float by color, 4 colors by quad @@ -2523,7 +2336,7 @@ static void LoadDefaultBuffers(void) quads.tcCounter = 0; quads.cCounter = 0; - TraceLog(INFO, "Default buffers initialized successfully in CPU (lines, triangles, quads)"); + TraceLog(INFO, "[CPU] Default buffers initialized successfully (lines, triangles, quads)"); //-------------------------------------------------------------------------------------------- // [GPU] Upload vertex data and initialize VAOs/VBOs (lines, triangles, quads) @@ -2541,20 +2354,21 @@ static void LoadDefaultBuffers(void) // Create buffers for our vertex data glGenBuffers(2, linesBuffer); - // Lines - Vertex positions buffer binding and attributes enable + // Lines - Vertex buffers binding and attributes enable + // Vertex position buffer (shader-location = 0) glBindBuffer(GL_ARRAY_BUFFER, linesBuffer[0]); glBufferData(GL_ARRAY_BUFFER, sizeof(float)*3*2*MAX_LINES_BATCH, lines.vertices, GL_DYNAMIC_DRAW); glEnableVertexAttribArray(currentShader.vertexLoc); glVertexAttribPointer(currentShader.vertexLoc, 3, GL_FLOAT, 0, 0, 0); - // Lines - colors buffer + // Vertex color buffer (shader-location = 3) glBindBuffer(GL_ARRAY_BUFFER, linesBuffer[1]); glBufferData(GL_ARRAY_BUFFER, sizeof(unsigned char)*4*2*MAX_LINES_BATCH, lines.colors, GL_DYNAMIC_DRAW); glEnableVertexAttribArray(currentShader.colorLoc); glVertexAttribPointer(currentShader.colorLoc, 4, GL_UNSIGNED_BYTE, GL_TRUE, 0, 0); - if (vaoSupported) TraceLog(INFO, "[VAO ID %i] Default buffers (lines) VAO initialized successfully", vaoLines); - else TraceLog(INFO, "[VBO ID %i][VBO ID %i] Default buffers (lines) VBOs initialized successfully", linesBuffer[0], linesBuffer[1]); + if (vaoSupported) TraceLog(INFO, "[VAO ID %i] Default buffers VAO initialized successfully (lines)", vaoLines); + else TraceLog(INFO, "[VBO ID %i][VBO ID %i] Default buffers VBOs initialized successfully (lines)", linesBuffer[0], linesBuffer[1]); // Upload and link triangles vertex buffers if (vaoSupported) @@ -2567,19 +2381,21 @@ static void LoadDefaultBuffers(void) // Create buffers for our vertex data glGenBuffers(2, trianglesBuffer); - // Enable vertex attributes + // Triangles - Vertex buffers binding and attributes enable + // Vertex position buffer (shader-location = 0) glBindBuffer(GL_ARRAY_BUFFER, trianglesBuffer[0]); glBufferData(GL_ARRAY_BUFFER, sizeof(float)*3*3*MAX_TRIANGLES_BATCH, triangles.vertices, GL_DYNAMIC_DRAW); glEnableVertexAttribArray(currentShader.vertexLoc); glVertexAttribPointer(currentShader.vertexLoc, 3, GL_FLOAT, 0, 0, 0); + // Vertex color buffer (shader-location = 3) glBindBuffer(GL_ARRAY_BUFFER, trianglesBuffer[1]); glBufferData(GL_ARRAY_BUFFER, sizeof(unsigned char)*4*3*MAX_TRIANGLES_BATCH, triangles.colors, GL_DYNAMIC_DRAW); glEnableVertexAttribArray(currentShader.colorLoc); glVertexAttribPointer(currentShader.colorLoc, 4, GL_UNSIGNED_BYTE, GL_TRUE, 0, 0); - if (vaoSupported) TraceLog(INFO, "[VAO ID %i] Default buffers (triangles) VAO initialized successfully", vaoTriangles); - else TraceLog(INFO, "[VBO ID %i][VBO ID %i] Default buffers (triangles) VBOs initialized successfully", trianglesBuffer[0], trianglesBuffer[1]); + if (vaoSupported) TraceLog(INFO, "[VAO ID %i] Default buffers VAO initialized successfully (triangles)", vaoTriangles); + else TraceLog(INFO, "[VBO ID %i][VBO ID %i] Default buffers VBOs initialized successfully(triangles)", trianglesBuffer[0], trianglesBuffer[1]); // Upload and link quads vertex buffers if (vaoSupported) @@ -2592,17 +2408,20 @@ static void LoadDefaultBuffers(void) // Create buffers for our vertex data glGenBuffers(4, quadsBuffer); - // Enable vertex attributes + // Quads - Vertex buffers binding and attributes enable + // Vertex position buffer (shader-location = 0) glBindBuffer(GL_ARRAY_BUFFER, quadsBuffer[0]); glBufferData(GL_ARRAY_BUFFER, sizeof(float)*3*4*MAX_QUADS_BATCH, quads.vertices, GL_DYNAMIC_DRAW); glEnableVertexAttribArray(currentShader.vertexLoc); glVertexAttribPointer(currentShader.vertexLoc, 3, GL_FLOAT, 0, 0, 0); + // Vertex texcoord buffer (shader-location = 1) glBindBuffer(GL_ARRAY_BUFFER, quadsBuffer[1]); glBufferData(GL_ARRAY_BUFFER, sizeof(float)*2*4*MAX_QUADS_BATCH, quads.texcoords, GL_DYNAMIC_DRAW); glEnableVertexAttribArray(currentShader.texcoordLoc); glVertexAttribPointer(currentShader.texcoordLoc, 2, GL_FLOAT, 0, 0, 0); + // Vertex color buffer (shader-location = 3) glBindBuffer(GL_ARRAY_BUFFER, quadsBuffer[2]); glBufferData(GL_ARRAY_BUFFER, sizeof(unsigned char)*4*4*MAX_QUADS_BATCH, quads.colors, GL_DYNAMIC_DRAW); glEnableVertexAttribArray(currentShader.colorLoc); @@ -2616,15 +2435,15 @@ static void LoadDefaultBuffers(void) glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(short)*6*MAX_QUADS_BATCH, quads.indices, GL_STATIC_DRAW); #endif - if (vaoSupported) TraceLog(INFO, "[VAO ID %i] Default buffers (quads) VAO initialized successfully", vaoQuads); - else TraceLog(INFO, "[VBO ID %i][VBO ID %i][VBO ID %i][VBO ID %i] Default buffers (quads) VBOs initialized successfully", quadsBuffer[0], quadsBuffer[1], quadsBuffer[2], quadsBuffer[3]); + if (vaoSupported) TraceLog(INFO, "[VAO ID %i] Default buffers VAO initialized successfully (quads)", vaoQuads); + else TraceLog(INFO, "[VBO ID %i][VBO ID %i][VBO ID %i][VBO ID %i] Default buffers VBOs initialized successfully (quads)", quadsBuffer[0], quadsBuffer[1], quadsBuffer[2], quadsBuffer[3]); // Unbind the current VAO if (vaoSupported) glBindVertexArray(0); //-------------------------------------------------------------------------------------------- } -// Update default buffers (VAOs/VBOs) with vertex array data +// Update default internal buffers (VAOs/VBOs) with vertex array data // NOTE: If there is not vertex data, buffers doesn't need to be updated (vertexCount > 0) // TODO: If no data changed on the CPU arrays --> No need to re-update GPU arrays (change flag required) static void UpdateDefaultBuffers(void) @@ -2695,7 +2514,165 @@ static void UpdateDefaultBuffers(void) if (vaoSupported) glBindVertexArray(0); } -// Unload default buffers vertex data from CPU and GPU +// Draw default internal buffers vertex data +// NOTE: We draw in this order: lines, triangles, quads +static void DrawDefaultBuffers(void) +{ + // Set current shader and upload current MVP matrix + if ((lines.vCounter > 0) || (triangles.vCounter > 0) || (quads.vCounter > 0)) + { + glUseProgram(currentShader.id); + + // Create modelview-projection matrix + Matrix matMVP = MatrixMultiply(modelview, projection); + + glUniformMatrix4fv(currentShader.mvpLoc, 1, false, MatrixToFloat(matMVP)); + glUniform1i(currentShader.mapDiffuseLoc, 0); + glUniform4f(currentShader.tintColorLoc, 1.0f, 1.0f, 1.0f, 1.0f); + } + + // Draw lines buffers + if (lines.vCounter > 0) + { + glBindTexture(GL_TEXTURE_2D, whiteTexture); + + if (vaoSupported) + { + glBindVertexArray(vaoLines); + } + else + { + // Bind vertex attrib: position (shader-location = 0) + glBindBuffer(GL_ARRAY_BUFFER, linesBuffer[0]); + glVertexAttribPointer(currentShader.vertexLoc, 3, GL_FLOAT, 0, 0, 0); + glEnableVertexAttribArray(currentShader.vertexLoc); + + // Bind vertex attrib: color (shader-location = 3) + glBindBuffer(GL_ARRAY_BUFFER, linesBuffer[1]); + glVertexAttribPointer(currentShader.colorLoc, 4, GL_UNSIGNED_BYTE, GL_TRUE, 0, 0); + glEnableVertexAttribArray(currentShader.colorLoc); + } + + glDrawArrays(GL_LINES, 0, lines.vCounter); + + if (!vaoSupported) glBindBuffer(GL_ARRAY_BUFFER, 0); + glBindTexture(GL_TEXTURE_2D, 0); + } + + // Draw triangles buffers + if (triangles.vCounter > 0) + { + glBindTexture(GL_TEXTURE_2D, whiteTexture); + + if (vaoSupported) + { + glBindVertexArray(vaoTriangles); + } + else + { + // Bind vertex attrib: position (shader-location = 0) + glBindBuffer(GL_ARRAY_BUFFER, trianglesBuffer[0]); + glVertexAttribPointer(currentShader.vertexLoc, 3, GL_FLOAT, 0, 0, 0); + glEnableVertexAttribArray(currentShader.vertexLoc); + + // Bind vertex attrib: color (shader-location = 3) + glBindBuffer(GL_ARRAY_BUFFER, trianglesBuffer[1]); + glVertexAttribPointer(currentShader.colorLoc, 4, GL_UNSIGNED_BYTE, GL_TRUE, 0, 0); + glEnableVertexAttribArray(currentShader.colorLoc); + } + + glDrawArrays(GL_TRIANGLES, 0, triangles.vCounter); + + if (!vaoSupported) glBindBuffer(GL_ARRAY_BUFFER, 0); + glBindTexture(GL_TEXTURE_2D, 0); + } + + // Draw quads buffers + if (quads.vCounter > 0) + { + int quadsCount = 0; + int numIndicesToProcess = 0; + int indicesOffset = 0; + + if (vaoSupported) + { + glBindVertexArray(vaoQuads); + } + else + { + // Bind vertex attrib: position (shader-location = 0) + glBindBuffer(GL_ARRAY_BUFFER, quadsBuffer[0]); + glVertexAttribPointer(currentShader.vertexLoc, 3, GL_FLOAT, 0, 0, 0); + glEnableVertexAttribArray(currentShader.vertexLoc); + + // Bind vertex attrib: texcoord (shader-location = 1) + glBindBuffer(GL_ARRAY_BUFFER, quadsBuffer[1]); + glVertexAttribPointer(currentShader.texcoordLoc, 2, GL_FLOAT, 0, 0, 0); + glEnableVertexAttribArray(currentShader.texcoordLoc); + + // Bind vertex attrib: color (shader-location = 3) + glBindBuffer(GL_ARRAY_BUFFER, quadsBuffer[2]); + glVertexAttribPointer(currentShader.colorLoc, 4, GL_UNSIGNED_BYTE, GL_TRUE, 0, 0); + glEnableVertexAttribArray(currentShader.colorLoc); + + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, quadsBuffer[3]); + } + + //TraceLog(DEBUG, "Draws required per frame: %i", drawsCounter); + + for (int i = 0; i < drawsCounter; i++) + { + quadsCount = draws[i].vertexCount/4; + numIndicesToProcess = quadsCount*6; // Get number of Quads * 6 index by Quad + + //TraceLog(DEBUG, "Quads to render: %i - Vertex Count: %i", quadsCount, draws[i].vertexCount); + + glBindTexture(GL_TEXTURE_2D, draws[i].textureId); + + // 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 +#if defined(GRAPHICS_API_OPENGL_33) + glDrawElements(GL_TRIANGLES, numIndicesToProcess, GL_UNSIGNED_INT, (GLvoid*) (sizeof(GLuint) * indicesOffset)); +#elif defined(GRAPHICS_API_OPENGL_ES2) + glDrawElements(GL_TRIANGLES, numIndicesToProcess, GL_UNSIGNED_SHORT, (GLvoid*) (sizeof(GLushort) * indicesOffset)); +#endif + //GLenum err; + //if ((err = glGetError()) != GL_NO_ERROR) TraceLog(INFO, "OpenGL error: %i", (int)err); //GL_INVALID_ENUM! + + indicesOffset += draws[i].vertexCount/4*6; + } + + if (!vaoSupported) + { + glBindBuffer(GL_ARRAY_BUFFER, 0); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); + } + + glBindTexture(GL_TEXTURE_2D, 0); // Unbind textures + } + + if (vaoSupported) glBindVertexArray(0); // Unbind VAO + + glUseProgram(0); // Unbind shader program + + // Reset draws counter + drawsCounter = 1; + draws[0].textureId = whiteTexture; + draws[0].vertexCount = 0; + + // Reset vertex counters for next frame + lines.vCounter = 0; + lines.cCounter = 0; + triangles.vCounter = 0; + triangles.cCounter = 0; + quads.vCounter = 0; + quads.tcCounter = 0; + quads.cCounter = 0; + + // Reset depth for next draw + currentDepth = -1.0f; +} + +// Unload default internal buffers vertex data from CPU and GPU static void UnloadDefaultBuffers(void) { // Unbind everything diff --git a/src/rlgl.h b/src/rlgl.h index cd8e6d1d..afc2ab96 100644 --- a/src/rlgl.h +++ b/src/rlgl.h @@ -280,29 +280,28 @@ unsigned int rlglLoadTexture(void *data, int width, int height, int textureForma RenderTexture2D rlglLoadRenderTexture(int width, int height); // Load a texture to be used for rendering (fbo with color and depth attachments) void rlglUpdateTexture(unsigned int id, int width, int height, int format, void *data); // Update GPU texture with new data void rlglGenerateMipmaps(Texture2D texture); // Generate mipmap data for selected texture - -// NOTE: There is a set of shader related functions that are available to end user, -// to avoid creating function wrappers through core module, they have been directly declared in raylib.h - -Model rlglLoadModel(Mesh mesh); // Upload vertex data into GPU and provided VAO/VBO ids -void rlglDrawModel(Model model, Vector3 position, Vector3 rotationAxis, float rotationAngle, Vector3 scale, Color color, bool wires); +void rlglLoadMesh(Mesh *mesh); // Upload vertex data into GPU and provided VAO/VBO ids +void rlglDrawEx(Mesh mesh, Material material, Matrix transform, bool wires); Vector3 rlglUnproject(Vector3 source, Matrix proj, Matrix view); // Get world coordinates from screen coordinates unsigned char *rlglReadScreenPixels(int width, int height); // Read screen pixel data (color buffer) void *rlglReadTexturePixels(Texture2D texture); // Read texture pixel data +// NOTE: There is a set of shader related functions that are available to end user, +// to avoid creating function wrappers through core module, they have been directly declared in raylib.h + #if defined(RLGL_STANDALONE) //------------------------------------------------------------------------------------ // Shaders System Functions (Module: rlgl) // NOTE: This functions are useless when using OpenGL 1.1 //------------------------------------------------------------------------------------ Shader LoadShader(char *vsFileName, char *fsFileName); // Load a custom shader and bind default locations -unsigned int LoadShaderProgram(char *vShaderStr, char *fShaderStr); // Load custom shader strings and return program id void UnloadShader(Shader shader); // Unload a custom shader from memory void SetCustomShader(Shader shader); // Set custom shader to be used in batch draw void SetDefaultShader(void); // Set default shader to be used in batch draw -void SetModelShader(Model *model, Shader shader); // Link a shader to a model +Shader GetDefaultShader(void); // Get default shader +Texture2D GetDefaultTexture(void); // Get default texture int GetShaderLocation(Shader shader, const char *uniformName); // Get shader uniform location void SetShaderValue(Shader shader, int uniformLoc, float *value, int size); // Set shader uniform value (float) -- cgit v1.2.3 From eeb151586f12f8694cccaecf12d92af7842338b5 Mon Sep 17 00:00:00 2001 From: raysan5 Date: Sat, 7 May 2016 18:28:40 +0200 Subject: Corrected issues with OpenGL 1.1 backend --- src/rlgl.c | 58 +++++++++++++++++++++++++++++++++------------------------- 1 file changed, 33 insertions(+), 25 deletions(-) (limited to 'src') diff --git a/src/rlgl.c b/src/rlgl.c index 02649e30..462ccdec 100644 --- a/src/rlgl.c +++ b/src/rlgl.c @@ -1080,22 +1080,24 @@ void rlglDrawEx(Mesh mesh, Material material, Matrix transform, bool wires) glBindTexture(GL_TEXTURE_2D, material.texDiffuse.id); // NOTE: On OpenGL 1.1 we use Vertex Arrays to draw model - glEnableClientState(GL_VERTEX_ARRAY); // Enable vertex array - glEnableClientState(GL_TEXTURE_COORD_ARRAY); // Enable texture coords array - glEnableClientState(GL_NORMAL_ARRAY); // Enable normals array + glEnableClientState(GL_VERTEX_ARRAY); // Enable vertex array + glEnableClientState(GL_TEXTURE_COORD_ARRAY); // Enable texture coords array + glEnableClientState(GL_NORMAL_ARRAY); // Enable normals array - glVertexPointer(3, GL_FLOAT, 0, mesh.vertices); // Pointer to vertex coords array - glTexCoordPointer(2, GL_FLOAT, 0, mesh.texcoords); // Pointer to texture coords array - glNormalPointer(GL_FLOAT, 0, mesh.normals); // Pointer to normals array + glVertexPointer(3, GL_FLOAT, 0, mesh.vertices); // Pointer to vertex coords array + glTexCoordPointer(2, GL_FLOAT, 0, mesh.texcoords); // Pointer to texture coords array + glNormalPointer(GL_FLOAT, 0, mesh.normals); // Pointer to normals array //glColorPointer(4, GL_UNSIGNED_BYTE, 0, mesh.colors); // Pointer to colors array (NOT USED) - rlMultMatrixf(MatrixToFloat(transform)); - - glDrawArrays(GL_TRIANGLES, 0, mesh.vertexCount); + rlPushMatrix(); + rlMultMatrixf(MatrixToFloat(transform)); + rlColor4ub(material.colDiffuse.r, material.colDiffuse.g, material.colDiffuse.b, material.colDiffuse.a); + glDrawArrays(GL_TRIANGLES, 0, mesh.vertexCount); + rlPopMatrix(); - glDisableClientState(GL_VERTEX_ARRAY); // Disable vertex array - glDisableClientState(GL_TEXTURE_COORD_ARRAY); // Disable texture coords array - glDisableClientState(GL_NORMAL_ARRAY); // Disable normals array + glDisableClientState(GL_VERTEX_ARRAY); // Disable vertex array + glDisableClientState(GL_TEXTURE_COORD_ARRAY); // Disable texture coords array + glDisableClientState(GL_NORMAL_ARRAY); // Disable normals array glDisable(GL_TEXTURE_2D); glBindTexture(GL_TEXTURE_2D, 0); @@ -1865,6 +1867,20 @@ void *rlglReadTexturePixels(Texture2D texture) // NOTE: Those functions are exposed directly to the user in raylib.h //---------------------------------------------------------------------------------- +// Get default internal texture (white texture) +Texture2D GetDefaultTexture(void) +{ + Texture2D texture; + + texture.id = whiteTexture; + texture.width = 1; + texture.height = 1; + texture.mipmaps = 1; + texture.format = UNCOMPRESSED_R8G8B8A8; + + return texture; +} + // Load a custom shader and bind default locations Shader LoadShader(char *vsFileName, char *fsFileName) { @@ -1930,7 +1946,12 @@ void SetDefaultShader(void) // Get default shader Shader GetDefaultShader(void) { +#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) return defaultShader; +#else + Shader shader = { 0 }; + return shader; +#endif } // Get shader uniform location @@ -2050,19 +2071,6 @@ static void LoadCompressedTexture(unsigned char *data, int width, int height, in } } -Texture2D GetDefaultTexture(void) -{ - Texture2D texture; - - texture.id = whiteTexture; - texture.width = 1; - texture.height = 1; - texture.mipmaps = 1; - texture.format = UNCOMPRESSED_R8G8B8A8; - - return texture; -} - // Load custom shader strings and return program id static unsigned int LoadShaderProgram(char *vShaderStr, char *fShaderStr) { -- cgit v1.2.3 From 0bcb873cbb3758d67b1d263fafb6be818ddbf067 Mon Sep 17 00:00:00 2001 From: raysan5 Date: Sun, 8 May 2016 15:24:02 +0200 Subject: Improved mesh support Depending on mesh data, it can be loaded and default vertex attribute location points are set, including colors, tangents and texcoords2 --- src/models.c | 120 ++++++++++++++++++++++++++++++++++++++++++++++------------- src/raylib.h | 6 +-- src/rlgl.c | 90 ++++++++++++++++++++++++++++++++++++-------- 3 files changed, 171 insertions(+), 45 deletions(-) (limited to 'src') diff --git a/src/models.c b/src/models.c index 9b139120..4aff7216 100644 --- a/src/models.c +++ b/src/models.c @@ -575,6 +575,89 @@ Model LoadModelEx(Mesh data) return model; } +// Load a 3d model from rRES file (raylib Resource) +Model LoadModelFromRES(const char *rresName, int resId) +{ + Model model = { 0 }; + bool found = false; + + char id[4]; // rRES file identifier + unsigned char version; // rRES file version and subversion + char useless; // rRES header reserved data + short numRes; + + ResInfoHeader infoHeader; + + FILE *rresFile = fopen(rresName, "rb"); + + if (rresFile == NULL) + { + TraceLog(WARNING, "[%s] rRES raylib resource file could not be opened", rresName); + } + else + { + // Read rres file (basic file check - id) + fread(&id[0], sizeof(char), 1, rresFile); + fread(&id[1], sizeof(char), 1, rresFile); + fread(&id[2], sizeof(char), 1, rresFile); + fread(&id[3], sizeof(char), 1, rresFile); + fread(&version, sizeof(char), 1, rresFile); + fread(&useless, sizeof(char), 1, rresFile); + + if ((id[0] != 'r') && (id[1] != 'R') && (id[2] != 'E') &&(id[3] != 'S')) + { + TraceLog(WARNING, "[%s] This is not a valid raylib resource file", rresName); + } + else + { + // Read number of resources embedded + fread(&numRes, sizeof(short), 1, rresFile); + + for (int i = 0; i < numRes; i++) + { + fread(&infoHeader, sizeof(ResInfoHeader), 1, rresFile); + + if (infoHeader.id == resId) + { + found = true; + + // Check data is of valid MODEL type + if (infoHeader.type == 8) + { + // TODO: Load model data + } + else + { + TraceLog(WARNING, "[%s] Required resource do not seem to be a valid MODEL resource", rresName); + } + } + else + { + // Depending on type, skip the right amount of parameters + switch (infoHeader.type) + { + case 0: fseek(rresFile, 6, SEEK_CUR); break; // IMAGE: Jump 6 bytes of parameters + case 1: fseek(rresFile, 6, SEEK_CUR); break; // SOUND: Jump 6 bytes of parameters + case 2: fseek(rresFile, 5, SEEK_CUR); break; // MODEL: Jump 5 bytes of parameters (TODO: Review) + case 3: break; // TEXT: No parameters + case 4: break; // RAW: No parameters + default: break; + } + + // Jump DATA to read next infoHeader + fseek(rresFile, infoHeader.size, SEEK_CUR); + } + } + } + + fclose(rresFile); + } + + if (!found) TraceLog(WARNING, "[%s] Required resource id [%i] could not be found in the raylib resource file", rresName, resId); + + return model; +} + // Load a heightmap image as a 3d model // NOTE: model map size is defined in generic units Model LoadHeightmap(Image heightmap, Vector3 size) @@ -613,18 +696,18 @@ void UnloadModel(Model model) free(model.mesh.vertices); free(model.mesh.texcoords); free(model.mesh.normals); - free(model.mesh.colors); - //if (model.mesh.texcoords2 != NULL) free(model.mesh.texcoords2); // Not used - //if (model.mesh.tangents != NULL) free(model.mesh.tangents); // Not used + if (model.mesh.colors != NULL) free(model.mesh.colors); + if (model.mesh.tangents != NULL) free(model.mesh.tangents); + if (model.mesh.texcoords2 != NULL) free(model.mesh.texcoords2); TraceLog(INFO, "Unloaded model data from RAM (CPU)"); rlDeleteBuffers(model.mesh.vboId[0]); // vertex rlDeleteBuffers(model.mesh.vboId[1]); // texcoords rlDeleteBuffers(model.mesh.vboId[2]); // normals - //rlDeleteBuffers(model.mesh.vboId[3]); // colors (NOT USED) - //rlDeleteBuffers(model.mesh.vboId[4]); // tangents (NOT USED) - //rlDeleteBuffers(model.mesh.vboId[5]); // texcoords2 (NOT USED) + rlDeleteBuffers(model.mesh.vboId[3]); // colors + rlDeleteBuffers(model.mesh.vboId[4]); // tangents + rlDeleteBuffers(model.mesh.vboId[5]); // texcoords2 rlDeleteVertexArrays(model.mesh.vaoId); } @@ -672,7 +755,7 @@ static Mesh GenMeshHeightmap(Image heightmap, Vector3 size) { #define GRAY_VALUE(c) ((c.r+c.g+c.b)/3) - Mesh mesh; + Mesh mesh = { 0 }; int mapX = heightmap.width; int mapZ = heightmap.height; @@ -687,7 +770,7 @@ static Mesh GenMeshHeightmap(Image heightmap, Vector3 size) mesh.vertices = (float *)malloc(mesh.vertexCount*3*sizeof(float)); mesh.normals = (float *)malloc(mesh.vertexCount*3*sizeof(float)); mesh.texcoords = (float *)malloc(mesh.vertexCount*2*sizeof(float)); - mesh.colors = (unsigned char *)malloc(mesh.vertexCount*4*sizeof(unsigned char)); // Not used... + mesh.colors = NULL; int vCounter = 0; // Used to count vertices float by float int tcCounter = 0; // Used to count texcoords float by float @@ -770,16 +853,12 @@ static Mesh GenMeshHeightmap(Image heightmap, Vector3 size) free(pixels); - // Fill color data - // NOTE: Not used any more... just one plain color defined at DrawModel() - for (int i = 0; i < (4*mesh.vertexCount); i++) mesh.colors[i] = 255; - return mesh; } static Mesh GenMeshCubicmap(Image cubicmap, Vector3 cubeSize) { - Mesh mesh; + Mesh mesh = { 0 }; Color *cubicmapPixels = GetImageData(cubicmap); @@ -1088,11 +1167,7 @@ static Mesh GenMeshCubicmap(Image cubicmap, Vector3 cubeSize) mesh.vertices = (float *)malloc(mesh.vertexCount*3*sizeof(float)); mesh.normals = (float *)malloc(mesh.vertexCount*3*sizeof(float)); mesh.texcoords = (float *)malloc(mesh.vertexCount*2*sizeof(float)); - mesh.colors = (unsigned char *)malloc(mesh.vertexCount*4*sizeof(unsigned char)); // Not used... - - // Fill color data - // NOTE: Not used any more... just one plain color defined at DrawModel() - for (int i = 0; i < (4*mesh.vertexCount); i++) mesh.colors[i] = 255; + mesh.colors = NULL; int fCounter = 0; @@ -1810,7 +1885,7 @@ static Mesh LoadOBJ(const char *fileName) mesh.vertices = (float *)malloc(mesh.vertexCount*3*sizeof(float)); mesh.texcoords = (float *)malloc(mesh.vertexCount*2*sizeof(float)); mesh.normals = (float *)malloc(mesh.vertexCount*3*sizeof(float)); - mesh.colors = (unsigned char *)malloc(mesh.vertexCount*4*sizeof(unsigned char)); + mesh.colors = NULL; int vCounter = 0; // Used to count vertices float by float int tcCounter = 0; // Used to count texcoords float by float @@ -1909,10 +1984,6 @@ static Mesh LoadOBJ(const char *fileName) // Security check, just in case no normals or no texcoords defined in OBJ if (numTexCoords == 0) for (int i = 0; i < (2*mesh.vertexCount); i++) mesh.texcoords[i] = 0.0f; - - // NOTE: We set all vertex colors to white - // NOTE: Not used any more... just one plain color defined at DrawModel() - for (int i = 0; i < (4*mesh.vertexCount); i++) mesh.colors[i] = 255; // Now we can free temp mid* arrays free(midVertices); @@ -1945,9 +2016,6 @@ static Material LoadMTL(const char *fileName) return material; } - // First reading pass: Get numVertex, numNormals, numTexCoords, numTriangles - // NOTE: vertex, texcoords and normals could be optimized (to be used indexed on faces definition) - // NOTE: faces MUST be defined as TRIANGLES (3 vertex per face) while(!feof(mtlFile)) { fscanf(mtlFile, "%c", &dataType); diff --git a/src/raylib.h b/src/raylib.h index c88a60f4..60384444 100644 --- a/src/raylib.h +++ b/src/raylib.h @@ -386,7 +386,7 @@ typedef struct Mesh { typedef struct Shader { unsigned int id; // Shader program id - // Variable attributes locations + // Vertex attributes locations (default locations) int vertexLoc; // Vertex attribute location point (default-location = 0) int texcoordLoc; // Texcoord attribute location point (default-location = 1) int normalLoc; // Normal attribute location point (default-location = 2) @@ -803,14 +803,14 @@ void DrawGizmo(Vector3 position); //------------------------------------------------------------------------------------ Model LoadModel(const char *fileName); // Load a 3d model (.OBJ) Model LoadModelEx(Mesh data); // Load a 3d model (from mesh data) -//Model LoadModelFromRES(const char *rresName, int resId); // TODO: Load a 3d model from rRES file (raylib Resource) +Model LoadModelFromRES(const char *rresName, int resId); // Load a 3d model from rRES file (raylib Resource) Model LoadHeightmap(Image heightmap, Vector3 size); // Load a heightmap image as a 3d model Model LoadCubicmap(Image cubicmap); // Load a map image as a 3d model (cubes based) void UnloadModel(Model model); // Unload 3d model from memory void SetModelTexture(Model *model, Texture2D texture); // Link a texture to a model Material LoadMaterial(const char *fileName); // Load material data (from file) -Material LoadDefaultMaterial(void); // Load default material (uses default models shader) +Material LoadDefaultMaterial(void); // Load default material (uses default models shader) void DrawModel(Model model, Vector3 position, float scale, Color tint); // Draw a model (with texture if set) void DrawModelEx(Model model, Vector3 position, Vector3 rotationAxis, float rotationAngle, Vector3 scale, Color tint); // Draw a model with extended parameters diff --git a/src/rlgl.c b/src/rlgl.c index 462ccdec..6ffcb8a5 100644 --- a/src/rlgl.c +++ b/src/rlgl.c @@ -811,9 +811,11 @@ void rlDeleteVertexArrays(unsigned int id) void rlDeleteBuffers(unsigned int id) { #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) - glDeleteBuffers(1, &id); - - if (!vaoSupported) TraceLog(INFO, "[VBO ID %i] Unloaded model vertex data from VRAM (GPU)", id); + if (id != 0) + { + glDeleteBuffers(1, &id); + if (!vaoSupported) TraceLog(INFO, "[VBO ID %i] Unloaded model vertex data from VRAM (GPU)", id); + } #endif } @@ -1638,6 +1640,7 @@ void rlglGenerateMipmaps(Texture2D texture) } // Upload vertex data into a VAO (if supported) and VBO +// TODO: Consider attributes: color, texcoords2, tangents (if available) void rlglLoadMesh(Mesh *mesh) { mesh->vaoId = 0; // Vertex Array Object @@ -1648,11 +1651,10 @@ void rlglLoadMesh(Mesh *mesh) mesh->vboId[4] = 0; // Vertex tangent VBO mesh->vboId[5] = 0; // Vertex texcoord2 VBO - // TODO: Consider attributes: color, texcoords2, tangents (if available) - + #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) GLuint vaoId = 0; // Vertex Array Objects (VAO) - GLuint vboId[3]; // Vertex Buffer Objects (VBOs) + GLuint vboId[6]; // Vertex Buffer Objects (VBOs) if (vaoSupported) { @@ -1661,36 +1663,92 @@ void rlglLoadMesh(Mesh *mesh) glBindVertexArray(vaoId); } - // Create buffers for our vertex data (positions, texcoords, normals) - glGenBuffers(3, vboId); - // NOTE: Attributes must be uploaded considering default locations points // Enable vertex attributes: position (shader-location = 0) + glGenBuffers(1, &vboId[0]); glBindBuffer(GL_ARRAY_BUFFER, vboId[0]); glBufferData(GL_ARRAY_BUFFER, sizeof(float)*3*mesh->vertexCount, mesh->vertices, GL_STATIC_DRAW); glVertexAttribPointer(0, 3, GL_FLOAT, 0, 0, 0); glEnableVertexAttribArray(0); // Enable vertex attributes: texcoords (shader-location = 1) + glGenBuffers(1, &vboId[1]); glBindBuffer(GL_ARRAY_BUFFER, vboId[1]); glBufferData(GL_ARRAY_BUFFER, sizeof(float)*2*mesh->vertexCount, mesh->texcoords, GL_STATIC_DRAW); glVertexAttribPointer(1, 2, GL_FLOAT, 0, 0, 0); glEnableVertexAttribArray(1); // Enable vertex attributes: normals (shader-location = 2) - glBindBuffer(GL_ARRAY_BUFFER, vboId[2]); - glBufferData(GL_ARRAY_BUFFER, sizeof(float)*3*mesh->vertexCount, mesh->normals, GL_STATIC_DRAW); - glVertexAttribPointer(2, 3, GL_FLOAT, 0, 0, 0); - glEnableVertexAttribArray(2); + if (mesh->normals != NULL) + { + glGenBuffers(1, &vboId[2]); + glBindBuffer(GL_ARRAY_BUFFER, vboId[2]); + glBufferData(GL_ARRAY_BUFFER, sizeof(float)*3*mesh->vertexCount, mesh->normals, GL_STATIC_DRAW); + glVertexAttribPointer(2, 3, GL_FLOAT, 0, 0, 0); + glEnableVertexAttribArray(2); + } + else + { + // Default color vertex attribute set to WHITE + glVertexAttrib3f(2, 1.0f, 1.0f, 1.0f); + glDisableVertexAttribArray(2); + } // Default color vertex attribute (shader-location = 3) - glVertexAttrib4f(3, 1.0f, 1.0f, 1.0f, 1.0f); // Color vertex attribute set to default: WHITE - glDisableVertexAttribArray(3); + if (mesh->colors != NULL) + { + glGenBuffers(1, &vboId[3]); + glBindBuffer(GL_ARRAY_BUFFER, vboId[3]); + glBufferData(GL_ARRAY_BUFFER, sizeof(unsigned char)*4*mesh->vertexCount, mesh->colors, GL_STATIC_DRAW); + glVertexAttribPointer(3, 4, GL_UNSIGNED_BYTE, GL_TRUE, 0, 0); + glEnableVertexAttribArray(3); + } + else + { + // Default color vertex attribute set to WHITE + glVertexAttrib4f(3, 1.0f, 1.0f, 1.0f, 1.0f); + glDisableVertexAttribArray(3); + } + + // Default tangent vertex attribute (shader-location = 4) + if (mesh->tangents != NULL) + { + glGenBuffers(1, &vboId[4]); + glBindBuffer(GL_ARRAY_BUFFER, vboId[4]); + glBufferData(GL_ARRAY_BUFFER, sizeof(float)*3*mesh->vertexCount, mesh->tangents, GL_STATIC_DRAW); + glVertexAttribPointer(4, 3, GL_FLOAT, 0, 0, 0); + glEnableVertexAttribArray(4); + } + else + { + // Default tangents vertex attribute + glVertexAttrib3f(4, 0.0f, 0.0f, 0.0f); + glDisableVertexAttribArray(4); + } + + // Default texcoord2 vertex attribute (shader-location = 5) + if (mesh->texcoords2 != NULL) + { + glGenBuffers(1, &vboId[5]); + glBindBuffer(GL_ARRAY_BUFFER, vboId[5]); + glBufferData(GL_ARRAY_BUFFER, sizeof(float)*2*mesh->vertexCount, mesh->texcoords2, GL_STATIC_DRAW); + glVertexAttribPointer(5, 2, GL_FLOAT, 0, 0, 0); + glEnableVertexAttribArray(5); + } + else + { + // Default tangents vertex attribute + glVertexAttrib2f(5, 0.0f, 0.0f); + glDisableVertexAttribArray(5); + } mesh->vboId[0] = vboId[0]; // Vertex position VBO mesh->vboId[1] = vboId[1]; // Texcoords VBO mesh->vboId[2] = vboId[2]; // Normals VBO + mesh->vboId[3] = vboId[3]; // Colors VBO + mesh->vboId[4] = vboId[4]; // Tangents VBO + mesh->vboId[5] = vboId[5]; // Texcoords2 VBO if (vaoSupported) { @@ -1703,7 +1761,7 @@ void rlglLoadMesh(Mesh *mesh) } else { - TraceLog(INFO, "[VBO ID %i][VBO ID %i][VBO ID %i] Mesh uploaded successfully to VRAM (GPU)", mesh->vboId[0], mesh->vboId[1], mesh->vboId[2]); + TraceLog(INFO, "[VBOs] Mesh uploaded successfully to VRAM (GPU)"); } #endif } -- cgit v1.2.3 From f7d4951165e8bece5ae58cd4c92b08e4f29cbef7 Mon Sep 17 00:00:00 2001 From: Ray Date: Sun, 8 May 2016 23:50:35 +0200 Subject: Improved vertex attribs support for models --- src/models.c | 2 +- src/raylib.h | 14 ++++++++------ src/rlgl.c | 45 ++++++++++++++++++++++++++++++++++++--------- 3 files changed, 45 insertions(+), 16 deletions(-) (limited to 'src') diff --git a/src/models.c b/src/models.c index 4aff7216..ee04b449 100644 --- a/src/models.c +++ b/src/models.c @@ -695,7 +695,7 @@ void UnloadModel(Model model) // Unload mesh data free(model.mesh.vertices); free(model.mesh.texcoords); - free(model.mesh.normals); + if (model.mesh.normals != NULL) free(model.mesh.normals); if (model.mesh.colors != NULL) free(model.mesh.colors); if (model.mesh.tangents != NULL) free(model.mesh.tangents); if (model.mesh.texcoords2 != NULL) free(model.mesh.texcoords2); diff --git a/src/raylib.h b/src/raylib.h index 60384444..1258bffc 100644 --- a/src/raylib.h +++ b/src/raylib.h @@ -369,12 +369,12 @@ typedef struct BoundingBox { // Vertex data definning a mesh typedef struct Mesh { int vertexCount; // num vertices - float *vertices; // vertex position (XYZ - 3 components per vertex) - float *texcoords; // vertex texture coordinates (UV - 2 components per vertex) - float *texcoords2; // vertex second texture coordinates (useful for lightmaps) - float *normals; // vertex normals (XYZ - 3 components per vertex) - float *tangents; // vertex tangents (XYZ - 3 components per vertex) - unsigned char *colors; // vertex colors (RGBA - 4 components per vertex) + float *vertices; // vertex position (XYZ - 3 components per vertex) (shader-location = 0) + float *texcoords; // vertex texture coordinates (UV - 2 components per vertex) (shader-location = 1) + float *texcoords2; // vertex second texture coordinates (useful for lightmaps) (shader-location = 5) + float *normals; // vertex normals (XYZ - 3 components per vertex) (shader-location = 2) + float *tangents; // vertex tangents (XYZ - 3 components per vertex) (shader-location = 4) + unsigned char *colors; // vertex colors (RGBA - 4 components per vertex) (shader-location = 3) BoundingBox bounds; // mesh limits defined by min and max points @@ -391,6 +391,8 @@ typedef struct Shader { int texcoordLoc; // Texcoord attribute location point (default-location = 1) int normalLoc; // Normal attribute location point (default-location = 2) int colorLoc; // Color attibute location point (default-location = 3) + int tangentLoc; // Tangent attribute location point (default-location = 4) + int texcoord2Loc; // Texcoord2 attribute location point (default-location = 5) // Uniform locations int mvpLoc; // ModelView-Projection matrix uniform location point (vertex shader) diff --git a/src/rlgl.c b/src/rlgl.c index 6ffcb8a5..2e4601df 100644 --- a/src/rlgl.c +++ b/src/rlgl.c @@ -1084,12 +1084,13 @@ void rlglDrawEx(Mesh mesh, Material material, Matrix transform, bool wires) // NOTE: On OpenGL 1.1 we use Vertex Arrays to draw model glEnableClientState(GL_VERTEX_ARRAY); // Enable vertex array glEnableClientState(GL_TEXTURE_COORD_ARRAY); // Enable texture coords array - glEnableClientState(GL_NORMAL_ARRAY); // Enable normals array + if (mesh.normals != NULL) glEnableClientState(GL_NORMAL_ARRAY); // Enable normals array + if (mesh.colors != NULL) glEnableClientState(GL_COLOR_ARRAY); // Enable colors array glVertexPointer(3, GL_FLOAT, 0, mesh.vertices); // Pointer to vertex coords array glTexCoordPointer(2, GL_FLOAT, 0, mesh.texcoords); // Pointer to texture coords array - glNormalPointer(GL_FLOAT, 0, mesh.normals); // Pointer to normals array - //glColorPointer(4, GL_UNSIGNED_BYTE, 0, mesh.colors); // Pointer to colors array (NOT USED) + if (mesh.normals != NULL) glNormalPointer(GL_FLOAT, 0, mesh.normals); // Pointer to normals array + if (mesh.colors != NULL) glColorPointer(4, GL_UNSIGNED_BYTE, 0, mesh.colors); // Pointer to colors array rlPushMatrix(); rlMultMatrixf(MatrixToFloat(transform)); @@ -1099,7 +1100,8 @@ void rlglDrawEx(Mesh mesh, Material material, Matrix transform, bool wires) glDisableClientState(GL_VERTEX_ARRAY); // Disable vertex array glDisableClientState(GL_TEXTURE_COORD_ARRAY); // Disable texture coords array - glDisableClientState(GL_NORMAL_ARRAY); // Disable normals array + if (mesh.normals != NULL) glDisableClientState(GL_NORMAL_ARRAY); // Disable normals array + if (mesh.colors != NULL) glDisableClientState(GL_NORMAL_ARRAY); // Disable colors array glDisable(GL_TEXTURE_2D); glBindTexture(GL_TEXTURE_2D, 0); @@ -1170,7 +1172,29 @@ void rlglDrawEx(Mesh mesh, Material material, Matrix transform, bool wires) glEnableVertexAttribArray(material.shader.normalLoc); } - // TODO: Bind mesh VBO data: colors, tangents, texcoords2 (if available) + // Bind mesh VBO data: vertex colors (shader-location = 3, if available) , tangents, texcoords2 (if available) + if (material.shader.colorLoc != -1) + { + glBindBuffer(GL_ARRAY_BUFFER, mesh.vboId[3]); + glVertexAttribPointer(material.shader.colorLoc, 4, GL_UNSIGNED_BYTE, GL_TRUE, 0, 0); + glEnableVertexAttribArray(material.shader.colorLoc); + } + + // Bind mesh VBO data: vertex tangents (shader-location = 4, if available) + if (material.shader.tangentLoc != -1) + { + glBindBuffer(GL_ARRAY_BUFFER, mesh.vboId[4]); + glVertexAttribPointer(material.shader.tangentLoc, 3, GL_FLOAT, 0, 0, 0); + glEnableVertexAttribArray(material.shader.tangentLoc); + } + + // Bind mesh VBO data: vertex texcoords2 (shader-location = 5, if available) + if (material.shader.texcoord2Loc != -1) + { + glBindBuffer(GL_ARRAY_BUFFER, mesh.vboId[5]); + glVertexAttribPointer(material.shader.texcoord2Loc, 2, GL_FLOAT, 0, 0, 0); + glEnableVertexAttribArray(material.shader.texcoord2Loc); + } } // Draw call! @@ -1640,16 +1664,15 @@ void rlglGenerateMipmaps(Texture2D texture) } // Upload vertex data into a VAO (if supported) and VBO -// TODO: Consider attributes: color, texcoords2, tangents (if available) void rlglLoadMesh(Mesh *mesh) { mesh->vaoId = 0; // Vertex Array Object mesh->vboId[0] = 0; // Vertex positions VBO mesh->vboId[1] = 0; // Vertex texcoords VBO mesh->vboId[2] = 0; // Vertex normals VBO - mesh->vboId[3] = 0; // Vertex color VBO - mesh->vboId[4] = 0; // Vertex tangent VBO - mesh->vboId[5] = 0; // Vertex texcoord2 VBO + mesh->vboId[3] = 0; // Vertex colors VBO + mesh->vboId[4] = 0; // Vertex tangents VBO + mesh->vboId[5] = 0; // Vertex texcoords2 VBO #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) @@ -2314,12 +2337,16 @@ static void LoadDefaultShaderLocations(Shader *shader) // vertex texcoord location = 1 // vertex normal location = 2 // vertex color location = 3 + // vertex tangent location = 4 + // vertex texcoord2 location = 5 // Get handles to GLSL input attibute locations shader->vertexLoc = glGetAttribLocation(shader->id, "vertexPosition"); shader->texcoordLoc = glGetAttribLocation(shader->id, "vertexTexCoord"); shader->normalLoc = glGetAttribLocation(shader->id, "vertexNormal"); shader->colorLoc = glGetAttribLocation(shader->id, "vertexColor"); + shader->tangentLoc = glGetAttribLocation(shader->id, "vertexTangent"); + shader->texcoord2Loc = glGetAttribLocation(shader->id, "vertexTexCoord2"); // Get handles to GLSL uniform locations (vertex shader) shader->mvpLoc = glGetUniformLocation(shader->id, "mvpMatrix"); -- cgit v1.2.3 From dc4d5dabcdf8f35f32acb9c5b48a24b1141c460f Mon Sep 17 00:00:00 2001 From: Ray Date: Mon, 9 May 2016 01:18:46 +0200 Subject: Added MTL loading info --- src/models.c | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/models.c b/src/models.c index ee04b449..7b6deb5c 100644 --- a/src/models.c +++ b/src/models.c @@ -2001,7 +2001,26 @@ static Material LoadMTL(const char *fileName) { Material material = { 0 }; - // TODO: Load mtl file + // TODO: Load mtl file (multiple variations of .mtl format) + /* + newmtl string Material newmtl (material name). Begins a new material description. + Ka float float float Ambient color Ka (red) (green) (blue) + Kd float float float Diffuse color Kd (red) (green) (blue) + Ks float float float Specular color Ks (red) (green) (blue) + Ke float float float Emmisive color + d float Tr float Dissolve factor. Transparency Tr (alpha). d is inverse of Tr + Ns int Shininess Ns (specular power). Ranges from 0 to 1000. Specular exponent. + Ni int Refraction index. + illum int Illumination model illum (1 / 2); 1 if specular disabled, 2 if specular enabled (lambertian model) + map_Kd string Texture map_Kd (filename) + map_Kd string Diffuse color texture map. + map_Ks string Specular color texture map. + map_Ka string Ambient color texture map. + map_Bump string Bump texture map. Alternative: bump string / map_bump string + map_d string Opacity texture map. + disp string Displacement map + refl Reflection type and map + */ char dataType; char comments[200]; -- cgit v1.2.3 From 3d0208223ad5b79cb4c3d6cd367d39f9d4e51662 Mon Sep 17 00:00:00 2001 From: Ray Date: Mon, 9 May 2016 12:40:59 +0200 Subject: First implementation of MTL loading Not tested yet --- src/models.c | 156 ++++++++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 132 insertions(+), 24 deletions(-) (limited to 'src') diff --git a/src/models.c b/src/models.c index 7b6deb5c..dff6b224 100644 --- a/src/models.c +++ b/src/models.c @@ -1997,33 +1997,16 @@ static Mesh LoadOBJ(const char *fileName) } // Load MTL material data +// NOTE: Texture map parameters are not supported static Material LoadMTL(const char *fileName) { - Material material = { 0 }; + #define MAX_BUFFER_SIZE 128 - // TODO: Load mtl file (multiple variations of .mtl format) - /* - newmtl string Material newmtl (material name). Begins a new material description. - Ka float float float Ambient color Ka (red) (green) (blue) - Kd float float float Diffuse color Kd (red) (green) (blue) - Ks float float float Specular color Ks (red) (green) (blue) - Ke float float float Emmisive color - d float Tr float Dissolve factor. Transparency Tr (alpha). d is inverse of Tr - Ns int Shininess Ns (specular power). Ranges from 0 to 1000. Specular exponent. - Ni int Refraction index. - illum int Illumination model illum (1 / 2); 1 if specular disabled, 2 if specular enabled (lambertian model) - map_Kd string Texture map_Kd (filename) - map_Kd string Diffuse color texture map. - map_Ks string Specular color texture map. - map_Ka string Ambient color texture map. - map_Bump string Bump texture map. Alternative: bump string / map_bump string - map_d string Opacity texture map. - disp string Displacement map - refl Reflection type and map - */ + Material material = { 0 }; // LoadDefaultMaterial(); - char dataType; - char comments[200]; + char buffer[MAX_BUFFER_SIZE]; + Vector3 color = { 1.0f, 1.0f, 1.0f }; + char *mapFileName; FILE *mtlFile; @@ -2037,7 +2020,132 @@ static Material LoadMTL(const char *fileName) while(!feof(mtlFile)) { - fscanf(mtlFile, "%c", &dataType); + fgets(buffer, MAX_BUFFER_SIZE, mtlFile); + + switch (buffer[0]) + { + case 'n': // newmtl string Material name. Begins a new material description. + { + // TODO: Support multiple materials in a single .mtl + sscanf(buffer, "newmtl %s", mapFileName); + + TraceLog(INFO, "[%s] Loading material...", mapFileName); + } + case 'i': // illum int Illumination model + { + // illum = 1 if specular disabled + // illum = 2 if specular enabled (lambertian model) + // ... + } + case 'K': // Ka, Kd, Ks, Ke + { + switch (buffer[1]) + { + case 'a': // Ka float float float Ambient color (RGB) + { + sscanf(buffer, "Ka %f %f %f", &color.x, &color.y, &color.z); + material.colAmbient.r = (unsigned char)(color.x*255); + material.colAmbient.g = (unsigned char)(color.y*255); + material.colAmbient.b = (unsigned char)(color.z*255); + } break; + case 'd': // Kd float float float Diffuse color (RGB) + { + sscanf(buffer, "Kd %f %f %f", &color.x, &color.y, &color.z); + material.colDiffuse.r = (unsigned char)(color.x*255); + material.colDiffuse.g = (unsigned char)(color.y*255); + material.colDiffuse.b = (unsigned char)(color.z*255); + } break; + case 's': // Ks float float float Specular color (RGB) + { + sscanf(buffer, "Ks %f %f %f", &color.x, &color.y, &color.z); + material.colSpecular.r = (unsigned char)(color.x*255); + material.colSpecular.g = (unsigned char)(color.y*255); + material.colSpecular.b = (unsigned char)(color.z*255); + } break; + case 'e': // Ke float float float Emmisive color (RGB) + { + // TODO: Support Ke ? + } break; + default: break; + } + } break; + case 'N': // Ns, Ni + { + if (buffer[1] == 's') // Ns int Shininess (specular exponent). Ranges from 0 to 1000. + { + sscanf(buffer, "Ns %i", &material.glossiness); + } + else if (buffer[1] == 'i') // Ni int Refraction index. + { + // Not supported... + } + } break; + case 'm': // map_Kd, map_Ks, map_Ka, map_Bump, map_d + { + switch (buffer[4]) + { + case 'K': // Color texture maps + { + if (buffer[5] == 'd') // map_Kd string Diffuse color texture map. + { + sscanf(buffer, "map_Kd %s", mapFileName); + if (mapFileName != NULL) material.texDiffuse = LoadTexture(mapFileName); + } + else if (buffer[5] == 's') // map_Ks string Specular color texture map. + { + sscanf(buffer, "map_Ks %s", mapFileName); + if (mapFileName != NULL) material.texSpecular = LoadTexture(mapFileName); + } + else if (buffer[5] == 'a') // map_Ka string Ambient color texture map. + { + // Not supported... + } + } break; + case 'B': // map_Bump string Bump texture map. + { + sscanf(buffer, "map_Bump %s", mapFileName); + if (mapFileName != NULL) material.texNormal = LoadTexture(mapFileName); + } break; + case 'b': // map_bump string Bump texture map. + { + sscanf(buffer, "map_bump %s", mapFileName); + if (mapFileName != NULL) material.texNormal = LoadTexture(mapFileName); + } break; + case 'd': // map_d string Opacity texture map. + { + // Not supported... + } break; + default: break; + } + } break; + case 'd': // d, disp + { + if (buffer[1] == ' ') // d float Dissolve factor. d is inverse of Tr + { + float alpha = 1.0f; + sscanf(buffer, "d %f", &alpha); + material.colDiffuse.a = (unsigned char)(alpha*255); + } + else if (buffer[1] == 'i') // disp string Displacement map + { + // Not supported... + } + } break; + case 'b': // bump string Bump texture map + { + sscanf(buffer, "bump %s", mapFileName); + if (mapFileName != NULL) material.texNormal = LoadTexture(mapFileName); + } break; + case 'T': // Tr float Transparency Tr (alpha). Tr is inverse of d + { + float ialpha = 0.0f; + sscanf(buffer, "Tr %f", &ialpha); + material.colDiffuse.a = (unsigned char)((1.0f - ialpha)*255); + + } break; + case 'r': // refl string Reflection texture map + default: break; + } } fclose(mtlFile); -- cgit v1.2.3 From c85cd290491baa7be2b107bdb72c4b039dfd8cb3 Mon Sep 17 00:00:00 2001 From: Ray Date: Mon, 9 May 2016 12:41:53 +0200 Subject: Added defines for default shader names --- src/rlgl.c | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/rlgl.c b/src/rlgl.c index 2e4601df..33f12deb 100644 --- a/src/rlgl.c +++ b/src/rlgl.c @@ -115,6 +115,15 @@ #define GL_UNSIGNED_SHORT_5_5_5_1 0x8034 #define GL_UNSIGNED_SHORT_4_4_4_4 0x8033 #endif + +// Default vertex attribute names on shader to set location points +#define DEFAULT_ATTRIB_POSITION_NAME "vertexPosition" // shader-location = 0 +#define DEFAULT_ATTRIB_TEXCOORD_NAME "vertexTexCoord" // shader-location = 1 +#define DEFAULT_ATTRIB_NORMAL_NAME "vertexNormal" // shader-location = 2 +#define DEFAULT_ATTRIB_COLOR_NAME "vertexColor" // shader-location = 3 +#define DEFAULT_ATTRIB_TANGENT_NAME "vertexTangent" // shader-location = 4 +#define DEFAULT_ATTRIB_TEXCOORD2_NAME "vertexTexCoord2" // shader-location = 5 + //---------------------------------------------------------------------------------- // Types and Structures Definition //---------------------------------------------------------------------------------- @@ -2220,12 +2229,12 @@ static unsigned int LoadShaderProgram(char *vShaderStr, char *fShaderStr) glAttachShader(program, fragmentShader); // NOTE: Default attribute shader locations must be binded before linking - glBindAttribLocation(program, 0, "vertexPosition"); - glBindAttribLocation(program, 1, "vertexTexCoord"); - glBindAttribLocation(program, 2, "vertexNormal"); - glBindAttribLocation(program, 3, "vertexColor"); - glBindAttribLocation(program, 4, "vertexTangent"); - glBindAttribLocation(program, 5, "vertexTexCoord2"); + glBindAttribLocation(program, 0, DEFAULT_ATTRIB_POSITION_NAME); + glBindAttribLocation(program, 1, DEFAULT_ATTRIB_TEXCOORD_NAME); + glBindAttribLocation(program, 2, DEFAULT_ATTRIB_NORMAL_NAME); + glBindAttribLocation(program, 3, DEFAULT_ATTRIB_COLOR_NAME); + glBindAttribLocation(program, 4, DEFAULT_ATTRIB_TANGENT_NAME); + glBindAttribLocation(program, 5, DEFAULT_ATTRIB_TEXCOORD2_NAME); // NOTE: If some attrib name is no found on the shader, it locations becomes -1 -- cgit v1.2.3 From ac44db26a2a2bed901c7e75d96e215fa09bd3705 Mon Sep 17 00:00:00 2001 From: Ray Date: Mon, 9 May 2016 13:16:44 +0200 Subject: Added reference --- src/models.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/models.c b/src/models.c index dff6b224..7d24e383 100644 --- a/src/models.c +++ b/src/models.c @@ -1996,7 +1996,7 @@ static Mesh LoadOBJ(const char *fileName) return mesh; } -// Load MTL material data +// Load MTL material data (specs: http://paulbourke.net/dataformats/mtl/) // NOTE: Texture map parameters are not supported static Material LoadMTL(const char *fileName) { -- cgit v1.2.3 From b7f8e97b0384ae37bff110a2ef42cc42cfa09228 Mon Sep 17 00:00:00 2001 From: Joshua Reisenauer Date: Tue, 10 May 2016 01:54:20 -0700 Subject: final fix for audiocontext system now it works --- src/audio.c | 74 ++++++++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 51 insertions(+), 23 deletions(-) (limited to 'src') diff --git a/src/audio.c b/src/audio.c index 8f6431f6..fbf53df6 100644 --- a/src/audio.c +++ b/src/audio.c @@ -102,6 +102,7 @@ typedef struct AudioContext_t { unsigned char channels; // 1=mono,2=stereo unsigned char mixChannel; // 0-3 or mixA-mixD, each mix channel can receive up to one dedicated audio stream bool floatingPoint; // if false then the short datatype is used instead + bool playing; ALenum alFormat; // openAL format specifier ALuint alSource; // openAL source ALuint alBuffer[MAX_STREAM_BUFFERS]; // openAL sample buffer @@ -211,7 +212,7 @@ AudioContext InitAudioContext(unsigned short sampleRate, unsigned char mixChanne else StopMusicStream(); if(!mixChannelsActive_g[mixChannel]){ - AudioContext_t *ac = malloc(sizeof(AudioContext_t)); + AudioContext_t *ac = (AudioContext_t*)malloc(sizeof(AudioContext_t)); ac->sampleRate = sampleRate; ac->channels = channels; ac->mixChannel = mixChannel; @@ -250,8 +251,8 @@ AudioContext InitAudioContext(unsigned short sampleRate, unsigned char mixChanne FillAlBufferWithSilence(ac, ac->alBuffer[x]); alSourceQueueBuffers(ac->alSource, MAX_STREAM_BUFFERS, ac->alBuffer); - alSourcei(ac->alSource, AL_LOOPING, AL_FALSE); // this could cause errors alSourcePlay(ac->alSource); + ac->playing = true; return ac; } @@ -264,6 +265,7 @@ void CloseAudioContext(AudioContext ctx) AudioContext_t *context = (AudioContext_t*)ctx; if(context){ alSourceStop(context->alSource); + context->playing = false; //flush out all queued buffers ALuint buffer = 0; @@ -287,22 +289,29 @@ void CloseAudioContext(AudioContext ctx) // Pushes more audio data into context mix channel, if none are ever pushed then zeros are fed in. // Call "UpdateAudioContext(ctx, NULL, 0)" if you want to pause the audio. // @Returns number of samples that where processed. -// All data streams should be of a length that is evenly divisible by MUSIC_BUFFER_SIZE, -// otherwise the remaining data will not be pushed. unsigned short UpdateAudioContext(AudioContext ctx, void *data, unsigned short numberElements) { AudioContext_t *context = (AudioContext_t*)ctx; - if(context && context->channels == 2 && numberElements % 2 != 0) return 0; // when there is two channels there must be an even number of samples + if(!context || (context->channels == 2 && numberElements % 2 != 0)) return 0; // when there is two channels there must be an even number of samples - if (!data || !numberElements) alSourcePause(context->alSource); // pauses audio until data is given - else{ // restart audio otherwise + if (!data || !numberElements) + { // pauses audio until data is given + alSourcePause(context->alSource); + context->playing = false; + return 0; + } + else + { // restart audio otherwise ALint state; alGetSourcei(context->alSource, AL_SOURCE_STATE, &state); - if (state != AL_PLAYING) alSourcePlay(context->alSource); + if (state != AL_PLAYING){ + alSourcePlay(context->alSource); + context->playing = true; + } } - if (context && mixChannelsActive_g[context->mixChannel] == context) + if (context && context->playing && mixChannelsActive_g[context->mixChannel] == context) { ALint processed = 0; ALuint buffer = 0; @@ -311,36 +320,55 @@ unsigned short UpdateAudioContext(AudioContext ctx, void *data, unsigned short n alGetSourcei(context->alSource, AL_BUFFERS_PROCESSED, &processed); // Get the number of already processed buffers (if any) - if(!processed) return 0;//nothing to process, queue is still full + if(!processed) return 0; // nothing to process, queue is still full + - if(numberRemaining)// buffer data stream in increments of MUSIC_BUFFER_SIZE + while (processed > 0) { - while (processed > 0) + if(context->floatingPoint) // process float buffers { - if(context->floatingPoint && numberRemaining >= MUSIC_BUFFER_SIZE_FLOAT) // process float buffers + float *ptr = (float*)data; + alSourceUnqueueBuffers(context->alSource, 1, &buffer); + if(numberRemaining >= MUSIC_BUFFER_SIZE_FLOAT) { - float *ptr = (float*)data; - alSourceUnqueueBuffers(context->alSource, 1, &buffer); alBufferData(buffer, context->alFormat, &ptr[numberProcessed], MUSIC_BUFFER_SIZE_FLOAT*sizeof(float), context->sampleRate); - alSourceQueueBuffers(context->alSource, 1, &buffer); numberProcessed+=MUSIC_BUFFER_SIZE_FLOAT; numberRemaining-=MUSIC_BUFFER_SIZE_FLOAT; } - else if(!context->floatingPoint && numberRemaining >= MUSIC_BUFFER_SIZE_SHORT) // process short buffers + else { - short *ptr = (short*)data; - alSourceUnqueueBuffers(context->alSource, 1, &buffer); - alBufferData(buffer, context->alFormat, &ptr[numberProcessed], MUSIC_BUFFER_SIZE_SHORT*sizeof(short), context->sampleRate); - alSourceQueueBuffers(context->alSource, 1, &buffer); + alBufferData(buffer, context->alFormat, &ptr[numberProcessed], numberRemaining*sizeof(float), context->sampleRate); + numberProcessed+=numberRemaining; + numberRemaining=0; + } + alSourceQueueBuffers(context->alSource, 1, &buffer); + processed--; + } + else if(!context->floatingPoint) // process short buffers + { + short *ptr = (short*)data; + alSourceUnqueueBuffers(context->alSource, 1, &buffer); + if(numberRemaining >= MUSIC_BUFFER_SIZE_SHORT) + { + alBufferData(buffer, context->alFormat, &ptr[numberProcessed], MUSIC_BUFFER_SIZE_FLOAT*sizeof(short), context->sampleRate); numberProcessed+=MUSIC_BUFFER_SIZE_SHORT; numberRemaining-=MUSIC_BUFFER_SIZE_SHORT; } - + else + { + alBufferData(buffer, context->alFormat, &ptr[numberProcessed], numberRemaining*sizeof(short), context->sampleRate); + numberProcessed+=numberRemaining; + numberRemaining=0; + } + alSourceQueueBuffers(context->alSource, 1, &buffer); processed--; } + else + break; } + return numberProcessed; } - return numberProcessed; + return 0; } // fill buffer with zeros, returns number processed -- cgit v1.2.3 From 1ddf594d15f31f1502eeb18f0f0c8039799fff01 Mon Sep 17 00:00:00 2001 From: raysan5 Date: Tue, 10 May 2016 18:24:28 +0200 Subject: Added support for indexed mesh data --- src/models.c | 21 +++++++++++++++++---- src/raylib.h | 7 +++++-- src/rlgl.c | 39 ++++++++++++++++++++++++++++----------- 3 files changed, 50 insertions(+), 17 deletions(-) (limited to 'src') diff --git a/src/models.c b/src/models.c index 7d24e383..41150179 100644 --- a/src/models.c +++ b/src/models.c @@ -553,7 +553,7 @@ Model LoadModel(const char *fileName) if (model.mesh.vertexCount == 0) TraceLog(WARNING, "Model could not be loaded"); else { - rlglLoadMesh(&model.mesh); // Upload vertex data to GPU + rlglLoadMesh(&model.mesh); // Upload vertex data to GPU model.transform = MatrixIdentity(); model.material = LoadDefaultMaterial(); @@ -567,7 +567,9 @@ Model LoadModelEx(Mesh data) { Model model = { 0 }; - rlglLoadMesh(&data); // Upload vertex data to GPU + model.mesh = data; + + rlglLoadMesh(&model.mesh); // Upload vertex data to GPU model.transform = MatrixIdentity(); model.material = LoadDefaultMaterial(); @@ -693,12 +695,13 @@ Model LoadCubicmap(Image cubicmap) void UnloadModel(Model model) { // Unload mesh data - free(model.mesh.vertices); - free(model.mesh.texcoords); + if (model.mesh.vertices != NULL) free(model.mesh.vertices); + if (model.mesh.texcoords != NULL) free(model.mesh.texcoords); if (model.mesh.normals != NULL) free(model.mesh.normals); if (model.mesh.colors != NULL) free(model.mesh.colors); if (model.mesh.tangents != NULL) free(model.mesh.tangents); if (model.mesh.texcoords2 != NULL) free(model.mesh.texcoords2); + if (model.mesh.indices != NULL) free(model.mesh.indices); TraceLog(INFO, "Unloaded model data from RAM (CPU)"); @@ -708,8 +711,11 @@ void UnloadModel(Model model) rlDeleteBuffers(model.mesh.vboId[3]); // colors rlDeleteBuffers(model.mesh.vboId[4]); // tangents rlDeleteBuffers(model.mesh.vboId[5]); // texcoords2 + rlDeleteBuffers(model.mesh.vboId[6]); // indices rlDeleteVertexArrays(model.mesh.vaoId); + + UnloadMaterial(model.material); } // Load material data (from file) @@ -743,6 +749,13 @@ Material LoadDefaultMaterial(void) return material; } +void UnloadMaterial(Material material) +{ + rlDeleteTextures(material.texDiffuse.id); + rlDeleteTextures(material.texNormal.id); + rlDeleteTextures(material.texSpecular.id); +} + // Link a texture to a model void SetModelTexture(Model *model, Texture2D texture) { diff --git a/src/raylib.h b/src/raylib.h index 1258bffc..05463325 100644 --- a/src/raylib.h +++ b/src/raylib.h @@ -368,18 +368,20 @@ typedef struct BoundingBox { // Vertex data definning a mesh typedef struct Mesh { - int vertexCount; // num vertices + int vertexCount; // number of vertices stored in arrays float *vertices; // vertex position (XYZ - 3 components per vertex) (shader-location = 0) float *texcoords; // vertex texture coordinates (UV - 2 components per vertex) (shader-location = 1) float *texcoords2; // vertex second texture coordinates (useful for lightmaps) (shader-location = 5) float *normals; // vertex normals (XYZ - 3 components per vertex) (shader-location = 2) float *tangents; // vertex tangents (XYZ - 3 components per vertex) (shader-location = 4) unsigned char *colors; // vertex colors (RGBA - 4 components per vertex) (shader-location = 3) + unsigned short *indices; // vertex indices (in case vertex data comes indexed) + int trianglesCount; // number of triangles to draw BoundingBox bounds; // mesh limits defined by min and max points unsigned int vaoId; // OpenGL Vertex Array Object id - unsigned int vboId[6]; // OpenGL Vertex Buffer Objects id (6 types of vertex data) + unsigned int vboId[7]; // OpenGL Vertex Buffer Objects id (7 types of vertex data) } Mesh; // Shader type (generic shader) @@ -813,6 +815,7 @@ void SetModelTexture(Model *model, Texture2D texture); // Link a textur Material LoadMaterial(const char *fileName); // Load material data (from file) Material LoadDefaultMaterial(void); // Load default material (uses default models shader) +void UnloadMaterial(Material material); // Unload material textures from VRAM void DrawModel(Model model, Vector3 position, float scale, Color tint); // Draw a model (with texture if set) void DrawModelEx(Model model, Vector3 position, Vector3 rotationAxis, float rotationAngle, Vector3 scale, Color tint); // Draw a model with extended parameters diff --git a/src/rlgl.c b/src/rlgl.c index 33f12deb..8364c4f1 100644 --- a/src/rlgl.c +++ b/src/rlgl.c @@ -783,16 +783,16 @@ void rlDisableDepthTest(void) // Unload texture from GPU memory void rlDeleteTextures(unsigned int id) { - glDeleteTextures(1, &id); + if (id != 0) glDeleteTextures(1, &id); } // Unload render texture from GPU memory void rlDeleteRenderTextures(RenderTexture2D target) { #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) - glDeleteFramebuffers(1, &target.id); - glDeleteTextures(1, &target.texture.id); - glDeleteTextures(1, &target.depth.id); + if (target.id != 0) glDeleteFramebuffers(1, &target.id); + if (target.texture.id != 0) glDeleteTextures(1, &target.texture.id); + if (target.depth.id != 0) glDeleteTextures(1, &target.depth.id); #endif } @@ -800,7 +800,7 @@ void rlDeleteRenderTextures(RenderTexture2D target) void rlDeleteShader(unsigned int id) { #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) - glDeleteProgram(id); + if (id != 0) glDeleteProgram(id); #endif } @@ -810,7 +810,7 @@ void rlDeleteVertexArrays(unsigned int id) #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) if (vaoSupported) { - glDeleteVertexArrays(1, &id); + if (id != 0) glDeleteVertexArrays(1, &id); TraceLog(INFO, "[VAO ID %i] Unloaded model data from VRAM (GPU)", id); } #endif @@ -1204,10 +1204,13 @@ void rlglDrawEx(Mesh mesh, Material material, Matrix transform, bool wires) glVertexAttribPointer(material.shader.texcoord2Loc, 2, GL_FLOAT, 0, 0, 0); glEnableVertexAttribArray(material.shader.texcoord2Loc); } + + if (mesh.indices != NULL) glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, quadsBuffer[3]); } // Draw call! - glDrawArrays(GL_TRIANGLES, 0, mesh.vertexCount); + if (mesh.indices != NULL) glDrawElements(GL_TRIANGLES, mesh.trianglesCount*3, GL_UNSIGNED_SHORT, 0); // Indexed vertices draw + else glDrawArrays(GL_TRIANGLES, 0, mesh.vertexCount); if (material.texNormal.id != 0) { @@ -1225,7 +1228,11 @@ void rlglDrawEx(Mesh mesh, Material material, Matrix transform, bool wires) glBindTexture(GL_TEXTURE_2D, 0); // Unbind textures if (vaoSupported) glBindVertexArray(0); // Unbind VAO - else glBindBuffer(GL_ARRAY_BUFFER, 0); // Unbind VBOs + else + { + glBindBuffer(GL_ARRAY_BUFFER, 0); // Unbind VBOs + if (mesh.indices != NULL) glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); + } glUseProgram(0); // Unbind shader program #endif @@ -1682,11 +1689,12 @@ void rlglLoadMesh(Mesh *mesh) mesh->vboId[3] = 0; // Vertex colors VBO mesh->vboId[4] = 0; // Vertex tangents VBO mesh->vboId[5] = 0; // Vertex texcoords2 VBO + mesh->vboId[6] = 0; // Vertex indices VBO #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) GLuint vaoId = 0; // Vertex Array Objects (VAO) - GLuint vboId[6]; // Vertex Buffer Objects (VBOs) + GLuint vboId[7]; // Vertex Buffer Objects (VBOs) if (vaoSupported) { @@ -1775,12 +1783,21 @@ void rlglLoadMesh(Mesh *mesh) glDisableVertexAttribArray(5); } + if (mesh->indices != NULL) + { + glGenBuffers(1, &vboId[6]); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vboId[6]); + glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned short)*mesh->trianglesCount*3, mesh->indices, GL_STATIC_DRAW); + } + + mesh->vboId[0] = vboId[0]; // Vertex position VBO mesh->vboId[1] = vboId[1]; // Texcoords VBO mesh->vboId[2] = vboId[2]; // Normals VBO mesh->vboId[3] = vboId[3]; // Colors VBO mesh->vboId[4] = vboId[4]; // Tangents VBO mesh->vboId[5] = vboId[5]; // Texcoords2 VBO + mesh->vboId[6] = vboId[6]; // Indices VBO if (vaoSupported) { @@ -2733,9 +2750,9 @@ static void DrawDefaultBuffers(void) // 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 #if defined(GRAPHICS_API_OPENGL_33) - glDrawElements(GL_TRIANGLES, numIndicesToProcess, GL_UNSIGNED_INT, (GLvoid*) (sizeof(GLuint) * indicesOffset)); + glDrawElements(GL_TRIANGLES, numIndicesToProcess, GL_UNSIGNED_INT, (GLvoid *)(sizeof(GLuint)*indicesOffset)); #elif defined(GRAPHICS_API_OPENGL_ES2) - glDrawElements(GL_TRIANGLES, numIndicesToProcess, GL_UNSIGNED_SHORT, (GLvoid*) (sizeof(GLushort) * indicesOffset)); + glDrawElements(GL_TRIANGLES, numIndicesToProcess, GL_UNSIGNED_SHORT, (GLvoid *)(sizeof(GLushort)*indicesOffset)); #endif //GLenum err; //if ((err = glGetError()) != GL_NO_ERROR) TraceLog(INFO, "OpenGL error: %i", (int)err); //GL_INVALID_ENUM! -- cgit v1.2.3 From aee5d9a39026d7cff2536806c1f10e25ccc57dcc Mon Sep 17 00:00:00 2001 From: raysan5 Date: Tue, 10 May 2016 19:24:05 +0200 Subject: Code tweak --- src/models.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/models.c b/src/models.c index 41150179..066b919e 100644 --- a/src/models.c +++ b/src/models.c @@ -2019,7 +2019,7 @@ static Material LoadMTL(const char *fileName) char buffer[MAX_BUFFER_SIZE]; Vector3 color = { 1.0f, 1.0f, 1.0f }; - char *mapFileName; + char *mapFileName = NULL; FILE *mtlFile; -- cgit v1.2.3 From 5c112ff5425356a81920d907caf25a40c9ba71df Mon Sep 17 00:00:00 2001 From: raysan5 Date: Tue, 10 May 2016 19:24:25 +0200 Subject: Corrected tipo --- src/raylib.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/raylib.h b/src/raylib.h index 05463325..ecfce9fc 100644 --- a/src/raylib.h +++ b/src/raylib.h @@ -376,7 +376,7 @@ typedef struct Mesh { float *tangents; // vertex tangents (XYZ - 3 components per vertex) (shader-location = 4) unsigned char *colors; // vertex colors (RGBA - 4 components per vertex) (shader-location = 3) unsigned short *indices; // vertex indices (in case vertex data comes indexed) - int trianglesCount; // number of triangles to draw + int triangleCount; // number of triangles to draw BoundingBox bounds; // mesh limits defined by min and max points -- cgit v1.2.3 From 6acfda599e5353b1c90bb11329ce50632851b1f5 Mon Sep 17 00:00:00 2001 From: raysan5 Date: Tue, 10 May 2016 19:25:06 +0200 Subject: Support indexed mesh data on OpenGL 1.1 path Keep asking myself why I maintain this rendering path... -___- --- src/rlgl.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/rlgl.c b/src/rlgl.c index 8364c4f1..3c0d9e79 100644 --- a/src/rlgl.c +++ b/src/rlgl.c @@ -1094,7 +1094,7 @@ void rlglDrawEx(Mesh mesh, Material material, Matrix transform, bool wires) glEnableClientState(GL_VERTEX_ARRAY); // Enable vertex array glEnableClientState(GL_TEXTURE_COORD_ARRAY); // Enable texture coords array if (mesh.normals != NULL) glEnableClientState(GL_NORMAL_ARRAY); // Enable normals array - if (mesh.colors != NULL) glEnableClientState(GL_COLOR_ARRAY); // Enable colors array + if (mesh.colors != NULL) glEnableClientState(GL_COLOR_ARRAY); // Enable colors array glVertexPointer(3, GL_FLOAT, 0, mesh.vertices); // Pointer to vertex coords array glTexCoordPointer(2, GL_FLOAT, 0, mesh.texcoords); // Pointer to texture coords array @@ -1104,7 +1104,9 @@ void rlglDrawEx(Mesh mesh, Material material, Matrix transform, bool wires) rlPushMatrix(); rlMultMatrixf(MatrixToFloat(transform)); rlColor4ub(material.colDiffuse.r, material.colDiffuse.g, material.colDiffuse.b, material.colDiffuse.a); - glDrawArrays(GL_TRIANGLES, 0, mesh.vertexCount); + + if (mesh.indices != NULL) glDrawElements(GL_TRIANGLES, mesh.triangleCount*3, GL_UNSIGNED_SHORT, mesh.indices); + else glDrawArrays(GL_TRIANGLES, 0, mesh.vertexCount); rlPopMatrix(); glDisableClientState(GL_VERTEX_ARRAY); // Disable vertex array @@ -1209,7 +1211,7 @@ void rlglDrawEx(Mesh mesh, Material material, Matrix transform, bool wires) } // Draw call! - if (mesh.indices != NULL) glDrawElements(GL_TRIANGLES, mesh.trianglesCount*3, GL_UNSIGNED_SHORT, 0); // Indexed vertices draw + if (mesh.indices != NULL) glDrawElements(GL_TRIANGLES, mesh.triangleCount*3, GL_UNSIGNED_SHORT, 0); // Indexed vertices draw else glDrawArrays(GL_TRIANGLES, 0, mesh.vertexCount); if (material.texNormal.id != 0) @@ -1787,7 +1789,7 @@ void rlglLoadMesh(Mesh *mesh) { glGenBuffers(1, &vboId[6]); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vboId[6]); - glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned short)*mesh->trianglesCount*3, mesh->indices, GL_STATIC_DRAW); + glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned short)*mesh->triangleCount*3, mesh->indices, GL_STATIC_DRAW); } -- cgit v1.2.3 From 6db44500b75097a581587cba15ce703963d2ced8 Mon Sep 17 00:00:00 2001 From: Joshua Reisenauer Date: Wed, 11 May 2016 00:37:10 -0700 Subject: adding multiple music streams --- src/audio.c | 181 ++++++++++++++++++++++++++--------------------------------- src/audio.h | 9 +-- src/raylib.h | 9 +-- 3 files changed, 91 insertions(+), 108 deletions(-) (limited to 'src') diff --git a/src/audio.c b/src/audio.c index fbf53df6..39be4eeb 100644 --- a/src/audio.c +++ b/src/audio.c @@ -61,6 +61,7 @@ //---------------------------------------------------------------------------------- #define MAX_STREAM_BUFFERS 2 #define MAX_AUDIO_CONTEXTS 4 // Number of open AL sources +#define MAX_MUSIC_STREAMS 2 #if defined(PLATFORM_RPI) || defined(PLATFORM_ANDROID) // NOTE: On RPI and Android should be lower to avoid frame-stalls @@ -81,13 +82,8 @@ typedef struct Music { stb_vorbis *stream; jar_xm_context_t *chipctx; // Stores jar_xm context - - ALuint buffers[MAX_STREAM_BUFFERS]; - ALuint source; - ALenum format; - - int channels; - int sampleRate; + AudioContext_t *ctx; // audio context + int totalSamplesLeft; float totalLengthSeconds; bool loop; @@ -117,8 +113,8 @@ typedef enum { INFO = 0, ERROR, WARNING, DEBUG, OTHER } TraceLogType; //---------------------------------------------------------------------------------- static AudioContext_t* mixChannelsActive_g[MAX_AUDIO_CONTEXTS]; // What mix channels are currently active static bool musicEnabled = false; -static Music currentMusic; // Current music loaded - // NOTE: Only one music file playing at a time +static Music currentMusic[2]; // Current music loaded, up to two can play at the same time + //---------------------------------------------------------------------------------- // Module specific Functions Declaration //---------------------------------------------------------------------------------- @@ -769,151 +765,129 @@ void SetSoundPitch(Sound sound, float pitch) // Start music playing (open stream) void PlayMusicStream(char *fileName) { + int musicIndex; + int mixIndex; + for(musicIndex = 0; musicIndex < MAX_MUSIC_STREAMS; musicIndex++) // find empty music slot + { + if(currentMusic[musicIndex].stream == NULL && !currentMusic[musicIndex].chipTune) break; + else if(musicIndex = MAX_MUSIC_STREAMS - 1) return; + } + for(mixIndex = 0; mixIndex < MAX_AUDIO_CONTEXTS; mixIndex++) // find empty mix channel slot + { + if(mixChannelsActive_g[mixIndex] == NULL) break; + else if(musicIndex = MAX_AUDIO_CONTEXTS - 1) return; + } + + if (strcmp(GetExtension(fileName),"ogg") == 0) { - // Stop current music, clean buffers, unload current stream - StopMusicStream(); - // Open audio stream - currentMusic.stream = stb_vorbis_open_filename(fileName, NULL, NULL); + currentMusic[musicIndex].stream = stb_vorbis_open_filename(fileName, NULL, NULL); - if (currentMusic.stream == NULL) + if (currentMusic[musicIndex].stream == NULL) { TraceLog(WARNING, "[%s] OGG audio file could not be opened", fileName); } else { // Get file info - stb_vorbis_info info = stb_vorbis_get_info(currentMusic.stream); - - currentMusic.channels = info.channels; - currentMusic.sampleRate = info.sample_rate; + stb_vorbis_info info = stb_vorbis_get_info(currentMusic[musicIndex].stream); TraceLog(INFO, "[%s] Ogg sample rate: %i", fileName, info.sample_rate); TraceLog(INFO, "[%s] Ogg channels: %i", fileName, info.channels); TraceLog(DEBUG, "[%s] Temp memory required: %i", fileName, info.temp_memory_required); - if (info.channels == 2) currentMusic.format = AL_FORMAT_STEREO16; - else currentMusic.format = AL_FORMAT_MONO16; + if (info.channels == 2){ + currentMusic[musicIndex].ctx = InitAudioContext(info.sample_rate, mixIndex, 2, false); + } + else{ + currentMusic[musicIndex].ctx = InitAudioContext(info.sample_rate, mixIndex, 1, false); + } - currentMusic.loop = true; // We loop by default + currentMusic[musicIndex].loop = true; // We loop by default musicEnabled = true; - // Create an audio source - alGenSources(1, ¤tMusic.source); // Generate pointer to audio source - - alSourcef(currentMusic.source, AL_PITCH, 1); - alSourcef(currentMusic.source, AL_GAIN, 1); - alSource3f(currentMusic.source, AL_POSITION, 0, 0, 0); - alSource3f(currentMusic.source, AL_VELOCITY, 0, 0, 0); - //alSourcei(currentMusic.source, AL_LOOPING, AL_TRUE); // ERROR: Buffers do not queue! - - // Generate two OpenAL buffers - alGenBuffers(2, currentMusic.buffers); - - // Fill buffers with music... - BufferMusicStream(currentMusic.buffers[0]); - BufferMusicStream(currentMusic.buffers[1]); - - // Queue buffers and start playing - alSourceQueueBuffers(currentMusic.source, 2, currentMusic.buffers); - alSourcePlay(currentMusic.source); - - // NOTE: Regularly, we must check if a buffer has been processed and refill it: UpdateMusicStream() - - currentMusic.totalSamplesLeft = stb_vorbis_stream_length_in_samples(currentMusic.stream) * currentMusic.channels; - currentMusic.totalLengthSeconds = stb_vorbis_stream_length_in_seconds(currentMusic.stream); + currentMusic[musicIndex].totalSamplesLeft = stb_vorbis_stream_length_in_samples(currentMusic[musicIndex].stream) * currentMusic[musicIndex].channels; + currentMusic[musicIndex].totalLengthSeconds = stb_vorbis_stream_length_in_seconds(currentMusic[musicIndex].stream); } } else if (strcmp(GetExtension(fileName),"xm") == 0) { - // Stop current music, clean buffers, unload current stream - StopMusicStream(); - - // new song settings for xm chiptune - currentMusic.chipTune = true; - currentMusic.channels = 2; - currentMusic.sampleRate = 48000; - currentMusic.loop = true; - // only stereo is supported for xm - if(!jar_xm_create_context_from_file(¤tMusic.chipctx, currentMusic.sampleRate, fileName)) + if(!jar_xm_create_context_from_file(¤tMusic[musicIndex].chipctx, 48000, fileName)) { - currentMusic.format = AL_FORMAT_STEREO16; - jar_xm_set_max_loop_count(currentMusic.chipctx, 0); // infinite number of loops - currentMusic.totalSamplesLeft = jar_xm_get_remaining_samples(currentMusic.chipctx); - currentMusic.totalLengthSeconds = ((float)currentMusic.totalSamplesLeft) / ((float)currentMusic.sampleRate); + currentMusic[musicIndex].chipTune = true; + currentMusic[musicIndex].loop = true; + jar_xm_set_max_loop_count(currentMusic[musicIndex].chipctx, 0); // infinite number of loops + currentMusic[musicIndex].totalSamplesLeft = jar_xm_get_remaining_samples(currentMusic[musicIndex].chipctx); + currentMusic[musicIndex].totalLengthSeconds = ((float)currentMusic[musicIndex].totalSamplesLeft) / ((float)currentMusic[musicIndex].sampleRate); musicEnabled = true; - TraceLog(INFO, "[%s] XM number of samples: %i", fileName, currentMusic.totalSamplesLeft); - TraceLog(INFO, "[%s] XM track length: %11.6f sec", fileName, currentMusic.totalLengthSeconds); - - // Set up OpenAL - alGenSources(1, ¤tMusic.source); - alSourcef(currentMusic.source, AL_PITCH, 1); - alSourcef(currentMusic.source, AL_GAIN, 1); - alSource3f(currentMusic.source, AL_POSITION, 0, 0, 0); - alSource3f(currentMusic.source, AL_VELOCITY, 0, 0, 0); - alGenBuffers(2, currentMusic.buffers); - BufferMusicStream(currentMusic.buffers[0]); - BufferMusicStream(currentMusic.buffers[1]); - alSourceQueueBuffers(currentMusic.source, 2, currentMusic.buffers); - alSourcePlay(currentMusic.source); + TraceLog(INFO, "[%s] XM number of samples: %i", fileName, currentMusic[musicIndex].totalSamplesLeft); + TraceLog(INFO, "[%s] XM track length: %11.6f sec", fileName, currentMusic[musicIndex].totalLengthSeconds); - // NOTE: Regularly, we must check if a buffer has been processed and refill it: UpdateMusicStream() + currentMusic[musicIndex].ctx = InitAudioContext(48000, mixIndex, 2, true); } else TraceLog(WARNING, "[%s] XM file could not be opened", fileName); } else TraceLog(WARNING, "[%s] Music extension not recognized, it can't be loaded", fileName); } -// Stop music playing (close stream) -void StopMusicStream(void) +// Stop music playing for individual music index of currentMusic array (close stream) +void StopMusicStream(int index) { - if (musicEnabled) + if (index < MAX_MUSIC_STREAMS && currentMusic[index].ctx) { - alSourceStop(currentMusic.source); - EmptyMusicStream(); // Empty music buffers - alDeleteSources(1, ¤tMusic.source); - alDeleteBuffers(2, currentMusic.buffers); + CloseAudioContext(currentMusic[index].ctx); - if (currentMusic.chipTune) + if (currentMusic[index].chipTune) { - jar_xm_free_context(currentMusic.chipctx); + jar_xm_free_context(currentMusic[index].chipctx); } else { - stb_vorbis_close(currentMusic.stream); + stb_vorbis_close(currentMusic[index].stream); } + + if(!getMusicStreamCount()) musicEnabled = false; } +} - musicEnabled = false; +//get number of music channels active at this time, this does not mean they are playing +int getMusicStreamCount(void) +{ + int musicCount; + for(int musicIndex = 0; musicIndex < MAX_MUSIC_STREAMS; musicIndex++) // find empty music slot + if(currentMusic[musicIndex].stream != NULL || currentMusic[musicIndex].chipTune) musicCount++; + + return musicCount; } // Pause music playing -void PauseMusicStream(void) +void PauseMusicStream(int index) { // Pause music stream if music available! if (musicEnabled) { TraceLog(INFO, "Pausing music stream"); - alSourcePause(currentMusic.source); + UpdateAudioContext(currentMusic[index].ctx, NULL, 0); // pushing null data auto pauses stream musicEnabled = false; } } // Resume music playing -void ResumeMusicStream(void) +void ResumeMusicStream(int index) { // Resume music playing... if music available! ALenum state; - alGetSourcei(currentMusic.source, AL_SOURCE_STATE, &state); - - if (state == AL_PAUSED) - { - TraceLog(INFO, "Resuming music stream"); - alSourcePlay(currentMusic.source); - musicEnabled = true; + if(currentMusic[musicIndex].ctx){ + alGetSourcei(currentMusic[musicIndex].ctx->alSource, AL_SOURCE_STATE, &state); + if (state == AL_PAUSED) + { + TraceLog(INFO, "Resuming music stream"); + alSourcePlay(currentMusic[musicIndex].ctx->alSource); + musicEnabled = true; + } } } @@ -922,17 +896,24 @@ bool IsMusicPlaying(void) { bool playing = false; ALint state; - - alGetSourcei(currentMusic.source, AL_SOURCE_STATE, &state); - if (state == AL_PLAYING) playing = true; + + for(int musicIndex = 0; musicIndex < MAX_MUSIC_STREAMS; musicIndex++) + { + if(currentMusic[musicIndex].ctx){ + alGetSourcei(currentMusic[musicIndex].ctx->alSource, AL_SOURCE_STATE, &state); + if (state == AL_PLAYING) playing = true; + } + } return playing; } // Set volume for music -void SetMusicVolume(float volume) +void SetMusicVolume(int index, float volume) { - alSourcef(currentMusic.source, AL_GAIN, volume); + if(currentMusic[musicIndex].ctx){ + alSourcef(currentMusic[musicIndex].ctx->alSource, AL_GAIN, volume); + } } // Get current music time length (in seconds) diff --git a/src/audio.h b/src/audio.h index afd881b7..a1602bd9 100644 --- a/src/audio.h +++ b/src/audio.h @@ -102,13 +102,14 @@ void SetSoundPitch(Sound sound, float pitch); // Set pitch for void PlayMusicStream(char *fileName); // Start music playing (open stream) void UpdateMusicStream(void); // Updates buffers for music streaming -void StopMusicStream(void); // Stop music playing (close stream) -void PauseMusicStream(void); // Pause music playing -void ResumeMusicStream(void); // Resume playing paused music +void StopMusicStream(int index); // Stop music playing (close stream) +void PauseMusicStream(int index); // Pause music playing +void ResumeMusicStream(int index); // Resume playing paused music bool IsMusicPlaying(void); // Check if music is playing -void SetMusicVolume(float volume); // Set volume for music (1.0 is max level) +void SetMusicVolume(int index, float volume); // Set volume for music (1.0 is max level) float GetMusicTimeLength(void); // Get music time length (in seconds) float GetMusicTimePlayed(void); // Get current music time played (in seconds) +int getMusicStreamCount(void); #ifdef __cplusplus } diff --git a/src/raylib.h b/src/raylib.h index d464c8e9..0c49a085 100644 --- a/src/raylib.h +++ b/src/raylib.h @@ -893,13 +893,14 @@ void SetSoundPitch(Sound sound, float pitch); // Set pitch for void PlayMusicStream(char *fileName); // Start music playing (open stream) void UpdateMusicStream(void); // Updates buffers for music streaming -void StopMusicStream(void); // Stop music playing (close stream) -void PauseMusicStream(void); // Pause music playing -void ResumeMusicStream(void); // Resume playing paused music +void StopMusicStream(int index); // Stop music playing (close stream) +void PauseMusicStream(int index); // Pause music playing +void ResumeMusicStream(int index); // Resume playing paused music bool IsMusicPlaying(void); // Check if music is playing -void SetMusicVolume(float volume); // Set volume for music (1.0 is max level) +void SetMusicVolume(int index, float volume); // Set volume for music (1.0 is max level) float GetMusicTimeLength(void); // Get current music time length (in seconds) float GetMusicTimePlayed(void); // Get current music time played (in seconds) +int getMusicStreamCount(void); #ifdef __cplusplus } -- cgit v1.2.3 From 4d78d27bd956f7f7592a9efc453b954436d57297 Mon Sep 17 00:00:00 2001 From: raysan5 Date: Wed, 11 May 2016 19:25:51 +0200 Subject: Updated structs Mesh and Shader --- src/rlgl.h | 51 ++++++++++++++++++++++++--------------------------- 1 file changed, 24 insertions(+), 27 deletions(-) (limited to 'src') diff --git a/src/rlgl.h b/src/rlgl.h index afc2ab96..6e694dae 100644 --- a/src/rlgl.h +++ b/src/rlgl.h @@ -137,37 +137,41 @@ typedef enum { OPENGL_11 = 1, OPENGL_33, OPENGL_ES_20 } GlVersion; Vector3 max; } BoundingBox; - // Mesh with vertex data type - // NOTE: If using OpenGL 1.1, data loaded in CPU; if OpenGL 3.3+ data loaded in GPU (vaoId) + // Vertex data definning a mesh typedef struct Mesh { - int vertexCount; // num vertices - float *vertices; // vertex position (XYZ - 3 components per vertex) - float *texcoords; // vertex texture coordinates (UV - 2 components per vertex) - float *texcoords2; // vertex second texture coordinates (useful for lightmaps) - float *normals; // vertex normals (XYZ - 3 components per vertex) - float *tangents; // vertex tangents (XYZ - 3 components per vertex) - unsigned char *colors; // vertex colors (RGBA - 4 components per vertex) + int vertexCount; // number of vertices stored in arrays + float *vertices; // vertex position (XYZ - 3 components per vertex) (shader-location = 0) + float *texcoords; // vertex texture coordinates (UV - 2 components per vertex) (shader-location = 1) + float *texcoords2; // vertex second texture coordinates (useful for lightmaps) (shader-location = 5) + float *normals; // vertex normals (XYZ - 3 components per vertex) (shader-location = 2) + float *tangents; // vertex tangents (XYZ - 3 components per vertex) (shader-location = 4) + unsigned char *colors; // vertex colors (RGBA - 4 components per vertex) (shader-location = 3) + unsigned short *indices; // vertex indices (in case vertex data comes indexed) + int triangleCount; // number of triangles to draw BoundingBox bounds; // mesh limits defined by min and max points unsigned int vaoId; // OpenGL Vertex Array Object id - unsigned int vboId[6]; // OpenGL Vertex Buffer Objects id (6 types of vertex data) + unsigned int vboId[7]; // OpenGL Vertex Buffer Objects id (7 types of vertex data) } Mesh; - // Shader type + // Shader type (generic shader) typedef struct Shader { - unsigned int id; // Shader program id - - // Variable attributes - int vertexLoc; // Vertex attribute location point (vertex shader) - int texcoordLoc; // Texcoord attribute location point (vertex shader) - int normalLoc; // Normal attribute location point (vertex shader) - int colorLoc; // Color attibute location point (vertex shader) - - // Uniforms + unsigned int id; // Shader program id + + // Vertex attributes locations (default locations) + int vertexLoc; // Vertex attribute location point (default-location = 0) + int texcoordLoc; // Texcoord attribute location point (default-location = 1) + int normalLoc; // Normal attribute location point (default-location = 2) + int colorLoc; // Color attibute location point (default-location = 3) + int tangentLoc; // Tangent attribute location point (default-location = 4) + int texcoord2Loc; // Texcoord2 attribute location point (default-location = 5) + + // Uniform locations int mvpLoc; // ModelView-Projection matrix uniform location point (vertex shader) int tintColorLoc; // Color uniform location point (fragment shader) + // Texture map locations int mapDiffuseLoc; // Diffuse map texture uniform location point (fragment shader) int mapNormalLoc; // Normal map texture uniform location point (fragment shader) int mapSpecularLoc; // Specular map texture uniform location point (fragment shader) @@ -205,13 +209,6 @@ typedef enum { OPENGL_11 = 1, OPENGL_33, OPENGL_ES_20 } GlVersion; float glossiness; float normalDepth; } Material; - - // 3d Model type - typedef struct Model { - Mesh mesh; - Matrix transform; - Material material; - } Model; // Color blending modes (pre-defined) typedef enum { BLEND_ALPHA = 0, BLEND_ADDITIVE, BLEND_MULTIPLIED } BlendMode; -- cgit v1.2.3 From ad3d270c429844462f3fd62fa12257760fac24b5 Mon Sep 17 00:00:00 2001 From: Joshua Reisenauer Date: Wed, 11 May 2016 18:14:59 -0700 Subject: added set pitch for music streams --- src/audio.c | 40 ++++++++++++++++++++++------------------ src/audio.h | 3 ++- src/raylib.h | 3 ++- 3 files changed, 26 insertions(+), 20 deletions(-) (limited to 'src') diff --git a/src/audio.c b/src/audio.c index 39be4eeb..f89be8bf 100644 --- a/src/audio.c +++ b/src/audio.c @@ -113,7 +113,7 @@ typedef enum { INFO = 0, ERROR, WARNING, DEBUG, OTHER } TraceLogType; //---------------------------------------------------------------------------------- static AudioContext_t* mixChannelsActive_g[MAX_AUDIO_CONTEXTS]; // What mix channels are currently active static bool musicEnabled = false; -static Music currentMusic[2]; // Current music loaded, up to two can play at the same time +static Music currentMusic[MAX_MUSIC_STREAMS]; // Current music loaded, up to two can play at the same time //---------------------------------------------------------------------------------- // Module specific Functions Declaration @@ -797,18 +797,18 @@ void PlayMusicStream(char *fileName) TraceLog(INFO, "[%s] Ogg channels: %i", fileName, info.channels); TraceLog(DEBUG, "[%s] Temp memory required: %i", fileName, info.temp_memory_required); + currentMusic[musicIndex].loop = true; // We loop by default + musicEnabled = true; + + currentMusic[musicIndex].totalSamplesLeft = stb_vorbis_stream_length_in_samples(currentMusic[musicIndex].stream) * info.channels; + currentMusic[musicIndex].totalLengthSeconds = stb_vorbis_stream_length_in_seconds(currentMusic[musicIndex].stream); + if (info.channels == 2){ currentMusic[musicIndex].ctx = InitAudioContext(info.sample_rate, mixIndex, 2, false); } else{ currentMusic[musicIndex].ctx = InitAudioContext(info.sample_rate, mixIndex, 1, false); } - - currentMusic[musicIndex].loop = true; // We loop by default - musicEnabled = true; - - currentMusic[musicIndex].totalSamplesLeft = stb_vorbis_stream_length_in_samples(currentMusic[musicIndex].stream) * currentMusic[musicIndex].channels; - currentMusic[musicIndex].totalLengthSeconds = stb_vorbis_stream_length_in_seconds(currentMusic[musicIndex].stream); } } else if (strcmp(GetExtension(fileName),"xm") == 0) @@ -820,7 +820,7 @@ void PlayMusicStream(char *fileName) currentMusic[musicIndex].loop = true; jar_xm_set_max_loop_count(currentMusic[musicIndex].chipctx, 0); // infinite number of loops currentMusic[musicIndex].totalSamplesLeft = jar_xm_get_remaining_samples(currentMusic[musicIndex].chipctx); - currentMusic[musicIndex].totalLengthSeconds = ((float)currentMusic[musicIndex].totalSamplesLeft) / ((float)currentMusic[musicIndex].sampleRate); + currentMusic[musicIndex].totalLengthSeconds = ((float)currentMusic[musicIndex].totalSamplesLeft) / 48000.f; musicEnabled = true; TraceLog(INFO, "[%s] XM number of samples: %i", fileName, currentMusic[musicIndex].totalSamplesLeft); @@ -891,18 +891,15 @@ void ResumeMusicStream(int index) } } -// Check if music is playing -bool IsMusicPlaying(void) +// Check if any music is playing +bool IsMusicPlaying(int index) { bool playing = false; ALint state; - for(int musicIndex = 0; musicIndex < MAX_MUSIC_STREAMS; musicIndex++) - { - if(currentMusic[musicIndex].ctx){ - alGetSourcei(currentMusic[musicIndex].ctx->alSource, AL_SOURCE_STATE, &state); - if (state == AL_PLAYING) playing = true; - } + if(index < MAX_MUSIC_STREAMS && currentMusic[index].ctx){ + alGetSourcei(currentMusic[index].ctx->alSource, AL_SOURCE_STATE, &state); + if (state == AL_PLAYING) playing = true; } return playing; @@ -911,8 +908,15 @@ bool IsMusicPlaying(void) // Set volume for music void SetMusicVolume(int index, float volume) { - if(currentMusic[musicIndex].ctx){ - alSourcef(currentMusic[musicIndex].ctx->alSource, AL_GAIN, volume); + if(index < MAX_MUSIC_STREAMS && currentMusic[index].ctx){ + alSourcef(currentMusic[index].ctx->alSource, AL_GAIN, volume); + } +} + +void SetMusicPitch(int index, float pitch) +{ + if(index < MAX_MUSIC_STREAMS && currentMusic[index].ctx){ + alSourcef(currentMusic[index].ctx->alSource, AL_PITCH, pitch); } } diff --git a/src/audio.h b/src/audio.h index a1602bd9..fef85b4f 100644 --- a/src/audio.h +++ b/src/audio.h @@ -105,11 +105,12 @@ void UpdateMusicStream(void); // Updates buffe void StopMusicStream(int index); // Stop music playing (close stream) void PauseMusicStream(int index); // Pause music playing void ResumeMusicStream(int index); // Resume playing paused music -bool IsMusicPlaying(void); // Check if music is playing +bool IsMusicPlaying(int index); // Check if music is playing void SetMusicVolume(int index, float volume); // Set volume for music (1.0 is max level) float GetMusicTimeLength(void); // Get music time length (in seconds) float GetMusicTimePlayed(void); // Get current music time played (in seconds) int getMusicStreamCount(void); +void SetMusicPitch(int index, float pitch); #ifdef __cplusplus } diff --git a/src/raylib.h b/src/raylib.h index 0c49a085..b9390d31 100644 --- a/src/raylib.h +++ b/src/raylib.h @@ -896,11 +896,12 @@ void UpdateMusicStream(void); // Updates buffe void StopMusicStream(int index); // Stop music playing (close stream) void PauseMusicStream(int index); // Pause music playing void ResumeMusicStream(int index); // Resume playing paused music -bool IsMusicPlaying(void); // Check if music is playing +bool IsMusicPlaying(int index); // Check if music is playing void SetMusicVolume(int index, float volume); // Set volume for music (1.0 is max level) float GetMusicTimeLength(void); // Get current music time length (in seconds) float GetMusicTimePlayed(void); // Get current music time played (in seconds) int getMusicStreamCount(void); +void SetMusicPitch(int index, float pitch); #ifdef __cplusplus } -- cgit v1.2.3 From 9737c58054d5d0cc636fca0c998b31a69d501970 Mon Sep 17 00:00:00 2001 From: Joshua Reisenauer Date: Wed, 11 May 2016 20:15:37 -0700 Subject: PlayMusicStream now uses index --- src/audio.c | 29 +++++++++++++++++++---------- src/audio.h | 2 +- src/raylib.h | 2 +- 3 files changed, 21 insertions(+), 12 deletions(-) (limited to 'src') diff --git a/src/audio.c b/src/audio.c index f89be8bf..6a8d251e 100644 --- a/src/audio.c +++ b/src/audio.c @@ -763,19 +763,18 @@ void SetSoundPitch(Sound sound, float pitch) //---------------------------------------------------------------------------------- // Start music playing (open stream) -void PlayMusicStream(char *fileName) +// returns 0 on success +int PlayMusicStream(int musicIndex, char *fileName) { - int musicIndex; + int musicIndex = index; int mixIndex; - for(musicIndex = 0; musicIndex < MAX_MUSIC_STREAMS; musicIndex++) // find empty music slot - { - if(currentMusic[musicIndex].stream == NULL && !currentMusic[musicIndex].chipTune) break; - else if(musicIndex = MAX_MUSIC_STREAMS - 1) return; - } + + if(currentMusic[musicIndex].stream != NULL || currentMusic[musicIndex].chipTune) return 1; // error + for(mixIndex = 0; mixIndex < MAX_AUDIO_CONTEXTS; mixIndex++) // find empty mix channel slot { if(mixChannelsActive_g[mixIndex] == NULL) break; - else if(musicIndex = MAX_AUDIO_CONTEXTS - 1) return; + else if(musicIndex = MAX_AUDIO_CONTEXTS - 1) return 2; // error } @@ -787,6 +786,7 @@ void PlayMusicStream(char *fileName) if (currentMusic[musicIndex].stream == NULL) { TraceLog(WARNING, "[%s] OGG audio file could not be opened", fileName); + return 3; // error } else { @@ -828,9 +828,18 @@ void PlayMusicStream(char *fileName) currentMusic[musicIndex].ctx = InitAudioContext(48000, mixIndex, 2, true); } - else TraceLog(WARNING, "[%s] XM file could not be opened", fileName); + else + { + TraceLog(WARNING, "[%s] XM file could not be opened", fileName); + return 4; // error + } + } + else + { + TraceLog(WARNING, "[%s] Music extension not recognized, it can't be loaded", fileName); + return 5; // error } - else TraceLog(WARNING, "[%s] Music extension not recognized, it can't be loaded", fileName); + return 0; // normal return } // Stop music playing for individual music index of currentMusic array (close stream) diff --git a/src/audio.h b/src/audio.h index fef85b4f..d09c4acc 100644 --- a/src/audio.h +++ b/src/audio.h @@ -100,7 +100,7 @@ bool IsSoundPlaying(Sound sound); // Check if a so void SetSoundVolume(Sound sound, float volume); // Set volume for a sound (1.0 is max level) void SetSoundPitch(Sound sound, float pitch); // Set pitch for a sound (1.0 is base level) -void PlayMusicStream(char *fileName); // Start music playing (open stream) +int PlayMusicStream(int musicIndex, char *fileName); // Start music playing (open stream) void UpdateMusicStream(void); // Updates buffers for music streaming void StopMusicStream(int index); // Stop music playing (close stream) void PauseMusicStream(int index); // Pause music playing diff --git a/src/raylib.h b/src/raylib.h index a6507906..cb17aa78 100644 --- a/src/raylib.h +++ b/src/raylib.h @@ -894,7 +894,7 @@ bool IsSoundPlaying(Sound sound); // Check if a so void SetSoundVolume(Sound sound, float volume); // Set volume for a sound (1.0 is max level) void SetSoundPitch(Sound sound, float pitch); // Set pitch for a sound (1.0 is base level) -void PlayMusicStream(char *fileName); // Start music playing (open stream) +int PlayMusicStream(int musicIndex, char *fileName); // Start music playing (open stream) void UpdateMusicStream(void); // Updates buffers for music streaming void StopMusicStream(int index); // Stop music playing (close stream) void PauseMusicStream(int index); // Pause music playing -- cgit v1.2.3 From f0ada8c40d7887654d05c62f980a0a5473c1d8a7 Mon Sep 17 00:00:00 2001 From: Joshua Reisenauer Date: Wed, 11 May 2016 22:37:53 -0700 Subject: apply index to remaining functions --- src/audio.c | 108 +++++++++++++++++++++++++++++++---------------------------- src/audio.h | 6 ++-- src/raylib.h | 6 ++-- 3 files changed, 62 insertions(+), 58 deletions(-) (limited to 'src') diff --git a/src/audio.c b/src/audio.c index 6a8d251e..94729566 100644 --- a/src/audio.c +++ b/src/audio.c @@ -118,12 +118,12 @@ static Music currentMusic[MAX_MUSIC_STREAMS]; // Current //---------------------------------------------------------------------------------- // Module specific Functions Declaration //---------------------------------------------------------------------------------- -static Wave LoadWAV(const char *fileName); // Load WAV file -static Wave LoadOGG(char *fileName); // Load OGG file -static void UnloadWave(Wave wave); // Unload wave data +static Wave LoadWAV(const char *fileName); // Load WAV file +static Wave LoadOGG(char *fileName); // Load OGG file +static void UnloadWave(Wave wave); // Unload wave data -static bool BufferMusicStream(ALuint buffer); // Fill music buffers with data -static void EmptyMusicStream(void); // Empty music buffers +static bool BufferMusicStream(int index, ALuint buffer); // Fill music buffers with data +static void EmptyMusicStream(int index); // Empty music buffers static unsigned short FillAlBufferWithSilence(AudioContext_t *context, ALuint buffer);// fill buffer with zeros, returns number processed static void ResampleShortToFloat(short *shorts, float *floats, unsigned short len); // pass two arrays of the same legnth in @@ -766,7 +766,6 @@ void SetSoundPitch(Sound sound, float pitch) // returns 0 on success int PlayMusicStream(int musicIndex, char *fileName) { - int musicIndex = index; int mixIndex; if(currentMusic[musicIndex].stream != NULL || currentMusic[musicIndex].chipTune) return 1; // error @@ -930,36 +929,36 @@ void SetMusicPitch(int index, float pitch) } // Get current music time length (in seconds) -float GetMusicTimeLength(void) +float GetMusicTimeLength(int index) { float totalSeconds; - if (currentMusic.chipTune) + if (currentMusic[index].chipTune) { - totalSeconds = currentMusic.totalLengthSeconds; + totalSeconds = currentMusic[index].totalLengthSeconds; } else { - totalSeconds = stb_vorbis_stream_length_in_seconds(currentMusic.stream); + totalSeconds = stb_vorbis_stream_length_in_seconds(currentMusic[index].stream); } return totalSeconds; } // Get current music time played (in seconds) -float GetMusicTimePlayed(void) +float GetMusicTimePlayed(int index) { float secondsPlayed; - if (currentMusic.chipTune) + if (currentMusic[index].chipTune) { uint64_t samples; - jar_xm_get_position(currentMusic.chipctx, NULL, NULL, NULL, &samples); - secondsPlayed = (float)samples / (currentMusic.sampleRate * currentMusic.channels); // Not sure if this is the correct value + jar_xm_get_position(currentMusic[index].chipctx, NULL, NULL, NULL, &samples); + secondsPlayed = (float)samples / (48000 * currentMusic[index].ctx->channels); // Not sure if this is the correct value } else { - int totalSamples = stb_vorbis_stream_length_in_samples(currentMusic.stream) * currentMusic.channels; - int samplesPlayed = totalSamples - currentMusic.totalSamplesLeft; - secondsPlayed = (float)samplesPlayed / (currentMusic.sampleRate * currentMusic.channels); + int totalSamples = stb_vorbis_stream_length_in_samples(currentMusic[index].stream) * currentMusic[index].ctx->channels; + int samplesPlayed = totalSamples - currentMusic[index].totalSamplesLeft; + secondsPlayed = (float)samplesPlayed / (currentMusic[index].ctx->sampleRate * currentMusic[index].ctx->channels); } @@ -971,9 +970,10 @@ float GetMusicTimePlayed(void) //---------------------------------------------------------------------------------- // Fill music buffers with new data from music stream -static bool BufferMusicStream(ALuint buffer) +static bool BufferMusicStream(int index, ALuint buffer) { short pcm[MUSIC_BUFFER_SIZE_SHORT]; + float pcmf[MUSIC_BUFFER_SIZE_FLOAT]; int size = 0; // Total size of data steamed (in bytes) int streamedBytes = 0; // samples of data obtained, channels are not included in calculation @@ -981,93 +981,97 @@ static bool BufferMusicStream(ALuint buffer) if (musicEnabled) { - if (currentMusic.chipTune) // There is no end of stream for xmfiles, once the end is reached zeros are generated for non looped chiptunes. + if (currentMusic[index].chipTune) // There is no end of stream for xmfiles, once the end is reached zeros are generated for non looped chiptunes. { - int readlen = MUSIC_BUFFER_SIZE_SHORT / 2; - jar_xm_generate_samples_16bit(currentMusic.chipctx, pcm, readlen); // reads 2*readlen shorts and moves them to buffer+size memory location - size += readlen * currentMusic.channels; // Not sure if this is what it needs + int readlen = MUSIC_BUFFER_SIZE_FLOAT / 2; + jar_xm_generate_samples(currentMusic[index].chipctx, pcmf, readlen); // reads 2*readlen shorts and moves them to buffer+size memory location + size += readlen * currentMusic[index].ctx->channels; // Not sure if this is what it needs + + alBufferData(buffer, currentMusic[index].ctx->alFormat, pcmf, size*sizeof(float), 48000); + currentMusic[index].totalSamplesLeft -= size; + if(currentMusic[index].totalSamplesLeft <= 0) active = false; } else { while (size < MUSIC_BUFFER_SIZE_SHORT) { - streamedBytes = stb_vorbis_get_samples_short_interleaved(currentMusic.stream, currentMusic.channels, pcm + size, MUSIC_BUFFER_SIZE_SHORT - size); - if (streamedBytes > 0) size += (streamedBytes*currentMusic.channels); + streamedBytes = stb_vorbis_get_samples_short_interleaved(currentMusic[index].stream, currentMusic[index].ctx->channels, pcm + size, MUSIC_BUFFER_SIZE_SHORT - size); + if (streamedBytes > 0) size += (streamedBytes*currentMusic[index].ctx->channels); else break; } + + if (size > 0) + { + alBufferData(buffer, currentMusic[index].ctx->alFormat, pcm, size*sizeof(short), currentMusic[index].ctx->sampleRate); + currentMusic[index].totalSamplesLeft -= size; + + if(currentMusic[index].totalSamplesLeft <= 0) active = false; // end if no more samples left + } + else + { + active = false; + TraceLog(WARNING, "No more data obtained from stream"); + } } TraceLog(DEBUG, "Streaming music data to buffer. Bytes streamed: %i", size); } - if (size > 0) - { - alBufferData(buffer, currentMusic.format, pcm, size*sizeof(short), currentMusic.sampleRate); - currentMusic.totalSamplesLeft -= size; - - if(currentMusic.totalSamplesLeft <= 0) active = false; // end if no more samples left - } - else - { - active = false; - TraceLog(WARNING, "No more data obtained from stream"); - } - return active; } // Empty music buffers -static void EmptyMusicStream(void) +static void EmptyMusicStream(int index) { ALuint buffer = 0; int queued = 0; - alGetSourcei(currentMusic.source, AL_BUFFERS_QUEUED, &queued); + alGetSourcei(currentMusic[index].source, AL_BUFFERS_QUEUED, &queued); while (queued > 0) { - alSourceUnqueueBuffers(currentMusic.source, 1, &buffer); + alSourceUnqueueBuffers(currentMusic[index].source, 1, &buffer); queued--; } } // Update (re-fill) music buffers if data already processed -void UpdateMusicStream(void) +void UpdateMusicStream(int index) { ALuint buffer = 0; ALint processed = 0; bool active = true; - if (musicEnabled) + if (index < MAX_MUSIC_STREAMS && musicEnabled) { // Get the number of already processed buffers (if any) - alGetSourcei(currentMusic.source, AL_BUFFERS_PROCESSED, &processed); + alGetSourcei(currentMusic[index].source, AL_BUFFERS_PROCESSED, &processed); while (processed > 0) { // Recover processed buffer for refill - alSourceUnqueueBuffers(currentMusic.source, 1, &buffer); + alSourceUnqueueBuffers(currentMusic[index].source, 1, &buffer); // Refill buffer active = BufferMusicStream(buffer); // If no more data to stream, restart music (if loop) - if ((!active) && (currentMusic.loop)) + if ((!active) && (currentMusic[index].loop)) { - if(currentMusic.chipTune) + if(currentMusic[index].chipTune) { - currentMusic.totalSamplesLeft = currentMusic.totalLengthSeconds * currentMusic.sampleRate; + currentMusic[index].totalSamplesLeft = currentMusic[index].totalLengthSeconds * currentMusic[index].ctx->sampleRate; } else { - stb_vorbis_seek_start(currentMusic.stream); - currentMusic.totalSamplesLeft = stb_vorbis_stream_length_in_samples(currentMusic.stream)*currentMusic.channels; + stb_vorbis_seek_start(currentMusic[index].stream); + currentMusic[index].totalSamplesLeft = stb_vorbis_stream_length_in_samples(currentMusic[index].stream)*currentMusic[index].ctx->channels; } active = BufferMusicStream(buffer); } // Add refilled buffer to queue again... don't let the music stop! - alSourceQueueBuffers(currentMusic.source, 1, &buffer); + alSourceQueueBuffers(currentMusic[index].source, 1, &buffer); if (alGetError() != AL_NO_ERROR) TraceLog(WARNING, "Error buffering data..."); @@ -1075,9 +1079,9 @@ void UpdateMusicStream(void) } ALenum state; - alGetSourcei(currentMusic.source, AL_SOURCE_STATE, &state); + alGetSourcei(currentMusic[index].source, AL_SOURCE_STATE, &state); - if ((state != AL_PLAYING) && active) alSourcePlay(currentMusic.source); + if ((state != AL_PLAYING) && active) alSourcePlay(currentMusic[index].source); if (!active) StopMusicStream(); } diff --git a/src/audio.h b/src/audio.h index d09c4acc..63c1c136 100644 --- a/src/audio.h +++ b/src/audio.h @@ -101,14 +101,14 @@ void SetSoundVolume(Sound sound, float volume); // Set volume fo void SetSoundPitch(Sound sound, float pitch); // Set pitch for a sound (1.0 is base level) int PlayMusicStream(int musicIndex, char *fileName); // Start music playing (open stream) -void UpdateMusicStream(void); // Updates buffers for music streaming +void UpdateMusicStream(int index); // Updates buffers for music streaming void StopMusicStream(int index); // Stop music playing (close stream) void PauseMusicStream(int index); // Pause music playing void ResumeMusicStream(int index); // Resume playing paused music bool IsMusicPlaying(int index); // Check if music is playing void SetMusicVolume(int index, float volume); // Set volume for music (1.0 is max level) -float GetMusicTimeLength(void); // Get music time length (in seconds) -float GetMusicTimePlayed(void); // Get current music time played (in seconds) +float GetMusicTimeLength(int index); // Get music time length (in seconds) +float GetMusicTimePlayed(int index); // Get current music time played (in seconds) int getMusicStreamCount(void); void SetMusicPitch(int index, float pitch); diff --git a/src/raylib.h b/src/raylib.h index cb17aa78..05c945f7 100644 --- a/src/raylib.h +++ b/src/raylib.h @@ -895,14 +895,14 @@ void SetSoundVolume(Sound sound, float volume); // Set volume fo void SetSoundPitch(Sound sound, float pitch); // Set pitch for a sound (1.0 is base level) int PlayMusicStream(int musicIndex, char *fileName); // Start music playing (open stream) -void UpdateMusicStream(void); // Updates buffers for music streaming +void UpdateMusicStream(int index); // Updates buffers for music streaming void StopMusicStream(int index); // Stop music playing (close stream) void PauseMusicStream(int index); // Pause music playing void ResumeMusicStream(int index); // Resume playing paused music bool IsMusicPlaying(int index); // Check if music is playing void SetMusicVolume(int index, float volume); // Set volume for music (1.0 is max level) -float GetMusicTimeLength(void); // Get current music time length (in seconds) -float GetMusicTimePlayed(void); // Get current music time played (in seconds) +float GetMusicTimeLength(int index); // Get current music time length (in seconds) +float GetMusicTimePlayed(int index); // Get current music time played (in seconds) int getMusicStreamCount(void); void SetMusicPitch(int index, float pitch); -- cgit v1.2.3 From 075f51e0a3abfd9ebdfc66ee862f933a993105ca Mon Sep 17 00:00:00 2001 From: raysan5 Date: Thu, 12 May 2016 12:20:23 +0200 Subject: Simplified internal (default) dynamic buffers --- src/raylib.h | 2 +- src/rlgl.c | 200 ++++++++++++++++++++++++----------------------------------- src/rlgl.h | 2 +- 3 files changed, 84 insertions(+), 120 deletions(-) (limited to 'src') diff --git a/src/raylib.h b/src/raylib.h index 634ab143..911fd8b5 100644 --- a/src/raylib.h +++ b/src/raylib.h @@ -376,7 +376,7 @@ typedef struct Mesh { float *tangents; // vertex tangents (XYZ - 3 components per vertex) (shader-location = 4) unsigned char *colors; // vertex colors (RGBA - 4 components per vertex) (shader-location = 3) unsigned short *indices; // vertex indices (in case vertex data comes indexed) - int triangleCount; // number of triangles to draw + int triangleCount; // number of triangles stored (indexed or not) BoundingBox bounds; // mesh limits defined by min and max points diff --git a/src/rlgl.c b/src/rlgl.c index 3c0d9e79..0c0da221 100644 --- a/src/rlgl.c +++ b/src/rlgl.c @@ -128,54 +128,23 @@ // Types and Structures Definition //---------------------------------------------------------------------------------- -// Vertex buffer (position + color arrays) -// NOTE: Used for lines and triangles VAOs +// Dynamic vertex buffers (position + texcoords + colors + indices arrays) typedef struct { - int vCounter; - int cCounter; - float *vertices; // 3 components per vertex - unsigned char *colors; // 4 components per vertex -} VertexPositionColorBuffer; - -// Vertex buffer (position + texcoords + color arrays) -// NOTE: Not used -typedef struct { - int vCounter; - int tcCounter; - int cCounter; - float *vertices; // 3 components per vertex - float *texcoords; // 2 components per vertex - unsigned char *colors; // 4 components per vertex -} VertexPositionColorTextureBuffer; - -// Vertex buffer (position + texcoords + normals arrays) -// NOTE: Not used -typedef struct { - int vCounter; - int tcCounter; - int nCounter; - float *vertices; // 3 components per vertex - float *texcoords; // 2 components per vertex - float *normals; // 3 components per vertex - //short *normals; // NOTE: Less data load... but padding issues and normalizing required! -} VertexPositionTextureNormalBuffer; - -// Vertex buffer (position + texcoords + colors + indices arrays) -// NOTE: Used for quads VAO -typedef struct { - int vCounter; - int tcCounter; - int cCounter; - float *vertices; // 3 components per vertex - float *texcoords; // 2 components per vertex - unsigned char *colors; // 4 components per vertex + int vCounter; // vertex position counter to process (and draw) from full buffer + int tcCounter; // vertex texcoord counter to process (and draw) from full buffer + int cCounter; // vertex color counter to process (and draw) from full buffer + float *vertices; // vertex position (XYZ - 3 components per vertex) (shader-location = 0) + float *texcoords; // vertex texture coordinates (UV - 2 components per vertex) (shader-location = 1) + 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; // 6 indices per quad (could be int) + unsigned int *indices; // vertex indices (in case vertex data comes indexed) (6 indices per quad) #elif defined(GRAPHICS_API_OPENGL_ES2) - unsigned short *indices; // 6 indices per quad (must be short) + unsigned short *indices; // vertex indices (in case vertex data comes indexed) (6 indices per quad) // NOTE: 6*2 byte = 12 byte, not alignment problem! #endif -} VertexPositionColorTextureIndexBuffer; + unsigned int vaoId; // OpenGL Vertex Array Object id + unsigned int vboId[4]; // OpenGL Vertex Buffer Objects id (4 types of vertex data) +} DynamicBuffer; // Draw call type // NOTE: Used to track required draw-calls, organized by texture @@ -205,18 +174,9 @@ static DrawMode currentDrawMode; static float currentDepth = -1.0f; -// Default vertex buffers for lines, triangles and quads -static VertexPositionColorBuffer lines; // No texture support -static VertexPositionColorBuffer triangles; // No texture support -static VertexPositionColorTextureIndexBuffer quads; - -// Default vertex buffers VAOs (if supported) -static GLuint vaoLines, vaoTriangles, vaoQuads; - -// Default vertex buffers VBOs -static GLuint linesBuffer[2]; // Lines buffers (position, color) -static GLuint trianglesBuffer[2]; // Triangles buffers (position, color) -static GLuint quadsBuffer[4]; // Quads buffers (position, texcoord, color, index) +static DynamicBuffer lines; +static DynamicBuffer triangles; +static DynamicBuffer quads; // Default buffers draw calls static DrawCall *draws; @@ -1207,7 +1167,7 @@ void rlglDrawEx(Mesh mesh, Material material, Matrix transform, bool wires) glEnableVertexAttribArray(material.shader.texcoord2Loc); } - if (mesh.indices != NULL) glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, quadsBuffer[3]); + if (mesh.indices != NULL) glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, quads.vboId[3]); } // Draw call! @@ -1692,7 +1652,6 @@ void rlglLoadMesh(Mesh *mesh) mesh->vboId[4] = 0; // Vertex tangents VBO mesh->vboId[5] = 0; // Vertex texcoords2 VBO mesh->vboId[6] = 0; // Vertex indices VBO - #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) GLuint vaoId = 0; // Vertex Array Objects (VAO) @@ -2407,22 +2366,28 @@ static void LoadDefaultBuffers(void) // Lines - Initialize arrays (vertex position and color data) lines.vertices = (float *)malloc(sizeof(float)*3*2*MAX_LINES_BATCH); // 3 float by vertex, 2 vertex by line lines.colors = (unsigned char *)malloc(sizeof(unsigned char)*4*2*MAX_LINES_BATCH); // 4 float by color, 2 colors by line + lines.texcoords = NULL; + lines.indices = NULL; for (int i = 0; i < (3*2*MAX_LINES_BATCH); i++) lines.vertices[i] = 0.0f; for (int i = 0; i < (4*2*MAX_LINES_BATCH); i++) lines.colors[i] = 0; lines.vCounter = 0; lines.cCounter = 0; + lines.tcCounter = 0; // Triangles - Initialize arrays (vertex position and color data) triangles.vertices = (float *)malloc(sizeof(float)*3*3*MAX_TRIANGLES_BATCH); // 3 float by vertex, 3 vertex by triangle triangles.colors = (unsigned char *)malloc(sizeof(unsigned char)*4*3*MAX_TRIANGLES_BATCH); // 4 float by color, 3 colors by triangle + triangles.texcoords = NULL; + triangles.indices = NULL; for (int i = 0; i < (3*3*MAX_TRIANGLES_BATCH); i++) triangles.vertices[i] = 0.0f; for (int i = 0; i < (4*3*MAX_TRIANGLES_BATCH); i++) triangles.colors[i] = 0; triangles.vCounter = 0; triangles.cCounter = 0; + triangles.tcCounter = 0; // Quads - Initialize arrays (vertex position, texcoord, color data and indexes) quads.vertices = (float *)malloc(sizeof(float)*3*4*MAX_QUADS_BATCH); // 3 float by vertex, 4 vertex by quad @@ -2468,96 +2433,95 @@ static void LoadDefaultBuffers(void) if (vaoSupported) { // Initialize Lines VAO - glGenVertexArrays(1, &vaoLines); - glBindVertexArray(vaoLines); + glGenVertexArrays(1, &lines.vaoId); + glBindVertexArray(lines.vaoId); } - // Create buffers for our vertex data - glGenBuffers(2, linesBuffer); - // Lines - Vertex buffers binding and attributes enable // Vertex position buffer (shader-location = 0) - glBindBuffer(GL_ARRAY_BUFFER, linesBuffer[0]); + glGenBuffers(2, &lines.vboId[0]); + glBindBuffer(GL_ARRAY_BUFFER, lines.vboId[0]); glBufferData(GL_ARRAY_BUFFER, sizeof(float)*3*2*MAX_LINES_BATCH, lines.vertices, GL_DYNAMIC_DRAW); glEnableVertexAttribArray(currentShader.vertexLoc); glVertexAttribPointer(currentShader.vertexLoc, 3, GL_FLOAT, 0, 0, 0); // Vertex color buffer (shader-location = 3) - glBindBuffer(GL_ARRAY_BUFFER, linesBuffer[1]); + glGenBuffers(2, &lines.vboId[1]); + glBindBuffer(GL_ARRAY_BUFFER, lines.vboId[1]); glBufferData(GL_ARRAY_BUFFER, sizeof(unsigned char)*4*2*MAX_LINES_BATCH, lines.colors, GL_DYNAMIC_DRAW); glEnableVertexAttribArray(currentShader.colorLoc); glVertexAttribPointer(currentShader.colorLoc, 4, GL_UNSIGNED_BYTE, GL_TRUE, 0, 0); - if (vaoSupported) TraceLog(INFO, "[VAO ID %i] Default buffers VAO initialized successfully (lines)", vaoLines); - else TraceLog(INFO, "[VBO ID %i][VBO ID %i] Default buffers VBOs initialized successfully (lines)", linesBuffer[0], linesBuffer[1]); + if (vaoSupported) TraceLog(INFO, "[VAO ID %i] Default buffers VAO initialized successfully (lines)", lines.vaoId); + else TraceLog(INFO, "[VBO ID %i][VBO ID %i] Default buffers VBOs initialized successfully (lines)", lines.vboId[0], lines.vboId[1]); // Upload and link triangles vertex buffers if (vaoSupported) { // Initialize Triangles VAO - glGenVertexArrays(1, &vaoTriangles); - glBindVertexArray(vaoTriangles); + glGenVertexArrays(1, &triangles.vaoId); + glBindVertexArray(triangles.vaoId); } - // Create buffers for our vertex data - glGenBuffers(2, trianglesBuffer); - // Triangles - Vertex buffers binding and attributes enable // Vertex position buffer (shader-location = 0) - glBindBuffer(GL_ARRAY_BUFFER, trianglesBuffer[0]); + glGenBuffers(1, &triangles.vboId[0]); + glBindBuffer(GL_ARRAY_BUFFER, triangles.vboId[0]); glBufferData(GL_ARRAY_BUFFER, sizeof(float)*3*3*MAX_TRIANGLES_BATCH, triangles.vertices, GL_DYNAMIC_DRAW); glEnableVertexAttribArray(currentShader.vertexLoc); glVertexAttribPointer(currentShader.vertexLoc, 3, GL_FLOAT, 0, 0, 0); // Vertex color buffer (shader-location = 3) - glBindBuffer(GL_ARRAY_BUFFER, trianglesBuffer[1]); + glGenBuffers(1, &triangles.vboId[1]); + glBindBuffer(GL_ARRAY_BUFFER, triangles.vboId[1]); glBufferData(GL_ARRAY_BUFFER, sizeof(unsigned char)*4*3*MAX_TRIANGLES_BATCH, triangles.colors, GL_DYNAMIC_DRAW); glEnableVertexAttribArray(currentShader.colorLoc); glVertexAttribPointer(currentShader.colorLoc, 4, GL_UNSIGNED_BYTE, GL_TRUE, 0, 0); - if (vaoSupported) TraceLog(INFO, "[VAO ID %i] Default buffers VAO initialized successfully (triangles)", vaoTriangles); - else TraceLog(INFO, "[VBO ID %i][VBO ID %i] Default buffers VBOs initialized successfully(triangles)", trianglesBuffer[0], trianglesBuffer[1]); + if (vaoSupported) TraceLog(INFO, "[VAO ID %i] Default buffers VAO initialized successfully (triangles)", triangles.vaoId); + else TraceLog(INFO, "[VBO ID %i][VBO ID %i] Default buffers VBOs initialized successfully(triangles)", triangles.vboId[0], triangles.vboId[1]); // Upload and link quads vertex buffers if (vaoSupported) { // Initialize Quads VAO - glGenVertexArrays(1, &vaoQuads); - glBindVertexArray(vaoQuads); + glGenVertexArrays(1, &quads.vaoId); + glBindVertexArray(quads.vaoId); } - // Create buffers for our vertex data - glGenBuffers(4, quadsBuffer); - // Quads - Vertex buffers binding and attributes enable // Vertex position buffer (shader-location = 0) - glBindBuffer(GL_ARRAY_BUFFER, quadsBuffer[0]); + glGenBuffers(1, &quads.vboId[0]); + glBindBuffer(GL_ARRAY_BUFFER, quads.vboId[0]); glBufferData(GL_ARRAY_BUFFER, sizeof(float)*3*4*MAX_QUADS_BATCH, quads.vertices, GL_DYNAMIC_DRAW); glEnableVertexAttribArray(currentShader.vertexLoc); glVertexAttribPointer(currentShader.vertexLoc, 3, GL_FLOAT, 0, 0, 0); // Vertex texcoord buffer (shader-location = 1) - glBindBuffer(GL_ARRAY_BUFFER, quadsBuffer[1]); + glGenBuffers(1, &quads.vboId[1]); + glBindBuffer(GL_ARRAY_BUFFER, quads.vboId[1]); glBufferData(GL_ARRAY_BUFFER, sizeof(float)*2*4*MAX_QUADS_BATCH, quads.texcoords, GL_DYNAMIC_DRAW); glEnableVertexAttribArray(currentShader.texcoordLoc); glVertexAttribPointer(currentShader.texcoordLoc, 2, GL_FLOAT, 0, 0, 0); // Vertex color buffer (shader-location = 3) - glBindBuffer(GL_ARRAY_BUFFER, quadsBuffer[2]); + glGenBuffers(1, &quads.vboId[2]); + glBindBuffer(GL_ARRAY_BUFFER, quads.vboId[2]); glBufferData(GL_ARRAY_BUFFER, sizeof(unsigned char)*4*4*MAX_QUADS_BATCH, quads.colors, GL_DYNAMIC_DRAW); glEnableVertexAttribArray(currentShader.colorLoc); glVertexAttribPointer(currentShader.colorLoc, 4, GL_UNSIGNED_BYTE, GL_TRUE, 0, 0); // Fill index buffer - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, quadsBuffer[3]); + glGenBuffers(1, &quads.vboId[3]); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, quads.vboId[3]); #if defined(GRAPHICS_API_OPENGL_33) glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(int)*6*MAX_QUADS_BATCH, quads.indices, GL_STATIC_DRAW); #elif defined(GRAPHICS_API_OPENGL_ES2) glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(short)*6*MAX_QUADS_BATCH, quads.indices, GL_STATIC_DRAW); #endif - if (vaoSupported) TraceLog(INFO, "[VAO ID %i] Default buffers VAO initialized successfully (quads)", vaoQuads); - else TraceLog(INFO, "[VBO ID %i][VBO ID %i][VBO ID %i][VBO ID %i] Default buffers VBOs initialized successfully (quads)", quadsBuffer[0], quadsBuffer[1], quadsBuffer[2], quadsBuffer[3]); + if (vaoSupported) TraceLog(INFO, "[VAO ID %i] Default buffers VAO initialized successfully (quads)", quads.vaoId); + else TraceLog(INFO, "[VBO ID %i][VBO ID %i][VBO ID %i][VBO ID %i] Default buffers VBOs initialized successfully (quads)", quads.vboId[0], quads.vboId[1], quads.vboId[2], quads.vboId[3]); // Unbind the current VAO if (vaoSupported) glBindVertexArray(0); @@ -2573,15 +2537,15 @@ static void UpdateDefaultBuffers(void) if (lines.vCounter > 0) { // Activate Lines VAO - if (vaoSupported) glBindVertexArray(vaoLines); + if (vaoSupported) glBindVertexArray(lines.vaoId); // Lines - vertex positions buffer - glBindBuffer(GL_ARRAY_BUFFER, linesBuffer[0]); + glBindBuffer(GL_ARRAY_BUFFER, lines.vboId[0]); //glBufferData(GL_ARRAY_BUFFER, sizeof(float)*3*2*MAX_LINES_BATCH, lines.vertices, GL_DYNAMIC_DRAW); glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(float)*3*lines.vCounter, lines.vertices); // target - offset (in bytes) - size (in bytes) - data pointer // Lines - colors buffer - glBindBuffer(GL_ARRAY_BUFFER, linesBuffer[1]); + glBindBuffer(GL_ARRAY_BUFFER, lines.vboId[1]); //glBufferData(GL_ARRAY_BUFFER, sizeof(float)*4*2*MAX_LINES_BATCH, lines.colors, GL_DYNAMIC_DRAW); glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(unsigned char)*4*lines.cCounter, lines.colors); } @@ -2590,15 +2554,15 @@ static void UpdateDefaultBuffers(void) if (triangles.vCounter > 0) { // Activate Triangles VAO - if (vaoSupported) glBindVertexArray(vaoTriangles); + if (vaoSupported) glBindVertexArray(triangles.vaoId); // Triangles - vertex positions buffer - glBindBuffer(GL_ARRAY_BUFFER, trianglesBuffer[0]); + glBindBuffer(GL_ARRAY_BUFFER, triangles.vboId[0]); //glBufferData(GL_ARRAY_BUFFER, sizeof(float)*3*3*MAX_TRIANGLES_BATCH, triangles.vertices, GL_DYNAMIC_DRAW); glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(float)*3*triangles.vCounter, triangles.vertices); // Triangles - colors buffer - glBindBuffer(GL_ARRAY_BUFFER, trianglesBuffer[1]); + glBindBuffer(GL_ARRAY_BUFFER, triangles.vboId[1]); //glBufferData(GL_ARRAY_BUFFER, sizeof(float)*4*3*MAX_TRIANGLES_BATCH, triangles.colors, GL_DYNAMIC_DRAW); glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(unsigned char)*4*triangles.cCounter, triangles.colors); } @@ -2607,20 +2571,20 @@ static void UpdateDefaultBuffers(void) if (quads.vCounter > 0) { // Activate Quads VAO - if (vaoSupported) glBindVertexArray(vaoQuads); + if (vaoSupported) glBindVertexArray(quads.vaoId); // Quads - vertex positions buffer - glBindBuffer(GL_ARRAY_BUFFER, quadsBuffer[0]); + glBindBuffer(GL_ARRAY_BUFFER, quads.vboId[0]); //glBufferData(GL_ARRAY_BUFFER, sizeof(float)*3*4*MAX_QUADS_BATCH, quads.vertices, GL_DYNAMIC_DRAW); glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(float)*3*quads.vCounter, quads.vertices); // Quads - texture coordinates buffer - glBindBuffer(GL_ARRAY_BUFFER, quadsBuffer[1]); + glBindBuffer(GL_ARRAY_BUFFER, quads.vboId[1]); //glBufferData(GL_ARRAY_BUFFER, sizeof(float)*2*4*MAX_QUADS_BATCH, quads.texcoords, GL_DYNAMIC_DRAW); glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(float)*2*quads.vCounter, quads.texcoords); // Quads - colors buffer - glBindBuffer(GL_ARRAY_BUFFER, quadsBuffer[2]); + glBindBuffer(GL_ARRAY_BUFFER, quads.vboId[2]); //glBufferData(GL_ARRAY_BUFFER, sizeof(float)*4*4*MAX_QUADS_BATCH, quads.colors, GL_DYNAMIC_DRAW); glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(unsigned char)*4*quads.vCounter, quads.colors); @@ -2659,17 +2623,17 @@ static void DrawDefaultBuffers(void) if (vaoSupported) { - glBindVertexArray(vaoLines); + glBindVertexArray(lines.vaoId); } else { // Bind vertex attrib: position (shader-location = 0) - glBindBuffer(GL_ARRAY_BUFFER, linesBuffer[0]); + glBindBuffer(GL_ARRAY_BUFFER, lines.vboId[0]); glVertexAttribPointer(currentShader.vertexLoc, 3, GL_FLOAT, 0, 0, 0); glEnableVertexAttribArray(currentShader.vertexLoc); // Bind vertex attrib: color (shader-location = 3) - glBindBuffer(GL_ARRAY_BUFFER, linesBuffer[1]); + glBindBuffer(GL_ARRAY_BUFFER, lines.vboId[1]); glVertexAttribPointer(currentShader.colorLoc, 4, GL_UNSIGNED_BYTE, GL_TRUE, 0, 0); glEnableVertexAttribArray(currentShader.colorLoc); } @@ -2687,17 +2651,17 @@ static void DrawDefaultBuffers(void) if (vaoSupported) { - glBindVertexArray(vaoTriangles); + glBindVertexArray(triangles.vaoId); } else { // Bind vertex attrib: position (shader-location = 0) - glBindBuffer(GL_ARRAY_BUFFER, trianglesBuffer[0]); + glBindBuffer(GL_ARRAY_BUFFER, triangles.vboId[0]); glVertexAttribPointer(currentShader.vertexLoc, 3, GL_FLOAT, 0, 0, 0); glEnableVertexAttribArray(currentShader.vertexLoc); // Bind vertex attrib: color (shader-location = 3) - glBindBuffer(GL_ARRAY_BUFFER, trianglesBuffer[1]); + glBindBuffer(GL_ARRAY_BUFFER, triangles.vboId[1]); glVertexAttribPointer(currentShader.colorLoc, 4, GL_UNSIGNED_BYTE, GL_TRUE, 0, 0); glEnableVertexAttribArray(currentShader.colorLoc); } @@ -2717,26 +2681,26 @@ static void DrawDefaultBuffers(void) if (vaoSupported) { - glBindVertexArray(vaoQuads); + glBindVertexArray(quads.vaoId); } else { // Bind vertex attrib: position (shader-location = 0) - glBindBuffer(GL_ARRAY_BUFFER, quadsBuffer[0]); + glBindBuffer(GL_ARRAY_BUFFER, quads.vboId[0]); glVertexAttribPointer(currentShader.vertexLoc, 3, GL_FLOAT, 0, 0, 0); glEnableVertexAttribArray(currentShader.vertexLoc); // Bind vertex attrib: texcoord (shader-location = 1) - glBindBuffer(GL_ARRAY_BUFFER, quadsBuffer[1]); + glBindBuffer(GL_ARRAY_BUFFER, quads.vboId[1]); glVertexAttribPointer(currentShader.texcoordLoc, 2, GL_FLOAT, 0, 0, 0); glEnableVertexAttribArray(currentShader.texcoordLoc); // Bind vertex attrib: color (shader-location = 3) - glBindBuffer(GL_ARRAY_BUFFER, quadsBuffer[2]); + glBindBuffer(GL_ARRAY_BUFFER, quads.vboId[2]); glVertexAttribPointer(currentShader.colorLoc, 4, GL_UNSIGNED_BYTE, GL_TRUE, 0, 0); glEnableVertexAttribArray(currentShader.colorLoc); - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, quadsBuffer[3]); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, quads.vboId[3]); } //TraceLog(DEBUG, "Draws required per frame: %i", drawsCounter); @@ -2806,21 +2770,21 @@ static void UnloadDefaultBuffers(void) glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); // Delete VBOs from GPU (VRAM) - glDeleteBuffers(1, &linesBuffer[0]); - glDeleteBuffers(1, &linesBuffer[1]); - glDeleteBuffers(1, &trianglesBuffer[0]); - glDeleteBuffers(1, &trianglesBuffer[1]); - glDeleteBuffers(1, &quadsBuffer[0]); - glDeleteBuffers(1, &quadsBuffer[1]); - glDeleteBuffers(1, &quadsBuffer[2]); - glDeleteBuffers(1, &quadsBuffer[3]); + glDeleteBuffers(1, &lines.vboId[0]); + glDeleteBuffers(1, &lines.vboId[1]); + glDeleteBuffers(1, &triangles.vboId[0]); + glDeleteBuffers(1, &triangles.vboId[1]); + glDeleteBuffers(1, &quads.vboId[0]); + glDeleteBuffers(1, &quads.vboId[1]); + glDeleteBuffers(1, &quads.vboId[2]); + glDeleteBuffers(1, &quads.vboId[3]); if (vaoSupported) { // Delete VAOs from GPU (VRAM) - glDeleteVertexArrays(1, &vaoLines); - glDeleteVertexArrays(1, &vaoTriangles); - glDeleteVertexArrays(1, &vaoQuads); + glDeleteVertexArrays(1, &lines.vaoId); + glDeleteVertexArrays(1, &triangles.vaoId); + glDeleteVertexArrays(1, &quads.vaoId); } // Free vertex arrays memory from CPU (RAM) diff --git a/src/rlgl.h b/src/rlgl.h index 6e694dae..7b88bc9e 100644 --- a/src/rlgl.h +++ b/src/rlgl.h @@ -147,7 +147,7 @@ typedef enum { OPENGL_11 = 1, OPENGL_33, OPENGL_ES_20 } GlVersion; float *tangents; // vertex tangents (XYZ - 3 components per vertex) (shader-location = 4) unsigned char *colors; // vertex colors (RGBA - 4 components per vertex) (shader-location = 3) unsigned short *indices; // vertex indices (in case vertex data comes indexed) - int triangleCount; // number of triangles to draw + int triangleCount; // number of triangles stored (indexed or not) BoundingBox bounds; // mesh limits defined by min and max points -- cgit v1.2.3 From e060944b34f11978392f5c24282c95781caae63e Mon Sep 17 00:00:00 2001 From: raysan5 Date: Thu, 12 May 2016 13:02:04 +0200 Subject: Added QuaternionInvert() --- src/raymath.h | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'src') diff --git a/src/raymath.h b/src/raymath.h index 52e92b50..59d66e56 100644 --- a/src/raymath.h +++ b/src/raymath.h @@ -158,6 +158,7 @@ RMDEF void PrintMatrix(Matrix m); // Print matrix ut //------------------------------------------------------------------------------------ RMDEF float QuaternionLength(Quaternion quat); // Compute the length of a quaternion RMDEF void QuaternionNormalize(Quaternion *q); // Normalize provided quaternion +RMDEF void QuaternionInvert(Quaternion *quat); // Invert provided quaternion RMDEF Quaternion QuaternionMultiply(Quaternion q1, Quaternion q2); // Calculate two quaternion multiplication RMDEF Quaternion QuaternionSlerp(Quaternion q1, Quaternion q2, float slerp); // Calculates spherical linear interpolation between two quaternions RMDEF Quaternion QuaternionFromMatrix(Matrix matrix); // Returns a quaternion for a given rotation matrix @@ -908,6 +909,23 @@ RMDEF void QuaternionNormalize(Quaternion *q) q->w *= ilength; } +// Invert provided quaternion +RMDEF void QuaternionInvert(Quaternion *quat) +{ + float length = QuaternionLength(*quat); + float lengthSq = length*length; + + if (lengthSq != 0.0) + { + float i = 1.0f/lengthSq; + + quat->x *= -i; + quat->y *= -i; + quat->z *= -i; + quat->w *= i; + } +} + // Calculate two quaternion multiplication RMDEF Quaternion QuaternionMultiply(Quaternion q1, Quaternion q2) { -- cgit v1.2.3 From 83dbc076507b7025aa0a1315e409ff46f75c1e0b Mon Sep 17 00:00:00 2001 From: Joshua Reisenauer Date: Thu, 12 May 2016 16:02:23 -0700 Subject: buffering of music now uses update audio context --- src/audio.c | 94 ++++++++++++++++++++--------------------------------------- src/easings.h | 10 +++---- 2 files changed, 37 insertions(+), 67 deletions(-) (limited to 'src') diff --git a/src/audio.c b/src/audio.c index 94729566..cc964cd1 100644 --- a/src/audio.c +++ b/src/audio.c @@ -118,12 +118,12 @@ static Music currentMusic[MAX_MUSIC_STREAMS]; // Current //---------------------------------------------------------------------------------- // Module specific Functions Declaration //---------------------------------------------------------------------------------- -static Wave LoadWAV(const char *fileName); // Load WAV file -static Wave LoadOGG(char *fileName); // Load OGG file -static void UnloadWave(Wave wave); // Unload wave data +static Wave LoadWAV(const char *fileName); // Load WAV file +static Wave LoadOGG(char *fileName); // Load OGG file +static void UnloadWave(Wave wave); // Unload wave data -static bool BufferMusicStream(int index, ALuint buffer); // Fill music buffers with data -static void EmptyMusicStream(int index); // Empty music buffers +static bool BufferMusicStream(int index); // Fill music buffers with data +static void EmptyMusicStream(int index); // Empty music buffers static unsigned short FillAlBufferWithSilence(AudioContext_t *context, ALuint buffer);// fill buffer with zeros, returns number processed static void ResampleShortToFloat(short *shorts, float *floats, unsigned short len); // pass two arrays of the same legnth in @@ -970,7 +970,7 @@ float GetMusicTimePlayed(int index) //---------------------------------------------------------------------------------- // Fill music buffers with new data from music stream -static bool BufferMusicStream(int index, ALuint buffer) +static bool BufferMusicStream(int index) { short pcm[MUSIC_BUFFER_SIZE_SHORT]; float pcmf[MUSIC_BUFFER_SIZE_FLOAT]; @@ -985,33 +985,17 @@ static bool BufferMusicStream(int index, ALuint buffer) { int readlen = MUSIC_BUFFER_SIZE_FLOAT / 2; jar_xm_generate_samples(currentMusic[index].chipctx, pcmf, readlen); // reads 2*readlen shorts and moves them to buffer+size memory location + UpdateAudioContext(currentMusic[index].ctx, pcmf, MUSIC_BUFFER_SIZE_FLOAT); size += readlen * currentMusic[index].ctx->channels; // Not sure if this is what it needs - - alBufferData(buffer, currentMusic[index].ctx->alFormat, pcmf, size*sizeof(float), 48000); currentMusic[index].totalSamplesLeft -= size; if(currentMusic[index].totalSamplesLeft <= 0) active = false; } else { - while (size < MUSIC_BUFFER_SIZE_SHORT) - { - streamedBytes = stb_vorbis_get_samples_short_interleaved(currentMusic[index].stream, currentMusic[index].ctx->channels, pcm + size, MUSIC_BUFFER_SIZE_SHORT - size); - if (streamedBytes > 0) size += (streamedBytes*currentMusic[index].ctx->channels); - else break; - } - - if (size > 0) - { - alBufferData(buffer, currentMusic[index].ctx->alFormat, pcm, size*sizeof(short), currentMusic[index].ctx->sampleRate); - currentMusic[index].totalSamplesLeft -= size; - - if(currentMusic[index].totalSamplesLeft <= 0) active = false; // end if no more samples left - } - else - { - active = false; - TraceLog(WARNING, "No more data obtained from stream"); - } + streamedBytes = stb_vorbis_get_samples_short_interleaved(currentMusic[index].stream, currentMusic[index].ctx->channels, pcm, MUSIC_BUFFER_SIZE_SHORT); + UpdateAudioContext(currentMusic[index].ctx, pcm, MUSIC_BUFFER_SIZE_SHORT); + currentMusic[index].totalSamplesLeft -= MUSIC_BUFFER_SIZE_SHORT; + if(currentMusic[index].totalSamplesLeft <= 0) active = false; } TraceLog(DEBUG, "Streaming music data to buffer. Bytes streamed: %i", size); } @@ -1038,53 +1022,39 @@ static void EmptyMusicStream(int index) // Update (re-fill) music buffers if data already processed void UpdateMusicStream(int index) { - ALuint buffer = 0; - ALint processed = 0; + ALenum state; bool active = true; if (index < MAX_MUSIC_STREAMS && musicEnabled) { - // Get the number of already processed buffers (if any) - alGetSourcei(currentMusic[index].source, AL_BUFFERS_PROCESSED, &processed); - - while (processed > 0) + active = BufferMusicStream(index); + + if ((!active) && (currentMusic[index].loop)) { - // Recover processed buffer for refill - alSourceUnqueueBuffers(currentMusic[index].source, 1, &buffer); - - // Refill buffer - active = BufferMusicStream(buffer); - - // If no more data to stream, restart music (if loop) - if ((!active) && (currentMusic[index].loop)) + if(currentMusic[index].chipTune) { - if(currentMusic[index].chipTune) - { - currentMusic[index].totalSamplesLeft = currentMusic[index].totalLengthSeconds * currentMusic[index].ctx->sampleRate; - } - else - { - stb_vorbis_seek_start(currentMusic[index].stream); - currentMusic[index].totalSamplesLeft = stb_vorbis_stream_length_in_samples(currentMusic[index].stream)*currentMusic[index].ctx->channels; - } - active = BufferMusicStream(buffer); + currentMusic[index].totalSamplesLeft = currentMusic[index].totalLengthSeconds * currentMusic[index].ctx->sampleRate; } + else + { + stb_vorbis_seek_start(currentMusic[index].stream); + currentMusic[index].totalSamplesLeft = stb_vorbis_stream_length_in_samples(currentMusic[index].stream)*currentMusic[index].ctx->channels; + } + active = BufferMusicStream(index); + } + - // Add refilled buffer to queue again... don't let the music stop! - alSourceQueueBuffers(currentMusic[index].source, 1, &buffer); - - if (alGetError() != AL_NO_ERROR) TraceLog(WARNING, "Error buffering data..."); + if (alGetError() != AL_NO_ERROR) TraceLog(WARNING, "Error buffering data..."); - processed--; - } + processed--; + } - ALenum state; - alGetSourcei(currentMusic[index].source, AL_SOURCE_STATE, &state); + + alGetSourcei(currentMusic[index].source, AL_SOURCE_STATE, &state); - if ((state != AL_PLAYING) && active) alSourcePlay(currentMusic[index].source); + if ((state != AL_PLAYING) && active) alSourcePlay(currentMusic[index].source); - if (!active) StopMusicStream(); - } + if (!active) StopMusicStream(); } // Load WAV file into Wave structure diff --git a/src/easings.h b/src/easings.h index e1e5465a..a8178f4a 100644 --- a/src/easings.h +++ b/src/easings.h @@ -18,11 +18,11 @@ * float speed = 1.f; * float currentTime = 0.f; * float currentPos[2] = {0,0}; -* float newPos[2] = {1,1}; -* float tempPosition[2] = currentPos;//x,y positions -* while(currentPos[0] < newPos[0]) -* currentPos[0] = EaseSineIn(currentTime, tempPosition[0], tempPosition[0]-newPos[0], speed); -* currentPos[1] = EaseSineIn(currentTime, tempPosition[1], tempPosition[1]-newPos[0], speed); +* float finalPos[2] = {1,1}; +* float startPosition[2] = currentPos;//x,y positions +* while(currentPos[0] < finalPos[0]) +* currentPos[0] = EaseSineIn(currentTime, startPosition[0], startPosition[0]-finalPos[0], speed); +* currentPos[1] = EaseSineIn(currentTime, startPosition[1], startPosition[1]-finalPos[0], speed); * currentTime += diffTime(); * * A port of Robert Penner's easing equations to C (http://robertpenner.com/easing/) -- cgit v1.2.3 From 5107a2dc40330623f61205ee70f067d24afb0af8 Mon Sep 17 00:00:00 2001 From: Joshua Reisenauer Date: Thu, 12 May 2016 21:14:02 -0700 Subject: bug fixes --- src/audio.c | 69 +++++++++++++++++++++++++++++++------------------------------ 1 file changed, 35 insertions(+), 34 deletions(-) (limited to 'src') diff --git a/src/audio.c b/src/audio.c index cc964cd1..d19d3306 100644 --- a/src/audio.c +++ b/src/audio.c @@ -77,19 +77,6 @@ // Types and Structures Definition //---------------------------------------------------------------------------------- -// Music type (file streaming from memory) -// NOTE: Anything longer than ~10 seconds should be streamed... -typedef struct Music { - stb_vorbis *stream; - jar_xm_context_t *chipctx; // Stores jar_xm context - AudioContext_t *ctx; // audio context - - int totalSamplesLeft; - float totalLengthSeconds; - bool loop; - bool chipTune; // True if chiptune is loaded -} Music; - // Audio Context, used to create custom audio streams that are not bound to a sound file. There can be // no more than 4 concurrent audio contexts in use. This is due to each active context being tied to // a dedicated mix channel. All audio is 32bit floating point in stereo. @@ -104,6 +91,19 @@ typedef struct AudioContext_t { ALuint alBuffer[MAX_STREAM_BUFFERS]; // openAL sample buffer } AudioContext_t; +// Music type (file streaming from memory) +// NOTE: Anything longer than ~10 seconds should be streamed... +typedef struct Music { + stb_vorbis *stream; + jar_xm_context_t *chipctx; // Stores jar_xm context + AudioContext_t *ctx; // audio context + + int totalSamplesLeft; + float totalLengthSeconds; + bool loop; + bool chipTune; // True if chiptune is loaded +} Music; + #if defined(AUDIO_STANDALONE) typedef enum { INFO = 0, ERROR, WARNING, DEBUG, OTHER } TraceLogType; #endif @@ -168,7 +168,10 @@ void InitAudioDevice(void) // Close the audio device for the current context, and destroys the context void CloseAudioDevice(void) { - StopMusicStream(); // Stop music streaming and close current stream + for(int index=0; index= MAX_AUDIO_CONTEXTS) return NULL; if(!IsAudioDeviceReady()) InitAudioDevice(); - else StopMusicStream(); if(!mixChannelsActive_g[mixChannel]){ AudioContext_t *ac = (AudioContext_t*)malloc(sizeof(AudioContext_t)); @@ -776,7 +778,6 @@ int PlayMusicStream(int musicIndex, char *fileName) else if(musicIndex = MAX_AUDIO_CONTEXTS - 1) return 2; // error } - if (strcmp(GetExtension(fileName),"ogg") == 0) { // Open audio stream @@ -888,12 +889,12 @@ void ResumeMusicStream(int index) { // Resume music playing... if music available! ALenum state; - if(currentMusic[musicIndex].ctx){ - alGetSourcei(currentMusic[musicIndex].ctx->alSource, AL_SOURCE_STATE, &state); + if(currentMusic[index].ctx){ + alGetSourcei(currentMusic[index].ctx->alSource, AL_SOURCE_STATE, &state); if (state == AL_PAUSED) { TraceLog(INFO, "Resuming music stream"); - alSourcePlay(currentMusic[musicIndex].ctx->alSource); + alSourcePlay(currentMusic[index].ctx->alSource); musicEnabled = true; } } @@ -976,9 +977,8 @@ static bool BufferMusicStream(int index) float pcmf[MUSIC_BUFFER_SIZE_FLOAT]; int size = 0; // Total size of data steamed (in bytes) - int streamedBytes = 0; // samples of data obtained, channels are not included in calculation bool active = true; // We can get more data from stream (not finished) - + if (musicEnabled) { if (currentMusic[index].chipTune) // There is no end of stream for xmfiles, once the end is reached zeros are generated for non looped chiptunes. @@ -992,12 +992,12 @@ static bool BufferMusicStream(int index) } else { - streamedBytes = stb_vorbis_get_samples_short_interleaved(currentMusic[index].stream, currentMusic[index].ctx->channels, pcm, MUSIC_BUFFER_SIZE_SHORT); - UpdateAudioContext(currentMusic[index].ctx, pcm, MUSIC_BUFFER_SIZE_SHORT); - currentMusic[index].totalSamplesLeft -= MUSIC_BUFFER_SIZE_SHORT; + int streamedBytes = stb_vorbis_get_samples_short_interleaved(currentMusic[index].stream, currentMusic[index].ctx->channels, pcm, MUSIC_BUFFER_SIZE_SHORT); + UpdateAudioContext(currentMusic[index].ctx, pcm, streamedBytes); + currentMusic[index].totalSamplesLeft -= streamedBytes*currentMusic[index].ctx->channels; if(currentMusic[index].totalSamplesLeft <= 0) active = false; } - TraceLog(DEBUG, "Streaming music data to buffer. Bytes streamed: %i", size); + // TraceLog(DEBUG, "Streaming music data to buffer. Bytes streamed: %i", size); } return active; @@ -1009,11 +1009,11 @@ static void EmptyMusicStream(int index) ALuint buffer = 0; int queued = 0; - alGetSourcei(currentMusic[index].source, AL_BUFFERS_QUEUED, &queued); + alGetSourcei(currentMusic[index].ctx->alSource, AL_BUFFERS_QUEUED, &queued); while (queued > 0) { - alSourceUnqueueBuffers(currentMusic[index].source, 1, &buffer); + alSourceUnqueueBuffers(currentMusic[index].ctx->alSource, 1, &buffer); queued--; } @@ -1045,16 +1045,17 @@ void UpdateMusicStream(int index) if (alGetError() != AL_NO_ERROR) TraceLog(WARNING, "Error buffering data..."); + + alGetSourcei(currentMusic[index].ctx->alSource, AL_SOURCE_STATE, &state); - processed--; - } - - - alGetSourcei(currentMusic[index].source, AL_SOURCE_STATE, &state); + if ((state != AL_PLAYING) && active) alSourcePlay(currentMusic[index].ctx->alSource); - if ((state != AL_PLAYING) && active) alSourcePlay(currentMusic[index].source); + if (!active) StopMusicStream(index); + + } + else + return; - if (!active) StopMusicStream(); } // Load WAV file into Wave structure -- cgit v1.2.3 From b46a8005979bb626f4e7a7199932c4b85e9efb3a Mon Sep 17 00:00:00 2001 From: Chris Hemingway Date: Sat, 14 May 2016 01:10:05 +0100 Subject: Make GRAPHICS_API_OPENGL_33 work on OSX, closes #113 --- src/core.c | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'src') diff --git a/src/core.c b/src/core.c index 54d42f83..a94ad48d 100644 --- a/src/core.c +++ b/src/core.c @@ -1447,7 +1447,11 @@ static void InitDisplay(int width, int height) glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); // Choose OpenGL minor version (just hint) glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); // Profiles Hint: Only 3.3 and above! // Other values: GLFW_OPENGL_ANY_PROFILE, GLFW_OPENGL_COMPAT_PROFILE +#ifdef __APPLE__ + glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // OSX Requires +#else glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_FALSE); // Fordward Compatibility Hint: Only 3.3 and above! +#endif //glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, GL_TRUE); } -- cgit v1.2.3 From ea4b5552c2b4ff8a907dd1fefc084317187af168 Mon Sep 17 00:00:00 2001 From: Joshua Reisenauer Date: Sat, 14 May 2016 00:25:40 -0700 Subject: corrected typos --- src/audio.c | 42 +++++++++++++++++++++++++++--------------- 1 file changed, 27 insertions(+), 15 deletions(-) (limited to 'src') diff --git a/src/audio.c b/src/audio.c index d19d3306..c53d6318 100644 --- a/src/audio.c +++ b/src/audio.c @@ -85,7 +85,7 @@ typedef struct AudioContext_t { unsigned char channels; // 1=mono,2=stereo unsigned char mixChannel; // 0-3 or mixA-mixD, each mix channel can receive up to one dedicated audio stream bool floatingPoint; // if false then the short datatype is used instead - bool playing; + bool playing; // false if paused ALenum alFormat; // openAL format specifier ALuint alSource; // openAL source ALuint alBuffer[MAX_STREAM_BUFFERS]; // openAL sample buffer @@ -165,13 +165,14 @@ void InitAudioDevice(void) alListener3f(AL_ORIENTATION, 0, 0, -1); } -// Close the audio device for the current context, and destroys the context +// Close the audio device for all contexts void CloseAudioDevice(void) { for(int index=0; indexplaying = true; currentMusic[musicIndex].totalSamplesLeft = stb_vorbis_stream_length_in_samples(currentMusic[musicIndex].stream) * info.channels; currentMusic[musicIndex].totalLengthSeconds = stb_vorbis_stream_length_in_seconds(currentMusic[musicIndex].stream); @@ -822,6 +824,7 @@ int PlayMusicStream(int musicIndex, char *fileName) currentMusic[musicIndex].totalSamplesLeft = jar_xm_get_remaining_samples(currentMusic[musicIndex].chipctx); currentMusic[musicIndex].totalLengthSeconds = ((float)currentMusic[musicIndex].totalSamplesLeft) / 48000.f; musicEnabled = true; + currentMusic[musicIndex].ctx->playing = true; TraceLog(INFO, "[%s] XM number of samples: %i", fileName, currentMusic[musicIndex].totalSamplesLeft); TraceLog(INFO, "[%s] XM track length: %11.6f sec", fileName, currentMusic[musicIndex].totalLengthSeconds); @@ -859,16 +862,21 @@ void StopMusicStream(int index) } if(!getMusicStreamCount()) musicEnabled = false; + if(currentMusic[index].stream || currentMusic[index].chipctx) + { + currentMusic[index].stream = NULL; + currentMusic[index].chipctx = NULL; + } } } //get number of music channels active at this time, this does not mean they are playing int getMusicStreamCount(void) { - int musicCount; + int musicCount = 0; for(int musicIndex = 0; musicIndex < MAX_MUSIC_STREAMS; musicIndex++) // find empty music slot if(currentMusic[musicIndex].stream != NULL || currentMusic[musicIndex].chipTune) musicCount++; - + return musicCount; } @@ -876,11 +884,11 @@ int getMusicStreamCount(void) void PauseMusicStream(int index) { // Pause music stream if music available! - if (musicEnabled) + if (currentMusic[index].ctx && musicEnabled) { TraceLog(INFO, "Pausing music stream"); - UpdateAudioContext(currentMusic[index].ctx, NULL, 0); // pushing null data auto pauses stream - musicEnabled = false; + alSourcePause(currentMusic[index].ctx->alSource); + currentMusic[index].ctx->playing = false; } } @@ -895,7 +903,7 @@ void ResumeMusicStream(int index) { TraceLog(INFO, "Resuming music stream"); alSourcePlay(currentMusic[index].ctx->alSource); - musicEnabled = true; + currentMusic[index].ctx->playing = true; } } } @@ -981,13 +989,17 @@ static bool BufferMusicStream(int index) if (musicEnabled) { + if(!currentMusic[index].ctx->playing) + { + UpdateAudioContext(currentMusic[index].ctx, NULL, 0); + return false; + } + if (currentMusic[index].chipTune) // There is no end of stream for xmfiles, once the end is reached zeros are generated for non looped chiptunes. { - int readlen = MUSIC_BUFFER_SIZE_FLOAT / 2; - jar_xm_generate_samples(currentMusic[index].chipctx, pcmf, readlen); // reads 2*readlen shorts and moves them to buffer+size memory location + jar_xm_generate_samples(currentMusic[index].chipctx, pcmf, MUSIC_BUFFER_SIZE_FLOAT / 2); // reads 2*readlen shorts and moves them to buffer+size memory location UpdateAudioContext(currentMusic[index].ctx, pcmf, MUSIC_BUFFER_SIZE_FLOAT); - size += readlen * currentMusic[index].ctx->channels; // Not sure if this is what it needs - currentMusic[index].totalSamplesLeft -= size; + currentMusic[index].totalSamplesLeft -= MUSIC_BUFFER_SIZE_FLOAT; if(currentMusic[index].totalSamplesLeft <= 0) active = false; } else @@ -997,7 +1009,7 @@ static bool BufferMusicStream(int index) currentMusic[index].totalSamplesLeft -= streamedBytes*currentMusic[index].ctx->channels; if(currentMusic[index].totalSamplesLeft <= 0) active = false; } - // TraceLog(DEBUG, "Streaming music data to buffer. Bytes streamed: %i", size); + TraceLog(DEBUG, "Buffering index:%i, chiptune:%i", index, (int)currentMusic[index].chipTune); } return active; -- cgit v1.2.3 From 8c5d403dda7a7070dc58204bb10539b3646f183b Mon Sep 17 00:00:00 2001 From: Joshua Reisenauer Date: Sat, 14 May 2016 15:26:17 -0700 Subject: new function to check if music stream is ready _g naming convention for globals, new error exit numbers. --- src/audio.c | 43 ++++++++++++++++++++++++++++++------------- 1 file changed, 30 insertions(+), 13 deletions(-) (limited to 'src') diff --git a/src/audio.c b/src/audio.c index c53d6318..0fd1bb13 100644 --- a/src/audio.c +++ b/src/audio.c @@ -112,7 +112,7 @@ typedef enum { INFO = 0, ERROR, WARNING, DEBUG, OTHER } TraceLogType; // Global Variables Definition //---------------------------------------------------------------------------------- static AudioContext_t* mixChannelsActive_g[MAX_AUDIO_CONTEXTS]; // What mix channels are currently active -static bool musicEnabled = false; +static bool musicEnabled_g = false; static Music currentMusic[MAX_MUSIC_STREAMS]; // Current music loaded, up to two can play at the same time //---------------------------------------------------------------------------------- @@ -126,8 +126,9 @@ static bool BufferMusicStream(int index); // Fill music buffers with da static void EmptyMusicStream(int index); // Empty music buffers static unsigned short FillAlBufferWithSilence(AudioContext_t *context, ALuint buffer);// fill buffer with zeros, returns number processed -static void ResampleShortToFloat(short *shorts, float *floats, unsigned short len); // pass two arrays of the same legnth in -static void ResampleByteToFloat(char *chars, float *floats, unsigned short len); // pass two arrays of same length in +static void ResampleShortToFloat(short *shorts, float *floats, unsigned short len); // pass two arrays of the same legnth in +static void ResampleByteToFloat(char *chars, float *floats, unsigned short len); // pass two arrays of same length in +static bool isMusicStreamReady(int index); // Checks if music buffer is ready to be refilled #if defined(AUDIO_STANDALONE) const char *GetExtension(const char *fileName); // Get the extension for a filename @@ -799,18 +800,21 @@ int PlayMusicStream(int musicIndex, char *fileName) TraceLog(DEBUG, "[%s] Temp memory required: %i", fileName, info.temp_memory_required); currentMusic[musicIndex].loop = true; // We loop by default - musicEnabled = true; - currentMusic[musicIndex].ctx->playing = true; + musicEnabled_g = true; + currentMusic[musicIndex].totalSamplesLeft = stb_vorbis_stream_length_in_samples(currentMusic[musicIndex].stream) * info.channels; currentMusic[musicIndex].totalLengthSeconds = stb_vorbis_stream_length_in_seconds(currentMusic[musicIndex].stream); if (info.channels == 2){ currentMusic[musicIndex].ctx = InitAudioContext(info.sample_rate, mixIndex, 2, false); + currentMusic[musicIndex].ctx->playing = true; } else{ currentMusic[musicIndex].ctx = InitAudioContext(info.sample_rate, mixIndex, 1, false); + currentMusic[musicIndex].ctx->playing = true; } + if(!currentMusic[musicIndex].ctx) return 4; // error } } else if (strcmp(GetExtension(fileName),"xm") == 0) @@ -823,24 +827,25 @@ int PlayMusicStream(int musicIndex, char *fileName) jar_xm_set_max_loop_count(currentMusic[musicIndex].chipctx, 0); // infinite number of loops currentMusic[musicIndex].totalSamplesLeft = jar_xm_get_remaining_samples(currentMusic[musicIndex].chipctx); currentMusic[musicIndex].totalLengthSeconds = ((float)currentMusic[musicIndex].totalSamplesLeft) / 48000.f; - musicEnabled = true; - currentMusic[musicIndex].ctx->playing = true; + musicEnabled_g = true; TraceLog(INFO, "[%s] XM number of samples: %i", fileName, currentMusic[musicIndex].totalSamplesLeft); TraceLog(INFO, "[%s] XM track length: %11.6f sec", fileName, currentMusic[musicIndex].totalLengthSeconds); currentMusic[musicIndex].ctx = InitAudioContext(48000, mixIndex, 2, true); + if(!currentMusic[musicIndex].ctx) return 5; // error + currentMusic[musicIndex].ctx->playing = true; } else { TraceLog(WARNING, "[%s] XM file could not be opened", fileName); - return 4; // error + return 6; // error } } else { TraceLog(WARNING, "[%s] Music extension not recognized, it can't be loaded", fileName); - return 5; // error + return 7; // error } return 0; // normal return } @@ -861,7 +866,7 @@ void StopMusicStream(int index) stb_vorbis_close(currentMusic[index].stream); } - if(!getMusicStreamCount()) musicEnabled = false; + if(!getMusicStreamCount()) musicEnabled_g = false; if(currentMusic[index].stream || currentMusic[index].chipctx) { currentMusic[index].stream = NULL; @@ -884,7 +889,7 @@ int getMusicStreamCount(void) void PauseMusicStream(int index) { // Pause music stream if music available! - if (currentMusic[index].ctx && musicEnabled) + if (currentMusic[index].ctx && musicEnabled_g) { TraceLog(INFO, "Pausing music stream"); alSourcePause(currentMusic[index].ctx->alSource); @@ -987,7 +992,7 @@ static bool BufferMusicStream(int index) int size = 0; // Total size of data steamed (in bytes) bool active = true; // We can get more data from stream (not finished) - if (musicEnabled) + if (musicEnabled_g) { if(!currentMusic[index].ctx->playing) { @@ -1031,13 +1036,25 @@ static void EmptyMusicStream(int index) } } +//determine if a music stream is ready to be written to +static bool isMusicStreamReady(int index) +{ + ALint processed = 0; + alGetSourcei(currentMusic[index].ctx->alSource, AL_BUFFERS_PROCESSED, &processed); + + if(processed) + return true; + + return false; +} + // Update (re-fill) music buffers if data already processed void UpdateMusicStream(int index) { ALenum state; bool active = true; - if (index < MAX_MUSIC_STREAMS && musicEnabled) + if (index < MAX_MUSIC_STREAMS && musicEnabled_g && isMusicStreamReady(index)) { active = BufferMusicStream(index); -- cgit v1.2.3 From d38d7a1bedadf7c4824c9bb1d4a9ecd3f0935b9c Mon Sep 17 00:00:00 2001 From: Joshua Reisenauer Date: Sat, 14 May 2016 16:30:32 -0700 Subject: clean up on buffering and preconditions --- src/audio.c | 89 ++++++++++++++++++++++++++++++++++--------------------------- 1 file changed, 50 insertions(+), 39 deletions(-) (limited to 'src') diff --git a/src/audio.c b/src/audio.c index 0fd1bb13..30e2343f 100644 --- a/src/audio.c +++ b/src/audio.c @@ -889,7 +889,7 @@ int getMusicStreamCount(void) void PauseMusicStream(int index) { // Pause music stream if music available! - if (currentMusic[index].ctx && musicEnabled_g) + if (index < MAX_MUSIC_STREAMS && currentMusic[index].ctx && musicEnabled_g) { TraceLog(INFO, "Pausing music stream"); alSourcePause(currentMusic[index].ctx->alSource); @@ -902,7 +902,7 @@ void ResumeMusicStream(int index) { // Resume music playing... if music available! ALenum state; - if(currentMusic[index].ctx){ + if(index < MAX_MUSIC_STREAMS && currentMusic[index].ctx){ alGetSourcei(currentMusic[index].ctx->alSource, AL_SOURCE_STATE, &state); if (state == AL_PAUSED) { @@ -962,17 +962,20 @@ float GetMusicTimeLength(int index) float GetMusicTimePlayed(int index) { float secondsPlayed; - if (currentMusic[index].chipTune) - { - uint64_t samples; - jar_xm_get_position(currentMusic[index].chipctx, NULL, NULL, NULL, &samples); - secondsPlayed = (float)samples / (48000 * currentMusic[index].ctx->channels); // Not sure if this is the correct value - } - else + if(index < MAX_MUSIC_STREAMS && currentMusic[index].ctx) { - int totalSamples = stb_vorbis_stream_length_in_samples(currentMusic[index].stream) * currentMusic[index].ctx->channels; - int samplesPlayed = totalSamples - currentMusic[index].totalSamplesLeft; - secondsPlayed = (float)samplesPlayed / (currentMusic[index].ctx->sampleRate * currentMusic[index].ctx->channels); + if (currentMusic[index].chipTune) + { + uint64_t samples; + jar_xm_get_position(currentMusic[index].chipctx, NULL, NULL, NULL, &samples); + secondsPlayed = (float)samples / (48000 * currentMusic[index].ctx->channels); // Not sure if this is the correct value + } + else + { + int totalSamples = stb_vorbis_stream_length_in_samples(currentMusic[index].stream) * currentMusic[index].ctx->channels; + int samplesPlayed = totalSamples - currentMusic[index].totalSamplesLeft; + secondsPlayed = (float)samplesPlayed / (currentMusic[index].ctx->sampleRate * currentMusic[index].ctx->channels); + } } @@ -989,33 +992,42 @@ static bool BufferMusicStream(int index) short pcm[MUSIC_BUFFER_SIZE_SHORT]; float pcmf[MUSIC_BUFFER_SIZE_FLOAT]; - int size = 0; // Total size of data steamed (in bytes) + int size = 0; // Total size of data steamed in L+R samples bool active = true; // We can get more data from stream (not finished) - if (musicEnabled_g) + + if(!currentMusic[index].ctx->playing && currentMusic[index].totalSamplesLeft > 0) { - if(!currentMusic[index].ctx->playing) - { - UpdateAudioContext(currentMusic[index].ctx, NULL, 0); - return false; - } - - if (currentMusic[index].chipTune) // There is no end of stream for xmfiles, once the end is reached zeros are generated for non looped chiptunes. - { - jar_xm_generate_samples(currentMusic[index].chipctx, pcmf, MUSIC_BUFFER_SIZE_FLOAT / 2); // reads 2*readlen shorts and moves them to buffer+size memory location - UpdateAudioContext(currentMusic[index].ctx, pcmf, MUSIC_BUFFER_SIZE_FLOAT); - currentMusic[index].totalSamplesLeft -= MUSIC_BUFFER_SIZE_FLOAT; - if(currentMusic[index].totalSamplesLeft <= 0) active = false; - } + UpdateAudioContext(currentMusic[index].ctx, NULL, 0); + return true; // it is still active, only it is paused + } + + + if (currentMusic[index].chipTune) // There is no end of stream for xmfiles, once the end is reached zeros are generated for non looped chiptunes. + { + if(currentMusic[index].totalSamplesLeft >= MUSIC_BUFFER_SIZE_FLOAT / 2) + size = MUSIC_BUFFER_SIZE_FLOAT / 2; else - { - int streamedBytes = stb_vorbis_get_samples_short_interleaved(currentMusic[index].stream, currentMusic[index].ctx->channels, pcm, MUSIC_BUFFER_SIZE_SHORT); - UpdateAudioContext(currentMusic[index].ctx, pcm, streamedBytes); - currentMusic[index].totalSamplesLeft -= streamedBytes*currentMusic[index].ctx->channels; - if(currentMusic[index].totalSamplesLeft <= 0) active = false; - } - TraceLog(DEBUG, "Buffering index:%i, chiptune:%i", index, (int)currentMusic[index].chipTune); + size = currentMusic[index].totalSamplesLeft / 2; + + jar_xm_generate_samples(currentMusic[index].chipctx, pcmf, size); // reads 2*readlen shorts and moves them to buffer+size memory location + UpdateAudioContext(currentMusic[index].ctx, pcmf, size * 2); + currentMusic[index].totalSamplesLeft -= size * 2; } + else + { + if(currentMusic[index].totalSamplesLeft >= MUSIC_BUFFER_SIZE_SHORT) + size = MUSIC_BUFFER_SIZE_SHORT; + else + size = currentMusic[index].totalSamplesLeft; + + int streamedBytes = stb_vorbis_get_samples_short_interleaved(currentMusic[index].stream, currentMusic[index].ctx->channels, pcm, size); + UpdateAudioContext(currentMusic[index].ctx, pcm, streamedBytes * currentMusic[index].ctx->channels); + currentMusic[index].totalSamplesLeft -= streamedBytes * currentMusic[index].ctx->channels; + } + + TraceLog(DEBUG, "Buffering index:%i, chiptune:%i", index, (int)currentMusic[index].chipTune); + if(currentMusic[index].totalSamplesLeft <= 0) active = false; return active; } @@ -1042,8 +1054,7 @@ static bool isMusicStreamReady(int index) ALint processed = 0; alGetSourcei(currentMusic[index].ctx->alSource, AL_BUFFERS_PROCESSED, &processed); - if(processed) - return true; + if(processed) return true; return false; } @@ -1054,11 +1065,11 @@ void UpdateMusicStream(int index) ALenum state; bool active = true; - if (index < MAX_MUSIC_STREAMS && musicEnabled_g && isMusicStreamReady(index)) + if (index < MAX_MUSIC_STREAMS && musicEnabled_g && currentMusic[index].ctx && isMusicStreamReady(index)) { active = BufferMusicStream(index); - if ((!active) && (currentMusic[index].loop)) + if (!active && currentMusic[index].loop && currentMusic[index].ctx->playing) { if(currentMusic[index].chipTune) { @@ -1067,7 +1078,7 @@ void UpdateMusicStream(int index) else { stb_vorbis_seek_start(currentMusic[index].stream); - currentMusic[index].totalSamplesLeft = stb_vorbis_stream_length_in_samples(currentMusic[index].stream)*currentMusic[index].ctx->channels; + currentMusic[index].totalSamplesLeft = stb_vorbis_stream_length_in_samples(currentMusic[index].stream) * currentMusic[index].ctx->channels; } active = BufferMusicStream(index); } -- cgit v1.2.3 From 86fbf4fd8f0cd5301259b2115125c45c9872c02a Mon Sep 17 00:00:00 2001 From: Joshua Reisenauer Date: Sun, 15 May 2016 02:09:57 -0700 Subject: logic bug fix --- src/audio.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/audio.c b/src/audio.c index 30e2343f..cc5ca1a2 100644 --- a/src/audio.c +++ b/src/audio.c @@ -996,10 +996,10 @@ static bool BufferMusicStream(int index) bool active = true; // We can get more data from stream (not finished) - if(!currentMusic[index].ctx->playing && currentMusic[index].totalSamplesLeft > 0) + if (!currentMusic[index].ctx->playing && currentMusic[index].totalSamplesLeft > 0) { UpdateAudioContext(currentMusic[index].ctx, NULL, 0); - return true; // it is still active, only it is paused + return true; // it is still active but it is paused } @@ -1071,7 +1071,7 @@ void UpdateMusicStream(int index) if (!active && currentMusic[index].loop && currentMusic[index].ctx->playing) { - if(currentMusic[index].chipTune) + if (currentMusic[index].chipTune) { currentMusic[index].totalSamplesLeft = currentMusic[index].totalLengthSeconds * currentMusic[index].ctx->sampleRate; } @@ -1080,7 +1080,7 @@ void UpdateMusicStream(int index) stb_vorbis_seek_start(currentMusic[index].stream); currentMusic[index].totalSamplesLeft = stb_vorbis_stream_length_in_samples(currentMusic[index].stream) * currentMusic[index].ctx->channels; } - active = BufferMusicStream(index); + active = true; } @@ -1088,7 +1088,7 @@ void UpdateMusicStream(int index) alGetSourcei(currentMusic[index].ctx->alSource, AL_SOURCE_STATE, &state); - if ((state != AL_PLAYING) && active) alSourcePlay(currentMusic[index].ctx->alSource); + if (state != AL_PLAYING && active && currentMusic[index].ctx->playing) alSourcePlay(currentMusic[index].ctx->alSource); if (!active) StopMusicStream(index); -- cgit v1.2.3 From 76ff4d220ee735b8b86bd4dae776665cf68e4fb4 Mon Sep 17 00:00:00 2001 From: Joshua Reisenauer Date: Sun, 15 May 2016 19:37:15 -0700 Subject: renamed everything so it is obvious what it does --- src/audio.c | 380 ++++++++++++++++++++++++++++------------------------------- src/audio.h | 15 +-- src/raylib.h | 15 +-- 3 files changed, 190 insertions(+), 220 deletions(-) (limited to 'src') diff --git a/src/audio.c b/src/audio.c index cc5ca1a2..584d3ad1 100644 --- a/src/audio.c +++ b/src/audio.c @@ -77,10 +77,10 @@ // Types and Structures Definition //---------------------------------------------------------------------------------- -// Audio Context, used to create custom audio streams that are not bound to a sound file. There can be -// no more than 4 concurrent audio contexts in use. This is due to each active context being tied to -// a dedicated mix channel. All audio is 32bit floating point in stereo. -typedef struct AudioContext_t { +// Used to create custom audio streams that are not bound to a specific file. There can be +// no more than 4 concurrent mixchannels in use. This is due to each active mixc being tied to +// a dedicated mix channel. +typedef struct MixChannel_t { unsigned short sampleRate; // default is 48000 unsigned char channels; // 1=mono,2=stereo unsigned char mixChannel; // 0-3 or mixA-mixD, each mix channel can receive up to one dedicated audio stream @@ -89,14 +89,14 @@ typedef struct AudioContext_t { ALenum alFormat; // openAL format specifier ALuint alSource; // openAL source ALuint alBuffer[MAX_STREAM_BUFFERS]; // openAL sample buffer -} AudioContext_t; +} MixChannel_t; // Music type (file streaming from memory) -// NOTE: Anything longer than ~10 seconds should be streamed... +// NOTE: Anything longer than ~10 seconds should be streamed into a mix channel... typedef struct Music { stb_vorbis *stream; - jar_xm_context_t *chipctx; // Stores jar_xm context - AudioContext_t *ctx; // audio context + jar_xm_context_t *chipctx; // Stores jar_xm mixc + MixChannel_t *mixc; // mix channel int totalSamplesLeft; float totalLengthSeconds; @@ -111,9 +111,9 @@ typedef enum { INFO = 0, ERROR, WARNING, DEBUG, OTHER } TraceLogType; //---------------------------------------------------------------------------------- // Global Variables Definition //---------------------------------------------------------------------------------- -static AudioContext_t* mixChannelsActive_g[MAX_AUDIO_CONTEXTS]; // What mix channels are currently active +static MixChannel_t* mixChannelsActive_g[MAX_AUDIO_CONTEXTS]; // What mix channels are currently active static bool musicEnabled_g = false; -static Music currentMusic[MAX_MUSIC_STREAMS]; // Current music loaded, up to two can play at the same time +static Music currentMusic[MAX_MUSIC_STREAMS]; // Current music loaded, up to two can play at the same time //---------------------------------------------------------------------------------- // Module specific Functions Declaration @@ -122,13 +122,17 @@ static Wave LoadWAV(const char *fileName); // Load WAV file static Wave LoadOGG(char *fileName); // Load OGG file static void UnloadWave(Wave wave); // Unload wave data -static bool BufferMusicStream(int index); // Fill music buffers with data -static void EmptyMusicStream(int index); // Empty music buffers +static bool BufferMusicStream(int index, int numBuffers); // Fill music buffers with data +static void EmptyMusicStream(int index); // Empty music buffers -static unsigned short FillAlBufferWithSilence(AudioContext_t *context, ALuint buffer);// fill buffer with zeros, returns number processed -static void ResampleShortToFloat(short *shorts, float *floats, unsigned short len); // pass two arrays of the same legnth in -static void ResampleByteToFloat(char *chars, float *floats, unsigned short len); // pass two arrays of same length in -static bool isMusicStreamReady(int index); // Checks if music buffer is ready to be refilled + +static MixChannel_t* InitMixChannel(unsigned short sampleRate, unsigned char mixChannel, unsigned char channels, bool floatingPoint); // For streaming into mix channels. +static void CloseMixChannel(MixChannel_t* mixc); // Frees mix channel +static unsigned short BufferMixChannel(MixChannel_t* mixc, void *data, int numberElements); // Pushes more audio data into mixc mix channel, if NULL is passed it pauses +static unsigned short FillAlBufferWithSilence(MixChannel_t *mixc, ALuint buffer); // Fill buffer with zeros, returns number processed +static void ResampleShortToFloat(short *shorts, float *floats, unsigned short len); // Pass two arrays of the same legnth in +static void ResampleByteToFloat(char *chars, float *floats, unsigned short len); // Pass two arrays of same length in +static int IsMusicStreamReadyForBuffering(int index); // Checks if music buffer is ready to be refilled #if defined(AUDIO_STANDALONE) const char *GetExtension(const char *fileName); // Get the extension for a filename @@ -139,7 +143,7 @@ void TraceLog(int msgType, const char *text, ...); // Outputs a trace log messa // Module Functions Definition - Audio Device initialization and Closing //---------------------------------------------------------------------------------- -// Initialize audio device and context +// Initialize audio device and mixc void InitAudioDevice(void) { // Open and initialize a device with default settings @@ -155,7 +159,7 @@ void InitAudioDevice(void) alcCloseDevice(device); - TraceLog(ERROR, "Could not setup audio context"); + TraceLog(ERROR, "Could not setup mix channel"); } TraceLog(INFO, "Audio device and context initialized successfully: %s", alcGetString(device, ALC_DEVICE_SPECIFIER)); @@ -171,14 +175,14 @@ void CloseAudioDevice(void) { for(int index=0; index= MAX_AUDIO_CONTEXTS) return NULL; if(!IsAudioDeviceReady()) InitAudioDevice(); if(!mixChannelsActive_g[mixChannel]){ - AudioContext_t *ac = (AudioContext_t*)malloc(sizeof(AudioContext_t)); - ac->sampleRate = sampleRate; - ac->channels = channels; - ac->mixChannel = mixChannel; - ac->floatingPoint = floatingPoint; - mixChannelsActive_g[mixChannel] = ac; + MixChannel_t *mixc = (MixChannel_t*)malloc(sizeof(MixChannel_t)); + mixc->sampleRate = sampleRate; + mixc->channels = channels; + mixc->mixChannel = mixChannel; + mixc->floatingPoint = floatingPoint; + mixChannelsActive_g[mixChannel] = mixc; // setup openAL format if(channels == 1) { if(floatingPoint) - ac->alFormat = AL_FORMAT_MONO_FLOAT32; + mixc->alFormat = AL_FORMAT_MONO_FLOAT32; else - ac->alFormat = AL_FORMAT_MONO16; + mixc->alFormat = AL_FORMAT_MONO16; } else if(channels == 2) { if(floatingPoint) - ac->alFormat = AL_FORMAT_STEREO_FLOAT32; + mixc->alFormat = AL_FORMAT_STEREO_FLOAT32; else - ac->alFormat = AL_FORMAT_STEREO16; + mixc->alFormat = AL_FORMAT_STEREO16; } // Create an audio source - alGenSources(1, &ac->alSource); - alSourcef(ac->alSource, AL_PITCH, 1); - alSourcef(ac->alSource, AL_GAIN, 1); - alSource3f(ac->alSource, AL_POSITION, 0, 0, 0); - alSource3f(ac->alSource, AL_VELOCITY, 0, 0, 0); + alGenSources(1, &mixc->alSource); + alSourcef(mixc->alSource, AL_PITCH, 1); + alSourcef(mixc->alSource, AL_GAIN, 1); + alSource3f(mixc->alSource, AL_POSITION, 0, 0, 0); + alSource3f(mixc->alSource, AL_VELOCITY, 0, 0, 0); // Create Buffer - alGenBuffers(MAX_STREAM_BUFFERS, ac->alBuffer); + alGenBuffers(MAX_STREAM_BUFFERS, mixc->alBuffer); //fill buffers int x; for(x=0;xalBuffer[x]); + FillAlBufferWithSilence(mixc, mixc->alBuffer[x]); - alSourceQueueBuffers(ac->alSource, MAX_STREAM_BUFFERS, ac->alBuffer); - alSourcePlay(ac->alSource); - ac->playing = true; + alSourceQueueBuffers(mixc->alSource, MAX_STREAM_BUFFERS, mixc->alBuffer); + mixc->playing = true; + alSourcePlay(mixc->alSource); - return ac; + return mixc; } return NULL; } -// Frees buffer in audio context -void CloseAudioContext(AudioContext ctx) +// Frees buffer in mix channel +static void CloseMixChannel(MixChannel_t* mixc) { - AudioContext_t *context = (AudioContext_t*)ctx; - if(context){ - alSourceStop(context->alSource); - context->playing = false; + if(mixc){ + alSourceStop(mixc->alSource); + mixc->playing = false; //flush out all queued buffers ALuint buffer = 0; int queued = 0; - alGetSourcei(context->alSource, AL_BUFFERS_QUEUED, &queued); + alGetSourcei(mixc->alSource, AL_BUFFERS_QUEUED, &queued); while (queued > 0) { - alSourceUnqueueBuffers(context->alSource, 1, &buffer); + alSourceUnqueueBuffers(mixc->alSource, 1, &buffer); queued--; } //delete source and buffers - alDeleteSources(1, &context->alSource); - alDeleteBuffers(MAX_STREAM_BUFFERS, context->alBuffer); - mixChannelsActive_g[context->mixChannel] = NULL; - free(context); - ctx = NULL; + alDeleteSources(1, &mixc->alSource); + alDeleteBuffers(MAX_STREAM_BUFFERS, mixc->alBuffer); + mixChannelsActive_g[mixc->mixChannel] = NULL; + free(mixc); + mixc = NULL; } } -// Pushes more audio data into context mix channel, if none are ever pushed then zeros are fed in. -// Call "UpdateAudioContext(ctx, NULL, 0)" if you want to pause the audio. +// Pushes more audio data into mixc mix channel, only one buffer per call +// Call "BufferMixChannel(mixc, NULL, 0)" if you want to pause the audio. // @Returns number of samples that where processed. -unsigned short UpdateAudioContext(AudioContext ctx, void *data, unsigned short numberElements) +static unsigned short BufferMixChannel(MixChannel_t* mixc, void *data, int numberElements) { - AudioContext_t *context = (AudioContext_t*)ctx; - - if(!context || (context->channels == 2 && numberElements % 2 != 0)) return 0; // when there is two channels there must be an even number of samples + if(!mixc || mixChannelsActive_g[mixc->mixChannel] != mixc) return 0; // when there is two channels there must be an even number of samples if (!data || !numberElements) { // pauses audio until data is given - alSourcePause(context->alSource); - context->playing = false; + if(mixc->playing){ + alSourcePause(mixc->alSource); + mixc->playing = false; + } return 0; } - else + else if(!mixc->playing) { // restart audio otherwise - ALint state; - alGetSourcei(context->alSource, AL_SOURCE_STATE, &state); - if (state != AL_PLAYING){ - alSourcePlay(context->alSource); - context->playing = true; - } + alSourcePlay(mixc->alSource); + mixc->playing = true; } - if (context && context->playing && mixChannelsActive_g[context->mixChannel] == context) + + ALuint buffer = 0; + + alSourceUnqueueBuffers(mixc->alSource, 1, &buffer); + if(!buffer) return 0; + if(mixc->floatingPoint) // process float buffers { - ALint processed = 0; - ALuint buffer = 0; - unsigned short numberProcessed = 0; - unsigned short numberRemaining = numberElements; - - - alGetSourcei(context->alSource, AL_BUFFERS_PROCESSED, &processed); // Get the number of already processed buffers (if any) - if(!processed) return 0; // nothing to process, queue is still full - - - while (processed > 0) - { - if(context->floatingPoint) // process float buffers - { - float *ptr = (float*)data; - alSourceUnqueueBuffers(context->alSource, 1, &buffer); - if(numberRemaining >= MUSIC_BUFFER_SIZE_FLOAT) - { - alBufferData(buffer, context->alFormat, &ptr[numberProcessed], MUSIC_BUFFER_SIZE_FLOAT*sizeof(float), context->sampleRate); - numberProcessed+=MUSIC_BUFFER_SIZE_FLOAT; - numberRemaining-=MUSIC_BUFFER_SIZE_FLOAT; - } - else - { - alBufferData(buffer, context->alFormat, &ptr[numberProcessed], numberRemaining*sizeof(float), context->sampleRate); - numberProcessed+=numberRemaining; - numberRemaining=0; - } - alSourceQueueBuffers(context->alSource, 1, &buffer); - processed--; - } - else if(!context->floatingPoint) // process short buffers - { - short *ptr = (short*)data; - alSourceUnqueueBuffers(context->alSource, 1, &buffer); - if(numberRemaining >= MUSIC_BUFFER_SIZE_SHORT) - { - alBufferData(buffer, context->alFormat, &ptr[numberProcessed], MUSIC_BUFFER_SIZE_FLOAT*sizeof(short), context->sampleRate); - numberProcessed+=MUSIC_BUFFER_SIZE_SHORT; - numberRemaining-=MUSIC_BUFFER_SIZE_SHORT; - } - else - { - alBufferData(buffer, context->alFormat, &ptr[numberProcessed], numberRemaining*sizeof(short), context->sampleRate); - numberProcessed+=numberRemaining; - numberRemaining=0; - } - alSourceQueueBuffers(context->alSource, 1, &buffer); - processed--; - } - else - break; - } - return numberProcessed; + float *ptr = (float*)data; + alBufferData(buffer, mixc->alFormat, ptr, numberElements*sizeof(float), mixc->sampleRate); + } + else // process short buffers + { + short *ptr = (short*)data; + alBufferData(buffer, mixc->alFormat, ptr, numberElements*sizeof(short), mixc->sampleRate); } - return 0; + alSourceQueueBuffers(mixc->alSource, 1, &buffer); + + return numberElements; } // fill buffer with zeros, returns number processed -static unsigned short FillAlBufferWithSilence(AudioContext_t *context, ALuint buffer) +static unsigned short FillAlBufferWithSilence(MixChannel_t *mixc, ALuint buffer) { - if(context->floatingPoint){ + if(mixc->floatingPoint){ float pcm[MUSIC_BUFFER_SIZE_FLOAT] = {0.f}; - alBufferData(buffer, context->alFormat, pcm, MUSIC_BUFFER_SIZE_FLOAT*sizeof(float), context->sampleRate); + alBufferData(buffer, mixc->alFormat, pcm, MUSIC_BUFFER_SIZE_FLOAT*sizeof(float), mixc->sampleRate); return MUSIC_BUFFER_SIZE_FLOAT; } else { short pcm[MUSIC_BUFFER_SIZE_SHORT] = {0}; - alBufferData(buffer, context->alFormat, pcm, MUSIC_BUFFER_SIZE_SHORT*sizeof(short), context->sampleRate); + alBufferData(buffer, mixc->alFormat, pcm, MUSIC_BUFFER_SIZE_SHORT*sizeof(short), mixc->sampleRate); return MUSIC_BUFFER_SIZE_SHORT; } } @@ -417,6 +376,28 @@ static void ResampleByteToFloat(char *chars, float *floats, unsigned short len) } } +// used to output raw audio streams, returns negative numbers on error +RawAudioContext InitRawAudioContext(int sampleRate, int channels, bool floatingPoint) +{ + int mixIndex; + for(mixIndex = 0; mixIndex < MAX_AUDIO_CONTEXTS; mixIndex++) // find empty mix channel slot + { + if(mixChannelsActive_g[mixIndex] == NULL) break; + else if(mixIndex = MAX_AUDIO_CONTEXTS - 1) return -1; // error + } + + if(InitMixChannel(sampleRate, mixIndex, channels, floatingPoint)) + return mixIndex; + else + return -2; // error +} + +void CloseRawAudioContext(RawAudioContext ctx) +{ + if(mixChannelsActive_g[ctx]) + CloseMixChannel(mixChannelsActive_g[ctx]); +} + //---------------------------------------------------------------------------------- @@ -807,14 +788,14 @@ int PlayMusicStream(int musicIndex, char *fileName) currentMusic[musicIndex].totalLengthSeconds = stb_vorbis_stream_length_in_seconds(currentMusic[musicIndex].stream); if (info.channels == 2){ - currentMusic[musicIndex].ctx = InitAudioContext(info.sample_rate, mixIndex, 2, false); - currentMusic[musicIndex].ctx->playing = true; + currentMusic[musicIndex].mixc = InitMixChannel(info.sample_rate, mixIndex, 2, false); + currentMusic[musicIndex].mixc->playing = true; } else{ - currentMusic[musicIndex].ctx = InitAudioContext(info.sample_rate, mixIndex, 1, false); - currentMusic[musicIndex].ctx->playing = true; + currentMusic[musicIndex].mixc = InitMixChannel(info.sample_rate, mixIndex, 1, false); + currentMusic[musicIndex].mixc->playing = true; } - if(!currentMusic[musicIndex].ctx) return 4; // error + if(!currentMusic[musicIndex].mixc) return 4; // error } } else if (strcmp(GetExtension(fileName),"xm") == 0) @@ -832,9 +813,9 @@ int PlayMusicStream(int musicIndex, char *fileName) TraceLog(INFO, "[%s] XM number of samples: %i", fileName, currentMusic[musicIndex].totalSamplesLeft); TraceLog(INFO, "[%s] XM track length: %11.6f sec", fileName, currentMusic[musicIndex].totalLengthSeconds); - currentMusic[musicIndex].ctx = InitAudioContext(48000, mixIndex, 2, true); - if(!currentMusic[musicIndex].ctx) return 5; // error - currentMusic[musicIndex].ctx->playing = true; + currentMusic[musicIndex].mixc = InitMixChannel(48000, mixIndex, 2, false); + if(!currentMusic[musicIndex].mixc) return 5; // error + currentMusic[musicIndex].mixc->playing = true; } else { @@ -853,9 +834,9 @@ int PlayMusicStream(int musicIndex, char *fileName) // Stop music playing for individual music index of currentMusic array (close stream) void StopMusicStream(int index) { - if (index < MAX_MUSIC_STREAMS && currentMusic[index].ctx) + if (index < MAX_MUSIC_STREAMS && currentMusic[index].mixc) { - CloseAudioContext(currentMusic[index].ctx); + CloseMixChannel(currentMusic[index].mixc); if (currentMusic[index].chipTune) { @@ -889,11 +870,11 @@ int getMusicStreamCount(void) void PauseMusicStream(int index) { // Pause music stream if music available! - if (index < MAX_MUSIC_STREAMS && currentMusic[index].ctx && musicEnabled_g) + if (index < MAX_MUSIC_STREAMS && currentMusic[index].mixc && musicEnabled_g) { TraceLog(INFO, "Pausing music stream"); - alSourcePause(currentMusic[index].ctx->alSource); - currentMusic[index].ctx->playing = false; + alSourcePause(currentMusic[index].mixc->alSource); + currentMusic[index].mixc->playing = false; } } @@ -902,13 +883,13 @@ void ResumeMusicStream(int index) { // Resume music playing... if music available! ALenum state; - if(index < MAX_MUSIC_STREAMS && currentMusic[index].ctx){ - alGetSourcei(currentMusic[index].ctx->alSource, AL_SOURCE_STATE, &state); + if(index < MAX_MUSIC_STREAMS && currentMusic[index].mixc){ + alGetSourcei(currentMusic[index].mixc->alSource, AL_SOURCE_STATE, &state); if (state == AL_PAUSED) { TraceLog(INFO, "Resuming music stream"); - alSourcePlay(currentMusic[index].ctx->alSource); - currentMusic[index].ctx->playing = true; + alSourcePlay(currentMusic[index].mixc->alSource); + currentMusic[index].mixc->playing = true; } } } @@ -919,8 +900,8 @@ bool IsMusicPlaying(int index) bool playing = false; ALint state; - if(index < MAX_MUSIC_STREAMS && currentMusic[index].ctx){ - alGetSourcei(currentMusic[index].ctx->alSource, AL_SOURCE_STATE, &state); + if(index < MAX_MUSIC_STREAMS && currentMusic[index].mixc){ + alGetSourcei(currentMusic[index].mixc->alSource, AL_SOURCE_STATE, &state); if (state == AL_PLAYING) playing = true; } @@ -930,15 +911,15 @@ bool IsMusicPlaying(int index) // Set volume for music void SetMusicVolume(int index, float volume) { - if(index < MAX_MUSIC_STREAMS && currentMusic[index].ctx){ - alSourcef(currentMusic[index].ctx->alSource, AL_GAIN, volume); + if(index < MAX_MUSIC_STREAMS && currentMusic[index].mixc){ + alSourcef(currentMusic[index].mixc->alSource, AL_GAIN, volume); } } void SetMusicPitch(int index, float pitch) { - if(index < MAX_MUSIC_STREAMS && currentMusic[index].ctx){ - alSourcef(currentMusic[index].ctx->alSource, AL_PITCH, pitch); + if(index < MAX_MUSIC_STREAMS && currentMusic[index].mixc){ + alSourcef(currentMusic[index].mixc->alSource, AL_PITCH, pitch); } } @@ -962,19 +943,19 @@ float GetMusicTimeLength(int index) float GetMusicTimePlayed(int index) { float secondsPlayed; - if(index < MAX_MUSIC_STREAMS && currentMusic[index].ctx) + if(index < MAX_MUSIC_STREAMS && currentMusic[index].mixc) { if (currentMusic[index].chipTune) { uint64_t samples; jar_xm_get_position(currentMusic[index].chipctx, NULL, NULL, NULL, &samples); - secondsPlayed = (float)samples / (48000 * currentMusic[index].ctx->channels); // Not sure if this is the correct value + secondsPlayed = (float)samples / (48000 * currentMusic[index].mixc->channels); // Not sure if this is the correct value } else { - int totalSamples = stb_vorbis_stream_length_in_samples(currentMusic[index].stream) * currentMusic[index].ctx->channels; + int totalSamples = stb_vorbis_stream_length_in_samples(currentMusic[index].stream) * currentMusic[index].mixc->channels; int samplesPlayed = totalSamples - currentMusic[index].totalSamplesLeft; - secondsPlayed = (float)samplesPlayed / (currentMusic[index].ctx->sampleRate * currentMusic[index].ctx->channels); + secondsPlayed = (float)samplesPlayed / (currentMusic[index].mixc->sampleRate * currentMusic[index].mixc->channels); } } @@ -987,32 +968,32 @@ float GetMusicTimePlayed(int index) //---------------------------------------------------------------------------------- // Fill music buffers with new data from music stream -static bool BufferMusicStream(int index) +static bool BufferMusicStream(int index, int numBuffers) { short pcm[MUSIC_BUFFER_SIZE_SHORT]; float pcmf[MUSIC_BUFFER_SIZE_FLOAT]; - int size = 0; // Total size of data steamed in L+R samples + int size = 0; // Total size of data steamed in L+R samples for xm floats, individual L or R for ogg shorts bool active = true; // We can get more data from stream (not finished) - - - if (!currentMusic[index].ctx->playing && currentMusic[index].totalSamplesLeft > 0) - { - UpdateAudioContext(currentMusic[index].ctx, NULL, 0); - return true; // it is still active but it is paused - } - if (currentMusic[index].chipTune) // There is no end of stream for xmfiles, once the end is reached zeros are generated for non looped chiptunes. { - if(currentMusic[index].totalSamplesLeft >= MUSIC_BUFFER_SIZE_FLOAT / 2) - size = MUSIC_BUFFER_SIZE_FLOAT / 2; + if(currentMusic[index].totalSamplesLeft >= MUSIC_BUFFER_SIZE_SHORT) + size = MUSIC_BUFFER_SIZE_SHORT / 2; else size = currentMusic[index].totalSamplesLeft / 2; - - jar_xm_generate_samples(currentMusic[index].chipctx, pcmf, size); // reads 2*readlen shorts and moves them to buffer+size memory location - UpdateAudioContext(currentMusic[index].ctx, pcmf, size * 2); - currentMusic[index].totalSamplesLeft -= size * 2; + + for(int x=0; xchannels, pcm, size); - UpdateAudioContext(currentMusic[index].ctx, pcm, streamedBytes * currentMusic[index].ctx->channels); - currentMusic[index].totalSamplesLeft -= streamedBytes * currentMusic[index].ctx->channels; + for(int x=0; xchannels, pcm, size); + BufferMixChannel(currentMusic[index].mixc, pcm, streamedBytes * currentMusic[index].mixc->channels); + currentMusic[index].totalSamplesLeft -= streamedBytes * currentMusic[index].mixc->channels; + if(currentMusic[index].totalSamplesLeft <= 0) + { + active = false; + break; + } + } } - - TraceLog(DEBUG, "Buffering index:%i, chiptune:%i", index, (int)currentMusic[index].chipTune); - if(currentMusic[index].totalSamplesLeft <= 0) active = false; return active; } @@ -1038,25 +1024,22 @@ static void EmptyMusicStream(int index) ALuint buffer = 0; int queued = 0; - alGetSourcei(currentMusic[index].ctx->alSource, AL_BUFFERS_QUEUED, &queued); + alGetSourcei(currentMusic[index].mixc->alSource, AL_BUFFERS_QUEUED, &queued); while (queued > 0) { - alSourceUnqueueBuffers(currentMusic[index].ctx->alSource, 1, &buffer); + alSourceUnqueueBuffers(currentMusic[index].mixc->alSource, 1, &buffer); queued--; } } //determine if a music stream is ready to be written to -static bool isMusicStreamReady(int index) +static int IsMusicStreamReadyForBuffering(int index) { ALint processed = 0; - alGetSourcei(currentMusic[index].ctx->alSource, AL_BUFFERS_PROCESSED, &processed); - - if(processed) return true; - - return false; + alGetSourcei(currentMusic[index].mixc->alSource, AL_BUFFERS_PROCESSED, &processed); + return processed; } // Update (re-fill) music buffers if data already processed @@ -1064,21 +1047,22 @@ void UpdateMusicStream(int index) { ALenum state; bool active = true; - - if (index < MAX_MUSIC_STREAMS && musicEnabled_g && currentMusic[index].ctx && isMusicStreamReady(index)) + int numBuffers = IsMusicStreamReadyForBuffering(index); + + if (currentMusic[index].mixc->playing && index < MAX_MUSIC_STREAMS && musicEnabled_g && currentMusic[index].mixc && numBuffers) { - active = BufferMusicStream(index); + active = BufferMusicStream(index, numBuffers); - if (!active && currentMusic[index].loop && currentMusic[index].ctx->playing) + if (!active && currentMusic[index].loop) { if (currentMusic[index].chipTune) { - currentMusic[index].totalSamplesLeft = currentMusic[index].totalLengthSeconds * currentMusic[index].ctx->sampleRate; + currentMusic[index].totalSamplesLeft = currentMusic[index].totalLengthSeconds * 48000; } else { stb_vorbis_seek_start(currentMusic[index].stream); - currentMusic[index].totalSamplesLeft = stb_vorbis_stream_length_in_samples(currentMusic[index].stream) * currentMusic[index].ctx->channels; + currentMusic[index].totalSamplesLeft = stb_vorbis_stream_length_in_samples(currentMusic[index].stream) * currentMusic[index].mixc->channels; } active = true; } @@ -1086,9 +1070,9 @@ void UpdateMusicStream(int index) if (alGetError() != AL_NO_ERROR) TraceLog(WARNING, "Error buffering data..."); - alGetSourcei(currentMusic[index].ctx->alSource, AL_SOURCE_STATE, &state); + alGetSourcei(currentMusic[index].mixc->alSource, AL_SOURCE_STATE, &state); - if (state != AL_PLAYING && active && currentMusic[index].ctx->playing) alSourcePlay(currentMusic[index].ctx->alSource); + if (state != AL_PLAYING && active) alSourcePlay(currentMusic[index].mixc->alSource); if (!active) StopMusicStream(index); diff --git a/src/audio.h b/src/audio.h index 63c1c136..d3276bf6 100644 --- a/src/audio.h +++ b/src/audio.h @@ -61,10 +61,7 @@ typedef struct Wave { short channels; } Wave; -// Audio Context, used to create custom audio streams that are not bound to a sound file. There can be -// no more than 4 concurrent audio contexts in use. This is due to each active context being tied to -// a dedicated mix channel. -typedef void* AudioContext; +typedef int RawAudioContext; #ifdef __cplusplus extern "C" { // Prevents name mangling of functions @@ -82,13 +79,6 @@ void InitAudioDevice(void); // Initialize au void CloseAudioDevice(void); // Close the audio device and context (and music stream) bool IsAudioDeviceReady(void); // True if call to InitAudioDevice() was successful and CloseAudioDevice() has not been called yet -// Audio contexts are for outputing custom audio waveforms, This will shut down any other sound sources currently playing -// The mixChannel is what mix channel you want to operate on, 0-3 are the ones available. Each mix channel can only be used one at a time. -// exmple usage is InitAudioContext(48000, 0, 2, true); // mixchannel 1, 48khz, stereo, floating point -AudioContext InitAudioContext(unsigned short sampleRate, unsigned char mixChannel, unsigned char channels, bool floatingPoint); -void CloseAudioContext(AudioContext ctx); // Frees audio context -unsigned short UpdateAudioContext(AudioContext ctx, void *data, unsigned short numberElements); // Pushes more audio data into context mix channel, if NULL is passed to data then zeros are played - Sound LoadSound(char *fileName); // Load sound to memory Sound LoadSoundFromWave(Wave wave); // Load sound to memory from wave data Sound LoadSoundFromRES(const char *rresName, int resId); // Load sound to memory from rRES file (raylib Resource) @@ -112,6 +102,9 @@ float GetMusicTimePlayed(int index); // Get current m int getMusicStreamCount(void); void SetMusicPitch(int index, float pitch); +RawAudioContext InitRawAudioContext(int sampleRate, int channels, bool floatingPoint); +void CloseRawAudioContext(RawAudioContext ctx); + #ifdef __cplusplus } #endif diff --git a/src/raylib.h b/src/raylib.h index ea9fbfcb..6efde710 100644 --- a/src/raylib.h +++ b/src/raylib.h @@ -451,10 +451,7 @@ typedef struct Wave { short channels; } Wave; -// Audio Context, used to create custom audio streams that are not bound to a sound file. There can be -// no more than 4 concurrent audio contexts in use. This is due to each active context being tied to -// a dedicated mix channel. -typedef void* AudioContext; +typedef int RawAudioContext; // Texture formats // NOTE: Support depends on OpenGL version and platform @@ -876,13 +873,6 @@ void InitAudioDevice(void); // Initialize au void CloseAudioDevice(void); // Close the audio device and context (and music stream) bool IsAudioDeviceReady(void); // True if call to InitAudioDevice() was successful and CloseAudioDevice() has not been called yet -// Audio contexts are for outputing custom audio waveforms, This will shut down any other sound sources currently playing -// The mixChannel is what mix channel you want to operate on, 0-3 are the ones available. Each mix channel can only be used one at a time. -// exmple usage is InitAudioContext(48000, 0, 2, true); // mixchannel 1, 48khz, stereo, floating point -AudioContext InitAudioContext(unsigned short sampleRate, unsigned char mixChannel, unsigned char channels, bool floatingPoint); -void CloseAudioContext(AudioContext ctx); // Frees audio context -unsigned short UpdateAudioContext(AudioContext ctx, void *data, unsigned short numberElements); // Pushes more audio data into context mix channel, if NULL is passed to data then zeros are played - Sound LoadSound(char *fileName); // Load sound to memory Sound LoadSoundFromWave(Wave wave); // Load sound to memory from wave data Sound LoadSoundFromRES(const char *rresName, int resId); // Load sound to memory from rRES file (raylib Resource) @@ -906,6 +896,9 @@ float GetMusicTimePlayed(int index); // Get current m int getMusicStreamCount(void); void SetMusicPitch(int index, float pitch); +RawAudioContext InitRawAudioContext(int sampleRate, int channels, bool floatingPoint); // used to output raw audio streams, returns negative numbers on error +void CloseRawAudioContext(RawAudioContext ctx); + #ifdef __cplusplus } #endif -- cgit v1.2.3 From 037edbaa1322a2174d91c7aeacc9170f5b6913cb Mon Sep 17 00:00:00 2001 From: raysan5 Date: Wed, 18 May 2016 13:22:14 +0200 Subject: Reorganize data for consistency --- src/models.c | 54 ++------ src/rlgl.c | 431 ++++++++++++++++++++++++++++++++++++----------------------- src/rlgl.h | 9 +- 3 files changed, 283 insertions(+), 211 deletions(-) (limited to 'src') diff --git a/src/models.c b/src/models.c index 066b919e..bb2d56c2 100644 --- a/src/models.c +++ b/src/models.c @@ -691,31 +691,14 @@ Model LoadCubicmap(Image cubicmap) return model; } -// Unload 3d model from memory +// Unload 3d model from memory (mesh and material) void UnloadModel(Model model) { - // Unload mesh data - if (model.mesh.vertices != NULL) free(model.mesh.vertices); - if (model.mesh.texcoords != NULL) free(model.mesh.texcoords); - if (model.mesh.normals != NULL) free(model.mesh.normals); - if (model.mesh.colors != NULL) free(model.mesh.colors); - if (model.mesh.tangents != NULL) free(model.mesh.tangents); - if (model.mesh.texcoords2 != NULL) free(model.mesh.texcoords2); - if (model.mesh.indices != NULL) free(model.mesh.indices); - - TraceLog(INFO, "Unloaded model data from RAM (CPU)"); - - rlDeleteBuffers(model.mesh.vboId[0]); // vertex - rlDeleteBuffers(model.mesh.vboId[1]); // texcoords - rlDeleteBuffers(model.mesh.vboId[2]); // normals - rlDeleteBuffers(model.mesh.vboId[3]); // colors - rlDeleteBuffers(model.mesh.vboId[4]); // tangents - rlDeleteBuffers(model.mesh.vboId[5]); // texcoords2 - rlDeleteBuffers(model.mesh.vboId[6]); // indices - - rlDeleteVertexArrays(model.mesh.vaoId); - + rlglUnloadMesh(&model.mesh); + UnloadMaterial(model.material); + + TraceLog(INFO, "Unloaded model data from RAM and VRAM"); } // Load material data (from file) @@ -1247,40 +1230,27 @@ void DrawModelEx(Model model, Vector3 position, Vector3 rotationAxis, float rota model.transform = MatrixMultiply(MatrixMultiply(matScale, matRotation), matTranslation); model.material.colDiffuse = tint; - rlglDrawEx(model.mesh, model.material, model.transform, false); + rlglDrawMesh(model.mesh, model.material, model.transform); } // Draw a model wires (with texture if set) void DrawModelWires(Model model, Vector3 position, float scale, Color tint) { - Vector3 vScale = { scale, scale, scale }; - Vector3 rotationAxis = { 0.0f, 0.0f, 0.0f }; - - // Calculate transformation matrix from function parameters - // Get transform matrix (rotation -> scale -> translation) - Matrix matRotation = MatrixRotate(rotationAxis, 0.0f); - Matrix matScale = MatrixScale(vScale.x, vScale.y, vScale.z); - Matrix matTranslation = MatrixTranslate(position.x, position.y, position.z); + rlEnableWireMode(); - model.transform = MatrixMultiply(MatrixMultiply(matScale, matRotation), matTranslation); - model.material.colDiffuse = tint; + DrawModel(model, position, scale, tint); - rlglDrawEx(model.mesh, model.material, model.transform, true); + rlDisableWireMode(); } // Draw a model wires (with texture if set) with extended parameters void DrawModelWiresEx(Model model, Vector3 position, Vector3 rotationAxis, float rotationAngle, Vector3 scale, Color tint) { - // Calculate transformation matrix from function parameters - // Get transform matrix (rotation -> scale -> translation) - Matrix matRotation = MatrixRotate(rotationAxis, rotationAngle*DEG2RAD); - Matrix matScale = MatrixScale(scale.x, scale.y, scale.z); - Matrix matTranslation = MatrixTranslate(position.x, position.y, position.z); + rlEnableWireMode(); - model.transform = MatrixMultiply(MatrixMultiply(matScale, matRotation), matTranslation); - model.material.colDiffuse = tint; + DrawModelEx(model, position, rotationAxis, rotationAngle, scale, tint); - rlglDrawEx(model.mesh, model.material, model.transform, true); + rlDisableWireMode(); } // Draw a billboard diff --git a/src/rlgl.c b/src/rlgl.c index 0c0da221..8e725521 100644 --- a/src/rlgl.c +++ b/src/rlgl.c @@ -740,6 +740,24 @@ void rlDisableDepthTest(void) glDisable(GL_DEPTH_TEST); } +// Enable wire mode +void rlEnableWireMode(void) +{ +#if defined (GRAPHICS_API_OPENGL_11) || defined(GRAPHICS_API_OPENGL_33) + // NOTE: glPolygonMode() not available on OpenGL ES + glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); +#endif +} + +// Disable wire mode +void rlDisableWireMode(void) +{ +#if defined (GRAPHICS_API_OPENGL_11) || defined(GRAPHICS_API_OPENGL_33) + // NOTE: glPolygonMode() not available on OpenGL ES + glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); +#endif +} + // Unload texture from GPU memory void rlDeleteTextures(unsigned int id) { @@ -1033,175 +1051,15 @@ void rlglClose(void) void rlglDraw(void) { #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) - UpdateDefaultBuffers(); - DrawDefaultBuffers(); -#endif -} - -// Draw a 3d mesh with material and transform -void rlglDrawEx(Mesh mesh, Material material, Matrix transform, bool wires) -{ -#if defined (GRAPHICS_API_OPENGL_11) || defined(GRAPHICS_API_OPENGL_33) - // NOTE: glPolygonMode() not available on OpenGL ES - if (wires) glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); -#endif - -#if defined(GRAPHICS_API_OPENGL_11) - glEnable(GL_TEXTURE_2D); - glBindTexture(GL_TEXTURE_2D, material.texDiffuse.id); - - // NOTE: On OpenGL 1.1 we use Vertex Arrays to draw model - glEnableClientState(GL_VERTEX_ARRAY); // Enable vertex array - glEnableClientState(GL_TEXTURE_COORD_ARRAY); // Enable texture coords array - if (mesh.normals != NULL) glEnableClientState(GL_NORMAL_ARRAY); // Enable normals array - if (mesh.colors != NULL) glEnableClientState(GL_COLOR_ARRAY); // Enable colors array - - glVertexPointer(3, GL_FLOAT, 0, mesh.vertices); // Pointer to vertex coords array - glTexCoordPointer(2, GL_FLOAT, 0, mesh.texcoords); // Pointer to texture coords array - if (mesh.normals != NULL) glNormalPointer(GL_FLOAT, 0, mesh.normals); // Pointer to normals array - if (mesh.colors != NULL) glColorPointer(4, GL_UNSIGNED_BYTE, 0, mesh.colors); // Pointer to colors array - - rlPushMatrix(); - rlMultMatrixf(MatrixToFloat(transform)); - rlColor4ub(material.colDiffuse.r, material.colDiffuse.g, material.colDiffuse.b, material.colDiffuse.a); - - if (mesh.indices != NULL) glDrawElements(GL_TRIANGLES, mesh.triangleCount*3, GL_UNSIGNED_SHORT, mesh.indices); - else glDrawArrays(GL_TRIANGLES, 0, mesh.vertexCount); - rlPopMatrix(); - - glDisableClientState(GL_VERTEX_ARRAY); // Disable vertex array - glDisableClientState(GL_TEXTURE_COORD_ARRAY); // Disable texture coords array - if (mesh.normals != NULL) glDisableClientState(GL_NORMAL_ARRAY); // Disable normals array - if (mesh.colors != NULL) glDisableClientState(GL_NORMAL_ARRAY); // Disable colors array - - glDisable(GL_TEXTURE_2D); - glBindTexture(GL_TEXTURE_2D, 0); -#endif - -#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) - glUseProgram(material.shader.id); - - // At this point the modelview matrix just contains the view matrix (camera) - // That's because Begin3dMode() sets it an no model-drawing function modifies it, all use rlPushMatrix() and rlPopMatrix() - Matrix matView = modelview; // View matrix (camera) - Matrix matProjection = projection; // Projection matrix (perspective) - - // Calculate model-view matrix combining matModel and matView - Matrix matModelView = MatrixMultiply(transform, matView); // Transform to camera-space coordinates - - // Calculate model-view-projection matrix (MVP) - Matrix matMVP = MatrixMultiply(matModelView, matProjection); // Transform to screen-space coordinates - - // Send combined model-view-projection matrix to shader - glUniformMatrix4fv(material.shader.mvpLoc, 1, false, MatrixToFloat(matMVP)); - - // Apply color tinting (material.colDiffuse) - // NOTE: Just update one uniform on fragment shader - float vColor[4] = { (float)material.colDiffuse.r/255, (float)material.colDiffuse.g/255, (float)material.colDiffuse.b/255, (float)material.colDiffuse.a/255 }; - glUniform4fv(material.shader.tintColorLoc, 1, vColor); - - // Set shader textures (diffuse, normal, specular) - glActiveTexture(GL_TEXTURE0); - glBindTexture(GL_TEXTURE_2D, material.texDiffuse.id); - glUniform1i(material.shader.mapDiffuseLoc, 0); // Texture fits in active texture unit 0 - - if ((material.texNormal.id != 0) && (material.shader.mapNormalLoc != -1)) - { - glActiveTexture(GL_TEXTURE1); - glBindTexture(GL_TEXTURE_2D, material.texNormal.id); - glUniform1i(material.shader.mapNormalLoc, 1); // Texture fits in active texture unit 1 - } - - if ((material.texSpecular.id != 0) && (material.shader.mapSpecularLoc != -1)) - { - glActiveTexture(GL_TEXTURE2); - glBindTexture(GL_TEXTURE_2D, material.texSpecular.id); - glUniform1i(material.shader.mapSpecularLoc, 2); // Texture fits in active texture unit 2 - } - - if (vaoSupported) - { - glBindVertexArray(mesh.vaoId); - } - else - { - // Bind mesh VBO data: vertex position (shader-location = 0) - glBindBuffer(GL_ARRAY_BUFFER, mesh.vboId[0]); - glVertexAttribPointer(material.shader.vertexLoc, 3, GL_FLOAT, 0, 0, 0); - glEnableVertexAttribArray(material.shader.vertexLoc); - - // Bind mesh VBO data: vertex texcoords (shader-location = 1) - glBindBuffer(GL_ARRAY_BUFFER, mesh.vboId[1]); - glVertexAttribPointer(material.shader.texcoordLoc, 2, GL_FLOAT, 0, 0, 0); - glEnableVertexAttribArray(material.shader.texcoordLoc); - - // Bind mesh VBO data: vertex normals (shader-location = 2, if available) - if (material.shader.normalLoc != -1) - { - glBindBuffer(GL_ARRAY_BUFFER, mesh.vboId[2]); - glVertexAttribPointer(material.shader.normalLoc, 3, GL_FLOAT, 0, 0, 0); - glEnableVertexAttribArray(material.shader.normalLoc); - } - - // Bind mesh VBO data: vertex colors (shader-location = 3, if available) , tangents, texcoords2 (if available) - if (material.shader.colorLoc != -1) - { - glBindBuffer(GL_ARRAY_BUFFER, mesh.vboId[3]); - glVertexAttribPointer(material.shader.colorLoc, 4, GL_UNSIGNED_BYTE, GL_TRUE, 0, 0); - glEnableVertexAttribArray(material.shader.colorLoc); - } - - // Bind mesh VBO data: vertex tangents (shader-location = 4, if available) - if (material.shader.tangentLoc != -1) - { - glBindBuffer(GL_ARRAY_BUFFER, mesh.vboId[4]); - glVertexAttribPointer(material.shader.tangentLoc, 3, GL_FLOAT, 0, 0, 0); - glEnableVertexAttribArray(material.shader.tangentLoc); - } - - // Bind mesh VBO data: vertex texcoords2 (shader-location = 5, if available) - if (material.shader.texcoord2Loc != -1) - { - glBindBuffer(GL_ARRAY_BUFFER, mesh.vboId[5]); - glVertexAttribPointer(material.shader.texcoord2Loc, 2, GL_FLOAT, 0, 0, 0); - glEnableVertexAttribArray(material.shader.texcoord2Loc); - } - - if (mesh.indices != NULL) glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, quads.vboId[3]); - } - - // Draw call! - if (mesh.indices != NULL) glDrawElements(GL_TRIANGLES, mesh.triangleCount*3, GL_UNSIGNED_SHORT, 0); // Indexed vertices draw - else glDrawArrays(GL_TRIANGLES, 0, mesh.vertexCount); - - if (material.texNormal.id != 0) - { - glActiveTexture(GL_TEXTURE1); - glBindTexture(GL_TEXTURE_2D, 0); - } - - if (material.texSpecular.id != 0) - { - glActiveTexture(GL_TEXTURE2); - glBindTexture(GL_TEXTURE_2D, 0); - } - - glActiveTexture(GL_TEXTURE0); // Set shader active texture to default 0 - glBindTexture(GL_TEXTURE_2D, 0); // Unbind textures - - if (vaoSupported) glBindVertexArray(0); // Unbind VAO - else +/* + for (int i = 0; i < modelsCount; i++) { - glBindBuffer(GL_ARRAY_BUFFER, 0); // Unbind VBOs - if (mesh.indices != NULL) glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); + rlglDrawMesh(models[i]->mesh, models[i]->material, models[i]->transform); } - - glUseProgram(0); // Unbind shader program -#endif - -#if defined (GRAPHICS_API_OPENGL_11) || defined(GRAPHICS_API_OPENGL_33) - // NOTE: glPolygonMode() not available on OpenGL ES - if (wires) glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); +*/ + // NOTE: Default buffers always drawn at the end + UpdateDefaultBuffers(); + DrawDefaultBuffers(); #endif } @@ -1776,6 +1634,245 @@ void rlglLoadMesh(Mesh *mesh) #endif } +// Update vertex data on GPU (upload new data to one buffer) +void rlglUpdateMesh(Mesh mesh, int buffer, int numVertex) +{ + // Activate mesh VAO + if (vaoSupported) glBindVertexArray(mesh.vaoId); + + switch (buffer) + { + case 0: // Update vertices (vertex position) + { + glBindBuffer(GL_ARRAY_BUFFER, mesh.vboId[0]); + if (numVertex >= mesh.vertexCount) glBufferData(GL_ARRAY_BUFFER, sizeof(float)*3*numVertex, mesh.vertices, GL_DYNAMIC_DRAW); + else glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(float)*3*numVertex, mesh.vertices); + + } break; + case 1: // Update texcoords (vertex texture coordinates) + { + glBindBuffer(GL_ARRAY_BUFFER, mesh.vboId[1]); + if (numVertex >= mesh.vertexCount) glBufferData(GL_ARRAY_BUFFER, sizeof(float)*2*numVertex, mesh.texcoords, GL_DYNAMIC_DRAW); + else glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(float)*2*numVertex, mesh.texcoords); + + } break; + case 2: // Update normals (vertex normals) + { + glBindBuffer(GL_ARRAY_BUFFER, mesh.vboId[0]); + if (numVertex >= mesh.vertexCount) glBufferData(GL_ARRAY_BUFFER, sizeof(float)*3*numVertex, mesh.normals, GL_DYNAMIC_DRAW); + else glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(float)*3*numVertex, mesh.normals); + + } break; + case 3: // Update colors (vertex colors) + { + glBindBuffer(GL_ARRAY_BUFFER, mesh.vboId[2]); + if (numVertex >= mesh.vertexCount) glBufferData(GL_ARRAY_BUFFER, sizeof(float)*4*numVertex, mesh.colors, GL_DYNAMIC_DRAW); + else glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(unsigned char)*4*numVertex, mesh.colors); + + } break; + case 4: // Update tangents (vertex tangents) + { + glBindBuffer(GL_ARRAY_BUFFER, mesh.vboId[0]); + if (numVertex >= mesh.vertexCount) glBufferData(GL_ARRAY_BUFFER, sizeof(float)*3*numVertex, mesh.tangents, GL_DYNAMIC_DRAW); + else glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(float)*3*numVertex, mesh.tangents); + } break; + case 5: // Update texcoords2 (vertex second texture coordinates) + { + glBindBuffer(GL_ARRAY_BUFFER, mesh.vboId[1]); + if (numVertex >= mesh.vertexCount) glBufferData(GL_ARRAY_BUFFER, sizeof(float)*2*numVertex, mesh.texcoords2, GL_DYNAMIC_DRAW); + else glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(float)*2*numVertex, mesh.texcoords2); + } break; + default: break; + } + + // Unbind the current VAO + if (vaoSupported) glBindVertexArray(0); + + // Another option would be using buffer mapping... + //mesh.vertices = glMapBuffer(GL_ARRAY_BUFFER, GL_READ_WRITE); + // Now we can modify vertices + //glUnmapBuffer(GL_ARRAY_BUFFER); +} + +// Draw a 3d mesh with material and transform +void rlglDrawMesh(Mesh mesh, Material material, Matrix transform) +{ +#if defined(GRAPHICS_API_OPENGL_11) + glEnable(GL_TEXTURE_2D); + glBindTexture(GL_TEXTURE_2D, material.texDiffuse.id); + + // NOTE: On OpenGL 1.1 we use Vertex Arrays to draw model + glEnableClientState(GL_VERTEX_ARRAY); // Enable vertex array + glEnableClientState(GL_TEXTURE_COORD_ARRAY); // Enable texture coords array + if (mesh.normals != NULL) glEnableClientState(GL_NORMAL_ARRAY); // Enable normals array + if (mesh.colors != NULL) glEnableClientState(GL_COLOR_ARRAY); // Enable colors array + + glVertexPointer(3, GL_FLOAT, 0, mesh.vertices); // Pointer to vertex coords array + glTexCoordPointer(2, GL_FLOAT, 0, mesh.texcoords); // Pointer to texture coords array + if (mesh.normals != NULL) glNormalPointer(GL_FLOAT, 0, mesh.normals); // Pointer to normals array + if (mesh.colors != NULL) glColorPointer(4, GL_UNSIGNED_BYTE, 0, mesh.colors); // Pointer to colors array + + rlPushMatrix(); + rlMultMatrixf(MatrixToFloat(transform)); + rlColor4ub(material.colDiffuse.r, material.colDiffuse.g, material.colDiffuse.b, material.colDiffuse.a); + + if (mesh.indices != NULL) glDrawElements(GL_TRIANGLES, mesh.triangleCount*3, GL_UNSIGNED_SHORT, mesh.indices); + else glDrawArrays(GL_TRIANGLES, 0, mesh.vertexCount); + rlPopMatrix(); + + glDisableClientState(GL_VERTEX_ARRAY); // Disable vertex array + glDisableClientState(GL_TEXTURE_COORD_ARRAY); // Disable texture coords array + if (mesh.normals != NULL) glDisableClientState(GL_NORMAL_ARRAY); // Disable normals array + if (mesh.colors != NULL) glDisableClientState(GL_NORMAL_ARRAY); // Disable colors array + + glDisable(GL_TEXTURE_2D); + glBindTexture(GL_TEXTURE_2D, 0); +#endif + +#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) + glUseProgram(material.shader.id); + + // At this point the modelview matrix just contains the view matrix (camera) + // That's because Begin3dMode() sets it an no model-drawing function modifies it, all use rlPushMatrix() and rlPopMatrix() + Matrix matView = modelview; // View matrix (camera) + Matrix matProjection = projection; // Projection matrix (perspective) + + // Calculate model-view matrix combining matModel and matView + Matrix matModelView = MatrixMultiply(transform, matView); // Transform to camera-space coordinates + + // Calculate model-view-projection matrix (MVP) + Matrix matMVP = MatrixMultiply(matModelView, matProjection); // Transform to screen-space coordinates + + // Send combined model-view-projection matrix to shader + glUniformMatrix4fv(material.shader.mvpLoc, 1, false, MatrixToFloat(matMVP)); + + // Apply color tinting (material.colDiffuse) + // NOTE: Just update one uniform on fragment shader + float vColor[4] = { (float)material.colDiffuse.r/255, (float)material.colDiffuse.g/255, (float)material.colDiffuse.b/255, (float)material.colDiffuse.a/255 }; + glUniform4fv(material.shader.tintColorLoc, 1, vColor); + + // Set shader textures (diffuse, normal, specular) + glActiveTexture(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_2D, material.texDiffuse.id); + glUniform1i(material.shader.mapDiffuseLoc, 0); // Texture fits in active texture unit 0 + + if ((material.texNormal.id != 0) && (material.shader.mapNormalLoc != -1)) + { + glActiveTexture(GL_TEXTURE1); + glBindTexture(GL_TEXTURE_2D, material.texNormal.id); + glUniform1i(material.shader.mapNormalLoc, 1); // Texture fits in active texture unit 1 + } + + if ((material.texSpecular.id != 0) && (material.shader.mapSpecularLoc != -1)) + { + glActiveTexture(GL_TEXTURE2); + glBindTexture(GL_TEXTURE_2D, material.texSpecular.id); + glUniform1i(material.shader.mapSpecularLoc, 2); // Texture fits in active texture unit 2 + } + + if (vaoSupported) + { + glBindVertexArray(mesh.vaoId); + } + else + { + // Bind mesh VBO data: vertex position (shader-location = 0) + glBindBuffer(GL_ARRAY_BUFFER, mesh.vboId[0]); + glVertexAttribPointer(material.shader.vertexLoc, 3, GL_FLOAT, 0, 0, 0); + glEnableVertexAttribArray(material.shader.vertexLoc); + + // Bind mesh VBO data: vertex texcoords (shader-location = 1) + glBindBuffer(GL_ARRAY_BUFFER, mesh.vboId[1]); + glVertexAttribPointer(material.shader.texcoordLoc, 2, GL_FLOAT, 0, 0, 0); + glEnableVertexAttribArray(material.shader.texcoordLoc); + + // Bind mesh VBO data: vertex normals (shader-location = 2, if available) + if (material.shader.normalLoc != -1) + { + glBindBuffer(GL_ARRAY_BUFFER, mesh.vboId[2]); + glVertexAttribPointer(material.shader.normalLoc, 3, GL_FLOAT, 0, 0, 0); + glEnableVertexAttribArray(material.shader.normalLoc); + } + + // Bind mesh VBO data: vertex colors (shader-location = 3, if available) , tangents, texcoords2 (if available) + if (material.shader.colorLoc != -1) + { + glBindBuffer(GL_ARRAY_BUFFER, mesh.vboId[3]); + glVertexAttribPointer(material.shader.colorLoc, 4, GL_UNSIGNED_BYTE, GL_TRUE, 0, 0); + glEnableVertexAttribArray(material.shader.colorLoc); + } + + // Bind mesh VBO data: vertex tangents (shader-location = 4, if available) + if (material.shader.tangentLoc != -1) + { + glBindBuffer(GL_ARRAY_BUFFER, mesh.vboId[4]); + glVertexAttribPointer(material.shader.tangentLoc, 3, GL_FLOAT, 0, 0, 0); + glEnableVertexAttribArray(material.shader.tangentLoc); + } + + // Bind mesh VBO data: vertex texcoords2 (shader-location = 5, if available) + if (material.shader.texcoord2Loc != -1) + { + glBindBuffer(GL_ARRAY_BUFFER, mesh.vboId[5]); + glVertexAttribPointer(material.shader.texcoord2Loc, 2, GL_FLOAT, 0, 0, 0); + glEnableVertexAttribArray(material.shader.texcoord2Loc); + } + + if (mesh.indices != NULL) glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, quads.vboId[3]); + } + + // Draw call! + if (mesh.indices != NULL) glDrawElements(GL_TRIANGLES, mesh.triangleCount*3, GL_UNSIGNED_SHORT, 0); // Indexed vertices draw + else glDrawArrays(GL_TRIANGLES, 0, mesh.vertexCount); + + if (material.texNormal.id != 0) + { + glActiveTexture(GL_TEXTURE1); + glBindTexture(GL_TEXTURE_2D, 0); + } + + if (material.texSpecular.id != 0) + { + glActiveTexture(GL_TEXTURE2); + glBindTexture(GL_TEXTURE_2D, 0); + } + + glActiveTexture(GL_TEXTURE0); // Set shader active texture to default 0 + glBindTexture(GL_TEXTURE_2D, 0); // Unbind textures + + if (vaoSupported) glBindVertexArray(0); // Unbind VAO + else + { + glBindBuffer(GL_ARRAY_BUFFER, 0); // Unbind VBOs + if (mesh.indices != NULL) glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); + } + + glUseProgram(0); // Unbind shader program +#endif +} + +// Unload mesh data from CPU and GPU +void rlglUnloadMesh(Mesh *mesh) +{ + if (mesh->vertices != NULL) free(mesh->vertices); + if (mesh->texcoords != NULL) free(mesh->texcoords); + if (mesh->normals != NULL) free(mesh->normals); + if (mesh->colors != NULL) free(mesh->colors); + if (mesh->tangents != NULL) free(mesh->tangents); + if (mesh->texcoords2 != NULL) free(mesh->texcoords2); + if (mesh->indices != NULL) free(mesh->indices); + + rlDeleteBuffers(mesh->vboId[0]); // vertex + rlDeleteBuffers(mesh->vboId[1]); // texcoords + rlDeleteBuffers(mesh->vboId[2]); // normals + rlDeleteBuffers(mesh->vboId[3]); // colors + rlDeleteBuffers(mesh->vboId[4]); // tangents + rlDeleteBuffers(mesh->vboId[5]); // texcoords2 + rlDeleteBuffers(mesh->vboId[6]); // indices + + rlDeleteVertexArrays(mesh->vaoId); +} + // Read screen pixel data (color buffer) unsigned char *rlglReadScreenPixels(int width, int height) { diff --git a/src/rlgl.h b/src/rlgl.h index 7b88bc9e..a33e3a0a 100644 --- a/src/rlgl.h +++ b/src/rlgl.h @@ -256,6 +256,8 @@ void rlEnableRenderTexture(unsigned int id); // Enable render texture (fbo) void rlDisableRenderTexture(void); // Disable render texture (fbo), return to default framebuffer void rlEnableDepthTest(void); // Enable depth test void rlDisableDepthTest(void); // Disable depth test +void rlEnableWireMode(void); // Enable wire mode +void rlDisableWireMode(void); // Disable wire mode void rlDeleteTextures(unsigned int id); // Delete OpenGL texture from GPU void rlDeleteRenderTextures(RenderTexture2D target); // Delete render textures (fbo) from GPU void rlDeleteShader(unsigned int id); // Delete OpenGL shader program from GPU @@ -277,8 +279,11 @@ unsigned int rlglLoadTexture(void *data, int width, int height, int textureForma RenderTexture2D rlglLoadRenderTexture(int width, int height); // Load a texture to be used for rendering (fbo with color and depth attachments) void rlglUpdateTexture(unsigned int id, int width, int height, int format, void *data); // Update GPU texture with new data void rlglGenerateMipmaps(Texture2D texture); // Generate mipmap data for selected texture -void rlglLoadMesh(Mesh *mesh); // Upload vertex data into GPU and provided VAO/VBO ids -void rlglDrawEx(Mesh mesh, Material material, Matrix transform, bool wires); + +void rlglLoadMesh(Mesh *mesh); // Upload vertex data into GPU and provided VAO/VBO ids +void rlglUpdateMesh(Mesh mesh, int buffer, int numVertex); // Update vertex data on GPU (upload new data to one buffer) +void rlglDrawMesh(Mesh mesh, Material material, Matrix transform); // Draw a 3d mesh with material and transform +void rlglUnloadMesh(Mesh *mesh); // Unload mesh data from CPU and GPU Vector3 rlglUnproject(Vector3 source, Matrix proj, Matrix view); // Get world coordinates from screen coordinates -- cgit v1.2.3 From 8bbbe8cd76d93bc85810fd13a6cb6c4df18bd30d Mon Sep 17 00:00:00 2001 From: raysan5 Date: Thu, 19 May 2016 13:50:29 +0200 Subject: Corrected namings --- src/rlgl.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) (limited to 'src') diff --git a/src/rlgl.c b/src/rlgl.c index 8e725521..888a9313 100644 --- a/src/rlgl.c +++ b/src/rlgl.c @@ -2425,12 +2425,12 @@ static void LoadDefaultShaderLocations(Shader *shader) // vertex texcoord2 location = 5 // Get handles to GLSL input attibute locations - shader->vertexLoc = glGetAttribLocation(shader->id, "vertexPosition"); - shader->texcoordLoc = glGetAttribLocation(shader->id, "vertexTexCoord"); - shader->normalLoc = glGetAttribLocation(shader->id, "vertexNormal"); - shader->colorLoc = glGetAttribLocation(shader->id, "vertexColor"); - shader->tangentLoc = glGetAttribLocation(shader->id, "vertexTangent"); - shader->texcoord2Loc = glGetAttribLocation(shader->id, "vertexTexCoord2"); + shader->vertexLoc = glGetAttribLocation(shader->id, DEFAULT_ATTRIB_POSITION_NAME); + shader->texcoordLoc = glGetAttribLocation(shader->id, DEFAULT_ATTRIB_TEXCOORD_NAME); + shader->normalLoc = glGetAttribLocation(shader->id, DEFAULT_ATTRIB_NORMAL_NAME); + shader->colorLoc = glGetAttribLocation(shader->id, DEFAULT_ATTRIB_COLOR_NAME); + shader->tangentLoc = glGetAttribLocation(shader->id, DEFAULT_ATTRIB_TANGENT_NAME); + shader->texcoord2Loc = glGetAttribLocation(shader->id, DEFAULT_ATTRIB_TEXCOORD2_NAME); // Get handles to GLSL uniform locations (vertex shader) shader->mvpLoc = glGetUniformLocation(shader->id, "mvpMatrix"); @@ -2447,8 +2447,8 @@ static void UnloadDefaultShader(void) { glUseProgram(0); - //glDetachShader(defaultShaderProgram, vertexShader); - //glDetachShader(defaultShaderProgram, fragmentShader); + //glDetachShader(defaultShader, vertexShader); + //glDetachShader(defaultShader, fragmentShader); //glDeleteShader(vertexShader); // Already deleted on shader compilation //glDeleteShader(fragmentShader); // Already deleted on sahder compilation glDeleteProgram(defaultShader.id); -- cgit v1.2.3 From b10425492ac692d7c53001290e3d0b678df634b0 Mon Sep 17 00:00:00 2001 From: Joshua Reisenauer Date: Thu, 19 May 2016 15:22:12 -0700 Subject: name correction --- src/audio.c | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) (limited to 'src') diff --git a/src/audio.c b/src/audio.c index 584d3ad1..6d8f876c 100644 --- a/src/audio.c +++ b/src/audio.c @@ -59,9 +59,9 @@ //---------------------------------------------------------------------------------- // Defines and Macros //---------------------------------------------------------------------------------- -#define MAX_STREAM_BUFFERS 2 -#define MAX_AUDIO_CONTEXTS 4 // Number of open AL sources -#define MAX_MUSIC_STREAMS 2 +#define MAX_STREAM_BUFFERS 2 // Number of buffers for each alSource +#define MAX_MIX_CHANNELS 4 // Number of open AL sources +#define MAX_MUSIC_STREAMS 2 // Number of simultanious music sources #if defined(PLATFORM_RPI) || defined(PLATFORM_ANDROID) // NOTE: On RPI and Android should be lower to avoid frame-stalls @@ -96,12 +96,12 @@ typedef struct MixChannel_t { typedef struct Music { stb_vorbis *stream; jar_xm_context_t *chipctx; // Stores jar_xm mixc - MixChannel_t *mixc; // mix channel + MixChannel_t *mixc; // mix channel int totalSamplesLeft; float totalLengthSeconds; bool loop; - bool chipTune; // True if chiptune is loaded + bool chipTune; // True if chiptune is loaded } Music; #if defined(AUDIO_STANDALONE) @@ -111,7 +111,7 @@ typedef enum { INFO = 0, ERROR, WARNING, DEBUG, OTHER } TraceLogType; //---------------------------------------------------------------------------------- // Global Variables Definition //---------------------------------------------------------------------------------- -static MixChannel_t* mixChannelsActive_g[MAX_AUDIO_CONTEXTS]; // What mix channels are currently active +static MixChannel_t* mixChannelsActive_g[MAX_MIX_CHANNELS]; // What mix channels are currently active static bool musicEnabled_g = false; static Music currentMusic[MAX_MUSIC_STREAMS]; // Current music loaded, up to two can play at the same time @@ -123,7 +123,7 @@ static Wave LoadOGG(char *fileName); // Load OGG file static void UnloadWave(Wave wave); // Unload wave data static bool BufferMusicStream(int index, int numBuffers); // Fill music buffers with data -static void EmptyMusicStream(int index); // Empty music buffers +static void EmptyMusicStream(int index); // Empty music buffers static MixChannel_t* InitMixChannel(unsigned short sampleRate, unsigned char mixChannel, unsigned char channels, bool floatingPoint); // For streaming into mix channels. @@ -212,7 +212,7 @@ bool IsAudioDeviceReady(void) // exmple usage is InitMixChannel(48000, 0, 2, true); // mixchannel 1, 48khz, stereo, floating point static MixChannel_t* InitMixChannel(unsigned short sampleRate, unsigned char mixChannel, unsigned char channels, bool floatingPoint) { - if(mixChannel >= MAX_AUDIO_CONTEXTS) return NULL; + if(mixChannel >= MAX_MIX_CHANNELS) return NULL; if(!IsAudioDeviceReady()) InitAudioDevice(); if(!mixChannelsActive_g[mixChannel]){ @@ -380,10 +380,10 @@ static void ResampleByteToFloat(char *chars, float *floats, unsigned short len) RawAudioContext InitRawAudioContext(int sampleRate, int channels, bool floatingPoint) { int mixIndex; - for(mixIndex = 0; mixIndex < MAX_AUDIO_CONTEXTS; mixIndex++) // find empty mix channel slot + for(mixIndex = 0; mixIndex < MAX_MIX_CHANNELS; mixIndex++) // find empty mix channel slot { if(mixChannelsActive_g[mixIndex] == NULL) break; - else if(mixIndex = MAX_AUDIO_CONTEXTS - 1) return -1; // error + else if(mixIndex = MAX_MIX_CHANNELS - 1) return -1; // error } if(InitMixChannel(sampleRate, mixIndex, channels, floatingPoint)) @@ -755,10 +755,10 @@ int PlayMusicStream(int musicIndex, char *fileName) if(currentMusic[musicIndex].stream || currentMusic[musicIndex].chipctx) return 1; // error - for(mixIndex = 0; mixIndex < MAX_AUDIO_CONTEXTS; mixIndex++) // find empty mix channel slot + for(mixIndex = 0; mixIndex < MAX_MIX_CHANNELS; mixIndex++) // find empty mix channel slot { if(mixChannelsActive_g[mixIndex] == NULL) break; - else if(mixIndex = MAX_AUDIO_CONTEXTS - 1) return 2; // error + else if(mixIndex = MAX_MIX_CHANNELS - 1) return 2; // error } if (strcmp(GetExtension(fileName),"ogg") == 0) -- cgit v1.2.3 From 41c5f3a0178027e9b74e563ab102f603a53e35bf Mon Sep 17 00:00:00 2001 From: Joshua Reisenauer Date: Thu, 19 May 2016 20:44:09 -0700 Subject: Buffer for raw audio --- src/audio.c | 22 ++++++++++++++++++---- src/audio.h | 4 ++++ src/raylib.h | 6 +++++- 3 files changed, 27 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/audio.c b/src/audio.c index 6d8f876c..43e8be14 100644 --- a/src/audio.c +++ b/src/audio.c @@ -128,8 +128,8 @@ static void EmptyMusicStream(int index); // Empty music buffers static MixChannel_t* InitMixChannel(unsigned short sampleRate, unsigned char mixChannel, unsigned char channels, bool floatingPoint); // For streaming into mix channels. static void CloseMixChannel(MixChannel_t* mixc); // Frees mix channel -static unsigned short BufferMixChannel(MixChannel_t* mixc, void *data, int numberElements); // Pushes more audio data into mixc mix channel, if NULL is passed it pauses -static unsigned short FillAlBufferWithSilence(MixChannel_t *mixc, ALuint buffer); // Fill buffer with zeros, returns number processed +static int BufferMixChannel(MixChannel_t* mixc, void *data, int numberElements); // Pushes more audio data into mixc mix channel, if NULL is passed it pauses +static int FillAlBufferWithSilence(MixChannel_t *mixc, ALuint buffer); // Fill buffer with zeros, returns number processed static void ResampleShortToFloat(short *shorts, float *floats, unsigned short len); // Pass two arrays of the same legnth in static void ResampleByteToFloat(char *chars, float *floats, unsigned short len); // Pass two arrays of same length in static int IsMusicStreamReadyForBuffering(int index); // Checks if music buffer is ready to be refilled @@ -292,7 +292,7 @@ static void CloseMixChannel(MixChannel_t* mixc) // Pushes more audio data into mixc mix channel, only one buffer per call // Call "BufferMixChannel(mixc, NULL, 0)" if you want to pause the audio. // @Returns number of samples that where processed. -static unsigned short BufferMixChannel(MixChannel_t* mixc, void *data, int numberElements) +static int BufferMixChannel(MixChannel_t* mixc, void *data, int numberElements) { if(!mixc || mixChannelsActive_g[mixc->mixChannel] != mixc) return 0; // when there is two channels there must be an even number of samples @@ -331,7 +331,7 @@ static unsigned short BufferMixChannel(MixChannel_t* mixc, void *data, int numbe } // fill buffer with zeros, returns number processed -static unsigned short FillAlBufferWithSilence(MixChannel_t *mixc, ALuint buffer) +static int FillAlBufferWithSilence(MixChannel_t *mixc, ALuint buffer) { if(mixc->floatingPoint){ float pcm[MUSIC_BUFFER_SIZE_FLOAT] = {0.f}; @@ -377,6 +377,7 @@ static void ResampleByteToFloat(char *chars, float *floats, unsigned short len) } // used to output raw audio streams, returns negative numbers on error +// if floating point is false the data size is 16bit short, otherwise it is float 32bit RawAudioContext InitRawAudioContext(int sampleRate, int channels, bool floatingPoint) { int mixIndex; @@ -398,6 +399,19 @@ void CloseRawAudioContext(RawAudioContext ctx) CloseMixChannel(mixChannelsActive_g[ctx]); } +int BufferRawAudioContext(RawAudioContext ctx, void *data, int numberElements) +{ + int numBuffered = 0; + if(ctx >= 0) + { + MixChannel_t* mixc = mixChannelsActive_g[ctx]; + numBuffered = BufferMixChannel(mixc, data, numberElements); + } + return numBuffered; +} + + + //---------------------------------------------------------------------------------- diff --git a/src/audio.h b/src/audio.h index d3276bf6..1140a60a 100644 --- a/src/audio.h +++ b/src/audio.h @@ -102,8 +102,12 @@ float GetMusicTimePlayed(int index); // Get current m int getMusicStreamCount(void); void SetMusicPitch(int index, float pitch); +// used to output raw audio streams, returns negative numbers on error +// if floating point is false the data size is 16bit short, otherwise it is float 32bit RawAudioContext InitRawAudioContext(int sampleRate, int channels, bool floatingPoint); + void CloseRawAudioContext(RawAudioContext ctx); +int BufferRawAudioContext(RawAudioContext ctx, void *data, int numberElements); // returns number of elements buffered #ifdef __cplusplus } diff --git a/src/raylib.h b/src/raylib.h index 6efde710..986dc7bf 100644 --- a/src/raylib.h +++ b/src/raylib.h @@ -896,8 +896,12 @@ float GetMusicTimePlayed(int index); // Get current m int getMusicStreamCount(void); void SetMusicPitch(int index, float pitch); -RawAudioContext InitRawAudioContext(int sampleRate, int channels, bool floatingPoint); // used to output raw audio streams, returns negative numbers on error +// used to output raw audio streams, returns negative numbers on error +// if floating point is false the data size is 16bit short, otherwise it is float 32bit +RawAudioContext InitRawAudioContext(int sampleRate, int channels, bool floatingPoint); + void CloseRawAudioContext(RawAudioContext ctx); +int BufferRawAudioContext(RawAudioContext ctx, void *data, int numberElements); // returns number of elements buffered #ifdef __cplusplus } -- cgit v1.2.3 From 179f2f9e4fd9ad43e120f186484a78984a2ac063 Mon Sep 17 00:00:00 2001 From: Joshua Reisenauer Date: Thu, 19 May 2016 20:56:38 -0700 Subject: windows automated compile Only works when raylib is installed on windows system. --- src/windows_compile.bat | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 src/windows_compile.bat (limited to 'src') diff --git a/src/windows_compile.bat b/src/windows_compile.bat new file mode 100644 index 00000000..f1d0fb29 --- /dev/null +++ b/src/windows_compile.bat @@ -0,0 +1,2 @@ +set PATH=C:\raylib\MinGW\bin;%PATH% +mingw32-make \ No newline at end of file -- cgit v1.2.3 From 7d1d9ff143cd7c6c55d3fd891b43e143431ea15f Mon Sep 17 00:00:00 2001 From: raysan5 Date: Fri, 20 May 2016 09:36:02 +0200 Subject: Support DYNAMIC_DRAW mesh loading --- src/models.c | 10 +++++----- src/raylib.h | 2 +- src/rlgl.c | 17 ++++++++++------- src/rlgl.h | 2 +- 4 files changed, 17 insertions(+), 14 deletions(-) (limited to 'src') diff --git a/src/models.c b/src/models.c index bb2d56c2..6d72d1e3 100644 --- a/src/models.c +++ b/src/models.c @@ -553,7 +553,7 @@ Model LoadModel(const char *fileName) if (model.mesh.vertexCount == 0) TraceLog(WARNING, "Model could not be loaded"); else { - rlglLoadMesh(&model.mesh); // Upload vertex data to GPU + rlglLoadMesh(&model.mesh, false); // Upload vertex data to GPU (static model) model.transform = MatrixIdentity(); model.material = LoadDefaultMaterial(); @@ -563,13 +563,13 @@ Model LoadModel(const char *fileName) } // Load a 3d model (from vertex data) -Model LoadModelEx(Mesh data) +Model LoadModelEx(Mesh data, bool dynamic) { Model model = { 0 }; model.mesh = data; - rlglLoadMesh(&model.mesh); // Upload vertex data to GPU + rlglLoadMesh(&model.mesh, dynamic); // Upload vertex data to GPU model.transform = MatrixIdentity(); model.material = LoadDefaultMaterial(); @@ -668,7 +668,7 @@ Model LoadHeightmap(Image heightmap, Vector3 size) model.mesh = GenMeshHeightmap(heightmap, size); - rlglLoadMesh(&model.mesh); + rlglLoadMesh(&model.mesh, false); // Upload vertex data to GPU (static model) model.transform = MatrixIdentity(); model.material = LoadDefaultMaterial(); @@ -683,7 +683,7 @@ Model LoadCubicmap(Image cubicmap) model.mesh = GenMeshCubicmap(cubicmap, (Vector3){ 1.0, 1.0, 1.5f }); - rlglLoadMesh(&model.mesh); + rlglLoadMesh(&model.mesh, false); // Upload vertex data to GPU (static model) model.transform = MatrixIdentity(); model.material = LoadDefaultMaterial(); diff --git a/src/raylib.h b/src/raylib.h index 986dc7bf..8a46ec35 100644 --- a/src/raylib.h +++ b/src/raylib.h @@ -803,7 +803,7 @@ void DrawGizmo(Vector3 position); // Model 3d Loading and Drawing Functions (Module: models) //------------------------------------------------------------------------------------ Model LoadModel(const char *fileName); // Load a 3d model (.OBJ) -Model LoadModelEx(Mesh data); // Load a 3d model (from mesh data) +Model LoadModelEx(Mesh data, bool dynamic); // Load a 3d model (from mesh data) Model LoadModelFromRES(const char *rresName, int resId); // Load a 3d model from rRES file (raylib Resource) Model LoadHeightmap(Image heightmap, Vector3 size); // Load a heightmap image as a 3d model Model LoadCubicmap(Image cubicmap); // Load a map image as a 3d model (cubes based) diff --git a/src/rlgl.c b/src/rlgl.c index 888a9313..cc2b8942 100644 --- a/src/rlgl.c +++ b/src/rlgl.c @@ -1500,7 +1500,7 @@ void rlglGenerateMipmaps(Texture2D texture) } // Upload vertex data into a VAO (if supported) and VBO -void rlglLoadMesh(Mesh *mesh) +void rlglLoadMesh(Mesh *mesh, bool dynamic) { mesh->vaoId = 0; // Vertex Array Object mesh->vboId[0] = 0; // Vertex positions VBO @@ -1510,6 +1510,9 @@ void rlglLoadMesh(Mesh *mesh) mesh->vboId[4] = 0; // Vertex tangents VBO mesh->vboId[5] = 0; // Vertex texcoords2 VBO mesh->vboId[6] = 0; // Vertex indices VBO + + int drawHint = GL_STATIC_DRAW; + if (dynamic) drawHint = GL_DYNAMIC_DRAW; #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) GLuint vaoId = 0; // Vertex Array Objects (VAO) @@ -1527,14 +1530,14 @@ void rlglLoadMesh(Mesh *mesh) // Enable vertex attributes: position (shader-location = 0) glGenBuffers(1, &vboId[0]); glBindBuffer(GL_ARRAY_BUFFER, vboId[0]); - glBufferData(GL_ARRAY_BUFFER, sizeof(float)*3*mesh->vertexCount, mesh->vertices, GL_STATIC_DRAW); + glBufferData(GL_ARRAY_BUFFER, sizeof(float)*3*mesh->vertexCount, mesh->vertices, drawHint); glVertexAttribPointer(0, 3, GL_FLOAT, 0, 0, 0); glEnableVertexAttribArray(0); // Enable vertex attributes: texcoords (shader-location = 1) glGenBuffers(1, &vboId[1]); glBindBuffer(GL_ARRAY_BUFFER, vboId[1]); - glBufferData(GL_ARRAY_BUFFER, sizeof(float)*2*mesh->vertexCount, mesh->texcoords, GL_STATIC_DRAW); + glBufferData(GL_ARRAY_BUFFER, sizeof(float)*2*mesh->vertexCount, mesh->texcoords, drawHint); glVertexAttribPointer(1, 2, GL_FLOAT, 0, 0, 0); glEnableVertexAttribArray(1); @@ -1543,7 +1546,7 @@ void rlglLoadMesh(Mesh *mesh) { glGenBuffers(1, &vboId[2]); glBindBuffer(GL_ARRAY_BUFFER, vboId[2]); - glBufferData(GL_ARRAY_BUFFER, sizeof(float)*3*mesh->vertexCount, mesh->normals, GL_STATIC_DRAW); + glBufferData(GL_ARRAY_BUFFER, sizeof(float)*3*mesh->vertexCount, mesh->normals, drawHint); glVertexAttribPointer(2, 3, GL_FLOAT, 0, 0, 0); glEnableVertexAttribArray(2); } @@ -1559,7 +1562,7 @@ void rlglLoadMesh(Mesh *mesh) { glGenBuffers(1, &vboId[3]); glBindBuffer(GL_ARRAY_BUFFER, vboId[3]); - glBufferData(GL_ARRAY_BUFFER, sizeof(unsigned char)*4*mesh->vertexCount, mesh->colors, GL_STATIC_DRAW); + glBufferData(GL_ARRAY_BUFFER, sizeof(unsigned char)*4*mesh->vertexCount, mesh->colors, drawHint); glVertexAttribPointer(3, 4, GL_UNSIGNED_BYTE, GL_TRUE, 0, 0); glEnableVertexAttribArray(3); } @@ -1575,7 +1578,7 @@ void rlglLoadMesh(Mesh *mesh) { glGenBuffers(1, &vboId[4]); glBindBuffer(GL_ARRAY_BUFFER, vboId[4]); - glBufferData(GL_ARRAY_BUFFER, sizeof(float)*3*mesh->vertexCount, mesh->tangents, GL_STATIC_DRAW); + glBufferData(GL_ARRAY_BUFFER, sizeof(float)*3*mesh->vertexCount, mesh->tangents, drawHint); glVertexAttribPointer(4, 3, GL_FLOAT, 0, 0, 0); glEnableVertexAttribArray(4); } @@ -1591,7 +1594,7 @@ void rlglLoadMesh(Mesh *mesh) { glGenBuffers(1, &vboId[5]); glBindBuffer(GL_ARRAY_BUFFER, vboId[5]); - glBufferData(GL_ARRAY_BUFFER, sizeof(float)*2*mesh->vertexCount, mesh->texcoords2, GL_STATIC_DRAW); + glBufferData(GL_ARRAY_BUFFER, sizeof(float)*2*mesh->vertexCount, mesh->texcoords2, drawHint); glVertexAttribPointer(5, 2, GL_FLOAT, 0, 0, 0); glEnableVertexAttribArray(5); } diff --git a/src/rlgl.h b/src/rlgl.h index a33e3a0a..a557ffa2 100644 --- a/src/rlgl.h +++ b/src/rlgl.h @@ -280,7 +280,7 @@ RenderTexture2D rlglLoadRenderTexture(int width, int height); // Load a textur void rlglUpdateTexture(unsigned int id, int width, int height, int format, void *data); // Update GPU texture with new data void rlglGenerateMipmaps(Texture2D texture); // Generate mipmap data for selected texture -void rlglLoadMesh(Mesh *mesh); // Upload vertex data into GPU and provided VAO/VBO ids +void rlglLoadMesh(Mesh *mesh, bool dynamic); // Upload vertex data into GPU and provided VAO/VBO ids void rlglUpdateMesh(Mesh mesh, int buffer, int numVertex); // Update vertex data on GPU (upload new data to one buffer) void rlglDrawMesh(Mesh mesh, Material material, Matrix transform); // Draw a 3d mesh with material and transform void rlglUnloadMesh(Mesh *mesh); // Unload mesh data from CPU and GPU -- cgit v1.2.3 From 03cc031d00ab97ab46b61b66a6281a175c33541f Mon Sep 17 00:00:00 2001 From: raysan5 Date: Fri, 20 May 2016 09:40:48 +0200 Subject: Remove TODO comments (already done) --- src/raylib.h | 1 - src/shapes.c | 1 - 2 files changed, 2 deletions(-) (limited to 'src') diff --git a/src/raylib.h b/src/raylib.h index 8a46ec35..32cd54a6 100644 --- a/src/raylib.h +++ b/src/raylib.h @@ -423,7 +423,6 @@ typedef struct Material { } Material; // 3d Model type -// TODO: Replace shader/testure by material typedef struct Model { Mesh mesh; // Vertex data buffers (RAM and VRAM) Matrix transform; // Local transform matrix diff --git a/src/shapes.c b/src/shapes.c index 14d11315..5b66e5ef 100644 --- a/src/shapes.c +++ b/src/shapes.c @@ -446,7 +446,6 @@ bool CheckCollisionCircleRec(Vector2 center, float radius, Rectangle rec) } // Get collision rectangle for two rectangles collision -// TODO: Depending on rec1 and rec2 order, it fails -> Review! Rectangle GetCollisionRec(Rectangle rec1, Rectangle rec2) { Rectangle retRec = { 0, 0, 0, 0 }; -- cgit v1.2.3 From c9e30f77540e9693ddecffc353964eb71e854842 Mon Sep 17 00:00:00 2001 From: raysan5 Date: Fri, 20 May 2016 10:53:31 +0200 Subject: Review struct typedef to avoid pointers for users --- src/physac.c | 12 ++++++------ src/raylib.h | 12 ++++++------ 2 files changed, 12 insertions(+), 12 deletions(-) (limited to 'src') diff --git a/src/physac.c b/src/physac.c index ed707474..181488ac 100644 --- a/src/physac.c +++ b/src/physac.c @@ -49,7 +49,7 @@ //---------------------------------------------------------------------------------- // Global Variables Definition //---------------------------------------------------------------------------------- -static PhysicObject *physicObjects[MAX_PHYSIC_OBJECTS]; // Physic objects pool +static PhysicObject physicObjects[MAX_PHYSIC_OBJECTS]; // Physic objects pool static int physicObjectsCount; // Counts current enabled physic objects static Vector2 gravityForce; // Gravity force @@ -463,10 +463,10 @@ void ClosePhysics() } // Create a new physic object dinamically, initialize it and add to pool -PhysicObject *CreatePhysicObject(Vector2 position, float rotation, Vector2 scale) +PhysicObject CreatePhysicObject(Vector2 position, float rotation, Vector2 scale) { // Allocate dynamic memory - PhysicObject *obj = (PhysicObject *)malloc(sizeof(PhysicObject)); + PhysicObject obj = (PhysicObject)malloc(sizeof(PhysicObjectData)); // Initialize physic object values with generic values obj->id = physicObjectsCount; @@ -498,7 +498,7 @@ PhysicObject *CreatePhysicObject(Vector2 position, float rotation, Vector2 scale } // Destroy a specific physic object and take it out of the list -void DestroyPhysicObject(PhysicObject *pObj) +void DestroyPhysicObject(PhysicObject pObj) { // Free dynamic memory allocation free(physicObjects[pObj->id]); @@ -520,7 +520,7 @@ void DestroyPhysicObject(PhysicObject *pObj) } // Apply directional force to a physic object -void ApplyForce(PhysicObject *pObj, Vector2 force) +void ApplyForce(PhysicObject pObj, Vector2 force) { if (pObj->rigidbody.enabled) { @@ -571,7 +571,7 @@ Rectangle TransformToRectangle(Transform transform) } // Draw physic object information at screen position -void DrawPhysicObjectInfo(PhysicObject *pObj, Vector2 position, int fontSize) +void DrawPhysicObjectInfo(PhysicObject pObj, Vector2 position, int fontSize) { // Draw physic object ID DrawText(FormatText("PhysicObject ID: %i - Enabled: %i", pObj->id, pObj->enabled), position.x, position.y, fontSize, BLACK); diff --git a/src/raylib.h b/src/raylib.h index 32cd54a6..fc1914bb 100644 --- a/src/raylib.h +++ b/src/raylib.h @@ -535,13 +535,13 @@ typedef struct Collider { int radius; // Used for COLLIDER_CIRCLE } Collider; -typedef struct PhysicObject { +typedef struct PhysicObjectData { unsigned int id; Transform transform; Rigidbody rigidbody; Collider collider; bool enabled; -} PhysicObject; +} PhysicObjectData, *PhysicObject; #ifdef __cplusplus extern "C" { // Prevents name mangling of functions @@ -856,14 +856,14 @@ void InitPhysics(Vector2 gravity); void UpdatePhysics(); // Update physic objects, calculating physic behaviours and collisions detection void ClosePhysics(); // Unitialize all physic objects and empty the objects pool -PhysicObject *CreatePhysicObject(Vector2 position, float rotation, Vector2 scale); // Create a new physic object dinamically, initialize it and add to pool -void DestroyPhysicObject(PhysicObject *pObj); // Destroy a specific physic object and take it out of the list +PhysicObject CreatePhysicObject(Vector2 position, float rotation, Vector2 scale); // Create a new physic object dinamically, initialize it and add to pool +void DestroyPhysicObject(PhysicObject pObj); // Destroy a specific physic object and take it out of the list -void ApplyForce(PhysicObject *pObj, Vector2 force); // Apply directional force to a physic object +void ApplyForce(PhysicObject pObj, Vector2 force); // Apply directional force to a physic object void ApplyForceAtPosition(Vector2 position, float force, float radius); // Apply radial force to all physic objects in range Rectangle TransformToRectangle(Transform transform); // Convert Transform data type to Rectangle (position and scale) -void DrawPhysicObjectInfo(PhysicObject *pObj, Vector2 position, int fontSize); // Draw physic object information at screen position +void DrawPhysicObjectInfo(PhysicObject pObj, Vector2 position, int fontSize); // Draw physic object information at screen position //------------------------------------------------------------------------------------ // Audio Loading and Playing Functions (Module: audio) -- cgit v1.2.3 From af890cf21021e4a306bf3f6e4397496b95c1c93a Mon Sep 17 00:00:00 2001 From: raysan5 Date: Fri, 20 May 2016 10:53:58 +0200 Subject: Updated to avoid pointers --- src/physac.h | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/physac.h b/src/physac.h index 37544686..6cef480a 100644 --- a/src/physac.h +++ b/src/physac.h @@ -66,13 +66,13 @@ typedef struct Collider { int radius; // Used for COLLIDER_CIRCLE } Collider; -typedef struct PhysicObject { +typedef struct PhysicObjectData { unsigned int id; Transform transform; Rigidbody rigidbody; Collider collider; bool enabled; -} PhysicObject; +} PhysicObjectData, *PhysicObject; #ifdef __cplusplus extern "C" { // Prevents name mangling of functions @@ -85,14 +85,14 @@ void InitPhysics(Vector2 gravity); void UpdatePhysics(); // Update physic objects, calculating physic behaviours and collisions detection void ClosePhysics(); // Unitialize all physic objects and empty the objects pool -PhysicObject *CreatePhysicObject(Vector2 position, float rotation, Vector2 scale); // Create a new physic object dinamically, initialize it and add to pool -void DestroyPhysicObject(PhysicObject *pObj); // Destroy a specific physic object and take it out of the list +PhysicObject CreatePhysicObject(Vector2 position, float rotation, Vector2 scale); // Create a new physic object dinamically, initialize it and add to pool +void DestroyPhysicObject(PhysicObject pObj); // Destroy a specific physic object and take it out of the list -void ApplyForce(PhysicObject *pObj, Vector2 force); // Apply directional force to a physic object +void ApplyForce(PhysicObject pObj, Vector2 force); // Apply directional force to a physic object void ApplyForceAtPosition(Vector2 position, float force, float radius); // Apply radial force to all physic objects in range Rectangle TransformToRectangle(Transform transform); // Convert Transform data type to Rectangle (position and scale) -void DrawPhysicObjectInfo(PhysicObject *pObj, Vector2 position, int fontSize); // Draw physic object information at screen position +void DrawPhysicObjectInfo(PhysicObject pObj, Vector2 position, int fontSize); // Draw physic object information at screen position #ifdef __cplusplus } -- cgit v1.2.3 From dcf5f45f687f2a534286aecd5e6471a0440b0c21 Mon Sep 17 00:00:00 2001 From: raysan5 Date: Fri, 20 May 2016 12:28:07 +0200 Subject: Add lighting system -IN PROGRESS- Improved materials --- src/models.c | 12 +++++ src/raylib.h | 30 ++++++++++- src/rlgl.c | 167 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- src/rlgl.h | 25 +++++++++ 4 files changed, 228 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/models.c b/src/models.c index 6d72d1e3..44e30390 100644 --- a/src/models.c +++ b/src/models.c @@ -732,6 +732,18 @@ Material LoadDefaultMaterial(void) return material; } +// Load standard material (uses standard models shader) +// NOTE: Standard shader supports multiple maps and lights +Material LoadStandardMaterial(void) +{ + Material material = LoadDefaultMaterial(); + + //material.shader = GetStandardShader(); + + return material; +} + +// Unload material from memory void UnloadMaterial(Material material) { rlDeleteTextures(material.texDiffuse.id); diff --git a/src/raylib.h b/src/raylib.h index fc1914bb..d98a0797 100644 --- a/src/raylib.h +++ b/src/raylib.h @@ -422,13 +422,38 @@ typedef struct Material { float normalDepth; // Normal map depth } Material; -// 3d Model type +// Model type typedef struct Model { Mesh mesh; // Vertex data buffers (RAM and VRAM) Matrix transform; // Local transform matrix Material material; // Shader and textures data } Model; +// Light type +// TODO: Review contained data to support different light types and features +typedef struct LightData { + int id; + int type; // LIGHT_POINT, LIGHT_DIRECTIONAL, LIGHT_SPOT + bool enabled; + + Vector3 position; + Vector3 direction; // Used on LIGHT_DIRECTIONAL and LIGHT_SPOT (cone direction) + float attenuation; // Lost of light intensity with distance (use radius?) + + Color diffuse; // Use Vector3 diffuse (including intensities)? + float intensity; + + Color specular; + //float specFactor; // Specular intensity ? + + //Color ambient; // Required? + + float coneAngle; // SpotLight +} LightData, *Light; + +// Light types +typedef enum { LIGHT_POINT, LIGHT_DIRECTIONAL, LIGHT_SPOT } LightType; + // Ray type (useful for raycast) typedef struct Ray { Vector3 position; @@ -849,6 +874,9 @@ void SetShaderValueMatrix(Shader shader, int uniformLoc, Matrix mat); // S void SetBlendMode(int mode); // Set blending mode (alpha, additive, multiplied) +Light CreateLight(int type, Vector3 position, Color diffuse); // Create a new light, initialize it and add to pool +void DestroyLight(Light light); // Destroy a light and take it out of the list + //---------------------------------------------------------------------------------- // Physics System Functions (Module: physac) //---------------------------------------------------------------------------------- diff --git a/src/rlgl.c b/src/rlgl.c index cc2b8942..e971d747 100644 --- a/src/rlgl.c +++ b/src/rlgl.c @@ -71,6 +71,8 @@ #define MAX_DRAWS_BY_TEXTURE 256 // Draws are organized by texture changes #define TEMP_VERTEX_BUFFER_SIZE 4096 // Temporal Vertex Buffer (required for vertex-transformations) // NOTE: Every vertex are 3 floats (12 bytes) + +#define MAX_LIGHTS 8 // Max lights supported by standard shader #ifndef GL_SHADING_LANGUAGE_VERSION #define GL_SHADING_LANGUAGE_VERSION 0x8B8C @@ -199,6 +201,10 @@ static bool texCompETC1Supported = false; // ETC1 texture compression support static bool texCompETC2Supported = false; // ETC2/EAC texture compression support static bool texCompPVRTSupported = false; // PVR texture compression support static bool texCompASTCSupported = false; // ASTC texture compression support + +// Lighting data +static Light lights[MAX_LIGHTS]; // Lights pool +static int lightsCount; // Counts current enabled physic objects #endif // Compressed textures support flags @@ -227,6 +233,7 @@ static void LoadCompressedTexture(unsigned char *data, int width, int height, in static unsigned int LoadShaderProgram(char *vShaderStr, char *fShaderStr); // Load custom shader strings and return program id static Shader LoadDefaultShader(void); // Load default shader (just vertex positioning and texture coloring) +static Shader LoadStandardShader(void); // Load standard shader (support materials and lighting) static void LoadDefaultShaderLocations(Shader *shader); // Bind default shader locations (attributes and uniforms) static void UnloadDefaultShader(void); // Unload default shader @@ -235,6 +242,8 @@ static void UpdateDefaultBuffers(void); // Update default internal buffers ( static void DrawDefaultBuffers(void); // Draw default internal buffers vertex data static void UnloadDefaultBuffers(void); // Unload default internal buffers vertex data from CPU and GPU +static void SetShaderLights(Shader shader); // Sets shader uniform values for lights array + static char *ReadTextFile(const char *fileName); #endif @@ -1749,11 +1758,19 @@ void rlglDrawMesh(Mesh mesh, Material material, Matrix transform) // Send combined model-view-projection matrix to shader glUniformMatrix4fv(material.shader.mvpLoc, 1, false, MatrixToFloat(matMVP)); - // Apply color tinting (material.colDiffuse) - // NOTE: Just update one uniform on fragment shader - float vColor[4] = { (float)material.colDiffuse.r/255, (float)material.colDiffuse.g/255, (float)material.colDiffuse.b/255, (float)material.colDiffuse.a/255 }; - glUniform4fv(material.shader.tintColorLoc, 1, vColor); - + // Setup shader uniforms for material related data + // TODO: Check if using standard shader to get location points + + // Upload to shader material.colDiffuse + float vColorDiffuse[4] = { (float)material.colDiffuse.r/255, (float)material.colDiffuse.g/255, (float)material.colDiffuse.b/255, (float)material.colDiffuse.a/255 }; + glUniform4fv(material.shader.tintColorLoc, 1, vColorDiffuse); + + // TODO: Upload to shader material.colAmbient + // glUniform4f(???, (float)material.colAmbient.r/255, (float)material.colAmbient.g/255, (float)material.colAmbient.b/255, (float)material.colAmbient.a/255); + + // TODO: Upload to shader material.colSpecular + // glUniform4f(???, (float)material.colSpecular.r/255, (float)material.colSpecular.g/255, (float)material.colSpecular.b/255, (float)material.colSpecular.a/255); + // Set shader textures (diffuse, normal, specular) glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, material.texDiffuse.id); @@ -1764,6 +1781,9 @@ void rlglDrawMesh(Mesh mesh, Material material, Matrix transform) glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, material.texNormal.id); glUniform1i(material.shader.mapNormalLoc, 1); // Texture fits in active texture unit 1 + + // TODO: Upload to shader normalDepth + //glUniform1f(???, material.normalDepth); } if ((material.texSpecular.id != 0) && (material.shader.mapSpecularLoc != -1)) @@ -1771,7 +1791,13 @@ void rlglDrawMesh(Mesh mesh, Material material, Matrix transform) glActiveTexture(GL_TEXTURE2); glBindTexture(GL_TEXTURE_2D, material.texSpecular.id); glUniform1i(material.shader.mapSpecularLoc, 2); // Texture fits in active texture unit 2 + + // TODO: Upload to shader glossiness + //glUniform1f(???, material.glossiness); } + + // Setup shader uniforms for lights + SetShaderLights(material.shader); if (vaoSupported) { @@ -2198,6 +2224,55 @@ void SetBlendMode(int mode) } } +// Create a new light, initialize it and add to pool +// TODO: Review creation parameters (only generic ones) +Light CreateLight(int type, Vector3 position, Color diffuse) +{ + // Allocate dynamic memory + Light light = (Light)malloc(sizeof(LightData)); + + // Initialize light values with generic values + light->id = lightsCount; + light->type = type; + light->enabled = true; + + light->position = position; + light->direction = (Vector3){ 0.0f, 0.0f, 0.0f }; + light->intensity = 1.0f; + light->diffuse = diffuse; + light->specular = WHITE; + + // Add new light to the array + lights[lightsCount] = light; + + // Increase enabled lights count + lightsCount++; + + return light; +} + +// Destroy a light and take it out of the list +void DestroyLight(Light light) +{ + // Free dynamic memory allocation + free(lights[light->id]); + + // Remove *obj from the pointers array + for (int i = light->id; i < lightsCount; i++) + { + // Resort all the following pointers of the array + if ((i + 1) < lightsCount) + { + lights[i] = lights[i + 1]; + lights[i]->id = lights[i + 1]->id; + } + else free(lights[i]); + } + + // Decrease enabled physic objects count + lightsCount--; +} + //---------------------------------------------------------------------------------- // Module specific Functions Definition //---------------------------------------------------------------------------------- @@ -2415,6 +2490,32 @@ static Shader LoadDefaultShader(void) return shader; } +// Load standard shader +// NOTE: This shader supports: +// - Up to 3 different maps: diffuse, normal, specular +// - Material properties: colDiffuse, colAmbient, colSpecular, glossiness, normalDepth +// - Up to 8 lights: Point, Directional or Spot +static Shader LoadStandardShader(void) +{ + Shader shader; + + char *vShaderStr; + char *fShaderStr; + + // TODO: Implement standard uber-shader, supporting all features (GLSL 100 / GLSL 330) + + // NOTE: Shader could be quite extensive so it could be implemented in external files (standard.vs/standard.fs) + + shader.id = LoadShaderProgram(vShaderStr, fShaderStr); + + if (shader.id != 0) TraceLog(INFO, "[SHDR ID %i] Standard shader loaded successfully", shader.id); + else TraceLog(WARNING, "[SHDR ID %i] Standard shader could not be loaded", shader.id); + + if (shader.id != 0) LoadDefaultShaderLocations(&shader); // TODO: Review locations fetching + + return shader; +} + // Get location handlers to for shader attributes and uniforms // NOTE: If any location is not found, loc point becomes -1 static void LoadDefaultShaderLocations(Shader *shader) @@ -2900,6 +3001,62 @@ static void UnloadDefaultBuffers(void) free(quads.indices); } +// Sets shader uniform values for lights array +// NOTE: It would be far easier with shader UBOs but are not supported on OpenGL ES 2.0f +// TODO: Review memcpy() and parameters pass +static void SetShaderLights(Shader shader) +{ + /* + // NOTE: Standard Shader must include the following data: + + // Shader Light struct + struct Light { + vec3 position; + vec3 direction; + + vec3 diffuse; + float intensity; + } + + const int maxLights = 8; + uniform int lightsCount; // Number of lights + uniform Light lights[maxLights]; + */ + + int locPoint; + char locName[32] = "lights[x].position\0"; + + glUseProgram(shader.id); + + locPoint = glGetUniformLocation(shader.id, "lightsCount"); + glUniform1i(locPoint, lightsCount); + + for (int i = 0; i < lightsCount; i++) + { + locName[7] = '0' + i; + + memcpy(&locName[10], "position\0", strlen("position\0")); + locPoint = glGetUniformLocation(shader.id, locName); + glUniform3f(locPoint, lights[i]->position.x, lights[i]->position.y, lights[i]->position.z); + + memcpy(&locName[10], "direction\0", strlen("direction\0")); + locPoint = glGetUniformLocation(shader.id, locName); + glUniform3f(locPoint, lights[i]->direction.x, lights[i]->direction.y, lights[i]->direction.z); + + memcpy(&locName[10], "diffuse\0", strlen("diffuse\0")); + locPoint = glGetUniformLocation(shader.id, locName); + glUniform4f(locPoint, (float)lights[i]->diffuse.r/255, (float)lights[i]->diffuse.g/255, (float)lights[i]->diffuse.b/255, (float)lights[i]->diffuse.a/255 ); + + memcpy(&locName[10], "intensity\0", strlen("intensity\0")); + locPoint = glGetUniformLocation(shader.id, locName); + glUniform1f(locPoint, lights[i]->intensity); + + // TODO: Pass to the shader any other required data from LightData struct + } + + glUseProgram(0); +} + // Read text data from file // NOTE: text chars array should be freed manually static char *ReadTextFile(const char *fileName) diff --git a/src/rlgl.h b/src/rlgl.h index a557ffa2..39941b33 100644 --- a/src/rlgl.h +++ b/src/rlgl.h @@ -209,6 +209,28 @@ typedef enum { OPENGL_11 = 1, OPENGL_33, OPENGL_ES_20 } GlVersion; float glossiness; float normalDepth; } Material; + + // Light type + // TODO: Review contained data to support different light types and features + typedef struct LightData { + int id; + int type; // LIGHT_POINT, LIGHT_DIRECTIONAL, LIGHT_SPOT + bool enabled; + + Vector3 position; + Vector3 direction; // Used on LIGHT_DIRECTIONAL and LIGHT_SPOT (cone direction) + float attenuation; // Lost of light intensity with distance (use radius?) + + Color diffuse; // Use Vector3 diffuse (including intensities)? + float intensity; + + Color specular; + //float specFactor; // Specular intensity ? + + //Color ambient; // Required? + + float coneAngle; // SpotLight + } LightData, *Light; // Color blending modes (pre-defined) typedef enum { BLEND_ALPHA = 0, BLEND_ADDITIVE, BLEND_MULTIPLIED } BlendMode; @@ -311,6 +333,9 @@ void SetShaderValuei(Shader shader, int uniformLoc, int *value, int size); // S void SetShaderValueMatrix(Shader shader, int uniformLoc, Matrix mat); // Set shader uniform value (matrix 4x4) void SetBlendMode(int mode); // Set blending mode (alpha, additive, multiplied) + +Light CreateLight(int type, Vector3 position, Color diffuse); // Create a new light, initialize it and add to pool +void DestroyLight(Light light); // Destroy a light and take it out of the list #endif #ifdef __cplusplus -- cgit v1.2.3