Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length

 
Advanced search

1104277 Posts in 45934 Topics- by 37613 Members - Latest Member: Daphna

March 30, 2015, 12:01:46 PM
  Show Posts
Pages: 1 [2] 3 4 ... 68
21  Developer / Technical / Re: Generic functions and game structure on: December 01, 2014, 12:23:21 PM
Namespaces can't be template parameters.

How I wish they could.  I'd also like to be able to make templated namespaces.  I was going to write both of those up as proposals for the next language revision, but got lazy and didn't do it.
22  Developer / Technical / Re: Generic functions and game structure on: November 30, 2014, 11:51:56 AM
I am working on a strategy/roguelike in C++. I have some functions for generating maps (generating random points, voronoi) and other functions that are not dependent on game code. Then of course I have classes like Map, Entity etc.

I haven't used C++ for some time now and I can't decide how to best organize my code. Should I create a namespaces for these functions, e.g. "voronoi" with generate function and other required functions for voronoi algorithm. Another approach would be to create classes with static methods.

What approach do you use in your games? It would be great if you could show some examples.

Namespaces are the proper C++ solution.  Classes with static methods are largely a hack used by languages like Java that don't support free functions.
23  Developer / Technical / Re: Beginner's Pains on: November 29, 2014, 11:54:04 AM
When i was starting out, the only thing I struggled with at first was assembly language. 6502 on the BBC master series Smiley

I'm doing 6502 now (I'm writing an Atari game) and it's so much fun.
24  Developer / Technical / Re: Beginner's Pains on: November 26, 2014, 05:38:06 AM
When you first started learning how to do (game) programming, what did you find to be the most difficult thing to grasp or get the hang of?

PEEK and POKE and the Apple II memory scheme.
25  Developer / Technical / Re: Pointers and references on: November 21, 2014, 04:06:51 PM
One disadvantage of references is that you cannot use the 'restrict' keyword on them, and if you're very performance focused that can be a significant problem. If you don't know what the restrict keyword is for, then you probably needn't worry about it.

restrict is a C keyword, not C++, unless C++14 adopted it.

As for pointers/references, my general rule is to that pointers denote ownership, references do not.  That is, if I give a function something by pointer, that function now owns that thing and is responsible for managing it.  It I pass something by reference, it belongs to the caller and the function is just using it.

Of course, I prefer pass/return by value over both of these whenever I can get away with it.
26  Developer / Technical / Re: [GML] How to check if a number is divisible by 5? on: November 19, 2014, 06:01:00 PM
The "remainder" operation is called modulo, and it's usually present as either mod or "%" (percent) in just about every programming language ever. See http://en.wikipedia.org/wiki/Modular_arithmetic

Not that it matters in this case, but modulo and remainder are only the same thing if the numbers involved are positive.  When negatives come into play, they're quite different.

I've seen very few languages that actually offer remainder, Ada is the only one that comes to mind, but Fortran probably has it too.
27  Developer / Technical / Re: Free ImGui library for tweaking/debug tools in C++ projects on: August 28, 2014, 07:34:02 AM
it is used internally

If it's an internal type, why is it in the header?  It also appears in types like ImGuiTextFilter, which makes those types break horribly when returned from a function.  If those are also internal types, they really ought to be removed from the header.

Quote
It's not "eliminating the one safe case"

Yes it is.  This isn't really arguable, it's defined by the language.
28  Developer / Technical / Re: Free ImGui library for tweaking/debug tools in C++ projects on: August 28, 2014, 05:37:12 AM
These types are a huge pile of bugs just waiting to happen.  Your vector class really needs a copy constructor/assignment operator, or a C++11 move constructor/assignment pair, or at least explicitly disable copying.  I know I expect objects to have proper value behavior and I'm not the only one.

Also, it's perfectly safe to delete/free null pointers.  In fact, null pointers are the only pointers that are guaranteed to be safe to delete.  Your if (ptr) checks are actually doing the precise opposite of what you intend, you're eliminating the one safe case, and the fact that the pointer is not null in no way indicates that it actually points to something you can free.

