about summary refs log tree commit diff
path: root/src/game/mapres_col.cpp
blob: d2f9d0c69b01fddf429abd9c5cdfddf56c85b2ac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <baselib/system.h>
#include <baselib/vmath.h>
#include <baselib/math.h>
#include "../engine/interface.h"
#include "mapres_col.h"
#include "mapres.h"

using namespace baselib;

/*
	Simple collision rutines!
*/
struct collision
{
	int w, h;
	unsigned char *data;
};

static collision col;
static int global_dividor;

int col_init(int dividor)
{
	mapres_collision *c = (mapres_collision*)map_find_item(MAPRES_COLLISIONMAP,0);
	if(!c)
	{
		dbg_msg("mapres_col", "failed!");
		return 0;
	}
	col.w = c->width;
	col.h = c->height;
	global_dividor = dividor;
	col.data = (unsigned char *)map_get_data(c->data_index);
	return col.data ? 1 : 0;
}

int col_check_point(int x, int y)
{
	int nx = x/global_dividor;
	int ny = y/global_dividor;
	if(nx < 0 || nx >= col.w || ny >= col.h)
		return 1;
	
	if(y < 0)
		return 0; // up == sky == free
	
	return col.data[ny*col.w+nx];
}

// TODO: rewrite this smarter!
bool col_intersect_line(vec2 pos0, vec2 pos1, vec2 *out)
{
	float d = distance(pos0, pos1);
	
	for(float f = 0; f < d; f++)
	{
		float a = f/d;
		vec2 pos = mix(pos0, pos1, a);
		if(col_check_point((int)pos.x, (int)pos.y))
		{
			if(out)
				*out = pos;
			return true;
		}
	}
	if(out)
		*out = pos1;
	return false;
}