Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Dynamic Multidimensional Arrays (UGH!@#)

SephirothSephiroth Fayetteville, NC, USAMember Posts: 1,035
OK, I finished ILE without using any dynamic memory allocation because the variables take up little resources, but I am now back to my 3D game and I ***MUST*** get a simple multidimensional array working. I use an array of chars to store the texture filenames used in each map. Thing is, a map can have between one and 30k textures on it, so why make a static 30k text array?
[code]
char **TexList;
...
TexList = (char**)new char[NumTextures][32];
...
delete [] TexList;
[/code]
Now whenever I try reading something into the array after the new statement, it crashes! I will be using filenames 32chars or less so the second var (string length) will ALWAYS be 32. What am I doing wrong here? It's driving me NUTS and my help files only show static allocation.

-[italic][b][red]S[/red][purple]e[/purple][blue]p[/blue][green]h[/green][red]i[/red][purple]r[/purple][blue]o[/blue][green]t[/green][red]h[/red][/b][/italic]

Comments

  • pingpongpingpong Member Posts: 937
    Seph, did you read my reply for your earlier multidimensional problem?

    If course it crashes, becasue TextList[0], TextList[1], ... are still not allocated...

    Anyways, here's the simplest way to do what you want:
    [code]
    char **TexList;
    int i;

    // allocate
    TexList = new char*[NumTextures];
    for(i = 0; i < NumTextures; i++)
    TexList[i] = new char[32];

    // use
    for(i = 0; i < NumTextures; i++)
    sprintf(TexList[i], "List #%d", i);

    // show that they work
    for(i = 0; i < NumTextures; i++)
    printf("%s
    ", TexList[i]); // or OutputDebugString or MessageBox

    // free
    for(i = 0; i < NumTextures; i++)
    delete TexList[i];
    delete [] TexList;
    [/code]

    : OK, I finished ILE without using any dynamic memory allocation because the variables take up little resources, but I am now back to my 3D game and I ***MUST*** get a simple multidimensional array working. I use an array of chars to store the texture filenames used in each map. Thing is, a map can have between one and 30k textures on it, so why make a static 30k text array?
    : [code]
    : char **TexList;
    : ...
    : TexList = (char**)new char[NumTextures][32];
    : ...
    : delete [] TexList;
    : [/code]
    : Now whenever I try reading something into the array after the new statement, it crashes! I will be using filenames 32chars or less so the second var (string length) will ALWAYS be 32. What am I doing wrong here? It's driving me NUTS and my help files only show static allocation.
    :
    : -[italic][b][red]S[/red][purple]e[/purple][blue]p[/blue][green]h[/green][red]i[/red][purple]r[/purple][blue]o[/blue][green]t[/green][red]h[/red][/b][/italic]
    :

  • SephirothSephiroth Fayetteville, NC, USAMember Posts: 1,035
    [b][red]This message was edited by Sephiroth at 2002-9-19 18:28:58[/red][/b][hr]
    Ah I missed it! That method is EXACTLY what I was looking for. It works like a charm! Now I have a question about "new". When a player exits a map, a function is called that uses "delete" to free the ram to that pointer. Now when the new map loads, can I use "new" on that same pointer again and allocate new memory to it, or should I re-learn realloc and use it for all my allocation needs? Basically, my question is, if I use delete or free on a pointer, may I use that same pointer in another new statement?
    After toying with new and crashing repeatedly, I thought I'd try realloc, with more crashing. Here's the source, look at "image".
    ERGH! Just realized I forgot a bitmap array is X/Y/3. Changed the code and it still crashes. Here's the update.
    OK, I found out the problem isn't reading the file. The problem seems to be in the memory allocation code. I've tried this repeatedly using just "malloc" and after the GL stuff "free", using "realloc" by itself, and by using what I have posted, "new". I have actually had to reboot my box twice now because it messes up Windoze.
    [code]
    BOOL LoadTextures()
    {
    char texbuf[128];
    short int width, height;
    GLubyte ***image;
    FILE *texfile;

    glGenTextures(area.numTextures, &texture[0]);

    for(short int loop = 0; loop < area.numTextures; loop++)
    {
    sprintf(texbuf, "Textures\%s", area.TexList[loop]);
    if((texfile = fopen(texbuf, "rb")) == NULL)
    return FALSE;

    fread(&width, sizeof(short int), 1, texfile);
    fread(&height, sizeof(short int), 1, texfile);

    image = new GLubyte**[height];
    for(short int ILoop = 0; ILoop < height; ILoop++)
    image[ILoop] = new GLubyte*[width];

    for(short int IX = 0; IX < width; IX++)
    for(short int IY = 0; IY < height; IY++)
    image[IY][IX] = new GLubyte[3];

    fread(&image, sizeof(image), 1, texfile);
    fclose(texfile);

    glBindTexture(GL_TEXTURE_2D, texture[loop]);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, width, height, GL_RGB, GL_UNSIGNED_BYTE, image);

    for(short int IX = 0; IX < width; IX++)
    for(short int IY = 0; IY < height; IY++)
    delete [] image[IY][IX];

    for(short int ILoop = 0; ILoop < height; ILoop++)
    delete [] image[ILoop];

    delete [] image;
    }

    return TRUE;
    }
    [/code]
    Thanks pingpong. Oh and I have tried that loop with AND without the delete references. Both fail.


  • SephirothSephiroth Fayetteville, NC, USAMember Posts: 1,035
    Guys I got it working. Not the way I was going, but I found a way that works just fine. The link below is to a screenshot of the game. I took the screenshot windowed because PrintScreen on a fullscreen OpenGL window pastes garbage. In fullscreen it is also dynamically lit. Oh and the door does work. The map isn't huge, but it's fairly decent. Thanks again for the help guys.

    http://dhta.oesm.org/Clipboard.jpg

    -[italic][b][red]S[/red][purple]e[/purple][blue]p[/blue][green]h[/green][red]i[/red][purple]r[/purple][blue]o[/blue][green]t[/green][red]h[/red][/b][/italic]

  • Chris BrownChris Brown USAMember Posts: 4,496 ✭✭

    _______ | http://forcoder.org | free video tutorials and ebooks about < C, Assembly, PL/SQL, Delphi, MATLAB, R, Python, Objective-C, Swift, PHP, C#, Visual Basic .NET, Java, Scratch, JavaScript, C++, Go, Ruby, Perl, Visual Basic ABAP, LabVIEW, D, Transact-SQL, Dart, Lua, Scheme, Rust, F#, Kotlin, Prolog, VBScript, Logo, Hack, Erlang, SAS, Julia, Ada, COBOL, FoxPro, Lisp, Fortran, Scala, Apex, Crystal, Bash, Clojure, ML, Awk, Alice /> ______________

Sign In or Register to comment.