Archived

This topic is now archived and is closed to further replies.

Nested "for" ....Nested "if" Nested ............

This topic is 5587 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

Execuse this question I have a program I am trying to understand, it must have 10 "for" loops in one function,each nesting about three levels of "for" loops. each of the outer and inner "for" loops containing "if" multi-nested statments as well. It is frustrating. I am trying to focus on the code, but I have to shift my focus to the structure to understand the code. And in order to understand the structure I must understand the code. I know eventually I will understand it, but can anyone reduce my suffering by giving a technique or approach to understand such structure.

Share this post


Link to post
Share on other sites
What was. btw, the question? You know, the sentence ending with a "?".

All I saw was you whining about a function you have to understand that is above your head - ok, an ugly one.

But no question.


Regards

Thomas Tomiczek
THONA Consulting Ltd.
(Microsoft MVP C#/.NET)

Share this post


Link to post
Share on other sites
The last sentence was a question. (Despite the lack of a question mark). Anyway, wild_pointer is right. Nobody can help you understand code that we can''t see...

Share this post


Link to post
Share on other sites
Thomas you do not know my head. If you have not nice to say do not say it.

It is not above my head. It is I just started doing C++ and directX. Only few hours a week. Never took any computer or programing course in my life. And I am 45 years old facinated by directX. Nothing will stop me from learning.

I know I will understand it, I just want to reduce the time by taking advice from the more experianced. This the idea of those clubs. People come from different levels and assist each other.

Back to technical : The question is when you get a piece of code which difficult to follow, what is the experianced people techniques to break it down and be able to comperhend.

A potential answer is to add break points, start the debugger with step in/step into/run to cursor.

Another potential answer is to have a print out, try to color the different "for" and "if" statments loop and blocks to make it more readable.

By the way since my first posting, I made good progress and I do not think I need help.But if I need help I will not hesitate to ask, there are plenty of people out there who helped me a lot already. Do not look to my profile because I e-mail, and they reply.


  
//-----------------------------------------------------------------------------

// Name: BuildDeviceList()

// Desc:

//-----------------------------------------------------------------------------

HRESULT CD3DApplication::BuildDeviceList()
{
const DWORD dwNumDeviceTypes = 2;
const TCHAR* strDeviceDescs[] = { _T("HAL"), _T("REF") };
const D3DDEVTYPE DeviceTypes[] = { D3DDEVTYPE_HAL, D3DDEVTYPE_REF };

BOOL bHALExists = FALSE;
BOOL bHALIsWindowedCompatible = FALSE;
BOOL bHALIsDesktopCompatible = FALSE;
BOOL bHALIsSampleCompatible = FALSE;

// Loop through all the adapters on the system (usually, there''s just one

// unless more than one graphics card is present).

for( UINT iAdapter = 0; iAdapter < m_pD3D->GetAdapterCount(); iAdapter++ )
{
// Fill in adapter info

D3DAdapterInfo* pAdapter = &m_Adapters[m_dwNumAdapters];
m_pD3D->GetAdapterIdentifier( iAdapter, D3DENUM_NO_WHQL_LEVEL, &pAdapter->d3dAdapterIdentifier );
m_pD3D->GetAdapterDisplayMode( iAdapter, &pAdapter->d3ddmDesktop );
pAdapter->dwNumDevices = 0;
pAdapter->dwCurrentDevice = 0;

// Enumerate all display modes on this adapter

D3DDISPLAYMODE modes[100];
D3DFORMAT formats[20];
DWORD dwNumFormats = 0;
DWORD dwNumModes = 0;
DWORD dwNumAdapterModes = m_pD3D->GetAdapterModeCount( iAdapter );

// Add the adapter''s current desktop format to the list of formats

formats[dwNumFormats++] = pAdapter->d3ddmDesktop.Format;

for( UINT iMode = 0; iMode < dwNumAdapterModes; iMode++ )
{
// Get the display mode attributes

D3DDISPLAYMODE DisplayMode;
m_pD3D->EnumAdapterModes( iAdapter, iMode, &DisplayMode );

// Filter out low-resolution modes

if( DisplayMode.Width < 640 || DisplayMode.Height < 400 )
continue;

// Check if the mode already exists (to filter out refresh rates)

for( DWORD m=0L; m<dwNumModes; m++ )
{
if( ( modes[m].Width == DisplayMode.Width ) &&
( modes[m].Height == DisplayMode.Height ) &&
( modes[m].Format == DisplayMode.Format ) )
break;
}

// If we found a new mode, add it to the list of modes

if( m == dwNumModes )
{
modes[dwNumModes].Width = DisplayMode.Width;
modes[dwNumModes].Height = DisplayMode.Height;
modes[dwNumModes].Format = DisplayMode.Format;
modes[dwNumModes].RefreshRate = 0;
dwNumModes++;

// Check if the mode''s format already exists

for( DWORD f=0; f<dwNumFormats; f++ )
{
if( DisplayMode.Format == formats[f] )
break;
}

// If the format is new, add it to the list

if( f== dwNumFormats )
formats[dwNumFormats++] = DisplayMode.Format;
}
}

// Sort the list of display modes (by format, then width, then height)

qsort( modes, dwNumModes, sizeof(D3DDISPLAYMODE), SortModesCallback );

// Add devices to adapter

for( UINT iDevice = 0; iDevice < dwNumDeviceTypes; iDevice++ )
{
// Fill in device info

D3DDeviceInfo* pDevice;
pDevice = &pAdapter->devices[pAdapter->dwNumDevices];
pDevice->DeviceType = DeviceTypes[iDevice];
m_pD3D->GetDeviceCaps( iAdapter, DeviceTypes[iDevice], &pDevice->d3dCaps );
pDevice->strDesc = strDeviceDescs[iDevice];
pDevice->dwNumModes = 0;
pDevice->dwCurrentMode = 0;
pDevice->bCanDoWindowed = FALSE;
pDevice->bWindowed = FALSE;
pDevice->MultiSampleTypeFullscreen = D3DMULTISAMPLE_NONE;
pDevice->MultiSampleTypeWindowed = D3DMULTISAMPLE_NONE;

// Examine each format supported by the adapter to see if it will

// work with this device and meets the needs of the application.

BOOL bFormatConfirmed[20];
DWORD dwBehavior[20];
D3DFORMAT fmtDepthStencil[20];

for( DWORD f=0; f<dwNumFormats; f++ )
{
bFormatConfirmed[f] = FALSE;
fmtDepthStencil[f] = D3DFMT_UNKNOWN;

// Skip formats that cannot be used as render targets on this device

if( FAILED( m_pD3D->CheckDeviceType( iAdapter, pDevice->DeviceType,
formats[f], formats[f], FALSE ) ) )
continue;

if( pDevice->DeviceType == D3DDEVTYPE_HAL )
{
// This system has a HAL device

bHALExists = TRUE;

if( pDevice->d3dCaps.Caps2 & D3DCAPS2_CANRENDERWINDOWED )
{
// HAL can run in a window for some mode

bHALIsWindowedCompatible = TRUE;

if( f == 0 )
{
// HAL can run in a window for the current desktop mode

bHALIsDesktopCompatible = TRUE;
}
}
}

// Confirm the device/format for HW vertex processing

if( pDevice->d3dCaps.DevCaps&D3DDEVCAPS_HWTRANSFORMANDLIGHT )
{
if( pDevice->d3dCaps.DevCaps&D3DDEVCAPS_PUREDEVICE )
{
dwBehavior[f] = D3DCREATE_HARDWARE_VERTEXPROCESSING |
D3DCREATE_PUREDEVICE;

if( SUCCEEDED( ConfirmDevice( &pDevice->d3dCaps, dwBehavior[f],
formats[f] ) ) )
bFormatConfirmed[f] = TRUE;
}

if ( FALSE == bFormatConfirmed[f] )
{
dwBehavior[f] = D3DCREATE_HARDWARE_VERTEXPROCESSING;

if( SUCCEEDED( ConfirmDevice( &pDevice->d3dCaps, dwBehavior[f],
formats[f] ) ) )
bFormatConfirmed[f] = TRUE;
}

if ( FALSE == bFormatConfirmed[f] )
{
dwBehavior[f] = D3DCREATE_MIXED_VERTEXPROCESSING;

if( SUCCEEDED( ConfirmDevice( &pDevice->d3dCaps, dwBehavior[f],
formats[f] ) ) )
bFormatConfirmed[f] = TRUE;
}
}

// Confirm the device/format for SW vertex processing

if( FALSE == bFormatConfirmed[f] )
{
dwBehavior[f] = D3DCREATE_SOFTWARE_VERTEXPROCESSING;

if( SUCCEEDED( ConfirmDevice( &pDevice->d3dCaps, dwBehavior[f],
formats[f] ) ) )
bFormatConfirmed[f] = TRUE;
}

// Find a suitable depth/stencil buffer format for this device/format

if( bFormatConfirmed[f] && m_bUseDepthBuffer )
{
if( !FindDepthStencilFormat( iAdapter, pDevice->DeviceType,
formats[f], &fmtDepthStencil[f] ) )
{
bFormatConfirmed[f] = FALSE;
}
}
}

// Add all enumerated display modes with confirmed formats to the

// device''s list of valid modes

for( DWORD m=0L; m<dwNumModes; m++ )
{
for( DWORD f=0; f<dwNumFormats; f++ )
{
if( modes[m].Format == formats[f] )
{
if( bFormatConfirmed[f] == TRUE )
{
// Add this mode to the device''s list of valid modes

pDevice->modes[pDevice->dwNumModes].Width = modes[m].Width;
pDevice->modes[pDevice->dwNumModes].Height = modes[m].Height;
pDevice->modes[pDevice->dwNumModes].Format = modes[m].Format;
pDevice->modes[pDevice->dwNumModes].dwBehavior = dwBehavior[f];
pDevice->modes[pDevice->dwNumModes].DepthStencilFormat = fmtDepthStencil[f];
pDevice->dwNumModes++;

if( pDevice->DeviceType == D3DDEVTYPE_HAL )
bHALIsSampleCompatible = TRUE;
}
}
}
}

// Select any 640x480 mode for default (but prefer a 16-bit mode)

for( m=0; m<pDevice->dwNumModes; m++ )
{
if( pDevice->modes[m].Width==640 && pDevice->modes[m].Height==480 )
{
pDevice->dwCurrentMode = m;
if( pDevice->modes[m].Format == D3DFMT_R5G6B5 ||
pDevice->modes[m].Format == D3DFMT_X1R5G5B5 ||
pDevice->modes[m].Format == D3DFMT_A1R5G5B5 )
{
break;
}
}
}

// Check if the device is compatible with the desktop display mode

// (which was added initially as formats[0])

if( bFormatConfirmed[0] && (pDevice->d3dCaps.Caps2 & D3DCAPS2_CANRENDERWINDOWED) )
{
pDevice->bCanDoWindowed = TRUE;
pDevice->bWindowed = TRUE;
}

// If valid modes were found, keep this device

if( pDevice->dwNumModes > 0 )
pAdapter->dwNumDevices++;
}

// If valid devices were found, keep this adapter

if( pAdapter->dwNumDevices > 0 )
m_dwNumAdapters++;
}

// Return an error if no compatible devices were found

if( 0L == m_dwNumAdapters )
return D3DAPPERR_NOCOMPATIBLEDEVICES;

// Pick a default device that can render into a window

// (This code assumes that the HAL device comes before the REF

// device in the device array).

for( DWORD a=0; a<m_dwNumAdapters; a++ )
{
for( DWORD d=0; d < m_Adapters[a].dwNumDevices; d++ )
{
if( m_Adapters[a].devices[d].bWindowed )
{
m_Adapters[a].dwCurrentDevice = d;
m_dwAdapter = a;
m_bWindowed = TRUE;

// Display a warning message

if( m_Adapters[a].devices[d].DeviceType == D3DDEVTYPE_REF )
{
if( !bHALExists )
DisplayErrorMsg( D3DAPPERR_NOHARDWAREDEVICE, MSGWARN_SWITCHEDTOREF );
else if( !bHALIsSampleCompatible )
DisplayErrorMsg( D3DAPPERR_HALNOTCOMPATIBLE, MSGWARN_SWITCHEDTOREF );
else if( !bHALIsWindowedCompatible )
DisplayErrorMsg( D3DAPPERR_NOWINDOWEDHAL, MSGWARN_SWITCHEDTOREF );
else if( !bHALIsDesktopCompatible )
DisplayErrorMsg( D3DAPPERR_NODESKTOPHAL, MSGWARN_SWITCHEDTOREF );
else // HAL is desktop compatible, but not sample compatible

DisplayErrorMsg( D3DAPPERR_NOHALTHISMODE, MSGWARN_SWITCHEDTOREF );
}

return S_OK;
}
}
}

return D3DAPPERR_NOWINDOWABLEDEVICES;
}

Share this post


Link to post
Share on other sites
Ok, then here is my experience of how I did it around four years ago.

Back to then - I was hired to lead a project basically resulting in finishing a program that consisted (and no, this is NO JOKE) if a loop with a switch statement with a lot of loops running in total around 80 pages (again, dont laugh - some people write this type of code).

Get structure into it. Print it out - it helps to have an old needle printer that can print on these endless pages.

Then get structure into it - try to identify subfunctions, move code into functions. Break it up - logically, not in the debugger. When debugging you look at too narrow a picture, and you first need an overview.

In your case, though, is there a particular reason to try to understand this monster? Maybe you would be way better of looking for other samples - especially when new to programming and directx. Sounds a lot like this is oer your head - and I do take the freedom to say so, because I am not 45, and I have more than 12 years of experience in professional software development. You might be better of turning around and learning from other source - unless there is a particular reason for this code.


Regards

Thomas Tomiczek
THONA Consulting Ltd.
(Microsoft MVP C#/.NET)

Share this post


Link to post
Share on other sites
I may have over reacted, but I know my head and you know yours.

Your question is why I need to know this. It is a good question if I asked it before I started. I could have done more usefull things with my time.

But since I started I may as well finish.

No heart feelings

Share this post


Link to post
Share on other sites
Well, the problem is that for learning I would definitly try to stick with easier code. The structure that you descvribe looks a lot like someone fucked it up - the only time I had a structure like this was for an ISP accounting package "growing over months" (with looks over customers, contracts, services, then special cases) and this got reengineered EXTREMELY fast into a class based design.

Search better samples - yours like more like something a specialist does to clean up the fuckup of a beginner, not like something I would try to learn DirectX.


Regards

Thomas Tomiczek
THONA Consulting Ltd.
(Microsoft MVP C#/.NET)

Share this post


Link to post
Share on other sites
Oh, you mean the windows message loop handler?

Frankly, that is pretty standard code and pretty simple, too :-) Or do you mean the code do analyze stuff like the one used to find the best card and mode? That is pretty standard stuff, too :-)

Regards

Thomas Tomiczek
THONA Consulting Ltd.
(Microsoft MVP C#/.NET)

Share this post


Link to post
Share on other sites
I don''t think your problem is so much you don''t understand the logic, but that you don''t understand D3D. There is nothing complicated going on here, break it down into smaller parts. Remove sections of code and see what happens, consult the SDK Docs to see what function parameters do, etc.



[My site|SGI STL|Bjarne FAQ|C++ FAQ Lite|MSDN|Jargon]
Ripped off from various people

Share this post


Link to post
Share on other sites