【发布时间】:2016-07-12 23:17:32
【问题描述】:
当我运行我的应用程序时,我遇到了大量的输入延迟。
更多详情: 当我按“w”、“a”、“s”、“d”(我分配的输入键)时,对象会移动,但是在释放键后它会继续移动很长一段时间。源代码在下面,但是为了缩短问题,已经剪掉了一小部分代码,但是如果下面的源代码无法编译,我会将所有代码都放在 github 上。 https://github.com/TreeStain/DodgeLinuxGame.git 谢谢你的时间。 -特里斯坦
道奇.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;
附:请随意批评我的方法和代码,因为我是编程新手,可以接受我能得到的所有批评。
【问题讨论】:
-
您可能想要分析您的代码。在此处查找您可以使用的工具:softwarerecs.stackexchange.com/questions/9992/linux-c-profiler
-
这可能不是您问题的解决方案。但如果可能的话,我会用适当数量的“”来代替对
mvprintw(y, x, " ");的多次调用。 gcc.gnu.org/onlinedocs/gcc/Variable-Length.html 可能会有所帮助
标签: c loops input delay ncurses