TUTORIAL - Setting Up DirectX Screens (updated for PureBasic 4.20)
If you read through the docs and look at the examples
you will see there are a number of different things you can draw to and you
will also get a better understanding on how to put the various commands together
properly.
If you are using a window then you want to use StartDrawing(WindowOutput())
to draw to this window. If you are using a DirectX screen (either Fullscreen
or Windowed mode) then you need to use StartDrawing(ScreenOutput()) etc.
Now StartDrawing() is used when you are using any of the 2D commands (lines,
circles, fonts) and you put these drawing routines within a StartDrawing()/StopDrawing()
block. Only put what is needed to accomplish the 2D drawing within these blocks.
Now if you are displaying Bitmaps, Jpeg Images, etc to a DirectX screen, you
would use LoadSprite() and DisplaySprite(). LoadImage() and DrawImage() would
be used to draw to a Window.
The first thing you must decide is what type of screen you want to do your work
on so... lets say I want to work with a DirectX screen and have the option of
using Full Screen or Windowed Mode.
First I will open a normal Window with some option gadgets so the user can select
the screen mode he wants. We'll use some constants to represent the various
gadgets we will be using
| Code: |
|
#Window_Mode=0 #Gadget_Mode_Option1=1 #Gadget_Mode_Option2=2 #Gadget_Mode_Ok=3 |
| Code: |
|
If OpenWindow(#Window_Mode,175,0,201,129,"Select Mode",#PB_Window_SystemMenu|#PB_Window_ScreenCentered) If CreateGadgetList(WindowID(#Window_Mode)) OptionGadget(#Gadget_Mode_Option1,45,25,95,15,"Window Mode") OptionGadget(#Gadget_Mode_Option2,45,45,95,15,"Screen Mode") ButtonGadget(#Gadget_Mode_Ok,70,85,60,20,"Ok") SetGadgetState(#Gadget_Mode_Option1,1) EndIf EndIf |
| Code: |
|
quitMode=0 Repeat EventID=WaitWindowEvent() Until quitMode |
| Code: |
|
Select EventID Case #PB_Event_CloseWindow If EventWindow()=#Window_Mode End EndIf EndSelect |
| Code: |
|
Case #PB_Event_Gadget Select EventGadgetID() Case #Gadget_Mode_Ok If GetGadgetState(#Gadget_Mode_Option1) screenmode=1 Else screenmode=0 EndIf quitMode=1 EndSelect |
| Code: |
|
#Window_Mode=0 #Gadget_Mode_Option1=1 #Gadget_Mode_Option2=2 #Gadget_Mode_Ok=3 If OpenWindow(#Window_Mode,175,0,201,129,"Select Mode",#PB_Window_SystemMenu|#PB_Window_ScreenCentered) If CreateGadgetList(WindowID(#Window_Mode)) OptionGadget(#Gadget_Mode_Option1,45,25,95,15,"Window Mode") OptionGadget(#Gadget_Mode_Option2,45,45,95,15,"Screen Mode") ButtonGadget(#Gadget_Mode_Ok,70,85,60,20,"Ok") SetGadgetState(#Gadget_Mode_Option1,1) quitMode=0 Repeat EventID=WaitWindowEvent() Select EventID Case #PB_Event_CloseWindow If EventWindow()=#Window_Mode End EndIf Case #PB_Event_Gadget Select EventGadget() Case #Gadget_Mode_Ok If GetGadgetState(#Gadget_Mode_Option1) screenmode=1 Else screenmode=0 EndIf quitMode=1 EndSelect EndSelect Until quitMode CloseWindow(#Window_Mode) EndIf EndIf |
| Code: |
|
If screenmode If OpenWindow(0,0,0,640,480,"MyApp",#PB_Window_TitleBar|#PB_Window_ScreenCentered) If OpenWindowedScreen(WindowID(0),0,0,640,480,0,0,0)=0 MessageRequester("Error","Could not open Windowed Screen",0) EndIf Else MessageRequester("Error","Could not create Window",0) End EndIf Else If OpenScreen(640,480,16,"MyApp")=0 MessageRequester("Error","Could not create DirectX Screen",0) EndIf EndIf |
Basically Windowed Mode is a regular window with a DirectX screen drawn on it.
Full screen is... well, Full Screen.
If the user has selected WIndow Mode, we try and open a Window, then try and
open a DirectX screen in it. If either fail, you get a message and the program
ends.
If the user has selected Full Screen, we try and open a fullscreen DirectX screen.
If it fails, error message and program ends.
Once thing to note, to work with DirectX stuff we need to initialize some directX
stuff... so at the start of our program we will add the following. (We will
also be loading JPEG images so we better include the JPEG image decoder)
| Code: |
|
If InitSprite()=0 Or InitKeyboard()=0 MessageRequester("Error","Could Not Initialize DirectX",0) End EndIf UseJPEGImageDecoder() |
| Code: |
|
SetFrameRate(75) |
Now lets load our 2 test images. I used these to test with...
| Code: |
|
LoadSprite(0,"image1.jpg") LoadSprite(1,"image2.jpg") |
| Code: |
|
quitMain=0 Repeat If screenmode WindowEvent() Delay(1) EndIf Until quitMain |
| Code: |
|
ExamineKeyboard() If KeyboardReleased(#PB_Key_Escape) quitMain=1 EndIf |
| Code: |
|
ClearScreen(0) DisplaySprite(0,50,50) FlipBuffers() |
| Code: |
|
image+1 If image>1 image=0 EndIf |
| Code: |
|
image+1 If image>1 image=0 EndIf DisplaySprite(image,50,50) |
| Code: |
|
counter+1 If counter>75 counter=0 image+1 If image>1 image=0 EndIf EndIf |
| Code: |
|
#Window_Mode=0 #Gadget_Mode_Option1=1 #Gadget_Mode_Option2=2 #Gadget_Mode_Ok=3 If InitSprite()=0 Or InitKeyboard()=0 MessageRequester("Error","Could Not Initialize DirectX",0) End EndIf UseJPEGImageDecoder() If OpenWindow(#Window_Mode,175,0,201,129,"Select Mode",#PB_Window_SystemMenu|#PB_Window_ScreenCentered) If CreateGadgetList(WindowID(#Window_Mode)) OptionGadget(#Gadget_Mode_Option1,45,25,95,15,"Window Mode") OptionGadget(#Gadget_Mode_Option2,45,45,95,15,"Screen Mode") ButtonGadget(#Gadget_Mode_Ok,70,85,60,20,"Ok") SetGadgetState(#Gadget_Mode_Option1,1) quitMode=0 Repeat EventID=WaitWindowEvent() Select EventID Case #PB_Event_CloseWindow If EventWindow()=#Window_Mode End EndIf Case #PB_Event_Gadget Select EventGadget() Case #Gadget_Mode_Ok If GetGadgetState(#Gadget_Mode_Option1) screenmode=1 Else screenmode=0 EndIf quitMode=1 EndSelect EndSelect Until quitMode CloseWindow(#Window_Mode) If screenmode If OpenWindow(0,0,0,640,480,"MyApp",#PB_Window_TitleBar|#PB_Window_ScreenCentered) If OpenWindowedScreen(WindowID(0),0,0,640,480,0,0,0)=0 MessageRequester("Error","Could not open Windowed Screen",0) EndIf Else MessageRequester("Error","Could not create Window",0) End EndIf Else If OpenScreen(640,480,16,"MyApp")=0 MessageRequester("Error","Could not create DirectX Screen",0) EndIf EndIf SetFrameRate(75) LoadSprite(0,"image1.jpg") LoadSprite(1,"image2.jpg") quitMain=0 Repeat If screenmode WindowEvent() Delay(1) EndIf ExamineKeyboard() If KeyboardReleased(#PB_Key_Escape) quitMain=1 EndIf ClearScreen(0) counter+1 If counter>75 counter=0 image+1 If image>1 image=0 EndIf EndIf DisplaySprite(image,50,50) FlipBuffers() Until quitMain EndIf EndIf |
Enjoy!
(written by Paul Leischow - Dec.6, 2003)