@hwnd%

Discussions related to graphics (2D and 3D), animation and games programming
Richard Russell
Posts: 607
Joined: Tue 18 Jun 2024, 09:32

Re: @hwnd%

Post by Richard Russell »

Ric wrote: Sat 07 Mar 2026, 23:58 Try running your own "tutorial 1" on D3D11 and you'll find the errors.
I eventually found that tutorial (a link would have been very helpful!) but I don't know where to get the source code and libraries from. There's a link to an old website belonging to my ISP. but that's long since gone. So I had to laboriously type in the definitions from scratch (I don't guarantee they are correct), which gave me this for the "to create a device and a swap chain" panel:

Code: Select all

      Width% = 640
      Height% = 512

      SYS "SDL_LoadObject", "D3D11.DLL" TO d3d11%
      SYS "SDL_LoadFunction", d3d11%, "D3D11CreateDeviceAndSwapChain" TO `D3D11CreateDeviceAndSwapChain`

      DIM DXGI_RATIONAL{Numerator%, Denominator%}
      DIM DXGI_SAMPLE_DESC{Count%, Quality%}
      DIM DXGI_MODE_DESC{Width%, Height%, RefreshRate{} = DXGI_RATIONAL{}, \
      \             Format%, ScanlineOrdering%, Scaling%}
      DIM DXGI_SWAP_CHAIN_DESC{BufferDesc{}=DXGI_MODE_DESC{}, SampleDesc{}=DXGI_SAMPLE_DESC{}, \
      \             BufferUsage%, BufferCount%, OutputWindow%, Windowed%, SwapEffect%, Flags%}

      DXGI_FORMAT_R8G8B8A8_UNORM = 28
      DXGI_USAGE_RENDER_TARGET_OUTPUT = 32
      D3D_DRIVER_TYPE_HARDWARE = 1
      D3D11_SDK_VERSION = 7
      NULL = 0
      S_OK = 0

      DIM sd{} = DXGI_SWAP_CHAIN_DESC{}
      sd.BufferCount% = 1
      sd.BufferDesc.Width% = Width%
      sd.BufferDesc.Height% = Height%
      sd.BufferDesc.Format% = DXGI_FORMAT_R8G8B8A8_UNORM
      sd.BufferDesc.RefreshRate.Numerator% = 60
      sd.BufferDesc.RefreshRate.Denominator% = 1
      sd.BufferUsage% = DXGI_USAGE_RENDER_TARGET_OUTPUT
      sd.OutputWindow% = @hwnd%
      sd.SampleDesc.Count% = 1
      sd.SampleDesc.Quality% = 0
      sd.Windowed% = 1

      SYS `D3D11CreateDeviceAndSwapChain`, NULL, D3D_DRIVER_TYPE_HARDWARE, \
      \   NULL, 0, NULL, 0, D3D11_SDK_VERSION, sd{}, ^pSwapChain%, \
      \   ^pd3dDevice%, NULL, ^pImmediateContext% TO hr%
      IF hr% <> S_OK ERROR 100, "CreateDeviceAndSwapChain failed: "+STR$~hr%
      !(^IDXGISwapChain{}+4) = !pSwapChain%
      !(^ID3D11Device{}+4) = !pd3dDevice%
      !(^ID3D11DeviceContext{}+4) = !pImmediateContext%
The key thing is that it runs without error in 32-bit BBCSDL! I know it can't actually work because I'm using SDL2's @hwnd% rather than the native Windows @hwnd% but we've discussed that before and you know the solution. But even with that known fault it doesn't crash or report an error.

Please POST A MINIMAL REPRODUCIBLE EXAMPLE of any code which is failing, rather than expecting me to do all the work.
Ric
Posts: 275
Joined: Tue 17 Apr 2018, 21:03

Re: @hwnd%

Post by Ric »

Thank you, thank you, thankyou
Kind Regards Ric.

6502 back in the day, BB4W 2017 onwards, BBCSDL from 2023
Ric
Posts: 275
Joined: Tue 17 Apr 2018, 21:03

Re: @hwnd%

