Oxygen Basic

Programming => Example Code => Topic started by: Brian Alvarez on August 03, 2019, 01:56:17 AM

Title: Dr. Mario
Post by: Brian Alvarez on August 03, 2019, 01:56:17 AM
 This is not a commercial game. It is not intended for sale.
This is only a programming excersize.

 Since DIR$ is still not working for 64 bit compilations, this code
works only for 32 bit for now.

 Due to the size of the resources... i cannot post them. The forum
says max 20000kb, and individual file size 15000kb... but it rejected
my 3.8mb RES file. If this is fixed i can post it, along with a copy of
the compiled program for oxygen and for PowerBASIC (same code
compiles fine for both).

Title: Re: Dr. Mario
Post by: Brian Alvarez on August 03, 2019, 02:03:46 AM

 Notes about this code... it is definately not for the faint of heart. It contains cryptic code
due to it being a conversion. 

 The PluriBASIC code is very transparent, and easy to understand. So far Oxygen can handle most
of what i throw at it. :)
Title: Re: Dr. Mario
Post by: Arnold on August 03, 2019, 07:17:10 AM
Hi Brian,

the sceenshots look very nice, and the code is very interesting in several aspects. Did you try to upload the .res file as a zip file? If I recall this correctly it worked for me in the past. But I was a beginner at that time and I suppose I exaggerated a little bit unknowingly.

I wonder why you prototyped ¤GetModuleHandle but use GetModuleHandle in several places of the code? This will allow compiling to an exe file, but running the code within the editor applying Oxygen.dll will result in an error message.

Regarding 32/64 bit: I assume there are several places where sys must be used instead of dword/int (or vice versa?), as pointers are treated differently in these machines. But I have noticed that you already discovered a reason in FindFirstFile.

Roland
Title: Re: Dr. Mario
Post by: Brian Alvarez on August 03, 2019, 11:00:39 AM
Hello Arnold, it is normally a 5.1 MB executable, the resources are 5.0MB, but
can be compressed down to 3.8 MB it cannot go smaller than that because of
already having compressed resources (mp3's).

 I will see if i can post them in parts, so that others can compile the res file. :)

 Regarding the getmodulehandle thing, some parts are generated by PluriBASIC,
and some others are manually entered by me. I normally use GetModuleHandle(0),
but pluribasic most of the times generates the other one.

 What editor are you refering to?
Title: Re: Dr. Mario
Post by: Arnold on August 03, 2019, 12:40:51 PM
I use PSPad where I can add an option to run drmario.o2bas directly with co2.exe. (I have first to comment out the lines $ filename ...; and: uses rtl32). But I can do this also with Oxide.exe in the folder of Oxygenbasic. The option: Compile / Run program directly (F5) runs the program using Oxygen.dll. Compile / Compile and Run Binary32/64 creates and starts the program. This is a cool option, because I can apply: uses name.res in the beginning of the code. Unfortunately my MS Defender puts the exe file in quarantine at the moment and I must get it back.
Title: Re: Dr. Mario (Playable and compilable)
Post by: Brian Alvarez on August 03, 2019, 10:00:56 PM

 I uploaded the code and resources to GitHub:

https://github.com/MrBrianAlvarez/DrMario-Exercise

 There is a playable copy compiled in Oxygen 32bits, I will upload
a PowerBASIC version and a 64bit version compiled with Oxygen.
Title: Re: Dr. Mario (Playable and compilable)
Post by: Arnold on August 04, 2019, 02:44:06 AM
Hi Brian,

this is certainly the most challenging contribution to Oxygenbasic, which is not from Charles. Comparable with it is only Oxyscheme, that should however be adapted to the newest version of O2.

As I never played the game, there is a need to read the rules of DrMario, but by experimenting a little bit I can see that this is a very nice app.

Something is strange though. It is possible to compile the code to a 32-bit exe, link the resource file and start the app. But I noticed that you created a 64-bit exe. When I try to do this on my system, compiling and linking will work but the the dialog will not open and the app blocks the memory. I compared my exe file with yours, it indicates the same size (5386752 / 5390336 Bytes), but something must be different. Did you use the same code for creating the 64-bit exe?

Roland
Title: Re: Dr. Mario (Playable and compilable)
Post by: Brian Alvarez on August 04, 2019, 09:19:04 AM
Hello, Yes it is fairly complex, but not that much. It only took me like a few hours during 2 days (not consecutive) but it was a very good excersize. It also served the purpose of finding bugs in complex code. Surprisingly, i found less bugs than i expected (in my own code) and only 2 in oxygen, which is pretty nice.

 I really onjoyed Dr. mario when i was a kid back in the 90's. Me and my sister used to play for hours (she usually kicked my butt in it). Now that i think of it, it would be a good excersize to convert it into a 2 player game over a TCP connection. But that will have to wait until i complete the TCP functionality.

 The answer to "Did you use the same code for creating the 64-bit exe?" is a YES. and also a NO.

 I used the exact same PluriBASIC code for compiling it to Oxygen32/64 and also to PowerBASIC. No changes required in absolute. But for oxygen the generated 64bit version has some small differences with the 32 bit one. For example, PluriBASIC generates different headers for window class definition, so, no... unfortunately it is not just a matter of changing the rtl32 to rtl64 in the oxygen code. Of course i can post if if you want.
Title: Re: Dr. Mario
Post by: Mike Lobanovsky on August 04, 2019, 10:42:54 AM
Hi Brian,

I'm sorry but your app doesn't work for me under my Windows 7 Ultimate. The app tries to launch for 3 or 4 seconds while the cursor turns to a spinner. But then nothing happens and the app aborts silently without a notice or crash. :-\

Another problem is that if you inspect your o2bas source file in the repo, you'll probably notice that most of your func names start with weird Japanese (?) glyphs instead of symbol/char pair as seen e.g. on this site or in my editors and Notepad... :o

Can the latter be somehow related to the former, do you think?
Title: Re: Dr. Mario
Post by: Brian Alvarez on August 04, 2019, 11:36:37 AM
 Hello Mike,

 That is strange, it was developed under Windows 7 Ultimate. So, the reason must be something else.

 It plays small SFX from resource (they are WAV files), and if the executable doesnt find one
of the mp3 sound effects, it tries to create the files from resource. I think that is what is taking a little
while at the beggining.

 In a moment, I will upload a PowerBASIC version of the executable, please check if that makes a
difference. If it does make a difference, then probably i (or charles) have some debugging to do.

 :)
Title: Re: Dr. Mario
Post by: Brian Alvarez on August 04, 2019, 11:50:28 AM
 Oh, about the Japanese characters, this is an encoding issue.

 Oxygen allows non-alphanumeric characters in the variable names, as well as in function, subs and other
components of the code. I prepended a special character ¤ to avoid collisions with user code. Nothing to worry
about, but it wont show correctly on a site or notepad.

 If you ran the executable, then I dont think the issues are related. But if you compiled the code manually, then
it all depends on the tools you used. Im not sure. I dont think it should be an issue, but then again, I'm not sure.

 Just to make sure, try the pre-compiled excutables to see if it makes a difference? If it doesnt make a difference
then at least i will have some clues on what to look for...

 A version for PowerBASIC has been uploaded.

 By the way... if the problem persists, please make sure your antivirus is not killing it before it finishes extracting
the files from resources, or if it already did! because if your antivirus didnt allow the executable to finish extracting
the files, they may exist and no more attempts to extract them will be made... but they may be corrupt now.

Title: Re: Dr. Mario
Post by: Arnold on August 04, 2019, 12:36:00 PM
This is indeed curious. After unzipping DrMario-Exercise-master.zip there are no problems with the characters of drmario.o2bas. But if I open the website with the code of drmario.o2bas I get the same result as Mike. Until now I did not see this different character representation of source code file and html.

But the app runs very nice on my system, as well compiled to 32-bit and also the provided 64-bit version.

