Merge pull request #13 from kichikuou/daibanchou

Implement the remaining HLLs for Big Bang Age
This commit is contained in:
Nunuhara Cabbage
2021-02-20 10:06:36 -08:00
committed by GitHub
9 changed files with 633 additions and 0 deletions
+14
View File
@@ -136,44 +136,58 @@ extern struct static_library lib_ACXLoader;
extern struct static_library lib_AliceLogo;
extern struct static_library lib_AliceLogo2;
extern struct static_library lib_AliceLogo3;
extern struct static_library lib_BanMisc;
extern struct static_library lib_Bitarray;
extern struct static_library lib_Confirm;
extern struct static_library lib_Confirm2;
extern struct static_library lib_DataFile;
extern struct static_library lib_DrawGraph;
extern struct static_library lib_DrawPluginManager;
extern struct static_library lib_File;
extern struct static_library lib_Gpx2Plus;
extern struct static_library lib_InputDevice;
extern struct static_library lib_InputString;
extern struct static_library lib_Math;
extern struct static_library lib_MenuMsg;
extern struct static_library lib_MsgLogManager;
extern struct static_library lib_MsgLogViewer;
extern struct static_library lib_MsgSkip;
extern struct static_library lib_OutputLog;
extern struct static_library lib_PlayDemo;
extern struct static_library lib_PlayMovie;
extern struct static_library lib_SACT2;
extern struct static_library lib_SengokuRanceFont;
extern struct static_library lib_SystemServiceEx;
extern struct static_library lib_Timer;
static struct static_library *static_libraries[] = {
&lib_ACXLoader,
&lib_AliceLogo,
&lib_AliceLogo2,
&lib_AliceLogo3,
&lib_BanMisc,
&lib_Bitarray,
&lib_Confirm,
&lib_Confirm2,
&lib_DataFile,
&lib_DrawGraph,
&lib_DrawPluginManager,
&lib_File,
&lib_Gpx2Plus,
&lib_InputDevice,
&lib_InputString,
&lib_Math,
&lib_MenuMsg,
&lib_MsgLogManager,
&lib_MsgLogViewer,
&lib_MsgSkip,
&lib_OutputLog,
&lib_PlayDemo,
&lib_PlayMovie,
&lib_SACT2,
&lib_SengokuRanceFont,
&lib_SystemServiceEx,
&lib_Timer,
NULL
};
+185
View File
@@ -0,0 +1,185 @@
/* Copyright (C) 2021 kichikuou <KichikuouChrome@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://gnu.org/licenses/>.
*/
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include "system4/string.h"
#include "hll.h"
#include "audio.h"
#include "savedata.h"
#include "vm/page.h"
#include "xsystem4.h"
static int BanMisc_SaveStruct(struct page *page, struct string *file_name)
{
char *path = unix_path(file_name->text);
FILE *fp = fopen(path, "w");
if (!fp) {
WARNING("Failed to open file '%s': %s", path, strerror(errno));
free(path);
return 0;
}
free(path);
cJSON *json = page_to_json(page);
char *str = cJSON_Print(json);
cJSON_Delete(json);
bool ok = fputs(str, fp) != EOF;
free(str);
fclose(fp);
if (!ok) {
WARNING("BanMisc.SaveStruct failed (fputs): %s", strerror(errno));
return 0;
}
return 1;
}
static int BanMisc_LoadStruct(struct page **_page, struct string *file_name)
{
struct page *page = *_page;
if (page->type != STRUCT_PAGE) {
VM_ERROR("BanMisc.LoadStruct of non-struct");
}
char *path = unix_path(file_name->text);
FILE *fp = fopen(path, "r");
if (!fp) {
WARNING("Failed to open file '%s': %s", path, strerror(errno));
free(path);
return 0;
}
free(path);
fseek(fp, 0, SEEK_END);
long len = ftell(fp);
fseek(fp, 0, SEEK_SET);
char *buf = xmalloc(len + 1);
buf[len] = '\0';
if (fread(buf, len, 1, fp) != 1) {
WARNING("BanMisc.LoadStruct failed (fread): %s", strerror(errno));
free(buf);
return 0;
}
fclose(fp);
cJSON *json = cJSON_Parse(buf);
free(buf);
if (!json) {
WARNING("BanMisc.LoadStruct failed to parse JSON");
return 0;
}
if (!cJSON_IsArray(json)) {
WARNING("BanMisc.LoadStruct incorrect type in parsed JSON");
cJSON_Delete(json);
return 0;
}
json_load_page(page, json);
cJSON_Delete(json);
return 1;
}
HLL_WARN_UNIMPLEMENTED( , void, BanMisc, Init, void *imainsystem);
static void BanMisc_FadePlay(int ch, int num, int time, int volume, int loop)
{
bgm_prepare(ch, num - 1);
bgm_play(ch);
// TODO: fading
}
static int BanMisc_IsPlay(int ch)
{
return bgm_is_playing(ch);
}
static void BanMisc_Stop(int ch)
{
bgm_stop(ch);
}
static void BanMisc_Fade(int ch, int time, int volume, int stopFlag)
{
bgm_fade(ch, time, volume, stopFlag);
}
HLL_WARN_UNIMPLEMENTED(0, int, BanMisc, IsFade, int ch);
static int BanMisc_SoundLoad(int ch, int num)
{
return wav_prepare(ch, num - 1);
}
static void BanMisc_SoundPlay(int ch, int loop)
{
if (loop != 1)
WARNING("sound loop is not supported");
wav_play(ch);
}
static void BanMisc_SoundStop(int ch)
{
wav_stop(ch);
}
static void BanMisc_SoundFade(int ch, int time, int vol, int stop)
{
wav_fade(ch, time, vol, stop);
}
static void BanMisc_SoundReverseLR(int ch)
{
wav_reverse_LR(ch);
}
static int BanMisc_SoundIsPlay(int ch)
{
return wav_is_playing(ch);
}
static int BanMisc_SoundRelease(int ch)
{
return wav_unprepare(ch);
}
static int BanMisc_IsMute(int ch)
{
// Returns 1 if ch is muted in the sound config dialog.
return 0;
}
HLL_LIBRARY(BanMisc,
HLL_EXPORT(SaveStruct, BanMisc_SaveStruct),
HLL_EXPORT(LoadStruct, BanMisc_LoadStruct),
HLL_EXPORT(Init, BanMisc_Init),
HLL_EXPORT(FadePlay, BanMisc_FadePlay),
HLL_EXPORT(IsPlay, BanMisc_IsPlay),
HLL_EXPORT(Stop, BanMisc_Stop),
HLL_EXPORT(Fade, BanMisc_Fade),
HLL_EXPORT(IsFade, BanMisc_IsFade),
HLL_EXPORT(SoundLoad, BanMisc_SoundLoad),
HLL_EXPORT(SoundPlay, BanMisc_SoundPlay),
HLL_EXPORT(SoundStop, BanMisc_SoundStop),
HLL_EXPORT(SoundFade, BanMisc_SoundFade),
HLL_EXPORT(SoundReverseLR, BanMisc_SoundReverseLR),
HLL_EXPORT(SoundIsPlay, BanMisc_SoundIsPlay),
HLL_EXPORT(SoundRelease, BanMisc_SoundRelease),
HLL_EXPORT(IsMute, BanMisc_IsMute));
+164
View File
@@ -0,0 +1,164 @@
/* Copyright (C) 2021 kichikuou <KichikuouChrome@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://gnu.org/licenses/>.
*/
#include <stdlib.h>
#include <string.h>
#include "hll.h"
#include "vm/page.h"
struct bitarray {
uint8_t *bits;
int size;
};
static struct bitarray *arrays;
static int nr_arrays;
static bool is_valid_bitarray(int id)
{
if (id < 0 || id >= nr_arrays || !arrays[id].bits) {
WARNING("Invalid bitarray handle %d", id);
return false;
}
return true;
}
static int Bitarray_Create(int length)
{
int i;
for (i = 0; i < nr_arrays && arrays[i].bits; i++)
;
if (i == nr_arrays) {
int new_size = nr_arrays + 16;
arrays = xrealloc_array(arrays, nr_arrays, new_size, sizeof(struct bitarray));
nr_arrays = new_size;
}
arrays[i].size = length;
arrays[i].bits = xcalloc((length + 31) / 32, 4); // 32-bit alignment
return i;
}
// void Resize(int nBitSet, int nLength);
static int Bitarray_Numof(int bitSet)
{
if (!is_valid_bitarray(bitSet))
return 0;
return arrays[bitSet].size;
}
static int Bitarray_GetBit(int bitSet, int posBit)
{
if (!is_valid_bitarray(bitSet))
return 0;
if (posBit < 0 || posBit >= arrays[bitSet].size) {
WARNING("Bitarray index out of bounds %d", posBit);
return 0;
}
return !!(arrays[bitSet].bits[posBit >> 3] & 1 << (posBit & 7));
}
static void Bitarray_SetBit(int bitSet, int posBit, int bit)
{
if (!is_valid_bitarray(bitSet))
return;
if (posBit < 0 || posBit >= arrays[bitSet].size) {
WARNING("Bitarray index out of bounds %d", posBit);
return;
}
if (bit)
arrays[bitSet].bits[posBit >> 3] |= 1 << (posBit & 7);
else
arrays[bitSet].bits[posBit >> 3] &= ~(1 << (posBit & 7));
}
// int GetByte(int nBitSet, int nPosByte);
// void SetByte(int nBitSet, int nPosByte, int nByte);
// int GetWord(int nBitSet, int nPosWord);
// void SetWord(int nBitSet, int nPosWord, int nWord);
// int GetInt(int nBitSet, int nPosInt);
// void SetInt(int nBitSet, int nPosInt, int nInt);
static void Bitarray_GetArray(int bitSet, struct page **page)
{
if (!is_valid_bitarray(bitSet))
return;
struct page *array = *page;
if (!array || array->type != ARRAY_PAGE || array->a_type != AIN_ARRAY_INT || array->rank != 1)
ERROR("Not an flat integer array");
int n = (arrays[bitSet].size + 31) / 32;
if (n > array->nr_vars)
n = array->nr_vars;
uint32_t *p = (uint32_t *)arrays[bitSet].bits;
for (int i = 0; i < n; i++)
p[i] = array->values[i].i;
}
static void Bitarray_SetArray(int bitSet, struct page **page)
{
if (!is_valid_bitarray(bitSet))
return;
struct page *array = *page;
if (!array || array->type != ARRAY_PAGE || array->a_type != AIN_ARRAY_INT || array->rank != 1)
ERROR("Not an flat integer array");
int n = (arrays[bitSet].size + 31) / 32;
if (n > array->nr_vars)
n = array->nr_vars;
uint32_t *p = (uint32_t *)arrays[bitSet].bits;
for (int i = 0; i < n; i++)
variable_set(array, i, AIN_INT, vm_int(p[i]));
}
static void Bitarray_Free(int bitSet)
{
if (!is_valid_bitarray(bitSet))
return;
free(arrays[bitSet].bits);
arrays[bitSet].bits = NULL;
arrays[bitSet].size = 0;
}
static void Bitarray_FreeAll(void)
{
for (int i = 0; i < nr_arrays; i++) {
if (arrays[i].bits)
Bitarray_Free(i);
}
}
HLL_LIBRARY(Bitarray,
HLL_EXPORT(Create, Bitarray_Create),
// HLL_EXPORT(Resize, Bitarray_Resize),
HLL_EXPORT(Numof, Bitarray_Numof),
HLL_EXPORT(GetBit, Bitarray_GetBit),
HLL_EXPORT(SetBit, Bitarray_SetBit),
// HLL_EXPORT(GetByte, Bitarray_GetByte),
// HLL_EXPORT(SetByte, Bitarray_SetByte),
// HLL_EXPORT(GetWord, Bitarray_GetWord),
// HLL_EXPORT(SetWord, Bitarray_SetWord),
// HLL_EXPORT(GetInt, Bitarray_GetInt),
// HLL_EXPORT(SetInt, Bitarray_SetInt),
HLL_EXPORT(GetArray, Bitarray_GetArray),
HLL_EXPORT(SetArray, Bitarray_SetArray),
HLL_EXPORT(Free, Bitarray_Free),
HLL_EXPORT(FreeAll, Bitarray_FreeAll));
+29
View File
@@ -0,0 +1,29 @@
/* Copyright (C) 2021 kichikuou <KichikuouChrome@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://gnu.org/licenses/>.
*/
#include "hll.h"
HLL_WARN_UNIMPLEMENTED( , void, Confirm, Init, void *imainsystem);
HLL_WARN_UNIMPLEMENTED(1, int, Confirm, ExistKeyFile, struct string *filename, int keynum);
//int Confirm_CheckProtectFile(struct string *filename, int keynum);
//int Confirm_CreateKeyFile(struct string *filename, int keynum);
HLL_LIBRARY(Confirm,
HLL_EXPORT(Init, Confirm_Init),
HLL_EXPORT(ExistKeyFile, Confirm_ExistKeyFile)
//HLL_EXPORT(CheckProtectFile, Confirm_CheckProtectFile),
//HLL_EXPORT(CreateKeyFile, Confirm_CreateKeyFile)
);
+96
View File
@@ -0,0 +1,96 @@
/* Copyright (C) 2021 kichikuou <KichikuouChrome@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://gnu.org/licenses/>.
*/
#include <time.h>
#include "system4/string.h"
#include "hll.h"
#include "input.h"
static void handle_events_throttled(void)
{
// Big Bang Age uses busy loops to wait input events. To prevent taking 100%
// CPU time, sleep 10ms every 30 input event checks.
static int count;
if (++count == 30) {
count = 0;
struct timespec ts = {
.tv_sec = 0,
.tv_nsec = 10000000L
};
nanosleep(&ts, NULL);
}
handle_events();
}
HLL_WARN_UNIMPLEMENTED( , void, InputDevice, Init, void *imainsystem);
void InputDevice_ClearKeyDownFlag(void)
{
key_clear_flag();
}
static int InputDevice_IsKeyDown(int key)
{
handle_events_throttled();
return key_is_down(key);
}
static int InputDevice_GetCsrPos(int *x, int *y)
{
handle_events_throttled();
mouse_get_pos(x, y);
return mouse_focus && keyboard_focus;
}
static int InputDevice_SetCsrPos(int x, int y)
{
handle_events_throttled();
mouse_set_pos(x, y);
return mouse_focus && keyboard_focus;
}
static void InputDevice_ClearWheelCount(void)
{
mouse_clear_wheel();
}
static int InputDevice_GetWheelCountForward(void)
{
int forward, back;
handle_events_throttled();
mouse_get_wheel(&forward, &back);
return forward;
}
static int InputDevice_GetWheelCountBack(void)
{
int forward, back;
handle_events_throttled();
mouse_get_wheel(&forward, &back);
return back;
}
HLL_LIBRARY(InputDevice,
HLL_EXPORT(Init, InputDevice_Init),
HLL_EXPORT(ClearKeyDownFlag, InputDevice_ClearKeyDownFlag),
HLL_EXPORT(IsKeyDown, InputDevice_IsKeyDown),
HLL_EXPORT(GetCsrPos, InputDevice_GetCsrPos),
HLL_EXPORT(SetCsrPos, InputDevice_SetCsrPos),
HLL_EXPORT(ClearWheelCount, InputDevice_ClearWheelCount),
HLL_EXPORT(GetWheelCountForward, InputDevice_GetWheelCountForward),
HLL_EXPORT(GetWheelCountBack, InputDevice_GetWheelCountBack));
+49
View File
@@ -0,0 +1,49 @@
/* Copyright (C) 2021 kichikuou <KichikuouChrome@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://gnu.org/licenses/>.
*/
#include "hll.h"
static bool enabled = false;
static int state = 0;
HLL_WARN_UNIMPLEMENTED( , void, MenuMsg, Init, void *imainsystem);
static void MenuMsg_SetState(int flag)
{
state = flag;
}
static void MenuMsg_SetEnable(int enable)
{
enabled = !!enable;
}
static int MenuMsg_GetState(void)
{
return state;
}
static int MenuMsg_GetEnable(void)
{
return enabled;
}
HLL_LIBRARY(MenuMsg,
HLL_EXPORT(Init, MenuMsg_Init),
HLL_EXPORT(SetState, MenuMsg_SetState),
HLL_EXPORT(SetEnable, MenuMsg_SetEnable),
HLL_EXPORT(GetState, MenuMsg_GetState),
HLL_EXPORT(GetEnable, MenuMsg_GetEnable));
+30
View File
@@ -0,0 +1,30 @@
/* Copyright (C) 2021 kichikuou <KichikuouChrome@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://gnu.org/licenses/>.
*/
#include "hll.h"
HLL_WARN_UNIMPLEMENTED(0, int, PlayDemo, Init, void *imainsystem);
//int PlayDemo_Load(struct string *filename, int ch, int bgm);
//int PlayDemo_Play(void);
//void PlayDemo_SetCheckClick(int flag);
HLL_LIBRARY(PlayDemo,
HLL_EXPORT(Init, PlayDemo_Init)
//HLL_EXPORT(Load, PlayDemo_Load),
//HLL_EXPORT(Play, PlayDemo_Play),
//HLL_EXPORT(SetCheckClick, PlayDemo_SetCheckClick)
);
+59
View File
@@ -0,0 +1,59 @@
/* Copyright (C) 2021 kichikuou <KichikuouChrome@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://gnu.org/licenses/>.
*/
#include <time.h>
#include "hll.h"
HLL_WARN_UNIMPLEMENTED(1, int, Timer, Init, void *imainsystem);
static int Timer_Get(void)
{
// Returns the same value as system.GetTime().
return vm_time();
}
static void Timer_Wait(int time)
{
struct timespec ts = {
.tv_sec = time / 1000,
.tv_nsec = (time % 1000) * 1000000L
};
nanosleep(&ts, NULL);
}
static void Timer_GetDayTime(int *year, int *month, int *day_of_week, int *day,
int *hour, int *minute, int *second, int *milliseconds)
{
struct timespec ts;
clock_gettime(CLOCK_REALTIME, &ts);
struct tm *tm = localtime(&ts.tv_sec);
*year = tm->tm_year + 1900;
*month = tm->tm_mon + 1;
*day_of_week = tm->tm_wday;
*day = tm->tm_mday;
*hour = tm->tm_hour;
*minute = tm->tm_min;
*second = tm->tm_sec;
*milliseconds = ts.tv_nsec / 1000000;
}
HLL_LIBRARY(Timer,
HLL_EXPORT(Init, Timer_Init),
HLL_EXPORT(Get, Timer_Get),
HLL_EXPORT(Wait, Timer_Wait),
HLL_EXPORT(GetDayTime, Timer_GetDayTime));
+7
View File
@@ -22,22 +22,29 @@ xsystem4 = ['audio.c',
'hll/AliceLogo.c',
'hll/AliceLogo2.c',
'hll/AliceLogo3.c',
'hll/BanMisc.c',
'hll/Bitarray.c',
'hll/Confirm.c',
'hll/Confirm2.c',
'hll/DataFile.c',
'hll/DrawGraph.c',
'hll/DrawPluginManager.c',
'hll/File.c',
'hll/Gpx2Plus.c',
'hll/InputDevice.c',
'hll/InputString.c',
'hll/Math.c',
'hll/MenuMsg.c',
'hll/MsgLogManager.c',
'hll/MsgLogViewer.c',
'hll/MsgSkip.c',
'hll/OutputLog.c',
'hll/PlayDemo.c',
'hll/PlayMovie.c',
'hll/SACT2.c',
'hll/SengokuRanceFont.c',
'hll/SystemServiceEx.c',
'hll/Timer.c',
]
xsystem4_deps = [libm, zlib, sdl2, sdl2ttf, sdl2mixer, ffi, gl, glew, libsys4_dep]