On 2017-05-24 04:13, Ryan Joseph wrote:

Any ideas on what’s wrong with this?

I actually used the Java + OpenGL tutorials on YouTube, and translating those examples to Object Pascal + OpenGL + SDL2 was pretty easy.

This is my favourite one, and uses "modern OpenGL" only.


https://www.youtube.com/watch?v=VS8wlS9hF8E&list=PLRIWtICgwaX0u7Rf9zkZhLoLuZVfUksDP

  "OpenGL 3D Game Tutorial" by ThinMatrix.

I also wanted something reusable, so I implemented classes in separate units that I can reuse between projects. Instead of 100's of loosely scattered OpenGL API calls. I was actually going to contribute all this work as part of my Lazarus Graphics Programming Contest entry, but I might just release it earlier than that.

Anyway, attached is one such unit setting up my display. I use proprietary NVIDIA drivers under FreeBSD, and I only target OpenGL 4.x with my code. It works perfectly here.

My program code is pretty much like this:

   TDisplayManager.CreateDisplay;

   while running do
   begin
     // do the "gaming loop" here
     TDisplayManager.UpdateDisplay;
   end;

   // clean-up code here
   TDisplayManager.CloseDisplay;



Regards,
  Graeme

--
fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal
http://fpgui.sourceforge.net/

My public PGP key:  http://tinyurl.com/graeme-pgp
unit DisplayManager;

{$mode objfpc}{$H+}

interface

uses
  SDL2,
  GL,
  GLext;

type

  TDisplayManager = class(TObject)
  private
    class var FSDLWindow: PSDL_Window;
    class var FSDLGLContext: TSDL_GLContext;
    const
      cWIDTH = 856;
      cHEIGHT = 480;
  public
    class procedure CreateDisplay; static;
    class procedure UpdateDisplay; static;
    class procedure CloseDisplay; static;
//    property  SDLWindow: PSDL_Window read FSDLWindow;
  end;

implementation

{ TDisplayManager }

class procedure TDisplayManager.CreateDisplay;
begin
  if SDL_Init(SDL_INIT_VIDEO) < 0 then
    HALT;

  // setting so that deprecated functions are disabled
  SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);

  //get an OpenGL window and create OpenGL context
  FSDLWindow := SDL_CreateWindow('OpenGL Game Tutorial', SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, cWIDTH, cHEIGHT, SDL_WINDOW_OPENGL);
  if FSDLWindow = nil then
    HALT;

  FSDLGLContext := SDL_GL_CreateContext(FSDLWindow);
  if FSDLGLContext = nil then
    HALT;

  //init OpenGL and load extensions
  if Load_GL_VERSION_4_0 = false then
    if Load_GL_VERSION_3_3 = false then
      if Load_GL_VERSION_3_2 = false then
//        if Load_GL_VERSION_3_0 = false then
        begin
          writeln(' ERROR: OpenGL 3.2 or higher needed. ');
          HALT;
        end;

  glViewport(0, 0, cWIDTH, cHEIGHT);

  //print out OpenGL vendor, version and shader version
  writeln( 'Vendor: ' + glGetString( GL_VENDOR ) );
  writeln( 'OpenGL Version: ' + glGetString( GL_VERSION ) );
  writeln( 'Shader Version: ' + glGetString( GL_SHADING_LANGUAGE_VERSION ) );
end;

class procedure TDisplayManager.UpdateDisplay;
begin
  SDL_GL_SwapWindow(FSDLWindow);
end;

class procedure TDisplayManager.CloseDisplay;
begin
  SDL_GL_DeleteContext(FSDLGLContext);
  SDL_DestroyWindow(FSDLWindow);
  SDL_Quit;
end;

end.

_______________________________________________
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Reply via email to