Title: Re: Dr. Mario
Post by: Brian Alvarez on August 04, 2019, 02:05:31 PM
What OS are you running Arnold?
Title: Re: Dr. Mario
Post by: Arnold on August 04, 2019, 02:29:09 PM
I assume it is Windows 10 Home. If I run cmd, the console indicates: Microsoft Windows [Version 10.0.17134.885]
Title: Re: Dr. Mario
Post by: Charles Pegge on August 04, 2019, 02:50:34 PM
o2 reserves ascii codes 127 and 248-255 for internal use. Ascii codes 128-247 have the same status as alpha characters.

Customised use of ! # $ % & ? in a word (except for the first character) is also possible:

Code: [Select]
string c$&$="abc"
print c$&$
Title: Re: Dr. Mario
Post by: Brian Alvarez on August 04, 2019, 03:13:11 PM
 Changing the ¤ to something more ascii-like is no problem. Maybe i will do that for the sake of clarity.

 By the way Charles, how dos this run in your PC?
Title: Re: Dr. Mario
Post by: Mike Lobanovsky on August 04, 2019, 03:36:35 PM
OK Brian,

The PB version performs quite nicely. It dumps initially SFX20.mp3 and then, as the game goes, it also dumps SFX21 to SFX24, pause, and resume music. If all those resources are present, the O2 version performs flawlessly as well.

Unfortunately, not so if there are no resources dumped yet. Then O2 would crash as shown in the topmost snapshot below. Or, if there are only some (but not all!) resources available, it would crash whenever it is supposed to dump a resource of its own. ???  (see the bottom-most snapshot below)

So, the offending code probably resides somewhere in the resource dumping procedures.

I was playing with your precompiled binaries and didn't try to compile my own yet. I will also try them under my Windows 10 a while later and will report back as soon as ready.

But overall, the game is rather entertaining. Thank you very much for this contribution to our code base!
Title: Re: Dr. Mario
Post by: Mike Lobanovsky on August 04, 2019, 04:01:27 PM
Everything seems to run perfectly fine on my Windows 10 box though the defender does take a few seconds to scan the O2 binary for threats even though the zip has been properly unblocked beforehand.

In both cases (W7 and W10), the folders were DL'ed and extracted directly on my desktops, and the binaries were run from there too, if this info may matter in any way.
Title: Re: Dr. Mario
Post by: Brian Alvarez on August 04, 2019, 04:41:44 PM
 Thank you Mike! this gives me more clues! I will have it fixed tonight.

 I will also correct the typo on Excercise.  :o  Too many words in my head, I mix languages sometimes. :)

 I believe this is the culprit:

Code: [Select]
    ' This part checks if the file exists, if it doesnt, it extracts it from resource.
    IF isfalse(len(dir$(resname & ".mp3"))) then
        FF = FREEFILE
        errclear
        OPEN resname & ".mp3" for binary as #ff
            Cont = resource$(rcdata, resname)
            errclear
            PUT #FF,,Cont
            seteof #ff
        CLOSE #FF
    end if

 Which is converted to oxygen like this:

Code: [Select]
   IF ¤ISFALSE(LEN(¤DIR(resname & ".mp3", byval 0, byval 0))) THEN
      ff = (FREEFILE())
      ERRCLEAR
      ¤OPENFILENM(resname & ".mp3", 4, 0, 0, ff, 128, 1, 1, Err)
      cont = (¤RESOURCE("data", resname))
      ERRCLEAR
      ¤PUTSTR(string, ff, -1, cont, 0, 0, 0)
      SETEOF(ff)
      CLOSE(ff, Err)
   END IF
Title: Re: Dr. Mario
Post by: Brian Alvarez on August 04, 2019, 08:44:54 PM
The new code "drmario.o2bas" has error checking to make sure it is able to extract files.

 It will try to extract the intro mp3 even if it exists. If the game proceeds, then it assumes
it can extract files. If it crashes, it will probably have something to do with the extraction
or the saving procedure.

 I have also extracted the source code, it is still 32bits. The resources remain the same.

Note: just a couple minutes after uploading the executables, I tested the downloaded executables
and they crash. The originals here are not crashing, but the downloaded ones are crashing. I will
make some tests to see if the location of the executable is causing this, or it is the executable that is
different.

Note 2: Gah, that didnt take long. The freshly compiled executables here work perfectly fine, but the downloaded
ones are crashing. I dont know what is corrupting them.

 For now, the safest option will be to make a fresh compile and link resources locally.

 If someone can recompile, upload to github and confirm that the fresh executable works fine, while the
