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 > noob needs some help

#166227 - cptblackbeard - Mon Feb 02, 2009 6:56 pm

hi,

im a first year student software developer from belgium (so exuus my bad english)
and im new at programming and im def new at c++ and game programming
ive been wresteling myself through some tuttorials
and im now making my verry own hello world and look i can actualy get some respons on a button program's :) so i whas thinking
on trying to make a little textbased game or something
i dont now what im doing wrong but i just cant get a freaking screen to disapear and another to appear
example.

welcome press A to contineu.

i can press A and then my second text will appear when i release (with the Held function) the button the first text comes back wich is all verry logical because its a loop
but now my first question.:p
how can i simply prevent the first text from reapairing

second question
is it normal that only loops
with while(1){}. let my print strings to the screen (question 3 wtf is (1))
i tried for example while(~held & KEY_TOUCH) {printf("blabla");}
this doesnt work .

i know these are noob questions but you got to start somewhere right.
thx

#166230 - Pete_Lockwood - Mon Feb 02, 2009 7:39 pm

It sounds like you want something like:

iprintf("Press A");
while(!<a pressed>)
{
swiWaitForVBlank();
}

iprintf("New message");
while(1)
{
swiWaitForVBlank();
}

but perhaps I'm not understanding the question?
_________________
It's not an illusion, it just looks like one.

#166232 - cptblackbeard - Mon Feb 02, 2009 8:42 pm

what i got so far

Code:
#include <nds.h>
#include <stdio.h>
 
void printWelcome(){
printf("welcome\n");
printf("aaaaaaaaa\n");
printf("aaaaaaaaa\n");
printf("aaaaaaaaa\n");
printf("aaaaaaaaa\n");
printf("\n\n\n\n\n  touch screen to continue \n");
}
void printIntro()
{
   printf("bbbbbbbb\n");
   printf("bbbbbbbb\n");
   printf("bbbbbbbb\n");
}

int main(void)
{
   consoleDemoInit();   
   while(1)
   {
      printWelcome();
      scanKeys();
      int held = keysHeld();
      if(held & KEY_TOUCH)
      {   
         consoleClear();
         printIntro();
      }
      swiWaitForVBlank();
      consoleClear();
   }
   return 0;
}

this codes works but isn what i wanted
i dont want the welcome text to reapair when i let go of the button

#166233 - Echo49 - Mon Feb 02, 2009 9:04 pm

Move printWelcome() outside the main loop. Also, you don't really need the consoleClear() after swiWaitForVBlank()?

As for your question about using "1" as the condition in the while loop; a while loop keeps looping until the condition becomes "false". "0" evaluates as false, while any other number evaluates as true. Therefore, "while (1)" is the same as "while (true)".

#166234 - kusma - Mon Feb 02, 2009 9:09 pm

Perhaps you want something along these lines?
Code:
int main(void)
{
   consoleDemoInit();
   scanKeys(); // call this once at start-up to make keysDown behave
   
   // put your message on screen
   printWelcome();

   while (1) // wait for a tap on the touch-screen
   {
      swiWaitForVBlank(); // there's no point in using all the CPU time at hand
      scanKeys();
      int held = keysDown(); // keysDown() doesn't trigger an event until the button is re-pressed, unlike keysHeld().
      if (held & KEY_TOUCH) break; // exit the loop if the touch-pad was hit
   }
   
   // clear and output the next message
   consoleClear();
   printIntro();
   while (1) // do the same all over again
   {
      swiWaitForVBlank();
      scanKeys();
      int held = keysDown();
      if (held & KEY_TOUCH) break;
   }
   printf("done.\n");
   return 0;
}

edit: added some comments

#166235 - cptblackbeard - Mon Feb 02, 2009 9:26 pm

yes thats it
thanks a 1000 000 guys
if (held & KEY_TOUCH) break
all seems so clear now

#166236 - bean_xp - Mon Feb 02, 2009 11:27 pm

cptblackbeard wrote:
with while(1){}. let my print strings to the screen (question 3 wtf is (1))


while(1) is the same as while(true) which is an infinate loop as the condition is always true.

#166266 - cptblackbeard - Tue Feb 03, 2009 5:47 pm

bean_xp wrote:
cptblackbeard wrote:
with while(1){}. let my print strings to the screen (question 3 wtf is (1))


while(1) is the same as while(true) which is an infinate loop as the condition is always true.


yes im begining to understand the logic there
like i said im new to the game programming and the idea of a infinate loop whas quite weird for me.
because if you should do this in oberon/f your fuckt

#166281 - kusma - Tue Feb 03, 2009 7:20 pm

cptblackbeard wrote:
like i said im new to the game programming and the idea of a infinate loop whas quite weird for me.
because if you should do this in oberon/f your fuckt

Well, it's not really infinate, the runtime is just unknown at entry. Another way of writing it that might be a bit easier to understand is this:
Code:
bool done = false;
while (!done)
{
   doStuff();
   if (condition) done = true;
}

...but I felt that there was no point in having an extra variable when the function was that short. If content of the while-loop is very long, it might be beneficial to do the latter (with an exit-flag), so one immediately see that the loop can end.