Class instantiation

New to Arduino and C, I am trying to write some classes to draw on a led matrix. ( I am web developer and trying to learn C by myself )

To describe simply what I try to achieve :
XSprite : _x,_y,_width,_height,_data, setData()
XMatrix : _sprites, numSprites, addSprite()

XSprite frame : this is the object I’m using to draw the led matrix.
XMatrix has n XSprite, I want to animate these sprites and update the frame object based on all these sprites.

So far, I can create XSprite frame, update its data and the led display updates as expected.

I am now trying to implement XMatrix.
When I instantiate new sprites (other than the frame), no problem, but once I add them to my Matrix class, the display doesn’t work any more :frowning:

I have been looking around for some kind explanation, but due to my lack of C knowledge it is pretty hard.

I am copying my code in hope for some generous soul to have a look, I have tried to keep things very simple and added comments.

Thank you in advance.

#include <TimerOne.h>

// GRAPHICS
byte A = {255,136,136,255,136,136,136}; //data representing letter A
byte B = {240,136,136,240,136,136,240}; // data representing letter B

// SHIFT REGISTER 595
int latchPin = 8;
int clockPin = 12;
int dataPin = 11;
int currentRow = 0;
byte rowsByte = {128,64,32,16,8,4,2,1};

class XSprite
{
public:
int _width; //num columns
int _height;//num rows
int _x;//position x in the XMatrix
int _y;//position x in the XMatrix
byte _data[7];//bytes for display
XSprite(int w, int h, int x, int y);//constructor
void setData(byte[7]);//set bytes for display
void move(int x, int y);//to update x y properties

};
//CONSTRUCTOR
XSprite::XSprite(int w, int h, int x, int y){
_width = w;
_height = h;
_x = x;
_y = y;
}
//COPY BYTE ARRAY IN _DATA
void XSprite::setData(byte data[7]){
for(int i = 0; i < _height; i++){
_data = data*;*
* }*
}
class XMatrix
{
* public:*
* int _numSprites;//number of sprites*
* XSprite _sprites;//array to store sprites*
* //XSprite _frame; //REFERENCE TO XSPRITE FRAME CODE, COMPILE FAILS WHEN I UNCOMMENT*
* XMatrix();//constructor*
* void addSprite(XSprite sprite);//add sprite in array and increment counter*
};
//CONSTRUCTOR
XMatrix::XMatrix()
{
* numSprites = 0;
_
}*

void XMatrix::addSprite(XSprite sprite)
{
* _sprites[_numSprites] = sprite;
numSprites++;
_
}*

//FRAME - XSPRITE INSTANCE
XSprite frame(5,7,0,0);
//TWO XSPRITES INSTANCES, IDEALY I WOULD LIKE TO BE ABLE TO ADD XSPRITES DYNAMICALY
XSprite xA(5,7,0,0);
XSprite xB(5,7,6,0);
*//MATRIX - ONLY ONE INSTANCE *
XMatrix matrix;
void setup() {
* //SHIFT REGISTER*
* pinMode(latchPin, OUTPUT);*
* pinMode(clockPin, OUTPUT);*
* pinMode(dataPin, OUTPUT);*

* //update frame in background process : microseconds*
* Timer1.initialize(1000);*
* Timer1. attachInterrupt(render);*

* //THIS IS THE PART THAT FAILS, CODE COMPILES BUT THE LED DISPLAY SHOW A FEW RANDOM DOTS AND STAYS SO*
* //WHEN UNCOMMENTING THESE 2 LINES NO PROBLEM*
* //xA.setData(A);//copies data declared in the beginning*
* //xB.setData(B);//copies data declared in the beginning*

* //WHEN UNCOMMENTING ONE OF THESE LINES CODE COMPILES BUT DISPLAY DOESNT WORK*
* //matrix.addSprite(xA);//add XSprite to matrix*
* //matrix.addSprite(xB);*
}
void loop() {
* frame.setData(A);//copies data declared in the beginning*
* delay(1000);*
* frame.setData(B);//copies data declared in the beginning*
* delay(1000);*
}
//UPDATE DISPLAY
void render(){
* digitalWrite(latchPin, LOW);*
* shiftOut(dataPin, clockPin, LSBFIRST, frame._data[currentRow]);//NOT SURE THIS IS THE WAY I SHOULD ACCESS THE FRAME DATA BUT IT WORKS*
* shiftOut(dataPin, clockPin, LSBFIRST, rowsByte[currentRow]);*
* digitalWrite(latchPin, HIGH);*
* currentRow++;*
* currentRow%=7;*
}

In case my long explanation is not totally clear, I have made a drawing 8) https://picasaweb.google.com/lh/photo/rLnLipIxKDrZELuS7tOU3-bcuCOFlv5LhEJP-bL9y9M?feat=directlink

Sprites in a 'matrix' => get the bits from the visible area => update frame => draw frame

I was quickly up to move/shift/rotate/scroll the bits in the frame. I write these kind of things in Javascript/Actionscript easily, C is another beast, syntax is nice, but memory/cursors .. are very new to me. I 'm not asking for someone to write the code, just some pointers, links or advices.

Thank you

//COPY BYTE ARRAY IN _DATA
void XSprite::setData(byte data[7])
    {
    for(int i = 0; i < _height; i++)
        {
        _data = data;
        }
     }

You need to index the arrays:

  _data[i] = data[i];

(Darn! Without the ‘code’ tags the brackets don’t show up! Edited.)

Note: You will be in deep trouble if _height is ever greater than 7.

I had started with something more dynamic, where I could have sprites of any length. As usual with things not working you strip down everything to the simpliest form. I started reading http://publications.gbdirect.co.uk/c_book/thecbook.pdf which is heavily boring as you might think. I want to avoid writing this program on desktop computer and updating arduino via serial. It was already difficult learning the hardware setup, shift registers, current inverters, resistors etc ... I figured the software part would be piece of cake :grin: