How To Use Bundles

This article will show you how to use bundles in a program. Bundles makes a program relatively easier to understand with less coding from your part.

Level

Advanced

Details

Please look at About Resources section to know about how to write a bundle script. Once you have your bundle script you will be able to use any TrueType font in your game environment, you can use an animation with ease and lot less coding, you can just load a music or sound effect file and play that file in your game, or you can use a timer that is already created in the bundles.

To be able to do all these things you need to follow the steps below:

  1. After opening the audio (Open Audio) and graphics window (Open Graphics Window) you need to load the bundles (Load Resource Bundle).
  2. Call the bitmap file (Bitmap Named) which is already loaded in bundle from the bundle script and set the cell details (Bitmap Set Cell Details) for that image. Please read about how to put bitmap (BITMAP) in Bundles.
  3. Get the animation by its name (Animation Script Named) which is already given in bundle script. Please read about how to put animation (ANIM) in bundles.
  4. Now create a sprite (Create Sprite) by calling that bitmap name and the animation name.
  5. After that the game loop is introduced and it shows all the options that the users have.
  6. Draw (Draw Sprite) and update (Update Sprite) the sprite that you have just created.
  7. Now depending on the user's command-
  • Start the animation (Sprite Start Animation) if the user types "A" (Key Typed(VK_A)).
  • Else draw the text (Draw Text) by calling the font name (Font Named) that is already loaded with the bundles if the user press and holds "T" (Key Down(VK_T)). Please read about resources to know how to put bitmap (BITMAP) in Bundles.
  • Else play the sound effect (Play Sound Effect) by calling the sound effect name (Sound Effect Named) that is already loaded with the bundles if the user types "S" (Key Typed(VK_S)).
  • Else play the music (Play Music) by calling the music name (Music Named) that is already loaded with the bundles if the user types "M" (Key Typed(VK_M)).

The code below shows a small program that demonstrates all these functions in action.

Source Code Widget

  • program HowToUseBundles;
    uses SwinGame,sgTypes;
    
    procedure Main();
    var
        explosion : Sprite;
    begin
        OpenAudio();
        OpenGraphicsWindow('Using Bundles' ,300 ,300 );
        LoadResourceBundle('how_to_bundles.txt' );
        BitmapSetCellDetails(BitmapNamed('fireBmp' )  ,38 ,38 ,8 ,2 ,16 );
        explosion := CreateSprite(BitmapNamed('fireBmp' )  ,AnimationScriptNamed('fire' )  );
        SpriteSetX(explosion ,131 );
        SpriteSetY(explosion ,131 );
        repeat
            ProcessEvents();
            ClearScreen(ColorWhite );
            DrawText('[A]nimation' ,ColorBlack ,0 ,0 );
            DrawText('[T]ext' ,ColorBlack ,0 ,10 );
            DrawText('[S]ound Effect' ,ColorBlack ,0 ,20 );
            DrawText('[M]usic' ,ColorBlack ,0 ,30 );
            DrawSprite(explosion );
            UpdateSprite(explosion );
            if  KeyTyped(vk_a )    then
                SpriteStartAnimation(explosion ,'FireExplosion' ) 
            else
                if  KeyDown(vk_t )    then
                    DrawText('Hi!!!' ,ColorRed ,FontNamed('harabaraText' )  ,120 ,125 ) 
                else
                    if  KeyTyped(vk_s )    then
                        PlaySoundEffect(SoundEffectNamed('danceBeat' )  ) 
                    else
                        if  KeyTyped(vk_m )    then
                            PlayMusic(MusicNamed('danceMusic' )  ,1 );
            RefreshScreen(60 );
        
        until WindowCloseRequested();
        FreeSprite(explosion );
        CloseAudio();
        ReleaseAllResources();
    end;
    begin
        Main();
    end.
    Download Now"
  • #include 
    #include "SwinGame.h"
    int main() 
    {
        sprite  explosion;
        open_audio();
        open_graphics_window("Using Bundles" ,300 ,300 );
        load_resource_bundle("how_to_bundles.txt" );
        bitmap_set_cell_details(bitmap_named("fireBmp" ) ,38 ,38 ,8 ,2 ,16 );
        explosion = create_sprite(bitmap_named("fireBmp" ) ,animation_script_named("fire" ) ) ;
        sprite_set_x(explosion ,131 );
        sprite_set_y(explosion ,131 );
        do 
        {
            process_events();
            clear_screen(color_white );
            draw_text("[A]nimation" ,color_black ,0 ,0 );
            draw_text("[T]ext" ,color_black ,0 ,10 );
            draw_text("[S]ound Effect" ,color_black ,0 ,20 );
            draw_text("[M]usic" ,color_black ,0 ,30 );
            draw_sprite(explosion );
            update_sprite(explosion );
            if ( key_typed(VK_A )  )
                sprite_start_animation(explosion ,"FireExplosion" );
            else
                if ( key_down(VK_T )  )
                    draw_text("Hi!!!" ,color_red ,font_named("harabaraText" ) ,120 ,125 );
                else
                    if ( key_typed(VK_S )  )
                        play_sound_effect(sound_effect_named("danceBeat" ) );
                    else
                        if ( key_typed(VK_M )  )
                            play_music(music_named("danceMusic" ) ,1 );
            refresh_screen(60 );
        }
        while ( ! (window_close_requested() ) );
        free_sprite(explosion );
        close_audio();
        release_all_resources();
        return 0;
    }
    
    Download Now"
  • Source Code Coming Soon

  • Source Code Coming Soon

  • Source Code Coming Soon

If there is a useful hint you will find it in a box like this

Possibilities

Suggested projects will appear here.