Learning C games dev for the ZXN

The Speccy's spritely young offspring. Discuss everything from FPGA to ZX
danboid
Dizzy
Posts: 62
Joined: Sun May 12, 2024 11:16 am

Learning C games dev for the ZXN

Post by danboid »

The Next has a heavy focus on NextBASIC as it primary language, as indicated by the majority of its great manual being dedicated to documenting the use of NextBASIC in great detail. That's fantastic for the retro purist who enjoys the simplicity offered by a nice modern version of BASIC with functions for doing nice looking graphics and sound.

That would surely be the obvious and easier way for me to go about writing a simple game for the Next but its not the way I want to do it. I already know the basics of C so I'd much rather attempt to write a Next game in C so as to improve my C skills which I can then mostly re-use to develop software for Linux, the Amiga, the Uzebox and just about any other platform that I'm interested in. It will be a lot more effort than using NextBASIC but I think its a much more productive use of my time.

My first ever C program and the only program I have written in C so far is IKD, a remake of the tank game from Atari's Combat for the open source Uzebox console. I kept a comprehensive diary of my slow, drawn out progress of putting that together on the Uzebox forum:

https://uzebox.org/forums/viewtopic.php?t=11136

TLDR; it took me about 2.5 years to finish. The only other contribution or help I got was with a Uzebox sound engine remake of the C64 Commando theme for the title screen, which I was happy enough with to send to Rob Hubbard and amazingly he replied and he said it was very nice and he was honoured people are still recreating his work four decades after he was ruling the SID. Doing this improved the Uzebox sound engne and its tooling.

The logical first project for me to learn how to develop using C for the Next is to port IKD to the Next. I thought it would be of benefit to the Next community if I did a similar step by step diary of me learning how to port my simple C game from the Uzebox to the Next as I did for when I learned the basics of C from scratch writing IKD, if only to document it for my own reference.

David and other members of the forum will be telling me how to do the things. It will be fun!
danboid
Dizzy
Posts: 62
Joined: Sun May 12, 2024 11:16 am

Re: Learning C games dev for the ZXN

Post by danboid »

A Youtuber by the name of NCOT technology has uploaded some great videos for learning how to develop software using C for the Next using z88dk such as this video which demonstrates how to set up z88dk



Unfortnately, z88dk is not in the Debian repos and it takes about 25 minutes to build on my laptop. On the positive side it is easy to build and install, just not quite apt get install easy.

NCOT has also uploaded this video on using Next sprites from within C:



I had trouble getting his sprite example code to run properly under CSpect but David explained to me where I was going wrong and I documented my fix in this github ticket:

https://github.com/ncot-technology/spec ... e/issues/1

My first small exercise that I want to do before I start recreating IKD for the Next is to try modifyng this simple sprite example so that the sprite images are embedded within the .nex file. What's the easiest way for me to do this?

My guess is that I will need to turn the images into C header file data but I dunno what tool might be best for doing that? Maybe there's a better way?
danboid
Dizzy
Posts: 62
Joined: Sun May 12, 2024 11:16 am

Re: Learning C games dev for the ZXN

Post by danboid »

I rewatched NCOT's sprite video and realised that he recommends two sprite tools - Jim's, which is Windows only, and he also mentions the zxnext bmp tools which unfortunately don't build for me with gcc 13.2.0.

I'm mainly a Linux user so I'd prefer to get nextbmp to build rather than have to use wine or a vm to run a Windows app under Linux.

Code: Select all

~/src/zxnext_bmp_tools$ gcc -O2 -Wall -lm -o nextbmp nextbmp.c
/usr/bin/ld: /tmp/ccxSvVXV.o: in function `c8_to_c3':
nextbmp.c:(.text+0x1ba): undefined reference to `round'
/usr/bin/ld: /tmp/ccxSvVXV.o: in function `main':
nextbmp.c:(.text.startup+0x3b0): undefined reference to `round'
/usr/bin/ld: nextbmp.c:(.text.startup+0x3cf): undefined reference to `round'
/usr/bin/ld: nextbmp.c:(.text.startup+0x3ec): undefined reference to `round'
/usr/bin/ld: nextbmp.c:(.text.startup+0x7f2): undefined reference to `round'
/usr/bin/ld: /tmp/ccxSvVXV.o:nextbmp.c:(.text.startup+0x840): more undefined references to `round' follow
collect2: error: ld returned 1 exit status

