Jump to content

  • Log In with Google      Sign In   
  • Create Account


l0calh05t

Member Since 16 Dec 2000
Offline Last Active Jul 09 2014 11:54 PM
-----

#5158078 Metal API .... whait what

Posted by l0calh05t on 04 June 2014 - 06:17 AM

 

Metal's C++ based shader language is an interesting idea. Generics/templates might be fun.

I don't think the shader langauge is c++ based. I never seen a double square bracket in c++. Looks more like object c to me

 

Like these? http://en.wikipedia.org/wiki/C++11#Attributes




#5138675 Ugh! Your shorthand is too short!

Posted by l0calh05t on 13 March 2014 - 06:23 AM


For instance, an electronic engineer will know that the formula for calculating a charging voltage of a capacitor in an RC circuit is:

U = U0 * (1-e^(-t/(R*C)))



So in code, I will reflect exactly that:

double U = U0 * 1-pow(e, 0.0-t/(R*C));

Every non-electronic engineer is going to look at that and go "WTF IS THIS CRAP".

 

The electrical engineer might wonder though why you wrote 0.0-t/(R*C) instead of just -t/(R*C) (is -0.0 an issue for the pow function you are using?). And the computer scientist might wonder why you are using pow(e,x) instead of exp(x) which is usually faster. And both might notice you forgot an opening parenthesis wink.png In any case it wouldn't hurt anyone to write

 

double voltage = starting_voltage * (1. - exp(-time/(resistance * capacitance)));

 

Instead, as then both the electrical engineer and the computer scientist would probably have a rough idea what its about, at the cost of only a few keystrokes (which tend to be fairly cheap wink.png).




#5138671 Industrial Strength Hash Table

Posted by l0calh05t on 13 March 2014 - 06:11 AM


Using braces instead of brackets for trying to call the constructor of an object

 

That's actually valid in C++11 and called uniform initialization syntax.




#5136040 Using a vector of base classes with CRTP

Posted by l0calh05t on 03 March 2014 - 03:35 AM

template<typename T>
std::vector<T> collection;
^^^ You can't have a templated variable like that.
That's saying, that when someone tries to use 'collection', instantiate a version of it for each T that they use... You use templates like that on functions/classes, but not variables.
e.g.

C++14 will add template variables (see http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3651.pdf)




#5127466 Poker monster

Posted by l0calh05t on 30 January 2014 - 09:06 AM

Yuck. So many named non-iterble things. So much code repetition. Heres an almost 20x shorter Python solution:

from collections import Counter

with open('poker.txt', 'r') as f:
	hands = [hand.strip() for hand in f]
	
values = [chr(ord('2')+k) for k in range(8)] + ['T', 'J', 'Q', 'K', 'A']
ace = values.index('A')
hands = [hand.split(' ') for hand in hands]

hand_values = [[values.index(card[0]) for card in hand] for hand in hands]
hand_suits = [[card[1] for card in hand] for hand in hands]

hand_values = [(hand[:5], hand[5:]) for hand in hand_values]
hand_suits = [(hand[:5], hand[5:]) for hand in hand_suits]

wins = [0, 0]
ties = 0
for vs, ss in zip(hand_values, hand_suits):
	rankings = []
	for v, s in zip(vs, ss):
		v = sorted(v, key=lambda x: [Counter(v)[x],x]) # bugfix
		straight = all(a + 1 == b for a, b in zip(v, v[1:]))
		flush = s.count(s[0]) == len(s)
		counts = Counter(Counter(v).values())
		if straight and flush and v[-1] == ace: rank = 9 # royal flush
		elif straight and flush: rank = 8 # straight flush
		elif counts[4] == 1: rank = 7 # four of a kind
		elif counts[3] == 1 and counts[2] == 1: rank = 6 # full house
		elif flush: rank = 5 # flush
		elif straight: rank = 4 # straight
		elif counts[3] == 1: rank = 3 # three of a kind
		elif counts[2] == 2: rank = 2 # two pairs
		elif counts[2] == 1: rank = 1 # one pair
		else: rank = 0 # high card
		rankings.append((rank, v[::-1]))
	winning_hand = max(rankings)
	if rankings.count(winning_hand) == 1:
		wins[rankings.index(winning_hand)] += 1
	else:
		ties += 1

print wins
print ties

