Two Questions regarding images

Understanding the language, error messages, etc.

Two Questions regarding images

Postby DrWhoCares » Sun Jan 18, 2015 6:05 pm

Question 1:
I want to display an image as a background that will never change over the course of gameplay. This image is not made up of pure rectangles and instead has points where it may be crooked or jagged. How do I go about doing this? How do I store it?

Question 2:
I have a player sprite that I want to display, easy enough. However, while he's running I want it to loop through running animations, how can I create a sort of "sprite sheet" and run through multiple different states. (A state machine is easy, the actual bitmap changing is the problem.)

Info:
Been looking at some code, but none of it quite answers my questions or works the same way. That, or it's just too alien to me.
Thanks.
DrWhoCares
 
Posts: 14
Joined: Fri Sep 05, 2014 6:48 pm

Re: Two Questions regarding images

Postby Skyrunner65 » Sun Jan 18, 2015 6:39 pm

1. I'm not sure how to help you.

2. This is made on the spot, and might not be perfect,but:
Code: Select all
volatile int player playerstand //change this to his standing pose's name
volatile int player.x 20 //this is the starting point
volatile int player.y 20 //starting point
volatile int player.flip NOFLIP //saying that he is facing left

void loop() {
  gb.display.drawBitmap(player,player.x,player.y,NOROT,player.flip); //draws player forever
  player == playerstand;
  if (gb.buttons.repeat(BTN_LEFT,1)) {  //If they press left
    player.flip == NOFLIP
    player.x -= 1; //go left
    if (player == playerstand) { //from || to /\
      player == playerwalk1;
    }
    if (player == playerwalk1) { //from /\ to |
      player == playerwalk2;
    }
    if (player == playerwalk2) { //from | to /\
      player == playerwalk1;
    }
  }
  if (gb.buttons.repeat(BTN_RIGHT,1)) {  //If they press right
      player.flip == FLIPH;
      player.x += 1; //go right
      if (player == playerstand) {
        player == playerwalk1;
      }
      if (player == playerwalk1) {
        player == playerwalk2;
      }
      if (player == playerwalk2) {
        player == playerwalk1;
      }
  }       
User avatar
Skyrunner65
 
Posts: 371
Joined: Thu Mar 20, 2014 5:37 pm
Location: NC,USA

Re: Two Questions regarding images

Postby Jamish » Sun Jan 18, 2015 7:04 pm

DrWhoCares wrote:Question 1:
I want to display an image as a background that will never change over the course of gameplay. This image is not made up of pure rectangles and instead has points where it may be crooked or jagged. How do I go about doing this? How do I store it?

Question 2:
I have a player sprite that I want to display, easy enough. However, while he's running I want it to loop through running animations, how can I create a sort of "sprite sheet" and run through multiple different states. (A state machine is easy, the actual bitmap changing is the problem.)

Info:
Been looking at some code, but none of it quite answers my questions or works the same way. That, or it's just too alien to me.
Thanks.


Question 1: I'm not totally sure what you're asking, but you
could try drawing a bunch of triangles every frame?

gb.display.drawTriangle(x0,y0,x1,y1,x2,y2)
gb.display.fillTriangle(x0,y0,x1,y1,x2,y2)

Question 2: I have animation in my game here. It's all in this file. You have to sort through all my extra player code... but when I have time later, I will come up with a more succinct answer. https://github.com/Jamish/GamebuinoGame ... Player.cpp
User avatar
Jamish
 
Posts: 73
Joined: Wed Dec 17, 2014 6:52 pm
Location: California

Re: Two Questions regarding images

Postby rodot » Sun Jan 18, 2015 7:09 pm

Question 1 :
Why don't you use a bitmap of the size of the screen ? You can make bitmaps using the bitmap encoder you will find in the Download page.

Question 2 :
As suggested in Jamish's example, you can make an array of bitmaps and iterate through it. If it's not clear please tell me, I'll write an example.
User avatar
rodot
Site Admin
 
Posts: 1290
Joined: Mon Nov 19, 2012 11:54 pm
Location: France

Re: Two Questions regarding images

Postby Jamish » Sun Jan 18, 2015 7:17 pm

Sorry for that short reply, I'm on my phone and out of the house for the day :P
User avatar
Jamish
 
Posts: 73
Joined: Wed Dec 17, 2014 6:52 pm
Location: California

Re: Two Questions regarding images

Postby DrWhoCares » Mon Jan 19, 2015 3:25 am

Thanks, as rodot suggested, I was looking to create a bitmap image and make that the entire screen. The encoder should be exactly what I need.

As for the second question, your example will should help, can't look at it currently but i'll give it a shot.

Thanks for your help.
DrWhoCares
 
Posts: 14
Joined: Fri Sep 05, 2014 6:48 pm

Re: Two Questions regarding images

Postby DrWhoCares » Mon Jan 19, 2015 5:13 am

I have another question. Jamish, you use the very useful format '0x' in your code to compress a:
Code: Select all
const byte PROGMEM playerSprite[] =
{
 8,8
 B00111100
 B00100100
 B00100100
 B01111100
 B11111100
 B11111100
 B11001100
 B11101110
}


into

Code: Select all
const byte PROGMEM playerSprite[] =
{
  {8,6, 0x78, 0x50, 0x78, 0xFC, 0x78, 0xCC,}
} //standing


Which I would absolutely prefer to use, however, I have never been taught this, at least not properly, and I would like to know how exactly you convert it to that.

EDIT:
Also a question, why do you do:

guy[][8] PROGMEM =

rather than something like:

guy[9][8] PROGMEM =

ALSO - You put a , after the very last {} contained within guy[] = {}
Is that correct, or should there be no comma, also seen here:

Code: Select all
const byte ani_list[][8] PROGMEM =
{
{1, 2, 3, 4, 5, 6, 7, 8}, <<<<<<<<<<<<-------- //running animation
};
DrWhoCares
 
Posts: 14
Joined: Fri Sep 05, 2014 6:48 pm

Re: Two Questions regarding images

Postby Jamish » Mon Jan 19, 2015 6:02 am

Actually, there should be no difference between the resulting program sizes of the 0x and B formats. They both compile to the same thing, so use whatever you want. I used Rodot's bitmap encoder tool in the downloads section. You can choose between hex (0x) and binary (B) form.

With "guy[][8]", the 8 is the length of the sub-arrays. That means that each array inside my guy[] array has a maximum of 8 items. The 8 is necessary and required by the compiler. As I have it written, it could be guy[9][8] but the 9 is NOT necessary to include. The compiler assumes it is 9 by how many rows I have specified, but you could always write guy[9][8] or even guy[12][8] (but that would be wasting blank space). Anyway, if I went and added another sprite to my sprite list, I'd have to update each time it to guy[10][8] and then guy[11][8] so on, so I just prefer to leave it off.

As far as the trailing comma, nope, it's not necessary. In fact, it's usually preferred to leave it off. It doesn't hurt anything and the compiler ignores it. I just left it there because the next time I add a row, I won't have to add a comma to the previous row. I'm just lazy :)
User avatar
Jamish
 
Posts: 73
Joined: Wed Dec 17, 2014 6:52 pm
Location: California

Re: Two Questions regarding images

Postby DrWhoCares » Mon Jan 19, 2015 6:09 am

What I meant by size, was size of code. Much less lines. I'll have to use the encoder.

I'm having significant trouble understanding your code, probably because of just how alien Arduino is to me. Currently, I'm throwing together a very crude, very terrible program that should hopefully show me my animations and accept left right button input.

I may need a tutorial for this multiple sprite animation thing. Would be wonderful to throw it into the Wiki too if you make one.

Here's that crappy code I'm making:

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

const byte player[][8] PROGMEM =
{
 {8,8,0x3C,0x24,0x24,0x7C,0xFC,0xFC,0xCC,0xEE}, //Standing
 {8,8,0x3C,0x24,0x24,0x7C,0xFC,0xFC,0x6C,0xCC}, //Running A
 {8,8,0x3C,0x24,0x24,0x7C,0xFC,0xFC,0xCC,0xD8}  //Running B
};

byte playerX = 0;
byte playerY = 0;
byte player_CurrentState = 0;
byte player_Facing = 1;

void setup()
{
  gb.begin(); //REQUIRED
  gb.titleScreen(F("Jumpin' Guy")); //SETUP TITLE SCREEN (F("text", logoVar))
  playerX = 10;
  playerY = 10;
 
}

void loop()
{
  if( gb.update() ) {
    byte flip = NOFLIP;
    if( player_Facing == -1 ) {
      flip = FLIPH;
    }
    gb.display.drawBitmap(playerX, playerY, player[player_CurrentState], NOROT, flip);
   
    //Movement Checks (CRUDE)
    if(gb.buttons.pressed(BTN_C)) {
      gb.titleScreen(F("Jumpin' Guy"));
    }
    if( gb.buttons.repeat(BTN_RIGHT, 1) ) {
      player_CurrentState = 1;
      playerX++;
      gb.display.drawBitmap(playerX, playerY, player[player_CurrentState], NOROT, flip);
      player_CurrentState = 2;
      gb.display.drawBitmap(playerX, playerY, player[player_CurrentState], NOROT, flip);
    } else if( gb.buttons.repeat(BTN_LEFT, 1) ) {
      player_Facing = -1;
      flip = FLIPH;
      player_CurrentState = 1;
      playerX--;
      gb.display.drawBitmap(playerX, playerY, player[player_CurrentState], NOROT, flip);
      player_CurrentState = 2;
      gb.display.drawBitmap(playerX, playerY, player[player_CurrentState], NOROT, flip);
    } else {
      player_CurrentState = 0;
      player_Facing = 1;
      gb.display.drawBitmap(playerX, playerY, player[player_CurrentState], NOROT, flip);
    }
  }
}


EDIT:
I updated my terrible code with slightly less terrible code. It wont compile due to "Too many initializers for 'const byte [8]' I get that error three times at line 10.
DrWhoCares
 
Posts: 14
Joined: Fri Sep 05, 2014 6:48 pm

Re: Two Questions regarding images

Postby Myndale » Mon Jan 19, 2015 6:40 am

You're very close. Look closer at this line:

Code: Select all
const byte player[][8] PROGMEM =


The first bracket pair (i.e. "[]") tells the compiler you'll be declaring an as-yet unspecified number of rows which we know to be 3 but the compiler will figure out later. The next pair ("[8]") says that each row will contain 8 bytes. But look at your rows:

Code: Select all
{8,8,0x3C,0x24,0x24,0x7C,0xFC,0xFC,0xCC,0xEE}, //Standing


That's 10 bytes...8 for the pixel data plus the extra 2 at the front that say how large the bitmap is. To fix your compiler error simply give it the correct number of bytes per row:

Code: Select all
const byte player[][10] PROGMEM =
Myndale
 
Posts: 507
Joined: Sat Mar 01, 2014 1:25 am

Next

Return to Programming Questions

Who is online

Users browsing this forum: No registered users and 15 guests