Save much CPU time by initializing vertex arrays in texture creation function
authorunc0rr
Sun, 22 Mar 2009 21:03:59 +0000
changeset 1912 c3d31fb59f0e
parent 1911 6283bd8a960b
child 1913 73d2da2df3c2
Save much CPU time by initializing vertex arrays in texture creation function
hedgewars/uConsts.pas
hedgewars/uMisc.pas
hedgewars/uStore.pas
--- a/hedgewars/uConsts.pas	Sun Mar 22 20:56:22 2009 +0000
+++ b/hedgewars/uConsts.pas	Sun Mar 22 21:03:59 2009 +0000
@@ -120,16 +120,19 @@
 			Pos: LongWord;
 			AmmoType: TAmmoType;
 			end;
+
+	TVertex2f = record
+		X, Y: GLfloat;
+		end;
+			
 	TTexture = record
 			id: GLuint;
 			w, h: LongInt;
 			rx, ry: GLfloat;
+			vb, tb: array [0..3] of TVertex2f;
 			end;
 	PTexture = ^TTexture;
 
-	TVertex2f = record
-		X, Y: GLfloat;
-		end;
 const
 	errmsgCreateSurface   = 'Error creating SDL surface';
 	errmsgTransparentSet  = 'Error setting transparent color';
--- a/hedgewars/uMisc.pas	Sun Mar 22 20:56:22 2009 +0000
+++ b/hedgewars/uMisc.pas	Sun Mar 22 21:03:59 2009 +0000
@@ -290,6 +290,30 @@
 while (toPowerOf2 < i) do toPowerOf2:= toPowerOf2 shl 1
 end;
 
+procedure ResetVertexArrays(texture: PTexture);
+begin
+with texture^ do
+	begin
+	vb[0].X:= 0;
+	vb[0].Y:= 0;
+	vb[1].X:= w;
+	vb[1].Y:= 0;
+	vb[2].X:= w;
+	vb[2].Y:= h;
+	vb[3].X:= 0;
+	vb[3].Y:= h;
+
+	tb[0].X:= 0;
+	tb[0].Y:= 0;
+	tb[1].X:= rx;
+	tb[1].Y:= 0;
+	tb[2].X:= rx;
+	tb[2].Y:= ry;
+	tb[3].X:= 0;
+	tb[3].Y:= ry
+	end;
+end;
+
 function NewTexture(width, height: Longword; buf: Pointer): PTexture;
 begin
 new(NewTexture);
@@ -298,6 +322,8 @@
 NewTexture^.rx:= 1.0;
 NewTexture^.ry:= 1.0;
 
+ResetVertexArrays(NewTexture);
+
 glGenTextures(1, @NewTexture^.id);
 
 glBindTexture(GL_TEXTURE_2D, NewTexture^.id);
@@ -415,8 +441,10 @@
 	glTexImage2D(GL_TEXTURE_2D, 0, mode, surf^.w, surf^.h, 0, mode, GL_UNSIGNED_BYTE, surf^.pixels);
 	end;
 
+ResetVertexArrays(Surface2Tex);
+
 if SDL_MustLock(surf) then
-   SDL_UnlockSurface(surf);
+	SDL_UnlockSurface(surf);
 
 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
--- a/hedgewars/uStore.pas	Sun Mar 22 20:56:22 2009 +0000
+++ b/hedgewars/uStore.pas	Sun Mar 22 21:03:59 2009 +0000
@@ -348,37 +348,18 @@
 end;
 
 procedure DrawTexture(X, Y: LongInt; Texture: PTexture);
-var VertexBuffer, TextureBuffer: array [0..3] of TVertex2f;
 begin
 glPushMatrix;
 glTranslatef(X, Y, 0);
 
 glBindTexture(GL_TEXTURE_2D, Texture^.id);
 
-VertexBuffer[0].X:= 0;
-VertexBuffer[0].Y:= 0;
-VertexBuffer[1].X:= Texture^.w;
-VertexBuffer[1].Y:= 0;
-VertexBuffer[2].X:= Texture^.w;
-VertexBuffer[2].Y:= Texture^.h;
-VertexBuffer[3].X:= 0;
-VertexBuffer[3].Y:= Texture^.h;
-
-TextureBuffer[0].X:= 0;
-TextureBuffer[0].Y:= 0;
-TextureBuffer[1].X:= Texture^.rx;
-TextureBuffer[1].Y:= 0;
-TextureBuffer[2].X:= Texture^.rx;
-TextureBuffer[2].Y:= Texture^.ry;
-TextureBuffer[3].X:= 0;
-TextureBuffer[3].Y:= Texture^.ry;
-
 glEnableClientState(GL_VERTEX_ARRAY);
 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
 
-glVertexPointer(2, GL_FLOAT, 0, @VertexBuffer[0]);
-glTexCoordPointer(2, GL_FLOAT, 0, @TextureBuffer[0]);
-glDrawArrays(GL_TRIANGLE_FAN, 0, Length(VertexBuffer));
+glVertexPointer(2, GL_FLOAT, 0, @Texture^.vb);
+glTexCoordPointer(2, GL_FLOAT, 0, @Texture^.tb);
+glDrawArrays(GL_TRIANGLE_FAN, 0, Length(Texture^.vb));
 
 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
 glDisableClientState(GL_VERTEX_ARRAY);