Exemplo n.º 1
0
// TODO: Use some magic here to allocate right size pools
bool mml_deserialize(MMLObject* mml, const char* string) {
	assert(mml);
	assert(string);

	mml->node_pool = darray_create(sizeof(MMLNode), 0);
	mml->str_pool = darray_create(sizeof(char), 0);

	// Tokenize
	DArray tokens;
	bool result = mml_tokenize(mml, string, &tokens);
	if(!result) {
		darray_free(&tokens);
		mml_free(mml);
		return false;
	}	

	// Parse
	result = mml_parse(mml, &tokens);
	if(!result) {
		darray_free(&tokens);
		mml_free(mml);
		return false;
	}	

	darray_free(&tokens);

	return true;
}	
Exemplo n.º 2
0
void mml_empty(MMLObject* mml) {
	assert(mml);

	mml->node_pool = darray_create(sizeof(MMLNode), 0);
	mml->str_pool = darray_create(sizeof(char), 0);

	// This code produces warning in release mode:
	//NodeIdx root = mml_node(mml, "root", "_");
	//assert(root == 0);
	mml_node(mml, "root", "_");
}	
Exemplo n.º 3
0
void
hashmap_add(hashmap *map, const char *key, void *data, boolean is_alias)
{
	hashnode *node;
	uint16_t bucket;

	assert(map);
	assert(key);
	assert(data);

	if ((node = malloc(sizeof(hashnode))) == NULL)
	{
		quit_error(POUTOFMEM);
	}

	node->key = key;
	node->data = data;
	node->hash = hashmap_hash(key);
	node->is_alias = is_alias;

	bucket = node->hash % map->buckets;

	if (!map->data[bucket])
	{
		map->data[bucket] = darray_create();
		darray_push(map->data[bucket], node);
	}
	else
	{
		darray_push(map->data[bucket], node);
	}
}
Exemplo n.º 4
0
Arquivo: graph.c Projeto: vdt/libcore
DArray* graph_get_edges(const Graph *g)
{
    unsigned long index;
    DArray *edges;
    Vertex *v;

    assert(g != NULL);

    edges = darray_create();

    if(NULL == edges) {
        fprintf(stderr, "Cannot create edge list (%s:%d)\n",
                __FUNCTION__, __LINE__);
        return NULL;
    }

    for(index = 0; index < darray_size(g->vertices); index++) {
        v = (Vertex *)darray_index(g->vertices, index);

        if(darray_concat(edges, v->edges) < 0) {
            fprintf(stderr, "Cannot construct edge list (%s:%d)\n",
                    __FUNCTION__, __LINE__);
            darray_free(edges);
            return NULL;
        }
    }

    return edges;
}
Exemplo n.º 5
0
Arquivo: graph.c Projeto: vdt/libcore
DArray* graph_vertex_get_out_edges(const Graph *g, const Vertex *v)
{
    unsigned long index;
    DArray *out_edges;
    Edge *e;

    assert(g != NULL);
    assert(v != NULL);

    out_edges = darray_create();

    if(NULL == out_edges) {
        fprintf(stderr, "Cannot create out-edge list (%s:%d)\n",
                __FUNCTION__, __LINE__);
        return NULL;
    }

    for(index = 0; index < darray_size(v->edges); index++) {
        e = (Edge *)darray_index(v->edges, index);
        if(v == e->source) {
            if(darray_append(out_edges, e) < 0) {
                fprintf(stderr, "Cannot construct out-edge list (%s:%d)\n",
                        __FUNCTION__, __LINE__);
                darray_free(out_edges);
                return NULL;
            }
        }
    }

    return out_edges;
}
Exemplo n.º 6
0
Arquivo: graph.c Projeto: vdt/libcore
Vertex* vertex_create(void *data)
{
    Vertex *new_vertex;

    new_vertex = malloc(sizeof(struct _vertex));
    if(NULL == new_vertex) {
        fprintf(stderr, "Out of memory (%s:%d)\n", __FUNCTION__, __LINE__);
        return NULL;
    }

    new_vertex->idx = -1;
    new_vertex->in_degree = 0;
    new_vertex->out_degree = 0;
    new_vertex->data = data;

    new_vertex->edges = darray_create();
    if(NULL == new_vertex->edges) {
        fprintf(stderr, "Can't create edge list (%s:%d)\n",
                __FUNCTION__, __LINE__);
        free(new_vertex);
        return NULL;
    }

    return new_vertex;
}
Exemplo n.º 7
0
int Server_queue_init()
{
    SERVER_QUEUE = darray_create(sizeof(Server), 100);
    check(SERVER_QUEUE != NULL, "Failed to create server management queue.");
    return 0;
error:
    return -1;
}
Exemplo n.º 8
0
Server *Server_create(bstring uuid, bstring default_host,
        bstring bind_addr, int port, bstring chroot, bstring access_log,
        bstring error_log, bstring pid_file, bstring control_port, int use_ssl)
{
    Server *srv = NULL;
    int rc = 0;

    srv = h_calloc(sizeof(Server), 1);
    check_mem(srv);

    srv->hosts = RouteMap_create(host_destroy_cb);
    check(srv->hosts != NULL, "Failed to create host RouteMap.");

    srv->handlers = darray_create(sizeof(Handler), 20);
    check_mem(srv->handlers);

    check(port > 0, "Invalid port given, must be > 0: %d", port);
    srv->port = port;
    srv->listen_fd = 0;

    srv->bind_addr = bstrcpy(bind_addr); check_mem(srv->bind_addr);
    srv->uuid = bstrcpy(uuid); check_mem(srv->uuid);

    // TODO: once mbedtls supports opening urandom early and keeping it open,
    //   put the rng initialization back here (before chroot)
    //if(use_ssl) {
    //    rc = Server_init_rng(srv);
    //    check(rc == 0, "Failed to initialize rng for server %s", bdata(uuid));
    //}

    if(blength(chroot) > 0) {
        srv->chroot = bstrcpy(chroot); check_mem(srv->chroot);
    } else {
        srv->chroot = NULL;
    }
    srv->access_log = bstrcpy(access_log); check_mem(srv->access_log);
    srv->error_log = bstrcpy(error_log); check_mem(srv->error_log);
    srv->pid_file = bstrcpy(pid_file); check_mem(srv->pid_file);
    if(blength(control_port) > 0) {
        srv->control_port = bstrcpy(control_port); check_mem(srv->control_port);
    } else {
        srv->control_port = NULL;
    }
    srv->default_hostname = bstrcpy(default_host);
    srv->use_ssl = use_ssl;
    srv->created_on = time(NULL);

    if(srv->use_ssl) {
        rc = Server_init_ssl(srv);
        check(rc == 0, "Failed to initialize ssl for server %s", bdata(uuid));
    }

    return srv;

error:
    Server_destroy(srv);
    return NULL;
}
Exemplo n.º 9
0
int Filter_init()
{
    REGISTERED_FILTERS = darray_create(sizeof(darray_t *), EVENT_END - EVENT_MIN);
    check_mem(REGISTERED_FILTERS);

    return 0;
error:
    return -1;
}
Exemplo n.º 10
0
static inline int setup_ssl_session_cache()
{
    if(SSL_SESSION_CACHE == NULL) {
        SSL_SESSION_CACHE = darray_create(SSL_INITIAL_CACHE_SIZE, sizeof(ssl_session));
        check_mem(SSL_SESSION_CACHE);
    }
    return 0;
error:
    return -1;
}
Exemplo n.º 11
0
char* mml_serialize_compact(MMLObject* mml) {
	DArray out;
	out = darray_create(sizeof(char), 0);

	_serialize_compact(mml, &out, 0);
	darray_append(&out, "\0");

	// Look at mml_serialize for explanation
	return (char*)out.data;
}	
Exemplo n.º 12
0
char* mml_serialize(MMLObject* mml) {
	DArray out;
	out = darray_create(sizeof(char), 0);

	_serialize(mml, &out, 0, 0);
	darray_append(&out, "\0");

	// This is a hack, but saves one alloc/free pair.
	// Freeing out.data is mostly the same as calling darray_free.
	return (char*)out.data;
}	
Exemplo n.º 13
0
char *test_create()
{
	array = darray_create(sizeof(int), 100);
	mu_assert(array!=NULL, "darray create failed.");
	mu_assert(array->contents!=NULL, "contents are wrong in darray");
	mu_assert(array->end==0, "end isn't at the right spot");
	mu_assert(array->element_size==sizeof(int), "element size is wrong.");
	mu_assert(array->max==100, "wrong max length on initial size");

	return NULL;
}
Exemplo n.º 14
0
static inline darray_t *Filter_lookup_create(StateEvent next)
{
    darray_t *filts = Filter_lookup(next);

    if(filts == NULL) {
        // lazy load them
        filts = darray_create(sizeof(Filter), 10);
        check_mem(filts);
        darray_set(REGISTERED_FILTERS, next - EVENT_MIN, filts);
    }

    return filts;
error:
    return NULL;
}
Exemplo n.º 15
0
void video_draw_line(uint layer, const Vector2* start,
					 const Vector2* end, Color color) {
	assert(layer < bucket_count);
	assert(start);
	assert(end);

	LineDesc new_line;
	new_line.start = *start;
	new_line.end = *end;
	new_line.color = color;

	if(!line_buckets[layer].reserved)
		line_buckets[layer] = darray_create(sizeof(LineDesc), 32);
	darray_append(&line_buckets[layer], &new_line);
}
Exemplo n.º 16
0
Hashmap *hashmap_create(Hashmap_compare compare, Hashmap_hash hash) {
    Hashmap *map = calloc(1, sizeof(Hashmap));
    check_mem(map);

    map->compare = compare == NULL ? default_compare : compare;
    map->hash = (hash == NULL) ? default_hash : hash;
    map->buckets = darray_create(sizeof(DArray *), DEFAULT_NUMBER_OF_BUCKETS);
    check_mem(map->buckets);
    map->buckets->end = map->buckets->max; // fake out expanding it

    return map;
error:
    if (map) {
        hashmap_destroy(map);
    }
    return NULL;
}
Exemplo n.º 17
0
DArray _gen_edges(DArray points, DArray geometry) {
	DArray edges = darray_create(sizeof(Edge), 0);
	Vector2* points_vec2 = DARRAY_DATA_PTR(points, Vector2);
	for(uint i = 0; i < points.size; ++i) {
		for(uint j = 0; j < points.size; ++j) {
			if(i >= j)
				continue;

			float sqr_dist = _node_distance_sq(points_vec2[i], points_vec2[j]);

			if(sqr_dist <= max_edge_distance*max_edge_distance) {
				Edge n = {i, j};

				Segment s = ai_shortest_path(points_vec2[i], points_vec2[j]);
				Segment s1, s2;
				bool split = ai_split_path(s, &s1, &s2);

				// Check for wall intersection
				Segment* geometry_s = DARRAY_DATA_PTR(geometry, Segment);
				bool intersects_arena = false;
				for(uint k = 0; k < geometry.size; ++k) {
					if(segment_intersect(s1, geometry_s[k], NULL)) {
						intersects_arena = true;
						break;
					}
					if(split && segment_intersect(s2, geometry_s[k], NULL)) {
						intersects_arena = true;
						break;
					}	
				}

				// Check distance to walls at midpoint
				Vector2 midpoint = vec2_scale(vec2_add(s1.p1, s1.p2), 0.5f);
				if(ai_wall_distance(midpoint, geometry) < min_wall_distance)
					intersects_arena = true;

				if(!intersects_arena)
					darray_append(&edges, (void*)&n);
			}	
		}
	}
	return edges;
}	
Exemplo n.º 18
0
int vrt_dispatch_data_init(int threads_count, char *test_target_groups, int connections)
{
    int j;
    
    dispatch_data_threads_count = threads_count;
    dispatch_data_threads = darray_create(threads_count, sizeof(dispatch_data_thread));
    if (dispatch_data_threads == NULL) {
        return VRT_ERROR;
    }

    for (j = 0; j < threads_count; j ++) {
        dispatch_data_thread *ddt = darray_push(dispatch_data_threads);
        if (dispatch_data_thread_init(ddt, test_target_groups, connections) != VRT_OK) {
            return VRT_ERROR;
        }
        ddt->id = j;
    }
    
    return VRT_OK;
}
Exemplo n.º 19
0
static inline DArray *hashmap_find_bucket(Hashmap *map, void *key, int create, uint32_t *hash_out) {
    uint32_t hash = map->hash(key);
    int bucket_n = hash % DEFAULT_NUMBER_OF_BUCKETS;
    check(bucket_n >= 0, "Invalid bucket found: %d", bucket_n);
    *hash_out = hash; // store it for the return so the caller can use it

    DArray *bucket = darray_get(map->buckets, bucket_n);

    if(!bucket && create) {
        //new bucket, set it up
        bucket = darray_create(sizeof(void *), DEFAULT_NUMBER_OF_BUCKETS);
        check_mem(bucket);
        darray_set(map->buckets, bucket_n, bucket);
    }

    return bucket;

error:
    return NULL;
}
Exemplo n.º 20
0
DArray _gen_navpoints(DArray geometry, DArray platforms) {
	DArray points = darray_create(sizeof(Vector2), 0);
	darray_append_multi(&points, platforms.data, platforms.size);

	uint fail_count = 0;
	do {
		Vector2 navpoint = vec2(rand_float_range(0.0f, SCREEN_WIDTH),
			rand_float_range(0.0f, SCREEN_HEIGHT));

		float wdist = ai_wall_distance(navpoint, geometry);
		float pdist = _point_distance(navpoint, points);

		if(wdist >= min_wall_distance && pdist >= min_point_distance) {
			darray_append(&points, (void*)&navpoint);
			fail_count = 0;
		}
		else {
			fail_count++;
		}	
	} while(fail_count < 1200);	
	return points;
}	
Exemplo n.º 21
0
Arquivo: graph.c Projeto: vdt/libcore
Graph* graph_create(GRAPH_TYPE type)
{
    Graph *new_graph;

    /* Graph container */
    new_graph = malloc(sizeof(struct _graph));
    if(NULL == new_graph) {
        fprintf(stderr, "Out of memory (%s:%d)\n", __FUNCTION__, __LINE__);
        return NULL;
    }

    if((type < 0) && (type >= GRAPH_TYPE_INVALID)) {
        fprintf(stderr, "Invalid graph type: %d\n", type);
        free(new_graph);
        return NULL;
    }

    new_graph->type         = type;
    new_graph->vertices     = darray_create();
    new_graph->edge_count   = 0;

    return new_graph;
}
Exemplo n.º 22
0
LinearContainer* linear_container_darray_create(DataDestroyFunc data_destroy,
        void* ctx)
{
    LinearContainer* thiz = (LinearContainer*)malloc(sizeof(
                                LinearContainer) + sizeof(PrivInfo));

    if (thiz != NULL) {
        PrivInfo* priv = (PrivInfo*)thiz->priv;
        priv->darray = darray_create(data_destroy, ctx);

        thiz->insert        =  linear_container_darray_insert;
        thiz->prepend       =  linear_container_darray_prepend;
        thiz->append        =  linear_container_darray_append;
        thiz->del           =  linear_container_darray_delete;
        thiz->get_by_index  =  linear_container_darray_get_by_index;
        thiz->set_by_index  =  linear_container_darray_set_by_index;
        thiz->length        =  linear_container_darray_length;
        thiz->find          =  linear_container_darray_find;
        thiz->foreach       =  linear_container_darray_foreach;
        thiz->destroy       =  linear_container_darray_destroy;
    }

    return thiz;
}
Exemplo n.º 23
0
Arquivo: heap.c Projeto: vdt/libcore
Heap* heap_create(CompareFn comparefn)
{
    Heap *new_heap;

    assert(comparefn != NULL);

    /* Heap container */
    new_heap = malloc(sizeof(struct _heap));
    if(NULL == new_heap) {
        fprintf(stderr, "Out of memory (%s:%d)\n", __FUNCTION__, __LINE__);
        return NULL;
    }

    new_heap->h = darray_create();
    if(NULL == new_heap->h) {
        fprintf(stderr, "Heap creation failed (%s:%d)\n", __FUNCTION__, __LINE__);
        free(new_heap);
        return NULL;
    }

    new_heap->comparefn = comparefn;

    return new_heap;
}
Exemplo n.º 24
0
int main(int argc, char* argv[])
{
    int i = 0;
    int n = 100;
    int data = 0;

    DArray* darray = darray_create(NULL, NULL);

    for (i = 0; i < n; i++) {
        darray_append(darray, (void*)(rand() % n));
    }

    darray_sort(darray, quick_sort, cmp_int);

    darray_foreach(darray, print_int, NULL);
    printf("\n");
    darray_get_by_index(darray, 0, (void*)&data);
    data = !data;
    darray_foreach(darray, unique_print_int, &data);
    printf("\n");
    darray_destroy(darray);

    return 0;
}
Exemplo n.º 25
0
NavMesh ai_precalc_navmesh(DArray geometry, DArray platforms,
	float width, float height) {
	NavMesh res;

	ai_precalc_bounds(width, height);

	DArray navpoints = _gen_navpoints(geometry, platforms);
	DArray edges = _gen_edges(navpoints, geometry);

	Vector2* navpoints_v = DARRAY_DATA_PTR(navpoints, Vector2);
	Edge* edges_e = DARRAY_DATA_PTR(edges, Edge);

	uint n = navpoints.size;

	res.n_nodes = n;
	res.navpoints = MEM_ALLOC(sizeof(Vector2) * n);
	res.neighbour_count = MEM_ALLOC(sizeof(uint) * n);
	res.neighbours_start = MEM_ALLOC(sizeof(uint) * n);
	float* adjacency = MEM_ALLOC(sizeof(float) * n*n);
	res.distance = MEM_ALLOC(sizeof(float) * n*n);
	res.radius = MEM_ALLOC(sizeof(float) * n);
	res.nn_grid_count = MEM_ALLOC(sizeof(uint) * nn_grid_cells);
	res.nn_grid_start = MEM_ALLOC(sizeof(uint) * nn_grid_cells);

	memset(res.neighbour_count, 0, sizeof(uint) * n);
	memset(adjacency, 0, sizeof(float) * n*n);
	memset(res.distance, 0, sizeof(float) * n*n);
	memset(res.nn_grid_count, 0, sizeof(uint) * nn_grid_cells);
	memset(res.nn_grid_start, 0, sizeof(uint) * nn_grid_cells);
		
	for(uint i = 0; i < navpoints.size; ++i) {
		res.navpoints[i] = 	navpoints_v[i];
		res.radius[i] = ai_wall_distance(navpoints_v[i], geometry);
	}

	for(uint i = 0; i < edges.size; ++i) {
		float dist = vec2_length(vec2_sub(
			navpoints_v[edges_e[i].v1], navpoints_v[edges_e[i].v2]));
		adjacency[IDX_2D(edges_e[i].v1, edges_e[i].v2, n)] = dist;
		adjacency[IDX_2D(edges_e[i].v2, edges_e[i].v1, n)] = dist;
		res.neighbour_count[edges_e[i].v1]++;
		res.neighbour_count[edges_e[i].v2]++;
	}

	// Pracalc node neighbour lists
	uint total_neighbours = res.neighbour_count[0];
	res.neighbours_start[0] = 0;
	for(uint i = 1; i < n; ++i) {
		res.neighbours_start[i] = 
			res.neighbours_start[i-1] + res.neighbour_count[i-1];
		total_neighbours +=	res.neighbour_count[i];
	}		
	res.neighbours = MEM_ALLOC(sizeof(uint) * total_neighbours);
	for(uint i = 0; i < n; ++i) {
		uint idx = res.neighbours_start[i];
		for(uint j = 0; j < n; ++j) {
			if(adjacency[IDX_2D(i, j, n)] > 0.0f)
				res.neighbours[idx++] = j;
		}
		assert(idx == res.neighbours_start[i] + res.neighbour_count[i]);
	}

	memcpy(res.distance, adjacency, sizeof(float) * n*n);
	// Change zero distances to infinities
	for(uint i = 0; i < n*n; ++i)
		if(res.distance[i] == 0.0f)
			res.distance[i] = 10000000.0f;

	// Floyd-Warshall
	for(uint i = 0; i < n; ++i)
		for(uint j = 0; j < n; ++j)
			for(uint k = 0; k < n; ++k)
				res.distance[IDX_2D(j, k, n)] = MIN(res.distance[IDX_2D(j, k, n)],
					res.distance[IDX_2D(j, i, n)]+res.distance[IDX_2D(i, k, n)]);

	// Precalc nearest-navpoint grid
	DArray grid_cell;
	DArray nn_grid;

	res.nn_grid_start[0] = 0;

	nn_grid = darray_create(sizeof(uint), 0);
	for(uint i = 0; i < nn_grid_cells; ++i) {
		grid_cell = darray_create(sizeof(uint), 0);
		
		for(uint j = 0; j < nn_samples; ++j) {
			Vector2 p = _rand_point_in_nn_grid_cell(i);
			uint nearest_navpoint = _nearest_navpoint(p, geometry, navpoints);
			if(nearest_navpoint >= n) // Point was inside wall, skip
				continue;

			bool unique = true;

			uint* existing_navpoints = DARRAY_DATA_PTR(grid_cell, uint);
			for(uint k = 0; k < grid_cell.size; ++k)
				if(existing_navpoints[k] == nearest_navpoint)
					unique = false;
			
			if(unique)
				darray_append(&grid_cell, &nearest_navpoint);
		}

		res.nn_grid_count[i] = grid_cell.size;
		if(i > 0)
			res.nn_grid_start[i] = 
				res.nn_grid_start[i-1] + res.nn_grid_count[i-1];
		
		uint* cell_navpoints = DARRAY_DATA_PTR(grid_cell, uint);
		darray_append_multi(&nn_grid, cell_navpoints, grid_cell.size);
		darray_free(&grid_cell);
	}	

	// Hack, works properly as long as darray_free only does
	// MEM_FREE(darray.data);
	res.nn_grid = (uint*)nn_grid.data;

	darray_free(&navpoints);
	darray_free(&edges);
	MEM_FREE(adjacency);

	_precalc_vis(geometry, &res);

	return res;
}
Exemplo n.º 26
0
void minimap_init(void){
	minimap_pointers = darray_create(sizeof(ObjRabbit*), 0);
	red_knob = sprsheet_get_handle("red_knob");
	blue_knob = sprsheet_get_handle("blue_knob");
	finish = sprsheet_get_handle("finish_tile");
}
Exemplo n.º 27
0
char *test_darray_operations()
{
    darray_t *array = darray_create(sizeof(int), 100);
    mu_assert(array != NULL, "darray_create failed.");
    mu_assert(array->contents != NULL, "contents are wrong in darray");
    mu_assert(array->end == 0, "end isn't at the right spot");
    mu_assert(array->element_size == sizeof(int), "element size is wrong.");
    mu_assert(array->max == 100, "wrong max length on initial size");

    int *val1 = darray_new(array);
    mu_assert(val1 != NULL, "failed to make a new element");

    int *val2 = darray_new(array);
    mu_assert(val2 != NULL, "failed to make a new element");

    darray_set(array, 0, val1);
    darray_set(array, 1, val2);

    mu_assert(darray_get(array, 0) == val1, "Wrong first value.");
    mu_assert(darray_get(array, 1) == val2, "Wrong second value.");

    int *val_check = darray_remove(array, 0);
    mu_assert(val_check != NULL, "Should not get NULL.");
    mu_assert(*val_check == *val1, "Should get the first value.");
    mu_assert(darray_get(array, 0) == NULL, "Should be gone.");
    darray_free(val_check);

    val_check = darray_remove(array, 1);
    mu_assert(val_check != NULL, "Should not get NULL.");
    mu_assert(*val_check == *val2, "Should get the first value.");
    mu_assert(darray_get(array, 1) == NULL, "Should be gone.");
    darray_free(val_check);

    int old_max = array->max;
    darray_expand(array);
    mu_assert(array->max == old_max + array->expand_rate, "Wrong size after expand.");

    darray_contract(array);
    mu_assert(array->max == array->expand_rate + 1, "Should stay at the expand_rate at least.");

    darray_contract(array);
    mu_assert(array->max == array->expand_rate + 1, "Should stay at the expand_rate at least.");

    int i = 0;
    for(i = 0; i < 1000; i++) {
        int *val = darray_new(array);
        darray_attach(array, val); 
        *val = i * 333;
        darray_push(array, val);
    }

    mu_assert(array->max == 1201, "Wrong max size.");

    for(i = 999; i > 0; i--) {
        int *val = darray_pop(array);
        mu_assert(val != NULL, "Shouldn't get a NULL.");
        mu_assert(*val == i * 333, "Wrong value.");
        darray_free(val);
    }

    darray_destroy(array);
 
    return NULL;
}
Exemplo n.º 28
0
void video_init_ex(uint width, uint height, uint v_width, uint v_height,
				   const char* name, bool fullscreen) {
	assert(width != 0 && height != 0);
	assert(v_width != 0 && v_height != 0);

	if(!_sys_video_initialized)
		_sys_video_init();

	_sys_set_title(name);

    screen_widthf = v_width;
    screen_heightf = v_height;

	touch_scale_x = (float)v_width / (float)width;
	touch_scale_y = (float)v_height / (float)height;

    LOG_INFO("Vendor     : %s", glGetString(GL_VENDOR));
    LOG_INFO("Renderer   : %s", glGetString(GL_RENDERER));
    LOG_INFO("Version    : %s", glGetString(GL_VERSION));
    LOG_INFO("Extensions : %s", glGetString(GL_EXTENSIONS));

    has_discard_extension = _check_extension("GL_EXT_discard_framebuffer");

	glEnable(GL_TEXTURE_2D);
	glShadeModel(GL_FLAT);
	glClearDepthf(1.0f);

	glEnable(GL_BLEND);
    glDisable(GL_ALPHA_TEST);
	_set_blendmode(BM_NORMAL);
	last_blend_mode = BM_NORMAL;

	glMatrixMode(GL_TEXTURE);
	glScalef(1.0f/tex_mul, 1.0f/tex_mul, 1.0f);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();

#ifdef TARGET_IOS
	if(width > height) {
        glViewport(0, 0, height, width);
        // Some tricky transformations to properly turn view sideways
        glOrthof(0.0f, (float)v_width, (float)v_height, 0.0f, -1.0f, 1.0f);
        glTranslatef((float)v_width/2.0f, (float)v_height/2.0f, 0.0f);
        glRotatef(90.0f, 0.0f, 0.0f, 1.0f);
        glTranslatef((float)v_height/-2.0f, (float)v_width/-2.0f, 0.0f);
        glScalef((float)v_height/(float)v_width, (float)v_width/(float)v_height, 1.0f);
    }
    else 
#endif
	{
        glViewport(0, 0, width, height);
        glOrthof(0.0f, (float)v_width, (float)v_height, 0.0f, -1.0f, 1.0f);
        //glScalef((float)v_width/(float)v_height, (float)v_height/(float)v_width, 1.0f);
        //glTranslatef(0.0f, (float)v_width/-2.0f, 0.0f);
    }

	frame = 0;

	x_size_factor = (float)v_width / (float)width;
	y_size_factor = (float)v_height / (float)height;

#ifndef NO_DEVMODE
	memset(&v_stats, 0, sizeof(v_stats));
	v_stats.n_layers = bucket_count;
	v_stats.layer_rects = (uint*)MEM_ALLOC(sizeof(uint) * bucket_count);
	v_stats.layer_lines = (uint*)MEM_ALLOC(sizeof(uint) * bucket_count);
#endif

	// Init render buckets & blend modes
	memset(rect_buckets, 0, sizeof(rect_buckets));
	memset(line_buckets, 0, sizeof(line_buckets));
	for(uint i = 0; i < bucket_count; ++i) {
		blend_modes[i] = BM_NORMAL;
	}

	// Temp bucket for sorting
	rects_out = darray_create(sizeof(TexturedRectDesc), 32);

	textures = darray_create(sizeof(Texture), 16);

	// Generate index buffer
	for(uint i = 0; i < max_vertices/4; ++i) {
		index_buffer[i*6 + 0] = i*4 + 0;
		index_buffer[i*6 + 1] = i*4 + 1;
		index_buffer[i*6 + 2] = i*4 + 3;
		index_buffer[i*6 + 3] = i*4 + 2;
		index_buffer[i*6 + 4] = i*4 + 1;
		index_buffer[i*6 + 5] = i*4 + 3;
	}

	vertex_buffer = darray_create(sizeof(Vertex), max_vertices);

	LOG_INFO("Video initialized");
}
Exemplo n.º 29
0
/*
 * Bucket sort algorithm.
 */
