   How to solve this little code problem?    
  I have 2 functions wich do mostly the same. However, the first function takes 8bpp colors and the other use RGB values. Since there is a little difference in handling the two by plotting pixels I don't know how to handle it. I currently have the 2 functions wich only differ in 3 or 4 lines. What is the best solution, something like this:  HRESULT DirectDraw::DrawLine(int X, int Y, int Color, UCHAR *Buffer, int lPitch){   DrawLine(X, Y, -1, -1, -1, Color, UCHAR *Buffer, int lPitch);   return (0);}HRESULT DirectDraw::DrawLine(int X, int Y, UCHAR Alpha, UCHAR Red, UCHAR Blue, UCHAR Green, UCHAR *Buffer, int lPitch){   // Line algo here   if (Bpp == 8)   { //Plot Pixel   }   if (Bpp == 16)   { // Plot Pixel   }   return (0);}    Is this a good solution?[EDIT]Fixed my code, I forgot the change function parameters[/EDIT]Sand Hawk    -----------------Earth is 98% full. Please delete anybody you can.My Site[edited by - sand_hawk on September 16, 2002 6:01:36 AM]
  First of all, graphics functions should always be as fast as possible for real time games. (I''m guessing that''s what you are making)If you are building your own graphics library then make each function as optimized as possible. (sometimes repeated code)The code performance must be considered first. Even if the code becomes a little larger at times.So, I recommend trying it both ways and see which meets your needs.;  I that case I think I will use my approach. It''s still fast(Even when I have 1 function call extra) and line drawing is hopefuly not used alot during games. And second, it keeps me from pasting some extra codeSand Hawk  ;quote:Original post by CodeJunkie  The code performance must be considered first. Even if the code becomes a little larger at times.  It doesn''t matter what you''re doing, premature optimization is the root of all evil.I''m not entirely sure what each of those functions are supposed to do, is the int Color parameter meant to be a packed colour?  If so, I''d define a structure like this:union{    struct { UCHAR a, r, g, b };    DWORD colour;};With a couple of appropriate constructors for that union, you would only need one function, instead of two.    If I had my way, I''d have all of you shot!codeka.com - Just click it.    ;  This function draws a line. Simple. int Color is used for the 8 bit modes, the other for RGB modes.Sand Hawk  ;  Your solution of having two functions and the more specific one is calling the generic one, is good. Optimize only if you feel that it''s too slow.I don''t know why you have the return values there but don''t omit them:   HRESULT DirectDraw::DrawLine(int X, int Y, int Color, UCHAR *Buffer, int lPitch){   DrawLine(X, Y, -1, -1, -1, Color, UCHAR *Buffer, int lPitch);   return (0);}  should be   HRESULT DirectDraw::DrawLine(int X, int Y, int Color, UCHAR *Buffer, int lPitch){   return DrawLine(X, Y, -1, -1, -1, Color, UCHAR *Buffer, int lPitch);}  