我在運行我的應用程序時遇到主要的輸入滯後量。如何減少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