collab/PineapplePad

git clone https://git.julia.blue/collab/PineapplePad

Joseph RyanFinishing touchesca230b6

master
4.5 KiB151 linesraw
1#include "../inc/graphics.h"
2#include "../inc/ST7735.h"
3#include "../inc/game.h"
4#include "../inc/random.h"
5#include "../inc/Images.h"
6
7Vec2 Vec3::project() {
8    return Vec2(
9        SCREEN_WIDTH / 2 + SCREEN_WIDTH * x /
10        (SCREEN_WIDTH + 2 * z * VIEW_X_MULT / VIEW_X_DIV),
11        SCREEN_HEIGHT / 2 + SCREEN_HEIGHT * y /
12        (SCREEN_HEIGHT + 2 * z * VIEW_Y_MULT / VIEW_Y_DIV)
13        );
14}
15
16void Cube::draw() {
17    //extrapolate points from origin
18    Vec2 p000 = pos.project();
19    Vec2 p001 = Vec3(pos.x, pos.y, pos.z + size).project();
20    Vec2 p010 = Vec3(pos.x, pos.y + size, pos.z).project();
21    Vec2 p100 = Vec3(pos.x + size, pos.y, pos.z).project();
22    Vec2 p011 = Vec3(pos.x, pos.y + size, pos.z + size).project();
23    Vec2 p101 = Vec3(pos.x + size, pos.y, pos.z + size).project();
24    Vec2 p110 = Vec3(pos.x + size, pos.y + size, pos.z).project();
25    Vec2 p111 = Vec3(pos.x + size, pos.y + size, pos.z + size).project();
26    //always visible
27    drawLine(p000, p100, color); //front bottom
28    drawLine(p010, p110, color); //front top
29    drawLine(p010, p011, color); //top left
30    drawLine(p110, p111, color); //top right
31    drawLine(p000, p010, color); //front left
32    drawLine(p100, p110, color); //front right
33    drawLine(p011, p111, color); //top back
34    //depends on camera
35    if (SELF_OCCLUDE && pos.x > 0) {
36        drawLine(p000, p001, color); //left bottom
37        drawLine(p001, p011, color); //left back
38    }else if (SELF_OCCLUDE && pos.x + size < 0) {
39        drawLine(p101, p111, color); //right back
40        drawLine(p100, p101, color); //right bottom
41    }
42    if (!SELF_OCCLUDE) {
43        drawLine(p001, p101, color); //bottom back
44    }
45}
46
47void drawPlayer(){
48    drawLine(Vec2(75, 5), Vec2(80, 10), playerColor);
49    drawLine(Vec2(80, 10), Vec2(85, 5), playerColor);
50    drawLine(Vec2(85, 5), Vec2(75, 5), playerColor);
51}
52
53void swap(int32_t& a, int32_t& b){
54    int32_t temp = a;
55    a = b;
56    b = temp;
57}
58
59int abs(int x){
60    return x < 0 ? -x : x;
61}
62
63void drawLine(Vec2 a, Vec2 b, int color) {
64    if (a.x >= SCREEN_WIDTH || a.x < 0 || b.x >= SCREEN_WIDTH || b.x < 0 ||
65        a.y >= SCREEN_HEIGHT || a.y < 0 || b.y >= SCREEN_HEIGHT || b.y < 0) {
66        return;
67    }
68    int x0 = a.y, y0 = a.x, x1 = b.y, y1 = b.x; // flipped because landscape
69    if (x0 == x1) {
70        if (y0 > y1) swap(y0, y1);
71        ST7735_DrawFastVLine(x0, y0, y1 - y0 + 1, color);
72    } else if (y0 == y1) {
73        if (x0 > x1) swap(x0, x1);
74        ST7735_DrawFastHLine(x0, y0, x1 - x0 + 1, color);
75    } else {
76        // Bresenham's algorithm for speedy line drawing
77        // todo: to use fast lines for near infinite/zero slopes
78        int16_t steep = abs(y1 - y0) > abs(x1 - x0);
79        if (steep) {
80            swap(x0, y0);
81            swap(x1, y1);
82        }
83        if (x0 > x1) {
84            swap(x0, x1);
85            swap(y0, y1);
86        }
87        int16_t dx, dy;
88        dx = x1 - x0;
89        dy = abs(y1 - y0);
90        int16_t err = dx / 2;
91        int16_t ystep;
92        if (y0 < y1) {
93            ystep = 1;
94        } else {
95            ystep = -1;
96        }
97        for (; x0 <= x1; x0++) {
98            if (steep) {
99                ST7735_DrawPixel(y0, x0, color);
100            } else {
101                ST7735_DrawPixel(x0, y0, color);
102            }
103            err -= dy;
104            if (err < 0) {
105                y0 += ystep;
106                err += dx;
107            }
108        }
109    }
110}
111
112static char lastScore[5] = { 10, 10, 10, 10, 10 };
113const uint16_t* numbers[10] = { zero, one, two, three, four, five, six, seven, eight, nine };
114void printScore(int x){
115    int screenX = 2 * SCREEN_HEIGHT / 3;
116    int screenY = SCREEN_WIDTH;
117    for (int i = 0; i < 5; ++i) {
118        int temp = x % 10;
119        if (temp != lastScore[i]) {
120            ST7735_DrawBitmap(screenX, screenY, numbers[temp], 30, 30);
121        }
122        x /= 10;
123        screenY -= 32;
124    }
125}
126
127void drawGround(int width, int depth){
128    Vec3 close(width, -50, 0);
129    Vec3 far(width, -50, 1000);
130    drawLine(close.project(), far.project(), ~bgColor);
131}
132
133// use the HSV spectrum to generate high saturation colors only
134int randomColor(){
135    int randColor = Random();
136    switch (Random32() % 6) {
137    case 0:
138        return ST7735_Color565(0, 255, randColor);
139    case 1:
140        return ST7735_Color565(255, 0, randColor);
141    case 2:
142        return ST7735_Color565(0, randColor, 255);
143    case 3:
144        return ST7735_Color565(255, randColor, 0);
145    case 4:
146        return ST7735_Color565(randColor, 0, 255);
147    case 5:
148        return ST7735_Color565(randColor, 255, 0);
149    }
150    return 0;
151}