Coding

All things computer programming related...

Batch file time saver

- Posted in Coding by

Part of my morning routine is to launch three applications which I use to record my previous day's caloric intake and expenditure (per data from my FitBit device).

The following text in a batch file suffices to save me a few clicks and the better part of a minute's time:

start cmd.exe /k cd C:/csdev/utils/cal
START C:/windows/system32/calc.exe
start cmd.exe /k cd C:/Users/[user]/Documents/DokuWik

Note that I've replaced my actual username in the third line of the batch file code above with [user], for security purposes.

This batch file launches Windows' calculator, and opens command prompts at the directories containing my calorie tracking console C# app, and the launcher for my local DokuWiki. It saves me time navigating the directory structure. All I have to do after clicking this batch file is enter the requisite program name in each command prompt — in my case, cal in one prompt, run in the other.

Squiffy timeout

- Posted in Coding by

It's easy to sleep or pause or delay for x milliseconds in Squiffy. The following code makes use of the javascript setTimeout function (each Section in your Squiffy source code can call javascript code if it precedes all other Squiffy script in that passage). You can copy/paste the following code into a new Squiffy project and it will run:

[code] [[Start]]: setTimeout(function () { squiffy.story.go("next"); }, 2000);

Hello Bob!

[[next]]: setTimeout(function () { squiffy.story.go("next2"); }, 2000); Bob, are you there??

[[next2]]: Hmm, Bob's not around... [/code]

Javascript has a function called setTimeout that allows us to run a function once after the specified interval of time. For instance, this code calls sayHi() after one second:

function sayHi() {
  alert('Hello');
}

setTimeout(sayHi, 1000);

ILMerge error

- Posted in Coding by

Sometimes you'll get “Unresolved assembly reference not allowed” when attempting to use ILMerge. To avoid this, specify the full path to the framework you're targeting, like this on the command line:

ilmerge /targetplatform:v4,"C:WindowsMicrosoft.NETFramework64v4.0.30319" /out:merged.exe myprogram.exe mylib1.dll mylib2.dll

Created specific sized file at the Windows command line

- Posted in Coding by

Create file of specific size at Windows command line:

fsutil file createnew filename number_of_bytes

Import a font using c-sharp

- Posted in Coding by
using System.Runtime.InteropServices;

      //interop to allow import of game font
      [System.Runtime.InteropServices.DllImport("gdi32.dll")]
      private static extern IntPtr AddFontMemResourceEx(IntPtr pbFont, uint cbFont,
          IntPtr pdv, [System.Runtime.InteropServices.In] ref uint pcFonts);

Also before the form's constructor, add a form-level variable (but make sure it comes after the Win32 function import):

Font myFont;

If you have the desired font as a .resx resource in your Visual Studio project, you'll use the following code inside the form's constructor:

//Form1 constructor code that pulls game font into memory
          byte[] fontData = Properties.Resources.brushstr;
          IntPtr fontPtr = System.Runtime.InteropServices.Marshal.AllocCoTaskMem(fontData.Length);
          System.Runtime.InteropServices.Marshal.Copy(fontData, 0, fontPtr, fontData.Length);
          uint dummy = 0;
          fonts.AddMemoryFont(fontPtr, Properties.Resources.brushstr.Length);
          AddFontMemResourceEx(fontPtr, (uint)Properties.Resources.brushstr.Length, IntPtr.Zero, ref dummy);
          System.Runtime.InteropServices.Marshal.FreeCoTaskMem(fontPtr);

          myFont = new Font(fonts.Families[0], 16.0F);

Tweego

- Posted in Coding by

At the time of this writing, I couldn't find a Windows binary release of the Tweego compiler for Twee source code files (Tweego is a program written in Go that can compile Twee source code files into HTML games that can be played in a web browser). << I found it.

I didn't make a careful record of the perambulating required to obtain a copy of the Tweego executable. It had to be built from source, which I recall involved building it with Node package manager. I managed to accomplish this, however, and installed Tweego.exe at C:\MyTweego on my Win10 PC, then added that path to my PATH environment variable.

Sweetness after that.

Play .wav or .mp3 media using c-sharp

- Posted in Coding by
using System;

namespace PlayWav{    

    class Program {                

