7 Commits

Author SHA1 Message Date
6ed61e107b flipped binary check to fix bug (#5220) 2025-09-30 21:49:58 +02:00
Ray
8932ba0b0a Update core_delta_time.c 2025-09-30 18:48:42 +02:00
Ray
6966ad5717 Update shaders_ascii_rendering.c 2025-09-30 18:39:27 +02:00
3f30533f1c [examples] Add core_delta_time (#5216)
* delta example

* clarification

* made it more clear what delta time is

* more explanation

* vector2 positions, removed all warnings

* removed keys, moved global to local, conventions & comments, show target fps

* new png

* remove more periods

* another period

* remove note about conventions, remove period
2025-09-30 18:35:15 +02:00
a553fbd0c7 [examples] Fix shaders_ascii_rendering (#5219)
* Add ascii post processing

* Fix broken characters and add more comments

* Rename example

* Refactored as requested
2025-09-30 18:34:06 +02:00
0e2e8ce225 [examples] Update shapes_bouncing_ball with gravity (#5217)
* [examples] Add shapes_bouncing_ball with gravity

* gravity could be enabled/disabled
2025-09-30 18:32:25 +02:00
Ray
5d9352a0a1 Update shapes_bullet_hell.c 2025-09-30 18:27:59 +02:00
10 changed files with 297 additions and 173 deletions

View File

@ -0,0 +1,112 @@
/*******************************************************************************************
*
* raylib [core] example - delta time
*
* Example complexity rating: [★☆☆☆] 1/4
*
* Example originally created with raylib 5.5
*
* Example contributed by Robin (@RobinsAviary) and reviewed by Ramon Santamaria (@raysan5)
*
* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
* BSD-like license that allows static linking with closed source software
*
* Copyright (c) 2025-2025 Robin (@RobinsAviary)
*
********************************************************************************************/
#include "raylib.h"
//------------------------------------------------------------------------------------
// Program main entry point
//------------------------------------------------------------------------------------
int main(void)
{
// Initialization
//--------------------------------------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib [core] example - delta time");
int currentFps = 60;
// Store the position for the both of the circles
Vector2 deltaCircle = { 0, (float)screenHeight/3.0f };
Vector2 frameCircle = { 0, (float)screenHeight*(2.0f/3.0f) };
// The speed applied to both circles
const float speed = 10.0f;
const float circleRadius = 32.0f;
SetTargetFPS(currentFps);
//--------------------------------------------------------------------------------------
// Main game loop
while (!WindowShouldClose()) // Detect window close button or ESC key
{
// Update
//----------------------------------------------------------------------------------
// Adjust the FPS target based on the mouse wheel
float mouseWheel = GetMouseWheelMove();
if (mouseWheel != 0)
{
currentFps += (int)mouseWheel;
if (currentFps < 0) currentFps = 0;
SetTargetFPS(currentFps);
}
// GetFrameTime() returns the time it took to draw the last frame, in seconds (usually called delta time)
// Uses the delta time to make the circle look like it's moving at a "consistent" speed regardless of FPS
// Multiply by 6.0 (an arbitrary value) in order to make the speed
// visually closer to the other circle (at 60 fps), for comparison
deltaCircle.x += GetFrameTime()*6.0f*speed;
// This circle can move faster or slower visually depending on the FPS
frameCircle.x += 0.1f*speed;
// If either circle is off the screen, reset it back to the start
if (deltaCircle.x > screenWidth) deltaCircle.x = 0;
if (frameCircle.x > screenWidth) frameCircle.x = 0;
// Reset both circles positions
if (IsKeyPressed(KEY_R))
{
deltaCircle.x = 0;
frameCircle.x = 0;
}
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(RAYWHITE);
// Draw both circles to the screen
DrawCircleV(deltaCircle, circleRadius, RED);
DrawCircleV(frameCircle, circleRadius, BLUE);
// Draw the help text
// Determine what help text to show depending on the current FPS target
const char *fpsText = 0;
if (currentFps <= 0) fpsText = TextFormat("FPS: unlimited (%i)", GetFPS());
else fpsText = TextFormat("FPS: %i (target: %i)", GetFPS(), currentFps);
DrawText(fpsText, 10, 10, 20, DARKGRAY);
DrawText(TextFormat("Frame time: %02.02f ms", GetFrameTime()), 10, 30, 20, DARKGRAY);
DrawText("Use the scroll wheel to change the fps limit, r to reset", 10, 50, 20, DARKGRAY);
// Draw the text above the circles
DrawText("FUNC: x += GetFrameTime()*speed", 10, 90, 20, RED);
DrawText("FUNC: x += speed", 10, 240, 20, BLUE);
EndDrawing();
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
return 0;
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

View File

@ -72,7 +72,7 @@ int main(void)
DrawText("move ball with mouse and click mouse button to change color", 10, 10, 20, DARKGRAY);
DrawText("Press 'H' to toggle cursor visibility", 10, 30, 20, DARKGRAY);
if (!IsCursorHidden()) DrawText("CURSOR HIDDEN", 20, 60, 20, RED);
if (IsCursorHidden()) DrawText("CURSOR HIDDEN", 20, 60, 20, RED);
else DrawText("CURSOR VISIBLE", 20, 60, 20, LIME);
EndDrawing();

View File

@ -10,58 +10,71 @@ varying vec4 finalColor;
uniform sampler2D texture0;
uniform vec2 resolution;
// Fontsize less then 9 may be not complete
uniform float fontSize;
float greyScale(in vec3 col) {
float GreyScale(in vec3 col)
{
return dot(col, vec3(0.2126, 0.7152, 0.0722));
}
float character(float n, vec2 p)
float GetCharacter(float n, vec2 p)
{
p = floor(p * vec2(4.0, -4.0) + 2.5);
// Check if the coordinate is inside the 5x5 grid (0 to 4).
if (clamp(p.x, 0.0, 4.0) == p.x && clamp(p.y, 0.0, 4.0) == p.y) {
if (int(mod(n / exp2(p.x + 5.0 * p.y), 2.0)) == 1) {
return 1.0; // The bit is on, so draw this part of the character.
p = floor(p*vec2(-4.0, 4.0) + 2.5);
// Check if the calculated coordinate is inside the 5x5 grid (from 0.0 to 4.0)
if (clamp(p.x, 0.0, 4.0) == p.x && clamp(p.y, 0.0, 4.0) == p.y)
{
float a = floor(p.x + 0.5) + 5.0*floor(p.y + 0.5);
// This checked if the 'a'-th bit of 'n' was set
float shiftedN = floor(n/pow(2.0, a));
if (mod(shiftedN, 2.0) == 1.0)
{
return 1.0; // The bit is on
}
}
return 0.0; // The bit is off, or we are outside the grid.
return 0.0; // The bit is off, or we are outside the grid
}
// -----------------------------------------------------------------------------
// Main shader logic
// -----------------------------------------------------------------------------
void main()
{
vec2 charPixelSize = vec2(fontSize, fontSize * 1.8);
vec2 uvCellSize = charPixelSize / resolution;
vec2 charPixelSize = vec2(fontSize, fontSize);
vec2 uvCellSize = charPixelSize/resolution;
vec2 cellUV = floor(fragTexCoord / uvCellSize) * uvCellSize;
// The cell size is based on the fontSize set by application
vec2 cellUV = floor(fragTexCoord/uvCellSize)*uvCellSize;
vec3 cellColor = texture2D(texture0, cellUV).rgb;
float gray = greyScale(cellColor);
float n = 4096;
// Gray is used to define what character will be selected to draw
float gray = GreyScale(cellColor);
float n = 4096.0;
// limited character set
// Character set from https://www.shadertoy.com/view/lssGDj
// Create new bitmaps https://thrill-project.com/archiv/coding/bitmap/
if (gray > 0.2) n = 65600.0; // :
if (gray > 0.3) n = 163153.0; // *
if (gray > 0.3) n = 18725316.0; // v
if (gray > 0.4) n = 15255086.0; // o
if (gray > 0.5) n = 13121101.0; // &
if (gray > 0.6) n = 15252014.0; // 8
if (gray > 0.7) n = 13195790.0; // @
if (gray > 0.8) n = 11512810.0; // #
vec2 localUV = (fragTexCoord - cellUV) / uvCellSize; // Range [0.0, 1.0]
vec2 localUV = (fragTexCoord - cellUV)/uvCellSize; // Range [0.0, 1.0]
vec2 p = localUV * 2.0 - 1.0;
vec2 p = localUV*2.0 - 1.0; // Range [-1.0, 1.0]
float charShape = character(n, p);
// cellColor and charShape will define the color of the char
vec3 color = cellColor*GetCharacter(n, p);
vec3 final_col = cellColor * charShape;
gl_FragColor = vec4(final_col, 1.0);
gl_FragColor = vec4(color, 1.0);
}

View File

@ -8,58 +8,71 @@ varying vec4 finalColor;
uniform sampler2D texture0;
uniform vec2 resolution;
// Fontsize less then 9 may be not complete
uniform float fontSize;
float greyScale(in vec3 col) {
float GreyScale(in vec3 col)
{
return dot(col, vec3(0.2126, 0.7152, 0.0722));
}
float character(float n, vec2 p)
float GetCharacter(float n, vec2 p)
{
p = floor(p * vec2(4.0, -4.0) + 2.5);
// Check if the coordinate is inside the 5x5 grid (0 to 4).
if (clamp(p.x, 0.0, 4.0) == p.x && clamp(p.y, 0.0, 4.0) == p.y) {
if (int(mod(n / exp2(p.x + 5.0 * p.y), 2.0)) == 1) {
return 1.0; // The bit is on, so draw this part of the character.
p = floor(p*vec2(-4.0, 4.0) + 2.5);
// Check if the calculated coordinate is inside the 5x5 grid (from 0.0 to 4.0)
if (clamp(p.x, 0.0, 4.0) == p.x && clamp(p.y, 0.0, 4.0) == p.y)
{
float a = floor(p.x + 0.5) + 5.0*floor(p.y + 0.5);
// This checked if the 'a'-th bit of 'n' was set
float shiftedN = floor(n/pow(2.0, a));
if (mod(shiftedN, 2.0) == 1.0)
{
return 1.0; // The bit is on
}
}
return 0.0; // The bit is off, or we are outside the grid.
return 0.0; // The bit is off, or we are outside the grid
}
// -----------------------------------------------------------------------------
// Main shader logic
// -----------------------------------------------------------------------------
void main()
{
vec2 charPixelSize = vec2(fontSize, fontSize * 1.8);
vec2 charPixelSize = vec2(fontSize, fontSize);
vec2 uvCellSize = charPixelSize / resolution;
vec2 cellUV = floor(fragTexCoord / uvCellSize) * uvCellSize;
// The cell size is based on the fontSize set by application
vec2 cellUV = floor(fragTexCoord / uvCellSize)*uvCellSize;
vec3 cellColor = texture2D(texture0, cellUV).rgb;
float gray = greyScale(cellColor);
float n = 4096;
// Gray is used to define what character will be selected to draw
float gray = GreyScale(cellColor);
float n = 4096.0;
// limited character set
// Character set from https://www.shadertoy.com/view/lssGDj
// Create new bitmaps https://thrill-project.com/archiv/coding/bitmap/
if (gray > 0.2) n = 65600.0; // :
if (gray > 0.3) n = 163153.0; // *
if (gray > 0.3) n = 18725316.0; // v
if (gray > 0.4) n = 15255086.0; // o
if (gray > 0.5) n = 13121101.0; // &
if (gray > 0.6) n = 15252014.0; // 8
if (gray > 0.7) n = 13195790.0; // @
if (gray > 0.8) n = 11512810.0; // #
vec2 localUV = (fragTexCoord - cellUV) / uvCellSize; // Range [0.0, 1.0]
vec2 localUV = (fragTexCoord - cellUV)/uvCellSize; // Range [0.0, 1.0]
vec2 p = localUV * 2.0 - 1.0;
vec2 p = localUV*2.0 - 1.0; // Range [-1.0, 1.0]
float charShape = character(n, p);
// cellColor and charShape will define the color of the char
vec3 color = cellColor*GetCharacter(n, p);
vec3 final_col = cellColor * charShape;
gl_FragColor = vec4(final_col, 1.0);
gl_FragColor = vec4(color, 1.0);
}

View File

@ -8,6 +8,8 @@ out vec4 finalColor;
uniform sampler2D texture0;
uniform vec2 resolution;
// Fontsize less then 9 may be not complete
uniform float fontSize;
float GreyScale(in vec3 col)
@ -15,16 +17,17 @@ float GreyScale(in vec3 col)
return dot(col, vec3(0.2126, 0.7152, 0.0722));
}
float GetCharacter(float n, vec2 p)
float GetCharacter(int n, vec2 p)
{
p = floor(p*vec2(4.0, -4.0) + 2.5);
p = floor(p*vec2(-4.0, 4.0) + 2.5);
// Check if the coordinate is inside the 5x5 grid (0 to 4)
if (clamp(p.x, 0.0, 4.0) == p.x && clamp(p.y, 0.0, 4.0) == p.y)
{
if (int(mod(n/exp2(p.x + 5.0 * p.y), 2.0)) == 1)
int a = int(round(p.x) + 5.0*round(p.y));
if (((n >> a) & 1) == 1)
{
return 1.0; // The bit is on, so draw this part of the character
return 1.0;
}
}
@ -34,30 +37,37 @@ float GetCharacter(float n, vec2 p)
// -----------------------------------------------------------------------------
// Main shader logic
// -----------------------------------------------------------------------------
void main()
{
vec2 charPixelSize = vec2(fontSize, fontSize*1.8);
vec2 charPixelSize = vec2(fontSize, fontSize);
vec2 uvCellSize = charPixelSize/resolution;
// The cell size is based on the fontSize set by application
vec2 cellUV = floor(fragTexCoord/uvCellSize)*uvCellSize;
vec3 cellColor = texture(texture0, cellUV).rgb;
// Gray is used to define what character will be selected to draw
float gray = GreyScale(cellColor);
float n = 4096;
int n = 4096;
// Limited character set
if (gray > 0.2) n = 65600.0; // :
if (gray > 0.3) n = 163153.0; // *
if (gray > 0.4) n = 15255086.0; // o
if (gray > 0.5) n = 13121101.0; // &
if (gray > 0.6) n = 15252014.0; // 8
if (gray > 0.7) n = 13195790.0; // @
if (gray > 0.8) n = 11512810.0; // #
// Character set from https://www.shadertoy.com/view/lssGDj
// Create new bitmaps https://thrill-project.com/archiv/coding/bitmap/
if (gray > 0.2) n = 65600; // :
if (gray > 0.3) n = 18725316; // v
if (gray > 0.4) n = 15255086; // o
if (gray > 0.5) n = 13121101; // &
if (gray > 0.6) n = 15252014; // 8
if (gray > 0.7) n = 13195790; // @
if (gray > 0.8) n = 11512810; // #
vec2 localUV = (fragTexCoord - cellUV)/uvCellSize; // Range [0.0, 1.0]
vec2 p = localUV*2.0 - 1.0;
float charShape = GetCharacter(n, p);
vec2 p = localUV*2.0 - 1.0; // Range [-1.0, 1.0]
finalColor = vec4(cellColor*charShape, 1.0);
vec3 color = cellColor*GetCharacter(n, p);
finalColor = vec4(color, 1.0);
}

View File

@ -48,14 +48,14 @@ int main(void)
int fontSizeLoc = GetShaderLocation(shader, "fontSize");
// Set the character size for the ASCII effect
float fontSize = 4.0f;
// Fontsize should be 9 or more
float fontSize = 9.0f;
// Send the updated values to the shader
float resolution[2] = { (float)screenWidth, (float)screenHeight };
SetShaderValue(shader, resolutionLoc, resolution, SHADER_UNIFORM_VEC2);
SetShaderValue(shader, fontSizeLoc, &fontSize, SHADER_UNIFORM_FLOAT);
Vector2 circlePos = (Vector2){40.0f, (float)screenHeight * 0.5f};
Vector2 circlePos = (Vector2){40.0f, (float)screenHeight*0.5f};
float circleSpeed = 1.0f;
// RenderTexture to apply the postprocessing later
@ -70,45 +70,49 @@ int main(void)
// Update
//----------------------------------------------------------------------------------
circlePos.x += circleSpeed;
if ((circlePos.x > 200.0f) || (circlePos.x < 40.0f)) circleSpeed *= -1;
//----------------------------------------------------------------------------------
if ((circlePos.x > 200.0f) || (circlePos.x < 40.0f)) circleSpeed *= -1; // Revert speed
if (IsKeyPressed(KEY_LEFT) && (fontSize > 9.0)) fontSize -= 1; // Reduce fontSize
if (IsKeyPressed(KEY_RIGHT) && (fontSize < 15.0)) fontSize += 1; // Increase fontSize
// Set fontsize for the shader
SetShaderValue(shader, fontSizeLoc, &fontSize, SHADER_UNIFORM_FLOAT);
// Draw
//----------------------------------------------------------------------------------
// Draw our scene to a render texture first
BeginTextureMode(target);
ClearBackground(WHITE);
// Draw scene in our render texture
DrawTexture(fudesumi, 500, -30, WHITE);
DrawTextureV(raysan, circlePos, WHITE);
EndTextureMode();
BeginDrawing();
ClearBackground(RAYWHITE);
BeginShaderMode(shader);
// Draw the render texture containing scene
// The shader will process every pixel on the screen
DrawTextureRec(target.texture,
// Draw the scene texture (that we rendered earlier) to the screen
// The shader will process every pixel of this texture
DrawTextureRec(target.texture,
(Rectangle){ 0, 0, (float)target.texture.width, (float)-target.texture.height },
(Vector2){ 0, 0 }, WHITE);
EndShaderMode();
DrawRectangle(0, 0, screenWidth, 40, BLACK);
DrawText("Ascii effect", 120, 10, 20, LIGHTGRAY);
DrawText(TextFormat("Ascii effect - FontSize:%2.0f - [Left] -1 [Right] +1 ", fontSize), 120, 10, 20, LIGHTGRAY);
DrawFPS(10, 10);
EndDrawing();
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
UnloadShader(shader); // Unload shader
UnloadTexture(fudesumi); // Unload texture
UnloadTexture(raysan); // Unload texture
UnloadRenderTexture(target); // Unload render texture
UnloadShader(shader); // Unload shader
UnloadTexture(fudesumi); // Unload texture
UnloadTexture(raysan); // Unload texture
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------

Binary file not shown.

Before

Width:  |  Height:  |  Size: 44 KiB

After

Width:  |  Height:  |  Size: 42 KiB

View File

@ -4,8 +4,10 @@
*
* Example complexity rating: [★☆☆☆] 1/4
*
* Example originally created with raylib 2.5, last time updated with raylib 2.5
* Example originally created with raylib 2.5, last time updated with raylib 5.6
*
* Example contributed by Jopestpe (@jopestpe)
*
* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
* BSD-like license that allows static linking with closed source software
*
@ -31,7 +33,9 @@ int main(void)
Vector2 ballPosition = { GetScreenWidth()/2.0f, GetScreenHeight()/2.0f };
Vector2 ballSpeed = { 5.0f, 4.0f };
int ballRadius = 20;
float gravity = 0.2f;
bool useGravity = true;
bool pause = 0;
int framesCounter = 0;
@ -43,16 +47,19 @@ int main(void)
{
// Update
//-----------------------------------------------------
if (IsKeyPressed(KEY_G)) useGravity = !useGravity;
if (IsKeyPressed(KEY_SPACE)) pause = !pause;
if (!pause)
{
ballPosition.x += ballSpeed.x;
ballPosition.y += ballSpeed.y;
if (useGravity) ballSpeed.y += gravity;
// Check walls collision for bouncing
if ((ballPosition.x >= (GetScreenWidth() - ballRadius)) || (ballPosition.x <= ballRadius)) ballSpeed.x *= -1.0f;
if ((ballPosition.y >= (GetScreenHeight() - ballRadius)) || (ballPosition.y <= ballRadius)) ballSpeed.y *= -1.0f;
if ((ballPosition.y >= (GetScreenHeight() - ballRadius)) || (ballPosition.y <= ballRadius)) ballSpeed.y *= -0.95f;
}
else framesCounter++;
//-----------------------------------------------------
@ -65,12 +72,15 @@ int main(void)
DrawCircleV(ballPosition, (float)ballRadius, MAROON);
DrawText("PRESS SPACE to PAUSE BALL MOVEMENT", 10, GetScreenHeight() - 25, 20, LIGHTGRAY);
if (useGravity) DrawText("GRAVITY: ON (Press G to disable)", 10, GetScreenHeight() - 50, 20, DARKGREEN);
else DrawText("GRAVITY: OFF (Press G to enable)", 10, GetScreenHeight() - 50, 20, RED);
// On pause, we draw a blinking message
if (pause && ((framesCounter/30)%2)) DrawText("PAUSED", 350, 200, 30, GRAY);
DrawFPS(10, 10);
EndDrawing();
//-----------------------------------------------------
}

View File

@ -16,26 +16,22 @@
********************************************************************************************/
#include "raylib.h"
#include <stdlib.h> // Required for: malloc(), free()
#include <math.h> // Required for: cosf(), sinf()
#define MAX_BULLETS 500000 // Max bullets that 800x450 can keep on minimum settings is 130.000 bullets
#include <stdlib.h> // Required for: calloc(), free()
#include <math.h> // Required for: cosf(), sinf()
#define MAX_BULLETS 500000 // Max bullets to be processed
//----------------------------------------------------------------------------------
// Types and Structures Definition
//----------------------------------------------------------------------------------
typedef struct Bullet {
Vector2 position;
Vector2 acceleration; // the amount of pixels to be incremented to position every frame
bool disabled; // skip processing and draw case out of screen
Color color;
Vector2 position; // Bullet position on screen
Vector2 acceleration; // Amount of pixels to be incremented to position every frame
bool disabled; // Skip processing and draw case out of screen
Color color; // Bullet color
} Bullet;
//----------------------------------------------------------------------------------
// Module Functions Declaration
//----------------------------------------------------------------------------------
//------------------------------------------------------------------------------------
// Program main entry point
//------------------------------------------------------------------------------------
@ -48,8 +44,8 @@ int main(void)
InitWindow(screenWidth, screenHeight, "raylib [shapes] example - bullet hell");
// Bullet
Bullet *bullets = (Bullet *)malloc(MAX_BULLETS*sizeof(Bullet)); // Bullets array
// Bullets definition
Bullet *bullets = (Bullet *)RL_CALLOC(MAX_BULLETS, sizeof(Bullet)); // Bullets array
int bulletCount = 0;
int bulletDisabledCount = 0; // Used to calculate how many bullets are on screen
int bulletRadius = 10;
@ -57,7 +53,7 @@ int main(void)
int bulletRows = 6;
Color bulletColor[2] = { RED, BLUE };
// Spawner
// Spawner variables
float baseDirection = 0;
int angleIncrement = 5; // After spawn all bullet rows, increment this value on the baseDirection for next the frame
float spawnCooldown = 2;
@ -70,15 +66,13 @@ int main(void)
RenderTexture bulletTexture = LoadRenderTexture(24, 24);
// Draw circle to bullet texture, then draw bullet using DrawTexture()
// This is being done to improve the performance, since DrawCircle() is slow
BeginDrawing();
// NOTE: This is done to improve the performance, since DrawCircle() is very slow
BeginTextureMode(bulletTexture);
DrawCircle(12, 12, bulletRadius, WHITE);
DrawCircleLines(12, 12, bulletRadius, BLACK);
DrawCircle(12, 12, bulletRadius, WHITE);
DrawCircleLines(12, 12, bulletRadius, BLACK);
EndTextureMode();
EndDrawing();
bool drawInPerformanceMode = true;
bool drawInPerformanceMode = true; // Switch between DrawCircle() and DrawTexture()
SetTargetFPS(60);
//--------------------------------------------------------------------------------------
@ -88,7 +82,6 @@ int main(void)
{
// Update
//----------------------------------------------------------------------------------
// Reset the bullet index
// New bullets will replace the old ones that are already disabled due to out-of-screen
if (bulletCount >= MAX_BULLETS)
@ -103,27 +96,25 @@ int main(void)
spawnCooldownTimer = spawnCooldown;
// Spawn bullets
float degreesPerRow = 360.0f / bulletRows;
float degreesPerRow = 360.0f/bulletRows;
for (int row = 0; row < bulletRows; row++)
{
if (bulletCount < MAX_BULLETS)
{
bullets[bulletCount].position = (Vector2){(float) screenWidth/2, (float) screenHeight/2};
bullets[bulletCount].disabled = false;
bullets[bulletCount].color = bulletColor[row % 2];
bullets[bulletCount].color = bulletColor[row%2];
float bulletDirection = baseDirection + (degreesPerRow * row);
float bulletDirection = baseDirection + (degreesPerRow*row);
// bullet speed * bullet direction, this will determine how much pixels will be incremented/decremented
// Bullet speed * bullet direction, this will determine how much pixels will be incremented/decremented
// from the bullet position every frame. Since the bullets doesn't change its direction and speed,
// only need to calculate it at the spawning time.
// 0 degrees = right, 90 degrees = down, 180 degrees = left and 270 degrees = up, basically clockwise.
// only need to calculate it at the spawning time
// 0 degrees = right, 90 degrees = down, 180 degrees = left and 270 degrees = up, basically clockwise
// Case you want it to be anti-clockwise, add "* -1" at the y acceleration
bullets[bulletCount].acceleration = (Vector2){
bulletSpeed * cosf(bulletDirection * DEG2RAD),
bulletSpeed * sinf(bulletDirection * DEG2RAD)
bulletSpeed*cosf(bulletDirection*DEG2RAD),
bulletSpeed*sinf(bulletDirection*DEG2RAD)
};
bulletCount++;
@ -131,29 +122,22 @@ int main(void)
}
baseDirection += angleIncrement;
}
// Update bullets position based on its acceleration
for (int i = 0; i < bulletCount; i++)
{
// Only update bullet if inside the screen
if (!bullets[i].disabled)
{
bullets[i].position.x += bullets[i].acceleration.x;
bullets[i].position.y += bullets[i].acceleration.y;
// Disable bullet if out of screen
if
(
bullets[i].position.x < -bulletRadius*2 ||
bullets[i].position.x > screenWidth + bulletRadius*2 ||
bullets[i].position.y < -bulletRadius*2 ||
bullets[i].position.y > screenHeight + bulletRadius*2
)
if ((bullets[i].position.x < -bulletRadius*2) ||
(bullets[i].position.x > screenWidth + bulletRadius*2) ||
(bullets[i].position.y < -bulletRadius*2) ||
(bullets[i].position.y > screenHeight + bulletRadius*2))
{
bullets[i].disabled = true;
bulletDisabledCount++;
@ -161,12 +145,12 @@ int main(void)
}
}
// Input
if ((IsKeyPressed(KEY_RIGHT) || IsKeyPressed(KEY_D)) && bulletRows < 359) bulletRows++;
if ((IsKeyPressed(KEY_LEFT) || IsKeyPressed(KEY_A)) && bulletRows > 1) bulletRows--;
// Input logic
if ((IsKeyPressed(KEY_RIGHT) || IsKeyPressed(KEY_D)) && (bulletRows < 359)) bulletRows++;
if ((IsKeyPressed(KEY_LEFT) || IsKeyPressed(KEY_A)) && (bulletRows > 1)) bulletRows--;
if (IsKeyPressed(KEY_UP) || IsKeyPressed(KEY_W)) bulletSpeed += 0.25f;
if ((IsKeyPressed(KEY_DOWN) || IsKeyPressed(KEY_S)) && bulletSpeed > 0.50f) bulletSpeed -= 0.25f;
if (IsKeyPressed(KEY_Z) && spawnCooldown > 1) spawnCooldown--;
if ((IsKeyPressed(KEY_DOWN) || IsKeyPressed(KEY_S)) && (bulletSpeed > 0.50f)) bulletSpeed -= 0.25f;
if (IsKeyPressed(KEY_Z) && (spawnCooldown > 1)) spawnCooldown--;
if (IsKeyPressed(KEY_X)) spawnCooldown++;
if (IsKeyPressed(KEY_ENTER)) drawInPerformanceMode = !drawInPerformanceMode;
@ -181,51 +165,42 @@ int main(void)
bulletCount = 0;
bulletDisabledCount = 0;
}
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(RAYWHITE);
// Draw magic circle
magicCircleRotation++;
DrawRectanglePro(
(Rectangle) { (float) screenWidth/2, (float) screenHeight/2, 120, 120 },
(Vector2) { 60, 60 },
magicCircleRotation,
PURPLE
);
DrawRectanglePro(
(Rectangle) { (float) screenWidth/2, (float) screenHeight/2, 120, 120 },
(Vector2) { 60, 60 },
magicCircleRotation + 45,
PURPLE
);
DrawRectanglePro((Rectangle){ (float)screenWidth/2, (float)screenHeight/2, 120, 120 },
(Vector2){ 60.0f, 60.0f }, magicCircleRotation, PURPLE);
DrawRectanglePro((Rectangle){ (float)screenWidth/2, (float)screenHeight/2, 120, 120 },
(Vector2){ 60.0f, 60.0f }, magicCircleRotation + 45, PURPLE);
DrawCircleLines(screenWidth/2, screenHeight/2, 70, BLACK);
DrawCircleLines(screenWidth/2, screenHeight/2, 50, BLACK);
DrawCircleLines(screenWidth/2, screenHeight/2, 30, BLACK);
// Draw bullets
// DrawInPerformanceMode = draw bullets using DrawTexture, DrawCircle is vary slow
if (drawInPerformanceMode)
{
// Draw bullets using pre-rendered texture containing circle
for (int i = 0; i < bulletCount; i++)
{
// Do not draw disabled bullets (out of screen)
if (!bullets[i].disabled)
{
DrawTexture(
bulletTexture.texture,
DrawTexture(bulletTexture.texture,
bullets[i].position.x - bulletTexture.texture.width*0.5f,
bullets[i].position.y - bulletTexture.texture.height*0.5f,
bullets[i].color
);
bullets[i].color);
}
}
} else {
}
else
{
// Draw bullets using DrawCircle(), less performant
for (int i = 0; i < bulletCount; i++)
{
// Do not draw disabled bullets (out of screen)
@ -248,25 +223,13 @@ int main(void)
DrawText("- C: Clear bullets", 40, 140, 10, LIGHTGRAY);
DrawRectangle(610, 10, 170, 30, (Color){0,0, 0, 200 });
if (drawInPerformanceMode)
{
DrawText("Draw method: DrawTexture(*)", 620, 20, 10, GREEN);
} else {
DrawText("Draw method: DrawCircle(*)", 620, 20, 10, RED);
}
if (drawInPerformanceMode) DrawText("Draw method: DrawTexture(*)", 620, 20, 10, GREEN);
else DrawText("Draw method: DrawCircle(*)", 620, 20, 10, RED);
DrawRectangle(135, 410, 530, 30, (Color){0,0, 0, 200 });
DrawText(
TextFormat(
"[ FPS: %d, Bullets: %d, Rows: %d, Bullet speed: %.2f, Angle increment per frame: %d, Cooldown: %.0f ]",
GetFPS(), bulletCount - bulletDisabledCount, bulletRows, bulletSpeed, angleIncrement, spawnCooldown
),
155,
420,
10,
GREEN
);
DrawText(TextFormat("[ FPS: %d, Bullets: %d, Rows: %d, Bullet speed: %.2f, Angle increment per frame: %d, Cooldown: %.0f ]",
GetFPS(), bulletCount - bulletDisabledCount, bulletRows, bulletSpeed, angleIncrement, spawnCooldown),
155, 420, 10, GREEN);
EndDrawing();
//----------------------------------------------------------------------------------
@ -274,10 +237,9 @@ int main(void)
// De-Initialization
//--------------------------------------------------------------------------------------
UnloadRenderTexture(bulletTexture); // Unload bullet texture
UnloadRenderTexture(bulletTexture);
free(bullets);
RL_FREE(bullets); // Free bullets array data
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------