[SDL] SDL 2D Camera - 2D 카메라
C, C++ 2024. 2. 1. 12:33 |SDL에 2D 카메라를 만들어 보자.
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
|
#include "SDL.h"
class Camera {
public:
int m_width;
int m_height;
int m_cameraBorder;
int m_target_width;
int m_target_height;
float m_x, m_y;
SDL_Texture* m_targetTexture;
public:
Camera(SDL_Renderer* renderer, int width, int height, int border, SDL_BlendMode mode) {
m_width = width;
m_height = height;
m_cameraBorder = border;
m_target_width = width + border * 2;
m_target_height = height + border * 2;
m_x = 0;
m_y = 0;
m_targetTexture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA32, SDL_TEXTUREACCESS_TARGET,
m_target_width, m_target_height);
SDL_SetTextureBlendMode(m_targetTexture, mode);
}
void Draw(SDL_Renderer* renderer, int windowHeight) {
float pixel_h = (float)windowHeight / m_height;
float correction_x = (int)m_x - m_x;
float correction_y = (int)m_y - m_y;
SDL_Rect dst = { (int)(correction_x * pixel_h - pixel_h * m_cameraBorder),
(int)(correction_y * pixel_h - pixel_h * m_cameraBorder),
(int)(m_target_width * pixel_h),
(int)(m_target_height * pixel_h) };
SDL_RenderCopy(renderer, m_targetTexture, NULL, &dst);
}
void Update(int xDirection, int yDirection) {
m_x += 0.5f * xDirection;
m_y += 0.5f * yDirection;
}
void CameraReset(SDL_Renderer* renderer, int width, int height, int border, SDL_BlendMode mode) {
m_width = width;
m_height = height;
m_cameraBorder = border;
m_target_width = width + border * 2;
m_target_height = height + border * 2;
SDL_DestroyTexture(m_targetTexture);
m_targetTexture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA32, SDL_TEXTUREACCESS_TARGET,
m_target_width, m_target_height);
SDL_SetTextureBlendMode(m_targetTexture, mode);
}
~Camera() {
SDL_DestroyTexture(m_targetTexture);
}
};
class Sprite {
public:
int width;
int height;
float m_destX, m_destY;
SDL_Surface* surface;
SDL_Texture* texture;
public:
Sprite(SDL_Renderer* renderer, const char* filename) {
surface = SDL_LoadBMP(filename);
width = surface->w;
height = surface->h;
m_destX = 0;
m_destY = 0;
texture = SDL_CreateTextureFromSurface(renderer, surface);
SDL_FreeSurface(surface);
}
void DrawSprite(SDL_Renderer* renderer, Camera* camera, int x, int y) {
SDL_Rect destRect = { (int)(m_destX - camera->m_x) + camera->m_cameraBorder + x,
(int)(m_destY - camera->m_y) + camera->m_cameraBorder + y,
width, height };
SDL_RenderCopy(renderer, texture, NULL, &destRect);
}
~Sprite() {
SDL_DestroyTexture(texture);
}
};
int main(int argc, char** argv) {
const int Window_Width = 640;
const int Window_Height = 480;
SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS);
SDL_Window* window = SDL_CreateWindow("SDL Simple Camera", SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED, Window_Width, Window_Height, SDL_WINDOW_RESIZABLE);
SDL_assert(window);
SDL_SetHint(SDL_HINT_RENDER_BATCHING, "1");
SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED |
SDL_RENDERER_PRESENTVSYNC);
SDL_assert(renderer);
// Init
Camera camera = Camera(renderer, 160, 120, 1, SDL_BLENDMODE_BLEND);
SDL_assert(camera.m_targetTexture);
Sprite shopSprite = Sprite(renderer, "shop.bmp");
Sprite characterSprite = Sprite(renderer, "character.bmp");
int xDirection = 0;
int yDirection = 0;
SDL_Event event;
bool quit = false;
while (!quit) {
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_QUIT:
quit = true;
break;
case SDL_KEYDOWN:
if (event.key.keysym.sym == SDLK_ESCAPE)
quit = true;
if (event.key.keysym.sym == SDLK_LEFT)
xDirection = -1;
if (event.key.keysym.sym == SDLK_RIGHT)
xDirection = 1;
if (event.key.keysym.sym == SDLK_UP)
yDirection = -1;
if (event.key.keysym.sym == SDLK_DOWN)
yDirection = 1;
if (event.key.keysym.sym == SDLK_r)
camera.CameraReset(renderer, 160, 120, 1, SDL_BLENDMODE_BLEND);
if (event.key.keysym.sym == SDLK_e)
camera.CameraReset(renderer, 640, 480, 1, SDL_BLENDMODE_BLEND);
break;
default:
xDirection = 0;
yDirection = 0;
break;
}
}
camera.Update(xDirection, yDirection);
// Camera target
SDL_SetRenderTarget(renderer, camera.m_targetTexture);
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0xFF);
SDL_RenderClear(renderer);
// Draw Sprite
shopSprite.DrawSprite(renderer, &camera, 0, 0);
characterSprite.DrawSprite(renderer, &camera, 116, 66);
// Screen target
SDL_SetRenderTarget(renderer, NULL);
SDL_SetRenderDrawColor(renderer, 0x00, 0xFF, 0x00, 0xFF);
SDL_RenderClear(renderer);
camera.Draw(renderer, Window_Height);
SDL_RenderPresent(renderer);
}
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
|
코드를 입력하고 빌드한다.
※ 참고
'C, C++' 카테고리의 다른 글
[SDL] SDL Box2D Collision Contact Point & Normal Vector - Box2D 충돌 지점과 법선 벡터 (0) | 2024.01.31 |
---|---|
[SDL] SDL Box2D Add Collision Shape - Box2D 충돌 감지 (1) | 2024.01.30 |
[SDL] SDL Box2D DebugDraw - 디버그 드로우 (1) | 2024.01.30 |
[SDL] SDL Box2D Collision - Box2D 충돌 (1) | 2024.01.30 |
[SDL] SDL Box2D - 물리 라이브러리 (0) | 2024.01.27 |