Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <os.h>
- #include "utils.c"
- #include "graphics3.c"
- #include "touchpad.c"
- uint16_t* loadBMP(char *path)
- {
- int size, offset, i;
- uint16_t *returnValue, color;
- FILE *temp = fopen(path, "rb");
- // Check if the file's 2 first char are BM (indicates bitmap)
- if(!(fgetc(temp) == 0x42 && fgetc(temp) == 0x4d))
- {
- printf("Image is not a bitmap\n");
- fclose(temp);
- return NULL;
- }
- // Check if the file is in 16-bits-per-pixel format
- fseek(temp, 0x1c, SEEK_SET);
- if(!(fgetc(temp) == 16))
- {
- printf("Wrong format\n");
- fclose(temp);
- return NULL;
- }
- // Gets the 4-bytes raw pixels size, situated at 0x22
- fseek(temp, 0x22, SEEK_SET);
- size = fgetc(temp) | (fgetc(temp) << 8) | (fgetc(temp) << 16) | (fgetc(temp) << 24);
- size >>= 1;
- // Gets the 4-bytes offset to the start of the pixel table, situated at 0x0a
- fseek(temp, 0x0a, SEEK_SET);
- offset = fgetc(temp) | (fgetc(temp) << 8) | (fgetc(temp) << 16) | (fgetc(temp) << 24);
- fseek(temp, offset, SEEK_SET);
- returnValue = malloc(size * sizeof(int));
- if(!returnValue)
- {
- printf("Couldn't allocate memory\n");
- fclose(temp);
- return NULL;
- }
- for(i = size - 1; i >= 0; i--)
- {
- color = (uint16_t)(fgetc(temp) | (fgetc(temp) << 8));
- returnValue[i] = (is_cx ? color : ((int)( 0.229 * (color >> 11) + 0.587 * ((color >> 6) & 0x1f) + 0.114 * (color & 0x1f)) >> 1));
- }
- fclose(temp);
- return returnValue;
- }
- #define texWidth 256
- #define texHeight 256
- #define screenWidth 240
- #define screenHeight 240
- int w = 240, h = 240, x, y;
- uint16_t *texture;
- int distanceTable[screenWidth][screenHeight];
- int angleTable[screenWidth][screenHeight];
- int buffer[screenWidth][screenHeight];
- void masksprite(void *buffer, int *data, int x, int y, int width, int height, int mask){
- int i, j;
- for(i = 0; i < height; i++){
- for(j = 0; j < width; j++){
- if(data[i*width+j] != mask && x+j < 320 && x+j > 0 && y+i < 240 && y+i > 0){
- setPixelBuf(buffer, x+j, y+i, data[i*width+j]);
- }
- }
- }
- }
- uint16_t RGBColor(uint8_t r, uint8_t g, uint8_t b)
- {
- return ((r / 8) << 11) | ((g / 4) << 5) | (b / 8);
- }
- int main(void) {
- char *screen;
- screen = (char*)malloc(SCREEN_BYTES_SIZE * sizeof(char)); // just a buffer
- if(!screen)
- {
- exit(0);
- }
- texture = loadBMP("/documents/ndless/texture.bmp.tns");
- if(!texture)
- {
- free(screen);
- exit(0);
- }
- initTP();
- lcd_ingray();
- for(x = 0; x < w; x++)
- for(y = 0; y < h; y++)
- {
- int angle, distance;
- float ratio = 32.0;
- distance = (unsigned int)(ratio * texHeight / sqrt((x - w / 2.0) * (x - w / 2.0) + (y - h / 2.0) * (y - h / 2.0))) % texHeight;
- angle = (unsigned int)(0.5 * texWidth * atan2(y - h / 2.0, x - w / 2.0) / M_PI);
- distanceTable[x][y] = distance;
- angleTable[x][y] = angle;
- }
- float animation = 0;
- int shiftY = (unsigned int)(texHeight * 0.25);
- //begin the loop
- while(!isKeyPressed(KEY_NSPIRE_ESC))
- {
- animation = animation+0.02;
- readTP();
- int TZ = getTouchedZone4();
- //calculate the shift values out of the animation value
- int shiftX = (unsigned int)(texWidth * 1.0 * animation);
- if(isKeyPressed(KEY_NSPIRE_RIGHT) || TZ==6)
- shiftY++;
- if(isKeyPressed(KEY_NSPIRE_LEFT) || TZ==4)
- shiftY--;
- for(x = 0; x < w; x++)
- for(y = 0; y < h; y++)
- {
- //get the texel from the texture by using the tables, shifted with the animation values
- int color = texture[(abs(angleTable[x][y] + shiftY) % texHeight)*texWidth+(abs(distanceTable[x][y] + shiftX) % texWidth)];
- buffer[x][y] = color;
- }
- masksprite(screen, buffer[0], 0, 0, 240, 240, 0xf800);
- refresh(screen);
- clearBuf(screen);
- }
- free(screen);
- endTP();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement