CEMETECH
Leading The Way To The Future
Login [Register]
Username:
Password:
Autologin:

Don't have an account? Register now to chat, post, use our tools, and much more.
Latest Headlines
Online Users
There are 122 users online: 3 members, 91 guests and 28 bots.
Members: Ashbad, shmibs.
Bots: VoilaBot (1), Spinn3r (1), MSN/Bing (1), Magpie Crawler (3), VoilaBot (3), Googlebot (18), MSN/Bing (1).
RSS & Social Media
SAX
You must log in to view the SAX chat widget
Author Message
Spenceboy98


Super-Expert


Joined: 06 Jan 2012
Posts: 824
Location: In the TARDIS

Posted: 22 Apr 2012 06:56:03 pm    Post subject: Bullet Code Problems

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!
Back to top
souvik1997


Guru-in-Training


Joined: 19 Apr 2010
Posts: 2870

Posted: 22 Apr 2012 06:57:41 pm    Post subject:

What's happening with your code? We can't just magically fix it if you don't tell us what's wrong.
_________________
CALCnet Tournament-38%


deviantArt
Back to top
Spenceboy98


Super-Expert


Joined: 06 Jan 2012
Posts: 824
Location: In the TARDIS

Posted: 22 Apr 2012 07:00:03 pm    Post subject:

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
Back to top
souvik1997


Guru-in-Training


Joined: 19 Apr 2010
Posts: 2870

Posted: 22 Apr 2012 07:08:19 pm    Post subject:

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.
_________________
CALCnet Tournament-38%


deviantArt
Back to top
Spenceboy98


Super-Expert


Joined: 06 Jan 2012
Posts: 824
Location: In the TARDIS

Posted: 22 Apr 2012 07:15:50 pm    Post subject:

Now how will I make it detect if the banana or coin(LSDOMG) hits it or not?.........
Back to top
souvik1997


Guru-in-Training


Joined: 19 Apr 2010
Posts: 2870

Posted: 22 Apr 2012 07:17:29 pm    Post subject:

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.
_________________
CALCnet Tournament-38%


deviantArt
Back to top
Spenceboy98


Super-Expert


Joined: 06 Jan 2012
Posts: 824
Location: In the TARDIS

Posted: 22 Apr 2012 07:21:10 pm    Post subject:

Yes, but it only detects when it is exactly the same coordinates.
Back to top
Ashbad


... I think redheaded girls are kind of cool


Joined: 01 Dec 2010
Posts: 2418
Location: Stomp Stomp Stomp, The Idiot Convention

Posted: 22 Apr 2012 07:22:23 pm    Post subject:

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.
_________________
-Ashbad
Back to top
_player1537


Guru-in-Training


Joined: 25 Nov 2009
Posts: 2958

Posted: 22 Apr 2012 07:24:28 pm    Post subject:

Or you could use the absolute value trick. Such as: if (abs(bulletX - playerX) <= 10) { /* within 10 pixels on either side */ }.
_________________
http://tanner.myserverathome.com
Back to top
Spenceboy98


Super-Expert


Joined: 06 Jan 2012
Posts: 824
Location: In the TARDIS

Posted: 23 Apr 2012 06:17:20 pm    Post subject:

_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?
Back to top
Ashbad


... I think redheaded girls are kind of cool


Joined: 01 Dec 2010
Posts: 2418
Location: Stomp Stomp Stomp, The Idiot Convention

Posted: 23 Apr 2012 06:19:37 pm    Post subject:

you need to define abs.


Code:
inline int abs(int a) { return (a < 0) ? (-a) : (a); }

_________________
-Ashbad
Back to top
Spenceboy98


Super-Expert


Joined: 06 Jan 2012
Posts: 824
Location: In the TARDIS

Posted: 23 Apr 2012 06:26:32 pm    Post subject:

Now, whenever you are underneath the bullet coming toward you, it exits anyways. No matter how far away from it you are.
Back to top
KermMartian


Site Admin


Joined: 14 Mar 2005
Posts: 55763
Location: Earth, Sol, Milky Way

Posted: 24 Apr 2012 12:57:48 pm    Post subject:

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?
_________________


Back to top
blue_bear_94


Member


Joined: 20 Apr 2012
Posts: 162

Posted: 24 Apr 2012 05:00:17 pm    Post subject:

I think the abs( function was already defined in stdlib.h.
_________________
Do NOT click any links!
Back to top
Ashbad


... I think redheaded girls are kind of cool


Joined: 01 Dec 2010
Posts: 2418
Location: Stomp Stomp Stomp, The Idiot Convention

Posted: 24 Apr 2012 05:01:16 pm    Post subject:

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.
_________________
-Ashbad
Back to top
merthsoft


File Archiver


Joined: 09 May 2010
Posts: 2735

Posted: 24 Apr 2012 05:02:01 pm    Post subject:

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.
_________________
Shaun
Back to top
Tari


Systems Integrator


Joined: 03 Jul 2006
Posts: 2113
Location: Always-winter, Michigan

Posted: 24 Apr 2012 05:26:51 pm    Post subject:

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.
_________________


Ask questions the smart way · タリ
Back to top
Spenceboy98


Super-Expert


Joined: 06 Jan 2012
Posts: 824
Location: In the TARDIS

Posted: 28 Apr 2012 03:12:48 pm    Post subject:

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
Back to top
Ashbad


... I think redheaded girls are kind of cool


Joined: 01 Dec 2010
Posts: 2418
Location: Stomp Stomp Stomp, The Idiot Convention

Posted: 28 Apr 2012 03:16:29 pm    Post subject:

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.
_________________
-Ashbad
Back to top
Spenceboy98


Super-Expert


Joined: 06 Jan 2012
Posts: 824
Location: In the TARDIS

Posted: 30 Apr 2012 09:05:43 pm    Post subject:

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;     
}
Back to top
Display posts from previous:   
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
    »
» View previous topic :: View next topic  
Page 1 of 1 » All times are GMT - 5 Hours

 
Jump to:  
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

© Copyright 2000-2013 Cemetech & Kerm Martian :: Page Execution Time: 0.047289 seconds.