EDIT: added one line of bugfixing (for card multiples, the multiple cards weren't properly used for decision before the high cards)...




#5123520 Using C++11 smart pointers for asset manager

Posted by l0calh05t on 14 January 2014 - 03:51 AM

Some time ago I wrote an asset manager based on boost smart pointers (essentially the same as C++11 smart pointers), so yes, it works. My approach was a little different though, as my manager only had weak pointers and the users had shared pointers, so as soon as there were no more users of a shared resource, it would be deallocated (the unused weak pointers would be discarded when a new resource is allocated).

 

@samoth: The advantage of a manager in this case is to prevent all kinds of objects loading the same resource again and again. The objects it manages however should behave in such a way that the destructor deallocates the resource.




#5107832 Stutter / Micro Stutter Even w/ VSync

Posted by l0calh05t on 08 November 2013 - 02:55 AM


Is it also expected to have the frame sometimes take 5ms and sometimes .3ms? I am checking now if triple buffering is enabled, but I do know that double is, as that is the SDL_SwapBuffers() command.

 

Absolutely. There might be a context switch and scheduler time slices are often quite long. Also, as tonemgub points out, you should really only be calling now() once per frame. (directly after swap is usually a good choice)




#5106871 The simplest of tasks...

Posted by l0calh05t on 04 November 2013 - 02:12 AM

 


verboseoscopy

My new favourite phrase.

 

It sounds almost as unpleasant as a colonoscopy (unnecessary over-use of semi-colons).

 

The code could really use a verbosectomy.




#5098123 Yiddish indentation

Posted by l0calh05t on 01 October 2013 - 01:40 PM

If you are interested in the topic, this is one of the better (and simpler) explanations: http://www.iamcal.com/understanding-bidirectional-text/

 

And there's an xkcd comic about it http://xkcd.com/1137/ :D




#5096389 This beauty...

Posted by l0calh05t on 24 September 2013 - 06:04 AM

 

I actually find that the original version is more easily understood than doing it with loops tongue.png

DXT3AlphaBlock compressDXT3Alpha(vec4 colors[16])
{
	DXT3AlphaBlock dxt3Alpha;

	for( int j=0; j!=2; ++j )
	{
		unsigned int result = 0;
		for( int i=7; i>=0; --i )
		{
			unsigned int quantized = quantize4(int(colors[i+j*8].w * 255.0 + .5));
			result = result<<2 | quantized;
		}
		dxt3Alpha.alphas[j] = result;
	}

	return dxt3Alpha;
}

Shouldn't that result be shifted by 4, not 2? Also, why not do a forward loop and shift quantized instead of result?

for(unsigned j = 0; j < 8; ++j)
{
	unsigned int quantized = quantize4(int(colors[i+j*8].w * 255.0 + .5));
	result |= quantized << (4 * j);
}

Most people would consider it more readable and the multiply will be optimized away during unrolling anyways. Personally I'd also write the reverse loop as (should mention though that some compilers don't manage to unroll this, but the specific compiler I'm thinking of is unable to unroll both types of reverse loop, so...)

for(unsigned j = 8; j-- != 0;) // or even for(unsigned j = 8; j--;)



#5088410 Proper use of VAO

Posted by l0calh05t on 23 August 2013 - 10:45 AM


vertex array objects are an entirely optional superstructure to organize buffers

 

This is not true for core OpenGL. See for example http://www.opengl.org/wiki/GLAPI/glVertexAttribPointer which states "GL_INVALID_OPERATION​ is generated if no vertex array object is bound."




#5084364 Easy-to-use Version Control on Windows? Needs to be able to easily ignore cer...

Posted by l0calh05t on 09 August 2013 - 05:03 AM


I've looked at Perforce but it's not exactly affordable for me, and it uses a different system than what I'm used to (I want Checkout/Update/Commit).

 

Isn't Perforce free for less than 20 users nowadays?




#5084228 Duplicated Code

Posted by l0calh05t on 08 August 2013 - 02:10 PM

Are those form booleans mutually exclusive? In that case it would be better to use an int to represent the current form. And if they are not, an array would probably lead to further reductions in code repetition (don't name something that is actually numbered like first, second, third... or x, y, z... use an array instead)




#5083480 [WIP] Tyler - Tilebased Map Editor

Posted by l0calh05t on 06 August 2013 - 02:45 AM

How about something like this for saving ? http://updates.html5rocks.com/2011/08/Saving-generated-files-on-the-client-side




#5082417 List of C++11 compliant compilers?

Posted by l0calh05t on 02 August 2013 - 01:55 AM


OP, have you tried the VS2013 preview? It supports more of C++11 -- indeed all, or nearly all, of the stuff you'd actually care to use. Again, its just a preview, but the real-deal is just around the corner.

 

Far from it. Implicit move generation is pretty important, and ref qualifiers are very helpful for correctness. And no proper constexpr. And no user defined literals. All delayed to post VC2013 CTPs (so not for production code). The saddest part? On the far right (lowest priority/farthest in the future) there are still two C++98 bullet points. Admittedly not that important but that standard is now 15 years old, and Visual Studio still isn't there yet, and I suspect that C++11/14 will take just as long.

 

Visual Studio is lagging behind extremely.

 

About Mingw, I usually get my MinGW from http://sourceforge.net/projects/mingwbuilds/ those are very up to date.






PARTNERS