Advertisement
Guest User

Tunnel.c

a guest
Jun 19th, 2013
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.79 KB | None | 0 0
  1. #include <os.h>
  2. #include "utils.c"
  3. #include "graphics3.c"
  4. #include "touchpad.c"
  5.  
  6. uint16_t* loadBMP(char *path)
  7. {
  8.     int size, offset, i;
  9.     uint16_t *returnValue, color;
  10.     FILE *temp = fopen(path, "rb");
  11.    
  12.     // Check if the file's 2 first char are BM (indicates bitmap)
  13.     if(!(fgetc(temp) == 0x42 && fgetc(temp) == 0x4d))
  14.     {
  15.         printf("Image is not a bitmap\n");
  16.         fclose(temp);
  17.         return NULL;
  18.     }
  19.    
  20.     // Check if the file is in 16-bits-per-pixel format
  21.     fseek(temp, 0x1c, SEEK_SET);
  22.     if(!(fgetc(temp) == 16))
  23.     {
  24.         printf("Wrong format\n");
  25.         fclose(temp);
  26.         return NULL;
  27.     }
  28.    
  29.     // Gets the 4-bytes raw pixels size, situated at 0x22
  30.     fseek(temp, 0x22, SEEK_SET);
  31.     size = fgetc(temp) | (fgetc(temp) << 8) | (fgetc(temp) << 16) | (fgetc(temp) << 24);
  32.     size >>= 1;
  33.    
  34.     // Gets the 4-bytes offset to the start of the pixel table, situated at 0x0a
  35.     fseek(temp, 0x0a, SEEK_SET);
  36.     offset = fgetc(temp) | (fgetc(temp) << 8) | (fgetc(temp) << 16) | (fgetc(temp) << 24);
  37.    
  38.     fseek(temp, offset, SEEK_SET);
  39.    
  40.     returnValue = malloc(size * sizeof(int));
  41.     if(!returnValue)
  42.     {
  43.         printf("Couldn't allocate memory\n");
  44.         fclose(temp);
  45.         return NULL;
  46.     }
  47.    
  48.     for(i = size - 1; i >= 0; i--)
  49.     {
  50.         color = (uint16_t)(fgetc(temp) | (fgetc(temp) << 8));
  51.         returnValue[i] = (is_cx ? color : ((int)( 0.229 * (color >> 11) + 0.587 * ((color >> 6) & 0x1f) + 0.114 * (color & 0x1f)) >> 1));
  52.     }
  53.     fclose(temp);
  54.     return returnValue;
  55. }
  56.  
  57. #define texWidth 256
  58. #define texHeight 256
  59. #define screenWidth 240
  60. #define screenHeight 240
  61.  
  62. int w = 240, h = 240, x, y;
  63.  
  64. uint16_t *texture;
  65.  
  66. int distanceTable[screenWidth][screenHeight];
  67. int angleTable[screenWidth][screenHeight];
  68. int buffer[screenWidth][screenHeight];
  69.  
  70. void masksprite(void *buffer, int *data, int x, int y, int width, int height, int mask){
  71.     int i, j;
  72.     for(i = 0; i < height; i++){
  73.         for(j = 0; j < width; j++){
  74.             if(data[i*width+j] != mask && x+j < 320 && x+j > 0 && y+i < 240 && y+i > 0){
  75.                 setPixelBuf(buffer, x+j, y+i, data[i*width+j]);
  76.             }
  77.         }          
  78.     }
  79. }
  80.  
  81. uint16_t RGBColor(uint8_t r, uint8_t g, uint8_t b)
  82. {
  83.   return ((r / 8) << 11) | ((g / 4) << 5) | (b / 8);
  84. }
  85.  
  86. int main(void) {
  87.     char *screen;
  88.     screen = (char*)malloc(SCREEN_BYTES_SIZE * sizeof(char));     // just a buffer
  89.     if(!screen)
  90.     {
  91.         exit(0);
  92.     }
  93.     texture = loadBMP("/documents/ndless/texture.bmp.tns");
  94.     if(!texture)
  95.     {
  96.         free(screen);
  97.         exit(0);
  98.     }
  99.     initTP();
  100.     lcd_ingray();
  101.     for(x = 0; x < w; x++)
  102.     for(y = 0; y < h; y++)
  103.     {
  104.         int angle, distance;
  105.         float ratio = 32.0;
  106.         distance = (unsigned int)(ratio * texHeight / sqrt((x - w / 2.0) * (x - w / 2.0) + (y - h / 2.0) * (y - h / 2.0))) % texHeight;
  107.         angle = (unsigned int)(0.5 * texWidth * atan2(y - h / 2.0, x - w / 2.0) / M_PI);
  108.         distanceTable[x][y] = distance;
  109.         angleTable[x][y] = angle;
  110.     }
  111.     float animation = 0;
  112.     int shiftY = (unsigned int)(texHeight * 0.25);
  113.     //begin the loop
  114.     while(!isKeyPressed(KEY_NSPIRE_ESC))
  115.     {
  116.         animation = animation+0.02;
  117.         readTP();
  118.         int TZ = getTouchedZone4();
  119.         //calculate the shift values out of the animation value
  120.         int shiftX = (unsigned int)(texWidth * 1.0 * animation);
  121.         if(isKeyPressed(KEY_NSPIRE_RIGHT) || TZ==6)
  122.             shiftY++;
  123.         if(isKeyPressed(KEY_NSPIRE_LEFT) || TZ==4)
  124.             shiftY--;
  125.         for(x = 0; x < w; x++)
  126.         for(y = 0; y < h; y++)
  127.         {
  128.             //get the texel from the texture by using the tables, shifted with the animation values
  129.             int color = texture[(abs(angleTable[x][y] + shiftY) % texHeight)*texWidth+(abs(distanceTable[x][y] + shiftX)  % texWidth)];
  130.             buffer[x][y] = color;
  131.         }
  132.         masksprite(screen, buffer[0], 0, 0, 240, 240, 0xf800);
  133.         refresh(screen);
  134.         clearBuf(screen);
  135.     }  
  136.     free(screen);
  137.     endTP();
  138.     return 0;
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement