C++ 关于ontimer函数求助

qq_39984395 2018-04-16 09:44:32

#define NDIV 50
#define PI 3.1415926

float xc[NDIV],yc[NDIV],xb,yb,xe,ye;
float AB=100,BC=330,CE=330,DE=100;
float xa=-120,ya=200,xd=120,yd=200;
int ipos=0;
void CalcAndDrawMech(float L,float ratio)
{

float phi2,phi3,phi4,phi5;
float x1,y1,x2,y2,x3,y3,x4,y4,x5,y5,x0,y0;

x0=0,y0=-150;
x1=x0;y1=y0+L;
x2=x0-L*cos(54*PI/180);y2=y0-L*sin(54*PI/180);
x3=x0+L*cos(18*PI/180);y3=y0+L*sin(18*PI/180);
x4=x0-L*cos(18*PI/180);y4=y0+L*sin(18*PI/180);
x5=x0+L*cos(54*PI/180);y5=y0-L*sin(54*PI/180);
glColor3f(1,0,0);

/*glBegin(GL_LINE_STRIP);
glVertex3f(x1*ratio,y1*ratio,0);
glVertex3f(x2*ratio,y2*ratio,0);
glVertex3f(x3*ratio,y3*ratio,0);
glVertex3f(x4*ratio,y4*ratio,0);
glVertex3f(x5*ratio,y5*ratio,0);
glVertex3f(x1*ratio,y1*ratio,0);
glEnd();*/
float k;
float a,b;

/*printf("x1=%f\n",x1);
printf("y1=%f\n",y1);
printf("x2=%f\n",x2);
printf("y2=%f\n",y2);
printf("x3=%f\n",x3);
printf("y3=%f\n",y3);
printf("x4=%f\n",x4);
printf("y4=%f\n",y4);*/
if(ipos<10) {a=ipos;b=10-ipos;k=a/b;xc[ipos]=(x1+k*x2)/ (k+1)*ratio;yc[ipos]=(y1+k*y2)/ (k+1)*ratio;}
else if(ipos<20) {a=ipos-10;b=10-(ipos-10);k=a/b;xc[ipos]=(x2+k*x3)/ (k+1)*ratio;yc[ipos]=(y2+k*y3)/ (k+1)*ratio;}
else if(ipos<30) {a=ipos-20;b=10-(ipos-20);k=a/b;xc[ipos]=(x3+k*x4)/ (k+1)*ratio;yc[ipos]=(y3+k*y4)/ (k+1)*ratio;}
else if(ipos<40) {a=ipos-30;b=10-(ipos-30);k=a/b;xc[ipos]=(x4+k*x5)/ (k+1)*ratio;yc[ipos]=(y4+k*y5)/ (k+1)*ratio;}
else if(ipos<50) {a=ipos-40;b=10-(ipos-40);k=a/b;xc[ipos]=(x5+k*x1)/ (k+1)*ratio;yc[ipos]=(y5+k*y1)/ (k+1)*ratio;}
else return;

//printf("ipos=%d\n",ipos);
printf("a=%f\n",a);
printf("b=%f\n",b);

printf("k=%f\n",k);
printf("xc=%f\n",xc[ipos]);
printf("yc=%f\n",yc[ipos]);

RRR(xc[ipos],yc[ipos],xa,ya,BC,AB,1,&phi2,&phi3);
RRR(xc[ipos],yc[ipos],xd,yd,CE,DE,-1,&phi4,&phi5);
xb=xc[ipos]+BC*cos(phi2);
yb=yc[ipos]+BC*sin(phi2);
xe=xc[ipos]+CE*cos(phi4);
ye=yc[ipos]+CE*sin(phi4);
printf("xb=%f\n",xb);
printf("yb=%f\n",yb);
printf("xe=%f\n",xe);
printf("ye=%f\n",ye);
glColor3f(1,0,0);

glBegin(GL_LINE_STRIP);
glVertex3f(xa*ratio,ya*ratio,0);
glVertex3f(xb*ratio,yb*ratio,0);
glVertex3f(xc[ipos]*ratio,yc[ipos]*ratio,0);
glVertex3f(xe*ratio,ye*ratio,0);
glVertex3f(xd*ratio,yd*ratio,0);
glEnd();
int i;
glPointSize(5.0f);
glBegin(GL_POINTS);
for(i=0;i<NDIV;i++)
glVertex3f(xc[i]*ratio,yc[i]*ratio,0);
glEnd();
}
int EYE=500;

