Link to home
Start Free TrialLog in
Avatar of Tuomas
Tuomas

asked on

DirectX program that doesn't work!

This program was originally from a book that was based on directx6. I have the version 9 installed but I can't get the program to work! ddraw.h -file mentioned the directdraw9's guid to be stored in IID_IDirectDraw7. Is that correct? This is my first dx-program, so I'm not very good at finding bugs from it. Please help! Here's the program (directx part only):

while (1)
{
     if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
     {
          if (msg.message == WM_QUIT)
               break;
         
          TranslateMessage(&msg);
          DispatchMessage(&msg);
     }

                              ////// THE MAIN PROGRAM //////

     srand(GetTickCount());

     LPDIRECTDRAW lpdd = NULL;
     LPDIRECTDRAW lpdd7 = NULL;
     LPDIRECTDRAWSURFACE7 lpddsprimary = NULL;
     DDSURFACEDESC2 ddsd;
     LPDIRECTDRAWPALETTE     lpddpal     = NULL;
     PALETTEENTRY palette[256];


     (DirectDrawCreate(NULL, &lpdd, NULL));

     lpdd->QueryInterface(IID_IDirectDraw7, (LPVOID*)&lpdd7);
     
     lpdd->Release();

     lpdd7->SetCooperativeLevel(hwnd, DDSCL_FULLSCREEN | DDSCL_ALLOWMODEX |
                                              DDSCL_EXCLUSIVE | DDSCL_ALLOWREBOOT);

     lpdd7->SetDisplayMode(640,480,8);

     memset(&ddsd, 0, sizeof(ddsd));
     ddsd.dwSize = sizeof(ddsd);

     ddsd.dwFlags = DDSD_CAPS;

     ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;

     lpdd7->CreateSurface(&ddsd, &lpddsprimary, NULL);

     for (int color=1; color < 255; color++)
     {
          palette[color].peRed     = rand()%256;
          palette[color].peGreen     = rand()%256;
          palette[color].peBlue     = rand()%256;
          palette[color].peFlags     = PC_NOCOLLAPSE;
     }

     palette[0].peRed     = 0;
     palette[0].peGreen     = 0;
     palette[0].peBlue     = 0;
     palette[0].peFlags     = PC_NOCOLLAPSE;

     palette[255].peRed          = 255;
     palette[255].peGreen     = 255;
     palette[255].peBlue          = 255;
     palette[255].peFlags     = PC_NOCOLLAPSE;

     lpdd7->CreatePalette(DDPCAPS_8BIT | DDPCAPS_ALLOW256 | DDPCAPS_INITIALIZE,
          palette, &lpddpal, NULL);

     lpddsprimary->SetPalette(lpddpal);

     memset(&ddsd,0,sizeof(ddsd));
     ddsd.dwSize = sizeof(ddsd);

     lpddsprimary->Lock(NULL, &ddsd, DDLOCK_SURFACEMEMORYPTR |
          DDLOCK_WAIT, NULL);

     int mempitch          = (int)ddsd.lPitch;
     UCHAR *video_buffer     = (UCHAR *)ddsd.lpSurface;

     for (int index=0; index < 1000; index++)
     {
          UCHAR color = rand()%256;
          int x = rand()%640;
          int y = rand()%480;

          video_buffer[x+y*mempitch] = color;
     }

     lpddsprimary->Unlock(NULL);

}
Avatar of ambience
ambience
Flag of Pakistan image

what kind of problems are you having, have u been able to compile it ? if not then post errors you are getting.

As a side note, long gone are days of 8-bit palletized graphics, i think you should start using 16 bit modes atleast, apart from other advantages over 8-bit mode as a programming convenience you donot have to do that cumbersome palette stuff anymore.
ASKER CERTIFIED SOLUTION
Avatar of ambience
ambience
Flag of Pakistan image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Salte
Salte

Actually palettes have their uses - also in modern programming.

You can change a picture extremely fast by changing the palette and NOT changing the picture.

To make a silly but very explanatory example consider a image that changes between a cross made of - and | and a cross that is 45 degrees rotation, like an X. Let's say the X cross is red on white background and the - | cross is blue on white background.

If you instead make the picture so that it has both crosses on top of each other and you can then divide the screen into four different types of sections:

A section that is background in both pictures is white all the way, give it a color of white (it won't change). Call this color 0. let color 0 be white in the palette. It won't change.

A section that is foreground for X but background for the other switches between blue and white. This is color 1.

A section that is foreground for - | cross but background for the X switches between red and white. This is color 2.

The remaining is foreground for both crosses, this is color 3.

When showing the X cross you get one setup and for the other cross it is the other setup:

            X         -|-
color 0    white     white
color 1    blue      white
color 2    white     red
color 3    blue      red

Now by only changing the 3 palette entries 1, 2 and 3 you can quickly change the image.

It is also a very efficient way to store textures etc in a game. For example a game like EverQuest which has many different types of clothing that players can wear do something like this to each clothing. They have patterns that the players doesn't see because two separate palette entries evaluate to the same color, however, another player may wear something that is exactly the same underlying texture model, but with a different palette he will have different colors in those two slots and so the pattern appear in his garment. This way a selection of few very well designed clothing models can be used by many players and different design and patterns appear to each player because they use different palettes. However, the palette that EverQuest uses is not in the game or video graphics though, it is their own palette which they use before building the image the player see on his or her screen.

The days of using video palette is probably numbered today, however, for some types of games it is not necessary with more than say 256 colors and then a palette can be used and if used cleverly can provide very fast graphics with very little communication with the video card.

Most modern video cards have no trouble handling communication though, using AGP slots etc, the graphics today is much faster than in the old days of VGA or SVGA.

Alf
Avatar of Tuomas

ASKER

Hmm, I thought that this was the most suitable answer for the question. All the literature these days seem to be old even before they are published! Today I was reading the directx 9 sdk's documents, and was wondering where all the simple 2D stuff is? Now I know why I didn't find them! Thx!