Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // 3D rotating cube for the TI-84 Plus CE / TI-83 Premium CE
- // Adriweb
- /* Standard headers - it's recommended to leave them included */
- #include <math.h>
- #include <stdbool.h>
- #include <stddef.h>
- #include <stdint.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- /* Other available headers */
- // #include <stdarg.h>
- // #include <setjmp.h>
- // #include <assert.h>
- // #include <ctype.h>
- // #include <float.h>
- // #include <iso646.h>
- // #include <limits.h>
- // #include <errno.h>
- // This header is needed, do not remove or comment.
- #include "CE.h"
- #define min(a,b) (((a)<(b))?(a):(b))
- #define max(a,b) (((a)>(b))?(a):(b))
- #define abs(x) (((x)<0)?-(x):(x))
- //*********************************************
- // Just a few Graphical Stuff (Nspire-Lua like)
- //*********************************************
- #define SCR_WIDTH 320
- #define SCR_HEIGHT 240
- static const uint16_t* screen_buf = (uint16_t*)0xD40000;
- typedef uint16_t color_t;
- #define rgb2color(r, g, b) ((color_t)(((r << 8) & 0xf800) | ((g << 3) & 0x07e0) | (b >> 3)))
- void gc_drawLine(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1);
- void gc_drawRect(uint16_t x, uint16_t y, uint16_t w, uint16_t h);
- void gc_fillRect(uint16_t x, uint16_t y, uint16_t w, uint16_t h);
- #define gc_drawPixel(x, y) *((uint16_t*)(screen_buf + x + y*SCR_WIDTH)) = globalColor;
- #define gc_setColor(c) globalColor = c;
- #define gc_setColorRGB(r, g, b) globalColor = rgb2color(r, g, b);
- enum colorNames { Black = 0, Grey, White, Red, Green, Blue };
- color_t Colors[6] = {
- rgb2color(0, 0, 0),
- rgb2color(127, 127, 127),
- rgb2color(255, 255, 255),
- rgb2color(255, 0, 0),
- rgb2color(0, 255, 0),
- rgb2color(0, 0, 255)
- };
- color_t globalColor = 0;
- uint8_t isAnyKeyPressed();
- void cleanUp();
- // 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/
- // a bit modified
- #define ORG (-50)
- double face1[5][2] = {
- { 150, 55 },
- { 250, 55 },
- { 250, 155 },
- { 150, 155 },
- { 150, 55 }
- };
- double face2[5][2] = {
- { 150+ORG, 55-ORG },
- { 250+ORG, 55-ORG },
- { 250+ORG, 155-ORG },
- { 150+ORG, 155-ORG },
- { 150+ORG, 55-ORG }
- };
- double angle = 0.0872664626; // 5.0 * pi / 180.0;
- double SIN_ANGLE = 0.0;
- double COS_ANGLE = 0.0;
- double midx1 = 0.0, midy1 = 0.0, midx2 = 0.0, midy2 = 0.0;
- bool doneOnce = false;
- void main()
- {
- uint8_t i;
- double xnew, ynew;
- cleanUp();
- SIN_ANGLE = sin(angle);
- COS_ANGLE = cos(angle);
- gc_setColorRGB(0, 0, 0);
- gc_fillRect(0, 0, SCR_WIDTH, SCR_HEIGHT);
- midx1 = (face1[0][0] + face1[1][0]) / 2.0;
- midy1 = (face1[1][1] + face1[2][1]) / 2.0;
- midx2 = (face2[0][0] + face2[1][0]) / 2.0;
- midy2 = (face2[1][1] + face2[2][1]) / 2.0;
- while (!isAnyKeyPressed())
- {
- gc_setColorRGB(0, 0, 0);
- for (i=0; i<4; i++)
- {
- gc_drawLine(face1[i][0], face1[i][1], face1[i+1][0], face1[i+1][1]);
- gc_drawLine(face2[i][0], face2[i][1], face2[i+1][0], face2[i+1][1]);
- gc_drawLine(face1[i][0], face1[i][1], face2[ i ][0], face2[ i ][1]);
- }
- for (i=0; i<5; i++)
- {
- xnew = 0.0;
- ynew = 0.0;
- xnew = midx1 + (face1[i][0] - midx1) * COS_ANGLE - (face1[i][1] - midy1) * SIN_ANGLE;
- ynew = midy1 + (face1[i][0] - midx1) * SIN_ANGLE + (face1[i][1] - midy1) * COS_ANGLE;
- face1[i][0] = xnew;
- face1[i][1] = ynew;
- xnew = midx2 + (face2[i][0] - midx2) * COS_ANGLE - (face2[i][1] - midy2) * SIN_ANGLE;
- ynew = midy2 + (face2[i][0] - midx2) * SIN_ANGLE + (face2[i][1] - midy2) * COS_ANGLE;
- face2[i][0] = xnew;
- face2[i][1] = ynew;
- }
- for (i=0; i<4; i++)
- {
- gc_setColorRGB(255, 0, 0);
- gc_drawLine(face1[i][0], face1[i][1], face1[i+1][0], face1[i+1][1]);
- //gc_setColorRGB(0, 255, 0);
- gc_drawLine(face2[i][0], face2[i][1], face2[i+1][0], face2[i+1][1]);
- //gc_setColorRGB(0, 0, 255);
- gc_drawLine(face1[i][0], face1[i][1], face2[ i ][0], face2[ i ][1]);
- }
- }
- GetCSC();
- cleanUp();
- }
- void gc_drawLine(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1)
- {
- if (y1 == y0)
- {
- uint16_t *base = screen_buf + min(x0, x1) + y0*SCR_WIDTH;
- uint16_t end = abs(x0-x1) + 1;
- uint16_t i;
- for (i = 0; i < end; i++)
- {
- *(base++) = globalColor;
- }
- } else {
- int dx = abs(x1-x0), sx = x0<x1 ? 1 : -1;
- int dy = abs(y1-y0), sy = y0<y1 ? 1 : -1;
- int err = (dx>dy ? dx : -dy)/2, e2;
- for(;;)
- {
- gc_drawPixel(x0, y0);
- if (x0 == x1 && y0 == y1)
- break;
- e2 = err;
- if (e2 >-dx) { err -= dy; x0 += sx; }
- if (e2 < dy) { err += dx; y0 += sy; }
- }
- }
- }
- void gc_drawRect(uint16_t x, uint16_t y, uint16_t w, uint16_t h)
- {
- gc_drawLine(x, y, x+w-1, y);
- gc_drawLine(x, y+h-1, x+w-1, y+h-1);
- gc_drawLine(x, y, x, y+h-1);
- gc_drawLine(x+w-1, y, x+w-1, y+h-1);
- }
- void gc_fillRect(uint16_t x, uint16_t y, uint16_t w, uint16_t h)
- {
- uint16_t i, j;
- uint16_t* base = screen_buf + x + y*SCR_WIDTH;
- for(j=0; j<h; j++)
- {
- uint16_t* lineBase = base;
- for(i=0; i<w; i++)
- {
- *(lineBase++) = globalColor;
- }
- base += SCR_WIDTH;
- }
- }
- uint8_t isAnyKeyPressed()
- {
- uint8_t* addr = (uint8_t*)0xF50012;
- uint8_t* last = (uint8_t*)0xF5001E;
- for (;addr<=last;addr+=2)
- if (*addr) return 1;
- return 0;
- }
- void cleanUp()
- {
- asm("CALL _DelRes");
- asm("CALL _ClrTxtShd");
- asm("CALL _ClrScrn");
- asm("set 0,(iy+3)"); //mark graph as dirty
- asm("CALL _HomeUp");
- asm("CALL _DrawStatusBar");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement