Esempio n. 1
0
def get_string_int_map(map_func, hash_func, string_map, case_sensitive=True):
    strings = list(string_map.iterkeys())
    writer = CodeWriter()
    if strings:
        hash_data = get_hash_function(hash_func, strings, case_sensitive)
        writer.putlnc(hash_data.code)
        hashes = dict((v, k) for (k, v) in hash_data.strings.iteritems())

    writer.putmeth('int %s' % map_func, 'const std::string & in')
    writer.putln('if (in.empty())')
    writer.indent()
    writer.putln('return -1;')
    writer.dedent()

    if strings:
        writer.putlnc('unsigned int hash = %s(&in[0], in.size());',
                      hash_func)
        writer.putlnc('switch (hash) {')
        writer.indent()
        for i in xrange(hash_data.max_hash_value + 1):
            value = hashes.get(i, None)
            if value is None:
                continue
            else:
                value = string_map[value]
            writer.putlnc('case %s: return %s;', i, value)
        writer.end_brace()
    writer.putlnc('return -1;')
    writer.end_brace()
    return writer.get_data()
Esempio n. 2
0
def get_string_int_map(map_func, hash_func, string_map, case_sensitive=True):
    strings = list(string_map.iterkeys())
    writer = CodeWriter()
    if strings:
        hash_data = get_hash_function(hash_func, strings, case_sensitive)
        writer.putlnc(hash_data.code)
        hashes = dict((v, k) for (k, v) in hash_data.strings.iteritems())

    writer.putmeth('int %s' % map_func, 'const std::string & in')
    writer.putln('if (in.empty())')
    writer.indent()
    writer.putln('return -1;')
    writer.dedent()

    if strings:
        writer.putlnc('unsigned int hash = %s(&in[0], in.size());', hash_func)
        writer.putlnc('switch (hash) {')
        writer.indent()
        for i in xrange(hash_data.max_hash_value + 1):
            value = hashes.get(i, None)
            if value is None:
                continue
            else:
                value = string_map[value]
            writer.putlnc('case %s: return %s;', i, value)
        writer.end_brace()
    writer.putlnc('return -1;')
    writer.end_brace()
    return writer.get_data()
Esempio n. 3
0
def main():
    import sys
    sys.path.append('..')
    sys.path.append('../..')
    from chowdren.codewriter import CodeWriter
    writer = CodeWriter('buttonmap.cpp')
    writer.putmeth('int remap_button', 'int n')
    for k, v in maps.iteritems():
        writer.putraw('#ifdef %s' % k)
        writer.putlnc('switch (n) {')
        writer.indent()
        for f, t in v:
            writer.putlnc('case %s: return %s;', f, t)
        writer.end_brace()
        writer.putraw('#endif')
    writer.putlnc('return n;')
    writer.end_brace()

    writer.putmeth('int unremap_button', 'int n')
    for k, v in maps.iteritems():
        writer.putraw('#ifdef %s' % k)
        writer.putlnc('switch (n) {')
        writer.indent()
        for f, t in v:
            writer.putlnc('case %s: return %s;', t, f)
        writer.end_brace()
        writer.putraw('#endif')
    writer.putlnc('return n;')
    writer.end_brace()
    writer.close()
Esempio n. 4
0
def main():
    import sys
    sys.path.append('..')
    sys.path.append('../..')
    from chowdren.codewriter import CodeWriter
    writer = CodeWriter('buttonmap.cpp')
    writer.putmeth('int remap_button', 'int n')
    for k, v in maps.iteritems():
        writer.putraw('#ifdef %s' % k)
        writer.putlnc('switch (n) {')
        writer.indent()
        for f, t in v:
            writer.putlnc('case %s: return %s;', f, t)
        writer.end_brace()
        writer.putraw('#endif')
    writer.putlnc('return n;')
    writer.end_brace()

    writer.putmeth('int unremap_button', 'int n')
    for k, v in maps.iteritems():
        writer.putraw('#ifdef %s' % k)
        writer.putlnc('switch (n) {')
        writer.indent()
        for f, t in v:
            writer.putlnc('case %s: return %s;', t, f)
        writer.end_brace()
        writer.putraw('#endif')
    writer.putlnc('return n;')
    writer.end_brace()
    writer.close()
Esempio n. 5
0
def main():
    # generate key definition files
    import sys
    sys.path.append('..')
    from chowdren.codewriter import CodeWriter
    from chowdren.common import get_base_path
    from chowdren.stringhash import get_string_int_map
    import os

    writer = CodeWriter(os.path.join(get_base_path(), 'keyconv.cpp'))

    # write keys file
    writer.putln('#include <string>')
    writer.putln('#include "keydef.h"')
    writer.putln('#include "stringcommon.h"')
    writer.putln('')

    writer.putmeth('int translate_vk_to_key', 'int vk')
    writer.putln('switch (vk) {')
    writer.indent()
    for vk, name in VK_TO_SDL.iteritems():
        writer.putln('case %s: return %s;' % (vk, name[0]))
    writer.end_brace()
    writer.putln('return -1;')
    writer.end_brace()
    writer.putln('')

    string_map = {}
    for vk, name in VK_TO_SDL.iteritems():
        string_name = VK_TO_NAME.get(vk, None)
        if string_name is None:
            continue
        string_map[string_name] = name[0]

    writer.putln(
        get_string_int_map('translate_string_to_key', 'get_key_string_hash',
                           string_map, False))

    writer.putmeth('std::string translate_vk_to_string', 'int vk')
    writer.putln('switch (vk) {')
    writer.indent()
    for vk, name in VK_TO_SDL.iteritems():
        string_name = VK_TO_NAME.get(vk, None)
        if string_name is None:
            continue
        writer.putlnc('case %s: return %r;', vk, string_name)
    writer.end_brace()
    writer.putln('return "";')
    writer.end_brace()
    writer.putln('')

    writer.putmeth('std::string translate_key_to_string', 'int key')
    writer.putln('switch (key) {')
    writer.indent()
    for name, string_name in KEY_TO_NAME.iteritems():
        writer.putlnc('case %s: return %r;', name, string_name)
    writer.end_brace()
    writer.putln('return "";')
    writer.end_brace()

    # keys = {}
    # for name, value in SDL_KEYS.iteritems():
    #     if isinstance(value, str):
    #         value = ord(value)
    #     keys[value] = name

    # writer.putmeth('int translate_key_to_index', 'int key')
    # writer.putln('switch (key) {')
    # writer.indent()
    # for index, value in enumerate(sorted(keys)):
    #     name = keys[value]
    #     writer.putlnc('case %s: return %r;', name, index)
    # writer.end_brace()
    # writer.putln('return -1;')
    # writer.end_brace()

    writer.close()
Esempio n. 6
0
def write_shaders():
    code = CodeWriter(os.path.join(get_base_path(), 'shaders.cpp'))

    for shader in SHADERS:
        shader_name = '%sShader' % shader.name
        code.putlnc('class %s : public BaseShader', shader_name)
        code.start_brace()
        code.put_access('public')

        for uniform in shader.uniforms:
            code.putlnc('static int %s;', uniform[0])

        if shader.uniforms:
            code.putln('')

        asset_name = 'SHADER_%s' % shader.asset_name.upper()
        args = [asset_name]
        options = []
        if shader.has_back:
            options.append('SHADER_HAS_BACK')
        if shader.has_tex_size:
            options.append('SHADER_HAS_TEX_SIZE')
        if not options:
            if shader.tex_param:
                args.append('0')
        else:
            args.append(' | '.join(options))
        if shader.tex_param:
            args.append('"%s"' % shader.tex_param)

        code.putlnc('%s()', shader_name)
        code.putlnc(': BaseShader(%s)', ', '.join(args))
        code.start_brace()
        code.end_brace()

        code.putln('')

        code.putmeth('void initialize_parameters')
        for uniform in shader.uniforms:
            code.putlnc('%s = get_uniform(%r);', uniform[0], uniform[0],
                        cpp=False)
        code.end_brace()
        code.putln('')

        code.putmeth('static void set_parameters', 'FrameObject * instance')

        for uniform in shader.uniforms:
            param = 'SHADER_PARAM_%s' % uniform[0].upper()
            code.putlnc('BaseShader::set_%s(instance, %s, %s);', uniform[1],
                        param, uniform[0])
        if shader.tex_param:
            param = 'SHADER_PARAM_%s' % shader.tex_param.upper()
            code.putlnc('BaseShader::set_image(instance, %s);', param)
        code.end_brace()

        code.end_brace(True)

        for uniform in shader.uniforms:
            code.putlnc('int %s::%s;', shader_name, uniform[0])

        code.putln('')

    # write static init code
    for shader in SHADERS:
        shader_type = '%sShader' % shader.name
        shader_name = '%s_shader' % shader.name.lower()
        code.putlnc('%s %s;', shader_type, shader_name)

    code.close()
Esempio n. 7
0
def main():
    # generate key definition files
    import sys
    sys.path.append('..')
    from chowdren.codewriter import CodeWriter
    from chowdren.common import get_base_path
    from chowdren.stringhash import get_string_int_map
    import os

    writer = CodeWriter(os.path.join(get_base_path(), 'keyconv.cpp'))

    # write keys file
    writer.putln('#include "chowstring.h"')
    writer.putln('#include "keydef.h"')
    writer.putln('#include "stringcommon.h"')
    writer.putln('')

    writer.putmeth('int translate_vk_to_key', 'int vk')
    writer.putln('switch (vk) {')
    writer.indent()
    for vk, name in VK_TO_SDL.iteritems():
        writer.putln('case %s: return %s;' % (vk, name[0]))
    writer.end_brace()
    writer.putln('return -1;')
    writer.end_brace()
    writer.putln('')

    string_map = {}
    for vk, name in VK_TO_SDL.iteritems():
        string_name = VK_TO_NAME.get(vk, None)
        if string_name is None:
            continue
        string_map[string_name] = name[0]

    writer.putln(get_string_int_map('translate_string_to_key',
                                    'get_key_string_hash',
                                    string_map, False))

    writer.putmeth('chowstring translate_vk_to_string',
                   'int vk')
    writer.putln('switch (vk) {')
    writer.indent()
    for vk, name in VK_TO_SDL.iteritems():
        string_name = VK_TO_NAME.get(vk, None)
        if string_name is None:
            continue
        writer.putlnc('case %s: return %r;', vk, string_name)
    writer.end_brace()
    writer.putln('return "";')
    writer.end_brace()
    writer.putln('')

    writer.putmeth('chowstring translate_key_to_string',
                   'int key')
    writer.putln('switch (key) {')
    writer.indent()
    for name, string_name in KEY_TO_NAME.iteritems():
        writer.putlnc('case %s: return %r;', name, string_name)
    writer.end_brace()
    writer.putln('return "";')
    writer.end_brace()

    # keys = {}
    # for name, value in SDL_KEYS.iteritems():
    #     if isinstance(value, str):
    #         value = ord(value)
    #     keys[value] = name

    # writer.putmeth('int translate_key_to_index', 'int key')
    # writer.putln('switch (key) {')
    # writer.indent()
    # for index, value in enumerate(sorted(keys)):
    #     name = keys[value]
    #     writer.putlnc('case %s: return %r;', name, index)
    # writer.end_brace()
    # writer.putln('return -1;')
    # writer.end_brace()

    writer.close()
Esempio n. 8
0
def main():
    # generate key definition files
    from chowdren.codewriter import CodeWriter
    from chowdren.common import get_base_path
    import os

    writer = CodeWriter(os.path.join(get_base_path(), 'keyconv.cpp'))

    # write keys file
    writer.putln('#include <string>')
    writer.putln('#include "keydef.h"')
    writer.putln('#include "stringcommon.h"')
    writer.putln('')

    writer.putmeth('int translate_vk_to_key', 'int vk')
    writer.putln('switch (vk) {')
    writer.indent()
    for vk, name in VK_TO_SDL.iteritems():
        writer.putln('case %s: return %s;' % (vk, name[0]))
    writer.end_brace()
    writer.putln('return -1;')
    writer.end_brace()
    writer.putln('')

    writer.putmeth('int translate_string_to_key',
                   'const std::string & in_name')
    writer.putln('std::string name = in_name;')
    writer.putln('to_lower(name);')
    for vk, name in VK_TO_SDL.iteritems():
        string_name = VK_TO_NAME.get(vk, None)
        if string_name is None:
            continue
        string_name = string_name.lower()
        writer.putlnc('if (name.compare(0, %s, %r) == 0) return %s;',
                      len(string_name), string_name, name[0], cpp=False)
    writer.putln('return -1;')
    writer.end_brace()
    writer.putln('')

    writer.putmeth('std::string translate_vk_to_string',
                   'int vk')
    writer.putln('switch (vk) {')
    writer.indent()
    for vk, name in VK_TO_SDL.iteritems():
        string_name = VK_TO_NAME.get(vk, None)
        if string_name is None:
            continue
        writer.putlnc('case %s: return %r;', vk, string_name)
    writer.end_brace()
    writer.putln('return "";')
    writer.end_brace()
    writer.putln('')

    writer.putmeth('std::string translate_key_to_string',
                   'int key')
    writer.putln('switch (key) {')
    writer.indent()
    for name, string_name in KEY_TO_NAME.iteritems():
        writer.putlnc('case %s: return %r;', name, string_name)
    writer.end_brace()
    writer.putln('return "";')
    writer.end_brace()

    writer.close()