Crosswar

GBA game

May 23rd, 2013
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.79 KB | None | 0 0
  1.  
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <tonc.h>
  5.  
  6. #include "brin.h"
  7. #include "pill.h"
  8. #include "smallpill.h"
  9. #include "collision_map.h"
  10. #include "pill_collision.h"
  11.  
  12. ///////////////////////////////foward declarations////////////////////////////////
  13.  
  14. void draw();
  15. bool collision(int x, int y);
  16. void init();
  17. void input();
  18. void obj_update();
  19.  
  20. ///////////////////////////////////globals//////////////////////////////////////////
  21.  
  22. OBJ_ATTR obj_buffer[128];
  23. OBJ_AFFINE *obj_aff_buffer= (OBJ_AFFINE*)obj_buffer;
  24.  
  25. OBJ_ATTR *pill= &obj_buffer[0];
  26.  
  27. int game_state = 1;
  28.  
  29. int pill_size = 2;
  30.  
  31. int pill_animation = 0;
  32.  
  33.  
  34. int player_x;
  35. int player_y;
  36. int player_height = 16;
  37. int player_width = 16;
  38.  
  39. int screen_width = 240;
  40. int screen_height= 160;
  41.  
  42. int backgound1_x = 0;
  43. int backgound1_y = 0;
  44.  
  45. unsigned short world_grid[64][32];
  46.  
  47. ////////////////////////////////////////////draw///////////////////////////////////////////////
  48.  
  49. void draw()
  50. {
  51. REG_BG1HOFS= backgound1_x;
  52. REG_BG1VOFS= backgound1_y;
  53.  
  54. tte_write("#{P:0,0}");
  55.  
  56.  
  57. if (pill_size == 2)
  58. {
  59. tte_write("Big Pill");
  60. }
  61. else
  62. {
  63. tte_write("Small Pill");
  64. }
  65. }
  66.  
  67. /////////////////////////////////////////////collision//////////////////////////////////////////
  68.  
  69. bool collision(int x, int y)
  70. {
  71. int sprite_x,sprite_y;
  72.  
  73.  
  74. for(sprite_x =0; sprite_x<16; sprite_x++)
  75. {
  76. for(sprite_y =0; sprite_y<16; sprite_y++)
  77. {
  78. if((pill_collisionBitmap[sprite_x+sprite_y*16])&&
  79. (collision_mapBitmap[(sprite_x+x)+(sprite_y+y)*512]))
  80. {
  81. return true;
  82. }
  83. }
  84. }
  85.  
  86. return false;
  87. }
  88.  
  89.  
  90. /////////////////////////////////////////////init//////////////////////////////////////////////
  91.  
  92. void init()
  93. {
  94. backgound1_x = 0;
  95. backgound1_y = 0;
  96.  
  97. //player_x = 14/pill_size;
  98. //player_y = 8/pill_size;
  99.  
  100. player_x = 14*8;
  101. player_y = 8*8;
  102.  
  103.  
  104. memcpy(pal_bg_mem, brinPal, brinPalLen);
  105. // Load tiles into CBB 0
  106. memcpy(&tile_mem[0][0], brinTiles, brinTilesLen);
  107. // Load map into SBB 30
  108. memcpy(&se_mem[30][0], brinMap, brinMapLen);
  109.  
  110.  
  111. if(pill_size == 2)
  112. {
  113. memcpy(&tile_mem[4][0], pillTiles, pillTilesLen);
  114. memcpy(pal_obj_mem, pillPal, pillPalLen);
  115. }
  116. else
  117. {
  118. memcpy(&tile_mem[4][0], smallpillTiles, smallpillTilesLen);
  119. memcpy(pal_obj_mem, smallpillPal, smallpillPalLen);
  120. }
  121.  
  122. oam_init(obj_buffer, 128);
  123. REG_DISPCNT= DCNT_MODE0 | DCNT_BG0 | DCNT_BG1 | DCNT_OBJ | DCNT_OBJ_1D;
  124. REG_BG1CNT= BG_CBB(0) | BG_SBB(30) | BG_4BPP | BG_REG_64x32;
  125.  
  126. // Init BG 0 for text on screen entries.
  127. tte_init_se_default(0, BG_CBB(1)|BG_SBB(29));
  128.  
  129.  
  130. //initialise world grid
  131. int xx,yy, map;
  132. for(map = 0; map<2; map++)//the map is 64X32 effectively 2 32X32 maps to be addressed separately
  133. {
  134. for(yy=0;yy<32;yy++)
  135. {
  136. for(xx=0;xx<32;xx++)
  137. {
  138. world_grid[xx+32*map][yy] = brinMap[xx+yy*32+1024*map];
  139. }
  140. }
  141. }
  142.  
  143. /*alternative version, does the same thing (faster, less readable)
  144. for(yy=0;yy<32;yy++)
  145. {
  146. for(xx=0;xx<64;xx++)
  147. {
  148. if(xx < 32)
  149. {
  150. world_grid[xx][yy] = brinMap[xx+yy*32];
  151. }
  152. else
  153. {
  154. world_grid[xx][yy] = brinMap[xx-32+yy*32+1024];
  155. }
  156. }
  157. }*/
  158. }
  159. ////////////////////////////////////////////input//////////////////////////////////////////////
  160.  
  161. void input()
  162. {
  163. key_poll();
  164.  
  165. /*
  166. ///////////////////////////////UP
  167. if ((key_is_down(KEY_UP))&&//the player is trying to go up
  168. (!world_grid[player_x*pill_size][(player_y-1)*pill_size])&&// check if the space is free
  169. (player_y>(4/pill_size)))//and is within the world limits
  170. {
  171. backgound1_y-=8*pill_size;
  172. player_y--;
  173. }
  174.  
  175. //////////////////////////////DOWN
  176. if ((key_is_down(KEY_DOWN))&&//the player is trying to go down
  177. (!world_grid[player_x*pill_size][(player_y+1)*pill_size])&&// check if the space is free
  178. (player_y<((26/pill_size)-1)))//and is within the world limits
  179. {
  180. backgound1_y+=8*pill_size;
  181. player_y++;
  182. }
  183.  
  184. //////////////////////////////RIGHT
  185. if ((key_is_down(KEY_RIGHT))&&//the player is trying to go right
  186. (!world_grid[(player_x+1)*pill_size][player_y*pill_size])&&// check if the space is free
  187. (player_x<((64/pill_size)-1)))//and is within the world limits
  188. {
  189. backgound1_x+=8*pill_size;
  190. player_x++;
  191. pill_animation = 0;
  192. }
  193.  
  194. //////////////////////////////LEFT
  195. if ((key_is_down(KEY_LEFT))&&//the player is trying to go left
  196. (!world_grid[(player_x-1)*pill_size][player_y*pill_size])&&// check if the space is free
  197. (player_x>(6/pill_size)))//and is within the world limits
  198. {
  199. backgound1_x-=8*pill_size;
  200. player_x--;
  201. pill_animation = 1;
  202. }*/
  203.  
  204. ///////////////////////////////UP
  205. if ((key_is_down(KEY_UP))&&//the player is trying to go up
  206. (!collision(player_x,player_y-1)))
  207. {
  208. backgound1_y--;
  209. player_y--;
  210. }
  211.  
  212. //////////////////////////////DOWN
  213. if ((key_is_down(KEY_DOWN))&&//the player is trying to go down
  214. (!collision(player_x,player_y+1)))
  215. {
  216. backgound1_y++;
  217. player_y++;
  218. }
  219.  
  220. //////////////////////////////RIGHT
  221. if ((key_is_down(KEY_RIGHT))&&//the player is trying to go right
  222. (!collision(player_x+1,player_y)))
  223. {
  224. backgound1_x++;
  225. player_x++;
  226. }
  227.  
  228. //////////////////////////////LEFT
  229. if ((key_is_down(KEY_LEFT))&&//the player is trying to go left
  230. (!collision(player_x-1,player_y)))
  231. {
  232. backgound1_x--;
  233. player_x--;
  234. }
  235.  
  236. if(key_hit(KEY_SELECT))
  237. {
  238. /*
  239. game_state=0;
  240. tte_write("#{P:0,0}");
  241. tte_write("#{es}");
  242.  
  243. OAM_CLEAR();//clear the sprites
  244. memset(&tile_mem[4][0], 0, pillTilesLen);
  245. memset(pal_obj_mem, 0, pillPalLen);
  246.  
  247. REG_DISPCNT= DCNT_MODE0 | DCNT_BG0;//disable background 1
  248. */
  249. }
  250.  
  251. }
  252.  
  253. ////////////////////////////////////////////obj_update/////////////////////////////////////////
  254.  
  255. void obj_update()
  256. {
  257. //int x= 96, y= 32;
  258. int x = (screen_width - player_width)/2;
  259. int y = (screen_height)/2 - player_height;
  260.  
  261. u32 tid= 0, pb= 0; // tile id, pal-bank
  262.  
  263. if(pill_size == 2)
  264. {
  265.  
  266. obj_set_attr(pill,
  267. ATTR0_SQUARE, // Square, regular sprite
  268. ATTR1_SIZE_16, // 16x16p,
  269. ATTR2_PALBANK(pb) | tid); // palbank 0, tile 0
  270. }
  271. else
  272. {
  273. obj_set_attr(pill,
  274. ATTR0_SQUARE, // Square, regular sprite
  275. ATTR1_SIZE_8, // 8x8p,
  276. ATTR2_PALBANK(pb) | tid); // palbank 0, tile 0
  277. }
  278.  
  279. if (pill_animation == 1)
  280. {
  281. pill->attr1 ^= ATTR1_HFLIP;
  282. }
  283.  
  284.  
  285. // Hey look, it's one of them build macros!
  286. pill->attr2= ATTR2_BUILD(tid, pb, 0);
  287. obj_set_pos(pill, x, y);
  288.  
  289. oam_copy(oam_mem, obj_buffer, 1); // only need to update one
  290.  
  291. }
  292.  
  293. ////////////////////////////////////////main/////////////////////////////////////
  294.  
  295. int main()
  296. {
  297.  
  298. // enable isr switchboard and VBlank interrupt
  299. irq_init(NULL);
  300. irq_add(II_VBLANK, NULL);
  301.  
  302. int selection = 2;
  303.  
  304. REG_DISPCNT= DCNT_MODE0 | DCNT_BG0;
  305.  
  306. // Init BG 0 for text on screen entries.
  307. tte_init_se_default(0, BG_CBB(1)|BG_SBB(29));
  308.  
  309. /////////////////////////skip the start screen///////////////////
  310.  
  311. game_state = 1;//start the game
  312. pill_size = selection;
  313. init();
  314. tte_write("#{es}");//clear the screen
  315.  
  316. /////////////////////////////////////////////////////////////////
  317.  
  318. while(1)
  319. {
  320.  
  321. switch (game_state)
  322. {
  323. case 0:///////////////////////////state 0(start screen)//////////////////////////////
  324.  
  325. /////////////////////////skip the start screen///////////////////
  326.  
  327. game_state = 1;//start the game
  328. pill_size = selection;
  329. init();
  330. tte_write("#{es}");//clear the screen
  331.  
  332. break;
  333. /////////////////////////////////////////////////////////////////
  334.  
  335. while(1)
  336. {
  337.  
  338. //vid_vsync();
  339. VBlankIntrWait();
  340. key_poll();
  341.  
  342.  
  343.  
  344. if (selection == 2)
  345. {
  346. tte_write("#{P:68,64}");
  347. tte_write(" ->Big Pill<- ");
  348.  
  349. tte_write("#{P:68,72}");
  350. tte_write(" Small Pill ");
  351. }
  352. else
  353. {
  354. tte_write("#{P:68,64}");
  355. tte_write(" Big Pill ");
  356.  
  357. tte_write("#{P:68,72}");
  358. tte_write("->Small Pill<- ");
  359. }
  360.  
  361. if(key_hit(KEY_UP))
  362. {
  363. selection = 2;
  364. }
  365.  
  366. if(key_hit(KEY_DOWN))
  367. {
  368. selection = 1;
  369. }
  370.  
  371. if(key_hit(KEY_START))
  372. {
  373. game_state = 1;//start the game
  374. pill_size = selection;
  375. init();
  376. tte_write("#{es}");//clear the screen
  377.  
  378. break;//leave infinite loop
  379. }
  380.  
  381.  
  382. }
  383. break;
  384. case 1://////////////////////////////state 1 game play//////////////////////////
  385.  
  386.  
  387. //game loop
  388. while(1)
  389. {
  390. //vid_vsync(); better to use the interrupt version
  391. VBlankIntrWait();
  392.  
  393. input();
  394.  
  395. if(game_state == 0)
  396. {
  397. break;
  398. }
  399.  
  400. obj_update();
  401.  
  402. draw();
  403.  
  404. }
  405.  
  406.  
  407. break;
  408. }
  409. }
  410.  
  411. return 0;
  412. }
Advertisement
Add Comment
Please, Sign In to add comment