GP32 Pyrocad B2


jlebrech

UFO Robot
Joined
Feb 25, 2003
Messages
899
Age
41
Here my code for my latest pyrocad, one small problem is with the rotation code which makes the shapes go round in circles and not actually rotate round themselves, has anyone got some tips. thanks.

the problem is in the code for button r i think.



Code:
#include "gp32.h"
#include "fileio.h"
#include "zda.h"

//extern unsigned char modfile[];


  
#define ZDAFILE "dev0:\\GPMM\\data.zda"

#define RED 0xF800

#define buttonspeed 12
#define cursorspeed 1


/* trig stufffffffffffffff :) */ 



int SinTab1[640]=
{ 2 , 3 , 5 , 6 , 8 , 9 , 11 , 13 ,
 14 , 16 , 17 , 19 , 20 , 22 , 23 , 25 ,
 27 , 28 , 30 , 31 , 33 , 34 , 36 , 37 ,
 39 , 40 , 42 , 43 , 45 , 46 , 48 , 49 ,
 50 , 52 , 53 , 55 , 56 , 58 , 59 , 60 ,
 62 , 63 , 64 , 66 , 67 , 68 , 70 , 71 ,
 72 , 74 , 75 , 76 , 78 , 79 , 80 , 81 ,
 82 , 84 , 85 , 86 , 87 , 88 , 89 , 91 ,
 92 , 93 , 94 , 95 , 96 , 97 , 98 , 99 ,
 100 , 101 , 102 , 103 , 104 , 105 , 106 , 106 ,
 107 , 108 , 109 , 110 , 111 , 111 , 112 , 113 ,
 114 , 114 , 115 , 116 , 116 , 117 , 118 , 118 ,
 119 , 119 , 120 , 121 , 121 , 122 , 122 , 122 ,
 123 , 123 , 124 , 124 , 125 , 125 , 125 , 126 ,
 126 , 126 , 126 , 127 , 127 , 127 , 127 , 127 ,
 128 , 128 , 128 , 128 , 128 , 128 , 128 , 128 ,
 128 , 128 , 128 , 128 , 128 , 128 , 128 , 127 ,
 127 , 127 , 127 , 127 , 126 , 126 , 126 , 126 ,
 125 , 125 , 125 , 124 , 124 , 123 , 123 , 122 ,
 122 , 122 , 121 , 121 , 120 , 119 , 119 , 118 ,
 118 , 117 , 116 , 116 , 115 , 114 , 114 , 113 ,
 112 , 111 , 111 , 110 , 109 , 108 , 107 , 106 ,
 106 , 105 , 104 , 103 , 102 , 101 , 100 , 99 ,
 98 , 97 , 96 , 95 , 94 , 93 , 92 , 91 ,
 89 , 88 , 87 , 86 , 85 , 84 , 82 , 81 ,
 80 , 79 , 78 , 76 , 75 , 74 , 72 , 71 ,
 70 , 68 , 67 , 66 , 64 , 63 , 62 , 60 ,
 59 , 58 , 56 , 55 , 53 , 52 , 50 , 49 ,
 48 , 46 , 45 , 43 , 42 , 40 , 39 , 37 ,
 36 , 34 , 33 , 31 , 30 , 28 , 27 , 25 ,
 23 , 22 , 20 , 19 , 17 , 16 , 14 , 13 ,
 11 , 9 , 8 , 6 , 5 , 3 , 2 , 0 ,
-2 ,-3 ,-5 ,-6 ,-8 ,-9 ,-11 ,-13 ,
-14 ,-16 ,-17 ,-19 ,-20 ,-22 ,-23 ,-25 ,
-27 ,-28 ,-30 ,-31 ,-33 ,-34 ,-36 ,-37 ,
-39 ,-40 ,-42 ,-43 ,-45 ,-46 ,-48 ,-49 ,
-50 ,-52 ,-53 ,-55 ,-56 ,-58 ,-59 ,-60 ,
-62 ,-63 ,-64 ,-66 ,-67 ,-68 ,-70 ,-71 ,
-72 ,-74 ,-75 ,-76 ,-78 ,-79 ,-80 ,-81 ,
-82 ,-84 ,-85 ,-86 ,-87 ,-88 ,-89 ,-91 ,
-92 ,-93 ,-94 ,-95 ,-96 ,-97 ,-98 ,-99 ,
-100 ,-101 ,-102 ,-103 ,-104 ,-105 ,-106 ,-106 ,
-107 ,-108 ,-109 ,-110 ,-111 ,-111 ,-112 ,-113 ,
-114 ,-114 ,-115 ,-116 ,-116 ,-117 ,-118 ,-118 ,
-119 ,-119 ,-120 ,-121 ,-121 ,-122 ,-122 ,-122 ,
-123 ,-123 ,-124 ,-124 ,-125 ,-125 ,-125 ,-126 ,
-126 ,-126 ,-126 ,-127 ,-127 ,-127 ,-127 ,-127 ,
-128 ,-128 ,-128 ,-128 ,-128 ,-128 ,-128 ,-128 ,
-128 ,-128 ,-128 ,-128 ,-128 ,-128 ,-128 ,-127 ,
-127 ,-127 ,-127 ,-127 ,-126 ,-126 ,-126 ,-126 ,
-125 ,-125 ,-125 ,-124 ,-124 ,-123 ,-123 ,-122 ,
-122 ,-122 ,-121 ,-121 ,-120 ,-119 ,-119 ,-118 ,
-118 ,-117 ,-116 ,-116 ,-115 ,-114 ,-114 ,-113 ,
-112 ,-111 ,-111 ,-110 ,-109 ,-108 ,-107 ,-106 ,
-106 ,-105 ,-104 ,-103 ,-102 ,-101 ,-100 ,-99 ,
-98 ,-97 ,-96 ,-95 ,-94 ,-93 ,-92 ,-91 ,
-89 ,-88 ,-87 ,-86 ,-85 ,-84 ,-82 ,-81 ,
-80 ,-79 ,-78 ,-76 ,-75 ,-74 ,-72 ,-71 ,
-70 ,-68 ,-67 ,-66 ,-64 ,-63 ,-62 ,-60 ,
-59 ,-58 ,-56 ,-55 ,-53 ,-52 ,-50 ,-49 ,
-48 ,-46 ,-45 ,-43 ,-42 ,-40 ,-39 ,-37 ,
-36 ,-34 ,-33 ,-31 ,-30 ,-28 ,-27 ,-25 ,
-23 ,-22 ,-20 ,-19 ,-17 ,-16 ,-14 ,-13 ,
-11 ,-9 ,-8 ,-6 ,-5 ,-3 ,-2 , 0, 
 2 , 3 , 5 , 6 , 8 , 9 , 11 , 13 ,
 14 , 16 , 17 , 19 , 20 , 22 , 23 , 25 ,
 27 , 28 , 30 , 31 , 33 , 34 , 36 , 37 ,
 39 , 40 , 42 , 43 , 45 , 46 , 48 , 49 ,
 50 , 52 , 53 , 55 , 56 , 58 , 59 , 60 ,
 62 , 63 , 64 , 66 , 67 , 68 , 70 , 71 ,
 72 , 74 , 75 , 76 , 78 , 79 , 80 , 81 ,
 82 , 84 , 85 , 86 , 87 , 88 , 89 , 91 ,
 92 , 93 , 94 , 95 , 96 , 97 , 98 , 99 ,
 100 , 101 , 102 , 103 , 104 , 105 , 106 , 106 ,
 107 , 108 , 109 , 110 , 111 , 111 , 112 , 113 ,
 114 , 114 , 115 , 116 , 116 , 117 , 118 , 118 ,
 119 , 119 , 120 , 121 , 121 , 122 , 122 , 122 ,
 123 , 123 , 124 , 124 , 125 , 125 , 125 , 126 ,
 126 , 126 , 126 , 127 , 127 , 127 , 127 , 127 ,
 128 , 128 , 128 , 128 , 128 , 128 , 128 , 128};

