-
Notifications
You must be signed in to change notification settings - Fork 0
/
lesson6.cpp
128 lines (122 loc) · 3.54 KB
/
lesson6.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
#include<iostream>
#include<SDL2/SDL.h>
#include<SDL2/SDL_ttf.h>
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
void logSDLError(std::ostream &os, const std::string &msg) {
os << msg << " Error: " << SDL_GetError() << '\n';
}
SDL_Texture* renderText(const std::string &message, const std::string filePath, SDL_Colour colour, int fontSize, SDL_Renderer *renderer) {
TTF_Font *font = TTF_OpenFont(filePath.c_str(), fontSize);
if (font == nullptr) {
logSDLError(std::cout, "OpenFont");
return nullptr;
}
SDL_Surface *surface = TTF_RenderText_Blended(font, message.c_str(), colour);
if (surface == nullptr) {
logSDLError(std::cout, "RenderText_Blended");
TTF_CloseFont(font);
return nullptr;
}
SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer, surface);
if (texture == nullptr) {
logSDLError(std::cout, "CreateTextureFromSurface");
}
SDL_FreeSurface(surface);
TTF_CloseFont(font);
return texture;
}
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);
}
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);
}
int main(int, char**) {
std::cout << "Initializing SDL sub-systems...\n";
if (SDL_Init(SDL_INIT_EVERYTHING) != 0) {
logSDLError(std::cout, "Init");
return 1;
}
std::cout << "SDL sub-systems initialized!\n";
std::cout << "Initializing font loading sub-systems...\n";
if (TTF_Init() != 0) {
logSDLError(std::cout, "TTF_Init");
SDL_Quit();
return 1;
}
std::cout << "Font loading sub-systems initialized!\n";
std::cout << "Creating window...\n";
SDL_Window *window = SDL_CreateWindow("Lesson 4", 100, 100, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
if (window == nullptr) {
logSDLError(std::cout, "CreateWindow");
SDL_Quit();
return 1;
}
std::cout << "Window created!\n";
std::cout << "Creating renderer...\n";
SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_SOFTWARE);
if (renderer == nullptr) {
logSDLError(std::cout, "CreateRenderer");
SDL_DestroyWindow(window);
SDL_Quit();
return 1;
}
std::cout << "Renderer created!\n";
std::cout << "Loading TTF font as texture...\n";
SDL_Colour colour = { 255, 255, 255, 255 };
SDL_Texture *image = renderText("TTF fonts are kewl!", "sample.ttf", colour, 64, renderer);
if (image == nullptr) {
SDL_DestroyTexture(image);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
TTF_Quit();
SDL_Quit();
return 1;
}
std::cout << "Font loaded and texture created!\n";
std::cout << "Rendering...\n";
int iW, iH;
SDL_QueryTexture(image, NULL, NULL, &iW, &iH);
int x = SCREEN_WIDTH / 2 - iW / 2;
int y = SCREEN_HEIGHT / 2 - iH / 2;
SDL_Event event;
bool quit = false;
// main loop
while (!quit) {
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_QUIT:
std::cout << "Window closed!\n";
quit = true;
break;
case SDL_KEYDOWN:
std::cout << "Key pressed!\n";
quit = true;
break;
case SDL_MOUSEBUTTONDOWN:
std::cout << "Mouse clicked!\n";
quit = true;
break;
}
}
SDL_RenderClear(renderer);
renderTexture(image, renderer, x, y);
SDL_RenderPresent(renderer);
}
std::cout << "Rendering done!\n";
std::cout << "Cleaning up and quitting...\n";
SDL_DestroyTexture(image);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
TTF_Quit();
SDL_Quit();
return 0;
}