I've been on projects where both of these problems have bitten us in the ass.  Save yourself before it's too late!
29  Developer / Technical / Re: Conditional compilation in Java? on: August 19, 2014, 05:38:02 PM
There's no reason why you can't just use a C preprocessor on your Java files.
30  Developer / Technical / Re: C# Equals Documentation Question on: July 31, 2014, 04:36:37 AM
I don't do C#, but if the examples of it on MSDN are anything like the C++ examples there, they're generally awful and should always be looked at with suspicion.
31  Developer / Technical / Re: learning c++ on: July 25, 2014, 03:54:09 PM
Be wary of any books or tutorials that you find that are more than a couple years old.  There a lot of things that were considered good practice before C++11 that are now bad ideas.

If there's one thing to understand about C++, it's that it's a value semantics language.  A lot of people try to write C++ as if it were Java or C# and that never works well.  Modern C++ rarely uses pointers or dynamic objects, with the new C++11 rvalue references the need for dynamic memory (and hence, the possibility of memory leaks) should be fairly rare.  This is a fairly advanced topic though, so make sure you make all the usual mistakes along the way first, then you'll understand it.

The object-oriented stuff is fine and dandy, but modern C++ is really about generic programming, what C++ calls templates.  Object-oriented C++ is sort of old hat these days.  It will never go away, but generic programming is where the action is.  Unfortunately, it can take a long time to truly grasp the things that can be done, so don't worry if you don't get it right away, plenty of other people don't either.
32  Developer / Technical / Re: What's your favorite programming language? on: July 13, 2014, 09:48:07 AM
Ada.  It's what C++ would be without all the C baggage.
33  Developer / Technical / Enum extensions for C++ 2011 on: June 18, 2014, 06:59:55 PM
I use enums a lot, in pretty much every language I code in.  My favorite enums happen to be in my favorite language, Ada.

I recently started a C++2011 project, and quickly began wishing I could do some of the things with C++ enums that I could do with Ada enums.  I've often said that one of the great things about C++ is that whatever the language doesn't support can usually be faked with templates.  I decided to whip up some easy to use enum extensions to get a handle on some of the new metaprogramming facilities in C++2011.

There were two big things I wanted to accomplish:

Enum iteration

Ada's for loop allows you to iterate through an entire (scalar) type.  Getting C++ to iterate through all values of an enum is a pain, especially if you assign unusual numeric values to the enum members.  Also, Ada can run this loop in both directions.  I was saddened to see that C++2011's new ranged for loop doesn't allow reverse iteration, I expect C++2014 will provide a library adapter for this, std::reverse or something.

String based IO

Ada enums are input and output as strings, not numbers.  I greatly prefer this to what C++ does, it's a huge aid in debugging and makes files containing enum output a whole lot easier to parse visually.

Here's a quick Ada snippet demonstrating these concepts:

Code:
bash-4.3$ cat enum_demo.adb
with Ada.Text_IO;

procedure Enum_Demo is
    -- This is an enumeration.
    type Day is (Mon, Tue, Wed, Thu, Fri, Sat, Sun);

    -- Enum IO instantiation.
    package Day_IO is new Ada.Text_IO.Enumeration_IO(Day);
begin
    -- Iterate through all values, printing them out.
    Ada.Text_IO.Put_Line("Enum forward:");

    for X in Day loop
        Day_IO.Put(X);
        Ada.Text_IO.Put(' ');
    end loop;

    Ada.Text_IO.New_Line;

    -- Do the same in reverse.
    Ada.Text_IO.Put_Line("Enum reverse:");

    for X in reverse Day loop
        Day_IO.Put(X);
        Ada.Text_IO.Put(' ');
    end loop;

    Ada.Text_IO.New_Line;
end Enum_Demo;

bash-4.3$ enum_demo
Enum forward:
MON TUE WED THU FRI SAT SUN
Enum reverse:
SUN SAT FRI THU WED TUE MON
bash-4.3$

With the help of some macros and a plethora of templates, I managed to reimplement these facilities in C++.  They do require the coder to provide some data, specifically the enum table and the strings that you want to use for IO.  Here's a demo of what I came up with:

