Author Topic: Sprite moving and leaving crap behind  (Read 744 times)

0 Members and 1 Guest are viewing this topic.

Offline kamelie1706

  • CPC464
  • **
  • Posts: 11
  • Country: fr
  • Liked: 6
  • Likes Given: 3
Sprite moving and leaving crap behind
« on: 20:35, 06 June 20 »
hi,


Do you know why my sprite does that?

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);
   }
}
« Last Edit: 00:06, 07 June 20 by kamelie1706 »
Rats! Rats! Rats!

Offline Arnaud

  • Supporter
  • 6128 Plus
  • *
  • Posts: 683
  • Country: fr
  • Liked: 601
  • Likes Given: 1227
Re: Sprite moving and leaving crap behind
« Reply #1 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
...






Offline kamelie1706

  • CPC464
  • **
  • Posts: 11
  • Country: fr
  • Liked: 6
  • Likes Given: 3
Re: Sprite moving and leaving crap behind
« Reply #2 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


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?
« Last Edit: 00:28, 07 June 20 by kamelie1706 »
Rats! Rats! Rats!

Offline Ast

  • 6128 Plus
  • ******
  • Posts: 1.128
  • Country: fr
    • Amstrad cpc Website of Ast/iMPACT
  • Liked: 1038
  • Likes Given: 755
Re: Sprite moving and leaving crap behind
« Reply #3 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.
_____________________

Ast/iMP4CT. "By the power of Grayskull, i've the power"

http://amstradplus.forumforever.com/index.php
http://impdos.wikidot.com/
http://impdraw.wikidot.com/

All friends are welcomed !

Offline kamelie1706

  • CPC464
  • **
  • Posts: 11
  • Country: fr
  • Liked: 6
  • Likes Given: 3
Re: Sprite moving and leaving crap behind
« Reply #4 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:
Rats! Rats! Rats!

Offline Arnaud

  • Supporter
  • 6128 Plus
  • *
  • Posts: 683
  • Country: fr
  • Liked: 601
  • Likes Given: 1227
Re: Sprite moving and leaving crap behind
« Reply #5 on: 09:08, 07 June 20 »
Or divide the diagonal move in two steps : move X then move Y

Offline Ast

  • 6128 Plus
  • ******
  • Posts: 1.128
  • Country: fr
    • Amstrad cpc Website of Ast/iMPACT
  • Liked: 1038
  • Likes Given: 755
Re: Sprite moving and leaving crap behind
« Reply #6 on: 11:19, 07 June 20 »
But, we are all French ! Next time, post in French topic.
_____________________

Ast/iMP4CT. "By the power of Grayskull, i've the power"

http://amstradplus.forumforever.com/index.php
http://impdos.wikidot.com/
http://impdraw.wikidot.com/

All friends are welcomed !

Offline kamelie1706

  • CPC464
  • **
  • Posts: 11
  • Country: fr
  • Liked: 6
  • Likes Given: 3
Re: Sprite moving and leaving crap behind
« Reply #7 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


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


Merci pour vos précieux conseils
Rats! Rats! Rats!