Created
January 4, 2017 03:30
-
-
Save seth10/bb63f6301fa276cd1f5c3f34c013c78c to your computer and use it in GitHub Desktop.
Reorganized all code to wait for VBlank, this is before simplifying with VCount
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Main written by Seth Tenembaum beginning Jan 2, 2017 | |
* | |
* The purpose of this document is to come to a better understanding of the fundamentals of game developmeny by building a simple platformer from scratch at a low level (but not quite down to assembly at the moment). | |
* | |
* TODO | |
* - fix issue when holding left and right (actually not possible on physical hardware...) | |
* - subpixel positioning and detach movement speed from frame rate | |
* - add a single platform | |
* - implement a global coordinate system, array of platform positions and sizes, and a camera | |
* | |
*/ | |
#include "gba.h" //memory addresses and type shortcuts | |
#include "screenmode.h" //video mode bits and SetMode macro | |
#include "keypad.h" //buttons and input register | |
#include "gfx.h" //RGB macro, sleep, random | |
s32 xpos = 10, // must be signed | |
ypos = 100, | |
xvel = 0, | |
yvel = 0, | |
xacc = 0, | |
yacc = 0; | |
const s32 GRAV = 1, | |
FLOOR = 30, | |
SPEED = 7, | |
JUMP = 10; | |
void gameLoop(); | |
int main(void) { | |
SetMode(MODE_3 | BG2_ENABLE); //screenmode.h | |
// 240 px wide, 160 px tall | |
// draw floor | |
word i; | |
for (i = 0; i < 240; i++) | |
(VideoBuffer)[(160-FLOOR)*240+i] = RGB(0,10,31); | |
word state = 0; // 0 = waiting for VBlank, 1 = waiting for VDraw | |
while (1) { | |
if (REG_DISPSTAT & 0x01) { // VBlank | |
if (state == 0) { | |
gameLoop(); | |
state = 1; | |
} | |
} else { // VDraw | |
state = 0; | |
} | |
} | |
} | |
void gameLoop() { | |
// erase old sprite | |
drawRect(xpos-5, 160-(ypos+10), 10, 10, 0, 0, 0); | |
// get input | |
if (KEYS & KEY_LEFT) | |
xacc--; | |
if (KEYS & KEY_RIGHT) | |
xacc++; | |
if (!(KEYS & (KEY_LEFT|KEY_RIGHT))) // decelerate if neither pressed | |
if (xvel > 0) | |
xacc = -1; | |
else if (xvel < 0) | |
xacc = 1; | |
else | |
xacc = 0; | |
if (KEYS & KEY_UP && ypos == FLOOR) // only jump if on floor | |
yvel = JUMP; | |
// simulate | |
yvel -= GRAV; // apply gravity | |
xvel += xacc; // apply acceleration | |
yvel += yacc; | |
if (xvel > SPEED) // enfore maximum velocities | |
xvel = SPEED; | |
if (xvel < -1*SPEED) | |
xvel = -1*SPEED; | |
// apply velocity | |
if (ypos > FLOOR) | |
xpos += 0.7*xvel; // air resistance | |
else | |
xpos += xvel; | |
ypos += yvel; | |
if (xpos > 240) // wrap x-coordinate | |
xpos -= 240; | |
if (xpos < 0) | |
xpos += 240; | |
if (ypos < FLOOR) { // pop out of floor | |
ypos = FLOOR; | |
yvel = 0; | |
} | |
// draw sprite | |
drawRect(xpos-5, 160-(ypos+10), 10, 10, 31, 31, 31); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment