Some things not working for me

Discussion about other targets
WauloK
Member
Posts: 63
Joined: Thu Oct 10, 2019 10:45 am

Some things not working for me

Post by WauloK »

This could very well be my not fully understanding C yet.

I am working on a library .h & .c for me and others to use on VZ200.

In the .h file I declare a function:

Code: Select all

#ifndef _FOXGLOVZ_H_
#define _FOXGLOVZ_H_

extern void fox_textat(int x, int y, int c, int spacing, char font[], char *text);
#endif
I have the actual function in the related .c file:

Code: Select all

void fox_textat(int x, int y, int c, int spacing, char font[], char *text)
{
...blah..
}
In my main.c file I use:
#include "foxglovz.h"
int main(void)
{
fox_textat(0,0,_vzred,1,font,"A");
}
and I get an error:

Code: Select all

symbol '_fox_textat' not defined
So I change it to
#include "foxglovz.c"
and it works.

I'm also trying to convert an ascii char to int and perform maths on it

Code: Select all

int temp;
int fontoffset = 0;
int textchar = text[i];
temp=textchar+fontoffset;
If I use this:

Code: Select all

printf("THIS=%d=%d=\n",textchar,temp);
and the character is "A" I get

THIS=65=1862=

I don't know why 65 + 0 = 1862

I tried:
int textchar = (int)text;
but the maths still comes out the same on the VZ200.
WauloK
Member
Posts: 63
Joined: Thu Oct 10, 2019 10:45 am

Post by WauloK »

hmm. I just tested something.
The
int fontoffset = 0;
is before a "for" loop
if I move the
fontoffset = 0
inside the for loop it seems to calculate.

so:

Code: Select all

int fontoffset =0;
for (i=0;i < strlen(text); i++) 
{
int textchar = text[i]; temp=textchar-fontoffset;
}
fontoffset doesn't work inside the loop
I have to change it to:

Code: Select all

for (i=0;i < strlen(text); i++) 
{
int fontoffset =0;
int textchar = text[i]; temp=textchar-fontoffset;
}
User avatar
dom
Well known member
Posts: 2076
Joined: Sun Jul 15, 2007 10:01 pm

Post by dom »

The first issue is expected. You've defined fox_textat in another file, so you have to compile the files together:

zcc +vz main.c foxglove.c

The second issue I can't reproduce - can you post a full source file that demonstrates the issue?
WauloK
Member
Posts: 63
Joined: Thu Oct 10, 2019 10:45 am

Post by WauloK »

Hi

Dropbox link: https://www.dropbox.com/s/5a86xz9uohyim ... g.zip?dl=0

I tried sending you a message through this board but that functionality is broken.

I've done some updates, but the problem now is similar.
I use:

Code: Select all

int fontoffset,arrayoffset;
arrayoffset = 2;
Then later confirm and get:
AOFF=1797
when run on VZEm emulator.

I thought there was some issue with my calculations for fontdatastart so you can see I separated each maths into separate lines but then realised the issue was arrayoffset being set to 2 but when calculations run it was now 1797.
WauloK
Member
Posts: 63
Joined: Thu Oct 10, 2019 10:45 am

Post by WauloK »

In fact, I've just done a shorter test:

Code: Select all

        arrayoffset = 2; printf("aoff=%d\n",arrayoffset);
        fontoffset = 32;
        fontarray[0] = fontspec.width;
        fontarray[1] = fontspec.height;        
        vz_color(c);
        printf("aoff=%d\n",arrayoffset);
The first printf shows arrayoffset=2 but when the second one happens it shows arrayoffset=1797
WauloK
Member
Posts: 63
Joined: Thu Oct 10, 2019 10:45 am

Post by WauloK »

Update:

Code: Select all

arrayoffset = 2; printf("aoff=%d\n",arrayoffset);
        fontoffset = 32;
        printf("aoff=%d\n",arrayoffset);
        fontarray[0] = fontspec.width;
        fontarray[1] = fontspec.height;        
        printf("aoff=%d\n",arrayoffset);
This outputs:
AOFF=2
AOFF=2
AOFF=1797
stefano
Well known member
Posts: 2137
Joined: Mon Jul 16, 2007 7:39 pm

Post by stefano »

You should set a pointer type for arrayoffset.
Void * or unsigned char * should work
stefano
Well known member
Posts: 2137
Joined: Mon Jul 16, 2007 7:39 pm

Post by stefano »

Ok, right. You shouldn't get this result.
And I saw your program, you are using an array already.
I wouldn't blame the compiler, it is most probably a memory overflow due to a bad sp position or a data range surpassed.
User avatar
dom
Well known member
Posts: 2076
Joined: Sun Jul 15, 2007 10:01 pm

Post by dom »

Right, I see the problem:

Code: Select all

        int i,j,fontdatastart,textchar;
        int fontoffset,arrayoffset;
        char fontarray[];
Which defines an array with 0 elements - i.e. it consumes no space on the stack. So you've got on the stack:

sp+0 = fontarray
sp+0 = arrayoffset
sp+2 = font offset

And then you've got:

Code: Select all

        fontarray[0] = fontspec.width;
        fontarray[1] = fontspec.height;
Which will overwrite array offset with the value 7 * 256 + 5 = 1797
WauloK
Member
Posts: 63
Joined: Thu Oct 10, 2019 10:45 am

Post by WauloK »

I tried
char fontarray[9]
But that didn't help
But will change to pointers thanks
WauloK
Member
Posts: 63
Joined: Thu Oct 10, 2019 10:45 am

Post by WauloK »

If I don't know the size of the array at the start and cannot define with char fontarray[] because it will muck up the stack, is there something else I can do?
WauloK
Member
Posts: 63
Joined: Thu Oct 10, 2019 10:45 am

Post by WauloK »

I'll go with malloc. I guess we can't delete posts.
WauloK
Member
Posts: 63
Joined: Thu Oct 10, 2019 10:45 am

Post by WauloK »

I'm reading the documentation and it says the stack is used for temporary storage of variables in a function. So why are they overwriting variables here?

In fact, calling a function with a struct as a parameter has the data out of order for some reason.
I'm very confused here.

Code: Select all

// Specifications passed from user program
struct myfontspecs
{
        unsigned char name[20];
        unsigned char xpos;
        unsigned char ypos;
        unsigned char spacing;
        unsigned char colour;
        unsigned char fx;
};

        struct myfontspecs myfont;
        strcpy(myfont.name, "sansserif5x7");
        myfont.xpos = 0;
        myfont.ypos = 20;
        myfont.spacing = 1;
        myfont.colour = _vzred;
        myfont.fx = _fxnormal;
        fox_textat(&myfont," a");
I get the parameters at the other end:

Code: Select all

