Post by benoit0123 on Feb 16, 2014 14:11:23 GMT -8
Hi all,
I am a newbie for all things related to Gameduino and Arduino, albeit I have some experience with C/C++/Java programing.
Therefore one of the first exercise I tried to do was to extend the Gameduino 2 based sample named "walk" by implementing the features below:
a) x-axis acceleration measured and applied for varying the x-axis speed of every sprite;
b) variable x-axis speed defined per sprite at init time;
c) sprite rendering based on a mix of BitmapHandle/Cell/Vertex2f when the sprite origin is off screen and Vertex2ii when the sprite origin is on screen. Therefore sprites walk smootly on the left edge of the screen instead
The code below is the modified code from the current design.
However, my current askings are:
a) On a Duemillanove w/ ATmega 328, increasing the NSPRITES to 256 would cause the program to hang quickly after booting;
b) In more general terms, is there a performance penalty in using the Vertex2f API call over calling the Vertex2ii API call when we are sure the sprite origin is on screen?
Future directions would be:
a) to cater for sprites moving from right to left, dealing with mirrored sprites as explained within the guide.
Thank you for reading and for any feedback/advice on the subject,
Best regards,
Benoît
I am a newbie for all things related to Gameduino and Arduino, albeit I have some experience with C/C++/Java programing.
Therefore one of the first exercise I tried to do was to extend the Gameduino 2 based sample named "walk" by implementing the features below:
a) x-axis acceleration measured and applied for varying the x-axis speed of every sprite;
b) variable x-axis speed defined per sprite at init time;
c) sprite rendering based on a mix of BitmapHandle/Cell/Vertex2f when the sprite origin is off screen and Vertex2ii when the sprite origin is on screen. Therefore sprites walk smootly on the left edge of the screen instead
The code below is the modified code from the current design.
However, my current askings are:
a) On a Duemillanove w/ ATmega 328, increasing the NSPRITES to 256 would cause the program to hang quickly after booting;
b) In more general terms, is there a performance penalty in using the Vertex2f API call over calling the Vertex2ii API call when we are sure the sprite origin is on screen?
Future directions would be:
a) to cater for sprites moving from right to left, dealing with mirrored sprites as explained within the guide.
Thank you for reading and for any feedback/advice on the subject,
Best regards,
Benoît
#include <EEPROM.h>
#include <SPI.h>
#include <GD2.h>
#include "walk_assets.h"
#define NSPRITES 240
#define SCALEY 0
typedef struct {
int ax, ay;
byte invspeed, count;
} tsprite;
static tsprite v[NSPRITES];
void setup() {
GD.begin();
LOAD_ASSETS();
//Serial.begin(19200);
for (int i = 0; i < NSPRITES; i++) {
v[i].ax = GD.random(511);
v[i].ay= i<<SCALEY;
v[i].count= v[i].invspeed = GD.random(2) + 1;
}
//Serial.println("Init done");
}
#define SPYINDEX 999
void loop() {
// Number of scans between two reads from accelerometer
static const byte nscansaccel= 16;
static byte ccscan= nscansaccel;
int ax, dummy, iv;
boolean bF;
tsprite *ptr;
byte curcell= -1, newcell;
GD.ClearColorRGB(0x000050);
GD.Clear();
GD.Begin(BITMAPS);
GD.BitmapHandle(WALK_HANDLE);
ptr= &v[0];
for (int i= 0; i < NSPRITES; i+= 1, ptr+= 1) {
ax= ptr->ax; bF= (ax < 0);
if(i == SPYINDEX) {
Serial.print("ptr->ax: ");
Serial.println(ax, DEC);
Serial.print("ptr->bF: ");
Serial.println(bF, DEC);
Serial.print("invspeed: ");
Serial.println(ptr->invspeed, DEC);
}
GD.ColorRGB(i, i, i);
newcell= (ax >> (bF ? 6 : 2)) & 7;
if(i == SPYINDEX)
{ Serial.print("newcell: "); Serial.println(newcell, DEC); }
if(bF && newcell!= curcell) {
if(i == SPYINDEX) { Serial.print("curcell: "); Serial.println(curcell, DEC); }
GD.Cell(newcell); curcell= newcell;
}
if(bF)
GD.Vertex2f(ax, ptr->ay);
else
GD.Vertex2ii(ax, ptr->ay, WALK_HANDLE, newcell);
ptr->count-= 1;
if(!(ptr->count)) {
if(i == SPYINDEX) Serial.println("Incrementation abscisse...");
ptr->count= ptr->invspeed;
#define nax ax
nax= (ptr->ax+= (bF ? 16 : 1));
if(nax > 511) {
ptr->ax= -32 * 16;
ptr->ay<<= 4;
}
if(nax > -1 && bF) {
ptr->ay>>= 4;
}
if(i == SPYINDEX) {
Serial.print("new nax: ");
Serial.println(nax, DEC);
}
#undef nax
}
}
GD.swap();
if(!--ccscan) {
ccscan= nscansaccel;
GD.get_accel(ax, dummy, dummy);
iv= (ax < -64) ? +1 : ((ax > 63) ? -1 : 0);
Serial.print("Acceleration speed gradient: ");
Serial.println(iv, DEC);
if(iv) {
#define vmin dummy
#define vmax ax
vmin= 999; vmax= -999;
ptr= &v[0];
for(int i= 0; i < NSPRITES; i+= 1, ptr+= 1) {
ptr->invspeed= constrain(ptr->invspeed + iv, 1, 4);
//vmin= min(vmin, ptr->invspeed);
//vmax= max(vmax, ptr->invspeed);
}
//Serial.print("invspeed vmin: "); Serial.println(vmin, DEC);
//Serial.print("invspeed vmax: "); Serial.println(vmax, DEC);
#undef vmin
#undef vmax
}
}
}