Showing posts with label example. Show all posts
Showing posts with label example. Show all posts

02 November 2009

Timers

Ok well I decided to take this in a little different direction. I am going to start putting small bits of code up here and piece them together into different things. That way you can learn and I can experiment. So if you are as excited as I am about programming then whip out your compiler and lets get started.
The first thing I will go over is how to create a simple timer which you can use to trigger things. What things? Anything you want really. In my example it will put a line out to the console after a certain amount of milliseconds.
By the way, 1000 milliseconds is equal to 1 second. So 10000 milliseconds would be equal to 10 seconds. It seems complicated but lets take a look.
One thing you should also consider is that time is relative when dealing with different operating systems. A tick is the smallest resolution of time that the CPU your dealing with can process. (I generally think of a tick as 1 millisecond) In my example I am using a long variable named sc_timer. It will capture the current time by using getticks(); function. To get the timer to trigger, you subtract sc_timer from getticks(). The result is how much time has passed since you last updated the sc_timer variable. Pretty dang cool. You can use an if statement to compare if the elapsed time is greater than a certain amount of milliseconds. In my example I used 10000 milliseconds (10 seconds).

I created a console application in Code::Blocks and modified it... main.cpp:

#include <iostream>
#include "sc_getticks.h"
using namespace std;
long sc_timer;
int main()
{
sc_timer=getticks();
while(1)
{
if( (getticks() - sc_timer) > 10000)
{
sc_timer=getticks();
cout << sc_timer << "\n";
}
}
return 0;
}



Next I created a new file and called it sc_getticks.cpp

#ifdef _WIN32
#include <windows.h>
#endif
#include "sc_getticks.h"
long getticks(void)
{
#ifdef _WIN32
return GetTickCount();
#else
long curtime; struct timeval tp; struct timezone tzp; static long secbase;
gettimeofday(&tp,&tzp); if(!secbase) { secbase = tp.tv_sec; return tp.tv_usec/1000; }
curtime = (tp.tv_sec - secbase)*1000 + tp.tv_usec/1000; return curtime;
#endif
}
You can see that I put in some preprocesser directives. Don't worry about that for now. All you want to concentrate on is that this function will return the current tick count. (Note: if you are just using Windows systems you can replace getticks(); function and file and just use GetTickCount(); )

One more file... (its a small one) sc_getticks.h
extern "C" long getticks(void);
Now when you compile all this together you should get a console application that spits out the current tick count once every 10 seconds.

There you have it, a relatively painless timer. What you do with it is up to you. I will build on this example so stay tuned!

Download files for this project

15 October 2009

MinGW & Code::Blocks


I'm going to talk about MinGW and Code::Blocks. I know what you're thinking... "What did he just call me?" Well relax, I didn't call you any names. Eh yeah... I'll just dive right into it then...

MinGW


What is it?

MinGW is a contraction of "Minimalist GNU for Windows", is a port of the GNU Compiler Collection (GCC), and GNU Binutils, for use in the development of native Microsoft Windows applications. (from the mingw.org website)

What does it do?

This suite of tools will allow you to compile executables for Windows from the following languages:
  • C++
  • C
  • Fortran
  • Java
  • Ada
  • Assembly Language
Why should I get it?

It's free. It's also widely used and robust.

Ok, now that you know what MinGW is, you can forget about it. Unless you are an overachiever, in which case you can learn more here.

All you really need to know about MinGW is that MinGW is a free C/C++ compiler that comes bundled with Code::Blocks. You CAN use other compilers/debuggers with Code::Blocks, if you are some kind of wierd engineer working on DNA sequencing software on some database that resides on some ancient mainframe hardware that requires a certain build of a certain kind of compiler that only compiles a modified fortran dialect (chuckle, what a loser)...

Code::Blocks

What is it?

Code::Blocks is a free C++ IDE built to meet the most demanding needs of its users. It is designed to be very extensible and fully configurable. (from the codeblocks.org website) In short it is a highly advanced super-version of Notepad.exe.

What does it do?

This thing will let you type in the C/C++ script and then click a button and compile and execute it. Very cool. More information can be found here.

Why should I get it?

Ok this is kind of like asking the question would I rather walk to the store which is 5 miles away, or do I get in my car and ride on my butt to the store which is 5 miles away, letting the car do all the hard work. Yes in this case the software is the car. So unless you want to pretend you are a computer scientist from the late 1970's and like dealing with command line prompts, I highly recommend you get this software.

There you have it. Open Source software which will allow you to create your own software. How great is that? In this post I am only going to talk about what MinGW and Code::Blocks are and I have provided you with the links to the websites. I'll go over creating some simple programs next. So go ahead and download and install, and get ready for the ride of your LIFE! WOooooOOoO!

Code::Blocks / MinGW Bundle Download from Sourceforge

14 October 2009

Thumbs.DB Annihilator


Here is a computer program that I created that I call Thumbs DB Annihilator!

This program will search your hard drives for all those Thumbs.db files that Windows thinks it has to create and it will DELETE every single one of them. WARNING: This WILL DELETE all THUMBS.DB files on ALL YOUR DRIVES. (Turn off the Windows System feature that creates thumbs.db files)

This program is specifically written for Windows systems and I don't think it will compile on anything else. In this instance it does not matter because all I care about is deleting the thumbs.db files that Windows creates. I will get into creating code that will compile on more than one OS a little later.

Here is the link to the zip file from my Google site:

Thumbsdb.zip

This file has the executable in it and also has the Visual C++ workspace to build it if you have that. The following files should be in the zip file:

c_thumbsdb.cpp contains class CThumbsdbCleaner
c_thumbsdb.h contains definition for class CThumbsdbCleaner
thumbsdb.cpp contains int main(int argc, char* argv[])

If you are using MinGW (GNU C) to compile from command line type these lines into the command prompt to compile it:

g++ -c thumbsdb.cpp
g++ -c c_thumbsdb.cpp
g++ *.o -o thumbsdb.exe

You'll probably notice that it is kind of overly complex code for what it is, but there is a reason for it and I will build on it in the future. I also left in alot of commented out code that you can toy around with to make it delete other files. Be extremely careful with this or you may delete some things you don't want it to. For now, take some time and just look around into the C++ class and structure.

Even with the extra class file and header file, it is a very simple program. It just reads the disks folders and looks through them for the thumbs.db files and if it finds one it zaps it. It adds the total number of files it deletes and the size of the files in case you are curious.

The best way to use this program is from a command prompt, but you can just double click on it from windows and it will still work. The only downfall to executing it by double click is that you won't be able to see the final results because it will close the console window immediately after the program exits.

I will be posting more of my little projects as time passes.

I will talk about Code::Blocks and MinGW coming up...