        static void Main(string[] args)
        {

            Console.Clear();            

            System.Media.SoundPlayer player = new System.Media.SoundPlayer();
            player.SoundLocation = "upbeat.wav";
            player.Play();
            Console.WriteLine("nn Press any key to exit...");
            Console.ReadKey();
        }

    }

}

Now, this is quite handy for small .wav files used as sound effects. But if you're going to play songs, you wanna use .mp3 format. That's not quite as simple. After some research, I found it's not difficult if you use a freely available library called NAudio. Your code must reference this library, whose code is contained in file NAudio.dll. I should note that the following code will suffice equally well whether you wish to play .wav or .mp3 files:

using System;
using NAudio;
using NAudio.Wave;

namespace PlayMp3{   

    class Program {                

        static void Main(string[] args)
        {

            Console.Clear(); 
            Console.WriteLine("n Press a key to play a 7-second segment from 'Somewhere Out There' (a 1986 song)");
            Console.ReadKey();
            using(var audioFile = new AudioFileReader("somewhere.mp3"))
            using(var outputDevice = new WaveOutEvent())
            {
                outputDevice.Init(audioFile);
                outputDevice.Play();
                while (outputDevice.PlaybackState == PlaybackState.Playing)
                {
                    System.Threading.Thread.Sleep(1000);
                }
            }
            Console.WriteLine("nn Press any key to exit...");
            Console.ReadKey();
        }

    }

}

Some of my programming creations

- Posted in Coding by
C# Programming

I've dabbled in C# since this programming language was first introduced. Here I provide links to useful tips and snippets, source code, and related thoughts. And let me assure you, these source code files represent a lot of time and learning on my part.

My C# Projects (you're welcome to use/modify the source code of any of these projects, even for commercial purposes) ↓

Found Money: A GUI for tracking piggy-bank deposits over time → | on box.com | on GDrive

Sudoku: My take on the Sudoku number grid gameon box.com | on GDrive

Proofy: My proofreading jobs / words-proofed tracker → | on box.com | on GDrive

KyrCrypt: My files and folders encrypter → | on box.com | on GDrive

<

p class="tab40">KyrHangman: a game I wrote for a donationcoder NANY → | on box.com | on GDrive

SLOC: My counter of source code lines → | on box.com | on GDrive

How Long Since?: Calculates elapsed time between any two dates → | on box.com | on GDrive

KyrPrayerMinder: Prayer journal or use as diary; searchable → | on box.com | on GDrive

Basic BlackJack; decent implementation of a card Deck class → | on box.com | on GDrive

Example of Square Proximity Algorithm: demonstrates detection of square proximity/overlap → | on box.com | on GDrive

My C# dabblings

- Posted in Coding by
C# Programming

I've dabbled in C# since this programming language was first introduced. Here I provide links to useful tips and snippets, source code, and related thoughts. And let me assure you, these source code files represent a lot of time and learning on my part.

My C# Projects (you're welcome to use/modify the source code of any of these projects, even for commercial purposes) ↓

Found Money: A GUI for tracking piggy-bank deposits over time → | on box.com | on GDrive

Sudoku: My take on the Sudoku number grid gameon box.com | on GDrive

Proofy: My proofreading jobs / words-proofed tracker → | on box.com | on GDrive

KyrCrypt: My files and folders encrypter → | on box.com | on GDrive

<

p class="tab40">KyrHangman: a game I wrote for a donationcoder NANY → | on box.com | on GDrive

SLOC: My counter of source code lines → | on box.com | on GDrive

How Long Since?: Calculates elapsed time between any two dates → | on box.com | on GDrive

KyrPrayerMinder: Prayer journal or use as diary; searchable → | on box.com | on GDrive

Basic BlackJack; decent implementation of a card Deck class → | on box.com | on GDrive

Example of Square Proximity Algorithm: demonstrates detection of square proximity/overlap → | on box.com | on GDrive

How to install nuget on Windows

- Posted in Coding by

Open a PowerShell prompt with elevated privilege (as Administrator)

Get nuget.exe command line wget https://dist.nuget.org/win-x86-commandline/latest/nuget.exe -OutFile nuget.exe

Download the C# Roslyn compiler (just a few megs, no need to 'install') .nuget.exe install Microsoft.Net.Compilers

Note: as of date 21 Aug 2018, this installs the Roslyn compiler here:

C:WindowsSystem32Microsoft.Net.Compilers.2.9.0tools

Page 10 of 11