direct x 7 question...
maxhamnerOkay, I've been looking on the net for this and am supprised I didn't find lots fo questions on it if not answers... I create a surface from a file (or whatever)... how do I release the surface to free the memory for other stuff? I have some surfaces I want to create for a loading screen, but I want to free them up for game use when done. I don't see an obvious dx7 direct draw to releasesurface unloadsurface destroysurface, etc.. (and of course release DC just releases a DC made from a surface, not the surface itself) Grrrr anyone have a quick answer?
dxgameWhy release it, when you can just load another image/data into it when needed?
sdwEventually you need to release it, otherwise it's a leak. I believe you release DirectDraw surfaces by setting the surface equal to nothing. [code]Set mySurface = Nothing[/code]
maxhamnerYou can't always just load another image into it unless you happen to have a lot of same size images... sdw is right... I figured it out almost within seconds, maybe a couple minutes of that post.. :D I would be more comfortable with a release function, but I'll do some video memory checks before and after a release to make sure it is correct. Thanks for the replies!
dxgameWell, you ALWAYS unload your surfaces at program end, but one of the nice things about DX7 is the ability to init a surface, say to the size of your game screen and then repeatedly use the same surface for loading level graphics, or sprite graphics, etc. Your game should know exactly how many surfaces you are going to need. I can't see a need to unload a surface at all in a DX7 Direct Draw game, until program's end.
Eric ColemanYou may have a need to create surfaces dynamically. I do that for text in directx7, it is actually very fast. For gladiator I used a bitmap with all the letters and symbols that I would need, and then whenever I need to display a string of text I create a surface large enough to hold it and then bitblt each letter to the surface. A string with 150 letters becomes a single bitblt instead of 150 bitblts, and trust me, 1 bitblt is a LOT faster than 150. Whenever the text changes (which isn't too often) the surface is destroyed and then recreated to hold the new text.
maxhamnerI do something similar to get messages on the screen, but I actually have a surface that I leave intact for the messages. It's currently 800 pixels wide and 100 pixels tall. When I need to change the text I do a bltcolorfill, making the whole surface my transparent color, then draw my text into the surface, then I can bitblt the message onto the screen with a single blit as often (and fast) as I want. I've started using font files instead of font bitmaps because it is easier to use, and I can use different sizes and colors as freely as I want with no additional overhead... i.e. For a menu I draw the menu text in black with a x+4,y+4 offset, then in my main color with a 0 offset to get a 'drop shadow' effect. If I decided to use a different font I just change like 2 lines of code and I'm done. I'll post the little font mod I use on here just for kicks, in a new thread (just the font load/unload/calcwidth stuff, not the DX7 part) It's all stuff that's out there, I just put it together in one place...
dxgame"You may have a need to create surfaces dynamically. I do that for text in directx7, it is actually very fast. For gladiator I used a bitmap with all the letters and symbols that I would need, and then whenever I need to display a string of text I create a surface large enough to hold it and then bitblt each letter to the surface. A string with 150 letters becomes a single bitblt instead of 150 bitblts, and trust me, 1 bitblt is a LOT faster than 150. Whenever the text changes (which isn't too often) the surface is destroyed and then recreated to hold the new text." Ofcourse 1 big blit is faster than multiple smaller blits. Especially if you're color keying. But I have done tests where a map draw routine was only every so slightly slower than a full screen draw. Especially when the character graphics were multiple of 16. Still, if you bypassed the "destroy surface" part by simply adding 1 dedicated surface it would technically be "faster" and DirectX wouldn't have to do anything internally. I just don't like destroying anything in a game loop that's running 60 fps or more. Don't like all those hiccups as DirectX shifts memory around like in all those managed 3D games. :)
Eric ColemanCreating a surface shouldn't be THAT slow to cause a jump in the frame rate. I've only tested my implimentation on fairly crappy video cards and it's really fast for me. What kind of video card have you experienced this on?
masterboodaAdding and destroying surfaces dynamically shouldn't be that much of a speed issue... at least I haven't seen one, and when you just load a new image onto an existing surface from what I remember in dx7, you are still really doing just that because dx7 is destroying it and recreating it... but as to which method to use it is up to the programmer really... because either way works well... As for me I use one surface for the sprites and another for the map data, this way I can build an entire huge map out of just one small surface... and all my sprites coming from one surface really helps keep a tighter game loop in my opinion, but of course it all depends on the style of the program and programmer... Dabooda out...
maxhamnerWell, it is important to clarify the difference between destroying surfaces used in non time related parts of a program (like my loading screen which was the original question/use or between levels) and destorying/creating while attempting to maintain animation rates... I agree with the master about the use of surfaces. I have one for ground tiles, one for object tiles, one for player tiles. It makes the code simple, plus there is like a 2K overhead for each surface in DX7 I believe so it's more effecient as well. I can have multiple types of players and just have a bitmap for each type and as long as they use the same locations in the bitmap for the same animations (i.e. walking) then the same code can display any character type without any extra slowdown for branching to handle the character type...
dxgameLooks like we're all basically doing similar methods. Still, I've coded dozens of DX7 Direct Draw apps over the years, and not one destroyed a surface until the user exited the program. DX7 is a great entry level way to start blitting sprites around the screen, very quick and easy to work with. It's almost scary to think DX8 and up make it even easier. ;)
maxhamnerLOL - If you ever load a bitmap into a surface that is a different size, I think you have destroyed a surface - it was just implicit as DX7 destroyed the original one and created a new one... like assigning a new string value to a variable you already used... but I get the idea!
dxgame"If you ever load a bitmap into a surface that is a different size..." Never did that either! If I create an app running at 640x480, then all of my surfaces are created at the same resolution. All of my image data is at the same resolution as well. LOL! :)