summaryrefslogtreecommitdiff
path: root/tests/projects/android/native_app/raylib_particles/src/main.cpp
blob: 5bbfc90e8569b72c80777fed83f313f3b220165d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <raylib.h>
#include <android/log.h>
#include <vector>
#include <cmath>

#define LOG_TAG "raydemo_particles"
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__)

struct Particle {
    Vector2 position;
    Vector2 velocity;
    Color color;
    float size;
    float life;
};

int main(int argc, char** argv) {
    InitWindow(0, 0, "Raylib Particles");
    SetTargetFPS(60);
    LOGD("raylib particles starting!");

    std::vector<Particle> particles;

    while (!WindowShouldClose()) {
        // Update
        bool inputActive = IsMouseButtonDown(MOUSE_BUTTON_LEFT) || GetTouchPointCount() > 0;
        Vector2 pos;

        if (inputActive) {
            pos = GetMousePosition();
            if (GetTouchPointCount() > 0) pos = GetTouchPosition(0);
        } else {
            // Auto emit when idle
            double time = GetTime();
            pos.x = GetScreenWidth() / 2.0f + cos(time * 3.0f) * (GetScreenWidth() / 4.0f);
            pos.y = GetScreenHeight() / 2.0f + sin(time * 2.0f) * (GetScreenHeight() / 4.0f);
        }

        if (inputActive || GetRandomValue(0, 100) < 50) {
            int count = inputActive ? 5 : 2;
            for (int i = 0; i < count; i++) {
                Particle p;
                p.position = pos;
                p.velocity = {(float)GetRandomValue(-200, 200) / 100.0f, (float)GetRandomValue(-200, 200) / 100.0f};
                p.color = (Color){(unsigned char)GetRandomValue(0, 255), (unsigned char)GetRandomValue(0, 255), (unsigned char)GetRandomValue(0, 255), 255};
                p.size = (float)GetRandomValue(5, 20);
                p.life = 1.0f;
                particles.push_back(p);
            }
        }

        for (auto it = particles.begin(); it != particles.end();) {
            it->position.x += it->velocity.x * 5.0f;
            it->position.y += it->velocity.y * 5.0f;
            it->life -= 0.02f;
            it->size *= 0.99f;

            if (it->life <= 0) {
                it = particles.erase(it);
            } else {
                ++it;
            }
        }

        // Draw
        BeginDrawing();
        ClearBackground(BLACK);
        
        for (const auto& p : particles) {
            DrawCircleV(p.position, p.size, Fade(p.color, p.life));
        }
        
        // Draw FPS
        const char* fpsText = TextFormat("%2i FPS", GetFPS());
        int fpsWidth = MeasureText(fpsText, 40);
        DrawText(fpsText, GetScreenWidth() / 2 - fpsWidth / 2, 30, 40, GREEN);

        // Draw centered instructions
        int fontSize = 40;
        const char* text = "Touch to create particles!";
        int textWidth = MeasureText(text, fontSize);
        DrawText(text, GetScreenWidth() / 2 - textWidth / 2, 80, fontSize, WHITE);

        EndDrawing();
    }

    CloseWindow();
    return 0;
}