-
Notifications
You must be signed in to change notification settings - Fork 0
/
lesson4.cpp
143 lines (135 loc) · 3.58 KB
/
lesson4.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
#include<iostream>
#include<SDL2/SDL.h>
#include<SDL2/SDL_image.h>
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int TILE_SIZE = 40;
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, 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);
}
bool moveTexture(SDL_KeyboardEvent &event, int &x, int &y) {
int increment = 1;
bool quit = false;
if (event.repeat) {
std::cout << "Key press repeated!\n";
increment = 10;
}
switch (event.keysym.sym) {
case SDLK_UP:
std::cout << "Up key pressed!\n";
y -= increment;
break;
case SDLK_DOWN:
std::cout << "Down key pressed!\n";
y += increment;
break;
case SDLK_LEFT:
std::cout << "Left key pressed!\n";
x -= increment;
break;
case SDLK_RIGHT:
std::cout << "Right key pressed!\n";
x += increment;
break;
default:
std::cout << "Some other key pressed!\n";
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-1.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";
// main loop
SDL_Event event;
bool quit = false;
int x = 0, y = 0;
while (!quit) {
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_QUIT:
std::cout << "Window closed!\n";
quit = true;
break;
case SDL_KEYDOWN:
quit = moveTexture(event.key, x, y);
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);
IMG_Quit();
SDL_Quit();
return 0;
}