-
Notifications
You must be signed in to change notification settings - Fork 0
/
lesson5.cpp
152 lines (145 loc) · 3.77 KB
/
lesson5.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
#include<iostream>
#include<SDL2/SDL.h>
#include<SDL2/SDL_image.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* loadTexture(const std::string &filePath, SDL_Renderer *renderer) {
SDL_Texture *texture = IMG_LoadTexture(renderer, filePath.c_str());
if (texture == nullptr) {
logSDLError(std::cout, "LoadTexture");
}
return texture;
}
void renderTexture(SDL_Texture *texture, SDL_Renderer *renderer, SDL_Rect dst, SDL_Rect *clip = nullptr) {
SDL_RenderCopy(renderer, texture, clip, &dst);
}
void renderTexture(SDL_Texture *texture, SDL_Renderer *renderer, int x, int y, SDL_Rect *clip = nullptr) {
SDL_Rect dst;
dst.x = x;
dst.y = y;
if (clip != nullptr) {
dst.w = clip->w;
dst.h = clip->h;
}
else {
SDL_QueryTexture(texture, NULL, NULL, &dst.w, &dst.h);
}
renderTexture(texture, renderer, dst, clip);
}
bool clipTexture(SDL_KeyboardEvent &event, int &clip) {
bool quit = false;
switch (event.keysym.sym) {
case SDLK_1:
std::cout << "Showing clip 1!\n";
clip = 0;
break;
case SDLK_2:
std::cout << "Showing clip 2!\n";
clip = 1;
break;
case SDLK_3:
std::cout << "Showing clip 3!\n";
clip = 2;
break;
case SDLK_4:
std::cout << "Showing clip 4!\n";
clip = 3;
break;
default:
quit = true;
}
return quit;
}
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 image loading sub-systems...\n";
if ((IMG_Init(IMG_INIT_PNG) & IMG_INIT_PNG) != IMG_INIT_PNG) {
logSDLError(std::cout, "IMG_Init");
SDL_Quit();
return 1;
}
std::cout << "Image 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 PNG image as texture...\n";
SDL_Texture *image = loadTexture("image-2.png", renderer);
if (image == nullptr) {
SDL_DestroyTexture(image);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
IMG_Quit();
SDL_Quit();
return 1;
}
std::cout << "PNG loaded and texture created!\n";
std::cout << "Rendering...\n";
int iW = 100, iH = 100;
int x = SCREEN_WIDTH / 2 - iW / 2;
int y = SCREEN_HEIGHT / 2 - iH / 2;
SDL_Rect clips[4];
int idx = 0;
for (int i = 0; i < 2; ++i) {
for (int j = 0; j < 2; ++j) {
clips[idx].x = iW * i;
clips[idx].y = iH * j;
clips[idx].w = iW;
clips[idx].h = iH;
++idx;
}
}
int clip = 0;
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:
quit = clipTexture(event.key, clip);
break;
case SDL_MOUSEBUTTONDOWN:
std::cout << "Mouse clicked!\n";
quit = true;
break;
}
}
SDL_RenderClear(renderer);
renderTexture(image, renderer, x, y, &clips[clip]);
SDL_RenderPresent(renderer);
}
std::cout << "Rendering done!\n";
std::cout << "Cleaning up and quitting...\n";
SDL_DestroyTexture(image);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
IMG_Quit();
SDL_Quit();
return 0;
}