-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbcg.c
116 lines (87 loc) · 2.66 KB
/
bcg.c
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
#include "bcg.h"
#include <stdlib.h>
#include "sprites.h"
Bcg bcg_Create(float vShift, int levelNum)
{
int i, j;
int baseLayer = levelNum * _BCG_LAYER_COUNT;
Bcg bcg;
bcg.slideOut = false;
int tw, th;
SpriteClassId startScid = sprites_GetIdByNameF("level_%d:bcg-start", levelNum);
sprites_GetDimensions(startScid, &tw, &th);
bcg.startImg = snge_AddSprite(startScid, point(0.0, vShift), baseLayer + _BCG_LAYER_COUNT);
bcg.startImgHeight = (float)th;
for(i = 0; i < _BCG_LAYER_COUNT; ++i)
{
SpriteClassId scid = sprites_GetIdByNameF("level_%d:bcg-layer-%d", levelNum, i);
int width;
int height;
sprites_GetDimensions(scid, &width, &height);
int xpos = (_ACTION_AREA_WIDTH - width) / 2;
int count = (_SCREEN_HEIGHT / height) + 2;
Sprite **sprites = malloc(sizeof(Sprite*) * count);
for(j = 0; j < count; ++j)
{
sprites[j] = snge_AddSprite(scid, point(xpos, j * height + vShift), baseLayer + i);
}
bcg.layers[i].bottom = count - 1;
bcg.layers[i].sprites = sprites;
bcg.layers[i].count = count;
bcg.layers[i].height = (float)height;
}
return bcg;
}
void bcg_Relativize(Bcg *pBcg)
{
int i, j;
for(i = 0; i < _BCG_LAYER_COUNT; ++i)
{
BcgLayer *l = &pBcg->layers[i];
for(j = 0; j < l->count; ++j)
snge_RelativizeSprite(l->sprites[j]);
}
snge_RelativizeSprite(pBcg->startImg);
}
void bcg_Move(Bcg *pBcg, float vdelta)
{
int i, j;
for(i = 0; i < _BCG_LAYER_COUNT; ++i)
{
BcgLayer *l = &pBcg->layers[i];
float pfactor = 1.0 - ((float)(_BCG_LAYER_COUNT - i) * _BCG_PARALLAX_FACTOR);
for(j = 0; j < l->count; ++j)
{
l->sprites[j]->y += vdelta * pfactor;
}
if(pBcg->slideOut)
continue;
float bottomy = l->sprites[l->bottom]->y + l->height;
if(bottomy <= _SCREEN_HEIGHT)
{
int newbottom = (l->bottom + 1) % l->count;
l->bottom = newbottom;
l->sprites[newbottom]->y = bottomy;
}
}
if(pBcg->startImg == NULL)
return;
pBcg->startImg->y += vdelta;
if((pBcg->startImg->y + pBcg->startImgHeight) <= 0.0)
{
pBcg->startImg->destroy = true;
pBcg->startImg = NULL;
}
}
void bcg_Cleanup(Bcg *pBcg)
{
if(pBcg->startImg != NULL)
pBcg->startImg->destroy = true;
int i, j;
for(i = 0; i < _BCG_LAYER_COUNT; ++i)
{
for(j = 0; j < pBcg->layers[i].count; ++j)
pBcg->layers[i].sprites[j]->destroy = true;
free(pBcg->layers[i].sprites);
}
}