Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 463 464 [465] 466 467 ... 796

Author Topic: if self.isCoder(): post() #Programming Thread  (Read 887791 times)

Mephisto

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #6960 on: February 04, 2015, 09:09:11 am »

The tricks that game devs came up with to go fast never cease to impress me.

It's probably been posted before, but this is a good read.

http://www.gamasutra.com/view/feature/194772/dirty_game_development_tricks.php
Logged

Rose

  • Bay Watcher
  • Resident Elf
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #6961 on: February 05, 2015, 12:44:57 am »

Today I learned something.

SomeGenericClass<int> and SomeGenericClass<string> don't share static variables between them.

I don't know why I thought they would.
Logged

i2amroy

  • Bay Watcher
  • Cats, ruling the world one dwarf at a time
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #6962 on: February 05, 2015, 01:43:30 am »

It seems that every day I learn about one more very useful thing in C++ that they decided not to allow to be used in Java for some reason (default parameter values I'm looking at you! >:().
Logged
Quote from: PTTG
It would be brutally difficult and probably won't work. In other words, it's absolutely dwarven!
Cataclysm: Dark Days Ahead - A fun zombie survival rougelike that I'm dev-ing for.

Reelya

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #6963 on: February 05, 2015, 01:54:53 am »

Can you simulate that by having two forms of the function with different numbers of parameters?

Take

//c++
int foo(int x, int y =3)
{
  // do stuff
}

and change it to:

// java
int foo(int x) { foo(x,3); };
int foo(int x, int y)
{
  // do stuff
}

that should give you the exact benefits of the default parameter.

Rose

  • Bay Watcher
  • Resident Elf
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #6964 on: February 05, 2015, 01:58:15 am »

the java devs basically decided that overloading is evil.
Logged

alway

  • Bay Watcher
  • 🏳️‍⚧️
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #6965 on: February 05, 2015, 02:04:39 am »

If you need it faster, then you can pre-compute a table which converts the random output to normals, and do linear interpolation to go the precise value. Much faster than doing square roots, cos and logs every time.

Precomputing tables of values when the program starts, and using interpolation, is also good for quick and dirty cos, sqrts etc when you know the input will be in some pre-defined range. Much faster than using your languages math library functions every time, and can be made as accurate as you want without sacrificing speed (but sacrificing memory instead).

Even better if you can trick your inputs to be ints. For example, i read that in Wolfenstein and Doom they don't use 360 degree circles or radian-based circles. They define a circle with 256 "degrees". Then any angle can be expressed in exactly 1 unsigned byte, you don't need any bounds-checking when rotating, since if it goes over 255 it wraps around naturally. And they just have arrays of 256 pre-computed sin and cos values directly stuck in the code, so they can use a direct index lookup instead instead of needing computation or maths functions. In any game that you can look around but doesn't need super accuracy, using 256-degree circles has some really nice benefits.
Or in places where memory access like that is expensive, you can reduce them down to approximations. For a system with MAD instructions (Multiply-ADd) (GPUs mostly) which can do a multiply followed by an add for the cost of just doing a multiply, you can approximate y = 1/4sin(x) with:
T = (x * Pi_Inverse) - 1.0;
y = (T * abs(T)) - T;
Where you're using the cost of 2 multiplies to replace a sin. Alternatively, if you can rework your equation such that you need sin to have a domain of 0 to 2 instead of 0 to 2PI, you can rework that to take out the first MAD and replace it with just a subtract. This replacement holds for a full cycle of sin, starting at 0, with maximum error of something like 13% IIRC.
(other stuff about MAD optimizing of shaders)
http://www.humus.name/Articles/Persson_LowLevelThinking.pdf

Edit: Also, do what Thief says here:
Graknorke, what language are you using? If it's C++, the (new) C++11 random library has std::normal_distribution to do that kind of thing.

Incidentally, if you're still using rand() in C++ code, STOP. It's one of the worst random generators ever created. Do yourself a favour and learn the new C++ random library.
Assuming you're not doing GPU randomness (noise), use the random library for any sort of random numbers you plan to rely heavily on. rand() is good for prototyping and bashing crap out fast, but that's about it. Moreover, rand() is entirely non-deterministic cross-platform, so a procedural system would generate completely different results depending on who implemented the rand function (I have tested this; it is very different and doesn't even seem to have a consistent range to it).

Though, again, if you do GPU stuff, you've just walked into Mordor and we don't have none of those fancy elven libraries with their big memory footprints they live in or their delicate speech patterns here.
« Last Edit: February 05, 2015, 02:12:09 am by alway »
Logged

Reelya

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #6966 on: February 05, 2015, 02:06:31 am »

Java: no default parameters, no templates, no overloading of names. Yeah it does sound horrible.

Putnam

  • Bay Watcher
  • DAT WIZARD
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #6967 on: February 05, 2015, 02:12:57 am »

Hey, some would argue that C++ is horrible precisely because of all those things.

I wouldn't, but some would. It's more a problem with the developer than the language, though (the arguments are mainly that people somehow feel like they ought to use all the features of the language even if it would work just fine writing it as C with classes or somesuch)

Reelya

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #6968 on: February 05, 2015, 02:19:46 am »

overloading names prevents a blow-out in differently-named functions that are related but take different types of operand. You see it in OpenGL with names like glVertex3f. Oh, so you typed glVertex3s by accident? That'll cause undocumented behavior! There are 24 versions of glVertex all with different names, for each type of parameter list:

https://www.opengl.org/sdk/docs/man2/xhtml/glVertex.xml

Overloading allows you to define one name which is easy to remember, but a range of parameter types and have the correct version automatically selected but apply different code each time.
« Last Edit: February 05, 2015, 02:25:11 am by Reelya »
Logged

alway

  • Bay Watcher
  • 🏳️‍⚧️
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #6969 on: February 05, 2015, 02:28:37 am »

Also operator overloading. Or since I don't feel like typing it out again
Operator overloading: the feature that seems silly right up until you try to write your first vector library in a language without it.

It tends to go along the lines of this:
Code: [Select]
vector A, B, C;
...
// Right then; time to write some code using my vector math!
C = A.Multiply(A.Add(B)).Divide(B);
// Or wait, was that my in-place operation function, or one that returned a modified copy?
C = A.MultiplyCopy(A.AddCopy(B)).DivideCopy(B);
// I really hope I don't have to optimize this; I can barely tell what it's doing now.
...
// Some time later:
C.Multiply(A.MultiplyCopy(A.AddCopy(B)).DivideCopy(B).Subtract(B.AddCopy(C).MultiplyCopy(D)).MultiplyCopy(7.0f));
//  I think I'll just switch to a geography major and drink alcohol until I die
As opposed to
Code: [Select]
C *= (A*(A+B)/B) - ((B+C) * D) * 7.0f;
Which is quite the breath of fresh air when you switch back to a language which trusts you with a bottle of glue, rather than only giving you glue sticks.

In completely unrelated stuff, here's something fun and mildly horrific: http://blog.regehr.org/archives/767
"Undefined Behavior Consequences Contest Winners"
So some people had a little contest in which they were showcasing some of the weirdest 'undefined behavior' things in C++. Winner #2 I find particularly fun, as it involves a pair of pointers with the same value pointing at different data.
Quote
#include <stdio.h>
#include <stdlib.h>
 
int main() {
  int *p = (int*)malloc(sizeof(int));
  int *q = (int*)realloc(p, sizeof(int));
  *p = 1;
  *q = 2;
  if (p == q)
    printf("%d %d\n", *p, *q);
}
The output of this program using a recent version of Clang on Linux is apparently: 1 2
Testing it in Visual Studio release mode, it prints out: 1 2
Testing it in Visual Studio debug mode, it prints out: 2 2
In all cases, p == q is true, since they are outputting, but when you look closer by compiling in debug mode, p == q is apparently just a little more true than before.
;)
« Last Edit: February 05, 2015, 02:30:21 am by alway »
Logged

i2amroy

  • Bay Watcher
  • Cats, ruling the world one dwarf at a time
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #6970 on: February 05, 2015, 02:42:45 am »

Can you simulate that by having two forms of the function with different numbers of parameters?

Take

//c++
int foo(int x, int y =3)
{
  // do stuff
}

and change it to:

// java
int foo(int x) { foo(x,3); };
int foo(int x, int y)
{
  // do stuff
}

that should give you the exact benefits of the default parameter.
That's actually exactly what you have to do, but it means doubling (or tripling, or quadrupling) the amount of functions you need, each one to take an additional default parameter. (And don't even get me started on trying to pass back multiple values from a function :P).
Logged
Quote from: PTTG
It would be brutally difficult and probably won't work. In other words, it's absolutely dwarven!
Cataclysm: Dark Days Ahead - A fun zombie survival rougelike that I'm dev-ing for.

