Advertisement
adriweb

3d cube TI-84 Plus CE

Oct 26th, 2015
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.03 KB | None | 0 0
  1. // 3D rotating cube for the TI-84 Plus CE / TI-83 Premium CE
  2. // Adriweb
  3.  
  4. /* Standard headers - it's recommended to leave them included */
  5. #include <math.h>
  6. #include <stdbool.h>
  7. #include <stddef.h>
  8. #include <stdint.h>
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. /* Other available headers */
  13. // #include <stdarg.h>
  14. // #include <setjmp.h>
  15. // #include <assert.h>
  16. // #include <ctype.h>
  17. // #include <float.h>
  18. // #include <iso646.h>
  19. // #include <limits.h>
  20. // #include <errno.h>
  21.  
  22. // This header is needed, do not remove or comment.
  23. #include "CE.h"
  24.  
  25. #define min(a,b) (((a)<(b))?(a):(b))
  26. #define max(a,b) (((a)>(b))?(a):(b))
  27. #define abs(x)   (((x)<0)?-(x):(x))
  28.  
  29. //*********************************************
  30. // Just a few Graphical Stuff (Nspire-Lua like)
  31. //*********************************************
  32.  
  33. #define SCR_WIDTH   320
  34. #define SCR_HEIGHT  240
  35.  
  36. static const uint16_t* screen_buf = (uint16_t*)0xD40000;
  37.  
  38. typedef uint16_t    color_t;
  39.  
  40. #define rgb2color(r, g, b)        ((color_t)(((r << 8) & 0xf800) | ((g << 3) & 0x07e0) | (b >> 3)))
  41.  
  42. void gc_drawLine(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1);
  43. void gc_drawRect(uint16_t x, uint16_t y, uint16_t w, uint16_t h);
  44. void gc_fillRect(uint16_t x, uint16_t y, uint16_t w, uint16_t h);
  45. #define gc_drawPixel(x, y)        *((uint16_t*)(screen_buf + x + y*SCR_WIDTH)) = globalColor;
  46. #define gc_setColor(c)            globalColor = c;
  47. #define gc_setColorRGB(r, g, b)   globalColor = rgb2color(r, g, b);
  48.  
  49. enum colorNames { Black = 0, Grey, White, Red, Green, Blue };
  50. color_t Colors[6] = {
  51.     rgb2color(0, 0, 0),
  52.     rgb2color(127, 127, 127),
  53.     rgb2color(255, 255, 255),
  54.     rgb2color(255, 0, 0),
  55.     rgb2color(0, 255, 0),
  56.     rgb2color(0, 0, 255)
  57. };
  58. color_t globalColor = 0;
  59.  
  60.  
  61. uint8_t isAnyKeyPressed();
  62.  
  63. void cleanUp();
  64.  
  65.  
  66. // code from http://electrofriends.com/source-codes/software-programs/c/graphics/c-program-to-implement-3-d-rotation-with-respect-to-x-axis-y-axis-and-z-axis/
  67. // a bit modified
  68.  
  69. #define ORG (-50)
  70.  
  71. double face1[5][2] = {
  72.     { 150, 55  },
  73.     { 250, 55  },
  74.     { 250, 155 },
  75.     { 150, 155 },
  76.     { 150, 55  }
  77. };
  78.  
  79. double face2[5][2] = {
  80.     { 150+ORG, 55-ORG  },
  81.     { 250+ORG, 55-ORG  },
  82.     { 250+ORG, 155-ORG },
  83.     { 150+ORG, 155-ORG },
  84.     { 150+ORG, 55-ORG  }
  85. };
  86.  
  87. double angle = 0.0872664626; // 5.0 * pi / 180.0;
  88. double SIN_ANGLE = 0.0;
  89. double COS_ANGLE = 0.0;
  90. double midx1 = 0.0, midy1 = 0.0, midx2 = 0.0, midy2 = 0.0;
  91. bool doneOnce = false;
  92.  
  93. void main()
  94. {
  95.     uint8_t i;
  96.     double xnew, ynew;
  97.    
  98.     cleanUp();
  99.    
  100.     SIN_ANGLE = sin(angle);
  101.     COS_ANGLE = cos(angle);
  102.    
  103.     gc_setColorRGB(0, 0, 0);
  104.     gc_fillRect(0, 0, SCR_WIDTH, SCR_HEIGHT);
  105.    
  106.     midx1 = (face1[0][0] + face1[1][0]) / 2.0;
  107.     midy1 = (face1[1][1] + face1[2][1]) / 2.0;
  108.     midx2 = (face2[0][0] + face2[1][0]) / 2.0;
  109.     midy2 = (face2[1][1] + face2[2][1]) / 2.0;
  110.  
  111.     while (!isAnyKeyPressed())
  112.     {
  113.         gc_setColorRGB(0, 0, 0);
  114.         for (i=0; i<4; i++)
  115.         {
  116.             gc_drawLine(face1[i][0], face1[i][1], face1[i+1][0], face1[i+1][1]);
  117.             gc_drawLine(face2[i][0], face2[i][1], face2[i+1][0], face2[i+1][1]);
  118.             gc_drawLine(face1[i][0], face1[i][1], face2[ i ][0], face2[ i ][1]);
  119.         }
  120.  
  121.         for (i=0; i<5; i++)
  122.         {
  123.             xnew = 0.0;
  124.             ynew = 0.0;
  125.  
  126.             xnew = midx1 + (face1[i][0] - midx1) * COS_ANGLE - (face1[i][1] - midy1) * SIN_ANGLE;
  127.             ynew = midy1 + (face1[i][0] - midx1) * SIN_ANGLE + (face1[i][1] - midy1) * COS_ANGLE;
  128.  
  129.             face1[i][0] = xnew;
  130.             face1[i][1] = ynew;
  131.  
  132.             xnew = midx2 + (face2[i][0] - midx2) * COS_ANGLE - (face2[i][1] - midy2) * SIN_ANGLE;
  133.             ynew = midy2 + (face2[i][0] - midx2) * SIN_ANGLE + (face2[i][1] - midy2) * COS_ANGLE;
  134.  
  135.             face2[i][0] = xnew;
  136.             face2[i][1] = ynew;
  137.         }
  138.  
  139.         for (i=0; i<4; i++)
  140.         {
  141.             gc_setColorRGB(255, 0, 0);
  142.             gc_drawLine(face1[i][0], face1[i][1], face1[i+1][0], face1[i+1][1]);
  143.             //gc_setColorRGB(0, 255, 0);
  144.             gc_drawLine(face2[i][0], face2[i][1], face2[i+1][0], face2[i+1][1]);
  145.             //gc_setColorRGB(0, 0, 255);
  146.             gc_drawLine(face1[i][0], face1[i][1], face2[ i ][0], face2[ i ][1]);
  147.         }
  148.     }
  149.    
  150.     GetCSC();
  151.     cleanUp();
  152. }
  153.  
  154. void gc_drawLine(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1)
  155. {
  156.     if (y1 == y0)
  157.     {
  158.         uint16_t *base = screen_buf + min(x0, x1) + y0*SCR_WIDTH;
  159.         uint16_t end = abs(x0-x1) + 1;
  160.         uint16_t i;
  161.         for (i = 0; i < end; i++)
  162.         {
  163.             *(base++) = globalColor;
  164.         }
  165.     } else {
  166.         int dx = abs(x1-x0), sx = x0<x1 ? 1 : -1;
  167.         int dy = abs(y1-y0), sy = y0<y1 ? 1 : -1;
  168.         int err = (dx>dy ? dx : -dy)/2, e2;
  169.         for(;;)
  170.         {
  171.             gc_drawPixel(x0, y0);
  172.             if (x0 == x1 && y0 == y1)
  173.                 break;
  174.             e2 = err;
  175.             if (e2 >-dx) { err -= dy; x0 += sx; }
  176.             if (e2 < dy) { err += dx; y0 += sy; }
  177.         }
  178.     }
  179. }
  180.  
  181. void gc_drawRect(uint16_t x, uint16_t y, uint16_t w, uint16_t h)
  182. {
  183.     gc_drawLine(x, y, x+w-1, y);
  184.     gc_drawLine(x, y+h-1, x+w-1, y+h-1);
  185.     gc_drawLine(x, y, x, y+h-1);
  186.     gc_drawLine(x+w-1, y, x+w-1, y+h-1);
  187. }
  188.  
  189. void gc_fillRect(uint16_t x, uint16_t y, uint16_t w, uint16_t h)
  190. {
  191.     uint16_t i, j;
  192.     uint16_t* base = screen_buf + x + y*SCR_WIDTH;
  193.     for(j=0; j<h; j++)
  194.     {
  195.         uint16_t* lineBase = base;
  196.         for(i=0; i<w; i++)
  197.         {
  198.             *(lineBase++) = globalColor;
  199.         }
  200.         base += SCR_WIDTH;
  201.     }
  202. }
  203.    
  204. uint8_t isAnyKeyPressed()
  205. {
  206.     uint8_t* addr = (uint8_t*)0xF50012;
  207.     uint8_t* last = (uint8_t*)0xF5001E;
  208.     for (;addr<=last;addr+=2)
  209.         if (*addr) return 1;
  210.     return 0;
  211. }
  212.    
  213. void cleanUp()
  214. {
  215.     asm("CALL _DelRes");
  216.     asm("CALL _ClrTxtShd");
  217.     asm("CALL _ClrScrn");
  218.     asm("set 0,(iy+3)"); //mark graph as dirty
  219.     asm("CALL _HomeUp");
  220.     asm("CALL _DrawStatusBar");
  221. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement