Gameboy Development Forum

Discussion about software development for the old-school Gameboys, ranging from the "Gray brick" to Gameboy Color
(Launched in 2008)

You are not logged in.

Ads

#1 2017-01-03 07:50:56

Zalo
Member
From: Spain
Registered: 2016-07-26
Posts: 63
Website

ZGB, a little engine for the Game Boy

After 2 games I think this is more than ready for public release.

Here is the engine I have been working on during the past year. Some of its features

- 4 directional scroll that allows maps bigger than 32x32
- Sprites pool
- Animations
- Collisions, sprite vs sprite and sprite vs background
- Automatized build support
- Bank management
- Fonts and printing
- Music
- Sound effects

This is the engine that we used to create Pretty Princess' Castle Escape and Super Princess' 2092 exodus

https://github.com/Zal0/bitbitjam2016/blob/develop/bitbit3/res/marketing/screenshots/pretty.gif?raw=true   https://github.com/Zal0/gbjam2016/raw/develop/res/marketing/gifs/fly.gif?raw=true

The last month I have been working on a step by step tutorial (available on github) hoping people will gain some interest and give it a try smile

Offline

 

#2 2017-01-03 14:42:32

gbjosh
Member
From: KY
Registered: 2016-06-15
Posts: 30

Re: ZGB, a little engine for the Game Boy

Awesome! I will be checking this out when I get some free time. Thanks!

Offline

 

#3 2017-01-04 11:36:36

Mills
Member
Registered: 2012-12-21
Posts: 122

Re: ZGB, a little engine for the Game Boy

Zalo wrote:

After 2 games I think this is more than ready for public release.

Here is the engine I have been working on during the past year. Some of its features

- 4 directional scroll that allows maps bigger than 32x32
- Sprites pool
- Animations
- Collisions, sprite vs sprite and sprite vs background
- Automatized build support
- Bank management
- Fonts and printing
- Music
- Sound effects

This is the engine that we used to create Pretty Princess' Castle Escape and Super Princess' 2092 exodus

https://github.com/Zal0/bitbitjam2016/b … f?raw=true   https://github.com/Zal0/gbjam2016/raw/d … f?raw=true

The last month I have been working on a step by step tutorial (available on github) hoping people will gain some interest and give it a try smile

I could compile the sample!, working perfect!.
I was making a New game, and I'll use this smile


I realized the cpu goes 100% when updating rows (when going down or up), so this could help:

When updating a row, you can use the gbdk function like this:

Code:

set_bkg_tiles(Pos_X,Pos_Y, 20, 1, "Number of the fisrt tile of the row");

This will update the row much faster and OK, because the tiles in a row are consecutive (3,4,5,6,7,8...) in the map array.

Updating columns seems to work much faster  smile so no need to improve it.

Thanks!

Last edited by Mills (2017-01-04 11:47:30)

Offline

 

#4 2017-01-06 08:19:10

Zalo
Member
From: Spain
Registered: 2016-07-26
Posts: 63
Website

Re: ZGB, a little engine for the Game Boy

Yes, I think that should go faster

But anyways for each tile in the background I need to call the function GetTileReplacement. This function spawns enemies and change the tile in the background accordingly.


The solution I see is creating a copy of the row, call this function on each tile and update the copy and then pass the array to set_bkg_tile.

How do you measure the cpu?

Offline

 

#5 2017-01-06 10:24:45

Mills
Member
Registered: 2012-12-21
Posts: 122

Re: ZGB, a little engine for the Game Boy

Zalo wrote:

How do you measure the cpu?

Just press esc while running BGB emulator, you will see a Green bar at the right of the window, showing the cpu usage.

The ZGB code is also working well on game boy color, just had to add set_bkg_palette and set_sprite_palette functions.

Last edited by Mills (2017-01-06 10:25:04)

Offline

 

#6 2017-01-08 10:30:38

npzman
Banned
From: Australia
Registered: 2014-11-19
Posts: 197

Re: ZGB, a little engine for the Game Boy

Been a while since I was on this site

I will give it a try


Avatar is me
Twitter : @Sfeedman please follow
Join : sfeed.club my website please join

Offline

 

#7 2017-01-10 03:40:27

Mills
Member
Registered: 2012-12-21
Posts: 122

Re: ZGB, a little engine for the Game Boy

Zalo wrote:

The last month I have been working on a step by step tutorial (available on github) hoping people will gain some interest and give it a try smile

