Sign in to follow this  

Help converting my C# to C++

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

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];

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

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

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) {<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

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