Gamestudio Links
Zorro Links
Newest Posts
zorro 64bit command line support
by 7th_zorro. 04/20/24 10:06
StartWeek not working as it should
by jcl. 04/20/24 08:38
Data from CSV not parsed correctly
by jcl. 04/20/24 08:32
Zorro FIX plugin - Experimental
by jcl. 04/20/24 08:30
folder management functions
by VoroneTZ. 04/17/24 06:52
lookback setting performance issue
by 7th_zorro. 04/16/24 03:08
Zorro FIX plugin - Experimental
by flink. 04/14/24 07:46
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
3 registered members (7th_zorro, Aku_Aku, 1 invisible), 579 guests, and 1 spider.
Key: Admin, Global Mod, Mod
Newest Members
EternallyCurious, howardR, 11honza11, ccorrea, sakolin
19047 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Page 2 of 6 1 2 3 4 5 6
Re: What's (currently) your favourite snippet of code? [Re: Superku] #444509
08/13/14 03:28
08/13/14 03:28
Joined: Mar 2006
Posts: 2,252
Hummel Offline
Expert
Hummel  Offline
Expert

Joined: Mar 2006
Posts: 2,252
Code:
titleLabel.Frame = new Rect(5f, 5f);// C# style


Last edited by Hummel; 08/13/14 15:36.
Re: What's (currently) your favourite snippet of code? [Re: Hummel] #444512
08/13/14 06:56
08/13/14 06:56
Joined: Apr 2007
Posts: 3,751
Canada
WretchedSid Offline
Expert
WretchedSid  Offline
Expert

Joined: Apr 2007
Posts: 3,751
Canada
I feel like this is missing the width and height, no? It's not like you can't just assign things in C++ (you could actually do it by value :p), but according to the specs, the C# rect also has an origin and a size. Where's the size in your example coming from?


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] #444533
08/13/14 15:33
08/13/14 15:33
Joined: Mar 2006
Posts: 2,252
Hummel Offline
Expert
Hummel  Offline
Expert

Joined: Mar 2006
Posts: 2,252
dunno. getter/setter magic? there is a C#-rect?

Last edited by Hummel; 08/13/14 15:37.
Re: What's (currently) your favourite snippet of code? [Re: Hummel] #444538
08/13/14 17:06
08/13/14 17:06
Joined: Apr 2007
Posts: 3,751
Canada
WretchedSid Offline
Expert
WretchedSid  Offline
Expert

Joined: Apr 2007
Posts: 3,751
Canada
So basically you are saying that you pulled that snippet out of your ass without understanding what mine does?
It's not too hard to write a really cool snippet with LINQ that actually demonstrates some cool C# stuff. Just saying.

Come on, some one impress me with C#s expression engine and the lazy evaluation of it. Go!


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] #444539
08/13/14 17:11
08/13/14 17:11
Joined: Mar 2006
Posts: 2,252
Hummel Offline
Expert
Hummel  Offline
Expert

Joined: Mar 2006
Posts: 2,252
The pro has spoken. Srsly...

Re: What's (currently) your favourite snippet of code? [Re: Hummel] #444540
08/13/14 17:41
08/13/14 17:41
Joined: Apr 2007
Posts: 3,751
Canada
WretchedSid Offline
Expert
WretchedSid  Offline
Expert

Joined: Apr 2007
Posts: 3,751
Canada
Not this again...


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] #444541
08/13/14 17:52
08/13/14 17:52
Joined: Aug 2003
Posts: 7,439
Red Dwarf
Michael_Schwarz Offline
Senior Expert
Michael_Schwarz  Offline
Senior Expert

