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

0 Members and 1 Guest are viewing this topic.

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 #15 on: January 29, 2012, 02:24:19 pm »
Don't write to LCD_MEM in grayscale mode :)
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 #16 on: January 29, 2012, 02:25:22 pm »
It's ranman's code: http://ourl.ca/9035/170957 :p
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 #17 on: January 29, 2012, 02:28:06 pm »
Ranman's code was meant for B/W, but grayscale is another story ;)

See http://debrouxl.github.com/gcc4ti/gray.html#GrayGetPlane :
Quote
GrayGetPlane returns a pointer to the grayscale plane plane. Valid values for plane are LIGHT_PLANE and DARK_PLANE. To draw in black, draw in both planes.

Note: Do not assume that any plane is on 0x4C00 when in grayscale mode, due to hardware version 2 support. Also do not assume that the 2 grayscale planes are consecutive, this is not the case on hardware version 1.

Some software grayscale implementations have two consecutive grayscale planes, but the one in TIGCC and GCC4TI doesn't.
« Last Edit: January 29, 2012, 02:30:06 pm by Lionel Debroux »
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 #18 on: January 29, 2012, 02:58:42 pm »
Thanks it's works!

But now i have a problem when i stop the program. The light plane is displayed in black and there is white rectangles which appear and disapear on the screen, i can't do anything. (Fortunatly i am on tiEmu)...
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 #19 on: January 29, 2012, 03:02:02 pm »
When exiting by ESC, you immediately "return 0;" instead of falling out of the outer while(), which calls GrayOff() ;)

If if still doesn't work properly after that, make sure you aren't drawing past the beginning or the end of the screen, and try using ClipSprite8 instead of Sprite8 :)

And if your map is only read from, but not written to, you really should declare it "static const", so that it ends up as a constant in your program's code, instead of being constructed onto the stack: it will be both smaller and faster.
« Last Edit: January 29, 2012, 03:15:47 pm by Lionel Debroux »
Member of the TI-Chess Team.
Co-maintainer of GCC4TI (GCC4TI online documentation), TILP and TIEmu.
Co-admin of TI-Planet.

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 #20 on: January 29, 2012, 03:08:45 pm »
sorry to interrupt again .___.
I tried modifying the code to suite my purposes and I keep getting these errors and I don't know why D:

'MAP_Y' undeclared (first use in this function).
'MAP_X' undeclared (first use in this function).
Unused variable 'map1'.


EDIT: also, KERNELBASE.dll seems to have a lot of issues with me, so ever time I get an error, it freezes and uses up tons of system resources and I have to press F10 a bazillion times and click okay on a bunch of popup error messages

Offline Torio

  • LV3 Member (Next: 100)
  • ***
  • Posts: 83
  • Rating: +22/-0
    • View Profile
Re: Sprites and Maps in C 68k
« Reply #21 on: January 29, 2012, 03:16:32 pm »
You have an error because MAP_X and MAP_Y are not defined in the code you copied.
You have to put before the code :

Code: [Select]
#define MAP_X 20
#define MAP_Y 12

I guess it should be 20 and 12 according to the table map1, but I'm not sure.
Sorry about my mistakes, I'm French.

Projects :  Pokemon TI-89 | Ti-Tank

Offline Kiligolo

  • LV5 Advanced (Next: 300)
  • *****
  • Posts: 218
  • Rating: +10/-1
    • View Profile
Re: Sprites and Maps in C 68k
« Reply #22 on: January 29, 2012, 03:27:32 pm »
Lionel > Yes
Xeda >
Code: [Select]
#include <tigcclib.h>

#define MAP_X 20
#define MAP_Y 12
It's the map size.
For map1 error:
Code: [Select]
unsigned char game_sprites[2][8] = //array of sprites
{ //Grass
   0b00000000,
   0b00101000,
   0b00010000,
   0b00000000,
   0b00000101,
   0b00000010,
   0b01010000,
   0b00100000,
},
{ //tree
   0b00000000,
   0b00000000,
   0b00111110,
   0b01111111,
   0b01111111,
   0b01111111,
   0b00111110,
   0b00001000,
}

