joeylib/audioplayer/player.c

456 lines
9 KiB
C

/*
* JoeyLib Based Example Audio Player
* Copyright (C) 2020 Scott Duensing <scott@kangaroopunch.com>
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
*/
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <assert.h>
#define JOEY_MAIN
#include "joey.h"
#ifdef JOEY_IIGS
segment "audioapp";
#endif
#ifdef DMALLOC
#define DMALLOC_FUNC_CHECK
#include "dmalloc.h"
#endif
void help(jlSurfaceT source, jlStnT *stencil, jint16 sx, jint16 sy, jint16 tx, jint16 ty) {
int mo; // Mask offset
int x;
int y;
byte b; // Mask bit index
printf("\n");
mo = ((sy & 0x1f) * 8 * 40) + (sx & 0x3f); // This is in tiles
printf("Offset: %04x sx: %02x sy: %02x\n", mo, sx, sy);
for (y=0; y<8; y++) {
b = 7;
printf("Byte: %02x ", stencil->pixels[mo]);
for (x=0; x<4; x++) {
if ((stencil->pixels[mo] & (1 << b--)) != 0) {
printf("X");
} else {
printf(".");
}
if ((stencil->pixels[mo] & (1 << b--)) != 0) {
printf("X");
} else {
printf(".");
}
}
mo += 40;
printf("\n");
}
jlDrawBlit8x8a(source, stencil, sx, sy, tx, ty);
}
// Font hacking!
__attribute__((__format__ (__printf__, 5, 0)))
void fontPrint(jlImgT *font, jlStnT *stencil, jint16 cx, jint16 cy, const char *what, ...) {
jint16 x;
jint16 y;
jint16 tx;
jint16 ty;
jint16 counter;
char msg[40]; // Very short messages (screen width). Be careful!
va_list va;
va_start(va, what);
vsprintf(msg, what, va);
va_end(va);
tx = cx * 8;
ty = cy * 8;
for (counter=0; counter<(int)strlen(msg); counter++) {
x = (msg[counter] % 40) * 8;
y = (msg[counter] / 40) * 8;
if (stencil) {
#ifdef JOEY_PC
help(jlImgSurfaceGet(font), stencil, x, y, tx, ty);
#else
jlDrawBlit8x8a(jlImgSurfaceGet(font), stencil, x, y, tx, ty);
#endif
} else {
jlDrawBlit8x8(jlImgSurfaceGet(font), x, y, tx, ty);
}
tx += 8;
}
}
void blitTest(void) {
jlImgT *font = NULL;
jlStnT *stencil = NULL;
jint16 y;
jint16 x;
if (!jlImgLoad(font, "font")) jlUtilDie("Unable to load font.img!");
if (!jlStnLoad(stencil, "font")) jlUtilDie("Unable to load font.stn!");
jlImgDisplay(font);
jlDisplayPresent();
jlKeyWaitForAny();
jlDrawColorSet(1);
jlDrawClear();
jlPaletteSet(15, 15, 15, 15);
fontPrint(font, NULL, 1, 2, "%s", "Blitting without stencil buffer.");
fontPrint(font, stencil, 1, 4, "%s", "Blitting with stencil buffer.");
jlDisplayPresent();
jlKeyWaitForAny();
y = 91;
while (!jlKeyPressed()) {
for (x=0; x<319-8; x++) {
jlDrawBlit8x8(jlImgSurfaceGet(font), 8, 0, x, y);
fontPrint(font, NULL, 1, 6, "Drawing at %d x %d ", x, y);
jlDisplayPresent();
jlDrawBlit8x8(jlImgSurfaceGet(font), 0, 0, x, y);
jlUtilSleep(1);
if (jlKeyPressed()) {
break;
}
}
}
jlKeyRead();
jlStnFree(stencil);
jlImgFree(font);
}
void exerciseAPI(void) {
jlImgT *screen = NULL;
jlDrawColorSet(0);
jlDrawClear();
jlDrawColorSet(15);
jlDrawPixelSet(10, 10);
assert(jlDrawPixelGet(10, 10) == 15);
// Should be able to grab the screen twice without allocation issues.
jlImgCreate(screen);
jlImgCreate(screen);
jlImgFree(screen);
}
void grid(void) {
int i;
jlDrawColorSet(0);
jlDrawClear();
jlDrawColorSet(15);
for (i=0; i<320; i+=8) {
jlDrawLine(i, 0, i, 199);
}
for (i=0; i<200; i+=8) {
jlDrawLine(0, i, 319, i);
}
while (!jlKeyPressed()) {
jlDisplayPresent();
}
jlKeyRead();
}
void lineTest(void) {
jlImgT *kanga = NULL;
jlImgT *font = NULL;
jint16 y;
jint16 phase = 0;
jint16 x2 = 0;
jint16 y2 = 0;
jint16 ox = 0;
jint16 oy = 0;
jint16 op = 0;
jint16 color = 15;
jint16 nextColor = 1;
char what[32];
if (!jlImgLoad(kanga, "kanga")) jlUtilDie("Unable to load kanga.img!");
if (!jlImgLoad(font, "font")) jlUtilDie("Unable to load font.img!");
jlImgDisplay(kanga);
jlDrawColorSet(1);
jlDrawBox(0, 0, 319, 199);
//jlSoundMusicPlay("music");
jlPaletteSet(15, 15, 15, 15);
strcpy(what, "Left to Right");
while (!jlKeyPressed()) {
y = 17;
fontPrint(font, NULL, 1, y++, "Drawing %s ", what);
jlDrawColorSet((byte)color);
if (phase < 2) {
jlDrawLine(x2, y2, 319-x2, 199-y2);
} else {
jlDrawLine(319-x2, 199-y2, x2, y2);
}
ox = x2;
oy = y2;
op = phase;
switch (phase) {
// Left, Y incrementing
case 0:
y2++;
if (y2 == 199) {
strcpy(what, "Bottom to Top");
phase = 1;
}
break;
// Bottom, X incrementing
case 1:
x2++;
if (x2 == 319) {
strcpy(what, "Right to Left");
phase = 2;
}
break;
// Right, Y decrementing
case 2:
y2--;
if (y2 == 0) {
strcpy(what, "Top to Bottom");
phase = 3;
}
break;
// Top, X decrementing
case 3:
x2--;
if (x2 == 0) {
strcpy(what, "Left to Right");
phase = 0;
}
break;
}
jlDisplayPresent();
jlDrawColorSet((byte)0);
if (op < 2) {
jlDrawLine(ox, oy, 319-ox, 199-oy);
} else {
jlDrawLine(319-ox, 199-oy, ox, oy);
}
color = nextColor;
nextColor++;
if (nextColor > 15) {
nextColor = 1;
}
}
jlKeyRead();
jlImgFree(font);
jlImgFree(kanga);
}
void musicTest(void) {
jlImgT *kanga = NULL;
jlImgT *font = NULL;
if (!jlImgLoad(kanga, "kanga")) jlUtilDie("Unable to load kanga.img!");
if (!jlImgLoad(font, "font")) jlUtilDie("Unable to load font.img!");
jlImgDisplay(kanga);
jlDisplayPresent();
jlSoundModPlay("music");
while (!jlKeyPressed()) {
fontPrint(font, NULL, 1, 1, "%dx%d %d %d ", jlGameGetAxis(0), jlGameGetAxis(1), jlGameGetButton(0), jlGameGetButton(1));
jlDisplayPresent();
}
jlKeyRead();
jlSoundModStop();
jlImgFree(font);
jlImgFree(kanga);
}
void showStencil(void) {
jlStnT *stencil = NULL;
jint16 y;
jint16 x;
jint16 count;
juint16 index;
byte bit;
if (!jlStnLoad(stencil, "font")) jlUtilDie("Unable to load font.stn!");
jlDrawColorSet(0);
jlDrawClear();
jlDrawColorSet(15);
// Draw stencil to screen linerally
index = -1;
count = 0;
for (y=0; y<200; y++) {
for (x=0; x<320; x++) {
count--;
if (count < 0) {
count = 7;
index++;
bit = stencil->pixels[index];
}
if (bit & (1 << count)) {
jlDrawPixelSet(x, y);
}
}
jlDisplayPresent();
}
jlKeyWaitForAny();
jlDrawColorSet(0);
jlDrawClear();
jlDrawColorSet(15);
// Draw stencil by pixel location - this fails on the IIgs
for (y=0; y<200; y++) {
for (x=0; x<320; x++) {
index = (y * 320 + x);
count = 7 - (index % 8);
index /= 8;
bit = stencil->pixels[index];
if (bit & (1 << count)) {
jlDrawPixelSet(x, y);
}
}
jlDisplayPresent();
}
jlKeyWaitForAny();
}
void stencilTest(void) {
jlImgT *kangaI = NULL;
jlImgT *biffI = NULL;
jlStnT *biffS = NULL;
jint16 y;
jint16 x;
jint16 i;
jint16 j;
if (!jlImgLoad(kangaI, "kanga")) jlUtilDie("Unable to load kanga.img!");
if (!jlImgLoad(biffI, "biff")) jlUtilDie("Unable to load biff.img!");
if (!jlStnLoad(biffS, "biff")) jlUtilDie("Unable to load biff.stn!");
jlImgDisplay(kangaI);
y = 50;
while (!jlKeyPressed()) {
// Draw Biff & grab background
for (x=0; x<319-32; x++) {
for (i=0; i<3; i++) {
for (j=0; j<4; j++) {
jlDrawSurfaceSet(jlImgSurfaceGet(biffI));
jlDrawBlit8x8(JOEY_DISPLAY, x + (j * 8), y + (i * 8), j * 8 + 32, i * 8 + 32);
jlDrawSurfaceSet(JOEY_DISPLAY);
jlDrawBlit8x8a(jlImgSurfaceGet(biffI), biffS, j * 8, i * 8, x + (j * 8), y + (i * 8));
}
}
jlDisplayPresent();
// Erase Biff
for (i=0; i<3; i++) {
for (j=0; j<4; j++) {
jlDrawBlit8x8(jlImgSurfaceGet(biffI), j * 8 + 32, i * 8 + 32, x + (j * 8), y + (i * 8));
}
}
// Check for early quit
if (jlKeyPressed()) {
break;
}
}
}
jlKeyRead();
jlStnFree(biffS);
jlImgFree(biffI);
jlImgFree(kangaI);
}
void timerTest(void) {
jlImgT *font = NULL;
if (!jlImgLoad(font, "font")) jlUtilDie("Unable to load font.img!");
jlDrawColorSet(0);
jlDrawClear();
while (!jlKeyPressed()) {
fontPrint(font, NULL, 1, 1, "Timer: %d ", jlUtilTimer());
jlDisplayPresent();
}
jlKeyRead();
jlImgFree(font);
}
int main(void) {
jlUtilStartup("JoeyLib Audio");
//blitTest();
//exerciseAPI();
//grid();
//lineTest();
//musicTest();
//showStencil();
stencilTest();
//timerTest();
jlUtilShutdown();
}