Jump to content

  • Log In with Google      Sign In   
  • Create Account

Awesome job so far everyone! Please give us your feedback on how our article efforts are going. We still need more finished articles for our May contest theme: Remake the Classics

Nypyren

Member Since 19 Aug 2002
Offline Last Active Yesterday, 09:30 PM
*****

#5061001 How would you concatenate an array of strings together?

Posted by Nypyren on 11 May 2013 - 12:20 AM

If you need delimiters between each string (such as comma separating a list), there is string.Join.

 

If you want to run a function on each string before joining them, you can do:  string.Join(separator, yourArrayOrCollection.Select(x => Operation(x));

 

If you want any kind of fancier operation, a StringBuilder and a loop might be the best approach.




#5059894 Why am I getting this IndexOutOfRangeException?

Posted by Nypyren on 06 May 2013 - 09:21 PM

0 < this.Rows

 

row < this.Rows




#5057931 Rotating an object along a curve

Posted by Nypyren on 29 April 2013 - 06:33 PM

The matrix seems reasonable to me. Perhaps the derivative is wrong.

You could just approximate the derivative at the point by picking another point slightly further up the curve, take the difference, and use that for your x and y values in that matrix the same way. Perhaps this is what you're already trying?


#5054083 Why do games keep track of ticks?

Posted by Nypyren on 16 April 2013 - 10:17 PM

Because computers have processors with different processing speeds.

Really old games often didn't do any timing - they assumed the CPU was 33 MHz or whatever, and would run correctly at that speed. But if you installed one of those games on a much newer processor, say a 133 MHz, the game would run so fast that the player couldn't react quickly enough anymore, because the processor could do 4 times the work in the same amount of real-world time.

So, modern games use the system clock to try to keep the game the same no matter what processor the player owns. There are generally two approaches:

1. Each frame of the game is assumed to take X real-world milliseconds. Each subsystem of the game performs a slice of work corresponding to X milliseconds. The game loop then attempts to align the work performed to real-world time so that the game doesn't stutter. (Fixed timestep game)

2. Each frame of the game is processed as fast as possible, no waiting. The game measures the time the last frame took to process, and passes that 'time delta' value to the various subsystems for the current frame. The frame is then rendered immediately and the next frame begins without waiting. (Variable timestep game)


#5053903 What is the difference between these two namespaces?

Posted by Nypyren on 16 April 2013 - 11:12 AM

Anything inside of the namespace scope has access to anything in the namespace and its parent namespaces without needing "using" directives. That's about it.

In other words, if you have:
namespace System.IO
{
   // You can use any types from "System" or "System.IO" here without adding "using" lines to the file.
}
In your example, both namespaces would have access to "greatstuff". goodstuff and evenbetterstuff would still need usings to access the other, though.


(EDIT) By "access" I mean "use a type without fully qualifying its name". You can always just type out the full type name such as "System.IO.Stream" even without using directives.


#5051367 Handling multitouch in Windows (like for Surface Pro)

Posted by Nypyren on 08 April 2013 - 07:38 PM

I haven't used it, but it looks like there is a new message type with this information:

http://msdn.microsoft.com/en-us/library/windows/desktop/dd371406(v=vs.85).aspx


#5048094 What is functional programming and how can it help us in game developing?

Posted by Nypyren on 29 March 2013 - 01:37 PM

If you come from an imperative background, it's kind of hard to envision what functional techniques are good for unless you try using them.

I use C#, so the only functional stuff I really use are first class functions and lambdas. For me, the benefit of using these is reduction in the amount of state that I need to manually implement.

For example, let's say I want to filter a collection:

Imperative style:
public List<int> FindPositiveIntegers(List<int> inputs)
{
  List<int> values = new List<int>();

  foreach (int input in inputs)
    if (input > 0)
      values.Add(input);

  return values;
}
Functional style:
List<int> values = inputs.Where(x => x > 0).ToList();
In my opinion, the functional case's advantages include:

- The code is shorter, which usually means it's easier to maintain.

- The variable mutation is entirely hidden from the programmer by the LINQ methods. Reducing the number of variables that a programmer has to deal with also means that there are fewer *effective* states that the program can be in (this is a good thing - fewer states means it's easier to prove that it works correctly in those states).

- There is less manual flow control being performed by the programmer. Flow control is another kind of state that a programmer has to consider.

- I can more easily write a function which gets negative numbers instead; In both cases I can copy/paste the whole thing and just change the condition, but in the functional case it's easier because there is less code to maintain.

- The "Where" and "ToList" methods seen here are reusable. They've been proven to work correctly, so the only burden on the programmer is understanding what they do and writing the lambda expression "x => x > 0" inside the Where call. Allowing for reuse of code where *parts of the algorithm itself* are interchangeable is a consistent theme when using functional style.


TL;DR:

OOP encapsulates data, Functional style encapsulates code.


#5043135 C++ Programming - What IDE to use ?

Posted by Nypyren on 14 March 2013 - 01:47 PM

Some things I'm missing from Visual Studio that for example QtCreator has natively:
- Smart indention (copy/paste a piece of code into a scope with different indention and it automatically adds or remove tabs for you, rather then manually select all lines and press tab several times).
- Pointer recognition (press . after a variable that is a pointer and it is replaced with -> automatically).
- Automatic closing quotes/brackets for literals/function calls/scopes/includes.
- Hotswap between cpp/header (VS only has this for cpp>header using context menu).
- Hotswap between declaration/implementation (VS only has separate go to definition/declaration that is still slow and buggy with large projects).
- Syntax highlighting is still limited in VS without plugins, although it's catching up with 2012.

Basically, when it comes to writing code, I find using plain old Visual Studio without third party plugins bad for your productivity.

None of those things would be particularly hard to implement (on top of the intellisense engine they already have, anyway). I think the only reason Microsoft hasn't added them is because they would put Visual Assist X (which includes all of your bullet points) out of business.


#5042819 reinterpret_cast problem

Posted by Nypyren on 13 March 2013 - 02:04 PM

Perhaps your book is *intentionally* trying to show you what happens if you manipulate bytes inside of larger data types?


#5040562 binary save, adding vars, and changing savegame file formats

Posted by Nypyren on 07 March 2013 - 03:35 PM

Generally what I do for buffering is use something like C#'s BinaryWriter and a MemoryStream.

MemoryStream is a wrapper around a byte array which allows the array to dynamically grow, and also keeps a Position pointer, which tracks where to read/write next.

BinaryWriter is a fairly simple class that just converts values to bytes, copies them to the underlying stream, and increments the stream's position pointer as it goes.

After you're done writing to the MemoryStream, you just grab its internal byte array and dump the entire thing into your file in a single call.


It's trivial to do this in C++ - you can either use existing stream classes which support most of this, or if you want to use a C interface, it's trivial to write it from scratch with minimal effort (it's a hundred lines of code or less - basically a few dozen functions with 1-3 lines apiece).


#5039342 binary save, adding vars, and changing savegame file formats

Posted by Nypyren on 04 March 2013 - 10:21 PM

For structs, there are generally two problems:

- You add a new field, but the old file doesn't have it.
- You remove a field, but the old file still has it.

There are two main types of implementations I've seen:

=== Option 1 ===

Keep a version number for each struct, serialize the version number (either per struct or once for the entire save file), then write a deserializer which can handle any version up to now. The main benefit here is that you can fread the entire struct if its version number matches your code's struct. If not, you fall back to a generalized reader which if/else reads each individual field based on whether it existed at that version or not (including skipping fields that you've removed). The idea is that it only uses the slow deserializer when the app gets patched, and then when you overwrite the save file, it will be blazing fast again.

Terraria uses the if/else approach, but most of its loading time is spent rebuilding data that isn't actually saved to the file.

The code can look like:
if (version == kCurrentVersion)
{
  ReadEntireStruct(); // fread in C, other languages may or may not have a fast equivalent.
}
else
{
  field1 = ReadString();

  if (version > 12)
    field2 = ReadInt32();

  if (version > 13) // Needed to increase bitfield size to 32 bits from 16
    field3 = ReadInt32();
  else
    field3 = ReadInt16();
}
=== OPTION 2 ===

Each field keeps a 'field id', and the serialized struct stores that field ID before the field contents. In addition, if you want the ability to deserialize "unknown" (removed) fields, you need to add more data that indicates how long (or what type) the field is.

Google's protobuf uses this approach.

The code here can be written in various ways. For example, protobuf.net can generate deserialization classes on the fly and compile them with JIT.
while (fieldInfo = ReadFieldId())
{
  switch (fieldInfo.id)
  {
    case 1: field1 = ReadString(); break;
    case 2: field2 = ReadInt32(); break;
    case 3: field3 = ReadInt16(); break;
    case 4: field3 = ReadInt32(); break;   // notice the field id also had to change when changing the field's type.

    default: SkipField(); break;
  }
}



#5038631 IDE/windows development dilemma

Posted by Nypyren on 02 March 2013 - 09:52 PM

If VS2010 is crashing, something else on your computer is causing it. There's no way for us to guess what your computer has on it that would cause it, though. Could be bad RAM, malware, corrupt registry, old copy of a 2010 beta/trial, etc.

Also you should give VS2012 Express for Windows Desktop a try as well. It includes C++, C# and VB.


#5037314 Why is this code like this? (Order of function calls in an equation)

Posted by Nypyren on 27 February 2013 - 02:33 PM

Your confusion is the #1 reason why I would NEVER modify a variable in an expression that contains the same variable more than once. I would explicitly split the expression into multiple lines like the C++ version has.

I have no idea what Javascript's convention for expression evaluation order or sequence points is, but your second C++ code is the better interpretation of the function.


#5035163 Client-side MYSQL queries - C++ - bad idea?

Posted by Nypyren on 21 February 2013 - 04:44 PM

I think it's really a bad idea. Even if you give them access to no tables - only stored procedures - they can STILL break the server. There are numerous well-documented ways of either crashing the server or making it consume all its resources.
 
The INFORMATION_SCHEMA tables are visible to everyone regardless of permissions, and they can still do SELECTs on them. So if they join one of the (more than one row) I_S tables with itself a dozen times, then the server will attempt to create a result set bigger than your disc (in temp storage normally).

I think ApochPiQ meant that he only allows the client to talk to the server service (not the SQL database itself) AND the server's service only runs stored procedures with sanitized parameters instead of raw SQL queries.


#5034774 C# tools/UI and Mac OSX

Posted by Nypyren on 20 February 2013 - 04:47 PM

I've done a bit of Windows-and-OSX GUI with C#. WinForms doesn't work well enough on OSX (multi-monitor scenarios will totally screw up mouse handling in a lot of WinForms apps on OSX).

Gtk# is what I used. It has a **much** more cumbersome API than WinForms (you can't really RAD with it as quickly as WinForms), but at least it behaves the same on both platforms.

The cross-platform UI support between Windows and OSX sucks so bad we even tried Flex and Java, but those were even worse (Our app was responsible for running several command-line apps, and in Java apparently there is no way to discover the ProcessID of the app you just launched like there is in C#). Flex's performance was just complete and total shit compared to C#. So we gave up and went back to C#.


I'm currently on a project where we use Unity, and we just write all of our tools to run in the editor's GUI (which is also horrible compared to WinForms since it has this disgusting concept of immediate-mode GUI, but as long as we never do anything too complex, we're fine).




PARTNERS