r/GraphicsProgramming • • 4h ago

Video Coding a satisfying animation

Enable HLS to view with audio, or disable this notification

AI - Disclaimer
I used AI to create the color palette

Full coding session:
https://www.youtube.com/watch?v=9f1FRngwWac

Source code:
https://github.com/FractalCodeRicardo/dopamine-programming/tree/main/squares-balls

#include <raylib.h>
#include <raymath.h>
#include <stdbool.h>
#include "time.h"

#define screenSize 800.0
#define gridSize  25.0

typedef struct{ 
  Vector2 pos;
  Vector2 dir;
  Color color;
} Ball;

typedef struct{ 
  Vector2 pos;
  Color color;
  bool empty;
  int life;
  Sound sound;
} Square;

typedef struct{ 
  Ball items[2500];
  int size;
} Balls;

typedef struct{ 
  Square items[2500];
  int size;
} Squares;

Squares squares;
Balls balls;

float squareSize;
float ballSize;

Sound sounds[7];
// Original palette I change it for catppuccin palette
// Color RandomColor() {
//   Color colors[] = {
//     GREEN, YELLOW, RED, BLUE, PURPLE,
//     ORANGE, BROWN, SKYBLUE, DARKPURPLE, MAGENTA
//   };
//
//   int index = GetRandomValue(0, 9);
//   return colors[index];
// }

Color RandomColor() {
 static const Color colors[] = {
      { 220, 138, 120, 255 }, // Rosewater
      { 221, 120, 120, 255 }, // Flamingo
      { 234, 118, 203, 255 }, // Pink
      { 136,  57, 239, 255 }, // Mauve
      { 210,  15,  57, 255 }, // Red
                              //
      { 230,  69,  83, 255 }, // Maroon
      { 254, 100,  11, 255 }, // Peach
      { 223, 142,  29, 255 }, // Yellow
      {  64, 160,  43, 255 }, // Green
      {  23, 146, 153, 255 }, // Teal
                              //
      {   4, 165, 229, 255 }, // Sky
      {  32, 159, 181, 255 }, // Sapphire
      {  30, 102, 245, 255 }, // Blue
      { 114, 135, 253, 255 }, // Lavender
    };  
    return colors[GetRandomValue(0, 15)];
}

void InitSounds() {
    const char *notes[] = {
        "a1.wav", "b1.wav", "c1.wav", "d1.wav",
        "e1.wav", "f1.wav", "g1.wav"
    };

    for (int i = 0; i < 7; i++) {
        sounds[i] = LoadSound(notes[i]);
        SetSoundVolume(sounds[1], 0.1);
    }
}

Sound RandomSound() {
    int index = GetRandomValue(0, 6);
    return sounds[index];
}

Vector2 RandomDir() {
  float sign[] = {1 , -1};

  float x = GetRandomValue(5, 10) / 10.0;
  float y = GetRandomValue(5, 10) / 10.0;

  x = x * sign[GetRandomValue(0, 1)];
  y = y * sign[GetRandomValue(0, 1)];

  return (Vector2) {
    x, y
  };
}

void InitSquares() {
  squareSize = screenSize / gridSize;

  int center = gridSize / 2;
  for(int y = 0; y < gridSize; y++) {
    for(int x = 0; x < gridSize; x++) {

      bool empty = false;
      if (x >= center - 2 && x <= center + 2)
        if (y >= center - 2 && y <= center + 2)
          empty = true;


      int i = y * gridSize + x;

      squares.items[i] = (Square) {
        (Vector2) { 
          x * squareSize,
          y * squareSize
        } ,
        RandomColor(),
        empty,
        5,
        RandomSound()
      };

      squares.size++;
    }
  }
}

void InitBalls() {
  ballSize = (screenSize / gridSize) / 2;

  float center = screenSize / 2;
  for(int i = 0; i < 4; i++) {
    balls.items[i] = (Ball) {
      (Vector2) { center, center} ,
      RandomDir(),
      RandomColor()
    };

    balls.size++;
  }

}

void Init() {
  InitSquares();
  InitBalls();
}

void DrawSquares() {
  for (int i = 0; i <squares.size; i++) {
    Square s = squares.items[i];

    if (s.empty)
      continue;

    DrawRectangle(
        s.pos.x + 1,
        s.pos.y + 1,
        squareSize - 1, 
        squareSize - 1, 
        s.color);
  }
}

void DrawBalls() {
  for (int i = 0; i <balls.size; i++) {
    Ball b = balls.items[i];

    DrawCircle(
        b.pos.x ,
        b.pos.y,
        ballSize / 2,
        b.color
        );
  }
}

void Draw() {
  DrawSquares();
  DrawBalls();
}

void ChangeSquareColor(Square *s) {
  s -> color.a = s -> color.a - 50;
}

void AddBall(Vector2 pos) {
  if (balls.size >= 1000)
    return;

  balls.items[balls.size] = (Ball) {
    pos ,
    RandomDir(),
    RandomColor()
  };

  balls.size ++;
}

void HandleCollision(Ball *b) {

  Rectangle ballRect = (Rectangle) {
    b -> pos.x - ballSize/2,
    b -> pos.y - ballSize/2,
    ballSize,
    ballSize,
  };

  for(int i = 0; i < squares.size; i++) {
    Square *s = &squares.items[i];

    if (s -> empty)
      continue;

    Rectangle squareRect = (Rectangle) {
      s -> pos.x,
      s -> pos.y,
      squareSize,
      squareSize,
    };

    Rectangle collision = GetCollisionRec(ballRect, squareRect);

    if (collision.width <= 0 ||
        collision.height <= 0)
      continue;

    if (collision.width < collision.height)
      b -> dir.x *= -1;
    else
      b -> dir.y *= -1;

    s -> life--;
    ChangeSquareColor(s);

    if (s -> life <= 0) {
      s -> empty = true;
      AddBall(b -> pos);
    }

    PlaySound(s -> sound);

    return;

  }
}

void MoveBalls() {
  for(int i = 0; i < balls.size; i++) {
    Ball *b = &balls.items[i];
    b -> pos = Vector2Add(b -> pos, b -> dir);
    HandleCollision(b);
  }
}

void Update() {
  MoveBalls();
}

int main() {
  SetRandomSeed(time(NULL));
  SetTargetFPS(100);
  InitAudioDevice();
  InitSounds();
  Init();
  InitWindow(screenSize, screenSize, "Squares/Balls");

  while(!WindowShouldClose()) {
    BeginDrawing();
    // ClearBackground(BLACK);
    DrawRectangle(0,0,800,800, Fade(DARKPURPLE, 0.15f));
    Draw();
    Update();
    EndDrawing();
  }
  CloseWindow();
}
3 Upvotes

1 comment sorted by

•

u/AutoModerator 4h ago

Recently updated rules. Please check that your post is in compliance with Rules 1, 4, 5, and 6 on the sidebar. TL;DR:

  • No showcases.
  • Disclose AI usage in post or project.
  • Present a topic to talk about, e.g. how something works.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.