summaryrefslogtreecommitdiff
path: root/apps
diff options
context:
space:
mode:
authorTomer Shalev <shalev.tomer@gmail.com>2010-02-27 18:23:46 +0000
committerTomer Shalev <shalev.tomer@gmail.com>2010-02-27 18:23:46 +0000
commitce8b00d5405a897b53fbc23ad5b9b051925f45cb (patch)
treee792eec496f229238b26b3b82736a75c958ba471 /apps
parent722ae978361cbe6c8d74d6af8d202aa52f840366 (diff)
downloadrockbox-ce8b00d5405a897b53fbc23ad5b9b051925f45cb.tar.gz
rockbox-ce8b00d5405a897b53fbc23ad5b9b051925f45cb.zip
Brickmania: Use helper function check_rect to test whether ball hits brick
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@24948 a1c6a512-1295-4272-9138-f99709370657
Diffstat (limited to 'apps')
-rw-r--r--apps/plugins/brickmania.c141
1 files changed, 94 insertions, 47 deletions
diff --git a/apps/plugins/brickmania.c b/apps/plugins/brickmania.c
index f932e4563a..1dd74f35cd 100644
--- a/apps/plugins/brickmania.c
+++ b/apps/plugins/brickmania.c
@@ -899,6 +899,15 @@ enum paddle_type
899 PADDLE_TYPE_SHOOTER, 899 PADDLE_TYPE_SHOOTER,
900}; 900};
901 901
902enum intersection
903{
904 INTERSECTION_NONE = 0,
905 INTERSECTION_TOP,
906 INTERSECTION_BOTTOM,
907 INTERSECTION_LEFT,
908 INTERSECTION_RIGHT,
909};
910
902struct brick 911struct brick
903{ 912{
904 bool used; /* Is the brick still in play? */ 913 bool used; /* Is the brick still in play? */
@@ -948,6 +957,11 @@ struct line
948 struct point p2; 957 struct point p2;
949}; 958};
950 959
960struct rect
961{
962 struct point top_left;
963 struct point bottom_right;
964};
951 965
952/* 966/*
953 * 967 *
@@ -1085,6 +1099,61 @@ static int check_lines(struct line *line1, struct line *line2,
1085 return 0; 1099 return 0;
1086} 1100}
1087 1101
1102
1103static int check_rect(struct line *line, struct rect *rect,
1104 enum intersection intersection, struct point *hitp)
1105{
1106 struct line edge;
1107
1108 switch (intersection)
1109 {
1110 case INTERSECTION_TOP:
1111 {
1112 edge.p1.x = rect->top_left.x;
1113 edge.p1.y = rect->top_left.y;
1114
1115 edge.p2.x = rect->bottom_right.x;
1116 edge.p2.y = rect->top_left.y;
1117
1118 break;
1119 }
1120 case INTERSECTION_BOTTOM:
1121 {
1122 edge.p1.x = rect->top_left.x;
1123 edge.p1.y = rect->bottom_right.y;
1124
1125 edge.p2.x = rect->bottom_right.x;
1126 edge.p2.y = rect->bottom_right.y;
1127
1128 break;
1129 }
1130 case INTERSECTION_LEFT:
1131 {
1132 edge.p1.x = rect->top_left.x;
1133 edge.p1.y = rect->top_left.y;
1134
1135 edge.p2.x = rect->top_left.x;
1136 edge.p2.y = rect->bottom_right.y;
1137
1138 break;
1139 }
1140 case INTERSECTION_RIGHT:
1141 {
1142 edge.p1.x = rect->bottom_right.x;
1143 edge.p1.y = rect->top_left.y;
1144
1145 edge.p2.x = rect->bottom_right.x;
1146 edge.p2.y = rect->bottom_right.y;
1147
1148 break;
1149 }
1150 default:
1151 return 0; /* shouldn't reach here */
1152 }
1153
1154 return check_lines(line, &edge, hitp);
1155}
1156
1088static void brickmania_init_game(bool new_game) 1157static void brickmania_init_game(bool new_game)
1089{ 1158{
1090 int i,j; 1159 int i,j;
@@ -1740,39 +1809,16 @@ static int brickmania_game_loop(void)
1740 /* The brick is a brick, but it may or may not be in use */ 1809 /* The brick is a brick, but it may or may not be in use */
1741 if(brick[i][j].used) 1810 if(brick[i][j].used)
1742 { 1811 {
1743 /* these lines are used to describe the brick */ 1812 struct rect brick_rect;
1744 struct line bot_brick, top_brick, left_brick, rght_brick; 1813
1745 brickx = LEFTMARGIN + j*BRICK_WIDTH; 1814 brickx = LEFTMARGIN + j*BRICK_WIDTH;
1746 bricky = TOPMARGIN + i*BRICK_HEIGHT; 1815 bricky = TOPMARGIN + i*BRICK_HEIGHT;
1747 1816
1748 /* Describe the brick for later collision checks */ 1817 brick_rect.top_left.x = brickx;
1749 /* Setup the bottom of the brick */ 1818 brick_rect.top_left.y = bricky;
1750 bot_brick.p1.x = brickx;
1751 bot_brick.p1.y = bricky + BRICK_HEIGHT;
1752
1753 bot_brick.p2.x = brickx + BRICK_WIDTH;
1754 bot_brick.p2.y = bricky + BRICK_HEIGHT;
1755 1819
1756 /* Setup the top of the brick */ 1820 brick_rect.bottom_right.x = brickx + BRICK_WIDTH;
1757 top_brick.p1.x = brickx; 1821 brick_rect.bottom_right.y = bricky + BRICK_HEIGHT;
1758 top_brick.p1.y = bricky;
1759
1760 top_brick.p2.x = brickx + BRICK_WIDTH;
1761 top_brick.p2.y = bricky;
1762
1763 /* Setup the left of the brick */
1764 left_brick.p1.x = brickx;
1765 left_brick.p1.y = bricky;
1766
1767 left_brick.p2.x = brickx;
1768 left_brick.p2.y = bricky + BRICK_HEIGHT;
1769
1770 /* Setup the right of the brick */
1771 rght_brick.p1.x = brickx + BRICK_WIDTH;
1772 rght_brick.p1.y = bricky;
1773
1774 rght_brick.p2.x = brickx + BRICK_WIDTH;
1775 rght_brick.p2.y = bricky + BRICK_HEIGHT;
1776 1822
1777 /* Draw the brick */ 1823 /* Draw the brick */
1778 rb->lcd_bitmap_part(brickmania_bricks,0, 1824 rb->lcd_bitmap_part(brickmania_bricks,0,
@@ -1799,6 +1845,8 @@ static int brickmania_game_loop(void)
1799 /* Check if any balls collided with the brick */ 1845 /* Check if any balls collided with the brick */
1800 for(k=0; k<used_balls; k++) 1846 for(k=0; k<used_balls; k++)
1801 { 1847 {
1848 int hit = 0;
1849
1802 /* Setup the ball path to describe the current ball 1850 /* Setup the ball path to describe the current ball
1803 * position and the line it makes to its next 1851 * position and the line it makes to its next
1804 * position. 1852 * position.
@@ -1818,39 +1866,38 @@ static int brickmania_game_loop(void)
1818 * Note that tempx/tempy store the next position 1866 * Note that tempx/tempy store the next position
1819 * that the ball should be drawn. 1867 * that the ball should be drawn.
1820 */ 1868 */
1821 if(ball[k].speedy <= 0 && 1869 if (ball[k].speedy <= 0 && check_rect(&misc_line,
1822 check_lines(&misc_line, &bot_brick, &pt_hit)) 1870 &brick_rect, INTERSECTION_BOTTOM, &pt_hit))
1823 { 1871 {
1824 ball[k].speedy = -ball[k].speedy; 1872 ball[k].speedy = -ball[k].speedy;
1825 ball[k].tempy = pt_hit.y; 1873 hit = 1;
1826 ball[k].tempx = pt_hit.x;
1827 brick_hit(i, j);
1828 } 1874 }
1829 /* Check the top, if the ball is moving up dont 1875 /* Check the top, if the ball is moving up dont
1830 * count it as a hit. 1876 * count it as a hit.
1831 */ 1877 */
1832 else if(ball[k].speedy > 0 && 1878 else if (ball[k].speedy > 0 && check_rect(&misc_line,
1833 check_lines(&misc_line, &top_brick, &pt_hit)) 1879 &brick_rect, INTERSECTION_TOP, &pt_hit))
1834 { 1880 {
1835 ball[k].speedy = -ball[k].speedy; 1881 ball[k].speedy = -ball[k].speedy;
1836 ball[k].tempy = pt_hit.y; 1882 hit = 1;
1837 ball[k].tempx = pt_hit.x;
1838 brick_hit(i, j);
1839 } 1883 }
1840 /* Check the left side of the brick */ 1884 /* Check the left side of the brick */
1841 else if( 1885 else if (check_rect(&misc_line, &brick_rect,
1842 check_lines(&misc_line, &left_brick, &pt_hit)) 1886 INTERSECTION_LEFT, &pt_hit))
1843 { 1887 {
1844 ball[k].speedx = -ball[k].speedx; 1888 ball[k].speedx = -ball[k].speedx;
1845 ball[k].tempy = pt_hit.y; 1889 hit = 1;
1846 ball[k].tempx = pt_hit.x;
1847 brick_hit(i, j);
1848 } 1890 }
1849 /* Check the right side of the brick */ 1891 /* Check the right side of the brick */
1850 else if( 1892 else if (check_rect(&misc_line, &brick_rect,
1851 check_lines(&misc_line, &rght_brick, &pt_hit)) 1893 INTERSECTION_RIGHT, &pt_hit))
1852 { 1894 {
1853 ball[k].speedx = -ball[k].speedx; 1895 ball[k].speedx = -ball[k].speedx;
1896 hit = 1;
1897 }
1898
1899 if (hit)
1900 {
1854 ball[k].tempy = pt_hit.y; 1901 ball[k].tempy = pt_hit.y;
1855 ball[k].tempx = pt_hit.x; 1902 ball[k].tempx = pt_hit.x;
1856 brick_hit(i, j); 1903 brick_hit(i, j);
@@ -1917,7 +1964,7 @@ static int brickmania_game_loop(void)
1917 /* the test for pos_y prevents the ball from bouncing back 1964 /* the test for pos_y prevents the ball from bouncing back
1918 * from _over_ the top to infinity on some rare cases */ 1965 * from _over_ the top to infinity on some rare cases */
1919 if (ball[k].pos_y > 0 && 1966 if (ball[k].pos_y > 0 &&
1920 check_lines(&misc_line, &screen_edge, &pt_hit)) 1967 check_lines(&misc_line, &screen_edge, &pt_hit))
1921 { 1968 {
1922 ball[k].tempy = pt_hit.y + 1; 1969 ball[k].tempy = pt_hit.y + 1;
1923 ball[k].tempx = pt_hit.x; 1970 ball[k].tempx = pt_hit.x;