hedgewars/uStore.pas
changeset 2697 75880595a9f1
parent 2695 ed789a7ef68d
child 2698 90585aba87ad
equal deleted inserted replaced
2696:41aa7b56c17b 2697:75880595a9f1
  1002 end;
  1002 end;
  1003 
  1003 
  1004 function  LoadImage(const filename: string; imageFlags: LongInt): PSDL_Surface;
  1004 function  LoadImage(const filename: string; imageFlags: LongInt): PSDL_Surface;
  1005 var tmpsurf: PSDL_Surface;
  1005 var tmpsurf: PSDL_Surface;
  1006     s: shortstring;
  1006     s: shortstring;
  1007 {$IFDEF DONTUSE}
       
  1008     tmpP: PLongWordArray;
       
  1009     tmpA, tmpR, tmpG, tmpB: LongWord;
       
  1010     i: LongInt;
       
  1011 {$ENDIF}
       
  1012 begin
  1007 begin
  1013 	WriteToConsole(msgLoading + filename + ' [flags: ' + inttostr(imageFlags) + ']... ');
  1008 	WriteToConsole(msgLoading + filename + ' [flags: ' + inttostr(imageFlags) + ']... ');
  1014 
  1009 
  1015 	s:= filename + '.png';
  1010 	s:= filename + '.png';
  1016 	tmpsurf:= IMG_Load(Str2PChar(s));
  1011 	tmpsurf:= IMG_Load(Str2PChar(s));
  1052 		exit(SDL_CreateRGBSurface(SDL_SWSURFACE, 32, 32, 32, RMask, GMask, BMask, AMask));
  1047 		exit(SDL_CreateRGBSurface(SDL_SWSURFACE, 32, 32, 32, RMask, GMask, BMask, AMask));
  1053 	end;
  1048 	end;
  1054 
  1049 
  1055 	tmpsurf:= doSurfaceConversion(tmpsurf);
  1050 	tmpsurf:= doSurfaceConversion(tmpsurf);
  1056 
  1051 
  1057 {$IFDEF DONTUSE}   // way too slow
       
  1058 {* http://bugzilla.libsdl.org/show_bug.cgi?id=868 but patched library doesn't work on ipod, so implementing workaround here *}
       
  1059 	if imageFlags and (ifAlpha or ifTransparent) > 0 then
       
  1060 	begin
       
  1061 		tmpP := tmpsurf^.pixels;
       
  1062 		for i:= 0 to (tmpsurf^.pitch shr 2) * tmpsurf^.h - 1 do 
       
  1063 		begin
       
  1064 {$IFDEF ENDIAN_LITTLE}
       
  1065 			tmpA:= tmpP^[i] shr 24 and $FF;
       
  1066 			tmpR:= tmpP^[i] shr 16 and $FF;
       
  1067 			tmpG:= tmpP^[i] shr 8 and $FF;
       
  1068 			tmpB:= tmpP^[i] and $FF;
       
  1069 {$ELSE}
       
  1070 			tmpA:= tmpP^[i] and $FF;
       
  1071 			tmpR:= tmpP^[i] shr 8 and $FF;
       
  1072 			tmpG:= tmpP^[i] shr 16 and $FF;
       
  1073 			tmpB:= tmpP^[i] shr 24 and $FF;
       
  1074 {$ENDIF}
       
  1075 			if tmpA <> 0 then
       
  1076 			begin
       
  1077 				tmpR:= round(tmpR * 255 / tmpA);
       
  1078 				tmpG:= round(tmpG * 255 / tmpA);
       
  1079 				tmpB:= round(tmpB * 255 / tmpA);
       
  1080 			end;
       
  1081 
       
  1082 			if tmpR > 255 then tmpR:= 255;
       
  1083 			if tmpG > 255 then tmpG:= 255;
       
  1084 			if tmpB > 255 then tmpB:= 255;
       
  1085 
       
  1086 {$IFDEF ENDIAN_LITTLE}
       
  1087 			tmpP^[i]:= (tmpA shl 24) or (tmpR shl 16) or (tmpG shl 8) or tmpB;
       
  1088 {$ELSE}
       
  1089 			tmpP^[i]:= (tmpA) or (tmpR shl 8) or (tmpG shl 16) or (tmpB shl 24);
       
  1090 {$ENDIF}
       
  1091 		end;
       
  1092 (*		for i:= 0 to (tmpsurf^.pitch shr 2) * tmpsurf^.h - 1 do
       
  1093 		begin
       
  1094 			tmpA:= tmpP^[i] shr 24 and $FF;
       
  1095 			tmpR:= tmpP^[i] shr 16 and $FF;
       
  1096 			tmpG:= tmpP^[i] shr 8 and $FF;
       
  1097 			tmpB:= tmpP^[i] and $FF;
       
  1098 			writeln(stdout, inttostr(tmpA) + ' | ' + inttostr(tmpR) + ' | ' + inttostr(tmpG)+ ' | ' + inttostr(tmpB));
       
  1099 		end; *)
       
  1100 	end;
       
  1101 {$ENDIF}
       
  1102 
       
  1103 	if (imageFlags and ifTransparent) <> 0 then
  1052 	if (imageFlags and ifTransparent) <> 0 then
  1104 		TryDo(SDL_SetColorKey(tmpsurf, SDL_SRCCOLORKEY, 0) = 0, errmsgTransparentSet, true);
  1053 		TryDo(SDL_SetColorKey(tmpsurf, SDL_SRCCOLORKEY, 0) = 0, errmsgTransparentSet, true);
  1105 
  1054 
  1106 	WriteLnToConsole('(' + inttostr(tmpsurf^.w) + ',' + inttostr(tmpsurf^.h) + ') ');
  1055 	WriteLnToConsole('(' + inttostr(tmpsurf^.w) + ',' + inttostr(tmpsurf^.h) + ') ');
  1107 	WriteLnToConsole(msgOK);
  1056 	WriteLnToConsole(msgOK);
  1123 		AddFileLog('OpenGL - "' + extension + '" loaded');
  1072 		AddFileLog('OpenGL - "' + extension + '" loaded');
  1124 {$ENDIF}
  1073 {$ENDIF}
  1125 end;
  1074 end;
  1126 
  1075 
  1127 procedure SetupOpenGL;
  1076 procedure SetupOpenGL;
       
  1077 {$IFDEF DEBUGFILE}
  1128 var vendor: shortstring;
  1078 var vendor: shortstring;
  1129 begin
  1079 {$ENDIF}
       
  1080 begin
       
  1081 {$IFDEF IPHONEOS}
       
  1082 //these are good performance savers, perhaps we could enable them by default
       
  1083 	SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 0);
       
  1084 	SDL_GL_SetAttribute(SDL_GL_RETAINED_BACKING, 1);
       
  1085 	SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 0);
       
  1086 	SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
       
  1087 	SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 6);
       
  1088 	SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
       
  1089 	SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 0);
       
  1090 	SDL_GL_SetAttribute(SDL_GL_ACCELERATED_VISUAL, 1);
       
  1091 	//SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, 32);
       
  1092 {$ELSE}
       
  1093 	SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
       
  1094 {$ENDIF}
       
  1095 
       
  1096 {$IFNDEF SDL13}
       
  1097 // this attribute is default in 1.3 and must be enabled in MacOSX
       
  1098 {$IFNDEF DARWIN}
       
  1099 	if cVSyncInUse then
       
  1100 {$ENDIF}
       
  1101 		SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL, 1);
       
  1102 {$ENDIF}
       
  1103 
  1130 	glGetIntegerv(GL_MAX_TEXTURE_SIZE, @MaxTextureSize);
  1104 	glGetIntegerv(GL_MAX_TEXTURE_SIZE, @MaxTextureSize);
       
  1105 
       
  1106 {$IFDEF DEBUGFILE}
  1131 	vendor:= LowerCase(string(pchar(glGetString(GL_VENDOR))));
  1107 	vendor:= LowerCase(string(pchar(glGetString(GL_VENDOR))));
  1132 
  1108 
  1133 {$IFDEF DEBUGFILE}
       
  1134 	AddFileLog('OpenGL - Renderer: ' + string(pchar(glGetString(GL_RENDERER))));
  1109 	AddFileLog('OpenGL - Renderer: ' + string(pchar(glGetString(GL_RENDERER))));
  1135 	AddFileLog('  |----- Vendor: ' + vendor);
  1110 	AddFileLog('  |----- Vendor: ' + vendor);
  1136 	AddFileLog('  |----- Version: ' + string(pchar(glGetString(GL_VERSION))));
  1111 	AddFileLog('  |----- Version: ' + string(pchar(glGetString(GL_VERSION))));
  1137 	AddFileLog('  \----- GL_MAX_TEXTURE_SIZE: ' + inttostr(MaxTextureSize));
  1112 	AddFileLog('  \----- GL_MAX_TEXTURE_SIZE: ' + inttostr(MaxTextureSize));
  1138 {$ENDIF}
  1113 {$ENDIF}
  1139 
  1114 
  1140 	if MaxTextureSize = 0 then
  1115 	if MaxTextureSize <= 0 then
  1141 	begin
  1116 	begin
  1142 		MaxTextureSize:= 1024;
  1117 		MaxTextureSize:= 1024;
  1143 {$IFDEF DEBUGFILE}
  1118 {$IFDEF DEBUGFILE}
  1144 		AddFileLog('OpenGL Warning - driver didn''t provide any valid max texture size; assuming 1024');
  1119 		AddFileLog('OpenGL Warning - driver didn''t provide any valid max texture size; assuming 1024');
  1145 {$ENDIF}
  1120 {$ENDIF}
  1146 	end;
  1121 	end;
  1147 
  1122 
  1148 {$IFNDEF IPHONEOS}
  1123 {$IFNDEF IPHONEOS}
  1149 if StrPos(Str2PChar(vendor), Str2PChar('nvidia')) <> nil then
  1124 	if StrPos(Str2PChar(vendor), Str2PChar('nvidia')) <> nil then
  1150 	cGPUVendor:= gvNVIDIA
  1125 		cGPUVendor:= gvNVIDIA
  1151 else if StrPos(Str2PChar(vendor), Str2PChar('intel')) <> nil then
  1126 	else if StrPos(Str2PChar(vendor), Str2PChar('intel')) <> nil then
  1152 	cGPUVendor:= gvATI
  1127 		cGPUVendor:= gvATI
  1153 else if StrPos(Str2PChar(vendor), Str2PChar('ati')) <> nil then
  1128 	else if StrPos(Str2PChar(vendor), Str2PChar('ati')) <> nil then
  1154 	cGPUVendor:= gvIntel;
  1129 		cGPUVendor:= gvIntel;
  1155 
       
  1156 //SupportNPOTT:= glLoadExtension('GL_ARB_texture_non_power_of_two');
  1130 //SupportNPOTT:= glLoadExtension('GL_ARB_texture_non_power_of_two');
  1157 {$ENDIF}
  1131 {$ENDIF}
  1158 
  1132 
  1159 // set view port to whole window
  1133 	// set view port to whole window
  1160 glViewport(0, 0, cScreenWidth, cScreenHeight);
  1134 	glViewport(0, 0, cScreenWidth, cScreenHeight);
  1161 
  1135 
  1162 glMatrixMode(GL_MODELVIEW);
  1136 	glMatrixMode(GL_MODELVIEW);
  1163 // prepare default translation/scaling
  1137 	// prepare default translation/scaling
  1164 glLoadIdentity();
  1138 	glLoadIdentity();
  1165 glScalef(2.0 / cScreenWidth, -2.0 / cScreenHeight, 1.0);
  1139 	glScalef(2.0 / cScreenWidth, -2.0 / cScreenHeight, 1.0);
  1166 {$IFDEF IPHONEOS}
  1140 {$IFDEF IPHONEOS}
  1167 //glRotatef(90, 0, 0, 1);
  1141 	//glRotatef(90, 0, 0, 1);
  1168 {$ENDIF}
  1142 {$ENDIF}
  1169 glTranslatef(0, -cScreenHeight / 2, 0);
  1143 	glTranslatef(0, -cScreenHeight / 2, 0);
  1170 
  1144 
  1171 // enable alpha blending
  1145 	// enable alpha blending
  1172 glEnable(GL_BLEND);
  1146 	glEnable(GL_BLEND);
  1173 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  1147 	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  1174 end;
  1148 end;
  1175 
  1149 
  1176 procedure SetScale(f: GLfloat);
  1150 procedure SetScale(f: GLfloat);
  1177 begin
  1151 begin
  1178 // leave immediately if scale factor did not change
  1152 	// leave immediately if scale factor did not change
  1179 if f = cScaleFactor then exit;
  1153 	if f = cScaleFactor then exit;
  1180 
  1154 
  1181 if f = 2.0 then // default scaling
  1155 	if f = 2.0 then glPopMatrix // "return" to default scaling
  1182 	glPopMatrix // "return" to default scaling
  1156 	else // other scaling
  1183 else // other scaling
       
  1184 	begin
  1157 	begin
  1185 	glPushMatrix; // save default scaling
  1158 		glPushMatrix; // save default scaling
  1186 	glLoadIdentity;
  1159 		glLoadIdentity;
  1187 	glScalef(f / cScreenWidth, -f / cScreenHeight, 1.0);
  1160 		glScalef(f / cScreenWidth, -f / cScreenHeight, 1.0);
  1188 {$IFDEF IPHONEOS}
  1161 {$IFDEF IPHONEOS}
  1189 //	glRotatef(90, 0, 0, 1);
  1162 		//glRotatef(90, 0, 0, 1);
  1190 {$ENDIF}
  1163 {$ENDIF}
  1191 	glTranslatef(0, -cScreenHeight / 2, 0);
  1164 		glTranslatef(0, -cScreenHeight / 2, 0);
  1192 	end;
  1165 	end;
  1193 
  1166 
  1194 cScaleFactor:= f;
  1167 	cScaleFactor:= f;
  1195 end;
  1168 end;
  1196 
  1169 
  1197 ////////////////////////////////////////////////////////////////////////////////
  1170 ////////////////////////////////////////////////////////////////////////////////
  1198 var ProgrTex: PTexture = nil;
  1171 var Step: LongInt = 0;
  1199     Step: integer = 0;
       
  1200 	squaresize : LongInt;
  1172 	squaresize : LongInt;
  1201 	numsquares : integer;
  1173 	numsquares : LongInt;
       
  1174 {$IFDEF SDL13notworking}
       
  1175 	ProgrTex: TSDL_TextureID = 0;
       
  1176 {$ELSE}
       
  1177 	ProgrTex: PTexture = nil;
       
  1178 {$ENDIF}
  1202 
  1179 
  1203 procedure AddProgress;
  1180 procedure AddProgress;
  1204 var r: TSDL_Rect;
  1181 var r: TSDL_Rect;
  1205     texsurf: PSDL_Surface;
  1182     texsurf: PSDL_Surface;
  1206 begin
  1183 begin
  1207 	if Step = 0 then
  1184 	if Step = 0 then
  1208 	begin
  1185 	begin
  1209 	{$IFDEF SDL_IMAGE_NEWER}
  1186 {$IFDEF SDL_IMAGE_NEWER}
  1210 		WriteToConsole('Init SDL_image... ');
  1187 		WriteToConsole('Init SDL_image... ');
  1211 		SDLTry(IMG_Init(IMG_INIT_PNG) <> 0, true);
  1188 		SDLTry(IMG_Init(IMG_INIT_PNG) <> 0, true);
  1212 		WriteLnToConsole(msgOK);
  1189 		WriteLnToConsole(msgOK);
  1213 	{$ENDIF}
  1190 {$ENDIF}
  1214 	
  1191 	
  1215 		WriteToConsole(msgLoading + 'progress sprite: ');
  1192 		WriteToConsole(msgLoading + 'progress sprite: ');
  1216 		texsurf:= LoadImage(Pathz[ptGraphics] + '/Progress', ifCritical or ifTransparent);
  1193 		texsurf:= LoadImage(Pathz[ptGraphics] + '/Progress', ifCritical or ifTransparent);
       
  1194 {$IFDEF SDL13notworking}
       
  1195 		ProgrTex:= SDL_CreateTextureFromSurface(0, texsurf);
       
  1196 {$ELSE}
  1217 		ProgrTex:= Surface2Tex(texsurf, false);
  1197 		ProgrTex:= Surface2Tex(texsurf, false);
       
  1198 {$ENDIF}
       
  1199 		squaresize:= texsurf^.w shr 1;
       
  1200 		numsquares:= texsurf^.h div squaresize;
  1218 		SDL_FreeSurface(texsurf);
  1201 		SDL_FreeSurface(texsurf);
  1219 		squaresize:= ProgrTex^.w shr 1;
       
  1220 		numsquares:= ProgrTex^.h div squaresize;
       
  1221 	end;
  1202 	end;
  1222 
  1203 
  1223 TryDo(ProgrTex <> nil, 'ProgrTex = nil!', true);
  1204 {$IFDEF SDL13notworking}
  1224 
  1205 	TryDo(ProgrTex <> 0, 'Error - Progress Texure is 0!', true);
  1225 glClear(GL_COLOR_BUFFER_BIT);
  1206 {$ELSE}
  1226 glEnable(GL_TEXTURE_2D);
  1207 	TryDo(ProgrTex <> nil, 'Error - Progress Texure is nil!', true);
  1227 if Step < numsquares then r.x:= 0
  1208 {$ENDIF}
  1228 else r.x:= squaresize;
  1209 
  1229 r.y:= (Step mod numsquares) * squaresize;
  1210 	glClear(GL_COLOR_BUFFER_BIT);
  1230 r.w:= squaresize;
  1211 	glEnable(GL_TEXTURE_2D);
  1231 r.h:= squaresize;
  1212 	if Step < numsquares then r.x:= 0
  1232 DrawFromRect( -squaresize div 2, (cScreenHeight - squaresize) shr 1, @r, ProgrTex);
  1213 	else r.x:= squaresize;
  1233 glDisable(GL_TEXTURE_2D);
  1214 	
  1234 SDL_GL_SwapBuffers();
  1215 	r.y:= (Step mod numsquares) * squaresize;
  1235 inc(Step);
  1216 	r.w:= squaresize;
       
  1217 	r.h:= squaresize;
       
  1218 	
       
  1219 {$IFDEF SDL13notworking}
       
  1220 	SDL_RenderCopy(ProgrTex, nil, @r);
       
  1221 {$ELSE}	
       
  1222 	DrawFromRect( -squaresize div 2, (cScreenHeight - squaresize) shr 1, @r, ProgrTex);
       
  1223 {$ENDIF}
       
  1224 	glDisable(GL_TEXTURE_2D);
       
  1225 	SDL_GL_SwapBuffers();
       
  1226 	inc(Step);
  1236 end;
  1227 end;
  1237 
  1228 
  1238 
  1229 
  1239 procedure FinishProgress;
  1230 procedure FinishProgress;
  1240 begin
  1231 begin
  1241 WriteLnToConsole('Freeing progress surface... ');
  1232 	WriteLnToConsole('Freeing progress surface... ');
  1242 FreeTexture(ProgrTex);
  1233 {$IFDEF SDL13notworking}
  1243 ProgrTex:= nil;
  1234 	SDL_DestroyTexture(ProgrTex);
       
  1235 {$ELSE}
       
  1236 	FreeTexture(ProgrTex);
       
  1237 {$ENDIF}
  1244 
  1238 
  1245 {$IFDEF IPHONEOS}
  1239 {$IFDEF IPHONEOS}
  1246 // show overlay buttons
  1240 	// show overlay buttons
  1247 IPH_showControls;
  1241 	IPH_showControls;
  1248 {$ENDIF}
  1242 {$ENDIF}
  1249 
       
  1250 end;
  1243 end;
  1251 
  1244 
  1252 procedure flipSurface(Surface: PSDL_Surface; Vertical: Boolean);
  1245 procedure flipSurface(Surface: PSDL_Surface; Vertical: Boolean);
  1253 var y, x, i, j: LongInt;
  1246 var y, x, i, j: LongInt;
  1254     tmpPixel: Longword;
  1247     tmpPixel: Longword;