New to z88dk zx dev

ZX80, ZX 81, ZX Spectrum, TS2068 and other clones
Post Reply
amateus
Member
Posts: 46
Joined: Fri Nov 15, 2019 9:13 am

New to z88dk zx dev

Post by amateus »

Hi guys,

I'm new to z88dk in general and I'm aiming to learn if for zx spectrum game development, so please bear with me if I ask some idiotic question.

I've read the documentation for z88dk and sp1 and I've read the excellent series of getting Getting Started Guide from Derek, but I have a few concern regarding game development for the platform.

General questions

1) Memory is scarce in spectrum, so do we have a way of knowing how much memory our program currently occupies, and what addresses? My concern here is a) the program occupies more memory than the available, b) our program overwrites memory than shouldn't and c) if I want to add other external routines, in what address should I add them.

2) Is sp1 the correct solution for game development or there are any better ones? I've seen biFrost, but apparently can only be used in a small ammout of screen?

3) Tiles. As I understood, tiles are just UDG, meaning in order to write a tile to the screen we need to replace a character by our graphic. So my question is how can we design complex screens, with background, platforms, etc, as I assume we'll run out of characters at some point

4) Screens. Can we use a scr printed to screen and then update just the playable area? This is tied to 1) and 3), as if possible, we need to load the scr in a specific memory address without overwriting stuff and *should* solve the lack of characters for tiles?

I'll continue digging and if a solution is found to any of these questions, or more probably new questions arise, I'll edit the post.

Thank you,
Ant?nio
benjymous
New member
Posts: 3
Joined: Fri Nov 01, 2019 9:05 am

Post by benjymous »

Hello,

I've only been experimenting a little while myself, so I'm sure there are people who can give better answers

1) easiest way is to use a memory browser in an emulator - if you're using the various pragmas to position your code, data, stack and heap in memory, you should be able to see them - see https://github.com/z88dk/z88dk/blob/mas ... Details.md for info about the pragmas

2) different libraries do different things - bifrost and nirvana are "multi colour" libraries - they use clever timing tricks to make the ZX hardware display more colours than it would otherwise usually be able to do, but because of that you're limited by what you can display. sp1 is a more generic library that can display masked (overlapping) sprites.

3) If you use sp1, then you can just define your tiles as binary data, then draw it to screen - there's no need to go via UDGs, that's just one shortcut to displaying 8x8 tiles

4) Yes, you just load the scr as normal, if you want to display it directly, or load it to a different location and copy it to the screen memory to display it otherwise. Then just do normal graphics operations on top
amateus
Member
Posts: 46
Joined: Fri Nov 15, 2019 9:13 am

Post by amateus »

Hi,

Thank you for you answer.
benjymous wrote:Hello,
1) easiest way is to use a memory browser in an emulator - if you're using the various pragmas to position your code, data, stack and heap in memory, you should be able to see them - see https://github.com/z88dk/z88dk/blob/mas ... Details.md for info about the pragmas
Thank you, I'll check this in more detail

benjymous wrote:3) If you use sp1, then you can just define your tiles as binary data, then draw it to screen - there's no need to go via UDGs, that's just one shortcut to displaying 8x8 tiles
No sure I follow, sorry. Can you detail? So far all examples I've see used sp1_TileEntry(<CHAR>, <GRAPH>); to add a tile, replacing character X by the tile graph. Is there a different way?
benjymous wrote:4) Yes, you just load the scr as normal, if you want to display it directly, or load it to a different location and copy it to the screen memory to display it otherwise. Then just do normal graphics operations on top
And because we usually only work with a rect of the screen, the display outside the rect maintains the scr. Got it, thanks.

Again, thank you for your reply.
Ant?nio
benjymous
New member
Posts: 3
Joined: Fri Nov 01, 2019 9:05 am

Post by benjymous »

For displaying by background graphics I've been using sp1_PrintAt

That takes a pointer to the binary data, so give it 8 bytes containing your tile pixels
Timmy
Well known member
Posts: 393
Joined: Sat Mar 10, 2012 4:18 pm

Post by Timmy »

Let me try to answer them too, although most answers are the same as previous answers...
amateus wrote:1) Memory is scarce in spectrum, so do we have a way of knowing how much memory our program currently occupies, and what addresses? My concern here is a) the program occupies more memory than the available, b) our program overwrites memory than shouldn't and c) if I want to add other external routines, in what address should I add them.
As the previous answers, use an emulator that can show you how much memory is used in any point during gameplay, and where it is used.
For Spectrum game development, you have to manage the memory yourself. If the emulator shows a large block of 0's at parts of the memory, you can probably safely use it.

