Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411485 Posts in 69371 Topics- by 58427 Members - Latest Member: shelton786

April 24, 2024, 07:12:43 AM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsDeveloperTechnical (Moderator: ThemsAllTook)The happy programmer room
Pages: 1 ... 153 154 [155] 156 157 ... 279
Print
Author Topic: The happy programmer room  (Read 678300 times)
ThemsAllTook
Administrator
Level 10
******



View Profile WWW
« Reply #3080 on: December 24, 2012, 04:05:30 PM »

I didnt think the parser would let you put the minusminus right next to the bracket like that

This is valid, too:

Code:
int a = 1, b = 2, c;
c = a+++++b;
// a == 2, b == 3, c == 4
Logged

impulse9
Guest
« Reply #3081 on: December 24, 2012, 04:16:27 PM »

Funniest thing in C:

Code:
int a[] = {10, 20, 30};
printf("%d %d", a[1], 1[a]); // outputs "20" twice

Explanation:

Code:
// the array subscript operator ( [] ) translates to *(a + b), from here we see that:
*(a + 1) == *(1 + a)
Logged
Quarry
Level 10
*****


View Profile
« Reply #3082 on: December 25, 2012, 02:36:25 AM »

Code:
Game.ply.moving = !(!(l ^ r) & !(u ^ d));

I made this crazy little thing that only moves you when you... idk how to explain it

You won't need an obfuscator it seems Tongue

It's shorter than many if conditiors, definately faster too

some meaningful names on those variables could help

Left right up and down for player, pretty much what it does is it prevents you from moving left and right or up and down at the same time but it also allows you to move up or down when you are holding both left and right at the same time and vice versa
Logged
nospoon
Level 1
*


View Profile
« Reply #3083 on: December 25, 2012, 03:13:15 AM »

This is awesome (works in java ):
Code:

int r;
while( (r=reader.read())!=-1){

}

Logged
InfiniteStateMachine
Level 10
*****



View Profile
« Reply #3084 on: December 25, 2012, 03:37:54 AM »

Code:
Game.ply.moving = !(!(l ^ r) & !(u ^ d));

I made this crazy little thing that only moves you when you... idk how to explain it

You won't need an obfuscator it seems Tongue

It's shorter than many if conditiors, definately faster too

some meaningful names on those variables could help

Left right up and down for player, pretty much what it does is it prevents you from moving left and right or up and down at the same time but it also allows you to move up or down when you are holding both left and right at the same time and vice versa

ah sweet! that's pretty awesome
Logged

nospoon
Level 1
*


View Profile
« Reply #3085 on: December 25, 2012, 04:06:50 AM »

Code:
Game.ply.moving = !(!(l ^ r) & !(u ^ d));

I made this crazy little thing that only moves you when you... idk how to explain it

You won't need an obfuscator it seems Tongue

It's shorter than many if conditiors, definately faster too

some meaningful names on those variables could help

Left right up and down for player, pretty much what it does is it prevents you from moving left and right or up and down at the same time but it also allows you to move up or down when you are holding both left and right at the same time and vice versa

ah sweet! that's pretty awesome

I might be stupid but
Code:
Game.ply.moving = !(!(l ^ r) & !(u ^ d));
shouldn't it be :
Code:
Game.ply.moving = ((l ^ r) & (u ^ d));
Like the negations kinda do nothing.
Logged
rivon
Level 10
*****



View Profile
« Reply #3086 on: December 25, 2012, 06:53:01 AM »

nospoon: very close but not correct. It should be this:
Code:
Game.ply.moving = (l ^ r) | (u ^ d);
The reason is that the negation of cunjunction is disjunction of negated propositions. Or in one of the logical notations:
Code:
_______      _   _
(A ^ B) <=> (A v B)

edit: or in simple AND/OR pseudocode:
Code:
!(a AND b) == ((!a) OR (!b))
Logged
Quarry
Level 10
*****


View Profile
« Reply #3087 on: December 25, 2012, 08:53:20 AM »

