Esempio n. 1
0
static int __draw_poly ( lua_State *_l ) {
    int nargs = lua_gettop(_l);
    int i, num;
    ex_vec2f_t *verts;
    const ex_color4f_t *c;
    bool close;

    // check if the first arguments is table
    luaL_checktype(_l, 1, LUA_TTABLE);
    num = luaL_getn(_l,1);
    verts = (ex_vec2f_t *)ex_malloc( sizeof(ex_vec2f_t) * num );

    for ( i = 0; i < num; ++i ) {
        lua_rawgeti(_l, 1, i+1);
        verts[i] = *ex_lua_tovec2f(_l,-1);
        lua_remove(_l, -1);
    }

    //
    c = &ex_color4f_white;
    close = true;
    if ( nargs == 2 ) {
        c = ex_lua_checkcolor4f(_l,2);
    }
    else if ( nargs == 3 ) {
        c = ex_lua_checkcolor4f(_l,2);
        close = lua_toboolean(_l,3);
    }

    //
    ex_draw_poly ( verts, num, c, close );
    ex_free( verts );

    return 0;
}
Esempio n. 2
0
int main (void) {
    int i = 0;
    void *buffer[BUF_SIZE];

    ex_sdk_init ();

    i = 0;
    while ( i < BUF_SIZE ) {
        buffer[i] = ex_malloc( EX_MB(1) );
        ++i;
    }

    i = 0;
    while ( i < BUF_SIZE ) {
        buffer[i] = ex_realloc( buffer[i], EX_KB(1) * rand() % 2048  );
        ++i;
    }

    i = 0;
    while ( i < BUF_SIZE ) {
        ex_free(buffer[i]);
        ++i;
    }

    ex_sdk_deinit ();
    return 0;
}
Esempio n. 3
0
File: cstr.c Progetto: ifzz/exlibs-c
static void normal () {
    // strcpy
    {
        char to[128];
        const char *text = "hello world";

        // right
        memset ( to, 1, 128 );
        strcpy( to, text );
        printf("for break");
    }

    // strncpy
    {
        char to[128];
        const char *text = "hello world";
        int len = strlen(text);

        // wrong
        memset ( to, 1, 128 );
        strncpy( to, text, len );

        // right
        memset ( to, 1, 128 );
        strncpy( to, text, len );
        to[len] = 0;
        printf("for break");
    }

    // strlen
    {
        char *to;
        const char *text = "hello world";
        int len = strlen(text);

        // wrong
        to = ex_malloc( len );
        strcpy( to, text );
        ex_free(to);

        // right
        to = ex_malloc( len+1 );
        strcpy( to, text );
        ex_free(to);
        printf("for break");
    }
}
Esempio n. 4
0
File: list.c Progetto: exdev/exsdk
ex_list_t *ex_list_alloc ( size_t _element_bytes ) {
    ex_list_t *list = ex_malloc( sizeof(ex_list_t) ); 
    ex_list_init ( list, 
                   _element_bytes,
                   ex_func_alloc,
                   ex_func_realloc,
                   ex_func_dealloc );
    return list;
}
Esempio n. 5
0
File: node.c Progetto: ifzz/exlibs-c
static bt_node_t *__bt_node_alloc ()
{
    bt_node_t *node = ex_malloc( sizeof(bt_node_t) );
    node->children = ex_array_notype ( sizeof(bt_node_t *), 8 );
    node->user_data = NULL;
    node->exec = NULL;
    node->free = NULL;
    return node;
}
Esempio n. 6
0
ex_mempool_t *ex_mempool_alloc ( size_t _element_bytes, size_t _count ) {
    ex_mempool_t *pool = ex_malloc ( sizeof(ex_mempool_t) );
    ex_mempool_init( pool, _element_bytes, _count,
                     ex_func_alloc,
                     ex_func_realloc,
                     ex_func_dealloc
                   );
    return pool;
}
Esempio n. 7
0
TEST( MemMng, leak, off )
{
    const int MAX_COUNT = 2;
    char* pTestBuffer = NULL;
    for ( int i = 0; i < MAX_COUNT; ++i )
    {
        pTestBuffer = (char*)ex_malloc( sizeof(char) ); 
    }
}
Esempio n. 8
0
ex_bitarray_t *ex_bitarray_alloc ( size_t _bitcount )
{
    ex_bitarray_t *bitArray = ex_malloc ( sizeof(ex_bitarray_t) );
    ex_bitarray_init ( bitArray, _bitcount, 
                       __ex_bitarray_alloc,
                       __ex_bitarray_realloc,
                       __ex_bitarray_dealloc );
    return bitArray;
}
Esempio n. 9
0
TEST( MemMng, alloc, off )
{
    const int BUFFER_SIZE = 5;
    const int MAX_COUNT = 256;

    ClassProbe* pBuffer = ex_new_array( ClassProbe, BUFFER_SIZE );
    for ( int i = 0; i < MAX_COUNT; ++i )
    {
        char* pTestBuffer = (char*)ex_malloc( sizeof(char)*BUFFER_SIZE ); 
        ex_free(pTestBuffer);
    }
    ex_delete_array(pBuffer);
}
Esempio n. 10
0
int amt_close(amt_sock_t **pSock)
{
    int res;
    ex_sock_t *pExSock  =  (ex_sock_t *) ex_malloc(sizeof(ex_sock_t));
    AMT_ERR_CHECK(pExSock!=NULL, AMT_ERR, "No Memory");
    *pExSock = **(ex_sock_t **)pSock;
    free(*pSock);
    *pSock=NULL;

    res = ex_close(&pExSock);
    if (res) { return res;}
    return 0;
}
Esempio n. 11
0
/* Löscht das erste Item vom Typ type aus dem Inventar von Spawn
 * Returnt 1, wenn was gelöscht wurde, 0 sonst.
 */
