02 August 2023

Visual Studio - flickering scroll bar when editing certain file types

I got a new laptop and installed Visual Studio 2022 on it. When editing sql and aspx files on the secondary monitor, I could not scroll down or right. The scrollbar just flickers when I hovered over it. With Visual Studio opened on the main monitor, scrolling works fine. It turns out this is an issue with Visual Studio rendering in combination with the main monitor scaled above 100 percent. There are two solutions for it.

Disable the Visual Studio option Environment > General > Optimize rendering for screens with different pixel densities and restart Visual Studio
This solves the scrolling issue, but causes text on all panels to appear a bit blurry.
Set Windows option System > Display > Scale to 100 percent.
This also solves the scrolling issue, but now your main monitor is not at the (for me at least) recommended 125 percent.

Windows 8-11 - start something automatically on start-up

For my work, I want a bat-file to run when Windows starts up. This starts up a local database server with my test database I use for all my development.

Startup folder

To do this, launch Explorer and go to %AppData%\Microsoft\Windows\Start Menu\Programs\Startup. Paste a shortcut to the file you want to run in this folder.

Run a command

Alternatively, you can run a command via Windows' Task Scheduler. Create a new task and set it for Windows 8 or 10. Add a trigger and set it to at log in. Add an action and enter the command including any arguments.

13 January 2022

SQL - check whether or not a column exists

This is a simple way for SQL Server and Sybase databases to check whether a column exists or not inside a query:

COL_LENGTH('Keywords', 'parent_id')

But you can't use that in a select clause. There is another trick though to select a column that may or may not exist:

SELECT (
  SELECT Keyw2.parent_id
  FROM (SELECT 0 AS parent_id) [Dummy]
  CROSS APPLY (SELECT FIRST parent_id FROM dba.Keywords WHERE keyword_id = Keyw.keyword_id) [Keyw2]
  ) [KeywordParent],
  ...
FROM dba.Keywords Keyw

The trick relies on not using a table alias in the subselect!

This can come in handy when columns may or may not exist depending on the application's database version.

07 January 2022

Visual Studio - High contrast syntax colors

When Windows is in a high contrast theme, Visual Studio displays all code in the same color. To change this, go to Tools > Options > Environment > Fonts and Colors and select Show settings for Text Editor. Now you can set the colors for identifiers (variable names), (user) type names, strings (text between quotes), literals, numbers, comments etc. This is my setup:

I've put this here so that next time I reinstall Visual Studio, I can refer to this screenshot to remember what colors I used.

Note that the Plain Text foreground is used for the caret's color.

02 March 2021

Math - Normalize a 3D vector

This method is part of the Vector3 class, and changes the vector into a unit vector.
void Normalize() {
  if (IsZero) return; //X, Y and Z are 0: it would set each component to NaN.
  float length = (float)Math.Sqrt(X * X + Y * Y + Z * Z);
  X /= length;
  Y /= length;
  Z /= length;
}

Math - Calculate a normal vector from three position vectors

This Vector3 constructor calculates a surface normal from three 3D vectors.
public Vector3(Vector3 a, Vector3 b, Vector3 c) {
  var u = b - a; //Requires an overloaded minus
  var v = c - a; //operator for two vector3's.
  X = (u.Y * v.Z) - (u.Z * v.Y);
  Y = (u.Z * v.X) - (u.X * v.Z);
  Z = (u.X * v.Y) - (u.Y * v.X);
  Normalize();
}

08 August 2020

Using Hiero Font Tool to create a distance field font to use with shaders

This article is about text rendering with a 3D graphics library, like OpenGL. A regular font atlas texture doesn't scale well. To render text that looks sharp at any size, you need a distance field texture atlas. Hiero is a tool that can create such a font. It requires Java.

  • Pick a font (system or file) and the characters you want (sample text).
  • Set Rendering to Java, so the effects are listed.
  • Remove the Color effect and add a Distance field effect.
  • Set it's Scale to 15 and Spread to 10. This high scale will make the sample slow to regenerate, so maybe do this last.
  • At the right-bottom, set X and Y to 0 and the four Padding values to 8.
  • Increase or decrease the Size of the glyphs, so they all fit on one page.

Now you can save the font. Hiero will create a png image of the glyphs and a fnt file describing their positions and sizes. With these, you can generate textured quads for your text. A specific fragment shader will use the distance fields in the texture to render sharp text.