z88dk y sp1

Si tienes alguna duda y crees que alguien podría resolverla, ponla aquí. || If you have any doubt about programming, post it here.

Moderador: CEZ

Responder
Avatar de Usuario
Emlyn_Hughes
8 bits
8 bits
Mensajes: 10
Registrado: 25 Ene 2009 21:08

z88dk y sp1

Mensaje por Emlyn_Hughes »

Buenas, he seguido un tutorial que hay en speccy.org del z88dk y sp1. Todo va bien, pero cuando empiezo modificarlo, se cuelga.

Al final viene el código, si descomento el último printf, peta, aunque evidentemente no es por esa instrucción. Estaría bien si alguno puede verificarlo con este mismo código, pero lo que de verdad me salvaría la vida es saber porqué pasa esto.

Además, teniendo en cuenta que es mi primer programa con z88dk, no estaría mal que alguno de los que hay por aquí que seguro que ya se ha peleado con las cosas del z88dk y del sp1, me dé algunos consejillos de las cosas menos obvias que conviene saber para hacer un programa con estas librerias.

Y mi última petición: si alguien me puede pasar el ejemplo más simple que funcione de poner un sprite mayor de 8x8 en pantalla con z88dk+sp1, le estaría muy agradecido

El código:

Código: Seleccionar todo

#include <sprites/sp1.h>
#include <malloc.h>
#include <spectrum.h>
#include <input.h>
#include <stdio.h>
 
#pragma output STACKPTR = 53248

long heap;

struct personaje {
   struct sp1_ss  *sprite;
   char           dx;
   char           dy;
   struct in_UDK keys;
   void *getjoy;
   uchar *frameoffset;
};
 
extern uchar enemigo_col0[];
short int posiciones[] = {5,4,20,25,20,3,1,5,12,12,18,18};
short int desplazamientos[] = {1,1,-1,1,1,-1,-1,-1,-1,-1,1,1};
extern uchar prota_col0[];

void *u_malloc(uint size)
{
   return malloc(size);
}
 
void u_free(void *addr)
{
   free(addr);
}
 
uchar fondo[] = {0x80,0x00,0x04,0x00,0x40,0x00,0x02,0x00}; 
 
struct sp1_Rect cr = {0, 0, 32, 24};  
 
main()
{
   struct personaje p;
   uchar input;
   struct personaje enemigos[6];
   int i;
   int xp, yp;
   int xe, ye;
   short int final;
   #asm
   di
   #endasm
 
   heap = 0L;
   sbrk(40000, 10000);
   zx_border(BLUE);


   sp1_Initialize(SP1_IFLAG_MAKE_ROTTBL | SP1_IFLAG_OVERWRITE_TILES | 
                  SP1_IFLAG_OVERWRITE_DFILE, INK_BLUE | PAPER_CYAN, ' '); 
   sp1_TileEntry(' ',fondo); 
   p.sprite = sp1_CreateSpr(SP1_DRAW_MASK2LB, SP1_TYPE_2BYTE, 2, 16, 0);
   sp1_AddColSpr(p.sprite, SP1_DRAW_MASK2RB, 0, 16, 0);
   sp1_MoveSprAbs(p.sprite, &cr, prota_col0, 12, 16, 0, 0);
   p.dx = 0;
   p.dy = 0;
   sp1_Invalidate(&cr);
   sp1_UpdateNow();
 

   p.getjoy = in_JoyKeyboard;	
   p.keys.left = in_LookupKey('o');
   p.keys.right = in_LookupKey('p');
   p.keys.up = in_LookupKey('q');
   p.keys.down = in_LookupKey('a');
   p.keys.fire = in_LookupKey('m');
   p.frameoffset = (uchar *) 0;

   for (i=0;i<6;i++)
   {
      enemigos[i].sprite = sp1_CreateSpr(SP1_DRAW_MASK2LB, SP1_TYPE_2BYTE, 2, 16, 0);
      sp1_AddColSpr(enemigos[i].sprite, SP1_DRAW_MASK2RB, 0, 16, 0);
      sp1_MoveSprAbs(enemigos[i].sprite, &cr, enemigo_col0, posiciones[2*i], posiciones[2*i+1], 0, 0);
      enemigos[i].dx = desplazamientos[2*i+1];
      enemigos[i].dy = desplazamientos[2*i];
   }

  final = 0;
  while (!final)
  {
      sp1_UpdateNow();
 
      input = (p.getjoy)(&p.keys);
      if (input & in_RIGHT && !(p.sprite->col > 30 && p.sprite->hrot > 0))
          p.dx = 1;
      if (input & in_LEFT && !(p.sprite->col < 1 && p.sprite->hrot < 1))
          p.dx = -1;
      if (input & in_DOWN && !(p.sprite->row > 22))
          p.dy = 1;
      if (input & in_UP && !(p.sprite->row < 1 && p.sprite->vrot < 129)) 
        p.dy = -1;
 
      sp1_MoveSprRel(p.sprite, &cr, p.frameoffset, 0, 0, p.dy, p.dx);
      p.dx = 0;
      p.dy = 0;
      xp = p.sprite->col*8 + p.sprite->hrot + 4;
      yp = p.sprite->row*8 + p.sprite->vrot + 4;

	for (i=0;i<6;i++)
	{
	    if (enemigos[i].sprite->col > 30 && enemigos[i].sprite->hrot > 0)
	        enemigos[i].dx = -enemigos[i].dx;
	    if (enemigos[i].sprite->row > 22)
	        enemigos[i].dy = -enemigos[i].dy;
	 
	    sp1_MoveSprRel(enemigos[i].sprite, &cr, enemigos[i].frameoffset,0, 0, enemigos[i].dy, enemigos[i].dx);
	    xe = enemigos[i].sprite->col*8 + enemigos[i].sprite->hrot;
            ye = enemigos[i].sprite->row*8 + enemigos[i].sprite->vrot;
            if (xe + 8  >= xp && xe <= xp && ye + 8 >= yp && ye <= yp)
	    {
	      zx_border(RED);
	      final = 1;
	    }
	}
  }

printf("\nGame Over");
// printf("\nInsert Coin");

}

