From ee52b13ae63f098c4e26a9812b2088f80fcbe7cc Mon Sep 17 00:00:00 2001 From: Ray Date: Wed, 16 Mar 2016 17:50:51 +0100 Subject: Corrected bug on GetCollisionRec() --- src/shapes.c | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/shapes.c b/src/shapes.c index 51730a05..46095d11 100644 --- a/src/shapes.c +++ b/src/shapes.c @@ -429,9 +429,24 @@ Rectangle GetCollisionRec(Rectangle rec1, Rectangle rec2) retRec.height = rec2.height - dyy; } } - - if (retRec.width >= rec2.width) retRec.width = rec2.width; - if (retRec.height >= rec2.height) retRec.height = rec2.height; + + if (rec1.width > rec2.width) + { + if (retRec.width >= rec2.width) retRec.width = rec2.width; + } + else + { + if (retRec.width >= rec1.width) retRec.width = rec1.width; + } + + if (rec1.height > rec2.height) + { + if (retRec.height >= rec2.height) retRec.height = rec2.height; + } + else + { + if (retRec.height >= rec1.height) retRec.height = rec1.height; + } } return retRec; -- cgit v1.2.3 From d6bc7b887721de660917125eddd53372be1bf3f8 Mon Sep 17 00:00:00 2001 From: Ray Date: Wed, 16 Mar 2016 17:51:21 +0100 Subject: Reset pointCount for gestures --- src/gestures.c | 2 ++ 1 file changed, 2 insertions(+) (limited to 'src') diff --git a/src/gestures.c b/src/gestures.c index 583b77cd..b0a80084 100644 --- a/src/gestures.c +++ b/src/gestures.c @@ -179,6 +179,7 @@ void ProcessGestureEvent(GestureEvent event) } touchDownDragPosition = (Vector2){ 0.0f, 0.0f }; + pointCount = 0; } else if (event.touchAction == TOUCH_MOVE) { @@ -257,6 +258,7 @@ void ProcessGestureEvent(GestureEvent event) pinchDistance = 0.0f; pinchAngle = 0.0f; pinchVector = (Vector2){ 0.0f, 0.0f }; + pointCount = 0; currentGesture = GESTURE_NONE; } -- cgit v1.2.3 From db4585b3e23cd3c5aa87da21aedc36fd8be21739 Mon Sep 17 00:00:00 2001 From: Ray Date: Wed, 16 Mar 2016 17:52:09 +0100 Subject: Improved gamepad support Now it works ok also in RaspberryPi --- src/core.c | 46 ++++++++++++++-------------------------------- src/raylib.h | 32 ++++++++++++++++++++++++++++++-- 2 files changed, 44 insertions(+), 34 deletions(-) (limited to 'src') diff --git a/src/core.c b/src/core.c index 6b2321c2..5bb59faa 100644 --- a/src/core.c +++ b/src/core.c @@ -127,6 +127,7 @@ #define MOUSE_SENSITIVITY 0.8f #define MAX_GAMEPAD_BUTTONS 11 + #define MAX_GAMEPAD_AXIS 5 #endif //---------------------------------------------------------------------------------- @@ -168,8 +169,7 @@ static bool gamepadReady = false; // Flag to know if gamepad is re pthread_t gamepadThreadId; // Gamepad reading thread id int gamepadButtons[MAX_GAMEPAD_BUTTONS]; -int gamepadAxisX = 0; -int gamepadAxisY = 0; +float gamepadAxisValues[MAX_GAMEPAD_AXIS]; #endif #if defined(PLATFORM_ANDROID) || defined(PLATFORM_RPI) @@ -1177,30 +1177,22 @@ bool IsGamepadAvailable(int gamepad) } // Return axis movement vector for a gamepad -Vector2 GetGamepadMovement(int gamepad) +float GetGamepadAxisMovement(int gamepad, int axis) { - Vector2 vec = { 0, 0 }; - - const float *axes; - int axisCount = 0; + float value = 0; #if defined(PLATFORM_RPI) - // TODO: Get gamepad axis information - // Use gamepadAxisX, gamepadAxisY + if (axis < MAX_GAMEPAD_AXIS) value = gamepadAxisValues[axis]; #else + const float *axes; + int axisCount = 0; + axes = glfwGetJoystickAxes(gamepad, &axisCount); -#endif - if (axisCount >= 2) - { - vec.x = axes[0]; // Left joystick X - vec.y = axes[1]; // Left joystick Y - - //vec.x = axes[2]; // Right joystick X - //vec.x = axes[3]; // Right joystick Y - } + if (axis < axisCount) value = axes[axis]; +#endif - return vec; + return value; } // Detect if a gamepad button has been pressed once @@ -2484,10 +2476,6 @@ static void *GamepadThread(void *arg) unsigned char number; // event axis/button number }; - // These values are sensible on Logitech Dual Action Rumble and Xbox360 controller - const int joystickAxisX = 0; - const int joystickAxisY = 1; - // Read gamepad event struct js_event gamepadEvent; @@ -2512,17 +2500,11 @@ static void *GamepadThread(void *arg) { TraceLog(DEBUG, "Gamepad axis: %i, value: %i", gamepadEvent.number, gamepadEvent.value); - if (gamepadEvent.number == joystickAxisX) gamepadAxisX = (int)gamepadEvent.value; - if (gamepadEvent.number == joystickAxisY) gamepadAxisY = (int)gamepadEvent.value; - /* - switch (gamepadEvent.number) + if (gamepadEvent.number < MAX_GAMEPAD_AXIS) { - case 0: // 1st Axis X - case 1: // 1st Axis Y - case 2: // 2st Axis X - case 3: // 2st Axis Y + // NOTE: Scaling of gamepadEvent.value to get values between -1..1 + gamepadAxisValues[gamepadEvent.number] = (float)gamepadEvent.value/32768; } - */ } } } diff --git a/src/raylib.h b/src/raylib.h index ddfccea7..d891467e 100644 --- a/src/raylib.h +++ b/src/raylib.h @@ -190,7 +190,35 @@ #define GAMEPAD_BUTTON_SELECT 9 #define GAMEPAD_BUTTON_START 10 -// TODO: Review Xbox360 USB Controller Buttons +// Xbox360 USB Controller Buttons +#define GAMEPAD_XBOX_BUTTON_A 0 +#define GAMEPAD_XBOX_BUTTON_B 1 +#define GAMEPAD_XBOX_BUTTON_X 2 +#define GAMEPAD_XBOX_BUTTON_Y 3 +#define GAMEPAD_XBOX_BUTTON_LB 4 +#define GAMEPAD_XBOX_BUTTON_RB 5 +#define GAMEPAD_XBOX_BUTTON_SELECT 6 +#define GAMEPAD_XBOX_BUTTON_START 7 + +#if defined(PLATFORM_RPI) + #define GAMEPAD_XBOX_AXIS_DPAD_X 32 + #define GAMEPAD_XBOX_AXIS_DPAD_Y 64 + #define GAMEPAD_XBOX_AXIS_RIGHT_X 3 + #define GAMEPAD_XBOX_AXIS_RIGHT_Y 4 + #define GAMEPAD_XBOX_AXIS_LT 2 + #define GAMEPAD_XBOX_AXIS_RT 5 +#else + #define GAMEPAD_XBOX_BUTTON_UP 10 + #define GAMEPAD_XBOX_BUTTON_DOWN 12 + #define GAMEPAD_XBOX_BUTTON_LEFT 13 + #define GAMEPAD_XBOX_BUTTON_RIGHT 11 + #define GAMEPAD_XBOX_AXIS_RIGHT_X 4 + #define GAMEPAD_XBOX_AXIS_RIGHT_Y 3 + #define GAMEPAD_XBOX_AXIS_LT_RT 2 +#endif + +#define GAMEPAD_XBOX_AXIS_LEFT_X 0 +#define GAMEPAD_XBOX_AXIS_LEFT_Y 1 // Android Physic Buttons #define ANDROID_BACK 4 @@ -592,7 +620,7 @@ 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 -Vector2 GetGamepadMovement(int gamepad); // Return axis movement vector for a gamepad +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 -- cgit v1.2.3 From 95c1bf954423f00a4fb8c6dc72820c2174d62dfa Mon Sep 17 00:00:00 2001 From: Ray Date: Wed, 16 Mar 2016 19:10:19 +0100 Subject: Removed previous change that introduced a bug --- src/shapes.c | 45 ++++++++++++++++++++++++++++++++++++--------- 1 file changed, 36 insertions(+), 9 deletions(-) (limited to 'src') diff --git a/src/shapes.c b/src/shapes.c index 46095d11..7e3b5634 100644 --- a/src/shapes.c +++ b/src/shapes.c @@ -180,17 +180,44 @@ void DrawRectangleGradient(int posX, int posY, int width, int height, Color colo // Draw a color-filled rectangle (Vector version) void DrawRectangleV(Vector2 position, Vector2 size, Color color) { - rlBegin(RL_TRIANGLES); - rlColor4ub(color.r, color.g, color.b, color.a); + if (rlGetVersion() == OPENGL_11) + { + rlBegin(RL_TRIANGLES); + rlColor4ub(color.r, color.g, color.b, color.a); - rlVertex2i(position.x, position.y); - rlVertex2i(position.x, position.y + size.y); - rlVertex2i(position.x + size.x, position.y + size.y); + rlVertex2i(position.x, position.y); + rlVertex2i(position.x, position.y + size.y); + rlVertex2i(position.x + size.x, position.y + size.y); - rlVertex2i(position.x, position.y); - rlVertex2i(position.x + size.x, position.y + size.y); - rlVertex2i(position.x + size.x, position.y); - rlEnd(); + rlVertex2i(position.x, position.y); + rlVertex2i(position.x + size.x, position.y + size.y); + rlVertex2i(position.x + size.x, position.y); + rlEnd(); + } + else if ((rlGetVersion() == OPENGL_33) || (rlGetVersion() == OPENGL_ES_20)) + { + // NOTE: This shape uses QUADS to avoid drawing order issues (view rlglDraw) + rlEnableTexture(whiteTexture); // Default white texture + + rlBegin(RL_QUADS); + rlColor4ub(color.r, color.g, color.b, color.a); + rlNormal3f(0.0f, 0.0f, 1.0f); + + rlTexCoord2f(0.0f, 0.0f); + rlVertex2f(position.x, position.y); + + rlTexCoord2f(0.0f, 1.0f); + rlVertex2f(position.x, position.y + size.y); + + rlTexCoord2f(1.0f, 1.0f); + rlVertex2f(position.x + size.x, position.y + size.y); + + rlTexCoord2f(1.0f, 0.0f); + rlVertex2f(position.x + size.x, position.y); + rlEnd(); + + rlDisableTexture(); + } } // Draw rectangle outline -- cgit v1.2.3 From 49df957058b2f602c7e6873fec0007fcd7a4dc4c Mon Sep 17 00:00:00 2001 From: Ray Date: Thu, 17 Mar 2016 12:54:36 +0100 Subject: Add support for multiple gamepads on RPI --- src/core.c | 102 +++++++++++++++++++++++++++++++++++------------------------ src/raylib.h | 8 ++--- 2 files changed, 65 insertions(+), 45 deletions(-) (limited to 'src') diff --git a/src/core.c b/src/core.c index 5bb59faa..7d229324 100644 --- a/src/core.c +++ b/src/core.c @@ -116,9 +116,9 @@ #if defined(PLATFORM_RPI) // Old device inputs system - #define DEFAULT_KEYBOARD_DEV STDIN_FILENO // Standard input - #define DEFAULT_MOUSE_DEV "/dev/input/mouse0" - #define DEFAULT_GAMEPAD_DEV "/dev/input/js0" + #define DEFAULT_KEYBOARD_DEV STDIN_FILENO // Standard input + #define DEFAULT_MOUSE_DEV "/dev/input/mouse0" // Mouse input + #define DEFAULT_GAMEPAD_DEV "/dev/input/js" // Gamepad input (base dev for all gamepads: js0, js1, ...) // New device input events (evdev) (must be detected) //#define DEFAULT_KEYBOARD_DEV "/dev/input/eventN" @@ -126,8 +126,10 @@ //#define DEFAULT_GAMEPAD_DEV "/dev/input/eventN" #define MOUSE_SENSITIVITY 0.8f - #define MAX_GAMEPAD_BUTTONS 11 - #define MAX_GAMEPAD_AXIS 5 + + #define MAX_GAMEPADS 2 // Max number of gamepads supported + #define MAX_GAMEPAD_BUTTONS 11 // Max bumber of buttons supported (per gamepad) + #define MAX_GAMEPAD_AXIS 8 // Max number of axis supported (per gamepad) #endif //---------------------------------------------------------------------------------- @@ -164,12 +166,12 @@ static bool mouseReady = false; // Flag to know if mouse is read pthread_t mouseThreadId; // Mouse reading thread id // Gamepad input variables -static int gamepadStream = -1; // Gamepad device file descriptor -static bool gamepadReady = false; // Flag to know if gamepad is ready -pthread_t gamepadThreadId; // Gamepad reading thread id +static int gamepadStream[MAX_GAMEPADS] = { -1 }; // Gamepad device file descriptor (two gamepads supported) +static bool gamepadReady[MAX_GAMEPADS] = { false }; // Flag to know if gamepad is ready (two gamepads supported) +pthread_t gamepadThreadId; // Gamepad reading thread id -int gamepadButtons[MAX_GAMEPAD_BUTTONS]; -float gamepadAxisValues[MAX_GAMEPAD_AXIS]; +int gamepadButtons[MAX_GAMEPADS][MAX_GAMEPAD_BUTTONS]; // Gamepad buttons state +float gamepadAxisValues[MAX_GAMEPADS][MAX_GAMEPAD_AXIS]; // Gamepad axis state #endif #if defined(PLATFORM_ANDROID) || defined(PLATFORM_RPI) @@ -1168,7 +1170,7 @@ bool IsGamepadAvailable(int gamepad) bool result = false; #if defined(PLATFORM_RPI) - if (gamepadReady && (gamepad == 0)) result = true; + if ((gamepad < MAX_GAMEPADS) && gamepadReady[gamepad]) result = true; #else if (glfwJoystickPresent(gamepad) == 1) result = true; #endif @@ -1182,7 +1184,10 @@ float GetGamepadAxisMovement(int gamepad, int axis) float value = 0; #if defined(PLATFORM_RPI) - if (axis < MAX_GAMEPAD_AXIS) value = gamepadAxisValues[axis]; + if ((gamepad < MAX_GAMEPADS) && gamepadReady[gamepad]) + { + if (axis < MAX_GAMEPAD_AXIS) value = gamepadAxisValues[gamepad][axis]; + } #else const float *axes; int axisCount = 0; @@ -1219,7 +1224,7 @@ bool IsGamepadButtonDown(int gamepad, int button) #if defined(PLATFORM_RPI) // Get gamepad buttons information - if ((gamepad == 0) && (gamepadButtons[button] == 1)) result = true; + if ((gamepad < MAX_GAMEPADS) && gamepadReady[gamepad] && (gamepadButtons[gamepad][button] == 1)) result = true; else result = false; #else const unsigned char *buttons; @@ -1258,7 +1263,7 @@ bool IsGamepadButtonUp(int gamepad, int button) #if defined(PLATFORM_RPI) // Get gamepad buttons information - if ((gamepad == 0) && (gamepadButtons[button] == 0)) result = true; + if ((gamepad < MAX_GAMEPADS) && gamepadReady[gamepad] && (gamepadButtons[gamepad][button] == 0)) result = true; else result = false; #else const unsigned char *buttons; @@ -2447,19 +2452,31 @@ static void *MouseThread(void *arg) // Init gamepad system static void InitGamepad(void) { - if ((gamepadStream = open(DEFAULT_GAMEPAD_DEV, O_RDONLY|O_NONBLOCK)) < 0) - { - TraceLog(WARNING, "Gamepad device could not be opened, no gamepad available"); - } - else + char gamepadDev[128] = ""; + + for (int i = 0; i < MAX_GAMEPADS; i++) { - gamepadReady = true; + sprintf(gamepadDev, "%s%i", DEFAULT_GAMEPAD_DEV, i); + + if ((gamepadStream[i] = open(gamepadDev, O_RDONLY|O_NONBLOCK)) < 0) + { + // NOTE: Only show message for first gamepad + if (i == 0) TraceLog(WARNING, "Gamepad device could not be opened, no gamepad available"); + } + else + { + gamepadReady[i] = true; - int error = pthread_create(&gamepadThreadId, NULL, &GamepadThread, NULL); + // NOTE: Only create one thread + if (i == 0) + { + int error = pthread_create(&gamepadThreadId, NULL, &GamepadThread, NULL); - if (error != 0) TraceLog(WARNING, "Error creating gamepad input event thread"); - else TraceLog(INFO, "Gamepad device initialized successfully"); - } + if (error != 0) TraceLog(WARNING, "Error creating gamepad input event thread"); + else TraceLog(INFO, "Gamepad device initialized successfully"); + } + } + } } // Process Gamepad (/dev/input/js0) @@ -2481,29 +2498,32 @@ static void *GamepadThread(void *arg) while (1) { - if (read(gamepadStream, &gamepadEvent, sizeof(struct js_event)) == (int)sizeof(struct js_event)) + for (int i = 0; i < MAX_GAMEPADS; i++) { - gamepadEvent.type &= ~JS_EVENT_INIT; // Ignore synthetic events - - // Process gamepad events by type - if (gamepadEvent.type == JS_EVENT_BUTTON) + if (read(gamepadStream[i], &gamepadEvent, sizeof(struct js_event)) == (int)sizeof(struct js_event)) { - TraceLog(DEBUG, "Gamepad button: %i, value: %i", gamepadEvent.number, gamepadEvent.value); + gamepadEvent.type &= ~JS_EVENT_INIT; // Ignore synthetic events - if (gamepadEvent.number < MAX_GAMEPAD_BUTTONS) + // Process gamepad events by type + if (gamepadEvent.type == JS_EVENT_BUTTON) { - // 1 - button pressed, 0 - button released - gamepadButtons[gamepadEvent.number] = (int)gamepadEvent.value; + TraceLog(DEBUG, "Gamepad button: %i, value: %i", gamepadEvent.number, gamepadEvent.value); + + if (gamepadEvent.number < MAX_GAMEPAD_BUTTONS) + { + // 1 - button pressed, 0 - button released + gamepadButtons[i][gamepadEvent.number] = (int)gamepadEvent.value; + } } - } - else if (gamepadEvent.type == JS_EVENT_AXIS) - { - TraceLog(DEBUG, "Gamepad axis: %i, value: %i", gamepadEvent.number, gamepadEvent.value); - - if (gamepadEvent.number < MAX_GAMEPAD_AXIS) + else if (gamepadEvent.type == JS_EVENT_AXIS) { - // NOTE: Scaling of gamepadEvent.value to get values between -1..1 - gamepadAxisValues[gamepadEvent.number] = (float)gamepadEvent.value/32768; + TraceLog(DEBUG, "Gamepad axis: %i, value: %i", gamepadEvent.number, gamepadEvent.value); + + if (gamepadEvent.number < MAX_GAMEPAD_AXIS) + { + // NOTE: Scaling of gamepadEvent.value to get values between -1..1 + gamepadAxisValues[i][gamepadEvent.number] = (float)gamepadEvent.value/32768; + } } } } diff --git a/src/raylib.h b/src/raylib.h index d891467e..a87b58da 100644 --- a/src/raylib.h +++ b/src/raylib.h @@ -174,8 +174,8 @@ // Gamepad Number #define GAMEPAD_PLAYER1 0 #define GAMEPAD_PLAYER2 1 -#define GAMEPAD_PLAYER3 2 -#define GAMEPAD_PLAYER4 3 +#define GAMEPAD_PLAYER3 2 // Not supported +#define GAMEPAD_PLAYER4 3 // Not supported // Gamepad Buttons // NOTE: Adjusted for a PS3 USB Controller @@ -201,8 +201,8 @@ #define GAMEPAD_XBOX_BUTTON_START 7 #if defined(PLATFORM_RPI) - #define GAMEPAD_XBOX_AXIS_DPAD_X 32 - #define GAMEPAD_XBOX_AXIS_DPAD_Y 64 + #define GAMEPAD_XBOX_AXIS_DPAD_X 7 + #define GAMEPAD_XBOX_AXIS_DPAD_Y 6 #define GAMEPAD_XBOX_AXIS_RIGHT_X 3 #define GAMEPAD_XBOX_AXIS_RIGHT_Y 4 #define GAMEPAD_XBOX_AXIS_LT 2 -- cgit v1.2.3 From e2ba22ec596757d62f8b22cf8b722d68040f23d3 Mon Sep 17 00:00:00 2001 From: Ray Date: Thu, 17 Mar 2016 13:51:48 +0100 Subject: Improved 2D-3D drawing Depth test disabled for 2D and only used on 3D; consequently LINES vs TRIANGLES vs QUADS buffers drawing order maters... but blending also works ok. --- src/core.c | 4 +++ src/rlgl.c | 14 ++++++++++- src/rlgl.h | 2 ++ src/shapes.c | 80 +++++++++++++++++++++++++++++++++++++++++++----------------- 4 files changed, 76 insertions(+), 24 deletions(-) (limited to 'src') diff --git a/src/core.c b/src/core.c index 7d229324..d27a031b 100644 --- a/src/core.c +++ b/src/core.c @@ -643,6 +643,8 @@ void Begin3dMode(Camera camera) // Setup Camera view Matrix matView = MatrixLookAt(camera.position, camera.target, camera.up); rlMultMatrixf(MatrixToFloat(matView)); // Multiply MODELVIEW matrix by view matrix (camera) + + rlEnableDepthTest(); // Enable DEPTH_TEST for 3D } // Ends 3D mode and returns to default 2D orthographic mode @@ -657,6 +659,8 @@ void End3dMode(void) rlLoadIdentity(); // Reset current matrix (MODELVIEW) //rlTranslatef(0.375, 0.375, 0); // HACK to ensure pixel-perfect drawing on OpenGL (after exiting 3D mode) + + rlDisableDepthTest(); // Disable DEPTH_TEST for 2D } // Set target FPS for the game diff --git a/src/rlgl.c b/src/rlgl.c index d9761732..f9722eda 100644 --- a/src/rlgl.c +++ b/src/rlgl.c @@ -780,6 +780,18 @@ void rlDisableTexture(void) #endif } +// Enable depth test +void rlEnableDepthTest(void) +{ + glEnable(GL_DEPTH_TEST); +} + +// Disable depth test +void rlDisableDepthTest(void) +{ + glDisable(GL_DEPTH_TEST); +} + // Unload texture from GPU memory void rlDeleteTextures(unsigned int id) { @@ -1579,7 +1591,7 @@ void rlglInitGraphics(int offsetX, int offsetY, int width, int height) //glClearDepth(1.0f); // Clear depth buffer (default) glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear used buffers, depth buffer is used for 3D - glEnable(GL_DEPTH_TEST); // Enables depth testing (required for 3D) + glDisable(GL_DEPTH_TEST); // Disable depth testing for 2D (only used for 3D) glDepthFunc(GL_LEQUAL); // Type of depth testing to apply glEnable(GL_BLEND); // Enable color blending (required to work with transparencies) diff --git a/src/rlgl.h b/src/rlgl.h index 69640feb..7d50b67a 100644 --- a/src/rlgl.h +++ b/src/rlgl.h @@ -244,6 +244,8 @@ 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 rlEnableDepthTest(void); // Enable depth test +void rlDisableDepthTest(void); // Disable depth test void rlDeleteTextures(unsigned int id); // Delete OpenGL texture 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 diff --git a/src/shapes.c b/src/shapes.c index 7e3b5634..14d11315 100644 --- a/src/shapes.c +++ b/src/shapes.c @@ -98,7 +98,7 @@ void DrawLineV(Vector2 startPos, Vector2 endPos, Color color) // Draw a color-filled circle void DrawCircle(int centerX, int centerY, float radius, Color color) { - DrawPoly((Vector2){ centerX, centerY }, 36, radius, 0, color); + DrawCircleV((Vector2){ centerX, centerY }, radius, color); } // Draw a gradient-filled circle @@ -119,17 +119,40 @@ void DrawCircleGradient(int centerX, int centerY, float radius, Color color1, Co } // Draw a color-filled circle (Vector version) +// NOTE: On OpenGL 3.3 and ES2 we use QUADS to avoid drawing order issues (view rlglDraw) void DrawCircleV(Vector2 center, float radius, Color color) { - rlBegin(RL_TRIANGLES); - for (int i = 0; i < 360; i += 10) - { - rlColor4ub(color.r, color.g, color.b, color.a); - rlVertex2i(center.x, center.y); - rlVertex2f(center.x + sin(DEG2RAD*i)*radius, center.y + cos(DEG2RAD*i)*radius); - rlVertex2f(center.x + sin(DEG2RAD*(i + 10)) * radius, center.y + cos(DEG2RAD*(i + 10)) * radius); - } - rlEnd(); + if (rlGetVersion() == OPENGL_11) + { + rlBegin(RL_TRIANGLES); + for (int i = 0; i < 360; i += 10) + { + rlColor4ub(color.r, color.g, color.b, color.a); + + rlVertex2i(center.x, center.y); + rlVertex2f(center.x + sin(DEG2RAD*i)*radius, center.y + cos(DEG2RAD*i)*radius); + rlVertex2f(center.x + sin(DEG2RAD*(i + 10)) * radius, center.y + cos(DEG2RAD*(i + 10)) * radius); + } + rlEnd(); + } + else if ((rlGetVersion() == OPENGL_33) || (rlGetVersion() == OPENGL_ES_20)) + { + rlEnableTexture(whiteTexture); // Default white texture + + rlBegin(RL_QUADS); + for (int i = 0; i < 360; i += 20) + { + rlColor4ub(color.r, color.g, color.b, color.a); + + rlVertex2i(center.x, center.y); + rlVertex2f(center.x + sin(DEG2RAD*i)*radius, center.y + cos(DEG2RAD*i)*radius); + rlVertex2f(center.x + sin(DEG2RAD*(i + 10)) * radius, center.y + cos(DEG2RAD*(i + 10)) * radius); + rlVertex2f(center.x + sin(DEG2RAD*(i + 20)) * radius, center.y + cos(DEG2RAD*(i + 20)) * radius); + } + rlEnd(); + + rlDisableTexture(); + } } // Draw circle outline @@ -178,6 +201,7 @@ void DrawRectangleGradient(int posX, int posY, int width, int height, Color colo } // Draw a color-filled rectangle (Vector version) +// NOTE: On OpenGL 3.3 and ES2 we use QUADS to avoid drawing order issues (view rlglDraw) void DrawRectangleV(Vector2 position, Vector2 size, Color color) { if (rlGetVersion() == OPENGL_11) @@ -196,7 +220,6 @@ void DrawRectangleV(Vector2 position, Vector2 size, Color color) } else if ((rlGetVersion() == OPENGL_33) || (rlGetVersion() == OPENGL_ES_20)) { - // NOTE: This shape uses QUADS to avoid drawing order issues (view rlglDraw) rlEnableTexture(whiteTexture); // Default white texture rlBegin(RL_QUADS); @@ -221,22 +244,33 @@ void DrawRectangleV(Vector2 position, Vector2 size, Color color) } // Draw rectangle outline +// NOTE: On OpenGL 3.3 and ES2 we use QUADS to avoid drawing order issues (view rlglDraw) void DrawRectangleLines(int posX, int posY, int width, int height, Color color) -{ - rlBegin(RL_LINES); - rlColor4ub(color.r, color.g, color.b, color.a); - rlVertex2i(posX + 1, posY + 1); - rlVertex2i(posX + width, posY + 1); +{ + if (rlGetVersion() == OPENGL_11) + { + rlBegin(RL_LINES); + rlColor4ub(color.r, color.g, color.b, color.a); + rlVertex2i(posX + 1, posY + 1); + rlVertex2i(posX + width, posY + 1); - rlVertex2i(posX + width, posY + 1); - rlVertex2i(posX + width, posY + height); + rlVertex2i(posX + width, posY + 1); + rlVertex2i(posX + width, posY + height); - rlVertex2i(posX + width, posY + height); - rlVertex2i(posX + 1, posY + height); + rlVertex2i(posX + width, posY + height); + rlVertex2i(posX + 1, posY + height); - rlVertex2i(posX + 1, posY + height); - rlVertex2i(posX + 1, posY + 1); - rlEnd(); + rlVertex2i(posX + 1, posY + height); + rlVertex2i(posX + 1, posY + 1); + rlEnd(); + } + else if ((rlGetVersion() == OPENGL_33) || (rlGetVersion() == OPENGL_ES_20)) + { + DrawRectangle(posX, posY, width, 1, color); + DrawRectangle(posX + width - 1, posY + 1, 1, height - 2, color); + DrawRectangle(posX, posY + height - 1, width, 1, color); + DrawRectangle(posX, posY + 1, 1, height - 2, color); + } } // Draw a triangle -- cgit v1.2.3 From 5e45c3c824b61abd94525c1dcd08abc5a10dc613 Mon Sep 17 00:00:00 2001 From: raysan5 Date: Sun, 20 Mar 2016 13:39:27 +0100 Subject: Redesign to work as standalone Redesigned to work as standalone and support fordward-compatible context (shaders review) --- src/rlgl.c | 51 +++++++++++++++++++++++++++++++++++++++++++++++---- src/rlgl.h | 6 +++++- 2 files changed, 52 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/rlgl.c b/src/rlgl.c index f9722eda..b6a179d6 100644 --- a/src/rlgl.c +++ b/src/rlgl.c @@ -32,7 +32,9 @@ #include // Declares malloc() and free() for memory management, rand() #include // Declares strcmp(), strlen(), strtok() -#include "raymath.h" // Required for Vector3 and Matrix functions +#ifndef RLGL_STANDALONE + #include "raymath.h" // Required for Vector3 and Matrix functions +#endif #if defined(GRAPHICS_API_OPENGL_11) #ifdef __APPLE__ // OpenGL include for OSX @@ -298,6 +300,7 @@ static pixel *GenNextMipmap(pixel *srcData, int srcWidth, int srcHeight); #if defined(RLGL_STANDALONE) static void TraceLog(int msgType, const char *text, ...); +float *MatrixToFloat(Matrix mat); // Converts Matrix to float array #endif #if defined(GRAPHICS_API_OPENGL_ES2) @@ -2581,6 +2584,7 @@ static Shader LoadDefaultShader(void) char fShaderStr[] = "#version 330 \n" "in vec2 fragTexCoord; \n" "in vec4 fragTintColor; \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) @@ -2590,8 +2594,13 @@ static Shader LoadDefaultShader(void) "uniform sampler2D texture0; \n" "void main() \n" "{ \n" - " vec4 texelColor = texture2D(texture0, fragTexCoord); \n" // NOTE: texture2D() is deprecated on OpenGL 3.3 and ES 3.0, use texture() instead - " gl_FragColor = texelColor*fragTintColor; \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" // NOTE: texture2D() is deprecated on OpenGL 3.3 and ES 3.0 + " gl_FragColor = texelColor*fragTintColor; \n" +#endif "} \n"; shader.id = LoadShaderProgram(vShaderStr, fShaderStr); @@ -2651,6 +2660,7 @@ static Shader LoadSimpleShader(void) #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) @@ -2660,8 +2670,13 @@ static Shader LoadSimpleShader(void) "uniform vec4 fragTintColor; \n" "void main() \n" "{ \n" - " vec4 texelColor = texture2D(texture0, fragTexCoord); \n" // NOTE: texture2D() is deprecated on OpenGL 3.3 and ES 3.0, use texture() instead +#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); @@ -3102,4 +3117,32 @@ static void TraceLog(int msgType, const char *text, ...) if (msgType == ERROR) exit(1); } + +// Converts Matrix to float array +// NOTE: Returned vector is a transposed version of the Matrix struct, +// it should be this way because, despite raymath use OpenGL column-major convention, +// Matrix struct memory alignment and variables naming are not coherent +float *MatrixToFloat(Matrix mat) +{ + static float buffer[16]; + + buffer[0] = mat.m0; + buffer[1] = mat.m4; + buffer[2] = mat.m8; + buffer[3] = mat.m12; + buffer[4] = mat.m1; + buffer[5] = mat.m5; + buffer[6] = mat.m9; + buffer[7] = mat.m13; + buffer[8] = mat.m2; + buffer[9] = mat.m6; + buffer[10] = mat.m10; + buffer[11] = mat.m14; + buffer[12] = mat.m3; + buffer[13] = mat.m7; + buffer[14] = mat.m11; + buffer[15] = mat.m15; + + return buffer; +} #endif diff --git a/src/rlgl.h b/src/rlgl.h index 7d50b67a..1a5260eb 100644 --- a/src/rlgl.h +++ b/src/rlgl.h @@ -36,7 +36,11 @@ #include "utils.h" // Required for function TraceLog() #endif -#include "raymath.h" +#ifdef RLGL_STANDALONE + #define RAYMATH_STANDALONE +#endif + +#include "raymath.h" // Required for types: Vector3, Matrix // Select desired OpenGL version // NOTE: Those preprocessor defines are only used on rlgl module, -- cgit v1.2.3 From ebc2b9a286b07f551689f13fc82367c93e7c3ade Mon Sep 17 00:00:00 2001 From: raysan5 Date: Sun, 20 Mar 2016 14:20:42 +0100 Subject: Improved windows resizing system... ...despite not being enabled on GLFW3 --- src/core.c | 17 ++++++++++------- src/rlgl.c | 15 +++++++++------ 2 files changed, 19 insertions(+), 13 deletions(-) (limited to 'src') diff --git a/src/core.c b/src/core.c index d27a031b..c8d78133 100644 --- a/src/core.c +++ b/src/core.c @@ -1488,11 +1488,11 @@ static void InitDisplay(int width, int height) TraceLog(INFO, "Viewport offsets: %i, %i", renderOffsetX, renderOffsetY); } - glfwSetWindowSizeCallback(window, WindowSizeCallback); + glfwSetWindowSizeCallback(window, WindowSizeCallback); // NOTE: Resizing not allowed by default! glfwSetCursorEnterCallback(window, CursorEnterCallback); glfwSetKeyCallback(window, KeyCallback); glfwSetMouseButtonCallback(window, MouseButtonCallback); - glfwSetCursorPosCallback(window, MouseCursorPosCallback); // Track mouse position changes + glfwSetCursorPosCallback(window, MouseCursorPosCallback); // Track mouse position changes glfwSetCharCallback(window, CharCallback); glfwSetScrollCallback(window, ScrollCallback); glfwSetWindowIconifyCallback(window, WindowIconifyCallback); @@ -1818,16 +1818,19 @@ static void CursorEnterCallback(GLFWwindow *window, int enter) } // 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(renderOffsetX, renderOffsetY, renderWidth, renderHeight); + 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; - - // TODO: Update render size? + 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); diff --git a/src/rlgl.c b/src/rlgl.c index b6a179d6..fc14a0af 100644 --- a/src/rlgl.c +++ b/src/rlgl.c @@ -188,6 +188,8 @@ typedef struct { // Framebuffer Object type typedef struct { GLuint id; + int width; + int height; GLuint colorTextureId; GLuint depthTextureId; } FBO; @@ -1071,8 +1073,8 @@ void rlglInitPostpro(void) quad.vertexCount = 6; - float w = (float)screenWidth; - float h = (float)screenHeight; + 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 }; @@ -1096,6 +1098,8 @@ 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 @@ -2339,22 +2343,21 @@ void SetCustomShader(Shader shader) } // Set postprocessing shader -// NOTE: Uses global variables screenWidth and screenHeight void SetPostproShader(Shader shader) { #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) if (!enabledPostpro) { enabledPostpro = true; - rlglInitPostpro(); + rlglInitPostpro(); // Lazy initialization on postprocessing usage } SetModelShader(&postproQuad, shader); Texture2D texture; texture.id = postproFbo.colorTextureId; - texture.width = screenWidth; - texture.height = screenHeight; + texture.width = postproFbo.width; + texture.height = postproFbo.height; postproQuad.material.texDiffuse = texture; -- cgit v1.2.3 From fa78023aa427e0c6e5a25e82102b296c51c24fe4 Mon Sep 17 00:00:00 2001 From: raysan5 Date: Sun, 20 Mar 2016 16:28:59 +0100 Subject: Understand mouse as touch in web --- src/core.c | 2 ++ 1 file changed, 2 insertions(+) (limited to 'src') diff --git a/src/core.c b/src/core.c index c8d78133..609da64e 100644 --- a/src/core.c +++ b/src/core.c @@ -1793,6 +1793,8 @@ static void MouseCursorPosCallback(GLFWwindow *window, double x, double y) // 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(); -- cgit v1.2.3 From 584e74c6765b63cf6b618f3c38214c3d49322cc2 Mon Sep 17 00:00:00 2001 From: raysan5 Date: Sun, 20 Mar 2016 16:48:23 +0100 Subject: Corrected bug on touch position --- src/core.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/core.c b/src/core.c index 609da64e..37d4a0bf 100644 --- a/src/core.c +++ b/src/core.c @@ -193,9 +193,7 @@ 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) -#if defined(PLATFORM_ANDROID) || defined(PLATFORM_WEB) static Vector2 touchPosition[MAX_TOUCH_POINTS]; // Touch position on screen -#endif #if defined(PLATFORM_DESKTOP) || defined(PLATFORM_RPI) || defined(PLATFORM_WEB) static const char *windowTitle; // Window text title... @@ -1786,6 +1784,9 @@ static void MouseCursorPosCallback(GLFWwindow *window, double x, double y) GestureEvent gestureEvent; gestureEvent.touchAction = TOUCH_MOVE; + + // Assign a pointer ID + gestureEvent.pointerId[0] = 0; // Register touch points count gestureEvent.pointCount = 1; -- cgit v1.2.3 From 269b120104f468df5f0f00ac3a173770aa5bf09f Mon Sep 17 00:00:00 2001 From: raysan5 Date: Mon, 21 Mar 2016 20:15:11 +0100 Subject: Review Android button inputs --- src/core.c | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) (limited to 'src') diff --git a/src/core.c b/src/core.c index 37d4a0bf..c05de93b 100644 --- a/src/core.c +++ b/src/core.c @@ -146,10 +146,12 @@ static bool windowMinimized = false; #elif defined(PLATFORM_ANDROID) static struct android_app *app; // Android activity static struct android_poll_source *source; // Android events polling source -static int ident, events; +static int ident, events; // Android ALooper_pollAll() variables + static bool windowReady = false; // Used to detect display initialization static bool appEnabled = true; // Used to detec if app is active static bool contextRebindRequired = false; // Used to know context rebind required + static int previousButtonState[128] = { 1 }; // Required to check if button pressed/released once static int currentButtonState[128] = { 1 }; // Required to check if button pressed/released once #elif defined(PLATFORM_RPI) @@ -401,13 +403,6 @@ void InitWindow(int width, int height, struct android_app *state) TraceLog(INFO, "Android app initialized successfully"); - // Init button states values (default up) - for(int i = 0; i < 128; i++) - { - currentButtonState[i] = 1; - previousButtonState[i] = 1; - } - // Wait for window to be initialized (display and context) while (!windowReady) { -- cgit v1.2.3