Code:
bash-4.3$ cat main.cpp
#include "enum_range.h"
#include "enum_io.h"
#include <iostream>

namespace NS
{
    // Enum class with range and IO.
    enum class Day {mon, tue, wed, thu, fri, sat, sun};
    // Enum with range, but not IO.
    enum Weekday {mon, tue, wed, thu, fri};

    class Calendar
    {
    public:
        static
        void print();

    private:
        // Private nested enum with values.
        enum Weekend {sat = 6, sun = 7};

        // Friendship required.
        DECLARE_ENUM_RANGE_FRIEND(Weekend);
    };

    // Bring the operator into the enum namespace for ADL.
    using EnumIO::operator <<;
}

// Declare the range for the enum class.
// A range declaration is required for the IO system.
// These macros must be placed in the global namespace.
DECLARE_ENUM_RANGE(NS::Day, NS::Day::mon,
                            NS::Day::tue,
                            NS::Day::wed,
                            NS::Day::thu,
                            NS::Day::fri,
                            NS::Day::sat,
                            NS::Day::sun);
// Range for the plain enum.
DECLARE_ENUM_RANGE(NS::Weekday,
                   NS::mon,
                   NS::tue,
                   NS::wed,
                   NS::thu,
                   NS::fri);
// Range for the nested enum.
DECLARE_ENUM_RANGE(NS::Calendar::Weekend, NS::Calendar::sat, NS::Calendar::sun);

// IO string definitons for the enum class and nested enum.
DECLARE_ENUM_IO(NS::Day, "mon", "tue", "wed", "thu", "fri", "sat", "sun");
DECLARE_ENUM_IO(NS::Calendar::Weekend, "sat", "sun");

void NS::Calendar::print()
{
    // Print the nested enum values going forward.
    std::cout << std::endl << "nested enum with values:" << std::endl;

    for (Weekend we : EnumRange::forward<Weekend>())
    {
        std::cout << we << ' ';
    }

    std::cout << std::endl;
}

int main()
{
    // Print out all days going forward.
    std::cout << "enum class forward:" << std::endl;

    for (NS::Day d : EnumRange::forward<NS::Day>())
    {
        std::cout << d << ' ';
    }

    // Do the same going backward.
    std::cout << std::endl << "enum class reverse:" << std::endl;

    for (NS::Day d : EnumRange::reverse<NS::Day>())
    {
        std::cout << d << ' ';
    }

    // Weekdays going forward, uses ordinary numeric IO.
    std::cout << std::endl << "enum forward, no strings:" << std::endl;

    for (NS::Weekday wd : EnumRange::forward<NS::Weekday>())
    {
        std::cout << wd << ' ';
    }

    NS::Calendar::print();
}

bash-4.3$ a.out
enum class forward:
mon tue wed thu fri sat sun
enum class reverse:
sun sat fri thu wed tue mon
enum forward, no strings:
0 1 2 3 4
nested enum with values:
sat sun
bash-4.3$


There are three macros that make all this possible.

DECLARE_ENUM_RANGE creates the enum range.  You simply give it the enum type, and a list of all its values.  This will allow the use of the functions EnumRange::forward<Type>() and EnumRange::reverse<Type>() to iterate the enum in either direction.

DECLARE_ENUM_IO builds the string table for the stream operators.  A range for this enum must have already been declared before the use of this macro.  A helpful static_assert will tell you when you screw this up.

DECLARE_ENUM_RANGE_FRIEND inserts necessary friend declarations to allow access to nested private enums.

The first two macros MUST be used at global scope.  The code they produce is in the EnumRange and EnumIO namespaces, so don't worry about namespace pollution.

Also, the EnumIO operators << and >> must be brought into the namespace that the enum lives in.  A simple using directive will accomplish this.

These examples don't demonstrate input, but it works the same way.

The range code is constexpr to the maximum extent I could pull off, so the compiler could optimize range iteration to near zero runtime overhead.  There is a small space overhead for the value table, but that's a constexpr std::array, so you don't get much smaller than that.

