Tutorial 1: Setting it up , and shutting it down.

 

Ok, so let's start....

I wont discuss how nebula scripting works but the C/C++ part neither i'll discuss multiple OS, only Win32.

I've divided the program in 4 parts, Win32 stuff(in one file) and Init(), Main() (which handles updates , input and draws) and End().

In the Win32 part we have 2 functions and 1 global :

The WinMain function. very simple and basic.... init loop end...hehe :) i could have made the Main() func loop for itself , but if want to do some stuff out of Main() it is simpler that way.... in a way....

#define WIN32_LEAN_AND_MEAN
#include <windows.h> 

bool active = false;

void Init();
void Main();
void End();

// The includes, prototypes and the global.

// WINMAIN
////////////////////////////////////////////////

int WINAPI WinMain( 
   HINSTANCE hinstance, 
   HINSTANCE hprevinstance,
   LPSTR lpcmdline,
   int ncmdshow)
{

   Init();
   active = true;
   while(active)
   {
      Main();
   }

   End();
	
   return(msg.wParam);
}

stops the loop in WinMain... Called in Main();

void EndProgram()
{
   active = false;
}

Now lets get to the action... Some includes and globals...


The kernel server object is the kernel ( :)) ) of the engine.. i e it handles some low level events and resource management.... the input server handles input ;) and GfxServer handles the window

#include <kernel/nkernelserver.h>
#include <input/ninputserver.h>
#include <gfx/ngfxserver.h>

nKernelServer *KernelServer;
nInputServer *Input; 
nGfxServer *GfxServer;

void EndProgram();	//prototype

Kinda simple hum? you just allocate a kernel server through new and then request kernelserver a inputserver. I'm not really sure but i think it is in this call that the kernel server loads the dll. GfxServer just handles the window for now...it will init OGL (note "nglserver") , but we wont use it...

void Init()
{
   KernelServer = new nKernelServer;
   Input = (nInputServer *)
      KernelServer->New("ninputserver","/sys/servers/input");
   GfxServer = (nGfxServer *)ks->New("nglserver","/sys/servers/gfx");
   GfxServer->SetDisplayMode("-type(win)-w(640)-h(480)");		
      //set the display mode ( yes, through a string! ;))
   GfxServer->SetClearColor(1.0f,1.0f,1.0f,1.0f);	
      //if i dont set this the window will be all screwed up 
      //in my comp, so i left it over here..
   GfxServer->OpenDisplay();		
      //open the window
}

by reading the above comments you'll fairly get some idea of whats going on... i think.

void Main()
{
   Input->Trigger();
      // This func handles internal events for the inputserver
   nInputEvent *InpEvent;
      // data block that holds the info about
   InpEvent = Input->FirstEvent(); 
      // get the fist event on list (or stack , i dont know) ... 
      // the func returns NULL if none

   if(InpEvent) 
   // if we have an input event(user has pressed anything)
   {
      if (InpEvent->IsDisabled()) return; 
      // if Input is disabled (?)
      switch (InpEvent->type)
      {
         case N_INPUT_KEY_DOWN:
         // user has presseda key
         {
            switch (InpEvent->key) 
            // what key?
            {
               case N_KEY_ESCAPE: EndProgram(); break; 
                  // if escape key , call endprogram to 
                  // shut down the progam.
            }
         }
      }

      while ((InpEvent = Input->NextEvent(InpEvent)));
      // empty the list(or stack) of input events
      Input->FlushEvents();// the same as above i think
   }

   if(!GfxServer->Trigger())
      EndProgram(); 
   // hadndles win32 events . when it receives a close message it 
   // returns 0 ann EndProgram() is called...
}

closes everything

void End()
{
   GfxServer->CloseDisplay();
      //close the window
   if (GfxServer) GfxServer->Release();
      // typical safe release method
   if (Input) Input->Release();
      // typical safe release method
   delete ks; 
      // delete it unconditionaly!!!! 
      //(the destructor will take care of everything)
}

 

So this is the end of tut1... it shows how simple and clean nebula is... See ya!

(if you found and error or something, i'll be pleased if you warn me at [email protected] , thanks!)

Hosted by www.Geocities.ws

1