Post by Ric »

I have put this code at thr beginning of the code you supplied

Code: Select all

   10 DIM SDL_SysWMinfo{     \
   20 \    version%,     \
   30 \    subsystem%,   \
   40 \    window%,      \ The window handle
   50 \    hdc%,         \ The window device context
   60 \    hinstance%    \ The instance handle
   70 \ }
   80
   90 SYS "SDL_GetWindowWMInfo", @hwnd% , SDL_SysWMinfo{}
  100
  110
  120 PRINT SDL_SysWMinfo.window%
and replaced @hwnd% with SDL_SysWMinfo.window% in the swapchain description.

Progress, but not success. I now get an address out of range error
Kind Regards Ric.

6502 back in the day, BB4W 2017 onwards, BBCSDL from 2023
Richard Russell
Posts: 607
Joined: Tue 18 Jun 2024, 09:32

Re: @hwnd%

Post by Richard Russell »

Ric wrote: Sun 08 Mar 2026, 17:46 Progress, but not success. I now get an address out of range error
An 'address out of range' error should be nearly as easy to diagnose as a 'No such variable' error. Presumably you know at which line it is failing (why didn't you say?) so you can discover what address is out of range. And having discovered that, you can check what the actual address is (either by PRINTing it in immediate mode, adding debug output to your program, or using the debugger - which lists the values of all the variables).

Knowing what address is out of range, and what value it actually has (zero would be a common cause of that error), you can trace it back to where in the code that address is set. Then the reason for it having an out-of-range value will hopefully be obvious.

I expect you noticed Jeremy's comments on your earlier code, including that you failed to include a 'sanity check' of a returned value not being zero. It's always a good idea to include such checks, because they can give an 'early warning' of a problem at a point in the code where it's easy to see why, rather than waiting for it to fail later on when the cause might be much less obvious.
Ric
Posts: 275
Joined: Tue 17 Apr 2018, 21:03

Re: @hwnd%

Post by Ric »

The error is at line 460 and SDL_SysWMinfo.window% is not equal to zero.

Code: Select all

   10 DIM SDL_SysWMinfo{     \
   20 \    version%,     \
   30 \    subsystem%,   \
   40 \    window%,      \ The window handle
   50 \    hdc%,         \ The window device context
   60 \    hinstance%    \ The instance handle
   70 \ }
   80
   90 SYS "SDL_GetWindowWMInfo", @hwnd% , SDL_SysWMinfo{}
  100
  110 PRINT "window% ";SDL_SysWMinfo.window%
  120
  130 Width% = 640
  140 Height% = 512
  150
  160 SYS "SDL_LoadObject", "D3D11.DLL" TO d3d11%
  170 SYS "SDL_LoadFunction", d3d11%, "D3D11CreateDeviceAndSwapChain" TO `D3D11CreateDeviceAndSwapChain`
  180
  190 DIM DXGI_RATIONAL{Numerator%, Denominator%}
  200 DIM DXGI_SAMPLE_DESC{Count%, Quality%}
  210 DIM DXGI_MODE_DESC{Width%, Height%, RefreshRate{} = DXGI_RATIONAL{}, \
  220 \             Format%, ScanlineOrdering%, Scaling%}
  230 DIM DXGI_SWAP_CHAIN_DESC{BufferDesc{}=DXGI_MODE_DESC{}, SampleDesc{}=DXGI_SAMPLE_DESC{}, \
  240 \             BufferUsage%, BufferCount%, OutputWindow%, Windowed%, SwapEffect%, Flags%}
  250
  260 DXGI_FORMAT_R8G8B8A8_UNORM = 28
  270 DXGI_USAGE_RENDER_TARGET_OUTPUT = 32
  280 D3D_DRIVER_TYPE_HARDWARE = 1
  290 D3D11_SDK_VERSION = 7
  300 NULL = 0
  310 S_OK = 0
  320
  330 DIM sd{} = DXGI_SWAP_CHAIN_DESC{}
  340 sd.BufferCount% = 1
  350 sd.BufferDesc.Width% = Width%
  360 sd.BufferDesc.Height% = Height%
  370 sd.BufferDesc.Format% = DXGI_FORMAT_R8G8B8A8_UNORM
  380 sd.BufferDesc.RefreshRate.Numerator% = 60
  390 sd.BufferDesc.RefreshRate.Denominator% = 1
  400 sd.BufferUsage% = DXGI_USAGE_RENDER_TARGET_OUTPUT
  410 sd.OutputWindow% = SDL_SysWMinfo.window%
  420 sd.SampleDesc.Count% = 1
  430 sd.SampleDesc.Quality% = 0
  440 sd.Windowed% = 1
  450
  460 SYS `D3D11CreateDeviceAndSwapChain`, NULL, D3D_DRIVER_TYPE_HARDWARE, \
  470 \   NULL, 0, NULL, 0, D3D11_SDK_VERSION, sd{}, ^pSwapChain%, \
  480 \   ^pd3dDevice%, NULL, ^pImmediateContext% TO hr%
  490 IF hr% <> S_OK ERROR 100, "CreateDeviceAndSwapChain failed: "+STR$~hr%
  500 !(^IDXGISwapChain{}+4) = !pSwapChain%
  510 !(^ID3D11Device{}+4) = !pd3dDevice%
  520 !(^ID3D11DeviceContext{}+4) = !pImmediateContext%
Kind Regards Ric.

6502 back in the day, BB4W 2017 onwards, BBCSDL from 2023
Richard Russell
Posts: 607
Joined: Tue 18 Jun 2024, 09:32

Re: @hwnd%

Post by Richard Russell »

Ric wrote: Sun 08 Mar 2026, 18:30 The error is at line 460
If I run the code you listed, in 32-bit BBCSDL, it doesn't give an error; it reports:

window% 2163800

and completes successfully.
and SDL_SysWMinfo.window% is not equal to zero.
But is `D3D11CreateDeviceAndSwapChain` zero? You need to check all the addresses used in the statement, not just one!
Ric
Posts: 275
Joined: Tue 17 Apr 2018, 21:03

Re: @hwnd%

Post by Ric »

Sorry I don't understand, what address do I check?
Kind Regards Ric.

6502 back in the day, BB4W 2017 onwards, BBCSDL from 2023
Ric
Posts: 275
Joined: Tue 17 Apr 2018, 21:03

Re: @hwnd%

Post by Ric »

My apologies Richard, I was accidentally running in 64bit version. It now runs in 32bit version. I will now try to make some progress on porting across to 32bit SDL.
THANK YOU FOR YOUR HELP
(I may well need it again)
Kind Regards Ric.

6502 back in the day, BB4W 2017 onwards, BBCSDL from 2023
Ric
Posts: 275
Joined: Tue 17 Apr 2018, 21:03

Re: @hwnd%

Post by Ric »

Just in case you are wondering if it really works, here is D3D up and running on BBCSDL(32bit)
https://1drv.ms/u/c/505513a4a01e9ba8/IQ ... 0?e=vjeX4l
Once again thanks to the help of Richard
Kind Regards Ric.

6502 back in the day, BB4W 2017 onwards, BBCSDL from 2023
Richard Russell
Posts: 607
Joined: Tue 18 Jun 2024, 09:32

Re: @hwnd%

Post by Richard Russell »

Ric wrote: Sun 08 Mar 2026, 20:52 Just in case you are wondering if it really works, here is D3D up and running on BBCSDL(32bit)
https://1drv.ms/u/c/505513a4a01e9ba8/IQ ... 0?e=vjeX4l
Beautiful, but I can't help thinking that it's a huge amount of code and huge amount of effort to achieve something (i.e. animating a texture-mapped object with specular illumination) that could have been done in a few lines, using the supplied BBC BASIC 3D libraries. And if done that way, it would have been compatible with BB4W and BBCSDL on the full range of supported platforms (including running in a browser).

Where did that shiny 'lump' object come from? If I can find the associated 3D model I could try animating it with the BBC BASIC library and comparing the results with yours to see if Direct3D 11 seems to have any significant benefits.