| 3 | | |
| 4 | | int main() |
| 5 | | { |
| 6 | | |
| 7 | | float posX = 0; |
| 8 | | float posY = 0; |
| 9 | | |
| 10 | | powerON(POWER_ALL); |
| 11 | | |
| 12 | | //set mode 0, enable BG0 and set it to 3D |
| 13 | | videoSetMode(MODE_0_3D); |
| 14 | | |
| 15 | | //irqs are nice |
| 16 | | irqInit(); |
| 17 | | irqEnable(IRQ_VBLANK); |
| 18 | | |
| 19 | | // initialize gl |
| 20 | | glInit(); |
| 21 | | |
| 22 | | // enable antialiasing |
| 23 | | glEnable(GL_ANTIALIAS); |
| 24 | | |
| 25 | | // setup the rear plane |
| 26 | | glClearColor(0, 0, 0, 31); // BG must be opaque for AA to work |
| 27 | | glClearPolyID(63); // BG must have a unique polygon ID for AA to work |
| 28 | | glClearDepth(0x7FFF); |
| 29 | | |
| 30 | | //this should work the same as the normal gl call |
| 31 | | glViewport(0, 0, 255, 191); |
| | 3 | #define WIDTH 1.f |
| | 4 | #define HEIGHT 1.f |
| | 5 | #define HEIGHT2 0.5f |
| | 6 | |
| | 7 | #define MOVEMENT_SPEED 0.01 // PER FRAME? |
| | 8 | |
| | 9 | #define XMARGIN 0.01f |
| | 10 | |
| | 11 | #define PADDLE_HEIGHT 0.1f |
| | 12 | #define PADDLE_HEIGHT2 0.05f |
| | 13 | #define PADDLE_WIDTH 0.01f |
| | 14 | |
| | 15 | #define BALL_SIZE 0.01f |
| | 16 | #define BALL_SIZE2 0.005f |
| | 17 | |
| | 18 | #define REAL_PADDLE_WIDTH 0.03f |
| | 19 | |
| | 20 | void drawPaddle(int player, float pos); |
| | 21 | void drawBall(float x, float y); |
| | 22 | void drawBackground(); |
| | 23 | |
| | 24 | int thinkComputer(float pos, float ballX, float ballY); |
| | 25 | void thinkCollidePaddle(int player, float playerPos, float xBall, float yBall, float *xxBall); |
| | 26 | void thinkCollideEdge(float xBall, float yBall, float *xxBall, float *yyBall); |
| | 27 | |
| | 28 | int main() { |
| | 29 | |
| | 30 | float player1 = HEIGHT2; |
| | 31 | float player2 = HEIGHT2; |
| | 32 | float ballx; |
| | 33 | float bally; |
| | 34 | float balldx; |
| | 35 | float balldy; |
| | 36 | |
| | 37 | powerON(POWER_ALL); |
| | 38 | |
| | 39 | //set mode 0, enable BG0 and set it to 3D |
| | 40 | videoSetMode(MODE_0_3D); |
| | 41 | |
| | 42 | //irqs are nice |
| | 43 | irqInit(); |
| | 44 | irqEnable(IRQ_VBLANK); |
| | 45 | |
| | 46 | // initialize gl |
| | 47 | glInit(); |
| | 48 | |
| | 49 | // enable antialiasing |
| | 50 | glEnable(GL_ANTIALIAS); |
| | 51 | |
| | 52 | // setup the rear plane |
| | 53 | glClearColor(0, 0, 0, 31); // BG must be opaque for AA to work |
| | 54 | glClearPolyID(63); // BG must have a unique polygon ID for AA to work |
| | 55 | glClearDepth(0x7FFF); |
| | 56 | |
| | 57 | //this should work the same as the normal gl call |
| | 58 | glViewport(0, 0, 255, 191); |
| 38 | | while(1) |
| 39 | | { |
| 40 | | //not a real gl function and will likely change |
| 41 | | glPolyFmt(POLY_ALPHA(31) | POLY_CULL_NONE); |
| 42 | | |
| 43 | | scanKeys(); |
| 44 | | |
| 45 | | u16 keys = keysHeld(); |
| 46 | | |
| 47 | | if((keys & KEY_UP)) posY += 0.1; |
| 48 | | if((keys & KEY_DOWN)) posY -= 0.1; |
| 49 | | if((keys & KEY_LEFT)) posX -= 0.1; |
| 50 | | if((keys & KEY_RIGHT)) posX += 0.1; |
| 51 | | |
| 52 | | //draw the obj |
| 53 | | glBegin(GL_QUAD); |
| 54 | | |
| 55 | | glColor3b(255, 0, 0); |
| 56 | | glVertex3f(posX, posY, 0); |
| 57 | | |
| 58 | | glColor3b(0, 255, 0); |
| 59 | | glVertex3f(posX + 1, posY, 0); |
| 60 | | |
| 61 | | glColor3b(0, 0, 255); |
| 62 | | glVertex3f(posX + 1, posY + 1, 0); |
| 63 | | |
| 64 | | glColor3b(255, 0, 255); |
| 65 | | glVertex3f(posX, posY + 1, 0); |
| 66 | | |
| 67 | | glEnd(); |
| 68 | | |
| 69 | | glFlush(0); |
| 70 | | |
| 71 | | swiWaitForVBlank(); |
| 72 | | } |
| 73 | | |
| 74 | | return 0; |
| 75 | | }//end main |
| | 65 | // not a real gl function and will likely change |
| | 66 | glPolyFmt(POLY_ALPHA(31) | POLY_CULL_NONE); |
| | 67 | |
| | 68 | ballx = 0.5f; |
| | 69 | bally = 0.5f; |
| | 70 | balldx = 0.003f; |
| | 71 | balldy = 0.003f; |
| | 72 | |
| | 73 | // Debugging |
| | 74 | |
| | 75 | // straight to edge |
| | 76 | // balldx = 0; |
| | 77 | // balldy = 0.01; |
| | 78 | |
| | 79 | // straight to paddle |
| | 80 | balldx = 0.002f; |
| | 81 | balldy = 0.f; |
| | 82 | |
| | 83 | while (1) { |
| | 84 | |
| | 85 | // think universe |
| | 86 | ballx += balldx; |
| | 87 | bally += balldy; |
| | 88 | thinkCollidePaddle(0, player1, ballx, bally, &balldx); |
| | 89 | thinkCollidePaddle(1, player2, ballx, bally, &balldx); |
| | 90 | thinkCollideEdge(ballx, bally, &balldx, &balldy); |
| | 91 | |
| | 92 | // think computer |
| | 93 | int direction; |
| | 94 | direction = thinkComputer(player2, ballx, bally); |
| | 95 | player2 += direction * MOVEMENT_SPEED; |
| | 96 | |
| | 97 | // think human |
| | 98 | scanKeys(); |
| | 99 | u16 keys = keysHeld(); |
| | 100 | if ((keys & KEY_UP)) player1 -= MOVEMENT_SPEED; |
| | 101 | if ((keys & KEY_DOWN)) player1 += MOVEMENT_SPEED; |
| | 102 | |
| | 103 | // draw stuff |
| | 104 | drawPaddle(0, player1); |
| | 105 | drawPaddle(1, player2); |
| | 106 | drawBall(ballx, bally); |
| | 107 | drawBackground(); |
| | 108 | glFlush(0); |
| | 109 | swiWaitForVBlank(); |
| | 110 | |
| | 111 | } |
| | 112 | |
| | 113 | return 0; |
| | 114 | |
| | 115 | } |
| | 116 | |
| | 117 | float getPlayerPos(int player) { |
| | 118 | |
| | 119 | if (!player) { |
| | 120 | return XMARGIN; |
| | 121 | |
| | 122 | } else { |
| | 123 | return WIDTH - XMARGIN; |
| | 124 | |
| | 125 | } |
| | 126 | |
| | 127 | } |
| | 128 | |
| | 129 | void drawPaddle(int player, float pos) { |
| | 130 | |
| | 131 | float xPos; |
| | 132 | float yPos; |
| | 133 | |
| | 134 | yPos = pos; |
| | 135 | xPos = getPlayerPos(player); |
| | 136 | |
| | 137 | glColor3b(255, 0, 0); |
| | 138 | glBegin(GL_QUAD); |
| | 139 | |
| | 140 | glVertex3f(xPos, yPos - PADDLE_HEIGHT2, 0); |
| | 141 | glVertex3f(xPos + PADDLE_WIDTH, yPos - PADDLE_HEIGHT2, 0); |
| | 142 | glVertex3f(xPos + PADDLE_WIDTH, yPos + PADDLE_HEIGHT2, 0); |
| | 143 | glVertex3f(xPos, yPos + PADDLE_HEIGHT2, 0); |
| | 144 | |
| | 145 | glEnd(); |
| | 146 | |
| | 147 | } |
| | 148 | |
| | 149 | void drawBackground() { |
| | 150 | |
| | 151 | glBegin(GL_QUAD); |
| | 152 | |
| | 153 | glColor3b(50,80,0); |
| | 154 | glVertex3v16(inttov16(-1), inttov16(-1),0); |
| | 155 | |
| | 156 | glColor3b(0,60,0); |
| | 157 | glVertex3v16(inttov16(1), inttov16(-1), 0); |
| | 158 | |
| | 159 | glColor3b(0,0,255); |
| | 160 | glVertex3v16(inttov16(1), inttov16(1), 0); |
| | 161 | |
| | 162 | glColor3b(0,90,255); |
| | 163 | glVertex3v16(inttov16(-1), inttov16(1), 0); |
| | 164 | |
| | 165 | glEnd(); |
| | 166 | |
| | 167 | } |
| | 168 | |
| | 169 | void drawBall(float x, float y) { |
| | 170 | |
| | 171 | glBegin(GL_QUAD); |
| | 172 | |
| | 173 | glVertex3f(x - BALL_SIZE2, y - BALL_SIZE2, 0); |
| | 174 | glVertex3f(x + BALL_SIZE2, y - BALL_SIZE2, 0); |
| | 175 | glVertex3f(x + BALL_SIZE2, y + BALL_SIZE2, 0); |
| | 176 | glVertex3f(x - BALL_SIZE2, y + BALL_SIZE2, 0); |
| | 177 | |
| | 178 | glEnd(); |
| | 179 | |
| | 180 | } |
| | 181 | |
| | 182 | int thinkComputer(float pos, float ballX, float ballY) { |
| | 183 | |
| | 184 | int dir; |
| | 185 | |
| | 186 | if (pos == ballY) |
| | 187 | dir = 0; |
| | 188 | else if (pos > ballY) |
| | 189 | dir = -1; |
| | 190 | else |
| | 191 | dir = 1; |
| | 192 | |
| | 193 | return dir; |
| | 194 | |
| | 195 | } |
| | 196 | |
| | 197 | void thinkCollidePaddle(int player, float playerY, float xBall, float yBall, float *xxBall) { |
| | 198 | |
| | 199 | float playerX; |
| | 200 | playerX = getPlayerPos(player); |
| | 201 | // if ( |
| | 202 | // playerY - PADDLE_HEIGHT2 > yBall && |
| | 203 | // playerY + PADDLE_HEIGHT2 < yBall |
| | 204 | // ) { |
| | 205 | |
| | 206 | if (player) { |
| | 207 | if ( |
| | 208 | playerX - REAL_PADDLE_WIDTH > xBall && |
| | 209 | playerX + REAL_PADDLE_WIDTH < xBall |
| | 210 | ) { |
| | 211 | *xxBall = -*xxBall; |
| | 212 | } |
| | 213 | } |
| | 214 | |
| | 215 | } |
| | 216 | |
| | 217 | void thinkCollideEdge(float xBall, float yBall, float *xxBall, float *yyBall) { |
| | 218 | |
| | 219 | if (yBall <= 0) { |
| | 220 | *yyBall = -*yyBall; |
| | 221 | } |
| | 222 | |
| | 223 | if (yBall >= HEIGHT) { |
| | 224 | *yyBall = -*yyBall; |
| | 225 | } |
| | 226 | |
| | 227 | } |
| | 228 | |
| | 229 | // vim: set ts=4 sw=4 expandtab |