Reelya

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #6971 on: February 05, 2015, 02:49:32 am »

If there's a return, you just chain the return:

int foo(int x) { return foo(x,3); };
int foo(int x, int y)
{
  return stuff;
}

If you need to return multiple values, make a class for that and use it as the return type. If you need to modify the parameters, then just do pass by reference on all functions. It's not much more writing. It's literally 1 line of code per extra default parameter. There is no code overhead on the client-side, which is where it's important.

Orange Wizard

  • Bay Watcher
  • mou ii yo
    • View Profile
    • S M U G
Re: if self.isCoder(): post() #Programming Thread
« Reply #6972 on: February 05, 2015, 03:07:18 am »

I'm not so keen on overloading. myself, but that's more of a personal perfectionism thing. I can definitely see why it's desirable, though, and I have no idea why you'd remove it.
Logged
Please don't shitpost, it lowers the quality of discourse
Hard science is like a sword, and soft science is like fear. You can use both to equally powerful results, but even if your opponent disbelieve your stabs, they will still die.

i2amroy

  • Bay Watcher
  • Cats, ruling the world one dwarf at a time
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #6973 on: February 05, 2015, 11:34:45 am »

It's true that no particular thing is to trying to handle (as you have pointed out) it's more the fact that the sum of the whole is rather annoying when you keep stumbling on little thing after little thing that could be done so much easier. Also Java doesn't do pass by reference; everything is pass by value (some people say weird things about objects, but really what is happening is that the pointers are being passed by value). It's just a line here, a class there, but it all adds up in the end.
Logged
Quote from: PTTG
It would be brutally difficult and probably won't work. In other words, it's absolutely dwarven!
Cataclysm: Dark Days Ahead - A fun zombie survival rougelike that I'm dev-ing for.

miauw62

  • Bay Watcher
  • Every time you get ahead / it's just another hit
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #6974 on: February 05, 2015, 12:18:53 pm »

More BYOND implementation fun:
You cannot have more functions than 65,535 (unsigned short), at that point it seems to start reading memory that it shouldn't be reading.
Apparently we've hit that limit, and now everything is exploding, especially all lists.
Logged

Quote from: NW_Kohaku
they wouldn't be able to tell the difference between the raving confessions of a mass murdering cannibal from a recipe to bake a pie.
Knowing Belgium, everyone will vote for themselves out of mistrust for anyone else, and some kind of weird direct democracy coalition will need to be formed from 11 million or so individuals.
Pages: 1 ... 463 464 [465] 466 467 ... 796