-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
325 lines (315 loc) · 12 KB
/
main.c
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
#include <stdio.h>
#include <stdlib.h>
#include <SDL3/SDL.h>
#include <SDL3_image/SDL_image.h>
#define MAX_WINDOWS 1
FILE *config_file;
SDL_Window *(window)[MAX_WINDOWS];
SDL_Renderer *(renderer)[MAX_WINDOWS];
IMG_Animation *(animation)[MAX_WINDOWS];
SDL_Texture **(Frames)[MAX_WINDOWS];
int width, height, pos_x, pos_y;
int transition = 1;
float aspect_ratio;
char chibi_path[1024] = "../Sitting.png";
char buf[1024] = {0};
SDL_Surface *SDL_CreateRGBSurface(Uint32 flags, int width, int height, int depth, Uint32 Rmask, Uint32 Gmask,
Uint32 Bmask, Uint32 Amask) {
return SDL_CreateSurface(width, height,
SDL_GetPixelFormatForMasks(depth, Rmask, Gmask, Bmask, Amask));
}
static const SDL_DialogFileFilter filters[] = {
{"All images", "png;jpg;jpeg;gif;webp"},
{"PNG images", "png"},
{"JPEG images", "jpg;jpeg"},
{"GIF images", "gif"},
{"Webp images", "webp"},
{"All files", "*"}
};
static void SDLCALL callback(void *userdata, const char *const*filelist, int filter) {
if (!filelist) {
SDL_Log("An error occured: %s", SDL_GetError());
return;
} else if (!*filelist) {
SDL_Log("The user did not select any file.");
SDL_Log("Most likely, the dialog was canceled.");
return;
}
if (*filelist) {
SDL_Log("Loading '%s'", *filelist);
IMG_FreeAnimation(animation[0]);
animation[0] = IMG_LoadAnimation(*filelist);
if (animation[0] == nullptr) {
SDL_Log("Chibi not found, Quitting.'%s', Bailing Out, Goodbye!", SDL_GetError());
exit(1);
}
SDL_Log("Loaded '%s'", *filelist);
Frames[0] = calloc(animation[0]->count, sizeof(SDL_Texture));
for (int i = 0; i < animation[0]->count; ++i) {
SDL_DestroyTexture(Frames[0][i]);
Frames[0][i] = SDL_CreateTextureFromSurface(renderer[0], animation[0]->frames[i]);
SDL_SetTextureBlendMode(Frames[0][i],SDL_BLENDMODE_BLEND);
}
aspect_ratio = (float) animation[0]->w / (float) animation[0]->h;
SDL_SetWindowAspectRatio(window[0], aspect_ratio, aspect_ratio);
SDL_SetWindowSize(window[0], SDL_min(animation[0]->w, 200 * aspect_ratio), SDL_min(animation[0]->h, 200));
SDL_SetWindowShape(window[0], animation[0]->frames[0]);
strcpy(chibi_path, *filelist);
SDL_GetWindowSize(window[0], &width, &height);
SDL_GetWindowPosition(window[0], &pos_x, &pos_y);
#ifdef SDL_PLATFORM_UNIX
char *home = getenv("HOME");
strcpy(buf, home);
strcat(buf, "/.config/el-creatura");
SDL_CreateDirectory(buf);
strcat(buf, "/config.txt");
#else
char *home = getenv("LocalAppData");
strcpy(buf, home);
strcat(buf, "/el-creatura");
SDL_CreateDirectory(buf);
strcat(buf, "/config.txt");
#endif
if ((config_file = fopen(buf, "w")) == nullptr){
perror("fopen");
exit(1);
}
SDL_Log("Writing to Config File");
sprintf(buf, "%d,%d,%d,%d,%d,%s", width, height, pos_x, pos_y, transition, chibi_path);
fputs(buf, config_file);
fclose(config_file);
}
}
// ReSharper disable once CppDFAConstantFunctionResult
int main(int argc, char *argv[]) {
if (argc > 1) {
if (argv[1][0] == '~') {
char *home = getenv("HOME");
strcpy(buf, home);
strcat(buf, argv[1] + 1);
}
config_file = fopen(argv[1], "r");
if (config_file == nullptr) {
SDL_Log("'%s' does not exist", argv[1]);
exit(1);
}
SDL_Log("Loading From '%s'", argv[1]);
if (fgets(buf, 1024, config_file) == nullptr) {
SDL_Log("Error in file structure");
exit(1);
}
char *token = strtok(buf, ",");
if (token == nullptr) {
SDL_Log("Error in file strcture");
exit(1);
}
width = (int) strtol(token, nullptr, 10);
token = strtok(nullptr, ",");
if (token == nullptr) {
SDL_Log("Error in file strcture");
exit(1);
}
height = (int) strtol(token, nullptr, 10);
token = strtok(nullptr, ",");
if (token == nullptr) {
SDL_Log("Error in file strcture");
exit(1);
}
pos_x = (int) strtol(token, nullptr, 10);
token = strtok(nullptr, ",");
if (token == nullptr) {
SDL_Log("Error in file strcture");
exit(1);
}
pos_y = (int) strtol(token, nullptr, 10);
token = strtok(nullptr, ",");
if (token == nullptr) {
SDL_Log("Error in file strcture");
exit(1);
}
transition = (int) strtol(token, nullptr, 10);
token = strtok(nullptr, ",");
if (token == nullptr) {
SDL_Log("Error in file strcture");
exit(1);
}
strcpy(chibi_path, token);
chibi_path[strcspn(chibi_path, "\n")] = 0;
fclose(config_file);
} else {
#ifdef SDL_PLATFORM_UNIX
char *home = getenv("HOME");
strcpy(buf, home);
strcat(buf, "/.config/el-creatura/config.txt");
#else
char *home = getenv("LocalAppData");
strcpy(buf, home);
strcat(buf, ".el-creatura/config.txt");
#endif
config_file = fopen(buf, "r");
if (config_file != nullptr) {
SDL_Log("Loading From '%s'", buf);
if (fgets(buf, 1024, config_file) == nullptr) {
SDL_Log("Error in file structure");
exit(1);
}
char *token = strtok(buf, ",");
if (token == nullptr) {
SDL_Log("Error in file strcture");
exit(1);
}
width = (int) strtol(token, nullptr, 10);
token = strtok(nullptr, ",");
if (token == nullptr) {
SDL_Log("Error in file strcture");
exit(1);
}
height = (int) strtol(token, nullptr, 10);
token = strtok(nullptr, ",");
if (token == nullptr) {
SDL_Log("Error in file strcture");
exit(1);
}
pos_x = (int) strtol(token, nullptr, 10);
token = strtok(nullptr, ",");
if (token == nullptr) {
SDL_Log("Error in file strcture");
exit(1);
}
pos_y = (int) strtol(token, nullptr, 10);
token = strtok(nullptr, ",");
if (token == nullptr) {
SDL_Log("Error in file strcture");
exit(1);
}
transition = (int) strtol(token, nullptr, 10);
token = strtok(nullptr, ",");
if (token == nullptr) {
SDL_Log("Error in file strcture");
exit(1);
}
strcpy(chibi_path, token);
chibi_path[strcspn(chibi_path, "\n")] = 0;
fclose(config_file);
} else {
SDL_Log("Loading Defaults");
width = height = 200;
pos_x = 1920 - 200;
pos_y = 1080 - 200;
}
}
if (!SDL_CreateWindowAndRenderer("EL creatura", width, height,
SDL_WINDOW_RESIZABLE | SDL_WINDOW_TRANSPARENT |
SDL_WINDOW_ALWAYS_ON_TOP | SDL_WINDOW_BORDERLESS | SDL_WINDOW_UTILITY |
SDL_WINDOW_NOT_FOCUSABLE, &window[0],
&renderer[0])) {
SDL_Log("Couldnt Create Window and Renderer. '%s', Bailing Out, Goodbye!", SDL_GetError());
exit(1);
}
if (!SDL_SetWindowPosition(window[0], pos_x, pos_y)) {
SDL_Log("Couldnt Move Window. '%s', Bailing Out, Goodbye!", SDL_GetError());
exit(1);
}
SDL_Log("Created Window %dx%d at %dx%d", width, height, pos_x, pos_y);
animation[0] = IMG_LoadAnimation(chibi_path);
if (animation[0] == nullptr) {
SDL_Log("Chibi not found. '%s', Bailing Out, Goodbye!", SDL_GetError());
exit(1);
}
SDL_Log("Loaded '%s'", chibi_path);
Frames[0] = calloc(animation[0]->count, sizeof(SDL_Texture));
for (int i = 0; i < animation[0]->count; ++i) {
Frames[0][i] = SDL_CreateTextureFromSurface(renderer[0], animation[0]->frames[i]);
SDL_SetTextureBlendMode(Frames[0][i],SDL_BLENDMODE_BLEND);
}
SDL_Event event;
bool quit = false;
bool bordered = false;
int current_frame_idx = 0;
int current_transparency_step = 0;
unsigned int lastTime = 0, currentTime;
aspect_ratio = (float) width / (float) height;
while (!quit) {
SDL_RenderClear(renderer[0]);
if (transition == 1) {
if (current_transparency_step > 0) {
currentTime = SDL_GetTicks();
if (currentTime > lastTime + 100) {
current_transparency_step--;
for (int j = 0; j < animation[0]->count; ++j) {
SDL_SetTextureAlphaMod(Frames[0][j], 51 * current_transparency_step);
}
lastTime = currentTime;
}
} else if (current_transparency_step < 0) {
currentTime = SDL_GetTicks();
if (currentTime > lastTime + 100) {
current_transparency_step++;
for (int j = 0; j < animation[0]->count; ++j) {
SDL_SetTextureAlphaMod(Frames[0][j], 51 * (5 + current_transparency_step));
}
lastTime = currentTime;
}
}
}
if (current_frame_idx < animation[0]->count) {
SDL_RenderTexture(renderer[0], Frames[0][current_frame_idx], nullptr, nullptr);
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_EVENT_QUIT: quit = true;
break;
case SDL_EVENT_MOUSE_BUTTON_UP:
if (event.button.clicks == 2 && event.button.button == SDL_BUTTON_LEFT)
SDL_ShowOpenFileDialog(callback, nullptr, window[0], filters, 6, nullptr, 0);
if (event.button.button == SDL_BUTTON_MIDDLE)
SDL_SetWindowAspectRatio(window[0], aspect_ratio, aspect_ratio);
if (event.button.button == SDL_BUTTON_RIGHT) {
SDL_SetWindowBordered(window[0], !bordered);
bordered = !bordered;
}
break;
case SDL_EVENT_WINDOW_MOUSE_ENTER:
current_transparency_step = 5;
break;
case SDL_EVENT_WINDOW_MOUSE_LEAVE:
current_transparency_step = -5;
break;
default: ;
}
}
SDL_SetRenderDrawColorFloat(renderer[0], 0, 0, 0,SDL_ALPHA_TRANSPARENT_FLOAT);
SDL_SetWindowShape(window[0], animation[0]->frames[current_frame_idx]);
SDL_RenderPresent(renderer[0]);
SDL_Delay(animation[0]->delays[current_frame_idx]);
current_frame_idx++;
} else {
current_frame_idx = 0;
}
}
SDL_GetWindowSize(window[0], &width, &height);
SDL_GetWindowPosition(window[0], &pos_x, &pos_y);
#ifdef SDL_PLATFORM_UNIX
char *home = getenv("HOME");
strcpy(buf, home);
strcat(buf, "/.config/el-creatura");
SDL_CreateDirectory(buf);
strcat(buf, "/config.txt");
#else
char *home = getenv("LocalAppData");
strcpy(buf, home);
strcat(buf, "/el-creatura");
SDL_CreateDirectory(buf);
strcat(buf, "/config.txt");
#endif
if ((config_file = fopen(buf, "w")) == nullptr){
perror("fopen");
exit(1);
}
SDL_Log("Writing to Config File");
sprintf(buf, "%d,%d,%d,%d,%d,%s", width, height, pos_x, pos_y, transition, chibi_path);
fputs(buf, config_file);
fclose(config_file);
IMG_FreeAnimation(animation[0]);
free(Frames[0]);
return 0;
}