int eventLoop(void* rect) { SDL_Event event; SDL_Rect* sprite = (SDL_Rect*)rect; while (SDL_WaitEvent(&event)) { switch (event.type) { case SDL_QUIT: SDL_Quit(); case SDL_KEYDOWN: switch (event.key.keysym.sym) { case SDLK_j: move_sprite(*sprite, DOWN); break; case SDLK_k: move_sprite(*sprite, UP); break; case SDLK_h: move_sprite(*sprite, RIGHT); break; case SDLK_l: move_sprite(*sprite, LEFT); break; case SDLK_q: SDL_Quit(); } } } // Should be unreacheable without errors. TODO: Logging system. std::cerr << "Error in event thread!" << std::flush; return 1; }
void hideCursor() { for(j=0;j!=4;j++) { move_sprite(j+SPRITE_ARRL_START,0, 0); } move_sprite(SPRITE_ARRT_START, 0, 0); }
void move_cursor(UBYTE x, UBYTE y) { move_sprite(23, x, y); move_sprite(24, x, y+8); move_sprite(25, x+8, y); move_sprite(26, x+8, y+8); }
void hideScore() { i = SCORE_SPACE; move_sprite(i, 0, 0); move_sprite(++i, 0, 0); move_sprite(++i, 0, 0); move_sprite(++i, 0, 0); }
void moveNaveKbd(unsigned char SC) { if ((SC == SC_LEFT) && ((nave_pos.x - MOVEMENTS_OFFSET_NAVE) < (NAVE_MAX_X)) && ((nave_pos.x - MOVEMENTS_OFFSET_NAVE) > (NAVE_MIN_X))) { nave_pos.x -= MOVEMENTS_OFFSET_NAVE; if (direction != 'L') { del_naves_screen(); sync_naves_pos(); direction = 'L'; draw_sprite(nave_esquerda, VIDEO_BASE_ADDRESS); } else move_sprite(nave_esquerda, nave_esquerda->x - MOVEMENTS_OFFSET_NAVE, nave_esquerda->y, VIDEO_BASE_ADDRESS); } else if ((SC == SC_RIGHT) && ((nave_pos.x + MOVEMENTS_OFFSET_NAVE) < (NAVE_MAX_X)) && ((nave_pos.x + MOVEMENTS_OFFSET_NAVE) > (NAVE_MIN_X))) { nave_pos.x += MOVEMENTS_OFFSET_NAVE; if (direction != 'R') { del_naves_screen(); sync_naves_pos(); direction = 'R'; draw_sprite(nave_direita, VIDEO_BASE_ADDRESS); } else move_sprite(nave_direita, nave_direita->x + MOVEMENTS_OFFSET_NAVE, nave_direita->y, VIDEO_BASE_ADDRESS); } else if ((SC == SC_UP) && (nave_pos.y - MOVEMENTS_OFFSET_NAVE < (NAVE_MAX_Y)) && (nave_pos.y - MOVEMENTS_OFFSET_NAVE > (NAVE_MIN_Y))) { nave_pos.y -= MOVEMENTS_OFFSET_NAVE; if (direction != 'U') { del_naves_screen(); sync_naves_pos(); direction = 'U'; draw_sprite(nave_parada, VIDEO_BASE_ADDRESS); } else move_sprite(nave_parada, nave_parada->x, nave_parada->y - MOVEMENTS_OFFSET_NAVE, VIDEO_BASE_ADDRESS); } else if ((SC == SC_DOWN) && (nave_pos.y + MOVEMENTS_OFFSET_NAVE < (NAVE_MAX_Y)) && (nave_pos.y + MOVEMENTS_OFFSET_NAVE > (NAVE_MIN_Y))) { if (direction != 'D') { nave_pos.y += MOVEMENTS_OFFSET_NAVE; del_naves_screen(); sync_naves_pos(); direction = 'D'; draw_sprite(nave_parada, VIDEO_BASE_ADDRESS); } else move_sprite(nave_parada, nave_parada->x, nave_parada->y + MOVEMENTS_OFFSET_NAVE, VIDEO_BASE_ADDRESS); } else { if (direction != 'U') { del_naves_screen(); sync_naves_pos(); direction = 'U'; draw_sprite(nave_parada, VIDEO_BASE_ADDRESS); } } }
/** * Updates player movement and animation. */ void updatePlayer() { UBYTE i, moved; // Get joypad input moved = 0; i = joypad(); if(i & J_RIGHT) { pposx += 1; pdir = DIR_RIGHT; moved = 1; } if(i & J_LEFT) { pposx -= 1; pdir = DIR_LEFT; moved = 1; } if(i & J_UP) { pposy -= 1; pdir = DIR_UP; moved = 1; } if(i & J_DOWN) { pposy += 1; pdir = DIR_DOWN; moved = 1; } // Keep player within bounds if(pposx < PMINX) pposx = PMINX; else if(pposx > PMAXX) pposx = PMAXX; if(pposy < PMINY) pposy = PMINY; else if(pposy > PMAXY) pposy = PMAXY; // Set player position move_sprite(0, pposx, pposy); move_sprite(1, pposx, pposy+8); move_sprite(2, pposx+8, pposy); move_sprite(3, pposx+8, pposy+8); // Set player sprite if(moved) { if(time % 8 == 0) { pframe = (pframe + 1) % 4; } } else { pframe = 1; } i = (pdir << 4) + (pframe << 2); set_sprite_tile(0, i++); set_sprite_tile(1, i++); set_sprite_tile(2, i++); set_sprite_tile(3, i); }
/*--------------------------------------------------------*/ void SET_MOVE_HAMUKO() { set_sprite_tile( 0, hamuko_tile[hd][hp][0] ); set_sprite_tile( 1, hamuko_tile[hd][hp][1] ); set_sprite_tile( 2, hamuko_tile[hd][hp][2] ); set_sprite_tile( 3, hamuko_tile[hd][hp][3] ); move_sprite( 0, hx * 8 + 8, hy * 8 + 16 ); move_sprite( 1, hx * 8 + 8, hy * 8 + 24 ); move_sprite( 2, hx * 8 + 16, hy * 8 + 16 ); move_sprite( 3, hx * 8 + 16, hy * 8 + 24 ); }
void SET_MOVE_SPRITE() { set_sprite_tile( 0, hamumi_tile[gd][gp][0] ); set_sprite_tile( 1, hamumi_tile[gd][gp][1] ); set_sprite_tile( 2, hamumi_tile[gd][gp][2] ); set_sprite_tile( 3, hamumi_tile[gd][gp][3] ); move_sprite( 0, gx * 8 + 8, gy * 8 + 16 ); move_sprite( 1, gx * 8 + 8, gy * 8 + 24 ); move_sprite( 2, gx * 8 + 16, gy * 8 + 16 ); move_sprite( 3, gx * 8 + 16, gy * 8 + 24 ); }
//----------------------------------------Sprite Methods //updates the player stuff (also updates bullet stuff) int PlayerUpdate(int SpriteNum) //0 - nothing happened, 1 - bullet died { //set the sprite set_sprite_tile(SpriteNum, P_FrameIndex); move_sprite(SpriteNum, 0, 0); move_sprite(SpriteNum, P_PosX, P_PosY); //update gun stuff GunUpdate(); //Update bullet stuff return BulletsUpdate(); }
void main(){ int xpos = 8 + GRAPHICS_WIDTH / 2; int ypos = 16 + GRAPHICS_HEIGHT / 2; int count = 0; int joy, i; set_sprite_data(0, 1, crosshair_tiledata); set_sprite_data(1, 6, explode_tiledata); SWITCH_ROM_MBC1(3); set_bkg_data(0, 179, title_tiledata); set_bkg_tiles(0, 0, 20, 18, title_tilemap); SHOW_BKG; DISPLAY_ON; waitpad(J_START); DISPLAY_OFF; SWITCH_ROM_MBC1(2); set_bkg_data(0, 182, earth_tiledata); set_bkg_tiles(0, 0, 20, 18, earth_tilemap); set_sprite_tile(0,0); move_sprite(0, xpos, ypos); SHOW_SPRITES; DISPLAY_ON; waitpadup(); while (1){ wait_vbl_done(); joy = joypad(); if (joy & J_UP && ypos > 14) --ypos; if (joy & J_DOWN && ypos < GRAPHICS_HEIGHT + 13) ++ypos; if (joy & J_LEFT && xpos > 8) --xpos; if (joy & J_RIGHT && xpos < GRAPHICS_WIDTH + 8) ++xpos; if (joy & J_A || joy & J_B){ ++count; move_sprite(count, xpos - 2, ypos - 2); for (i = 1; i != 7; ++i){ set_sprite_tile(count, i); wait_vbl_done(); wait_vbl_done(); } set_sprite_tile(count, 4); set_sprite_prop(count, 1 << 4); if (count == 39){ delay(500); end(); } } move_sprite(0, xpos, ypos); } }
int grav() /* gravity and falling func. */ { if(test){fall=0; return 0;} /* test turns off gravity */ y++; /* check we are stood on something using the arrary*/ if(collide(x,y)){y--; fall=0; return 0; } fall=1; /* if NOT, we fall.... */ move_sprite(0,x,y); delay(7); y++; /* Do this twice so we fall down 2 before l/r movement*/ if(collide(x,y)){y--; fall=0; return 0; } move_sprite(0,x,y); delay(7); return 1; } /* end of gravity and falling func. */
void updateScore() { i = SCORE_SPACE; if(score >= 10) { x1 = score/10; x2 = x1*4; set_sprite_tile(i, numbers_offset+(x2)); set_sprite_tile(i+1, numbers_offset+(x2+2)); move_sprite(i, 72, 30); move_sprite(i+1, 72+8, 30); x1 *= 10; x2 = score-x1; x2 *= 4; set_sprite_tile(i+2, numbers_offset+(x2)); set_sprite_tile(i+3, numbers_offset+(x2+2)); move_sprite(i+2, 72+16, 30); move_sprite(i+3, 72+16+8, 30); } else { x1 = score*4; set_sprite_tile(i, numbers_offset+(x1)); set_sprite_tile(i+1, numbers_offset+(x1+2)); move_sprite(i, 80, 30); move_sprite(i+1, 80+8, 30); move_sprite(i+2, 0, 0); move_sprite(i+3, 0, 0); } }
void updateGraphics() { disable_interrupts(); scroll_bkg(1, 0); move_sprite(0, playerX, playerY); move_sprite(1, badGuyX, badGuyY); move_sprite(2, playerShotX, playerShotY); enable_interrupts(); }
void empty_screen (void) { UBYTE temp1; HIDE_BKG; HIDE_SPRITES; VBK_REG = 0; SWITCH_ROM_MBC1(1); for (temp1=0; temp1<40; temp1++) { move_sprite(temp1,0,200); set_sprite_prop (temp1,0); } for (temp1=0; temp1<20; temp1++) { set_bkg_tiles (0,temp1,20,1,(unsigned char *) &cEMPTY[0]); set_win_tiles (0,temp1,20,1,(unsigned char *) &cEMPTY[0]); VBK_REG = 1; set_bkg_tiles (0,temp1,20,1,(unsigned char *) &cEMPTY[0]); set_win_tiles (0,temp1,20,1,(unsigned char *) &cEMPTY[0]); VBK_REG = 0; } move_bkg (0,0); SWITCH_ROM_MBC1(0); SHOW_BKG; SHOW_SPRITES; }
//loads the player sprites (this doesn't increment sprite num since it starts at 0 void PlayerLoad() { //load sprite data set_sprite_tile(0, P_FrameIndex); move_sprite(0, P_PosX, P_PosY); }
void moveNaveMouse(int dx, int dy) { if (mouse_state.mouse_move == false) return; int new_x = nave_pos.x + dx; int new_y = nave_pos.y + dy; if ((new_x > (NAVE_MIN_X)) && (new_x < (NAVE_MAX_X)) && (new_y > (NAVE_MIN_Y)) && (new_y < (NAVE_MAX_Y))) { nave_pos.x = new_x; nave_pos.y = new_y; } else return; //del_naves_screen(); //sync_naves_pos(); /* if (dx > 0) draw_sprite(nave_direita, VIDEO_BASE_ADDRESS); else if (dx < 0) draw_sprite(nave_esquerda, VIDEO_BASE_ADDRESS); else draw_sprite(nave_parada, VIDEO_BASE_ADDRESS); */ nave_parada->x = new_x; nave_parada->y = new_y; move_sprite(nave_parada, new_x, new_y, VIDEO_BASE_ADDRESS); }
unsigned int mainMenu() { int selection; unsigned long int loopCount; DISPLAY_OFF; HIDE_BKG; move_bkg(0,0); set_bkg_palette( 7, 1, &menuPalCGB[28] ); set_bkg_palette( 6, 1, &menuPalCGB[24] ); set_bkg_palette( 5, 1, &menuPalCGB[20] ); set_bkg_palette( 4, 1, &menuPalCGB[16] ); set_bkg_palette( 3, 1, &menuPalCGB[12] ); set_bkg_palette( 2, 1, &menuPalCGB[8] ); set_bkg_palette( 1, 1, &menuPalCGB[4] ); set_bkg_palette( 0, 1, &menuPalCGB[0] ); set_bkg_data( 0, 141, menuTiles ); VBK_REG = 1; set_bkg_tiles(0,0,menuMapWidth,menuMapHeight,menuPal); VBK_REG = 0; set_bkg_tiles(0,0,menuMapWidth,menuMapHeight,menuMap); SHOW_BKG; DISPLAY_ON; setupPointer(); loopCount = 0; do { loopCount++; if((loopCount % 125) == 0) updatePointer(); /* animate the pointer */ selection = checkButtons(); } while(!selection); /* user hasn't made a selection */ set_sprite_tile(0, 0); /* set to straight pointer while waiting */ while(joypad() & (J_A | J_START)); /* wait for the user to let up */ /* "Shoot" the pointer across the screen */ loopCount = 0; while((pointerX + loopCount) <= 168) { move_sprite(0, pointerX + loopCount, pointerYPositions[currSelection]); loopCount++; delay(4); } hidePointer(); return selection; }
/** * Moves the box to a random position. */ void addBox() { boxcx = (UBYTE)rand() % MAPW; boxcy = (UBYTE)rand() % MAPH; boxx = boxcx * 16 + 24; boxy = boxcy * 16 + 28; move_sprite(4, boxx+4, boxy+8); }
main(){ UBYTE counter,x,y; x=40; y=17; disable_interrupts(); DISPLAY_OFF; /*load background * */ set_bkg_data(0,4,backgroundcharacters); for(counter=0;counter<=16;counter+=2) set_bkg_tiles( 0, counter, 20, 2, bgmap); //load sprite SPRITES_8x8; set_sprite_data(0, 1, spritetiles); set_sprite_tile(0,0); move_sprite(0,x,y); SHOW_BKG; SHOW_SPRITES; DISPLAY_ON; enable_interrupts(); while(1) { /* Skip four VBLs (slow down animation) */ for(counter = 0; counter < 4; counter++){ wait_vbl_done(); } counter = joypad(); if(counter & J_UP) y--; if(counter & J_DOWN) y++; if(counter & J_LEFT) x--; if(counter & J_RIGHT) x++; move_sprite(0,x,y); } }
/*************************************************** スプライト初期化 ***************************************************/ void init_sprite (void) { int i; //すべて画面外へ for (i = 0; i < MAX_SPRITE; i++) { move_sprite (i, SCREEN_WIDTH, SCREEN_HEIGHT); } }
void setCursor() { if(cursorColumnLast != cursorColumn) { if(cursorColumn>0xF0U) cursorColumn=0x03U; if(cursorColumn>0x03U) cursorColumn=0U; cursorEnable[cursorColumn]=1U; if(!joyState[6]) { cursorEnable[cursorColumnLast]=0U; } //cursorRow[cursorColumn] = cursorRowMain; cursorColumnLast = cursorColumn; } for(j=0;j!=4;j++) { if(shiftSelect && !joyState[6] && cursorColumn != j) { cursorEnable[j] = 0; } if(cursorEnable[j]) { if(cursorRow[j] > 0xF0U) cursorRow[j] = 0x08U; if(cursorRow[j] > 0x08U) cursorRow[j] = 0U; move_sprite(SPRITE_ARRT_START, cursorBigStartX[cursorColumn], cursorBigStartY[0]); move_sprite(j+SPRITE_ARRL_START, cursorStartX[j], (cursorRow[j] * SCREEN_YSPACE) + SCREEN_PSTARTY + SCREEN_YO); if(j==cursorColumn) cursorRowMain = cursorRow[j]; } } for(j=0;j!=4;j++) { if(!cursorEnable[j]) { cursorRow[j] = cursorRowMain; move_sprite(j+SPRITE_ARRL_START,0, 0); } } if(!joyState[6]) { shiftSelect = 0; } else { shiftSelect = 1; } printhelp(); }
void npc_setVisibility(EntityData *npc, UBYTE spriteIndex){ //if we are dying, we can keep the dying animation visible if (!(npc->dying)){ //if we are one layer off from the player, // we will be showing the shadow if ((npc->position.z == (player_data.position.z + 1)) || ((npc->position.z + 1) == player_data.position.z)){ npc->visibility = SHADOW; //cheaper than doing a Modulo npc->animFrame = 0;//npc->animFrame & (NUM_SHADOW_FRAMES - 1); tileSprite(spriteIndex, shadow_tiles[npc->animFrame], npc->type); } else if (npc->position.z != player_data.position.z){ npc->visibility = NONE; //hide the sprite move_sprite(spriteIndex, 0, 0); move_sprite(spriteIndex+1, 0, 0); } else { npc->visibility = FULL; tileSprite(spriteIndex, entity_tiles_ref[npc->type][npc->animFrame], npc->type); } } }
void main() { /* sets the complete sprite data from tile 0 for 1 tile of smile (data begins at tile 0 and ends after 1 tile) */ set_sprite_data(0,1,smile); /* sets tile number 0 to tile 0 of smile */ set_sprite_tile(0,0); /* moves sprite 0 to X-75 Y-75 */ move_sprite(0,75,75); SHOW_SPRITES; } /* end main */
static void move_hero(game_t *p_game, player_t *p_player) { move_sprite(p_game, &p_player->sprite); /* Check exit to the right/left */ if (p_player->sprite.pt.x < 0) { if (p_game->p_cur_level->p_adjacent_screens[LEFT]) level_goto(p_game, p_game->p_cur_level->p_adjacent_screens[LEFT], pt(300, p_player->sprite.pt.y) ); } else if (p_player->sprite.pt.x + p_player->sprite.p_bounds[p_player->sprite.frame].bottom_right.x > LEVEL_WIDTH-4) { if (p_game->p_cur_level->p_adjacent_screens[RIGHT]) level_goto(p_game, p_game->p_cur_level->p_adjacent_screens[RIGHT], pt(0, p_player->sprite.pt.y) ); } }
unsigned int checkButtons() { int joy,doMove; static unsigned int lastDir; joy = joypad(); doMove = 0; if(!joy)/*nothing was pressed */ { lastDir = 0; return 0; } else if(!(joy & J_DOWN || joy & J_SELECT || joy & J_UP)) lastDir = 0; if(joy & J_A || joy & J_START) { return currSelection; } if((joy & J_DOWN || joy & J_SELECT) && (lastDir != J_DOWN)) { currSelection++; if(currSelection > NUM_SELECTIONS) currSelection = 1; doMove = 1; lastDir = J_DOWN; } else if((joy & J_UP) && (lastDir != J_UP)) { currSelection--; if(currSelection <= 0) currSelection = NUM_SELECTIONS; doMove = 1; lastDir = J_UP; } if(doMove) { move_sprite(0, pointerX, pointerYPositions[currSelection]); } return 0; /* no final selection was made */ }
void Enemy::move_enemy(sf::Vector2f fullLength, string x, string y, float lengthOfLine) { cout << lengthOfLine << endl; // This whole thing works like this: // You have 2 sums. One is for keeping the current sum of both xs and ys (basically, where the object is at this moment) // And another sum, which keeps track of the previous iteration of sum (where the object was an iteration ago) // Then the program finds the difference between the current and previous sums and then gets the integer amount of that difference. // That difference is exactly the amount of pixels the object should move in the next iteration. // All of this is necessary because it`s not possible to move pixels by a float value (e.g. 3.45) // Here`s an example - you have to move 3.45 pixels as y coordinate, every iteration. This is how the sum will look like: // 3.45, 6.9, 10.35, 13.8, 17.25 and here`s how much y coordinate should be moved every iteration: // 3, 3, 4, 3, 4 and so on... movedSumBefore.y = movedSum.y; fractPartBefore.y = modf(movedSumBefore.y, &intPartBefore.y); movedSumBefore.x = movedSum.x; fractPartBefore.x = modf(movedSumBefore.x, &intPartBefore.x); // Now the sqrt(2) part is a bit different - the sqrt(2) itself is the ratio between moving in straight line and diagonally: // Example - if your movement speed is 10 pixels per frame, then by moving in a straight line, you`ll move 100 pixels in 10 frames. // But if you`re moving diagonally, you`ll move same 100 pixels in only 7 frames (because you move both x and y by 10, thus when you get to 100 pixels in length (hypotenuse), // You will have moved 71 pixels in X axis and 71 in Y, thus the divisor can be calculated ny taking square root from 2 (1 pixel x and 1 pixel y) // It also has to be divided by 2 (that`s how we get how much to move both x and y (identical, about 0.707 each) and finally by multiplying by movement speed, we get the // Amount of pixels the player should move with each frame if (y == "+") { movedSum.y += sqrt(2) / 2 * movementSpeed; } else if (y == "-") { movedSum.y -= sqrt(2) / 2 * movementSpeed; } fractPart.y = modf(movedSum.y, &intPart.y); if (x == "+") { movedSum.x += sqrt(2) / 2 * movementSpeed; } else if (x == "-") { movedSum.x -= sqrt(2) / 2 * movementSpeed; } fractPart.x = modf(movedSum.x, &intPart.x); timesToMoveInteger.y = intPart.y - intPartBefore.y; timesToMoveInteger.x = intPart.x - intPartBefore.x; if (lengthOfLine >= distanceFromPlayer) { // Move the enemy and it`s health bar move_sprite(timesToMoveInteger); moveHealthBar(); } }
void setupPointer() { SPRITES_8x8; currSelection = MENU_STORYLINE; pointerTile = 1; /* the current tile for the pointer */ set_sprite_palette(0, 1, &pointerPalCGB[0]); set_sprite_data(0, 9, pointerTiles); set_sprite_tile(0, pointerTile); set_sprite_prop(0, pointerTilesCGB[pointerTile]); move_sprite(0, pointerX, pointerYPositions[currSelection]); SHOW_SPRITES; }
int jump() /* jump function */ { for(q=0;q<12;q++) { if(key==J_A+J_RIGHT){x++;} if(key==J_A+J_LEFT){x--;} for(p=0;p<2;p++) { y--; if(collide(x,y)) { if(key==J_A+J_RIGHT){x--; y++; return 0;} if(key==J_A+J_LEFT){x++; y++; return 0;} y++; return 0; } move_sprite(0,x,y); delay(7); } } return 1; }
//----------------------------------------Game Loop int main() { SetupGame(); //this is the game loop. this loops forever. while (1) { SHOW_SPRITES; //monster stuff MonsterUpdate(SpriteNum); //player stuff num = PlayerCheckControls(SpriteNum); if (num == 1) //then a bullet was instantiated { SpriteNum++; PlayerSlideBack(); PlayerSlideBack(); PlayerSlideBack(); PlayerSlideBack(); PlayerUpdate(SpriteNum); HIDE_SPRITES; //clear unused sprites } num = PlayerUpdate(SpriteNum); if (num == 1) //then a bullet died { set_sprite_tile(SpriteNum, 5); //hide the players current sprite SpriteNum--; move_sprite(SpriteNum, P_PosX, P_PosY); //and show the new } //woah there sparky delay(20); } }
void empty_screen (void) { UBYTE temp1; //DISPLAY_OFF; HIDE_BKG; HIDE_SPRITES; VBK_REG = 0; ENABLE_RAM_MBC1; for (temp1=0; temp1<40; temp1++) { move_sprite(temp1,0,200); set_sprite_prop (temp1,0); } SWITCH_ROM_MBC1(4); for (temp1=0; temp1<20; temp1++) { set_bkg_tiles (0,temp1,20,1,(unsigned char *) &cEMPTY[0]); set_win_tiles (0,temp1,20,1,(unsigned char *) &cEMPTY[0]); VBK_REG = 1; set_bkg_tiles (0,temp1,20,1,(unsigned char *) &cEMPTY[0]); set_win_tiles (0,temp1,20,1,(unsigned char *) &cEMPTY[0]); VBK_REG = 0; } SWITCH_ROM_MBC1(0); DISABLE_RAM_MBC1; move_bkg (0,0); SHOW_BKG; SHOW_SPRITES; //DISPLAY_ON; }