hedgewars/uStore.pas
changeset 2630 079ef82eac75
parent 2624 0c3d5549912e
child 2633 9eb131794a6e
equal deleted inserted replaced
2629:be70fd3458c0 2630:079ef82eac75
   263 var ii: TSprite;
   263 var ii: TSprite;
   264     fi: THWFont;
   264     fi: THWFont;
   265     ai: TAmmoType;
   265     ai: TAmmoType;
   266     tmpsurf: PSDL_Surface;
   266     tmpsurf: PSDL_Surface;
   267     i: LongInt;
   267     i: LongInt;
   268 {$IFDEF IPHONEOS}
       
   269 tmpP: PLongWordArray;
       
   270 tmpA, tmpR, tmpG, tmpB: LongWord;
       
   271 {$ENDIF}
       
   272 begin
   268 begin
   273 
   269 
   274 for fi:= Low(THWFont) to High(THWFont) do
   270 for fi:= Low(THWFont) to High(THWFont) do
   275 	with Fontz[fi] do
   271 	with Fontz[fi] do
   276 		begin
   272 		begin
   302 				if tmpsurf = nil then
   298 				if tmpsurf = nil then
   303 					tmpsurf:= LoadImage(Pathz[AltPath] + '/' + FileName, ifAlpha or ifCritical or ifTransparent);
   299 					tmpsurf:= LoadImage(Pathz[AltPath] + '/' + FileName, ifAlpha or ifCritical or ifTransparent);
   304 				end;
   300 				end;
   305 
   301 
   306 			if tmpsurf <> nil then
   302 			if tmpsurf <> nil then
   307 				begin
   303 			begin
   308 {$IFDEF IPHONEOS}   
       
   309 {* http://bugzilla.libsdl.org/show_bug.cgi?id=868 but patched library doesn't work on ipod, so implementing workaround here *}
       
   310 				if (ifAlpha or ifTransparent) > 0 then
       
   311 				begin
       
   312 					tmpP := tmpsurf^.pixels;
       
   313 					for i:= 0 to (tmpsurf^.pitch shr 2) * tmpsurf^.h - 1 do 
       
   314 					begin
       
   315 {$IFDEF ENDIAN_LITTLE}
       
   316 						tmpA:= tmpP^[i] shr 24 and $FF;
       
   317 						tmpR:= tmpP^[i] shr 16 and $FF;
       
   318 						tmpG:= tmpP^[i] shr 8 and $FF;
       
   319 						tmpB:= tmpP^[i] and $FF;
       
   320 {$ELSE}
       
   321 						tmpA:= tmpP^[i] and $FF;
       
   322 						tmpR:= tmpP^[i] shr 8 and $FF;
       
   323 						tmpG:= tmpP^[i] shr 16 and $FF;
       
   324 						tmpB:= tmpP^[i] shr 24 and $FF;
       
   325 {$ENDIF}
       
   326 						if tmpA <> 0 then
       
   327 						begin
       
   328 							tmpR:= round(tmpR * 255 / tmpA);
       
   329 							tmpG:= round(tmpG * 255 / tmpA);
       
   330 							tmpB:= round(tmpB * 255 / tmpA);
       
   331 						end;
       
   332 
       
   333 						if tmpR > 255 then tmpR:= 255;
       
   334 						if tmpG > 255 then tmpG:= 255;
       
   335 						if tmpB > 255 then tmpB:= 255;
       
   336 
       
   337 {$IFDEF ENDIAN_LITTLE}
       
   338 						tmpP^[i]:= (tmpA shl 24) or (tmpR shl 16) or (tmpG shl 8) or tmpB;
       
   339 {$ELSE}
       
   340 						tmpP^[i]:= (tmpA) or (tmpR shl 8) or (tmpG shl 16) or (tmpB shl 24);
       
   341 {$ENDIF}
       
   342 					end;
       
   343 				end;
       
   344 {$ENDIF}
       
   345 				
       
   346 				if imageWidth = 0 then imageWidth:= tmpsurf^.w;
   304 				if imageWidth = 0 then imageWidth:= tmpsurf^.w;
   347 				if imageHeight = 0 then imageHeight:= tmpsurf^.h;
   305 				if imageHeight = 0 then imageHeight:= tmpsurf^.h;
   348 				if Width = 0 then Width:= tmpsurf^.w;
   306 				if Width = 0 then Width:= tmpsurf^.w;
   349 				if Height = 0 then Height:= tmpsurf^.h;
   307 				if Height = 0 then Height:= tmpsurf^.h;
   350 				if (ii in [sprSky, sprSkyL, sprSkyR, sprHorizont, sprHorizontL, sprHorizontR]) then
   308 				if (ii in [sprSky, sprSkyL, sprSkyR, sprHorizont, sprHorizontL, sprHorizontR]) then
   351 					Texture:= Surface2Tex(tmpsurf, true)
   309 					Texture:= Surface2Tex(tmpsurf, true)
   352 				else
   310 				else
   353 					begin
   311 					begin
   354 					Texture:= Surface2Tex(tmpsurf, false);
   312 					Texture:= Surface2Tex(tmpsurf, false);
   355 					if (ii = sprWater) and not cReducedQuality then // HACK: We should include some sprite attribute to define the texture wrap directions
   313 					if (ii = sprWater) and not cReducedQuality then // HACK: We should include some sprite attribute to define the texture wrap directions
   356 						begin
   314 					begin
   357 					(*	REMOVE ME WHEN BUG ABOVE IS FIXED
       
   358 						tmpP := tmpsurf^.pixels; 
       
   359 						for i:= 0 to (tmpsurf^.pitch shr 2) * tmpsurf^.h - 1 do
       
   360 						begin
       
   361 							tmpA:= tmpP^[i] shr 24 and $FF;
       
   362 							tmpR:= tmpP^[i] shr 16 and $FF;
       
   363 							tmpG:= tmpP^[i] shr 8 and $FF;
       
   364 							tmpB:= tmpP^[i] and $FF;
       
   365 						
       
   366 							writeln(stdout, inttostr(tmpA) + ' | ' + inttostr(tmpR) + ' | ' + inttostr(tmpG)+ ' | ' + inttostr(tmpB));
       
   367 						end;
       
   368 					*)
       
   369 						glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
   315 						glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
   370 						end;
       
   371 					end;
   316 					end;
       
   317 				end;
   372 				if saveSurf then Surface:= tmpsurf else SDL_FreeSurface(tmpsurf)
   318 				if saveSurf then Surface:= tmpsurf else SDL_FreeSurface(tmpsurf)
   373 				end
   319 				end
   374 			else
   320 			else
   375 				Surface:= nil
   321 				Surface:= nil
   376 		end;
   322 		end;
  1028 end;
   974 end;
  1029 
   975 
  1030 function  LoadImage(const filename: string; imageFlags: Integer): PSDL_Surface;
   976 function  LoadImage(const filename: string; imageFlags: Integer): PSDL_Surface;
  1031 var tmpsurf: PSDL_Surface;
   977 var tmpsurf: PSDL_Surface;
  1032     s: shortstring;
   978     s: shortstring;
  1033 begin
   979 {$IFDEF IPHONEOS}
  1034 WriteToConsole(msgLoading + filename + '... ');
   980     tmpP: PLongWordArray;
  1035 
   981     tmpA, tmpR, tmpG, tmpB: LongWord;
  1036 s:= filename + '.png';
   982     i: LongInt;
  1037 tmpsurf:= IMG_Load(Str2PChar(s));
   983 {$ENDIF}
  1038 
   984 begin
  1039 if ((imageFlags and ifLowRes) <> 0) then
   985 	WriteToConsole(msgLoading + filename + '... ');
       
   986 
       
   987 	s:= filename + '.png';
       
   988 	tmpsurf:= IMG_Load(Str2PChar(s));
       
   989 
       
   990 	if (imageFlags and ifLowRes) <> 0 then
  1040 	begin
   991 	begin
  1041 		s:= filename + '-lowres.png';
   992 		s:= filename + '-lowres.png';
  1042 		if (tmpsurf <> nil) then
   993 		if (tmpsurf <> nil) then
  1043 		begin
   994 		begin
  1044 			if ((tmpsurf^.w > MaxTextureSize) or (tmpsurf^.h > MaxTextureSize)) then
   995 			if ((tmpsurf^.w > MaxTextureSize) or (tmpsurf^.h > MaxTextureSize)) then
  1045 			begin
   996 			begin
  1046 				SDL_FreeSurface(tmpsurf);
   997 				SDL_FreeSurface(tmpsurf);
  1047 				WriteLnToConsole('Image too big, trying to load lowres version: ' + s);
   998 				{$IFDEF DEBUGFILE}
       
   999 				AddFileLog('...image too big, trying to load lowres version: ' + s + '...');
       
  1000 				{$ENDIF}
  1048 				tmpsurf:= IMG_Load(Str2PChar(s))
  1001 				tmpsurf:= IMG_Load(Str2PChar(s))
  1049 			end;
  1002 			end;
  1050 		end
  1003 		end
  1051 		else
  1004 		else
  1052 		begin
  1005 		begin
  1053 			WriteLnToConsole('Image not found, trying to load lowres version: ' + s);
  1006 			{$IFDEF DEBUGFILE}
       
  1007 			AddFileLog('...image not found, trying to load lowres version: ' + s + '...');
       
  1008 			{$ENDIF}
  1054 			tmpsurf:= IMG_Load(Str2PChar(s))
  1009 			tmpsurf:= IMG_Load(Str2PChar(s))
  1055 		end;
  1010 		end;
  1056 	end;
  1011 	end;
  1057 
  1012 
  1058 if tmpsurf = nil then
  1013 	if tmpsurf = nil then
  1059 	begin
  1014 	begin
  1060 	OutError(msgFailed, (imageFlags and ifCritical) <> 0);
  1015 		OutError(msgFailed, (imageFlags and ifCritical) <> 0);
  1061 	exit(nil)
  1016 		exit(nil)
  1062 	end;
  1017 	end;
  1063 
  1018 
  1064 if ((imageFlags and ifIgnoreCaps) = 0) and ((tmpsurf^.w > MaxTextureSize) or (tmpsurf^.h > MaxTextureSize)) then
  1019 	if ((imageFlags and ifIgnoreCaps) = 0) and ((tmpsurf^.w > MaxTextureSize) or (tmpsurf^.h > MaxTextureSize)) then
  1065 	begin
  1020 	begin
  1066 		SDL_FreeSurface(tmpsurf);
  1021 		SDL_FreeSurface(tmpsurf);
  1067 		OutError(msgFailedSize, (imageFlags and ifCritical) <> 0);
  1022 		OutError(msgFailedSize, (imageFlags and ifCritical) <> 0);
  1068 		//dummy surface to replace non-critical textures that failed to load due to their size
  1023 		// dummy surface to replace non-critical textures that failed to load due to their size
  1069 		exit(SDL_CreateRGBSurface(SDL_SWSURFACE, 32, 32, 32, RMask, GMask, BMask, AMask));
  1024 		exit(SDL_CreateRGBSurface(SDL_SWSURFACE, 32, 32, 32, RMask, GMask, BMask, AMask));
  1070 	end;
  1025 	end;
  1071 
  1026 
  1072 tmpsurf:= doSurfaceConversion(tmpsurf);
  1027 	tmpsurf:= doSurfaceConversion(tmpsurf);
  1073 
  1028 
  1074 if (imageFlags and ifTransparent) <> 0 then TryDo(SDL_SetColorKey(tmpsurf, SDL_SRCCOLORKEY, 0) = 0, errmsgTransparentSet, true);
  1029 {$IFDEF IPHONEOS}   
  1075 //if (imageFlags and ifAlpha) <> 0 then Result:= SDL_DisplayFormatAlpha(tmpsurf) else Result:= SDL_DisplayFormat(tmpsurf);
  1030 {* http://bugzilla.libsdl.org/show_bug.cgi?id=868 but patched library doesn't work on ipod, so implementing workaround here *}
  1076 WriteLnToConsole('(' + inttostr(tmpsurf^.w) + ',' + inttostr(tmpsurf^.h) + ') ');
  1031 	if imageFlags and (ifAlpha or ifTransparent) > 0 then
  1077 WriteLnToConsole(msgOK);
  1032 	begin
  1078 
  1033 		tmpP := tmpsurf^.pixels;
  1079 LoadImage:= tmpsurf //Result
  1034 		for i:= 0 to (tmpsurf^.pitch shr 2) * tmpsurf^.h - 1 do 
       
  1035 		begin
       
  1036 {$IFDEF ENDIAN_LITTLE}
       
  1037 			tmpA:= tmpP^[i] shr 24 and $FF;
       
  1038 			tmpR:= tmpP^[i] shr 16 and $FF;
       
  1039 			tmpG:= tmpP^[i] shr 8 and $FF;
       
  1040 			tmpB:= tmpP^[i] and $FF;
       
  1041 {$ELSE}
       
  1042 			tmpA:= tmpP^[i] and $FF;
       
  1043 			tmpR:= tmpP^[i] shr 8 and $FF;
       
  1044 			tmpG:= tmpP^[i] shr 16 and $FF;
       
  1045 			tmpB:= tmpP^[i] shr 24 and $FF;
       
  1046 {$ENDIF}
       
  1047 			if tmpA <> 0 then
       
  1048 			begin
       
  1049 				tmpR:= round(tmpR * 255 / tmpA);
       
  1050 				tmpG:= round(tmpG * 255 / tmpA);
       
  1051 				tmpB:= round(tmpB * 255 / tmpA);
       
  1052 			end;
       
  1053 
       
  1054 			if tmpR > 255 then tmpR:= 255;
       
  1055 			if tmpG > 255 then tmpG:= 255;
       
  1056 			if tmpB > 255 then tmpB:= 255;
       
  1057 
       
  1058 {$IFDEF ENDIAN_LITTLE}
       
  1059 			tmpP^[i]:= (tmpA shl 24) or (tmpR shl 16) or (tmpG shl 8) or tmpB;
       
  1060 {$ELSE}
       
  1061 			tmpP^[i]:= (tmpA) or (tmpR shl 8) or (tmpG shl 16) or (tmpB shl 24);
       
  1062 {$ENDIF}
       
  1063 		end;
       
  1064 (*		for i:= 0 to (tmpsurf^.pitch shr 2) * tmpsurf^.h - 1 do
       
  1065 		begin
       
  1066 			tmpA:= tmpP^[i] shr 24 and $FF;
       
  1067 			tmpR:= tmpP^[i] shr 16 and $FF;
       
  1068 			tmpG:= tmpP^[i] shr 8 and $FF;
       
  1069 			tmpB:= tmpP^[i] and $FF;
       
  1070 			writeln(stdout, inttostr(tmpA) + ' | ' + inttostr(tmpR) + ' | ' + inttostr(tmpG)+ ' | ' + inttostr(tmpB));
       
  1071 		end; *)
       
  1072 	end;
       
  1073 {$ENDIF}
       
  1074 
       
  1075 	if (imageFlags and ifTransparent) <> 0 then
       
  1076 		TryDo(SDL_SetColorKey(tmpsurf, SDL_SRCCOLORKEY, 0) = 0, errmsgTransparentSet, true);
       
  1077 
       
  1078 	WriteLnToConsole('(' + inttostr(tmpsurf^.w) + ',' + inttostr(tmpsurf^.h) + ') ');
       
  1079 	WriteLnToConsole(msgOK);
       
  1080 
       
  1081 	LoadImage:= tmpsurf //Result
  1080 end;
  1082 end;
  1081 
  1083 
  1082 function glLoadExtension(extension : string) : boolean;
  1084 function glLoadExtension(extension : string) : boolean;
  1083 begin
  1085 begin
  1084 {$IFDEF IPHONEOS}
  1086 {$IFDEF IPHONEOS}
  1097 procedure SetupOpenGL;
  1099 procedure SetupOpenGL;
  1098 var vendor: shortstring;
  1100 var vendor: shortstring;
  1099 begin
  1101 begin
  1100 glGetIntegerv(GL_MAX_TEXTURE_SIZE, @MaxTextureSize);
  1102 glGetIntegerv(GL_MAX_TEXTURE_SIZE, @MaxTextureSize);
  1101 
  1103 
       
  1104 WriteLnToConsole('OpenGL - Renderer: ' + string(pchar(glGetString(GL_RENDERER))));
  1102 {$IFDEF DEBUGFILE}
  1105 {$IFDEF DEBUGFILE}
  1103 AddFileLog('OpenGL - Renderer: ' + string(pchar(glGetString(GL_RENDERER))));
       
  1104 AddFileLog('OpenGL - Vendor: ' + string(pchar(glGetString(GL_VENDOR))));
  1106 AddFileLog('OpenGL - Vendor: ' + string(pchar(glGetString(GL_VENDOR))));
  1105 AddFileLog('OpenGL - Version: ' + string(pchar(glGetString(GL_VERSION))));
  1107 AddFileLog('OpenGL - Version: ' + string(pchar(glGetString(GL_VERSION))));
  1106 AddFileLog('OpenGL - GL_MAX_TEXTURE_SIZE: ' + inttostr(MaxTextureSize));
  1108 {$ENDIF}
  1107 {$ENDIF}
  1109 WriteLnToConsole('OpenGL - GL_MAX_TEXTURE_SIZE: ' + inttostr(MaxTextureSize));
  1108 
  1110 
  1109 if MaxTextureSize = 0 then
  1111 	if MaxTextureSize = 0 then
  1110 	begin
  1112 	begin
  1111 	MaxTextureSize:= 1024;
  1113 	MaxTextureSize:= 1024;
  1112 {$IFDEF DEBUGFILE}
  1114 {$IFDEF DEBUGFILE}
  1113 	AddFileLog('OpenGL Warning - driver didn''t provide any valid max texture size; assuming 1024');
  1115 	AddFileLog('OpenGL Warning - driver didn''t provide any valid max texture size; assuming 1024');
  1114 {$ENDIF}
  1116 {$ENDIF}