hedgewars/uStore.pas
changeset 7129 0ce4f478ea6c
parent 7127 8e5e3fef16fc
child 7138 f8248bcba8f1
equal deleted inserted replaced
7127:8e5e3fef16fc 7129:0ce4f478ea6c
    55 {$IFDEF SDL13}
    55 {$IFDEF SDL13}
    56     SDLwindow: PSDL_Window;
    56     SDLwindow: PSDL_Window;
    57     SDLGLcontext: PSDL_GLContext;
    57     SDLGLcontext: PSDL_GLContext;
    58 {$ELSE}
    58 {$ELSE}
    59     SDLPrimSurface: PSDL_Surface;
    59     SDLPrimSurface: PSDL_Surface;
       
    60 {$ENDIF}
       
    61 {$IFDEF GL2}
       
    62     Shader: GLuint;
    60 {$ENDIF}
    63 {$ENDIF}
    61 
    64 
    62 function WriteInRect(Surface: PSDL_Surface; X, Y: LongInt; Color: LongWord; Font: THWFont; s: ansistring): TSDL_Rect;
    65 function WriteInRect(Surface: PSDL_Surface; X, Y: LongInt; Color: LongWord; Font: THWFont; s: ansistring): TSDL_Rect;
    63 var w, h: LongInt;
    66 var w, h: LongInt;
    64     tmpsurf: PSDL_Surface;
    67     tmpsurf: PSDL_Surface;
   624     SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 0); // no alpha channel required
   627     SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 0); // no alpha channel required
   625     SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, 16); // buffer has to be 16 bit only
   628     SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, 16); // buffer has to be 16 bit only
   626     SDL_GL_SetAttribute(SDL_GL_ACCELERATED_VISUAL, 1); // try to prefer hardware rendering
   629     SDL_GL_SetAttribute(SDL_GL_ACCELERATED_VISUAL, 1); // try to prefer hardware rendering
   627 end;
   630 end;
   628 
   631 
       
   632 {$IFDEF GL2}
       
   633 function CompileShader(shaderFile: string; shaderType: GLenum): GLuint;
       
   634 var
       
   635     shader: GLuint;
       
   636     f: Textfile;
       
   637     source, line: AnsiString;
       
   638     sourceA: Pchar;
       
   639     lengthA: GLint;
       
   640     compileResult: GLint;
       
   641     logLength: GLint;
       
   642     log: PChar;
       
   643 begin
       
   644     Assign(f, Pathz[ptShaders] + '/' + shaderFile);
       
   645     filemode:= 0; // readonly
       
   646     Reset(f);
       
   647     if IOResult <> 0 then
       
   648     begin
       
   649         AddFileLog('Unable to load ' + shaderFile);
       
   650         halt(-1);
       
   651     end;
       
   652 
       
   653     source:='';
       
   654     while not eof(f) do
       
   655     begin
       
   656         ReadLn(f, line);
       
   657         source:= source + line + #10;
       
   658     end;
       
   659     
       
   660     CloseFile(f);
       
   661 
       
   662     writeln('Compiling shader: ' + Pathz[ptShaders] + '/' + shaderFile);
       
   663 
       
   664     sourceA:=PChar(source);
       
   665     lengthA:=Length(source);
       
   666 
       
   667     shader:=glCreateShader(shaderType);
       
   668     glShaderSource(shader, 1, @sourceA, @lengthA);
       
   669     glCompileShader(shader);
       
   670     glGetShaderiv(shader, GL_COMPILE_STATUS, @compileResult);
       
   671     glGetShaderiv(shader, GL_INFO_LOG_LENGTH, @logLength);
       
   672 
       
   673     if logLength > 1 then
       
   674     begin
       
   675         GetMem(log, logLength);
       
   676         glGetShaderInfoLog(shader, logLength, nil, log);
       
   677         writeln('========== Compiler log  ==========');
       
   678         writeln(log);
       
   679         writeln('===================================');
       
   680         FreeMem(log, logLength);
       
   681     end;
       
   682 
       
   683     if compileResult <> GL_TRUE then
       
   684     begin
       
   685         writeln('Shader compilation failed, halting');
       
   686         halt(-1);
       
   687     end;
       
   688 
       
   689     CompileShader:= shader;
       
   690 end;
       
   691 
       
   692 function CompileProgram(shaderName: string): GLuint;
       
   693 var
       
   694     program_: GLuint;
       
   695     vs, fs: GLuint;
       
   696     linkResult: GLint;
       
   697     logLength: GLint;
       
   698     log: PChar;
       
   699 begin
       
   700     program_:= glCreateProgram();
       
   701     vs:= CompileShader(shaderName + '.vs', GL_VERTEX_SHADER);
       
   702     fs:= CompileShader(shaderName + '.fs', GL_FRAGMENT_SHADER);
       
   703     glAttachShader(program_, vs);
       
   704     glAttachShader(program_, fs);
       
   705     glLinkProgram(program_);
       
   706     glDeleteShader(vs);
       
   707     glDeleteShader(fs);
       
   708 
       
   709     glGetProgramiv(program_, GL_LINK_STATUS, @linkResult);
       
   710     glGetProgramiv(program_, GL_INFO_LOG_LENGTH, @logLength);
       
   711 
       
   712     if logLength > 1 then
       
   713     begin
       
   714         GetMem(log, logLength);
       
   715         glGetProgramInfoLog(program_, logLength, nil, log);
       
   716         writeln('========== Compiler log  ==========');
       
   717         writeln(log);
       
   718         writeln('===================================');
       
   719         FreeMem(log, logLength);
       
   720     end;
       
   721 
       
   722     if linkResult <> GL_TRUE then
       
   723     begin
       
   724         writeln('Linking program failed, halting');
       
   725         halt(-1);
       
   726     end;
       
   727 
       
   728     CompileProgram:= program_;
       
   729 end;
       
   730 {$ENDIF}
       
   731 
   629 procedure SetupOpenGL;
   732 procedure SetupOpenGL;
   630 //var vendor: shortstring = '';
   733 //var vendor: shortstring = '';
   631 var buf: array[byte] of char;
   734 var buf: array[byte] of char;
   632 begin
   735 begin
   633     buf[0]:= char(0); // avoid compiler hint
   736     buf[0]:= char(0); // avoid compiler hint
   680     AddFileLog('  |----- Vendor: ' + shortstring(pchar(glGetString(GL_VENDOR))));
   783     AddFileLog('  |----- Vendor: ' + shortstring(pchar(glGetString(GL_VENDOR))));
   681     AddFileLog('  |----- Version: ' + shortstring(pchar(glGetString(GL_VERSION))));
   784     AddFileLog('  |----- Version: ' + shortstring(pchar(glGetString(GL_VERSION))));
   682     AddFileLog('  |----- Texture Size: ' + inttostr(MaxTextureSize));
   785     AddFileLog('  |----- Texture Size: ' + inttostr(MaxTextureSize));
   683     AddFileLog('  \----- Extensions: ' + shortstring(pchar(glGetString(GL_EXTENSIONS))));
   786     AddFileLog('  \----- Extensions: ' + shortstring(pchar(glGetString(GL_EXTENSIONS))));
   684     //TODO: don't have the Extensions line trimmed but slipt it into multiple lines
   787     //TODO: don't have the Extensions line trimmed but slipt it into multiple lines
       
   788 
       
   789 {$IFDEF GL2}
       
   790     Load_GL_VERSION_2_0;
       
   791     Shader:= CompileProgram('default');
       
   792     glUseProgram(Shader);
       
   793     glUniform1i(glGetUniformLocation(Shader, 'tex'), 0);
       
   794 {$ENDIF}
   685 
   795 
   686 {$IFNDEF S3D_DISABLED}
   796 {$IFNDEF S3D_DISABLED}
   687     if (cStereoMode = smHorizontal) or (cStereoMode = smVertical) or (cStereoMode = smAFR) then
   797     if (cStereoMode = smHorizontal) or (cStereoMode = smVertical) or (cStereoMode = smAFR) then
   688     begin
   798     begin
   689         // prepare left and right frame buffers and associated textures
   799         // prepare left and right frame buffers and associated textures
  1149 {$ENDIF}
  1259 {$ENDIF}
  1150 end;
  1260 end;
  1151 
  1261 
  1152 procedure freeModule;
  1262 procedure freeModule;
  1153 begin
  1263 begin
       
  1264 {$IFDEF GL2}
       
  1265     glDeleteProgram(Shader);
       
  1266 {$ENDIF}
  1154     StoreRelease(false);
  1267     StoreRelease(false);
  1155     TTF_Quit();
  1268     TTF_Quit();
  1156 {$IFDEF SDL13}
  1269 {$IFDEF SDL13}
  1157     SDL_GL_DeleteContext(SDLGLcontext);
  1270     SDL_GL_DeleteContext(SDLGLcontext);
  1158     SDL_DestroyWindow(SDLwindow);
  1271     SDL_DestroyWindow(SDLwindow);