[SDL] SDL Image Color Key - SDL 이미지 컬러키
C, C++ 2024. 1. 24. 13:23 |로드한 이미지에 컬러키를 설정하고 투명하게 처리해 보자.
파란 배경의 BMP 파일을 준비한다.
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
|
#include <iostream>
#include "SDL.h"
#pragma comment(lib, "sdl2.lib")
#pragma comment(lib, "sdl2main.lib")
int main(int argc, char* argv[]) {
SDL_Init(SDL_INIT_VIDEO);
SDL_Window* window = SDL_CreateWindow("SDL Test", SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED, 640, 480, SDL_WINDOW_RESIZABLE);
SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, 0);
SDL_Surface* bmpSurface = SDL_LoadBMP("player.bmp");
SDL_SetColorKey(bmpSurface, SDL_TRUE, SDL_MapRGB(bmpSurface->format, 0, 0, 0xFF));
// Set the color key (transparent pixel) in a surface.
// The color key defines a pixel value that will be treated as transparent in a blit.
// For example, one can use this to specify that cyan pixels should be considered
// transparent, and therefore not rendered.
SDL_Rect destRect = { 0, 0, bmpSurface->w, bmpSurface->h };
SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, bmpSurface);
SDL_Event event;
bool quit = false;
while (!quit) {
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_QUIT:
quit = true;
break;
case SDL_KEYDOWN:
printf("Key pressed: %s\n", SDL_GetKeyName(event.key.keysym.sym));
if (event.key.keysym.sym == SDLK_ESCAPE)
quit = true;
break;
default:
break;
}
}
SDL_RenderCopy(renderer, texture, NULL, &destRect);
SDL_RenderPresent(renderer);
}
SDL_FreeSurface(bmpSurface);
SDL_DestroyTexture(texture);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
|
파란색(0, 0, 255)을 컬러키로 설정하는 코드를 작성하고 빌드한다.
컬러키 설정 부분을 주석처리하고 빌드해 보자.
//SDL_SetColorKey(bmpSurface, SDL_TRUE, SDL_MapRGB(bmpSurface->format, 0, 0, 0xFF));
'C, C++' 카테고리의 다른 글
[SDL] Dear ImGui SDL Renderer Examples - Dear ImGui SDL 렌더러 예제 (0) | 2024.01.25 |
---|---|
[SDL] SDL_mixer Audio - SDL 믹서 오디오 (0) | 2024.01.24 |
[SDL] SDL_image Image Rendering - SDL 이미지 렌더링 (0) | 2024.01.23 |
[SDL] SDL_ttf Font Rendering - SDL 텍스트 폰트 렌더링 (1) | 2024.01.23 |
[SDL] Simple DirectMedia Layer Setup and Getting Started - SDL 설정 및 초기화 (0) | 2024.01.22 |