summaryrefslogtreecommitdiff
path: root/apps/codecs/dumb/examples/dumbplay.c
diff options
context:
space:
mode:
Diffstat (limited to 'apps/codecs/dumb/examples/dumbplay.c')
-rw-r--r--apps/codecs/dumb/examples/dumbplay.c238
1 files changed, 238 insertions, 0 deletions
diff --git a/apps/codecs/dumb/examples/dumbplay.c b/apps/codecs/dumb/examples/dumbplay.c
new file mode 100644
index 0000000000..6421cfdf34
--- /dev/null
+++ b/apps/codecs/dumb/examples/dumbplay.c
@@ -0,0 +1,238 @@
1/* _______ ____ __ ___ ___
2 * \ _ \ \ / \ / \ \ / / ' ' '
3 * | | \ \ | | || | \/ | . .
4 * | | | | | | || ||\ /| |
5 * | | | | | | || || \/ | | ' ' '
6 * | | | | | | || || | | . .
7 * | |_/ / \ \__// || | |
8 * /_______/ynamic \____/niversal /__\ /____\usic /| . . ibliotheque
9 * / \
10 * / . \
11 * dumbplay.c - Not-so-simple program to play / / \ \
12 * music. It used to be simpler! | < / \_
13 * | \/ /\ /
14 * By entheh. \_ / > /
15 * | \ / /
16 * IMPORTANT NOTE: This file is not very friendly. | ' /
17 * I strongly recommend AGAINST using it as a \__/
18 * reference for writing your own code. If you would
19 * like to write a program that uses DUMB, or add DUMB to an existing
20 * project, please use docs/howto.txt. It will help you a lot more than this
21 * file can. (If you have difficulty reading documentation, you are lacking
22 * an important coding skill, and now is as good a time as any to learn.)
23 */
24
25#include <stdlib.h>
26#include <allegro.h>
27
28#ifndef ALLEGRO_DOS
29#include <string.h>
30#endif
31
32/* Note that your own programs should use <aldumb.h> not "aldumb.h". <> tells
33 * the compiler to look in the compiler's default header directory, which is
34 * where DUMB should be installed before you use it (make install does this).
35 * Use "" when it is your own header file. This example uses "" because DUMB
36 * might not have been installed yet when the makefile builds it.
37 */
38#include "aldumb.h"
39
40
41
42#ifndef ALLEGRO_DOS
43static volatile int closed = 0;
44static void closehook(void) { closed = 1; }
45#else
46#define closed 0
47#endif
48
49#ifdef ALLEGRO_WINDOWS
50#define GFX_DUMB_MODE GFX_GDI
51#include <winalleg.h>
52#define YIELD() Sleep(1)
53#else
54#define GFX_DUMB_MODE GFX_AUTODETECT_WINDOWED
55#ifdef ALLEGRO_UNIX
56#include <sys/time.h>
57static void YIELD(void)
58{
59 struct timeval tv;
60 tv.tv_sec = 0;
61 tv.tv_usec = 1;
62 select(0, NULL, NULL, NULL, &tv);
63}
64#else
65#define YIELD() yield_timeslice()
66#endif
67#endif
68
69
70
71#ifdef ALLEGRO_DOS
72static int loop_callback(void *data)
73{
74 (void)data;
75 printf("Music has looped.\n");
76 return 0;
77}
78
79static int xm_speed_zero_callback(void *data)
80{
81 (void)data;
82 printf("Music has stopped.\n");
83 return 0;
84}
85#else
86static int gfx_half_width;
87
88static int loop_callback(void *data)
89{
90 (void)data;
91 if (gfx_half_width) {
92 acquire_screen();
93 textout_centre(screen, font, "Music has looped.", gfx_half_width, 36, 10);
94 release_screen();
95 }
96 return 0;
97}
98
99static int xm_speed_zero_callback(void *data)
100{
101 (void)data;
102 if (gfx_half_width) {
103 text_mode(0); /* In case this is overwriting "Music has looped." */
104 acquire_screen();
105 textout_centre(screen, font, "Music has stopped.", gfx_half_width, 36, 10);
106 release_screen();
107 }
108 return 0;
109}
110#endif
111
112
113
114static void usage(const char *exename)
115{
116 allegro_message(
117#ifdef ALLEGRO_WINDOWS
118 "Usage:\n"
119 " At the command line: %s file\n"
120 " In Windows Explorer: drag a file on to this program's icon.\n"
121#else
122 "Usage: %s file\n"
123#endif
124 "This will play the music file specified.\n"
125 "File formats supported: IT XM S3M MOD.\n"
126 "You can control playback quality by editing dumb.ini.\n", exename);
127
128 exit(1);
129}
130
131
132
133int main(int argc, const char *const *argv) /* I'm const-crazy! */
134{
135 DUH *duh;
136 AL_DUH_PLAYER *dp;
137
138 if (allegro_init())
139 return 1;
140
141 if (argc != 2)
142 usage(argv[0]);
143
144 set_config_file("dumb.ini");
145
146 if (install_keyboard()) {
147 allegro_message("Failed to initialise keyboard driver!\n");
148 return 1;
149 }
150
151 set_volume_per_voice(0);
152
153 if (install_sound(DIGI_AUTODETECT, MIDI_NONE, NULL)) {
154 allegro_message("Failed to initialise sound driver!\n%s\n", allegro_error);
155 return 1;
156 }
157
158 atexit(&dumb_exit);
159
160 dumb_register_packfiles();
161
162 duh = dumb_load_it(argv[1]);
163 if (!duh) {
164 duh = dumb_load_xm(argv[1]);
165 if (!duh) {
166 duh = dumb_load_s3m(argv[1]);
167 if (!duh) {
168 duh = dumb_load_mod(argv[1]);
169 if (!duh) {
170 allegro_message("Failed to load %s!\n", argv[1]);
171 return 1;
172 }
173 }
174 }
175 }
176
177 dumb_resampling_quality = get_config_int("sound", "dumb_resampling_quality", 4);
178 dumb_it_max_to_mix = get_config_int("sound", "dumb_it_max_to_mix", 128);
179
180#ifndef ALLEGRO_DOS
181 {
182 const char *fn = get_filename(argv[1]);
183 gfx_half_width = strlen(fn);
184 if (gfx_half_width < 22) gfx_half_width = 22;
185 gfx_half_width = (gfx_half_width + 2) * 4;
186
187 /* set_window_title() is not const-correct (yet). */
188 set_window_title((char *)"DUMB Music Player");
189
190 if (set_gfx_mode(GFX_DUMB_MODE, gfx_half_width*2, 80, 0, 0) == 0) {
191 acquire_screen();
192 textout_centre(screen, font, fn, gfx_half_width, 20, 14);
193 textout_centre(screen, font, "Press any key to exit.", gfx_half_width, 52, 11);
194 release_screen();
195 } else
196 gfx_half_width = 0;
197 }
198
199#if ALLEGRO_VERSION*10000 + ALLEGRO_SUB_VERSION*100 + ALLEGRO_WIP_VERSION >= 40105
200 set_close_button_callback(&closehook);
201#else
202 set_window_close_hook(&closehook);
203#endif
204
205#endif
206
207 set_display_switch_mode(SWITCH_BACKGROUND);
208
209 dp = al_start_duh(duh, 2, 0, 1.0f,
210 get_config_int("sound", "buffer_size", 4096),
211 get_config_int("sound", "sound_freq", 44100));
212
213 {
214 DUH_SIGRENDERER *sr = al_duh_get_sigrenderer(dp);
215 DUMB_IT_SIGRENDERER *itsr = duh_get_it_sigrenderer(sr);
216 dumb_it_set_loop_callback(itsr, &loop_callback, NULL);
217 dumb_it_set_xm_speed_zero_callback(itsr, &xm_speed_zero_callback, NULL);
218 }
219
220 for (;;) {
221 if (keypressed()) {
222 readkey();
223 break;
224 }
225
226 if (al_poll_duh(dp) || closed)
227 break;
228
229 YIELD();
230 }
231
232 al_stop_duh(dp);
233
234 unload_duh(duh);
235
236 return 0;
237}
238END_OF_MAIN();