Domanda

I'm trying to build a simple 3D-game engine in D using Derelict3. Things have been going smoothly until I started using associative arrays for mapping opengl glGenTextures/glGenBuffers. For this I constructed a simple struct which contains a reference to the texture/vbo which was to be bound and the id returned from opengl. This was then to be mapped with the hash of the texture/vbo for later retrieval.

However, as soon as it finishes setting up the actual mapping, the mapping is removed magically. I have yet to understand why. Below is a simple example on what I am trying to achieve, and the same behaviour can be observed.

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);
        }
    }
}

Calling placeholders[value.toHash()] = info manually when no id exists temporarily fixes it but then I start getting object.Error: Access Violation in _aaApply2 and _d_delclass whenever I try to access an instance of info after a few seconds.

Anyone seeing anything obvious that I'm missing?

È stato utile?

Soluzione

Best I could guess is the usual pseudo-behavior of uninitialized associative arrays. Meaning passing them by value and then adding keys to them works but only if that array was already initialized. It's a current edge-case in the implementation. Try using ref however, it should fix things:

static ResourceInfo getOrCreate(Object resource, ref ResourceInfo[uint] map)
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top