浏览代码

Updated to version 1.0.2

Some functions added (collision detection)
Check CHANGELOG for details
raysan5 11 年之前
父节点
当前提交
294533ccda
共有 7 个文件被更改,包括 179 次插入9 次删除
  1. 1 1
      README.md
  2. 8 1
      release/win32-mingw/include/raylib.h
  3. 二进制
      release/win32-mingw/lib/libraylib.a
  4. 36 4
      src/models.c
  5. 7 1
      src/raylib.h
  6. 97 2
      src/shapes.c
  7. 30 0
      src/textures.c

+ 1 - 1
README.md

@@ -60,7 +60,7 @@ raylib has been developed using exclusively two tools:
    * Notepad++ (text editor) - [http://notepad-plus-plus.org/](http://notepad-plus-plus.org/)
    * MinGW (GCC compiler) - [http://www.mingw.org/](http://www.mingw.org/)
    
-Those are the tools I recommended to develop with raylib, actually, my students develop using this tools. 
+Those are the tools I recommend to develop with raylib, in fact, those are the tools my students use. 
 I believe those are the best tools to train spartan-programmers.
 
 Someone could argue about debugging. raylib is a library intended for learning and I think C it's a clear enough language

+ 8 - 1
release/win32-mingw/include/raylib.h

@@ -1,6 +1,6 @@
 /*********************************************************************************************
 * 
-*   raylib 1.0.1 (www.raylib.com)
+*   raylib 1.0.2 (www.raylib.com)
 *    
 *   A simple and easy-to-use library to learn C videogames programming
 *
@@ -301,6 +301,11 @@ void DrawTriangleLines(Vector2 v1, Vector2 v2, Vector2 v3, Color color);
 void DrawPoly(Vector2 *points, int numPoints, Color color);                                        // Draw a closed polygon defined by points
 void DrawPolyLine(Vector2 *points, int numPoints, Color color);                                    // Draw polygon lines
 
+bool CheckCollisionRecs(Rectangle rec1, Rectangle rec2);                                           // Check collision between two rectangles
+bool CheckCollisionCircles(Vector2 center1, float radius1, Vector2 center2, float radius2);        // Check collision between two circles
+bool CheckCollisionCircleRec(Vector2 center, float radius, Rectangle rec);                         // Check collision between circle and rectangle
+Rectangle GetCollisionRec(Rectangle rec1, Rectangle rec2);                                         // Get collision rectangle for two rectangles collision
+
 //------------------------------------------------------------------------------------
 // Texture Loading and Drawing Functions (Module: textures)
 //------------------------------------------------------------------------------------
@@ -313,10 +318,12 @@ void DrawTexture(Texture2D texture, int posX, int posY, Color tint);
 void DrawTextureEx(Texture2D texture, Vector2 position, float rotation, float scale, Color tint);  // Draw a Texture2D with extended parameters
 void DrawTextureRec(Texture2D texture, Rectangle sourceRec, Vector2 position, Color tint);         // Draw a part of a texture defined by a rectangle
 void DrawTexturePro(Texture2D texture, Rectangle sourceRec, Rectangle destRec, Vector2 origin, float rotation, Color tint); // Draw a part of a texture defined by a rectangle with 'pro' parameters
+Texture2D CreateTexture2D(Image image);                                                            // Create a Texture2D from Image data
 
 //------------------------------------------------------------------------------------
 // Font Loading and Text Drawing Functions (Module: text)
 //------------------------------------------------------------------------------------
+SpriteFont GetDefaultFont();                                                                       // Get the default SpriteFont
 SpriteFont LoadSpriteFont(const char *fileName);                                                   // Load a SpriteFont image into GPU memory
 void UnloadSpriteFont(SpriteFont spriteFont);                                                      // Unload SpriteFont from GPU memory
 void DrawText(const char *text, int posX, int posY, int fontSize, Color color);                    // Draw text (using default font)

二进制
release/win32-mingw/lib/libraylib.a


+ 36 - 4
src/models.c

@@ -662,6 +662,38 @@ Model LoadModel(const char *fileName)
     return model;
 }
 
+// Load a heightmap image as a 3d model
+// TODO: Just do it...
+Model LoadHeightmap(Image heightmap, Vector3 resolution)
+{
+    Model model;
+    
+    int mapX = heightmap.width;
+	int mapZ = heightmap.height;
+    
+    // NOTE: One vertex per pixel
+    // TODO: Consider resolution when generating model data?
+    int numTriangles = (mapX-1)*(mapZ-1)*2;    // One quad every four pixels
+  
+    model.numVertices = numTriangles*3;
+
+    model.vertices = (Vector3 *)malloc(model.numVertices * sizeof(Vector3));
+    model.normals = (Vector3 *)malloc(model.numVertices * sizeof(Vector3));
+    model.texcoords = (Vector2 *)malloc(model.numVertices * sizeof(Vector2));
+
+	for(int z = 0; z < mapZ; z++)
+	{
+		for(int x = 0; x < mapX; x++)
+		{
+			// TODO: Fill vertices array with data
+		}
+	}
+
+	//SmoothHeightmap(&model);    // TODO: Smooth vertex interpolation
+    
+    return model;
+}
+
 // Unload 3d model from memory
 void UnloadModel(Model model)
 {
@@ -743,9 +775,9 @@ void DrawBillboard(Camera camera, Texture2D texture, Vector3 basePos, float size
 void DrawBillboardRec(Camera camera, Texture2D texture, Rectangle sourceRec, Vector3 basePos, float size, Color tint)
 {
     // NOTE: Billboard size will represent the width, height maintains aspect ratio
-    Vector3 centerPos = { basePos.x, basePos.y + size * (float)texture.height/(float)texture.width/2, basePos.z };
-    Vector2 sizeRatio = { size, size * (float)texture.height/texture.width };
-    Vector3 rotation = { 90, 0, 0 };
+    //Vector3 centerPos = { basePos.x, basePos.y + size * (float)texture.height/(float)texture.width/2, basePos.z };
+    //Vector2 sizeRatio = { size, size * (float)texture.height/texture.width };
+    //Vector3 rotation = { 90, 0, 0 };
     
     // TODO: Calculate Y rotation to face always camera (use matrix)
     // OPTION: Lock Y-axis
@@ -766,7 +798,7 @@ void DrawHeightmap(Image heightmap, Vector3 centerPos, Vector3 scale, Color colo
     // NOTE: Heightmap resolution will depend on image size (one quad per pixel)
 
     // TODO: Review how this function works... probably we need:
-    // Model LoadHeightmap(Image image, Vector3 resolution);
+    // Model LoadHeightmap(Image heightmap, Vector3 resolution);
     
     // NOTE: We are allocating and de-allocating vertex data every frame! --> framerate drops 80%! CRAZY!
     Vector3 *terrainVertex = (Vector3 *)malloc(heightmap.width * heightmap.height * sizeof(Vector3));

+ 7 - 1
src/raylib.h

@@ -1,6 +1,6 @@
 /*********************************************************************************************
 * 
-*   raylib 1.0.1 (www.raylib.com)
+*   raylib 1.0.2 (www.raylib.com)
 *    
 *   A simple and easy-to-use library to learn C videogames programming
 *
@@ -301,6 +301,11 @@ void DrawTriangleLines(Vector2 v1, Vector2 v2, Vector2 v3, Color color);
 void DrawPoly(Vector2 *points, int numPoints, Color color);                                        // Draw a closed polygon defined by points
 void DrawPolyLine(Vector2 *points, int numPoints, Color color);                                    // Draw polygon lines
 
+bool CheckCollisionRecs(Rectangle rec1, Rectangle rec2);                                           // Check collision between two rectangles
+bool CheckCollisionCircles(Vector2 center1, float radius1, Vector2 center2, float radius2);        // Check collision between two circles
+bool CheckCollisionCircleRec(Vector2 center, float radius, Rectangle rec);                         // Check collision between circle and rectangle
+Rectangle GetCollisionRec(Rectangle rec1, Rectangle rec2);                                         // Get collision rectangle for two rectangles collision
+
 //------------------------------------------------------------------------------------
 // Texture Loading and Drawing Functions (Module: textures)
 //------------------------------------------------------------------------------------
@@ -313,6 +318,7 @@ void DrawTexture(Texture2D texture, int posX, int posY, Color tint);
 void DrawTextureEx(Texture2D texture, Vector2 position, float rotation, float scale, Color tint);  // Draw a Texture2D with extended parameters
 void DrawTextureRec(Texture2D texture, Rectangle sourceRec, Vector2 position, Color tint);         // Draw a part of a texture defined by a rectangle
 void DrawTexturePro(Texture2D texture, Rectangle sourceRec, Rectangle destRec, Vector2 origin, float rotation, Color tint); // Draw a part of a texture defined by a rectangle with 'pro' parameters
+Texture2D CreateTexture2D(Image image);                                                            // Create a Texture2D from Image data
 
 //------------------------------------------------------------------------------------
 // Font Loading and Text Drawing Functions (Module: text)

+ 97 - 2
src/shapes.c

@@ -2,7 +2,7 @@
 *
 *   raylib.shapes
 *
-*   Basic functions to draw 2d Shapes
+*   Basic functions to draw 2d Shapes and check collisions
 *     
 *   Copyright (c) 2013 Ramon Santamaria (Ray San - [email protected])
 *    
@@ -26,7 +26,9 @@
 #include "raylib.h"
 
 #include <GL/gl.h>      // OpenGL functions
+#include <stdlib.h>     // Required for abs() function
 #include <math.h>       // Math related functions, sin() and cos() used on DrawCircle*
+                        // sqrt() and pow() and abs() used on CheckCollision*
 
 //----------------------------------------------------------------------------------
 // Defines and Macros
@@ -323,4 +325,97 @@ void DrawPolyLine(Vector2 *points, int numPoints, Color color)
         
         //glDisable(GL_LINE_SMOOTH);
     }
-}                                                
+}
+
+// Check collision between two rectangles
+bool CheckCollisionRecs(Rectangle rec1, Rectangle rec2)
+{
+    bool collision = false;
+    
+    int dx = abs((rec1.x + rec1.width / 2) - (rec2.x + rec2.width / 2));
+    int dy = abs((rec1.y + rec1.height / 2) - (rec2.y + rec2.height / 2));
+    
+    if ((dx <= (rec1.width / 2 + rec2.width / 2)) && ((dy <= (rec1.height / 2 + rec2.height / 2)))) collision = true;
+    
+    return collision;
+}
+
+// Check collision between two circles
+bool CheckCollisionCircles(Vector2 center1, float radius1, Vector2 center2, float radius2)
+{
+    bool collision = false;
+    
+    float dx = center2.x - center1.x;      // X distance between centers
+    float dy = center2.y - center1.y;      // Y distance between centers
+    
+    float distance = sqrt(dx*dx + dy*dy);  // Distance between centers
+    
+    if (distance <= (radius1 + radius2)) collision = true;
+    
+    return collision;
+}
+
+// Check collision between circle and rectangle
+bool CheckCollisionCircleRec(Vector2 center, float radius, Rectangle rec)
+{
+    bool collision = false;
+    
+    float dx = abs((rec.x + rec.width / 2) - center.x);
+    float dy = abs((rec.y + rec.height / 2) - center.y);
+    
+    if ((dx <= (rec.width / 2 + radius)) && (dy <= (rec.height / 2 + radius))) collision = true;
+    
+    return collision;
+}
+
+// Get collision rectangle for two rectangles collision
+Rectangle GetCollisionRec(Rectangle rec1, Rectangle rec2)
+{
+    Rectangle retRec = { 0, 0, 0, 0 };
+    
+    if (CheckCollisionRecs(rec1, rec2))
+    {
+        int dxx = abs(rec1.x - rec2.x);
+        int dyy = abs(rec1.y - rec2.y);
+        
+        if (rec1.x <= rec2.x)
+        {
+            if (rec1.y <= rec2.y)
+            {	
+                retRec.x = rec2.x;
+                retRec.y = rec2.y;
+                retRec.width = rec1.width - dxx;
+                retRec.height = rec1.height - dyy;
+            }
+            else
+            {
+                retRec.x = rec2.x;
+                retRec.y = rec1.y;
+                retRec.width = rec1.width - dxx;
+                retRec.height = rec2.height - dyy;
+            }
+        }
+        else
+        {
+            if (rec1.y <= rec2.y)
+            {	
+                retRec.x = rec1.x;
+                retRec.y = rec2.y;
+                retRec.width = rec2.width - dxx;
+                retRec.height = rec1.height - dyy;
+            }
+            else
+            {
+                retRec.x = rec1.x;
+                retRec.y = rec1.y;
+                retRec.width = rec2.width - dxx;
+                retRec.height = rec2.height - dyy;
+            }
+        }
+        
+        if (retRec.width >= rec2.width) retRec.width = rec2.width;
+        if (retRec.height >= rec2.height) retRec.height = rec2.height;
+    }
+    
+    return retRec;
+}                                    

+ 30 - 0
src/textures.c

@@ -157,6 +157,36 @@ Texture2D LoadTextureEx(const char *fileName, bool createPOT, bool mipmaps)
     return texture;
 }
 
+// Create a Texture2D from Image data
+// NOTE: Image is not unloaded, it should be done manually...
+Texture2D CreateTexture2D(Image image)
+{
+    Texture2D texture;
+    
+    // Convert image data to OpenGL texture
+    //----------------------------------------
+    GLuint id;
+    glGenTextures(1, &id);         // Generate Pointer to the Texture
+    
+    glBindTexture(GL_TEXTURE_2D, id);
+    
+    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);       // Set texture to repead on x-axis
+    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);       // Set texture to repead on y-axis
+    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);  // Filter for pixel-perfect drawing, alternative: GL_LINEAR 
+    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);  // Filter for pixel-perfect drawing, alternative: GL_LINEAR
+    
+    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, image.width, image.height, 0, GL_RGBA, GL_UNSIGNED_BYTE, image.pixels);
+    
+    // NOTE: Not using mipmappings (texture for 2D drawing)
+    // At this point we have the image converted to texture and uploaded to GPU
+    
+    texture.glId = id;
+    texture.width = image.width;
+    texture.height = image.height;
+    
+    return texture;
+}
+
 // Unload texture from GPU memory
 void UnloadTexture(Texture2D texture)
 {