ones downloaded from github (or some othe place) crashes, Please do.

 It might give a hint that something is wrong with the executable (since the PowerBASIC one does not
present this issue).
Title: Re: Dr. Mario
Post by: Arnold on August 05, 2019, 03:17:51 AM
Hi Brian,

after extracting your latest DrMario-Exercise-master.zip into a fresh folder, I can confirm that ox32_drmario.exe, ox64-drmario.exe and PowerBasic_drmario.exe run well with my system, with or without the initial created mp3 files. I can also compile, link and start drmario.exe as a 32-bit application. But the 64-bit exe will only load sfx21.mp3 but the window will not open and I have to kill the app with the task-manager.

I am still curious what is different with your 64-bit exe. With me function DialogNew returns a result of 0. Did you apply GetWindowLongPtr / SetWindowLongPtr for the 64-bit app?

Roland

Title: Re: Dr. Mario
Post by: Charles Pegge on August 05, 2019, 05:03:08 AM
Brian,

All binaries working on my Windows10 system. 32bit o2 compilation also okay, but I see many dword handles in the script, preventing 64bit operation.
Title: Re: Dr. Mario
Post by: Mike Lobanovsky on August 05, 2019, 08:46:32 AM
Gentlemen,

It is now more or less clear that everything is, or eventually will be, all right with both 32- and 64-bit compilation under Windows 10. So let's leave it aside for the moment. Our immediate concern is not Win10 or 64 bits!

Our most urgent concern is that, under Win7, even 32-bit OxygenBasic user binary doesn't work properly if it is downloaded from the net!

Is there at least one more user here who's got a decent Win7 installation rather than those kiddie's-first-gadget Win10 laptops? If yes, then please confirm or disprove that Brian's 32-bit game exe crashes under Win7 if downloaded from the GitHub repo rather than compiled by you!


And Brian, please make sure and confirm that you have

i) unblocked properly your zip downloaded from the repo;
ii) disabled your Defender real-time monitoring; and
iii) disabled your other AV SW (if any)

before you launch your 32-bit game exe only to see it crash under your Win7 again and again. I hereby confirm that under my Win7, the game keeps crashing even if I take all the three above-mentioned precautions.


(On a side note, I've always said that I don't like a careless and brute-force approach to the resources with this primitive LinkRes2Exe app. If the app author(s) is/are on such close terms with its code then please make LinkRes2Exe also recalculate and write a correct checksum into the binary's PE header once the .RES file is "linked" in it. And the users who generate .RES files for their executables with 3rd party SW, please make sure your .RC and .RES files have

-- an app icon,
-- a version resource,
-- and an appropriate manifest resource defined and precompiled in them.

James has a nice universal manifest file he uses for compiling his executables with indie C compilers like e.g. TinyC Compiler/TCC. I'm sure it can be used successfully for O2 user binaries as well. The version resource syntax isn't overcomplicated either. So why not make it a habit to compile and test sterling Windows-compliant executables that won't be randomly f@#$ed up by AVs of all sorts?

For all I care GitHub may have its own anti-virus service which screws the exes that people upload there... ::))
Title: Re: Dr. Mario
Post by: Brian Alvarez on August 05, 2019, 10:30:32 AM
 No matter what i do, the downloaded ones always crash (including Mike's 3 steps).
 No matter what i do, the locally compiled ones always work fine (including different locations).

 I have already implemented this functionality:

Code: [Select]
#RESOURCE Manifest, 1, "winxp.xml"
 I have a couple universal manifests here that i can try with, I will report back my results, however
the PowerBASIC ones do not have a manifest either, and it works fine here when downloaded
from GitHub, so, my expectations are low for this approach.

 I also think that something in GitHub is causing this. That is my best guess right now.

 There must be something it doesnt like about Oxygen's executables. Other tools like ILMerge.exe do not work
on Oxygen's executables either. PECompact.exe works with them though.
Title: Re: Dr. Mario
Post by: Brian Alvarez on August 05, 2019, 11:00:57 AM
 How about that... with the manifest, it doesnt crash anymore (ox32_drmario.exe)...  ;D

Correction.... it crashes less.
Title: Re: Dr. Mario
Post by: Aurel on August 05, 2019, 11:07:54 AM
Quote
Is there at least one more user here who's got a decent Win7

I have of course win7 32bit.

But i must to know what exactly i need to download ?

'---------------------------------------------------------------------------------
Do i must download oxygenbasicprogress.zip
'-----------------------------------------------------------------------------
You know me ,i don't like to use untested versions,i still use 043
which work fine for me,if i stuck in something i know where to look.

by the way i am using Kaspersky  which never complain about any o2 exe  :)
Title: Re: Dr. Mario
Post by: Mike Lobanovsky on August 05, 2019, 04:22:59 PM
Hi Aurel,

Please go to this message (https://www.oxygenbasic.org/forum/index.php?topic=1946.msg20877#msg20877). It contains a link to Brian's GitHub repository. Follow the link and download the zip with his code.

Unblock the zip by right-clicking on the file and then extract drmario.exe from it. That's a 32-bit O2 executable of Brian's game. Please launch it and tell us if it crashes for you or not.

TIA
Title: Re: Dr. Mario
Post by: Mike Lobanovsky on August 05, 2019, 04:44:14 PM
Hi Brian,

James' manifest file comes in this message (https://www.oxygenbasic.org/forum/index.php?topic=1947.msg20905#msg20905) of his.

I would only suggest changing his processorArchitecture="amd64" (fifth line from the top) that makes it valid only for 64-bit executables to processorArchitecture="*" that will make it "universal" for both 32 and 64 bits. It's compatible with every Windows OS from Vista up to W10.

But the manifest alone won't work against duller AVs. The exes must also contain a valid version resource, and the locale of other resources (images, audio, etc) precompiled in the exe must also correspond to the locale set forth in the version resource.

All those measures are a prerequisite that some day VirusTotal will stop flagging O2 executables with false positives.

Re. the PB exe, PowerBASIC is historically too well-known to public at large for the AV writers to fiddle with and get away with it. I am almost sure more than one of them suffered losses from the late Bob Zale for what they have been doing to indie developers. ;)
Title: Re: Dr. Mario
Post by: Brian Alvarez on August 05, 2019, 04:47:24 PM
Ok, im trying...
Title: Re: Dr. Mario
Post by: Brian Alvarez on August 05, 2019, 05:14:35 PM
I have re uploaded ox32_drmario.exe and also updated the RES file.

 The locale for the resources is the same as the version info.
Title: Re: Dr. Mario
Post by: Aurel on August 05, 2019, 11:38:17 PM
Hello boys

OK
i download both versions powerbasic and oxygenbasic 32 bit
and put it on disk C
both run and seems to me that work properly . some sort of tetris game funny  :)
so both run fine on my win7 32 bit

By the way who can explain to me
which version of o2 is under oxygenbasicprogress
because of constant changes i lost the line  ::)
Title: Re: Dr. Mario
Post by: Arnold on August 05, 2019, 11:53:42 PM
You can check this with a simple statement:

print version

Output e.g.:

0.2.5 2019-08-01T10:29:45
Title: Re: Dr. Mario
Post by: Mike Lobanovsky on August 06, 2019, 07:45:02 AM
Aurel, thank you very much for your prompt feedback!

Brian, excellent! This time the 32-bit version runs smoothly and flawlessly for me under both Win7 and Win10 (no more a few seconds of annoying and suspicious delay for the arguably AV check under Win10).

Unfortunately, the 64-bit version wouldn't run past sfx21 but then freeze and crash. Something must still be wrong with handle declarations, as Charles suggests.
Title: Re: Dr. Mario
Post by: Brian Alvarez on August 06, 2019, 10:09:45 AM
Unfortunately, the 64-bit version wouldn't run past sfx21 but then freeze and crash. Something must still be wrong with handle declarations, as Charles suggests.

 I didnt update the 64 bit version. Let me do that quickly, i will also post the 64bit source code.

 Done. I also posted the PowerBASIC version of it. Its much clearer, but not nearly as organized as
