Skip to main content
gamedev.net
🔒 Locked

casting void pointers

Started by docyoung83 5 replies 650+ views
Original Post
docyoung83
docyoung83
I''m writing a DirectDraw program, and in order to access the video memory, I want to be able to use one pointer variable, no matter what the bit depth is set to. For example, if the bit depth is 8-bit, then I need a UCHAR pointer, if it is 16-bit, then I need a USHORT pointer, etc. This is what I''ve been trying to do, but to no avail. I''ve actually never seen this done before (and I may find out why after I receive a few replies ), and Google didn''t turn up any help.
#include <iostream>

using namespace std;

int main()
{
    void *video_buffer = NULL;
    
    switch (BIT_DEPTH_OF_PRIMARY_SURFACE)
    {
        case 8:
        {
            video_buffer = (UCHAR *)video_buffer;
            video_buffer = (UCHAR *)VOID_POINTER_TO_SURFACE;
        }

     ......  same for 16 and 32-bit, but with different casts
    

    return 0;
}
 
Basically, I have two void pointers, and I need to convert both of them to the same pointer type so that I can assign one of them to the other. I could make video_buffer_8, video_buffer_16, etc, but I''m trying to do it this way so that I can just use video_buffer variable every single time in my code and it will automatically always be the right one.
Zipster
Zipster
Void pointers don''t hold any type information. Thus, if you want to use a generic void pointer, every time you need to use it you need to check what bit depth it is and cast accordingly.
Zahlman
Zahlman
Yeah. When you do

foo = (bar)baz;

This casts baz, but cannot change the already-declared type of foo (or baz for that matter). All it is doing is (hopefully!) making it possible to assign baz to foo. And any pointer can be assigned to a void * anyway.

You might be able to solve it like this:


int main()
{
UCHAR * video_buffer_8 = NULL;
WORD * video_buffer_16 = NULL;
DWORD * video_buffer_32 = NULL;
switch (BIT_DEPTH_OF_PRIMARY_SURFACE)
{
case 8:
{
video_buffer_8 =(UCHAR *)VOID_POINTER_TO_SURFACE;
}

...... same for 16 and 32-bit, but assigning an
appropriate variable, and then using that variable
whereever needed
return 0;
}


But this is ugly as sin. Somewhat less uglily, maybe you could #define TYPE_OF_PRIMARY_SURFACE_POINTER in the same place you define BIT_DEPTH_OF_PRIMARY_SURFACE, and perhaps use some #ifdef logic to ensure that they always match up.
docyoung83
docyoung83
Thanks for the help. I understand the issue now. I will be working on it!
LessBread
LessBread
StonieJ, I think a union might do the trick for you

here's some pseudo code



union video_buffer_u {
VOID * video_buffer;
UCHAR * video_buffer_8;
WORD * video_buffer_16;
DWORD * video_buffer_32;
};

video_buffer_u videobuffer;
videobuffer.video_buffer = VOID_POINTER_TO_SURFACE;

switch (BIT_DEPTH_OF_PRIMARY_SURFACE)
{
case 8:
// do stuff with videobuffer.video_buffer_8
case 16:
// do stuff with videobuffer.video_buffer_16
case 24:
// do stuff with videobuffer.video_buffer_24
...



This use of a union affords a means of "casting" a value without actually casting it.


[edited by - lessbread on May 10, 2004 9:59:10 PM]
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
Chris Hare
Chris Hare
If it has to be done automatically, you''d best write a class.


blah blah blah.

Topic Locked

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

Sign in to reply to this topic.