Demo entry 6688404

Animation

   

Submitted by ShirleyM on Jan 03, 2018 at 04:47
Language: C++. Code size: 5.2 kB.

void AnimationLoop(){
    int time[] = {1,1,1,1,1,1,1,1,1,1,0,0,1,1,1,1,0,0,0,0,1,1,1,1,1,0,0,0,0};//雷电动画轴
	int Tag_stop = 0;
	int Tag_NSwind = 0;
	int Tag_WEwind = 0;
	int mouse_right_down = 0;
	int mouse_left_down = 0;
	int wheelvalue = 0;
	key_msg k = {0};
	mouse_msg msg = {0};
	int middle_len_x = 0;
	int middle_len_y = 0;
	int Button_x = 300;
	int Button_y = 300;
	int rainnum = 3;
	int rotatemsgX;
	int rotatemsgY;
    node* p;//读取随机链表的指针(会被不断重置为qhead)
    node* atail;//动画队列的尾指针
    //初始化链表
    p = init();
    p = data(p);
    randomX(p);
    p = qhead;
    atail= qhead;
    for(int i = 0; i< rainnum;i++){
        atail = atail -> next;
    }
    //三维查看的参数(旋转与缩放)
    double lrangle = 5;
    double udangle = 5;
    double scalestep = 1 ;
    //荷叶与雨滴的矩阵和向量
    Vector4d rain;
    Vector4d leave;
    Vector4d MVPleave;
    Vector4d MVPrain;
    Matrix4d MVP;
    leave<<100,300,-1000,1;
    //主循环,职能是读取输入操作以及绘制每一帧的画面
    for(;is_run(); delay_fps(60)){
        cleardevice();
        //读取键盘与鼠标的输入
        while(mousemsg()){
            msg = getmouse();
        }
        //滚轮滑动
        wheelvalue = msg.wheel;
        if(wheelvalue==120){
            scalestep += 0.01;

        }else if (wheelvalue==-120){
            scalestep -= 0.01;
        }
        wheelvalue = 0;
        //左键操作风力轴
        if((int)msg.is_down()==1&&msg.is_left())mouse_left_down = 1;
        if((int)msg.is_up()==1&&msg.is_left())mouse_left_down = 0;
        if( mouse_left_down == 1 ){
            if(msg.x<=500&&msg.x>=100&&msg.y<=500&&msg.y>=100){
                middle_len_x = (300 - msg.x)*-0.1;
                middle_len_y = (300 - msg.y)*-0.1;
                Button_x = msg.x;
                Button_y = msg.y;
            }
        }
        if(Tag_WEwind<middle_len_x){
                Tag_WEwind ++;
            }
        else if(Tag_WEwind>middle_len_x){
                Tag_WEwind --;
        }
        if(Tag_NSwind<middle_len_y){
                Tag_NSwind ++;
            }
        else if(Tag_NSwind>middle_len_y){
                Tag_NSwind --;
        }
        //右键操作摄像机旋转镜头
        if((int)msg.is_down()==1&&msg.is_right()){
                mouse_right_down = 1;
                rotatemsgX = msg.x;
                rotatemsgY = msg.y;
        }
        if((int)msg.is_up()==1&&msg.is_right())mouse_right_down = 0;
        if( mouse_right_down == 1 ){
                lrangle = msg.x - rotatemsgX;
                udangle = -(msg.y - rotatemsgY);
        }
        //键盘左右键实现调整雨滴的大小
        if(kbhit()){
                k = getkey();
                if(k.key == VK_LEFT){
                    Tag_stop = 1;
                }
                else if(k.key == VK_RIGHT){
                    Tag_stop = 2;
                }

        }
        //生成MVP矩阵
        MVP = createMVP(lrangle ,udangle,scalestep);
        //绘制环境
        drawwater(0,0,-1000,MVP);
        MVPleave = MVP*leave;
        drawleave(MVPleave(0,0),MVPleave(1,0),udangle,lrangle,scalestep);
        drawlotus(100,300,-1000,2,MVP);
        drawcloud(100,300,1000,2,udangle,lrangle,scalestep,MVP);
        //绘制雨滴
        for(int i = 0; p!=atail  ; i++) {
			//雨滴下落中
			if(p->z >= p->h){
                p->x += Tag_WEwind;//风力
                p->y += Tag_NSwind;//风力
                p->z -= p->dy;//下落
                //vector
                rain<<p->x,p->y,p->z,1;
                MVPrain = MVP*rain;
                drawrain(p->color,MVPrain(0,0),MVPrain(1,0),p->r*scalestep);
            //雨滴落地,波纹扩散
			}else if(p->z < p->h && p->r < p->ra){
                p->r+=p->kv;//调整圆半径大小来实现波纹的扩散效果
                if(!(p->x>-100&&p->x<300&&p->y>100&&p->y<500)&&(p->x>-1000&&p->x<1000&&p->y>-1000&&p->y<1000)){
                    rain<<p->x,p->y,p->z,1;
                    MVPrain = MVP*rain;
                    drawraincircle(p->color,MVPrain(0,0),MVPrain(1,0),p->r*scalestep,p->r*scalestep*sin(udangle/180*pi));
                }
            //扩散完成
            }else if(p->z < p->h && p->r > p->ra){
                p->r=0;
                p->kv=0;
                Qhead_Forward();//将已经播放完毕的动画移除出队列
                Qtail_Forward();//补充新的动画,维持队列长度不变
                switch(Tag_stop){//根据前面的操作决定是否改变动画队列的长度,即以此来控制雨滴的疏密
                    case 0:atail = atail->next;break;//默认情况:不改变队列长度
                    case 1:if(qhead->next==atail){atail = atail->next;}break;//缩减队列,雨变小
                    case 2:for(int i = 0;i<5;i++){ //增大队列,雨变大
                        if(atail->next!=qtail){
                            atail = atail->next;
                        }
                    }break;
                }
                //判断是否打雷
                if(p->x<50&&p->y<50&&time[i]==1){
                    lighting(100,300,800,2,MVP);//打雷
                }else if (p->x<50&&p->y<50&&time[i]== 0 ){
                //lighted();//不打雷
                }
                i<20?i++:i=0;
            }
            p = p->next;
        }
    Tag_stop = 0; //把Tag_stop 重置回中间值
    p = qhead;
    drawcube(MVP);//绘制边框
    //绘制UI界面
    liner();
    drawbutton(Button_x,Button_y);
    }
}

This snippet took 0.02 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).