andreasjhkarlsson / gbdk-n

gbdk libraries updated for newer versions of sdcc
174 stars 25 forks source link

gotoxy #19

Open konsumer opened 4 years ago

konsumer commented 4 years ago

It seems like gotoxy() isn't working.

I compile this:

#include <stdio.h>
#include <gb/gb.h>
#include <gb/console.h>

// clear the screen
void cls (void) NONBANKED;

void main () {
    DISPLAY_ON;
    cls();
    gotoxy(11, 8);
    printf("O HAI!");
}

with this:

BIN=/opt/gbdk/bin
OBJ=./obj

build:
    mkdir -p $(OBJ)
    $(BIN)/gbdk-n-compile.sh demo.c -o $(OBJ)/demo.rel
    $(BIN)/gbdk-n-link.sh $(OBJ)/demo.rel -o $(OBJ)/demo.ihx
    $(BIN)/gbdk-n-make-rom.sh $(OBJ)/demo.ihx demo.gb

clean:
    rm -rf $(OBJ)
    rm -f demo.gb

and I get this:

Screenshot from 2020-03-13 22-41-33

Am I doing it wrong?

Info:

I'm running in this docker environment.

Debian GNU/Linux 10 (buster) SDCC : 3.8.0 #10562 (Linux)

JL2210 commented 4 years ago

I think gotoxy makes the top left corner of the screen those coordinates and then printf prints at the top left.

konsumer commented 4 years ago

I think gotoxy makes the top left corner of the screen those coordinates and then printf prints at the top left.

Hmm, not in other versions of gbdk. Normally, this puts the text in the middle of the screen.

If the API is meant to be changed from the original, how would you accomplish centering this text on the screen?

JL2210 commented 4 years ago

I don't know, then. But also, older versions of gbdk have issues with multiplying 1 by 32, so it could've been a bug. Not sure.

konsumer commented 4 years ago

I don't know, then. But also, older versions of gbdk have issues with multiplying 1 by 32, so it could've been a bug. Not sure.

I'm not sure I understand. So you are saying, first that it works differently than documented, and also differently than gotoxy works in standard C library? and also that the correct behavior in the non -n version of gbdk is a bug? That seriously makes no sense.

JL2210 commented 4 years ago

Sorry, you're right. I don't know what I was thinking. I wonder why this is. As far as I know, the library hasn't been changed that much from the official release of GBDK.

konsumer commented 4 years ago

No prob.

Basically, I'm trying to figure out if I should switch to standard gbdk in my fancy docker-based gameboy dev-environment. I like that gbdk-n uses modern SDCC from the OS, rather than including a copy of an old SDCC that needs to be compiled. I wrote some code that stopped working correctly, because gotoxy doesn't work the same, which brought me here to make an issue. I think I'll make the switch, but I appreciate you taking a look.

JL2210 commented 4 years ago

It might be worthwhile trying to fix the issue. The newer SDCC seems to have better optimizations and support for newer features of C.

JL2210 commented 4 years ago

I might've found the source; see https://github.com/andreasjhkarlsson/gbdk-n/commit/da61fac4066a9db3f6a208aa78526d49bd21990e#commitcomment-38708912 (and I'll go ahead and process it with maccer and create a pull request with the output if it changes anything).

JL2210 commented 4 years ago

wasn't the issue but see new https://github.com/Zal0/gbdk-2020

basxto commented 4 years ago

https://github.com/andreasjhkarlsson/gbdk-n/blob/master/libc/font.s#L514:

    .area   _CODE
    ; Support routines
_gotoxy::           ; Banked
    lda hl,.BANKOV(sp)
    ld  a,(hl+)
    ld  (.curx),a
    ld  a,(hl)
    ld  (.cury),a
    ret

gotoxy() is quite short, it just sets two variables and nothing more. I stepped through it in BGB and it sets them to the correct values.

So I guess something else is broken and ignore those variables.

JL2210 commented 4 years ago

I have reason to believe it's caused by the org directive. There's a Game Boy development Discord channel that I can send the link to if you're interested.

basxto commented 4 years ago
gotoxy(11, 8);
setchar('h');

This one works without a problem.

void main () {
    DISPLAY_ON;
    cls();
    printf(" ");
    gotoxy(11, 8);
    printf("O HAI!");
}

gotoxy totally works if there was a print before. What’s an org directive?

basxto commented 4 years ago
#include <gb/drawing.h>
void main () {
    DISPLAY_ON;
    cls();
    gotogxy(11, 8);
    gprintf("O HAI!");
}

I think this is what you are looking for? I never worked with these functions, I always write my own print functions. printf is from sdcc, gotoxy, gotogxy and gprintf are from gdbk.

konsumer commented 4 years ago

@baxto that doesn't work. In my docker-env, with gbdk-n I get this:

mkdir -p ./obj
/opt/gbdk/bin/gbdk-n-compile.sh demo.c -o ./obj/demo.rel
+ sdcc -mgbz80 --no-std-crt0 -I /opt/gbdk/bin/../include -I /opt/gbdk/bin/../include/asm -c demo.c -o ./obj/demo.rel
demo.c:5: warning 112: function 'cls' implicit declaration
demo.c:4: error 20: Undefined identifier 'DISPLAY_ON'
make: *** [Makefile:6: build] Error 1

If I compile the code at the top of this issue, it doesn't error, it just doesn't work right.

Screen Shot 2020-05-08 at 3 10 31 AM
konsumer commented 4 years ago

This seems to work:

#include <gb/gb.h>
#include <gb/drawing.h>

void main () {
    gotogxy(7, 8);
    gprintf("O HAI!");
}
Screen Shot 2020-05-08 at 5 21 59 AM

I could have sworn that the other code worked on an old version of gbdk, but maybe not. For my own project, I'm using a tile-font and custom goto/print anyway, like you, so I suppose it doesn't matter.

JL2210 commented 4 years ago

It would've worked in old versions if GBDK. The old GBDK had its own printf, but the printf in GBDK-n is from SDCC.

basxto commented 3 years ago

but the printf in GBDK-n is from SDCC.

That is only partially true. printf() relies on putchar() and that is implemented by gbdk-n.