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

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

#include <libopenmpt/libopenmpt.h>

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

static const int WINDOW_WIDTH = 160, WINDOW_HEIGHT = 90;

struct blob {
	void *data;
	size_t size;
};

SDL_Window *window = NULL;

struct blob load_file(char const *const name);

struct userdata {
	SDL_AudioCallback callback;
	void *user;
} 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)) == NULL) {
		eprintf("failed to create a window: %s\n");
		SDL_Quit();
		return EXIT_FAILURE;
	}
	
	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_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);
					//SDL_LockAudioDevice(audio);
					//openmpt_module *mod = openmpt_module_create_from_memory2(file.data, file.size, &libopenmpt_example_logfunc, NULL, &libopenmpt_example_errfunc, NULL, &mod_err, &mod_err_str, NULL);
					//SDL_UnlockAudioDevice(audio);
					free(file.data);
					break;
				
				default:
			}
		}
	}
	done:
	
	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) {
		goto realloc_error;
	}
	fclose(file);
	return (struct blob) {.data = newdata, .size = used};
	
	realloc_error:
	free(data);
	fclose(file);
	return (struct blob) {.data = NULL};
}