Category: Visual Studio Code

Bit of an oddity with VS Code

Bit of an oddity with VS Code

When I first started using it, the C++ extension, and configuring for C++, I got a tasks.json one which was suited for gcc, but recently when I install it, (and the C/C++ Extension for Visual Studio Code, the only choices seem to be these. What happened to the the ones for clang/gcc? The one on the right is what I’m expecting. Even with a C/C++ file open as the instructions here say, I’m getting the one on the left.

VS Code ConfigureVs Configure C++
It’s possible that I’m getting this because I’m using the headmelted and VsCodium versions on a Raspberry Pi.

There’s a bit of a question mark about using the official extension on non-official build of Visual Studio. Headmelted allows it, but VsCodium has its own marketplace.

It’s easy enough to copy tasks.json over so not really a problem but just a minor irritation.

Playing with VSCodium

Playing with VSCodium

open-vsx.orgThere are some subtle differences between it and Visual Studio Code. The main one is the not having access to the VisualStudio market place. The C/C++ template isn’t there but instead is installed by default.

However I have found that the tasks and launch json files created from the F1 menu don’t have the C++ choices. What this means is that you have to get them from an official Visual Studio source, like Visual Studio Code installed on 64-bit Windows/Ubuntu/Mac.

I’d already done this so I used WinSCP to send them via ssh from my Windows box to the Pi. It’s a slight irritation but once you’ve got them setup it works ok. I can build applications haven’t got debugging working yet though.

There is an alternate Extensions Marketplace which has 266 items in it.  That’s a screenshot of the programming language extensions.

Another Visual Studio Code source – VSCodium

Another Visual Studio Code source – VSCodium

VSCodiumAs they say “This is not a fork. This is a repository of scripts to automatically build Microsoft’s vscode repository into freely-licensed binaries with a community-driven default configuration.”

Microsoft’s code includes telemetry and tracking code. The code built here is the same as Microsoft’s but with that telemetry and tracking code removed. Also it includes builds for both ARM (32 and 64-bit) which you don’t get from Microsoft.

The Docs page provides info on how to copy JSON bindings from Visual Studio Code to VSCodium. I shall be giving this a try. Much as I like the code.headmelted.com Visual Studio Code, it doesn’t appear to be updated very often whereas VSCodium seems to be built every night.

 

 

Developing for Raspberry Pi without one

Developing for Raspberry Pi without one

Hyper-V RaspberryPi running VS CodeYes it is possible, as the screenshot shows. You can download a copy of Raspbian from Raspberry-pi. You can either download the .iso directly or save them a bit of bandwidth and do it via a file Torrent. It then takes about 15 minutes or so to install it. When you first run it, it will do an update.

It looks exactly like Raspbian on a PI but there are one or two little things to bear in mind. It’s 32-bit, the clipboard doesn’t seem to work and SSH by default is disabled. It’s real easy to enable it, just find Raspberry Pi Configuration. Click the Raspberry in a circle menu button at the top, then click Preferences and there it is on the menu that pops up. In the Interfaces tab, tick the Enabled checkbox for SSH.

I use the excellent WinSCP to connect to it. Conveniently the Pi will show you its ip address (or from a terminal run ifconfig and it will show you the ip address of eth0) and just put that in with username pi and your pi’s password which you should have changed when you set up Raspbian.

Finally you have to install Visual Studio Code and as usual, it’s not quite so easy. Most Linux distros these days are 64-bit and the official VS Code is only available for Debian in 64-bit and Raspbian is of course 32-bit. But there are community builds and it’s the same place that I got the ARM version from.

And here’s the result. A slightly sluggish (well it is under Hyper-V). I had to comment out the code that initialises the audio. Line 580 ish.

/*	int success=Mix_OpenAudio(22050, AUDIO_S16LSB, 2, 8192);
	if (success==-1 ) {
		LogError("InitSetup failed to init audio");
	}
	LoadSoundFiles();
*/

And here’s the results.
Asteroids running on Hyper-V Raspbian

Raspberry Pi 4 – Perfect for arcade quality games and development

Raspberry Pi 4 – Perfect for arcade quality games and development

lot s of asteroidsI got my Pi 4 a week ago and have been doing experiments on it with my Asteroids game. If I disable the line of code that kills the player ship in the DestroyObject() function and just add a return after case tPlayer: and uncomment the code that adds Asteroids when you press A then I can have lots of asteroids on screen. Also set the MAXASTEROIDS #define to 128.

Then you get the likes of this screenshot which is a 1024 x 768 size window. It’s still running at 40 fps, there are 116 asteroids on screen, around 60-70 explosions and the Pi’s temperature is still a balmy 44.79 C. I’ve never got the Pi’s temperature (and it does have an internal 3.3V fan) to rise above 51 C.

But what’s also impressive is that with Visual Studio Code and Clang on the PI, I can compile the whole 2,200 lines of C on the PI in about a second. This makes the Pi very usable for developing games. And I say this as someone who is used to powerful Windows PCs for compiling code.

This is it running in debug mode; that’s all the numbers and wire frame bounding boxes around every moving object.

Further experimenting and removing the limitations (I used a While loop to limit it to 60 fps back before I had enabled the hardware video flyback). With that while removed and the hardware video flyback disabled, I was getting over 100 fps with 100 asteroids on screen at once.  I pushed it up to 6,000 asteroids and the frame rate dropped to 10 fps. Given the amount of stuff that was colliding and blowing up, that’s still very good.

New version of Visual Studio Code

New version of Visual Studio Code

Update to Visual Studio codeNormally I wouldn’t mention it, as there’s nothing really outstanding about the update (accessibility improvements, Timeline view, Better quick open for files etc.) You can read all about it here.

But the online documentation has improved and more importantly there’s a new tutorial using C++ on Linux. I could have done with this a few weeks back; everything there I had to figure out for myself! In our case I’ve used clang not gcc and C not C++ but those are very minor differences.

But it does have better explanations for many of the fields in the various .json files. Also the changes to tasks.json to allow multiple files. I just added them in based on gcc.

 

Building Asteroids on a Raspberry-Pi

Building Asteroids on a Raspberry-Pi

My asteroids running on a Raspberry-Pi 3B+Silly me completely forgot that Raspberry Pis have a different CPU architecture compared to my PC. You just get used to something working on Linux and it was only after copying and it didn’t run that I realised my mistake. So I now have to recompile Asteroids (from Chapter-48.zip).

I’ve had a Raspberry-Pi 3 B+ for a year and wanted to run the Linux asteroids on it, now that I have it working on Ubuntu.

To do that I had to setup  Pi, running Raspbian downloaded from the Raspberry Pi website onto my Windows PC and then I used Win32 DiskImager (free Windows software) to burn the Raspbian OS onto a SD Card. Raspberry-Pis boot from SD Cards and the better and faster the SD card, the quicker the OS runs. Get a class 10 with A1 SD Card if you can.

After the Pi booted and Raspbian was installed and configured I had to enable SSH on the Pi; it’s disabled by default.

So now my PC was talking to the Pi using the excellent free WinSCP.  I copied all the files over, including the masks, sounds and images folders and all the source code and my exe built on Ubuntu which was the wrong file type. (Trying to run Intel code on an ARM- good luck with that!)

Now it turns out that the Raspbian version I installed (I’d gone for the Raspbian Buster with desktop and recommended software-2.5 GB download) included the non-dev version of SDL2. But as I needed to recompile the whole program, I had to install Visual Studio Code, Clang and the dev versions of SDL2, image and mixer.

Visual Studio Code on Arm?

Haedmelted VS Code running on Raspberry Pi 3b+Microsoft don’t do an official version for Raspberry-Pi or other ARM boards. However I discovered that a developer called Jay Rodgers has taken their source code (VS Code is open source) and  you can get a version from his site. It’s very usable on the Pi. Almost but not quite identical.

After installing the dev versions of libsdl2, image and mixer, I was almost able to compile it. I’d installed Clang but unlike the version on Ubuntu which was Clang-6, the version on Raspbian (based on Debian Buster) is Clang-7.

This means you have to edit the tasks.json file in the hidden .vscode folder. On line 25 where it says “command”: change the path to “/usr/bin/clang-7”.

That built ok, but when I ran it, it stopped with an error in the errorlog.txt file. For some reason, it failed loading the file “masks/am2.msk”.  Now I’d had no errors copying files from Windows to the Pi. This had been a very intermittent problem when copying several files in one go from Windows 10 to Ubuntu running under Hyper-V. Occasionally it would come up with some weird error but usually copied OK on the second go.

Despite several attempts, I could not get it to load that mask so as a temp fix I commented the line out. This means that one of the four asteroids sizes can never collide with anything. But it now ran.

However, I’m only getting about 25-27 fps with it in the early levels, not the 60 fps that I get on Ubuntu on other hardware. Given that the bulk of the work is blitting graphics, I suspect the GPU is just a bit under powered. Apparently on the Raspberry Pi 4, the GPU is newer and twice as fast as on the 3B+ so I’ll have to try it on a 4 when I get one. But the game works and is just about playable.

Now I’ll have to try and figure out why it won’t load that one mask. This has the makings of an interesting bug…

 

Install Clang and Visual Studio Code on Ubuntu

Install Clang and Visual Studio Code on Ubuntu

Visual Studio CodeClang is very easy to install. Open a terminal and issue this command:

sudo apt install clang

It takes a little bit more for Visual Studio Code (aka VS Code). Start on the download page of VS Code. Just google for download Visual Studio Code or click here. Click the Deb rectangle and pick the Open with Software install (default) an that will install it.

After that is installed, run VS Code (open a terminal and type code) and click the extensions icon (5th down on the left) and type in C++ in the search box. The first result is by Microsoft so select it and click install.

Now I created a Projects folder under my home, so click on open Folder in the File menu and select Projects. You can create individual folders for applications under that. If you look closely you can see Asteroids, AsteroidsDND and Examples with Asteroids being open and containing two .c files.

To compile, make sure the open tab is the file you wish to compile then click Terminal on the top menu and Run Build Task…  You’ll see the compiled programs on the left under the Asteroids folder (decnot and ex1).

There’s a bit more for configuring Builds and Debugging but I’ll cover that another time.

 

 

A little trick in Visual Studio Code

A little trick in Visual Studio Code

Matching braces in Visual Stduio CodeIf you have a bit of nesting and you want to make sure your braces match up, Visual Studio code (VSC) can help you with that.

Just drag the cursor over the left or right brace and it will highlight the corresponding brace. In the screenshot you can see the brace on the 2nd line and 5th lines are highlighted.

Here’s a second trick. You can quite easily collapse code blocks. On the left below is uncollapsed code. Now move the cursor between 3 and typedef and a down arrow should appear. Click it and it will collapse the block. It now shows a right arrow and you click it to expand it back.

Before Collapsing a block in C
Collapsed code

How to view disassembled C/C++ code in Visual Studio Code

How to view disassembled C/C++ code in Visual Studio Code

Disassembled C source codeThere are four steps to do this. That’s assuming that you have successfully compiled your C or C++ program and have the source file handy.

  1. Install Disassembly Explorer extension into Visual Studio Code.
  2. Set a configuration setting.
  3. Run a single line command in terminal.
  4. Select a command in the Visual Studio Code command palette.

Install Disassembly Explorer.

In Visual Studio Code just switch to the extension tab and search for Dissassembly Explorer and click the install button.

Disassembly Explorer

 

 

 

Next is configuration

View the installed extensions and click the cogwheel for Disassembly Explorer then click Extension Settings at the bottom of the popup menu. A Settings tab will open, click Edit in settings.json.

You should see a few settings but only this one matters: “compilerexplorer.compilationDirectory”: “${workspaceFolder}/.vscode” . That path is where the compile etc. .json files are located.

Now open a terminal

I’ve assumed that you are running on Linux or Mac. If you are on Windows, you can also use Visual Studio Community Edition which includes disassembly features and why I’m not mentioning it here.

In the folder with your source files issue this command in the terminal.

clang -g -o asteroids.S -S asteroids.c

If OTOH you are running gcc then just substitute gcc for clang.

Finally, back in Visual Studio Code select your source file as the open tab then press F1 and in the menu that appears type in enough of Disassembly Explorer: Show until you can select it and a second tab should open with the disassembly. Click on a function in the source tab and it will jump to the correct place in the disassembled code.