Drawimage not working

Just starting out? Need help? Post your questions and find answers here.
Cammar
New User
New User
Posts: 8
Joined: Mon Mar 20, 2006 2:55 pm

Drawimage not working

Post by Cammar »

Hi everyone, just starting out in purebasic after using blitz based products for the past few years. PureBasic seems very powerfull and intuitive. I was just trying out some simple things to get to grips with the syntax, structure etc. I've hit a small problem which I'm certain is something small and easy to fix to anyone who knows how to code in PB. Anyway this is the code:

Code: Select all

img = CreateImage(#PB_Any, 100, 100)

StartDrawing(ImageOutput())
   Box(10, 10, 80, 80, RGB(Random(255),Random(255),Random(255)))

StopDrawing()

If InitSprite() And InitKeyboard() And OpenScreen(800,600,16,"")

    Repeat   
        FlipBuffers()        

        If StartDrawing(ScreenOutput())
          DrawText("hello!")
          DrawImage(img, 10, 10)
          StopDrawing()
        EndIf
      
        ExamineKeyboard()
        If KeyboardPushed(#PB_Key_Escape)   ; press Esc to quit
           End
         EndIf
    ForEver
EndIf
The above code does display the text but not the image. I've tried loading an image and displaying it but that doesn't work either. Any ideas?

Thanks
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8452
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post by netmaestro »

What you're into there is the difference between an image number and an image ID. When you draw, you must reference the ID. You are referencing the image number with your code. There are two ways to fix your code:

1. Change:
img = CreateImage(#PB_Any, 100, 100)
to:
img = CreateImage(0, 100, 100)
which will cause img to contain an id instead of an image number and you can leave your drawing code as is

or

1. Leave your CreateImage code as is and change:
DrawImage(img, 10, 10)
to:
DrawImage(UseImage(img), 10, 10)
which will convert the existing image# in the img variable to an id.

The image ID is the handle the OS uses to reference the image, while the image number is something PB alone works with. You will use both items for different purposes, but you must know which it is you're using. When you "hardcode" an image number, like 0 or 34, etc. (a different one for each image) the CreateImage command will return an image ID. (handle) When you use #PB_Any, it will return an image number. The number it returns will be some large value that can resemble an id so it can be confusing.

Welcome to the forums Cammar, if you have any questions while becoming hopelessly hooked on PureBasic, don't hesitate to ask.
BERESHEIT
Cammar
New User
New User
Posts: 8
Joined: Mon Mar 20, 2006 2:55 pm

Post by Cammar »

Thanks for the quick reply, it's all become so clear now. I guess I'm still used to the blitz way of doing things. I take it the ID/handle thing is universal for all things in purebasic? i.e. sound, gfx etc
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8452
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post by netmaestro »

Yes, it's pretty common. That'll help you grasp the usage of other tools all the more quickly now that you understand how it is put together.
BERESHEIT
Post Reply