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

Login with username, password and session length

Advanced search

1212632 Posts in 52637 Topics- by 43421 Members - Latest Member: BeeZee

May 06, 2016, 12:43:12 pm

Need hosting? Check out Digital Ocean
(more details in this thread)
  Show Posts
Pages: 1 [2] 3 4 ... 68
21  Developer / Technical / Re: Generic functions and game structure on: December 09, 2014, 04:29:33 pm
The thing for me is that a class is a data type.  If your class can only support a single instance, it's not a data type, it's just data.  You're using the wrong tool for the job.

It's akin to using a goto and a label instead of a while loop.  Does it work?  Sure, but the language provides you with better options.

Even if people insist on making these things classes (which I think is indicative of insane OOP indoctrination) make them proper classes that can support multiple instances, be copied/moved etc...
22  Developer / Technical / Re: Generic functions and game structure on: December 05, 2014, 02:51:43 pm
Yeah ultimately it's not a big issue. Neither of them really hinder your progress.

To me it's a big issue, but I'm a "language guy."

Let's just say that if someone brought me C++ code with a singleton when I was doing a job interview, my impression would be that this person doesn't really understand the language.
23  Developer / Technical / Re: glDrawArrays failing silently...halp? on: December 04, 2014, 04:51:49 pm
Man, this is frustrating. It seems as though a 3.3 context with a compatibility profile is simply not a thing.

Are you on a Mac?  I may be wrong about this, but I believe there are no compatibility contexts for 3.3+ on Macs.
24  Developer / Technical / Re: Generic functions and game structure on: December 03, 2014, 04:29:50 pm
Singletons normally get constructed the first time you ask for an instance, not before main executes.

I find this behavior completely obnoxious and it amazes me that people sometimes claim this is a benefit of singletons (not that I'm saying you're arguing that).  I want my stuff to either initialize when I tell it to so I can handle errors, or before main() so I don't even start running if there are problems.  Having possibly critical code initialize at some undisclosed future time is just a bug waiting to happen.
25  Developer / Technical / Re: Generic functions and game structure on: December 02, 2014, 04:40:52 pm
A namespace will be fine, but I personally only use namespaces for "global" free functions that need containment, or to resolve anything that might cause a name conflict. If I'm going to be storing any sort of data, I prefer to use a Singleton.

This is what the unnamed namespace is for.  You put the data in the source file like so:

#include "my_header.h"

    // Unit data.
    int thing;
    int the_other_thing;

This makes the data completely inaccessible from outside the source file.  Singletons, like "static classes," are a hack used in a pure object-oriented languages like Smalltalk in order to fake this sort of internal linkage.  There's really no place for them in C++, which natively supports the features that singletons simulate.
26  Developer / Technical / Re: Generic functions and game structure on: December 02, 2014, 05:34:43 am
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.

In other words, Namespaces are what JC would do.

I'm assuming that's John Carmack, in which case no, he would just do it in C.
27  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.
28  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.
29  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.
30  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.
31  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.
32  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.
33  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.

It's not "eliminating the one safe case"

Yes it is.  This isn't really arguable, it's defined by the language.
34  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!
35  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.
36  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.
37  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.
38  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.
39  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:

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);
    -- Iterate through all values, printing them out.
    Ada.Text_IO.Put_Line("Enum forward:");

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


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

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

end Enum_Demo;

bash-4.3$ enum_demo
Enum forward:
Enum reverse:

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:

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
        void print();

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

        // Friendship required.

    // 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.
// Range for the plain enum.
// 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 << ' ';


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

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.
40  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.
Pages: 1 [2] 3 4 ... 68
Theme orange-lt created by panic