From de29b11a88dbdd3af0824e59b51528b91ee73c54 Mon Sep 17 00:00:00 2001 From: ptitSeb Date: Thu, 30 Nov 2017 22:49:38 +0100 Subject: First commit. Version works on Linux (keyboard only, not configurable) --- src/psp/audio.c | 87 +++++++++++++++++++ src/psp/audio.h | 24 ++++++ src/psp/graphics.c | 249 +++++++++++++++++++++++++++++++++++++++++++++++++++++ src/psp/graphics.h | 66 ++++++++++++++ src/psp/input.c | 75 ++++++++++++++++ src/psp/input.h | 21 +++++ src/psp/system.c | 48 +++++++++++ src/psp/system.h | 16 ++++ 8 files changed, 586 insertions(+) create mode 100644 src/psp/audio.c create mode 100644 src/psp/audio.h create mode 100644 src/psp/graphics.c create mode 100644 src/psp/graphics.h create mode 100644 src/psp/input.c create mode 100644 src/psp/input.h create mode 100644 src/psp/system.c create mode 100644 src/psp/system.h (limited to 'src/psp') diff --git a/src/psp/audio.c b/src/psp/audio.c new file mode 100644 index 0000000..7b4242f --- /dev/null +++ b/src/psp/audio.c @@ -0,0 +1,87 @@ +#include "audio.h" +#include "../game.h" + +void PHL_AudioInit() +{ + oslInitAudio(); +} + +void PHL_AudioClose() +{ + oslDeinitAudio(); +} + +//Each system can use a custom music file format +OSL_SOUND* PHL_LoadMusic(char* fname, int loop) +{ + char stream = 0; + + OSL_SOUND* result = NULL; + + char fullPath[40]; + strcpy(fullPath, fname); + strcat(fullPath, ".bgm"); + + FILE* file; + + if ( (file = fopen(fullPath, "r")) ) { + fclose(file); + result = oslLoadSoundFile(fullPath, stream); + oslSetSoundLoop(result, loop); + } + + return result; +} + +OSL_SOUND* PHL_LoadSound(char* fname) +{ + char stream = 0; + + OSL_SOUND* result = NULL; + + char fullPath[40]; + strcpy(fullPath, fname); + + FILE* file; + + if ( (file = fopen(fullPath, "r")) ) { + fclose(file); + result = oslLoadSoundFile(fullPath, stream); + oslSetSoundLoop(result, 0); + } + + return result; +} + + +void PHL_PlayMusic(OSL_SOUND* snd) +{ + //Music always plays on the first channel + PHL_PlaySound(snd, 0); +} + +void PHL_PlaySound(OSL_SOUND* snd, int channel) +{ + if (snd) { + oslPlaySound(snd, channel); + } +} + +void PHL_StopMusic() +{ + PHL_StopSound(bgmMusic, 0); +} + +void PHL_StopSound(OSL_SOUND* snd, int channel) +{ + if (snd) { + oslStopSound(snd); + } +} + +void PHL_FreeSound(OSL_SOUND* snd) +{ + if (snd) { + oslDeleteSound(snd); + } +} \ No newline at end of file diff --git a/src/psp/audio.h b/src/psp/audio.h new file mode 100644 index 0000000..c77c983 --- /dev/null +++ b/src/psp/audio.h @@ -0,0 +1,24 @@ +#ifndef PSPAUDIO_H +#define PSPAUDIO_H + +#include + +#define PHL_Music OSL_SOUND +#define PHL_Sound OSL_SOUND + +void PHL_AudioInit(); +void PHL_AudioClose(); + +OSL_SOUND* PHL_LoadMusic(char* fname, int loop); //Same as PHL_LoadSound, but expects a file name without extension +OSL_SOUND* PHL_LoadSound(char* fname); + +void PHL_PlayMusic(OSL_SOUND* snd); +void PHL_PlaySound(OSL_SOUND* snd, int channel); + +void PHL_StopMusic(); +void PHL_StopSound(OSL_SOUND* snd, int channel); + +#define PHL_FreeMusic PHL_FreeSound +void PHL_FreeSound(OSL_SOUND* snd); + +#endif \ No newline at end of file diff --git a/src/psp/graphics.c b/src/psp/graphics.c new file mode 100644 index 0000000..469c12e --- /dev/null +++ b/src/psp/graphics.c @@ -0,0 +1,249 @@ +#include "graphics.h" +#include "../game.h" +#include "../qda.h" + +int screenOffsetX = 80; +int screenOffsetY = 16; +int fullscreen = 0; +int blurFilter = 0; + +//One time graphics setup +void PHL_GraphicsInit() +{ + oslInit(0); + oslInitGfx(OSL_PF_8888, 1); + + screen = oslCreateImage(320, 240, OSL_IN_VRAM, OSL_PF_5650); + + PHL_StartDrawing(); +} + +void PHL_GraphicsExit() +{ + oslEndGfx(); + oslDeleteImage(screen); + + //closeQDA(); +} + +void PHL_StartDrawing() +{ + oslStartDrawing(); + oslSetDrawBuffer(screen); +} + +void PHL_EndDrawing() +{ + oslSetDrawBuffer(OSL_DEFAULT_BUFFER); + + oslSetBilinearFilter(blurFilter); + oslDrawImageXY(screen, screenOffsetX, screenOffsetY); + oslSetBilinearFilter(0); + + oslEndDrawing(); + oslEndFrame(); + oslSyncFrame(); +} + +void PHL_ForceScreenUpdate() +{ + oslSetDrawBuffer(OSL_DEFAULT_BUFFER); + oslDrawImageXY(screen, screenOffsetX, screenOffsetY); + oslEndDrawing(); + oslAudioVSync(); + oslSyncFrame(); + + oslStartDrawing(); + oslSetDrawBuffer(screen); +} + +void PHL_SetDrawbuffer(OSL_IMAGE* surf) +{ + //unused in psp version +} + +void PHL_ResetDrawbuffer() +{ + //unused in psp version +} + +void PHL_SetColorKey(OSL_IMAGE* surf, int r, int g, int b) +{ + +} + +OSL_IMAGE* PHL_NewSurface(int w, int h) +{ + OSL_IMAGE* surf = oslCreateImage(w, h, OSL_IN_RAM, OSL_PF_8BIT); + + return surf; +} + +void PHL_FreeSurface(OSL_IMAGE* surf) +{ + if (surf) { + oslDeleteImage(surf); + surf = NULL; + } +} + +OSL_IMAGE* PHL_LoadBMP(int index) +{ + OSL_IMAGE* result = NULL; + + FILE* f; + if ( (f = fopen("bmp.qda", "rb")) ) { + //Load QDA file data + unsigned char* QDAFile = (unsigned char*)malloc(headers[index].size); + fseek(f, headers[index].offset, SEEK_SET); + fread(QDAFile, headers[index].size, 1, f); + + OSL_COLOR palette[20][18]; + + //Read data from header + unsigned short w, h; + + memcpy(&w, &QDAFile[18], 2); + memcpy(&h, &QDAFile[22], 2); + + result = oslCreateImage(w, h, OSL_IN_RAM, OSL_PF_8888); + oslUnswizzleImage(result); + oslClearImage(result, RGBA(255, 255, 255, 255)); + + //Load Palette + int dx, dy; + int count = 0; + for (dx = 0; dx < 20; dx++) { + for (dy = 0; dy < 16; dy++) { + palette[dx][dy] = RGB(QDAFile[54 + count + 2], QDAFile[54 + count + 1], QDAFile[54 + count]); + count += 4; + } + } + + OSL_COLOR alphaKey = palette[0][0]; + + //Darkness special case + if (index == 27) { + alphaKey = RGB(0, 0, 0); + } + + //Edit surface pixels + oslLockImage(result); + for (dx = w; dx > 0; dx--) { + for (dy = h; dy >= 0; dy--) { + int pix = w - dx + w * dy; + + int px = QDAFile[1078 + pix] / 16; + int py = QDAFile[1078 + pix] % 16; + + if (palette[px][py] == alphaKey) { + oslSetImagePixel(result, w - dx, h - dy - 1, RGBA(0, 0, 0, 0)); + }else{ + oslSetImagePixel(result, w - dx, h - dy - 1, palette[px][py]); + } + } + } + oslUnlockImage(result); + + oslSwizzleImage(result); + + free(QDAFile); + } + + fclose(f); + + return result; +} + +void PHL_DrawRect(int x, int y, int w, int h, OSL_COLOR col) +{ + oslDrawFillRect(x/2, y/2, (x + w) / 2, (y + h) / 2, col); +} + +void PHL_DrawSurface(double x, double y, OSL_IMAGE* surface) +{ + oslDrawImageXY(surface, (int)(x/2), (int)(y/2)); +} + +void PHL_DrawSurfacePart(double x, double y, int cropx, int cropy, int cropw, int croph, OSL_IMAGE* surface) +{ + //Consider earthquake + if (quakeTimer > 0) { + int val = quakeTimer % 4; + if (val == 0) { + y -= 2; + }else if (val == 2) { + y += 2; + } + } + + OSL_IMAGE *crop = oslCreateImageTileSize(surface, cropx/2, cropy/2, cropw/2, croph/2); + oslDrawImageXY(crop, (int)(x/2), (int)(y/2)); + oslDeleteImage(crop); +} + +void PHL_DrawBackground(PHL_Background back, PHL_Background fore) +{ + int xx, yy; + + for (yy = 0; yy < 12; yy++) + { + for (xx = 0; xx < 16; xx++) + { + //Draw Background tiles + PHL_DrawSurfacePart(xx * 40, yy * 40, back.tileX[xx][yy] * 40, back.tileY[xx][yy] * 40, 40, 40, images[imgTiles]); + + //Only draw foreground tile if not a blank tile + if (fore.tileX[xx][yy] != 0 || fore.tileY[xx][yy] != 0) { + PHL_DrawSurfacePart(xx * 40, yy * 40, fore.tileX[xx][yy] * 40, fore.tileY[xx][yy] * 40, 40, 40, images[imgTiles]); + } + } + } +} + +void PHL_UpdateBackground(PHL_Background back, PHL_Background fore) +{ + //Unused in psp version +} + +void setBlur(int blur) +{ + blurFilter = blur; +} + +int getBlur() +{ + return blurFilter; +} + +void setScreenSize(int size) +{ + int screenW = 320; + int screenH = 240; + + if (size == 1) { + screenW = 368; + screenH = 272; + } + if (size == 2) { + screenW = 480; + screenH = 272; + } + + screenOffsetX = (480 - screenW) / 2; + screenOffsetY = (272 - screenH) / 2; + + screen->stretchX = screenW; + screen->stretchY = screenH; +} + +int getScreenSize() +{ + if (screen->stretchX == 368) { + return 1; + } + if (screen->stretchX == 480) { + return 2; + } + return 0; +} \ No newline at end of file diff --git a/src/psp/graphics.h b/src/psp/graphics.h new file mode 100644 index 0000000..4dec54e --- /dev/null +++ b/src/psp/graphics.h @@ -0,0 +1,66 @@ +#ifndef GRAPHICS_H +#define GRAPHICS_H + +#include +#include + +#define PHL_Surface OSL_IMAGE + +#define PHL_RGB OSL_COLOR +#define PHL_NewRGB RGB + +typedef struct { + int tileX[16][12]; + int tileY[16][12]; +} PHL_Background; + +/* +typedef struct { + unsigned int r, g, b; +} PHL_RGB; +*/ +/* +typedef struct { + OSL_IMAGE* pxdata; + int width; + int height; + PHL_RGB colorKey; +} PHL_Surface; +*/ +OSL_IMAGE* screen; + +void PHL_GraphicsInit(); +void PHL_GraphicsExit(); + +void PHL_StartDrawing(); +void PHL_EndDrawing(); + +void PHL_ForceScreenUpdate(); + +void PHL_SetDrawbuffer(OSL_IMAGE* surf); +void PHL_ResetDrawbuffer(); + +//PHL_RGB PHL_NewRGB(int r, int g, int b); +void PHL_SetColorKey(OSL_IMAGE* surf, int r, int g, int b); + +OSL_IMAGE* PHL_NewSurface(int w, int h); +void PHL_FreeSurface(OSL_IMAGE* surf); + +//PHL_Surface PHL_LoadBMP(char* fname); +OSL_IMAGE* PHL_LoadBMP(int index); + +void PHL_DrawRect(int x, int y, int w, int h, OSL_COLOR col); + +void PHL_DrawSurface(double x, double y, OSL_IMAGE* surface); +void PHL_DrawSurfacePart(double x, double y, int cropx, int cropy, int cropw, int croph, OSL_IMAGE* surface); + +void PHL_DrawBackground(PHL_Background back, PHL_Background fore); +void PHL_UpdateBackground(PHL_Background back, PHL_Background fore); + +void setBlur(int blur); +void setScreenSize(int size); + +int getBlur(); +int getScreenSize(); + +#endif \ No newline at end of file diff --git a/src/psp/input.c b/src/psp/input.c new file mode 100644 index 0000000..f54abfa --- /dev/null +++ b/src/psp/input.c @@ -0,0 +1,75 @@ +#include "input.h" +#include "../text.h" + +int deadZone = 64; + +void updateKey(Button* btn, int state); + +void PHL_ScanInput() +{ + oslReadKeys(); + + int pUp = 0, pDown = 0, pLeft = 0, pRight = 0; + + if (osl_keys->held.up || osl_keys->analogY < -deadZone) { pUp = 1; } + if (osl_keys->held.down || osl_keys->analogY > deadZone) { pDown = 1; } + if (osl_keys->held.left || osl_keys->analogX < -deadZone) { pLeft = 1; } + if (osl_keys->held.right || osl_keys->analogX > deadZone) { pRight = 1; } + + updateKey(&btnUp, pUp); + updateKey(&btnDown, pDown); + updateKey(&btnLeft, pLeft); + updateKey(&btnRight, pRight); + + updateKey(&btnL, osl_keys->held.L); + updateKey(&btnR, osl_keys->held.R); + + updateKey(&btnStart, osl_keys->held.start); + updateKey(&btnSelect, osl_keys->held.select); + + updateKey(&btnFaceRight, osl_keys->held.circle); + updateKey(&btnFaceDown, osl_keys->held.cross); + updateKey(&btnFaceLeft, osl_keys->held.square); + updateKey(&btnFaceUp, osl_keys->held.triangle); + + btnAccept = btnFaceDown; + btnDecline = btnFaceRight; + //Swap buttons for japanese + if (getLanguage() == JAPANESE) { + btnAccept = btnFaceRight; + btnDecline = btnFaceDown; + } + + /* + if (btnFaceUp.pressed == 1) { + FILE* f; + + if ( (f = fopen("debug.txt", "a")) ) { + fprintf(f, "\n%i bytes available", oslGetRamStatus().maxAvailable); + } + + fclose(f); + } + */ +} + +void updateKey(Button* btn, int state) +{ + if (state) { + if (btn->held == 1) { + btn->pressed = 0; + }else{ + btn->pressed = 1; + } + btn->held = 1; + btn->released = 0; + }else{ + if (btn->held == 1) { + btn->released = 1; + }else{ + btn->released = 0; + } + btn->held = 0; + btn->pressed = 0; + } +} \ No newline at end of file diff --git a/src/psp/input.h b/src/psp/input.h new file mode 100644 index 0000000..99adb9d --- /dev/null +++ b/src/psp/input.h @@ -0,0 +1,21 @@ +#ifndef INPUT_H +#define INPUT_H + +#include + +typedef struct { + int pressed, + held, + released; +} Button; + +Button btnUp, btnDown, btnLeft, btnRight; +Button btnFaceUp, btnFaceDown, btnFaceLeft, btnFaceRight; +Button btnL, btnR; +Button btnStart, btnSelect; +Button btnAccept, btnDecline; +int axisX, axisY; + +void PHL_ScanInput(); + +#endif \ No newline at end of file diff --git a/src/psp/system.c b/src/psp/system.c new file mode 100644 index 0000000..b14bf40 --- /dev/null +++ b/src/psp/system.c @@ -0,0 +1,48 @@ +#include "system.h" +#include "../text.h" + +PSP_MODULE_INFO("Hydra Castle Labrynth", 0, 1, 1); +PSP_MAIN_THREAD_ATTR(THREAD_ATTR_USER | THREAD_ATTR_VFPU); +//PSP_HEAP_SIZE_KB(12*1024); +PSP_HEAP_SIZE_KB(-1024); + +int PHL_MainLoop() +{ + if (quitGame == 1) { + return 0; + } + return 1; +} + +void PHL_ConsoleInit() +{ + pspDebugScreenInit(); +} + +void PHL_GameQuit() +{ + quitGame = 1; +} + +void PHL_ErrorScreen(char* message) +{ + /*consoleInit(GFX_TOP, NULL); + + printf(message); + printf("\n\nPress START to close"); + + u32 kDown; + while (PHL_MainLoop()) { + //gfxFlushBuffers(); + //gfxSwapBuffers(); + hidScanInput(); + kDown = hidKeysHeld(); + + if (kDown & KEY_START) { break; } + + gspWaitForVBlank(); + } + */ + + exit(1); +} \ No newline at end of file diff --git a/src/psp/system.h b/src/psp/system.h new file mode 100644 index 0000000..d7adc9b --- /dev/null +++ b/src/psp/system.h @@ -0,0 +1,16 @@ +#ifndef SYSTEM_H +#define SYSTEM_H + +#include +#include +#include "../PHL.h" + +char quitGame; + +int PHL_MainLoop(); +void PHL_ConsoleInit(); +void PHL_GameQuit(); + +void PHL_ErrorScreen(char* message); + +#endif \ No newline at end of file -- cgit v1.2.3