Help to rewrite code for BlitzPlus
Blitz3D Forums/Blitz3D Beginners Area/Help to rewrite code for BlitzPlus
| ||
Could someone help rewrite the code below to work in BlitzPlus. I am struggling with getting examples to work in BlitzPlus and haven't been able to figure it out from the documentation. I believe if I could see how to write the code below to work in B+, it would answer a lot of questions. Thanks in advance. ; demo06-07.bb - A ReadPixelFast/WritePixeFast Example Graphics 800,600 Print "Press any key to use ReadPixel" ;wait for user to do something WaitKey ;load rectangle image image =LoadImage("rectangle.bmp") ;Draw the intro screen DrawImage image,0,0 DrawImage image,100,100 ;Hold up a second Delay (1000) ;Create a pixel array that will hold the entire screen Dim pixelarray(GraphicsWidth(),GraphicsHeight()) ;lock the buffer REQUIRED LockBuffer ;Copy all of the pixels of the screen to the array For rows=0 To GraphicsWidth() For cols=0 To GraphicsHeight() ;Copy the current pixel pixelarray(rows,cols)=ReadPixelFast(rows,cols) Next Next ;Unlock the buffer UnlockBuffer Cls ;Locate 0,0 Print "Press another key to copy pixels backwards" ;Wait for key press WaitKey ;Lock the buffer to allow WritePixelFast LockBuffer ; Use WritePixel to redraw the screen using the color information we got earlier For rows=0 To GraphicsWidth() For cols=0 To GraphicsHeight() ;Draw the current pixels WritePixelFast rows,cols,pixelarray(GraphicsWidth()-rows,cols) Next Next ; Unlock buffer after using WritePixelFast UnlockBuffer Print "Press a key to exit" WaitKey |
| ||
Ok heres a few things I noticed.... 1. You haven't set up a drawing buffer. Try 'Setbuffer BackBuffer()' after your graphics statement. 2. You are trying to lock a buffer but no buffer is defined - see above. 3. You are using a 'print' statement after setting graphics up. Try using the 'Text' command instead. 4. I couldn't see a 'flip' anywhere. Try putting this command before your 'CLS' command. Hope that helps. |
| ||
Thanks. I will try all those steps. |
| ||
Following those instructions worked. I actually had to add a flip after each write or text command. The code is below that finally worked. If anyone is using BlitzPlus with "Game Programming for Teens" this hsould help with the examples at the end of chapter 6. ; demo06-07.bb - A ReadPixelFast/WritePixeFast Example Graphics 800,600,32,2 SetBuffer BackBuffer() Text 0,0,"Press any key to use ReadPixel" Flip ;wait for user to do something WaitKey ;load rectangle image image =LoadImage("rectangle.bmp") ;Draw the intro screen DrawImage image,0,0 DrawImage image,100,100 Flip ;Hold up a second Delay (1000) ;Create a pixel array that will hold the entire screen Dim pixelarray(GraphicsWidth(),GraphicsHeight()) ;lock the buffer REQUIRED LockBuffer ;Copy all of the pixels of the screen to the array For rows=0 To GraphicsWidth() For cols=0 To GraphicsHeight() ;Copy the current pixel pixelarray(rows,cols)=ReadPixelFast(rows,cols) Next Next ;Unlock the buffer UnlockBuffer Cls ;Locate 0,0 Text 0,0,"Press another key to copy pixels backwards" ;Wait for key press WaitKey ;Lock the buffer to allow WritePixelFast LockBuffer ; Use WritePixel to redraw the screen using the color information we got earlier For rows=0 To GraphicsWidth() For cols=0 To GraphicsHeight() ;Draw the current pixels WritePixelFast rows,cols,pixelarray(GraphicsWidth()-rows,cols) Next Next Flip ; Unlock buffer after using WritePixelFast UnlockBuffer Text 0,0,"Press a key to exit" Flip WaitKey |
| ||
Following those instructions worked. I actually had to add a flip after each write or text command. Kaminari, I don't want to sound patronising but are you actually familiar with the concept of double buffering? If you are, you should realise why you neeed to put a Flip after Text and DrawImage in order to actually see what's been drawn. |
| ||
big10p, Thanks. I am realizing slowly but surely many things about BlitzBasic. I'm actually just learning BlitzBasic and game programming in general. That is why I was asking questions in the "Beginners" area. I am using the "Game Programming For Teens" book which actually used the code at the top which must work in whatever version was used when the book was written. (Blitz2D I suppose) Many of the examples, however, though they may work, as written, in Blitz2D, have to be modified to run in BlitzPlus. That is what I was asking for help on. I would love to read a book written specifically for BlitzPlus, but haven't come across one, yet. I liked the format of the GPFT book because I am using it to teach my 8 year old and twin 4 year olds to program in a fun way. I am also showing them how forums work and how they will be able to ask other programmers out there for help when example code they read in a book does not function as expected. |
| ||
We welcome your questions and will happily explain anything you're trying to wrap you head around. |
| ||
It sounds to me like the example code from the book doesn't work without alteration because Blitz2D defaults to using the FrontBuffer, whereas BlitzPlus defaults to using the BackBuffer. I can't be certain as I don't own BlitzPlus. Try running the original example code in BlitzPlus, but add the line 'SetBuffer FrontBuffer()' directly after the Graphics command. Does it work then? |
| ||
In BlitzPlus, FrontBuffer() and BackBuffer() are the exact same thing--the Back Buffer. In other words, FrontBuffer() is just there for backwards compatibility, and you still need to add a flip, because it's the back buffer either way. One cannot draw directly to the front buffer anymore. |
| ||
Ah, then that explains why the original code doesn't work in BlitzPlus. |