lunes, 4 de junio de 2012

C++ std::min/std::max compilation issues

I just came across this issue today: http://support.microsoft.com/kb/143208

Basically, if you have code like this:

#include "windows.h"

...

int minimum = std::min(a,b);

it's going to blow up in your face.

Notice this at the bottom of that page: "This behavior is by design" . Seriously???

I solved the issue using extra parenthesis to prevent macro expansion thanks to stackoverflow:

int minimum = (std::min)(a,b);

And by the way... "windows.h" redefines lots of other stuff, beware! e.g. try to define the function DeleteFile and see what happens!

domingo, 27 de mayo de 2012

Good explanation of the Levenshtein distance: http://www.csse.monash.edu.au/~lloyd/tildeAlgDS/Dynamic/Edit/

And a good implementation: http://stevehanov.ca/blog/index.php?id=114

domingo, 20 de mayo de 2012

Very good generic game action framework software engineering discussion here:
http://www.gamedev.net/topic/624995-am-i-over-engineering-a-generalized-game-action-framework/page__view__findpost__p__4940963

miércoles, 4 de abril de 2012

Easy grammar parsing with CodeTalker

Looking at libgdx-cpp I wondered how they ported libgdx from Java to C++ so fast. The two languages have very similar syntax, but still, one would think it's still quite tedious to manually rewrite each class, thus there must be a way to translate the few differences into C++. Even if the translation is not perfect, at least it speeds up the whole process.

It seems that they were using CodeTalker. It certainly is parsing made easy, check this JSON parser!

miércoles, 28 de marzo de 2012

Searching file names from a directory in a given file

At work, at some point I had to clean up old stuff so that we could use it as a base for the next project. I needed a script that would search the *.sprite filenames in a given .game file, which was just a simple file with references to those sprite filenames.

If I remember well, it executed fast enough (in about 100ms for our whole sprite folders). TODO note to self: explain the algorithm!

import sys
import re
import os  

class UsedSpritesLister:
    """ Class that contains util functions to find and list the sprites that are not used in a .game file """
    
    def __init__ (self):
        # members that need to be set for the processing to work
        self.input_sprite_path = ""
        self.input_gamelevels_path = ""
        self.ignoreDirSet = set(['.svn'])
        self.fileExtSet = set(['.level'])
        
        self.spriteNameSet = set()
        self.foundPatternsSet = set()
        self.notUsedSpritesNameSet = set()
        
        # caches the sets of whole words of the texts read from disk
        self.textSetCache = dict()
        
    def process (self):
        
        print 'Processing sprite names...'
        self.spriteNameSet = self.getSpriteNamesSet(self.input_sprite_path)
        
        if not self.spriteNameSet:
            print 'No sprites found, stopping.'
            return
        
        print 'Checking which sprite is not used...'
        self.foundPatternsSet = set()
        
        for spriteName in self.spriteNameSet:
            found = self.searchInDir(self.input_gamelevels_path, spriteName)
            if found:
                self.foundPatternsSet.add(spriteName)
        
        #print "found patterns: " , self.foundPatternsSet
        #print "sprite name set: " , self.spriteNameSet
        
        self.notUsedSpritesNameSet = self.spriteNameSet.difference(self.foundPatternsSet)
        
        #print "difference: " , self.notUsedSpritesNameSet
        
    
    def printResults (self):
        """ Prints the processing results. If nothing was processed, an empty set is printed """
        print 'Not used sprites: '
        
        for spriteName in self.notUsedSpritesNameSet: 
            print '--- ' , spriteName
    
    def getSpriteNamesSet (self, dir):
        """ Gets the names of the sprites in a given dir. Note that this function
            assumes that all names are UNIQUE, since a set is being used for storing the names
            
            Params:
                - dir: directory where to start looking for sprites
            
            Returns: set with the names of the sprites found
        """
        
        spritesSet = set()
        
        for (dirpath, dirnames, filenames) in os.walk(dir):
            for filename in filenames:
                if filename.endswith('.sprite'):
                    spriteName = os.path.splitext(filename)[0]
                    spritesSet.add(spriteName)
            
            for dirname in dirnames:
                # not ignored dir?
                if dirname in self.ignoreDirSet:
                    # remove it from the list so that we don't recurse there
                    dirnames.remove(dirname)
        
        return spritesSet
        
    
    def searchInDir (self, dir, pattern):
        """ Walks the given directory recursively searching for the given patterns in the files.
        
            Params:
                - dir: directory to search recursively
                - pattern: pattern to search in the files of the directory
            
            Returns: True if pattern is found, False otherwise.
        """
        
        # search the pattern in the files recursively. If found in a file,
        # the search will be stopped.
        for (dirpath, dirnames, filenames) in os.walk(dir):
            for filename in filenames:
                #only search in files with the given extensions
                file = os.path.join(dirpath, filename)
                ext = os.path.splitext(file)[1]
                if ext in self.fileExtSet:
                    found = self.searchInText(file, pattern)
                    if (found):
                        return True #found the pattern, so stop searching
            
            for dirname in dirnames:
                # not ignored dir?
                if dirname in self.ignoreDirSet:
                    # remove it from the list so that we don't recurse there
                    dirnames.remove(dirname)
        
        return False
                    
    def searchInText (self, file, pattern):
        """ Searches a pattern in the given file.
        
            Returns: True if found, False otherwise
        """
        
        # file loaded previously?
        if (file in self.textSetCache):
            gamefile_set = self.textSetCache[file]
        else:
            fileHandler = open(file, 'r')
                    
            gamefile_text = fileHandler.read()
            
            # find all whole words (CASE-INSENSITIVE) and create a set from them
            gamefile_set = set(re.findall(r"\b\w+\b", gamefile_text, re.IGNORECASE))
            
            # cache the set
            self.textSetCache[file] = gamefile_set
        
            fileHandler.close();
        
        # search for the given pattern
        found = pattern in gamefile_set;
        if (found):
            #print 'Found ' , pattern, ' in file: ' , file
            return True
        
        return False

