summaryrefslogtreecommitdiffhomepage
path: root/Stars45/DriveSprite.cpp
blob: 4163bc9108a197ac7de8f40ab5dae36577ea1cb9 (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
132
133
134
135
136
137
138
139
140
141
142
/*  Starshatter: The Open Source Project
    Copyright (c) 2021-2022, Starshatter: The Open Source Project Contributors
    Copyright (c) 2011-2012, Starshatter OpenSource Distribution Contributors
    Copyright (c) 1997-2006, Destroyer Studios LLC.

    AUTHOR:       John DiCamillo


    OVERVIEW
    ========
    Sprite for rendering drive flares.  Remains visible at extreme ranges.
*/

#include "MemDebug.h"
#include "DriveSprite.h"

#include "Bitmap.h"
#include "Camera.h"
#include "Scene.h"
#include "Video.h"

// +--------------------------------------------------------------------+

DriveSprite::DriveSprite()
    : Sprite(), glow(0), effective_radius(0), front(0,0,0), bias(0)
{ luminous = true; }

DriveSprite::DriveSprite(Bitmap* animation, Bitmap* g)
    : Sprite(animation), glow(g), effective_radius(0), front(0,0,0), bias(0)
{ luminous = true; }

DriveSprite::DriveSprite(Bitmap* animation, int length, int repeat, int share)
    : Sprite(animation, length, repeat, share), glow(0), effective_radius(0),
      front(0,0,0), bias(0)
{ luminous = true; }

DriveSprite::~DriveSprite()
{ }

// +--------------------------------------------------------------------+

void
DriveSprite::SetFront(const Vec3& f)
{
    front = f;
    front.Normalize();
}

void
DriveSprite::SetBias(DWORD b)
{
    bias = b;
}

// +--------------------------------------------------------------------+

void
DriveSprite::Render(Video* video, DWORD flags)
{
    if (!video || ((flags & RENDER_ADDITIVE) == 0))
    return;

    if (shade > 0  && !hidden && (life > 0 || loop)) {
        const Camera*  cam = video->GetCamera();
        bool           z_disable = false;

        if (bias)
        video->SetRenderState(Video::Z_BIAS, bias);

        if (front.length()) {
            Point test = loc;

            if (scene && cam) {
                Vec3   dir = front;

                double intensity = cam->vpn() * dir * -1;
                double distance  = Point(cam->Pos() - test).length();

                if (intensity > 0.05) {
                    if (!scene->IsLightObscured(cam->Pos(), test, 8)) {
                        video->SetRenderState(Video::Z_ENABLE, false);
                        z_disable = true;

                        if (glow) {
                            intensity = pow(intensity, 3);

                            if (distance > 5e3)
                            intensity *= (1 - (distance-5e3)/45e3);

                            if (intensity > 0) {
                                Bitmap*  tmp_frame   = frames;
                                double   tmp_shade   = shade;
                                int      tmp_w       = w;
                                int      tmp_h       = h;

                                if (glow->Width() != frames->Width()) {
                                    double   wscale      = glow->Width()   / frames->Width();
                                    double   hscale      = glow->Height()  / frames->Height();

                                    w = (int) (w * wscale);
                                    h = (int) (h * hscale);
                                }

                                shade  = intensity;
                                frames = glow;

                                Sprite::Render(video, flags);

                                frames = tmp_frame;
                                shade  = tmp_shade;
                                w      = tmp_w;
                                h      = tmp_h;
                            }
                        }
                    }
                }
            }
        }

        if (effective_radius-radius > 0.1) {
            double scale_up = effective_radius / radius;
            int tmp_w = w;
            int tmp_h = h;

            w = (int) (w * scale_up);
            h = (int) (h * scale_up);

            Sprite::Render(video, flags);

            w = tmp_w;
            h = tmp_h;
        }

        else {
            Sprite::Render(video, flags);
        }

        if (bias)      video->SetRenderState(Video::Z_BIAS,   0);
        if (z_disable) video->SetRenderState(Video::Z_ENABLE, true);
    }
}