News:

Printed Amstrad Addict magazine announced, check it out here!

Main Menu
avatar_kamelie1706

Sprite moving and leaving crap behind

Started by kamelie1706, 18:35, 06 June 20

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

kamelie1706

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  :-[

#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);
   }
}
Rats! Rats! Rats!

Arnaud

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 :

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
...







kamelie1706

#2
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?
Rats! Rats! Rats!

Ast


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 welcome !

kamelie1706

Works perfect, thx ... but now my 2 pixel margin is not enought!
I guess I need 4pixels all around  :doh:
Rats! Rats! Rats!

Arnaud

Or divide the diagonal move in two steps : move X then move Y

Ast

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 welcome !

kamelie1706

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!

Powered by SMFPacks Menu Editor Mod