Advertisement
Guest User

C for ti89t (fixed)

a guest
Mar 6th, 2023
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.91 KB | Source Code | 0 0
  1. #define USE_TI89
  2. #define SAVE_SCREEN
  3.  
  4. #include <kbd.h>
  5. #include <system.h>
  6. #include <intr.h>
  7. #include <graph.h>
  8. #include <stdlib.h>
  9. #include <stdio.h>
  10. #include <vat.h>
  11. #include <alloc.h>
  12.  
  13. #include "extgraph.h" // Made by TICT. Thank you TICT HQ and Lionel Debroux :)
  14.  
  15. const unsigned char WIDTH = 160; // Replace these values.
  16. const unsigned char HEIGHT = 100;
  17. const unsigned char FPS = 10;
  18.  
  19.  
  20. void resetTimer(float* extra) {
  21.     OSFreeTimer(USER1_TIMER);
  22.     float toWait = (float) 311 / (float) FPS; // 20 = OSC2 / 2^9 , therefore OSC2 = 10240. 320 = 10000 / 2^5. 312.5 is how many cycles to wait for 1 second. (not exact???)
  23.     if (*extra >= 1) {  // 295, 305, 308, 309, 310: too fast.
  24.         *extra -= 1;    // 312.5: too slow.
  25.         toWait += 1;
  26.     }
  27.     short shortToWait = (short) toWait;
  28.     OSRegisterTimer(USER1_TIMER, shortToWait);
  29.     *extra += toWait - (float) shortToWait; // Keep track of decimal time that should have been waited. (more accurate fps hopefully)
  30.     return;
  31. }
  32.  
  33. unsigned short waitForFrame(float* extra) {
  34.     while (!OSTimerExpired(USER1_TIMER));
  35.     if (OSCheckBreak()) { // Did you press ON
  36.         return 54321;
  37.     }
  38.     resetTimer(extra);
  39.     return kbhit();
  40. }
  41.  
  42. unsigned short onFrameEnd(unsigned char* x0, unsigned char* x1, unsigned char* y0, unsigned char* currentColor, unsigned int* currentFrame, float* extra) {
  43.     *y0 = 0;
  44.     *x0 = 0;
  45.     *x1 = 0;
  46.     unsigned short key = waitForFrame(extra);
  47.     *currentFrame = *currentFrame + 1;
  48.     *currentColor = 0;
  49.     return key;
  50. }
  51.  
  52. short handleInput(unsigned short key, int* fileI, int totalFileI) {
  53.     if (key == 13) { // Pause the video
  54.         GKeyFlush();
  55.         while (!kbhit()); // Wait for unpause keypress
  56.         GKeyFlush();
  57.     } else if (key == 344) { // Forward
  58.         *fileI = min(totalFileI - 1, *fileI);
  59.         GKeyFlush();
  60.         return 1;
  61.     } else if (key == 338) { // Backward
  62.         *fileI = max(0, *fileI - 2);
  63.         GKeyFlush();
  64.         return 1;
  65.     }
  66.     GKeyFlush();
  67.     return 0;
  68. }
  69.  
  70. SYM_ENTRY* openBinVAT(char *varName) { // Quicker file reader than default fopen. Thanks Lionel
  71.     SYM_ENTRY *symptr = DerefSym(SymFind(SYMSTR(varName)));
  72.     if (!symptr) {
  73.         printf("%s not found\n", varName);
  74.         ngetchx();
  75.     }
  76.     return symptr;
  77. }
  78.  
  79. void _main(void) {
  80.     ClrScr();
  81.     PRG_setRate(0); // Initial setup
  82.     OSFreeTimer(APD_TIMER);
  83.     OSRegisterTimer(APD_TIMER, (unsigned long) 10000000); // Is there a better way to do this. :[
  84.    
  85.     float extra = 0;
  86.     resetTimer(&extra);
  87.    
  88.     const unsigned char TOTAL_STR = 10; // Replace these values too.
  89.     const char *VARNAME_STRS[] = {"ba\\px1","ba\\px2","ba\\px3","ba\\px4","ba\\px5","ba\\px6","ba\\px7","ba\\px8","ba\\px9","ba\\px10",}
  90.  
  91.     // Start for file analysis
  92.     int fileI;
  93.     for(fileI = 0; fileI < TOTAL_STR; fileI++) { // Go through every file (We can assume frame will not be interrupted).
  94.         unsigned char x0 = 0, x1 = 0, y0 = 0, currentColor = 0, lastByte = 0; // Initialize vars for writing
  95.         unsigned int currentFrame = 0, i = 0;
  96.        
  97.         SYM_ENTRY *dataSymPtr = openBinVAT(VARNAME_STRS[fileI]); // Define the data using the vat
  98.         unsigned char* data = HLock(dataSymPtr->handle);
  99.         data+=2;
  100.        
  101.         unsigned int dataLength = 0; // Define the length of the data
  102.         memcpy(&dataLength, data, sizeof(unsigned int));
  103.  
  104.         for (i = 2; i < dataLength; i++) { // Display data
  105.             unsigned char nextByte = data[i];
  106.             if (y0 == HEIGHT) {
  107.                 unsigned short key = onFrameEnd(&x0, &x1, &y0, &currentColor, &currentFrame, &extra);
  108.                 if (key == 54321) { // Must be broken. (on was pressed)
  109.                     fileI = TOTAL_STR;
  110.                     break;
  111.                 } else if (key) {
  112.                     if (handleInput(key, &fileI, TOTAL_STR)) { // Did you go forward or backword? then break this subloop.
  113.                         break;  // Slightly scuffed, just go to the end of the chunk loop.
  114.                     }
  115.                 }
  116.             }
  117.             unsigned char newColor = !currentColor; // Defined here so it doesnt have to be recalculated.
  118.             if (nextByte == 255 || (lastByte < WIDTH && nextByte < lastByte)) { // New horizontal line? Then finish the previous row.
  119.                 FastDrawHLine_R(LCD_MEM, x0, WIDTH - 1, y0, newColor);
  120.                 x0 = 0;
  121.                 y0++;
  122.                 x1 = 0;
  123.             }
  124.             if (nextByte < WIDTH) { // This is the normal line draw. Majority
  125.                 x1 = nextByte;
  126.                 FastDrawHLine_R(LCD_MEM, x0, x1, y0, newColor);
  127.                 currentColor = newColor;
  128.                 x0 = x1;
  129.             } else if (nextByte == 254) { // This is if the frame has not changed. Just wait.
  130.                 waitForFrame(&extra);
  131.             } else if (nextByte == 253) { // Draw a full black screen, and wait
  132.                 unsigned char j;
  133.                 for (j = 0; j < HEIGHT; j++) {
  134.                     FastDrawHLine_R(LCD_MEM, 0, WIDTH - 1, j, 1);
  135.                 }
  136.                 waitForFrame(&extra);
  137.             } else if (nextByte == 252) { // Draw an empty frame, wait
  138.                 ClrScr();
  139.                 waitForFrame(&extra);
  140.             }
  141.             lastByte = nextByte;
  142.         }
  143.        
  144.         HeapUnlock(dataSymPtr->handle);
  145.     }
  146.     // End of file analysis
  147.    
  148.     PRG_setRate(1);
  149.     OSFreeTimer(APD_TIMER);
  150.     OSRegisterTimer(APD_TIMER, 20 * 360);
  151.    
  152.     return;
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement