Author Topic: Sprites and Maps in C 68k  (Read 19806 times)

0 Members and 1 Guest are viewing this topic.

Offline Kiligolo

  • LV5 Advanced (Next: 300)
  • *****
  • Posts: 218
  • Rating: +10/-1
    • View Profile
Sprites and Maps in C 68k
« on: February 02, 2011, 08:11:32 am »
Hello,

I would like to program in C on my TI-89 Titanium and make one (tile) map. I looked at the code a bit of Zelda, but it was terrible...
Does someone could tell me specifically how to make a map and sprites in C?
I already downloaded TIGCC.

Thank you!
Spoiler For Calcul Mental:
Version 1.3 :100%!!
Here is a program that reduces your dependence on the calculator! Click here!
Spoiler For Some screen shots:
       
The screenshots are in french but there is an english version

Offline Ranman

  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1354
  • Rating: +83/-0
    • View Profile
Re: Sprites and Maps in C 68k
« Reply #1 on: February 02, 2011, 10:39:52 am »
What is your experience level with the C language?
Ranman
Bringing Randy Glover's Jumpman to the TI-89 calculator. Download available at Ticalc.

Offline Kiligolo

  • LV5 Advanced (Next: 300)
  • *****
  • Posts: 218
  • Rating: +10/-1
    • View Profile
Re: Sprites and Maps in C 68k
« Reply #2 on: February 03, 2011, 03:31:28 am »
I know how to make a sprite now, but not maps.

unsigned char homme[] = {
          0b01111110,
         0b11111111,
         0b10100101,
         0b10000001,
         0b01111110,
         0b10100101,
         0b10111101,
         0b01111110
         };
« Last Edit: February 03, 2011, 03:31:59 am by Kiligolo »
Spoiler For Calcul Mental:
Version 1.3 :100%!!
Here is a program that reduces your dependence on the calculator! Click here!
Spoiler For Some screen shots:
       
The screenshots are in french but there is an english version

Offline Ranman

  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1354
  • Rating: +83/-0
    • View Profile
Re: Sprites and Maps in C 68k
« Reply #3 on: February 03, 2011, 04:46:40 am »
I know how to make a sprite now, but not maps.

unsigned char homme[] = {
          0b01111110,
         0b11111111,
         0b10100101,
         0b10000001,
         0b01111110,
         0b10100101,
         0b10111101,
         0b01111110
         };


First off... Here are some great resources for programming in C with TIGCC:

* TIGCC Documentation
* Technoplaza Tutorials
* The C Book


A tilemap is essentially an array of sprites. So, if this is one sprite:

unsigned char homme[8] =
{
    0b01111110,
    0b11111111,
    0b10100101,
    0b10000001,
    0b01111110,
    0b10100101,
    0b10111101,
    0b01111110
};


Then an array of sprites (tilemap) would look like this:

unsigned char homme[4][8] =
{
    {
        0b01111110,
        0b11111111,
        0b10100101,
        0b10000001,
        0b01111110,
        0b10100101,
        0b10111101,
        0b01111110
    },
    {
        0b01111110,
        0b11111111,
        0b10100101,
        0b10000001,
        0b01111110,
        0b10100101,
        0b10111101,
        0b01111110
    },
    {
        0b01111110,
        0b11111111,
        0b10100101,
        0b10000001,
        0b01111110,
        0b10100101,
        0b10111101,
        0b01111110
    },
    {
        0b01111110,
        0b11111111,
        0b10100101,
        0b10000001,
        0b01111110,
        0b10100101,
        0b10111101,
        0b01111110
    },
};


Keep in mind... in your example as well as my example, the sprite is just a simple black & white sprite without a corresponding mask. If you intend on using grayscale along with masks, then to completely define each sprite you will need a sprite for each video plane as well as a sprite mask (total of 3 sprites).

Do you understand the purpose of a sprite mask? How about the idea of multiple planes for grayscale? If not... the  TIGCC Documentation is fairly helpful.

I'll be around if you have any more questions.
Ranman
Bringing Randy Glover's Jumpman to the TI-89 calculator. Download available at Ticalc.

Offline Kiligolo

  • LV5 Advanced (Next: 300)
  • *****
  • Posts: 218
  • Rating: +10/-1
    • View Profile
Re: Sprites and Maps in C 68k
« Reply #4 on: February 03, 2011, 04:52:53 am »
What is the routine to call a tile map?
Spoiler For Calcul Mental:
Version 1.3 :100%!!
Here is a program that reduces your dependence on the calculator! Click here!
Spoiler For Some screen shots:
       
