aboutsummaryrefslogtreecommitdiff
path: root/gst/fxs.c
blob: 90f56b203ae690882d3e984f02cf6771f8008bd1 (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
#include <stdlib.h>
#include <stdio.h>

#include "../umath/vec.h"

#include "fxs.h"
#include "units.h"

void fx_init (fxs *fx) {
    fx->bullets = (bullet*)malloc(sizeof(bullet)*1024);
    fx->bulletslen = 0;
}

void fx_add_bullet (fxs *fx, bullet *b) {
    fx->bullets[fx->bulletslen] = *b;
    fx->bulletslen++;
}

void fx_process (fxs *fx, float time) {
    for (int i=0; i<fx->bulletslen; i++) {
        bullet *b = fx->bullets+i;
        if (time > b->endtime) {
            // remove by replacing with last, dont care for order
            *b = fx->bullets[fx->bulletslen-1];
            fx->bulletslen--;
        }
    }
}


void fx_render (SDL_Renderer *rend, fxs *fx, float cam[], float time) {
    for (int i=0; i<fx->bulletslen; i++) {
        bullet *b = fx->bullets+i;
        float travel_time = b->endtime - b->starttime;
        float interval_time = time - b->starttime;
        float amt = interval_time / travel_time;
        if (amt < 0) amt = 0; if (amt > 1) amt = 1;
        float pos[2] = {
            b->from[0] *(1-amt) + b->to[0] *amt,
            b->from[1] *(1-amt) + b->to[1] *amt
        };
        float head[2]; vec2_sub(head, b->from, b->to);
        float mag = 10;
        float dist = amt*vec2_mag(head);
        if (dist < 10) { mag = dist; }
        vec2_norm(head); vec2_mul(head, head, mag);
        SDL_SetRenderDrawColor(rend, 
            b->color[0], 
            b->color[1], 
            b->color[2],
            255);
        SDL_RenderDrawLine(rend, 
            -cam[0] + pos[0], 
            -cam[1] + pos[1],
            -cam[0] + pos[0] + head[0], 
            -cam[1] + pos[1] + head[1]
        );
    }
}