Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Load Surfaces To Video/System Memory Based on GFX Card
#1
Alright, so every now and then I'll go through posts and see a post that says something along the lines of : "The best option would be to get the specs of the graphic card, and load surfaces based on that, since video memory is faster." So one day, I finally decided to look into and, and found out that this is actually really easy to accomplish xD. What this does is, depending on a few values, decides on a certain amount of video memory the client can use for surfaces.

This won't be full on Copy & Paste, you're going to have to work a little!

This is all client side, no worries Smile

Add this variable to modGlobals as a Public Variable. This is the variable that will hold the video memory left for the program to use.
Code:
' Video Memory
Public VRAM As Integer

And add these constants. These constants are set by you, and are the constants which will be used as the maximum amount of memory for the program to use and the minimum required for the surfaces to be set for video memory :

Code:
' Video Memory Modifier
Public Const MAX_USAGE As Byte = 64
Public Const MIN_USAGE As Byte = 16

I personally find these good values for the following reason : a ) 64MB is plenty of memory for all your surfaces. b ) If the users card has 64mb or less, it's probably (by today's standards) a low end computer, so only use 16MB of video memory for this program. c ) If the user has a 16mb video card, the program should definitely not be using video memory xD

Now in sub InitDirectDraw, add this at the top :

Code:
Dim DDSCaps As DDSCAPS2             'DirectDraw capabilities structure

DDSCAPS2 is a special structure provided with the DX7 .dll which holds the graphic card's capabilities. This is the key to finding the video memorySmile

Now, in between where you Set the Cooperative Level of DD (challenging :wink: ) and where you initialize the DDSD_Primary flags ( hint : With DDSD_Primary), add the following code :

Code:
' Set Video Memory Allowed To Be Used
    VRAM = Val(DD.GetFreeMem(DDSCaps)) / 1024 / 1024
    
    If VRAM < MAX_USAGE And VRAM > MIN_USAGE Then VRAM = MIN_USAGE     ' IF IN BETWEEN MAX AND MIN, MID LEVEL GFX CARD, SET TO MIN USAGE
    If VRAM > MAX_USAGE Then VRAM = MAX_USAGE                        ' IF HIGH LEVEL GFX CARD, SET TO MAX USAGE
    If VRAM < MIN_USAGE Then VRAM = 0                                ' IF LOW LEVEL GFX CARD, SET TO NO USAGE
[/code]

Now first, this gets the free video memory and converts it to megabytes, and then based on the value it got, checks with the MAX_USAGE and the MIN_USAGE constants to see how much video memory it should use.

Now that the video memory variable is set, we have to use it someplace don't we! This is how we'll load a surface using it. First, in your sub InitDDSurf, add the variable :

Code:
Dim FileSize As Integer

and add the following line after the check to see if the file exists :

Code:
FileSize = FileLen(tempFileName) / 1024 / 1024

FileLen is a built-in vb6 function which gives the length of a file in bytes. So we convert it to megabytes for simplification Smile. Now we have to check how much the file size is compared to the Video Memory we have left to allocate. So , where you are setting the surface description, replace it with the lines :

Code:
If VRAM >= FileSize Then
        
        SurfDesc.lFlags = DDSD_CAPS
        SurfDesc.DDSCaps.lCaps = DDSCAPS_OFFSCREENPLAIN Or DDSCAPS_VIDEOMEMORY
        
        VRAM = VRAM - FileSize
    Else
        
        SurfDesc.lFlags = DDSD_CAPS
        SurfDesc.DDSCaps.lCaps = DDSCAPS_OFFSCREENPLAIN Or DDSCAPS_SYSTEMMEMORY
    
    End If

Now what this does is checks to make sure there is enough video memory left to allocate to the file, and then if there is, sets it to use video memory (DDSCAPS_VIDEOMEMORY) instead of system memory (DDSCAPS_SYSTEMMEMORY) and if so, it takes away the amount of megabytes used by the file from the variable VRAM for the next surface.

And there you go Big Grin Pretty simple don't you think? Smile
Reply
#2
Little typo:

Code:
FileSize = FileLen(FilePath) / 1024 / 1204
Reply
#3
GIAKEN Wrote:Little typo:

Code:
FileSize = FileLen(FilePath) / 1024 / 1204

Ah, thanks for that Smile Fixed in the main post Smile
Reply
#4
Edited tutorial on September 6th 2008 :
-Modified it to work with MSE 3.50 (current version)
-Fixed a typo which would cause a RTE
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)