-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGraphics.cpp
160 lines (125 loc) · 3.05 KB
/
Graphics.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#include "Graphics.h"
Graphics* Graphics::sInstance = NULL;
bool Graphics::sInitialized = false;
Graphics* Graphics::Instance()
{
if (sInstance == NULL)
{
sInstance = new Graphics();
}
return sInstance;
}
void Graphics::Release()
{
delete sInstance;
sInstance = NULL;
sInitialized = false;
}
bool Graphics::Initialized()
{
return sInitialized;
}
Graphics::Graphics()
{
mBackBuffer = NULL;
sInitialized = Init();
}
Graphics::~Graphics()
{
SDL_DestroyWindow(mWindow);
mWindow = NULL;
SDL_DestroyRenderer(mRenderer);
mRenderer = NULL;
SDL_FreeSurface(mBackBuffer);
mBackBuffer = NULL;
TTF_Quit();
IMG_Quit();
SDL_Quit();
}
bool Graphics::Init()
{
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO < 0))
{
printf("SDL Initialization Error: %s\n", SDL_GetError());
return false;
}
mWindow = SDL_CreateWindow(WINDOW_TITLE, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
if (mWindow == NULL)
{
printf("Window Creation Error: %s\n", SDL_GetError());
return false;
}
mRenderer = SDL_CreateRenderer(mWindow, -1, SDL_RENDERER_ACCELERATED);
if (mRenderer == NULL)
{
printf("Renderer Creation Error: %s\n", SDL_GetError());
return false;
}
SDL_SetRenderDrawColor(mRenderer, 0x00, 0x00, 0x00, 0xFF);
int flags = IMG_INIT_PNG;
if (!IMG_Init(flags) & flags)
{
printf("Image Initialization Error: %s\n", IMG_GetError());
return false;
}
if (TTF_Init() == -1)
{
printf("TTF Initialization Error: %s\n", TTF_GetError());
return false;
}
mBackBuffer = SDL_GetWindowSurface(mWindow);
return true;
}
SDL_Texture* Graphics::LoadTexture(string path)
{
SDL_Texture* tex = NULL;
SDL_Surface* surface = IMG_Load(path.c_str());
if (surface == NULL)
{
printf("Image Load Error: Path(%s) - Error(%s)\n", path.c_str(), IMG_GetError());
return tex;
}
tex = SDL_CreateTextureFromSurface(mRenderer, surface);
if (tex == NULL)
{
printf("Create Texture Error: %s\n", SDL_GetError());
return tex;
}
SDL_FreeSurface(surface);
return tex;
}
SDL_Texture* Graphics::CreateTextTexture(TTF_Font* font, string text, SDL_Color color)
{
SDL_Surface* surface = TTF_RenderText_Solid(font, text.c_str(), color);
if (surface == NULL)
{
printf("Text Render Error: %s\n", TTF_GetError());
return NULL;
}
SDL_Texture* texture = SDL_CreateTextureFromSurface(mRenderer, surface);
if (texture == NULL)
{
printf("Text Texture Creation Error: %s\n", SDL_GetError());
return NULL;
}
SDL_FreeSurface(surface);
return texture;
}
void Graphics::ClearBackBuffer()
{
SDL_RenderClear(mRenderer);
}
void Graphics::DrawTexture(SDL_Texture* tex, SDL_Rect* clip, SDL_Rect* rend, float angle, SDL_RendererFlip flip)
{
SDL_RenderCopyEx(mRenderer, tex, clip, rend, angle, NULL, flip);
}
void Graphics::DrawLine(float startX, float startY, float endX, float endY)
{
SDL_SetRenderDrawColor(mRenderer, 255, 255, 255, SDL_ALPHA_OPAQUE);
SDL_RenderDrawLine(mRenderer, startX, startY, endX, endY);
SDL_SetRenderDrawColor(mRenderer, 0, 0, 0, SDL_ALPHA_OPAQUE);
}
void Graphics::Render()
{
SDL_RenderPresent(mRenderer);
}