The IO code is also pretty streamlined, the only real work is in the << and >> operators.  The only space overhead is the string table, once again a constexpr std::array.

The EnumRange and EnumIO namespaces also include traits testers, HasRange and HasTraits.  These are used in the implementation, but can also be used for general metaprogramming purposes.

Sadly, the one thing I couldn't accomplish was applying these features to a enum declared locally in a function body.

If anyone things this stuff is neat and wants to use it, feel free to nab the two headers here.
34  Developer / Technical / Re: Scrolling games that scroll poorly on: May 26, 2014, 03:26:10 PM
I think a lot of games would do themselves a world of good by only supporting fullscreen (I never understood the trend of gaming in a tiny window), setting the refresh-rate themselves, and if the game slows down, too bad. Most indie games have absolutely no excuse for dropping below 60fps on any semi-modern computer.

I prefer playing games in a window more often than not.

I think developers should stop trying to force console paradigms onto PCs.  PCs are a totally different world, and there's so much more you can take advantage of.  Take the time to allow the user to run your game the way they want to.  If the player wants to run my game in a 20x30 window at 4 FPS, that's their business.
35  Developer / Technical / Re: Scrolling games that scroll poorly on: May 26, 2014, 06:59:39 AM
Fixed timesteps are the way to go unless determinism doesn't matter (which is almost never).

There are a lot of games in which determinism really doesn't matter.  Anything turn-based, for example.
36  Developer / Technical / Re: How to proceed with a multiplayer game, first the game or first the net code? on: April 27, 2014, 07:49:39 AM
It's generally easiest to not even differentiate between single player and multiplayer.  "Single player" works identically to any other situation, it just talks to a local server.
37  Developer / Technical / Re: Creating a new programming language from scratch? on: April 12, 2014, 09:55:35 AM
Thanks for the recommendation Cheesegrater, I'll look into that. Is that book more regarding theory of compilers, or is is based around an actual compiler that is dissected and studied in-depth?

In practice, the theory is nearly impossible to separate from the implementation, unless you're writing a bad compiler, I guess.

Also, if its not proprietary information, would you mind if take a look over your own created language, and maybe clue me in to important lessons you had to learn the hard away?

It's not far enough along to have anything to show, just notes and stuff in my head, really.  However, one of my current game projects has its own simple scripting language.  It's not a "programming language" in any real sense, but the code does have a complete parser and tokenizer.  It's all GPL, so feel free to look at it.  Source is included in all the downloads.
38  Developer / Technical / Re: Creating a new programming language from scratch? on: April 11, 2014, 05:23:44 PM
I've written compilers for college, and I've also been dabbling in a language of my own design for many years (all generic, all the time!).

Compilers are, as has been pointed out, not trivial to write, and languages are much harder to design than you might expect.  One thing to consider is that compilers can generally be broken down into semi-independant parts and you may want to start be learning how to write some of those smaller parts.

In particular, knowing how to write a general language parser is immensely valuable.  I can't think of a single significant project that I've worked on that didn't involve writing a language parser at some point.
39  Developer / Technical / Re: Simple cross-platform gamepad library on: April 01, 2014, 06:54:57 PM
Yeah, figured it was a bad assumption. Without that it makes menu navigation to get to the controller mapping require a keyboard which is not ideal but meh I suppose.

This is why I insist on making controller configuration mouse driven, preferably in a configuration program external to the game.
40  Developer / Technical / Re: Simple cross-platform gamepad library on: April 01, 2014, 06:31:19 PM
Yeah, we mapped that one a bit back and thought it was interesting. That one has no ill effects for us though, unlike my external webcam which registers as a multiaxis controller and generates axis 0 and axis 1 data for no apparent reason. Based on our mappings every controller has reported axis 0/1 as left stick - is this a universal truth or a bad assumption (and yeah, there is 0 consistency for any other stick/triggers/buttons.)

I think my flightstick reports the throttle as axis 0 and the stick as 1/2, but I would have to check.

Either way, I wouldn't make any assumptions at all about what's what,
Pages: 1 [2] 3 4 ... 68
Theme orange-lt created by panic