gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

DS development > Drawing Buttons

#165646 - Doogle - Thu Jan 01, 2009 10:02 am

Major DS newbie here (although many years programming experience in languages other than C and platforms other than DS.)

I've been playing with WiFi and have managed to connect to an AP and buffer and receive data. I'm now getting into the part where I want to send some data via touching a 'button' on the bottom screen and / or one of the physical buttons.

I've been looking at various example code but just can't get my head round the 'simple' problem of drawing a rectangle at a given location and size and putting some text into it. I can draw a rectangle, using videoSetMode(MODE_FB0) but I can't see how to get the text into it. Is there a simple example somewhere or can someone perhaps provide a snippet of code I could work with please.

Thansk and Happy New Year etc..

#165790 - Doogle - Tue Jan 06, 2009 9:42 pm

Well, I think I learnt quite a lot, "simple" was a bit of an understatement!

Having looked through the various tutorials and example code I've managed to achieve the desired results. However, whether it's a 'good' solution (in terms of coding and platform) or not......

The approach I took was to generate an 8 X 8 Font and insert my own graphic characters (representing horizontal and vertical lines, top left and right, bottom left and right corners) at arbitrary locations in the font strip(I used 0x01 through 0x08), and then drew them. Then I used the Custom Font example as a base to draw rectangles and buttons. (Constructive) comments would be appreciated.
Code:

//---------------------------------------------------------------------------------
// Doogle's attempt at drawing 'buttons'
// Based on various nds examples supplied with devkit
// (with apologies to proper C Programmers)
//---------------------------------------------------------------------------------

#include <nds.h>

#include <stdio.h>

//Include the font header generated by grit

#include "font1.h"

PrintConsole * console;

void PrintChar(y,x,st){
   /*
   Print a Character at x,y)
   */   
   iprintf("\x1b[%i;%iH",x,y);
   iprintf("%c",st);
}

void DrawRect(x0,y0,x1,y1) {
/*
Draw a rectangle at (x0,y0),(x1,y1)
*/
   int i;
   for (i=x0;i<x1;i++)
   {
      PrintChar(i,y0,(char) 0x08); // Horizontal line (Top)
      PrintChar(i,y1,(char) 0x01); // Horizontal line (Bottom)
   }
   for (i = y0;i<y1;i++)
   {
      PrintChar(x0,i,(char) 0x02); // Vertical line (Left)
      PrintChar(x1,i,(char) 0x03); // Vertical line (Right)
   }
   PrintChar(x0,y0, (char) 0x05); // Top LH Corner
   PrintChar(x0,y1, (char) 0x06); // Bottom LH Corner
   PrintChar(x1,y0, (char) 0x04); // Top RH Corner
   PrintChar(x1,y1, (char) 0x07); // Bottom RH Corner
}

void DrawButton(int x0,int y0,int x1,int y1, char *st){
/*
Draw a Button at (x0,y0), (x1,y1)
*/
   int i=0;
   DrawRect(x0,y0,x1,y1);
   while (*st){
   /*
   This sort of centers the text within the rectangle
   It's missing loads of tests to make sure it fits etc
   but that can come later
   */
      PrintChar(x0 + sizeof(st)/2 + i,((y1-y0)/2)+y0,*st);
      st++;
      i++;
   }
}
   
int main(void) {
/*
Most of this code was lifted from the 'Custom Font' example
*/
   const int tile_base = 0;
   const int map_base = 20;

   videoSetModeSub(MODE_0_2D);   
   vramSetBankC(VRAM_C_SUB_BG);

   console = consoleInit(0,0, BgType_Text4bpp, BgSize_T_256x256, map_base, tile_base, false);
   ConsoleFont font;

   font.gfx = (u16*)font1Tiles;
   font.pal = (u16*)font1Pal;
   font.numChars = 128;
   font.numColors =  font1PalLen / 2;
   font.bpp = 8;
   font.asciiOffset = 0;
   font.convertSingleColor = false;
   consoleSetFont(console, &font);
   
   DrawButton(6,0,16,4,"Option 1");
   DrawButton(12,6,22,8,"Option 2");
   DrawButton(1,10,12,14,"Option 3");
   
   while(1) {
      swiWaitForVBlank();
   }

   return 0;
}

This really is good fun !!