void fox_textat(struct myfontspecs *myfont, char *text)
{
When I check myfont.xpos myfont.ypos etc they are all wrong.
Ie.
myfont.fx is set to 1
myfont.xpos is set to 114
myfont.ypos is set to 0
myfont.spacing is set to 0
myfont.colour is set to 20
myfont.name is "7^sansserif5x7"
User avatar
dom
Well known member
Posts: 2076
Joined: Sun Jul 15, 2007 10:01 pm

Post by dom »

There's definitely something wrong with your setup and I'm really confused too as to what's going on.

The first example with the array - I made the array size 9 and got this result:

Code: Select all

off=2 fonts.h=7 txtch=32 off=32 fontst=2
aoff=2 fonts.h=7 txtch=32 off=32 fontst=2
aoff=2 fonts.h=7 txtch=32 off=32 fontst=2
aoff=2 fonts.h=7 txtch=32 off=32 fontst=2
aoff=2 fonts.h=7 txtch=32 off=32 fontst=2
aoff=2 fonts.h=7 txtch=32 off=32 fontst=2
aoff=2 fonts.h=7 txtch=32 off=32 fontst=2
aoff=2 fonts.h=7 txtch=97 off=32 fontst=457
aoff=2 fonts.h=7 txtch=97 off=32 fontst=457
aoff=2 fonts.h=7 txtch=97 off=32 fontst=457
aoff=2 fonts.h=7 txtch=97 off=32 fontst=457
aoff=2 fonts.h=7 txtch=97 off=32 fontst=457
aoff=2 fonts.h=7 txtch=97 off=32 fontst=457
The second example with the structs, I tried out this test program:

Code: Select all

#include <string.h>
#include <stdio.h>

struct myfontspecs
{
    unsigned char name[20];
    unsigned char xpos;
    unsigned char ypos;
    unsigned char spacing;
    unsigned char colour;
    unsigned char fx;
};

void fox_textat(struct myfontspecs *myfont, char *text)
{
   printf("Name: %s\n",myfont->name);
   printf("xpos: %d\n",myfont->xpos);
   printf("ypos: %d\n",myfont->ypos);
   printf("spacing: %d\n",myfont->spacing);
   printf("colour: %d\n",myfont->colour);
   printf("fx: %d\n",myfont->fx);

}

int main() {
    struct myfontspecs myfont;
    strcpy(myfont.name, "sansserif5x7");
    myfont.xpos = 0;
    myfont.ypos = 20;
    myfont.spacing = 1;
    myfont.colour = 3; // _vzred;
    myfont.fx = 12; // _fxnormal;
   fox_textat(&myfont," a");
}
Compiled with "zcc +test test.c" and then ran with "z88dk-ticks a.bin" and got this:

Code: Select all

% z88dk-ticks a.bin
Name: sansserif5x7
xpos: 0
ypos: 20
spacing: 1
colour: 3
fx: 12
So that's working too - if you use +test do you get the same result as me?

Regarding memory allocation, it looks like we're missing a wiki page that I'll have to write this weekend. However, remove all the mallinit stuff and just use malloc/calloc/free and compile with -DAMALLOC - this will automatically setup the heap
WauloK
Member
Posts: 63
Joined: Thu Oct 10, 2019 10:45 am

Post by WauloK »

with my code and +test and ticks I get:
ticks a.bin
text= aname=?■sansserif5x7 colour=20 xpos=0 ypos=0100000002
WauloK
Member
Posts: 63
Joined: Thu Oct 10, 2019 10:45 am

Post by WauloK »

With your code above:
Name: sansserif5x7
xpos: 0
ypos: 20
spacing: 1
colour: 3
fx: 12
WauloK
Member
Posts: 63
Joined: Thu Oct 10, 2019 10:45 am

Post by WauloK »

Maybe the way I access it is wrong?
Here's an output test combining your code and mine:

Code: Select all

printf("name=%s colour=%d xpos=%d ypos=%d\n",myfont.name,myfont.colour,myfont.xpos,myfont.ypos);
   printf("Name: %s\n",myfont->name);
   printf("xpos: %d\n",myfont->xpos);
   printf("ypos: %d\n",myfont->ypos);
   printf("spacing: %d\n",myfont->spacing);
   printf("colour: %d\n",myfont->colour);
   printf("fx: %d\n",myfont->fx);
ticks a.bin
name=?■sansserif5x7 colour=20 xpos=0 ypos=0
Name: sansserif5x7
xpos: 0
ypos: 20
spacing: 1
colour: 3
fx: 12
WauloK
Member
Posts: 63
Joined: Thu Oct 10, 2019 10:45 am

Post by WauloK »

Yep. Changing your code output to:
printf("Name: %s\n",myfont.name);
breaks it:
Name: ?■sansserif5x7

According to tutorials I read this should be a valid way of accessing struct data?
User avatar
dom
Well known member
Posts: 2076
Joined: Sun Jul 15, 2007 10:01 pm

Post by dom »

So, there's obviously a diagnostic missing which I'm currently adding.

Running though clang gets this error:

str2.c:16:30: error: member reference type 'struct myfontspecs *' is a pointer; did you mean to use
'->'?
printf("Name: %s\n",myfont.name);

Which shows the problem, you have to use "->" to access members if you've got a pointer to the struct, you use "." if you've got the actual struct (as we do in main())
WauloK
Member
Posts: 63
Joined: Thu Oct 10, 2019 10:45 am

Post by WauloK »

Oh I see. Thanks for your help. Sorry I'm still learning a lot.
User avatar
dom
Well known member
Posts: 2076
Joined: Sun Jul 15, 2007 10:01 pm

Post by dom »

No worries. We got it confused many times in both libraries and examples it has transpired.

There?s now errors when you get it wrong - it would be easy to make it work, but it?s best to match the behaviour of other compilers.
WauloK
Member
Posts: 63
Joined: Thu Oct 10, 2019 10:45 am

Post by WauloK »

Oh? A new build? I'll try it out. Thanks! :)

I hope once I complete my project it can be used on all supported platforms which have monographic sprites.
WauloK
Member
Posts: 63
Joined: Thu Oct 10, 2019 10:45 am

Post by WauloK »

Finally getting somewhere.

One Question: Using monographics sprites is the colour hard-coded? There's no way to change the colour the monochrome sprites are displayed in?
I get that monochrome usually means white-on-black so a random colour may have been picked in the VZ case since it doesn't have these.
Or does monochrome mean only one colour, but I can pick which single colour?
WauloK
Member
Posts: 63
Joined: Thu Oct 10, 2019 10:45 am

Post by WauloK »

Well, I suppose it's time to share what I've been working on all this time.

Many thanks to dom and stefano for getting me this far!

Foxglovz is a new Graphics Library for the VZ computer. Basically this name derives from Fox Graphics Library On VZ.

It was originally developed for the VZ 200 and VZ 300 computers by Dick Smith, but technically should work on any platform supported by the Z88DK development kit so long as it supports monochrome graphics sprites.

It was written by Jason Oakley because the Z88DK seems to only have great support for graphics and, hence, fonts for Spectrum computers. A lot of work has been put in for them, but the others have fallen on the wayside.

Initially, Foxglovz will support rendering fonts in monochrome graphics but will hopefully be expanded with other functionality in the future.

You can read about the first version here: https://bluebilby.com/foxglovz/
User avatar
dom
Well known member
Posts: 2076
Joined: Sun Jul 15, 2007 10:01 pm

Post by dom »

That is quite interesting - always good to have another font library and it's an interesting approach to it as well - you do have a memory leak though: fontarray isn't freed! The approach makes me wonder if it could be adapted for handling fzx fonts without having to write a driver for every machine.

If a target has either bitmapped graphics, or redefinable character set then we've added 8x8 font support: I know I skipped doing the VZ since the resolution wasn't great, however it would be good to support it for completeness.

Stefano made a 4x6 font routine that uses plot/unplot, I think RobertK ended up using that for his HTron project.
Post Reply