EASYX精确帧率控制

编程入门 行业动态 更新时间:2024-10-09 05:19:15

EASYX<a href=https://www.elefans.com/category/jswz/34/1764196.html style=精确帧率控制"/>

EASYX精确帧率控制

eg1:小球左右摆动的代码

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <easyx.h>
#include <iostream>
#include <math.h>
#include <stdlib.h>
#include <conio.h>
#include <time.h>
#define PI 3.14、
/*计算时长的步骤1:获取一次计数器当前的数值,记为startCount2:处理事务3:再次获取计算器当前数值,记为endCount4:用(endCouny - startCount)/F ,计算所耗时长,单位为秒,将秒转换为微秒的话可以乘以1000000
*/int main() {initgraph(800, 600);setbkcolor(RGB(164, 225, 202));cleardevice();setfillcolor(WHITE);int x = 0; int y = 300;int vx = 5;// 表示批量绘制的开始BeginBatchDraw();while (1) {cleardevice();// 圆形绘制函数,圆心的坐标为(x,y)半径为50solidcircle(x, y, 50);// 改变坐标向右一直绘制移动x += vx;// 对小球的位置做出判断不能让小球操作画面的边缘if (x <= 0 || x >= 800) {vx = -vx;}Sleep(40);// 批量绘制显示一下FlushBatchDraw();}EndBatchDraw();closegraph();return 0;
}

修改小球左右摆动的代码实现精确帧率控制

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <easyx.h>
#include <iostream>
#include <math.h>
#include <stdlib.h>
#include <conio.h>
#include <time.h>
#define PI 3.14、
/*计算时长的步骤1:获取一次计数器当前的数值,记为startCount2:处理事务3:再次获取计算器当前数值,记为endCount4:用(endCouny - startCount)/F ,计算所耗时长,单位为秒,将秒转换为微秒的话可以乘以1000000
*/int main() {// 提高系统定时器时钟分辨率为1毫秒timeBeginPeriod(1);initgraph(800, 600);setbkcolor(RGB(164, 225, 202));cleardevice();setfillcolor(WHITE);// 开始时间,结束时间,频率FLARGE_INTEGER startCount, endCount, F;// 获取频率FQueryPerformanceFrequency(&F);int x = 0; int y = 300;int vx = 5;// 表示批量绘制的开始BeginBatchDraw();while (1) {// 获取起始计数QueryPerformanceCounter(&startCount);​	// 准备画面cleardevice();// 圆形绘制函数,圆心的坐标为(x,y)半径为50solidcircle(x, y, 50);// 改变坐标向右一直绘制移动x += vx;// 对小球的位置做出判断不能让小球操作画面的边缘if (x <= 0 || x >= 800) {vx = -vx;}// 获取结束计数QueryPerformanceCounter(&endCount);// 计算时差long long elapse = (endCount.QuadPart - startCount.QuadPart)/ F.QuadPart * 1000000;while(elapse < 40000){Sleep(1);// 重新获取结束时间QueryPerformanceCounter(&endCount);// 更新时差elapse = (endCount.QuadPart - startCount.QuadPart)* 1000000 / F.QuadPart;}// 批量绘制显示一下FlushBatchDraw();}EndBatchDraw();closegraph();//timeBeginPeriod与timeEndPeriod必须成对存在,并且传一样的值timeEndPeriod(1);return 0;}

小球处于运动状态之中

更多推荐

EASYX精确帧率控制

本文发布于:2023-11-16 18:54:06,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1631319.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:精确   EASYX

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!