IGNORED

# Vectrex screen coordinates system

## Recommended Posts

Hello there,

I was studying the incredible Vectrex, and to understand more how it works I started to code a small sample. Well for now it's just a moving "sprite", but maybe in the future it may become a small game. ?

I'm almost unfamiliar with assembly language (my small skills are only on the 6502) so I used the CMOC compiler to compile a source written in C. For now everything is fine. I found some samples, and with them I was able to start.

But now I have issues with the coordinates system. I understand that (0,0) is the screen center.

My sprite position, when I move it to (0,0) is perfectly aligned with the screen center :

My x/y variables are defined with uint8_t values.

When I move slightly to the left (x = x-1), as my x value can't be <0 it becomes like 250, and it works fine. (Looping between 0...255, normal).

So now I want to add a missile. When pushing a button a second "sprite" is added, moving on the +x axis, starting at the actual player position. The idea is to "cut" the missile once it is away from the screen.

So in my code I have something like this :

Quote

if ( ShootInProgress==1 &&  missileX >= 254 )
ShootInProgress=0;

if ( ShootInProgress==1)

drawMissile();

to "reset" the missile once it goes away from the screen. BUT 254 doesn't works as a limit, as x=254 means the middle of the screen.  The good "edge" value should be 128 is the missile is fired on the right part of the screen, 255 if left.

So I'm not sure how to deal with that. I can of course use an other variable or programming trick to solve this problem, but maybe it's not the good way to work.

Is there a good way to deal with coordinates ? Like using a signed variable ??? (something from -128 to +128) ???

Thanks for your feedback...

##### Share on other sites

Yes, life will be much simpler if you use signed chars for X and Y coordinates!

##### Share on other sites

I tried to use int8_t variables to store my positions. Seems to work fine now !!

Now I can go deeper....

Thanks ! ?

##### Share on other sites

Ok my knowledge on this is slowly increasing, but there is still some weird things that I need to understand, like the set_scale function that could be used to size objects.

It looks like that a scale value of 128 leave the object untouched, 64 divide the size by two, and so one.

But I don't see how to deal with multiples values, like putting two set_scales instructions together. It seems that everything is relative !

I didn't manage to find a programming manual or similar stuff on this (only for assembly...).

Is there resources for the Vectrex for C programming ?

##### Share on other sites

• 5 weeks later...

Is there any C gurus here ? I have some issues with my custom "itoa" function that I use to convert a signed char (or unsigned) into a string :

```/* reverse:  reverse string s in place */
void m_reverse(char s[])
{
char i, j;
char c;

for (i = 0, j = strlen(s)-1; i<j; i++, j--) {
c = s[i];
s[i] = s[j];
s[j] = c;
}
}

//* itoa:  convert n to characters in s */
void m_itoa(char n, char s[])
{
char i, sign;

if ((sign = n) < 0)  /* record sign */
n = -n;          /* make n positive */
i = 0;
do {       /* generate digits in reverse order */
s[i++] = n % 10 + '0';   /* get next digit */
} while ((n /= 10) > 0);     /* delete it */
if (sign < 0)
s[i++] = '-';
s[i] = '\0';
m_reverse(s);
}```

(I can't remember the source).

And, in my display loop :

```  m_itoa(x, sX);
m_itoa(y, sY);

print_str_c(127, 190, sX);  //y  x
print_str_c(127 , 0, sY);  //y  x```

It does works fine with positives or negative numbers :

But it doesn't works with numbers  between -9 and 9 (with one digit). A kind of underscore is displayed instead :

I didn't manage to solve this. IS there something wrong with my itoa function ? Maybe a problem on the internal while loop... I'll check.

##### Share on other sites

• 2 weeks later...

The functions look ok - though I didn't test them.

It might just be the printStr() functions of the vectrex BIOS.

For them to display correct results you need strings with AT LEAST THREE characters.

Try adding an additional function, which puts in leading spaces (or trailing) so the "numbers" are at least 3 characters long.

##### Share on other sites

Ok so this actually works :

```//* itoa:  convert n to characters in s */

void m_itoa2(unsigned char n, char s[])

{

unsigned char i, sign,f;

if ((sign = n) < 0)  /* record sign */

n = -n;          /* make n positive */

f=n;

i = 0;

do {       /* generate digits in reverse order */

s[i++] = n % 10 + '0';   /* get next digit */

} while ((n /= 10) > 0);     /* delete it */

if (f < 10)

s[i++] = '0' + n ;

if (sign < 0)

s[i++] = '-';

s[i] = '\0';

m_reverse(s);

}```
```
```

It adds a zero in numbers <10.

So yes, maybe it's just a small issue in the ROM code...

##### Share on other sites

I know you are using cmoc instead of gcc.

But I don't know if you know about Vide. (http://vide.malban.de/)

Even if you don't use Vide, the blog, the documentation (help and other docs provided with it) and the links I have collected provide much information about "everything vectrex".

If you have not looked at those yet - I'd recomend it.

(also many example "C" code)

Just a thought...

Also Roger Boesch has developed a "beginners" IDE for CMOC coding for the Vectrex, check out his Video: https://www.youtube.com/watch?v=I3f8qO8AU_k

Cheers

Malban

Edited by Malban
##### Share on other sites

My actual dev enviroment is very light, (Qt creator as IDE, a batch file to compile, and ParaJVE for execution). It works, but it's absolutely not easy to wok with.

I've downloaded Vide, I'll sure take a deep look at it. ?

##### Share on other sites

On 7/21/2021 at 12:50 AM, Yoruk said:

Ok so this actually works :

>  if ((sign = n) < 0) n = -n;

Any code with that logic in it will fail on the largest negative number.

Integers go from -MAX-1 to MAX so negating the largest negative number results in MAX+1 which overflows MAX, but negating MAX merely results in -MAX which is not as negative as -MAX-1.

So, on a machine with a divide, it's marginally better to do:

if ((sign = m) > 0) n == -n

and divide or mod by -10 each step.  I.e. work with negative numbers until you come to outputting the digits.

However on the Vectrex I try to avoid divides - see for example  "unsigned int8_t itoa" in http://gtoal.com/vectrex/vecavern/v.c.html

although I must admit I've never done a benchmark to confirm that it is faster number to ascii conversion code. I should do that one day...

G

Edited by gtoal
##### Share on other sites

I'll try this "unsigned int8_t itoa" function, thanks !!

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.