DirectX program that doesn't work!

Posted on 2003-02-20
Medium Priority
Last Modified: 2008-03-10
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)

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


     LPDIRECTDRAWSURFACE7 lpddsprimary = NULL;
     LPDIRECTDRAWPALETTE     lpddpal     = NULL;
     PALETTEENTRY palette[256];

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

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

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


     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;

          palette, &lpddpal, NULL);


     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;


Question by:Tuomas
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
LVL 22

Expert Comment

ID: 7986349
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.
LVL 22

Accepted Solution

ambience earned 300 total points
ID: 7986368
IID_IDirectDraw7 is the latest available DirectDraw that you can use, this is because since DirectX8 direct draw is deemed obsolete and is only there for backward-compatibility and other reasons. The new paradigm forces you to use 3D even for 2D graphics.
LVL 12

Expert Comment

ID: 7986581
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.


Author Comment

ID: 7989347
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!

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question