https://github.com/stefanbylund/zxnext_ ... s/issues/3
dfzx
Manic Miner
Posts: 705
Joined: Mon Nov 13, 2017 6:55 pm
Location: New Forest, UK
Contact:

Re: Learning C games dev for the ZXN

Post by dfzx »

Put the math library link flag at the end of the line:

Code: Select all

gcc -O2 -Wall -o nextbmp nextbmp.c -lm
Derek Fountain, author of the ZX Spectrum C Programmer's Getting Started Guide and various open source games, hardware and other projects, including an IF1 and ZX Microdrive emulator.
Timmy
Manic Miner
Posts: 263
Joined: Sat Apr 23, 2022 7:13 pm
Location: The Netherlands

Re: Learning C games dev for the ZXN

Post by Timmy »

danboid wrote: Fri May 17, 2024 1:03 am My guess is that I will need to turn the images into C header file data but I dunno what tool might be best for doing that? Maybe there's a better way?
I usually write my own tool converting image data into DEFB.

But I understand there is also an assembly directive called INCBIN "filename.dat" that can insert data as is. (Of course you'd still have to convert sprites yourself somehow.)
Unfortnately, z88dk is not in the Debian repos and it takes about 25 minutes to build on my laptop. On the positive side it is easy to build and install, just not quite apt get install easy.
Well, z88dk is being actively developed, and distros would rather like long term fixed versions before they are put into apt or other repositories. So there's always been this disconnect between active applications and distributions.

As for installation, it shouldn't be that hard nowadays since I've wrote a step-by-step list in the z88dk forums how to achieve that. Building takes a bit of time but shouldn't be 25 minutes?
dfzx
Manic Miner
Posts: 705
Joined: Mon Nov 13, 2017 6:55 pm
Location: New Forest, UK
Contact:

Re: Learning C games dev for the ZXN

Post by dfzx »

Timmy wrote: Fri May 17, 2024 9:54 am Building takes a bit of time but shouldn't be 25 minutes?
Building z88dk on my old (around 12 years old) desktop takes about 15 minutes. I could believe it takes 25 minutes on a modest laptop. But you only have to do it once in a while. :)
Derek Fountain, author of the ZX Spectrum C Programmer's Getting Started Guide and various open source games, hardware and other projects, including an IF1 and ZX Microdrive emulator.
danboid
Dizzy
Posts: 62
Joined: Sun May 12, 2024 11:16 am

Re: Learning C games dev for the ZXN

Post by danboid »

dfzx wrote: Fri May 17, 2024 8:24 am Put the math library link flag at the end of the line:

Code: Select all

gcc -O2 -Wall -o nextbmp nextbmp.c -lm
Thanks dfzx, that's fixed it!

The build command in the nextbmp README doesn't work but I've opened a gh ticket. I can submit a PR to fix that now.
danboid
Dizzy
Posts: 62
Joined: Sun May 12, 2024 11:16 am

Re: Learning C games dev for the ZXN

Post by danboid »

Timmy wrote: Fri May 17, 2024 9:54 am I usually write my own tool converting image data into DEFB.

But I understand there is also an assembly directive called INCBIN "filename.dat" that can insert data as is. (Of course you'd still have to convert sprites yourself somehow.)



Well, z88dk is being actively developed, and distros would rather like long term fixed versions before they are put into apt or other repositories. So there's always been this disconnect between active applications and distributions.

As for installation, it shouldn't be that hard nowadays since I've wrote a step-by-step list in the z88dk forums how to achieve that. Building takes a bit of time but shouldn't be 25 minutes?

Hi Timmy

As I explained, I'm a newb C programmer so I will in most cases be doing things the easiest way and not the fastest, smallest or smartest way with this initial Next game, which is also my second ever C project.

Have you uploaded the code to your DEFB image converter tool anywhere? Hopefully its written in C and not z80 so that I might have a chance of understanding how it works.

Is z88dk a pretty new project then? I expect it will make its way into the Deban repos if it gets enough demand and use.
danboid
Dizzy
Posts: 62
Joined: Sun May 12, 2024 11:16 am

Re: Learning C games dev for the ZXN

Post by danboid »

dfzx wrote: Fri May 17, 2024 9:57 am Building z88dk on my old (around 12 years old) desktop takes about 15 minutes. I could believe it takes 25 minutes on a modest laptop. But you only have to do it once in a while. :)
You're right, I don't think I will have to update or rebuild z88dk very often so its not a big deal.
danboid
Dizzy
Posts: 62
Joined: Sun May 12, 2024 11:16 am