void integer_sort(int *array, int n)
{
	int max, _max;           /* Max number in array. */
	int range;               /* Bucket range.        */
	int i, j, k;             /* Loop indexes.        */
	int *indexes;            /* Index for buckets.   */
	struct darray **buckets; /* Buckets.            */

	indexes = smalloc(NUM_BUCKETS*sizeof(int));

	/* Create buckets. */
	buckets = smalloc(NUM_BUCKETS*sizeof(struct darray *));
	for (i = 0; i < NUM_BUCKETS; i++)
		buckets[i] = darray_create(n/NUM_BUCKETS);
	
	max = INT_MIN;
	
	#pragma omp parallel private(i, j, k, _max)
	{	
		_max = INT_MIN;

		/* Find max number in the array. */
		#pragma omp for schedule(static)
		for (i = 0; i < n; i++)
		{
			/* Found. */
			if (array[i] > _max)
				_max = array[i];
		}

		#pragma omp critical
		{
			if (_max > max) {
				max = _max;
			}
		}
		
		#pragma omp master
		range = max/NUM_BUCKETS;
		#pragma omp barrier
		
		/* Distribute numbers into buckets. */
		#pragma omp master
		for (i = 0; i < n; i++)
		{
			j = array[i]/range;
			if (j >= NUM_BUCKETS)
				j = NUM_BUCKETS - 1;

			darray_append(buckets[j], array[i]);
		}
		
		/* Sort Each bucket. */
		#pragma omp for schedule(dynamic)
		for (i = 0; i < NUM_BUCKETS; i++)
		{
			if (darray_size(buckets[i]) > 0)
				sort(buckets[i]);
		}
		
		#pragma omp master
		{
			/* Build indexes. */
			indexes[0] = 0;
			for (i = 1; i < NUM_BUCKETS; i++)
				indexes[i] = indexes[i - 1] + darray_size(buckets[i]);
		
			/* Rebuild array. */
			for (i = 0; i < NUM_BUCKETS; i++)
			{
				k = indexes[i];
					
				for (j = 0; j < darray_size(buckets[i]); j++)
					array[k + j] = darray_get(buckets[i], j);
			}
		}
	}
	
	/* House keeping. */
	for (i = 0; i < NUM_BUCKETS; i++)
		darray_destroy(buckets[i]);
	free(buckets);
	free(indexes);
}
Exemplo n.º 30
0
void video_draw_rect_rotated(TexHandle tex, uint layer,
							 const RectF* source, const RectF* dest, float rotation, Color tint) {
	assert(layer < bucket_count);
	assert(dest);

	assert(tex < textures.size);
	Texture* t = DARRAY_DATA_PTR(textures, Texture);
	assert(t[tex].active);
	uint fixed_scale = (uint)(t[tex].scale * (tex_mul+1.0f));
	uint texture_width = t[tex].width;
	uint texture_height = t[tex].height;

	int16 real_src_l = 0;
	int16 real_src_t = 0;
	int16 real_src_r = texture_width;
	int16 real_src_b = texture_height;

	if(source != NULL) {
		real_src_l = (int16)(source->left);
		real_src_t = (int16)(source->top);
		real_src_r = (int16)(source->right);
		real_src_b = (int16)(source->bottom);
	}

	// This is faster than comparing floats
	bool full_dest = (*((uint*)&dest->right) | *((uint*)&dest->bottom)) == 0U;
	RectF real_dest = *dest;

    int w = real_src_r - real_src_l;
    int h = real_src_b - real_src_t;

    if(full_dest) {
		float width = (float)w;
		float height = (float)h;
		real_dest.right = real_dest.left + width;
		real_dest.bottom = real_dest.top + height;
	}

    // Don't draw rect if it's not inside screen rect
    if(transform[layer] == NULL) {
        RectF screen = {0.0f, 0.0f, screen_widthf, screen_heightf};
        if(!rectf_rectf_collision(&screen, &real_dest))
            return;
    }

	assert(is_pow2((texture_width * fixed_scale) >> 15));
	assert(is_pow2((texture_height * fixed_scale) >> 15));

	uint wlog2 = _ilog2((texture_width * fixed_scale) >> 15);
	uint hlog2 = _ilog2((texture_height * fixed_scale) >> 15);

	real_src_l = ((real_src_l << 15) - 1) >> wlog2;
	real_src_t = ((real_src_t << 15) - 1) >> hlog2;
	real_src_r = ((real_src_r << 15) - 1) >> wlog2;
	real_src_b = ((real_src_b << 15) - 1) >> hlog2;

	TexturedRectDesc new_rect = {
		.tex = tex,
		.src_l = real_src_l,
		.src_t = real_src_t,
		.src_r = real_src_r,
		.src_b = real_src_b,
		.dest = real_dest,
		.tint = tint,
		.rotation = rotation
	};

    // Do something uglier instead of darray_append, saving a memcpy here is worth it
	//darray_append(&rect_buckets[layer], &new_rect);

    DArray* bucket = &rect_buckets[layer];
	if(!bucket->reserved)
		rect_buckets[layer] = darray_create(sizeof(TexturedRectDesc), 32);
	else if(bucket->reserved - bucket->size < 1) {
        bucket->reserved *= 2;
        bucket->data = MEM_REALLOC(bucket->data, bucket->item_size * bucket->reserved);
    }

    TexturedRectDesc* rects = DARRAY_DATA_PTR(*bucket, TexturedRectDesc);
    rects[bucket->size] = new_rect;
    bucket->size++;
}