-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.cpp
executable file
·46 lines (43 loc) · 1.09 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
#include <SDL/SDL_image.h>
#include <ctime>
#include "base/app.h"
#include "background.h"
Background::Background()
{
std::string name[7] = {"Blue", "Brown", "Gray", "Green", "Pink", "Purple", "Yellow"};
std::string n("images/Background/");
n += name[rand()%7] + ".png";
image = IMG_Load(n.c_str());
if (!image)
{
std::cerr << IMG_GetError();
exit(EXIT_FAILURE);
}
int i, window_height;
App::instance->window_size(NULL, &window_height);
for (i=0; i<window_height+image->h; i+=image->h)
ys.push_back(i);
}
void Background::update()
{
for (int i=0; i<(int)ys.size(); ++i)
{
if (ys[i]+image->h <= 0)
ys[i] = ys[i?(i-1):ys.size()-1]+image->h;
ys[i] -= 2;
}
}
void Background::draw(SDL_Surface* screen)
{
int window_width;
App::instance->window_size(&window_width, NULL);
for (int i=0; i<window_width; i+=image->w)
{
for (int j=0; j<(int)ys.size(); ++j)
{
rect.x = i;
rect.y = ys[j];
SDL_BlitSurface(image, NULL, screen, &rect);
}
}
}