Hey Zalo, Could you include a tutorial about changing from one scene/state to another?

Also you could include non-sprite animations, i mean, animations made by changing bkg tiles, you'd just include all animation states in a special part of the big map, (or a separate mini map using the same tile data), and then use set_bkg_tiles, to set them.

Offline

 

#8 2017-01-10 03:50:18

Zalo
Member
From: Spain
Registered: 2016-07-26
Posts: 63
Website

Re: ZGB, a little engine for the Game Boy

You can change states using SetState (you can see this in the tutorial when the character gets killed by an enemy, I am changin to the current state again but it could be any other one)

Anims in the bg sounds like a good idea

Offline

 

#9 2017-01-11 23:57:21

npzman
Banned
From: Australia
Registered: 2014-11-19
Posts: 197

Re: ZGB, a little engine for the Game Boy

https://github.com/Zal0/gbjam2016
can you teach me how to compile this

step by setp


Avatar is me
Twitter : @Sfeedman please follow
Join : sfeed.club my website please join

Offline

 

#10 2017-01-12 06:04:18

Zalo
Member
From: Spain
Registered: 2016-07-26
Posts: 63
Website

Re: ZGB, a little engine for the Game Boy

Follow the steps here  to install ZGB
Then download https://github.com/Zal0/gbjam2016 and run build.bat

Offline

 

#11 2017-01-12 23:55:27

npzman
Banned
From: Australia
Registered: 2014-11-19
Posts: 197

Re: ZGB, a little engine for the Game Boy

This repo, anywhere but create an env var called ZGB_PATH pointing the common folder eg: c:\ZGB\common

How to do this step I don't have visual studio but how can I do that ?


Avatar is me
Twitter : @Sfeedman please follow
Join : sfeed.club my website please join

Offline

 

#12 2017-01-13 03:34:05

Mills
Member
Registered: 2012-12-21
Posts: 122

Re: ZGB, a little engine for the Game Boy

npzman wrote:

This repo, anywhere but create an env var called ZGB_PATH pointing the common folder eg: c:\ZGB\common

How to do this step I don't have visual studio but how can I do that ?

http://www.computerhope.com/issues/ch000549.htm

Offline

 

#13 2017-01-14 18:47:18

npzman
Banned
From: Australia
Registered: 2014-11-19
Posts: 197

Re: ZGB, a little engine for the Game Boy

how many levels I can make ?


Avatar is me
Twitter : @Sfeedman please follow
Join : sfeed.club my website please join

Offline

 

#14 2017-01-15 00:38:41

npzman
Banned
From: Australia
Registered: 2014-11-19
Posts: 197

Re: ZGB, a little engine for the Game Boy

npzman wrote:

msys/bin to your Path environment var

where do I put this in ?

and I really need help on compiling I am running windows 10

Last edited by npzman (2017-01-15 01:05:55)


Avatar is me
Twitter : @Sfeedman please follow
Join : sfeed.club my website please join

Offline

 

#15 2017-01-15 09:24:44

Mills
Member
Registered: 2012-12-21
Posts: 122

Re: ZGB, a little engine for the Game Boy

Zalo wrote:

You can change states using SetState (you can see this in the tutorial when the character gets killed by an enemy, I am changin to the current state again but it could be any other one)

Anims in the bg sounds like a good idea

Did it!.

I'm creating a puzzle game with your engine:
New Puzzle Game for Game Boy

I still have some questions:

Is there a function to know the tile at the sprite position?. For exanple, how do you know you are over stairs, so that you can climb in your princess game?

How do i use the printf function in the window? I want to show some variables on the window, but i don't know how to actívate the window, and print over it.

Thanks a lot!

Last edited by Mills (2017-01-15 09:27:50)

Offline

 

#16 2017-01-15 09:55:40

Zalo
Member
From: Spain
Registered: 2016-07-26
Posts: 63
Website

Re: ZGB, a little engine for the Game Boy

This is exciting smile

you can use GetScrollTile or GetScrollTilePtr in Scroll.h to get the tile at position x, y. X and Y here are are tile coordinates, so you probably you do >>3 to divide by eight if you are using pixel coordinates. Also be careful when using GetScrollTilePtr directly since this one will only work when the scroll bank has been selected

