Gamestudio Links
Zorro Links
Newest Posts
Help with plotting multiple ZigZag
by degenerate_762. 04/30/24 23:23
M1 Oversampling
by 11honza11. 04/30/24 08:16
Trading Journey
by howardR. 04/28/24 09:55
Zorro Trader GPT
by TipmyPip. 04/27/24 13:50
Data from CSV not parsed correctly
by jcl. 04/26/24 11:18
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
3 registered members (AndrewAMD, Quad, M_D), 1,217 guests, and 1 spider.
Key: Admin, Global Mod, Mod
Newest Members
firatv, wandaluciaia, Mega_Rod, EternallyCurious, howardR
19050 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Page 1 of 6 1 2 3 4 5 6
What's (currently) your favourite snippet of code? #444492
08/12/14 19:17
08/12/14 19:17
Joined: Nov 2008
Posts: 946
T
the_clown Offline OP
User
the_clown  Offline OP
User
T

Joined: Nov 2008
Posts: 946
Given that Acknex is often called an engine for programmers, I guess a lot of you here actually are programmers, so quick and dirty, what's currently your favourite snippet of code that does something cool with only a few lines? May be any language, any context, as long as the length/effect ratio is cool in any way.

Oh and if you feel like it, do NOT tell us what it actually does, instead let us have a guess. If it's not too hard. wink

I'll start off with my current favourite in C++ - this should be rather easy to guess:

Code:
template <class T>
static T& Instance(){
   static T t;
   return t;
}


Re: What's (currently) your favourite snippet of code? [Re: the_clown] #444493
08/12/14 19:23
08/12/14 19:23
Joined: Apr 2007
Posts: 3,751
Canada
WretchedSid Offline
Expert
WretchedSid  Offline
Expert

Joined: Apr 2007
Posts: 3,751
Canada
Singleton. Breaks horribly when part of a library and is used in an compilation unit that generates code linked into another binary/library. Source: Raynes singleton system breaking horribly on Windows.

My favourite. Should be easy to guess, but can someone point out the beauty of this snippet?
Code:
_titleLabel->SetFrame([&]() -> RN::Rect {
	RN::Rect rect = _titleLabel->GetFrame();
	
	rect.x = 5.0f;
	rect.y = 5.0f;
	
	return rect;
}());



Shitlord by trade and passion. Graphics programmer at Laminar Research.
I write blog posts at feresignum.com
Re: What's (currently) your favourite snippet of code? [Re: WretchedSid] #444494
08/12/14 19:39
08/12/14 19:39
Joined: Mar 2011
Posts: 3,150
Budapest
sivan Offline
Expert
sivan  Offline
Expert

Joined: Mar 2011
Posts: 3,150
Budapest
probably my 1st program in Basic for C64. totally useless, sorry grin
Code:
10 PRINT "A"
20 GOTO 10



Free world editor for 3D Gamestudio: MapBuilder Editor
Re: What's (currently) your favourite snippet of code? [Re: WretchedSid] #444495
08/12/14 19:43
08/12/14 19:43
Joined: Nov 2008
Posts: 946
T
the_clown Offline OP
User
the_clown  Offline OP
User
T

Joined: Nov 2008
Posts: 946
Originally Posted By: WretchedSid
Singleton. Breaks horribly when part of a library and is used in an compilation unit that generates code linked into another binary/library. Source: Raynes singleton system breaking horribly on Windows.


Oh it does? Good to know, how exactly does it break?

Your snippet sets the size of a label, or the label's frame, to 5x5 I guess, by basically making a copy of the existing frame, modifying the properties of that copy and then setting it as new frame for the label?

Re: What's (currently) your favourite snippet of code? [Re: the_clown] #444496
08/12/14 20:18
08/12/14 20:18
Joined: Apr 2007
Posts: 3,751
Canada
WretchedSid Offline
Expert
WretchedSid  Offline
Expert

Joined: Apr 2007
Posts: 3,751
Canada
Originally Posted By: the_clown
Oh it does? Good to know, how exactly does it break?


Assuming you have a library and a normal application, and both use this to create a singleton of type "Foo". The library and application will then both have their very own instance that is not shared. Normally the linker is responsible for getting rid of the multiple template instantiations and to make them into one, however, the linker doesn't work cross libraries, so the application and library both have their very own instances of the code and there of "Foo".

Originally Posted By: the_clown
Your snippet sets the size of a label, or the label's frame, to 5x5 I guess, by basically making a copy of the existing frame, modifying the properties of that copy and then setting it as new frame for the label?

Well, kind of. It only sets the origin and doesn't touch the size, but that's not the beauty of the snippet. Want to take another guess or should I say it?


Shitlord by trade and passion. Graphics programmer at Laminar Research.
I write blog posts at feresignum.com
Re: What's (currently) your favourite snippet of code? [Re: WretchedSid] #444497
08/12/14 20:33
08/12/14 20:33
Joined: Nov 2008
Posts: 946
T
the_clown Offline OP
User
the_clown  Offline OP
User
T

Joined: Nov 2008
Posts: 946
Ah, so the problem here is the fact that it's a templated method?
Interesting pitfall.

