CPCWiki forum

General Category => Programming => Topic started by: kamelie1706 on 20:35, 06 June 20

Title: Sprite moving and leaving crap behind
Post by: kamelie1706 on 20:35, 06 June 20
hi,


Do you know why my sprite does that?
https://youtu.be/3WQK9qrqjXA (https://youtu.be/3WQK9qrqjXA)


I try to clean the previous sprite spot before writing the sprite on its new position but this creates glitches.


The codes used is based on "easy/keyboard" from CPCTelera  :-[
Code: [Select]
#include <cpctelera.h>
#include "mouse_left.h"
#include "mouse_right.h"
#include "mouse_up.h"
#include "mouse_down.h"


// Sprite size (in bytes)
#define SP_W   8
#define SP_H   32


// Screen size (in bytes)
#define SCR_W   80
#define SCR_H  200


// Number of loops to wait between each sprite move
#define WAITLOOPS 200


//
// MAIN: Using keyboard to move a sprite example
//
void main(void) {
   u8  x=10, y=10;   // Sprite coordinates
   u8* pvideomem;    // Pointer to video memory
   u32 i;            // 32 bits counter, to let it count passed 65536 (up to 4 Billions)


   //
   // Set up the screen
   //
   // Disable firmware to prevent it from interfering with setPalette and setVideoMode
   cpct_disableFirmware();


   // Set the colour palette
   // cpct_fw2hw     (g_palette, 16); // Convert our palette from firmware to hardware colours
   cpct_setPalette(g_palette, 16); // Set up the hardware palette using hardware colours
   
   // Set video mode 0 (160x200, 16 colours)
   cpct_setVideoMode(0);
   
   // Clear and set the background color to blue using the palette index
   cpct_clearScreen(cpct_px2byteM0(1,1));
     
   //
   // Infinite moving loop
   //
   while(1) {
      // Scan Keyboard (fastest routine)
      // The Keyboard has to be scanned to obtain pressed / not pressed status of
      // every key before checking each individual key's status.
      cpct_scanKeyboard_f();


      /*
      if (cpct_isAnyKeyPressed()) {
         // Get video memory byte for coordinates x, y of the sprite (in bytes)
         pvideomem = cpct_getScreenPtr(CPCT_VMEM_START, x, y);
         // clear the sprite
         cpct_drawSolidBox(pvideomem, cpct_px2byteM0(1,1), SP_W, SP_H);
      }
      */


      // Check if user has pressed a Cursor Key and, if so, move the sprite if
      // it will still be inside screen boundaries
      if      (cpct_isKeyPressed(Key_CursorRight) && x < (SCR_W - SP_W) ) ++x;
      else if (cpct_isKeyPressed(Key_CursorLeft)  && x > 0              ) --x;
      if      (cpct_isKeyPressed(Key_CursorUp)    && y > 0              ) --y;
      else if (cpct_isKeyPressed(Key_CursorDown)  && y < (SCR_H - SP_H) ) ++y;
     
      // Get video memory byte for coordinates x, y of the sprite (in bytes)
      pvideomem = cpct_getScreenPtr(CPCT_VMEM_START, x, y);
   
      // Draw the sprite in the video memory location got from coordinates x, y
      if      (cpct_isKeyPressed(Key_CursorRight)) cpct_drawSprite(g_mouse_right, pvideomem, SP_W, SP_H);
      else if (cpct_isKeyPressed(Key_CursorLeft)) cpct_drawSprite(g_mouse_left, pvideomem, SP_W, SP_H);
      else if (cpct_isKeyPressed(Key_CursorUp)) cpct_drawSprite(g_mouse_up, pvideomem, SP_W, SP_H);
      else if (cpct_isKeyPressed(Key_CursorDown)) cpct_drawSprite(g_mouse_down, pvideomem, SP_W, SP_H);
      else cpct_drawSprite(g_mouse_down, pvideomem, SP_W, SP_H);
   
      // set the speed
      for(i=0; i < WAITLOOPS; ++i);
   }
}
Title: Re: Sprite moving and leaving crap behind
Post by: Arnaud on 21:33, 06 June 20
Erasing sprites trails all a story !

The easiest solution is your sprite have large border with the color of the background it will overwrite the trails when moving.

Or you have to delete the trail at the previous position of your sprite, with something like that :

Code: [Select]
if (move == TOP)
{
u8* pvideomemTrail = cpct_getScreenPtr(CPCT_VMEM_START, x, y + SP_H + 1);
cpct_drawSolidBox(pvideomemTrail, cpct_px2byteM0(1,1), SP_W, 1);
}
else
if (move == LEFT)
{
u8* pvideomemTrail = cpct_getScreenPtr(CPCT_VMEM_START, x + SP_W + 1, y);
cpct_drawSolidBox(pvideomemTrail, cpct_px2byteM0(1,1), 1, SP_H);
}
else
...





Title: Re: Sprite moving and leaving crap behind
Post by: kamelie1706 on 00:05, 07 June 20
Great thx!
I went for "fick border", in mode 0 you are on the safe side with 2 pixel all around ... no I need to work on diagonal movement/sprite  :P


https://youtu.be/ihXTHoQXvj4 (https://youtu.be/ihXTHoQXvj4)


One aspect I am not yet familiar is the speed movement. The horizontal movement looks way faster than vertical movement ... any trick for that or it is inherent to mode 0?
Title: Re: Sprite moving and leaving crap behind
Post by: Ast on 00:35, 07 June 20

Hi,

When you add +1 in x, add +4 in y (in mode 0) to keep the same speed in x,y.
Title: Re: Sprite moving and leaving crap behind
Post by: kamelie1706 on 00:59, 07 June 20
Works perfect, thx ... but now my 2 pixel margin is not enought!
I guess I need 4pixels all around  :doh:
Title: Re: Sprite moving and leaving crap behind
Post by: Arnaud on 09:08, 07 June 20
Or divide the diagonal move in two steps : move X then move Y
Title: Re: Sprite moving and leaving crap behind
Post by: Ast on 11:19, 07 June 20
But, we are all French ! Next time, post in French topic.
Title: Re: Sprite moving and leaving crap behind
Post by: kamelie1706 on 15:30, 07 June 20
I can do both!  ;D


It was not straight forward, but created extra sprites for diagonal and this is the result playing with the keyboard
https://youtu.be/RXvhREW60Cw (https://youtu.be/RXvhREW60Cw)


Good enough at this stage for making my moving function for both rats and player ...


Merci pour vos précieux conseils