Some snow based on a C64 BASIC routine

Dependencies:   PokittoLib

Fork of HelloWorld by Pokitto Community Team

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "Pokitto.h"
00002 
00003 Pokitto::Core mygame;
00004 
00005 
00006 // snow flake data
00007 const byte PROGMEM frames[60]={
00008 4,8,12,16,12,8,4,0,
00009 24,20,24,28,32,36,32,28,
00010 56,52,48,44,40,44,48,52,
00011 44,48,52,56,52,48,44,40,
00012 64,68,72,76,80,76,72,68,
00013 64,60,76,80,76,72,68,64,
00014 60,64,68,72,96,100,96,92,
00015 88,84,88,92};
00016 const byte PROGMEM flkimage[104] ={
00017 192,224,96,0,0,96,112,48,
00018 0,0,60,24,0,6,14,12,
00019 3,7,6,0,128,192,64,0,
00020 0,64,96,32,0,0,24,24,
00021 0,2,6,4,1,3,2,0,
00022 192,128,0,0,0,192,64,0,
00023 0,0,48,0,0,12,8,0,
00024 12,4,0,0,128,128,0,0,
00025 0,128,64,0,0,0,96,0,
00026 0,0,48,0,0,16,32,0,
00027 16,16,0,0,128,0,0,0,
00028 0,128,0,0,0,0,64,0,
00029 0,32,0,0,32,0,0,0,
00030 };
00031 int offset[18]={
00032 1,0,8,3,3,6,6,6,
00033 6,6,5,5,5,4,0,0,
00034 1,3};
00035 unsigned char mx[18]={
00036 7,7,9,7,7,9,7,7,
00037 9,7,7,9,7,9,7,7,
00038 7,7};
00039 unsigned char fptrs[18]={
00040 8,16,32,52,24,42,16,8,
00041 42,24,52,32,16,32,8,8,
00042 0,0};
00043 unsigned char vert[18]={
00044 168,24,144,160,16,88,128,120,
00045 40,8,56,176,104,56,80,152,
00046 152,96};
00047 unsigned char countdn[18]={
00048 0,5,1,5,2,2,4,3,
00049 2,1,6,5,5,3,2,2,
00050 1,0};
00051 unsigned char spead[18]={
00052 4,4,4,4,4,4,4,5,
00053 5,5,5,5,5,3,3,3,
00054 2,1};
00055 unsigned char tempimg[72]={
00056 0,0,0,0,0,0,0,0,
00057 0,0,0,0,0,0,0,0,
00058 0,0,0,0,0,0,0,0,
00059 0,0,0,0,0,0,0,0,
00060 0,0,0,0,0,0,0,0,
00061 0,0,0,0,0,0,0,0,
00062 0,0,0,0,0,0,0,0,
00063 0,0,0,0,0,0,0,0,
00064 0,0,0,0,0,0,0,0}; 
00065 
00066 int numCols = mygame.display.width/8;
00067 int maxHeight = mygame.display.height;
00068 
00069 unsigned char rowStart[27]; // max width in 8x8 tiles
00070 unsigned char snowRow[176]; // max height
00071 
00072 void updateSnow(){
00073 // Main snow-feild code
00074 //unsigned char ctr=0;
00075 unsigned char stp = 1;
00076 for(int loop=17; loop>stp+7; loop-=stp){ // 17
00077 countdn[loop]--;
00078 if(countdn[loop]==0){
00079     countdn[loop]=spead[loop];
00080     //remove old flake image
00081     int y = vert[loop];
00082     int a=0;
00083     for(a=0; a<4; a++){
00084         snowRow[y] &= (255-tempimg[loop*4+a]);
00085         y++; if(y>=maxHeight){y=0;} // was 176
00086     }//a
00087 
00088     // rem calculate new vertical position
00089     // rem (vert) & frame number (offset)
00090     vert[loop]++;
00091     if(vert[loop]>=maxHeight){vert[loop]=0;}
00092     offset[loop]--;
00093     if(offset[loop]<0){offset[loop]=mx[loop];}
00094         // rem get next frame of animation
00095         y=pgm_read_byte(&frames[fptrs[loop]+offset[loop]]);
00096         for(a=0; a<4; a++){
00097           tempimg[loop*4+a]=pgm_read_byte(&flkimage[y+a]);
00098         }
00099         // rem add new frame at new position
00100     y=vert[loop];
00101     for(a=0; a<4; a++){
00102       snowRow[y] |= tempimg[loop*4+a];
00103       y++; if(y>=maxHeight){y=0;}
00104     }
00105     
00106   } // countdn
00107    }// loop   
00108   for(char x=0; x<numCols; x++){
00109     for(char y=0; y<maxHeight; y++){
00110       char p = rowStart[x]+y;
00111       if(p>=maxHeight){p-=maxHeight;}
00112       for(char t=0; t<7; t++){
00113         char col = 0;
00114         if(snowRow[p] & (1<<t)) col=1;
00115         mygame.display.drawPixel(t+(x*8),y,col);
00116       }
00117     }
00118   }
00119 }
00120 
00121 
00122 int main () {
00123     
00124     for(char t=0; t<18; t++){
00125         vert[t]=random(maxHeight>>1);
00126         if(t%1==0){vert[t]<<=1;}
00127     }
00128     for(char t=0; t<numCols; t++){
00129         rowStart[t]=(random(maxHeight>>1));
00130         if(t%1==0){rowStart[t]<<=1;}
00131     }    
00132     
00133     mygame.begin();
00134     while (mygame.isRunning()) {
00135         if (mygame.update()) {
00136             updateSnow();
00137             } 
00138         }    
00139     
00140 }