////////////////////////////////////////////////// // EZsketch (Etch-A-Sketch) // main.c source code file // Dennis R. Shows // http://www.geocities.com/dr_shows // dr_shows@yahoo.com ////////////////////////////////////////////////// #define MULTIBOOT int __gba_multiboot; MULTIBOOT //define register for changing the video mode #define REG_DISPCNT *(unsigned long*)0x4000000 //define RGB #define RGB(r,g,b) ((((b)>>3)<<10)+(((g)>>3)<<5)+((r)>>3)) // define R_CTRLINPUT #define MEM_IO 0x04000000 typedef unsigned short int u16; #define R_CTRLINPUT *(volatile u16 *)(MEM_IO + 0x130) #define F_CTRLINPUT_A_PRESSED (~R_CTRLINPUT & 0x0001) #define F_CTRLINPUT_B_PRESSED ((~R_CTRLINPUT & 0x0002)>>1) #define F_CTRLINPUT_SELECT_PRESSED ((~R_CTRLINPUT & 0x0004)>>2) #define F_CTRLINPUT_START_PRESSED ((~R_CTRLINPUT & 0x0008)>>3) #define F_CTRLINPUT_RIGHT_PRESSED ((~R_CTRLINPUT & 0x0010)>>4) #define F_CTRLINPUT_LEFT_PRESSED ((~R_CTRLINPUT & 0x0020)>>5) #define F_CTRLINPUT_UP_PRESSED ((~R_CTRLINPUT & 0x0040)>>6) #define F_CTRLINPUT_DOWN_PRESSED ((~R_CTRLINPUT & 0x0080)>>7) #define F_CTRLINPUT_R_PRESSED ((~R_CTRLINPUT & 0x0100)>>8) #define F_CTRLINPUT_L_PRESSED ((~R_CTRLINPUT & 0x0200)>>9) //video mode 3 = 240x160 16-bit #define MODE_3 0x3 //use background 2 #define BG2_ENABLE 0x400 //create a pointer to the video buffer unsigned short* videoBuffer = (unsigned short*)0x6000000; //draw a pixel on the mode 3 video buffer void DrawPixel3(int x, int y, unsigned short c) { videoBuffer[y * 240 + x] = c; } ////////////////////////////////////////////////// // Function: main() ////////////////////////////////////////////////// int main(void) { int x, y; int delay; int dx,dy; dx = 120; dy = 80; //switch to video mode 3 (240x160 16-bit) REG_DISPCNT = (MODE_3 | BG2_ENABLE); //fill the screen with white pixels for (x = 20; x < 220; x++) { for (y = 20; y < 140; y++) { DrawPixel3(x, y, RGB(255,255,255)); } } //draw Etch-a-Scetch borders //top for (x = 0; x < 240; x++) { for (y = 0; y < 20; y++) { DrawPixel3(x, y, RGB(255,0,0)); } } //bot for (x = 0; x < 240; x++) { for (y = 140; y < 160; y++) { DrawPixel3(x, y, RGB(255,0,0)); } } //left for (x = 0; x < 20; x++) { for (y = 20; y < 140; y++) { DrawPixel3(x, y, RGB(255,0,0)); } } //right for (x = 220; x < 240; x++) { for (y = 20; y < 140; y++) { DrawPixel3(x, y, RGB(255,0,0)); } } while(1){ // if (F_CTRLINPUT_A_PRESSED) //keyboard z // { if (F_CTRLINPUT_RIGHT_PRESSED) { DrawPixel3(dx, dy, RGB(0,0,0)); if (dx<219) dx++; } if (F_CTRLINPUT_LEFT_PRESSED) { DrawPixel3(dx, dy, RGB(0,0,0)); if (dx>20) dx--; } if (F_CTRLINPUT_UP_PRESSED) { DrawPixel3(dx, dy, RGB(0,0,0)); if (dy>20) dy--; } if (F_CTRLINPUT_DOWN_PRESSED) { DrawPixel3(dx, dy, RGB(0,0,0)); if (dy<139) dy++; } // } //close if A pressed //slow down the program delay = 50000; while (delay--); } //close while return 0; } //close main /* Extensions Challenge 1: Change the color, shape, or size of the Ecth-a-Sketch. Add knobs. Challenge 2: Move the "DrawPixel3(dx, dy, RGB(0,0,0));" into a funtion and call it when needed. Challenge 3: Add a funtion for changing the color or the pixels depending on what button is pressed. Add an eraser. Challenge 4: Make a cursor so the user can tell where the pointer is. Change the size of of the thickness of the line. Challenge 5: Put the "fill the screen with white pixels" and "draw Etch-a-Scetch" sections in a function and have a menu pop up for erasing the screen. Be sure to save the screen before covering it up. Challenge 6: Remove the Etch-a-Sketch border and intead put a image on the screen to draw over. See http://www.geocities.com/dr_shows/EZsketch.bmp for an image. Challenge 7: Make the knobs appear to move when the user draws. For answers to challenges, more demos, help, etc. go to http://www.geocities.com/dr_shows */