Jump to content

  • Log In with Google      Sign In   
  • Create Account


Member Since 16 Mar 2005
Offline Last Active Today, 01:27 AM

#5237560 Avoid rewriting type to access member typedef

Posted by rip-off on 29 June 2015 - 02:43 PM

I've not used much of the most recent versions of C++, but the "using" keyword's recent upgrade might be worth looking into:

namespace some {
  namespace name {
    namespace space {
       template <typename T> struct Foo {
         typedef int SomeTypedef;
         SomeTypedef hi() { return 42; }
struct Bar {};
int main() {
  using fubar = some::name::space::Foo<Bar>;
  fubar test;
  fubar::SomeTypedef stuff = test.hi();


There is also decltype, but it looks funky:

struct Bar {};
int main() {
  some::name::space::Foo<Bar> test;
  decltype(test)::SomeTypedef stuff = test.hi();

#5237087 Can't convert array of strings to char array

Posted by rip-off on 27 June 2015 - 07:27 AM

Here is an interesting one:
Menu::Menu( std::string mitem_source[] )
    // ...
    int stringcount = ( ( sizeof( mitem_source ) / sizeof( mitem_source[ 0 ] ) ) + 1 );
    // ...
In C++, arrays declarations for parameters aren't actually arrays, they are syntactic sugar for pointers. A real array's type encodes information about the arrays length, hence, sizeof(someArray) is the total size of the full array. Pointers lack such information, so sizeof(somePointer) is just the size of the pointer object, typically a machine address.

This, the sizeof(x) / sizeof(x[0]) trick does not work for array parameters, it can only work for local or global arrays.

A type safe trick in C++ is to use a template to extract the length of the array:
template<typename T, unsigned N>
unsigned array_size(T (&)[N]) {
    return N;
This will work with local / global arrays, but won't silently do the wrong thing with pointers or array parameters:
#include <iostream>

template<typename T, unsigned N>
unsigned array_size(T (&)[N]) {
    return N;

void example(std::string array[]) {
#if 0
    std::cout << "Parameter:" << array_size(array) << std::endl;

int main() {
    std::string array[5];
    std::cout << "Local: " << array_size(array) << std::endl;

#5235930 Get object from std::function

Posted by rip-off on 20 June 2015 - 03:55 PM

No official / safe way. There is no guarantee that a std::function is bound to an object, it could be bound to a free function.

A potential alternative, if the code is like your example, might be to modify it so that it is std::function<Foo &(int)>, and wrap the member function in something that returns the instance.

#5235906 Code crashing when exiting if statement

Posted by rip-off on 20 June 2015 - 01:40 PM

When you're leaving a scope, any temporary variables are destroyed. For complex objects, examples in your code include std::string, std::ifstream, the destructor will be called. For example, if you have corrupted a std::string's internal representation, there could be a crash when running it's destruction logic.

You need to be very careful when interacting with low level networking APIs, a bug like a Buffer Overflow could allow an attacker to remotely take control of the server and execute their own code!

For example here:
char decodedMessage[plength];
for(int v = 6; v<totalLen; v++)
    decodedMessage[v-6] = bigBuf[v] ^ mask[(v-6)%4];
string message = string(decodedMessage).substr(0,plength);
Are you sure that "decodedMessage" will be NUL terminated? As mentioned, you cannot rely on the client submitting a NUL terminator.

#5235193 Debugging problems

Posted by rip-off on 16 June 2015 - 03:40 PM

Debugging can change the behaviour of the memory allocators to help flush out certain types of bugs. However, the downside is that your program can end up depending on the behaviour of a debug allocator, which could be why you're seeing that behaviour.

Is is possible to link the library statically and check for a crash? Executables and DLLs on Windows can have separate heaps, possibly you're seeing something related to this?

Yet more ideas here: http://stackoverflow.com/questions/1010106/how-to-debug-heap-corruption-errors

Good luck!

#5235025 C++ normalize given epoch timestamp

Posted by rip-off on 16 June 2015 - 12:56 AM

Running in codepad.org (http://codepad.org/uTC5KjnF) and locally, I get 123379200. However, the "local" in localtime does hint what might be going on - time zones. I'm in UTC + 1. Using gmtime instead of localtime, I still get the expected value.

#5234658 2D Game Logic for a RTS

Posted by rip-off on 13 June 2015 - 04:05 PM

Game logic generally excludes rendering. That is, the path finding sub-system might be able to treat the map as a two dimensional grid. A hill might be represented by a movement penalty on that tile, which you might already have to represent other terrain (e.g. fording a river).

When you go to render the unit, the height of the terrain does need to be taken into account as you have noted. If you accept certain minor limitations to your maps, such as not having multiple "levels" available at the same position, then you can compute the 3D position of a 2D point by having a "height map", a 2D grid of the height values of the terrain. The 2D position can be used to lookup the height in the height map, which can be used to calculate the position of a given object.

#5234634 float value increment in a loop?

Posted by rip-off on 13 June 2015 - 12:58 PM

For anyone interested about Java error this generates:

Same basic issue, you must use 0.3f for a float literal.

#5233387 Serialize Object to specific directory

Posted by rip-off on 07 June 2015 - 11:47 AM

Are you asking how to determine the "project's folder"?

I guess the bigger question might be what are you trying to serialize, settings, save games, something else?

#5230927 1 (interface) header with 2 cpp (implementation) files?

Posted by rip-off on 25 May 2015 - 04:43 PM

Generally, this would not be considered idiomatic and could be confusing. Where possible, classes should be small and focused - having to spread the implementation across multiple files suggests this is not the case.

For my case this is a good solution.

It could be a fine solution. You don't want to spend infinite time designing and implementing your game, so at some point you have to put the pencil down.

Peripheral code, like a menu system, doesn't deserve the same amount time and attention to the core, which you'll be iterating on more and where poor design will slow you down more and more over time.

Separating into another class will create extra complexity with interfacing and continously sending data around from one class to another.

This is the crux of design, can you come up with a way of decoupling this data that you currently feel you need to "send around"?

I'd have to see some code to understand exactly what is going on, but it isn't immediately obvious to me why the menu and application need to interact so heavily. If you were to show us the header file, and outline which functions would be implemented in the different source files, we would really start to see your design.

For very simple games, a simple menu enumeration can suffice. There is nothing wrong with a few switch statements to call your main menu rendering logic, your audio settings rendering logic and having another switch in your menu event loop dispatcher - it gets the job done.

For games with a bit more going on, I've also used a simple "game state" model, the application delegates to the current state, e.g. update, render, handle input. The current state can return a new state at the end of each loop. The state could be an PlayIntroState, a MainMenuState, various other menu states, a LevelLoadingState and a PlayLevelState. With such a design, you might end up with a lot more classes / files, but each one is very simple and does just one thing. The application can be largely ignorant of the actual details of the states it is cycling through, once it has the initial state.

For complex games (which I don't write), I imagine you'll probably be configuring / scripting / skinning whatever menu systems the game engine provides you.

#5229303 Another basic array C problem

Posted by rip-off on 16 May 2015 - 08:40 AM

It is important not to be intimidated by the more complex interface of a modern and full-featured IDE like Visual Studio. It was a mistake I made when I was starting out. Having good tools is vital.

As for your program, there are definitely a few things the compiler should be noticing for you. Look into configuring your build tools to have a very high warning level (ideally, all warnings) and also to treat warnings as errors. Particularly as a beginner, any warning is very likely to be an issue you should fix.

When I compile your program (slightly modified for Linux), I get the following output:
$ gcc gamedev.c -Wall -Werror
gamedev.c: In function ‘main’:
gamedev.c:16:1: error: statement with no effect [-Werror=unused-value]
gamedev.c:19:1: error: format ‘%d’ expects argument of type ‘int *’, but argument 2 has type ‘double *’ [-Werror=format=]
gamedev.c:37:8: error: character constant too long for its type [-Werror]
gamedev.c:38:1: error: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘double’ [-Werror=format=]
 case 'H':printf("\nHighest temperature is  %d in %d city in %d day",temp[1][1],j,i);
gamedev.c:40:1: error: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘double’ [-Werror=format=]
 case 'L':printf("\nLowest temperature is %d in %d city in %d day ",temp[5][3],j,i);
cc1: all warnings being treated as errors
These should be simple enough to fix, but see if you can configure your environment to give you a similar set of warnings - the exact warnings offered vary between compilers, do don't worry if you don't get them all or see other ones.

Be consistent with your formatting and indentation. It is hard to know because without the proper [code=auto:0] tags, much of the indentation was lost, but a simple example is where you place the curly braces after a loop or conditional statement. The two most common conventions are on the same line (possibly with whitespace preceding it), or on the next line. It doesn't matter which one you pick, but it does matter that you pick one and be consistent.

Also, another thing that will make your code easier to read is to use more whitespace around expressions. Again, the exact details can vary, but almost everyone would agree that this:
Is a bit harder to read than this:
for (i = 1; i <= MAXDAY; i++)
    for (j = 1; j <= MAXCITY; j++)
        if (temp[i][j] >= temp[i + 1][j + 1])
            swap = 0;
            swap = temp[i][j];
            temp[i][j] = temp[i + 1][j + 1];
            temp[i + 1][j + 1] = swap;
Typically I'd put a space between function parameters too, for example:
// Instead of:
printf("Day= %d City=%d ",day,city);
// Consider
printf("Day= %d City=%d ", day, city);
Even with all this, you won't have fixed everything, but you'll be much better placed to solve the remaining issue.

#5228125 Terrible Broken Age sales...!? (steamspy)

Posted by rip-off on 09 May 2015 - 09:47 AM

Have you watched the "Double Fine Adventure" documentary series? It is now available for free on Youtube, and there are entries that cover the initial release (in particular towards the end of EP 16 and start of EP 17). It's a fascinating documentary, worth a watch I think.

I believe humble bundles include DRM free versions, which could mean that not every HB sale can be counted against the Steam numbers - and the game is available on a bunch of other platforms too.

#5221706 please help with SDL

Posted by rip-off on 06 April 2015 - 03:39 PM

Please note that text is the preferred communication mechanism between programmers. You can include or link to images to aid understanding, but usually just copying and pasting the error message(s) and any offending code as text is a better approach.

The problem with images is that even if I can see code it in, I cannot easily edit / compile / run. I would have to type it out again. Likewise, with error messages, I cannot easily throw them into Google, I would have to re-type them.

Generally, I'd recommend not including images unless you're having difficulty explaining the issue without it.

#5218931 Future proof scoreboard server design pattern

Posted by rip-off on 24 March 2015 - 05:37 PM

I'd recommend including a version identifier in client requests, preferably in the URL.

Yep. What do you mean by "preferably in the URL"? Do you mean that my PHP scripts shall have a version ID in their name?
This will allow multiple versions of my app to access the same database. It will for instance be possible to rename a variable. Is that the reason for this version ID?

Something like yourgame.yoursite.com/v1/scoreboard

If you need to create a new version of the API, just point them at /v2/whatever instead. This way, it is very easy to distinguish requests using the older version. In particular, it is each to configure front-facing servers to match on URLs and forward requests to different back-end servers.

This may not be a concern right now, but this gives flexibility in future.

Another thing to consider is to specify a couple of response types now that all client versions can understand. For example, a particular HTTP status and/or payload might indicate that the client is out of date, so the client could display a "please update" message to the user.

#5218640 Future proof scoreboard server design pattern

Posted by rip-off on 23 March 2015 - 04:38 PM

I'd recommend including a version identifier in client requests, preferably in the URL.