nospoon: very close but not correct. It should be this:
Code:
Game.ply.moving = (l ^ r) | (u ^ d);
The reason is that the negation of cunjunction is disjunction of negated propositions. Or in one of the logical notations:
Code:
_______      _   _
(A ^ B) <=> (A v B)

edit: or in simple AND/OR pseudocode:
Code:
!(a AND b) == ((!a) OR (!b))

I forgot everything about logic and the version I posted was simply trial and arror, thanks a lot!

Logged
Sergi
Level 1
*



View Profile WWW
« Reply #3088 on: December 25, 2012, 09:20:40 AM »

So you're using a mix of bitwise and boolean operators there? Is that guaranteed to work properly everywhere?
Logged

Quarry
Level 10
*****


View Profile
« Reply #3089 on: December 25, 2012, 09:33:00 AM »

I'm not exactly sure if every language allows that, Java allows me to use both bitwise and boolean ops for booleans
Logged
rivon
Level 10
*****



View Profile
« Reply #3090 on: December 25, 2012, 09:48:58 AM »

My version uses bitwise operators only so it should work properly everywhere.
Logged
Sergi
Level 1
*



View Profile WWW
« Reply #3091 on: December 25, 2012, 10:36:16 AM »

Oh, okay, I was thinking C. I think using a chain of bitwise operators in a "boolean way" is not safe in C, but it's not the case, so nevermind Smiley
Logged

ThemsAllTook
Administrator
Level 10
******



View Profile WWW
« Reply #3092 on: December 30, 2012, 01:06:26 PM »

I have a very specific audio processing need: I record Minecraft LPs on Windows (game video and audio) and voice commentary on a Mac, and for whatever reason, the voice recording comes out just slightly faster than the game audio recording. Over the course of an hour-long recording session, the difference adds up to noticeable desync by the end. I calculated the amount that I needed to slow down the voice track to get them to line up (0.99996879577636719), and tried to do it with an audio editor... The problem is, most audio editors don't seem to give me enough precision to do it properly. Audacity truncates my percent change to three decimal places, which isn't enough. Amadeus Pro can sort of do it, but I couldn't get it to change pitch in sync with tempo, which caused a bit of artifacting when it stretches the audio to match the slower speed.

