| 1 | #define SDL_MAIN_HANDLED |
| 2 | #include <SDL2/SDL.h> |
| 3 | #include <SDL2/SDL_ttf.h> |
| 4 | #include <SDL2/SDL_mixer.h> |
| 5 | #include <iostream> |
| 6 | |
| 7 | #include "src/render.hh" |
| 8 | #include "src/ball.hh" |
| 9 | #include "src/player.hh" |
| 10 | #include "src/brick.hh" |
| 11 | |
| 12 | #include "font.h" |
| 13 | |
| 14 | #if defined(_WIN32) || defined(_WIN64) |
| 15 | int WinMain(void) { |
| 16 | #else |
| 17 | int main(void) { |
| 18 | #endif |
| 19 | Render r; |
| 20 | Player p(r); |
| 21 | Ball bl(r, p); |
| 22 | Brick br(r); |
| 23 | |
| 24 | if (SDL_Init(SDL_INIT_EVERYTHING) < 0) { |
| 25 | std::cout << "SDL_Init()に失敗" << '\n'; |
| 26 | return -1; |
| 27 | } |
| 28 | |
| 29 | if (SDL_CreateWindowAndRenderer(r.width, r.height, 0, &r.window, &r.renderer) < 0) { |
| 30 | std::cout << "SDL_CreateWindowAndRenderer()に失敗" << '\n'; |
| 31 | SDL_Quit(); |
| 32 | return -1; |
| 33 | } |
| 34 | |
| 35 | if (Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 2048) < 0) { |
| 36 | std::cout << "Mix_OpenAudio()に失敗" << '\n'; |
| 37 | SDL_DestroyRenderer(r.renderer); |
| 38 | SDL_DestroyWindow(r.window); |
| 39 | SDL_Quit(); |
| 40 | return -1; |
| 41 | } |
| 42 | |
| 43 | SDL_SetWindowTitle(r.window, "ブリックブレイカー"); |
| 44 | |
| 45 | TTF_Init(); |
| 46 | r.font = TTF_OpenFontRW(SDL_RWFromMem(__font_ttf, __font_ttf_len), 1, r.fontsize); |
| 47 | if (!r.font) { |
| 48 | std::cout << "フォントを読込に失敗" << '\n'; |
| 49 | SDL_DestroyRenderer(r.renderer); |
| 50 | SDL_DestroyWindow(r.window); |
| 51 | Mix_CloseAudio(); |
| 52 | SDL_Quit(); |
| 53 | return -1; |
| 54 | } |
| 55 | |
| 56 | static int lastTime = 0; |
| 57 | br.resetBricks(r, p, bl); |
| 58 | |
| 59 | while (r.running) { |
| 60 | r.lastFrame = SDL_GetTicks(); |
| 61 | if (r.lastFrame >= lastTime + 1000) { |
| 62 | lastTime = r.lastFrame; |
| 63 | r.fps = r.frameCount; |
| 64 | r.frameCount = 0; |
| 65 | } |
| 66 | |
| 67 | if (!r.gameover) { |
| 68 | r.update(r, p, bl, br); |
| 69 | } |
| 70 | r.input(r, p, bl, br); |
| 71 | r.render(p, bl, br); |
| 72 | } |
| 73 | |
| 74 | TTF_CloseFont(r.font); |
| 75 | SDL_DestroyRenderer(r.renderer); |
| 76 | SDL_DestroyWindow(r.window); |
| 77 | SDL_Quit(); |
| 78 | Mix_FreeMusic(r.music); |
| 79 | Mix_CloseAudio(); |
| 80 | TTF_Quit(); |
| 81 | |
| 82 | return 0; |
| 83 | } |