Drawing Map from arrays

Understanding the language, error messages, etc.

Re: Drawing Map from arrays

Postby STUDIOCRAFTapps » Fri Oct 07, 2016 1:35 pm

Ok, merci pour le conseil :)
User avatar
STUDIOCRAFTapps
 
Posts: 86
Joined: Sun Oct 02, 2016 11:58 pm
Location: Deep in the web

Re: Drawing Map from arrays

Postby STUDIOCRAFTapps » Fri Oct 07, 2016 8:00 pm

*Deleted useless content*
Last edited by STUDIOCRAFTapps on Fri Oct 07, 2016 8:08 pm, edited 1 time in total.
User avatar
STUDIOCRAFTapps
 
Posts: 86
Joined: Sun Oct 02, 2016 11:58 pm
Location: Deep in the web

Re: Drawing Map from arrays

Postby STUDIOCRAFTapps » Fri Oct 07, 2016 8:07 pm

I've teak out the script and the problem change...

I realy need some help, if someone can put it in an emulator and help me...

Code: Select all
#include <Backlight.h>
#include <Battery.h>
#include <Buttons.h>
#include <Display.h>
#include <Gamebuino.h>
#include <Sound.h>
#include <SPI.h>

#include <math.h>

Gamebuino gb;

const PROGMEM byte Empty[] = //ID: 0
{
  8,8,
  B00000000,
  B00000000,
  B00000000,
  B00000000,
  B00000000,
  B00000000,
  B00000000,
  B00000000,
};

const PROGMEM byte Brick[] = //ID: 1
{
  8,8,
  B11111111,
  B00100001,
  B11111111,
  B10001000,
  B11111111,
  B00100001,
  B11111111,
  B10001000,
};

const PROGMEM byte SharpBrick[]= //ID: 2
{
  8,8,
  B11111111,
  B11000011,
  B10100101,
  B10011001,
  B10011001,
  B10100101,
  B11000011,
  B11111111,
};
const PROGMEM byte TowerBrick[]= //ID: 3
{
  8,8,
  B10101101,
  B10110101,
  B10101101,
  B10110101,
  B10101101,
  B10110101,
  B10101101,
  B10110101,
};

const PROGMEM byte Piston[]= //ID: 4
{
  8,8,
  B00000000,
  B01111110,
  B00100100,
  B00100100,
  B01000010,
  B00011000,
  B01111110,
  B00000000,
};

const PROGMEM byte Holder[]= //ID: 5
{
  8,8,
  B11111111,
  B10100101,
  B11111111,
  B00000000,
  B00000000,
  B00000000,
  B00000000,
  B00000000,
};

const PROGMEM byte  Teleporter1[]= //ID: 6
{
  8,8,
  B11111111,
  B10101011,
  B11010110,
  B10101100,
  B10101100,
  B11010110,
  B10101011,
  B11111111,
};

const PROGMEM byte Teleporter0[]= //ID: 7
{
  8,8,
  B11111111,
  B11010101,
  B01101011,
  B00110101,
  B00110101,
  B01101011,
  B11010101,
  B11111111,
};

const PROGMEM byte Wall[]= //ID: 8
{
  8,8,
  B11110000,
  B10010000,
  B10010000,
  B10010000,
  B10010000,
  B10010000,
  B10010000,
  B11110000,
};

const PROGMEM byte Ennemie[]= //ID: 8
{
  8,8,
  B11111111,
  B10000001,
  B10010011,
  B10010011,
  B10000001,
  B10000001,
  B10000001,
  B11111111,
};

const byte Map[] PROGMEM = {
0,0,0,0,0,0,0,0,0,0,0,0,2,
2,1,2,5,5,8,0,0,0,0,0,0,6,
3,0,0,0,0,8,0,0,0,0,0,0,2,
3,0,0,0,0,0,4,5,2,0,0,0,3,
2,0,2,0,0,0,0,0,0,0,0,0,3,
3,0,3,0,0,0,0,0,0,0,0,0,3,
3,0,3,0,0,0,2,5,5,0,5,5,2,
3,0,3,0,9,0,3,0,9,0,0,0,6,
2,0,2,1,1,1,2,1,1,1,1,1,2,
};

#define LCDWidth 84
#define LCDHeight 48

byte MapHeigth = 9;
byte MapWidth = 13;

byte SpawnX = 8;
byte SpawnY = 7;

const byte* sprites[10] = {
  Empty,
  Brick,
  SharpBrick,
  TowerBrick,
  Piston,
  Holder,
  Teleporter0,
  Teleporter1,
  Wall,
  Ennemie
};

byte getTile(byte x, byte y){
  /*if(x & 1)//odd
    return pgm_read_byte(Map + y*(13) + x/2) & B00001111;
  else //even
    return (pgm_read_byte(Map + y*(9) + x/2) >> 4);*/
    return pgm_read_byte(Map + (x+y*13));
}

int CPosX = 0;
int CPosY = 0;

int PPosX = 0;
int PPosY = 0;

byte SquiseVertical = 8;
byte SquiseHorizontal = 8;

void setup() {
  // put your setup code here, to run once:
  gb.begin();
  gb.titleScreen(F("BIGBLACKBOX Testing"));
  PPosX = 0;
  PPosY = 8;
}

