aboutsummaryrefslogtreecommitdiffstats
path: root/src/sldj.c
blob: a2b0c47e7757fa51fd9be5b30c42e99245fb43ff (plain) (blame)
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
#include "sldj.h"

void initSldj(Sldj* sldj)
{
    InitWindow(WINDOW_WIDTH, WINDOW_HEIGHT, "Scanline DJ");
    SetWindowState(FLAG_WINDOW_RESIZABLE);
    SetTargetFPS(FRAME_RATE);

    sldj->screenTexture = LoadRenderTexture(SCREEN_WIDTH, SCREEN_HEIGHT);
    sldj->frameCounter = 0;
}

uint16_t noise = 0;

Color scanPoint(uint16_t x, uint16_t y, int frame)
{
    noise ^= frame;
    noise ^= x ^ y;
    return (Color){.r = (y >> (6 - noise)) ^ (frame << 4), .g = noise >> x, .b = 0, .a = 255};
}

void updateSldj(Sldj* sldj)
{
    BeginDrawing();

    // Scan image.
    BeginTextureMode(sldj->screenTexture);

    uint16_t xScaled = 0;
    uint16_t yScaled = 0;

    for (uint16_t y = 0; y < SCREEN_HEIGHT; ++y)
    {
        xScaled = 0;
        
        for (uint16_t x = 0; x < SCREEN_WIDTH; ++x)
        {
            DrawPixel(x, y, scanPoint(xScaled, yScaled, sldj->frameCounter));
            xScaled += 65536 / SCREEN_WIDTH;
        }

        yScaled += 65536 / SCREEN_HEIGHT;
    }

    EndTextureMode();

    // Draw scanned image.
    DrawTexturePro(
        sldj->screenTexture.texture,
        (Rectangle){0.0, 0.0, SCREEN_WIDTH, SCREEN_HEIGHT},
        (Rectangle){0.0, 0.0, SCREEN_WIDTH, SCREEN_HEIGHT},
        (Vector2){0.0, 0.0},
        0.0,
        WHITE
    );

    EndDrawing();

    ++sldj->frameCounter;
}

void closeSldj(Sldj* sldj)
{
    UnloadRenderTexture(sldj->screenTexture);
    
    CloseWindow();
}