Re: Learning C games dev for the ZXN

Post by danboid »

I forgot to request something in my first post in this topic.

I know that we will have many very experienced C developers active on this forum. I bet a lot of the experienced C coders could port my game to the Next in an afternoon or over a weekend but they most likely wouldn't be writing all of the notes that I intend to be posting as replies to this topic as I learn step by step how to do all of the stuff required to port my game to the ZXN.

If someone does port my game before I manage to do it, I might lose interest in doing it myself and the community will lose out on having my notes which would otherwise have been written and which might be useful for anyone else wanting to write Next games using C.

Please be patient with me. Its only a small and simple 2D game but I expect this will take me at least several weeks if not months to finish because I'm still a beginner. I'm not always in the mood for coding, especially in the summer when I should be outside.
User avatar
clebin
Manic Miner
Posts: 991
Joined: Thu Jun 25, 2020 1:06 pm
Location: Vale of Glamorgan
Contact:

Re: Learning C games dev for the ZXN

Post by clebin »

danboid wrote: Fri May 17, 2024 1:03 am A Youtuber by the name of NCOT technology has uploaded some great videos for learning how to develop software using C for the Next using z88dk such as this video which demonstrates how to set up z88dk
That sprites one looks like an incredibly helpful video which I'll definitely be watching. I've found getting the right information on z88dk for Next is not easy. You may have found the same.

The biggest problem is the 'classic' vs 'newlib' situation. Half the libraries, tutorials, documentation, forums posts etc. are for classic and the other half are for newlib. Each library has its pros and cons but newlib is officially a dead end. Or at least semi-officially to add to the confusion. So I've ended up ping-ponging between the two. It's a total mess unfortunately. There's no nicer way to put it.

So, interestingly this video uses newlib as does the other Next stuff I've read. It seems that newlib has become the de facto standard for Next development so far, so from what I can see it makes sense to keep going down that avenue. Unless anyone can make any more sense of it than me...
danboid
Dizzy
Posts: 62
Joined: Sun May 12, 2024 11:16 am

Re: Learning C games dev for the ZXN

Post by danboid »

Thanks for that insight clebin. I have not done enough coding on my XBP yet to have even read about classic vs newlib. In fact I've not written a single line of code for the Next yet. David C hasn't mentioned this division to me so far, probably because he hasn't had to, yet? I've been giving him more than enough questions to deal without me even being aware of newlib vs classic option or having started to write any code.

I can't think of a single platform where there's only one way of doing things unless that platform only has one user but I understand community and software fragmentation issues. I'd be amazed if the Next had magically escaped peoples opinions of a best way to do anything.
User avatar
Cheez26
Microbot
Posts: 124
Joined: Sat May 04, 2024 2:36 am
Location: Midwestern United States
Contact:

Re: Learning C games dev for the ZXN

Post by Cheez26 »

Given that the Next is basically an upgraded 128k with an extension port for Pi Zero and a SD card slot, I would think you would start with developing for the ZX Spectrum +2 or whatnot, y'know?

Then again, I prefer Boriel BASIC for making stuff from scratch. Otherwise, I would use a game creation tool. I know one person is working on a Godot-based ZX Spectrum game creation solution, and that also takes z88dk iirc.
Chelsea E., a Speccy fan from the U.S.
Also a musician and a beginning games developer.
🏳️‍⚧️ p r i d e 🏳️‍🌈
danboid
Dizzy
Posts: 62
Joined: Sun May 12, 2024 11:16 am

Re: Learning C games dev for the ZXN

Post by danboid »

I have no intent of trying to write for Speccy's earlier than the next. I know colour clash etc has its devout fans but I want to use the Next's improved graphics capabilities in my next game, not in this. The graphics in IKD could easily be done on a classic Speccy, I have no doubt, but srill I'm really only interested in learning to target the Next via C.

