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/wii/audio.c | 98 ++++++++++++++++++ src/wii/audio.h | 35 +++++++ src/wii/graphics.c | 290 +++++++++++++++++++++++++++++++++++++++++++++++++++++ src/wii/graphics.h | 61 +++++++++++ src/wii/input.c | 162 ++++++++++++++++++++++++++++++ src/wii/input.h | 20 ++++ src/wii/system.c | 47 +++++++++ src/wii/system.h | 12 +++ 8 files changed, 725 insertions(+) create mode 100644 src/wii/audio.c create mode 100644 src/wii/audio.h create mode 100644 src/wii/graphics.c create mode 100644 src/wii/graphics.h create mode 100644 src/wii/input.c create mode 100644 src/wii/input.h create mode 100644 src/wii/system.c create mode 100644 src/wii/system.h (limited to 'src/wii') diff --git a/src/wii/audio.c b/src/wii/audio.c new file mode 100644 index 0000000..d60242b --- /dev/null +++ b/src/wii/audio.c @@ -0,0 +1,98 @@ +#include "audio.h" + +void PHL_AudioInit() +{ + //oslInitAudio(); + if (Mix_OpenAudio(MIX_DEFAULT_FREQUENCY, MIX_DEFAULT_FORMAT, 1, 4096)) + { + fprintf(stderr, "SDL_Mixer Error: %s\n", Mix_GetError()); + exit(EXIT_FAILURE); + } +} + +void PHL_AudioClose() +{ + Mix_CloseAudio(); +} + +//Each system can use a custom music file extension +PHL_Music PHL_LoadMusic(char* fname, int loop) +{ + PHL_Music result; + result.loop = loop; + + char fullPath[40]; + strcpy(fullPath, fname); + strcat(fullPath, ".ogg"); + + FILE* file; + if ((file = fopen(fullPath, "r"))) { + fclose(file); + + result.data = Mix_LoadMUS(fullPath); + result.used = 1; + }else{ + result.used = 0; + } + + return result; +} + +//PHL_Sound PHL_LoadSound(char* fname, int loop, int stream, int channel) +PHL_Sound PHL_LoadSound(char* fname) +{ + PHL_Sound result; + + FILE* file; + if ((file = fopen(fname, "r"))) { + fclose(file); + + result.data = Mix_LoadWAV(fname); + result.used = 1; + }else{ + result.used = 0; + } + + return result; +} + +void PHL_PlayMusic(PHL_Music snd) +{ + if (snd.used == 1) { + int loop = 1; + if (snd.loop == 1) { + loop = -1; + } + Mix_PlayMusic(snd.data, loop); + } +} + +//void PHL_PlaySound(PHL_Sound snd) +void PHL_PlaySound(PHL_Sound snd, int channel) +{ + if (snd.used == 1) { + Mix_PlayChannel(channel, snd.data, 0); + } +} + +void PHL_StopMusic() +{ + Mix_HaltMusic(); +} + +void PHL_StopSound(PHL_Sound snd, int channel) +{ + +} + +void PHL_FreeMusic(PHL_Music snd) +{ + Mix_FreeMusic(snd.data); + snd.used = 0; +} + +void PHL_FreeSound(PHL_Sound snd) +{ + Mix_FreeChunk(snd.data); + snd.used = 0; +} \ No newline at end of file diff --git a/src/wii/audio.h b/src/wii/audio.h new file mode 100644 index 0000000..ec32470 --- /dev/null +++ b/src/wii/audio.h @@ -0,0 +1,35 @@ +#ifndef WIIAUDIO_H +#define WIIAUDIO_H + +#include + +typedef struct { + Mix_Music* data; + + int loop; + int used; +} PHL_Music; + +typedef struct { + Mix_Chunk* data; + + int loop; + int used; +} PHL_Sound; + +void PHL_AudioInit(); +void PHL_AudioClose(); + +PHL_Music PHL_LoadMusic(char* fname, int loop); //Same as PHL_LoadSound, but expects a file name without extension +PHL_Sound PHL_LoadSound(char* fname); + +void PHL_PlayMusic(PHL_Music snd); +void PHL_PlaySound(PHL_Sound snd, int channel); + +void PHL_StopMusic(); +void PHL_StopSound(PHL_Sound snd, int channel); + +void PHL_FreeMusic(PHL_Music snd); +void PHL_FreeSound(PHL_Sound snd); + +#endif \ No newline at end of file diff --git a/src/wii/graphics.c b/src/wii/graphics.c new file mode 100644 index 0000000..bd6e4cf --- /dev/null +++ b/src/wii/graphics.c @@ -0,0 +1,290 @@ +//Wii graphics.c +#include "graphics.h" +#include "../PHL.h" +#include "../game.h" +#include "../qda.h" +#include + +//One time graphics setup +void PHL_GraphicsInit() +{ + if ( SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0) { + fprintf(stderr, "Error"); + SDL_Delay(5000); + exit(EXIT_FAILURE); + } + + SDL_ShowCursor(SDL_DISABLE); + + surfScreen = SDL_SetVideoMode(640, 480, 16, SDL_HWSURFACE|SDL_DOUBLEBUF); + backBuffer = PHL_NewSurface(640, 480); + db.fb = surfScreen; +} + +void PHL_GraphicsExit() +{ + SDL_FreeSurface(surfScreen); + SDL_Quit(); + //closeQDA(); +} + +void PHL_StartDrawing() +{ + /*int i; + for (i = 0; i < 10; i++) { + xfb[i] = 0xff; + }*/ +} + +void PHL_EndDrawing() +{ + SDL_Flip(surfScreen); + //frameLimit(); + //VIDEO_Flush(); + //VIDEO_WaitVSync(); + //frameLimit(); +} + +void PHL_ForceScreenUpdate() +{ + +} + +void PHL_SetDrawbuffer(PHL_Surface surf) +{ + db.fb = surf.surf; +} + +void PHL_ResetDrawbuffer() +{ + db.fb = surfScreen; +} + +PHL_RGB PHL_NewRGB(int r, int g, int b) { + PHL_RGB col; + col.r = (unsigned int)r; + col.g = (unsigned int)g; + col.b = (unsigned int)b; + return col; +} + +void PHL_SetColorKey(PHL_Surface surf, int r, int g, int b) +{ + Uint32 colorKey = SDL_MapRGB(surf.surf->format, r, g, b); + SDL_SetColorKey(surf.surf, SDL_SRCCOLORKEY, colorKey); +} + +PHL_Surface PHL_NewSurface(int w, int h) +{ + PHL_Surface surf; + surf.surf = SDL_CreateRGBSurface(0, w, h, 32, 0, 0, 0, 0); + return surf; +} + +void PHL_FreeSurface(PHL_Surface surface) +{ + SDL_FreeSurface(surface.surf); +} + +PHL_Surface PHL_LoadBMP(int index) +{ + PHL_Surface surf; + + FILE* f; + if ( (f = fopen("bmp.qda", "rb")) ) { + unsigned char* QDAFile = (unsigned char*)malloc(headers[index].size); + fseek(f, headers[index].offset, SEEK_SET); + fread(QDAFile, 1, headers[index].size, f); + fclose(f); + + FILE* temp; + if ( (temp = fopen("temp.bmp", "wb")) ) { + fwrite(QDAFile, sizeof(unsigned char*), sizeof(QDAFile), temp); + } + fclose(temp); + + PHL_RGB palette[20][18]; + + unsigned short w, h; + + //Read data from header + memcpy(&w, &QDAFile[18], 2); + memcpy(&h, &QDAFile[22], 2); + + surf = PHL_NewSurface(w * 2, h * 2); + //surf = PHL_NewSurface(200, 200); + + //Load Palette + int dx, dy; + int count = 0; + for (dx = 0; dx < 20; dx++) { + for (dy = 0; dy < 16; dy++) { + palette[dx][dy].b = QDAFile[54 + count]; + palette[dx][dy].g = QDAFile[54 + count + 1]; + palette[dx][dy].r = QDAFile[54 + count + 2]; + count += 4; + } + } + + for (dx = w; dx > 0; dx--) { + for (dy = 0; dy < h; dy++) { + + int pix = w - dx + w * dy; + int px = QDAFile[1078 + pix] / 16; + int py = QDAFile[1078 + pix] % 16; + + PHL_RGB c = palette[px][py]; + + //PHL_DrawRect(dx * 2, dy * 2, 2, 2, c); + SDL_Rect rect = {dx * 2, dy * 2, 2, 2}; + SDL_FillRect(surf.surf, &rect, SDL_MapRGB(surf.surf->format, c.r, c.g, c.b)); + } + } + free(QDAFile); + } + + fclose(f); + + return surf; +} + +/* +PHL_Surface PHL_LoadBMP(char* fname) +{ + SDL_Surface* temp; + SDL_Surface* stretchX; + SDL_Surface* final; + + temp = SDL_LoadBMP(fname); + int width = temp->w; + int height = temp->h; + + stretchX = SDL_CreateRGBSurface(0, width * 2, height, 32, 0, 0, 0, 0); + + SDL_Rect crop, offset; + offset.y = 0; + crop.y = 0; + crop.w = 1; + crop.h - height; + + int i; + for (i = 0; i < width; i++) + { + crop.x = i; + offset.x = i * 2; + + SDL_BlitSurface(temp, &crop, stretchX, &offset); + offset.x += 1; + SDL_BlitSurface(temp, &crop, stretchX, &offset); + } + + final = SDL_CreateRGBSurface(0, width * 2, height * 2, 32, 0, 0, 0, 0); + offset.x = 0; offset.y = 0; + crop.x = 0; crop.y = 0; + crop.w = width * 2; + crop.h = 1; + + for (i = 0; i < height; i++) + { + crop.y = i; + offset.y = i * 2; + + SDL_BlitSurface(stretchX, &crop, final, &offset); + offset.y += 1; + SDL_BlitSurface(stretchX, &crop, final, &offset); + } + + //Cleanup + SDL_FreeSurface(temp); + SDL_FreeSurface(stretchX); + + PHL_Surface result; + result.surf = SDL_DisplayFormat(final); + + return result; +} +*/ + +void PHL_DrawRect(int x, int y, int w, int h, PHL_RGB col) { + SDL_Rect rect = {x, y, w, h}; + + SDL_FillRect(db.fb, &rect, SDL_MapRGB(db.fb->format, col.r, col.g, col.b)); +} + +void PHL_DrawSurface(double x, double y, PHL_Surface surface) +{ + if (quakeTimer > 0) { + int val = quakeTimer % 4; + if (val == 0) { + y -= 1; + }else if (val == 2) { + y += 1; + } + } + + SDL_Rect offset; + offset.x = x; + offset.y = y; + + SDL_BlitSurface(surface.surf, NULL, db.fb, &offset); +} + +void PHL_DrawSurfacePart(double x, double y, int cropx, int cropy, int cropw, int croph, PHL_Surface surface) +{ + if (quakeTimer > 0) { + int val = quakeTimer % 4; + if (val == 0) { + y -= 1; + }else if (val == 2) { + y += 1; + } + } + + SDL_Rect crop, offset; + crop.x = cropx; + crop.y = cropy; + crop.w = cropw; + crop.h = croph; + + offset.x = x; + offset.y = y; + + SDL_BlitSurface(surface.surf, &crop, db.fb, &offset); +} + +void PHL_DrawBackground(PHL_Background back, PHL_Background fore) +{ + PHL_DrawSurface(0, 0, backBuffer); +} + +void PHL_UpdateBackground(PHL_Background back, PHL_Background fore) +{ + PHL_SetDrawbuffer(backBuffer); + + 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]); + } + } + } + + PHL_ResetDrawbuffer(); +} + +/*void frameLimit() +{ + currentTime = SDL_GetTicks() - lastTime; + if (currentTime < FRAMEWAIT) { + SDL_Delay(FRAMEWAIT - currentTime); + } + lastTime = SDL_GetTicks(); +}*/ \ No newline at end of file diff --git a/src/wii/graphics.h b/src/wii/graphics.h new file mode 100644 index 0000000..27794cf --- /dev/null +++ b/src/wii/graphics.h @@ -0,0 +1,61 @@ +//Wii graphics.h +#ifndef GRAPHICS_H +#define GRAPHICS_H + +//#include +#include + +typedef struct { + int tileX[16][12]; + int tileY[16][12]; +} PHL_Background; + +typedef struct { + unsigned int r; + unsigned int g; + unsigned int b; +} PHL_RGB; + +typedef struct { + SDL_Surface* fb; + int width; + int height; +} DrawBuffer; + +typedef struct { + SDL_Surface* surf; +} PHL_Surface; + +PHL_Surface backBuffer; +SDL_Surface *surfScreen; +DrawBuffer db; + +void PHL_GraphicsInit(); +void PHL_GraphicsExit(); + +void PHL_StartDrawing(); +void PHL_EndDrawing(); + +void PHL_ForceScreenUpdate(); + +void PHL_SetDrawbuffer(PHL_Surface surf); +void PHL_ResetDrawbuffer(); + +PHL_RGB PHL_NewRGB(int r, int g, int b); +void PHL_SetColorKey(PHL_Surface surf, int r, int g, int b); + +PHL_Surface PHL_NewSurface(int w, int h); +void PHL_FreeSurface(PHL_Surface surface); + +//PHL_Surface PHL_LoadBMP(char* fname); +PHL_Surface PHL_LoadBMP(int index); + +void PHL_DrawRect(int x, int y, int w, int h, PHL_RGB col); + +void PHL_DrawSurface(double x, double y, PHL_Surface surface); +void PHL_DrawSurfacePart(double x, double y, int cropx, int cropy, int cropw, int croph, PHL_Surface surface); + +void PHL_DrawBackground(PHL_Background back, PHL_Background fore); +void PHL_UpdateBackground(PHL_Background back, PHL_Background fore); + +#endif \ No newline at end of file diff --git a/src/wii/input.c b/src/wii/input.c new file mode 100644 index 0000000..53b9400 --- /dev/null +++ b/src/wii/input.c @@ -0,0 +1,162 @@ +//Wii +#include "input.h" +#include +#include + +double deadZone = 0.5; + +void updateKey(Button* btn, int state); + +void PHL_ScanInput() +{ + //btnStartPrev = btnStart; + + WPAD_ScanPads(); + //PAD_ScanPads(); + + u32 pressed = WPAD_ButtonsHeld(0); + //u32 gPressed = PAD_ButtonsHeld(0); + + int pUp = 0, pDown = 0, pLeft = 0, pRight = 0; + int pFaceLeft = 0, pFaceRight = 0, /*pFaceUp = 0,*/ pFaceDown = 0; + int pL = 0, pR = 0; + int pSelect = 0, pStart = 0; + int pAccept = 0, pDecline = 0; + + //if (pressed & WPAD_BUTTON_RIGHT || pressed & WPAD_CLASSIC_BUTTON_UP || gPressed & PAD_BUTTON_RIGHT) { pUp = 1; } + WPADData* cData = WPAD_Data(0); + + if (cData->exp.type == WPAD_EXP_NONE) + { //Normal Wiimote*/ + pUp = pressed & WPAD_BUTTON_RIGHT; + pDown = pressed & WPAD_BUTTON_LEFT; + pLeft = pressed & WPAD_BUTTON_UP; + pRight = pressed & WPAD_BUTTON_DOWN; + + pSelect = pressed & WPAD_BUTTON_MINUS; + pStart = pressed & WPAD_BUTTON_PLUS; + + pR = pressed & WPAD_BUTTON_A; + + pFaceLeft = pressed & WPAD_BUTTON_1; + pFaceDown = pressed & WPAD_BUTTON_2; + pFaceRight = pressed & WPAD_BUTTON_B; + + pAccept = pFaceDown; + pDecline = pFaceLeft; + }else if (cData->exp.type == WPAD_EXP_CLASSIC) + { //Classic Controller + //Get x and y axis from the joystick + //Taken from the Wii SDL source + float mag = 0.0, + ang = 0.0; + + mag = cData->exp.classic.ljs.mag; + ang = cData->exp.classic.ljs.ang; + + double xAxis = mag * sin((3.14159 * ang)/180.0f); + double yAxis = mag * cos((3.14159 * ang)/180.0f); + + if (yAxis < -deadZone || pressed & WPAD_CLASSIC_BUTTON_DOWN) { pDown = 1; } + if (yAxis > deadZone || pressed & WPAD_CLASSIC_BUTTON_UP) { pUp = 1; } + if (xAxis < -deadZone || pressed & WPAD_CLASSIC_BUTTON_LEFT) { pLeft = 1; } + if (xAxis > deadZone || pressed & WPAD_CLASSIC_BUTTON_RIGHT) { pRight = 1; } + + if (pUp == 1 && pDown == 1) { + pUp = 0; + pDown = 0; + } + if (pLeft == 1 && pRight == 1) { + pLeft = 0; + pRight = 0; + } + + pSelect = pressed & WPAD_CLASSIC_BUTTON_MINUS; + pStart = pressed & WPAD_CLASSIC_BUTTON_PLUS; + + pL = pressed & WPAD_CLASSIC_BUTTON_FULL_L; + pR = pressed & WPAD_CLASSIC_BUTTON_FULL_R; + + pFaceLeft = pressed & WPAD_CLASSIC_BUTTON_Y; + pFaceDown = pressed & WPAD_CLASSIC_BUTTON_B; + pFaceRight = pressed & WPAD_CLASSIC_BUTTON_A; + + pAccept = pFaceRight; + pDecline = pFaceDown; + } + + //Gamecube Controller + { + PAD_ScanPads(); + pressed = PAD_ButtonsHeld(0); + + double xAxis = PAD_StickX(0) / 25; + double yAxis = PAD_StickY(0) / 25; + + if (pressed & PAD_BUTTON_UP || yAxis > deadZone) { pUp = 1; } + if (pressed & PAD_BUTTON_DOWN || yAxis < -deadZone) { pDown = 1; } + if (pressed & PAD_BUTTON_LEFT || xAxis < -deadZone) { pLeft = 1; } + if (pressed & PAD_BUTTON_RIGHT || xAxis > deadZone) { pRight = 1; } + + if (pUp == 1 && pDown == 1) { + pUp = 0; + pDown = 0; + } + if (pLeft == 1 && pRight == 1) { + pLeft = 0; + pRight = 0; + } + + if (pressed & PAD_BUTTON_START) { pStart = 1; } + if (pressed & PAD_BUTTON_X) { pSelect = 1; } + + if (pressed & PAD_BUTTON_B) { pFaceLeft = 1; } + if (pressed & PAD_BUTTON_A) { pFaceDown = 1; } + if (pressed & PAD_BUTTON_Y) { pFaceRight = 1; } + + if (pressed & PAD_TRIGGER_L) { pL = 1; } + if (pressed & PAD_TRIGGER_R) { pR = 1; } + + pAccept = pFaceDown; + pDecline = pFaceLeft; + } + + updateKey(&btnUp, pUp); + updateKey(&btnDown, pDown); + updateKey(&btnLeft, pLeft); + updateKey(&btnRight, pRight); + + updateKey(&btnStart, pStart); + updateKey(&btnSelect, pSelect); + + updateKey(&btnL, pL); + updateKey(&btnR, pR); + + updateKey(&btnFaceLeft, pFaceLeft); + updateKey(&btnFaceDown, pFaceDown); + updateKey(&btnFaceRight, pFaceRight); + + updateKey(&btnAccept, pAccept); + updateKey(&btnDecline, pDecline); +} + +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/wii/input.h b/src/wii/input.h new file mode 100644 index 0000000..cf76e0e --- /dev/null +++ b/src/wii/input.h @@ -0,0 +1,20 @@ +#ifndef INPUT_H +#define INPUT_H + +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/wii/system.c b/src/wii/system.c new file mode 100644 index 0000000..d28c5e1 --- /dev/null +++ b/src/wii/system.c @@ -0,0 +1,47 @@ +#include "system.h" +#include +#include +#include + +int quitGame = 0; + +int PHL_MainLoop() +{ + if (quitGame == 1) { + return 0; + } + return 1; +} + +void PHL_ConsoleInit() +{ + //console_init(xfb,20,20,rmode->fbWidth,rmode->xfbHeight,rmode->fbWidth*VI_DISPLAY_PIX_SZ); +} + +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/wii/system.h b/src/wii/system.h new file mode 100644 index 0000000..d88444f --- /dev/null +++ b/src/wii/system.h @@ -0,0 +1,12 @@ +#ifndef SYSTEM_H +#define SYSTEM_H + +int 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