Skip to main content

Posts

Showing posts from April, 2016

C#: Using the Background Worker to thread your application processing.

In this article I go over using the background worker control in C# .NET Framework (Visual Studio 2015). This control allows you to easily do processing intensive tasks without locking up your interface thread. I use this a lot in winforms applications where I expect code to take any amount of time that the user would notice the interface being locked up while processing. This control gives you the ability to send progress updates to the interface thread as well as cancel processing any any time. The code for this example is on my GitHub here. https://www.youtube.com/watch?v=PKKRAyn3TTs Here is the important code from the main form: public enum CurrentStatus { None, Reset, Loading, Cancelled, Success, Busy } public class MainForm : Form { private BackgroundWorker bwInstance; private Button btnStartWorker; private Button btnStopWorker; private Label lblLoadingStatus; private ProgressBar prgLoadingProgress; private CurrentStatus processin...

Android: Working with Preferences and Settings

In this article I go over using the built-in Android settings and preference system. I have a settings class as well as an activity that manages user input. A spinner is assigned the task of letting the user turn on or off a vibration feature. When the application is started again, the setting is accessed and assigned to the spinner. It also enables or disables the vibrator based on our saved preference. The code is available on GitHub here. https://www.youtube.com/watch?v=8h88t7sJC2o Here is our Settings class: public class Settings { public static final String PREFS_NAME = "twoc_settings_example"; public static final String KEY_SETTINGS_AVAILABLE = "settingsavailable"; // your custom settings in the application public static final String KEY_STATE_VIBRATION = "vibrationstate"; // the object used to access settings for this application stored in a predefined setting area SharedPreferences prefAccessor; public Settings(Context...

Javascript and HTML: Async Communication Prototype

In this article I go over an asynchronous communication prototype I had made in straight Javascript that you use to POST to a server script and pass along the response to a Javascript function specified by the initial call to the prototype. The main benefit here is that you don’t need a full page refresh, which is great for implementing web interfaces that have an application feel to them. It doesn’t use any bulky libraries and should work on all browsers. An ideal use case for this would be websites hosted in an internal network that you wanted to act like an application. Though, with extensive modification you could add security features or just run it through https with proper verification code in the PHP script. The example code is located on GitHub. https://www.youtube.com/watch?v=73yU2jtaVBo In my example I have four files: example.html = the html page that gives the user two buttons that fire off asynchronous calls to a server side script. AsyncManager.js = The prototype (aka. c...

Android: Basic game loop with scaled graphics.

In this article I go over implementing a simple game loop with a custom view. The standard draw method is used with some timing checks so that we can get an approximately consistent refresh rate. This is a somewhat simple way to get started without having to use something OpenGL. The full source code is on GitHub. https://www.youtube.com/watch?v=K_ukPCLhCR8 The main activity handles the creation of our custom view and passes along screen size information once it’s available. This is a key step because it allows us to get that screen information before the view actually starts being displayed. I have a previous tutorial on just that here so I am going to omit the main activity code and the related xml layout file. If you want an exact game update loop structure, you might need further restrict any numeric changes in your game specifically by time rather than having them in the middle of the draw loop that is approximately updated on a given time frame. I haven’t done any testing to ver...

Android: Get the size of a custom view before display.

When developing applications or simple games for Android where you are drawing or writing to a view, an issue arises upon startup of the view. In my case I had a game where I wanted to calculate screen tile size before the game view starts, but with most tutorials I had seen everything happened after the view was fully running. That turned into a chicken and the egg scenario. I needed some key information before I could start the game view, but the default techniques didn’t allow for that. https://www.youtube.com/watch?v=TkjWTUFW5UY Here is the code on GitHub for this article. I was able to get around the issue with a few techniques. Have my main Activity linked with an xml layout file that has two empty nested linear layouts that fill the screen. Make a custom view that houses the main draw code. For this example, all I do is fill the screen with black and display the available screen size. Attach a ViewTreeObserver.OnGlobalLayoutListener in the Activity that will be the boot code upo...