Wednesday, December 16, 2015

Remove a single character from std::string

This  might probably be one of the oldest tricks for 2015 for std::string but still blogging about it anyways.

To remove all instances of a single character from a std::string, you can use std::remove and string::erase.

#include // include for remove()

std::string str = "This is a test";
str.erase( std::remove( str.begin(), str.end(), 'e' ), str.end() );

This would remove the character e from string, and will provide result of "This is a tst"

If you want to remove quotes from the string,

std::string str = "This \" is a test \"";
str.erase( std::remove( str.begin(), str.end(), '\"' ), str.end() );

result str will have "This is a test" without the quotes. 

Thursday, December 10, 2015

C++ and aggregate initialization

There is an easier way to initialize a char array with all 0s instead of using the traditional memset

So this code

char array1[1024];
memset(array1, 0, sizeof(array1));

can be replaced with

char array[1024] = {0};
char array[1024] = {}; //same as {0}

Which is basically the same but on a single line implementation.

Section 8.5.1 of the ISO Spec states

"An aggregate is an array or a class with  no user declared constructors, no private or protected non static data members, no base classes, and no virtual functions"

If you want to initialize such an aggregate you can use {}.

This is not only  used to set everything to 0, but to also initialize class members.

For e.g.

struct ExampleStruct
{
   int x;
   int y;
   int z;
};

If you have a simple struct like this with no constructors, all public members, then you can initialize its members in a single line like this

ExampleStruct Object = { 2, 3, 6 };

Which would set x = 2, y = 3 and z = 6.

Also as per C++11, = {0} is required to set the padding bytes to 0 by the standards. For e.g

struct ExampleStruct
{
   char x;
   int y;
}

ExampleStruct Object = {0}
will set x = 0 and y = 0 , and the padded bytes between x and y will be 0.



Thursday, November 26, 2015

Hosting webserver on Mac with Python

I needed to host a webserver with basic setup to load a file for a project. Here is what I did

1. in Mac, I opened terminal, and went to the folder that had index.html and typed
python -m SimpleHTTPServer 8000
This spit out the following log
Serving HTTP on 0.0.0.0 port 8000 ...
And it hung there. Which is indication that the server is running on port 8000

That is all that you would have to do to host a webserver on Mac with python.

In order to access this file from a different system, we need the name or ip address for localhost.
ip address can be obtained by typing ifconfig in terminal, and getting the inet address of en0 section, if you are connected to the internet via ethernet LAN cable.

Or you can go to system preferences -> Network and get the ip address given in the right side section of the preference window in Mac

After getting the ip address, if you type http://xxx.yyy.zzz.uuu:8000/index.html, it should display the webpage.


Hope this helps. 

Thursday, November 19, 2015

CPPCheck and latest 1.71 version


I normally use CPPCheck to verify the code sanity in my c++ projects. Few days back I tried to update to latest from 1.69 version. I uninstalled 1.69, and installed latest 1.71 from http://cppcheck.sourceforge.net. But when attempting to run the application, it says MSVCP140.dll missing.

Here is how I fixed it, and thought this blog post might help others who also have similar issue.
This dll belongs to MS Visual C++ as the name in the dll suggests. Downloading Visual C++ redistributable for Visual Studio 2015, even though you dont use 2015 version, should fix this missing dll issue. It can be downloaded and installed from https://www.microsoft.com/en-us/download/confirmation.aspx?id=48145


Hope this helps. 

Sunday, January 25, 2015

Global Game Jam 2015

About the Game:
This year's theme was "What do we do now?"

The Maze is ALIVE.. ALIVE.. you hear ?
You and your friend have to escape alive and sane.

There are multiple exits. Some are blocked, some are not. You (green color coded) have to exit through your exits (green color coded). Your friend (red color) has to exit via their designated exits (red color). But there are door blocks stopping you from escaping via some of your exits.

Oh! look there is switch some where in the maze to make those obstacles disappear. But you cannot pull its trigger. Nope. Your partner (other player) has to do that for you. And vice versa, you (blue) can tap the switch to destroy the door blocks for your friend's exits(red).

There are some unhindered exits, that you can escape through. But what about your play pal? If they cannot escape from the maze because all of their exits are blocked, and you got selfish and left them to die in maze, you don't win either. You both have to escape in order to win the game and laugh about it later.

The maze is alive and crazy. It can make you lose track of yourself and think you are invisible. You have to count your steps( imagine proper movement count) to exit when invisible on screen.

Sounds Easy?
You have to escape, and you have to help escape your friend,  in exactly half a minute. Just 30 seconds.


Technical aspects:

Maze is procedurally generated every time game restarts, by using perfect maze generation algorithm (Recursive Division). Number of exits change each time, and door blocks change location and exit doors on each run. Switch's location and players' location changes randomly at every restart.
Simple collision exits, where the players if they bump into each other, cannot proceed, and one has to leave way for the other. After all, this is a cooperative game play mode.

First Game Jam for me. And first time I am using Unity for a concrete , non prototype game.

Code here : https://github.com/swtsvn/GGJ15Sujatha
Global Game Jam Page here : http://globalgamejam.org/2015/games/haunted-maze

Page is yet to be complete. Check back soon.