If you use sp1, all memory from ce00-ffff is used by/for sp1, too. So you can't use those bytes for yourself. For more information, try: https://github.com/z88dk/z88dk/blob/mas ... tomize.asm and https://www.worldofspectrum.org/forums/ ... ion/27642/ (there are even more info on wos but let's start with this one.)

In general, my games usually uses all memory from 25000-65535. (Memory 25000-32767 is slower than 32768-65535, but it doesn't matter too much for me.)

Usually I put the music code (external routines) between the game code and sp1, so somewhere before ce00.
2) Is sp1 the correct solution for game development or there are any better ones? I've seen biFrost, but apparently can only be used in a small ammout of screen?
biFrost and nirvana (biFrost's successor) are both multicoloured sprites/tiles engines. I haven't used them much because I think they use even more memory and cycles for the Spectrum than I want to. But there are many good games that are using it, so it shouldn't be a problem. I think Nirvana can use almost all screen, too.

sp1 is a masked tile sprite engine. It's a very good compromise if you are making fast arcade games. I use it often but not always, for example in Heart Stealer or in Forest Raider Cherry.

I also use other engines depending on the game I'm making. For example, for Future Looter I used a xor-based sprite engine. And lately I'm making a lot more "thinking" games where I use a very simple UDG drawing engine (because I don't use sprites).

I would suggest you to use sp1 for starters. It's just very good and fast.
3) Tiles. As I understood, tiles are just UDG, meaning in order to write a tile to the screen we need to replace a character by our graphic. So my question is how can we design complex screens, with background, platforms, etc, as I assume we'll run out of characters at some point
Some of the sp1 functions will let you use a memory address instead of the UDG number, so basically you can use any memory in the machine. And of course you can redefine any UDG at any moment so it doesn't really matter. In practice, you will want to use UDGs instead, because if your game needs 6K memory for one screen then you'll find your game won't have too many screens.
4) Screens. Can we use a scr printed to screen and then update just the playable area? This is tied to 1) and 3), as if possible, we need to load the scr in a specific memory address without overwriting stuff and *should* solve the lack of characters for tiles?
Yes, the Churrera engine does exactly this. Although, for sp1, you need to make sure the "playable area", where your sprites appear, that part must should be in UDGs, because the engine depends on reprinting that background when the sprites are being updated.
Timmy
Well known member
Posts: 393
Joined: Sat Mar 10, 2012 4:18 pm

Post by Timmy »

Some extra notes:

1) I think there's still an sp1 demo from me somewhere where I demonstrate many sp1 things (but not really explaining them). I don't know where the latest version of it is, but it's possible that's in z88dk somewhere.

2) The Churrera engine doesn't exactly used sp1 but an earlier version of it.

3) There's also this: The ZX Spectrum Programmer's Z88DK Getting Started Guide
https://github.com/z88dk/z88dk/blob/f6c ... edGuide.md
derekfountain
Member
Posts: 121
Joined: Mon Mar 26, 2018 1:49 pm

Post by derekfountain »

When I'd learnt the stuff I needed (and documented in the getting started guide) I wrote a ZX Spectrum game called ZX Wonky One Key using z88dk and SP1. You might find the sources helpful.

You'll probably find that as you progress past simple examples you'll naturally become intimately familiar with the memory map of your programs. Examining the memory of a running machine (i.e. emulator) is the simple way, but it's a bit of a nuisance having to do that all the time. I added a script to my build which picks up the map file generated by the linker (i.e. the file which contains all the addresses, offsets, variable locations, etc.) and extracts a few key values from it to give me an indication of how much free memory I have left. As an approach it's a bit limited because it can only tell you what's statically arranged where at program start up. It doesn't tell you what memory gets consumed as the program runs, or where the stack goes down to, or what's on the heap... You need to keep a manual track of those sorts of things, but you'll probably just do it naturally when it becomes necessary.

