Advertisement Jump to content
Sign in to follow this  

DirectDraw, reading in bitmaps, memcpy, writing to surfaces

This topic is 4900 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Apologies for my first post here being a question (and an uber n00b one at that) but this has been annoying me for some time and I don't know what I'm doing wrong. I'm trying to get to grips with DirectDraw while learning anything more than basic C at the same time. I'm following an old book (Windows Game Programming For Dummies - Lamothe) so my techniques may be a bit dated but I want to get this sorted! Anyway I've got everything set up and can plot pixels on screen, wow. I'm now trying to load in a bitmap and display it on screen. Earlier today I looked up the bitmap file structure and figured out C's I/O functions enough to be able to read in the file header, info header correctly (printing the size showed to be correct, as did the dimensions). The following is the bitmap loading code:
input = fopen("test.bmp", "r"); //Opens the file

fread(&bitmapfileheader, sizeof(BITMAPFILEHEADER), 1, input); //Reads file header
fread(&bitmapinfoheader, sizeof(BITMAPINFOHEADER), 1, input); //Reads info header
//printf("The bitmap is %d bytes in size.\n", bmfh.bfSize); //These lines show the correct information
//printf("The bitmap is %d pixels high.\n", bmih.biHeight); //when in a console program.
fseek(input, bitmapfileheader.bfOffBits, SEEK_SET); //Find the start of the bitmap data

data = (USHORT *)malloc(bitmapinfoheader.biSizeImage); //Allocate the memory for data

fread(&data, bitmapinfoheader.biSizeImage, 1, input); //Read actual data
So as far as I'm aware I now have the bitmap data itself in the 'data' structure (which is declared USHORT *data; as a global). The following is what actually displays (or should) the bitmap on the screen:
USHORT *video_buffer = NULL;
memset(&ddsd,0,sizeof(ddsd)); //Not entirely sure what these mean but they work, defining the surface maybe?
ddsd.dwSize = sizeof(ddsd);

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

// get video pointer
video_buffer = (USHORT *)ddsd.lpSurface;

int i;
for (i=0; i<32; i++)
	memcpy(&video_buffer[i*ddsd.lPitch], // dest address
        &data[i*32*2],                       // src address
        32*2);                               // bytes to copy
lpddsprimary->Unlock(video_buffer); //Unlock the surface
The for loop is to display the bitmap on screen, copy each row at a time (the bitmap is 32x32x16, I'm not concerned about allowing for other sizes at the moment) The problem is that every time I run this I get a variety of error messages: -Various errors in memcpy.asm reading location 0x00000000 -Errors in the program seeming to crash out at the Lock() function (the cursor is here when it returns to VC++) I can understand that I may have got the reading the bitmap info into the data structure part wrong but I would expect to at least see corruption on the screen rather than it crashing. I have got corruption on the screen (with no resemblance to my bitmap) sometimes. Ok so what I'm asking is if someone with more experience than me could have a quick look and see if there's any glaring problems. It all compiles fine, but I'm completely new to a number of these functions (namely the file handling ones) so I can't be sure I've used them right. I also realise (from looking at other DD tutorials) that I seem to be using a rather outdated method (come to think of it, who uses DirectDraw now anyway?!) but I was getting on well with it so far! I'm mostly concerned with the reading in from the bitmap and the copying and allocating memory. Thanks, Ray

Share this post

Link to post
Share on other sites
I suggest you to learn Direct3D and render 3D stuff in 2D maner, you'll use full potencial of graphic hardware.
in direct3D you will have just one line of code to load bitmap, and few one to draw it on screen.

Share this post

Link to post
Share on other sites
Thanks Streamer, I will be doing so very soon as it does feel like I'm in the stoneage here... Looking at the GDnet articles on DirectDraw they're all from 99/00 and there's hardly any DD topics here.

But I would still very much appreciate if anyone can see where I'm going wrong, this has been a very good learning experience for me in file handling which is obviously transferable to other areas of programming, as is memory handling, so I'm interested to see where I'm going wrong.

[edit] I also realise from googling this that there seem to be built-in DirectDraw functions and functions that other people have written that would do the job so much quicker and easier, but as I say it's more a learning experience[/edit]

Thanks for your reply

Share this post

Link to post
Share on other sites
hey. Ya, i learnt DD, and there isn't much out there (new at least) about it. I was using Tips of the Game Programming Gurus 2nd edition (get it. it is super usefull) and I was using that memcpy() thing, etc. I could never get it to work, but in the DX SDK, there are functions that load in .bmp files into surfaces. So I just took thos, and fixed them up for what I needed. If you would like these functions, PM me, and i'll send them to you. As it this is how they work.

MySurface = LoadBMP("filename.bmp",sizeX,sizeY,bpp)

Now its been a while since I checked that, so it might be a little different. Either way, thats roughly how it works. Now I know its not the same as what you were asking, and you don't learn *as* much as writing your own functions, but these ones seem fast, and have yet to fail me :P Be warned, they only seem to load 32 bit bmps :s Maybe you could tool around and fix that up, as for me, i wanted the 32 bmp to be loaded, so...I didn't really care.

I don't know my way around thos functions in your example, so i can't really tell. But let me know, i'm happy to help, such as it is.

BTW, i'm writting a 2d DD game engine in Borland C++ (you can atleast look at the source), so if you want, you can look at that as well to get an idea about other aspects of the gameengine. Anyways, GL m8! and if you want that stuff reply here, but also sent me a PM, as I might forget to check here :P


Share this post

Link to post
Share on other sites
some body should wright a bunch of tutorials for directdraw.

Or not, considering that the DirectDraw API has been deprecated for two DirectX versions now, and as video cards get better, support for 2D graphics is getting worse..

Share this post

Link to post
Share on other sites
That would be good if it's not too much trouble _Sigma, would be interesting to see the source for an engine that I might have some chance of understanding! I'll PM you my email if that's alright. I think the book you're using and the one I'm using are written by the same author so use the same techniques, good to hear that I'm not the only who can't get it to work though!



Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!