void DrawPlayer () {
    gb.display.fillRect((PPosX - round(SquiseHorizontal/2.0) + LCDWIDTH/2) - CPosX, (PPosY - round(SquiseVertical/2.0) + LCDHEIGHT/2) - CPosY, SquiseHorizontal, SquiseVertical);
}

void ClampCamera () {
  /*CPosX = PPosX + (LCDWidth/2);
  CPosY = PPosY + (LCDHeight/2);
 
  if(PPosX + (LCDWidth/2) < 0) {
    CPosX = (LCDWidth/2);
  } else {
    //CPosX = PPosX - (LCDWidth/2);
  }
 
  if(PPosY + (LCDHeight/2) < 0) {
    CPosY = (LCDHeight/2);
  } else {
    CPosY = PPosY - (LCDHeight/2);
  }

  if(PPosX - (LCDWidth/2) >= MapWidth) {
    CPosX = MapWidth - (LCDWidth/2);
  } else {
    //CPosX = PPosX - (LCDWidth/2);
  }
 
  if(PPosY - (LCDHeight/2) >= MapHeigth) {
    CPosY = MapWidth - (LCDHeight/2);
  } else {
    //CPosY = PPosY - (LCDHeight/2);
  }
  */

 //Minimum: 0,8
 //Maximum: 8*MapWidth-LCDWidth,8*MapHeight-LCDHeight
 
  CPosX = ClampInt(-(8*MapWidth-LCDWidth),0,PPosX);
  CPosY = ClampInt(-(8*MapHeigth-LCDHeight),-8,PPosY);

  //CPosX = -(8*MapWidth-LCDWidth);
  //CPosY = -(8*MapHeigth-LCDHeight);
}

int ClampInt (int minv, int maxv, int value) {
  if(value < minv)
    return minv;
  else if(value >= maxv)
    return maxv;
  else
    return value;
}

void loop() {
  // put your main code here, to run repeatedly:
  if(gb.update()) {
      if(gb.buttons.pressed(BTN_UP))
        PPosY++;
      if(gb.buttons.pressed(BTN_DOWN))
        PPosY--;
      if(gb.buttons.pressed(BTN_RIGHT))
        PPosX--;
      if(gb.buttons.pressed(BTN_LEFT))
        PPosX++;

      //CPosX = PPosX;
      //CPosY = PPosY;

      DrawPlayer();
      ClampCamera();
     
      for(byte x = 0; x < 13; x++) {
        for(byte y = 0; y < 9; y++) {
          byte Id;
          Id = getTile(x,y);
          gb.display.drawBitmap(CPosX + x*8, CPosY + y*8, sprites[Id]);
        }
      }

      //gb.display.print(PPosX+", "+PPosY);
  }
}
User avatar
STUDIOCRAFTapps
 
Posts: 86
Joined: Sun Oct 02, 2016 11:58 pm
Location: Deep in the web

Re: Drawing Map from array

Postby Duhjoker » Tue Oct 11, 2016 3:01 am

Ok first it's

Code: Select all
Const byte bitmap[] PROGMEM = { B00000000, B101010 etc

Const has to be next to byte.

Next your trying to give each bit map an id to call it in your sketch but that's not the way to do it. You want to use........

Code: Select all
const byte *spritesheet[] = {brick, stone, etc }


What that does is saves the bit maps to flash or Progmem then you give them a constants integer where brick would be 0 stone would be 1 and etc 2 and on down the list.

Then you can use the tilemap function to create the const byte arrays to make world/game maps. It's super cheap on Progmem, also the spritesheet command doesn't use the [10] in its arguments

It would also be easier to create a bitmap Sprite of your player since you have already built the terrain sprites in that way.

I don't understand some of the variables you've listed and cannot find them in the reference. The camera controls used in the collide bitmap newbs thread would be better an easier for you to implement til you can learn better ways. They are proven to work and should help you out quite a bit.

The LCD height and width are predefined in the display library and not needed in the sketch.

Lastly you never closed your void loop with a };

Have you checked the newbs threads? Everything your asking and trying to do is in the latter half and it's quite commented. I started with out being able to compile a bitmap and now I'm halfway through building my game.

Have you compiled anything at all? Not trying to sound rude have you tried to compile anything small like with a bitmap or anything really. You should start small like that then work your way up. Looks like your trying to do too much at once with out properly understanding what your doing. That's fine we're here to help but I suggest doing some studying of the Gamebuino reference and clicking on each one for more info then just looking at the source
User avatar
Duhjoker
 
Posts: 446
Joined: Sat Jul 02, 2016 4:57 am
Location: Where Palm trees grow

Re: Drawing Map from arrays

Postby Sutchig » Sat Oct 15, 2016 6:36 pm

Hi,
since its been awhile since your last post, I want to know if your problem still exists.
I compiled your last code without errors. It runs, but the movements are weird i guess.
What do want to achieve? Move tile by tile (8 px per step) or 1px per step/button press?
And you have a lot of 'int's, where I think 'char' should be enough.
Sutchig
 
Posts: 67
Joined: Sat May 23, 2015 3:48 pm

Previous

Return to Programming Questions

Who is online

Users browsing this forum: No registered users and 18 guests

cron