Async Task, friend of a responsive UI

So, it’s been a while. I have been trying to enhance my engine to do certain things like be responsive. I happen to know that you cannot do things that are resource intense with UI thread. Otherwise your program / game becomes unresponsive and that is not a good thing.

Async Task for the rescue!  This mechanism (classes) provided by Android API allows developer to do things in the background while not blocking the UI thread. In my word game I need to process database, possibly copy & read it at the very first time, find combinations of words and depending on the size of the database and length of the words, it might take few seconds. So, I decided to add a small splash screen for first time copy with AsyncTask. What it does, it copies the database from the APK to phone and shows a splash screen with a indicator.

Calling and using AsyncTask is very simple. Check the examples here. I got it up and running at the first try without problems. You can use it to process and load Images for access network resources and similar.

Here’s the splash screen. What you think? The picture is transparent so it looks a bit different on black background.

splashscreen

Advertisements

Achievement “Avowed Intent Retracted” unlocked

I have been up from early morning to late evening, not even going outside to check what is happening. Okay, I have been slacking a bit too. I did eat and watch some football to relax my mind.

The main game is coming along quite nicely. I have been raising my ambition level once again. I am definitely not happy with bare bones implementation. I will be putting it to Google Play just to know what it takes. Along with that I have multiple more advanced ideas.

This is how the main screen looks currently. I have drawn some better(?) buttons and I did a custom font thingy. I am not sure how one is supposed to do these in games but I drew my custom font and I was thinking to show some of my texts in the game with this a bit more fancy looking purple with white shadow font. Objections?

Image

It seems there is a whole lot of things to consider when drawing directly to the “screen” and I am starting to have doubts that this is the right way. Having a game engine would probably solve at least some of the issues. At least if it’s any good. Mine isn’t since I have to implement every small thing by myself. Like I am having MASSIVE trouble when doing a “High Score” -screen since I need some keyboard, keys and then I need code check the key events and stuff.  And I need to draw all that too!

There’s already stuff for this in the API but since uh, this is custom engine, I am out of luck. Or skill? Probably both. It looks like I am in the edge of rebuilding this whole thing in some already proven engine or then I have to do some heavy refactoring to my engine. Either of the options do not seem very nice.

Other problem I already had was with gestures. I have a instructions page and it’s a bit longer what I expected and it needs some kind of scrolling. Well, would you mind adding a component provided by Google? I wouldn’t but it doesn’t fit in because I draw directly to the bitmap by using Canvas. If anyone knows how to do it, it would be really nice to hear.

Oh well. This is what development is. Let’s see how things proceed. Now where is my bottle of beer…

Problems at the datawarehouse

So, I have been coding a lot lately. Specially last weekend. I would love to buy few more hours so I could be focusing for what I really like, but I just can’t afford not to go sleep. Otherwise I turn into useless zombie and my brains are needed in the day time too.

I have a simple screen to which I can draw the letters of a static word. Then I have some simple very ugly buttons so I can do some simple actions like check the word player is suggesting if it’s valid. In order to check the validity I was thinking to have a local database of words.

For some time I considered other options like if I should check the word by using some remote call to some (hopefully free) service in the internet. The problem with this solution is that when ever player is playing the network connection is required, this makes offline play impossible. Other thing I found out is that there is not that many free online services from which you could check your word against. Wiktionary could be one. Then there was some with fees.

Since I chose to go with the local database, I am going to need a database implementation. Of course for a simple word list function I could just have a file and create a good indexing system and fast search algorithm but since databases are invented I decided not to reinvent the wheel. I did some search in the internet and found out that SQLite is popular option and it seems that Android SDK has API accessing SQLite databases. Neat!

Downloading and “installing” the database was simple. But problems arouse when I had to create the database and have it part of my game.  Android requires some specific stuff to the tables that are created. One is ANDROID_METADATA table and special notation of id columns.

If you know some SQL then creation of the tables and columns is very easy.  This is what you need:


CREATE TABLE "android_metadata" ("locale" TEXT DEFAULT 'en_US');
INSERT INTO "android_metadata" VALUES ('en_US');

If you are going to use cursors for reading your tables then I suggest you create an id column for your tables. This id column should be named “_id” so that cursor will understand it (otherwise you might get an Exception). Here’s how you create it:


CREATE TABLE myexampletable (
_id INTEGER PRIMARY KEY,
description TEXT
);