int delete_item_by_type(unsigned int type, Spawn *spawn){
    /* wenn noch mehr im Inventar ist */
    if(spawn->inventory_size > 1) {
        unsigned int new_size = spawn->inventory_size - 1, i, j;
        bool update_selected=false, deleted=false;
        Item** new_inventory = (Item**)ex_malloc(new_size * sizeof(Item*));
        
        for(i = 0, j = 0; i < spawn->inventory_size; ++i) {
            if(spawn->inventory[i]->type != type || deleted) {
                new_inventory[j] = spawn->inventory[i];
                ++j;
            } else {
                free_item(spawn->inventory[i]);
                deleted=true;
                if(i<=spawn->selected_item)
                    update_selected=true;
            }
        }
        free(spawn->inventory);
        spawn->inventory = new_inventory;
        if(deleted){
            --spawn->inventory_size;
            
            if(!spawn->npc && update_selected) {
                /* ausgewähltes Item anpassen */
                spawn->selected_item = (spawn->selected_item > 1) ? spawn->selected_item - 1 : 0;
            }
        	return 1;
        }
        else
            return 0;
    } 
    /* nur letztes Element wird gelöscht */
    else if(spawn->inventory_size == 1 && spawn->inventory[0]->type==type) {
        free_item(spawn->inventory[0]);
        free(spawn->inventory);
        spawn->inventory = NULL;
        --spawn->inventory_size;
        
        if(!spawn->npc) {
            spawn->selected_item = 0;
        }
        return 1;
    }
    else
    	return 0;

}
Esempio n. 12
0
result_t BinarySerializeNode::loadFromBuffer ( void* _pData, uint _bufferSize )
{
    // read raw data
    m_Capacity = uint(_bufferSize);
    m_CurPos = 0;

    if ( m_Capacity > 0 )
    {
        m_pData = ex_malloc( m_Capacity ); 
        ex::mem::cpy( m_pData, _pData, m_Capacity );
    }

    //
    setLoading ();
    return EResult::ok;
}
Esempio n. 13
0
void XmlParser::OnCharacterData ( const tchar* _pData, int _len )
{
    // get white space
    const tchar* white_space_list;
    uint list_count = 0;
    ex::str::getWSList( white_space_list, list_count );

    // skip empty content
    bool validContent = false;
    for ( int i = 0; i < _len; ++i )
    {
        if ( !str::isInTheList( _pData[i], white_space_list, list_count ) )
        {
            validContent = true;
            break;
        }
    }

    // retrun if invalid content ( empty content )
    if ( !validContent )
        return;

    // TODO: implement temp string allocator
    // 
    char* pMallocBuffer = NULL;
    char* pStrBuffer = NULL;
    char str[EX_INT16_MAX+1];

    if ( _len < EX_INT16_MAX+1 )
    {
        pStrBuffer = str;
    }
    else
    {
        pStrBuffer = pMallocBuffer = (char*)ex_malloc( _len + 1 );
    }
    memcpy( pStrBuffer, _pData, _len );
    pStrBuffer[_len] = 0;

    // set contents
    ex_assert( !m_NodeStack.empty(), "Invalid Node Stack" );
    if ( !m_NodeStack.empty() )
    {
        m_NodeStack.top()->SetContent( pStrBuffer );
    }
    ex_free(pMallocBuffer);
}
Esempio n. 14
0
// managed
ex_hashmap_t *ex_hashmap_alloc ( strid_t _key_typeid, size_t _key_bytes, 
                                 strid_t _value_typeid, size_t _value_bytes, 
                                 size_t _count, 
                                 hashkey_t _hashkey, keycmp_t _keycmp )
{
    ex_hashmap_t *hashmap = ex_malloc( sizeof(ex_hashmap_t) );
    ex_hashmap_init ( hashmap, 
                      _key_typeid, _key_bytes, 
                      _value_typeid, _value_bytes, 
                      _count,
                      _hashkey, _keycmp,
                      __ex_hashmap_alloc,
                      __ex_hashmap_realloc,
                      __ex_hashmap_dealloc
                      );
    return hashmap;
}
Esempio n. 15
0
int ex_lua_dofile ( lua_State *_l, const char *_filepath, const char *_modname ) {
    int status;
    ex_file_t *file;
    size_t buf_size;
    void *buffer;

    // open the file
    file = ex_fopen_r(_filepath);
    if ( file == NULL ) {
        ex_error ( "can't find the file %s", _filepath );
        return -1;
    }

    // get the file to the buffer we allocated.
    buf_size = ex_fsize (file);
    buffer = ex_malloc (buf_size);
    ex_fread (file, buffer, buf_size );
    ex_fclose(file);

    // parse the buffer by lua interpreter
    status = luaL_loadbuffer( _l, (const char *)buffer, buf_size, _filepath );
    if ( status ) {
        ex_lua_alert(_l);
        goto PARSE_FAILED;
    }

    // call the script 
    if ( _modname ) {
        lua_pushstring(_l,_modname);
        status = lua_pcall(_l, 1, LUA_MULTRET, 0);
    }
    else {
        status = lua_pcall(_l, 0, LUA_MULTRET, 0);
    }
    if ( status ) {
        ex_lua_alert(_l);
        goto PARSE_FAILED;
    }

PARSE_FAILED:
    ex_free(buffer);
    return -1;
}
Esempio n. 16
0
BinarySerializeNode::BinarySerializeNode ( uint _bytesToReserve, bool _isSaving )
    : ISerializeNode ()
    , m_pData (NULL)
    , m_Capacity (0)
    , m_CurPos (0)
{
    //
    if ( _isSaving )
        setSaving ();
    else
        setLoading ();

    //
    if ( _bytesToReserve >= 0 )
    {

        m_pData = ex_malloc ( _bytesToReserve );
        m_Capacity = _bytesToReserve;
        m_CurPos = 0;
    }
}
Esempio n. 17
0
File: wiz.c Progetto: ifzz/exsdk
void wiz_create_window ( lua_State *_l, int _refID, int _w, int _h ) {
    SDL_Window *sdl_window;
    SDL_Renderer *sdl_renderer;
    __window_info_t *win_info;

    // create SDL window
    sdl_window = SDL_CreateWindow( "Window",
                                   SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
                                   _w, _h,
                                   SDL_WINDOW_RESIZABLE | SDL_WINDOW_SHOWN /*| SDL_WINDOW_BORDERLESS*/
                                 );
    if ( !sdl_window ) {
        ex_log ( "[SDL] Error: Could not create window: %s", SDL_GetError() );
        return;
    }

    // create SDL renderer
    sdl_renderer = SDL_CreateRenderer( sdl_window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC );
    if ( !sdl_renderer ) {
        ex_log ( "[SDL] Error: Could not create renderer: %s", SDL_GetError() );
        return;
    }

    // add window info
    win_info = ex_malloc( sizeof(__window_info_t) );
    win_info->sdl_window = sdl_window;
    win_info->sdl_renderer = sdl_renderer;
    win_info->refID = _refID;
    win_info->dirty = true;
    ex_color3u_set ( &win_info->bg_color, 255, 255, 255 );

    // if this is the first window, create queue
    if ( ex_array_count(__window_list) == 0 ) {
        primary_win_info = win_info;
        ex_set_main_sdl_renderer(sdl_renderer);
    }
    ex_array_add ( __window_list, &win_info );
}
Esempio n. 18
0
/*---------------------------------------------------------------------------*/
void delete_item(Item *item, Spawn *spawn) {
    /* wenn noch mehr im Inventar ist */
    if(spawn->inventory_size > 1) {
        unsigned int new_size = spawn->inventory_size - 1, i, j;
        Item** new_inventory = (Item**)ex_malloc(new_size * sizeof(Item*));
        
        for(i = 0, j = 0; i < spawn->inventory_size; ++i) {
            if(spawn->inventory[i] != item) {
                new_inventory[j] = spawn->inventory[i];
                ++j;
            } else {
                free_item(spawn->inventory[i]);
            }
        }
        
        --spawn->inventory_size;
        free(spawn->inventory);
        spawn->inventory = new_inventory;
        
        if(!spawn->npc) {
            /* ausgewähltes Item anpassen */
            spawn->selected_item = (spawn->selected_item > 1) ? spawn->selected_item - 1 : 0;
        }
    } 
    /* nur letztes Element wird gelöscht */
    else if(spawn->inventory_size == 1) {
        free_item(spawn->inventory[0]);
        free(spawn->inventory);
        spawn->inventory = NULL;
        --spawn->inventory_size;
        
        if(!spawn->npc) {
            spawn->selected_item = 0;
        }
    }
}
Esempio n. 19
0
File: main.c Progetto: MarcelHB/c37
int main(int argc, char *argv[]){
	Spawn *player;
	InterfaceData idata = {0, NULL, -1, NULL, 1};
	SDL_Event event;
	int num_tiles = OUTPUT_IN_GLYPHS_X * OUTPUT_IN_GLYPHS_Y, i;
	
	/*SDL anmachen*/
    if(SDL_Init(SDL_INIT_VIDEO))
        return EXIT_FAILURE;
    SDL_EnableKeyRepeat(200, 50);
    
    /*Karte laden*/
    if(argc==2)
        map=load_map(argv[1]);
    else{
        fprintf(stderr,"Kartennamen angeben\n");
        return EXIT_FAILURE;
    }
    if(map == NULL) {
        fprintf(stderr,"Fehler beim Laden der Karte\n");
        return EXIT_FAILURE;
    }
    
    player = get_player_spawn(map);
    if(player==NULL){
        fprintf(stderr, "Kein Spieler auf der Karte\n");
        return EXIT_FAILURE;
    }
    
    /*Map zeichnen*/
    
    /* Ausgabepuffer initialisieren */
    buf = (BufferTile*)ex_malloc(sizeof(BufferTile) * num_tiles);
    for(i = 0; i < num_tiles; ++i) {
        BufferTile bt = {' ', 0x00000000};
        buf[i] = bt;
    }
    
    output_init(OUTPUT_IN_GLYPHS_X, OUTPUT_IN_GLYPHS_Y, map->name);
    
    explore_area(player, map);
    create_output_buffer(map, buf, num_tiles);
    get_interface_data(map, &idata);
    output_draw(buf, num_tiles, &idata);
    
    /*Eingabeloop*/
    int quit=0;
    KeyAction current_action = INVALID;
    while(SDL_WaitEvent(&event)){
        if(event.type == SDL_KEYDOWN) {
            current_action = get_action(event.key.keysym.sym);
            /*bei Escape beenden*/
            if(event.key.keysym.sym == SDLK_ESCAPE){
                quit=1;
                break;
            }
            if(current_action != INVALID) {
                process_event(current_action, map);
            }
            create_output_buffer(map, buf, num_tiles);
            get_interface_data(map, &idata);
            output_draw(buf, num_tiles, &idata);
        } else if(event.type == SDL_QUIT) {
            quit=1;
            break;
        }
        SDL_Delay(1);
        /*Affe tot => Klappe zu*/
        if(player->hp<=0){
            game_over(0);
            break;
        }
        /* Ende erreicht */
        if(map->finished) {
            game_over(1);
            break;
        }
    }
    if(!quit){
        while(SDL_WaitEvent(&event)){
            /*bei Escape beenden*/
            if(event.type == SDL_QUIT || (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_ESCAPE)) {
                break;
            }
            SDL_Delay(1);
        }
    }
    free(buf);
    flush_map(map);
    free(idata.message); free(idata.item_name);
    
    output_close();
    SDL_Quit();
    return EXIT_SUCCESS;
}
Esempio n. 20
0
static int __lua_font_wrap_text ( lua_State *_l ) {
    ex_font_t *font;
    const char *text, *whitespace;
    int maxWidth;

    const char *str, *nextstr, *word_start;
    char *newtext, *newtext_p, *last_newtext;
    int ch, next_ch, len, newlen, cpylen;
    uint ft_index, prev_ft_index;
    int cur_x, word_start_x;
    ex_glyph_t *glyph;
    bool linebreak, beginningOfLine, trimWhitespace, skipcpy; 
    bool wrapword, collapseSpace, collapseLinebreak;

    // get lua arguments
    ex_lua_check_nargs(_l,5);
    text = luaL_checkstring(_l,1);
    luaL_checktype( _l, 2, LUA_TLIGHTUSERDATA );
    font = lua_touserdata(_l,2);
    whitespace = luaL_checkstring(_l,3);
    maxWidth = luaL_checkint(_l,4);
    trimWhitespace = beginningOfLine = (luaL_checkint(_l,5) == 1);

    //
    len = strlen(text);
    str = nextstr = word_start = text;
    newtext_p = newtext = last_newtext = ex_malloc( len * sizeof(char) );
    prev_ft_index = -1;

    // get wrapMode
    wrapword = false;
    collapseSpace = collapseLinebreak = false;
    if ( !strncmp( whitespace, "pre-wrap", 8 ) ) {
        wrapword = true;
        collapseSpace = false;
        collapseLinebreak = false;
    }
    else if ( !strncmp( whitespace, "pre-line", 8 ) ) {
        wrapword = true;
        collapseSpace = true;
        collapseLinebreak = false;
    }
    else if ( !strncmp( whitespace, "normal", 6 ) ) {
        wrapword = true;
        collapseSpace = true;
        collapseLinebreak = true;
    }
    else if ( !strncmp( whitespace, "nowrap", 6 ) ) {
        wrapword = false;
        collapseSpace = true;
        collapseLinebreak = true;
    }
    else if ( !strncmp( whitespace, "pre", 3 ) ) {
        wrapword = false;
        collapseSpace = false;
        collapseLinebreak = false;
    }

    // process text
    cur_x = word_start_x = 0;
    linebreak = false;
    while ( *str ) {
        skipcpy = false;
        nextstr += utf8proc_iterate ((const uint8_t *)str, -1, &ch);

        // if this is line-break
        if ( ch == '\n' || ch == '\r' ) {
            if ( collapseLinebreak ) {
                ch = ' '; // turn it to space
            }
            else {
                linebreak = true;
            }
        }

        // if this is space 
        if ( ch == ' ' || ch == '\t' || ch == '\f' ) {
            if ( collapseSpace ) {
                const char * nextnextstr = nextstr;
                while ( *nextnextstr ) {
                    nextstr = nextnextstr;
                    nextnextstr += utf8proc_iterate ((const uint8_t *)nextnextstr, -1, &next_ch);

                    // if next_ch is white-space, then collapse this char
                    if ( next_ch == ' ' || next_ch == '\t' || next_ch == '\f' ) {
                        str = nextstr;
                        continue;
                    }

                    // if next_ch is line-break and collapseLinebreak is true, then collapse this char
                    if ( next_ch == '\n' || next_ch == '\r' ) {
                        if ( collapseLinebreak ) {
                            str = nextstr;
                            continue;
                        }
                    }

                    //
                    break;
                }

                // skip first-time collapse
                if ( trimWhitespace ) {
                    trimWhitespace = false;
                    str = nextstr;
                    continue;
                }

                // yes, must turn it to space to make sure only one space
                ch = ' ';
            }
        }

        //
        trimWhitespace = false;

        // process word-break, word-wrap
        if ( wrapword ) {
            word_start = str;
            word_start_x = cur_x;

            // if this character can break
            if ( nextstr == NULL || __can_word_break (ch) ) {
                // advanced character
                ft_index = ex_font_get_index ( font, ch );
                glyph = ex_font_get_glyph ( font, ft_index );
                cur_x += ex_font_get_kerning( font, prev_ft_index, ft_index );
                cur_x += glyph->advance_x;
                prev_ft_index = ft_index;

                // check if the word exceed content width
                if ( cur_x > maxWidth ) {
                    if ( !beginningOfLine ) {
                        linebreak = true;

                        // skip copy the white-space if it is at the end of the wrap
                        if ( ch == ' ' || ch == '\t' || ch == '\f' ) {
                            skipcpy = true;
                        }
                        else {
                            nextstr = word_start;
                            cur_x = word_start_x;
                        }
                    }
                }

                beginningOfLine = false;
            }
            else {
                // advanced current character
                ft_index = ex_font_get_index ( font, ch );
                glyph = ex_font_get_glyph ( font, ft_index );
                cur_x += ex_font_get_kerning( font, prev_ft_index, ft_index );
                cur_x += glyph->advance_x;
                prev_ft_index = ft_index;

                const char * nextnextstr = nextstr;
                while ( *nextnextstr ) {
                    nextstr = nextnextstr;
                    nextnextstr += utf8proc_iterate ((const uint8_t *)nextnextstr, -1, &next_ch);

                    // if this character can break
                    if ( __can_word_break (next_ch) ) {
                        break;
                    }

                    // advanced character
                    ft_index = ex_font_get_index ( font, next_ch );
                    glyph = ex_font_get_glyph ( font, ft_index );
                    cur_x += ex_font_get_kerning( font, prev_ft_index, ft_index );
                    cur_x += glyph->advance_x;
                    prev_ft_index = ft_index;

                    // TODO: process word-break
                    // check if the word exceed content width
                    if ( cur_x > maxWidth ) {
                        if ( !beginningOfLine ) {
                            linebreak = true;

                            nextstr = word_start;
                            cur_x = word_start_x;
                            skipcpy = true;
                            break;
                        }
                    }
                }
            }
        }
        else {
            // advanced character
            ft_index = ex_font_get_index ( font, ch );
            glyph = ex_font_get_glyph ( font, ft_index );
            cur_x += ex_font_get_kerning( font, prev_ft_index, ft_index );
            cur_x += glyph->advance_x;
            prev_ft_index = ft_index;
        } 

        // copy character to newtext_p
        if ( !skipcpy ) {
            cpylen = nextstr - str;
            if ( cpylen > 0 ) {
                strncpy( newtext_p, str, cpylen);
                newtext_p += cpylen;
            }
        }

        // step
        str = nextstr;
        if ( linebreak ) {
            break;
        }
    }

    // text1
    newlen = newtext_p-newtext;
    if ( newlen > 0 ) {
        lua_pushlstring(_l, newtext, newlen);
    }
    else {
        lua_pushnil(_l);
    }

    // text2
    if ( linebreak && *str ) {
        lua_pushstring(_l, str );
    }
    else {
        lua_pushnil(_l);
    }
    lua_pushinteger(_l,cur_x); // width
    lua_pushboolean(_l,linebreak); // line-break

    //
    ex_free(newtext);

    return 4; // text1(can be nil), text2(can be nil), width of text1, linebreak
}