Switch to full style
Understanding the language, error messages, etc.
Post a reply

Include numbers in gb.popup

Mon Oct 10, 2016 6:03 pm

Hey!
I'm trying to include numbers in popup but i get a lot of error...

Code:
      int Level = 3;
     
      if(gb.buttons.pressed(BTN_A))
        gb.popup(F("You beat the level " + Level), 20);


I don't know how F() works.
I alway get this error :
Code:
 in expansion of macro 'F'


...

Re: Include numbers in gb.popup

Tue Oct 11, 2016 2:08 am

I can't "I" am going to say this,lol. But you should really check out the reference for the functions found here....

http://gamebuino.com/wiki/index.php?title=Reference

where you are trying to add an argument for the level is actually for duration of the pop up. Instead it would be easier to write a popup for each level and then have it called once per level.

Re: Include numbers in gb.popup

Tue Oct 11, 2016 11:50 am

DON'T LAUGH ABOUT MY ORTHOGRAPH... edit: And grammar
Last edited by STUDIOCRAFTapps on Tue Oct 11, 2016 11:54 am, edited 1 time in total.

Re: Include numbers in gb.popup

Tue Oct 11, 2016 11:54 am

Duhjoker wrote:I can't "I" am going to say this,lol. But you should really check out the reference for the functions found here....

http://gamebuino.com/wiki/index.php?title=Reference

where you are trying to add an argument for the level is actually for a duration of the pop-up. Instead, it would be easier to write a popup for each level and then have it called once per level.


Write a popup for each level? If we replace level by score, I need to add a lot of pop-ups.

Re: Include numbers in gb.popup

Tue Oct 11, 2016 12:19 pm

Popups only accept stings stored in PROGMEM so it doesn't eat up all the RAM. When you use F("your string"), it calls a macro that will make sure the string is stored in flash memory (aka PROGMEM) and *not* transferred to RAM when the function is called.

Pros : it saves RAM
Cons : content must be set at compilation time, it can't be dynamically changed at running time. So you can't use if for level or score, except if you create a separate string for each level as Duhjocker suggested.

Alternative solution : make your own popup function using the Arduino String class. Takes more RAM, but it would be more versatile.

Here is a short example I just made you :

Code:
#include <SPI.h>
#include <Gamebuino.h>
Gamebuino gb;

String myPopupText;
byte myPopupTimeLeft;

void setup(){
  gb.begin();
  gb.titleScreen(F("Custom Popup"));
}

void loop(){
    if(gb.update()){ //update everything
      gb.display.println(F("Custom popups"));
      gb.display.println(gb.frameCount);

      if(gb.buttons.pressed(BTN_A)){
        myPopup(String("Frames = ") + gb.frameCount, 40);
      }

      myPopupUpdate();

      if(gb.buttons.pressed(BTN_C)){
        gb.titleScreen(F("Custom Popup"));
      }
    }
}

void myPopup(String text, byte time){
  myPopupText = text;
  myPopupTimeLeft = time;
}

void myPopupUpdate(){
  if (myPopupTimeLeft){
    gb.display.fontSize = 1;
    gb.display.setColor(WHITE);
    gb.display.fillRoundRect(0,LCDHEIGHT-gb.display.fontHeight-3,84,gb.display.fontHeight+3,3);
    gb.display.setColor(BLACK);
    gb.display.drawRoundRect(0,LCDHEIGHT-gb.display.fontHeight-3,84,gb.display.fontHeight+3,3);
    gb.display.cursorX = 4;
    gb.display.cursorY = LCDHEIGHT-gb.display.fontHeight-1;
    gb.display.print(myPopupText);
    myPopupTimeLeft--;
  }
}

Re: Include numbers in gb.popup

Thu Oct 13, 2016 12:26 am

great post Rodot and something i really needed to look at to for my current Gamebuino project
Post a reply