Dynamic Credits Screen [Tutorial]

Discussion in 'Engineering & Reverse Engineering' started by Pacca, Apr 17, 2018.

  1. Pacca


    ASM amateur Member
    Labyrinth Zone
    Sonic 2 CD Remix
    I finally got around to centralizing all the main code for my credits screen, so that it's portable enough to release! It reads text from a raw .txt file, so editing it is easy; no more fiddling around with mappings or a spider web of macros required! The screen does all the work for you, so you can do what's important; credit the people who helped you along the way :3

    Here's a demo rom that doubles as the credits for this very tutorial :v: The entire thing was built on top of Hitaxas' Splash Screen, which handles the screen clearing, background, and other stuff like that.

    Keep in mind that this guide is only for the Sonic 2 GitHub Disassembly! It uses a lot of its' labels, constants, and macros, so converting it into something more universal seemed like it would be a pain. So for now, no other games/disassemblies are supported.

    Implementing the main code

    For starters, download this. It includes the main assets required for it to work. copy the contents of the zip file into your disassembly.

    Next, copy the following into your disassembly. It could technically go anywhere, but I'd recommend putting it near the end of the rom, before the "end of 'ROM'" line.

    Code (Text):
    1. CreditsCustom:
    2.     INCLUDE "CreditsScreen/CreditsScreen.asm"; Credits Screen Code
    Now search for "s2.constants.asm". below that line, add this:

    Code (Text):
    1.     include "CreditsScreen/constants.asm"
    Next, search for "GameModesArray:". You should find this:

    Code (Text):
    1. ; loc_3A2:
    2. GameModesArray: ;;
    3. GameMode_SegaScreen:    bra.w    SegaScreen        ; SEGA screen mode
    4. GameMode_TitleScreen:    bra.w    TitleScreen        ; Title screen mode
    5. GameMode_Demo:        bra.w    Level            ; Demo mode
    6. GameMode_Level:        bra.w    Level            ; Zone play mode
    7. GameMode_SpecialStage:    bra.w    SpecialStage        ; Special stage play mode
    8. GameMode_ContinueScreen:bra.w    ContinueScreen        ; Continue mode
    9. GameMode_2PResults:    bra.w    TwoPlayerResults    ; 2P results mode
    10. GameMode_2PLevelSelect:    bra.w    LevelSelectMenu2P    ; 2P level select mode
    11. GameMode_EndingSequence:bra.w    JmpTo_EndingSequence    ; End sequence mode
    12. GameMode_OptionsMenu:    bra.w    OptionsMenu        ; Options mode
    13. GameMode_LevelSelect:    bra.w    LevelSelectMenu        ; Level select mode
    Add the following line to the end of it:

    Code (Text):
    1. GameMode_CustomCredits:    bra.w    CustomCredits
    Then scroll down to "LevelSelectMenu:". Copy this code after all the "=" characters, but before the ">" characters:

    Code (Text):
    1. CustomCredits:
    2.     jmp    CreditScreen_Init
    Now we need to add the new text object to the main object pointers list. Search for "ObjPtr_RingPrize:". You should find this:

    Code (Text):
    1. ObjPtr_RingPrize:    dc.l ObjDC    ; Ring prize from Casino Night Zone
    After that, insert this line:

    Code (Text):
    1. ObjPtr_TextChar:        dc.l ObjTextChar    ; Credits Text
    And with that, the Credits screen is now technically installed. Of course, there's no way to access it, though...


    As it currently stands, the old, hardcoded credits screen still runs after you beat the game! Lets' fix that:

    Search for "EndgameCredits:". A ways below that should be this code:

    Code (Text):
    1.  ; Bug: The '+4' shouldn't be here; clearRAM accidentally clears an additional 4 bytes
    2.  clearRAM Horiz_Scroll_Buf,Horiz_Scroll_Buf_End+4
    below that, paste this:

    Code (Text):
    1. st (Level_Inactive_flag).w
    2.    move.b    #GameModeID_CreditsCustom,(Game_Mode).w ; => CustomCredits
    3.     rts
    And that's it! The credits now appear at the end of the game!


    Now, you can see the credits screen; but what's the point in crediting people if very few players will make it to the end? Heck, maybe your making a demo, or removing Death Egg Zone and the end cutscene entirely! How will people see the credits then? Fear no more, for this section will add the credits to the title screen!

    First, download this file and replace the old one in "mappings/sprite". Then, download this file and replace the old one in "art/nemesis". These new graphics will be used for the credits title screen menu.

    Now we need to edit the menu object itself. Search of "Obj0f:". Then find this line below it:

    Code (Text):
    1. move.w    #make_art_tile(ArtTile_ArtKos_LevelArt,0,0),art_tile(a0)
    Replace that line with this:

    Code (Text):
    1.     move.w    #make_art_tile(ArtTile_ArtNem_Player1VS2,0,0),art_tile(a0)    ;changed to make mappings edits easier
    Now that the graphics are fixed, we need to change some values to add our new option. Go down a bit further, and find this line:

    Code (Text):
    1.     move.b    #2,d2
    Change the 2 to a 3. Then find this line:

    Code (Text):
    1.     cmpi.b    #3,d2
    And replace the 3 with a 4.

    Finally, search for "TitleScreen_ChoseOptions:". Add this code just under the label:

    Code (Text):
    1.  subq.b #1,d0
    2.  bne.s TitleScreen_Option4
    And add this code before all the "=" signs:

    Code (Text):
    1. ;Pacguys' custom credits screen
    2. TitleScreen_Option4:
    3.     move.b    #GameModeID_CreditsCustom,(Game_Mode).w ; => Custom Credits screen
    4.     move.b    #0,(Options_menu_box).w
    5.     rts
    And that's it! Editing the credits is extremely easy; just open up Credits.txt and start typing! There are some tips at the end of the file, to help you figure out some of the quirks of the Credits screen (I highly recommend reading them).

    Of course, I can tell some of you aren't gonna be satisfied with just that, so here's some optional things you can do:

    Use your favorite mappings editor to load CreditsScreen/Alphabet.asm and art/nemesis/Credit Text.bin . This will give you the ability to change the art used by the Character object. Each mapping frame corresponds to a character in the Microsoft ANSI character set; if you want to add more characters, number, lowercase letters, etc., just find the appropriate mapping frame (the hex numbers on the Wikipedia page correspond to the mapping frame), and add some new font art in.

    Maybe your unsatisfied with the control characters I used. Maybe your name has a dollar sign in it; or maybe you like to end every post with a ~. Whatever the case, this can be easily fixed. Open CreditsScreen.asm and search for "CustomCredits_readChars:". You should see the following:
    Code (Text):
    1. ;this section finds control characters, and responds accordingly
    2. ;If you want to make your own control character, just add a check here!
    3.     cmp.b    #$A,d2    ;is d2 a carriage return character (an actual ANSI control character for the end of a line)?
    4.     beq.s    CustomCredits_readChars_endloop    ;if so, end loop
    5.     cmp.b    #$24,d2    ;is d2 a '$' (control code defined by PACGUY)?
    6.     beq.s    CustomCredits_readChars_setcolorflag    ;if so, set color flag and adjust header text
    7.     cmp.b    #$7E,d2    ;is d2 a '~' (control code defined by PACGUY)?
    8.     beq.s    CustomCredits_endoffile    ;if so, set as end of file (prevents further text reading)
    10.     bsr.s    Hitaxas_splash_spawn_credits_object    ;otherwise, spawn a character/letter/symbol object
    11.     bra.s    CustomCredits_readChars    ;continue looping inside character read loop
    These are where control characters are defined. The first one should never be touched; it's for an actual ANSI control character, which determines where the end of a line is. The others, however, can be freely changed; the hex values correspond to Microsoft ANSI characters, so you can replace them with what you like. You could even add your own custom control characters like so:
    Code (Text):
    1.     cmp.b    #$7,d2    ;is d2 a 'bel' character?
    2.     beq.s    CustomCredits_readChars_playRingSound    ;if so, run a routine that makes a ring sound :3
    As you've probably seen by my videos, the credits screen in Sonic 2 CD Remix spawns the players, and lets them run amuck while the credits are going. It might not look quite as professional, but its' fun! The code for this was intentionally left in, but some things still need to be changed for it to work. Open up CreditsScreen.asm and find this line:
    Code (Text):
    1.     ;comment out this line if you want Sonic and/or Tails to spawn!
    2.     bra.s    CustomCredits_skipplayers
    It should be self explanatory. Now, that'll spawn the players, and some walls and a floor. However, there will be some odd issues. To fix them, open up s2.asm and search for "RunObjects:". You should see this:
    Code (Text):
    1.     cmpi.b    #GameModeID_Demo,(Game_Mode).w    ; demo mode?
    2.     beq.s    +    ; if in a level in a demo, branch
    Below that, insert this:
    Code (Text):
    1.     cmpi.b    #GameModeID_CreditsCustom,(Game_Mode).w    ; are we in Credits mode?
    2.     beq.s    +    ; if in PACGUYs' custom credits mode, branch
    This will fix issues with Tails' Tails and the Skidding/Spindash dust.

    Yes! You can go all out and make your own mini game out of the credits! This will assume you've already setup Sonic and Tails to spawn (hint hint, it's the spoiler above this one)! To start, crack open CreditsScreen.asm and find "ObjTextChar_Init:" Below that, add this:
    Code (Text):
    1.     move.b    #$47,collision_flags(a0)
    That line allows Sonic and Tails to collide with the letters! On it's own, however, that'll make it so that touching a letter crashes the game :psyduck:

    But don't worry! It's an easy fix! Go to "ObjTextChar_Index:". You should see this:
    Code (Text):
    1. ObjTextChar_Index:      
    2.     dc.w    ObjTextChar_Init-ObjTextChar_Index        ;0
    3.     dc.w    ObjTextChar_Main-ObjTextChar_Index        ;2
    Below that, add this:
    Code (Text):
    1.     dc.w    ObjTextChar_Collide-ObjTextChar_Index        ;4
    Now, make a new routine called "ObjTextChar_Collide". Here's an example:

    Code (Text):
    1. ObjTextChar_Collide:
    2.     jmp    DeleteObject
    This will make the letters disappear when you touch them! You can do anything in this routine; play sounds, change the colors, hurt Sonic and/or Tails, instakill them, move the letters, etc. The possibilities are endless!

    EDIT: Fixed some dead links, and cleaned up some minor grammer/formatting issues
  2. Hitaxas


    Retro 80's themed Twich streamer Member
    Oh my! I just swung around for the first time in forever to see what has been going on around here, only to see this?! I am honored and intrigued. I haven't dabbled in anything like this in a while and to see that my work inspired you to create this is awesome my friend. I am on mobile right now so I only took a brief look at the code, but I think this makes for a credits sequence that is far eaiser to edit and maintain. It's also got my head going about how to pull off some other fancy stuff that could be based on this. I might just have to mess with this sometime and see what I can do. Mad props, nice work.

    Edit: I checked it out properly and got annoyed by a few lines under
    Code (Text):
    1. Hitaxas_splash_spawn_credits_object:
    Instead of using multiple add.w lines to space letters correctly like this:
    Code (Text):
    1.     add.w       d3,d3   ;multiply d3 by 2
    2.     add.w       d3,d3   ;multiply d3 by 2 again
    3.     add.w       d3,d3   ;multiply d3 by 2 again
    4.     add.w       d3,d3   ;multiply d3 by 2 again (it needs to be big enough to bridge the gaps between chars)
    You can achieve the same effect using one line:
    Code (Text):
    1.         mulu.w    #$0010,d3 ; multiply d3 enough to space letters correctly
    Also just a thought, but maybe a feature could be implemented to check for specific lines in the text file? This could be utilized to do some flashy stuff, for example, if a certain line of text is at center screen when scrolling, have that line start off-screen left and scroll in ( would make for a cool header effect)
  3. Pacca


    ASM amateur Member
    Labyrinth Zone
    Sonic 2 CD Remix
    I'm glad you liked it! The code was made to be fiddled with, so feel free to go nuts! Apologies for the unoptimized code, I initially wasn't planning on making something for release, so things like that ended up slipping in.
  4. Ralakimus


    pretty much a dead account Tech Member
    On the topic of optimization, it would be even better would be to just do

    Code (Text):
    1. lsl.w #4,d3
    Each bit shifted left is a multiplication of 2 (right being division by 2), and using LSL for something like this is pretty much faster than using MULU/MULS, and in this case, it's also still a single line.