-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpixel(deprecated).cpp
More file actions
executable file
·158 lines (124 loc) · 3.13 KB
/
Copy pathpixel(deprecated).cpp
File metadata and controls
executable file
·158 lines (124 loc) · 3.13 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
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
153
154
155
156
157
158
#include <iostream>
#include <string>
#include "Game.hpp"
using namespace std;
bool init();
void kill();
bool loop();
// Pointers to our window, renderer, texture, and font
SDL_Window* window;
SDL_Renderer* renderer;
SDL_Texture *texture, *text;
TTF_Font* font;
string input;
int main(int argc, char** args) {
if ( !init() ) {
system("pause");
return 1;
}
while ( loop() ) {
// wait before processing the next frame
SDL_Delay(10);
}
kill();
return 0;
}
bool loop() {
static const unsigned char* keys = SDL_GetKeyboardState( NULL );
SDL_Event e;
SDL_Rect dest;
// Clear the window to white
SDL_SetRenderDrawColor( renderer, 255, 255, 255, 255 );
SDL_RenderClear( renderer );
// Event loop
while ( SDL_PollEvent( &e ) != 0 ) {
switch (e.type) {
case SDL_QUIT:
return false;
case SDL_TEXTINPUT:
input += e.text.text;
break;
case SDL_KEYDOWN:
if (e.key.keysym.sym == SDLK_BACKSPACE && input.size()) {
input.pop_back();
}
break;
}
}
// Render texture
SDL_RenderCopy(renderer, texture, NULL, NULL);
SDL_Color foreground = { 0, 0, 0 };
if ( input.size() ) {
SDL_Surface* text_surf = TTF_RenderText_Solid(font, input.c_str(), foreground);
text = SDL_CreateTextureFromSurface(renderer, text_surf);
dest.x = 320 - (text_surf->w / 2.0f);
dest.y = 240;
dest.w = text_surf->w;
dest.h = text_surf->h;
SDL_RenderCopy(renderer, text, NULL, &dest);
SDL_DestroyTexture(text);
SDL_FreeSurface(text_surf);
}
// Update window
SDL_RenderPresent( renderer );
return true;
}
bool init() {
if ( SDL_Init( SDL_INIT_EVERYTHING ) < 0 ) {
cout << "Error initializing SDL: " << SDL_GetError() << endl;
return false;
}
if ( IMG_Init(IMG_INIT_PNG) < 0 ) {
cout << "Error initializing SDL_image: " << IMG_GetError() << endl;
return false;
}
// Initialize SDL_ttf
if ( TTF_Init() < 0 ) {
cout << "Error intializing SDL_ttf: " << TTF_GetError() << endl;
return false;
}
window = SDL_CreateWindow( "Example", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, SDL_WINDOW_SHOWN );
if ( !window ) {
cout << "Error creating window: " << SDL_GetError() << endl;
return false;
}
renderer = SDL_CreateRenderer( window, -1, SDL_RENDERER_ACCELERATED );
if ( !renderer ) {
cout << "Error creating renderer: " << SDL_GetError() << endl;
return false;
}
SDL_Surface* buffer = IMG_Load("test.png");
if ( !buffer ) {
cout << "Error loading image test.png: " << SDL_GetError() << endl;
return false;
}
texture = SDL_CreateTextureFromSurface( renderer, buffer );
SDL_FreeSurface( buffer );
buffer = NULL;
if ( !texture ) {
cout << "Error creating texture: " << SDL_GetError() << endl;
return false;
}
// Load font
font = TTF_OpenFont("font.ttf", 72);
if ( !font ) {
cout << "Error loading font: " << TTF_GetError() << endl;
return false;
}
// Start sending SDL_TextInput events
SDL_StartTextInput();
return true;
}
void kill() {
SDL_StopTextInput();
TTF_CloseFont( font );
SDL_DestroyTexture( texture );
texture = NULL;
SDL_DestroyRenderer( renderer );
SDL_DestroyWindow( window );
window = NULL;
renderer = NULL;
TTF_Quit();
IMG_Quit();
SDL_Quit();
}