Skip to main content
GameDev.net gamedev.net
🔒 Locked

I want to see 'em sexy lines of code!

Started by ordered_disorder Sep 11, 2005 at 6:59 AM 35 replies 6.8k views
Original Post
ordered_disorder
ordered_disorder
What are you favorite code statements in your current project. Favorite being completely reletive, please explain why you chose it and what it does. Language doesn't matter. Here's mine!

			Vector interpolated_pos 
		    = *((*gameworld->PolygonEntityList_i)->m_Body->prev_vertex_pos) 
	        + (*((*gameworld->PolygonEntityList_i)->m_Body->vertex_pos) - 
			   (*(*gameworld->PolygonEntityList_i)->m_Body->prev_vertex_pos))*PercentTillNextLogicLoop;
			

I like this piece of code because 1) It shows the the encapsulation levels of my game and how it's logically nested and organized. The Game World knows all the entities that are in it. Each entity knows what body/model represents it, and finally each body knows where it is. 2) It shows off my ub3r noob3r interpolated rendering which means while I'm waiting for my logic loop to queue up, I can render frames of animation between my previous and final frame, making the game look very pretty!11onetwo 3) I love pointers to pointers to pointers add infinitum.
The Rug
The Rug
No single line of code can show off the sheer (lack of) brilliance in my project. For that, you'd have to look at the moth-eaten scraps of paper I call a design document.
the rug - funpowered.com
vNistelrooy
vNistelrooy
	ALIGN(16) matT matcol0[4];	ALIGN(16) matT matcol1[4];	ALIGN(16) matT matcol2[4];	ALIGN(16) matT matcol3[4];	ALIGN(16) matT m0[4];	ALIGN(16) matT m1[4];	ALIGN(16) matT m2[4];	ALIGN(16) matT m3[4];	__asm	{		mov		eax,DWORD PTR [this]		prefetcht1 BYTE PTR [matcol0]		// Matrix transpose		movlps	xmm1,QWORD PTR [eax]		movlps	xmm3,QWORD PTR [eax+8]		movlps	xmm5,QWORD PTR [eax+32]		movlps	xmm4,QWORD PTR [eax+40]		movhps	xmm1,QWORD PTR [eax+16]		movhps	xmm3,QWORD PTR [eax+24]		movhps	xmm5,QWORD PTR [eax+48]		movhps	xmm4,QWORD PTR [eax+56]		movaps	xmm0,xmm1		movaps	xmm2,xmm3		prefetcht1 BYTE PTR [matcol1]		prefetcht1 BYTE PTR [matcol3]		shufps	xmm1,xmm5,0ddh		shufps	xmm3,xmm4,0ddh		shufps	xmm0,xmm5,088h		shufps	xmm2,xmm4,088h		shufps	xmm1,xmm1,04eh		shufps	xmm3,xmm3,04eh		movaps	XMMWORD PTR [matcol0],xmm0		movaps	XMMWORD PTR [matcol2],xmm2		movaps	XMMWORD PTR [matcol1],xmm1		movaps	XMMWORD PTR [matcol3],xmm3		// Cofactors calculation		movaps	xmm4,xmm2		movaps	xmm5,xmm2		movaps	xmm6,xmm1		movaps	xmm7,xmm0		mulps	xmm4,xmm3		mulps	xmm5,xmm3		shufps	xmm4,xmm4,0b1h		shufps	xmm5,xmm5,01bh		mulps	xmm1,xmm4		mulps	xmm0,xmm4		mulps	xmm6,xmm5		mulps	xmm7,xmm5		subps	xmm6,xmm1		subps	xmm7,xmm0		prefetcht1 BYTE PTR [m0]		prefetcht1 BYTE PTR [m1]		shufps	xmm7,xmm7,04eh		movaps	XMMWORD PTR [m0],xmm6		movaps	XMMWORD PTR [m1],xmm7		movaps	xmm1,XMMWORD PTR [matcol1]		movaps	xmm5,XMMWORD PTR [matcol1]		movaps	xmm4,xmm2		mulps	xmm1,xmm2		mulps	xmm5,xmm2		movaps	xmm6,xmm3		shufps	xmm1,xmm1,0b1h		shufps	xmm5,xmm5,01bh		movaps	xmm7,XMMWORD PTR [m0]		mulps	xmm3,xmm1		mulps	xmm6,xmm5		addps	xmm7,xmm3		movaps	xmm0,XMMWORD PTR [matcol0]		movaps	xmm2,XMMWORD PTR [matcol0]		subps	xmm7,xmm6		mulps	xmm0,xmm1		mulps	xmm2,xmm5		prefetcht1 BYTE PTR [m0]		prefetcht1 BYTE PTR [m3]		prefetcht1 BYTE PTR [matcol1]		subps	xmm2,xmm0		prefetcht1 BYTE PTR [matcol2]		prefetcht1 BYTE PTR [matcol3]		prefetcht1 BYTE PTR [matcol0]		shufps	xmm2,xmm2,04eh		movaps	XMMWORD PTR [m0],xmm7		movaps	XMMWORD PTR [m3],xmm2		movaps	xmm1,XMMWORD PTR [matcol1]		movaps	xmm2,XMMWORD PTR [matcol2]		movaps	xmm3,XMMWORD PTR [matcol3]		shufps	xmm1,xmm1,04eh		movaps	xmm0,XMMWORD PTR [matcol0]		movaps	xmm4,xmm2		mulps	xmm1,xmm3		movaps	xmm5,xmm7		shufps	xmm4,xmm4,04eh		shufps	xmm1,xmm1,0b1h		movaps	xmm2,xmm4		movaps	xmm6,xmm0		mulps	xmm4,xmm1		mulps	xmm6,xmm1		movaps	XMMWORD PTR [matcol2],xmm2		addps	xmm5,xmm4		shufps	xmm1,xmm1,04eh		mulps	xmm2,xmm1		mulps	xmm0,xmm1		subps	xmm5,xmm2		subps	xmm0,xmm6		prefetcht1 BYTE PTR [m0]		prefetcht1 BYTE PTR [m2]		shufps	xmm0,xmm0,04eh		movaps	XMMWORD PTR [m0],xmm5		movaps	XMMWORD PTR [m2],xmm0		movaps	xmm5,xmm0		movaps	xmm1,XMMWORD PTR [matcol1]		movaps	xmm7,XMMWORD PTR [matcol1]		movaps	xmm0,XMMWORD PTR [matcol0]		movaps	xmm2,XMMWORD PTR [matcol2]		mulps	xmm1,xmm0		mulps	xmm7,xmm0		movaps	xmm4,xmm3		movaps	xmm6,XMMWORD PTR [m3]		shufps	xmm1,xmm1,0b1h		shufps	xmm7,xmm7,01bh		mulps	xmm4,xmm1		mulps	xmm2,xmm1		addps	xmm5,xmm4		subps	xmm2,xmm6		mulps	xmm3,xmm7		mulps	xmm7,XMMWORD PTR [matcol2]		subps	xmm3,xmm5		prefetcht1 BYTE PTR [m2]		prefetcht1 BYTE PTR [m3]		subps	xmm2,xmm7		movaps	XMMWORD PTR [m2],xmm3		movaps	XMMWORD PTR [m3],xmm2				movaps	xmm1,XMMWORD PTR [matcol3]		movaps	xmm7,XMMWORD PTR [matcol3]		movaps	xmm2,XMMWORD PTR [matcol2]		movaps	xmm6,XMMWORD PTR [matcol1]		mulps	xmm1,xmm0		mulps	xmm7,xmm0		movaps	xmm5,XMMWORD PTR [m1]		movaps	xmm4,xmm2		shufps	xmm1,xmm1,0b1h		shufps	xmm7,xmm7,01bh		mulps	xmm4,xmm1		mulps	xmm6,xmm1		subps	xmm5,xmm4		addps	xmm3,xmm6		mulps	xmm2,xmm7		mulps	xmm7,XMMWORD PTR [matcol1]		addps	xmm5,xmm2		prefetcht1 BYTE PTR [m1]		prefetcht1 BYTE PTR [m2]		subps	xmm3,xmm7		movaps	XMMWORD PTR [m1],xmm5		movaps	XMMWORD PTR [m2],xmm3				movaps	xmm1,XMMWORD PTR [matcol2]		movaps	xmm7,XMMWORD PTR [matcol2]		movaps	xmm3,XMMWORD PTR [matcol3]		movaps	xmm6,XMMWORD PTR [matcol1]		mulps	xmm1,xmm0		mulps	xmm7,xmm0		movaps	xmm4,xmm3		shufps	xmm1,xmm1,0b1h		shufps	xmm7,xmm7,01bh		movaps	xmm2,XMMWORD PTR [m3]		mulps	xmm4,xmm1		mulps	xmm6,xmm1		mulps	xmm3,xmm7		addps	xmm5,xmm4		subps	xmm2,xmm6		subps	xmm5,xmm3		mulps	xmm7,XMMWORD PTR [matcol1]		prefetcht1 BYTE PTR [m0]		prefetcht1 BYTE PTR [m1]		prefetcht1 BYTE PTR [m3]		addps	xmm2,xmm7		movaps	XMMWORD PTR [m1],xmm5		movaps	XMMWORD PTR [m3],xmm2		// Approximate the determinant and its reciprocal		movaps	xmm6,xmm0		movaps	xmm7,xmm0		movaps	xmm1,XMMWORD PTR [m0]		movaps	xmm4,xmm2		mulps	xmm0,xmm1		mulps	xmm6,xmm1		mulps	xmm7,xmm1		movaps	xmm2,xmm5		shufps	xmm0,xmm0,04eh		shufps	xmm7,xmm7,04eh		addps	xmm0,xmm6		addss	xmm7,xmm6		shufps	xmm0,xmm0,0b1h		addss	xmm0,xmm7		rcpss	xmm5,xmm0		movss	xmm6,xmm5		addss	xmm5,xmm5		movaps	xmm3,XMMWORD PTR [m2]		mulss	xmm6,xmm6		mulss	xmm0,xmm6		subss	xmm5,xmm0		prefetcht1 BYTE PTR [eax]		shufps	xmm5,xmm5,0h		prefetcht1 BYTE PTR [eax+16]		prefetcht1 BYTE PTR [eax+32]		prefetcht1 BYTE PTR [eax+48]		mulps	xmm1,xmm5		mulps	xmm2,xmm5		mulps	xmm3,xmm5		mulps	xmm4,xmm5		movaps	XMMWORD PTR [eax],xmm1		movaps	XMMWORD PTR [eax+16],xmm2		movaps	XMMWORD PTR [eax+32],xmm3		movaps	XMMWORD PTR [eax+48],xmm4	}



Yeah matrix inverse is hard work. <400 cycles.
"C lets you shoot yourself in the foot rather easily. C++ allows you to reuse the bullet!"
Scet
Scet
inline void ARM_BRANCH(){Opcode.Operation=bool(Opcode.Value&0x08000000);if(Opcode.Operation){Opcode.Immediate=Opcode.Value&0x00FFFFFF;if(Opcode.Immediate&0x800000){Opcode.Immediate|=0xFF000000;}Opcode.Immediate<<=2;Opcode.Immediate+=4;}else{Opcode.Rn=Opcode.Value&0x0000000F;}return;}inline void ARM_B(){Register[15]+=signed(Opcode.Immediate);}inline void ARM_BL(){Register[14]=Register[15]+4;Register[15]+=signed(Opcode.Immediate);}inline void ARM_BX(){Thumb=bool(Register[Opcode.Rn]&0x00000001);if(Thumb){Register[15]=Register[Opcode.Rn]-4;Register[15]&=0xFFFFFFFE;}else{Register[15]=Register[Opcode.Rn];Register[15]&=0xFFFFFFFC;}}


The first one is the decoder function, it gathers all the information from the opcode needed for a branch function. The other three are the actual branch functions.
Diodor
Diodor
My favorite lines are the assert statements that practically hold the project together by themselves.
Xiachunyi
Xiachunyi
start	movlw 	b'000001'	;Move 00001 to working register	movwf	PORTA		;Move contents of the working register to PORTA - Define which ones are active	call	delay		;External Function - Call Delay Function	movlw 	b'000010'	;Move 00010 to working regiser	movwf	PORTA		;Move contents of the working register to PORTA - Define which ones are active	call	delay		;External Function - Call Delay Function	goto	start		;Go back to "start" label


Back and forth on/off between pins 1 and 2 of a PIC, in this case for LEDs, is always fun.
Endar
Endar
I've been looking for the code for about 15 mins, but can't find it.

I remember that it was a single line of code that returned true or false, based on whether a file was opened and a valid FILE* returned from fopen. The catch is that if the the wasn't opened, it opened a file stream to a default file and then that was used as the basis for a valid FILE* check.

It was something like that. I remember that it stretched the length of the screen and had nested ?: statements.
[size="2"][size=2]Mort, Duke of Sto Helit: NON TIMETIS MESSOR -- Don't Fear The Reaper
klems
klems
return 0;
GnuVince
GnuVince
@@services = {    :rubyurl => Service.new("rubyurl.com") { |s|      s.code = 302      s.method = :get      s.action = "/rubyurl/create"      s.field = "rubyurl[website_url]"      s.block = lambda { |body| URI.extract(body)[0].gsub("rubyurl/show/", "") }    },        :tinyurl => Service.new("tinyurl.com") { |s|      s.action = "/create.php"      s.block = lambda { |body| URI.extract(body).grep(/tinyurl/)[-1] }    },        :shorl => Service.new("shorl.com") { |s|      s.action = "/create.php"      s.block = lambda { |body| URI.extract(body)[2] }    },    :snipurl => Service.new("snipurl.com") { |s|      s.action = "/index.php"      s.field = "link"      s.block = lambda { |body|        line = body.split("\n").grep(/txt/)[0]        short_url = URI.extract(line)[1][0..-2] # Remove trailing '      }    }}


I like how I created a small domain-specific language for POSTing web forms and retrieving data from the response. That's Ruby code by the way.
ajones
ajones
    template<typename T> struct type_policy    {   typedef typename policy_checker<T,BINDING::simple_type<T>::supported>::type type;    };


Selects an appropriate type for my automated C++ -> lua binding system.

policy_checker consists of a few pages of partial template specialisations that basically map various versions of a given type to its const or non-const reference type (i.e. some form of T -> [const] T&).

The type selected by type_policy will form part of a template-driven proxy object that sets/gets (set only available to non-const objects) a C++ bound value and forms part of a lua trampoline function.
kSquared
kSquared
Some source code from Metalyzer's Bayesian bagging subsystem. Comments that don't actually appear in the code have a '*' at the beginning. (You can read more about Bayesian bagging in this recent paper.)




namespace kSquared.Metalyzer.AnalysisEngine{  // * An online analysis engine is one that doesn't need in advance all the data  // * it will be operating on.  /// <summary>  /// A Bayesian bagger performs Bayesian bagging (short for "ootstrap [agg]regat[ing]")  /// on a volatile set of input data samples X = {x1, ..., xm}. The samples are all  /// required to be <see cref="IToken"/> instances. It is an ensemble learning  /// method that improves on space consumption and processing speed at the cost of  /// nonuniform <see cref="PoissonWeight"/>s for the resulting distribution.  /// </summary>  /// <remarks>  /// __TODO: [ Finish. References to Russell (2001) and Rubin (1981) papers? ]  /// </remarks>  public class BayesianBag<T> : IOnlineAnalysisEngine where T : IToken  {     // ...     // __TODO: Missing documentation.     public void ProcessNext<T>(T[] tokens)     {       this.Tokens.PushRange(tokens);       this.SignalProcessing();     }     // ...     private void BagNext<T>(T[] tokens)     {       OrderedSet<T> poissonWeights = this.Computer.GeneratePoissonDistribution<T>(tokens, this.KnownDistribution.GetElements());       OrderedSet<T> gammaRandoms = this.Computer.GenerateGammaRandoms<T>(tokens, this.GammaPrngParameters);       try       {         OrderedSet<Pair<T,T>> result = OrderedSet.Associate(poissonWeights, gammaRandoms);       }       catch (ArgumentException e)       {         // At least one piece of invalid data. Discard data.         result = new OrderedSet(Math.Max(poissonWeights.Size, gammaRandoms.Size));       }       Bag b = this.GenerateOrdering(result);       for (int i = 0; i < poissonWeights.Size; ++i)       {         foreach (T token in tokens)         {           double poissonWeight = result.Arg2 * this.Computer.GenerateGammaRandom<T>(token);           // Force sum to 1.           b.RenormalizeWeights(poissonWeight + 1);           // Update results.           result.Arg1 = b.Normalize(b.PoissonParameter * poissonWeight);           this.BayesianPredictor = b.Predictor;         }       }     }     // ...  }}
- k2 "Choose a job you love, and you'll never have to work a day in your life." — Confucius"Logic will get you from A to B. Imagination will get you everywhere." — Albert Einstein"Money is the most egalitarian force in society. It confers power on whoever holds it." — Roger Starr{General Programming Forum FAQ<
Promit
Promit
Quote:
Original post by vNistelrooy
*** Source Snippet Removed ***

Yeah matrix inverse is hard work. <400 cycles.


You lose. Intel does it in a snappy 210 cycles.
SlimDX | Ventspace Blog | Twitter | Diverse teams make better games. I am currently hiring capable C++ engine developers in Baltimore, MD.
vNistelrooy
vNistelrooy
Quote:
Original post by Promit
Quote:
Original post by vNistelrooy
*** Source Snippet Removed ***

Yeah matrix inverse is hard work. <400 cycles.


You lose. Intel does it in a snappy 210 cycles.


My code is based on Intel's method. My less than 400 cycles were benched on a Prescott, while Intel used a P3, a completly different arch. Hell, give me 10 minutes and I will benchmark on a P3.
"C lets you shoot yourself in the foot rather easily. C++ allows you to reuse the bullet!"
Mercury
Mercury
void trimPath(char *path, char **filename){	strcpy(*filename, path);	*filename = &(filename[0][strlen(*filename)-1]);	while (**filename != '\\') (*filename)--;	(*filename)++;	for (int i=strlen(*filename)-1; i>0; i--)		if ((*filename) == '.')		{			(*filename) = '\0';			break;		}}


Don't whine to me that that isn't "safe". It does what it needs to for now and I'll rewrite it when I feel like it.
vNistelrooy
vNistelrooy
Screw that. I haven't got the DirectX 9.0c there.
"C lets you shoot yourself in the foot rather easily. C++ allows you to reuse the bullet!"
superdeveloper
superdeveloper
Quote:
Original post by vNistelrooy
*** Source Snippet Removed ***


Yeah matrix inverse is hard work. <400 cycles.


Is that an affine matrix inversion? (i.e. a matrix with no sheering, scaling)
Benjamin Heath
Benjamin Heath
This is the start of a lua script that runs my game. An application, in this instance, is an object that does a little event-handling. You can also start it and stop it with appropriate methods. A window is another object that handles some graphics and window management. The underlying program is written in C with SDL.

local gstate = {normal = 0}local game = {	app = GSL_Application.new(),	window = GSL_Window.new(),	state = gstate.normal}game.app:start()game.window:setmode {width=640, height=480}game.window:setcaption {title="GSL Test App"}while game.app:running() do	game.window:update ()	-- test for given states	if game.state = gstate.normal then		-- do normal stuff	else		-- invalid state, i suppose. time to stop		game.app:stop()	endend
Ilici
Ilici
I don't have any ugly code, nor any fancy stuff.

Here's something though:
float Angle(float theta1, float phi1, float theta2, float phi2){	float cospsi = Sin(theta1) * Sin(theta2) * Cos(phi2 - phi1) + Cos(theta1) * Cos(theta2);  	if (cospsi > 1)  return 0;  	if (cospsi < -1) return PI;  	float ac = acos(cospsi);	return ac;	}


Angle between two vectors given in spherical coordinates.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.