The screenshots are in french but there is an english version

Offline Ranman

  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1354
  • Rating: +83/-0
    • View Profile
Re: Sprites and Maps in C 68k
« Reply #5 on: February 04, 2011, 02:23:50 am »
What is the routine to call a tile map?
I may have been a little misleading with my previous post...

A traditional tilemap is essentially a 2 dimensional array of sprites that represents a visual 2D map for use in some types of games.

Example of an 8x8 tilemap:

unsigned char game_tilemap[8][8] =
{
  {0,0,1,1,1,1,0,0},
  {0,1,1,1,0,0,0,0},
  {1,1,1,0,0,0,0,3},
  {1,0,0,3,0,4,0,3},
  {0,0,0,0,0,0,0,0},
  {3,0,3,0,0,2,2,2},
  {3,0,0,0,2,2,2,2},
};


legend: 0 = grass, 1 = mountain, 2 = water, 3 = tree, 4 = house

Notice that each item in the game_tilemap uniquely represents the index of the sprite that will be drawn.


Next we will need to define a sprite for each item used in the tilemap. From the above example, there will be 5 sprites and each will visually represent the items defined in legend (0 = grass, 1 = mountain, 2 = water, 3 = tree, 4 = house).

Now it is time to actually define the array of sprites utilized by the tilemap (note: 5 sprites, each sprite 8 pixels tall):

unsigned char game_sprites[5][8] =   
{
  { // 0 = grass
    0b10001000,
    0b00000000,
    0b00100010,
    0b00000000,
    0b10001000,
    0b00000000,
    0b00100010,
    0b00000000,
  },
  { // 1 = mountain
    0b00000000,
    0b00010000,
    0b00111000,
    0b01111100,
    0b11011110,
    0b10101111,
    0b01110111,
    0b11111011,
  },
  { // 2 = water
    0b11110000,
    0b00001111,
    0b11110000,
    0b00001111,
    0b11110000,
    0b00001111,
    0b11110000,
    0b00001111,
  },
  { // 3 = tree
    0b00000000,
    0b00111110,
    0b01111111,
    0b01111111,
    0b01111111,
    0b00111110,
    0b00001000,
    0b00001000,
  },
  { // 4 = building
    0b00000000,
    0b00011000,
    0b00111100,
    0b01111110,
    0b11111111,
    0b01100110,
    0b01100110,
    0b01100110,
  },
};


A pointer (or address) to the beginning of an 8bit wide sprite can be defined like this:

unsigned char* pSprite;


Example: if we need to get the address of the tree sprite, then we can do this:   

    pSprite = &game_sprites[3][0];
      or
    pSprite = game_sprites[3];


Next we need to draw the tilemap to the screen


int tilemap_x;
int tilemap_y;
int lcd_x;
int lcd_y;
unsigned char* pSprite;
unsigned char sprite_index;

// initialize the lcd Y value since we starting the first row
lcd_y = 0;

for (tilemap_y = 0; tilemap_y < 8; tilemap_y++)
{
  // initialize the lcd X value since we are starting a new collumn
  lcd_x = 0;

  for (tilemap_x = 0; tilemap_x < 8; tilemap_x++)
  {
    // first, get the sprite index from the tilemap
    sprite_index = game_tilemap[tilemap_y][tilemap_x];

    // second, get the pointer (address) to the sprite
    pSprite = &game_sprites[sprite_index][0];

    // draw the sprite to the screen
    Sprite8 (lcd_x, lcd_y, 8, pSprite, LCD_MEM, SPRT_RPLC); // refer to TIGCC docs for Sprite8 details

    // increment the lcd X position in preparation for next sprite
    lcd_x = lcd_x + 8;
  }

  // increment the lcd Y position in preparation for next row of sprites
  lcd_y = lcd_y + 8;
}


Obviously, this is not the most optimized method for drawing a tilemap to the screen; but I think it illustrates each of the main ideas well.


Does this help any?
« Last Edit: February 04, 2011, 03:08:17 am by Ranman »
Ranman
Bringing Randy Glover's Jumpman to the TI-89 calculator. Download available at Ticalc.

Offline Lionel Debroux

  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2135
  • Rating: +290/-45
    • View Profile
    • TI-Chess Team
Re: Sprites and Maps in C 68k
« Reply #6 on: February 05, 2011, 05:59:42 am »
I think that this does help, because you're explaining the principles behind the Genlib and ExtGraph tilemap engines that I mentioned to Kiligolo on TI-Bank :)
Member of the TI-Chess Team.
Co-maintainer of GCC4TI (GCC4TI online documentation), TILP and TIEmu.
Co-admin of TI-Planet.

