| 1 | // (_||_/ |
| 2 | // ( ) |
| 3 | // ( o 0 ) |
| 4 | //-OOO°--(_)---°OOO--------------------------------------- |
| 5 | // Copyright (C) 2006 By Dominique Roux-Serret |
| 6 | // .OOOo oOOO. [email protected] |
| 7 | //-( )------( )--------------------------------------- |
| 8 | // ( ( ) / Copyright (C) 2023 By Johnny Jazeix |
| 9 | // (_) (_/ [email protected] |
| 10 | |
| 11 | // This program is free software; you can redistribute it and/or modify |
| 12 | // it under the terms of the GNU General Public License as published by |
| 13 | // the Free Software Foundation; either version 2 or version 3 of the License. |
| 14 | |
| 15 | // This program is distributed in the hope that it will be useful, |
| 16 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | // GNU General Public License for more details. |
| 19 | |
| 20 | // You should have received a copy of the GNU General Public License along |
| 21 | // with this program; if not, write to the Free Software Foundation, Inc., |
| 22 | // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 23 | |
| 24 | #include <cstdlib> |
| 25 | #include <cstring> |
| 26 | #include <SDL2/SDL_error.h> // for SDL_GetError |
| 27 | #include <SDL2/SDL_hints.h> // for SDL_SetHint, SDL_HINT_RENDER_SCALE_QUALITY |
| 28 | #include <SDL2/SDL_log.h> // for SDL_LogError, SDL_LOG_CATEGORY_APPLICATION |
| 29 | #include <SDL2/SDL_mouse.h> // for SDL_ShowCursor |
| 30 | #include <SDL2/SDL_render.h> // for SDL_CreateRenderer, SDL_DestroyRenderer |
| 31 | #include <SDL2/SDL_timer.h> // for SDL_GetTicks |
| 32 | #include <SDL2/SDL_video.h> // for SDL_CreateWindow, SDL_DestroyWindow |
| 33 | #include <SDL2/SDL.h> |
| 34 | #include <SDL2/SDL_mixer.h> |
| 35 | |
| 36 | #include "config.h" |
| 37 | #include "preference.h" |
| 38 | #include "game.h" |
| 39 | #include "gamepad.h" |
| 40 | #include "audio.h" |
| 41 | #include "sprite.h" |
| 42 | #include "screen.h" |
| 43 | #include "mouse.h" |
| 44 | #include "menu.h" |
| 45 | #include "level.h" |
| 46 | #include "editor.h" |
| 47 | #include "utils.h" |
| 48 | |
| 49 | /*** Variables globales ***/ |
| 50 | /************************/ |
| 51 | SDL_Window *sdlWindow; // Pointe sur l'écran video |
| 52 | SDL_Renderer *sdlRenderer; // Pointe sur l'écran video |
| 53 | |
| 54 | char Titre[] = "Li-ri V" VERSION; |
| 55 | |
| 56 | Sprite *Sprites = nullptr; // Pointe sur les sprites |
| 57 | int NSprites = 0; // Nombre de sprites en mémoire |
| 58 | Screen Ec; // Pointe sur les 2 buffets vidéo |
| 59 | sNewPreference Pref; // Tableau des préférences. |
| 60 | Level level; // Gère les niveaux |
| 61 | |
| 62 | int Horloge = 0; // Horloges du jeu |
| 63 | int HorlogeAvant = 0; |
| 64 | |
| 65 | #if defined(__unix__) || defined(__HAIKU__) |
| 66 | char DefPath[256]; // Chemin par defaut dans arg |
| 67 | #endif |
| 68 | |
| 69 | /*** Initialise les preferences ***/ |
| 70 | /**********************************/ |
| 71 | void InitPref() |
| 72 | { |
| 73 | #if defined(__unix__) || defined(__HAIKU__) |
| 74 | DefPath[0] = 0; |
| 75 | #endif |
| 76 | |
| 77 | for (int i = 0; i < 8; i++) { // Vide les scores |
| 78 | Pref.Sco[i].Score = 0; |
| 79 | Pref.Sco[i].Name[0] = 0; |
| 80 | } |
| 81 | |
| 82 | Utils::LoadPref(); |
| 83 | } |
| 84 | |
| 85 | /*** Preogramme principale ***/ |
| 86 | /*****************************/ |
| 87 | int main(int narg, char *argv[]) |
| 88 | { |
| 89 | int i; |
| 90 | eMenu RetM, RetMenu = mMenu; |
| 91 | |
| 92 | // Initialise les préferences |
| 93 | InitPref(); |
| 94 | #if defined(__unix__) || defined(__HAIKU__) |
| 95 | if (narg > 1) { |
| 96 | strcpy(DefPath, argv[1]); |
| 97 | } |
| 98 | #endif |
| 99 | |
| 100 | SDL_SetHint(SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS, "1"); |
| 101 | // Initiliase SDL |
| 102 | if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_GAMECONTROLLER) < 0) { |
| 103 | SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Unable to initialize SDL: %s", SDL_GetError()); |
| 104 | exit(-1); |
| 105 | } |
| 106 | // Ferme le programme correctement quand quit |
| 107 | atexit(SDL_Quit); |
| 108 | |
| 109 | // Demande la resolution Video |
| 110 | int vOption = SDL_WINDOW_RESIZABLE; |
| 111 | if (Pref.FullScreen) { |
| 112 | vOption |= SDL_WINDOW_FULLSCREEN_DESKTOP; |
| 113 | } |
| 114 | |
| 115 | sdlWindow = SDL_CreateWindow(Titre, 0, 0, 800, 600, vOption); |
| 116 | sdlRenderer = SDL_CreateRenderer(sdlWindow, -1, 0); |
| 117 | SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "linear"); |
| 118 | SDL_RenderSetLogicalSize(sdlRenderer, 800, 600); |
| 119 | |
| 120 | SDL_ShowCursor(0); // Masque le curseur |
| 121 | |
| 122 | Audio audio; |
| 123 | audio.Init(); |
| 124 | if (LoadSprites() == false) { |
| 125 | exit(-1); |
| 126 | } |
| 127 | if (level.Load() == false) { |
| 128 | exit(-1); |
| 129 | } |
| 130 | |
| 131 | audio.PlayMusic(); |
| 132 | Mouse mouse { audio }; |
| 133 | mouse.InitStart(); |
| 134 | |
| 135 | Gamepad gamepad; |
| 136 | gamepad.Initialize(); |
| 137 | |
| 138 | Game game { audio, gamepad }; |
| 139 | Editor editor { mouse, game, gamepad }; |
| 140 | |
| 141 | Menu MainMenu { game, audio, mouse, gamepad }; |
| 142 | game.setMenu(&MainMenu); |
| 143 | |
| 144 | HorlogeAvant = Horloge = SDL_GetTicks(); |
| 145 | srand(SDL_GetTicks()); |
| 146 | |
| 147 | // ask locale if first run |
| 148 | if (Pref.Langue == -1) { |
| 149 | RetMenu = MainMenu.SDLMain_Language(); |
| 150 | LoadLangue(); |
| 151 | } |
| 152 | |
| 153 | // menu switch |
| 154 | do { |
| 155 | switch (RetMenu) { |
| 156 | case mMenu: |
| 157 | RetM = MainMenu.SDLMain(); |
| 158 | break; |
| 159 | case mLangue: |
| 160 | RetM = MainMenu.SDLMain_Language(); |
| 161 | break; |
| 162 | case mOption: |
| 163 | RetM = MainMenu.SDLMain_Options(); |
| 164 | break; |
| 165 | case mScoreEdit: |
| 166 | RetM = MainMenu.SDLMain_Score(true); |
| 167 | break; |
| 168 | case mScore: |
| 169 | RetM = MainMenu.SDLMain_Score(); |
| 170 | break; |
| 171 | case mMenuSpeed: |
| 172 | RetM = MainMenu.SDLMain_Speed(); |
| 173 | break; |
| 174 | case mMenuNiveau: |
| 175 | RetM = MainMenu.SDLMain_Level(); |
| 176 | break; |
| 177 | case mGame: |
| 178 | audio.LoadMusic(1); |
| 179 | RetM = game.SDLMain(); |
| 180 | audio.LoadMusic(0); |
| 181 | break; |
| 182 | case mEdit: |
| 183 | RetM = editor.SDLMain(0); |
| 184 | break; |
| 185 | default: |
| 186 | RetM = mQuit; |
| 187 | } |
| 188 | RetMenu = RetM; |
| 189 | } while (RetMenu != mQuit); |
| 190 | |
| 191 | // close program |
| 192 | Mix_HaltMusic(); |
| 193 | Mix_FreeMusic(audio.Music); |
| 194 | |
| 195 | for (i = 0; i < NSprites; i++) { |
| 196 | Sprites[i].Delete(); |
| 197 | } |
| 198 | delete[] Sprites; |
| 199 | |
| 200 | Utils::SauvePref(); |
| 201 | SDL_DestroyRenderer(sdlRenderer); |
| 202 | SDL_DestroyWindow(sdlWindow); |
| 203 | |
| 204 | Mix_Quit(); |
| 205 | SDL_Quit(); |
| 206 | return 0; |
| 207 | } |