int angle, radian;
int radius;

//#define PI 3.1415926535897932384626433832795


void rot (int *dstx, int *dsty, int x, int y, int radius, int angle){
	*dsty = (
    (SinTab1[angle]) * radius)+y;
	*dstx = (
    (SinTab1[angle+(512/4)]) * radius)+x;
}














/*stuctures :) */

typedef struct {
   int start_x,start_y;
   int end_x, end_y;
} line;

line lines[1000];


/* custom screen routine thx mr mirko*/

u16 *framebuffer[2];

char swapper=0;
void swap_screen () {
   gp_setFramebuffer(framebuffer[swapper],1);
   swapper++; if (swapper == 2) swapper=0;
}


/* zda file loading */

char * loadFile(char * filename){

    int  real_headersize;
    int  usize,csize,offset,err;
	
	
	char *uncompressed;
   char header_12bytes[12];  // The ZDA header is always 12 Bytes big
   char *full_header;        // The full headersize depend on content
   char *compressed;

    smc_read(ZDAFILE,header_12bytes,0,12); // read in the 12 bytes header.
     real_headersize=gp_zda_headersize(header_12bytes);      // calculate the real headersize

     full_header = (char*) malloc(real_headersize);          // malloc space for full header
     smc_read(ZDAFILE,full_header,0,real_headersize); // read in the full header

     csize  = gp_zda_csize (full_header, filename);     // get compressed   size of file
     usize  = gp_zda_usize (full_header, filename);     // get uncompressed size of file
     offset = gp_zda_offset(full_header, filename);     // get the offset for file, counting from file beginning

     compressed   = (char*) malloc(csize);  // temp buffer for compressed file
     uncompressed = (char*) malloc(usize);  // buffer for uncompressed data
     err=smc_read(ZDAFILE,compressed,offset,csize);  // read in the compressed data

  err = gp_uncompressZDA( (u8*)uncompressed, &usize, (u8*)compressed, csize ); // uncompress
  
  // free all memory
     free (full_header);
     free (compressed);
     //free (uncompressed);
  
  return uncompressed;
}