void myDisplay(void)
{
glEnable(GL_DEPTH_TEST);
glShadeModel(GL_SMOOTH); //GL_FLAT
//设置缓冲颜色
glClearColor(0.7,0.7,0.7,1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
InitLight();

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(75, 1, 1, 5000);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
//设置视点
gluLookAt(0, 0, EYE, 0, 0, 0, 0, 1, 0);
//添加坐标轴

//DrawAxis();
//RenderParts();
//printf("ipos=%d\n",ipos);
CalcAndDrawMech(60,1);


glFlush();
}
void OnTimer(int id)
{
if(ipos<NDIV-1) ipos++;
else ipos=0;
printf("ipos=%d\n",ipos);
glutPostRedisplay();

glutTimerFunc(100,OnTimer,1);
}

限于篇幅就只复制了一部分关键部分的代码
问题在于改变ipos的时候 输出显示当ipos=2和3的时候display未被调用。。即ipos的确是加上去了但是各点坐标没有计算。。当ipos等于其他的时候都是可以的。。并且在一次循环后,ipos重新从0开始时2和3就可以计算了。。。数组的也会被重新分配值。
函数本身没有问题 ,ontimer函数调用也是正确的。
其中最奇怪的是当改变ontimer的时间间隔的时候,即调整gluttimerfunc的第一个数值的时候,不同的值会使不同的ipos的值出现上述问题。。当其大于1000(估计值,并不精确)时,函数完整顺利的调用。。
求大佬帮忙。。
附上程序运行效果截图


...全文
1096 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2018-04-17
  • 打赏
  • 举报
回复
仅供参考:
#pragma comment(lib,"user32")
#include <stdio.h>
#include <time.h>
#include <sys/timeb.h>
#include <windows.h>
CRITICAL_SECTION cs_log;
char datestr[16];
char timestr[16];
char mss[4];
void log(char *s) {
    struct tm *now;
    struct timeb tb;

    EnterCriticalSection(&cs_log);
    ftime(&tb);
    now=localtime(&tb.time);
    sprintf(datestr,"%04d-%02d-%02d",now->tm_year+1900,now->tm_mon+1,now->tm_mday);
    sprintf(timestr,"%02d:%02d:%02d",now->tm_hour     ,now->tm_min  ,now->tm_sec );
    sprintf(mss,"%03d",tb.millitm);
    printf("%s %s.%s %s",datestr,timestr,mss,s);
    LeaveCriticalSection(&cs_log);
}
VOID CALLBACK myTimerProc1(
  HWND hwnd, // handle of window for timer messages
  UINT uMsg, // WM_TIMER message
  UINT idEvent, // timer identifier
  DWORD dwTime // current system time
) {
 log("In myTimerProc1\n");
}
VOID CALLBACK myTimerProc2(
  HWND hwnd, // handle of window for timer messages
  UINT uMsg, // WM_TIMER message
  UINT idEvent, // timer identifier
  DWORD dwTime // current system time
) {
 log("In myTimerProc2\n");
}
int main() {
    int i;
    MSG msg;

    InitializeCriticalSection(&cs_log);
    SetTimer(NULL,0,1000,myTimerProc1);
    SetTimer(NULL,0,2000,myTimerProc2);
    for (i=0;i<20;i++) {
        Sleep(500);
        log("In main\n");
        if (GetMessage(&msg,NULL,0,0)) {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }

    }
    DeleteCriticalSection(&cs_log);
    return 0;
}
//2018-04-17 10:07:54.218 In main
//2018-04-17 10:07:54.718 In myTimerProc1
//2018-04-17 10:07:55.218 In main
//2018-04-17 10:07:55.718 In myTimerProc2
//2018-04-17 10:07:56.218 In main
//2018-04-17 10:07:56.218 In myTimerProc1
//2018-04-17 10:07:56.718 In main
//2018-04-17 10:07:56.718 In myTimerProc1
//2018-04-17 10:07:57.218 In main
//2018-04-17 10:07:57.718 In myTimerProc2
//2018-04-17 10:07:58.218 In main
//2018-04-17 10:07:58.218 In myTimerProc1
//2018-04-17 10:07:58.718 In main
//2018-04-17 10:07:58.718 In myTimerProc1
//2018-04-17 10:07:59.218 In main
//2018-04-17 10:07:59.718 In myTimerProc2
//2018-04-17 10:08:00.218 In main
//2018-04-17 10:08:00.218 In myTimerProc1
//2018-04-17 10:08:00.718 In main
//2018-04-17 10:08:00.718 In myTimerProc1
//2018-04-17 10:08:01.218 In main
//2018-04-17 10:08:01.718 In myTimerProc2
//2018-04-17 10:08:02.218 In main
//2018-04-17 10:08:02.218 In myTimerProc1
//2018-04-17 10:08:02.718 In main
//2018-04-17 10:08:02.718 In myTimerProc1
//2018-04-17 10:08:03.218 In main
//2018-04-17 10:08:03.718 In myTimerProc2
//2018-04-17 10:08:04.218 In main
//2018-04-17 10:08:04.218 In myTimerProc1
//2018-04-17 10:08:04.718 In main
//2018-04-17 10:08:04.718 In myTimerProc1
//2018-04-17 10:08:05.218 In main
//2018-04-17 10:08:05.718 In myTimerProc2
//2018-04-17 10:08:06.218 In main
//2018-04-17 10:08:06.218 In myTimerProc1
//2018-04-17 10:08:06.718 In main
//2018-04-17 10:08:06.718 In myTimerProc1
//2018-04-17 10:08:07.218 In main
//2018-04-17 10:08:07.718 In myTimerProc2

赵4老师 2018-04-17
  • 打赏
  • 举报
回复
只要你使用和我这个例子相同的写日志功能,就应该不会有问题;即使有问题,也很容易找到原因。
qq_39984395 2018-04-17
  • 打赏
  • 举报
回复
引用 1 楼 zhao4zhong1 的回复:
仅供参考:
#pragma comment(lib,"user32")
#include <stdio.h>
#include <time.h>
#include <sys/timeb.h>
#include <windows.h>
CRITICAL_SECTION cs_log;
char datestr[16];
char timestr[16];
char mss[4];
void log(char *s) {
    struct tm *now;
    struct timeb tb;

    EnterCriticalSection(&cs_log);
    ftime(&tb);
    now=localtime(&tb.time);
    sprintf(datestr,"%04d-%02d-%02d",now->tm_year+1900,now->tm_mon+1,now->tm_mday);
    sprintf(timestr,"%02d:%02d:%02d",now->tm_hour     ,now->tm_min  ,now->tm_sec );
    sprintf(mss,"%03d",tb.millitm);
    printf("%s %s.%s %s",datestr,timestr,mss,s);
    LeaveCriticalSection(&cs_log);
}
VOID CALLBACK myTimerProc1(
  HWND hwnd, // handle of window for timer messages
  UINT uMsg, // WM_TIMER message
  UINT idEvent, // timer identifier
  DWORD dwTime // current system time
) {
 log("In myTimerProc1\n");
}
VOID CALLBACK myTimerProc2(
  HWND hwnd, // handle of window for timer messages
  UINT uMsg, // WM_TIMER message
  UINT idEvent, // timer identifier
  DWORD dwTime // current system time
) {
 log("In myTimerProc2\n");
}
int main() {
    int i;
    MSG msg;

    InitializeCriticalSection(&cs_log);
    SetTimer(NULL,0,1000,myTimerProc1);
    SetTimer(NULL,0,2000,myTimerProc2);
    for (i=0;i<20;i++) {
        Sleep(500);
        log("In main\n");
        if (GetMessage(&msg,NULL,0,0)) {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }

    }
    DeleteCriticalSection(&cs_log);
    return 0;
}
//2018-04-17 10:07:54.218 In main
//2018-04-17 10:07:54.718 In myTimerProc1
//2018-04-17 10:07:55.218 In main
//2018-04-17 10:07:55.718 In myTimerProc2
//2018-04-17 10:07:56.218 In main
//2018-04-17 10:07:56.218 In myTimerProc1
//2018-04-17 10:07:56.718 In main
//2018-04-17 10:07:56.718 In myTimerProc1
//2018-04-17 10:07:57.218 In main
//2018-04-17 10:07:57.718 In myTimerProc2
//2018-04-17 10:07:58.218 In main
//2018-04-17 10:07:58.218 In myTimerProc1
//2018-04-17 10:07:58.718 In main
//2018-04-17 10:07:58.718 In myTimerProc1
//2018-04-17 10:07:59.218 In main
//2018-04-17 10:07:59.718 In myTimerProc2
//2018-04-17 10:08:00.218 In main
//2018-04-17 10:08:00.218 In myTimerProc1
//2018-04-17 10:08:00.718 In main
//2018-04-17 10:08:00.718 In myTimerProc1
//2018-04-17 10:08:01.218 In main
//2018-04-17 10:08:01.718 In myTimerProc2
//2018-04-17 10:08:02.218 In main
//2018-04-17 10:08:02.218 In myTimerProc1
//2018-04-17 10:08:02.718 In main
//2018-04-17 10:08:02.718 In myTimerProc1
//2018-04-17 10:08:03.218 In main
//2018-04-17 10:08:03.718 In myTimerProc2
//2018-04-17 10:08:04.218 In main
//2018-04-17 10:08:04.218 In myTimerProc1
//2018-04-17 10:08:04.718 In main
//2018-04-17 10:08:04.718 In myTimerProc1
//2018-04-17 10:08:05.218 In main
//2018-04-17 10:08:05.718 In myTimerProc2
//2018-04-17 10:08:06.218 In main
//2018-04-17 10:08:06.218 In myTimerProc1
//2018-04-17 10:08:06.718 In main
//2018-04-17 10:08:06.718 In myTimerProc1
//2018-04-17 10:08:07.218 In main
//2018-04-17 10:08:07.718 In myTimerProc2

那个。。这边只是一个大学生的编程水平。。能否解释一下这个和我问题的具体关联呢。。。 大概看得明白是在研究每一步的用时。。 意思是要利用这个代码来判断我这个程序的运行用时吗? 另外如果运行用时的原因,之前用到ontimer即使间隔设置为10也没出现什么问题啊。。

64,652

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