Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <tonc.h>
- #include "brin.h"
- #include "pill.h"
- #include "smallpill.h"
- #include "collision_map.h"
- #include "pill_collision.h"
- ///////////////////////////////foward declarations////////////////////////////////
- void draw();
- bool collision(int x, int y);
- void init();
- void input();
- void obj_update();
- ///////////////////////////////////globals//////////////////////////////////////////
- OBJ_ATTR obj_buffer[128];
- OBJ_AFFINE *obj_aff_buffer= (OBJ_AFFINE*)obj_buffer;
- OBJ_ATTR *pill= &obj_buffer[0];
- int game_state = 1;
- int pill_size = 2;
- int pill_animation = 0;
- int player_x;
- int player_y;
- int player_height = 16;
- int player_width = 16;
- int screen_width = 240;
- int screen_height= 160;
- int backgound1_x = 0;
- int backgound1_y = 0;
- unsigned short world_grid[64][32];
- ////////////////////////////////////////////draw///////////////////////////////////////////////
- void draw()
- {
- REG_BG1HOFS= backgound1_x;
- REG_BG1VOFS= backgound1_y;
- tte_write("#{P:0,0}");
- if (pill_size == 2)
- {
- tte_write("Big Pill");
- }
- else
- {
- tte_write("Small Pill");
- }
- }
- /////////////////////////////////////////////collision//////////////////////////////////////////
- bool collision(int x, int y)
- {
- int sprite_x,sprite_y;
- for(sprite_x =0; sprite_x<16; sprite_x++)
- {
- for(sprite_y =0; sprite_y<16; sprite_y++)
- {
- if((pill_collisionBitmap[sprite_x+sprite_y*16])&&
- (collision_mapBitmap[(sprite_x+x)+(sprite_y+y)*512]))
- {
- return true;
- }
- }
- }
- return false;
- }
- /////////////////////////////////////////////init//////////////////////////////////////////////
- void init()
- {
- backgound1_x = 0;
- backgound1_y = 0;
- //player_x = 14/pill_size;
- //player_y = 8/pill_size;
- player_x = 14*8;
- player_y = 8*8;
- memcpy(pal_bg_mem, brinPal, brinPalLen);
- // Load tiles into CBB 0
- memcpy(&tile_mem[0][0], brinTiles, brinTilesLen);
- // Load map into SBB 30
- memcpy(&se_mem[30][0], brinMap, brinMapLen);
- if(pill_size == 2)
- {
- memcpy(&tile_mem[4][0], pillTiles, pillTilesLen);
- memcpy(pal_obj_mem, pillPal, pillPalLen);
- }
- else
- {
- memcpy(&tile_mem[4][0], smallpillTiles, smallpillTilesLen);
- memcpy(pal_obj_mem, smallpillPal, smallpillPalLen);
- }
- oam_init(obj_buffer, 128);
- REG_DISPCNT= DCNT_MODE0 | DCNT_BG0 | DCNT_BG1 | DCNT_OBJ | DCNT_OBJ_1D;
- REG_BG1CNT= BG_CBB(0) | BG_SBB(30) | BG_4BPP | BG_REG_64x32;
- // Init BG 0 for text on screen entries.
- tte_init_se_default(0, BG_CBB(1)|BG_SBB(29));
- //initialise world grid
- int xx,yy, map;
- for(map = 0; map<2; map++)//the map is 64X32 effectively 2 32X32 maps to be addressed separately
- {
- for(yy=0;yy<32;yy++)
- {
- for(xx=0;xx<32;xx++)
- {
- world_grid[xx+32*map][yy] = brinMap[xx+yy*32+1024*map];
- }
- }
- }
- /*alternative version, does the same thing (faster, less readable)
- for(yy=0;yy<32;yy++)
- {
- for(xx=0;xx<64;xx++)
- {
- if(xx < 32)
- {
- world_grid[xx][yy] = brinMap[xx+yy*32];
- }
- else
- {
- world_grid[xx][yy] = brinMap[xx-32+yy*32+1024];
- }
- }
- }*/
- }
- ////////////////////////////////////////////input//////////////////////////////////////////////
- void input()
- {
- key_poll();
- /*
- ///////////////////////////////UP
- if ((key_is_down(KEY_UP))&&//the player is trying to go up
- (!world_grid[player_x*pill_size][(player_y-1)*pill_size])&&// check if the space is free
- (player_y>(4/pill_size)))//and is within the world limits
- {
- backgound1_y-=8*pill_size;
- player_y--;
- }
- //////////////////////////////DOWN
- if ((key_is_down(KEY_DOWN))&&//the player is trying to go down
- (!world_grid[player_x*pill_size][(player_y+1)*pill_size])&&// check if the space is free
- (player_y<((26/pill_size)-1)))//and is within the world limits
- {
- backgound1_y+=8*pill_size;
- player_y++;
- }
- //////////////////////////////RIGHT
- if ((key_is_down(KEY_RIGHT))&&//the player is trying to go right
- (!world_grid[(player_x+1)*pill_size][player_y*pill_size])&&// check if the space is free
- (player_x<((64/pill_size)-1)))//and is within the world limits
- {
- backgound1_x+=8*pill_size;
- player_x++;
- pill_animation = 0;
- }
- //////////////////////////////LEFT
- if ((key_is_down(KEY_LEFT))&&//the player is trying to go left
- (!world_grid[(player_x-1)*pill_size][player_y*pill_size])&&// check if the space is free
- (player_x>(6/pill_size)))//and is within the world limits
- {
- backgound1_x-=8*pill_size;
- player_x--;
- pill_animation = 1;
- }*/
- ///////////////////////////////UP
- if ((key_is_down(KEY_UP))&&//the player is trying to go up
- (!collision(player_x,player_y-1)))
- {
- backgound1_y--;
- player_y--;
- }
- //////////////////////////////DOWN
- if ((key_is_down(KEY_DOWN))&&//the player is trying to go down
- (!collision(player_x,player_y+1)))
- {
- backgound1_y++;
- player_y++;
- }
- //////////////////////////////RIGHT
- if ((key_is_down(KEY_RIGHT))&&//the player is trying to go right
- (!collision(player_x+1,player_y)))
- {
- backgound1_x++;
- player_x++;
- }
- //////////////////////////////LEFT
- if ((key_is_down(KEY_LEFT))&&//the player is trying to go left
- (!collision(player_x-1,player_y)))
- {
- backgound1_x--;
- player_x--;
- }
- if(key_hit(KEY_SELECT))
- {
- /*
- game_state=0;
- tte_write("#{P:0,0}");
- tte_write("#{es}");
- OAM_CLEAR();//clear the sprites
- memset(&tile_mem[4][0], 0, pillTilesLen);
- memset(pal_obj_mem, 0, pillPalLen);
- REG_DISPCNT= DCNT_MODE0 | DCNT_BG0;//disable background 1
- */
- }
- }
- ////////////////////////////////////////////obj_update/////////////////////////////////////////
- void obj_update()
- {
- //int x= 96, y= 32;
- int x = (screen_width - player_width)/2;
- int y = (screen_height)/2 - player_height;
- u32 tid= 0, pb= 0; // tile id, pal-bank
- if(pill_size == 2)
- {
- obj_set_attr(pill,
- ATTR0_SQUARE, // Square, regular sprite
- ATTR1_SIZE_16, // 16x16p,
- ATTR2_PALBANK(pb) | tid); // palbank 0, tile 0
- }
- else
- {
- obj_set_attr(pill,
- ATTR0_SQUARE, // Square, regular sprite
- ATTR1_SIZE_8, // 8x8p,
- ATTR2_PALBANK(pb) | tid); // palbank 0, tile 0
- }
- if (pill_animation == 1)
- {
- pill->attr1 ^= ATTR1_HFLIP;
- }
- // Hey look, it's one of them build macros!
- pill->attr2= ATTR2_BUILD(tid, pb, 0);
- obj_set_pos(pill, x, y);
- oam_copy(oam_mem, obj_buffer, 1); // only need to update one
- }
- ////////////////////////////////////////main/////////////////////////////////////
- int main()
- {
- // enable isr switchboard and VBlank interrupt
- irq_init(NULL);
- irq_add(II_VBLANK, NULL);
- int selection = 2;
- REG_DISPCNT= DCNT_MODE0 | DCNT_BG0;
- // Init BG 0 for text on screen entries.
- tte_init_se_default(0, BG_CBB(1)|BG_SBB(29));
- /////////////////////////skip the start screen///////////////////
- game_state = 1;//start the game
- pill_size = selection;
- init();
- tte_write("#{es}");//clear the screen
- /////////////////////////////////////////////////////////////////
- while(1)
- {
- switch (game_state)
- {
- case 0:///////////////////////////state 0(start screen)//////////////////////////////
- /////////////////////////skip the start screen///////////////////
- game_state = 1;//start the game
- pill_size = selection;
- init();
- tte_write("#{es}");//clear the screen
- break;
- /////////////////////////////////////////////////////////////////
- while(1)
- {
- //vid_vsync();
- VBlankIntrWait();
- key_poll();
- if (selection == 2)
- {
- tte_write("#{P:68,64}");
- tte_write(" ->Big Pill<- ");
- tte_write("#{P:68,72}");
- tte_write(" Small Pill ");
- }
- else
- {
- tte_write("#{P:68,64}");
- tte_write(" Big Pill ");
- tte_write("#{P:68,72}");
- tte_write("->Small Pill<- ");
- }
- if(key_hit(KEY_UP))
- {
- selection = 2;
- }
- if(key_hit(KEY_DOWN))
- {
- selection = 1;
- }
- if(key_hit(KEY_START))
- {
- game_state = 1;//start the game
- pill_size = selection;
- init();
- tte_write("#{es}");//clear the screen
- break;//leave infinite loop
- }
- }
- break;
- case 1://////////////////////////////state 1 game play//////////////////////////
- //game loop
- while(1)
- {
- //vid_vsync(); better to use the interrupt version
- VBlankIntrWait();
- input();
- if(game_state == 0)
- {
- break;
- }
- obj_update();
- draw();
- }
- break;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment