summaryrefslogtreecommitdiff
path: root/src/client/radar.c
blob: 6fd3a685094e926666a67e8318e0be6984fe88f2 (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
114
115
116
117
118
119
120
121
122
123
124
125
126

#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
addToRadar(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;
      }
   }

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

void
delFromRadar(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) {
         delListItem(listRadar, i, destroyRadarItem);
         return;
      }
   }
}

void
initRadar()
{
   assert(isImageInicialized() == TRUE);
   assert(isInterfaceInicialized() == TRUE);

   g_radar =
      addImageData("radar.png", IMAGE_NO_ALPHA, "radar", IMAGE_GROUP_USER);
   listRadar = newList();
}

void
drawRadar(arena_t * arena)
{
   int i;

   drawImage(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) {
         drawImage(g_radar,
                   RADAR_LOCATION_X + 1 + x, RADAR_LOCATION_Y + 1 + y,
                   2 * thisRadarItem->type, RADAR_SIZE_Y + 2, 2, 2);
      }
   }
}

void
quitRadar()
{
   destroyListItem(listRadar, destroyRadarItem);
}