* added documentation (in-code and pasdoc generated)
[glBitmap.git] / examples / GrabScreen / GrabScreen.lpr
diff --git a/examples/GrabScreen/GrabScreen.lpr b/examples/GrabScreen/GrabScreen.lpr
new file mode 100644 (file)
index 0000000..c3b7b77
--- /dev/null
@@ -0,0 +1,58 @@
+program SimpleLoadFromFile;
+
+{$mode objfpc}{$H+}
+
+uses
+  {$IFDEF UNIX}{$IFDEF UseCThreads}
+  cthreads,
+  {$ENDIF}{$ENDIF}
+  Classes, Windows, SysUtils, dglOpenGL, glBitmap, Helper;
+
+var
+  oglWindow: TOpenGLWindow;
+  running: Boolean = true;
+
+function WindowProc(hWnd: HWND; Msg: UINT; wParam: WPARAM; lParam: LPARAM): LRESULT; stdcall;
+var
+  tex: TglBitmap2D;
+begin
+  case Msg of
+    WM_DESTROY: begin
+      running := false;
+    end;
+
+    WM_KEYDOWN: begin
+      if wParam = VK_RETURN then begin
+        tex := TglBitmap2D.Create;                                                // create empty texture
+        try
+          tex.GrabScreen(0, 0, 800, 600, tfRGB8ub3);                              // copy current framebuffer content to texture
+          tex.SaveToFile(ExtractFilePath(ApplicationName) + 'screen.bmp', ftBMP); // save texture to file
+          WriteLn('screen saved to screen.bmp');
+        finally
+          FreeAndNil(tex);
+        end;
+      end;
+    end;
+  end;
+  result := DefWindowProc(hWnd, Msg, wParam, lParam);
+end;
+
+procedure RenderLoop;
+begin
+  glBegin(GL_TRIANGLES);
+    glColor4f(1, 0, 0, 1); glVertex2f(400, 100);
+    glColor4f(0, 1, 0, 1); glVertex2f(100, 500);
+    glColor4f(0, 0, 1, 1); glVertex2f(700, 500);
+  glEnd;
+end;
+
+begin
+  oglWindow := CreateOpenGLWindow('GrapScreen (hit enter to grab screen)', 800, 600, @WindowProc);
+  while running and ProgressMesages do begin
+    RenderLoop;
+    SwapBuffers(oglWindow.DC);
+  end;
+  DestroyOpenGLWindow(oglWindow);
+end.
+
+