-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSDL_utils.cpp
73 lines (65 loc) · 1.75 KB
/
SDL_utils.cpp
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
//#define SDL_MAIN_HANDLED
#include<SDL.h>
#include "SDL_utils.h"
#include<iostream>
#include<SDL_image.h>
#include <SDL_ttf.h>
using namespace std;
void logSDLError(std::ostream& os,
const std::string& msg, bool fatal)
{
os << msg << " Error: " << SDL_GetError() << std::endl;
if (fatal) {
SDL_Quit();
exit(1);
}
}
void waitUntilKeyPressed()
{
SDL_Event e;
while (true) {
if (SDL_WaitEvent(&e) != 0 &&
(e.type == SDL_KEYDOWN || e.type == SDL_QUIT))
return;
SDL_Delay(100);
}
}
void quitSDL(SDL_Window* window, SDL_Renderer* renderer)
{
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
}
SDL_Texture* LoadTexture(const std::string filepath, SDL_Renderer* render)
{
//create temp surface then load IMG to it
SDL_Surface* tmpsurface = NULL;
tmpsurface = IMG_Load(filepath.c_str());
if (tmpsurface == NULL)
{
printf("Unable to load image %s! SDL_image Error: %s\n", filepath.c_str(), IMG_GetError());
}
//create texture to load it into
SDL_Texture* texture = NULL;
texture = SDL_CreateTextureFromSurface(render, tmpsurface);
if (texture == NULL)
{
cout << "Unable to create texture from %s! SDL Error: %s\n", filepath.c_str(), SDL_GetError();
}
//free temp surface
SDL_FreeSurface(tmpsurface);
//return the loadedtexture
return texture;
}
void renderTexture(SDL_Texture* tex, SDL_Renderer* ren, int x, int y, int w, int h)
{
//Thiết lập hình chữ nhật đích mà chúng ta muốn vẽ ảnh vào trong
SDL_Rect dst;
dst.x = x;
dst.y = y;
dst.w = w;
dst.h = h;
//Đưa toàn bộ ảnh trong texture vào hình chữ nhật đích
//(ảnh sẽ co dãn cho khớp với kích cỡ mới)
SDL_RenderCopy(ren, tex, NULL, &dst);
}