我在运行我的应用程序时遇到主要的输入滞后量。如何减少NCurses C应用程序中的输入滞后
更多详细信息: 当我按'w','a','s','d'(我分配的输入键)时,物体会移动,但在键持续一段时间后它会继续移动被释放。源代码在下面,但是代码的小部分已经被剪掉以缩短问题,但是如果下面的源代码不能编译,我将所有的代码放在github上。 https://github.com/TreeStain/DodgeLinuxGame.git谢谢你的时间。 -Tristan
dodge.c:
#define ASPECT_RATIO_X 2
#define ASPECT_RATIO_Y 1
#define FRAMES_PER_SECOND 60
#include <ncurses.h>
#include "object.h"
#include "render.h"
int main()
{
initscr();
cbreak();
noecho();
nodelay(stdscr, 1);
object objs[1];
object colObj; colObj.x = 10; colObj.y = 6;
colObj.w = 2; colObj.h = 2;
colObj.sprite = '*';
colObj.ySpeed = 1;
colObj.xSpeed = 1;
objs[0] = colObj;
//halfdelay(1);
while (1)
{
char in = getch();
if (in == 'w')
objs[0].y -= objs[0].ySpeed * ASPECT_RATIO_Y;
if (in == 's')
objs[0].y += objs[0].ySpeed * ASPECT_RATIO_Y;
if (in == 'a')
objs[0].x -= objs[0].xSpeed * ASPECT_RATIO_X;
if (in == 'd')
objs[0].x += objs[0].xSpeed * ASPECT_RATIO_X;
render(objs, 1);
napms(FRAMES_PER_SECOND);
}
getch();
endwin();
return 0;
}
render.h:
void render(object obj[], int objectNum);
void render(object obj[], int objectNum) //Takes array of objects and prints them to screen
{
int x, y, i, scrWidth, scrHeight;
getmaxyx(stdscr, scrHeight, scrWidth); //Get terminal height and width
for (y = 0; y < scrHeight; y++)
{
for (x = 0; x < scrWidth; x++)
{
mvprintw(y, x, " ");
}
}
for (i = 0; i < objectNum; i++)
{
int xprint = 0, yprint = 0;
for (yprint = obj[i].y; yprint < obj[i].y + (obj[i].h * ASPECT_RATIO_Y); yprint++)
{
for (xprint = obj[i].x; xprint < obj[i].x + (obj[i].w * ASPECT_RATIO_X); xprint++)
mvprintw(yprint, xprint, "%c", obj[i].sprite);
}
}
refresh();
}
object.h:
typedef struct
{
int x, y, w, h, ySpeed, xSpeed;
char sprite;
}object;
P.S.请随时批评我的方法和代码,因为我在编程方面相当新颖,可以采取所有我能得到的批评。
您可能想要分析您的代码。在这里寻找你可以使用的工具:http://softwarerecs.stackexchange.com/questions/9992/linux-c-profiler – Plouff
这可能不是你的问题的解决方案。但是如果可能的话,我会用多次调用“mvprintw(y,x,”“);'”来调用多次调用。 https://gcc.gnu.org/onlinedocs/gcc/Variable-Length.html可能会帮助 – Plouff