-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.cpp
More file actions
83 lines (67 loc) · 1.94 KB
/
background.cpp
File metadata and controls
83 lines (67 loc) · 1.94 KB
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
/*
* Copyright (c) 2014 Ross Simpson
* All Right Reserved
*
* This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
* To view a copy of this license,visit http://creativecommons.org/licenses/by-nc-sa/4.0/.
*/
#include "background.h"
#include "gamestate.h"
#include <SDL2/SDL_image.h>
#include <string>
#include <time.h>
using namespace std;
void Background::genStars()
{
static const uint32_t maxStars = 100;
srand(time(NULL));
// Generate our intiail start pattern
for (size_t i = 0; i < maxStars; i++)
{
Vector2D tmpStar(0, 0);
tmpStar.setX(rand() % GameState::get()->getWindowWidth());
tmpStar.setY(rand() % GameState::get()->getWindowHeight());
stars.push_back(tmpStar);
}
}
Background::~Background()
{
SDL_DestroyTexture(backgroundTexture);
}
bool Background::init()
{
// TODO: Really, really implement a texture handler
const string backgroundTextureFile = "/home/rsimpson/Development/EndlessQuest/images/nebula2.png";
backgroundTexture = IMG_LoadTexture(GameState::get()->getRenderer(), backgroundTextureFile.c_str());
if (backgroundTexture == nullptr)
{
logSDLError(cout, "Background::init(), IMG_LoadTexture");
return false;
}
genStars();
return true;
}
void Background::update()
{
Vector2D velocity(0, 1);
for (vector<Vector2D>::iterator star = stars.begin(); star != stars.end(); ++star)
{
*star += velocity;
// If the star has gone off the screen, respawn at random location
// at the top
if (star->getY() > GameState::get()->getWindowHeight())
{
star->setY(0);
star->setX(rand() % GameState::get()->getWindowWidth());
}
}
}
void Background::render(SDL_Renderer *renderer)
{
SDL_RenderCopy(renderer, backgroundTexture, NULL, NULL);
SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0xFF);
for (vector<Vector2D>::iterator star = stars.begin(); star != stars.end(); ++star)
{
SDL_RenderDrawPoint(renderer, star->getX(), star->getY());
}
}