使用 bool* 管理的 OpenGL/GLUT 键盘:所有值在第二次初始化时似乎都设置为整数。有什么想法吗?

发布于 2024-11-18 12:41:24 字数 739 浏览 3 评论 0原文

使用 OpenGL/GLUT 和 C++ 制作游戏。

我听说在 GLUT 中管理键盘内容的最简单方法是使用布尔数组。

基本上,这就是我所拥有的:

bool* keys = new bool[256];

...

void keyDown(unsigned char ch, int x, int y)
{
    keys[ch] = true;    
}

void keyUp(unsigned char ch, int x, int y)
{
    keys[ch] = false;
}

然后,如果我想知道某个键的状态,我只需返回keys[key]。

我对特殊键有类似的设置。现在,在我的游戏中,当你死亡时,它会重置所有需要重置的值并重新开始游戏。直到现在,

keys = new bool[256];

还包括特殊键数组的类似声明。对我来说毫无意义的是,偶尔,这种假定的数组重置会导致键中的部分或全部值变成两位或三位整数(因此总是评估 true 并使玩家角色无法控制地移动,直到一个或更多明显按下的键被按下)。我怀疑发生这种情况是因为在游戏结束时,其中一个键被按下,从而当 C++ 试图使整个数组为假并发现有一件事被卡住时,它感到困惑。

伙计,这太令人困惑了。据我所知,这个问题已经通过消除所有额外的密钥初始化得到解决。但我不明白。有人对我如何设法将一些布尔值转换为整数有任何想法吗?

Making a game with OpenGL/GLUT and C++.

I've heard the easiest way to manage keyboard stuff in GLUT is to use an array of bools.

So basically, here's what I have:

bool* keys = new bool[256];

...

void keyDown(unsigned char ch, int x, int y)
{
    keys[ch] = true;    
}

void keyUp(unsigned char ch, int x, int y)
{
    keys[ch] = false;
}

Then if I want to know the state of a key, I just return keys[key].

I have a similar setup for the special keys. Now, in my game when you die, it resets all the values that need to be reset and starts the game again. Until just now, that included

keys = new bool[256];

and a similar declaration for the special keys array. What makes no sense to me is that occasionally, this supposed resetting of the arrays would cause some or all of the values in keys to become two- or three-digit integers (therefore always evaluating true and making the player character move uncontrollably until one or more of the keys that were apparently down were depressed). I have a suspicion that this happened because at time of endgame one of the keys was pressed down, thereby confusing C++ when it tried to make the whole array false and found that one thing was stuck true.

Man, that's confusing. The problem has been solved by eliminating all the extra initialisations of keys, as far as I can see. But I don't get it. Anyone got any ideas on how I managed to turn some bools into ints?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

时光瘦了 2024-11-25 12:41:25

由于尺寸小,数据类型(bool)也小,数组大小是确定性的;我会将它分配在堆栈中(在类级别或全局级别 - 无论 keys 声明的位置)。

class GlutWrapper
{
    bool Keys[256];
...
};

并且有一个函数,即 ResetKeys

void GlutWrapper::ResetKeys()
{ 
   memset(Keys, 0, sizeof(Keys));
}

Since the size is small, data type (bool) is small, and array size is deterministic; I would have allocated it in stack (at class level, or global level - wherever keys are declared).

class GlutWrapper
{
    bool Keys[256];
...
};

And having a function, that would ResetKeys:

void GlutWrapper::ResetKeys()
{ 
   memset(Keys, 0, sizeof(Keys));
}
眼眸 2024-11-25 12:41:24

该语句

keys = new bool[256];

分配内存。它将该内存初始化为任何特定值。为此,您可以将 memset(keys, 0, sizeof(bool) * 256) 设置为零。

但是,您不应该分配内存(这就是 new 所做的)来重新初始化数组。您实际上应该使用 memset 或用于使用“false”值初始化它的任何功能来重新初始化它。

最后,为什么要首先用 new 来分配这个数组?只需将其设为全局数组即可:bool keys[256]。在使用它之前初始化它(使用memset),一切都会好起来的。

我本来建议使用 std::vector,但遗憾的是,这些不可用。不过,您可以使用 std::vector ,其中 0 表示 false。或者是 256 位的 std::bitset。所有这些都在内部包含它们的分配(因此是安全的),并且有方法将它们的内存清除到某个值。

使用 OpenGL/GLUT 和 C++ 制作游戏。

顺便说一句,我不建议这样做。 GLUT 并不是一款专为大多数游戏所需的计时工具而设计的工具。 GLFW 允许您控制主循环,而不是使用回调,因此它是更适合游戏的工具。

This statement

keys = new bool[256];

Allocates memory. It does not initialize that memory to any particular value. To do that, you could do a memset(keys, 0, sizeof(bool) * 256) to zero.

However, you shouldn't allocate memory (which is what new does) to reinitialize an array. You should actually reinitialize it, using memset or whatever feature you used to initialize it with "false" values.

Lastly, why are you allocating this array with new to begin with? Just make it a global array: bool keys[256]. Initialize it (with memset) before you use it, and everything will be fine.

I would have suggested a std::vector<bool>, but sadly, those are not usable. You could use a std::vector<char> though, with 0 meaning false. Or a std::bitset with 256 bits. All of these contain their allocations internally (thus being safe), and have ways to clear their memory to some value.

making a game with OpenGL/GLUT and C++.

As an aside, I wouldn't advise that. GLUT isn't a tool designed for the kinds of timings that most games need. GLFW allows you to control the main loop, rather than using callbacks, so it is a more appropriate tool for a game.

琉璃梦幻 2024-11-25 12:41:24

由于 CPU 粒度,您只能以字节的形式访问事物,因此 bool 始终解析为某种大小为 1 字节的无符号字符类型。这样您可以获得 0-255 范围内的值。发生的情况是任何非零值都为真,0 为假。由于这种粒度,我们无法将 bool 实现为单个位(除了在 C++ STL 向量等中...它们被转换为位字段),除非您决定在特定条目中创建一个数组和索引位。

Because of CPU granularity you can only access things as a byte therefore bools always resolve to some kind of unsigned char type which are 1 byte in size. This way you can get from 0-255 range in values. What happens is that any non zero value is true and 0 is false. We can't implement bool as a single bit (except in C++ STL vector etc... where they are converted to bitfields) because of that granularity unless you decide to make an array and index bits in particular entries.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文