summaryrefslogtreecommitdiff
path: root/src/client/layer.c
blob: 102af1c111f81d48651ffa28f71769746e578a93 (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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150

#include <stdlib.h>
#include <assert.h>

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

#include "interface.h"
#include "layer.h"
#include "image.h"

#include "screen_world.h"

static list_t *listLayer;

static bool_t isLayerInit = FALSE;

bool_t isLayerInicialized()
{
	return isLayerInit;
}

/*
 * Inicializuje globalny zoznam vsrtiev
 */
void initLayer()
{
	listLayer = newList();
	isLayerInit = TRUE;
}

/*
 * Prida novu polozku do zoznamu vrstiev
 * *img - obrazok, ktory obsahuje polozka
 * x y w h - suradnca kam sa obrazok vykresli a jeho rozmery
 * px py pw ph - suradnica casti obrazka *img ktora sa ma zobrazit
 * layer - vrstva na ktorej sa zobrazi obrazok
 */
void addLayer(SDL_Surface *img,
		int x,int y, int px,int py,
		int w,int h, int player)
{
	layer_t *new;
	layer_t *this;
	int i, j;

	assert( img != NULL );

	new = malloc( sizeof(layer_t) );
	new->x = x;
	new->y = y;
	new->w = w;
	new->h = h;
	new->px = px;
	new->py = py;
	new->layer = player;
	new->image = img;

	for( i = 0 ; i < listLayer->count ; i++ )
	{
		this = (layer_t *)listLayer->list[i];
		
		if( this->layer == new->layer )
		{
			for( j = i ; j < listLayer->count ; j++ )
			{
				this = (layer_t *)listLayer->list[j];

				if( this->y+this->h > new->y+new->h )
				{
					insList(listLayer, j, new);
					return;
				}
			}
		}
	}

	addList(listLayer, new);
}

/*
 * Vykresli vsetky polozky na obrazovku
 * poukadane podla vrstvy a suradnice Y+H
 * po dokresleni sa zoznam uvolni.
 */
void drawLayer()
{
	layer_t *this;
	int i;

	for( i = 0 ; i < listLayer->count ; i++ )
	{
		this = (layer_t *)listLayer->list[i];

		drawImage(this->image,
			this->x, this->y,
			this->px, this->py,
			this->w, this->h);
	}

	destroyListItem(listLayer, free);
	listLayer = newList();
}

void drawLayerCenter(int x, int y)
{
	layer_t *this;
	int screen_x, screen_y;
	int i;

	getCenterScreen(&screen_x, &screen_y, x, y);

	for( i = 0 ; i < listLayer->count ; i++ )
	{
		this = (layer_t *)listLayer->list[i];

		if( (this->x - screen_x) + this->w < 0 || (this->x - screen_x) > WINDOW_SIZE_X ||
		    (this->y - screen_y) + this->h < 0 || (this->y - screen_y) > WINDOW_SIZE_Y )
		{
			continue;
		}

		drawImage(this->image,
			this->x - screen_x, this->y - screen_y,
			this->px, this->py,
			this->w, this->h);

		//printf("%d %d\n", this->x - screen_x, this->y - screen_y);
	}

	destroyListItem(listLayer, free);
	listLayer = newList();
}

void flushLayer()
{
	destroyListItem(listLayer, free);
	listLayer = newList();
}

/*
 * Odstrani zoznam vrtsiev z pamete.
 */
void quitLayer()
{
	destroyListItem(listLayer, free);
	isLayerInit = FALSE;
}