I have used Godot before. Its great and that would help me writing an original game but Godot isn't any use to me with this simple port that I already have the graphics and most of the code for, pretty much. I'm quite happy with just using Kate and its ability for me to create keyboard shortcuts to run CSPect or zesarux to test my builds.
Timmy
Manic Miner
Posts: 263
Joined: Sat Apr 23, 2022 7:13 pm
Location: The Netherlands

Re: Learning C games dev for the ZXN

Post by Timmy »

danboid wrote: Fri May 17, 2024 12:20 pm Have you uploaded the code to your DEFB image converter tool anywhere? Hopefully its written in C and not z80 so that I might have a chance of understanding how it works.
It's not written in C, because I used languages that can easily access pixels from images. C is not very useful there.

To be honest it's also only for ZX, MSX and CPC only, I haven't even touched ZXN yet, and a good conversion also depends on the screen mode you are using. So it's not much for you right now.
Is z88dk a pretty new project then? I expect it will make its way into the Deban repos if it gets enough demand and use.
It's very old by now, I made games for 10+ years now. It's just being updated like daily or weekly (but mostly for more obscure platforms.) So for us there won't be much need to update often.
danboid
Dizzy
Posts: 62
Joined: Sun May 12, 2024 11:16 am

Re: Learning C games dev for the ZXN

Post by danboid »

I've not watched these videos yet but I'll be watching them soon. I thought I might as well post them here now.

Joystick and Keyboard routines on the Spectrum Next using Z88DK and C




I've not watched this copper vid yet but I have tried building and running its code. I got it build and run but it didn't seem to be working correctly under CSpect for me. I need to try running it on my XBP to see if it works on real hardware.

https://github.com/ncot-technology/spec ... r/issues/1

Amiga style demo effects on a ZX Spectrum Next

dfzx
Manic Miner
Posts: 705
Joined: Mon Nov 13, 2017 6:55 pm
Location: New Forest, UK
Contact:

Re: Learning C games dev for the ZXN

Post by dfzx »

clebin wrote: Fri May 17, 2024 1:24 pm The biggest problem is the 'classic' vs 'newlib' situation. Half the libraries, tutorials, documentation, forums posts etc. are for classic and the other half are for newlib. Each library has its pros and cons but newlib is officially a dead end. Or at least semi-officially to add to the confusion. So I've ended up ping-ponging between the two. It's a total mess unfortunately. There's no nicer way to put it.
That's true, unfortunately. I'd settled on newlib for 48K development, but the wisdom on the z88dk forum is that the newlib content has now been (mostly) merged into classic, and classic is getting the attention, so classic is the way to go. But I'm not sure about for the Next.

Pinging @Alcoholics Anonymous just in case he's listening and can offer an opinion on which library to use for Next z88dk development.
Derek Fountain, author of the ZX Spectrum C Programmer's Getting Started Guide and various open source games, hardware and other projects, including an IF1 and ZX Microdrive emulator.
danboid
Dizzy
Posts: 62
Joined: Sun May 12, 2024 11:16 am

Re: Learning C games dev for the ZXN

Post by danboid »

User avatar
Cheez26
Microbot
Posts: 124
Joined: Sat May 04, 2024 2:36 am
Location: Midwestern United States
Contact:

Re: Learning C games dev for the ZXN

Post by Cheez26 »

@danboid Believe it or not, Celeste Classic is one of my favorite games in recent history. I wish I could play the Speccy Next version, but...

1. I don't even have a N-GO yet.
2. ZEsarUX is kinda buggy and idk how to set it to emulate Next anyway
3. There's almost no other Speccy Next emulators besides ZEsarUX.

Of course, that doesn't stop me from offering some advice on what your first game could be. I think you should start with a platformer like Celeste or I Wanna Be The Guy, but that's probably just me.

Good luck on your dev journey.
Chelsea E., a Speccy fan from the U.S.
Also a musician and a beginning games developer.
🏳️‍⚧️ p r i d e 🏳️‍🌈
User avatar
PeterJ
Site Admin
Posts: 6953
Joined: Thu Nov 09, 2017 7:19 pm
Location: Surrey, UK

