easyx测试

· · 个人记录

#include<easyx.h>
#include<conio.h>
#include<cstdio>
#include<iostream>
#include<thread>
#include<random>
#include<chrono>

#define stdFallingSpeed 7.5//单位px 
#define prepTime 2000
#define frameTime 10
#define myWHITE ((Color){255,255,255})
#define myBLUE ((Color){10,195,255})
#define myGOLD ((Color){240,237,105})
#define lineF(x) ((firstx*lsty-lstx*firsty)/(firstx-lstx))
using namespace std::chrono;

std::mt19937 rng(time(0));
int noteCnt=0,nowNote=1,width,height,lineCnt=1;
steady_clock::time_point nowTime,startTime;

int calDurationMilliSec(steady_clock::time_point after,steady_clock::time_point before)
{
    return (int)duration<double,std::milli>(after-before).count();
}
void getTime()
{
    std::thread getT(
    []()
    {
        while(1)
        {
            nowTime=steady_clock::now();
            //printf("%d\n",calDurationMilliSec(nowTime,startTime));
            Sleep(2);
        }
    }
    );
    getT.detach();
}
struct Color{
    short R,G,B;
};
class DeciLine{
    private:
        int id;
        bool isShow;
        double lineK,firstx,firsty,lstx,lsty;
        double rendX1,rendY1,rendX2,rendY2;
        Color col;
    public:
        DeciLine()
        {
            id=-1,isShow=0;
            lineK=0,firstx=firsty=lstx=lsty=0;
            col=myWHITE;
        }
        void setParameter(double X1,double Y1,double X2,double Y2,int idx,Color c=myWHITE)
        {
            id=idx;
            firstx=X1,firsty=Y1,lstx=X2,lsty=Y2;
            col=c;
            if(lstx==firstx)
            {
                lineK=1e9;
                rendX1=firstx,rendY1=0,rendX2=lstx,rendY2=height;
            }
            else
            {
                lineK=(lsty-firsty)/(lstx-firstx);
                if(lsty==firsty)
                {
                    rendX1=0,rendY1=firsty,rendX2=width,rendY2=lsty;
                }
                else
                {
                    rendX1=firstx,rendY1=firsty,rendX2=lstx,rendY2=lsty;
                }
            }
        }
        void showLine()
        {
            isShow=1;
        }
        void rend()
        {
            setlinestyle(PS_SOLID,3);
            setlinecolor(RGB(col.R,col.G,col.B));
            line(rendX1,rendY1,rendX2,rendY2);
        }
        double giveY()
        {
            return firsty;
        }
}DLine[30];
class Note{
    private:
        bool isShow;
        int downMoment,showTime,decisionLine;
        double speed,nowX,nowY;
        Color fillCol,lineCol;
    public:
        Note()
        {
            isShow=false;
            showTime=0;
            downMoment=0;
            decisionLine=0;
            speed=1;
            nowX=nowY=0;
            fillCol=myBLUE,lineCol=myWHITE;
        }
        void setParameter(double X,double Y,double Speed,int downTime,int dLine,Color c1=myBLUE,Color c2=myWHITE)
        {
            nowX=X,nowY=Y,speed=Speed,downMoment=prepTime+downTime;
            decisionLine=dLine;
            showTime=downMoment-frameTime*DLine[decisionLine].giveY()*1.0/(speed*stdFallingSpeed);//暂未适配非水平判定线 
            fillCol=c1,lineCol=c2;
        }
        void showNote()
        {
            isShow=true;
        }
        int giveShowTime()
        {
            return showTime;
        }
        void rend()
        {
            if(isShow)
            {
                setlinecolor(RGB(lineCol.R,lineCol.G,lineCol.B));
                setfillcolor(RGB(fillCol.R,fillCol.G,fillCol.B));
                fillroundrect(nowX-30,nowY+3,nowX+30,nowY-3,3,3);
            }
        }
        void Move()
        {
            std::thread myMove(
            [this]()
            {
                while(calDurationMilliSec(nowTime,startTime)<=downMoment+80)
                {
                    nowY=(calDurationMilliSec(nowTime,startTime)-showTime)*1.0/frameTime*speed*stdFallingSpeed;
                    std::this_thread::sleep_for(std::chrono::milliseconds(frameTime));
                }
                isShow=false;
            });
            myMove.detach();
        }
}note[5005];
class Render{
    public:
    void rend()
    {
        std::thread RendNote(
        [this]()
        {
            BeginBatchDraw();
            while(1)
            {
                cleardevice();
                for(int i=0;i<lineCnt;i++)
                {
                    DLine[i].rend();
                }
                for(int i=1;i<=noteCnt;i++)
                {
                    note[i].rend();
                }
                FlushBatchDraw();
                std::this_thread::sleep_for(std::chrono::milliseconds(frameTime));
            }
            EndBatchDraw();
        }
        );
        RendNote.detach();
    }
    void noteControl()
    {
        std::thread ControlNote(
        []()
        {
            while(nowNote<=noteCnt)
            {
                printf("");//由于未知原因,删去这行会导致整个循环不执行 
                if(note[nowNote].giveShowTime()<=calDurationMilliSec(nowTime,startTime))
                {
                    printf("nowNote:%d nowTime:%d showTime:%d\n",nowNote,calDurationMilliSec(nowTime,startTime),note[nowNote].giveShowTime());
                    note[nowNote].showNote();
                    note[nowNote].Move();
                    nowNote++;
                }
            }
        }
        );
        ControlNote.detach();
    }
}render;
int main()
{
    initgraph(960,720,EX_SHOWCONSOLE);
    width=getwidth(),height=getheight();

    DLine[0].setParameter(0,480,width,480,0,myGOLD);
    lineCnt=1;
    for(int i=1;i<=5;i++)
    {
        note[i*6-5].setParameter(240,0,1,i*600-500,0);
        note[i*6-4].setParameter(400,0,1,i*600-400,0);
        note[i*6-3].setParameter(560,0,1,i*600-300,0);
        note[i*6-2].setParameter(720,0,1,i*600-200,0);
        note[i*6-1].setParameter(560,0,1,i*600-100,0);
        note[i*6].setParameter(400,0,1,i*600,0);
        noteCnt+=6;
    }   

    startTime=steady_clock::now();
    getTime();
    render.rend();
    render.noteControl();
    _getch();
    closegraph();
    return 0;
}

目前已做:

目前待做: