Advertisement
xerpi

Basic draw NDS functions

Apr 22nd, 2012
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.57 KB | None | 0 0
  1. /*Basic draw functions by xerpi*/
  2.  
  3. #include <nds.h>
  4. #include <stdio.h>
  5.  
  6. void drawLine(int x0, int y0, int x, int y, unsigned int color);
  7. void drawHline(int y, int x0, int x1, unsigned int color);
  8. void drawVline(int x, int y0, int y1, unsigned int color);
  9. void drawPlot(int x, int y, unsigned int color);
  10. void drawRect(int x, int y, int w, int h, unsigned int color);
  11. void drawFillRect(int x, int y, int w, int h, unsigned int color);
  12.  
  13. int main(void){
  14.     int i;
  15.     touchPosition touch;
  16.     consoleDemoInit(); 
  17.     videoSetMode(MODE_FB0 | DISPLAY_BG3_ACTIVE);
  18.     vramSetBankA(VRAM_A_LCD);
  19.  
  20.     printf("Basic draw functions by xerpi!\n");
  21.  
  22.     for(i = 0; i < 256 * 192; i++)
  23.         VRAM_A[i] = RGB15(31,0,0);
  24.        
  25.     drawRect( 10, 10, 20, 80, RGB15(0, 31, 0));
  26.     drawFillRect( 100, 100, 50, 30, RGB15(0, 0, 31));
  27.     drawLine( 0, 0, 255, 192, RGB15(0, 0, 31));
  28.     drawPlot(255, 191, RGB15(0, 0, 31));
  29.    
  30.    
  31.     while(1){
  32.         scanKeys();
  33.         consoleClear();
  34.         printf("Basic draw functions by xerpi!\n");
  35.         if(keysHeld() & KEY_TOUCH)
  36.             touchRead(&touch);
  37.         printf("x: %i y: %i!\n", touch.px, touch.py);
  38.         VRAM_A[touch.px + touch.py * 256] = rand();
  39.         if(keysHeld() & KEY_A)
  40.             printf("Key A is pressed");
  41.         swiWaitForVBlank();
  42.     }
  43.  
  44.     return 0;
  45. }
  46.  
  47.  
  48. void drawLine(int x0, int y0, int x, int y, unsigned int color){
  49.     if(x == x0){
  50.         drawVline(x0, y0, y, color);
  51.         return;
  52.     }
  53.    
  54.     if(y == y0){
  55.         drawHline(y0, x0, x, color);
  56.         return;
  57.     }
  58.     int i, x_dist, y_dist;
  59.     float ratio;
  60.    
  61.     if(x0 > x)
  62.         x_dist = x0 - x;
  63.     else
  64.         x_dist = x - x0;
  65.     if(y0 > y)
  66.         y_dist = y0 - y;
  67.     else
  68.         y_dist = y - y0;
  69.    
  70.     ratio = (float)x_dist/(float)y_dist;   
  71.        
  72.     for(i = 0; i < y_dist; i++){   
  73.         VRAM_A[ (int)(x0 + i * ratio) + (int)(y0 + i*256)] = color;
  74.     }
  75. }
  76.  
  77.  
  78. void drawPlot(int x, int y, unsigned int color){
  79.     VRAM_A[x + y*256] = color;
  80. }
  81.  
  82. void drawHline(int y, int x0, int x1, unsigned int color){
  83.     int i;
  84.     if(x0 < x1)
  85.         for(i = x0; i < x1; i++)
  86.             VRAM_A[i + y*256] = color; 
  87.     else
  88.         for(i = x1; i < x0; i++)
  89.             VRAM_A[i + y*256] = color; 
  90.  
  91. }
  92.  
  93.  
  94. void drawVline(int x, int y0, int y1, unsigned int color){
  95.     int i;
  96.     if(y0 < y1)
  97.         for(i = y0; i < y1; i++)
  98.             VRAM_A[x + i*256] = color; 
  99.     else
  100.         for(i = y1; i < y0; i++)
  101.             VRAM_A[x + i*256] = color; 
  102.  
  103. }
  104.  
  105. void drawRect(int x, int y, int w, int h, unsigned int color){
  106.     drawVline(x, y, y + h, color);
  107.     drawVline(x + w, y, y + h, color);
  108.     drawHline(y + h, x, x + w, color);
  109.     drawHline(y, x, x + w, color);
  110. }
  111.  
  112. void drawFillRect(int x, int y, int w, int h, unsigned int color){
  113.     int i;
  114.     for(i = y; i < y + h; i++)
  115.         drawHline(i, x, x + w, color);
  116.  
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement