UniversalPUB / Mit++ Library Infothread 1
Posted: Wed Jul 14, 2010 2:23 am
Here we will dissect and discuss the sample codes provided with the library. The official name is UniversalPUB but I have unofficially dubbed this the Mit++ Library.
We will start, traditionally, with the classic 'Hello World' sample.
Also, please bear with me here. I'm not going to claim to know how this all works, as I'm rather new to this programming stuff. Some stuff is semi-documented within the code. I will do my best to outline most else. Some i just really have no idea yet what the technical function is, but I'm sure Mit could clue us in on some bits =).
Anyway, here we go. I will break this code in to blocks so my comments don't blend in with the code, but if you had the proper library files, you would be able to past in each box in succession and compile.
In this area, we tell the compiler what files will be included with this one.
Public variables and constructors and such will be available that are in the included file names specified.
WindowsMsgProc receives messages sent from Windows... There are many. the HWND tells it which window its referring to (i think lol).
the UINT msg is a general windows message. There are many as well here, a google search of either WM_ messages in the example will lead you to more. http://msdn.microsoft.com/en-us/library/ is a big help with info on stuff like that.
WPARAM is like a sub-message i guess. In the case of WM_KEYDOWN, it contains info on which key was pressed. VK_ESCAPE would obviously mean it was the ESCAPE key pressed. the PostQuitMessage( 0 ); tells it to exit that window(application). I believe it also sends a WM_DESTROY message from Windows.
switch( ) is a conditional statement. based upon the variable name you put between the ( )'s
So what it's doing is looking at (msg). whenever (msg) is WM_KEYDOWN, it then in turn does another switch to evaluate wParam. And, when it says the ESC key was pressed, it closes the program.
There must be a break; at the end of each condition of the switch. This tells it the switch is complete. If there is no break; it will just continue down and execute the next condition for some reason. And no, this isn't a drunken Mit coding issue, it's just how it is in the language =).
and default defines what to do if none of the conditions are met.
Initializes and creates the window to work within. Details are fuzzy here for now, except the InterfaceInitWindow specifies the title at the top bar of the window.
The MainInitialize section is just that. It initializes anything you may need initializing. It is called from the Main section found below, but that's not important right at the moment.
You may create a procedure that only needs to happen once when the program starts. This is where you would call it from.
This is executed once per frame. Documented clearly enough =) I'll just go over the text drawing details.
InterfaceText( 0, 10, 10, "Hello World", 0xFFFFFFFF, 1 );
InterfaceText is the name of the procedure to do this.
The first value passed with this is the layer to be rendered on. The lower the number renders first. Higher numbers render on top of lower numbers, covering them.
Second value specifies X coord, third is Y coord to start drawing at.
third value is what its going to print on the screen. After that is the color in hex value, and then the last number is the font to draw in (Havnt messed with changing fonts, or how the font number is determined yet).
INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR lpCmdLine, INT ) is the entry point of the application. this is called once, except the WHILE loop that runs continuous. Note here is where MainInitialize is called. It only runs once, t hen it hits the while and keeps that looping until it receives a quit message and shutdown is initalized.
Hope this helps a little bit. Next post we will go a little deeper and I will touch on the interface and how 2d graphics are put into play.
[/url]
We will start, traditionally, with the classic 'Hello World' sample.
Also, please bear with me here. I'm not going to claim to know how this all works, as I'm rather new to this programming stuff. Some stuff is semi-documented within the code. I will do my best to outline most else. Some i just really have no idea yet what the technical function is, but I'm sure Mit could clue us in on some bits =).
Anyway, here we go. I will break this code in to blocks so my comments don't blend in with the code, but if you had the proper library files, you would be able to past in each box in succession and compile.
Code: Select all
//-----------------------------------------------------------------------------------------------------------------------
// main.cpp
//
// HelloWorld sample for Interface library
//
//-----------------------------------------------------------------------------------------------------------------------
#include <StdWinInclude.h> // If we need to know about windows stuff like HWNDs, HINSTANCEs etc, then we need to include this first
#include <StandardDef.h> // This specifies a minimal set of 'standard' defines like BOOL
#include <Interface.h> // The 2d graphics interface library
//-----------------------------------------------------------------------------------------------------------------------
Public variables and constructors and such will be available that are in the included file names specified.
Code: Select all
//-----------------------------------------------------------------------------------------------------------------------
// ------------------------------------------- Windows specific stuff
HWND ghwndMain; // Global window handle
//-------------------------------------------------------------------------
// Function : WindowsMsgProc
// Description :
//-------------------------------------------------------------------------
LRESULT WINAPI WindowsMsgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
switch( msg )
{
case WM_KEYDOWN:
//Handle a key press event
switch (wParam)
{
case VK_ESCAPE:
PostQuitMessage( 0 );
return( DefWindowProc( hWnd, msg, wParam, lParam ) );
break;
}
break;
case WM_DESTROY:
// Handle close & quit
PostQuitMessage( 0 );
return 0;
break;
default:
break;
}
return( DefWindowProc( hWnd, msg, wParam, lParam ) );
}
the UINT msg is a general windows message. There are many as well here, a google search of either WM_ messages in the example will lead you to more. http://msdn.microsoft.com/en-us/library/ is a big help with info on stuff like that.
WPARAM is like a sub-message i guess. In the case of WM_KEYDOWN, it contains info on which key was pressed. VK_ESCAPE would obviously mean it was the ESCAPE key pressed. the PostQuitMessage( 0 ); tells it to exit that window(application). I believe it also sends a WM_DESTROY message from Windows.
switch( ) is a conditional statement. based upon the variable name you put between the ( )'s
So what it's doing is looking at (msg). whenever (msg) is WM_KEYDOWN, it then in turn does another switch to evaluate wParam. And, when it says the ESC key was pressed, it closes the program.
There must be a break; at the end of each condition of the switch. This tells it the switch is complete. If there is no break; it will just continue down and execute the next condition for some reason. And no, this isn't a drunken Mit coding issue, it's just how it is in the language =).
and default defines what to do if none of the conditions are met.
Code: Select all
//-------------------------------------------------------------------------
// Function : InitialiseWindow
// Description :
//-------------------------------------------------------------------------
void InitialiseWindow( void )
{
WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, WindowsMsgProc, 0L, 0L,
GetModuleHandle(NULL), NULL, NULL, NULL, NULL,
"UniversalHelloWorldSample", NULL };
// Create window
ghwndMain = InterfaceInitWindow( "Universal - Interface Library - HelloWorld sample", &wc );
}
//-----------------------------------------------------------------------------------------------------------------------
Code: Select all
//-------------------------------------------------------------------------
// Function : MainInitialise
// Description :
//-------------------------------------------------------------------------
void MainInitialise( void )
{
// Create the window for viewing things in
InitialiseWindow();
// Initialise D3D
InterfaceInitDisplayDevice( 0 );
// Initialise the interface
InterfaceInit();
}
You may create a procedure that only needs to happen once when the program starts. This is where you would call it from.
Code: Select all
//-------------------------------------------------------------------------
// Function : MainUpdate
// Description :
//-------------------------------------------------------------------------
void MainUpdate( void )
{
// New frame - clear the back buffer
InterfaceNewFrame( 0 );
// Signal start of rendering pass
InterfaceBeginRender();
// Draw stuff here
InterfaceText( 0, 10, 10, "Hello World", 0xFFFFFFFF, 1 );
// Flush the Interface renderer
InterfaceDraw();
// Signal end of this pass
InterfaceEndRender();
// Present the results to the user
InterfacePresent();
}
InterfaceText( 0, 10, 10, "Hello World", 0xFFFFFFFF, 1 );
InterfaceText is the name of the procedure to do this.
The first value passed with this is the layer to be rendered on. The lower the number renders first. Higher numbers render on top of lower numbers, covering them.
Second value specifies X coord, third is Y coord to start drawing at.
third value is what its going to print on the screen. After that is the color in hex value, and then the last number is the font to draw in (Havnt messed with changing fonts, or how the font number is determined yet).
Code: Select all
//-------------------------------------------------------------------------
// Function : MainShutdown
// Description :
//-------------------------------------------------------------------------
void MainShutdown( void )
{
InterfaceFree();
}
//-------------------------------------------------------------------------
// Function : WinMain
// Description : The application's entry point
//-------------------------------------------------------------------------
INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR lpCmdLine, INT )
{
// ghInstance = hInst;
MainInitialise();
MSG msg;
ZeroMemory( &msg, sizeof(msg) );
// Do the main loop
while( msg.message != WM_QUIT )
{
if ( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) )
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
else
{
MainUpdate();
}
}
MainShutdown();
return 0;
}
Hope this helps a little bit. Next post we will go a little deeper and I will touch on the interface and how 2d graphics are put into play.
[/url]