summary refs log tree commit diff
path: root/src/sdl.c
blob: 875cde5692bc5d7e917eec1bc38dd9e7b6599a14 (plain)
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
#define SDL_MAIN_HANDLED
#include <SDL2/SDL.h>

#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>

#include "include.h"

int module_openmpt(struct blob *file, struct userdata *userdata);

#define eprintf(...) fprintf(stderr, __VA_ARGS__)

static const int WINDOW_WIDTH = 160, WINDOW_HEIGHT = 90;

SDL_Window *window = NULL;

struct blob load_file(char const *const name);

struct userdata userdata = {
	.callback = NULL,
	.user = NULL,
};

void audio_callback(void *userdata_void, unsigned char *stream_void, int const length) {
	struct userdata *user = userdata_void;
	float *stream = (float *) stream_void;
	if (user->callback == NULL) {
		for (int i = 0; i < length / sizeof (float); i++) {
			stream[i] = 0.0;
		}
	} else {
		user->callback(user->user, stream_void, length);
	}
}

int main(void) {
	if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO)) {
		eprintf("failed to init SDL: %s\n");
		return EXIT_FAILURE;
	}
	
	SDL_StopTextInput();
	
	if ((window = SDL_CreateWindow("mu-sdl", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, WINDOW_WIDTH, WINDOW_HEIGHT, SDL_WINDOW_RESIZABLE | SDL_WINDOW_HIDDEN)) == NULL) {
		eprintf("failed to create a window: %s\n");
		SDL_Quit();
		return EXIT_FAILURE;
	}
	
	SDL_Renderer *renderer;
	if ((renderer = SDL_CreateRenderer(window, -1, 0)) == NULL) {
		eprintf("failed to create a renderer: %s\n");
		SDL_DestroyWindow(window);
		SDL_Quit();
		return EXIT_FAILURE;
	}
	
	SDL_ShowWindow(window);
	SDL_SetRenderDrawColor(renderer, 0, 0, 0, SDL_ALPHA_OPAQUE);
	SDL_RenderClear(renderer);
	SDL_RenderPresent(renderer);
	
	SDL_AudioSpec desired = {
		.freq = 48000,
		.format = AUDIO_F32SYS,
		.channels = 2,
		.samples = 8096,
		.callback = audio_callback,
		.userdata = &userdata,
	};
	SDL_AudioDeviceID audio = SDL_OpenAudioDevice(NULL, 0, &desired, NULL, 0);
	
	SDL_PauseAudioDevice(audio, 0);
	while (true) {
		SDL_Event evt;
		while (SDL_PollEvent(&evt)) {
			switch (evt.type) {
				case SDL_QUIT:
					goto done;

				case SDL_WINDOWEVENT:
					SDL_RenderClear(renderer);
					SDL_RenderPresent(renderer);
					break;
				
				case SDL_KEYDOWN:
					// ^Q
					if (evt.key.keysym.sym == SDLK_q && evt.key.keysym.mod & KMOD_CTRL) {
						goto done;
					}
					// ESC
					if (evt.key.keysym.sym == SDLK_ESCAPE) {
						goto done;
					}
					break;
				
				case SDL_DROPFILE:
					puts(evt.drop.file);
					struct blob file = load_file(evt.drop.file);
					if (file.data == NULL) {
						perror(evt.drop.file);
						SDL_free(evt.drop.file);
						break;
					}
					SDL_free(evt.drop.file);
					struct userdata newuser = {.callback = NULL, .freefunc = NULL};
					if (module_openmpt(&file, audio, &newuser)) {
						// error
					} else {
						SDL_LockAudioDevice(audio);
						if (userdata.freefunc != NULL) {
							userdata.freefunc(userdata.user);
						}
						userdata = newuser;
						SDL_UnlockAudioDevice(audio);
					}
					free(file.data);
					break;
				
				default:
			}
		}
	}
	done:
	if (userdata.freefunc != NULL) {
		SDL_LockAudioDevice(audio);
		userdata.freefunc(userdata.user);
		SDL_UnlockAudioDevice(audio);
	}
	
	SDL_DestroyRenderer(renderer);
	
	SDL_DestroyWindow(window);
	
	SDL_CloseAudioDevice(audio);
	
	SDL_Quit();
	
	return EXIT_SUCCESS;
}

struct blob load_file(char const *const name) {
	const size_t START_SIZE = 1;
	FILE *file = fopen(name, "rb");
	if (file == NULL) {
		return (struct blob) {.data = NULL};
	}
	void *data = malloc(START_SIZE);
	size_t allocated = START_SIZE;
	size_t used = 0;
	while (1) {
		size_t read = fread(data + used, 1, allocated - used, file);
		if (read != allocated - used) {
			used += read;
			break;
		}
		used += read;
		allocated *= 2;
		void *const newdata = realloc(data, allocated);
		if (newdata == NULL) {
			goto realloc_error;
		}
		data = newdata;
	}
	void *const newdata = realloc(data, used);
	if (newdata == NULL && used != 0) {
		goto realloc_error;
	}
	fclose(file);
	return (struct blob) {.data = newdata, .size = used};
	
	realloc_error:
	free(data);
	fclose(file);
	return (struct blob) {.data = NULL};
}