| 1 | |
| 2 | /** |
| 3 | * |
| 4 | * @file filemenu.cpp |
| 5 | * |
| 6 | * Part of the OpenJazz project |
| 7 | * |
| 8 | * @par Licence: |
| 9 | * Copyright (c) 2015-2026 Carsten Teibes |
| 10 | * |
| 11 | * OpenJazz is distributed under the terms of |
| 12 | * the GNU General Public License, version 2.0 |
| 13 | * |
| 14 | * @par Description: |
| 15 | * Deals with the running of the menus used save and load a game. |
| 16 | * |
| 17 | */ |
| 18 | |
| 19 | #include "menu.h" |
| 20 | |
| 21 | #include "io/controls.h" |
| 22 | #include "io/gfx/font.h" |
| 23 | #include "io/gfx/video.h" |
| 24 | #include "io/sound.h" |
| 25 | #include "jj1/save/jj1save.h" |
| 26 | #include "loop.h" |
| 27 | #include "util.h" |
| 28 | #include <memory> |
| 29 | |
| 30 | int FileMenu::main (bool forSaving, bool showCustom) { |
| 31 | int x, y; |
| 32 | int chosen = 0; |
| 33 | std::unique_ptr<JJ1Save> save[4]; |
| 34 | int options = showCustom ? 5 : 4; |
| 35 | |
| 36 | // load save games |
| 37 | |
| 38 | char* fileName = createString("SAVE.0"); |
| 39 | for (int i = 0; i < 4; i++) { |
| 40 | save[i] = std::make_unique<JJ1Save>(fileName); |
| 41 | fileName[5]++; |
| 42 | } |
| 43 | delete[] fileName; |
| 44 | |
| 45 | const char *loadGameOptions[5] = {save[0]->name, save[1]->name, save[2]->name, save[3]->name, "custom"}; |
| 46 | |
| 47 | auto fileChosen = [&]() -> bool { |
| 48 | if (chosen < 4 && !forSaving) { |
| 49 | if(!save[chosen]->valid) { |
| 50 | playSound(SE::WAIT); |
| 51 | |
| 52 | return false; |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | playConfirmSound(); |
| 57 | return true; |
| 58 | }; |
| 59 | |
| 60 | video.setPalette(menuPalette); |
| 61 | |
| 62 | while (true) { |
| 63 | |
| 64 | if (loop(NORMAL_LOOP) == E_QUIT) return E_QUIT; |
| 65 | |
| 66 | if (controls.release(C_ESCAPE)) return E_RETURN; |
| 67 | |
| 68 | if (controls.release(C_UP)) chosen = (chosen + options - 1) % options; |
| 69 | |
| 70 | if (controls.release(C_DOWN)) chosen = (chosen + 1) % options; |
| 71 | |
| 72 | if (controls.release(C_ENTER)) { |
| 73 | if(fileChosen()) |
| 74 | return chosen; |
| 75 | } |
| 76 | |
| 77 | if (controls.getCursor(x, y)) { |
| 78 | |
| 79 | if ((x < 100) && (y >= canvasH - 12) && controls.wasCursorReleased()) return E_RETURN; |
| 80 | |
| 81 | x -= canvasW >> 2; |
| 82 | y -= (canvasH >> 1) - (options << 3); |
| 83 | |
| 84 | if ((x >= 0) && (x < 256) && (y >= 0) && (y < (options << 4))) { |
| 85 | |
| 86 | chosen = y >> 4; |
| 87 | |
| 88 | if (controls.wasCursorReleased()) { |
| 89 | if(fileChosen()) |
| 90 | return chosen; |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | } |
| 95 | |
| 96 | SDL_Delay(T_MENU_FRAME); |
| 97 | |
| 98 | video.clearScreen(0); |
| 99 | |
| 100 | fontmn2->showString(forSaving ? "SAVE GAME" : "LOAD GAME", canvasW >> 1, (canvasH >> 1) - (options << 3) - 32, alignX::Center); |
| 101 | |
| 102 | for (int i = 0; i < options; i++) { |
| 103 | |
| 104 | if (i == chosen) fontmn2->mapPalette(240, 8, 114, 16); |
| 105 | |
| 106 | fontmn2->showString(loadGameOptions[i], (canvasW >> 1) - 80, |
| 107 | (canvasH >> 1) + (i << 4) - (options << 3)); |
| 108 | |
| 109 | if (i == chosen) fontmn2->restorePalette(); |
| 110 | |
| 111 | } |
| 112 | |
| 113 | showEscString(); |
| 114 | } |
| 115 | |
| 116 | return E_NONE; |
| 117 | } |