aboutsummaryrefslogtreecommitdiff
path: root/examples/particle_system/main.cpp
blob: a838bedc3b4e431f64deaf4d425b002614e0bec0 (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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include <iostream>

#include "libjin/jin.h"

using namespace std;
using namespace JinEngine::AI;
using namespace JinEngine::Game;
using namespace JinEngine::Math;
using namespace JinEngine::Input;
using namespace JinEngine::Graphics;
using namespace JinEngine::Graphics::Shaders;
using namespace JinEngine::Graphics::Particles;
using namespace JinEngine::Filesystem;
using namespace JinEngine::Time;

ParticleSystem* p;
Timer timer;
Timer::Handler* hnd;
Texture* tex;
Shader* shader;
Sprite* spr;
const char* shader_code = R"(
#VERTEX_SHADER 
Vertex vert(Vertex v) 
{
    return v;
}
#END_VERTEX_SHADER
#FRAGMENT_SHADER 
Color frag(Color col, Texture tex, Vertex v)
{
    Color c = texel(tex, v.uv);
	return c * col;
}
#END_FRAGMENT_SHADER
)";
const float Pi = 3.14f;
void onLoad()
{
    tex = Texture::createTexture("texture.png");
    spr = new Sprite(tex, Sprite::Origin::MiddleCenter);
    shader = Shader::createShader(shader_code);
    ParticleSystemDef def;
    def.maxParticleCount = 30;

    def.emitterDef.emitRateDef.rate.rate = 0.01f;
    def.emitterDef.forceDef.force.force = 10.f;
    def.emitterDef.positionDef.enableRandom = true;
    def.emitterDef.positionDef.position.random.floor = Vector2<float>(0, 0);
    def.emitterDef.positionDef.position.random.ceil = Vector2<float>(0, 10);

    def.emitterDef.directionDef.enableRandom = true;
    def.emitterDef.directionDef.direction.random.floor = 0;
    def.emitterDef.directionDef.direction.random.ceil = Pi*2;

    def.particleDef.colorOverTimeDef.enable = true;
    def.particleDef.colorOverTimeDef.colorStart = Color(255, 255, 0, 255);
    def.particleDef.colorOverTimeDef.colorEnd = Color(255, 0, 0, 0);
/*
    def.particleDef.angularSpeedDef.enableRandom = true;
    def.particleDef.angularSpeedDef.angularSpeed.random.floor = -1;
    def.particleDef.angularSpeedDef.angularSpeed.random.ceil = 1;
*/

    //def.particleDef.linearAccelarationDef.linearAccelaration = Vector2<float>(0, 10);

    def.particleDef.sizeOverTimeDef.enable = true;
    def.particleDef.sizeOverTimeDef.start = 1;
    def.particleDef.sizeOverTimeDef.end = 0.5;

    def.particleDef.lifeTimeDef.life.life = 0.5;
    p = new ParticleSystem(def);
    p->setSprite(spr);
}

void onEvent(Event* e)
{
    static Application* Application = Application::get();
    if (e->type == EventType::QUIT)
        Application->stop();
}

void onUpdate(int ms)
{
    p->update(ms / 1000.f);
    timer.update(ms);
}

void onDraw()
{
    shader->use();
    Mouse* m = Mouse::get();
    int x, y;
    m->getState(&x, &y);
    p->setPosition(x, y);
    p->render();
    shader->unuse();
}

int main(int argc, char* argv[])
{
    Application* Application = Application::get();
    Application::Setting setting;
    setting.loader = onLoad;
    setting.eventHandler = onEvent;
    setting.updater = onUpdate;
    setting.drawer = onDraw;
    Application->init(&setting);

    AssetDatabase::get()->mount(".");

    Window* wnd = Window::get();
    Window::Setting wndSetting;
    wndSetting.width = 600;
    wndSetting.height = 512;
    wndSetting.title = "Jin v0.1.1";
    wndSetting.fps = 60;
    wndSetting.vsync = false;
    wndSetting.fullscreen = false;
    wndSetting.resizable = false;
    wndSetting.icon = ".";
    wnd->init(&wndSetting);
    wnd->show();

    Application->run();

    Application->quit();
    wnd->quit();

    return 0;
}