我试着用Derelict3在D中构建一个简单的3D游戏引擎。事情一直进展顺利,直到我开始使用关联数组来映射opengl、glGenTextures/glGenBuffers。为此,我构造了一个简单的结构,其中包含对要绑定的纹理/vbo的引用,以及从opengl返回的id。然后用纹理/vbo的散列映射,以便以后检索。
但是,一旦它完成了实际映射的设置,映射就会神奇地被移除。我还不明白为什么。下面是一个简单的例子,说明我正在努力实现什么,同样的行为也可以被观察到。
module main;
import std.datetime;
import std.stdio;
class Placeholder {
string value;
this(string value) {
this.value = value;
}
}
private class ResourceInfo {
uint id;
uint time;
Object resource;
static ResourceInfo getOrCreate(Object resource, ResourceInfo[uint] map) {
uint hash = resource.toHash();
ResourceInfo* temp = (hash in map);
ResourceInfo info;
if (!temp){
info = new ResourceInfo();
info.resource = resource;
map[hash] = info;
} else{
info = *temp;
}
// placeholders.lenght is now 1 (!)
info.time = stdTimeToUnixTime(Clock.currStdTime);
return info;
}
}
protected ResourceInfo[uint] placeholders;
void main() {
Placeholder value = new Placeholder("test");
while(true) {
ResourceInfo info = ResourceInfo.getOrCreate(value, placeholders);
// placeholders.lenght is now 0 (!)
if (!info.id) {
info.id = 1; // Here we call glGenBuffers(1, &info.id); in the engine
} else {
// This never runs
writeln("FOUND: ", info.id);
}
}
}当id不存在时,手动调用placeholders[value.toHash()] = info,临时修复它,但是每当我尝试在几秒钟后访问info实例时,就开始在_aaApply2和_d_delclass中获取它。
有人看到我失踪的明显情况了吗?
发布于 2014-05-12 21:29:37
最好的,我可以猜测是通常的伪行为的未初始化关联数组。这意味着按值传递它们,然后将键添加到它们,但如果已经初始化了该数组,则只能使用,只有。这是当前在实现中的边缘情况。然而,尝试使用ref,它应该修复一些事情:
static ResourceInfo getOrCreate(Object resource, ref ResourceInfo[uint] map)https://stackoverflow.com/questions/23615649
复制相似问题