/* Draw the horizontal and vertical lines to see a grid*/

void grid (int units){

int i;

	for (i = 0; i < (240/units); i++){
         gp_drawLine16(0, 
      units * i, 
   320, 
   units * i, 0xffff, framebuffer[swapper]);	
	}
	for (i = 0; i < (320/units); i++){
         gp_drawLine16(units * i, 
      0, 
   units*i, 
      240, 0xffff, framebuffer[swapper]);	
	}
      	
}


/*main stuff
   where it starts*/

int main() {
      
    int i;
	
	angle = 0;

    for ( i = 0; i < 1000; i++){
    lines[i].start_x = 0; //hide all points until used
    lines[i].start_y = 0;
    lines[i].end_x = 0;
    lines[i].end_y = 0;
	}

  int spritex=-150;
  int x = 120, y = 180;
  int pan_x = 0, pan_y = 0;
  int cline = 0;
  int step = 1; // set to slow speed
  int end = 0; // end as in which end of the line
  int snap = 0; // snap to grid also show grid

  framebuffer[0] = (u16*)  FRAMEBUFFER1;
  framebuffer[1] = (u16*)  FRAMEBUFFER2;
 

  gp_setCpuspeed(133);

  gp_drawString(10,10,10,"LOADING",0x0000,framebuffer[swapper]);
  
  //char * image = loadFile("garfield.raw");
  
  gp_initRTC();
  
  gp_initFramebuffer(framebuffer[0],16,85);
  gp_clearRTC();
  
  int curimg = 0, loop = 0;
  char imgfname[20];
  char statustxt[25];

  
  /*
  char * images[12];
  for ( i = 0; i < 12; i++){
    sprintf (imgfname,"%d.raw",i);
	images[i] = loadFile(imgfname);
  }*/
  
  
  /* these are the speeds for the action that the buttons perform 
      You can tweak their speed as you want
  */
  

  
  
  unsigned int buttonwait;  
  buttonwait = gp_getRTC() + buttonspeed; 
  
  unsigned int cursorwait;
  cursorwait = gp_getRTC()+ cursorspeed; 
  
 while (1) {

  // Drawing in Sprite header mode, only usefull if the sprite got a header.

     

  //free (uncompressed);
  
  

  swap_screen();
 
    gp_clearFramebuffer16(framebuffer[swapper],0x0000); // very very fast asm, faster than memset

	
	
if (cursorwait < gp_getRTC()){  // change to switch
	cursorwait = gp_getRTC() + cursorspeed;
	
  if (gp_getButton()&BUTTON_LEFT){ 
     x = x - step;
  }
  if (gp_getButton()&BUTTON_RIGHT) { 
    x = x + step;
  }
  
  if (gp_getButton()&BUTTON_DOWN){ 
     y = y + step;
  }
  if (gp_getButton()&BUTTON_UP) { 
    y = y - step;
  }
  
  }
  
    if (buttonwait < gp_getRTC()){
        buttonwait = gp_getRTC() + buttonspeed; //  1 second
	
  
  
  if (gp_getButton()&BUTTON_START) { // this need a switch
  
 
         switch (step){
              case 1: step = 2; grid(2); break;
           case 2: step = 5; grid(5); break;
           case 5: step = 10; grid(10); break;
           case 10: step = 20; grid(20); break;
           case 20: step = 40; grid(40); break;
    case 40: step = 80; grid(80); break; // draw the grid breifly
    case 80: step = 1; break;
  	}

  }
	
	// Snap to grid
	// just show grid atm
	if (gp_getButton()&BUTTON_SELECT) { 
      snap = !snap;
	}
	
	// undo
	// the simplest one to do, yet seems so hard :D
    if (gp_getButton()&BUTTON_L) { 
     cline = (cline - 1)%999;
  
     lines[cline].start_x = 0;
     lines[cline].start_y = 0;
  lines[cline].end_x = 0;
     lines[cline].end_y = 0;
	}
	

	

    // pan to the cursor
	if (gp_getButton()&BUTTON_A) { 
   pan_x = -(x - 160); pan_y = -(y - 120);  // center to the cursor
   
   
	}
	
    // rotate
    if (gp_getButton()&BUTTON_R) { 
   angle=(angle+8)%512;
   
   // rotate each point in every line :D
        for ( i = 0; i < 1000; i++){
             if (angle>0){
                 if (lines[i%1000].end_x > 0){
                  rot (&lines[i%1000].start_x, &lines[i%1000].start_y, lines[i%1000].start_x, lines[i%1000].start_y, 1, angle);
               rot (&lines[i%1000].end_x, &lines[i%1000].end_y, lines[i%1000].end_x, lines[i%1000].end_y, 1, angle);
             }
             }
        }
  
  x = lines[1].start_x;
  y = lines[1].start_y;
	}
	
	
  
	if (gp_getButton()&BUTTON_B) {
	
      /* if line == 0 then draw add first point in the line */
      if (end == 0){
       lines[cline].start_x = x;//%320;
          lines[cline].start_y = y;//%240;
    
    end = 1;
  } else {
  
  /* if line == 1 then draw second point in line then move to the next line in array */
   
       lines[cline].end_x = x;//%320;
          lines[cline].end_y = y;//%240;
    
             cline = (cline + 1)%1000;
    
    end = 0;
  }
	}
  
  }

  
  
    // draw the grid if snap is on
  
  	if (snap){
  switch (step){
         case 1: grid(2); break; // draw the current grid selected
      case 2: grid(5); break;
      case 5: grid(10); break;
      case 10: grid(20); break;
      case 20: grid(40); break;
  	case 40: grid(80); break;
  }
	}
  

  // draw the cursor
  gp_drawLine16 ( 0,//(x%320)-5,
                                y + pan_y,
                                320,//(x%320)+5, 
        y +pan_y,
        RED, framebuffer[swapper]);
        
  gp_drawLine16 ( x + pan_x,
                                0,//(y%240)-5, 
        x + pan_x,
        240,//(y%240)+5, 
        RED, framebuffer[swapper]);
  

  
  
  
  // draw each point into a sequence of lines
    for ( i = 0; i < 1000; i++){
     if (lines[i%1000].end_x > 0){
            gp_drawLine16(lines[i%1000].start_x + pan_x, // if previous line = -1 then draw between line 9 and 0
                                  lines[i%1000].start_y + pan_y, 
             lines[i%1000].end_x + pan_x, 
             lines[i%1000].end_y + pan_y, 0xffff, framebuffer[swapper]);
     }
	}
	
  
  char txbuf[30];
  sprintf (txbuf,"x: %d y: %d  ox:%d oy:%d a%d", x, y, lines[1].start_x, lines[i].start_y, angle);
  gp_drawString(10,10,30,txbuf,0xffff,framebuffer[swapper]);
  
  if (!end){
     gp_drawString(10,220,13,"Start of line",0xffff,framebuffer[swapper]);
  } else {
     gp_drawString(10,220,11,"End of line",0xffff,framebuffer[swapper]);
  }
	
   
   //while( endwait < gp_getRTC() );

   
 // if (gp_getButton()&BUTTON_A) gp_Reset();
 }
 
 

 
  
}
 
Next feature is saves and loads then people might want to use it. the main purpose will be to load backgrounds for games and to make slopes in platformers.
 
Back
Top