| 1 | #define SDL_MAIN_HANDLED |
| 2 | #include <SDL2/SDL.h> |
| 3 | #include <SDL2/SDL_ttf.h> |
| 4 | #include <iostream> |
| 5 | #include <string> |
| 6 | |
| 7 | #include "Peepo.h" |
| 8 | |
| 9 | #define WIDTH 720 |
| 10 | #define HEIGHT 720 |
| 11 | #define FONTSIZE 32 |
| 12 | #define BALLSIZE 16 |
| 13 | #define BALLSPEED 16 |
| 14 | #define SPEED 9 |
| 15 | #define PI 3.1415926535897323846 |
| 16 | |
| 17 | SDL_Renderer *renderer; |
| 18 | SDL_Window *window; |
| 19 | TTF_Font *font; |
| 20 | SDL_Color color; |
| 21 | bool running; |
| 22 | int frameCount, timerFPS, lastFrame, fps; |
| 23 | |
| 24 | SDL_Rect player, npc, ball, scoreboard, leveldisplay; |
| 25 | float xvelocity, yvelocity, ballvelocity, ballsize; |
| 26 | std::string score, slevel; |
| 27 | int lscore, rscore, level, games; |
| 28 | bool turn, isgameover; |
| 29 | std::string version = "1.0.1"; |
| 30 | |
| 31 | void serve() { |
| 32 | player.y = npc.y = (HEIGHT/2) - (player.h/2); |
| 33 | if (turn) { |
| 34 | ball.x = player.x + (player.w * 4); |
| 35 | xvelocity = (BALLSPEED*ballvelocity)/2; |
| 36 | turn = false; |
| 37 | } else { |
| 38 | ball.x = npc.x - (player.w * 4); |
| 39 | xvelocity = -(BALLSPEED*ballvelocity)/2; |
| 40 | turn = true; |
| 41 | } |
| 42 | |
| 43 | yvelocity = 0; |
| 44 | ball.y = HEIGHT / 2 - ((BALLSIZE-static_cast<int>(ballsize))/2); |
| 45 | } |
| 46 | |
| 47 | void write(std::string text, int x, int y) { |
| 48 | SDL_Surface *surface; |
| 49 | SDL_Texture *texture; |
| 50 | const char* t = text.c_str(); |
| 51 | |
| 52 | surface = TTF_RenderText_Solid(font, t, color); |
| 53 | texture = SDL_CreateTextureFromSurface(renderer, surface); |
| 54 | |
| 55 | |
| 56 | scoreboard.w = surface->w; |
| 57 | scoreboard.h = surface->h; |
| 58 | scoreboard.x = x - scoreboard.w; |
| 59 | scoreboard.y = y - scoreboard.h; |
| 60 | |
| 61 | |
| 62 | leveldisplay.w = surface->w; |
| 63 | leveldisplay.h = surface->h; |
| 64 | leveldisplay.x = x - scoreboard.w + 8; |
| 65 | leveldisplay.y = y - scoreboard.h + 8; |
| 66 | |
| 67 | |
| 68 | SDL_FreeSurface(surface); |
| 69 | SDL_RenderCopy(renderer, texture, NULL, &scoreboard); |
| 70 | SDL_DestroyTexture(texture); |
| 71 | } |
| 72 | |
| 73 | void gameover() { |
| 74 | xvelocity = yvelocity = ballvelocity = 0; |
| 75 | } |
| 76 | |
| 77 | void update() { |
| 78 | |
| 79 | if (SDL_HasIntersection(&ball, &npc)) { |
| 80 | double rel = (npc.y + (npc.h / 2)) - (ball.y + ((BALLSIZE-ballsize) / 2)); |
| 81 | double norm = rel / (npc.h / 2); |
| 82 | double bounce = norm * (5 * PI / 12); |
| 83 | xvelocity = -(BALLSPEED*ballvelocity) * cos(static_cast<float>(bounce)); |
| 84 | yvelocity = (BALLSPEED*ballvelocity) * -sin(static_cast<float>(bounce)); |
| 85 | } |
| 86 | |
| 87 | |
| 88 | if (SDL_HasIntersection(&ball, &player)) { |
| 89 | double rel = (player.y + (player.h / 2)) - (ball.y + ((BALLSIZE-ballsize) / 2)); |
| 90 | double norm = rel / (player.h / 2); |
| 91 | double bounce = norm * (5 * PI / 12); |
| 92 | xvelocity = (BALLSPEED*ballvelocity) * cos(static_cast<float>(bounce)); |
| 93 | yvelocity = (BALLSPEED*ballvelocity) * -sin(static_cast<float>(bounce)); |
| 94 | } |
| 95 | |
| 96 | |
| 97 | if (ball.y > npc.y + (npc.h/2)) npc.y += ((SPEED+level*0.5f)*static_cast<int>(ballvelocity)); |
| 98 | if (ball.y < npc.y + (npc.h/2)) npc.y -= ((SPEED+level*0.5f)*static_cast<int>(ballvelocity)); |
| 99 | |
| 100 | |
| 101 | if (ball.x <= 0) { |
| 102 | rscore++; |
| 103 | serve(); |
| 104 | } else if (ball.x + (BALLSIZE-ballsize) >= WIDTH) { |
| 105 | lscore++; |
| 106 | serve(); |
| 107 | games++; |
| 108 | } |
| 109 | |
| 110 | |
| 111 | if (games == ((level + 1) * 2)) { |
| 112 | level++; |
| 113 | ballvelocity += 0.25f; |
| 114 | ballsize -= 0.2f; |
| 115 | games = 0; |
| 116 | } |
| 117 | |
| 118 | |
| 119 | if (rscore > 9) { |
| 120 | isgameover = true; |
| 121 | } |
| 122 | |
| 123 | |
| 124 | if (ball.y <= 0 || ball.y + (BALLSIZE-ballsize) >= HEIGHT) yvelocity = -yvelocity; |
| 125 | |
| 126 | |
| 127 | ball.x += static_cast<int>(xvelocity); |
| 128 | ball.y += static_cast<int>(yvelocity); |
| 129 | |
| 130 | |
| 131 | score = std::to_string(lscore) + " " + std::to_string(rscore); |
| 132 | slevel = "Lv" + std::to_string(level); |
| 133 | |
| 134 | |
| 135 | if (player.y < 0) player.y = 0; |
| 136 | if (player.y + player.h > HEIGHT) player.y = HEIGHT-player.h; |
| 137 | |
| 138 | |
| 139 | if (npc.y < 0) npc.y = 0; |
| 140 | if (npc.y + npc.h > HEIGHT) npc.y = HEIGHT-npc.h; |
| 141 | } |
| 142 | |
| 143 | void input() { |
| 144 | SDL_Event e; |
| 145 | const Uint8 *keystates = SDL_GetKeyboardState(NULL); |
| 146 | |
| 147 | while (SDL_PollEvent(&e)) { |
| 148 | if (e.type == SDL_QUIT) { |
| 149 | running = false; |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | if (keystates[SDL_SCANCODE_Q]) running = false; |
| 154 | if (!isgameover) { |
| 155 | if (keystates[SDL_SCANCODE_UP]) player.y -= (SPEED*static_cast<int>(ballvelocity)); |
| 156 | if (keystates[SDL_SCANCODE_DOWN]) player.y += (SPEED*static_cast<int>(ballvelocity)); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | void render() { |
| 161 | |
| 162 | SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00, 255); |
| 163 | SDL_RenderClear(renderer); |
| 164 | |
| 165 | frameCount++; |
| 166 | timerFPS = SDL_GetTicks() - lastFrame; |
| 167 | if (timerFPS < (1000/60)) { |
| 168 | SDL_Delay((1000/60) - timerFPS); |
| 169 | } |
| 170 | |
| 171 | |
| 172 | SDL_SetRenderDrawColor(renderer, color.r, color.g, color.b, 255); |
| 173 | |
| 174 | |
| 175 | if (!isgameover) { |
| 176 | SDL_RenderFillRect(renderer, &player); |
| 177 | SDL_RenderFillRect(renderer, &npc); |
| 178 | SDL_RenderFillRect(renderer, &ball); |
| 179 | } |
| 180 | |
| 181 | write(score, WIDTH/2+FONTSIZE, FONTSIZE*2); |
| 182 | write(slevel, WIDTH/2+FONTSIZE, FONTSIZE*3); |
| 183 | if (isgameover) { |
| 184 | write("GAME OVER", WIDTH/2+84, HEIGHT/2); |
| 185 | } |
| 186 | write("076.moe SUWAKO NO PONG v" + version, WIDTH-150, HEIGHT-20); |
| 187 | SDL_RenderPresent(renderer); |
| 188 | } |
| 189 | |
| 190 | #if defined(_WIN32) || defined(_WIN64) |
| 191 | int WinMain(void) { |
| 192 | #else |
| 193 | int main(void) { |
| 194 | #endif |
| 195 | |
| 196 | if (SDL_Init(SDL_INIT_EVERYTHING) < 0) { |
| 197 | std::cout << "SDL_Init()に失敗" << std::endl; |
| 198 | return -1; |
| 199 | } |
| 200 | |
| 201 | |
| 202 | if (SDL_CreateWindowAndRenderer(WIDTH, HEIGHT, 0, &window, &renderer) < 0) { |
| 203 | std::cout << "SDL_CreateWindowAndRenderer()に失敗" << std::endl; |
| 204 | SDL_Quit(); |
| 205 | return -1; |
| 206 | } |
| 207 | |
| 208 | std::string tit = "Pong " + std::string(version); |
| 209 | |
| 210 | SDL_SetWindowTitle(window, tit.c_str()); |
| 211 | |
| 212 | |
| 213 | TTF_Init(); |
| 214 | font = TTF_OpenFontRW(SDL_RWFromMem(Peepo_ttf, Peepo_ttf_len), 1, FONTSIZE); |
| 215 | if (!font) { |
| 216 | std::cout << "フォントを読込に失敗" << std::endl; |
| 217 | SDL_DestroyRenderer(renderer); |
| 218 | SDL_DestroyWindow(window); |
| 219 | SDL_Quit(); |
| 220 | return -1; |
| 221 | } |
| 222 | |
| 223 | |
| 224 | running = true; |
| 225 | static int lastTime = 0; |
| 226 | |
| 227 | color.r = color.g = color.b = 255; |
| 228 | lscore = rscore = games = 0; |
| 229 | ballvelocity = ballsize = 1.0f; |
| 230 | level = 1; |
| 231 | player.x = 32; |
| 232 | player.h = (HEIGHT/4)/(static_cast<int>(ballsize)*1.15f); |
| 233 | player.y = (HEIGHT/2) - (player.h/2); |
| 234 | player.w = 12; |
| 235 | npc = player; |
| 236 | npc.x = WIDTH - npc.w - 32; |
| 237 | ball.w = ball.h = (BALLSIZE-static_cast<int>(ballsize)); |
| 238 | |
| 239 | serve(); |
| 240 | |
| 241 | |
| 242 | while (running) { |
| 243 | lastFrame = SDL_GetTicks(); |
| 244 | if (lastFrame >= (lastTime + 1000)) { |
| 245 | lastTime = lastFrame; |
| 246 | fps = frameCount; |
| 247 | frameCount = 0; |
| 248 | } |
| 249 | |
| 250 | if (!isgameover) { |
| 251 | update(); |
| 252 | input(); |
| 253 | render(); |
| 254 | } else { |
| 255 | gameover(); |
| 256 | input(); |
| 257 | render(); |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | |
| 262 | TTF_CloseFont(font); |
| 263 | SDL_DestroyRenderer(renderer); |
| 264 | SDL_DestroyWindow(window); |
| 265 | SDL_Quit(); |
| 266 | |
| 267 | return 0; |
| 268 | } |