aboutsummaryrefslogtreecommitdiffstats
path: root/donut.c
blob: 36d7a191debcbeb69a211d2d40b79dc6bf623052 (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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*
    the REAL donut.c
    compile with -lm, -lX11
    Copyright (c) 2023 turret. <turret@turret.cyou>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.
*/

#include "config.h"

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <math.h>

#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xos.h>
#include <X11/extensions/Xdbe.h>

char *argv0;
#include "arg.h"

#define BETWEEN(x, min, max) min < x && x< max

const float theta_spacing = 0.07;
const float phi_spacing = 0.02;

int height = 24;
int width = 80;

double utime(){
    struct timeval tv;
    gettimeofday(&tv, NULL);
    return tv.tv_sec * 1000 + tv.tv_usec / 1000;
}

void drawTorus (float *b, const float *Az, const float *Bz) {
    float A = *Az;
    float B = *Bz;

    float *z = malloc(width * height * sizeof(float));//[width * height];
    memset(z, 0, width * height * sizeof(float) / sizeof(char)); // clear z-buffer

    // render torus
    float sinA = sin(A);
    float sinB = sin(B);
    float cosA = cos(A);
    float cosB = cos(B);
    for (float theta = 0; 2*M_PI > theta; theta += theta_spacing) {
        float sinTheta = sin(theta);
        float cosTheta = cos(theta);
        float h = cosTheta + 2;

        for (float phi = 0; 2*M_PI > phi; phi += phi_spacing) {
            float sinPhi = sin(phi);
            float cosPhi = cos(phi);
            float depth = 1 / (sinPhi * h * sinA + sinTheta * cosA + 5);
            float t = sinPhi * h * cosA - sinTheta * sinA;
            int x = width/2 + (30 * width / 80) * depth * (cosPhi * h * cosB - t * sinB);
            int y = height/2 + (15 * height / 24) * depth * (cosPhi * h * sinB + t * cosB);
            int index = x + width * y;
            float lum = ((sinTheta * sinA - sinPhi * cosTheta * cosA) * cosB - sinPhi * cosTheta * sinA - sinTheta * cosA - cosPhi * cosTheta * sinB);
            if (BETWEEN(y, 0, height - 2) && BETWEEN(x, 0, width) && depth > z[index]) { //22 > y && y > 0 && x > 0 && 80 > x && D > z[o]) {
                z[index] = depth;
                b[index] = lum + 1;
            }
        }
    }

    free(z);
}

void usage() {
    fprintf(stderr, "usage: %s [-h] [-s scale] [-r speed]\n", argv0);
    exit(1);
}

int main(int argc, char **argv) {
    /*#define SCALE 2
    #define ROTATION_SPEED_FACTOR 1*/

    int SCALE = 2;
    int ROTATION_SPEED_FACTOR = 1;

    ARGBEGIN {
        case 's':
            SCALE = atoi(EARGF(usage()));
            break;
        case 'r':
            ROTATION_SPEED_FACTOR = atoi(EARGF(usage()));
            break;
        case 'h':
            usage();
            break;
        default:
            break;
    } ARGEND

    Display *dis = XOpenDisplay((char*)0);
    int screen = DefaultScreen(dis);
    unsigned long black = BlackPixel(dis, screen), white = WhitePixel(dis, screen);

    Window win = XCreateSimpleWindow(dis, DefaultRootWindow(dis), 0, 0, 640, 480, 5, white, black);
    XSetStandardProperties(dis, win, "DOnut!", "donut", None, NULL, 0, NULL);
    XSelectInput(dis, win, ExposureMask);
    GC gc = XCreateGC(dis, win, 0, 0);
    XSetBackground(dis, gc, white);
    XSetForeground(dis, gc, black);
    XClearWindow(dis, win);
    XMapRaised(dis, win);

    XdbeSwapInfo swap_info = {
        win,
        XdbeUndefined
    };
    XdbeBackBuffer back_buffer = XdbeAllocateBackBufferName(dis, win, swap_info.swap_action);

    // torus rotation floats
    float A = 0; // x-axis
    float B = 0; // z-axis
    while(1){
        double stime = utime();

        XSync(dis, 1);

        XWindowAttributes attr;
        XGetWindowAttributes(dis, win, &attr);
        width = attr.width / SCALE;
        height = attr.height / SCALE;

        float *b = malloc(width * height * sizeof(float));
        memset(b, 0, width * height * sizeof(float) / sizeof(char));
        drawTorus(b, &A, &B);

        // old ascii console generation code. kept for presevation sakes
//#define LUMINANCE_SET " .,-~:;=!*#$@"
        //printf("\x1b[H");
        //for (int k = 0; width * height >= k; k++){
        //    putchar(k % width ? (b[k] > 0 ? LUMINANCE_SET[(int)MAX((b[k] - 1) * 8, 0)] : ' ') : 10);
        //}

        for(int x = 0; width >= x; x++){
            for(int y = 0; height >= y; y++){
                int color = (b[x + width * y] - 1 + M_SQRT2) / sqrt(8) * 255;
                XSetForeground(dis, gc, color << 16 | color << 8 | color);
                XFillRectangle(dis, back_buffer, gc, x * SCALE, y * SCALE, SCALE, SCALE);
            }
        }

        XdbeSwapBuffers(dis, &swap_info, 1);

        free(b);

        XSync(dis, 1);
        double etime = utime();

        if(etime < stime + 1000/FPS_MAX)
            usleep((1000/FPS_MAX - (etime - stime)) * 1000);
        double rtime = utime();

        A += (rtime-stime) * (0.04f * ROTATION_SPEED_FACTOR) / (1000/FPS_MAX);// / (1000/FPS_MAX), 0.04f * ROTATION_SPEED_FACTOR);
        B += (rtime-stime) * (0.02f * ROTATION_SPEED_FACTOR) / (1000/FPS_MAX);// / (1000/FPS_MAX), 0.04f * ROTATION_SPEED_FACTOR);

        printf("fps: %.1f     \r", 1000/(rtime-stime));
    }
}