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:获取一次计数器当前的数值,记为startCount
  2:处理事务
  3:再次获取计算器当前数值,记为endCount
  4:用(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)半径为50
		solidcircle(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:获取一次计数器当前的数值,记为startCount
  2:处理事务
  3:再次获取计算器当前数值,记为endCount
  4:用(endCouny - startCount)/F ,计算所耗时长,单位为秒,将秒转换为微秒的话可以乘以1000000
*/

int main() {
	// 提高系统定时器时钟分辨率为1毫秒
	timeBeginPeriod(1);

	initgraph(800, 600);
	setbkcolor(RGB(164, 225, 202));
	cleardevice();
	setfillcolor(WHITE);
	
	// 开始时间,结束时间,频率F
	LARGE_INTEGER startCount, endCount, F;
	
	// 获取频率F
	QueryPerformanceFrequency(&F);


	int x = 0; 
	int y = 300;
	int vx = 5;
	// 表示批量绘制的开始
	BeginBatchDraw();
	while (1) {
		// 获取起始计数
		QueryPerformanceCounter(&startCount);// 准备画面
		cleardevice();
		// 圆形绘制函数,圆心的坐标为(x,y)半径为50
		solidcircle(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;

}

小球处于运动状态之中
在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值