SP1 worked out well for me when I wrote my game. The multicolour engines like BiFROST are very interesting and clever, but definitely not for beginners. SP1 is a reasonable balance between complexity and features, and for the right type of game (anything with sprites) it's ideal. The big drawback with it, other than the amount of memory it takes, is the limited documentation. The tutorial is rather dated but it still introduces all the right concepts. Alvin's written loads of excellent information on SP1, but it's all in posts on this forum so it takes some finding. There's probably enough example code out there now to get you going on any project so I'd suggest SP1 is a good a starting point as any other for a newbie Spectrum game developer.
amateus
Member
Posts: 46
Joined: Fri Nov 15, 2019 9:13 am

Post by amateus »

Hi guys,

Thanks Timmy and Derek for you insights. I'll have to digest all :)

Memory management is, in fact, what concerns me the most, as we don't have an easy, direct way of knowing how much memory is free. I guess I have to really start learning spectrum memory :)

@Derek tahnk you for your GettingStarted guide, it really helped me to understand what z88 and sp1 was. I wish you'd write some more :)

Thank you once again,
Ant?nio
Timmy
Well known member
Posts: 393
Joined: Sat Mar 10, 2012 4:18 pm

Post by Timmy »

Oh, I found my sp1 demo, it's here: https://github.com/z88dk/z88dk/tree/f6c ... _sp1/demo2

I am currently a bit busy but I might actually want to explain this whole code one day. Hope it's very soon.

As for memory management, I'm glad that we can do this memory management ourselves. In other machines you have a heap where you can do everything and get a number back, but here you can use every single byte of the machine, even outside of the heap.

Note also that sp1 uses memory on the 'heap' whenever you declare sprites, so the number of free memory left is different even when running the game.
jordi
Member
Posts: 61
Joined: Sun Oct 28, 2018 3:35 pm

Post by jordi »

I tried to explain (it might have mistakes) Splib with a more complete game, so you could see there a LOT of concepts required to create a game:
https://github.com/jsmolina/z88dk-tutorial-sp1

I'm totally open to pull requests (to documentation, and code!)
derekfountain
Member
Posts: 121
Joined: Mon Mar 26, 2018 1:49 pm

Post by derekfountain »

Is the game project likely to build on Linux? The bas2tap in the repo appears to be an OSX version. I built a Linux version from source, but then hit "/bin/sh: 1: png2udg: not found". Not sure how to fix that.
jordi
Member
Posts: 61
Joined: Sun Oct 28, 2018 3:35 pm

Post by jordi »

derekfountain wrote:Is the game project likely to build on Linux? The bas2tap in the repo appears to be an OSX version. I built a Linux version from source, but then hit "/bin/sh: 1: png2udg: not found". Not sure how to fix that.
sure, png2udg it's a Python script:
https://github.com/jsmolina/png2sp1sprite

Just install in your system like any other
$ python setup.py install

and you'll have the required console scripts.


Regards.
derekfountain
Member
Posts: 121
Joined: Mon Mar 26, 2018 1:49 pm

Post by derekfountain »

OK, then I got:

ls *.bin
ls: cannot access '*.bin': No such file or directory
Makefile:2: recipe for target 'compile' failed

which I fixed in the makefile, and then it compiled.

Hey, that's a pretty good Pacman! I'm not sure when I'll get time to look at the code, but it's clearly another excellent SP1 example project.

I suggest you tidy up the build to it works out of the box, then maybe add a thread at Spectrum Computing's programming forum?
jordi
Member
Posts: 61
Joined: Sun Oct 28, 2018 3:35 pm

Post by jordi »

derekfountain wrote:OK, then I got:

ls *.bin
ls: cannot access '*.bin': No such file or directory
Makefile:2: recipe for target 'compile' failed

which I fixed in the makefile, and then it compiled.

Hey, that's a pretty good Pacman! I'm not sure when I'll get time to look at the code, but it's clearly another excellent SP1 example project.

I suggest you tidy up the build to it works out of the box, then maybe add a thread at Spectrum Computing's programming forum?
I'm glad you like it.
As I said in another thread, I'm missing nice sounds for this game and I'm having some difficulties, so as it's on github and open source it's also opened to any pull request :)

I'll post it on spectrum computing, good idea.
jordi
Member
Posts: 61
Joined: Sun Oct 28, 2018 3:35 pm

Post by jordi »

derekfountain wrote:OK, then I got:

ls *.bin
ls: cannot access '*.bin': No such file or directory
Makefile:2: recipe for target 'compile' failed

which I fixed in the makefile, and then it compiled.

