lronaldo / cpctelera

Astonishingly fast Amstrad CPC game engine for C developers
http://lronaldo.github.io/cpctelera/
GNU Lesser General Public License v3.0
229 stars 54 forks source link

Drawing strings in double buffer & stack relocated #167

Closed DjPoke closed 1 year ago

DjPoke commented 1 year ago

Hi,

I've tried to draw strings while in double buffer mode. If the stack is relocated, the strings are not drawn. Only a line is drawn.

lronaldo commented 1 year ago

Hi @DjPoke

This explanation of a problem does not give enough information to even understand what you are exactly doing. In fact, there are several different drawString routines, which are different. For any problem, a full report of the circumstances is required. Otherwise, it is impossible to analyse. The code and execution results are mandatory to have something to start analysing.

With respect to the stack, there is no relation at all with draw string routines, other than the relation any routine has. There is no reason for a change in the stack location to affect drawString routines. The only problems I can imagine are undefined behaviour problems due to failing to fulfill usage and/or parameter requirements of the routines. Another posibility, that would produce same efects whereever you draw, is not having the BASIC ROM configured. If you happen to launch two instances of WinAPE at the same time, the second instance corrupts WinAPE .ini file producing such results as having no ROMs configured. This could go unnoticed if you are launching snapshots instead of CDTs or DSKs, as Snapshots do not require the machine to start up. However, as there is no ROM, there are also no character definitions.

Please, revise your code taking into account all details of the functions you are using. The documentation of the functions explains all preconditions and circumstances that you need to fulfill not to produce undefined behaviour. Be sure that your fulfill all of them.

DjPoke commented 1 year ago

Hi Francisco,

I use WinCPC as emulator, with French BASIC & Bios roms configured by my own. I don’t know why, when i relocate stack at #0400, only the texts are replaced by a line. (of the same color as the text should be) As you say, chars are surely destroyed, or Something like that.

I suppose the firmware copy ROM chars to RAM, to use them without the Firmware activated. May be i destroy them by relocating everything : My program is located at #1000, with a music at #0800 and sfx at #0c00 And i use a double buffer, swaping from #8000 to #c000, alternatively.

I suppose chars are store somewhere i destroy them. (may be between #8000 and #bfff) What is strange is that they are only destroyed after the stack relocation : cpct_setStackLocation((u8*)0x0400);

I’ll try other stuffs…

Thanks a lot Best Regards Bruno

lronaldo commented 1 year ago

As I previously said, the first thing you should do is to carefully read documentation of the functions you use. drawString functions clearly state that they read from the lower ROM to draw the characters, and so their code and data should be above 0x4000 in memory to not be shadowed by ROM when it gets activated.

If you put your stack at 0x400 you are also violating the conditions, as the function activates the ROM and the stack gets shadowed. From that moment onwards, whatever happens is undefined. In fact, the most probably outcome is a crash, and you may have been "lucky" that the function even returns. Moreover, if your program is located at 0x1000, you are directly violating the precondition of code and data being above 0x4000 to use drawString. Hence, you are experience undefined behaviour.

If you want to properly use CPCtelera library functions, making assumptions about how the functions should be working is similar to playing russian roulette. Every now and then you will be shooting yourself in the head. The appropriate way to go is to drink a glass full of patience and carefully read every detail in the documentation to understand (and not assume) how things work.

CPCtelera functions, in fact, are designed not to use firmware at all. There are only a few exceptions. The others are completely independent from firmware. Therefore, assumptions as "firmware copies ROM chars to RAM" are completely wrong in the context of using CPCtelera library functions, unless you know exactly all the details of what your are doing, and you are actively using firmware on your own.

If you still want to put your code and stack there, you will have to modify drawString to read from RAM instead of ROM, and include character definitions in your binary. It's an easy task to do, and I have videos on my youtube channel explaining it step by step. If that's what you want, you can go there.

Follow recommendations and refrain from assuming things. Carefully read documentation and you will prevent yourself from wasting your time and gaininig frustration. Comprehension first, action last.