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;
  tex: TglBitmap2D;
  data: TglBitmapData;

function WindowProc(hWnd: HWND; Msg: UINT; wParam: WPARAM; lParam: LPARAM): LRESULT; stdcall;
begin
  case Msg of
    WM_DESTROY: begin
      running := false;
    end;
  end;
  result := DefWindowProc(hWnd, Msg, wParam, lParam);
end;

procedure RenderLoop;
begin
  tex.Bind();
  glColor4f(1, 1, 1, 1);
  glBegin(GL_QUADS);
    glTexCoord2f(0, 0); glVertex2f(100, 100);
    glTexCoord2f(1, 0); glVertex2f(700, 100);
    glTexCoord2f(1, 1); glVertex2f(700, 500);
    glTexCoord2f(0, 1); glVertex2f(100, 500);
  glEnd;
  tex.Unbind();
end;

begin
  oglWindow := CreateOpenGLWindow('SimpleLoadFromFile', 800, 600, @WindowProc);
  try
    tex  := TglBitmap2D.Create;               // create texture object
    data := TglBitmapData.Create;             // create texture data object
    try
      data.LoadFromFile(                      // load texture data from file
        ExtractFilePath(ApplicationName) +
        '../textures/BMP_24_RGB8.bmp');
      tex.UploadData(data);                   // upload data to video card
    finally
      FreeAndNil(data);                       // after upload is done, the data object could be freed to save memory
    end;

    while running and ProgressMesages do begin
      RenderLoop;
      SwapBuffers(oglWindow.DC);
    end;
  finally
    FreeAndNil(tex);
    DestroyOpenGLWindow(oglWindow);
  end;
end.