Re: Learning C games dev for the ZXN

Post by PeterJ »

Cheez26 wrote: Sat May 18, 2024 5:07 pm There's almost no other Speccy Next emulators besides ZEsarUX.
CSpect is the best Next emulator. It takes a while to get into (take a look at some of the emulator threads), but it's worth the effort.

I believe the OP has chosen his first game.
User avatar
Cheez26
Microbot
Posts: 124
Joined: Sat May 04, 2024 2:36 am
Location: Midwestern United States
Contact:

Re: Learning C games dev for the ZXN

Post by Cheez26 »

@PeterJ Thanks for reminding me about CSpect, but I did say almost no other emulators.

But yeah you're probably right about OP already choosing a game to develop.
Chelsea E., a Speccy fan from the U.S.
Also a musician and a beginning games developer.
🏳️‍⚧️ p r i d e 🏳️‍🌈
User avatar
PeterJ
Site Admin
Posts: 6953
Joined: Thu Nov 09, 2017 7:19 pm
Location: Surrey, UK

Re: Learning C games dev for the ZXN

Post by PeterJ »

Cheez26 wrote: Sat May 18, 2024 5:31 pm @PeterJ Thanks for reminding me about CSpect, but I did say almost no other emulators.
I wasn't trying to be pedantic, just trying to help as it's the defacto Next emulator. You can also get CSpect to run on Linux with Mono (see one of my earlier posts). This was some time back though so things may have changed.
danboid
Dizzy
Posts: 62
Joined: Sun May 12, 2024 11:16 am

Re: Learning C games dev for the ZXN

Post by danboid »

CSpect still runs fine under Linux using mono. I've tested it recently with Debian.

Whilst it may not be as accurate as CSpect, I'm not sure, I like zesarux because its open source and it uses SDL, which most Linux users will already have installed as opposed to mono, which they will probably won't and its not in the Debian or Ubuntu repos and its super bloaty. CSpect is the only mono app I use under Linux.

It would be cool if Mike could ditch mono and port CSpect to SDL like almost every other portable emulator (MAME, MESS, FinalBurn, EUAE, vice, cuzebox et al) and game does. Pretty please Mike! :D
User avatar
clebin
Manic Miner
Posts: 991
Joined: Thu Jun 25, 2020 1:06 pm
Location: Vale of Glamorgan
Contact:

Re: Learning C games dev for the ZXN

Post by clebin »

danboid wrote: Sat May 18, 2024 5:56 pm CSpect still runs fine under Linux using mono. I've tested it recently with Debian.

Whilst it may not be as accurate as CSpect, I'm not sure, I like zesarux because its open source and it uses SDL, which most Linux users will already have installed as opposed to mono, which they will probably won't and its not in the Debian or Ubuntu repos and its super bloaty. CSpect is the only mono app I use under Linux.

It would be cool if Mike could ditch mono and port CSpect to SDL like almost every other portable emulator (MAME, MESS, FinalBurn, EUAE, vice, cuzebox et al) and game does. Pretty please Mike! :D
I don't know, I really like Mono if done properly. I know it installs a load of packages, but HD space is cheap. Unfortunately CSpect is just very rough and I suspect not much tested outside of Window. Maybe the Linux version is better than Mac? On the Mac the window resizing is completely broken and it's quite crashy. Rather than start again, I just hope Mike continues to make it better and gives the Mac version some love. Right now I only use it very reluctantly.
danboid
Dizzy
Posts: 62
Joined: Sun May 12, 2024 11:16 am

Re: Learning C games dev for the ZXN

Post by danboid »

Does anyone on this forum have experience in writing games in C on the Amiga and writing games in C for the ZXN? I'd like to know how the two compare.

I expect it should be quite a bit easier to write a game in C on the Amiga today with its larger amounts of RAM and faster CPU combined with stuff like Amiga gcc or whatever the best Amiga OS native C compiler is/was (SAS?).

I realise of course that the proper way to write an Amiga game is and was in m68k asm. If I wanted to write an Amiga game I'd most likely use the Scorpion engine but thats relatively new and some of you wouldn't have had that option in the 80's and 90's.

It might be possible to write a game engine that targets the Next and (AGA) Amigas or maybe they could add Next support to Scorpion?
Post Reply