So, I decided to try writing a tiny C utility to do it for me. Turned out to be very short and simple, and quite speedy. On my laptop, this can process 10 minutes of audio in just under a second. The result is exactly the right speed and artifact free (nearest neighbor filtering isn't noticeable at all at 48KHz). Here's the program in its entirety:

Code:
#include <stdio.h>
#include <stdlib.h>

#define SPEED_MULTIPLIER 0.99996879577636719

int main(int argc, char ** argv) {
size_t originalLength, scaledLength;
unsigned int sampleIndex;
double sampleFraction;
char wavHeader[44];
char sample[2];
const double speedMultiplierFraction = 1.0 / SPEED_MULTIPLIER - 1.0;

fseek(stdin, 0, SEEK_END);
originalLength = (ftell(stdin) & ~1) - 44;
fseek(stdin, 0, SEEK_SET);
fread(wavHeader, 1, 44, stdin);
scaledLength = originalLength / SPEED_MULTIPLIER;
wavHeader[40] = scaledLength & 0xFF;
wavHeader[41] = scaledLength >> 8 & 0xFF;
wavHeader[42] = scaledLength >> 16 & 0xFF;
wavHeader[43] = scaledLength >> 24 & 0xFF;
fwrite(wavHeader, 1, 44, stdout);
sampleFraction = 0.0;
for (sampleIndex = 0; sampleIndex < originalLength; sampleIndex += 2) {
sample[0] = getchar();
sample[1] = getchar();
putchar(sample[0]);
putchar(sample[1]);
sampleFraction += speedMultiplierFraction;
if (sampleFraction >= 1.0) {
putchar(sample[0]);
putchar(sample[1]);
sampleFraction -= 1.0;
}
}
return EXIT_SUCCESS;
}

It must be tough to be doing creative work without the knowledge to do stuff like this. If your tools can't do what you need them to, you're pretty much stuck. I absolutely love knowing how to code stuff like this and being able to solve such things so simply and cleanly.
Logged

d
Level 0
***


View Profile
« Reply #3093 on: December 30, 2012, 02:43:12 PM »

Oh no, not the wavX API.

Not again.

Never again.


Never mind, I just noticed you only used a similar naming scheme.
« Last Edit: December 30, 2012, 02:48:30 PM by d » Logged
Moczan
Guest
« Reply #3094 on: December 31, 2012, 06:40:48 AM »

I have a very specific audio processing need: I record Minecraft LPs on Windows (game video and audio) and voice commentary on a Mac, and for whatever reason, the voice recording comes out just slightly faster than the game audio recording.
[...] I absolutely love knowing how to code stuff like this and being able to solve such things so simply and cleanly.

Couldn't you just sync the voice with video in your video editing software? That would be simple and clean  Tongue

Logged
ThemsAllTook
Administrator
Level 10
******



View Profile WWW
« Reply #3095 on: December 31, 2012, 10:45:02 AM »

Couldn't you just sync the voice with video in your video editing software? That would be simple and clean  Tongue

Possibly. Up until recently, I edited with QuickTime Pro, which is extremely basic and doesn't give me any facilities for that. I bought Sony Vegas last week though, and haven't completely gotten to know it yet. I'll have to look around and see if it has a way to stretch the audio track by just a tiny bit. It's hard to beat the amount of effort it takes now that my stretch utility is written, though; record, save WAV file, run one command to stretch it, and import into Vegas. Nice and easy.
Logged

d
Level 0
***


View Profile
« Reply #3096 on: January 01, 2013, 03:10:02 PM »

Removed a forgotten

Code:
Sleep(5);

Frame rate jumped from 130 to 700.
Logged
Sergi
Level 1
*



View Profile WWW
« Reply #3097 on: January 01, 2013, 03:58:21 PM »

Removed a forgotten

Code:
Sleep(5);

Frame rate jumped from 130 to 700.

Awesome, that sounds like it would be the same feeling as when you find money in the pockets of old clothing Smiley You feel like you won something, but it was yours to begin with Big Laff
Logged

_Tommo_
Level 8
***


frn frn frn


View Profile WWW
« Reply #3098 on: January 02, 2013, 06:33:15 PM »

I'm thinking about redoing my gameplay code around C++11 lambdas, and the concept is already so clean that makes me an happy programmer Big Laff

Code:

class Player : public State
{

public:

class Idle : public State
{
public:

Idle( const Player& self )
{
event( "begin", [=]
{
self.setAnimation( "idle" );
self.speed.x = 0;
});

event( "update", [=]
{
self.setHorizontalSpeed();

if( !self.isOnGround() )
return new Falling( self ); //returning a new state means that this substate will be replaced with the new one! (in the parent)
else if( self.speed.x ) //states should be set only via return to ensure than when the current state is deleted
return new Walking( self );     //this one isn't still executing
});

event( "keyPressed", [=]
{
const KeyEvent& e = (const KeyEvent&)this->getEventData();
if( e.key == JUMP_KEY )
return new Jump( self );
} )
}
};

class Walking : public State
{
public:
Walking( const Player & self )
{
event( "begin", [=]
{
self.setAnimation( "walk" );
});

event( "update", [=]
{
if( !self.isOnGround() )
return new Falling( self ); //jumped out of a ledge!

self.setHorizontalSpeed();

if( self.speed.x == 0 )
return new Idle( self );
});

event( "keyPressed", [=]
{
const KeyEvent& e = (const KeyEvent&)this->getEventData();
if( e.key == JUMP_KEY )
return new Jumping( self );
} )
}
};

class Jumping() : public State
{
public:

float boostTime = 0.1;

Jumping( const Player & self )
{
event( "begin", [=]
{
self.setAnimation( "jump" );
self.speed.y = JUMP_SPEED;

Platform::getSingleton().getSound()->playSound( "playerJumped" );
});

event( "update" [=, &boostTime ]
{
const & UpdateEvent e = (const UpdateEvent&)this->getEventData();

//keep going up if JUMP is still pressed, for 0.1 s (like super mario)
if( boostTime > 0 && Platform::getSingleton().getInput()->isKeyDown( JUMP_KEY ) )
self.speed.y = JUMP_SPEED;
else
self.speed.y -= GRAVITY * e.dt;

//be able to move
self.setHorizontalSpeed();

boostTime -= e.dt;

if( self.speed.y <= 0 ) //start falling
return new Falling( self  );
});
}
}

class Falling() : public State
{
public:

float killTime = 1; //DIE if more than this elapses!

Jumping( const Player & self )
{
event( "begin", [=]
{
self.setAnimation( "fall" );
});

event( "update" [=, &killTime ]
{
const & UpdateEvent e = (const UpdateEvent&)this->getEventData();

//keep going up if JUMP is still pressed, for 0.1 s (like super mario)
self.speed.y -= GRAVITY * e.dt;

//be able to move
self.setHorizontalSpeed();

killTime -= e.dt;

if( self.isOnGround() )
{
if( killTime > 0 )
return self.speed.x != 0 ? new Walking( self ) : new Idle( self ); //hit the ground running?
else
return new Dying( self );
}
});

event( "end", [=]
{
Platform::getSingleton().getSound()->playSound( "playerLanded" );
});
}
}

//... other states

Player( ... )
{
setState( new Idle( *this ) ); //sets a sub state and destroys the old one, if existing
}

void setHorizontalSpeed()
{
InputSystem* i = Platform::getSingleton().getInput();
if( i->isKeyDown( KEY_RIGHT )
self.speed.x = -WALK_SPEED;
else if( i->isKeyDown( KEY_LEFT ) )
self.speed.x = WALK_SPEED;

self.scale.x = sign( self.speed.x );
}


};


///--------- STATE STUFF --------

///override this one to handle unhandled messages etc - default behaviour is to crash or log an error (release)
virtual void onUnhandledEvent( const String& event, EventData* eventData )
{
DEBUG_FAIL( "Unhandled message: " + event);
}

virtual State* _callback( const EventHandler& h, EventData* eventData)
{
mEventData = eventData; //set event data this way because to use h( eventData ) we would need to add it to each single event's definition
mNextState = h();
mEventData = NULL;
}

//only override this if really needed - originally in State class
virtual bool _dispatch( const String& event, EventData* eventData )
{
//dispatch to substate/delegate if found
bool absorbed = false;
if( mSubState )
absorbed = mSubState->_dispatch( event );

//the substate failed to manage this event, see if we have a fallback
if( !absorbed )
{
HandlerMap::iterator handler = mHandleMap.find( event );
if( absorbed = (handler != mHandleMap.end() ) )
_callback( handler.second, eventData );
}

return absorbed;
}

//this is called externally
bool dispatchEvent( const String& event, EventData* eventData )
{
if( !_dispatch( event, eventData ) )
onUnhandledEvent( event, eventData );
}


this way you get to manage each state indipendently from the others, without any kind of annoying switch or if/else list  Beer!
And you can broadcast/forward messages really easily!

still the "self" everywhere annoys me a bit, how could I kill it?
« Last Edit: January 02, 2013, 06:44:49 PM by _Tommo_ » Logged

Maikel_Ortega
Level 1
*


...and the stars, my destination.


View Profile WWW
« Reply #3099 on: January 03, 2013, 12:32:02 AM »

A few day's ago I decided to give GameMaker a try.
Yesterday I tried it and I liked it.
Today there's a 66% sale of GM Professional.


 Beer!
Logged

Melee Studios:http://melee-studios.com<br />Forever WIP (ESP devlog): http://maikel.londite.es/devlog/
Pages: 1 ... 153 154 [155] 156 157 ... 279
Print
Jump to:  

Theme orange-lt created by panic