summaryrefslogtreecommitdiff
path: root/src/paint.cpp
blob: 9362504fdc9323d1d592c9f3c176816e3899a37a (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
#if SPECIAL
#include "main.h"
#endif


static void
SlidingBrush(pixel_buffer *Buffer, v2i Pos, brush_tool Brush)
{
    v2i Min = {0,0};
    v2i Max = Min + (int16)Brush.Size;

    v2 Center = {(real32)Max.x / 2.0f, (real32)Max.y / 2.0f};
    real32 MaxLength = sqrt(LengthSq(Center));

    v2i LayerMin = Pos - (Brush.Size / 2);
    v2i LayerMax = Pos + (Brush.Size / 2);

    rectangle LayerBounds = {0, 0, Buffer->Width, Buffer->Height};
    rectangle Paint = ClipRectangle({LayerMin, LayerMax}, LayerBounds);

    uint8 *Row = ((uint8 *)Buffer->OriginalBuffer + Buffer->Pitch*Paint.Min.y);
    for(int Y = Paint.Min.y;
        Y < Paint.Max.y;
        ++Y)
    {
        uint32 *Pixel = (uint32 *)(Row + Paint.Min.x*sizeof(uint32));
        for(int X = Paint.Min.x;
            X < Paint.Max.x;
            ++X)
        {
            v2 Pos = V2(abs(Center.x - (X - Paint.Min.x)), abs(Center.y - (Y - Paint.Min.y)));
            real32 L = sqrt(LengthSq(Pos)) + Center.x;
            real32 Gradient = pow(Ceil(L, MaxLength) / MaxLength, Brush.Hardness);
            RenderAlpha(Pixel, ColToUint32({1.0f, 0.0f, 0.0f, 1.0f - Gradient}));
            Pixel++;
        }
        Row += Buffer->Pitch;
    }
}

/*
static void
Paint(sdl_input Input, project_layer *Layer, brush_tool Brush)
{
    int16 X = Input.Mouse.x - UI.CompX; // convert to comp space
    int16 Y = Input.Mouse.y - UI.CompY;

    real32 Rad = (Layer->rotation.CurrentValue.f * (PI / 180));
    real32 s = Layer->scale.CurrentValue.f;
    v2 Scale = {Layer->Raster.Width * s, Layer->Raster.Height * s};

    v2 XAxis = (Layer->Raster.Width * s)*V2(cos(Rad), sin(Rad));
    v2 YAxis = (Layer->Raster.Height * -s)*V2(sin(Rad), -cos(Rad));

    real32 AnchorX = Layer->ax.CurrentValue.f;
    real32 AnchorY = Layer->ay.CurrentValue.f;

    v2 Pos = {Layer->x.CurrentValue.f, Layer->y.CurrentValue.f};
    v2 Origin = Pos - (XAxis * AnchorX) - (YAxis * AnchorY);

    real32 XLengthSq = 1.0f / LengthSq(XAxis);
    real32 YLengthSq = 1.0f / LengthSq(YAxis);

    v2 CurrentPixel = V2(X, Y);
    v2 StartVector = CurrentPixel - Origin;

    real32 U = XLengthSq*Inner(StartVector, XAxis);
    real32 V = YLengthSq*Inner(StartVector, YAxis);

    v2i TexelCoord = {};
    TexelCoord.x = 1.0f + ((U*(real32)(Layer->Raster.Width - 1)) + 0.5f);
    TexelCoord.y = 1.0f + ((V*(real32)(Layer->Raster.Height - 1)) + 0.5f);

    v2i Min = {0,0};
    v2i Max = Min + (int16)Brush.Size;

    v2 Center = {(real32)Max.x / 2.0f, (real32)Max.y / 2.0f};
    real32 MaxLength = sqrt(LengthSq(Center));

    v2i LayerMin = TexelCoord - (Brush.Size / 2);
    v2i LayerMax = TexelCoord + (Brush.Size / 2);

    rectangle LayerBounds = {0, 0, Layer->Raster.Width, Layer->Raster.Height};
    rectangle Paint = {LayerMin, LayerMax};

    uint8 *Row = ((uint8 *)Layer->Raster.OriginalBuffer + Layer->Raster.Pitch*Paint.Min.y);
    for(int Y = Paint.Min.y;
        Y < Paint.Max.y;
        ++Y)
    {
        uint32 *Pixel = (uint32 *)(Row + Paint.Min.x*sizeof(uint32));
        for(int X = Paint.Min.x;
            X < Paint.Max.x;
            ++X)
        {
            if (X < Layer->Raster.Width && X > 0) {
                v2 Pos = V2(abs(Center.x - (X - Paint.Min.x)), abs(Center.y - (Y - Paint.Min.y)));
                real32 L = sqrt(LengthSq(Pos)) + Center.x;
                real32 Gradient = pow(Ceil(L, MaxLength) / MaxLength, Brush.Hardness);
                RenderAlpha(Pixel, ColToUint32({1.0f, 1.0f, 1.0f, 1.0f - Gradient}));
            }
            Pixel++;
        }
        Row += Layer->Raster.Pitch;
    }
}
*/