aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorraysan5 <raysan5@gmail.com>2013-11-30 18:12:40 +0100
committerraysan5 <raysan5@gmail.com>2013-11-30 18:12:40 +0100
commit294533ccda115b5e638ab1f9219f86d73c9c4550 (patch)
tree79231c1eba341e091dcbcb5c8182c27222af3195 /src
parentc7220ab1b3692d81c999fcf37afdf9908d728350 (diff)
downloadraylib-294533ccda115b5e638ab1f9219f86d73c9c4550.tar.gz
raylib-294533ccda115b5e638ab1f9219f86d73c9c4550.zip
Updated to version 1.0.2
Some functions added (collision detection) Check CHANGELOG for details
Diffstat (limited to 'src')
-rw-r--r--src/models.c40
-rw-r--r--src/raylib.h8
-rw-r--r--src/shapes.c99
-rw-r--r--src/textures.c30
4 files changed, 170 insertions, 7 deletions
diff --git a/src/models.c b/src/models.c
index e15ea31f..8e4f3555 100644
--- a/src/models.c
+++ b/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));
diff --git a/src/raylib.h b/src/raylib.h
index 54065dd0..7f14113f 100644
--- a/src/raylib.h
+++ b/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)
diff --git a/src/shapes.c b/src/shapes.c
index ea55f198..56467665 100644
--- a/src/shapes.c
+++ b/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 - raysan@raysanweb.com)
*
@@ -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;
+}
diff --git a/src/textures.c b/src/textures.c
index 3ccb5358..19eb09b4 100644
--- a/src/textures.c
+++ b/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)
{