Compare commits
3 Commits
6fb53a6435
...
2ad5241005
| Author | SHA1 | Date | |
|---|---|---|---|
| 2ad5241005 | |||
| 717c5715e9 | |||
| f8f181bd33 |
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
.vscode/*
|
||||
*.exe
|
||||
@@ -24,14 +24,9 @@ bool AppController::Init(void)
|
||||
|
||||
drawManager = new DrawManager(gameInfo->modeGrafic);
|
||||
inputManager = new InputManager(gameInfo);
|
||||
musicManager = new MusicManager();
|
||||
|
||||
if ( !drawManager->Init() ) { return false; }
|
||||
musicManager->Init();
|
||||
|
||||
SDL_WM_SetCaption( APPLICATION_NAME, NULL );
|
||||
|
||||
SDL_ShowCursor(0);
|
||||
music_init();
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -44,43 +39,43 @@ void AppController::Go()
|
||||
switch ( gameInfo->estado ) {
|
||||
case ESTADO_SEQUENCIA:
|
||||
SequenceController *sequenceController;
|
||||
sequenceController = new SequenceController(drawManager, inputManager, musicManager);
|
||||
sequenceController = new SequenceController(drawManager, inputManager);
|
||||
sequenceController->Go(gameInfo);
|
||||
delete sequenceController;
|
||||
break;
|
||||
case ESTADO_MENU:
|
||||
MenuController *menuController;
|
||||
menuController = new MenuController(drawManager, inputManager, musicManager);
|
||||
menuController = new MenuController(drawManager, inputManager);
|
||||
menuController->Go(gameInfo);
|
||||
delete menuController;
|
||||
break;
|
||||
case ESTADO_PASSWORD:
|
||||
PasswordController *passwordController;
|
||||
passwordController = new PasswordController(drawManager, inputManager, musicManager);
|
||||
passwordController = new PasswordController(drawManager, inputManager);
|
||||
passwordController->Go(gameInfo);
|
||||
delete passwordController;
|
||||
break;
|
||||
case ESTADO_PREFASE:
|
||||
PrefaseController *prefaseController;
|
||||
prefaseController = new PrefaseController(drawManager, inputManager, musicManager);
|
||||
prefaseController = new PrefaseController(drawManager, inputManager);
|
||||
prefaseController->Go(gameInfo);
|
||||
delete prefaseController;
|
||||
break;
|
||||
case ESTADO_JUEGO:
|
||||
GameController *gameController;
|
||||
gameController = new GameController(drawManager, inputManager, musicManager);
|
||||
gameController = new GameController(drawManager, inputManager);
|
||||
gameController->Go(gameInfo);
|
||||
delete gameController;
|
||||
break;
|
||||
case ESTADO_POSTFASE:
|
||||
PostfaseController *postfaseController;
|
||||
postfaseController = new PostfaseController(drawManager, inputManager, musicManager);
|
||||
postfaseController = new PostfaseController(drawManager, inputManager);
|
||||
postfaseController->Go(gameInfo);
|
||||
delete postfaseController;
|
||||
break;
|
||||
case ESTADO_MORT:
|
||||
MortController *mortController;
|
||||
mortController = new MortController(drawManager, inputManager, musicManager);
|
||||
mortController = new MortController(drawManager, inputManager);
|
||||
mortController->Go(gameInfo);
|
||||
delete mortController;
|
||||
break;
|
||||
@@ -94,7 +89,6 @@ void AppController::Finalize(void)
|
||||
{
|
||||
delete drawManager;
|
||||
delete inputManager;
|
||||
delete musicManager;
|
||||
delete gameInfo;
|
||||
|
||||
SDL_Quit();
|
||||
|
||||
@@ -19,7 +19,6 @@ private:
|
||||
|
||||
DrawManager *drawManager;
|
||||
InputManager *inputManager;
|
||||
MusicManager *musicManager;
|
||||
GameInfo *gameInfo;
|
||||
|
||||
};
|
||||
|
||||
@@ -816,3 +816,40 @@ void ArounderProcesor::put_pixel( int x, int y, Uint32 pixel )
|
||||
{
|
||||
pixels[ ( y * mapa->w ) + x ] = pixel;
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
#define COLOR 0xFFFFFFFF;
|
||||
|
||||
void vline(const int x, const int y1, const int y2) {
|
||||
if (x < 0 || x > 319) return;
|
||||
const int yy1 = y1 < 0 ? 0 : y1 > 239 ? 239 : y1;
|
||||
const int yy2 = y2 < 0 ? 0 : y2 > 239 ? 239 : y2;
|
||||
for (int y=yy1;y<=yy2;y++) pixels[x+y*pitch] = COLOR;
|
||||
}
|
||||
|
||||
void hline(const int x1, const int x2, const int y) {
|
||||
if (y < 0 || y > 239) return;
|
||||
const int yy = y*pitch;
|
||||
const int xx1 = x1 < 0 ? 0 : x1 > 319 ? 319 : x1;
|
||||
const int xx2 = x2 < 0 ? 0 : x2 > 319 ? 319 : x2;
|
||||
for (int x=xx1;x<=xx2;x++) pixels[x+yy] = COLOR;
|
||||
}
|
||||
|
||||
void circle(const int x, const int y) {
|
||||
hline(x+9, x+9+7, y+0); hline(x+9, x+9+7, y+24);
|
||||
hline(x+7, x+7+11, y+1); hline(x+7, x+7+11, y+23);
|
||||
hline(x+5, x+5+15, y+2); hline(x+5, x+5+15, y+22);
|
||||
hline(x+4, x+4+17, y+3); hline(x+4, x+4+17, y+21);
|
||||
hline(x+3, x+3+19, y+4); hline(x+3, x+3+19, y+20);
|
||||
hline(x+2, x+2+21, y+5); hline(x+2, x+2+21, y+19);
|
||||
hline(x+2, x+2+21, y+6); hline(x+2, x+2+21, y+18);
|
||||
hline(x+1, x+1+23, y+7); hline(x+1, x+1+23, y+17);
|
||||
hline(x+1, x+1+23, y+8); hline(x+1, x+1+23, y+16);
|
||||
hline(x, x+25, y+9); hline(x, x+25, y+15);
|
||||
hline(x, x+25, y+10); hline(x, x+25, y+14);
|
||||
hline(x, x+25, y+11); hline(x, x+25, y+13);
|
||||
hline(x, x+25, y+12);
|
||||
}
|
||||
|
||||
*/
|
||||
@@ -4,38 +4,31 @@
|
||||
#include "SDL2/SDL_image.h"
|
||||
|
||||
|
||||
SDL_Window *sdlWindow = NULL;
|
||||
SDL_Renderer *sdlRenderer = NULL;
|
||||
|
||||
DrawManager::DrawManager(int pMode)
|
||||
{
|
||||
DrawManager::DrawManager(int pMode) {
|
||||
mode = pMode;
|
||||
screen = NULL;
|
||||
screenBig = NULL;
|
||||
}
|
||||
|
||||
DrawManager::~DrawManager(void)
|
||||
{
|
||||
DrawManager::~DrawManager(void) {
|
||||
SDL_FreeSurface(temp);
|
||||
SDL_FreeSurface(black);
|
||||
SDL_FreeSurface(faded);
|
||||
|
||||
SDL_DestroyRenderer(sdlRenderer);
|
||||
SDL_DestroyWindow(sdlWindow);
|
||||
}
|
||||
|
||||
bool DrawManager::Init(void)
|
||||
{
|
||||
switch (mode) {
|
||||
case 1:
|
||||
screenBig = SDL_SetVideoMode( SCREEN_WIDTH<<1, SCREEN_HEIGHT<<1, SCREEN_BPP, SDL_SWSURFACE );
|
||||
if( screenBig == NULL ) { return false; }
|
||||
screen = LoadBitmap("black.gif");
|
||||
break;
|
||||
case 2:
|
||||
screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_FULLSCREEN );
|
||||
if( screen == NULL ) { return false; }
|
||||
break;
|
||||
default:
|
||||
screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE );
|
||||
if( screen == NULL ) { return false; }
|
||||
break;
|
||||
}
|
||||
bool DrawManager::Init(void) {
|
||||
|
||||
sdlWindow = SDL_CreateWindow(APPLICATION_NAME, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, SDL_WINDOW_SHOWN);
|
||||
sdlRenderer = SDL_CreateRenderer(sdlWindow, -1, 0);
|
||||
SDL_RenderSetLogicalSize(sdlRenderer, 320, 240);
|
||||
|
||||
SDL_ShowCursor(0);
|
||||
|
||||
color_blanco = -1;
|
||||
color_rojo = SDL_MapRGB( screen->format, 255, 0, 0 );
|
||||
color_verde = SDL_MapRGB( screen->format, 0, 255, 0 );
|
||||
@@ -50,34 +43,15 @@ bool DrawManager::Init(void)
|
||||
|
||||
bool DrawManager::Flip(void)
|
||||
{
|
||||
if (mode == 1) {
|
||||
for (int x=0; x<320; x++) {
|
||||
for (int y=0; y<200; y++) {
|
||||
put_pixel32(screenBig, x<<1 , y<<1 , get_pixel32(screen, x, y));
|
||||
put_pixel32(screenBig, (x<<1)+1, y<<1 , get_pixel32(screen, x, y));
|
||||
put_pixel32(screenBig, x<<1 , (y<<1)+1, get_pixel32(screen, x, y));
|
||||
put_pixel32(screenBig, (x<<1)+1, (y<<1)+1, get_pixel32(screen, x, y));
|
||||
}
|
||||
}
|
||||
if (SDL_Flip( screenBig ) == -1 ) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
if (SDL_Flip( screen ) == -1 ) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
SDL_RenderPresent(sdlRenderer);
|
||||
return true;
|
||||
}
|
||||
|
||||
SDL_Surface *DrawManager::LoadBitmap(char *bitmapfilename, bool doColorKey)
|
||||
SDL_Surface *DrawManager::LoadBitmap(const char *bitmapfilename, const bool doColorKey)
|
||||
{
|
||||
//Get the bitmap's buffer and size from the resource file
|
||||
int filesize = 0;
|
||||
char *buffer = GetBufferFromResource(bitmapfilename, &filesize);
|
||||
const char *buffer = file_getBufferFromResource(bitmapfilename, filesize);
|
||||
|
||||
//Load the buffer into a surface using RWops
|
||||
SDL_RWops *rw = SDL_RWFromMem(buffer, filesize);
|
||||
@@ -111,11 +85,11 @@ SDL_Surface *DrawManager::LoadBitmap(char *bitmapfilename, bool doColorKey)
|
||||
}
|
||||
|
||||
|
||||
SDL_Surface *DrawManager::LoadMask(char *bitmapfilename)
|
||||
SDL_Surface *DrawManager::LoadMask(const char *bitmapfilename)
|
||||
{
|
||||
//Get the bitmap's buffer and size from the resource file
|
||||
int filesize = 0;
|
||||
char *buffer = GetBufferFromResource(bitmapfilename, &filesize);
|
||||
const char *buffer = file_getBufferFromResource(bitmapfilename, filesize);
|
||||
|
||||
//Load the buffer into a surface using RWops
|
||||
SDL_RWops *rw = SDL_RWFromMem(buffer, filesize);
|
||||
@@ -162,7 +136,7 @@ void DrawManager::Blit( int x, int y, SDL_Surface* source, SDL_Rect* clip )
|
||||
apply_surface(x, y, source, screen, clip);
|
||||
}
|
||||
|
||||
void DrawManager::Print( int x, int y, SDL_Surface* source, int w, int h, char *text, SDL_Surface *dest ) {
|
||||
void DrawManager::Print( int x, int y, SDL_Surface* source, int w, int h, const char *text, SDL_Surface *dest ) {
|
||||
int index = 0;
|
||||
SDL_Rect clip;
|
||||
|
||||
@@ -273,7 +247,7 @@ void DrawManager::DrawFadedBack() {
|
||||
Blit(0,0, faded);
|
||||
}
|
||||
|
||||
SDL_Surface *DrawManager::LoadFont(char *bitmapfilename, Uint32 color)
|
||||
SDL_Surface *DrawManager::LoadFont(const char *bitmapfilename, const Uint32 color)
|
||||
{
|
||||
if (color == 0xFFFFFFFF) {
|
||||
return LoadBitmap(bitmapfilename, true);
|
||||
|
||||
@@ -10,17 +10,17 @@ public:
|
||||
bool Init(void);
|
||||
bool Flip(void);
|
||||
|
||||
SDL_Surface *LoadBitmap(char *bitmapfilename, bool doColorKey = false);
|
||||
SDL_Surface *LoadMask(char *bitmapfilename);
|
||||
SDL_Surface *LoadBitmap(const char *bitmapfilename, const bool doColorKey = false);
|
||||
SDL_Surface *LoadMask(const char *bitmapfilename);
|
||||
void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect* clip = NULL );
|
||||
void Blit( int x, int y, SDL_Surface* source, SDL_Rect* clip = NULL );
|
||||
void Print( int x, int y, SDL_Surface* source, int w, int h, char *text, SDL_Surface* dest = NULL );
|
||||
void Print( int x, int y, SDL_Surface* source, int w, int h, const char *text, SDL_Surface* dest = NULL );
|
||||
void FadeOut();
|
||||
void FadeIn();
|
||||
void FadeOutGray();
|
||||
void FadeInGray();
|
||||
void DrawFadedBack();
|
||||
SDL_Surface *LoadFont(char *bitmapfilename, Uint32 color = 0xFFFFFFFF);
|
||||
SDL_Surface *LoadFont(const char *bitmapfilename, const Uint32 color = 0xFFFFFFFF);
|
||||
|
||||
int mode;
|
||||
|
||||
@@ -30,7 +30,6 @@ public:
|
||||
Uint32 color_azul;
|
||||
private:
|
||||
SDL_Surface *screen;
|
||||
SDL_Surface *screenBig;
|
||||
SDL_Surface *black;
|
||||
SDL_Surface *temp;
|
||||
SDL_Surface *faded;
|
||||
|
||||
@@ -2,13 +2,12 @@
|
||||
#include "const.h"
|
||||
#include "fileManager.h"
|
||||
|
||||
GameController::GameController(DrawManager *p_drawManager, InputManager *p_inputManager, MusicManager *p_musicManager) {
|
||||
GameController::GameController(DrawManager *p_drawManager, InputManager *p_inputManager) {
|
||||
drawManager = p_drawManager;
|
||||
inputManager = p_inputManager;
|
||||
musicManager = p_musicManager;
|
||||
}
|
||||
|
||||
GameController::~GameController(void) {
|
||||
GameController::~GameController() {
|
||||
}
|
||||
|
||||
bool GameController::Init()
|
||||
@@ -33,9 +32,9 @@ bool GameController::Init()
|
||||
arounderSeleccionat = primerArounder;
|
||||
|
||||
if ( ((gameInfo->fase+1) % 5) == 0) {
|
||||
musicManager->Load( "mus6.mp3");
|
||||
music_load( "mus6.mp3");
|
||||
} else {
|
||||
musicManager->Load( "mus4.mp3");
|
||||
music_load( "mus4.mp3");
|
||||
}
|
||||
|
||||
return true;
|
||||
@@ -53,7 +52,7 @@ void GameController::Go(GameInfo *pGameInfo)
|
||||
|
||||
if ( !Init() ) { salir = true; gameInfo->estado = ESTADO_SALIR; }
|
||||
|
||||
musicManager->Play(-1);
|
||||
music_play(-1);
|
||||
drawManager->Blit(0,0, fondo);
|
||||
drawManager->Blit(0,0, mapa);
|
||||
drawManager->FadeIn();
|
||||
@@ -183,7 +182,7 @@ void GameController::Go(GameInfo *pGameInfo)
|
||||
|
||||
}
|
||||
|
||||
musicManager->FadeOut();
|
||||
music_fadeOut();
|
||||
drawManager->FadeOut();
|
||||
|
||||
Finalize();
|
||||
@@ -253,7 +252,7 @@ bool GameController::Pausa() {
|
||||
return salirJuego;
|
||||
}
|
||||
|
||||
bool GameController::Menu(bool bypass) {
|
||||
bool GameController::Menu(const bool bypass) {
|
||||
|
||||
if (!bypass) {
|
||||
PintarEscena();
|
||||
@@ -312,10 +311,10 @@ bool GameController::Menu(bool bypass) {
|
||||
return salirJuego;
|
||||
}
|
||||
|
||||
void GameController::CarregarMapa(int numMapa)
|
||||
void GameController::CarregarMapa(const int numMapa)
|
||||
{
|
||||
int filesize = 0;
|
||||
char *buffer = GetBufferFromResource("MAPES.BAL", &filesize);
|
||||
const char *buffer = file_getBufferFromResource("MAPES.BAL", filesize);
|
||||
|
||||
int punter = numMapa * 212;
|
||||
|
||||
@@ -404,7 +403,7 @@ void GameController::Finalize(void)
|
||||
}
|
||||
}
|
||||
|
||||
char *GameController::formatejar(int numero) {
|
||||
const char *GameController::formatejar(const int numero) {
|
||||
char *resultat;
|
||||
resultat = (char *) malloc(3);
|
||||
|
||||
|
||||
@@ -12,8 +12,8 @@
|
||||
class GameController
|
||||
{
|
||||
public:
|
||||
GameController(DrawManager *p_drawManager, InputManager *p_inputManager, MusicManager *p_musicManager);
|
||||
~GameController(void);
|
||||
GameController(DrawManager *p_drawManager, InputManager *p_inputManager);
|
||||
~GameController();
|
||||
|
||||
bool Init();
|
||||
void Go(GameInfo *pGameInfo);
|
||||
@@ -22,7 +22,6 @@ public:
|
||||
private:
|
||||
DrawManager *drawManager;
|
||||
InputManager *inputManager;
|
||||
MusicManager *musicManager;
|
||||
AiguaProcesor *aiguaProcesor;
|
||||
MarcadorProcesor *marcadorProcesor;
|
||||
|
||||
@@ -51,8 +50,8 @@ private:
|
||||
void PintarEscena();
|
||||
void PintarCursor();
|
||||
bool Pausa();
|
||||
bool Menu(bool bypass = false);
|
||||
void CarregarMapa(int numMapa);
|
||||
char *formatejar(int numero);
|
||||
bool Menu(const bool bypass = false);
|
||||
void CarregarMapa(const int numMapa);
|
||||
const char *formatejar(const int numero);
|
||||
|
||||
};
|
||||
|
||||
@@ -1,10 +1,9 @@
|
||||
#include "MenuController.h"
|
||||
#include "const.h"
|
||||
|
||||
MenuController::MenuController(DrawManager *p_drawManager, InputManager *p_inputManager, MusicManager *p_musicManager) {
|
||||
MenuController::MenuController(DrawManager *p_drawManager, InputManager *p_inputManager) {
|
||||
drawManager = p_drawManager;
|
||||
inputManager = p_inputManager;
|
||||
musicManager = p_musicManager;
|
||||
}
|
||||
|
||||
MenuController::~MenuController(void) {
|
||||
@@ -13,9 +12,9 @@ MenuController::~MenuController(void) {
|
||||
bool MenuController::Init() {
|
||||
fondo = drawManager->LoadBitmap("menuprin.gif");
|
||||
cursor = drawManager->LoadFont("cursor.gif");
|
||||
if (!musicManager->Sonant()) {
|
||||
musicManager->Load( "mus3.mp3");
|
||||
musicManager->Play(-1);
|
||||
if (!music_sonant()) {
|
||||
music_load("mus3.mp3");
|
||||
music_play(-1);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
@@ -66,7 +65,7 @@ void MenuController::Go(GameInfo *gameInfo) {
|
||||
}
|
||||
}
|
||||
}
|
||||
if (gameInfo->estado == ESTADO_SALIR) musicManager->FadeOut();
|
||||
if (gameInfo->estado == ESTADO_SALIR) music_fadeOut();
|
||||
drawManager->FadeOut();
|
||||
}
|
||||
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
class MenuController
|
||||
{
|
||||
public:
|
||||
MenuController(DrawManager *p_drawManager, InputManager *p_inputManager, MusicManager *p_musicManager);
|
||||
MenuController(DrawManager *p_drawManager, InputManager *p_inputManager);
|
||||
~MenuController(void);
|
||||
|
||||
bool Init();
|
||||
@@ -18,7 +18,6 @@ public:
|
||||
private:
|
||||
DrawManager *drawManager;
|
||||
InputManager *inputManager;
|
||||
MusicManager *musicManager;
|
||||
|
||||
SDL_Surface *fondo;
|
||||
SDL_Surface *cursor;
|
||||
|
||||
@@ -1,10 +1,9 @@
|
||||
#include "MortController.h"
|
||||
#include "const.h"
|
||||
|
||||
MortController::MortController(DrawManager *p_drawManager, InputManager *p_inputManager, MusicManager *p_musicManager) {
|
||||
MortController::MortController(DrawManager *p_drawManager, InputManager *p_inputManager) {
|
||||
drawManager = p_drawManager;
|
||||
inputManager = p_inputManager;
|
||||
musicManager = p_musicManager;
|
||||
}
|
||||
|
||||
MortController::~MortController(void) {
|
||||
@@ -13,7 +12,7 @@ MortController::~MortController(void) {
|
||||
bool MortController::Init() {
|
||||
fondo = drawManager->LoadBitmap("mort.gif");
|
||||
cursor = drawManager->LoadFont("cursor.gif");
|
||||
musicManager->Load( "mus5.mp3");
|
||||
music_load( "mus5.mp3");
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -23,7 +22,7 @@ void MortController::Go(GameInfo *gameInfo) {
|
||||
|
||||
if ( !Init() ) { salir = true; gameInfo->estado = ESTADO_SALIR; }
|
||||
|
||||
musicManager->Play(-1);
|
||||
music_play(-1);
|
||||
drawManager->Blit(0,0, fondo);
|
||||
drawManager->FadeIn();
|
||||
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
class MortController
|
||||
{
|
||||
public:
|
||||
MortController(DrawManager *p_drawManager, InputManager *p_inputManager, MusicManager *p_musicManager);
|
||||
MortController(DrawManager *p_drawManager, InputManager *p_inputManager);
|
||||
~MortController(void);
|
||||
|
||||
bool Init();
|
||||
@@ -18,7 +18,6 @@ public:
|
||||
private:
|
||||
DrawManager *drawManager;
|
||||
InputManager *inputManager;
|
||||
MusicManager *musicManager;
|
||||
|
||||
SDL_Surface *fondo;
|
||||
SDL_Surface *cursor;
|
||||
|
||||
@@ -1,52 +1,43 @@
|
||||
#include "MusicManager.h"
|
||||
#include "const.h"
|
||||
#include "jail_audio.h"
|
||||
#include "fileManager.h"
|
||||
|
||||
MusicManager::MusicManager(void) {
|
||||
music = NULL;
|
||||
JA_Music music = NULL; //= JA_LoadMusic("intro2.ogg");
|
||||
|
||||
void music_init() {
|
||||
JA_Init(48000, AUDIO_S16, 2);
|
||||
//return Mix_OpenAudio(MIX_DEFAULT_FREQUENCY, MIX_DEFAULT_FORMAT, 2, 1024);
|
||||
}
|
||||
|
||||
MusicManager::~MusicManager(void) {
|
||||
Mix_FreeMusic(music);
|
||||
Mix_CloseAudio();
|
||||
void music_quit() {
|
||||
if (music != NULL) JA_DeleteMusic(music);
|
||||
}
|
||||
|
||||
bool MusicManager::Init() {
|
||||
return Mix_OpenAudio(MIX_DEFAULT_FREQUENCY, MIX_DEFAULT_FORMAT, 2, 1024);
|
||||
}
|
||||
|
||||
void MusicManager::Load(char *musicfilename)
|
||||
{
|
||||
void music_load(const char *musicfilename) {
|
||||
if (music != NULL) {
|
||||
Mix_HaltMusic();
|
||||
Mix_FreeMusic(music);
|
||||
JA_StopMusic();
|
||||
JA_DeleteMusic(music);
|
||||
}
|
||||
Mix_VolumeMusic(MIX_MAX_VOLUME);
|
||||
//Get the bitmap's buffer and size from the resource file
|
||||
int filesize = 0;
|
||||
char *buffer = GetBufferFromResource(musicfilename, &filesize);
|
||||
const char *buffer = file_getBufferFromResource(musicfilename, filesize);
|
||||
|
||||
//Load the buffer into a surface using RWops
|
||||
SDL_RWops *rw = SDL_RWFromMem(buffer, filesize);
|
||||
//SDL_Surface *temp = SDL_LoadBMP_RW(rw, 1);
|
||||
music = Mix_LoadMUS_RW(rw);
|
||||
|
||||
//Release the bitmap buffer memory
|
||||
music = JA_LoadMusic((Uint8*)buffer, filesize);
|
||||
//free(buffer);
|
||||
}
|
||||
|
||||
void MusicManager::Play(int loops) {
|
||||
Mix_PlayMusic(music, loops);
|
||||
void music_play(const int loops) {
|
||||
JA_PlayMusic(music, loops);
|
||||
}
|
||||
|
||||
void MusicManager::Pause() {
|
||||
Mix_PauseMusic();
|
||||
void music_pause() {
|
||||
JA_PauseMusic();
|
||||
}
|
||||
|
||||
void MusicManager::FadeOut() {
|
||||
Mix_FadeOutMusic(500);
|
||||
void music_fadeOut() {
|
||||
JA_StopMusic();
|
||||
}
|
||||
|
||||
bool MusicManager::Sonant() {
|
||||
return (Mix_PlayingMusic() == 1) && (Mix_FadingMusic() != MIX_FADING_OUT);
|
||||
const bool music_sonant() {
|
||||
return JA_GetMusicState() == JA_MUSIC_PLAYING;
|
||||
}
|
||||
@@ -1,18 +1,9 @@
|
||||
#pragma once
|
||||
#include "SDL2/SDL_mixer.h"
|
||||
|
||||
class MusicManager
|
||||
{
|
||||
public:
|
||||
MusicManager(void);
|
||||
~MusicManager(void);
|
||||
|
||||
bool Init(void);
|
||||
void Load(char *musicfilename);
|
||||
void Play(int loops);
|
||||
void Pause();
|
||||
void FadeOut();
|
||||
bool Sonant();
|
||||
private:
|
||||
Mix_Music *music;
|
||||
};
|
||||
void music_init();
|
||||
void music_quit();
|
||||
void music_load(const char *musicfilename);
|
||||
void music_play(const int loops);
|
||||
void music_pause();
|
||||
void music_fadeOut();
|
||||
const bool music_sonant();
|
||||
|
||||
@@ -2,10 +2,9 @@
|
||||
#include "const.h"
|
||||
#include "fileManager.h"
|
||||
|
||||
PasswordController::PasswordController(DrawManager *p_drawManager, InputManager *p_inputManager, MusicManager *p_musicManager) {
|
||||
PasswordController::PasswordController(DrawManager *p_drawManager, InputManager *p_inputManager) {
|
||||
drawManager = p_drawManager;
|
||||
inputManager = p_inputManager;
|
||||
musicManager = p_musicManager;
|
||||
}
|
||||
|
||||
PasswordController::~PasswordController(void) {
|
||||
@@ -82,7 +81,7 @@ char PasswordController::ObtenerTecla() {
|
||||
|
||||
int PasswordController::ObtenerFaseDePassword(char *password) {
|
||||
int filesize = 0;
|
||||
char *buffer = GetBufferFromResource("offsets.bal", &filesize);
|
||||
const char *buffer = file_getBufferFromResource("offsets.bal", filesize);
|
||||
|
||||
int punter = 0;
|
||||
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
class PasswordController
|
||||
{
|
||||
public:
|
||||
PasswordController(DrawManager *p_drawManager, InputManager *p_inputManager, MusicManager *p_musicManager);
|
||||
PasswordController(DrawManager *p_drawManager, InputManager *p_inputManager);
|
||||
~PasswordController(void);
|
||||
|
||||
bool Init();
|
||||
@@ -21,7 +21,6 @@ private:
|
||||
|
||||
DrawManager *drawManager;
|
||||
InputManager *inputManager;
|
||||
MusicManager *musicManager;
|
||||
|
||||
SDL_Surface *fondo;
|
||||
SDL_Surface *cursor;
|
||||
|
||||
@@ -2,10 +2,9 @@
|
||||
#include "const.h"
|
||||
#include "fileManager.h"
|
||||
|
||||
PostfaseController::PostfaseController(DrawManager *p_drawManager, InputManager *p_inputManager, MusicManager *p_musicManager) {
|
||||
PostfaseController::PostfaseController(DrawManager *p_drawManager, InputManager *p_inputManager) {
|
||||
drawManager = p_drawManager;
|
||||
inputManager = p_inputManager;
|
||||
musicManager = p_musicManager;
|
||||
}
|
||||
|
||||
PostfaseController::~PostfaseController(void) {
|
||||
@@ -25,8 +24,8 @@ void PostfaseController::Go(GameInfo *gameInfo) {
|
||||
gameInfo->estado = ESTADO_SEQUENCIA;
|
||||
return;
|
||||
} else {
|
||||
musicManager->Load( "mus3.mp3");
|
||||
musicManager->Play(-1);
|
||||
music_load( "mus3.mp3");
|
||||
music_play(-1);
|
||||
}
|
||||
|
||||
if (gameInfo->fase % 5 == 0 && gameInfo->fase != 30) {
|
||||
@@ -67,7 +66,7 @@ void PostfaseController::Go(GameInfo *gameInfo) {
|
||||
|
||||
char *PostfaseController::ObtenerPasswordDeFase(int fase) {
|
||||
int filesize = 0;
|
||||
char *buffer = GetBufferFromResource("offsets.bal", &filesize);
|
||||
const char *buffer = file_getBufferFromResource("offsets.bal", filesize);
|
||||
|
||||
int punter = (fase-1)*11;
|
||||
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
class PostfaseController
|
||||
{
|
||||
public:
|
||||
PostfaseController(DrawManager *p_drawManager, InputManager *p_inputManager, MusicManager *p_musicManager);
|
||||
PostfaseController(DrawManager *p_drawManager, InputManager *p_inputManager);
|
||||
~PostfaseController(void);
|
||||
|
||||
bool Init();
|
||||
@@ -21,7 +21,6 @@ private:
|
||||
|
||||
DrawManager *drawManager;
|
||||
InputManager *inputManager;
|
||||
MusicManager *musicManager;
|
||||
|
||||
SDL_Surface *fondo;
|
||||
SDL_Surface *font;
|
||||
|
||||
@@ -2,10 +2,9 @@
|
||||
#include "const.h"
|
||||
#include "fileManager.h"
|
||||
|
||||
PrefaseController::PrefaseController(DrawManager *p_drawManager, InputManager *p_inputManager, MusicManager *p_musicManager) {
|
||||
PrefaseController::PrefaseController(DrawManager *p_drawManager, InputManager *p_inputManager) {
|
||||
drawManager = p_drawManager;
|
||||
inputManager = p_inputManager;
|
||||
musicManager = p_musicManager;
|
||||
}
|
||||
|
||||
PrefaseController::~PrefaseController(void) {
|
||||
@@ -64,7 +63,7 @@ void PrefaseController::Go(GameInfo *gameInfo) {
|
||||
salir = true;
|
||||
}
|
||||
}
|
||||
musicManager->FadeOut();
|
||||
music_fadeOut();
|
||||
drawManager->FadeOut();
|
||||
}
|
||||
|
||||
@@ -74,10 +73,10 @@ void PrefaseController::Finalize(void) {
|
||||
SDL_FreeSurface(font);
|
||||
}
|
||||
|
||||
void PrefaseController::CarregarMapa(int numMapa)
|
||||
void PrefaseController::CarregarMapa(const int numMapa)
|
||||
{
|
||||
int filesize = 0;
|
||||
char *buffer = GetBufferFromResource("MAPES.BAL", &filesize);
|
||||
const char *buffer = file_getBufferFromResource("MAPES.BAL", filesize);
|
||||
|
||||
int punter = numMapa * 212;
|
||||
|
||||
@@ -87,7 +86,7 @@ void PrefaseController::CarregarMapa(int numMapa)
|
||||
aroundersNec = buffer[punter++];
|
||||
}
|
||||
|
||||
char *PrefaseController::formatejar(int numero) {
|
||||
const char *PrefaseController::formatejar(const int numero) {
|
||||
char *resultat;
|
||||
resultat = (char *) malloc(3);
|
||||
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
class PrefaseController
|
||||
{
|
||||
public:
|
||||
PrefaseController(DrawManager *p_drawManager, InputManager *p_inputManager, MusicManager *p_musicManager);
|
||||
PrefaseController(DrawManager *p_drawManager, InputManager *p_inputManager);
|
||||
~PrefaseController(void);
|
||||
|
||||
bool Init();
|
||||
@@ -16,12 +16,11 @@ public:
|
||||
void Finalize(void);
|
||||
|
||||
private:
|
||||
char *formatejar(int numero);
|
||||
void CarregarMapa(int numMapa);
|
||||
const char *formatejar(const int numero);
|
||||
void CarregarMapa(const int numMapa);
|
||||
|
||||
DrawManager *drawManager;
|
||||
InputManager *inputManager;
|
||||
MusicManager *musicManager;
|
||||
|
||||
SDL_Surface *fondo;
|
||||
SDL_Surface *cursor;
|
||||
|
||||
@@ -2,10 +2,9 @@
|
||||
#include "const.h"
|
||||
#include "fileManager.h"
|
||||
|
||||
SequenceController::SequenceController(DrawManager *p_drawManager, InputManager *p_inputManager, MusicManager *p_musicManager) {
|
||||
SequenceController::SequenceController(DrawManager *p_drawManager, InputManager *p_inputManager) {
|
||||
drawManager = p_drawManager;
|
||||
inputManager = p_inputManager;
|
||||
musicManager = p_musicManager;
|
||||
}
|
||||
|
||||
SequenceController::~SequenceController(void) {
|
||||
@@ -18,7 +17,7 @@ bool SequenceController::Init() {
|
||||
void SequenceController::Go(GameInfo *pGameInfo) {
|
||||
bool salir = false;
|
||||
int pantalla = 0;
|
||||
char *file;
|
||||
const char *file;
|
||||
int filesize;
|
||||
|
||||
gameInfo = pGameInfo;
|
||||
@@ -26,28 +25,28 @@ void SequenceController::Go(GameInfo *pGameInfo) {
|
||||
if (gameInfo->fase % 5 == 0 || gameInfo->fase < 0) {
|
||||
switch (gameInfo->fase) {
|
||||
case -1:
|
||||
file = GetBufferFromResource("seqIN.seq", &filesize);
|
||||
file = file_getBufferFromResource("seqIN.seq", filesize);
|
||||
break;
|
||||
case 0:
|
||||
file = GetBufferFromResource("seq00.seq", &filesize);
|
||||
file = file_getBufferFromResource("seq00.seq", filesize);
|
||||
break;
|
||||
case 5:
|
||||
file = GetBufferFromResource("seq05.seq", &filesize);
|
||||
file = file_getBufferFromResource("seq05.seq", filesize);
|
||||
break;
|
||||
case 10:
|
||||
file = GetBufferFromResource("seq10.seq", &filesize);
|
||||
file = file_getBufferFromResource("seq10.seq", filesize);
|
||||
break;
|
||||
case 15:
|
||||
file = GetBufferFromResource("seq15.seq", &filesize);
|
||||
file = file_getBufferFromResource("seq15.seq", filesize);
|
||||
break;
|
||||
case 20:
|
||||
file = GetBufferFromResource("seq20.seq", &filesize);
|
||||
file = file_getBufferFromResource("seq20.seq", filesize);
|
||||
break;
|
||||
case 25:
|
||||
file = GetBufferFromResource("seq25.seq", &filesize);
|
||||
file = file_getBufferFromResource("seq25.seq", filesize);
|
||||
break;
|
||||
case 30:
|
||||
file = GetBufferFromResource("seq30.seq", &filesize);
|
||||
file = file_getBufferFromResource("seq30.seq", filesize);
|
||||
break;
|
||||
}
|
||||
ProcesarSecuencia(file);
|
||||
@@ -61,7 +60,7 @@ void SequenceController::Go(GameInfo *pGameInfo) {
|
||||
}
|
||||
}
|
||||
|
||||
void SequenceController::ProcesarSecuencia(char *file) {
|
||||
void SequenceController::ProcesarSecuencia(const char *file) {
|
||||
char numDiapositives = (char)file[0];
|
||||
int punter = 1;
|
||||
char *filename;
|
||||
@@ -201,12 +200,12 @@ void SequenceController::FadeOut() {
|
||||
|
||||
void SequenceController::FadeOutWithMusic() {
|
||||
drawManager->FadeOut();
|
||||
musicManager->FadeOut();
|
||||
music_fadeOut();
|
||||
}
|
||||
|
||||
void SequenceController::Musica(char *archivo, int loop) {
|
||||
musicManager->Load(archivo);
|
||||
musicManager->Play(loop);
|
||||
music_load(archivo);
|
||||
music_play(loop);
|
||||
}
|
||||
|
||||
void SequenceController::Finalize(void) {
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
class SequenceController
|
||||
{
|
||||
public:
|
||||
SequenceController(DrawManager *p_drawManager, InputManager *p_inputManager, MusicManager *p_musicManager);
|
||||
SequenceController(DrawManager *p_drawManager, InputManager *p_inputManager);
|
||||
~SequenceController(void);
|
||||
|
||||
bool Init();
|
||||
@@ -16,7 +16,7 @@ public:
|
||||
void Finalize(void);
|
||||
|
||||
private:
|
||||
void ProcesarSecuencia(char *file);
|
||||
void ProcesarSecuencia(const char *file);
|
||||
|
||||
bool Esperar(Uint32 temps);
|
||||
void FadeIn(char *archivo);
|
||||
@@ -28,7 +28,6 @@ private:
|
||||
|
||||
DrawManager *drawManager;
|
||||
InputManager *inputManager;
|
||||
MusicManager *musicManager;
|
||||
GameInfo *gameInfo;
|
||||
|
||||
SDL_Surface *fondo;
|
||||
|
||||
166
fileManager.cpp
166
fileManager.cpp
@@ -1,90 +1,100 @@
|
||||
#include "fileManager.h"
|
||||
#include "stdio.h"
|
||||
#include "stdlib.h"
|
||||
#include "string.h"
|
||||
#include "fcntl.h"
|
||||
#ifdef _WIN32
|
||||
#include "io.h"
|
||||
#else
|
||||
#include "unistd.h"
|
||||
#endif
|
||||
#include <SDL2/SDL.h>
|
||||
#include <fstream>
|
||||
|
||||
char *resourceFileName = "data.jrf";
|
||||
#pragma pack(push,1)
|
||||
|
||||
void setResourceFile(char *p_resourceFileName) {
|
||||
struct DATA_Header {
|
||||
char magic[4];
|
||||
Uint32 num_files;
|
||||
Uint32 index_offset;
|
||||
};
|
||||
|
||||
struct DATA_Info {
|
||||
Uint32 offset;
|
||||
Uint32 length;
|
||||
char name[13];
|
||||
};
|
||||
|
||||
struct DATA_Index {
|
||||
DATA_Info* file_info;
|
||||
};
|
||||
|
||||
struct DATA_File {
|
||||
DATA_Header header;
|
||||
DATA_Index index;
|
||||
};
|
||||
|
||||
#pragma pack(pop)
|
||||
|
||||
static const char *resourceFileName = "data.jrf";
|
||||
static DATA_File *data_file = NULL;
|
||||
|
||||
void file_setResourceFile(const char *p_resourceFileName) {
|
||||
resourceFileName = p_resourceFileName;
|
||||
}
|
||||
|
||||
char *GetBufferFromResource(char *resourcename, int *filesize)
|
||||
{
|
||||
//Try to open the resource file in question
|
||||
int fd = open(resourceFileName, O_RDONLY);
|
||||
if (fd < 0)
|
||||
{
|
||||
perror("Error opening resource file");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
//Make sure we're at the beginning of the file
|
||||
lseek(fd, 0, SEEK_SET);
|
||||
|
||||
//Read the first INT, which will tell us how many files are in this resource
|
||||
int numfiles;
|
||||
int resultat = read(fd, &numfiles, sizeof(int));
|
||||
|
||||
#ifdef _WIN32
|
||||
int final = eof(fd);
|
||||
#endif
|
||||
static void getDataFile() {
|
||||
std::ifstream fd( resourceFileName, std::ios::in | std::ios::binary );
|
||||
|
||||
//Get the pointers to the stored files
|
||||
int *filestart = (int *) malloc(sizeof(int) * numfiles);
|
||||
resultat = read(fd, filestart, sizeof(int) * numfiles);
|
||||
|
||||
//Loop through the files, looking for the file in question
|
||||
int filenamesize;
|
||||
char *buffer;
|
||||
int i;
|
||||
for(i=0;i<numfiles;i++)
|
||||
{
|
||||
int result = 129;
|
||||
char *filename;
|
||||
//Seek to the location
|
||||
lseek(fd, filestart[i], SEEK_SET);
|
||||
//Get the filesize value
|
||||
read(fd, filesize, sizeof(int));
|
||||
//Get the size of the filename string
|
||||
read(fd, &filenamesize, sizeof(int));
|
||||
//Size the buffer and read the filename
|
||||
filename = (char *) malloc(filenamesize + 1);
|
||||
result = read(fd, filename, filenamesize);
|
||||
//Remember to terminate the string properly!
|
||||
filename[filenamesize] = '\0';
|
||||
//Compare to the string we're looking for
|
||||
if (strcmp(filename, resourcename) == 0)
|
||||
{
|
||||
//Get the contents of the file
|
||||
buffer = (char *) malloc(*filesize);
|
||||
read(fd, buffer, *filesize);
|
||||
free(filename);
|
||||
break;
|
||||
}
|
||||
//Free the filename buffer
|
||||
free(filename);
|
||||
}
|
||||
|
||||
//Release memory
|
||||
free(filestart);
|
||||
|
||||
//Close the resource file!
|
||||
close(fd);
|
||||
|
||||
//Did we find the file within the resource that we were looking for?
|
||||
if (buffer == NULL)
|
||||
{
|
||||
printf("Unable to find '%s' in the resource file!\n", resourcename);
|
||||
if( fd.fail() ) {
|
||||
perror("No s'ha pogut obrir l'arxiu de recursos");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
//Return the buffer
|
||||
data_file = (DATA_File*)malloc( sizeof( DATA_File ) );
|
||||
|
||||
fd.read( (char*)&data_file->header, sizeof( DATA_Header ) );
|
||||
|
||||
fd.seekg( data_file->header.index_offset );
|
||||
|
||||
data_file->index.file_info = (DATA_Info*)malloc( data_file->header.num_files * sizeof( DATA_Info ) );
|
||||
|
||||
fd.read( (char*)data_file->index.file_info, data_file->header.num_files * sizeof( DATA_Info ) );
|
||||
|
||||
fd.close();
|
||||
}
|
||||
|
||||
const char *file_setBufferFromResource(const char *resourcename, int& filesize) {
|
||||
|
||||
if( data_file == NULL ) {
|
||||
getDataFile();
|
||||
}
|
||||
|
||||
bool found = false;
|
||||
int count = 0;
|
||||
while( !found && count < data_file->header.num_files ) {
|
||||
found = ( strcmp( resourcename, data_file->index.file_info[count].name ) == 0 );
|
||||
if( !found ) count++;
|
||||
}
|
||||
|
||||
if( !found ) {
|
||||
perror("El recurs no s'ha trobat en l'arxiu de recursos");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
filesize = data_file->index.file_info[count].length;
|
||||
|
||||
std::ifstream fd( resourceFileName, std::ios::in | std::ios::binary );
|
||||
|
||||
if( fd.fail() ) {
|
||||
perror("No s'ha pogut obrir l'arxiu de recursos");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
fd.seekg( data_file->index.file_info[count].offset );
|
||||
|
||||
char* buffer = (char*)malloc( filesize );
|
||||
fd.read( buffer, filesize );
|
||||
|
||||
fd.close();
|
||||
|
||||
return buffer;
|
||||
}
|
||||
|
||||
void file_quit() {
|
||||
if( data_file != NULL ) {
|
||||
free( data_file->index.file_info );
|
||||
free( data_file );
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
void setResourceFile(char *p_resourceFileName);
|
||||
void file_setResourceFile(const char *p_resourceFileName);
|
||||
|
||||
char *GetBufferFromResource(char *resourcename, int *filesize);
|
||||
const char *file_getBufferFromResource(const char *resourcename, int& filesize);
|
||||
|
||||
void file_quit();
|
||||
|
||||
240
jail_audio.cpp
Normal file
240
jail_audio.cpp
Normal file
@@ -0,0 +1,240 @@
|
||||
#include "jail_audio.h"
|
||||
#include "stb_vorbis.c"
|
||||
#include <SDL2/SDL.h>
|
||||
|
||||
#define JA_MAX_SIMULTANEOUS_CHANNELS 5
|
||||
|
||||
struct JA_Sound_t {
|
||||
Uint32 length {0};
|
||||
Uint8* buffer {NULL};
|
||||
};
|
||||
|
||||
struct JA_Channel_t {
|
||||
JA_Sound sound;
|
||||
int pos {0};
|
||||
int times {0};
|
||||
JA_Channel_state state { JA_CHANNEL_FREE };
|
||||
};
|
||||
|
||||
struct JA_Music_t {
|
||||
int samples {0};
|
||||
int pos {0};
|
||||
int times {0};
|
||||
short* output {NULL};
|
||||
JA_Music_state state {JA_MUSIC_INVALID};
|
||||
};
|
||||
|
||||
JA_Music current_music{NULL};
|
||||
JA_Channel_t channels[JA_MAX_SIMULTANEOUS_CHANNELS];
|
||||
|
||||
int JA_freq {48000};
|
||||
SDL_AudioFormat JA_format {AUDIO_S16};
|
||||
Uint8 JA_channels {2};
|
||||
|
||||
void audioCallback(void * userdata, uint8_t * stream, int len) {
|
||||
SDL_memset(stream, 0, len);
|
||||
if (current_music != NULL && current_music->state == JA_MUSIC_PLAYING) {
|
||||
const int size = SDL_min(len, current_music->samples*2-current_music->pos);
|
||||
SDL_memcpy(stream, current_music->output+current_music->pos, size);
|
||||
current_music->pos += size/2;
|
||||
if (size < len) {
|
||||
if (current_music->times != 0) {
|
||||
SDL_memcpy(stream+size, current_music->output, len-size);
|
||||
current_music->pos = (len-size)/2;
|
||||
if (current_music->times > 0) current_music->times--;
|
||||
} else {
|
||||
current_music->pos = 0;
|
||||
current_music->state = JA_MUSIC_STOPPED;
|
||||
}
|
||||
}
|
||||
}
|
||||
// Mixar els channels mi amol
|
||||
for (int i = 0; i < JA_MAX_SIMULTANEOUS_CHANNELS; i++) {
|
||||
if (channels[i].state == JA_CHANNEL_PLAYING) {
|
||||
const int size = SDL_min(len, channels[i].sound->length - channels[i].pos);
|
||||
SDL_MixAudioFormat(stream, channels[i].sound->buffer + channels[i].pos, AUDIO_S16, size, 64);
|
||||
channels[i].pos += size;
|
||||
if (size < len) {
|
||||
if (channels[i].times != 0) {
|
||||
SDL_MixAudioFormat(stream + size, channels[i].sound->buffer, AUDIO_S16, len-size, 64);
|
||||
channels[i].pos = len-size;
|
||||
if (channels[i].times > 0) channels[i].times--;
|
||||
} else {
|
||||
JA_StopChannel(i);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void JA_Init(const int freq, const SDL_AudioFormat format, const int channels) {
|
||||
JA_freq = freq;
|
||||
JA_format = format;
|
||||
JA_channels = channels;
|
||||
SDL_AudioSpec audioSpec{JA_freq, JA_format, JA_channels, 0, 1024, 0, 0, audioCallback, NULL};
|
||||
SDL_AudioDeviceID sdlAudioDevice = SDL_OpenAudioDevice(NULL, 0, &audioSpec, NULL, 0);
|
||||
SDL_PauseAudioDevice(sdlAudioDevice, 0);
|
||||
}
|
||||
|
||||
JA_Music JA_LoadMusic(const char* filename) {
|
||||
int chan, samplerate;
|
||||
JA_Music music = new JA_Music_t();
|
||||
music->samples = stb_vorbis_decode_filename(filename, &chan, &samplerate, &music->output);
|
||||
|
||||
SDL_AudioCVT cvt;
|
||||
SDL_BuildAudioCVT(&cvt, AUDIO_S16, chan, samplerate, JA_format, JA_channels, JA_freq);
|
||||
cvt.len = music->samples * chan * 2;
|
||||
cvt.buf = (Uint8 *) SDL_malloc(cvt.len * cvt.len_mult);
|
||||
SDL_memcpy(cvt.buf, music->output, cvt.len);
|
||||
SDL_ConvertAudio(&cvt);
|
||||
free(music->output);
|
||||
music->output = (short*)cvt.buf;
|
||||
|
||||
music->pos = 0;
|
||||
music->state = JA_MUSIC_STOPPED;
|
||||
|
||||
return music;
|
||||
}
|
||||
|
||||
JA_Music JA_LoadMusic(const Uint8* mem, int len) {
|
||||
int chan, samplerate;
|
||||
JA_Music music = new JA_Music_t();
|
||||
music->samples = stb_vorbis_decode_memory(mem, len, &chan, &samplerate, &music->output);
|
||||
|
||||
SDL_AudioCVT cvt;
|
||||
SDL_BuildAudioCVT(&cvt, AUDIO_S16, chan, samplerate, JA_format, JA_channels, JA_freq);
|
||||
cvt.len = music->samples * chan * 2;
|
||||
cvt.buf = (Uint8 *) SDL_malloc(cvt.len * cvt.len_mult);
|
||||
SDL_memcpy(cvt.buf, music->output, cvt.len);
|
||||
SDL_ConvertAudio(&cvt);
|
||||
free(music->output);
|
||||
music->output = (short*)cvt.buf;
|
||||
|
||||
music->pos = 0;
|
||||
music->state = JA_MUSIC_STOPPED;
|
||||
|
||||
return music;
|
||||
}
|
||||
|
||||
void JA_PlayMusic(JA_Music music, const int loop) {
|
||||
if (current_music != NULL) {
|
||||
current_music->pos = 0;
|
||||
current_music->state = JA_MUSIC_STOPPED;
|
||||
}
|
||||
current_music = music;
|
||||
current_music->pos = 0;
|
||||
current_music->state = JA_MUSIC_PLAYING;
|
||||
current_music->times = loop;
|
||||
}
|
||||
|
||||
void JA_PauseMusic() {
|
||||
if (current_music == NULL || current_music->state == JA_MUSIC_INVALID) return;
|
||||
current_music->state = JA_MUSIC_PAUSED;
|
||||
}
|
||||
|
||||
void JA_ResumeMusic() {
|
||||
if (current_music == NULL || current_music->state == JA_MUSIC_INVALID) return;
|
||||
current_music->state = JA_MUSIC_PLAYING;
|
||||
}
|
||||
|
||||
void JA_StopMusic() {
|
||||
if (current_music == NULL || current_music->state == JA_MUSIC_INVALID) return;
|
||||
current_music->pos = 0;
|
||||
current_music->state = JA_MUSIC_STOPPED;
|
||||
}
|
||||
|
||||
JA_Music_state JA_GetMusicState() {
|
||||
if (current_music == NULL) return JA_MUSIC_INVALID;
|
||||
return current_music->state;
|
||||
}
|
||||
|
||||
void JA_DeleteMusic(JA_Music music) {
|
||||
if (current_music == music) current_music = NULL;
|
||||
SDL_free(music->output);
|
||||
delete music;
|
||||
}
|
||||
|
||||
JA_Sound JA_NewSound(Uint8* buffer, Uint32 length) {
|
||||
JA_Sound sound = new JA_Sound_t();
|
||||
sound->buffer = buffer;
|
||||
sound->length = length;
|
||||
return sound;
|
||||
}
|
||||
|
||||
JA_Sound JA_LoadSound(const char* filename) {
|
||||
JA_Sound sound = new JA_Sound_t();
|
||||
SDL_AudioSpec wavSpec;
|
||||
SDL_LoadWAV(filename, &wavSpec, &sound->buffer, &sound->length);
|
||||
|
||||
SDL_AudioCVT cvt;
|
||||
SDL_BuildAudioCVT(&cvt, wavSpec.format, wavSpec.channels, wavSpec.freq, JA_format, JA_channels, JA_freq);
|
||||
cvt.len = sound->length;
|
||||
cvt.buf = (Uint8 *) SDL_malloc(cvt.len * cvt.len_mult);
|
||||
SDL_memcpy(cvt.buf, sound->buffer, sound->length);
|
||||
SDL_ConvertAudio(&cvt);
|
||||
SDL_FreeWAV(sound->buffer);
|
||||
sound->buffer = cvt.buf;
|
||||
sound->length = cvt.len_cvt;
|
||||
|
||||
return sound;
|
||||
}
|
||||
|
||||
int JA_PlaySound(JA_Sound sound, const int loop) {
|
||||
int channel = 0;
|
||||
while (channel < JA_MAX_SIMULTANEOUS_CHANNELS && channels[channel].state != JA_CHANNEL_FREE) { channel++; }
|
||||
if (channel == JA_MAX_SIMULTANEOUS_CHANNELS) channel = 0;
|
||||
|
||||
channels[channel].sound = sound;
|
||||
channels[channel].times = loop;
|
||||
channels[channel].pos = 0;
|
||||
channels[channel].state = JA_CHANNEL_PLAYING;
|
||||
return channel;
|
||||
}
|
||||
|
||||
void JA_DeleteSound(JA_Sound sound) {
|
||||
for (int i = 0; i < JA_MAX_SIMULTANEOUS_CHANNELS; i++) {
|
||||
if (channels[i].sound == sound) JA_StopChannel(i);
|
||||
}
|
||||
SDL_free(sound->buffer);
|
||||
delete sound;
|
||||
}
|
||||
|
||||
void JA_PauseChannel(const int channel) {
|
||||
if (channel == -1) {
|
||||
for (int i = 0; i < JA_MAX_SIMULTANEOUS_CHANNELS; i++) {
|
||||
if (channels[i].state == JA_CHANNEL_PLAYING) channels[i].state = JA_CHANNEL_PAUSED;
|
||||
}
|
||||
} else if (channel >= 0 && channel < JA_MAX_SIMULTANEOUS_CHANNELS) {
|
||||
if (channels[channel].state == JA_CHANNEL_PLAYING) channels[channel].state = JA_CHANNEL_PAUSED;
|
||||
}
|
||||
}
|
||||
|
||||
void JA_ResumeChannel(const int channel) {
|
||||
if (channel == -1) {
|
||||
for (int i = 0; i < JA_MAX_SIMULTANEOUS_CHANNELS; i++) {
|
||||
if (channels[i].state == JA_CHANNEL_PAUSED) channels[i].state = JA_CHANNEL_PLAYING;
|
||||
}
|
||||
} else if (channel >= 0 && channel < JA_MAX_SIMULTANEOUS_CHANNELS) {
|
||||
if (channels[channel].state == JA_CHANNEL_PAUSED) channels[channel].state = JA_CHANNEL_PLAYING;
|
||||
}
|
||||
}
|
||||
|
||||
void JA_StopChannel(const int channel) {
|
||||
if (channel == -1) {
|
||||
for (int i = 0; i < JA_MAX_SIMULTANEOUS_CHANNELS; i++) {
|
||||
channels[i].state = JA_CHANNEL_FREE;
|
||||
channels[i].pos = 0;
|
||||
channels[i].sound = NULL;
|
||||
}
|
||||
} else if (channel >= 0 && channel < JA_MAX_SIMULTANEOUS_CHANNELS) {
|
||||
channels[channel].state = JA_CHANNEL_FREE;
|
||||
channels[channel].pos = 0;
|
||||
channels[channel].sound = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
JA_Channel_state JA_GetChannelState(const int channel) {
|
||||
if (channel < 0 || channel >= JA_MAX_SIMULTANEOUS_CHANNELS) return JA_CHANNEL_INVALID;
|
||||
return channels[channel].state;
|
||||
}
|
||||
|
||||
28
jail_audio.h
Normal file
28
jail_audio.h
Normal file
@@ -0,0 +1,28 @@
|
||||
#pragma once
|
||||
#include <SDL2/SDL.h>
|
||||
|
||||
enum JA_Channel_state { JA_CHANNEL_INVALID, JA_CHANNEL_FREE, JA_CHANNEL_PLAYING, JA_CHANNEL_PAUSED };
|
||||
enum JA_Music_state { JA_MUSIC_INVALID, JA_MUSIC_PLAYING, JA_MUSIC_PAUSED, JA_MUSIC_STOPPED };
|
||||
|
||||
typedef struct JA_Sound_t *JA_Sound;
|
||||
typedef struct JA_Music_t *JA_Music;
|
||||
|
||||
void JA_Init(const int freq, const SDL_AudioFormat format, const int channels);
|
||||
|
||||
JA_Music JA_LoadMusic(const char* filename);
|
||||
JA_Music JA_LoadMusic(const Uint8* mem, int len);
|
||||
void JA_PlayMusic(JA_Music music, const int loop = -1);
|
||||
void JA_PauseMusic();
|
||||
void JA_ResumeMusic();
|
||||
void JA_StopMusic();
|
||||
JA_Music_state JA_GetMusicState();
|
||||
void JA_DeleteMusic(JA_Music music);
|
||||
|
||||
JA_Sound JA_NewSound(Uint8* buffer, Uint32 length);
|
||||
JA_Sound JA_LoadSound(const char* filename);
|
||||
int JA_PlaySound(JA_Sound sound, const int loop = 0);
|
||||
void JA_PauseChannel(const int channel);
|
||||
void JA_ResumeChannel(const int channel);
|
||||
void JA_StopChannel(const int channel);
|
||||
JA_Channel_state JA_GetChannelState(const int channel);
|
||||
void JA_DeleteSound(JA_Sound sound);
|
||||
6
main.cpp
6
main.cpp
@@ -5,14 +5,14 @@
|
||||
int main( int argc, char* args[] )
|
||||
{
|
||||
int modeGrafic = MODE_ZOOMX2;
|
||||
char *porDefecto = "data.jrf";
|
||||
setResourceFile(porDefecto);
|
||||
const char *porDefecto = "data.jrf";
|
||||
file_setResourceFile(porDefecto);
|
||||
|
||||
if (argc > 1) {
|
||||
for (int i=1;i<argc;i++) {
|
||||
if (!strcmp(args[i], "normal")) modeGrafic = MODE_NORMAL;
|
||||
if (!strcmp(args[i], "fullscreen")) modeGrafic = MODE_FULLSCREEN;
|
||||
if (args[i][4] == '.') setResourceFile(args[i]);
|
||||
if (args[i][4] == '.') file_setResourceFile(args[i]);
|
||||
}
|
||||
}
|
||||
AppController *appController;
|
||||
|
||||
5565
stb_vorbis.c
Normal file
5565
stb_vorbis.c
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user