#asm
._prota_col0
   DEFB 255, 0, 255, 0, 255, 0, 255, 0
   DEFB 255, 0, 255, 0, 255, 0 ,255, 0
   DEFB 199,56,131,124,199,56,239,16
   DEFB 131,124,109,146,215,40,215,40
   ; Siempre tiene que haber un caracter vacio debajo
   DEFB 255,  0,255,  0,255,  0,255,  0
   DEFB 255,  0,255,  0,255,  0,255,  0
._enemigo_col0
    DEFB 255, 0, 255, 0, 255, 0, 255, 0
    DEFB 255, 0, 255, 0, 255, 0, 255, 0
    DEFB 231,24,195,36,129,90,0,153
    DEFB 129,90,195,36,231,24,255,0
    DEFB 255,0,255,0,255,0,255,0
    DEFB 255,0,255,0,255,0,255,0
#endasm

compilado con:
zcc +zx -vn name.c -o name.bin -lndos -lsp1 -lmalloc
Avatar de Usuario
saboteur2
32 bits
32 bits
Mensajes: 574
Registrado: 20 Dic 2004 16:37
Ubicación: Pinto (Madrid)

Re: z88dk y sp1

Mensaje por saboteur2 »

¿Qué versión estás utilizando? Porque yo tengo la 1.8 y los nombres de estructuras y funciones son diferentes.

La he pillado de http://www.geocities.com/aralbrec/sprit ... -intro.htm, y ya no sé si es la página oficial o no.

EDITO: bueno, en realidad no sé qué versión tengo. Lo mismo, la mia es la antigua. :lol:
Spectrum +4 600Ghz 8GB RAM HD 120PB Gráfica 4D Sonido 12.3THX
Mis videojuegos: Videojuegos de Rafa Vico
Avatar de Usuario
Emlyn_Hughes
8 bits
8 bits
Mensajes: 10
Registrado: 25 Ene 2009 21:08

Re: z88dk y sp1

Mensaje por Emlyn_Hughes »

Por si sirve de algo, estoy usando Sprite Pack v3.0 (la que trae de serie el z88dk)
Alcoholics Anonymous
8 bits
8 bits
Mensajes: 14
Registrado: 21 Mar 2006 08:07
Ubicación: Canada

Re: z88dk y sp1

Mensaje por Alcoholics Anonymous »

Hola,

Sorry for the English again! But I did know enough Spanish to say "Hola" :-)

The latest version of the library is sp1 that is integrated into z88dk. splib2 is an earlier version and the tutorial link posted above is for splib2, though many things remain almost the same.

For sp1 there is a series of tutorial examples included with z88dk that can be viewed online:

http://z88dk.cvs.sourceforge.net/viewvc ... /examples/

Unfortunately they only really scratch the surface but there's enough there that with a little imagination you could probably figure most of everything out.

As to the question: why does the program crash with an extra printf() in there? I believe your program is getting large enough to overlap with the malloc memory space.

In main(), you have this statement:

sbrk(40000, 10000);

which adds memory from 40000-49999 for allocation through malloc(). Your program is compiled at 32768 by default so once it becomes 40000-32768 = 7232 bytes in size, it overlaps with the malloc area. So, time to change where that sbrk() call gets its memory from. You should keep in mind what area(s) of memory your program code occupies, what area(s) are reserved for malloc and what areas are reserved by the sp1 engine, especially since you've only got 48k ram to deal with.

The default sp1 memory map (for the spectrum) is documented here:

http://z88dk.cvs.sourceforge.net/viewvc ... iew=markup

You can rearrange things if you wish by editing this file and recompiling the library.

Be careful with what library code you make use of as well. The implementation of stdio in z88dk 1.8 is not 100% machine code and is quite large -- a single printf() call can result in dragging in more than 3-4k of library code.
Avatar de Usuario
Emlyn_Hughes
8 bits
8 bits
Mensajes: 10
Registrado: 25 Ene 2009 21:08

Re: z88dk y sp1

Mensaje por Emlyn_Hughes »

Hi, thanks for the reply. I've already seen those samples, a lot of useful stuff, although I can't make the last one (ex6b) to work with my configuration (all default).

As I've said before in this thread, I'm working with sp1, so I don't expect to have the library problem you describe.

And about the printf, I reached an absurdly simple solution, that consists on not using printf but puts_cons or sp1_PrintString. I'm not used to work with the spectrum memory model, I hoped to use some default setting that allowed me to code without messing with memory concerns too much. I'll check where and how much memory is used if I can understand well all those concepts.

I'm drawing some graphics now, so I have no more problems so far, but I'm sure they will come back soon, and maybe I'll ask for help here and in w.o.s./development forums.

Thanks and stay tuned
Responder