-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCBackground.cpp
More file actions
executable file
·56 lines (42 loc) · 1.93 KB
/
CBackground.cpp
File metadata and controls
executable file
·56 lines (42 loc) · 1.93 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
//
// CBackground.cpp
// Fifth
//
// Created by Didrik Munther on 08/07/15.
// Copyright (c) 2015 Didrik Munther. All rights reserved.
//
#include "CBackground.h"
#include "CAssetManager.h"
#include "CCamera.h"
#include "NSurface.h"
#include <iostream>
CBackground::CBackground(std::string spriteKey, float parallax, BackgroundOffset backgroundOffset) :
_spriteKey(spriteKey), _parallax(parallax), _backgroundOffset(backgroundOffset) { }
void CBackground::onRender(CWindow* window, CCamera* camera) {
CSprite* sprite = CAssetManager::getSprite(_spriteKey);
if(!sprite) return;
int amountX = ceil((sprite->getSource()->w * _backgroundOffset.scale) / camera->getWidth()) + 1;
for(int i = -1; i < amountX; i++) {
NSurface::renderSprite(i * _backgroundOffset.scale * sprite->getSource()->w + -camera->offsetX() * _parallax, -camera->offsetY() * _parallax + _backgroundOffset.y, sprite->getSource()->w * _backgroundOffset.scale, sprite->getSource()->h * _backgroundOffset.scale, sprite, window, SDL_RendererFlip::SDL_FLIP_NONE);
}
}
void CBackground::onSerialize(rapidjson::Value* value, rapidjson::Document::AllocatorType* alloc, CInstance* instance) {
addNumber(value, alloc, "x", _backgroundOffset.x);
addNumber(value, alloc, "y", _backgroundOffset.y);
addNumber(value, alloc, "scale", _backgroundOffset.scale);
addString(value, alloc, "spriteKey", _spriteKey);
addNumber(value, alloc, "parallax", _parallax);
}
void CBackground::onDeserialize(const rapidjson::Value* value, CInstance* instance) {
assignInt(value, "x", &_backgroundOffset.x);
assignInt(value, "y", &_backgroundOffset.y);
assignFloat(value, "scale", &_backgroundOffset.scale);
assignString(value, "spriteKey", &_spriteKey);
assignFloat(value, "parallax", &_parallax);
}
float CBackground::getParallax() {
return _parallax;
}
void CBackground::setParallax(float parallax) {
_parallax = parallax;
}