Bullet is the name of the game I'm making, but the code is being spo annoying. I can't figure out what's wrong. Help please:


Code:
#include "keyboard.hpp"   
#include "color.h"   
#include "display.h"   
#include "keyboard_syscalls.h"   
#include "PictureData.h"   
#include "stdlib.h"

void CopySprite(const void* datar, int x, int y, int width, int height) {     
   color_t*data = (color_t*) datar;     
   color_t* VRAM = (color_t*)0xA8000000;     
   VRAM += LCD_WIDTH_PX*y + x;     
   for(int j=y; j<y+height; j++) {     
      for(int i=x; i<x+width; i++) {     
         *(VRAM++) = *(data++);     
     }     
     VRAM += LCD_WIDTH_PX-width;     
   }     
}   

void CopySpriteMasked(const void*datar, int x, int y, int width, int height, int maskcolor) {
   color_t*data = (color_t*) datar;
   color_t* VRAM = (color_t*)0xA8000000;
   VRAM += LCD_WIDTH_PX*y + x;
   for(int j=y; j<y+height; j++) {
      for(int i=x; i<x+width;  i++) {
         if (*(data) != maskcolor) {
            *(VRAM++) = *(data++);
         } else { VRAM++; data++; }
      }
      VRAM += LCD_WIDTH_PX-width;
   }
}


int PRGM_GetKey(){     
  unsigned char buffer[12];     
  PRGM_GetKey_OS( buffer );     
  return ( buffer[1] & 0x0F ) * 10 + ( ( buffer[2] & 0xF0 ) >> 4 );     
}   



struct Coord {   


   int x;   


   int y;   


};   

int keydown(int basic_keycode)   
{   
   const unsigned short* keyboard_register = (unsigned short*)0xA44B0000;   
   int row, col, word, bit;   
   row = basic_keycode%10;   
   col = basic_keycode/10-1;   
   word = row>>1;   
   bit = col + 8*(row&1);   
   return (0 != (keyboard_register[word] & 1<<bit));   
}   

void moveCursor(struct Coord* cursor) {       
   if( keydown(KEY_PRGM_LEFT) && cursor->x>45) cursor->x-=8;   
   if( keydown(KEY_PRGM_RIGHT) && cursor->x<302) cursor->x+=8;   
}   



int main() {     
  int y = 129;   
  while(1) {     
    int key = PRGM_GetKey();     
    if(key == KEY_PRGM_MENU) {  GetKey(&key); }         
    CopySprite(BulletMenu, 0, 0, 384, 216);
    CopySpriteMasked(BulletMenuSelector, 7, y, 18, 16, COLOR_RED);
    if(keydown(KEY_PRGM_UP) && y>129)
    y-=16;
    if(keydown(KEY_PRGM_DOWN) && y<145)
    y+=16;
    if(keydown(KEY_PRGM_SHIFT) && y == 129) {
while(1) {     
  struct Coord cur;   
  cur.x = 128;   
  int w = rand()%(340-45+1) + 45;
  int b = rand()%(340-45+1) + 45;
  int j = 0;
  int s = 0;
    int key = PRGM_GetKey();     
    if(key == KEY_PRGM_MENU) {  GetKey(&key); }         
    moveCursor(&cur);   
    CopySprite(BulletBackground, 0, 0, 384, 216);
    CopySpriteMasked(SpenBullet, cur.x, 159, 38, 43, COLOR_RED);
    CopySpriteMasked(Banana, w, j, 75, 80, COLOR_RED);
    CopySpriteMasked(LSDOMG, b, j, 34, 34, COLOR_RED);
    if(w == cur.x && j == 159)
    break;
    if(b == cur.x && j == 159)
    s++;
    j+=8;
    if(j >= 216) {
  j = 0;
  w = rand()%(340-45+1) + 45;
  b = rand()%(340-45+1) + 45;
}
    if(keydown(KEY_PRGM_EXIT))
    break;
    Bdisp_PutDisp_DD();     
    Bdisp_AllCr_VRAM();
  }     }
    if(keydown(KEY_PRGM_SHIFT) && y == 145) {
while(1) {
    CopySprite(Instructions, 0, 0, 384, 216);
    if(keydown(KEY_PRGM_EXIT))
    break;
    Bdisp_PutDisp_DD();     
    Bdisp_AllCr_VRAM();
}
  return 0;
}
    Bdisp_PutDisp_DD();     
    Bdisp_AllCr_VRAM();

}

  return 0;     
}


