Skip to main content
GameDev.net gamedev.net
🔒 Locked

void* casting

Started by newera Jul 13, 2006 at 11:23 PM 6 replies 1.6k views
Original Post
newera
newera
Hello all, I have a question about the void pointer, which is best described by what I am trying to do:

// allocate memory for image data
unsigned char *imageData = new unsigned char[imageSize];
// copy the SDL_Surface void* pixel array into imageData
memcpy(imageData, surf->pixels, imageSize);
// change BGR to RGB so OpenGL can use the data
for (int imageIdx = 0; imageIdx < imageSize; imageIdx += image->nBPP)
{
        // Is there a way to NOT copy the data, and simply cast
        // surf->pixels to access its data?
	colorSwap = imageData[imageIdx];
	imageData[imageIdx] = imageData[imageIdx+2];
	imageData[imageIdx + 2] = colorSwap;
}
I would like to think there is a way to cast surf->pixels from a void* to unsigned char* so I don't have to waste time doing a memcpy. So my question is, is there a way I can cut out imageData from this picture, and just use surf-pixels? thanks, Tony
deathkrush
deathkrush
In C:
unsigned char *imageData = (unsigned char*)surf->pixels;

In C++:
unsigned char *imageData = static_cast(surf->pixels);
deathkrushPS3/Xbox360 Graphics Programmer, Mass Media.Completed Projects: Stuntman Ignition (PS3), Saints Row 2 (PS3), Darksiders(PS3, 360)
newera
newera
I tried that, and my application crashes =/
LostAndConfused
LostAndConfused
So my question is, is there a way I can cut out imageData from this picture, and just use surf-pixels?

If you move the address offset. Why you would want to do it this way, I don't know but you asked. In the same way this works for the char array -- it works with a struct too. Each variable in the struct is an offset from the base address just as each char in the array.


void* vptr = "Hello\0";

MessageBox( NULL, (const char*)vptr, "", MB_OK );

// move the address ahead 1 byte
vptr = (char*)vptr + 0x01;

MessageBox( NULL, (const char*)vptr, "", MB_OK );

// move the address ahead 1 byte
vptr = (char*)vptr + 0x01;

MessageBox( NULL, (const char*)vptr, "", MB_OK );


Here is the same thing:

void* vptr = "Hello\0";
MessageBox( NULL, (const char*)vptr, "", MB_OK );
MessageBox( NULL, (char*)vptr + 0x01, "", MB_OK );
MessageBox( NULL, (char*)vptr + 0x02, "", MB_OK );
TravisWells
TravisWells
Quote:
Original post by newera
So my question is, is there a way I can cut out imageData from this picture, and just use surf-pixels?

Yes, but not in the way you think. You can tell OpenGL that your surface is in BGR. I believe you just use GL_BGR instead of GL_RGB as the format in your call to glTexImage2D.

Now, there's a slight problem with doing this on windows: GL_BGR isn't defined by the OpenGL headers, but GL_BGR_EXT is, which is the same. So you can just do this
#ifndef GL_BGR#define GL_BGR GL_BGR_EXT#endif

And still run on non-windows platforms.

EDIT: Quoted the wrong person!

[Edited by - TravisWells on July 14, 2006 12:01:56 AM]
LostAndConfused
LostAndConfused
That is not my qoute, it is the one who asked the question. :) I don't know how to use all the fancy quote tools... LOL
newera
newera
Thanks guys:)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.