load a logo before the program

edited August 2015 in How To...

I have written a program in processing. However I need to load my logo before the program starts and display it for one second.

How do I go about it? I tried

void setup(){

size(200,200); logo = loadImage("cover.png"); logo.resize(200,200); image(logo,0,0);

delay(1000);

...

}

but all that does is it just introduce a delay. I understand this is because setup calls the image(, , ) command just once, displays the image and then goes over to the delay command.

But I can't call this in void draw since I want to show the logo just once.

So how do I go about this issue?

Answers

  • you can use the concept of states. A state tells you, in which state or screen the sketch is:

    // consts for states  
    final int stateGame=0;
    final int stateMenu = 1;
    final int stateSplashScreen = 2;
    final int stateHelp = 3;  // when he hits F1 
    // current state 
    int state = stateSplashScreen;
    //
    int playerNumber = 0; 
    //
    int startTime;
    
    // ---------------------------------------------------------------
    //
    void setup()
    {
      // setup() runs once
      // init
      size(800, 600);
      startTime=millis();
    } // func 
    //
    //
    void draw() 
    { 
      // draw() runs on and on in a loop
      background(255);
      fill(0);
      stroke(0);
      textSize(22);
    
      // states: 
      switch(state) {
      case stateGame:
        drawForStateGame();
        break;
      case stateSplashScreen:
        // show splash screen / start screen
        drawForStateSplashScreen();
        break; 
      case stateHelp:
        drawForStateHelp();
        break;  
      case stateMenu:
        drawForStateMenu();
        break; 
      default:
        // error 
        break;
      } // switch
      //
    } // func 
    
    // ------------------------------------------
    // draw the states
    
    void drawForStateGame() {
      text("Game...", 200, 200);
      text(playerNumber 
        + " Players. Hit F1 for help. Hit r to restart.", 
      200, height-20);
      for (int i = 1; i < 10 * playerNumber; i++) 
        signHome(0 + i*20, 100);
    }
    
    void drawForStateSplashScreen() {
      // 
      // the big rect for the splashscreen
      //
      // measure time 
      if (startTime+1600<millis()) { 
        // next state 
        state = stateMenu;
      }
      //
      noStroke();
      fill(111);  // gray 
      rect (100, 100, width-200, height-200);
      // the texts 
      fill(255, 0, 0); // red 
      textSize(32);
      text ("Cool Game!", width/2-100, height /2);
      fill(0);
      textSize(22);
      text ("No mouse, only keyboard...", width/2-10, height /2+60);
      // the small game scene 
      fill(233, 2, 2);
      ellipse (200, 150, 10, 10);  // big ball
      // little trace  
      fill(0, 255, 0); // green 
      noStroke();  
      int step=6;
      for (int i = 1; i < 8; i++) 
        ellipse (200-i*step, 150-i*step, 3, 3);
      rect  (250, 110, 6, 60);  // the paddle
    }
    
    void drawForStateHelp() {
      text("the help...", 200, 100);
    }
    
    void drawForStateMenu() {
      text("the menu", 200, 100);
      text("   1. Play with 2 players ", 200, 150);
      text("   2. Play with 4 players ", 200, 200);
      text("hit 1 or 2  ", 200, 250);
    }
    
    // ---------------------------------------------------
    // Inputs 
    
    void keyPressed() {
      // states: 
      switch(state) {
    
      case stateGame:
        //     
        if (keyCode==java.awt.event.KeyEvent.VK_F1) {
          // F1
          state =  stateHelp;
        } // if 
        else if (key == 'r') {
          startTime=millis();
          state = stateSplashScreen;
        } else if (key==ESC) {
          key=0; // kill esc
          // next state 
          state = stateMenu;
        } else {
          // do nothing
        }
        break;
    
      case stateSplashScreen:
        // next state 
        state = stateMenu;
        if (key==ESC) {
          key=0; // kill esc
        }
        break; 
    
      case stateHelp:
        // any key 
        // back to game 
        state = stateGame;
        if (key==ESC) {
          key=0;  // kill esc
        }
        break;  
    
      case stateMenu:
        // read key in menu
        switch (key) {
        case '1' :
          playerNumber = 2;
          state = stateGame;
          break;
        case '2' :
          playerNumber = 4;
          state = stateGame;
          break;
        case 'x':
          exit();
          break;
        default :
          println ("unknown input");
          break;
        } // inner switch  
        break; 
    
      default:
        // error 
        println("unknown state");
        exit(); 
        break;
      } // outer switch
      //
    }
    
    // =====================================================================
    
    void signHome(int x, int y) {
      // gives a sign for a house / home sign
    
      final int width1=6;
      final int height1=8;
    
      fill( 2, 255, 0 );
      strokeWeight(1);
      stroke ( 2, 255, 0 );
      noFill();
      x+=21;
      y+=14;
      triangle ( x-width1, y, 
      x, y-height1, 
      x+width1, y );
      rect(x-width1/2, y, width1, width1);
      rect(x-width1/4+1, y+height1/4+1, width1/4, width1/3);
      strokeWeight(1);
    }
    
    // ======================================================
    
  • edited August 2015 Answer ✓

    You can do it in the draw() :

    void draw() {
      if (millis() < 1000) { // If the program is running since less than a second ...
        if (frameCount == 1) { // If it's the 1st frame (so it's displayed once) ...
          image(logo, 0, 0, 200, 200); // Display ! And no need to resize the logo with this !
        }
      }
      else {
        // Your code
      }
    }
    

    And in one line :

    void draw() {
      if (millis() < 1000) if (frameCount == 1) image(logo, 0, 0, 200, 200);
      else {
        // Your code
      }
    }
    
Sign In or Register to comment.