Mon Jul 13, 2015 6:21 am
#include <SPI.h>
#include <Gamebuino.h>
const byte PROGMEM map_test[]={
1,1,1,1,1,1,1,1,1,1,1,1,0,2,2,2,2,2,2,2,0,0,0,3,
1,1,1,1,1,1,1,1,1,1,1,0,0,0,2,2,2,2,2,0,0,0,0,3,
2,1,1,1,1,1,1,1,1,1,1,0,0,0,0,2,2,0,0,0,0,0,0,3,
2,2,0,0,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,3,
2,2,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3,
2,2,2,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,3,
2,2,2,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,3,3,
0,2,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,3,3,
3,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,3,3,3,
3,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3,3,3,3,
3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,
3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3
};
const byte PROGMEM tile_0[]={ 8,8,0x80,0x84,0x10,0x41,0x00,0x20,0x02,0x00 };
const byte PROGMEM tile_1[]={ 8,8,0x10,0x30,0x28,0x60,0x54,0xC0,0xFE,0x10 };
const byte PROGMEM tile_2[]={ 8,8,0x00,0x00,0x08,0x14,0x26,0x52,0xD9,0x88 };
const byte PROGMEM tile_3[]={ 8,8,0xFA,0xDF,0xAD,0xFA,0xEF,0xD7,0xFF,0xFD };
const byte* tiles[]={ tile_0, tile_1, tile_2, tile_3 };
Gamebuino gb;
void setup(){
gb.begin();
gb.setFrameRate(60);
const byte map_width = 24;
const byte map_height = 24;
const byte tile_width = 8;
const byte tile_height = 8;
byte camera_x = 0;
byte camera_y = 0;
byte x_v = 1;
byte y_v = 1;
while(1){
if(gb.update()){
for(byte y = 0; y <= 6; y++){
for(byte x = 0; x <= 11; x++){
gb.display.drawBitmap(x*tile_width-camera_x%8, y*tile_height-camera_y%8, tiles[pgm_read_byte(map_test+((camera_y/8+y)*map_width+camera_x/8+x))]);
}
}
camera_y+=-gb.buttons.repeat(BTN_UP,1)*(camera_y > 0)+gb.buttons.repeat(BTN_DOWN,1)*(camera_y < map_height*tile_height-48);
camera_x+=-gb.buttons.repeat(BTN_LEFT,1)*(camera_x > 0)+gb.buttons.repeat(BTN_RIGHT,1)*(camera_x < map_width*tile_width-84);
}
}
}
void loop(){
while(1){
}
}
Mon Jul 13, 2015 11:54 am
int mapx = camera_x/8+x;
int mapy = camera_y/8+y;
if ((mapx>=0) && (mapx<map_width) && (mapy>=0) && (mapy<map_height))
gb.display.drawBitmap(x*tile_width-camera_x%8, y*tile_height-camera_y%8, tiles[pgm_read_byte(map_test+mapy*map_width+mapx)]);
Mon Jul 13, 2015 12:56 pm
Myndale wrote:With respect to the underlying cause, map_width and map_height are both being set to 24 but the map array itself is 24x12, so the first step is to set map_height to 12.
#include <SPI.h>
#include <Gamebuino.h>
const byte PROGMEM map_test[]={
2,2,2,2,2,2,2,2,2,2,2,2,1,3,3,3,3,3,3,3,1,1,1,4,
2,2,2,2,2,2,2,2,2,2,2,1,1,1,3,3,3,3,3,1,1,1,1,4,
3,2,2,2,2,2,2,2,2,2,2,1,1,1,1,3,3,1,1,1,1,1,1,4,
3,3,1,1,2,2,2,2,2,2,1,1,1,1,1,1,1,1,1,1,1,1,1,4,
3,3,1,1,1,1,2,2,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,4,
3,3,3,1,1,1,1,1,1,1,1,1,2,2,1,1,1,1,1,1,1,1,1,4,
3,3,3,1,1,1,1,1,1,1,1,2,2,2,1,1,1,1,1,1,1,1,4,4,
1,3,1,1,1,1,1,1,1,1,2,2,2,1,1,1,1,1,1,1,1,1,4,4,
4,1,1,1,1,1,1,1,1,1,1,2,1,1,1,1,1,1,1,1,1,4,4,4,
4,4,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,4,4,4,4,
4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,
4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4
};
const byte PROGMEM tile_0[]={
8,8,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF };
const byte PROGMEM tile_1[]={
8,8,0x80,0x84,0x10,0x41,0x00,0x20,0x02,0x00 };
const byte PROGMEM tile_2[]={
8,8,0x10,0x30,0x28,0x60,0x54,0xC0,0xFE,0x10 };
const byte PROGMEM tile_3[]={
8,8,0x00,0x00,0x08,0x14,0x26,0x52,0xD9,0x88 };
const byte PROGMEM tile_4[]={
8,8,0xFA,0xDF,0xAD,0xFA,0xEF,0xD7,0xFF,0xFD };
const byte* tiles[]={
tile_0, tile_1, tile_2, tile_3, tile_4 };
const byte PROGMEM player[]={
8,8,0x18,0x24,0x24,0x18,0x3C,0x5A,0x24,0x24 };
#define MAP_WIDTH 24
#define MAP_HEIGHT 12
#define TILE_WIDTH 8
#define TILE_HEIGHT 8
Gamebuino gb;
void setup(){
gb.begin();
gb.display.persistence = true;
byte x = 6;
byte y = 6;
byte i;
int camera_x = x*TILE_WIDTH-LCDWIDTH/2+4;
int camera_y = y*TILE_HEIGHT-LCDHEIGHT/2+4;
draw_map(camera_x, camera_y, MAP_WIDTH, MAP_HEIGHT);
gb.display.drawBitmap(x*TILE_WIDTH-camera_x, y*TILE_WIDTH-camera_y, player);
gb.display.update();
while(1){
if(gb.update()){
char x_temp=-gb.buttons.repeat(BTN_LEFT, 1)*(x > 0)+gb.buttons.repeat(BTN_RIGHT, 1)*(x < MAP_WIDTH-1);
char y_temp=-gb.buttons.repeat(BTN_UP, 1)*(y > 0)+gb.buttons.repeat(BTN_DOWN, 1)*(y < MAP_HEIGHT-1);
if(x_temp || y_temp){
for(i = 1; i <=8; i++){
gb.display.clear();
camera_x = x*TILE_WIDTH-LCDWIDTH/2+4+i*x_temp;
camera_y = y*TILE_HEIGHT-LCDHEIGHT/2+4+i*y_temp;
draw_map(camera_x, camera_y, MAP_WIDTH, MAP_HEIGHT);
gb.display.drawBitmap(x*TILE_WIDTH-camera_x+i*x_temp, y*TILE_WIDTH-camera_y+i*y_temp, player);
gb.display.update();
}
x+=x_temp;
y+=y_temp;
}
}
}
}
void loop(){
while(1){
}
}
void draw_map(int camera_x, int camera_y, byte map_width, byte map_height){
for(byte y = 0; y <= 6; y++){
for(byte x = 0; x <= 11; x++){
int tile_x = camera_x/TILE_WIDTH+x;
int tile_y = camera_y/TILE_HEIGHT+y;
byte tile = 0;
if(tile_x >= 0 && tile_x < map_width && tile_y >= 0 && tile_y < map_height){
tile = pgm_read_byte(map_test+(tile_y)*map_width+tile_x);
}
gb.display.drawBitmap(x*TILE_WIDTH-camera_x%TILE_WIDTH, y*TILE_HEIGHT-camera_y%TILE_HEIGHT, tiles[tile]);
}
}
}
Mon Jul 13, 2015 6:26 pm
Mon Jul 13, 2015 6:35 pm
Mon Jul 13, 2015 7:33 pm
byte map[] PROGMEM = {B1000 0001...};
byte map[] PROGMEM = {B1000 0001...};
byte first_half = (map[0] >> 4);
byte second_half = map[0] & (map[0] >> 4);
Mon Jul 13, 2015 7:40 pm
Mon Jul 13, 2015 8:07 pm
Mon Jul 13, 2015 8:20 pm
Tue Jul 14, 2015 8:48 am
#include <SPI.h>
#include <Gamebuino.h>
const byte PROGMEM map_test[]={
2,2,2,2,2,2,2,2,2,2,2,2,1,3,3,3,3,3,3,3,1,1,1,4,
2,2,2,2,2,2,2,2,2,2,2,1,1,1,3,3,3,3,3,1,1,1,1,4,
3,2,2,2,2,2,2,2,2,2,2,1,1,1,1,3,3,1,1,1,1,1,1,4,
3,3,1,1,2,2,2,2,2,2,1,1,1,1,1,1,1,1,1,1,1,1,1,4,
3,3,1,1,1,1,2,2,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,4,
3,3,3,1,1,1,1,1,1,1,1,1,2,2,1,1,1,1,1,1,1,1,1,4,
3,3,3,1,1,1,1,1,1,1,1,2,2,2,1,1,1,1,1,1,1,1,4,4,
1,3,1,1,1,1,1,1,1,1,2,2,2,1,1,1,1,1,1,1,1,1,4,4,
4,1,1,1,1,1,1,1,1,1,1,2,1,1,1,1,1,1,1,1,1,4,4,4,
4,4,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,4,4,4,4,
4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,
4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4
};
const byte PROGMEM tile_0[]={
8,8,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF };
const byte PROGMEM tile_1[]={
8,8,0x80,0x84,0x10,0x41,0x00,0x20,0x02,0x00 };
const byte PROGMEM tile_2[]={
8,8,0x10,0x30,0x28,0x60,0x54,0xC0,0xFE,0x10 };
const byte PROGMEM tile_3[]={
8,8,0x00,0x00,0x08,0x14,0x26,0x52,0xD9,0x88 };
const byte PROGMEM tile_4[]={
8,8,0xFA,0xDF,0xAD,0xFA,0xEF,0xD7,0xFF,0xFD };
const byte* tiles[]={
tile_0, tile_1, tile_2, tile_3, tile_4 };
const byte PROGMEM player[]={
8,8,0x18,0x24,0x24,0x18,0x3C,0x5A,0x24,0x24 };
#define MAP_WIDTH 24
#define MAP_HEIGHT 12
#define TILE_WIDTH 8
#define TILE_HEIGHT 8
Gamebuino gb;
void setup(){
gb.begin();
gb.display.persistence = true;
byte player_x = 6;
byte player_y = 6;
byte i;
int camera_x = player_x*TILE_WIDTH-LCDWIDTH/2+4;
int camera_y = player_y*TILE_HEIGHT-LCDHEIGHT/2+4;
draw_map(camera_x, camera_y, MAP_WIDTH, MAP_HEIGHT);
gb.display.drawBitmap(player_x*TILE_WIDTH-camera_x, player_y*TILE_WIDTH-camera_y, player);
gb.display.update();
while(1){
if(gb.update()){
char x_temp = -gb.buttons.repeat(BTN_LEFT, 1)*(player_x > 0)+gb.buttons.repeat(BTN_RIGHT, 1)*(player_x < MAP_WIDTH-1);
char y_temp = -gb.buttons.repeat(BTN_UP, 1)*(player_y > 0)+gb.buttons.repeat(BTN_DOWN, 1)*(player_y < MAP_HEIGHT-1);
if(x_temp || y_temp){
for(i = 1; i <=8; i++){
gb.display.clear();
camera_x = (player_x*TILE_WIDTH-LCDWIDTH/2+4+i*x_temp);
camera_x = camera_x*(camera_x > 0)+(MAP_WIDTH*TILE_WIDTH-LCDWIDTH-camera_x)*(camera_x > MAP_WIDTH*TILE_WIDTH-LCDWIDTH);
camera_y = player_y*TILE_HEIGHT-LCDHEIGHT/2+4+i*y_temp;
camera_y = camera_y*(camera_y > 0)+(MAP_HEIGHT*TILE_HEIGHT-LCDHEIGHT-camera_y)*(camera_y > MAP_HEIGHT*TILE_HEIGHT-LCDHEIGHT);
draw_map(camera_x, camera_y, MAP_WIDTH, MAP_HEIGHT);
gb.display.drawBitmap(player_x*TILE_WIDTH-camera_x+i*x_temp, player_y*TILE_WIDTH-camera_y+i*y_temp, player);
gb.display.update();
}
player_x += x_temp;
player_y += y_temp;
}
}
}
}
void loop(){
}
void draw_map(int camera_x, int camera_y, byte map_width, byte map_height){
for(byte y = 0; y <= 6; y++){
for(byte x = 0; x <= 11; x++){
int tile_x = camera_x/TILE_WIDTH+x;
int tile_y = camera_y/TILE_HEIGHT+y;
if(tile_x >= 0 && tile_x < map_width && tile_y >= 0 && tile_y < map_height){
gb.display.drawBitmap(x*TILE_WIDTH-camera_x%TILE_WIDTH, y*TILE_HEIGHT-camera_y%TILE_HEIGHT, tiles[pgm_read_byte(map_test+(tile_y)*map_width+tile_x)]);
}
}
}
}