BASIC

Author Topic: Screen Manager  (Read 1469 times)

Offline Dark Schneider

  • Mr. Polyvector
  • ***
  • Posts: 128
    • View Profile
Screen Manager
« on: 2010-Dec-16 »
Hi, here is a centralized screen manager for GLBasic (those ones that uses CREATESCREEN and USESCREEN), so we only need to ask for a screen with CreateScreen, use it with RenderBegin, ask for its sprite associated with GetSprite, and release them with Destroy, with no worry about if the screen requested is in use or not (CreateScreen always will give us a free one if available). Remember to destroy not more needed ones.

Code: GLBasic [Select]
// --------------------------------- //
// TYPE ScreenManager
// --------------------------------- //

CONSTANT ScreenManagerVersionMajor%=1, ScreenManagerVersionMinor%=0, ScreenManagerVersionBuild%=0, ScreenManagerVersionRevision%=2

//! Screen Manager
//!
//! Author: Dark Schneider
//!
//! Management of SCREENS


//---------------------//
// --- DEFINITIONS --- //
//---------------------//


//-------------------//
// --- VARIABLES --- //
//-------------------//
GLOBAL ScreenManagerSprites%[], ScreenManagerSpritesCursor%=0


//-------------------//
// --- FUNCTIONS --- //
//-------------------//
//! FUNCTIONS

//! initializes the Screen Manager system, MUST be called ONLY ONCE before using it
FUNCTION ScreenManagerInit:
        IF ScreenManagerIsInitialized() THEN ScreenManagerDispose()
        DIM ScreenManagerSprites[32]
        FOR i%=0 TO LEN(ScreenManagerSprites[])-1
                ScreenManagerSprites[i]=-1
        NEXT
        ScreenManagerSpritesCursor=0
ENDFUNCTION


//! releases all the resources
FUNCTION ScreenManagerDispose:
        ScreenManagerDestroyScreenAll()
        DIM ScreenManagerSprites[0]
ENDFUNCTION


//! checks if the Screen Manager system has been initialized
// \return TRUE if the Screen Manager system has been initialized, FALSE if not
FUNCTION ScreenManagerIsInitialized:
        IF LEN(ScreenManagerSprites[])>0 THEN RETURN TRUE
        RETURN FALSE
ENDFUNCTION


//! screens iterator
// \return the next screen available, -1 if no available
@FUNCTION ScreenManagerNext:
        LOCAL l_cursor%=ScreenManagerSpritesCursor
        WHILE ScreenManagerSprites[ScreenManagerSpritesCursor]<>-1
                INC ScreenManagerSpritesCursor
                IF ScreenManagerSpritesCursor>=LEN(ScreenManagerSprites[]) THEN ScreenManagerSpritesCursor=0
                IF ScreenManagerSpritesCursor=l_cursor THEN RETURN -1
        WEND
        RETURN ScreenManagerSpritesCursor
ENDFUNCTION


//! creates a new screen with parameters
// \param width% - width of the screen
// \param height% - height of the screen
// \return screen number, range 0-31, -1 if there is no screen available
FUNCTION ScreenManagerCreateScreenWithParams: width%, height%
        LOCAL l_screen%=ScreenManagerNext()
        IF l_screen=-1 THEN RETURN -1
        ScreenManagerSprites[l_screen]=GENSPRITE(); CREATESCREEN l_screen, ScreenManagerSprites[l_screen], width, height
        RETURN l_screen
ENDFUNCTION


//! creates a new screen with the screen resolution as size
// \return screen number, range 0-31, -1 if there is no screen available
FUNCTION ScreenManagerCreateScreen:
        LOCAL scw%,sch%
        GETSCREENSIZE scw,sch
        RETURN ScreenManagerCreateScreenWithParams(scw,sch)
ENDFUNCTION


//! destroys a screen and releases the resources used
FUNCTION ScreenManagerDestroyScreen: screen_num%
        IF ScreenManagerIsScreenInit(screen_num) THEN LOADSPRITE "xx", ScreenManagerSprites[screen_num]
        ScreenManagerSprites[screen_num]=-1
ENDFUNCTION


//! destroys all the screens and releases the resources used
FUNCTION ScreenManagerDestroyScreenAll:
        FOR i%=0 TO LEN(ScreenManagerSprites[])-1
                ScreenManagerDestroyScreen(i)
        NEXT
ENDFUNCTION


//! checks if an indicated screen has been created
// \param screen_num% - screen number to check
// \return TRUE if created, FALSE if not
FUNCTION ScreenManagerIsScreenInit: screen_num%
        IF ScreenManagerSprites[screen_num]<>-1 THEN RETURN TRUE
        RETURN FALSE
ENDFUNCTION


//! gets the sprite associated to a screen number
// \param screen_num% - screen number to check
// \return SPRITE ID associated to the screen, -1 if that screen has not been initialized
FUNCTION ScreenManagerGetSprite: screen_num%
        RETURN ScreenManagerSprites[screen_num]
ENDFUNCTION


//! gets the size of the indicated screen
// \param screen_num% - screen number to check
// \param BYREF width% - return value of the screen width
// \param BYREF height% - return value of the screen height
FUNCTION ScreenManagerGetScreenSize: screen_num%, BYREF width%, BYREF height%
        IF ScreenManagerIsScreenInit(screen_num)
                GETSPRITESIZE ScreenManagerGetSprite(screen_num),width,height
                RETURN TRUE
        ELSE
                width=-1; height=-1; RETURN FALSE
        ENDIF
ENDFUNCTION


//! sets the indicated screen as render target
// \param screen_num% - screen number to check
// \return TRUE is success, FALSE if screen indicated has not been initialized
FUNCTION ScreenManagerRenderBegin: screen_num%
        IF ScreenManagerIsScreenInit(screen_num)=FALSE THEN RETURN FALSE
        USESCREEN screen_num
        RETURN TRUE
ENDFUNCTION


//! sets the backbuffer as render target
FUNCTION ScreenManagerRenderEnd:
        USESCREEN -1
ENDFUNCTION