diff --git a/Source/pdxinfo b/Source/pdxinfo index 12467cb..3a413fa 100644 --- a/Source/pdxinfo +++ b/Source/pdxinfo @@ -2,6 +2,6 @@ name=PlayGB author=Risolvi Productions description=A Game Boy emulator based on Peanut-GB bundleID=com.risolvipro.playgb -version=0.7.6 -buildNumber=6 +version=0.7.7 +buildNumber=7 imagePath=launcher diff --git a/main.c b/main.c index c7f718c..0f628c4 100644 --- a/main.c +++ b/main.c @@ -17,23 +17,26 @@ static int update(void* userdata); -DllExport int eventHandler(PlaydateAPI *pd, PDSystemEvent event, uint32_t arg) { - - if(event == kEventInit){ +DllExport int eventHandler(PlaydateAPI *pd, PDSystemEvent event, uint32_t arg) +{ + if(event == kEventInit) + { playdate = pd; PGB_init(); pd->system->setUpdateCallback(update, pd); } - else if (event == kEventTerminate){ + else if (event == kEventTerminate) + { PGB_quit(); } return 0; } -int update(void* userdata) { +int update(void* userdata) +{ PlaydateAPI *pd = userdata; float dt = pd->system->getElapsedTime(); diff --git a/src/app.c b/src/app.c index 0646c0a..f5b2375 100644 --- a/src/app.c +++ b/src/app.c @@ -13,8 +13,8 @@ PGB_Application *PGB_App; -void PGB_init(void) { - +void PGB_init(void) +{ PGB_App = pgb_malloc(sizeof(PGB_Application)); PGB_App->scene = NULL; @@ -43,20 +43,23 @@ void PGB_init(void) { PGB_present(libraryScene->scene); } -void PGB_update(float dt) { - +void PGB_update(float dt) +{ PGB_App->dt = dt; PGB_App->crankChange = playdate->system->getCrankChange(); - if(PGB_App->scene){ + if(PGB_App->scene) + { void *managedObject = PGB_App->scene->managedObject; PGB_App->scene->update(managedObject); } - if(PGB_App->pendingScene){ + if(PGB_App->pendingScene) + { // present pending scene - if(PGB_App->scene){ + if(PGB_App->scene) + { prefereces_save_to_disk(); void *managedObject = PGB_App->scene->managedObject; @@ -77,12 +80,14 @@ void PGB_update(float dt) { float refreshRate = 30; float compensation = 0; - if(PGB_App->scene){ + if(PGB_App->scene) + { refreshRate = PGB_App->scene->preferredRefreshRate; compensation = PGB_App->scene->refreshRateCompensation; } - if(refreshRate > 0){ + if(refreshRate > 0) + { float refreshInterval = 1.0f / refreshRate + compensation; while(playdate->system->getElapsedTime() < refreshInterval); } @@ -90,16 +95,17 @@ void PGB_update(float dt) { #endif } -void PGB_present(PGB_Scene *scene) { - +void PGB_present(PGB_Scene *scene) +{ PGB_App->pendingScene = scene; } -void PGB_quit(void) { - +void PGB_quit(void) +{ prefereces_save_to_disk(); - if(PGB_App->scene){ + if(PGB_App->scene) + { void *managedObject = PGB_App->scene->managedObject; PGB_App->scene->free(managedObject); } diff --git a/src/array.c b/src/array.c index ecaa5c8..1048f45 100644 --- a/src/array.c +++ b/src/array.c @@ -7,27 +7,31 @@ #include "array.h" -PGB_Array* array_new(void) { +PGB_Array* array_new(void) +{ PGB_Array *array = pgb_malloc(sizeof(PGB_Array)); array->length = 0; - array->items = pgb_malloc(0); + array->items = NULL; return array; } -void array_push(PGB_Array *array, void *item) { +void array_push(PGB_Array *array, void *item) +{ array->length++; array->items = pgb_realloc(array->items, array->length * sizeof(item)); array->items[array->length - 1] = item; } -void array_clear(PGB_Array *array) { +void array_clear(PGB_Array *array) +{ array->length = 0; - array->items = pgb_realloc(array->items, 0); + pgb_free(array->items); + array->items = NULL; } -void array_free(PGB_Array *array) { - +void array_free(PGB_Array *array) +{ pgb_free(array->items); pgb_free(array); } diff --git a/src/game_scene.c b/src/game_scene.c index 5b705df..343806d 100644 --- a/src/game_scene.c +++ b/src/game_scene.c @@ -42,8 +42,8 @@ static const char *selectButtonText = "select"; static uint8_t PGB_GameScene_bitmask[256][4][4]; static bool PGB_GameScene_bitmask_done = false; -PGB_GameScene* PGB_GameScene_new(const char *rom_filename){ - +PGB_GameScene* PGB_GameScene_new(const char *rom_filename) +{ PGB_Scene *scene = PGB_Scene_new(); PGB_GameScene *gameScene = pgb_malloc(sizeof(PGB_GameScene)); @@ -94,12 +94,14 @@ PGB_GameScene* PGB_GameScene_new(const char *rom_filename){ PGB_GameSceneError romError; uint8_t *rom = read_rom_to_ram(rom_filename, &romError); - if(rom){ + if(rom) + { context->rom = rom; enum gb_init_error_e gb_ret = gb_init(&context->gb, rom, gb_error, NULL); - if(gb_ret == GB_INIT_NO_ERROR){ + if(gb_ret == GB_INIT_NO_ERROR) + { char *save_filename = pgb_save_filename(rom_filename, false); gameScene->save_filename = save_filename; @@ -107,7 +109,8 @@ PGB_GameScene* PGB_GameScene_new(const char *rom_filename){ context->gb.gb_cart_ram = context->cart_ram; - if(gameScene->audioEnabled){ + if(gameScene->audioEnabled) + { // init audio playdate->sound->channel->setVolume(playdate->sound->getDefaultChannel(), 0.2f); @@ -125,14 +128,16 @@ PGB_GameScene* PGB_GameScene_new(const char *rom_filename){ // set game state to loaded gameScene->state = PGB_GameSceneStateLoaded; } - else { + else + { gameScene->state = PGB_GameSceneStateError; gameScene->error = PGB_GameSceneErrorFatal; playdate->system->logToConsole("%s:%i: Error initializing gb context", __FILE__, __LINE__); } } - else { + else + { gameScene->state = PGB_GameSceneStateError; gameScene->error = romError; } @@ -140,8 +145,8 @@ PGB_GameScene* PGB_GameScene_new(const char *rom_filename){ return gameScene; } -void PGB_GameScene_selector_init(PGB_GameScene *gameScene){ - +void PGB_GameScene_selector_init(PGB_GameScene *gameScene) +{ int startButtonWidth = playdate->graphics->getTextWidth(PGB_App->labelFont, startButtonText, strlen(startButtonText), kUTF8Encoding, 0); int selectButtonWidth = playdate->graphics->getTextWidth(PGB_App->labelFont, selectButtonText, strlen(selectButtonText), kUTF8Encoding, 0); @@ -194,22 +199,27 @@ void PGB_GameScene_selector_init(PGB_GameScene *gameScene){ /** * Returns a pointer to the allocated space containing the ROM. Must be freed. */ -uint8_t *read_rom_to_ram(const char *filename, PGB_GameSceneError *sceneError) { +uint8_t *read_rom_to_ram(const char *filename, PGB_GameSceneError *sceneError) +{ *sceneError = PGB_GameSceneErrorUndefined; SDFile *rom_file = playdate->file->open(filename, kFileReadData); - if(rom_file == NULL){ + if(rom_file == NULL) + { const char *fileError = playdate->file->geterr(); playdate->system->logToConsole("%s:%i: Can't open rom file %s", __FILE__, __LINE__, filename); playdate->system->logToConsole("%s:%i: File error %s", __FILE__, __LINE__, fileError); *sceneError = PGB_GameSceneErrorLoadingRom; - if(fileError){ + if(fileError) + { char *fsErrorCode = pgb_extract_fs_error_code(fileError); - if(fsErrorCode){ - if(strcmp(fsErrorCode, "0709") == 0){ + if(fsErrorCode) + { + if(strcmp(fsErrorCode, "0709") == 0) + { *sceneError = PGB_GameSceneErrorWrongLocation; } } @@ -223,7 +233,8 @@ uint8_t *read_rom_to_ram(const char *filename, PGB_GameSceneError *sceneError) { uint8_t *rom = pgb_malloc(rom_size); - if(playdate->file->read(rom_file, rom, rom_size) != rom_size){ + if(playdate->file->read(rom_file, rom, rom_size) != rom_size) + { playdate->system->logToConsole("%s:%i: Can't read rom file %s", __FILE__, __LINE__, filename); pgb_free(rom); @@ -239,13 +250,15 @@ uint8_t *read_rom_to_ram(const char *filename, PGB_GameSceneError *sceneError) { void read_cart_ram_file(const char *save_filename, uint8_t **dest, const size_t len){ /* If save file not required. */ - if(len == 0) { + if(len == 0) + { *dest = NULL; return; } /* Allocate enough memory to hold save file. */ - if((*dest = pgb_malloc(len)) == NULL){ + if((*dest = pgb_malloc(len)) == NULL) + { playdate->system->logToConsole("%s:%i: Error allocating save file %s", __FILE__, __LINE__, save_filename); } @@ -253,7 +266,8 @@ void read_cart_ram_file(const char *save_filename, uint8_t **dest, const size_t /* It doesn't matter if the save file doesn't exist. We initialise the * save memory allocated above. The save file will be created on exit. */ - if(f == NULL){ + if(f == NULL) + { memset(*dest, 0, len); return; } @@ -263,15 +277,17 @@ void read_cart_ram_file(const char *save_filename, uint8_t **dest, const size_t playdate->file->close(f); } -void write_cart_ram_file(const char *save_filename, uint8_t **dest, const size_t len){ - - if(len == 0 || *dest == NULL){ +void write_cart_ram_file(const char *save_filename, uint8_t **dest, const size_t len) +{ + if(len == 0 || *dest == NULL) + { return; } SDFile *f = playdate->file->open(save_filename, kFileWrite); - if(f == NULL){ + if(f == NULL) + { playdate->system->logToConsole("%s:%i: Can't write save file", __FILE__, __LINE__, save_filename); return; } @@ -285,28 +301,30 @@ void write_cart_ram_file(const char *save_filename, uint8_t **dest, const size_t * Handles an error reported by the emulator. The emulator context may be used * to better understand why the error given in gb_err was reported. */ -void gb_error(struct gb_s *gb, const enum gb_error_e gb_err, const uint16_t val) { - +void gb_error(struct gb_s *gb, const enum gb_error_e gb_err, const uint16_t val) +{ PGB_GameSceneContext *context = gb->direct.priv; bool is_fatal = false; - if(gb_err == GB_INVALID_OPCODE){ + if(gb_err == GB_INVALID_OPCODE) + { is_fatal = true; playdate->system->logToConsole("%s:%i: Invalid opcode %#04x at PC: %#06x, SP: %#06x", __FILE__, __LINE__, val, gb->cpu_reg.pc - 1, gb->cpu_reg.sp); } - else if(gb_err == GB_INVALID_READ || gb_err == GB_INVALID_WRITE){ - + else if(gb_err == GB_INVALID_READ || gb_err == GB_INVALID_WRITE) + { playdate->system->logToConsole("%s:%i: Invalid read / write", __FILE__, __LINE__); } - else { + else + { is_fatal = true; - playdate->system->logToConsole("%s:%i: Unknown error occurred", __FILE__, __LINE__); } - if(is_fatal){ + if(is_fatal) + { // write recovery .sav char *recovery_filename = pgb_save_filename(context->scene->rom_filename, true); write_cart_ram_file(recovery_filename, &context->gb.gb_cart_ram, gb_get_save_size(gb)); @@ -322,8 +340,8 @@ void gb_error(struct gb_s *gb, const enum gb_error_e gb_err, const uint16_t val) return; } -void PGB_GameScene_update(void *object){ - +void PGB_GameScene_update(void *object) +{ PGB_GameScene *gameScene = object; PGB_Scene_update(gameScene->scene); @@ -333,19 +351,24 @@ void PGB_GameScene_update(void *object){ gameScene->selector.startPressed = false; gameScene->selector.selectPressed = false; - if(!playdate->system->isCrankDocked()){ + if(!playdate->system->isCrankDocked()) + { float angle = fmaxf(0, fminf(360, playdate->system->getCrankAngle())); - if(angle <= (180 - gameScene->selector.deadAngle)){ - if(angle >= gameScene->selector.triggerAngle){ + if(angle <= (180 - gameScene->selector.deadAngle)) + { + if(angle >= gameScene->selector.triggerAngle) + { gameScene->selector.startPressed = true; } float adjustedAngle = fminf(angle, gameScene->selector.triggerAngle); progress = 0.5f - adjustedAngle / gameScene->selector.triggerAngle * 0.5f; } - else if(angle >= (180 + gameScene->selector.deadAngle)){ - if(angle <= (360 - gameScene->selector.triggerAngle)){ + else if(angle >= (180 + gameScene->selector.deadAngle)) + { + if(angle <= (360 - gameScene->selector.triggerAngle)) + { gameScene->selector.selectPressed = true; } @@ -360,16 +383,19 @@ void PGB_GameScene_update(void *object){ int selectorIndex; - if(gameScene->selector.startPressed && gameScene->selector.selectPressed){ + if(gameScene->selector.startPressed && gameScene->selector.selectPressed) + { selectorIndex = -1; } else { selectorIndex = 1 + roundf(progress * (gameScene->selector.numberOfFrames - 2)); - if(progress == 0){ + if(progress == 0) + { selectorIndex = 0; } - else if(progress == 1){ + else if(progress == 1) + { selectorIndex = gameScene->selector.numberOfFrames - 1; } } @@ -378,12 +404,14 @@ void PGB_GameScene_update(void *object){ bool needsDisplay = false; - if(gameScene->model.empty || gameScene->needsDisplay || gameScene->model.state != gameScene->state || gameScene->model.error != gameScene->error){ + if(gameScene->model.empty || gameScene->needsDisplay || gameScene->model.state != gameScene->state || gameScene->model.error != gameScene->error) + { needsDisplay = true; } bool needsDisplaySelector = false; - if(needsDisplay || gameScene->model.selectorIndex != gameScene->selector.index){ + if(needsDisplay || gameScene->model.selectorIndex != gameScene->selector.index) + { needsDisplaySelector = true; } @@ -393,8 +421,8 @@ void PGB_GameScene_update(void *object){ gameScene->model.selectorIndex = gameScene->selector.index; gameScene->needsDisplay = false; - if(gameScene->state == PGB_GameSceneStateLoaded){ - + if(gameScene->state == PGB_GameSceneStateLoaded) + { PGB_GameSceneContext *context = gameScene->context; PDButtons current; @@ -410,7 +438,8 @@ void PGB_GameScene_update(void *object){ context->gb.direct.joypad_bits.right = !(current & kButtonRight); context->gb.direct.joypad_bits.down = !(current & kButtonDown); - if(needsDisplay){ + if(needsDisplay) + { playdate->graphics->clear(kColorBlack); } @@ -425,8 +454,8 @@ void PGB_GameScene_update(void *object){ gameScene->scene->preferredRefreshRate = gb_draw ? 60 : 0; gameScene->scene->refreshRateCompensation = gb_draw ? (1.0f / 60 - PGB_App->dt) : 0; - if(gb_draw){ - + if(gb_draw) + { uint8_t *framebuffer = playdate->graphics->getFrame(); int skip_counter = 0; @@ -441,16 +470,19 @@ void PGB_GameScene_update(void *object){ int y_offset; int next_y_offset = 2; - for(int y = 0; y < (LCD_HEIGHT - 1); y++){ + for(int y = 0; y < (LCD_HEIGHT - 1); y++) + { y_offset = next_y_offset; - if(skip_counter == 5){ + if(skip_counter == 5) + { y_offset = 1; next_y_offset = 1; skip_counter = 0; single_line = true; } - else if(single_line){ + else if(single_line) + { next_y_offset = 2; single_line = false; } @@ -458,17 +490,19 @@ void PGB_GameScene_update(void *object){ uint8_t *pixels; uint8_t *old_pixels; - if(context->gb.display.back_fb_enabled){ + if(context->gb.display.back_fb_enabled) + { pixels = gb_front_fb[y]; old_pixels = gb_back_fb[y]; } - else { + else + { pixels = gb_back_fb[y]; old_pixels = gb_front_fb[y]; } - if(memcmp(pixels, old_pixels, LCD_WIDTH) != 0){ - + if(memcmp(pixels, old_pixels, LCD_WIDTH) != 0) + { int d_row1 = y2 & 3; int d_row2 = (y2 + 1) & 3; @@ -476,23 +510,27 @@ void PGB_GameScene_update(void *object){ int fb_index2 = lcd_rows + row_offset; memset(&framebuffer[fb_index1], 0x00, PGB_LCD_ROWSIZE); - if(y_offset == 2){ + if(y_offset == 2) + { memset(&framebuffer[fb_index2], 0x00, PGB_LCD_ROWSIZE); } uint8_t bit = 0; - for(int x = 0; x < LCD_WIDTH; x++){ + for(int x = 0; x < LCD_WIDTH; x++) + { uint8_t pixel = pixels[x]; framebuffer[fb_index1] |= PGB_GameScene_bitmask[pixel][bit][d_row1]; - if(y_offset == 2){ + if(y_offset == 2) + { framebuffer[fb_index2] |= PGB_GameScene_bitmask[pixel][bit][d_row2]; } bit++; - if(bit == 4){ + if(bit == 4) + { bit = 0; fb_index1++; fb_index2++; @@ -511,7 +549,8 @@ void PGB_GameScene_update(void *object){ y2 += y_offset; lcd_rows += (y_offset == 1) ? row_offset : row_offset2; - if(!single_line){ + if(!single_line) + { skip_counter++; } } @@ -519,12 +558,14 @@ void PGB_GameScene_update(void *object){ gameScene->rtc_timer += PGB_App->dt; - if(gameScene->rtc_timer >= 1){ + if(gameScene->rtc_timer >= 1) + { gameScene->rtc_timer = gameScene->rtc_timer - 1; gb_tick_rtc(&context->gb); } - if(needsDisplay){ + if(needsDisplay) + { playdate->graphics->setFont(PGB_App->labelFont); playdate->graphics->setDrawMode(kDrawModeFillWhite); @@ -534,13 +575,16 @@ void PGB_GameScene_update(void *object){ playdate->graphics->setDrawMode(kDrawModeCopy); } - if(needsDisplaySelector){ + if(needsDisplaySelector) + { LCDBitmap *bitmap; - if(selectorIndex < 0){ + if(selectorIndex < 0) + { bitmap = PGB_App->startSelectBitmap; } - else { + else + { bitmap = playdate->graphics->getTableBitmap(PGB_App->selectorBitmapTable, selectorIndex); } @@ -551,25 +595,29 @@ void PGB_GameScene_update(void *object){ PDRect highlightFrame = gameScene->debug_highlightFrame; playdate->graphics->fillRect(highlightFrame.x, highlightFrame.y, highlightFrame.width, highlightFrame.height, kColorBlack); - for(int y = 0; y < PGB_LCD_HEIGHT; y++){ + for(int y = 0; y < PGB_LCD_HEIGHT; y++) + { int absoluteY = PGB_LCD_Y + y; - if(gameScene->debug_updatedRows[absoluteY]){ + if(gameScene->debug_updatedRows[absoluteY]) + { playdate->graphics->fillRect(highlightFrame.x, absoluteY, highlightFrame.width, 1, kColorWhite); } } #endif - if(preferences_display_fps){ + if(preferences_display_fps) + { playdate->system->drawFPS(0, 0); } } - else if(gameScene->state == PGB_GameSceneStateError){ - + else if(gameScene->state == PGB_GameSceneStateError) + { gameScene->scene->preferredRefreshRate = 30; gameScene->scene->refreshRateCompensation = 0; - if(needsDisplay){ + if(needsDisplay) + { char *errorTitle = "Oh no!"; int errorMessagesCount = 1; @@ -577,16 +625,19 @@ void PGB_GameScene_update(void *object){ errorMessages[0] = "A generic error occurred"; - if(gameScene->error == PGB_GameSceneErrorLoadingRom){ + if(gameScene->error == PGB_GameSceneErrorLoadingRom) + { errorMessages[0] = "Can't load the selected ROM"; } - else if(gameScene->error == PGB_GameSceneErrorWrongLocation){ + else if(gameScene->error == PGB_GameSceneErrorWrongLocation) + { errorTitle = "Wrong location"; errorMessagesCount = 2; errorMessages[0] = "Please move the ROM to"; errorMessages[1] = "/Data/*.playgb/games/"; } - else if(gameScene->error == PGB_GameSceneErrorFatal){ + else if(gameScene->error == PGB_GameSceneErrorFatal) + { errorMessages[0] = "A fatal error occurred"; } @@ -609,7 +660,8 @@ void PGB_GameScene_update(void *object){ int messageY = titleY + titleHeight + titleToMessageSpacing; - for(int i = 0; i < errorMessagesCount; i++){ + for(int i = 0; i < errorMessagesCount; i++) + { char *errorMessage = errorMessages[i]; int messageX = (float)(playdate->display->getWidth() - playdate->graphics->getTextWidth(PGB_App->bodyFont, errorMessage, strlen(errorMessage), kUTF8Encoding, 0)) / 2; @@ -622,8 +674,8 @@ void PGB_GameScene_update(void *object){ } } -void PGB_GameScene_didSelectSave(void *userdata) { - +void PGB_GameScene_didSelectSave(void *userdata) +{ PGB_GameScene *gameScene = userdata; gameScene->audioLocked = true; @@ -633,8 +685,8 @@ void PGB_GameScene_didSelectSave(void *userdata) { gameScene->audioLocked = false; } -void PGB_GameScene_didSelectLibrary(void *userdata) { - +void PGB_GameScene_didSelectLibrary(void *userdata) +{ PGB_GameScene *gameScene = userdata; gameScene->audioLocked = true; @@ -643,56 +695,62 @@ void PGB_GameScene_didSelectLibrary(void *userdata) { PGB_present(libraryScene->scene); } -void PGB_GameScene_refreshMenu(PGB_GameScene *gameScene) { - +void PGB_GameScene_refreshMenu(PGB_GameScene *gameScene) +{ playdate->system->removeAllMenuItems(); playdate->system->addMenuItem("Library", PGB_GameScene_didSelectLibrary, gameScene); - if(gameScene->state == PGB_GameSceneStateLoaded){ + if(gameScene->state == PGB_GameSceneStateLoaded) + { playdate->system->addMenuItem("Save", PGB_GameScene_didSelectSave, gameScene); } } -void PGB_GameScene_menu(void *object) { - +void PGB_GameScene_menu(void *object) +{ PGB_GameScene *gameScene = object; PGB_GameScene_refreshMenu(gameScene); } -void PGB_GameScene_saveGame(PGB_GameScene *gameScene) { - - if(gameScene->state == PGB_GameSceneStateLoaded){ +void PGB_GameScene_saveGame(PGB_GameScene *gameScene) +{ + if(gameScene->state == PGB_GameSceneStateLoaded) + { PGB_GameSceneContext *context = gameScene->context; - if(gameScene->save_filename){ + if(gameScene->save_filename) + { write_cart_ram_file(gameScene->save_filename, &context->gb.gb_cart_ram, gb_get_save_size(&context->gb)); } } } -void PGB_GameScene_generateBitmask(void){ - - if(PGB_GameScene_bitmask_done){ +void PGB_GameScene_generateBitmask(void) +{ + if(PGB_GameScene_bitmask_done) + { return; } PGB_GameScene_bitmask_done = true; - for(int c = 0; c < 256; c++){ + for(int c = 0; c < 256; c++) + { int palette = c & 3; - for(int y = 0; y < 4; y++){ - + for(int y = 0; y < 4; y++) + { int x_offset = 0; - for(int i = 0; i < 4; i++){ - + for(int i = 0; i < 4; i++) + { int mask = 0x00; for(int x = 0; x < 2; x++){ - if(PGB_patterns[palette][y][x_offset + x] == 1){ + if(PGB_patterns[palette][y][x_offset + x] == 1) + { int n = i * 2 + x; mask |= (1 << (7 - n)); } @@ -702,7 +760,8 @@ void PGB_GameScene_generateBitmask(void){ x_offset += 2; - if(x_offset == 4){ + if(x_offset == 4) + { x_offset = 0; } } @@ -710,8 +769,8 @@ void PGB_GameScene_generateBitmask(void){ } } -void PGB_GameScene_free(void *object){ - +void PGB_GameScene_free(void *object) +{ PGB_GameScene *gameScene = object; PGB_GameSceneContext *context = gameScene->context; @@ -725,15 +784,18 @@ void PGB_GameScene_free(void *object){ pgb_free(gameScene->rom_filename); - if(gameScene->save_filename){ + if(gameScene->save_filename) + { pgb_free(gameScene->save_filename); } - if(context->rom){ + if(context->rom) + { pgb_free(context->rom); } - if(context->cart_ram){ + if(context->cart_ram) + { pgb_free(context->cart_ram); } diff --git a/src/library_scene.c b/src/library_scene.c index 7ee840d..ef8d887 100644 --- a/src/library_scene.c +++ b/src/library_scene.c @@ -18,8 +18,8 @@ static void PGB_LibraryScene_menu(void *object); static PDMenuItem *audioMenuItem; static PDMenuItem *fpsMenuItem; -PGB_LibraryScene* PGB_LibraryScene_new(void) { - +PGB_LibraryScene* PGB_LibraryScene_new(void) +{ PGB_Scene *scene = PGB_Scene_new(); PGB_LibraryScene *libraryScene = pgb_malloc(sizeof(PGB_LibraryScene)); @@ -45,29 +45,32 @@ PGB_LibraryScene* PGB_LibraryScene_new(void) { return libraryScene; } -void PGB_LibraryScene_listFiles(const char *filename, void *userdata) { - +void PGB_LibraryScene_listFiles(const char *filename, void *userdata) +{ PGB_LibraryScene *libraryScene = userdata; char *extension; char *dot = strrchr(filename, '.'); - if(!dot || dot == filename){ + if(!dot || dot == filename) + { extension = ""; } else { extension = dot + 1; } - if((strcmp(extension, "gb") == 0 || strcmp(extension, "gbc") == 0)){ + if((strcmp(extension, "gb") == 0 || strcmp(extension, "gbc") == 0)) + { PGB_Game *game = PGB_Game_new(filename); array_push(libraryScene->games, game); } } -void PGB_LibraryScene_reloadList(PGB_LibraryScene *libraryScene) { - - for(int i = 0; i < libraryScene->games->length; i++){ +void PGB_LibraryScene_reloadList(PGB_LibraryScene *libraryScene) +{ + for(int i = 0; i < libraryScene->games->length; i++) + { PGB_Game *game = libraryScene->games->items[i]; PGB_Game_free(game); } @@ -78,21 +81,24 @@ void PGB_LibraryScene_reloadList(PGB_LibraryScene *libraryScene) { PGB_Array *items = libraryScene->listView->items; - for(int i = 0; i < items->length; i++){ + for(int i = 0; i < items->length; i++) + { PGB_ListItem *item = items->items[i]; PGB_ListItem_free(item); } array_clear(items); - for(int i = 0; i < libraryScene->games->length; i++){ + for(int i = 0; i < libraryScene->games->length; i++) + { PGB_Game *game = libraryScene->games->items[i]; PGB_ListItemButton *itemButton = PGB_ListItemButton_new(game->filename); array_push(items, itemButton->item); } - if(items->length > 0){ + if(items->length > 0) + { libraryScene->tab = PGB_LibrarySceneTabList; } else { @@ -102,13 +108,14 @@ void PGB_LibraryScene_reloadList(PGB_LibraryScene *libraryScene) { PGB_ListView_reload(libraryScene->listView); } -void PGB_LibraryScene_update(void *object) { - +void PGB_LibraryScene_update(void *object) +{ PGB_LibraryScene *libraryScene = object; PGB_Scene_update(libraryScene->scene); - if(!libraryScene->firstLoad){ + if(!libraryScene->firstLoad) + { libraryScene->firstLoad = true; PGB_LibraryScene_reloadList(libraryScene); } @@ -116,7 +123,8 @@ void PGB_LibraryScene_update(void *object) { PDButtons released; playdate->system->getButtonState(NULL, NULL, &released); - if(released & kButtonA){ + if(released & kButtonA) + { int selectedItem = libraryScene->listView->selectedItem; if(selectedItem >= 0 && selectedItem < libraryScene->listView->items->length){ @@ -129,29 +137,31 @@ void PGB_LibraryScene_update(void *object) { bool needsDisplay = false; - if(libraryScene->model.empty || libraryScene->model.tab != libraryScene->tab){ + if(libraryScene->model.empty || libraryScene->model.tab != libraryScene->tab) + { needsDisplay = true; } libraryScene->model.empty = false; libraryScene->model.tab = libraryScene->tab; - if(needsDisplay){ + if(needsDisplay) + { playdate->graphics->clear(kColorWhite); } - if(libraryScene->tab == PGB_LibrarySceneTabList){ - + if(libraryScene->tab == PGB_LibrarySceneTabList) + { libraryScene->listView->needsDisplay = needsDisplay; libraryScene->listView->frame = PDRectMake(0, 0, playdate->display->getWidth(), playdate->display->getHeight()); PGB_ListView_update(libraryScene->listView); PGB_ListView_draw(libraryScene->listView); } - else if(libraryScene->tab == PGB_LibrarySceneTabEmpty){ - - if(needsDisplay){ - + else if(libraryScene->tab == PGB_LibrarySceneTabEmpty) + { + if(needsDisplay) + { static const char *title = "PlayGB"; static const char *message1 = "Connect to a computer and"; static const char *message2 = "copy games to Data/*.playgb/games"; @@ -184,24 +194,24 @@ void PGB_LibraryScene_update(void *object) { } } -void PGB_LibraryScene_didSelectRefresh(void *userdata){ - +void PGB_LibraryScene_didSelectRefresh(void *userdata) +{ PGB_LibraryScene *libraryScene = userdata; PGB_LibraryScene_reloadList(libraryScene); } -void PGB_LibraryScene_didChangeSound(void *userdata){ - +void PGB_LibraryScene_didChangeSound(void *userdata) +{ preferences_sound_enabled = playdate->system->getMenuItemValue(audioMenuItem); } -void PGB_LibraryScene_didChangeFPS(void *userdata){ - +void PGB_LibraryScene_didChangeFPS(void *userdata) +{ preferences_display_fps = playdate->system->getMenuItemValue(fpsMenuItem); } -void PGB_LibraryScene_menu(void *object) { - +void PGB_LibraryScene_menu(void *object) +{ PGB_LibraryScene *libraryScene = object; // playdate->system->addMenuItem("Refresh", PGB_LibraryScene_didSelectRefresh, libraryScene); @@ -210,20 +220,22 @@ void PGB_LibraryScene_menu(void *object) { fpsMenuItem = playdate->system->addCheckmarkMenuItem("Show FPS", preferences_display_fps, PGB_LibraryScene_didChangeFPS, libraryScene); } -void PGB_LibraryScene_free(void *object) { - +void PGB_LibraryScene_free(void *object) +{ PGB_LibraryScene *libraryScene = object; PGB_Scene_free(libraryScene->scene); - for(int i = 0; i < libraryScene->games->length; i++){ + for(int i = 0; i < libraryScene->games->length; i++) + { PGB_Game *game = libraryScene->games->items[i]; PGB_Game_free(game); } PGB_Array *items = libraryScene->listView->items; - for(int i = 0; i < items->length; i++){ + for(int i = 0; i < items->length; i++) + { PGB_ListItem *item = items->items[i]; PGB_ListItem_free(item); } @@ -235,7 +247,8 @@ void PGB_LibraryScene_free(void *object) { pgb_free(libraryScene); } -PGB_Game* PGB_Game_new(const char *filename) { +PGB_Game* PGB_Game_new(const char *filename) +{ PGB_Game *game = pgb_malloc(sizeof(PGB_Game)); game->filename = string_copy(filename); @@ -246,8 +259,8 @@ PGB_Game* PGB_Game_new(const char *filename) { return game; } -void PGB_Game_free(PGB_Game *game) { - +void PGB_Game_free(PGB_Game *game) +{ pgb_free(game->filename); pgb_free(game->fullpath); diff --git a/src/listview.c b/src/listview.c index ab82490..ab1370b 100644 --- a/src/listview.c +++ b/src/listview.c @@ -24,7 +24,8 @@ static float PGB_ListView_repeatInterval2 = 2; static float PGB_ListView_crankResetMinTime = 2; static float PGB_ListView_crankMinChange = 30; -PGB_ListView* PGB_ListView_new(void) { +PGB_ListView* PGB_ListView_new(void) +{ PGB_ListView *listView = pgb_malloc(sizeof(PGB_ListView)); listView->items = array_new(); listView->frame = PDRectMake(0, 0, 200, 200); @@ -66,11 +67,13 @@ PGB_ListView* PGB_ListView_new(void) { return listView; } -void PGB_ListView_invalidateLayout(PGB_ListView *listView){ +void PGB_ListView_invalidateLayout(PGB_ListView *listView) +{ int y = 0; - for(int i = 0; i < listView->items->length; i++){ + for(int i = 0; i < listView->items->length; i++) + { PGB_ListItem *item = listView->items->items[i]; item->offsetY = y; y += item->height; @@ -81,13 +84,15 @@ void PGB_ListView_invalidateLayout(PGB_ListView *listView){ int scrollHeight = listView->frame.height - PGB_ListView_scrollInset * 2; bool indicatorVisible = false; - if(listView->contentSize > listView->frame.height){ + if(listView->contentSize > listView->frame.height) + { indicatorVisible = true; } listView->scroll.indicatorVisible = indicatorVisible; float indicatorHeight = 0; - if(listView->contentSize > listView->frame.height && listView->frame.height != 0){ + if(listView->contentSize > listView->frame.height && listView->frame.height != 0) + { indicatorHeight = PGB_MAX(scrollHeight * (listView->frame.height / listView->contentSize), PGB_ListView_scrollIndicatorMinHeight); } listView->scroll.indicatorHeight = indicatorHeight; @@ -99,15 +104,19 @@ void PGB_ListView_reload(PGB_ListView *listView){ int numberOfItems = listView->items->length; - if(numberOfItems > 0){ - if(listView->selectedItem < 0){ + if(numberOfItems > 0) + { + if(listView->selectedItem < 0) + { PGB_ListView_selectItem(listView, 0, false); } - else if(listView->selectedItem >= numberOfItems){ + else if(listView->selectedItem >= numberOfItems) + { PGB_ListView_selectItem(listView, numberOfItems - 1, false); } } - else { + else + { listView->scroll.active = false; listView->contentOffset = 0; listView->selectedItem = -1; @@ -122,44 +131,55 @@ void PGB_ListView_update(PGB_ListView *listView){ PDButtons pressed; playdate->system->getButtonState(&pressed, &pushed, NULL); - if(pushed & kButtonDown){ + if(pushed & kButtonDown) + { int nextIndex = listView->selectedItem + 1; - if(nextIndex >= 0 && nextIndex < listView->items->length){ + if(nextIndex >= 0 && nextIndex < listView->items->length) + { PGB_ListView_selectItem(listView, nextIndex, true); } } - else if(pushed & kButtonUp){ + else if(pushed & kButtonUp) + { int prevIndex = listView->selectedItem - 1; - if(prevIndex >= 0 && prevIndex < listView->items->length){ + if(prevIndex >= 0 && prevIndex < listView->items->length) + { PGB_ListView_selectItem(listView, prevIndex, true); } } listView->crankChange += PGB_App->crankChange; - if(listView->crankChange != 0){ + if(listView->crankChange != 0) + { listView->crankResetTime += PGB_App->dt; } - else { + else + { listView->crankResetTime = 0; } - if(listView->crankChange > 0 && listView->crankChange >= PGB_ListView_crankMinChange){ + if(listView->crankChange > 0 && listView->crankChange >= PGB_ListView_crankMinChange) + { int nextIndex = listView->selectedItem + 1; - if(nextIndex >= 0 && nextIndex < listView->items->length){ + if(nextIndex >= 0 && nextIndex < listView->items->length) + { PGB_ListView_selectItem(listView, nextIndex, true); listView->crankChange = 0; } } - else if(listView->crankChange < 0 && listView->crankChange <= (-PGB_ListView_crankMinChange)){ + else if(listView->crankChange < 0 && listView->crankChange <= (-PGB_ListView_crankMinChange)) + { int prevIndex = listView->selectedItem - 1; - if(prevIndex >= 0 && prevIndex < listView->items->length){ + if(prevIndex >= 0 && prevIndex < listView->items->length) + { PGB_ListView_selectItem(listView, prevIndex, true); listView->crankChange = 0; } } - if(listView->crankResetTime > PGB_ListView_crankResetMinTime){ + if(listView->crankResetTime > PGB_ListView_crankResetMinTime) + { listView->crankResetTime = 0; listView->crankChange = 0; } @@ -167,55 +187,69 @@ void PGB_ListView_update(PGB_ListView *listView){ PGB_ListViewDirection old_direction = listView->direction; listView->direction = PGB_ListViewDirectionNone; - if(pressed & kButtonUp){ + if(pressed & kButtonUp) + { listView->direction = PGB_ListViewDirectionUp; } - else if(pressed & kButtonDown){ + else if(pressed & kButtonDown) + { listView->direction = PGB_ListViewDirectionDown; } - if(listView->direction == PGB_ListViewDirectionNone || listView->direction != old_direction){ + if(listView->direction == PGB_ListViewDirectionNone || listView->direction != old_direction) + { listView->repeatIncrementTime = 0; listView->repeatLevel = 0; listView->repeatTime = 0; } - else { + else + { listView->repeatIncrementTime += PGB_App->dt; float repeatInterval = PGB_ListView_repeatInterval1; - if(listView->repeatLevel > 0){ + if(listView->repeatLevel > 0) + { repeatInterval = PGB_ListView_repeatInterval2; } - if(listView->repeatIncrementTime >= repeatInterval){ + if(listView->repeatIncrementTime >= repeatInterval) + { listView->repeatLevel = PGB_MIN(3, listView->repeatLevel + 1); listView->repeatIncrementTime = fmodf(listView->repeatIncrementTime, repeatInterval); } - if(listView->repeatLevel > 0){ + if(listView->repeatLevel > 0) + { listView->repeatTime += PGB_App->dt; float repeatRate = 0.16; - if(listView->repeatLevel == 2){ + if(listView->repeatLevel == 2) + { repeatRate = 0.1; } - else if(listView->repeatLevel == 3){ + else if(listView->repeatLevel == 3) + { repeatRate = 0.05; } - if(listView->repeatTime >= repeatRate){ + if(listView->repeatTime >= repeatRate) + { listView->repeatTime = fmodf(listView->repeatTime, repeatRate); - if(listView->direction == PGB_ListViewDirectionUp){ + if(listView->direction == PGB_ListViewDirectionUp) + { int prevIndex = listView->selectedItem - 1; - if(prevIndex >= 0 && prevIndex < listView->items->length){ + if(prevIndex >= 0 && prevIndex < listView->items->length) + { PGB_ListView_selectItem(listView, prevIndex, true); } } - else if(listView->direction == PGB_ListViewDirectionDown){ + else if(listView->direction == PGB_ListViewDirectionDown) + { int nextIndex = listView->selectedItem + 1; - if(nextIndex >= 0 && nextIndex < listView->items->length){ + if(nextIndex >= 0 && nextIndex < listView->items->length) + { PGB_ListView_selectItem(listView, nextIndex, true); } } @@ -223,32 +257,35 @@ void PGB_ListView_update(PGB_ListView *listView){ } } - if(listView->scroll.active){ + if(listView->scroll.active) + { listView->scroll.time += PGB_App->dt; float progress = pgb_easeInOutQuad(fminf(1, listView->scroll.time / listView->scroll.duration)); listView->contentOffset = listView->scroll.start + (listView->scroll.end - listView->scroll.start) * progress; - if(listView->scroll.time >= listView->scroll.duration){ + if(listView->scroll.time >= listView->scroll.duration) + { listView->scroll.time = 0; listView->scroll.active = false; } } float indicatorOffset = PGB_ListView_scrollInset; - if(listView->contentSize > listView->frame.height){ + if(listView->contentSize > listView->frame.height) + { int scrollHeight = listView->frame.height - (PGB_ListView_scrollInset * 2 + listView->scroll.indicatorHeight); indicatorOffset = PGB_ListView_scrollInset + (listView->contentOffset / (listView->contentSize - listView->frame.height)) * scrollHeight; } listView->scroll.indicatorOffset = indicatorOffset; } -void PGB_ListView_draw(PGB_ListView *listView){ - +void PGB_ListView_draw(PGB_ListView *listView) +{ bool needsDisplay = false; - if(listView->model.empty || listView->needsDisplay || listView->model.selectedItem != listView->selectedItem || listView->model.contentOffset != listView->contentOffset || listView->model.scrollIndicatorVisible != listView->scroll.indicatorVisible || listView->model.scrollIndicatorOffset != listView->scroll.indicatorOffset || listView->scroll.indicatorHeight != listView->scroll.indicatorHeight){ - + if(listView->model.empty || listView->needsDisplay || listView->model.selectedItem != listView->selectedItem || listView->model.contentOffset != listView->contentOffset || listView->model.scrollIndicatorVisible != listView->scroll.indicatorVisible || listView->model.scrollIndicatorOffset != listView->scroll.indicatorOffset || listView->scroll.indicatorHeight != listView->scroll.indicatorHeight) + { needsDisplay = true; } @@ -261,31 +298,36 @@ void PGB_ListView_draw(PGB_ListView *listView){ listView->model.scrollIndicatorOffset = listView->scroll.indicatorOffset; listView->model.scrollIndicatorHeight = listView->scroll.indicatorHeight; - if(needsDisplay){ - + if(needsDisplay) + { int listX = listView->frame.x; int listY = listView->frame.y; playdate->graphics->fillRect(listX, listY, listView->frame.width, listView->frame.height, kColorWhite); - for(int i = 0; i < listView->items->length; i++){ + for(int i = 0; i < listView->items->length; i++) + { PGB_ListItem *item = listView->items->items[i]; int rowY = listY + item->offsetY - listView->contentOffset; bool selected = (i == listView->selectedItem); - if(selected){ + if(selected) + { playdate->graphics->fillRect(listX, rowY, listView->frame.width, item->height, kColorBlack); } - if(item->type == PGB_ListViewItemTypeButton){ + if(item->type == PGB_ListViewItemTypeButton) + { PGB_ListItemButton *itemButton = item->object; - if(selected){ + if(selected) + { playdate->graphics->setDrawMode(kDrawModeFillWhite); } - else { + else + { playdate->graphics->setDrawMode(kDrawModeFillBlack); } @@ -299,7 +341,8 @@ void PGB_ListView_draw(PGB_ListView *listView){ } } - if(listView->scroll.indicatorVisible){ + if(listView->scroll.indicatorVisible) + { int indicatorLineWidth = 1; PDRect indicatorFillRect = PDRectMake(listView->frame.width - PGB_ListView_scrollInset - PGB_ListView_scrollIndicatorWidth, listView->scroll.indicatorOffset, PGB_ListView_scrollIndicatorWidth, listView->scroll.indicatorHeight); @@ -319,13 +362,15 @@ void PGB_ListView_selectItem(PGB_ListView *listView, unsigned int index, bool an int centeredOffset = 0; - if(listView->contentSize > listHeight){ + if(listView->contentSize > listHeight) + { centeredOffset = item->offsetY - ((float)listHeight / 2 - (float)PGB_ListView_rowHeight / 2); centeredOffset = PGB_MAX(0, centeredOffset); centeredOffset = PGB_MIN(centeredOffset, listView->contentSize - listHeight); } - if(animated){ + if(animated) + { listView->scroll.active = true; listView->scroll.start = listView->contentOffset; listView->scroll.end = centeredOffset; @@ -339,18 +384,21 @@ void PGB_ListView_selectItem(PGB_ListView *listView, unsigned int index, bool an listView->selectedItem = index; } -void PGB_ListView_free(PGB_ListView *listView){ +void PGB_ListView_free(PGB_ListView *listView) +{ array_free(listView->items); pgb_free(listView); } -PGB_ListItem* PGB_ListItem_new(void) { +PGB_ListItem* PGB_ListItem_new(void) +{ PGB_ListItem *item = pgb_malloc(sizeof(PGB_ListItem)); return item; } -PGB_ListItemButton* PGB_ListItemButton_new(char *title) { +PGB_ListItemButton* PGB_ListItemButton_new(char *title) +{ PGB_ListItem *item = PGB_ListItem_new(); @@ -367,21 +415,21 @@ PGB_ListItemButton* PGB_ListItemButton_new(char *title) { return buttonItem; } -void PGB_ListItem_super_free(PGB_ListItem *item){ - +void PGB_ListItem_super_free(PGB_ListItem *item) +{ pgb_free(item); } -void PGB_ListItemButton_free(PGB_ListItemButton *itemButton){ - +void PGB_ListItemButton_free(PGB_ListItemButton *itemButton) +{ PGB_ListItem_super_free(itemButton->item); pgb_free(itemButton->title); pgb_free(itemButton); } -void PGB_ListItem_free(PGB_ListItem *item){ - +void PGB_ListItem_free(PGB_ListItem *item) +{ if(item->type == PGB_ListViewItemTypeButton){ PGB_ListItemButton_free(item->object); } diff --git a/src/preferences.c b/src/preferences.c index 271f50c..c9966f8 100644 --- a/src/preferences.c +++ b/src/preferences.c @@ -15,26 +15,30 @@ static SDFile *pref_file; bool preferences_sound_enabled = false; bool preferences_display_fps = false; -void cpu_endian_to_big_endian(unsigned char *src, unsigned char *buffer, size_t size, size_t len); +static void cpu_endian_to_big_endian(unsigned char *src, unsigned char *buffer, size_t size, size_t len); static uint8_t prefereces_read_uint8(void); static void prefereces_write_uint8(uint8_t value); static uint32_t prefereces_read_uint32(void); static void prefereces_write_uint32(uint32_t value); -void prefereces_init(void){ - - if(playdate->file->stat(pref_filename, NULL) != 0){ +void prefereces_init(void) +{ + if(playdate->file->stat(pref_filename, NULL) != 0) + { prefereces_save_to_disk(); } - else { + else + { prefereces_read_from_disk(); } } -void prefereces_read_from_disk(void){ +void prefereces_read_from_disk(void) +{ pref_file = playdate->file->open(pref_filename, kFileReadData); - if(pref_file){ + if(pref_file) + { // read model version prefereces_read_uint32(); @@ -45,8 +49,8 @@ void prefereces_read_from_disk(void){ } } -void prefereces_save_to_disk(void){ - +void prefereces_save_to_disk(void) +{ pref_file = playdate->file->open(pref_filename, kFileWrite); prefereces_write_uint32(pref_version); @@ -57,41 +61,49 @@ void prefereces_save_to_disk(void){ playdate->file->close(pref_file); } -uint8_t prefereces_read_uint8(void){ +static uint8_t prefereces_read_uint8(void) +{ uint8_t buffer[1]; playdate->file->read(pref_file, buffer, sizeof(uint8_t)); return buffer[0]; } -void prefereces_write_uint8(uint8_t value){ +static void prefereces_write_uint8(uint8_t value) +{ playdate->file->write(pref_file, &value, sizeof(uint8_t)); } -uint32_t prefereces_read_uint32(void){ +static uint32_t prefereces_read_uint32(void) +{ unsigned char buffer[sizeof(uint32_t)]; playdate->file->read(pref_file, buffer, sizeof(uint32_t)); return buffer[0] << 24 | buffer[1] << 16 | buffer[2] << 8 | buffer[3]; } -void prefereces_write_uint32(uint32_t value){ +static void prefereces_write_uint32(uint32_t value) +{ unsigned char buffer[sizeof(uint32_t)]; cpu_endian_to_big_endian((unsigned char*)&value, buffer, sizeof(uint32_t), 1); playdate->file->write(pref_file, buffer, sizeof(uint32_t)); } -void cpu_endian_to_big_endian(unsigned char *src, unsigned char *buffer, size_t size, size_t len){ - +static void cpu_endian_to_big_endian(unsigned char *src, unsigned char *buffer, size_t size, size_t len) +{ int x = 1; - if(*((char*)&x) == 1){ + if(*((char*)&x) == 1) + { // little endian machine, swap - for(size_t i = 0; i < len; i++){ - for (size_t ix = 0; ix < size; ix++){ + for(size_t i = 0; i < len; i++) + { + for (size_t ix = 0; ix < size; ix++) + { buffer[size * i + ix] = src[size * i + (size - 1 - ix)]; } } } - else { + else + { memcpy(buffer, src, size * len); } } diff --git a/src/scene.c b/src/scene.c index 41308b0..c75f5f9 100644 --- a/src/scene.c +++ b/src/scene.c @@ -9,7 +9,8 @@ static void PGB_Scene_menu(void *object); -PGB_Scene* PGB_Scene_new(void) { +PGB_Scene* PGB_Scene_new(void) +{ PGB_Scene *scene = pgb_malloc(sizeof(PGB_Scene)); scene->update = PGB_Scene_update; @@ -22,16 +23,18 @@ PGB_Scene* PGB_Scene_new(void) { return scene; } -void PGB_Scene_update(void *object) { +void PGB_Scene_update(void *object) +{ } -void PGB_Scene_menu(void *object) { +void PGB_Scene_menu(void *object) +{ } -void PGB_Scene_free(void *object) { - +void PGB_Scene_free(void *object) +{ PGB_Scene *scene = object; pgb_free(scene); } diff --git a/src/utility.c b/src/utility.c index 26ffcef..fe4357c 100644 --- a/src/utility.c +++ b/src/utility.c @@ -39,31 +39,37 @@ const uint8_t PGB_patterns[4][4][4] = { } }; -char* string_copy(const char *string){ +char* string_copy(const char *string) +{ char *copied = pgb_malloc(strlen(string) + 1); strcpy(copied, string); return copied; } -char* pgb_save_filename(const char *path, bool isRecovery){ +char* pgb_save_filename(const char *path, bool isRecovery) +{ char *filename; char *slash = strrchr(path, '/'); - if(!slash){ + if(!slash) + { filename = (char*)path; } - else { + else + { filename = slash + 1; } size_t len; char *dot = strrchr(filename, '.'); - if(!dot || dot == filename){ + if(!dot || dot == filename) + { len = strlen(filename); } - else { + else + { len = strlen(filename) - strlen(dot); } @@ -72,7 +78,8 @@ char* pgb_save_filename(const char *path, bool isRecovery){ strncat(filenameNoExt, filename, len); char *suffix = ""; - if(isRecovery){ + if(isRecovery) + { suffix = " (recovery)"; } @@ -84,21 +91,24 @@ char* pgb_save_filename(const char *path, bool isRecovery){ return buffer; } -char* pgb_extract_fs_error_code(const char *fileError){ +char* pgb_extract_fs_error_code(const char *fileError) +{ char *findStr = "uC-FS error: "; char *fsErrorPtr = strstr(fileError, findStr); - if(fsErrorPtr){ + if(fsErrorPtr) + { return fsErrorPtr + strlen(findStr); } return NULL; } -float pgb_easeInOutQuad(float x){ +float pgb_easeInOutQuad(float x) +{ return (x < 0.5f) ? 2 * x * x : 1 - powf(-2 * x + 2, 2) * 0.5f; } -void pgb_fillRoundRect(PDRect rect, int radius, LCDColor color) { - +void pgb_fillRoundRect(PDRect rect, int radius, LCDColor color) +{ int r2 = radius * 2; playdate->graphics->fillRect(rect.x, rect.y + radius, radius, rect.height - r2, color); @@ -111,8 +121,8 @@ void pgb_fillRoundRect(PDRect rect, int radius, LCDColor color) { playdate->graphics->fillEllipse(rect.x, rect.y + rect.height - r2, r2, r2, -180, -90, color); } -void pgb_drawRoundRect(PDRect rect, int radius, int lineWidth, LCDColor color){ - +void pgb_drawRoundRect(PDRect rect, int radius, int lineWidth, LCDColor color) +{ int r2 = radius * 2; playdate->graphics->fillRect(rect.x, rect.y + radius, lineWidth, rect.height - r2, color); @@ -126,18 +136,24 @@ void pgb_drawRoundRect(PDRect rect, int radius, int lineWidth, LCDColor color){ playdate->graphics->drawEllipse(rect.x, rect.y + rect.height - r2, r2, r2, lineWidth, -180, -90, color); } -void* pgb_malloc(size_t size) { +void* pgb_malloc(size_t size) +{ return playdate->system->realloc(NULL, size); } -void* pgb_realloc(void *ptr, size_t size) { +void* pgb_realloc(void *ptr, size_t size) +{ return playdate->system->realloc(ptr, size); } -void* pgb_calloc(size_t count, size_t size) { +void* pgb_calloc(size_t count, size_t size) +{ return memset(pgb_malloc(count * size), 0, count * size); } -void pgb_free(void *ptr) { - playdate->system->realloc(ptr, 0); +void pgb_free(void *ptr) +{ + if(ptr){ + playdate->system->realloc(ptr, 0); + } }