-
Notifications
You must be signed in to change notification settings - Fork 0
/
lesson1.cpp
65 lines (64 loc) · 1.97 KB
/
lesson1.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
#include<iostream>
#include<SDL2/SDL.h>
int main(int, char**) {
std::cout << "Initializing SDL sub-systems...\n";
if (SDL_Init(SDL_INIT_VIDEO) != 0) {
std::cout << "Init Error: " << SDL_GetError() << '\n';
return 1;
}
std::cout << "SDL sub-systems initialized!\n";
std::cout << "Creating window...\n";
SDL_Window *window = SDL_CreateWindow("Hello, World!", 100, 100, 640, 480, SDL_WINDOW_SHOWN);
if (window == nullptr) {
std::cout << "CreateWindow Error: " << SDL_GetError() << '\n';
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) {
std::cout << "CreateRenderer Error: " << SDL_GetError() << '\n';
SDL_DestroyWindow(window);
SDL_Quit();
return 1;
}
std::cout << "Renderer created!\n";
std::cout << "Loading BMP onto surface...\n";
char imagePath[] = "hello.bmp";
SDL_Surface *bmp = SDL_LoadBMP(imagePath);
if (bmp == nullptr) {
std::cout << "LoadBMP Error: " << SDL_GetError() << '\n';
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return 1;
}
std::cout << "BMP loaded onto surface!\n";
std::cout << "Creating texture from surface and freeing said surface...\n";
SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer, bmp);
SDL_FreeSurface(bmp);
if (texture == nullptr) {
std::cout << "CreateTextureFromSurface Error: " << SDL_GetError() << '\n';
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return 1;
}
std::cout << "Texture created!\n";
std::cout << "Rendering...\n";
// main loop
for (int i = 0; i < 3; ++i) {
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, texture, NULL, NULL);
SDL_RenderPresent(renderer);
SDL_Delay(1000);
}
std::cout << "Rendering done!\n";
std::cout << "Cleaning up and quitting...\n";
SDL_DestroyTexture(texture);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}