News:

Printed Amstrad Addict magazine announced, check it out here!

Main Menu

programming in c using cpcrslib and z88dk

Started by arnoldemu, 15:29, 19 February 10

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

arnoldemu

I wanted to define a fixed length string, and then set the last char in the string so that I could load different backgrounds.

This is the magic:


static char *String[] = "BANGEL69.BK0";

.
.
.

Filename(sizeof(Filename)-2] = 0x030+Index;


The first defines a string array.

sizeof() returns length of string including null terminator (\0 or 0 char).
So we have to subtract 2 to get to last char in string.
Then we can set it.
My games. My Games
My website with coding examples: Unofficial Amstrad WWW Resource

norecess

I hope you don't have more than 10 images to load ;)
In the end, I think it's safer+easier to use a custom bigfile-system : many packed files stored in a single file, with an index table to retrieve them - ... so in the end, you would have  Unpack( FileID, 0xC000 ) (0xc000 being any char ptr).

arnoldemu

If you use:

const unsigned short Value=13;

and then reference it in your code, the z88dk compiler actually generates a word value:

Value:
defw 13

and then in the code does this to use it:

ld hl,(_Value)

So If you want a constant value, use #define e.g.

#define Value 13

In addition, if your #define results in a constant, ensure you use enough brackets. e.g.

#define tile_width 8
#define tile_height 16
#define tile_size (tile_width>>1)*tile_height

should be written as:

#define tile_width 8
  #define tile_height 16
  #define tile_size ((tile_width>>1)*tile_height)

If you don't do this, and you use tile_size in arithmetic, then the compiler could end up generated non-optimised code.
With the extra brackets in there you force the compiler to evaluate this, realise it is constant and use more optimised code.

My games. My Games
My website with coding examples: Unofficial Amstrad WWW Resource


arnoldemu

Quote from: norecess on 22:10, 07 September 10
Good tip.  ;)
I need all the help I can to squeeze Blue Angel 69 into ram now that I've fixed the AI ;)


BTW, I haven't forgotten about that header file I was going to make for you. Been really busy recently.
My games. My Games
My website with coding examples: Unofficial Amstrad WWW Resource

arnoldemu

If you have code like this:


If (Tile<0)
{
TileGfx = 23;
}
else
{
TileGfx = 22;
}


Writing it as this will save some bytes:


TileGfx = (Tile<0) ? 23 : 22;


My games. My Games
My website with coding examples: Unofficial Amstrad WWW Resource

arnoldemu

#31
If you have code like this:


Player1Type = HUMAN;
Player2Type = HUMAN;


If you write it like this:


Player1Type = Player2Type = HUMAN;


This will also save bytes.

The generated code for this first is like this:


ld hl,HUMAN
ld (Player1Type),hl
ld hl,HUMAN
ld (Player2Type),hl


And for the second:


ld hl,HUMAN
ld (Player1Type),hl
ld (Player2Type),hl


EDIT: Note this doesn't apply to arrays. So Array[0] = Array[1] = 3; will not generate efficient code as you think ;)
My games. My Games
My website with coding examples: Unofficial Amstrad WWW Resource

norecess


arnoldemu

Quote from: norecess on 14:26, 08 September 10
Which compiler are you using?
all of this applies to z88dk version 1.9

when I've finished this project I will look at other compilers ;)
My games. My Games
My website with coding examples: Unofficial Amstrad WWW Resource

norecess

Yeah, I have been personally happy to switch from z88dk to SDCC. I globally found it less hacky and much cleaner. The only thing that really suck with SDCC is its ASM syntax.

AMSDOS

arnoldemu wrote:

all of this applies to z88dk version 1.9

when I've finished this project I will look at other compilers ;)


Compilers are wonderful, I just hope people aren't writing them for the sake of writing them though!  ???
* Using the old Amstrad Languages :D   * with the Firmware :P
* I also like to problem solve code in BASIC :)   * And type-in Type-Ins! :D

Home Computing Weekly Programs
Popular Computing Weekly Programs
Your Computer Programs
Updated Other Program Links on Profile Page (Update April 16/15 phew!)
Programs for Turbo Pascal 3

arnoldemu

Quote from: CP/M User on 09:48, 09 September 10
arnoldemu wrote:

all of this applies to z88dk version 1.9

when I've finished this project I will look at other compilers ;)


Compilers are wonderful, I just hope people aren't writing them for the sake of writing them though!  ???
I don't think so. There are 2 major compilers running on pc that will generate z80 code.
sdcc and z88dk, both have been around for a while and both have fairly good libraries.

The project is now finished! :)
Next project will start in a couple of weeks and this time I will be using asm.
My games. My Games
My website with coding examples: Unofficial Amstrad WWW Resource

AMSDOS

arnoldemu wrote:

I don't think so. There are 2 major compilers running on pc that will generate z80 code.
sdcc and z88dk, both have been around for a while and both have fairly good libraries.


Yes it seems that since I made those last remarks about people just making compilers for the sake of it, that indeed people in the communties are using them to good extent. There is perhaps also arguments now that even BASIC could be used as an alternative given a compiler is available which supports Locomotive BASIC & Sprites Alive. Other languages or game kits have emerged, I don't think Pandora was on NVG a couple of years ago ( ::) ??). Guess that's crying out for a manual, though Sprites Alive probably has more advantages and an edge over Pandora.

The project is now finished! :)
Next project will start in a couple of weeks and this time I will be using asm.


There's always one language the CPC is best at though!  ;)
* Using the old Amstrad Languages :D   * with the Firmware :P
* I also like to problem solve code in BASIC :)   * And type-in Type-Ins! :D

Home Computing Weekly Programs
Popular Computing Weekly Programs
Your Computer Programs
Updated Other Program Links on Profile Page (Update April 16/15 phew!)
Programs for Turbo Pascal 3

TFM

Quote from: CP/M User on 10:48, 08 October 10

The project is now finished! :)
Next project will start in a couple of weeks and this time I will be using asm.


There's always one language the CPC is best at though!  ;)

Indeed!  ;)
TFM of FutureSoft
Also visit the CPC and Plus users favorite OS: FutureOS - The Revolution on CPC6128 and 6128Plus

demoniak

Norecess: the links to your projets seems to be broken...

Powered by SMFPacks Menu Editor Mod