the PluriBASIC code. The original code uses macros, and separate source files to make it more
readable and mantainable. I may post it later as well.

 I was also able to test the 32bit one for like 30 minutes with no crashes. That looks like good news. :)

 However the 64bit one still crashes...  all updates applied. For this one it may not be the download
process crashing it tough. I am looking into what may cause that even the original one crashes in the
same point.

Upodate: It is DIR$ for 64 bits. The workaround was not enough. I will have to wait for a permanent
fix until Charles fixes CHAR arrays for TYPEs. In the meantime i have uploaded a version that just calls
FindNextFile twice before starting file searches. This seems to fix the issue and makes it playable for now.

 In fact that may very well be the reason it was sometimes crashing on 32bit as well. Let's wait and see.
Title: Re: Dr. Mario
Post by: Charles Pegge on August 06, 2019, 01:20:12 PM
Just posted o2 version 0.2.6

You will need to use the new RTL32.inc and RTL64.inc to match oxygen.dll.


ox64_drmario appears to be stable with this release.
Title: Re: Dr. Mario
Post by: Brian Alvarez on August 06, 2019, 02:24:25 PM
Thanks Charles! downloading it now.

RTL32.inc and RTL64.inc do not seem to be updated, is that normal? last modification date is from april.

 Anyway... it appears to work fine, i can now compare CHAR strings from UDT members.  :)
Title: Re: Dr. Mario
Post by: Mike Lobanovsky on August 06, 2019, 06:54:21 PM
Brian, perfect! 64 bits are working for me now under my Win7 like a charm!

Thanks a lot! :)
Title: Re: Dr. Mario
Post by: Brian Alvarez on August 06, 2019, 08:54:50 PM
You are welcome. :) Im glad you liked it.
Title: Re: Dr. Mario
Post by: Arnold on August 07, 2019, 12:49:38 AM
Hi Brian,

you have shown that you are a very capable developer. In addition, you have rendered valuable services to Charles in the development of Oxygenbasic.

Maybe this could be interesting for prototyping the WinApi functions. If you compare corewin.inc, windata.inc, user.inc, you can see how Charles applied mode64bit to run functions like GetWindowLongPtr/SetWindowLongPtr as well as in 32-bit as in 64-bit. This way you perhaps can use the same code for 32-bit and 64-bit. In my opinion mode64bit is helpful in many cases.

I also noticed that you make a difference of int / sys in Type WNDCLASSEX for the handles; sys would work for 32-bit and 64-bit.

What is the purpose of Wow64DisableWow64FsRedirection? I read the MSDN doc but I suppose I have not yet fully understood this function.

Roland

Title: Re: Dr. Mario
Post by: Brian Alvarez on August 07, 2019, 01:17:39 AM
Thank you! Im humbled by your kind comments. :)

 Whatever declarations you can suggest, i am willing to try.

 When i added the WNDCLASSEX definition I was very new to Oxygen. Perhaps i can go back to that.

Regarding Wow64DisableWow64FsRedirection, please read this:

https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-findfirstfilea

 Specifically i added it because of this:

Quote
If you are writing a 32-bit application to list all the files in a directory and the application may be run on a 64-bit computer, you should call the Wow64DisableWow64FsRedirectionfunction before calling FindFirstFile and call Wow64RevertWow64FsRedirection after the last call to FindNextFile. For more information, see File System Redirector.

 That is where i based myself for declarations of FindFirstFileA, which shows i am still in need of declarations.

Gah! in other news... i found a pretty nasty bug in PowerBASIC! :o
Title: Re: Dr. Mario
Post by: Charles Pegge on August 07, 2019, 09:06:33 PM
Hi Brian,
I can run ox64-drmario in 32bit and 64bit by snipping off all the protypes from the sdk declares, and a few other minor changes :)
Title: Re: Dr. Mario
Post by: Brian Alvarez on August 08, 2019, 07:29:18 AM
 Yes... that is very good and i dont know how you made it. :D

 Maybe instead of generating full prototypes i should also just trip off parameters from the generated code.

 I would nevertheless like to create a full API for error reporting though. You know, number of parameters, data type, etc.