TGC Codebase Backup



Log to file and scrollable area by MobileCreator

10th Oct 2011 14:39
Summary

This routine allows you to log to a scrollable area on your game and/or file, depends on few module sets



Description

This module creates a scrollable area allowing you to display up to 10 lines at the time. The old message will be gone when a new one is added.

The attachment contains the font used to make it, but you can customize the way you want.

Any place on your code, you can call

Log(String) where the string can be anything, like contant messages and variables. Non-strings variables must be converted using Str. Ex:

Log("Coord X = " + str(x))

Another important thing is calling ShowLogs() from inside the function that renders the screen. This will actually show the message on the screen.

If you notice, there are a LOG_FILE variable at the beginning of the module. Setting it to 1 will make the module to create a log file in the application working folder. Set to zero will make the messages be displayed on screen only.





Code
                                    ` This code was downloaded from The Game Creators
                                    ` It is reproduced here with full permission
                                    ` http://www.thegamecreators.com
                                    
                                    RemStart
**********************************************************
 Log Manager - by Paulo Garcia

 Besides adding this module to your project, declare
 the following glogal variables in your main.agc

    #constant MAX_LOGS = 10
    Dim logs[MAX_LOGS] as String
    Dim textLog[MAX_LOGS]
    Global log_idx = 0
    Global fontLog = 0
**********************************************************
RemEnd

// Constants
#constant INITIAL_Y = 700
#constant FONT_SIZE = 16
#constant FONT_ALPHA = 150
#constant LOG_FILENAME = "log.txt"

// optional features - set 0 to disable, 1 to enable
#constant LOG_FILE = 1

#constant ADD_TIMER = 1

// -----------------------------------------------------------
// Initialize Text objects
// -----------------------------------------------------------
Function InitLogs()
    log_idx = 0

    y = INITIAL_Y

    logFont = LoadImage("img/logfont.png")

    For i = 0 to MAX_LOGS - 1
        logs[i] = ""
        textLog[i] = CreateText("")
        SetTextColor(textLog[i], 255, 0, 0, FONT_ALPHA)
        SetTextFontImage(textLog[i], logFont)
        SetTextVisible(textLog[i], 1)
        SetTextPosition(textLog[i], 0, y)
        SetTextSize(textLog[i], FONT_SIZE)
        SetTextSpacing(textLog[i], -5)
        y = y + FONT_SIZE
    Next i

    If LOG_FILE = 1
        // Create log file
        lf = OpenToWrite(LOG_FILENAME, 0)
        WriteLine(lf, "------------------------------ START -------------------------------------")
        CloseFile(lf)
    EndIf


EndFunction

// -----------------------------------------------------------
// Show log messages - call from update or render
// -----------------------------------------------------------
Function ShowLogs()
    rem Print logs
    For i = 0 to MAX_LOGS - 1
        SetTextString(textLog[i], logs[i])
    Next i
EndFunction

// -----------------------------------------------------------
// Add log message - call whereever is needed to log something
// -----------------------------------------------------------
Function Log(log_string as String)

    If ADD_TIMER = 1
        log_string = "<" + Str(Timer())+ "> " + log_string
    EndIf

    If LOG_FILE = 1
        // Create log file
        lf = OpenToWrite(LOG_FILENAME, 1)
        WriteLine(lf, log_string)
        CloseFile(lf)
    EndIf


    logs[log_idx] = log_string
    log_idx = log_idx + 1
    if log_idx >= MAX_LOGS
        ShiftArray()
    EndIf

EndFunction

// -----------------------------------------------------------
// Shift array - older message will be lost
// -----------------------------------------------------------
Function ShiftArray()
    For x = 0 to MAX_LOGS -2
        logs[x] = logs[x+1]
    Next x
    log_idx = MAX_LOGS - 1
    logs[log_idx] = ""
EndFunction