-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsdl.h
95 lines (73 loc) · 1.92 KB
/
sdl.h
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
#pragma once
#include <SDL.h>
#include <SDL_image.h>
#include "variable.h"
SDL_Window* window; // SDL 창을 나타내는 포인터 변수
SDL_Renderer* renderer; // SDL 렌더러를 나타내는 포인터 변수
// 초기화 함수
static int initAll()
{
if (SDL_Init(SDL_INIT_EVENTS) != 0) // SDL 초기화
{
fprintf(stderr, "%s\n", (SDL_GetError()));
return (0);
}
// SDL 창 생성
window = SDL_CreateWindow("Freezer", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, width, height, SDL_WINDOW_OPENGL);
if (window == 0)
{
fprintf(stderr, "%s\n", (SDL_GetError()));
return (0);
}
// 렌더러 생성 (가속화 및 디스플레이 주사율과 동기화)
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
if (renderer == 0)
{
fprintf(stderr, "%s\n", (SDL_GetError()));
return (0);
}
int imgFlags = IMG_INIT_PNG;
if (!(IMG_Init(imgFlags) & imgFlags)) // SDL_image 초기화
{
printf("SDL_image could not initialize! SDL_image Error: %s\n", IMG_GetError());
return (0);
}
return (1);
}
// SDL 종료 함수
static void closeAll()
{
SDL_DestroyRenderer(renderer); // 렌더러 해제
SDL_DestroyWindow(window); // 창 해제
IMG_Quit(); // SDL_image 종료
SDL_Quit(); // SDL 종료
}
// 텍스처 로딩 함수
SDL_Texture* loadTexture(const char* file) {
SDL_Surface* surface;
SDL_Texture* texture;
surface = IMG_Load(file);
if (surface == NULL) {
printf("fail to read %s\n", file);
return NULL;
}
texture = SDL_CreateTextureFromSurface(renderer, surface);
if (texture == NULL) {
printf("unable to create texture.\n");
}
SDL_FreeSurface(surface);
return texture;
}
// 텍스처 그리기 함수
void drawTexture(SDL_Texture* texture, int x, int y) {
SDL_Rect src;
SDL_Rect dst;
src.x = 0;
src.y = 0;
SDL_QueryTexture(texture, NULL, NULL, &src.w, &src.h);
dst.x = x;
dst.y = y;
dst.w = src.w;
dst.h = src.h;
SDL_RenderCopy(renderer, texture, &src, &dst);
}