summaryrefslogtreecommitdiff
path: root/src/client/radar.c
blob: e8457e96b447958573eb75119993f03a83d70d8c (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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

#include "main.h"
#include "list.h"
#include "tux.h"
#include "arena.h"

#include "interface.h"
#include "image.h"
#include "radar.h"

static image_t *g_radar;
static list_t *listRadar;

typedef struct radar_item_struct {
	int id;
	int x, y;
	int type;
} radar_item_t;

static radar_item_t *newRadarItem(int id, int x, int y, int type)
{
	radar_item_t *new;

	new = malloc(sizeof(radar_item_t));
	new->id = id;
	new->x = x;
	new->y = y;
	new->type = type;

	return new;
}

static void destroyRadarItem(radar_item_t *p)
{
	assert(p != NULL);
	free(p);
}

void radar_add(int id, int x, int y, int type)
{
	int i;

	for (i = 0; i < listRadar->count; i++) {
		radar_item_t *thisRadarItem;

		thisRadarItem = (radar_item_t *) listRadar->list[i];

		if (thisRadarItem->id == id) {
			thisRadarItem->x = x;
			thisRadarItem->y = y;
			thisRadarItem->type = type;
			return;
		}
	}

	list_add(listRadar, newRadarItem(id, x, y, type));
}

void radar_del(int id)
{
	int i;

	for (i = 0; i < listRadar->count; i++) {
		radar_item_t *thisRadarItem;

		thisRadarItem = (radar_item_t *) listRadar->list[i];

		if (thisRadarItem->id == id) {
			list_del_item(listRadar, i, destroyRadarItem);
			return;
		}
	}
}

void radar_init()
{
	assert(image_is_inicialized() == TRUE);
	assert(interface_is_inicialized() == TRUE);

	g_radar = image_add("radar.png", IMAGE_NO_ALPHA, "radar", IMAGE_GROUP_USER);
	listRadar = list_new();
}

void radar_draw(arena_t *arena)
{
	int i;

	image_draw(g_radar, RADAR_LOCATION_X, RADAR_LOCATION_Y, 0, 0, RADAR_SIZE_X + 2, RADAR_SIZE_Y + 2);

	for (i = 0; i < listRadar->count; i++) {
		radar_item_t *thisRadarItem;
		int x, y;

		thisRadarItem = (radar_item_t *) listRadar->list[i];

		x = (((float) RADAR_SIZE_X) / ((float) arena->w)) * ((float) thisRadarItem->x);

		y = (((float) RADAR_SIZE_Y) / ((float) arena->h)) * ((float) thisRadarItem->y);

		if (x >= 0 && x <= RADAR_SIZE_X && y >= 0 && y <= RADAR_SIZE_Y) {
			image_draw(g_radar, RADAR_LOCATION_X + 1 + x, RADAR_LOCATION_Y + 1 + y,
				   2 * thisRadarItem->type, RADAR_SIZE_Y + 2, 2, 2);
		}
	}
}

void radar_quit()
{
	list_destroy_item(listRadar, destroyRadarItem);
}