-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.cpp
77 lines (61 loc) · 1.15 KB
/
background.cpp
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
#include "background.h"
static int backgroundX;
static Star stars[MAX_STARS];
static SDL_Texture* background;
void initBackground(void)
{
background = loadTexture("gfx/background.png");
backgroundX = 0;
}
void initStarfield(void)
{
int i;
for (i = 0; i < MAX_STARS; i++)
{
stars[i].x = rand() % SCREEN_WIDTH;
stars[i].y = rand() % SCREEN_HEIGHT;
stars[i].speed = 1 + rand() % 8;
}
}
void doBackground(void)
{
if (--backgroundX < -SCREEN_WIDTH)
{
backgroundX = 0;
}
}
void doStarfield(void)
{
int i;
for (i = 0; i < MAX_STARS; i++)
{
stars[i].x -= stars[i].speed;
if (stars[i].x < 0)
{
stars[i].x = SCREEN_WIDTH + stars[i].x;
}
}
}
void drawStarfield(void)
{
int i, c;
for (i = 0; i < MAX_STARS; i++)
{
c = 32 * stars[i].speed;
SDL_SetRenderDrawColor(app.renderer, c, c, c, 255);
SDL_RenderDrawLine(app.renderer, stars[i].x, stars[i].y, stars[i].x + 3, stars[i].y);
}
}
void drawBackground(void)
{
SDL_Rect dest;
int x;
for (x = backgroundX; x < SCREEN_WIDTH; x += SCREEN_WIDTH)
{
dest.x = x;
dest.y = 0;
dest.w = SCREEN_WIDTH;
dest.h = SCREEN_HEIGHT;
SDL_RenderCopy(app.renderer, background, NULL, &dest);
}
}