Making a MaxGUI Application: StripAnimMakerLite Part 2- Loading Images
(c) Assari Jan 16 2005

Now that we have our skeleton done, lets proceed to put more meat onto the bone.

First we are going to add the ability to load an image. The format will be whatever format that Bltzmax supports which I believe currently are png, jpg and bmp.

To hold the image, we will use two pixmaps, one to store the loaded image and the other to mirror the image that we are going to display on our canvas.

Once we have made the modifications described below, our program should now be able to do this:-
Note the File Menu as well as the loaded image displayed on the canvas.
   

New Additions to Part 1 - Variable Definitions

We need variables for our Pixmaps (PixMap1 and Pix)

    SuperStrict
'==========================================================
'DEFINE VARIABLES - ALPHABETICALLY
'==========================================================
'
Global Canvas1:TGadget '---------Canvas1 is our Main Canvas
Global MyWindow:TGadget '--------Main Window

Global Pix:TPixmap '-------------Pixmap To hold canvas image
Global PixMap1:TPixmap '---------Loaded image


New Additions to Part 2 - Gadget Creation Block

We now need Menus so that we can have the normal File Open menu to load our image with. And  we need a function to load the image

    '==========================================================
'GADGET CREATION BLOCK
'==========================================================

'Main Window and Main Canvas
MyWindow:TGadget=CreateMainWindow(800,600)

'Create the required Canvases
CreateCanvases()

'Create Required Menus
CreateMenus()

'--------------------------------------------------------
'load Image for Canvas
LoadCanvasImage("1945.bmp")


New Additions to Part 3 - MAIN LOOP

Since we have added menus to our program, we need to now have the Process_MenuActionEvent function to handle the menu events
    '==========================================================
'MAIN LOOP starts here
'==========================================================
Repeat

  WaitEvent()
  Select EventID()
    Case EVENT_WINDOWCLOSE, EVENT_APPTERMINATE
        End

    Case EVENT_MENUACTION
        Process_MenuActionEvent
       

      Case EVENT_GADGETPAINT
        Process_GadgetPaintEvent
       
  End Select
  DisplayStatusText

Forever

End


New Additions to Part 4 - Function Definitions

In this block we need to do the following
Thats quite a bit so lets get started to understand what we have to do

The first one is to create the required menus
    '---------------------------------------------
'Function Create Required Menu
'---------------------------------------------
Function CreateMenus:Int()

  Local FileMenu:TGadget=CreateMenu("File",200,MyWindow)
  CreateMenu("Exit",210,FileMenu,KEY_F4,MODIFIER_OPTION)
  CreateMenu("Open",220,FileMenu)

  UpdateWindowMenu MyWindow

End Function


The above code should not look too unfamiliar to you. We are now simply creating two menu item to the File Menu, Exit and Open.

The next function we need is the LoadCanvasImage function, this function will need a bit of explanation so lets get stuck into it.
    '----------------------------------------------------------
'Load the image either the default or a user requested file
'Make a copy of the Pixmap for the canvas
'----------------------------------------------------------
Function LoadCanvasImage:int(file:String="")

  If file="" file=RequestFile("Load Image File")
  PixMap1:TPixmap=LoadPixmap(file)
  If PixMap1=Null Then
    PixMap1=LoadPixmap("1945.bmp") 'loads default if fails
  EndIf
  Pix:TPixmap=PixmapWin(PixMap1,0,0)

End Function


This function expects a filename to load but if none exists, then the string variable file defaults to a zero length string.
    Function LoadCanvasImage:int(file:String="")

If no parameter was given, we then use the Requestfile function to allow the user to select which file he wants to load from his drives
      If file="" file=RequestFile("Load Image File")

We then load the file using the LoadPixmap function. If the load fails for whatever reason,  the variable PixMap1 will contain a null value. We then proceed to load our default file to ensure that our program will not bomb.
      PixMap1:TPixmap=LoadPixmap(file)
  If PixMap1=Null Then
    PixMap1=LoadPixmap("1945.bmp") 'loads default if fails
  EndIf


We then retrieve a chunk of the loaded image into the Pix variable so that we can later on display on the main canvas. This is done via another function called PixMapWin. I will explain how the function PixMapWin works next.
      Pix:TPixmap=PixmapWin(PixMap1,0,0)

The PixMapWin function may look complicated but all it does is read a portion of the Pixmap that we loaded from our image file and write that onto a pixmap of the same size as our canvas. All the checks are to ensure that the read/write operations do not exceed the bounds of the pixmap

    Function PixmapWin:TPixMap(PixMap:TPixMap,x:Int,y:Int)

Local Tmp:TPixmap=CreatePixmap(GadgetWidth(Canvas1)+1,GadgetHeight(Canvas1)+1, ..
                    PixmapFormat(PixMap))
Local rgb:Int

Local MaxX:Int=GadgetWidth(Canvas1)
Local MaxY:Int=GadgetHeight(Canvas1)

If PixmapWidth(PixMap)<MaxX MaxX=PixmapWidth(Pixmap)
If PixmapHeight(PixMap)<MaxY MaxY=PixmapHeight(Pixmap)

For Local i:Int=0 To MaxX-1
  For Local j:Int=0 To MaxY-1
    rgb = ReadPixel(PixMap,i+x,j+y)
    WritePixel(Tmp,i,j, rgb)    
  Next
Next

Return Tmp

End Function


Now we need to modify the Process_GadgetpaintEvent function to draw our loaded image to the Canvas
    Function Process_GadgetPaintEvent:Int()

    SetGraphics CanvasGraphics (Canvas1)
    Cls
    DrawPixmap Pix,0,0
    Flip

End Function


Now the last thing we need to do before we can run the program is to create the function to process menu events. 
    Function Process_MenuActionEvent:Int()

 Select EventData()

     Case 210 'Exit or ALT-F4
       End

     Case 220 'File Open
       LoadCanvasImage("")

  End Select

End Function


Download the source file (link) and the default bitmap file (link) into your project directory and load into the MaxIDE. Make sure that both the source file and the 1945.bmp file is in the same folder. Compile and run the program, you should now be able to see on your screen something similar to the image shown at the beginning of this tutorial

That's all for now. Return to Main Index.