collab/PineapplePad
git clone https://git.julia.blue/collab/PineapplePad
ca230b6
master
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 () { 8return Vec2 ( 9SCREEN_WIDTH /2 + SCREEN_WIDTH * x / 10 (SCREEN_WIDTH + 2 * z * VIEW_X_MULT /VIEW_X_DIV ), 11SCREEN_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 18Vec2 p000 = pos .project (); 19Vec2 p001 = Vec3 (pos .x ,pos .y ,pos .z + size ).project (); 20Vec2 p010 = Vec3 (pos .x ,pos .y + size ,pos .z ).project (); 21Vec2 p100 = Vec3 (pos .x + size ,pos .y ,pos .z ).project (); 22Vec2 p011 = Vec3 (pos .x ,pos .y + size ,pos .z + size ).project (); 23Vec2 p101 = Vec3 (pos .x + size ,pos .y ,pos .z + size ).project (); 24Vec2 p110 = Vec3 (pos .x + size ,pos .y + size ,pos .z ).project (); 25Vec2 p111 = Vec3 (pos .x + size ,pos .y + size ,pos .z + size ).project (); 26//always visible 27drawLine (p000 ,p100 ,color );//front bottom 28drawLine (p010 ,p110 ,color );//front top 29drawLine (p010 ,p011 ,color );//top left 30drawLine (p110 ,p111 ,color );//top right 31drawLine (p000 ,p010 ,color );//front left 32drawLine (p100 ,p110 ,color );//front right 33drawLine (p011 ,p111 ,color );//top back 34//depends on camera 35if (SELF_OCCLUDE && pos .x > 0 ) { 36drawLine (p000 ,p001 ,color );//left bottom 37drawLine (p001 ,p011 ,color );//left back 38 }else if (SELF_OCCLUDE && pos .x + size < 0 ) { 39drawLine (p101 ,p111 ,color );//right back 40drawLine (p100 ,p101 ,color );//right bottom 41 } 42if (!SELF_OCCLUDE ) { 43drawLine (p001 ,p101 ,color );//bottom back 44 } 45} 46 47void drawPlayer (){ 48drawLine (Vec2 (75 ,5 ),Vec2 (80 ,10 ),playerColor ); 49drawLine (Vec2 (80 ,10 ),Vec2 (85 ,5 ),playerColor ); 50drawLine (Vec2 (85 ,5 ),Vec2 (75 ,5 ),playerColor ); 51} 52 53void swap (int32_t & a ,int32_t & b ){ 54int32_t temp = a ; 55a = b ; 56b = temp ; 57} 58 59int abs (int x ){ 60return x < 0 ?- x :x ; 61} 62 63void drawLine (Vec2 a ,Vec2 b ,int color ) { 64if (a .x >=SCREEN_WIDTH || a .x < 0 || b .x >=SCREEN_WIDTH || b .x < 0 || 65a .y >=SCREEN_HEIGHT || a .y < 0 || b .y >=SCREEN_HEIGHT || b .y < 0 ) { 66return ; 67 } 68int x0 = a .y ,y0 = a .x ,x1 = b .y ,y1 = b .x ;// flipped because landscape 69if (x0 == x1 ) { 70if (y0 > y1 )swap (y0 ,y1 ); 71ST7735_DrawFastVLine (x0 ,y0 ,y1 - y0 + 1 ,color ); 72 }else if (y0 == y1 ) { 73if (x0 > x1 )swap (x0 ,x1 ); 74ST7735_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 78int16_t steep = abs (y1 - y0 )> abs (x1 - x0 ); 79if (steep ) { 80swap (x0 ,y0 ); 81swap (x1 ,y1 ); 82 } 83if (x0 > x1 ) { 84swap (x0 ,x1 ); 85swap (y0 ,y1 ); 86 } 87int16_t dx ,dy ; 88dx = x1 - x0 ; 89dy = abs (y1 - y0 ); 90int16_t err = dx /2 ; 91int16_t ystep ; 92if (y0 < y1 ) { 93ystep = 1 ; 94 }else { 95ystep = -1 ; 96 } 97for (;x0 <=x1 ;x0 ++ ) { 98if (steep ) { 99ST7735_DrawPixel (y0 ,x0 ,color ); 100 }else { 101ST7735_DrawPixel (x0 ,y0 ,color ); 102 } 103err -= dy ; 104if (err < 0 ) { 105y0 += ystep ; 106err += 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 ){ 115int screenX = 2 * SCREEN_HEIGHT /3 ; 116int screenY = SCREEN_WIDTH ; 117for (int i = 0 ;i < 5 ;++ i ) { 118int temp = x %10 ; 119if (temp != lastScore [i ]) { 120ST7735_DrawBitmap (screenX ,screenY ,numbers [temp ],30 ,30 ); 121 } 122x /=10 ; 123screenY -= 32 ; 124 } 125} 126 127void drawGround (int width ,int depth ){ 128Vec3 close (width ,-50 ,0 ); 129Vec3 far (width ,-50 ,1000 ); 130drawLine (close .project (),far .project (), ~bgColor ); 131} 132 133// use the HSV spectrum to generate high saturation colors only 134int randomColor (){ 135int randColor = Random (); 136switch (Random32 () %6 ) { 137case 0 : 138return ST7735_Color565 (0 ,255 ,randColor ); 139case 1 : 140return ST7735_Color565 (255 ,0 ,randColor ); 141case 2 : 142return ST7735_Color565 (0 ,randColor ,255 ); 143case 3 : 144return ST7735_Color565 (255 ,randColor ,0 ); 145case 4 : 146return ST7735_Color565 (randColor ,0 ,255 ); 147case 5 : 148return ST7735_Color565 (randColor ,255 ,0 ); 149 } 150return 0 ; 151}