Hey, that's a pretty good Pacman! I'm not sure when I'll get time to look at the code, but it's clearly another excellent SP1 example project.

I suggest you tidy up the build to it works out of the box, then maybe add a thread at Spectrum Computing's programming forum?
wop, right. I improved the makefile. I added such ls after I had these ls files :D

It's my second game, and I guess there are already optimizations that could be done.

I also created misifu (Alleycat demake) as open source, but code became less clean. It had tons of learning and short of time:
https://github.com/jsmolina/speccy-misifu
Timmy
Well known member
Posts: 393
Joined: Sat Mar 10, 2012 4:18 pm

Post by Timmy »

jordi wrote:I tried to explain (it might have mistakes) Splib with a more complete game, so you could see there a LOT of concepts required to create a game:
https://github.com/jsmolina/z88dk-tutorial-sp1

I'm totally open to pull requests (to documentation, and code!)
Thanks, that is very useful text!


Some other things I use:

* Masks should have at least one pixel bordering the sprite...

Use a drawing program that lets you can draw a mask for you. I use Gimp (can be done in any drawing program), and there, I can select a sprite, let it 'grow' a pixel from the currecnt selection, then I paint all of this new selection.


* How to draw a screen without too many calls

In my demo (which you've already linked to), I use the following code:

sp1_PutTiles(&levelbrect, (struct sp1_tp *)(levelb));

This lets you draw lots of UDGs on screen with sp1 (in this case 768 UDGs) in one call. The only drawback is that it uses 768*3 bytes, drawing is not very fast, and you need to set up this background buffer.


Also, as I said before, for me it's a lot of work to have to download all your work and then still have to compile it to see what you did. It's easier for me if you put your compiled result on github as well.

Maybe one day I will write more text to my tutorial code, but I am very busy working on another game right now (not Spectrum). Good luck with your second game!
jordi
Member
Posts: 61
Joined: Sun Oct 28, 2018 3:35 pm

Post by jordi »

Timmy wrote:
jordi wrote:I tried to explain (it might have mistakes) Splib with a more complete game, so you could see there a LOT of concepts required to create a game:
https://github.com/jsmolina/z88dk-tutorial-sp1

I'm totally open to pull requests (to documentation, and code!)
Thanks, that is very useful text!


Some other things I use:

* Masks should have at least one pixel bordering the sprite...

Use a drawing program that lets you can draw a mask for you. I use Gimp (can be done in any drawing program), and there, I can select a sprite, let it 'grow' a pixel from the currecnt selection, then I paint all of this new selection.


* How to draw a screen without too many calls

In my demo (which you've already linked to), I use the following code:

sp1_PutTiles(&levelbrect, (struct sp1_tp *)(levelb));

This lets you draw lots of UDGs on screen with sp1 (in this case 768 UDGs) in one call. The only drawback is that it uses 768*3 bytes, drawing is not very fast, and you need to set up this background buffer.


Also, as I said before, for me it's a lot of work to have to download all your work and then still have to compile it to see what you did. It's easier for me if you put your compiled result on github as well.

Maybe one day I will write more text to my tutorial code, but I am very busy working on another game right now (not Spectrum). Good luck with your second game!
Hi!
Yes, there's is a compiled version, you should check it there:
https://github.com/jsmolina/z88dk-tutor ... /tag/0.0.5

It's a nice idea to use PutTiles, it would be nice some also one-shot of assigning tiles to UDGs (maybe iterating like I did for the font?)
Timmy
Well known member
Posts: 393
Joined: Sat Mar 10, 2012 4:18 pm

Post by Timmy »

jordi wrote:Hi!
Yes, there's is a compiled version, you should check it there:
https://github.com/jsmolina/z88dk-tutor ... /tag/0.0.5
Thanks! I've played it for a bit, and it looks and plays nice! (I'm not very good at it.)
You should also post it on worldofspectrum too. (Not that I don't like the other sites but I really am trying to spend more time on making games. I haven't done a big release last year and now I am trying to make up for that. :) )
It's a nice idea to use PutTiles, it would be nice some also one-shot of assigning tiles to UDGs (maybe iterating like I did for the font?)
I didn't found that UDG function in the sp1 library, but I don't think the one call UDG part is very necessary. Switching screens, however, is something that I'd like to do very quickly and using only one call really helps (because PrintAt is really slow.)
Post Reply