-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCamera.cpp
More file actions
86 lines (73 loc) · 2.35 KB
/
Camera.cpp
File metadata and controls
86 lines (73 loc) · 2.35 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
84
85
86
#include "settings.h"
#include "precomp.h"
#include "tmpl8math.h"
#include "Camera.h"
#include "surface.h"
void Camera::Tick(){
}
void Camera::SetWorldPosition(float2 wpos) {
int2 logicsize = GetLogicSize();
m_worldPosition = wpos;
if (m_worldPosition.x < logicsize.x / 2.0f) {
m_worldPosition.x = TO_FLOAT(logicsize.x) / 2.0f;
}
if (m_worldPosition.x > MAP_WIDTH * TILE_WIDTH - logicsize.x / 2.0f) {
m_worldPosition.x = MAP_WIDTH * TILE_WIDTH - logicsize.x / 2.0f;
}
if (m_worldPosition.y > MAP_HEIGHT * TILE_HEIGHT - logicsize.y / 2.0f) {
m_worldPosition.y = MAP_HEIGHT * TILE_HEIGHT - logicsize.y / 2.0f;
}
if (m_worldPosition.y < logicsize.y / 2.0f) {
m_worldPosition.y = logicsize.y / 2.0f;
}
}
void Camera::Render(
const Surface* const logicscreen,
Surface* const screen, const Surface* const
#if defined (_DEBUG) && defined(VISUAL_DEBUG)
debugscreen
#endif
) {
const int2 readCoord
(
static_cast<int>(m_worldPosition.x - m_subscreenSize.x / 2),
static_cast<int>(m_worldPosition.y - m_subscreenSize.y / 2)
);
const int2 writeCoord
(
clamp(m_screenPosition.x - m_subscreenSize.x / 2, 0, screen->m_width),
clamp(m_screenPosition.y - m_subscreenSize.y / 2 + UI_BAR_HEIGHT, 0, screen->m_height)
);
uint* const startr{ logicscreen->m_pixels + readCoord.x + readCoord.y * screen->m_width };
uint* const startw{ screen->m_pixels + writeCoord.x + writeCoord.y * screen->m_width };
constexpr int bytesPerPixel{ 4 };
const int lineLength{ m_subscreenSize.x * bytesPerPixel };
for (int y{ 0 }; y < m_subscreenSize.y; y++) {
memcpy
(
// to
startw + y * screen->m_width,
// from
startr + y * logicscreen->m_width,
// length
lineLength
);
}
#if defined (_DEBUG) && defined(VISUAL_DEBUG)
if (debugscreen) {
for (int cameray{ 0 }; cameray < m_subscreenSize.y; cameray++) {
for (int camerax{ 0 }; camerax < m_subscreenSize.x; camerax++) {
const int x{ camerax + readCoord.x };
const int y{ cameray + readCoord.y };
const int readIndex{ x + y * logicscreen->m_width };
const int writex{ camerax + writeCoord.x };
const int writey{ cameray + writeCoord.y };
const int writeIndex{ writex + writey * screen->m_width };
if (debugscreen && debugscreen->m_pixels[readIndex] != TRANSPARENT_COLOR) {
screen->m_pixels[writeIndex] = debugscreen->m_pixels[readIndex];
}
}
}
}
#endif
}