| 1 | #include "render.hh" |
| 2 | #include "ball.hh" |
| 3 | #include "player.hh" |
| 4 | #include "brick.hh" |
| 5 | #include <string> |
| 6 | |
| 7 | #define DEBUG false |
| 8 | |
| 9 | void Render::gameOver(Player &p, Brick &br, bool winner) { |
| 10 | if (winner) { |
| 11 | Render::winner = true; |
| 12 | } else { |
| 13 | Render::winner = false; |
| 14 | } |
| 15 | |
| 16 | Render::gameover = true; |
| 17 | Mix_HaltMusic(); |
| 18 | } |
| 19 | |
| 20 | void Render::write(Player &p, Brick &br, std::string text, int x, int y) { |
| 21 | SDL_Surface *surface; |
| 22 | SDL_Texture *texture; |
| 23 | const char* t = text.c_str(); |
| 24 | surface = TTF_RenderText_Solid(Render::font, t, br.color); |
| 25 | texture = SDL_CreateTextureFromSurface(Render::renderer, surface); |
| 26 | |
| 27 | p.lives.w = surface->w; |
| 28 | p.lives.h = surface->h; |
| 29 | p.lives.x = x - p.lives.w; |
| 30 | p.lives.y = y - p.lives.h; |
| 31 | |
| 32 | SDL_FreeSurface(surface); |
| 33 | SDL_RenderCopy(Render::renderer, texture, NULL, &p.lives); |
| 34 | SDL_DestroyTexture(texture); |
| 35 | } |
| 36 | |
| 37 | void Render::update(Render &r, Player &p, Ball &bl, Brick &br) { |
| 38 | if (p.livesCount <= 0) { |
| 39 | gameOver(p, br, false); |
| 40 | } |
| 41 | |
| 42 | if (SDL_HasIntersection(&bl.ball, &p.player)) { |
| 43 | double rel = (p.player.x + ((float)p.player.w/2)) - ((float)bl.ball.x + ((float)bl.size/2)); |
| 44 | double norm = rel / ((float)p.player.w/2); |
| 45 | double bounce = norm * (5 * M_PI / 12); |
| 46 | if (fabs(bounce - bounceSave) <= .04) bounce += .4; |
| 47 | |
| 48 | bl.yvelocity = -bl.ballspeed * cos(bounce); |
| 49 | bl.xvelocity = bl.ballspeed * -sin(bounce); |
| 50 | |
| 51 | if (bl.yvelocity == bl.yvelSave || -bl.yvelocity == bl.yvelSave) bl.xvelocity += .4f; |
| 52 | bl.yvelSave = bl.yvelocity; |
| 53 | |
| 54 | bounceSave = bounce; |
| 55 | p.scoreMultiplier = 1; |
| 56 | } |
| 57 | |
| 58 | if (bl.ball.y <= 0) { |
| 59 | bl.yvelocity = -bl.yvelocity; |
| 60 | } |
| 61 | if (bl.ball.y + bl.size >= Render::height) { |
| 62 | bl.yvelocity = -bl.yvelocity; |
| 63 | p.livesCount--; |
| 64 | bl.resetBall(r, p); |
| 65 | } |
| 66 | if (bl.ball.x <= 0 || bl.ball.x + bl.size >= Render::width) { |
| 67 | bl.xvelocity = -bl.xvelocity; |
| 68 | if (bl.xvelocity == bl.xvelSave || -bl.xvelocity == bl.xvelSave) bl.yvelocity += .4f; |
| 69 | bl.xvelSave = bl.xvelocity; |
| 70 | } |
| 71 | bl.ball.x += bl.xvelocity; |
| 72 | bl.ball.y += bl.yvelocity; |
| 73 | |
| 74 | if (p.player.x < 0) { |
| 75 | p.player.x = 0; |
| 76 | } |
| 77 | if (p.player.x + p.player.w > Render::width) { |
| 78 | p.player.x = Render::width - p.player.w; |
| 79 | } |
| 80 | |
| 81 | bool reset = true; |
| 82 | for (int i = 0; i < br.col*br.row; i++) { |
| 83 | br.setBricks(i); |
| 84 | |
| 85 | if (SDL_HasIntersection(&bl.ball, &br.brick) && br.bricks[i]) { |
| 86 | br.bricks[i] = 0; |
| 87 | p.score += 100 * p.scoreMultiplier; |
| 88 | p.scoreMultiplier++; |
| 89 | |
| 90 | if (bl.ball.x >= br.brick.x) { |
| 91 | bl.xvelocity = -bl.xvelocity; |
| 92 | bl.ball.x -= 1; |
| 93 | } |
| 94 | if (bl.ball.x <= br.brick.x) { |
| 95 | bl.xvelocity = -bl.xvelocity; |
| 96 | bl.ball.x += 1; |
| 97 | } |
| 98 | if (bl.ball.y <= br.brick.y) { |
| 99 | bl.yvelocity = -bl.yvelocity; |
| 100 | bl.xvelocity = -bl.xvelocity; |
| 101 | bl.ball.y -= 1; |
| 102 | } |
| 103 | if (bl.ball.y >= br.brick.y) { |
| 104 | bl.yvelocity = -bl.yvelocity; |
| 105 | bl.xvelocity = -bl.xvelocity; |
| 106 | bl.ball.y += 1; |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | if (br.bricks[i]) { |
| 111 | reset = false; |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | if (reset) { |
| 116 | Render::level++; |
| 117 | if (r.level > 7) { |
| 118 | gameOver(p, br, true); |
| 119 | } else { |
| 120 | br.resetBricks(r, p, bl); |
| 121 | } |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | void Render::input(Render &r, Player &p, Ball &bl, Brick &br) { |
| 126 | SDL_Event e; |
| 127 | const Uint8 *keystates = SDL_GetKeyboardState(NULL); |
| 128 | |
| 129 | while (SDL_PollEvent(&e)) { |
| 130 | if (e.type == SDL_QUIT) { |
| 131 | Render::running = false; |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | if (keystates[SDL_SCANCODE_Q]) Render::running = false; |
| 136 | #if DEBUG |
| 137 | if (keystates[SDL_SCANCODE_E]) p.livesCount = 3; |
| 138 | if (keystates[SDL_SCANCODE_R]) { |
| 139 | if (level <= 7) { |
| 140 | br.resetBricks(r, p, bl); |
| 141 | bl.resetBall(r, p); |
| 142 | gameover = false; |
| 143 | } |
| 144 | } |
| 145 | if (keystates[SDL_SCANCODE_O]) { |
| 146 | bl.ball.x = 250.f; |
| 147 | bl.ball.y = 400.f; |
| 148 | bl.xvelocity = 0.f; |
| 149 | bl.yvelocity = 8.f; |
| 150 | } |
| 151 | if (keystates[SDL_SCANCODE_P]) { |
| 152 | bl.ball.x = 250.f; |
| 153 | bl.ball.y = 400.f; |
| 154 | bl.xvelocity = 8.f; |
| 155 | bl.yvelocity = 0.f; |
| 156 | } |
| 157 | if (keystates[SDL_SCANCODE_1]) { |
| 158 | Render::level = 1; |
| 159 | br.resetBricks(r, p, bl); |
| 160 | } |
| 161 | if (keystates[SDL_SCANCODE_2]) { |
| 162 | Render::level = 2; |
| 163 | br.resetBricks(r, p, bl); |
| 164 | } |
| 165 | if (keystates[SDL_SCANCODE_3]) { |
| 166 | Render::level = 3; |
| 167 | br.resetBricks(r, p, bl); |
| 168 | } |
| 169 | if (keystates[SDL_SCANCODE_4]) { |
| 170 | Render::level = 4; |
| 171 | br.resetBricks(r, p, bl); |
| 172 | } |
| 173 | if (keystates[SDL_SCANCODE_5]) { |
| 174 | Render::level = 5; |
| 175 | br.resetBricks(r, p, bl); |
| 176 | } |
| 177 | if (keystates[SDL_SCANCODE_6]) { |
| 178 | Render::level = 6; |
| 179 | br.resetBricks(r, p, bl); |
| 180 | } |
| 181 | if (keystates[SDL_SCANCODE_7]) { |
| 182 | Render::level = 7; |
| 183 | br.resetBricks(r, p, bl); |
| 184 | } |
| 185 | #endif |
| 186 | if (!Render::gameover) { |
| 187 | if (keystates[SDL_SCANCODE_LEFT]) p.player.x -= Render::xpos; |
| 188 | if (keystates[SDL_SCANCODE_RIGHT]) p.player.x += Render::xpos; |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | void Render::render(Player &p, Ball &bl, Brick &br) { |
| 193 | SDL_SetRenderDrawColor(Render::renderer, 0x00, 0x00, 0x00, 255); |
| 194 | SDL_RenderClear(Render::renderer); |
| 195 | |
| 196 | Render::frameCount++; |
| 197 | Render::timerFPS = SDL_GetTicks() - Render::lastFrame; |
| 198 | |
| 199 | if (Render::timerFPS < (1000/60)) { |
| 200 | SDL_Delay((1000/60) - Render::timerFPS); |
| 201 | } |
| 202 | |
| 203 | SDL_SetRenderDrawColor(Render::renderer, 255, 255, 255, 255); |
| 204 | SDL_RenderFillRect(Render::renderer, &p.player); |
| 205 | SDL_RenderFillRect(Render::renderer, &bl.ball); |
| 206 | write(p, br, "SCORE: " + std::to_string(p.score), Render::width, Render::fontsize*1.5f); |
| 207 | write(p, br, "LIVES: " + std::to_string(p.livesCount), 130, Render::fontsize*1.5f); |
| 208 | write(p, br, "LEVEL: " + std::to_string(Render::level), 130, Render::fontsize*3.5f); |
| 209 | |
| 210 | if (Render::gameover) { |
| 211 | if (Render::winner) write(p, br, "YOU WIN", Render::width/2+Render::fontsize, Render::height/2); |
| 212 | else write(p, br, "YOU LOSS", Render::width/2+Render::fontsize, Render::height/2); |
| 213 | } |
| 214 | |
| 215 | for (int i = 0; i < br.col*br.row; i++) { |
| 216 | SDL_SetRenderDrawColor(Render::renderer, c1r, c1g, c1b, 255); |
| 217 | if (i%2 == 0) SDL_SetRenderDrawColor(Render::renderer, c2r, c2g, c2b, 255); |
| 218 | if (i%3 == 0) SDL_SetRenderDrawColor(Render::renderer, c3r, c3g, c3b, 255); |
| 219 | if (i%4 == 0) SDL_SetRenderDrawColor(Render::renderer, c4r, c4g, c4b, 255); |
| 220 | if (i%5 == 0) SDL_SetRenderDrawColor(Render::renderer, c5r, c5g, c5b, 255); |
| 221 | if (i%6 == 0) SDL_SetRenderDrawColor(Render::renderer, c6r, c6g, c6b, 255); |
| 222 | |
| 223 | if (br.bricks[i]) { |
| 224 | br.setBricks(i); |
| 225 | SDL_RenderFillRect(Render::renderer, &br.brick); |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | SDL_RenderPresent(Render::renderer); |
| 230 | } |