Hmm I could imagine it's the fact you're doing the whole thing with a single function call using C++'s Lambda feature? At least I think that's quite a nice thing.

Re: What's (currently) your favourite snippet of code? [Re: the_clown] #444498
08/12/14 20:42
08/12/14 20:42
Joined: Apr 2007
Posts: 3,751
Canada
WretchedSid Offline
Expert
WretchedSid  Offline
Expert

Joined: Apr 2007
Posts: 3,751
Canada
Originally Posted By: the_clown
Ah, so the problem here is the fact that it's a templated method?
Interesting pitfall.

Kind of, but it wouldn't work without templates either. If you declare it static, you would get an instantiation per compilation unit that makes use of it. Without static, your linker would complain about duplicated symbols. Because it's a template, the linker will collapse the multiple instantiations down to one.

Originally Posted By: the_clown
Hmm I could imagine it's the fact you're doing the whole thing with a single function call using C++'s Lambda feature? At least I think that's quite a nice thing.

Getting close. The real nice thing is that it doesn't pollute the outer scope with "rect" and makes the whole thing look nicer (imho anyways). Here is the context: https://github.com/uberpixel/Downpour/bl...w.cpp#L169-L175

It really is just a much nicer way of writing this:
Code:
RN::Rect rect = _titleLabel->GetFrame();

rect.x = 5.0f;
rect.y = 5.0f;

_titleLabel->SetFrame(rect);

// Why does rect have to still be visible here?!



I sue this kind of sparingly and only when it makes sense to limit the scope and visibility of variables.

Edit: Are we allowed multiple posts? I know one can only have _one_ favourite, but what about other really neat snippets?

Last edited by WretchedSid; 08/12/14 20:52.

Shitlord by trade and passion. Graphics programmer at Laminar Research.
I write blog posts at feresignum.com
Re: What's (currently) your favourite snippet of code? [Re: WretchedSid] #444499
08/12/14 21:04
08/12/14 21:04
Joined: Nov 2008
Posts: 946
T
the_clown Offline OP
User
the_clown  Offline OP
User
T

Joined: Nov 2008
Posts: 946
Originally Posted By: WretchedSid

Kind of, but it wouldn't work without templates either. If you declare it static, you would get an instantiation per compilation unit that makes use of it. Without static, your linker would complain about duplicated symbols. Because it's a template, the linker will collapse the multiple instantiations down to one.


Hmm I'm trying to make sure I get you right here (even if that collapses the thread miles into off-topic, but as it's my thread I guess I can do that), you say if I made this a non-templated method and did something like this
Code:
static Singleton& Instance(){
       static Singleton s;
       return s;
}



that would break when used in a library, too?

EDIT: And you're welcome to post more than one snippet.

Last edited by the_clown; 08/12/14 21:05.
Re: What's (currently) your favourite snippet of code? [Re: the_clown] #444502
08/12/14 22:07
08/12/14 22:07
Joined: Apr 2007
Posts: 3,751
Canada
WretchedSid Offline
Expert
WretchedSid  Offline
Expert

Joined: Apr 2007
Posts: 3,751
Canada
Originally Posted By: the_clown
that would break when used in a library, too?

It would just break. Static functions are only visible to the current compilation unit and aren't shared. You can test this yourself by returning the pointer to the static variable and printing that in the callee, then do this from two compilation units and link it all together.

Here is my second snippet. It also uses static variables, and it's a tad longer.
Code:
class Initializer
{
public:
	typedef void (*Callback)();
	
	Initializer(Callback ctor, Callback dtor) :
		_dtor(dtor)
	{
		if(ctor) ctor();
	}
	
	~Initializer()
	{
		if(_dtor) _dtor();
	}
	
private:
	Callback _dtor;
};

#define RN_REGISTER_INITIALIZER(name, body) \
	namespace { \
		static void __RNGlobalInit##name##Callback() { body; } \
		static RN::Initializer __RNGlobalInit##name (__RNGlobalInit##name##Callback, nullptr); \
	}
#define RN_REGISTER_DESTRUCTOR(name, body) \
	namespace { \
		static void __RNGlobalDestructor##name##Callback() { body; } \
		static RN::Initializer __RNGlobalDestructor##name (nullptr, __RNGlobalDestructor##name##Callback); \
	}



Shitlord by trade and passion. Graphics programmer at Laminar Research.
I write blog posts at feresignum.com
Re: What's (currently) your favourite snippet of code? [Re: WretchedSid] #444504
08/12/14 22:27
08/12/14 22:27
Joined: Sep 2003
Posts: 6,861
Kiel (Germany)
Superku Offline
Senior Expert
Superku  Offline
Senior Expert

Joined: Sep 2003
Posts: 6,861
Kiel (Germany)
Code:
my.pan = your.pan + random(360);



"Falls das Resultat nicht einfach nur dermassen gut aussieht, sollten Sie nochmal von vorn anfangen..." - Manual

Check out my new game: Pogostuck: Rage With Your Friends
Page 1 of 6 1 2 3 4 5 6

Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1