run-time cast possible in c++ ?

Started by
13 comments, last by sprinter_trueno 20 years, 10 months ago
The anonymous poster presented a typesafe way of doing this through boost::any, I don''t see why you wouldn''t use that.
--God has paid us the intolerable compliment of loving us, in the deepest, most tragic, most inexorable sense.- C.S. Lewis
Advertisement
quote:
To better understand it you have to take into account what beernuts said: "you cannot change what type pvArray is, but you can change how you access it"

If there really isn't a way to do so then this is actually the answer to my question.

It's just that putting the 'if else' so close to the array access makes my code highly performance inefficient. To work around this I need to put my complete overhead (which is quite alot and for every case of course the same) into the 'if else' construct (ugly, inflexible).
Changing the pointers type at runtime would have been a neat way but c++ seems to have a weakness regarding this.

Thanks for all the comments on this.

Lorenz

[edited by - sprinter_trueno on May 30, 2003 1:35:16 AM]
I seriously doubt you need if/else statements. Tell us the context this code will be used in, as I''m sure we can come up with a good way of doing it.

~CGameProgrammer( );

~CGameProgrammer( );Developer Image Exchange -- New Features: Upload screenshots of your games (size is unlimited) and upload the game itself (up to 10MB). Free. No registration needed.
I''d love to agree.
Short description: (GB emu) I convert the sprite and tile data of the GB (which is 2-bit) into either 16 or 32 bit format and write the pixels into a directdraw surface.

There are 256 tiles stored in the array and each tile is 8x8 pixels in size.

After a lock of the surface the pointer to the surface is void* and you make the cast by the display mode the user chose. 16 bit or 32.

for (Tile = 0; Tile < 256; Tile++){   ...   ...   for (Line = 0; Line < 8; Line++)   {      ...      ...      for (Row = 0; Row < 8; Row++)      {         Color = ....... ;         // The next line is my problem         pTileSurface[...] = PixelLookUpTable[Color];         ...         ...      } // END OF ROW      ...      ...			   } // END OF LINE   ...   ...		} // END OF TILE TABLE


pTileSurface varies between two types. Using ''if'' performance efficiently I have to place the whole loop thing once for 16 bit and once for 32 bit since I need two different pointers.

Is there another way ?
I assume you mean something like this?
((WORD*)pTileSurface)[(Y * TilePitch) + (X << BytesPerPixel)] = Color;//vs((DWORD*)pTileSurface)[(Y * TilePitch) + (X << BytesPerPixel)] = Color;

There''s no good way to reduce those literal equations. The compiler assigns 16-bit values through the CPU''s 16-bit move instruction, and 32-bit data through the 32-bit instruction. They''re different instructions that are being called so you can''t alter that at run-time (unless you use self-modifying code, but I''ve never tried that).

However, aren''t you just creating these tiles at startup, not at runtime? If so then you are being absurd in trying to optimize this through the elimination of a single if statement. Either write two versions of the loop or put the if statement in the inner-most one, it does not matter for simple initialization code, unless you are running the emulator on ENIAC.

~CGameProgrammer( );

~CGameProgrammer( );Developer Image Exchange -- New Features: Upload screenshots of your games (size is unlimited) and upload the game itself (up to 10MB). Free. No registration needed.

This topic is closed to new replies.

Advertisement