def printUsage ():
    print ''
    print 'usage: %prog input_path game_levels_path'
    print ''
    print 'Lists the sprites from a given folder that are not used in the game levels in the given folder'
    print '  input_path           path to the input folder without quotation marks'
    print '  game_levels_path     path to the game levels folder without quotation marks'

if __name__ == '__main__':
        
    if len(sys.argv[1:]) != 2:
        print 'Invalid number of arguments'        
        printUsage()
        exit(2)
    
    input_sprite_path = sys.argv[1]
    input_gamelevels_path = sys.argv[2]
    
    if (not os.path.exists(input_sprite_path)):
        print 'ERROR: input folder <' , input_sprite_path , '> does not exist'
        printUsage()
        exit(2)
    
    if (not os.path.exists(input_gamelevels_path)):
        print 'ERROR: game folder <' , input_gamelevels_path , '> does not exist'
        printUsage()
        exit(2)
    
    spriteLister = UsedSpritesLister()
    spriteLister.input_sprite_path = input_sprite_path
    spriteLister.input_gamelevels_path = input_gamelevels_path
    
    print 'Input dir: ' , input_sprite_path
    print 'Game levels dir: ', input_gamelevels_path
    print 'Processing...'
    
    spriteLister.process()
    spriteLister.printResults()
    
    print 'Finished'
        


viernes, 9 de marzo de 2012

Vertex arrays vs VBOs

Just came across this post at StackExchange. Particularly interesting is the third link referenced from the accepted answer, which is a presentation from a GDC.

domingo, 5 de febrero de 2012

Syntax highlighting

Looks like I finally have syntax highlighting... thanks to this!
Turns out the previous link worked only for Java and XML and I'm too lazy to compile my own version for supporting other languages, so I'm now resorting to google-code-prettify. To make it work here, I followed this tutorial.
I didn't quite like the default style as I wanted line numbers for every single line. Also, I didn't want it to have a completely white background, so after linking the default CSS file, I overrode some styles:
<style type="text/css">
pre.prettyprint, code.prettyprint {
        background-color: #C4C4C4;
}
li.L0, li.L1, li.L2, li.L3, li.L4, li.L5, li.L6, li.L7, li.L8, li.L9
{
    background: #F7F7F7;
    list-style-type: decimal;
}
</style>
Java example:
public class SyntaxHighlighterWorksFineHere
{
    public static void main(String[] args)
    {
        System.out.println("Yes!");
    }
}
In order to avoid to having to place the script loading line each time, I just added it to the HTML template. Et voilà!