Offline Kiligolo

  • LV5 Advanced (Next: 300)
  • *****
  • Posts: 218
  • Rating: +10/-1
    • View Profile
Re: Sprites and Maps in C 68k
« Reply #7 on: February 05, 2011, 08:06:05 am »
Thanks.
Spoiler For Calcul Mental:
Version 1.3 :100%!!
Here is a program that reduces your dependence on the calculator! Click here!
Spoiler For Some screen shots:
       
The screenshots are in french but there is an english version

Offline Kiligolo

  • LV5 Advanced (Next: 300)
  • *****
  • Posts: 218
  • Rating: +10/-1
    • View Profile
Re: Sprites and Maps in C 68k
« Reply #8 on: March 25, 2011, 08:01:47 am »
up! ;D
I would like to know, with Sprite16() routine, how do the equivalent of Pt-Off() in Axe. I want all the pixels in the area where the sprite will be turn off until another sprite is displayed.
Is it possible?
« Last Edit: March 25, 2011, 08:02:08 am by Kiligolo »
Spoiler For Calcul Mental:
Version 1.3 :100%!!
Here is a program that reduces your dependence on the calculator! Click here!
Spoiler For Some screen shots:
       
The screenshots are in french but there is an english version

Offline Lionel Debroux

  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2135
  • Rating: +290/-45
    • View Profile
    • TI-Chess Team
Re: Sprites and Maps in C 68k
« Reply #9 on: March 25, 2011, 08:08:24 am »
I replied to you on TI-Bank ;)
Member of the TI-Chess Team.
Co-maintainer of GCC4TI (GCC4TI online documentation), TILP and TIEmu.
Co-admin of TI-Planet.

Offline Kiligolo

  • LV5 Advanced (Next: 300)
  • *****
  • Posts: 218
  • Rating: +10/-1
    • View Profile
