-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsdl-image.cpp
More file actions
98 lines (85 loc) · 2.34 KB
/
Copy pathsdl-image.cpp
File metadata and controls
98 lines (85 loc) · 2.34 KB
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
#include <SDL.h>
#include <SDL_image.h>
#include <SDL_video.h>
#include <cstddef>
#include <iostream>
using namespace std;
const int SCREEN_WIDTH = 680;
const int SCREEN_HEIGHT = 540;
SDL_Window* gWindow = NULL;
SDL_Surface* gScreenSurface = NULL;
bool init() {
bool success = true;
// initialize SDL
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
cout << "SDL could not initialize " << SDL_GetError() << endl;
success = false;
} else {
// create window
gWindow = SDL_CreateWindow("SDL Image formats", SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH,
SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
if (gWindow == NULL) {
cout << "Window couldnot be created " << SDL_GetError() << endl;
success = false;
} else {
// init PNG loading
if (!(IMG_Init(IMG_INIT_PNG) & IMG_INIT_PNG)) {
cout << "SDL image could not be initialize! SDL_image error "
<< SDL_GetError() << endl;
success = false;
} else {
// Get window surface
gScreenSurface = SDL_GetWindowSurface(gWindow);
}
}
}
return success;
}
SDL_Surface* loadSurface(string path) {
SDL_Surface* optimizedSurface = NULL;
SDL_Surface* loadedSurface = IMG_Load(path.c_str());
if (loadedSurface == NULL) {
cout << "Unable to load image! SDL_image Error " << SDL_GetError() << endl;
} else {
optimizedSurface =
SDL_ConvertSurface(loadedSurface, gScreenSurface->format, 0);
if (optimizedSurface == NULL) {
cout << "Unable to optimize image! SDL_image Error " << SDL_GetError()
<< endl;
}
SDL_FreeSurface(loadedSurface);
}
return optimizedSurface;
}
void close() {
SDL_FreeSurface(gScreenSurface);
gScreenSurface = NULL;
SDL_DestroyWindow(gWindow);
gWindow = NULL;
SDL_Quit();
}
void keepWindowOpen() {
SDL_Event e;
bool quit = false;
while (!quit) {
while (SDL_PollEvent(&e)) {
if (e.type == SDL_QUIT) {
quit = true;
}
}
}
}
int main() {
if (!init()) {
cout << "SDL could not be initialized" << endl;
} else {
SDL_BlitSurface(loadSurface("assets/signature.png"), NULL, gScreenSurface,
NULL);
// loadSurface("assets/signature.png");
SDL_UpdateWindowSurface(gWindow);
keepWindowOpen();
}
close();
return EXIT_SUCCESS;
}