File: bg.c

package info (click to toggle)
ketm 0.0.6-17sarge1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 9,356 kB
  • ctags: 875
  • sloc: ansic: 7,303; sh: 410; makefile: 185
file content (227 lines) | stat: -rw-r--r-- 3,755 bytes parent folder | download | duplicates (3)
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#include "bg.h"

extern double fps_factor;
extern SDL_Surface *screen;

int akt_bgtype;
double bg_alpha;

/* clouds */
SPRITE *w1[5];
SPRITE *w2[10];
SPRITE *w3[20];

/* tile */
SDL_Surface *btile=NULL;
double btile_y;

void bg_init(int bg_type)
{

	if((bg_type>=BG_LAST)||(bg_type<BG_BLACK))
		error(ERR_FATAL,"unknown BG_TYPE: %d",bg_type);

	akt_bgtype=bg_type;
	bg_alpha=0;
	switch(bg_type) {

	case BG_BLACK:
		break;

	case BG_CLOUDS:
		clouds_init();
		break;

	case BG_STARS:
		// stars_init();
		break;

	case BG_TILE:
		tile_init();
		break;
	}
	
}

void bg_work()
{
	if(akt_bgtype<0) {
		error(ERR_WARN,"bg_work with no bg_type");
		return;
	}

	if(bg_alpha<255)
		bg_alpha+=4*fps_factor;
	else
		bg_alpha=255;

	switch(akt_bgtype) {

	case BG_BLACK:
		SDL_FillRect(screen,NULL,SDL_MapRGB(screen->format,0,0,0));
		break;

	case BG_CLOUDS:
		// SDL_FillRect(screen,NULL,SDL_MapRGB(screen->format,0,0,0xff));
		SDL_FillRect(screen,NULL,SDL_MapRGB(screen->format,0,0,bg_alpha));
		break;

	case BG_STARS:
		SDL_FillRect(screen,NULL,SDL_MapRGB(screen->format,0,0,0));
		break;

	case BG_TILE:
		tile_work();
		tile_display();
		break;
	}
	
}

void bg_destroy()
{
	if(akt_bgtype<0) {
		error(ERR_WARN,"bg_destroy with no bg_type");
		return;
	}
	switch(akt_bgtype) {

	case BG_BLACK:
		break;

	case BG_CLOUDS:
		clouds_remove();
		break;

	case BG_STARS:
		// stars_remove();
		break;

	case BG_TILE:
		tile_remove();
		break;
	}
	akt_bgtype=-1;
}


void clouds_init()
{
	int i;
	CLOUDS_DATA *d;

	for(i=0;i<20;i++) {
		w3[i]=sprite_add_file("wolke03.bmp",1,PR_BACK0);
		w3[i]->x=rand()%WIDTH-w3[i]->w;
		w3[i]->y=rand()%HEIGHT;
		w3[i]->flags&=~SP_FLAG_COLCHECK;
		w3[i]->flags|=SP_FLAG_VISIBLE;
		w3[i]->type=SP_ETC;
		w3[i]->mover=clouds_mover;
		d=mmalloc(sizeof(CLOUDS_DATA));
		w3[i]->data=d;
		w3[i]->alpha=bg_alpha;
		d->speed_base=1;
		d->speed_rand=(double)(rand()%100)/100;
	}

	for(i=0;i<10;i++) {
		w2[i]=sprite_add_file("wolke02.bmp",1,PR_BACK1);
		w2[i]->x=rand()%WIDTH-w2[i]->w;
		w2[i]->y=rand()%HEIGHT;
		w2[i]->flags&=~SP_FLAG_COLCHECK;
		w2[i]->flags|=SP_FLAG_VISIBLE;
		w2[i]->type=SP_ETC;
		w2[i]->mover=clouds_mover;
		d=mmalloc(sizeof(CLOUDS_DATA));
		w2[i]->data=d;
		w2[i]->alpha=bg_alpha;
		d->speed_base=2;
		d->speed_rand=(double)(rand()%100)/100;
	}

	for(i=0;i<5;i++) {
		w1[i]=sprite_add_file("wolke01.bmp",1,PR_BACK2);
		w1[i]->x=rand()%WIDTH-w1[i]->w;
		w1[i]->y=rand()%HEIGHT;
		w1[i]->flags&=~SP_FLAG_COLCHECK;
		w1[i]->flags|=SP_FLAG_VISIBLE;
		w1[i]->type=SP_ETC;
		w1[i]->mover=clouds_mover;
		d=mmalloc(sizeof(CLOUDS_DATA));
		w1[i]->data=d;
		w1[i]->alpha=bg_alpha;
		d->speed_base=2;
		d->speed_rand=(double)(rand()%100)/100;
	}
	
}

void clouds_remove()
{
	int i;

	for(i=0;i<20;i++) {
		if(i<5) w1[i]->type=-1;
		if(i<10) w2[i]->type=-1;
		w3[i]->type=-1;
	}
}

void clouds_mover(SPRITE *c)
{
	CLOUDS_DATA *d=(CLOUDS_DATA *)c->data;

	c->y+=d->speed_base*fps_factor;
	c->y+=d->speed_rand*fps_factor;
	if(c->y>HEIGHT) {
		c->y-=HEIGHT+c->w;
		d->speed_rand=(double)(rand()%100)/100;
		c->x=rand()%WIDTH-c->w;
		c->alpha=bg_alpha;
	}
}


void tile_init()
{
	if(btile==NULL) {
		btile=loadbmp("back02.bmp");
	}
	btile_y=0;
}

void tile_work()
{
	btile_y+=fps_factor;
	if(btile_y>btile->h-1)
		btile_y-=btile->h;
}

void tile_display()
{
	int i,j;
	SDL_Rect r;
	if(bg_alpha<255) {
	 	SDL_FillRect(screen,NULL,SDL_MapRGB(screen->format,0,0,0));
		SDL_SetAlpha(btile,SDL_SRCALPHA,bg_alpha);
	} else
		SDL_SetAlpha(btile,SDL_SRCALPHA,255);

	r.w=btile->w;
	r.h=btile->h;
	for(i=0;i<WIDTH;i+=btile->w) {
		for(j=-btile->h;j<HEIGHT;j+=btile->h) {
			r.x=i;
			r.y=j+btile_y;
			SDL_BlitSurface(btile,NULL,screen,&r);
		}
	}
}

void tile_remove()
{ 
	if(btile!=NULL)
		unloadbmp_by_surface(btile);
	btile=NULL;
}