-
Notifications
You must be signed in to change notification settings - Fork 0
/
sdl_helpers.cpp
70 lines (59 loc) · 1.62 KB
/
sdl_helpers.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
#include "sdl_helpers.h"
bool initializeSDL(SDL_Window*& win, SDL_Renderer*& ren)
{
if (SDL_Init(SDL_INIT_VIDEO) !=0)
{
logSDLError(std::cout, "SDL Init");
return false;
}
win = SDL_CreateWindow("Hello World", 100, 100, 640, 480, SDL_WINDOW_SHOWN);
if (win == nullptr)
{
logSDLError(std::cout, "SDL_CreateWindow");
SDL_Quit();
return false;
}
ren = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
if (ren == nullptr)
{
SDL_DestroyWindow(win);
logSDLError(std::cout, "SDL_CreateRenderer");
SDL_Quit();
return false;
}
if( (IMG_Init(IMG_INIT_PNG) & IMG_INIT_PNG) != IMG_INIT_PNG)
{
logSDLError(std::cout, "IMG_init");
SDL_Quit();
return 1;
}
return true;
}
void logSDLError(std::ostream &os, const std::string &msg)
{
os << msg << " Error: " << SDL_GetError() << std::endl;
}
SDL_Texture* loadTexture(const std::string &path, SDL_Renderer* renderer)
{
SDL_Texture *texture = IMG_LoadTexture(renderer, path.c_str());
if (texture == nullptr)
{
logSDLError(std::cout, "IMG_LoadTexture");
}
return texture;
}
void renderTexture(SDL_Texture *texture, SDL_Renderer *renderer, int x, int y)
{
int w, h;
SDL_QueryTexture(texture, NULL, NULL, &w, &h);
renderTexture(texture, renderer, x, y, w, h);
}
void renderTexture(SDL_Texture *texture, SDL_Renderer *renderer, int x, int y, int w, int h)
{
SDL_Rect dst;
dst.x = x;
dst.y = y;
dst.w = w;
dst.h = h;
SDL_RenderCopy(renderer, texture, NULL, &dst);
}