#include <nds.h>
#include <registers_alt.h>
static int shape_width = 15;
static int shape_height = 15;
static int old_x = 0;
static int old_y = 0;
static int shape_x = 0;
static int shape_y = 0;
static uint16 line_color = RGB15(31,31,31);
static uint16 COLOR_A = RGB15(31,0,0);
static uint16 COLOR_B = RGB15(0,0,31);
static int MID_X=SCREEN_WIDTH /2;
static int MID_Y=SCREEN_HEIGHT/2;
static int direction = 1;
int min_x = 4096 , min_y = 4096, max_x = 0, max_y = 0;
int min_px = 4096 , min_py = 4096, max_px = 0 , max_py = 0;
touchPosition touch;
volatile int frame = 0;
enum { CONTINUOUS, SINGLE } TouchType = CONTINUOUS;
void draw_shape(int x, int y, uint16* buffer, uint16 color)
{
buffer += y * SCREEN_WIDTH + x;
int i;
for(i = 0; i < shape_height; ++i) {
uint16* line = buffer + (SCREEN_WIDTH * i);
int j;
for(j = 0; j < shape_width; ++j) {
*line++ = color;
}
}
}
void draw_shape_b(int x, int y, uint16* buffer, uint16 color){
draw_shape(x-shape_width/2,y-shape_width/2,buffer, color);
}
void draw_line_horiz(int x, int y,int deltax,uint16* buffer, uint16 color)
{
buffer += y * SCREEN_WIDTH + x;
int i=0;
//for(i = 0; i < shape_height; ++i) {
uint16* line = buffer + (SCREEN_WIDTH * i);
int j;
for(j = 0; j < deltax; ++j) {
*line++ = color;
}
// }
}
void draw_line_vert(int x, int y,int deltax,const uint16* buf, uint16 color)
{
uint16* buffer = buf +y * SCREEN_WIDTH + x;
uint16* line = buffer;// + (SCREEN_WIDTH * i);
int j;
for(j = 0; j < deltax; ++j) {
*line = color;
line += SCREEN_WIDTH;
}
}
void draw_rect(int ulx1,int uly1,int w1){
draw_line_horiz(ulx1,uly1,w1,VRAM_A,line_color);
draw_line_horiz(ulx1,SCREEN_HEIGHT-uly1,w1,VRAM_A,line_color);
draw_line_vert(ulx1,uly1,w1,VRAM_A,line_color);
draw_line_vert(SCREEN_WIDTH-ulx1,uly1,w1,VRAM_A,line_color);
}
void draw_board(){
int w1=(SCREEN_HEIGHT*90)/100;
int ulx1 =MID_X - w1/2;
int uly1 = MID_Y-w1/2;
int w2=(SCREEN_HEIGHT*60)/100;
int ulx2 =MID_X - w2/2;
int uly2 = MID_Y-w2/2;
int w3=(SCREEN_HEIGHT*30)/100;
int ulx3 =MID_X - w3/2;
int uly3 = MID_Y-w3/2;
//int w1=(SCREEN_HEIGHT*90)/100;
//int h1 = 20;
draw_rect(ulx1,uly1,w1);
draw_rect(ulx2,uly2,w2);
draw_rect(ulx3,uly3,w3);
draw_line_horiz(ulx1,MID_Y,ulx3-ulx1,VRAM_A,line_color);
draw_line_horiz(SCREEN_WIDTH-ulx3,MID_Y,ulx3-ulx1,VRAM_A,line_color);
draw_line_vert(MID_X,uly1,ulx3-ulx1,VRAM_A,line_color);
draw_line_vert(MID_X,SCREEN_HEIGHT-uly3,ulx3-ulx1,VRAM_A,line_color);
int board[7][7];
int i,j;
for (i=0;i<7;i++){
for (j=0;j<7;j++){
board[i][j]=0;
}
}
board[0][0] = 1;
board[0][3] = 1;
board[0][6] = 1;
board[6][0] = 2;
board[2][2] = 2;
board[6][3] = 2;
// board[0][0] = 1;
// board[0][0] = 1;
// board[0][0] = 1;
int m = (SCREEN_WIDTH*30/2)/100;
int n = (SCREEN_HEIGHT*30/2)/100+1;
for (i=0;i<7;++i){
for (j=0;j<7;++j){
// int xx = ulx1+(i*SCREEN_WIDTH*30)/100;
int xx = ulx1+i*n;//30;//SCREEN_WIDTH*30)/100;
int yy = uly1+n*j;
if (board[i][j]==1){
draw_shape_b(xx,yy,VRAM_A,COLOR_A);
}else if (board[i][j]==2){
draw_shape_b(xx,yy,VRAM_A,COLOR_B);
}
}
}
//draw_shape_b(SCREEN_WIDTH-ulx1,uly1,VRAM_A,RGB15(0,0,31));
}
void on_irq()
{
if(REG_IF & IRQ_VBLANK) {
// draw_shape(old_x, old_y, VRAM_A, RGB15(0, 0, 0));
// draw_shape(shape_x, shape_y, VRAM_A, RGB15(31, 0, 0));
// Tell the DS we handled the VBLANK interrupt
VBLANK_INTR_WAIT_FLAGS |= IRQ_VBLANK;
REG_IF |= IRQ_VBLANK;
}
else {
// Ignore all other interrupts
REG_IF = REG_IF;
}
}
void InitInterruptHandler()
{
REG_IME = 0;
IRQ_HANDLER = on_irq;
REG_IE = IRQ_VBLANK;
REG_IF = ~0;
DISP_SR = DISP_VBLANK_IRQ;
REG_IME = 1;
}
void Vblank() {
//---------------------------------------------------------------------------------
//frame++;
}
int main(int argc, char ** argv)
{
int min_x = 4096 , min_y = 4096, max_x = 0, max_y = 0;
int min_px = 4096 , min_py = 4096, max_px = 0 , max_py = 0;
touchPosition touch;
powerON(POWER_ALL_2D);
// put the main screen on the bottom lcd
lcdMainOnBottom();
// Initialise the interrupt system
irqInit();
// install our simple vblank handler
irqSet(IRQ_VBLANK, Vblank);
// enable the interrupt
irqEnable(IRQ_VBLANK);
//initOAM();
//enable vram and map it to the right places
vramSetMainBanks( VRAM_A_MAIN_SPRITE, //A and B maped consecutivly as sprite memory
VRAM_B_MAIN_SPRITE, //this gives us 256KB which is the max
VRAM_C_MAIN_BG_0x06000000, //map C to background memory
VRAM_D_LCD //not using D
);
//set the video mode
videoSetMode( MODE_0_2D |
DISPLAY_SPR_ACTIVE | //turn on sprites
DISPLAY_BG0_ACTIVE | //turn on background 0
DISPLAY_SPR_1D //this is used when in tile mode
);
int i;
// black backdrop
BG_PALETTE[0]=RGB15(0,0,0);
BG0_CR = BG_MAP_BASE(31);//use bg0 for the text
BG_PALETTE[255] = RGB15(31,31,31);//by default font rendered with color 255
//consoleInit() is a lot more flexible but this gets you up and running quick
//consoleInitDefault((u16*)SCREEN_BASE_BLOCK(31), (u16*)CHAR_BASE_BLOCK(0), 16);
videoSetMode(MODE_FB0);
vramSetBankA(VRAM_A_LCD);
irqInit();
// install our simple vblank handler
irqSet(IRQ_VBLANK, Vblank);
// enable the interrupt
irqEnable(IRQ_VBLANK);
//videoSetMode(0);
videoSetModeSub(MODE_0_2D | DISPLAY_BG0_ACTIVE);
vramSetBankC(VRAM_C_SUB_BG);
SUB_BG0_CR = BG_MAP_BASE(31);
// Set the colour of the font to White.
BG_PALETTE_SUB[255] = RGB15(31,31,31);
consoleInitDefault((u16*)SCREEN_BASE_BLOCK_SUB(31), (u16*)CHAR_BASE_BLOCK_SUB(0), 16);
printf("\n\n\tNine Men's Morris\n");
//lcdSwap();
iprintf("\x1b[4;8HTouch Screen Test");
iprintf("\x1b[15;4HRight Shoulder toggles");
while(1) {
//swiWaitForVBlank();
scanKeys();
touchPosition touchXY;
touchXY.px = 0;
touchXY.py = 0;
int pressed = keysDown(); // buttons pressed this loop
uint32 kh = keysHeld();
if (kh&KEY_TOUCH){
touchXY = touchReadXY();
}
printf("\x1b[10;0H");
printf("Touch x = %d \n", touchXY.px);
printf("Touch y = %d \n", touchXY.py);
// read the button states
// scanKeys();
// read the touchscreen coordinates
touch=touchReadXY();
// int pressed = keysDown(); // buttons pressed this loop
int held = keysHeld(); // buttons currently held
// Right Shoulder button toggles the mode
if ( pressed & KEY_R) TouchType ^= SINGLE;
iprintf("\x1b[14;4HTouch mode: %s",TouchType==CONTINUOUS?"CONTINUOUS ":"SINGLE SHOT");
iprintf("\x1b[6;5HTouch x = %04X, %04X\n", touch.x, touch.px);
iprintf("\x1b[7;5HTouch y = %04X, %04X\n", touch.y, touch.py);
iprintf("\x1b[0;18Hkeys: %08X\n", keysHeld());
iprintf("\x1b[9;10HFrame %d\n", frame);
if ( TouchType == SINGLE && !(pressed & KEY_TOUCH) ) continue;
if ( !(held & KEY_TOUCH) || touch.x == 0 || touch.y == 0) continue;
iprintf("\x1b[12;12H(%d,%d) ",touch.px,touch.py);
if ( touch.x > max_x) max_x = touch.x;
if ( touch.y > max_y) max_y = touch.y;
if ( touch.px > max_px) max_px = touch.px;
if ( touch.py > max_py) max_py = touch.py;
if ( touch.x < min_x) min_x = touch.x;
if ( touch.y < min_y) min_y = touch.y;
if ( touch.px < min_px) min_px = touch.px;
if ( touch.py < min_py) min_py = touch.py;
iprintf("\x1b[0;0H(%d,%d) ",min_px,min_py);
iprintf("\x1b[1;0H(%d,%d) ",min_x,min_y);
iprintf("\x1b[22;21H(%d,%d)",max_x,max_y);
iprintf("\x1b[23;23H(%d,%d)",max_px,max_py);
if (TouchType==SINGLE){
draw_board();
}
} return 0;
} // End of main()
|