Help fix it. PLEASE!
What's happening with your code? We can't just magically fix it if you don't tell us what's wrong.
When I put it on my Prizm, the Banana/LSDOMG sprites jerk around the top of the screen. Even though it is supposed to make them scroll to the bottom of the screen and regenerate somewhere else at the top.

Edit:

Here is the g3a file. Enjoy.....

http://www.mediafire.com/?79bchn1nyyfcyv2
Quote:
int w = rand()%(340-45+1) + 45;
int b = rand()%(340-45+1) + 45;
int j = 0;
int s = 0;
int key = PRGM_GetKey();
if(key == KEY_PRGM_MENU) { GetKey(&key); }
moveCursor(&cur);
CopySprite(BulletBackground, 0, 0, 384, 216);
CopySpriteMasked(SpenBullet, cur.x, 159, 38, 43, COLOR_RED);
CopySpriteMasked(Banana, w, j, 75, 80, COLOR_RED);
CopySpriteMasked(LSDOMG, b, j, 34, 34, COLOR_RED);

You're setting the coordinates for the sprites every time the loop repeats. Set the variables outside the loop.
Now how will I make it detect if the banana or coin(LSDOMG) hits it or not?.........
Spenceboy98 wrote:
Now how will I make it detect if the banana or coin(LSDOMG) hits it or not?.........

Umm..

Code:
if(w == cur.x && j == 159)
    break;
    if(b == cur.x && j == 159)
    s++;

You already have code to detect if it hit the cursor.
Yes, but it only detects when it is exactly the same coordinates.
Spenceboy98 wrote:
Yes, but it only detects when it is exactly the same coordinates.


Seems you're going to need to check if it's in a box of a certain size, using position offsets and the >, <, >=, <= operators instead of the == operator.
Or you could use the absolute value trick. Such as: if (abs(bulletX - playerX) <= 10) { /* within 10 pixels on either side */ }.
_player1537 wrote:
Or you could use the absolute value trick. Such as: if (abs(bulletX - playerX) <= 10) { /* within 10 pixels on either side */ }.


Now it says


Code:
main.c:99:5: warning: implicit declaration of function 'abs' [-Wimplicit-functio
n-declaration]


Now what?
you need to define abs.


Code:
inline int abs(int a) { return (a < 0) ? (-a) : (a); }
Now, whenever you are underneath the bullet coming toward you, it exits anyways. No matter how far away from it you are.
Ashbad wrote:
you need to define abs.


Code:
inline int abs(int a) { return (a < 0) ? (-a) : (a); }
There's a better way to do this:


Code:
#define abs(a) ((a) < 0 ? (-(a)):(a))
This will work for any numeric data type. Smile

