You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

92 lines
2.3 KiB

  1. unit Helper;
  2. {$mode objfpc}{$H+}
  3. interface
  4. uses
  5. Classes, SysUtils, Windows, dglOpenGL;
  6. type
  7. TOpenGLWindow = packed record
  8. LWndClass: TWndClass;
  9. hMainHandle: HWND;
  10. DC: HDC;
  11. RC: HGLRC;
  12. end;
  13. function CreateOpenGLWindow(const aCaption: String; const aWidth, aHeight: Integer; const aWndProc: WNDPROC): TOpenGLWindow;
  14. function ProgressMesages: Boolean;
  15. procedure DestroyOpenGLWindow(const aWindow: TOpenGLWindow);
  16. implementation
  17. function CreateOpenGLWindow(const aCaption: String; const aWidth, aHeight: Integer; const aWndProc: WNDPROC): TOpenGLWindow;
  18. begin
  19. //create the window
  20. result.LWndClass.hInstance := hInstance;
  21. with result.LWndClass do begin
  22. lpszClassName := 'MyWinApiWnd';
  23. Style := CS_PARENTDC or CS_BYTEALIGNCLIENT;
  24. hIcon := LoadIcon(hInstance,'MAINICON');
  25. lpfnWndProc := aWndProc;
  26. hbrBackground := COLOR_BTNFACE+1;
  27. hCursor := LoadCursor(0,IDC_ARROW);
  28. end;
  29. RegisterClass(result.LWndClass);
  30. result.hMainHandle := CreateWindow(
  31. result.LWndClass.lpszClassName,
  32. PAnsiChar(aCaption),
  33. WS_CAPTION or WS_MINIMIZEBOX or WS_SYSMENU or WS_VISIBLE,
  34. (GetSystemMetrics(SM_CXSCREEN) - aWidth) div 2,
  35. (GetSystemMetrics(SM_CYSCREEN) - aHeight) div 2,
  36. aWidth, aHeight, 0, 0, hInstance, nil);
  37. // create and activate rendering context
  38. result.DC := GetDC(result.hMainHandle);
  39. if (result.DC = 0) then begin
  40. WriteLn('unable to get DeviceContext');
  41. halt;
  42. end;
  43. result.RC := CreateRenderingContext(result.DC, [opDoubleBuffered], 32, 24, 0, 0, 0, 0);
  44. if (result.RC = 0) then begin
  45. WriteLn('unable to create RenderingContext');
  46. halt;
  47. end;
  48. ActivateRenderingContext(result.DC, result.RC);
  49. // init OpenGL
  50. glViewport(0, 0, aWidth, aHeight);
  51. glClearColor(0, 0, 0, 0);
  52. glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT);
  53. glDisable(GL_DEPTH_TEST);
  54. glDisable(GL_CULL_FACE);
  55. glMatrixMode(GL_PROJECTION);
  56. glLoadIdentity;
  57. glOrtho(0, aWidth, aHeight, 0, -10, 10);
  58. glMatrixMode(GL_MODELVIEW);
  59. glLoadIdentity;
  60. end;
  61. function ProgressMesages: Boolean;
  62. var
  63. Msg: TMSG;
  64. begin
  65. result := GetMessage(Msg, 0, 0, 0);
  66. if result then begin
  67. TranslateMessage(Msg);
  68. DispatchMessage(Msg);
  69. end;
  70. end;
  71. procedure DestroyOpenGLWindow(const aWindow: TOpenGLWindow);
  72. begin
  73. DestroyRenderingContext(aWindow.RC);
  74. ReleaseDC(aWindow.hMainHandle, aWindow.DC);
  75. end;
  76. end.