All this SQL can be executed in SQLite console. Then you have your database created but empty. If you want some content you need to populate it, like a list of words 😉

Even though I thought I had it complete when deploying the database to virtual device sometimes when the database structure was changed I ended up in strange errors. Workaround for this problem was to delete the file from the virtual device before redeploying the game. This didn’t happen in the real Android device.

I am using currently SQLiteOpenHelper for reading the database and it works just great. It writes the database in the first run to Androids inner file system (from the application package) so the first run takes a bit longer depending on your DB size. Next thing I need is a word combination algorithm…

Raise the bar, ambition level grows

So I kicked the project going and started to type. There’s some excellent tutorials about how you create your project, for example here: (http://developer.android.com/training/basics/firstapp/index.html)

Since this was my first Android application ever, first I need to draw something on the screen. Because I am trying to create a game I wanted to draw something to the Canvas. By reading the API it was pretty easy to find what to methods to call. The real problem was just how would I be able to see it on the device. I thought initially that virtual device would be the answer and it is of course to some extent. But starting the emulator is FREAKIN’ SLOW. The first run when emulator created all stuff it needs, it can take several minutes.

I was put down by the time it took to run the small piece I wrote. It would really take ages if I would have to wait minutes just to kick the emulator running. Luckily the first time is the slowest and it gets faster. Moreover, you don’t need to exit the emulator, just redeploy and it is faster. I also read somewhere that using “the host GPU” setting in “AVD Manager” can make a lot difference. I turned that on and it was now at acceptable level, slow but acceptable.

What I spent next (many) hours was trying to produce simple things on the screen like text, shapes, color etc. so I would know what and how to draw things. Next step was to create classes responsible for different things, like GameButton, Menu etc. I drew some ugly placeholder graphics with Paint (yeah, the default “drawing tool” for Windows !!) so I could just see some of the graphics in action and more over I needed measurements of the images (width, height) so I could check it something had been pressed.

The more I wrote, the more aware I became that I was not going to be satisfied by only if I get the game done. I wanted more. I created a list of features that my game needs to have. Initially my list was something like this:

  1. Create different difficulty settings
  2. Add different game modes (if possible)
  3. Add high scores
  4. Add Google Ads (if possible)
  5. Add sounds to game events

I also have a tendency of doing things so whatever feature I work on, I get it first just barely working and then I later polish and tune it better. This applies to code, but also now to the graphics and placing of elements to the screen.

This is what I have currently, it’s the game main screen with few letters and a score panel. Letters are drawn as separate objects defined by a word (currently static). Letters are images that are created at the runtime. That means that I don’t have an image for every letter in my assets folder.

Screenshot001

I have actually a menu before this but it’s just a screen with “play”, “settings” and “other” menu items. Now I just need more functionality and it comes only by typing!

Ready, steady, code!

In the previous post I showed a small sketch I made about the possible screen about my word game project. I have read some brief tutorials specifically about Android development and I really tried to read some of the good Android documentation provided by Google but apparently it was too much for my excitement level and I really wanted to get hands-on. I probably regret later skipping the reading part!

What is needed? You are going to need Android SDK, most likely an IDE (not mandatory but helps). If you are using an IDE, you probably want to debug your project as well, so you are going to need a plugin as well. Get the Android SDK (http://developer.android.com/sdk/index.html), if you use Eclipse like I do, then ADT (http://developer.android.com/tools/sdk/eclipse-adt.html) is a good choice. Downloading is done via Eclipse plugin management, instructions here: http://developer.android.com/sdk/installing/installing-adt.html

Installing Android SDK is straight forward, at least for Windows. Setting up the IDE (Eclipse in my case) was easy if you know how to manage plugins. Then you are good to go. Run the the “SDK Manager” so it checks updates, downloads them and keeps your SDK up to date.

If you don’t have any existing Android device you need to rely on virtual device(s). Virtual device is an emulator emulating the a hardware (and software). You can set up your virtual device by using the Android SDK. Run the AVD tool from your SDK, it’s named “AVD Manager”.  You can run it separately from SDK folder or from “SDK Manager” by selecting “tools” -> “Manage AVDs..”.

How you know what device you need to emulate? Of course you can have several, if you i.e. need to test different screen sizes and resolutions. You probably can select older API level as well if you have downloaded it via “SDK Manager”, but I had only level 18 (currently the latest) so I chose that. Instructions here: (http://developer.android.com/tools/devices/managing-avds.html)

Here’s a nice chart about the API level distribution which helps you when you are selecting the target API level for you Android project: (http://developer.android.com/about/dashboards/index.html) I read somewhere that rule of thumb could be trying to target around 80% of the possible devices. So currently to be on the safe side API level 10 would be more than enough.

Now the IDE has been set up, giddy up my brave IDE, it’s time to trot to real challenges!

p.s. It’s Friday 17:41 o’clock and I am going to open a bottle of red wine and steer my ride to help me with my first Android Project aka word game.

So, I need an idea for the game

One of the hardest things in the developing an App or a game is the to come up with an idea what the App or game will be about.

Normally an App would probably try to solve a problem, make some process or task easier but game doesn’t need to do that. There are probably plenty of good articles about how to make a good game and what kind of elements it should have. I am honest, I have read none. If anyone can recommend me good articles, please, comment.

Since my initial ambition level was only to make a game and make it actually complete I don’t need to worry about if anyone ever plays it or even find it entertaining. Also since I realized that making a game requires some understanding of graphics and rendering things on the screen I rejected any ideas that might require 3D graphics.

With these parameters, and some Googling, and trying some browser games I decided (without much of inventing myself) to create a game of guessing words. Yes, I know there are gazillion similar games but I like the idea of simplicity. There will be random letters from which the player arranges words of different lengths. Simple, doable and no need of 3D. Stale? Yes.

Few more things why I like the word game idea. It will require a database. Having a database, read / write operations on it actually will teach me something that might be useful when doing mobile development in general. I probably need some algorithm for generating the words as well, it should be also interesting.

So, a word game that is…

Initial sketch up of my word game

Initial sketch up of my word game

It does look a bit ugly and gray scaled, doesn’t it? Maybe I will need help from someone who can actually draw. We will see.

Beginning of a new journey

I have started a new journey. The destination is somewhat unknown but luckily I know why I am at it.

For a long time, almost all my life, I have had a dream of being part of the gaming industry. I have always liked computer games and played a lot of them too. As a kid, me and my brother had ZX Spectrum and Amstrad CPC home computers and even then I used to play a lot. But also, I was very intrigued by making games and programming. I even did some small “games” or programs by using thee BASIC language. I still remember how some of those small “products” gave me a lot of joy when something was rendered to the screen.

I ended up doing software as a job, but it never had anything to do with the games. Rather, I just spent countless hours in Vanilla WoW raids and capping flags in BF3 after long hours at the office. That’s not the only thing I ever did, don’t worry.

Then just recently some small things happened in my life which made me question what I should do with my spare time, and from the depths of my mind I started to ponder about the game programming once again. Would it still be possible to learn something? Could I learn enough skills to possibly move to the industry which I like? Would I really enjoy the game programming as much I remember I did?

I was excited about the idea and started to evaluate how and where would I start. Like what would be the platform? What skills do I have and what I need to learn? What would be the easiest way to start?

I went through a lot of thoughts like “Should I use a game maker of some kind?”, “Creating games by using Visual Studio and DirectX must be difficult.” and “What would benefit me in my everyday work, if any?”. I happen to know some C/C++ but Java I have used almost 15 years, professionally. It definitely would be an advantage to know the syntax, even though I almost never consider the syntax an issue when evaluating languages. Also building games in Windows platform requires decent(?) skills in C/C++ and possibly DirectX and/or some game engine. I am pretty sure it would be possible to learn by doing but it just didn’t sound tempting enough.

So I chose mobile platform and more over, Android. I personally have an old Lumia which runs on Windows Mobile 7.8 and I never owned a single Android device in my life. Yet, Java and free open ecosystem and possibility to learn mobile development (which possibly can support my everyday work) made the deal for me. I could have chosen the iOS but I don’t have any Apple device either and it apparently would have required an investment of at least 1000 € to get all the things I need, even if I would have used second hand.

A question, how long would it take for me to get my investment back if I was to make money with it? Me thinks, loooong!

So here I am, at the very beginning of my journey. I don’t have any other ambitions or goals at the start but to have fun and learn. Oh, there is definitely one thing I absolutely must achieve. The game must be finished and working (of course I get to define what it means).

May the code serve you well, I hope to does the same for me!