Problems with converting
#1
Hi all,
I want to switch from FreeBASIC to QB64, but it seems I have some problems ... I tried to convert a mandelbrot example but got a weird behaviour; all colours - except for black and white - are missing.
The following screenshot shows how it should be:

This is the result with QB64:


And now the source code:
Code:
_TITLE "Mandelbrot"
SCREEN _NEWIMAGE(800, 600, 32)



CLS , _RGB(255, 255, 255)

t1 = TIMER
zm% = 120
miter% = 128

FOR y% = 0 TO 600
   FOR x% = 0 TO 800
       zx# = 0
       zy# = 0
       cx# = (x% - 400) / zm%
       cy# = (y% - 300) / zm%
       i% = miter%
       WHILE (zx# * zx# + zy# * zy# < 4) AND (i% > 0)
           tmp# = zx# * zx# - zy# * zy# + cx#
           zy# = 2 * zx# * zy# + cy#
           zx# = tmp#
           i% = i% - 1
       WEND
       PSET (x%, y%), _RGB(i% * 12, i% * 8, i% * 4)
   NEXT
NEXT

t2 = TIMER

PRINT USING "Time needed: ####.##ms"; (t2 - t1) * 1000
PRINT "Press any key to exit."
SLEEP
SYSTEM
Any ideas what am I doing wrong here?
Reply
#2
@Cybermonkey342,

First, Welcome to the community.

Second, the issue that you are running into is that QB64's _RGB and _RGB32 commands do not wrap higher element values so it will start back over at 0, but instead caps each element to a value of 255 if it is greater than 255.

Here is a snippet of the QB64 source code for the _RGB statement to show you what I mean:

Code:
 uint32 func__rgb(int32 r,int32 g,int32 b,int32 i,int32 passed){
   if (new_error) return 0;
   if (r<0) r=0;
   if (r>255) r=255;
   if (g<0) g=0;
   if (g>255) g=255;
   if (b<0) b=0;
   if (b>255) b=255;

To have the color element values wrap around like you are wanting, you would simply use the MOD command.

Here is a modified version of your code that uses the MOD command to get the effect that you are wanting:

Code:
_TITLE "Mandelbrot"
SCREEN _NEWIMAGE(800, 600, 32)



CLS , _RGB(255, 255, 255)

t1 = TIMER
zm% = 120
miter% = 128

FOR y% = 0 TO 600
   FOR x% = 0 TO 800
       zx# = 0
       zy# = 0
       cx# = (x% - 400) / zm%
       cy# = (y% - 300) / zm%
       i% = miter%
       WHILE (zx# * zx# + zy# * zy# < 4) AND (i% > 0)
           tmp# = zx# * zx# - zy# * zy# + cx#
           zy# = 2 * zx# * zy# + cy#
           zx# = tmp#
           i% = i% - 1
       WEND
       PSET (x%, y%), _RGB32(i% * 12 MOD 256, i% * 8 MOD 256, i% * 4 MOD 256)
   NEXT
NEXT

t2 = TIMER

PRINT USING "Time needed: ####.##ms"; (t2 - t1) * 1000
PRINT "Press any key to exit."
SLEEP
SYSTEM

The modification is in the color statement, which I changed to _RGB32 from _RGB. Using _RGB32 in 32-bit color modes allows the app to run faster since it does do all kinds of checking.

Here is a screenshot of what the modified code above produces:



Please let us know if you have any other issues.
Dedicated to empowering computer programming hobbyists, tinkerers, amateurs, and enthusiasts.
profile for Walter Whitman at Stack Overflow, Q&A for professional and enthusiast programmers


Reply
#3
Very nice, thanks a lot. That does the trick.
I am sure I will come up with other questions in the future...
I really like this "complete package" which QB64 offers - and I really like the look of this forum, too. Smile
Reply
#4
Cybermonkey!  Wow and Holy Cow!

Welcome to the forum!

Walter, did you know Cybermonkey was forum administrator at BP.org and told me I should use code tags the first time I ever posted some code about 3 years ago. At that time I didn't know what the heck he was talking about but with JRS doggin' my tail I figured it out fast enough. Cybermonkey, dare I say legendary developer, German modesty might protest but with a smile? Smile

http://www.egsl.retrogamecoding.org/

Cybermonkey are you still working with Lua?
B += x
Reply
#5
Yep, still doing some things with Lua. I made the successor of EGSL which is called PulsarLua and uses my Pulsar2D engine (http://pulsar2d.org/).
As you know I always wanted a complete BASIC for retro style games, so I recently tried QB64 and was rather impressed.
Reply
#6
Yes, QB64 still being very actively updated by triumvirate(Steve, Fellippe and Luke the silent partner), followers of Galleon (if I have the story straight), then there is Walter who forked his own way and who I call the forum pioneer. With Bestof inviting first, Walter setup board for SmallBASIC, so of course I had to come over... and then try out his fork and then join .net community...  I was fooling around with Freebasic but something was not appealing about it or forum or... ??? It is certainly nice to have the speed and power from compiled exe!
B += x
Reply
#7
@Cybermonkey342,

Quote:I am sure I will come up with other questions in the future..

Please do not be shy to ask.

Quote:I really like this "complete package" which QB64 offers

QB64 has a lot to offer, that is for sure. If you are curious about any internal things of QB64, please ask. I know a tidbit about the package. I have been using QB64 since 2009 and register as a member in 2011.

Quote:and I really like the look of this forum, too.

I spent a long time trying to coming up with a decent look that inspires creativity and is fun to look at. I am not a graphic artist, but I play one at work (for the last 17 years). Every-so-often I get a little inspired and tinker with the forum, so you may see it change from time to time.

This site was originally created to give us QB64 users a place to go when QB64.net went down for two weeks in 2014. It was only suppose to be temporary, but I decided not to press the delete button just to see where this site takes us.

@bplus,

Quote:Walter, did you know Cybermonkey was forum administrator at BP.org

No I didn't. I never been to bp.org.  Well, welcome fellow ADMIN! If you have any suggestions or would like to help, please let me know. This started out as a fun little project, and now I am trying to make it decent. I am relatively new to the forum scene so I do not know what it takes to run a successful site. I could use some help if anyone is interested.

It takes a lot of work to run these forums, even if it is for fun like for me. Just because you build it does not mean people will come. You have to not only attract people to your site, but you have to keep them interested in coming back.

Quote:and told me I should use code tags the first time I ever posted some code about 3 years ago.

I remember I had that same problem when I first started on QB64.net, which by the way is the first forum I ever registered on.

Quote:Cybermonkey, dare I say legendary developer,

I love the sound of that! However, that is a rare title to give out, at least for me. There are only a few people I would give that title to, but all the others are unsung hero's in my book. Those few people are: John Carmack, John Romero, Ken Silverman (I do have the bragging rights here in saying, I got to talk to very briefly with him), and my personal favorite and ultimate legendary developer whom I would love to talk to, Michael Abrash. Michael Abrash is the guy who has and still does inspired me in the computer programming industry. He was the one that taught me the significance of, and how to do optimization if code from assembly language to others. Michael Abrash is a God of software developers in my book.

What makes Michael Abrash better than John Carmack? Well Michael wrote about his experiences and shared them with the world.

Quote:As you know I always wanted a complete BASIC for retro style games,

QB64 is powerful enough to do more than retro games, but it does great at them however.

Quote:Yes, QB64 still being very actively updated by triumvirate(Steve, Fellippe and Luke the silent partner),

To think that I would had been a part of that scene gives me the chills. I've tried. But it is a good-ole-boy system and I had some issues with them along the way which has caused me to stay away. There have been many times I would make fixes to QB64 and the delete them without sharing due to how I felt like I was being treated. Finally, I had enough of the crap and decided to start my BASIC programming language called, "CrystalBASIC".

I will not get into what I have done on it so far, but it is a lot more than many people think. I will not prove anything nor share anything about what I have or haven't done with CrystalBASIC.

But in light of recent events, I have decided to take a copy of QB64, upload it to GitHub (I will not fork it due to some security issues I ran into on GitHub with Travis), and then start adding parts of CrystalBASIC to QB64. I really want to work on the graphical parts as well as a few others of CrystalBASIC, and enhancing a copy of QB64 with those things will allow me to get them done so much faster. Plus I will be sharing it with the community under the flag, "QB64-CBE" which stands for, "QB64 - CrystalBASIC Enhanced".

Quote:then there is Walter who forked his own way and who I call the forum pioneer.

I faked the QB64 forking thing originally, but I will be doing it correctly this time around. The new version of QB64 that will be found on my site and this forum, will be on Github which will compile QB64 for me and store it on this site.

Yeah, I guess you could say I am a "forum pioneer" because it is something I never knew anything about except for using the one over at QB64.net. On this forum, I want it to be as fun looking as I can make it, and provide the members with as many ways to showcase their work and share their thoughts about things.

Quote:ith Bestof inviting first, Walter setup board for SmallBASIC, so of course I had to come over...

I decided earlier this year that this forum was going to be more than being about the QB64 programming language.

@Cybermonkey342,

Click on my member name and take a look at all my threads I started and you will see many cool things that QB64 can do. Or you can scan through the forum and see what thing I have shared, along with the other great projects shared by all the other Joyful members. Also, there is a zombie text adventure game that I help co-create on this forum that comes complete with sound, graphics, and graphical tricks. It even has a SAVE/LOAD feature along with many other things. The really cool thing about the game is you play a guy who is battling his zombie infliction while trying to get to a military base that has the cure. There are four levels of character understand and speaking, so as your health gets low, the screen will pulsate with red to show damage, and the messages will get more incoherent as well as garbled. It really is a fun game, though there are many things to complete. There is even a YouTube video that another group made about the game, which you can watch on this forum.

@bplus,

Quote:It is certainly nice to have the speed and power from compiled exe!


Sadly, your apps could run a bit faster after compilation if Galleon hadn't made QB64 in permanent DEBUG mode. But of course there is a command that will allow a developer to turn off debug mode on specific parts of code, which makes those parts run faster. That command is $CHECKING: OFF and $CHECKING: ON

See http://www.qb64.net/wiki/index.php/$CHECKING for more details of the command.
Dedicated to empowering computer programming hobbyists, tinkerers, amateurs, and enthusiasts.
profile for Walter Whitman at Stack Overflow, Q&A for professional and enthusiast programmers


Reply
#8
Yeah bplus, if he's so leg-ee-ndary how comes he's only gots 1 star and drawers pictures of lady bugs?

@ Cybermonkey: Welcome. If you have any old QBasic stuff in storage, it will most likely run without any modification in QB64. I was one of those who waited it out when QB was abandoned by MS, and used QB64 through the development phase. It has come a long ways since the early days. If a real Android version ever comes out, htat would be the last thing on my wish list. Until then, I have looked into other mobile BASIC languages, like Small Basic.

Best of luck with your program conversions,

Pete Big Grin
Reply
#9
QB64 is powerful enough ?
hmm yes & no...
I think that is not poweful like FreeBasic simply because is not native compiler
FreeBasic use GAS assembler
but in same time FreeBasic sucks in many areas
first after all this years is not complet package then USE WHAT YOU THINK THAT YOU NEED
beeep...
Oxygen is little bit better but also sucks in documention etc ..stc

ahhh...it is so boring to explain ... a hhhh
Reply
#10
@Aurel,

If Galleon would translate the QB64 code to C++ correctly then it would be much faster. However, the biggest speed up would come if he had a "RELEASE" mode so QB64 isn't forever stuck in "DEBUG" mode. But what do I know?

As I had mentioned elsewhere on this forum, you can speed up your apps and make it more of a "RELEASE" mode by using the $CHECKING: ON and $CHECKING: OFF commands in the most time critical parts. Those commands move all the error checking code as well as the checking for any timer events, so you will need to make sure to only use it in the most critical parts. It also prevents checking the keyboard as well.
Dedicated to empowering computer programming hobbyists, tinkerers, amateurs, and enthusiasts.
profile for Walter Whitman at Stack Overflow, Q&A for professional and enthusiast programmers


Reply
#11
Walt, are you sure about the keyboard part? This runs...

Code:
$CHECKING:OFF
DO
    _LIMIT 30
    b$ = INKEY$
    IF LEN(b$) THEN
        IF b$ = CHR$(27) THEN END
        PRINT b$;
    END IF
LOOP

Maybe you mean something other than INKEY$?

Pete Huh
Reply
#12
Hi Walter, 

I remember your mentioning the checking thing before or someone else at .net... I am saving that option for time I really need it like if ever we get a chess AI going and we want to try higher levels of ply? moves ahead...

I vaguely remember Quick Basic running in interpreted mode until compiled into "bat out of hell" speed from .exe and was surprised that seems to be missing from QB64, if I am remembering correctly?

Hi Pete, 

You might find amusing this experiment with SmallBASIC, create a loop with SmallBASIC drawing or doing some heavy computing stuff see how fast it runs and then add INKEY checking to the loop say for some special functions or mods and see how much the loop is bogged down! A work around is using DEFINEKEY and attaching a sub to the keypress.

I was very impressed how much of a slow down key checking creates.
B += x
Reply
#13
@Pete,

This may have been corrected in the lastest version of QB64. Good to know.
Dedicated to empowering computer programming hobbyists, tinkerers, amateurs, and enthusiasts.
profile for Walter Whitman at Stack Overflow, Q&A for professional and enthusiast programmers


Reply
#14
Code:
_TITLE "Mandelbrot"
h& = _NEWIMAGE(1366, 768, 32)
SCREEN h&
DO
LOOP UNTIL _SCREENEXISTS
_DEST h&
_SCREENMOVE 0, 0
t1 = TIMER
DIM DemoX AS _UNSIGNED INTEGER: DemoX = _WIDTH(h&)
DIM DemoxHalf AS _UNSIGNED INTEGER: DemoxHalf = DemoX / 2

DIM DemoY AS _UNSIGNED INTEGER: DemoY = _HEIGHT(h&)
DIM DemoYHalf AS _UNSIGNED INTEGER: DemoYHalf = DemoY / 2

DIM iteri AS _UNSIGNED INTEGER
DIM iterm AS _UNSIGNED INTEGER
DIM zmb AS _BYTE
DIM ScreenY AS _UNSIGNED INTEGER
DIM screenX AS _UNSIGNED INTEGER
CLS , _RGB(255, 255, 255)
zmb = 120
iterm = 128

FOR ScreenY = 0 TO DemoY - 1
    FOR screenX = 0 TO DemoX - 1
        zx# = 0
        zy# = 0
        cx# = (screenX - DemoxHalf) / zmb
        cy# = (ScreenY - DemoYHalf) / zmb
        FOR iteri = iterm TO 1 STEP -1
            zx2# = zx# * zx#
            zy2# = zy# * zy#
            IF zx2# + zy2# < 4 THEN
                zy# = 2 * zx# * zy# + cy#
                zx# = zx2# - zy2# + cx#
            ELSE
                EXIT FOR
            END IF
        NEXT
        PSET (screenX, ScreenY), _RGB32((iteri * 12) AND 255, (iteri * 8 AND 255), (iteri * 4) AND 255)
    NEXT
NEXT
t2 = TIMER
LOCATE 1, 1
PRINT USING "Time needed: ###.###"; ((t2 - t1) * 1000)
PRINT "Press any key to exit."
SLEEP
SCREEN 0
_FREEIMAGE h&
SYSTEM
Not sure what tmp# was doing, so I eliminated it.
down to 270ms average from 328ms (600 * 800). Also changed literals to variables to adapt easily to screen size. Nice demoSmile.
380msec for 1366*768 resolution on my laptop.
Reply