Spence: Then you didn't use it correctly. You should check when the absolute difference in X is less than or equal to (some distance), and the difference is Y is also less than or equal to (some distance). Maybe you aren't properly cancelling the bullet?
I think the abs( function was already defined in stdlib.h.
blue_bear_94 wrote:
I think the abs( function was already defined in stdlib.h.


Unfortunately, the PrizmSDK's C standard library is far from being completely filled in; abs() isn't defined there officially yet.
KermMartian wrote:
Ashbad wrote:
you need to define abs.


Code:
inline int abs(int a) { return (a < 0) ? (-a) : (a); }
There's a better way to do this:


Code:
#define abs(a) ((a) < 0 ? (-(a)):(a))
This will work for any numeric data type. Smile
And breaks for abs(someFuncThatReturnsAValue()) and abs(x++), right? I remember benryves and Catherine talking about this last night. In any case, I prefer functions to macros. Macros feel dirty.
Ashbad wrote:
blue_bear_94 wrote:
I think the abs( function was already defined in stdlib.h.


Unfortunately, the PrizmSDK's C standard library is far from being completely filled in; abs() isn't defined there officially yet.

You're both wrong. abs() is a GCC builtin (unless you force strict adherence to the standard), but nothing included the prototype, hence the warning-- note that was not an error, but a warning.


Code:
int abs(int);

will do the trick until somebody bothers putting them in the headers.
I'm guessing that it is too big to compile:


Code:
/prizmsdk-0.3/prizmsdk-0.3/bin/../lib/gcc/sh3eb-elf/4.6.0/../../../../sh3eb-
elf/bin/ld.exe: region `rom' overflowed by 159776 bytes
collect2: ld returned 1 exit status
make: *** [Bullet.bin] Error 1
Spenceboy98 wrote:
I'm guessing that it is too big to compile:


Code:
/prizmsdk-0.3/prizmsdk-0.3/bin/../lib/gcc/sh3eb-elf/4.6.0/../../../../sh3eb-
elf/bin/ld.exe: region `rom' overflowed by 159776 bytes
collect2: ld returned 1 exit status
make: *** [Bullet.bin] Error 1


What did you stick in it, 3 screen-sized textures? If you want to make it bigger, change your "rom: l=500K" (or something like that) in Prizm.ld in the "common" folder of the SDK to "l=1250K", which is a safe number.
It doesn't display the pic at the end. Here's code:


Code:
int main() {     
  int y = 129;   
  while(1) {     
    int key = PRGM_GetKey();     
    if(key == KEY_PRGM_MENU) {  GetKey(&key); }         
    CopySprite(BulletMenu, 0, 0, 384, 216);
    CopySpriteMasked(BulletMenuSelector, 7, y, 18, 16, COLOR_RED);
    if(keydown(KEY_PRGM_UP) && y>129)
    y-=16;
    if(keydown(KEY_PRGM_DOWN) && y<145)
    y+=16;
    if(keydown(KEY_PRGM_SHIFT) && y == 129) {
  struct Coord cur;   
  cur.x = 128;   
  cur.y = 159;
  int w = rand()%(265-45+1) + 45;
  int b = rand()%(306-45+1) + 45;
  int j = -80;
  int m = -34;
  int s = 0;
  int t = 0;
while(1) {     
    int key = PRGM_GetKey();     
    if(key == KEY_PRGM_MENU) {  GetKey(&key); }         
    moveCursor(&cur);   
    CopySprite(BulletBackground, 0, 0, 384, 216);
    CopySpriteMasked(SpenBullet, cur.x, cur.y, 38, 43, COLOR_RED);
    CopySpriteMasked(Banana, w, j, 75, 80, COLOR_RED);
    CopySpriteMasked(LSDOMG, b, m, 34, 34, COLOR_RED);
    if ((cur.x >= w+10 && cur.x <= w+59) && (cur.y >= j+15 && cur.y <= j+71))
    break;
    if ((cur.x >= b+4 && cur.x <= b+30) && (cur.y >= m+4 && cur.y <= m+30)) {
    s++;
    m = -34;
    b = rand()%(340-45+1) + 45;}
    j+=8;
    m+=8;
    if(j > 216) {
  j = -80;
  w = rand()%(340-45+1) + 45; }
    if(m > 216) {
    m = -34;
    b = rand()%(340-45+1) + 45;}
    if(keydown(KEY_PRGM_EXIT))
    break;
    t++;
    if(t >= 1000){
    CopySprite(BulletPerson, 0, 0, 384, 216);
    break;}
    Bdisp_PutDisp_DD();     
    Bdisp_AllCr_VRAM();
  }     }
    if(keydown(KEY_PRGM_SHIFT) && y == 145) {
while(1) {
    CopySprite(Instructions, 0, 0, 384, 216);
    if(keydown(KEY_PRGM_EXIT))
    break;
    Bdisp_PutDisp_DD();     
    Bdisp_AllCr_VRAM();
}

}
    Bdisp_PutDisp_DD();     
    Bdisp_AllCr_VRAM();

}

  return 0;     
}
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement