forked from glouw/paperview
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
148 lines (136 loc) · 3.53 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
#include <SDL2/SDL.h>
#include <X11/Xlib.h>
#include <dirent.h>
typedef struct
{
char** path;
size_t size;
}
Paths;
typedef struct
{
SDL_Texture** texture;
size_t size;
}
Textures;
typedef struct
{
Display* x11d;
SDL_Window* window;
SDL_Renderer* renderer;
}
Video;
static int32_t Compare(const void* a, const void* b)
{
char* const pa = *(char**) a;
char* const pb = *(char**) b;
const size_t la = strlen(pa);
const size_t lb = strlen(pb);
return (la > lb) ? 1 : (la < lb) ? -1 : strcmp(pa, pb);
}
static void Sort(Paths* self)
{
qsort(self->path, self->size, sizeof(*self->path), Compare);
}
static Paths Populate(const char* base)
{
size_t max = 8;
Paths self;
self.size = 0;
self.path = malloc(max * sizeof(*self.path));
DIR* const dir = opendir(base);
for(struct dirent* entry; (entry = readdir(dir)); )
{
const char* const path = entry->d_name;
if(strstr(path, ".bmp"))
{
char* const slash = "/";
char* const buffer = malloc(strlen(base) + strlen(slash) + strlen(path) + 3);
strcpy(buffer, base);
strcat(buffer, slash);
strcat(buffer, path);
if(self.size == max)
{
max *= 2;
self.path = realloc(self.path, max * sizeof(*self.path));
}
self.path[self.size] = buffer;
self.size += 1;
}
}
closedir(dir);
Sort(&self);
return self;
}
static void Depopulate(Paths* self)
{
for(size_t i = 0; i < self->size; i++)
free(self->path[i]);
free(self->path);
}
static Textures Cache(Paths* paths, SDL_Renderer* renderer)
{
Textures self;
self.size = paths->size;
self.texture = malloc(self.size * sizeof(*self.texture));
for(size_t i = 0; i < self.size; i++)
{
SDL_Surface* const surface = SDL_LoadBMP(paths->path[i]);
self.texture[i] = SDL_CreateTextureFromSurface(renderer, surface);
SDL_FreeSurface(surface);
}
return self;
}
static void Destroy(Textures* self)
{
for(size_t i = 0; i < self->size; i++)
SDL_DestroyTexture(self->texture[i]);
free(self->texture);
}
static Video Setup(void)
{
Video self;
self.x11d = XOpenDisplay(NULL);
const Window x11w = RootWindow(self.x11d, DefaultScreen(self.x11d));
SDL_Init(SDL_INIT_VIDEO);
self.window = SDL_CreateWindowFrom((void*) x11w);
self.renderer = SDL_CreateRenderer(self.window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
return self;
}
static void Teardown(Video* self)
{
XCloseDisplay(self->x11d);
SDL_Quit();
SDL_DestroyWindow(self->window);
SDL_DestroyRenderer(self->renderer);
}
int main(int argc, char* argv[])
{
if(argc != 3)
{
puts(
"./paperview train 5\n"
"./paperview FOLDER SPEED\n");
return 1;
}
const char* const base = argv[1];
const uint32_t speed = atoi(argv[2]);
Video video = Setup();
Paths paths = Populate(base);
Textures textures = Cache(&paths, video.renderer);
for(int32_t cycles = 0; /* TRUE */; cycles++)
{
const int32_t index = cycles / speed;
const int32_t frame = index % textures.size;
SDL_RenderCopy(video.renderer, textures.texture[frame], NULL, NULL);
SDL_RenderPresent(video.renderer);
SDL_Event event;
SDL_PollEvent(&event);
if(event.type == SDL_QUIT)
break;
}
Destroy(&textures);
Depopulate(&paths);
Teardown(&video);
return 0;
}