Re: Sprites and Maps in C 68k
« Reply #10 on: April 13, 2011, 04:07:48 am »
I know it is off topic but how can I do a pause with a determinate time? (Bad english... ^^')
Spoiler For Calcul Mental:
Version 1.3 :100%!!
Here is a program that reduces your dependence on the calculator! Click here!
Spoiler For Some screen shots:
       
The screenshots are in french but there is an english version

Offline Lionel Debroux

  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2135
  • Rating: +290/-45
    • View Profile
    • TI-Chess Team
Re: Sprites and Maps in C 68k
« Reply #11 on: April 13, 2011, 04:12:32 am »
You can:
* use the AMS timers (OSRegisterTimer & friends), based on AUTO_INT_5 whose rate is normally ~19.32 Hz (on HW2 calculators);
* use the programmable rate generator (AUTO_INT_5) directly, and set its rate mostly as you wish through the PRG_* functions;
* use WaitForMillis-type functions, busy wait loops calibrated for waiting approximately the number of seconds passed to the function. Between others, TI-Chess uses such a function for debouncing keypresses.
Member of the TI-Chess Team.
Co-maintainer of GCC4TI (GCC4TI online documentation), TILP and TIEmu.
Co-admin of TI-Planet.

Offline Kiligolo

  • LV5 Advanced (Next: 300)
  • *****
  • Posts: 218
  • Rating: +10/-1
    • View Profile
Re: Sprites and Maps in C 68k
« Reply #12 on: April 13, 2011, 04:17:38 am »
Thanks.
Spoiler For Calcul Mental:
Version 1.3 :100%!!
Here is a program that reduces your dependence on the calculator! Click here!
Spoiler For Some screen shots:
       
The screenshots are in french but there is an english version

Offline Kiligolo

  • LV5 Advanced (Next: 300)
  • *****
  • Posts: 218
  • Rating: +10/-1
    • View Profile
Re: Sprites and Maps in C 68k
« Reply #13 on: January 29, 2012, 02:13:37 pm »
Hi there!

I have a problem with grayscales and ams planes. I made a tilemap but when i start the programm there is nothing displayed but the TI isn't frozen i can press ESC key to see a big bug...
Code: [Select]
int tilemap_x;
int tilemap_y;
int lcd_x;
int lcd_y;
unsigned char* pSprite;
unsigned char sprite_index;
int key = 0, pUserx = 1, pUsery = 1;

unsigned char map1[MAP_Y][MAP_X] =
{
{01,01,01,01,01,01,01,01,01,01,01,01,01,01,01,01,01,01,01,01},
{01,02,02,02,02,02,02,02,00,00,00,00,00,00,00,00,00,00,00,01},
{01,02,02,02,02,02,02,00,00,00,00,05,06,07,00,00,00,00,00,01},
{01,02,02,02,00,00,00,00,00,00,00,10,11,12,00,00,00,00,00,01},
{01,02,00,00,00,00,15,16,00,00,00,13,14,13,00,00,00,00,00,01},
{01,00,00,00,00,00,17,20,00,00,00,21,21,21,03,04,04,03,00,01},
{00,00,00,00,00,00,00,00,00,00,00,00,21,00,03,04,04,03,00,01},
{21,21,21,05,06,07,00,00,00,00,21,21,21,00,03,03,03,03,02,01},
{00,00,21,10,11,12,00,00,21,21,21,00,00,00,00,00,02,02,02,01},
{01,00,21,13,14,13,00,21,21,00,00,00,00,00,02,02,02,02,02,01},
{01,00,21,21,21,21,21,21,00,00,00,00,02,02,02,02,02,02,02,01},
{01,01,01,01,01,01,01,01,01,01,01,01,01,01,01,01,01,01,01,01},
};

ClrScr();
GrayOn();

while (key != KEY_ESC)
{
while (map1[pUsery][pUserx] != 14)
{
//Affichage

    //Gris clair
    GraySetAMSPlane(LIGHT_PLANE);
lcd_y = 0;
for (tilemap_y = 0; tilemap_y < MAP_Y; tilemap_y++)
{
  lcd_x = 0;
  for (tilemap_x = 0; tilemap_x < MAP_X; tilemap_x++)
  {
    sprite_index = map1[tilemap_y][tilemap_x];
    pSprite = &game_sprites_light[sprite_index][0];
Sprite8 (lcd_x, lcd_y, 8, vide, LCD_MEM, SPRT_AND);
    Sprite8 (lcd_x, lcd_y, 8, pSprite, LCD_MEM, SPRT_OR);
    lcd_x += 8;
  }
  lcd_y += 8;
}

    //Gris foncé
    GraySetAMSPlane(DARK_PLANE);
lcd_y = 0;
for (tilemap_y = 0; tilemap_y < MAP_Y; tilemap_y++)
{
  lcd_x = 0;
  for (tilemap_x = 0; tilemap_x < MAP_X; tilemap_x++)
  {
    sprite_index = map1[tilemap_y][tilemap_x];
    pSprite = &game_sprites_dark[sprite_index][0];
    Sprite8 (lcd_x, lcd_y, 8, pSprite, LCD_MEM, SPRT_OR);
    lcd_x += 8;
  }
  lcd_y += 8;
}
Sprite8 (pUserx * 8, pUsery * 8, 8, vide, LCD_MEM, SPRT_AND);
Sprite8 (pUserx * 8, pUsery * 8, 8, mec, LCD_MEM, SPRT_OR);
    GraySetAMSPlane(LIGHT_PLANE);
Sprite8 (pUserx * 8, pUsery * 8, 8, mec, LCD_MEM, SPRT_OR);

//Demande de touche
key = ngetchx();
if (key)
{
//Actualisation de la position de l'utilisateur
pUserx += (key == KEY_RIGHT) - (key == KEY_LEFT);
pUsery += (key == KEY_DOWN) - (key == KEY_UP);

//Verification de sa position
if (map1[pUsery][pUserx] == 1 || map1[pUsery][pUserx] == 3 || (map1[pUsery][pUserx] > 4 && map1[pUsery][pUserx] != 14 && map1[pUsery][pUserx] < 21) || pUserx == 21 || pUserx == -1 || pUsery == -1 || pUsery == 13)
{
pUserx -= (key == KEY_RIGHT) - (key == KEY_LEFT);
pUsery -= (key == KEY_DOWN) - (key == KEY_UP);
}
if (key == KEY_ESC)
return 0;
}
}
}
GrayOff();
return 0;
}

Thanks.
« Last Edit: January 29, 2012, 02:14:46 pm by Kiligolo »
Spoiler For Calcul Mental:
Version 1.3 :100%!!
Here is a program that reduces your dependence on the calculator! Click here!
Spoiler For Some screen shots:
       
The screenshots are in french but there is an english version

Offline Xeda112358

  • they/them
  • Moderator
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 4704
  • Rating: +719/-6
  • Calc-u-lator, do doo doo do do do.
    • View Profile
Re: Sprites and Maps in C 68k
« Reply #14 on: January 29, 2012, 02:20:43 pm »
Sorry, I do not know an answer to this, but I am also learning C for the 68K calcs, so I am here just to say thank you ^-^ The code is very helpful for me to study :)