Better than that you can use TranslateSprite on Sprite.h that will return the collided tile. This is also explained in the [https://github.com/Zal0/ZGB#collisions]tutorial[/url]. But yeah, if you are not using collisions, then just use the ones I said above

In order to use the window you must compile a deveopment build, this is as simple as running build_debug.bat instead of build.bat. Then just check the las part of the tutorial

Let me know if you need more help

Offline

 

#17 2017-01-15 20:48:33

npzman
Banned
From: Australia
Registered: 2014-11-19
Posts: 197

Re: ZGB, a little engine for the Game Boy

Well I still need help

Zalo please make a video tutorial on setting it up


Avatar is me
Twitter : @Sfeedman please follow
Join : sfeed.club my website please join

Offline

 

#18 2017-01-16 01:49:25

Zalo
Member
From: Spain
Registered: 2016-07-26
Posts: 63
Website

Re: ZGB, a little engine for the Game Boy

Here is how to setup environment vars on Windows 8 and 10

Sorry, I just don't have time to create a video tutorial. There is all the documentation I wrote on github that I think should be enough to get things working. It still requires a basic knowledge of C and other stuff that I cannot really help too much with

Offline

 

#19 2017-01-16 02:38:56

npzman
Banned
From: Australia
Registered: 2014-11-19
Posts: 197

Re: ZGB, a little engine for the Game Boy

I am new to gb dev come on please make a video or anyone else if they can


Avatar is me
Twitter : @Sfeedman please follow
Join : sfeed.club my website please join

Offline

 

#20 2017-01-16 03:59:31

Mills
Member
Registered: 2012-12-21
Posts: 122

Re: ZGB, a little engine for the Game Boy

Zalo wrote:

There is all the documentation I wrote on github that I think should be enough to get things working. It still requires a basic knowledge of C and other stuff that I cannot really help too much with

Your tutorial is very good. But there is also a lot of info inside your princess games source, I got the printf worknig and showing variables on a window smile.

Code:

    InitWindow(0, 0, Width, Height, map, Bank);
    WY_REG = 16*8; //Had to set this
    SHOW_WIN;
    
    INIT_FONT(font, 3, PRINT_WIN);
    PRINT_POS(0, 0);
    Printf("MY_VARIABLE = %d", (int) some_variable);

I also found how to detect the tiles in whith a sprite is located:

Code:

    if(GetScrollTile((THIS->x + THIS->coll_x) >> 3, (THIS->y + THIS->coll_y) >> 3) == 23u)
        princes_state = PRINCESS_STATE_LADDER;

https://dl.dropboxusercontent.com/u/757056/GBC/bgb00001.gif

smile

Last edited by Mills (2017-01-16 04:28:18)

Offline

 

#21 2017-01-17 02:25:13

npzman
Banned
From: Australia
Registered: 2014-11-19
Posts: 197

Re: ZGB, a little engine for the Game Boy

Still I please just make a video tutorial so anyone can do it
the written tutorial isn't working for me.


Avatar is me
Twitter : @Sfeedman please follow
Join : sfeed.club my website please join

Offline

 

#22 2017-01-27 19:06:33

Mills
Member
Registered: 2012-12-21
Posts: 122

Re: ZGB, a little engine for the Game Boy

Testing how to improve the engine, I noticed "set_bkg_tiles" function from gbdk was using too much cpu, when setting just one tile, as it is used in zgb. So I decided to create an asm function to set just one tile at any position.

My asm knowledge is limited, so this might not be the best way to do it.

Code:

_Set_Tile:        
    LD    HL, #0x9800    ; map address 
    LD    (HL), #1    ; 1 = tile number to set
    ret

Added some variables to test it from C. The function set the tile at the correct position, But the tile number was not correct.

("file.s" to compile with the rest of the C code)

Code:

        .globl     _Set_Tile
        
.X        =    #0  Position X on the map
.Y        =    #0  Position Y on the map
.TILE        =    #1  Tile Number

_Set_Tile:        
    LD    HL, #0x9800 + (#.Y*32+#.X)    ; map address + position 
    LD    (HL), #.TILE    ;  tile number to set
    RET

I tested this inside the c code to emulate the ZGB functions, so I Updated a row and a column (42 tiles) every frame and captured the cpu usage bar from bgb.

Code:

    while(1) {

        for(i = 0; i < 41; i++) Set_Tile(0,0,0); //arguments not working
        wait_vbl_done();
    }

Then I compared with the original "set_bkg_tiles", setting just one tile at any position.

Code:

    while(1) {

        for(i = 0; i < 41; i++) set_bkg_tiles(3,3,1,1,20);
        wait_vbl_done();
    }

CPU usage:

https://dl.dropboxusercontent.com/u/757056/GBC/CPU.png


Now I have to pass the arguments from C "Set_Tile(int X, int Y, int Tile)" to asm, and I don't know how to do it.


Thanks smile

Last edited by Mills (2017-01-27 19:17:29)

Offline

 

#23 2017-01-27 19:46:55

AntonioND
Member
Registered: 2014-06-17
Posts: 108
Website

Re: ZGB, a little engine for the Game Boy

The arguments are pushed to the stack. If you use bgb you can take a look and use "ld hl,sp+const" to load in hl a pointer to that information, it's pretty obscure. It can be done, but it is a pain. Maybe you could just check the GBDK library for a function with the same arguments and see how they are loaded.

Anyway, you have 2 big problems:

-  "LD    HL, #0x9800 + (#.Y*32+#.X)" is going to just work for the value you assign to X and Y when compiling. You need to do all of that by hand, by using bit shifts to do the "Y*32" and additions.

- You don't check if you are in VBL or HBL: http://bgb.bircd.org/pandocs.htm#accessingvramandoam

Offline

 

#24 2017-01-28 06:17:41

Mills
Member
Registered: 2012-12-21
Posts: 122

Re: ZGB, a little engine for the Game Boy

AntonioND wrote:

Maybe you could just check the GBDK library for a function with the same arguments and see how they are loaded.

Function move_bkg(in x, int y) looks like this in asm:

Code:

_move_bkg2::
    LDA    HL,2(SP)    ; Skip return address
    LD    A,(HL+)
    LDH    (.SCX),A
    LD    A,(HL+)
    LDH    (.SCY),A
2$:
    RET

It didn't work well alone in a separate .s and a custom c code, it looks like one of the arguments is working, and the other is ignored.

EDIT: Solved the argument issue, I just had to declare like this

Code:

void move_bkg2(UINT8 x,UINT8 y);

But now the values are negative... lol

EDIT: Three it is...

Code:

_ZGB_SET_TILE:    
    
    LDA    HL,3(SP)    ; Skip return address
    PUSH BC
    DEC HL
    LD    D,#0
    LD    B,#0
    LD    A,(HL+)        ; A = First    argument(X)
    LD    E,A            ; DE = X
    LD    A,(HL+)        ; A = Second argument (Y)
    LD    C,A            ; BC = Y
    LD    A,(HL+)        ; A = Third argument (TILE)
    ; Multiply Y*32
    LD    HL,#0x0000
    ADD    HL,BC        ; HL = Y
    ADD    HL,HL        ; Y*2 
    ADD    HL,HL        ; Y*4
    ADD    HL,HL        ; Y*8
    ADD    HL,HL        ; Y*16
    ADD    HL,HL        ; Y*32
    LD    BC,#0x9800    ; BC = 9800
    ADD HL,BC        ; HL = 9800 + 32*Y
    ADD    HL,DE        ; HL = 9800 + 32*Y + X
    
    LD  (HL),A        ; Tile Number to map address

    POP BC
    RET

Declare in C like this:

Code:

void ZGB_SET_TILE(UINT8 x,UINT8 y, UINT8 tile_number);

It works well, if zgb does not scroll the bkg.

When scrolling, it updates well the tile position, but it looks like the function is not setting the correct tile number.

I'll have to check if GB is in VBL or HBL Maybe that will add the tiles correctly smile.

Last edited by Mills (2017-01-30 16:35:51)

Offline

 

#25 2017-02-18 07:34:55

Mills
Member
Registered: 2012-12-21
Posts: 122

Re: ZGB, a little engine for the Game Boy

I added some improvements to the engine:

    - Auto fade to/from white for game boy color, when changing scene.
    - BKG Map animations: Using the loaded tiles to anímate big parts of the screen changing the map (like big sprites).
    - BKG Tile animations: Loading custom tiles to anímate a specific tile repeated all over the map (making waterfall animations for example).
    - Added Carillon music player (need some fix), GBT player is still there, so you can use the one you like more.

I'd like to make a pull request on Zalo's github, but I don't really know how to do it, if someone wants to do it, i'll upload the source.

I have to fit just one more function in bank 1, so that I can keep GBT player and Carillon. GBT is vey easy to use, But carillon can produce much more complex music, so it is ok to keep both I think.

Thanks!

Last edited by Mills (2017-02-18 07:40:00)

Offline

 

Board footer

Powered by PunBB
© Copyright 2002–2005 Rickard Andersson