-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
247 lines (193 loc) · 6.13 KB
/
main.cpp
File metadata and controls
247 lines (193 loc) · 6.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#include <SDL.h>
#include <SDL_image.h>
#include <iostream>
#include <SDL_ttf.h>
#include "main.h"
#include "init.h"
#include "media.h"
#include "drawui.h"
Uint32 frameStart;
Uint32 frameEnd;
int currentFPS;
int seconds;
//Frees media and shuts down SDL
void close()
{
SDL_DestroyTexture( gPNGSurface );
gPNGSurface = NULL;
//Destroy window
SDL_DestroyWindow( gWindow );
gWindow = NULL;
//Quit SDL subsystems
IMG_Quit();
SDL_Quit();
TTF_Quit();;
}
// 使用路径读取图片的方法
SDL_Texture* loadSurface( std::string path )// 接受path 返回SDL_Surface* 指针
//
{
// 两个指针变量
SDL_Surface* loadedSurface = IMG_Load( path.c_str() );
if( loadedSurface == NULL )// 如果 IMG_Load读取失败则返回空 以此抛出错误
{
std::cout << "Unable to load image ! SDL_image Error: " << path.c_str() << IMG_GetError() << std::endl;
}
else
{
SDL_Texture* optimizedSurface;
//Convert surface to screen format
//optimizedSurface = SDL_ConvertSurface( loadedSurface, gScreenSurface->format, 0 );
// 使用SDL_ConvertSurface处理
optimizedSurface = SDL_CreateTextureFromSurface(renderer, loadedSurface);
if( optimizedSurface == NULL )
{
std::cout << "Unable to optimize image %s! SDL Error: " << path.c_str() << SDL_GetError() << std::endl;
}
else{
//释放内存
SDL_FreeSurface(loadedSurface);
return optimizedSurface;
}
}
return 0;// 寄了哦
}
int pos1, pos2 = 0;
float ddpi, hdpi, vdpi;
bool posmode1,posmode2;
int displayIndex;// 显示器索引,通常为0
std::string text;
int main( int argc, char* args[] )
{
init();
float ddpi, hdpi, vdpi;
if (SDL_GetDisplayDPI(displayIndex, &ddpi, &hdpi, &vdpi) != 0) {
SDL_Log("无法获取显示器DPI信息: %s\n", SDL_GetError());
SDL_Quit();
return 1;
}
SDL_Log("逻辑 DPI: ", ddpi);
SDL_Log("水平DPI:%f\n", hdpi);
SDL_Log("垂直DPI:%f\n", vdpi);
SDL_Rect displayBounds;
if (SDL_GetDisplayBounds(0, &displayBounds) != 0) {
SDL_Log("无法获取显示器尺寸: %s", SDL_GetError());
SDL_Quit();
return 1;
}
// 打印显示器尺寸信息到SDL日志
SDL_Log("显示器尺寸:");
SDL_Log("左上角坐标: (%d, %d)", displayBounds.x, displayBounds.y);
SDL_Log("宽度: %d", displayBounds.w);
SDL_Log("高度: %d", displayBounds.h);
text = " ";
if (renderer == NULL) {
// 处理渲染器创建失败的情况
SDL_Log("渲染器创建失败",SDL_GetError());
}
//Start up SDL and create window
SDL_Log("欢迎来到sdl2");
PlayMusic(); // 音频播放
//加载图像
if( !loadBackground() )
{
std::cout << "Failed to load Background!\n" << std::endl;
}
else
{
//Main loop flag
bool quit = false;
//Event handler
//事件处理
SDL_Event e;
//While application is running
while(!quit)
{
frameStart = SDL_GetTicks(); // 记录每帧开始时间
// 计算并显示游戏运行时间
seconds = (frameStart / 1000) % 60;
//Handle events on queue
while( SDL_PollEvent( &e ) != 0 )
{
if(e.type == SDL_TEXTINPUT){
if (TextInputFlag){
text += e.edit.text;
}
}
//User requests quit
if( e.type == SDL_QUIT )
{
quit = true;
}else if (e.type == SDL_MOUSEBUTTONDOWN) {
if (e.button.button == SDL_BUTTON_LEFT) {
int mouseX = e.button.x;
int mouseY = e.button.y;
TextInput(mouseX, mouseY);
}
}
if (e.key.keysym.sym == SDLK_BACKSPACE && TextInputFlag && !text.empty()) {
// 在这里处理按下退格键的逻辑
// 例如,删除字符串中的最后一个字符
text.pop_back();
}
}
// 那么一共有四个移动模式,向右动,向下动,向左动,向上动
// pos1是X pos2是Y
// 算法 默认 * 1.5/(DPI / 64)
if (pos1 == displayBounds.h-2500){//证明碰到了最下侧,则需要向上动
posmode1=true;
//
}
if (pos1 == 0){// 证明碰到最上侧,需要向下动
posmode1=false;
}
if (pos2 == displayBounds.w-2500){// 证明碰到最右侧,需要向左动
posmode2=true;
}
if (pos2 == 0){// 证明碰到最左侧,需要向右动
posmode2=false;
}
if (posmode1){
pos1++;
}
else{
pos1--;
}
if (posmode2){
pos2++;
}
else{
pos2--;
}
SDL_Rect destinationRect = {pos2, pos1, 2500,2500};
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, gPNGSurface, NULL, &destinationRect);
DrawUI();
if(seconds%2 != 0 && TextInputFlag){
int len = text.size();
if (len > 5){
len --;
}
if (len > 10){
len --;
}
if (len > 15){
len --;
}
if (len > 20){
len --;
}
SDL_RenderDrawLine(renderer, 30+len*10, 120, 30+len*10, 136);// 设置默认坐标 然后根据字符长度来判断坐标位置
}
InputInText();
SDL_RenderPresent(renderer);
frameEnd = SDL_GetTicks();
int frameTime = frameEnd - frameStart;
currentFPS = 1000 / frameTime;
SDL_UpdateWindowSurface(gWindow);
}
}
//Free resources and close SDL
close();
return 0;
}