Sign in to follow this  

Help converting my C# to C++

This topic is 3841 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am currently working on converting my c# over to c++ and i have run into a bit of trouble. This is how the texture appears when read in... ok and heres the c# code to do so..
dataSize = br.ReadInt32();
imageData = new int[textures[texturePos].width, textures[texturePos].height];

for (yPos = 0; yPos < textures[texturePos].height; yPos++){
    for (xPos = 0; xPos < textures[texturePos].width; xPos++){
        paletteIndex = br.ReadByte();
        imageData[yPos, xPos] = System.Drawing.Color.FromArgb(textures[texturePos].palette[paletteIndex].Flags, textures[texturePos].palette[paletteIndex].Red, textures[texturePos].palette[paletteIndex].Green, textures[texturePos].palette[paletteIndex].Blue).ToArgb();
    }
}

GraphicsStream gs = textures[texturePos].texture.LockRectangle(0, LockFlags.Discard);
gs.Write(imageData);
textures[texturePos].texture.UnlockRectangle(0);

and heres my attempt at the same thing in c++ with a less then perfect result... fooked
int pos = 0;
D3DCOLOR* image;
image = new D3DCOLOR[dataSize];

for(yPos = 0; yPos < textures[texturePos].height; yPos++){
    for(xPos = 0; xPos < textures[texturePos].width; xPos++){
        readBinary(is, paletteIndex);
        image[pos] = palette[paletteIndex];
        pos++;
    }
}

textures[texturePos].texture->LockRect(0, &rect, NULL, D3DLOCK_DISCARD);
memcpy(rect.pBits, image, dataSize);
textures[texturePos].texture->UnlockRect(0);

delete [] image;

texture format in both cases is A8R8G8B8

Share this post


Link to post
Share on other sites
What is the prototype of readBinary?

When you lock a rectangle, you must use the pitch of the surface when copying data, because each line of the image may be padded by a certain number of bytes. See

http://msdn2.microsoft.com/en-us/library/bb206357.aspx

for a discussion of pitch vs. width.

So, instead of copying the entire image with memcpy() call, you may need to copy the image line by line, taking account of the pitch of the image.

Another problem is the size you're passing to memcpy(). I believe it should be "dataSize * sizeof(D3DCOLOR)" instead of "dataSize".

Share this post


Link to post
Share on other sites
the readBinary is a template..


template <typename T>
void readBinary(istream& is, T& t) {
is.read(reinterpret_cast<char*>(&t), sizeof(t));
}



And you were right I just needed to change the memcpy so it was dataSize * sizeof(D3DCOLOR).

Thanks very much for the help tweduk!

Share this post


Link to post
Share on other sites

This topic is 3841 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this