[SDL] Simple DirectMedia Layer Setup and Getting Started - SDL 설정 및 초기화
C, C++ 2024. 1. 22. 21:59 |반응형
아래 링크에서 SDL을 다운로드하고 C++을 이용해 사용할 수 있도록 설정해 보자.
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
|
#include <iostream>
#include "SDL.h"
int main(int argc, char* argv[]) {
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
// Initialize the SDL library.
printf("SDL Initialization Fail: %s\n", SDL_GetError());
// Retrieve a message about the last error that occurred on the current thread.
return -1;
}
SDL_Window* window = SDL_CreateWindow("Hello World", SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED, 640, 480, SDL_WINDOW_RESIZABLE);
// Create a window with the specified position, dimensions, and flags.
if (!window) {
printf("SDL_CreateWindow Error: %s\n", SDL_GetError());
SDL_Quit();
// Clean up all initialized subsystems.
return -1;
}
SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, 0);
// Create a 2D rendering context for a window.
SDL_Event event;
bool quit = false;
while (!quit) {
while (SDL_PollEvent(&event)) {
// Poll for currently pending events.
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_SetRenderDrawColor(renderer, 255, 255, 255, SDL_ALPHA_OPAQUE);
// Set the color used for drawing operations (Rect, Line and Clear).
SDL_RenderClear(renderer);
// Clear the current rendering target with the drawing color.
SDL_RenderPresent(renderer);
// Update the screen with any rendering performed since the previous call.
}
SDL_DestroyRenderer(renderer);
// Destroy the rendering context for a window and free associated textures.
SDL_DestroyWindow(window);
// Destroy a window.
SDL_Quit();
// Clean up all initialized subsystems.
return 0;
}
|
코드를 입력하고 빌드한다.
반응형
'C, C++' 카테고리의 다른 글
[SDL] SDL_image Image Rendering - SDL 이미지 렌더링 (0) | 2024.01.23 |
---|---|
[SDL] SDL_ttf Font Rendering - SDL 텍스트 폰트 렌더링 (1) | 2024.01.23 |
Qt platform plugin error fix (0) | 2021.09.26 |
Qt6 설치 및 간단한 사용법 (0) | 2021.09.25 |
MariaDB(MySQL) C API (0) | 2021.08.29 |