Joined: Aug 2003
Posts: 7,439
Red Dwarf
Back on topic, I am sure this could have been done much, MUCH better. But since I am really terrible at maths, this is how I avoided loops for calculating the "index" (i.e. the n'th time it happened) of a weekly recurring appointment for a calendar app in javascript:

Code:
// Does it occur on target date?
if ((this.pattern.weekdays & p.date.get.dotw(h.target)))
{
    // How many week has it been since this appointment started?
    var weeksSinceStart = p.date.get.weeksBetween(h.current, h.target);

    // Skip this week?
    if (weeksSinceStart % this.pattern.periodicity)
    {
        return null;
    }

    // How many blind occurrences are there?
    // e.g. appointment starts on a WED but the pattern is MO/_WED_/TH/SU = 1 blind (MO)
    var blind_occurrences = 0;

    // Avoid loop with clever switch
    switch ((this.pattern.weekdays & p.date.get.dotw(this.start)))
    {
        // INTENTIONAL FALL-THROUGHS!!! //                                                                                                                    
        case FLAG_SUNDAY:
            (this.pattern.weekdays & FLAG_SATURDAY) && blind_occurrences++;
        case FLAG_SATURDAY:
            (this.pattern.weekdays & FLAG_FRIDAY) && blind_occurrences++;
        case FLAG_FRIDAY:
            (this.pattern.weekdays & FLAG_THURSDAY) && blind_occurrences++;
        case FLAG_THURSDAY:
            (this.pattern.weekdays & FLAG_WEDNESDAY) && blind_occurrences++;
        case FLAG_WEDNESDAY:
            (this.pattern.weekdays & FLAG_TUESDAY) && blind_occurrences++;
        case FLAG_TUESDAY:
            (this.pattern.weekdays & FLAG_MONDAY) && blind_occurrences++;
    }

    // How often has it already occurred *this* week?
    var occurrenceIndexThisWeek = -blind_occurrences;

    // Avoid loop with clever switch
    switch ((this.pattern.weekdays & p.date.get.dotw(h.target)))
    {
        // INTENTIONAL FALL-THROUGHS!!! //                                                                                                                     
        case FLAG_SUNDAY:
            (this.pattern.weekdays & FLAG_SATURDAY) && occurrenceIndexThisWeek++;
        case FLAG_SATURDAY:
            (this.pattern.weekdays & FLAG_FRIDAY) && occurrenceIndexThisWeek++;
        case FLAG_FRIDAY:
            (this.pattern.weekdays & FLAG_THURSDAY) && occurrenceIndexThisWeek++;
        case FLAG_THURSDAY:
            (this.pattern.weekdays & FLAG_WEDNESDAY) && occurrenceIndexThisWeek++;
        case FLAG_WEDNESDAY:
            (this.pattern.weekdays & FLAG_TUESDAY) && occurrenceIndexThisWeek++;
        case FLAG_TUESDAY:
            (this.pattern.weekdays & FLAG_MONDAY) && occurrenceIndexThisWeek++;
    }

    // Set index
    var index = (weeksSinceStart * this.occurrencesPerWeek / this.pattern.periodicity) + occurrenceIndexThisWeek;


Last edited by Michael_Schwarz; 08/13/14 17:53.

"Sometimes JCL reminds me of Notch, but more competent" ~ Kiyaku
Re: What's (currently) your favourite snippet of code? [Re: Michael_Schwarz] #444542
08/13/14 17:59
08/13/14 17:59
Joined: Apr 2005
Posts: 4,506
Germany
F
fogman Offline
Expert
fogman  Offline
Expert
F

Joined: Apr 2005
Posts: 4,506
Germany
using System.Collections.Generic;


no science involved
Re: What's (currently) your favourite snippet of code? [Re: fogman] #444544
08/13/14 18:20
08/13/14 18:20
Joined: Nov 2008
Posts: 946
T
the_clown Offline OP
User
the_clown  Offline OP
User
T

Joined: Nov 2008
Posts: 946
@Sid, am I right to assume that the problem with statics only occurs with the use of shared libraries, but not with the use of static libraries?
Also, I have no clue what your second snippet does. tongue

@Michael, I think I see what you did there, nice workaround.
@fog, that's not a snippet. tongue

Re: What's (currently) your favourite snippet of code? [Re: the_clown] #444548
08/13/14 21:29
08/13/14 21:29
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
@Sid, am I right to assume that the problem with statics only occurs with the use of shared libraries, but not with the use of static libraries?

Yes.

Originally Posted By: the_clown
Also, I have no clue what your second snippet does. tongue

It provides a way to execute some code at startup or exit. The macros wrap a static object that is instantiated at startup (or when the library is loaded), and which is destroyed at the end, and depending on which macro is used, it calls a callback in either the constructor or destructor. It exploits the automatic object lifetime in C++.

Similar, a scope guard, which can be used for automatic clean up (more code snippet):
Code:
namespace RN
{
	class ScopeGuard
	{
	public:
		template<typename F>
		explicit ScopeGuard(F &&rollback) :
			_committed(false),
			_rollback(std::move(rollback))
		{}
		
		ScopeGuard(ScopeGuard &&other) :
			_committed(other._committed),
			_rollback(std::move(other._rollback))
		{}
		
		ScopeGuard &operator=(ScopeGuard &&other)
		{
			_rollback = std::move(other._rollback);
			_committed = other._committed;
			
			return *this;
		}
		
		~ScopeGuard()
		{
			if(!_committed)
				_rollback();
		}
		
		
		void Commit()
		{
			_committed = true;
		}
		
	private:
		bool _committed;
		Function _rollback;
	};
}




Example usage:
Code:
void foo()
{
	FILE *file = fopen(...);

	RN::ScopeGuard guard([&]() {
		fclose(file); // Close the file descriptor
	});

	bar(file); // In this case, bar can throw an exception. 
	// Either way, exception or normal exit, once the scope guard goes out of scope, the file is closed.
}



PS: Don't use "FILE" in C++ tongue


Shitlord by trade and passion. Graphics programmer at Laminar Research.
I write blog posts at feresignum.com
Page 2 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