unsigned char map[8][8] =
{
   {1,1,1,1,1,1,1,1},
   {1,0,0,0,0,0,0,1},
   {1,0,0,0,0,0,0,1},
   {1,0,0,0,0,0,0,1},
   {1,0,0,0,0,0,0,1},
   {1,0,0,0,0,0,0,1},
   {1,0,0,0,0,0,0,1},
   {1,1,1,1,1,1,1,1},
};
//0 = Grass and 1 = Tree
//Drawing:
int tilemap_x, tilemap_y;
int lcd_x, 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); // --> TIGCC lib

    // 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;
}
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 #23 on: January 29, 2012, 03:33:35 pm »
Thanks lionel im very tired today i go to sleep! :p
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 #24 on: January 29, 2012, 04:00:29 pm »
Thank you much! I wasn't sure how to define MAP_X and MAP_Y, but now I know! Thanks!

Offline Kiligolo

  • LV5 Advanced (Next: 300)
  • *****
  • Posts: 218
  • Rating: +10/-1
    • View Profile
Re: Sprites and Maps in C 68k
« Reply #25 on: January 30, 2012, 12:55:41 pm »
Hi! I have a big problem with dynamic allocation...
Here is the code:
Code: [Select]
int i, j;
unsigned char ** map = malloc(MAP_Y * sizeof(unsigned char));
for (i = 0; i < MAP_Y; i++)
{
map[i] = malloc(MAP_X * sizeof(unsigned char));
}
and at the end :
Code: [Select]
for (i = 0; i < MAP_Y ; i++)
{
free(map[i]);
}
free(map);
When i start the program, the Ti89 display BUSY icon and freeze but when i add ngetchx(); in this part of code:
Code: [Select]
int i, j;
unsigned char ** map = malloc(MAP_Y * sizeof(unsigned char));
for (i = 0; i < MAP_Y; i++)
{
                printf("%d",i);
                ngetchx(); //Here
map[i] = malloc(MAP_X * sizeof(unsigned char));
}
The 89 don't freeze and the program start but when i stop the program and i go to Var-Link menu, my asm program is now and expression (64000bytes).
I'm lost
« Last Edit: January 30, 2012, 12:55:51 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 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 #26 on: January 30, 2012, 01:15:59 pm »
I'm not sure why you need dynamic allocation here ?
(and by they way, two-dimensional arrays suck for efficiency)

The symptoms (BUSY on exit, program size changed, program behaviour changing by adding unrelated lines) point out to severe memory corruption, by overflowing buffers on the heap. Check your code :)
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 #27 on: January 30, 2012, 01:37:10 pm »
Ok i'm going to try without it.
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 Torio

  • LV3 Member (Next: 100)
  • ***
  • Posts: 83
  • Rating: +22/-0
    • View Profile
Re: Sprites and Maps in C 68k
« Reply #28 on: January 30, 2012, 03:08:27 pm »
I'm not sure but I think you have a memory corruption because when you call malloc for the first time, you will allocate unsigned char for *map.
But in the for loop, the array will contain not an unsigned char, but a pointer so it is a long int.
So the adress to which *map points to will be wrong.
That will cause a corruption when freeing the array.

What you should do is :
Code: [Select]
unsigned char *map = malloc(MAP_X * MAP_Y * sizeof(unsigned char));
Sorry about my mistakes, I'm French.

Projects :  Pokemon TI-89 | Ti-Tank

Offline Kiligolo

  • LV5 Advanced (Next: 300)
  • *****
  • Posts: 218
  • Rating: +10/-1
    • View Profile
Re: Sprites and Maps in C 68k
« Reply #29 on: January 31, 2012, 02:09:29 pm »
Thanks for your help! My program is okay now :p
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