Showing posts with label DarkGDK. Show all posts
Showing posts with label DarkGDK. Show all posts

Animating 3d models in DarkGDK using dbAppendObject

Been watching 3d Animation tutorials lately but I’ve never really found a tutorial or detailed example for that matter that actually teaches how to animate 3d models in DarkGDK in a less unclear way. And so I’m sharing this example in hope that it will be used by some computer enthusiast out there like myself as a starting point in creating their very own 3d computer game.

I have used the Babe model in this example. Babe model is a Dark Matter Model that is accesible by default when you install DarkGDK and is located at C:\Program Files\The Game Creators\Dark GDK\Media\Dark Matter Models\People. The controls and actions of our model in this example are illustrated in the following table.

ControlsActions
W,A,S,DMove.x
TabDie.x
QImpact.x
No key PressedIdle.x


Enough said, here’s the code:


  1. // Dark GDK - The Game Creators - www.thegamecreators.com  
  2. // the wizard has created a very simple project that uses Dark GDK  
  3. // it contains the basic code for a GDK application  
  4. // whenever using Dark GDK you must ensure you include the header file  
  5. #include "DarkGDK.h"  
  6. // the main entry point for the application is this function  
  7. void DarkGDK ( void )  
  8. {  
  9. // turn on sync rate and set maximum rate to 30 fps  
  10.  dbSyncOn();  
  11.  dbSyncRate(30);  
  12.   
  13.  //loads our 3d model  
  14.  //we'll be borrowing the Babe DirectX Darkmatter model  
  15.  //it's included in the DarkGdk package located in  
  16.  //C:\Program Files\The Game Creators\Dark GDK\Media\Dark Matter Models\People\Babe  
  17.  //Just copy the contents of the Babe folder to your game project folder  
  18.  dbLoadObject("H-Babe-Idle.x",2);  
  19.  //Idle.x has 25 frames  
  20.  //to find how how many frames there is in a .x animation  
  21.  //Click Start>All Programs>The Game Creators>DarkGDK>Documentation>Information>Type "Frame"   
  22.  //in the Textbox and the command used to determine the number of frames will reveal itself to you.  
  23.  //appends the number of frames of the Move.x animation to the loaded model  
  24.  //Move.x has 25 frames  
  25.  dbAppendObject("H-Babe-Move.x",2,26);  
  26.  //Attack.x has 24 frames  
  27.  //adds the number of frames of Attack.x to the loaded model  
  28.  dbAppendObject("H-Babe-Attack1.x",2,51);  
  29.  //Die.x has 50 frames  
  30.  //adds the number of frames of Die.x to the loaded model  
  31.  dbAppendObject("H-Babe-Die.x",2,76);  
  32.  //Impact.x has 10 frames  
  33.  //adds the number of frames of Impact.x to the loaded model  
  34.  dbAppendObject("H-Babe-Impact.x",2,126);  
  35.  //positions the object in the -y axis cause its a little bit top aligned by default when loaded  
  36.  dbPositionObject(2,0,-1,0);  
  37.   
  38.   
  39.  while (LoopGDK())  
  40.  {  
  41.  //display descriptive text  
  42.  dbSetTextSize(14);  
  43.  dbText(10,0,"WASD-Movement");  
  44.  dbText(10,10,"Spacebar-Attack.x");  
  45.  dbText(10,20,"Tab-Die.x");  
  46.  dbText(10,30,"Q-Impact.x");  
  47.  dbText(10,40,"No key pressed-Idle.x");  
  48.     
  49.   
  50.    
  51.    //if the W or A or S or D or Spacebar or Tab or Q is pressed  
  52.    if ( dbKeyState(17) || dbKeyState(31) ||dbKeyState(32) || dbKeyState(30)||dbKeyState(57) || dbKeyState(15)|| dbKeyState(16))  
  53.    {  
  54.    //stop the presently looping animation  
  55.   dbStopObject(2);  
  56.    //retrieves the object present angle  
  57.    int ObjAngleY=dbObjectAngleY(2);  
  58.    //determines if the object is presently looping  
  59.    int looping=dbObjectLooping(2);  
  60.       //if the key pressed is W  
  61.    if(dbKeyState(17))   
  62.    {   
  63.    //move the object on the -Z axis  
  64.      dbMoveObject(2,-0.05f);  
  65.    //is the object presently not playing?  
  66.    if (looping==0 )  
  67.    {  
  68.    //if yes then execute move.x  
  69.       //move Move.x has 25 frames  
  70.    //the number of frames from the starting frame to the ending frame must be at least 25 frames  
  71.   
  72.     dbLoopObject ( 2,26, 51 );  
  73.    };  
  74.    }  
  75.    //if the key pressed is S  
  76.    if (dbKeyState(31))  
  77.    {  
  78.    //move the object on the Z axis  
  79.     dbMoveObject(2,0.05f);  
  80.    //is the object currently not playing?  
  81.    if (looping==0 )  
  82.    {  
  83.       //if yes then execute move.x  
  84.       //move directx animation has 25 frames  
  85.    //starting frame up to the end frame must be at least 25 frames  
  86.   
  87.     dbLoopObject ( 2,26, 51 );  
  88.   
  89.    };  
  90.    }  
  91.    //if the A key is pressed  
  92.   
  93.    if (dbKeyState(30))  
  94.    {  
  95.   //rotate the object along the Y axis  
  96.   //for instance, if the objects current angle is 275-5  
  97.   //The object will then be rotated 270 degrees which is exactly south  
  98.   //from the user's POV  
  99.   //the dbWrapValue is to ensure that the object rotation will not exceed 360 degrees  
  100.   
  101.     dbYRotateObject(2,dbWrapValue(ObjAngleY-5.0f));  
  102.    }  
  103.   
  104.   //if the D key is pressed  
  105.    if (dbKeyState(32))  
  106.    {  
  107.    
  108.      //rotate the object along the Y axis  
  109.   //for instance, if the objects current angle is 85+5  
  110.   //The object will then be rotated 90 degrees which is exactly north  
  111.   //from the user's POV  
  112.   //the dbWrapValue is to ensure that the object rotation will not exceed 360 degrees  
  113.     dbYRotateObject(2,dbWrapValue(ObjAngleY+5.0f));  
  114.    }  
  115.      //if the space key is pressed  
  116.    if(dbKeyState(57))  
  117.    {  
  118.   //is the object currently not playing?  
  119.    if (looping==0 )  
  120.    {  
  121.    //if yes then execute Attack.x  
  122.    //Attack.x has 24 frames so from the starting to ending frames  
  123.    //must be at least 24 frames  
  124.     dbLoopObject ( 2,51,77 );  
  125.   
  126.   
  127.    };  
  128.    }  
  129.    //if the Tab key is pressed  
  130.    if(dbKeyState(15))  
  131.    {  
  132.    //is the object currently not playing?  
  133.    if (looping==0 )  
  134.    {  
  135.    //if yes then execute Die.x  
  136.    //Die.x has 50 frames so from the starting to ending frames  
  137.    //must be at least 50 frames  
  138.     dbLoopObject ( 2,78,127 );  
  139.   
  140.   
  141.    };  
  142.    }  
  143.        //if the Q key is pressed  
  144.     
  145.    if(dbKeyState(16))  
  146.    {  
  147.   //is the object currently not playing?  
  148.    if (looping==0 )  
  149.    {  
  150.   //if yes then execute Impact.x  
  151.      //Impact.x has 10 frames so from the starting to ending frames  
  152.      //must be at least 10 frames  
  153.     dbLoopObject ( 2,127,137 );  
  154.   
  155.   
  156.    };  
  157.    }  
  158.   
  159.   
  160.    }  
  161.   else  
  162.   //if no key is pressed  
  163.   //execute Idle.x  
  164.   //Idle.x has 25 frames so from the starting to the ending frames miust be at least 25 frames  
  165.   {  
  166.    dbLoopObject(2,1,25);  
  167.   }  
  168. // update the screen  
  169.   dbSync();  
  170.  }   
  171. //return to windows  
  172. return;  
  173. }  



Another way of animating models is by using the dbShowObject and dbHideObject commands. I'll post an example of that here when I have time. If you want more information on animating darkGDK models or creating games per se, you can visit DarkGDK's official site www.thegamecreators.com

Creating a Hit Points (HP) Indicator in DarkGDK

HP indicators are normally used in Adventure games to show the character’s present HP level. There are several ways to create an HP level indicator in DarkGDK but the easiest way (I guess) is to create a sprite sheet with three or more columns and with different HP numbers per column. For instance, three HP’s on the first column, two on the second and one on the last. If a character dies, display the appropriate column relative to the character’s HP level. This method can also be used to specify the remaining number of bullets left or the character’s current energy level. If you are confused, follow these steps:

1. Start Microsoft Visual 2008 C++ Express Edition.

2. Click File>New Project>Select the Wizards Project type>Select Dark GDK-2d Game from the Visual Studio installed templates.

3. Click View>Solution Explorer. Double-click main.cpp. The following should then appear:
  1. // Dark GDK - The Game Creators - www.thegamecreators.com  
  2.   
  3. // the wizard has created a very simple 2D project that uses Dark GDK  
  4. // it can be used as a starting point in making your own 2D games  
  5.   
  6. // whenever using Dark GDK you must ensure you include the header file  
  7. #include "DarkGDK.h"  
  8.   
  9. // the main entry point for the application is this function  
  10. void DarkGDK ( void )  
  11. {  
  12. // in this application a backdrop is loaded and then several  
  13. // animated sprites are displayed on screen  
  14.   
  15. // when starting a Dark GDK program it is useful to set global  
  16. // application properties, we begin by turning the sync rate on,  
  17. // this means we control when the screen is updated, we also set  
  18. // the maximum rate to 60 which means the maximum frame rate will  
  19. // be set at 60 frames per second  
  20. dbSyncOn   ( );  
  21. dbSyncRate ( 60 );  
  22.   
  23. // a call is made to this function so we can stop the GDK from  
  24. // responding to the escape key, we can then add in some code in our  
  25. // main loop so we can control what happens when the escape key is pressed  
  26. dbDisableEscapeKey ( );  
  27.   
  28. // now we will set the random seed value to the timer, this will  
  29. // help us to get more random values each time we run the program  
  30. dbRandomize ( dbTimer ( ) );  
  31.   
  32. // we are going to display a backdrop for the scene, to do this  
  33. // we load our image and give it an ID number of 1, this particular  
  34. // image is of a sky at night with stars  
  35. dbLoadImage ( "backdrop.bmp", 1 );  
  36.   
  37. // the next step is to create a sprite that uses this image, this  
  38. // is achieved by calling dbSprite and passing in a value of 1 for the  
  39. // sprites ID, 0 for the X coordinate, 0 for the Y coordinates and a  
  40. // value of 1 for the image  
  41. dbSprite ( 1, 0, 0, 1 );  
  42.   
  43. // next we will load in some animated sprites, before doing this  
  44. // we need to adjust the image color key, by using this function we  
  45. // can make a specific color be transparent, in our case we want this  
  46. // to be bright pink  
  47. dbSetImageColorKey ( 255, 0, 255 );  
  48.   
  49. // in this loop we're going to create some animated sprites, the image  
  50. // we load contains frames of animation for an asteroid  
  51. for ( int i = 2; i < 30; i++ )  
  52.  {  
  53.   // create an animated sprite and give it the ID number from the  
  54.   // variable i, next is the filename, now we come to how many frames  
  55.   // across and down, in our case this is 4, finally we come to the image  
  56.   // ID that the sprite will use, again we use i  
  57.   dbCreateAnimatedSprite ( i, "sprite.bmp", 4, 4, i );  
  58.   
  59.   // position our sprite at a random location  
  60.   dbSprite ( i, dbRnd ( 640 ), -dbRnd ( 1500 ), i );  
  61.  }  
  62.   
  63.  // now we come to our main loop, we call LoopGDK so some internal  
  64.  // work can be carried out by the GDK  
  65.  while ( LoopGDK ( ) )  
  66.  {  
  67.   // run a loop through all our sprites  
  68.   for ( int i = 2; i < 30; i++ )  
  69.   {  
  70.    // move the sprite down and play its animation  
  71.    // moving from frame 1 to 16 with a delay of 60 ms  
  72.    dbMoveSprite ( i, -2 );  
  73.    dbPlaySprite ( i, 1, 16, 60 );  
  74.   
  75.    // check the position of the sprite, if it has gone off scren  
  76.    // then reposition it back to the top  
  77.    if ( dbSpriteY ( i ) > 500 )  
  78. dbSprite ( i, dbRnd ( 640 ), -dbRnd ( 1500 ), i );  
  79. }  
  80.   
  81. // here we check if the escape key has been pressed, when it has  
  82. // we will break out of the loop  
  83. if ( dbEscapeKey ( ) )  
  84. break;  
  85.   
  86. // here we make a call to update the contents of the screen  
  87. dbSync ( );  
  88. }  
  89.   
  90. // when the user presses escape the code will break out to this location  
  91. // and we can free up any previously allocated resources  
  92.   
  93. // delete all the sprites  
  94. for ( int i = 1; i < 30; i++ )  
  95.   dbDeleteSprite ( i );  
  96.   
  97.  // delete the backdrop image  
  98.  dbDeleteImage ( 1 );  
  99.   
  100.  // and now everything is ready to return back to Windows  
  101.  return;  
  102. }  
4. Since we intend to make our sample application from the scratch, delete all the pre-made codes, except the following:
  1. // Dark GDK - The Game Creators - www.thegamecreators.com  
  2. #include "DarkGDK.h"  
  3.   
  4. // the main entry point for the application is this function  
  5. void DarkGDK ( void )  
  6. {  
  7.    
  8.  dbSyncOn   ( );  
  9.  dbSyncRate ( 60 );  
  10.   
  11.    
  12.  while ( LoopGDK ( ) )  
  13.  {  
  14.     
  15.   
  16.   // here we make a call to update the contents of the screen  
  17.   dbSync ( );  
  18.  }  
  19.   
  20. }  
5. Make a spritesheet using gimp or photoshop. In this example we will be using the following image: 6. Later on we will be dividing this image into three columns. If you noticed if we divide this image into three divisions each divisions we will have different number of HP’s. 7. Copy this image from whatever location you have saved it. In this example, I have saved it on my desktop so all I need to do is go to the desktop. Select the file “hplevel.bmp”. Then press CTRL + C to copy it.
8. Switch to Micorsoft Visual C++ 2008 DarkGDK to view our game again then click the Open File icon from the formatting toolbar. 9. This causes the Open File dialog box to appear. Click on the empty pane>Press Ctrl + V to paste our “hplevel.bmp” file. 10. The next thing that you’ll need to do is to load this image into the computer’s memory, and divide it into three columns. We can do so by using the dbCreateAnimatedSprite command. Click the cancel button from the open dialog box to close it. This causes our code window to appear. Enter the ff. after the dbSyncRate(60); line.
  1. //divides the image into 3 columns  
  2. dbCreateAnimatedSprite(1,"hplevel.bmp",3,1,1);  
11. If you run your application now, it will just display a blue screen. To display your animated sprite,use the dbSprite command. Enter the following code after the dbCreateAnimatedSprite line.
  1. //displays our animated sprite  
  2. dbSprite(1,0,0,1);  
12. Press Ctrl+ F5 to test our application again. If you noticed the first column of our animated sprite is shown together with a white background. To clear the white background, use the dbSetImageColorKey command. Enter the following before the dbCreateAnimated line:
  1. //clears the white background  
  2. dbSetImageColorKey(255,255,255);  
13. Your code should now look like this:
  1. // Dark GDK - The Game Creators - www.thegamecreators.com  
  2. #include "DarkGDK.h"  
  3.   
  4. // the main entry point for the application is this function  
  5. void DarkGDK ( void )  
  6. {  
  7.   
  8. dbSyncOn   ( );  
  9. dbSyncRate ( 60 );  
  10. //clears the white background  
  11. dbSetImageColorKey(dbRgb(255,255,255));  
  12. //divides the image into 3 columns  
  13. dbCreateAnimatedSprite(1,"hplevel.bmp",3,1,1);  
  14. //displays our animated sprite  
  15. dbSprite(1,0,0,1);  
  16.   
  17.   
  18.   
  19. while ( LoopGDK ( ) )  
  20. {  
  21.   
  22.   
  23. // here we make a call to update the contents of the screen  
  24. dbSync ( );  
  25. }  
  26.   
  27. }  
14. The next thing that we want to do is to subtract the number of HP’s everytime the space key is pressed. Of course you can replace this with more advanced algorithms such as when your player dies or when a shot is fired, etc. But for the sake of simplicity, lets just go with the space key(hehehe). 15. Declare an integer variable named intspritesheetcols just below #include "DarkGDK.h" line and assign a a default value 1. 16. Enter the following after the while ( LoopGDK ( ) ){
  1. //you can replace dbSpaceKey with  "if a player dies" or "if a gun is fired" algorithms  
  2. if (dbSpaceKey())  
  3. {  
  4. //increment the value of intspritesheetcols by 1  
  5. //if you recall we've initially assigned a starting value 1 to  
  6. //our intspritesheetcols variable. If you press the  
  7. //the space key for the first time it will be incremented by 1  
  8. intspritesheetcols=intspritesheetcols + 1;  
  9. //displays the spritesheet division relative to the specified  
  10. //intspritesheetcols value. The first time you press the space key  
  11. //intspritescols will have a value to causing the second column  
  12. //of our spritesheet to be displayed  
  13. dbSetSpriteFrame(1,intspritesheetcols);  
  14. //displays our sprite  
  15. dbSprite(1,0,0,1);  
  16. }  
  17. //if the number of columns exceeds 3  
  18. //you can display your game over screen here but in this case let's  
  19. //just exit the game  
  20. if(intspritesheetcols>3)  
  21. {  
  22. return;  
  23. }  
17. Your whole code should now look like this:
  1. // Dark GDK - The Game Creators - www.thegamecreators.com  
  2. #include "DarkGDK.h"  
  3. int intspritesheetcols=1;  
  4. // the main entry point for the application is this function  
  5. void DarkGDK ( void )  
  6. {  
  7.   
  8. dbSyncOn   ( );  
  9. dbSyncRate(5);  
  10. //clears the white background  
  11. dbSetImageColorKey(255,255,255);  
  12. //divides the image into 3 columns  
  13. dbCreateAnimatedSprite(1,"hplevel.bmp",3,1,1);  
  14. //displays our animated sprite  
  15. dbSprite(1,0,0,1);  
  16.   
  17. while ( LoopGDK ( ) )  
  18. {  
  19. //you can replace dbSpaceKey with  "if a player dies" or "if a gun is fired" algorithms  
  20. if (dbSpaceKey())  
  21. {  
  22. //increment the value of intspritesheetcols by 1  
  23. //if you recall we've initially assigned a starting value 1 to  
  24. //our intspritesheetcols variable. If you press the  
  25. //the space key for the first time it will be incremented by 1  
  26. intspritesheetcols=intspritesheetcols + 1;  
  27. //displays the spritesheet division relative to the specified  
  28. //intspritesheetcols value. The first time you press the space key  
  29. //intspritescols will have a value to causing the second column  
  30. //of our spritesheet to be displayed  
  31. dbSetSpriteFrame(1,intspritesheetcols);  
  32. //displays our sprite  
  33. dbSprite(1,0,0,1);  
  34. }  
  35. //if the number of columns exceeds 3  
  36. //you can display your game over screen here but in this case let's  
  37. //just exit the game  
  38. if(intspritesheetcols>3)  
  39. {  
  40. return;  
  41. }  
  42. // here we make a call to update the contents of the screen  
  43. dbSync ( );  
  44. }  
  45.   
  46. }  
18. Press CTRL + F5 to run your application. You should now see an output similar to the following:
19. That’s all.

Creating a menu system in Dark GDK

There are several ways on how to create a menu system in Dark GDK but since I hate to complicate simple things, today we will be learning the simplest. The idea is to assign a sprite as a customized mouse pointer and hide that sprite when your game is loaded using dbHideSprite so that only the mouse pointer is visible and not your customized sprite pointer. The next problem would be to test which menu item or button was clicked, to be able to do that, simply use dbSpriteHit or dbSpriteCollission to check whether your hidden sprite acting as your mouse pointer collided with your button sprite. It’s that simple. If you are confused, follow these steps:

1. Before the actual programming, draw the buttons and customized mouse pointer that will be needed in your game using your favorite image creation software such as Paint, GIMP, or PhotoShop. It does not matter how your customized mouse pointer looks like because it will not show up on your form, just make sure that you buttons and your customized pointer are of the same height and width.



2. Start Microsoft Visual C++. Click File>New>Project>Select Wizards>Dark GDK-Game>Type “Using Menu” in the name textbox no quotes.

3. Minimize the MSVC++ window then go to My Documents>Visual Studio 2008>Projects>Using Menu>Using Menu> Then paste the images that you have created in step 1.



4. Maximize Visual C++>View>Solution Explorer>Double-Click Main.cpp. The following should then come into view:
  1. // Dark GDK - The Game Creators - www.thegamecreators.com  
  2.   
  3. // the wizard has created a very simple project that uses Dark GDK  
  4. // it contains the basic code for a GDK application  
  5.   
  6. // whenever using Dark GDK you must ensure you include the header file  
  7. #include "DarkGDK.h"  
  8.   
  9. // the main entry point for the application is this function  
  10. void DarkGDK ( void )  
  11. {  
  12.  // turn on sync rate and set maximum rate to 60 fps  
  13.  dbSyncOn   ( );  
  14.  dbSyncRate ( 60 );  
  15.   
  16.  // our main loop  
  17.  while ( LoopGDK ( ) )  
  18.  {  
  19.   // update the screen  
  20.   dbSync ( );  
  21.  }  
  22.   
  23.  // return back to windows  
  24.  return;  
  25. }  

5. Locate the line void DarkGDK ( void ) then type the following after the line dbSyncRate ( 60 );
  1. //loads our images  
  2. dbLoadImage("hibutton.jpg",1);  
  3. dbLoadImage("exitbutton.jpg",2);  
  4. dbLoadImage("customizedpointer.jpg",3);  
  5. //displays our images  
  6. dbSprite(1,400,420,1);  
  7. dbSprite(2,500,420,2);  

6.If you try to run this,only the "Say Hi" and "Exit" buttons are visible.That's because we haven't display our sprite pointer yet.To view it, locate the line while ( LoopGDK ( ) ) and type the following aster the open curly bracket:

  1. //assigns the currentX and CurrentY position  
  2. //of our mouse an X and Y coordinate of  
  3. //our customized pointer  
  4. //we need to place it inside the  
  5. //while loop so that it is always  
  6. //updated depending on the current mouse coordinate  
  7. dbSprite(3,dbMouseX(),dbMouseY(),3);  
  8. //hides the sprite that acts as our pointer  
  9. dbHideSprite(3);  

7. The next thing that we need to do is to enable our button to react to mouse click. To be able to do that locate the line dbHideSprite(3); and type the ff. below it:

  1. //if our hidden sprite collided with the “Say Hi” button   
  2. //and the mouse button is clicked then  
  3. if (dbSpriteCollision(3,1)==1 && dbMouseClick()==1)  
  4. {  
  5. //do something…you can add your game sequence action codes here  
  6. //but in this case we will be displaying a “Hi!” text  
  7. dbSetTextFont("Garamond");  
  8. dbSetTextSize(70);  
  9. dbText(0,0,"Hi!");        
  10. }  
  11. //if our hidden sprite collided with the “Exit” button   
  12. //and the mouse button is clicked then  
  13. if (dbSpriteCollision(3,2)==1 && dbMouseClick()==1)  
  14. {  
  15. //quit the form  
  16. return;   
  17. }  

8. Let us have a quick look of our codes once again:

  1. // Dark GDK - The Game Creators - www.thegamecreators.com  
  2.   
  3. // the wizard has created a very simple project that uses Dark GDK  
  4. // it contains the basic code for a GDK application  
  5.   
  6. // whenever using Dark GDK you must ensure you include the header file  
  7. #include "DarkGDK.h"  
  8.   
  9. // the main entry point for the application is this function  
  10. void DarkGDK ( void )  
  11. {  
  12.  // turn on sync rate and set maximum rate to 60 fps  
  13.  dbSyncOn   ( );  
  14.  dbSyncRate ( 60 );  
  15.  //loads our images  
  16.  dbLoadImage("hibutton.jpg",1);  
  17.  dbLoadImage("exitbutton.jpg",2);  
  18.  dbLoadImage("customizedpointer.jpg",3);  
  19.  //displays our images  
  20.  dbSprite(1,400,420,1);  
  21.  dbSprite(2,500,420,2);  
  22.   
  23.   
  24.   
  25.  // our main loop  
  26.  while ( LoopGDK ( ) )  
  27.  {  
  28.    
  29.   dbSprite(3,dbMouseX(),dbMouseY(),3);  
  30.   dbHideSprite(3);  
  31.   if (dbSpriteCollision(3,1)==1 && dbMouseClick()==1)  
  32.   {  
  33.    dbSetTextFont("Garamond");  
  34.    dbSetTextSize(70);  
  35.    dbText(0,0,"Hi!");   
  36.   }  
  37.   if (dbSpriteCollision(3,2)==1 && dbMouseClick()==1)  
  38.   {  
  39.   return;   
  40.   }  
  41.   
  42.   
  43.   // update the screen  
  44.   dbSync ( );  
  45.  }  
  46.   
  47.  // return back to windows  
  48.  return;  
  49. }  

9. Press F5 to test your game. Try clicking the “Say Hi” and "Exit" button.



10. And that would be all. One of the first menu system tutorial in Dark GDK. My buttons looks disgusting because I just rushed it, I didn’t have time to make one in Blender. Hope you learn something from it, till next time.

A simple 2D pong game in darkGDK

In case you are wondering, Pong is a video game that simulates ping pong. It is a game where you maneuver a bat or paddle by pressing a control or joystick and the goal of the game is to return the ball to the opposing side just like the real ping pong. If you totally don't have any idea how pong works, I suggest googling it first before moving onto the next section. Being accustomed with the game and how it works greatly aids in trasforming it into codes.

Apart from familiarizing the game, we also need to learn few darkGDK game operations and the underlying commands behind them. These operations and commands are enumerated below:

1. Displaying an Image

Before you can display an image in darkGDK, you'll need to load it first into the computer memory. To be able to do that, use the dbLoadImage command which has the following syntax:
  1. dbLoadImage("Imagefilename",imageloadid);  
Where;
1. Imagefilename is an existent image file saved inside your game project folder. darkGDK supports .bmp, .jpg, and .png image file types.

2. imageloadid is a unique image numeric id that can be used later on to refer to a loaded image.

Example:
  1. dbLoadImage("Galaxian.jpg",1);  
Once an image is loaded in the computer memory, you can then display it by using dbPasteImage or dbSprite. dbSprite has the following syntax:
  1. dbSprite(spriteid, x-coordinate, y-coordinate, imageloadid);  
Where;
1. spriteid is a numerical id that can be use to manipulate the displayed image. spriteid can be identical to the imageloadid.

2. x-coordinate and y-coordinate are the x and y coordinate of the upper-left corner of your sprite or image. If you are unfamiliar of x and y coordinate, think of the screen as the fourth quadrant of the Cartesian coordinate system. The upper left corner is the origin which has the coordinate of x=0,y=0. The value of x coordinate increases as you move right. The value of the y coordinate increases as you move down.

3. imageloadid is the numeric id of a loaded image that you wanted to display.

Example:
  1. dbSprite(1,0,01);  
2. Moving Sprites

Sprites and images can be moved or animated by using dbMoveSprite or by simply incrementing or decrementing the x and y coordinate of an image. If you are familiar with java, c++, or visual basic, you probably know what I am talking about.

3. Detecting Collisions

darkGDK comes with powerful command that can be used to detect sprite or object collision. To detect collision, simply use dbSpriteCollission or dbSpriteHit. dbSpriteHit has the following syntax:
  1. dbSpriteHit(Spriteid1, Spriteid2);  
Where;
1. Spriteid1 and Spriteid2 are the numeric spriteids of the images that you wanted to test for collision.

Now that you have learned few basic commands and operations in darkGDK,I guess we can proceed into making our pong game. The following steps demonstrates how:

1. Click Start>All Programs>Microsoft Visual C++ Express Edition.

2. Click on File>New>Project. In the Project Types tree control, select on Wizards.

3. Select Dark GDK-Game from the available Game project options.

4. Type “Pong” no quotes in the Project Name textbox then Click Ok.

5. Click on View>Solution Explorer. Locate Main.cpp in the Solution Explorer Window then double-click it.

6. The following should then appear:

  1. // Dark GDK - The Game Creators - www.thegamecreators.com     
  2. // the wizard has created a very simple project that uses Dark GDK     
  3. // it contains the basic code for a GDK application       
  4. // whenever using Dark GDK you must ensure you include the header file     
  5. #include "DarkGDK.h"      
  6. // the main entry point for the application is this function     
  7. void DarkGDK ( void )     
  8. {     
  9. // turn on sync rate and set maximum rate to 60 fps     
  10. dbSyncOn   ( );     
  11. dbSyncRate ( 60 );       
  12. // our main loop     
  13. while ( LoopGDK ( ) )     
  14. {     
  15. // update the screen     
  16. dbSync ( );     
  17. }       
  18. // return back to windows     
  19. return;     
  20. }     

7. Locate the line #include "DarkGDK.h" and declare the following variables below it:
  1. //handles the ball velocity, the larger the value   
  2. //the faster the ball will move  
  3. int ballxvelocity=5,ballyvelocity=5;  
  4. //handles the updated x and y coordinate of the ball  
  5. int ballxcoord,ballycoord;  
  6. //handles the updated x and y coordinate   
  7. //of player1's paddle  
  8. int bat1xcoord,bat1ycoord;  
  9. //handles the updated x and y coordinate   
  10. //of player2's paddle  
  11. int bat2xcoord,bat2ycoord;  

8. Locate the line dbSyncOn and dbSynRate(60) then type the following below it:
  1. //sets the window title to pong  
  2. dbSetWindowTitle("Pong");  
  3. //sets the window size to 600x400 that is  
  4. //x coordinates runs from 0-600  
  5. //y coordinates runs from 0-400  
  6. dbSetWindowSize(600,400);  
  7. //loads an image located in your game project folder  
  8. //and gives it an id of 1  
  9. dbLoadImage("ball.png",1);  
  10. //loads an image located in your game project folder  
  11. //and gives it an id of 2  
  12. dbLoadImage("bat1.jpg",2);  
  13. //loads an image located in your game project folder  
  14. //and gives it an id of 3  
  15. dbLoadImage("bat2.jpg",3);  
  16.   
  17. //displays the ball at the center of the screen  
  18. dbSprite(1,300,200,1);  
  19. //player1 paddle at the left side of the screen  
  20. dbSprite(2,50,150,2);   
  21. //player2 paddle at the right side of the screen   
  22. dbSprite(3,500,150,3);  


9. Locate the line while ( LoopGDK ( ) ) and type the following after the open curly bracket:
  1. //move the ball  
  2. //adds the value of ballxvelocity   
  3. //and the current x coordinate of the ball  
  4. //and assign it as a new ball x coord value  
  5. ballxcoord=dbSpriteX(1) + ballxvelocity;  
  6. //do the same with y  
  7. ballycoord=dbSpriteY(1) + ballyvelocity;  
  8. //displays the ball in the updated x and y coord positions  
  9. dbSprite(1,ballxcoord,ballycoord,1);  
  10. //if the ball collides with the   
  11. //top or bottom of the screen then  
  12. if (ballycoord <= 0 || ballycoord >= 400)   
  13. {  
  14. //Multiply ballyvelocity by negative 1  
  15. //The result will  later on be added to the  
  16. //current y coordinate of the ball  
  17. //causing the ball to change position  
  18. //depending on the value of ballyvelocity and  
  19. //the current coord of the ball  
  20. ballyvelocity=ballyvelocity*-1;  
  21. }  
  22. //if the ball collides with the left side   
  23. //or right side of the screen then  
  24. if (ballxcoord <= 0 || ballxcoord >= 600)  
  25. {  
  26. //Multiply ballxvelocity by negative 1  
  27. //The result will later on be added to the  
  28. //current x coordinate of the ball  
  29. //causing the ball to change position  
  30. //depending on the value of ballxvelocity and  
  31. //the current x coord of the ball  
  32. ballxvelocity=ballxvelocity*-1;  
  33. }  
  34.   
  35. //check bat collision  
  36. //if the ball collides with player 1's paddle  
  37. if (dbSpriteHit(1,2)==1)  
  38. {  
  39. //if at this point you still don't   
  40. //understand what this do  
  41. //I suggest recalling the rules in multiplying  
  42. //and adding negative numbers  
  43. ballxvelocity=ballxvelocity*-1;  
  44. }  
  45. //if the ball collides with player 2's paddle  
  46. if (dbSpriteHit(1,3)==1)  
  47. {  
  48. //reverse the direction of the ball  
  49. ballxvelocity=ballxvelocity*-1;  
  50. }  
  51.   
  52. //move the player 1 bat  
  53. //if the enter key is pressed  
  54. if (dbReturnKey()==1)  
  55. {  
  56. //decrement the paddle y coord value  
  57. //causing the paddle to move up  
  58. bat1ycoord=dbSpriteY(2)-5;  
  59. //display the paddle in the  
  60. //updated position  
  61. dbSprite(2,bat1xcoord,bat1ycoord,2);  
  62. }  
  63. //if the down arrow is pressed  
  64. if (dbShiftKey()==1)  
  65. {    
  66. //increment the paddle y coord value  
  67. //causing the paddle to move down  
  68. bat1ycoord=dbSpriteY(2)+5;  
  69. //display the paddle in its  
  70. //updated position  
  71.   
  72.   
  73.   
  74. //move player 2 bat  
  75. //if the up arrow is pressed  
  76. if (dbUpKey()==1)  
  77. {  
  78. //decrement the paddle y coord value  
  79. //causing the paddle to move up  
  80. bat2ycoord=dbSpriteY(3)-5;  
  81. dbSprite(3,bat2xcoord,bat2ycoord,3);  
  82. }  
  83. //if the shift key is pressed  
  84. if (dbDownKey()==1)  
  85. {  
  86. //increment the paddle y coord value  
  87. //causing the paddle to move down  
  88. bat2ycoord=dbSpriteY(3)+5;  
  89. dbSprite(3,bat2xcoord,bat2ycoord,3);  
  90. }  
10. Locate the line dbSync ( ); and type the following after the open curly bracket:
  1. //Deletes the loaded image  
  2. //and sprites when the game is closed  
  3. //this will free up the used memory  
  4. for ( int i = 1; i < 30; i++ )  
  5. dbDeleteSprite ( i );  
  6. dbDeleteImage ( 1 );  
11. Let's have a glance of our codes again and this time comments omitted:
  1. // Dark GDK - The Game Creators - www.thegamecreators.com  
  2.   
  3. // the wizard has created a very simple project that uses Dark GDK  
  4. // it contains the basic code for a GDK application  
  5.   
  6. // whenever using Dark GDK you must ensure you include the header file  
  7. #include "DarkGDK.h"  
  8. int ballxvelocity=5,ballyvelocity=5;  
  9. int ballxcoord,ballycoord;  
  10. int bat1xcoord,bat1ycoord;  
  11. int bat2xcoord,bat2ycoord;  
  12. // the main entry point for the application is this function  
  13. void DarkGDK ( void )  
  14. {  
  15. // turn on sync rate and set maximum rate to 60 fps  
  16. dbSyncOn   ( );  
  17. dbSyncRate ( 60 );  
  18. dbSetWindowTitle("Pong");  
  19. dbSetWindowSize(600,400);  
  20. dbLoadImage("ball.png",1);  
  21. dbLoadImage("bat1.jpg",2);  
  22. dbLoadImage("bat2.jpg",3);  
  23.   
  24.    
  25. dbSprite(1,300,200,1);     
  26. dbSprite(2,50,150,2);    
  27. dbSprite(3,500,150,3);    
  28.   
  29.   
  30.   
  31. // our main loop  
  32. while ( LoopGDK ( ) )  
  33. {  
  34. //move the ball  
  35. ballxcoord=dbSpriteX(1) + ballxvelocity;  
  36. ballycoord=dbSpriteY(1) + ballyvelocity;  
  37. dbSprite(1,ballxcoord,ballycoord,1);  
  38. if (ballycoord <= 0 || ballycoord >= 400)   
  39. {  
  40. ballyvelocity=ballyvelocity*-1;  
  41. }  
  42. if (ballxcoord <= 0 || ballxcoord >= 600)  
  43. {  
  44. ballxvelocity=ballxvelocity*-1;  
  45. }  
  46.   
  47. //check bat collission  
  48. if (dbSpriteHit(1,2)==1)  
  49. {  
  50.   
  51. ballxvelocity=ballxvelocity*-1;  
  52. }  
  53. if (dbSpriteHit(1,3)==1)  
  54. {  
  55.   
  56. ballxvelocity=ballxvelocity*-1;  
  57. }  
  58.   
  59. //moves player 1 bat  
  60. if (dbReturnKey()==1)  
  61. {  
  62. bat1ycoord=dbSpriteY(2)-5;  
  63. dbSprite(2,bat1xcoord,bat1ycoord,2);  
  64. }  
  65. if (dbShiftKey()==1)  
  66. {  
  67. bat1ycoord=dbSpriteY(2)+5;  
  68. dbSprite(2,bat1xcoord,bat1ycoord,2);  
  69. }  
  70.   
  71.   
  72. //moves player 2 bat  
  73. if (dbUpKey()==1)  
  74. {  
  75. bat2ycoord=dbSpriteY(3)-5;  
  76. dbSprite(3,bat2xcoord,bat2ycoord,3);  
  77. }  
  78. if (dbDownKey()==1)  
  79. {  
  80. bat2ycoord=dbSpriteY(3)+5;  
  81. dbSprite(3,bat2xcoord,bat2ycoord,3);  
  82. }  
  83.   
  84.   
  85. // update the screen  
  86. dbSync ( );  
  87. }  
  88. for ( int i = 1; i < 30; i++ )  
  89. {  
  90. dbDeleteSprite ( i );  
  91. dbDeleteImage ( 1 );  
  92. }  
  93. // return back to windows  
  94. return;  
  95. }  

12. Press F5 to test the game. You can press the Enter key and shift key to move player1's bat and the up and down arrow to move player2's bat.

13. And that's it. A very simple pong game using few lines of codes. At the point of writing I guess this was the very first pong game tutorial written using darkGDK. I will add attract mode animation, menus, scores, and AI enemy to this when I have time but for now, though it's incomplete, you can use it as starting point. Hope you'll learn something from it and enjoy the game.

A simple 2D Color Slot Machine in Dark GDK

I’m sure every one of us is familiar with slot machine or fruit machine game. Today we will be making a slight version of that game, and we are going to name it as “Color Slot Machine”. There are several elements missing in this game such as the game title screen and the game overview screen so I’ll just explain to you the mechanics of the game. Before the game starts you have an initial account of 100000. To play the game, click on the Spin button. After clicking on the Spin button the computer will generate random colors and your score depends upon the generated color. The scoring system is presented below:

ColorWin
Red-Green-Blue+2000
Red-Red-Red+1000
Green-Green-Green+1000
Blue-Blue-Blue+1000
Others-1000
The following steps demonstrate how to create this game:

1.Prepare the images that will be needed in your game. You can use Ms-Paint, Photoshop or GIMP. The following images are required for this game:



2. After preparing the images, Start Visual C++>File>New>Projects>Wizards>Select Dark GDK-Game>Type “Color Machine” in the name textbox (no quotes).

3. Assuming that you have saved the project in the default directory, minimize Microsoft Visual C++ then go to My Documents>Visual Studio 2008>Projects>Color Machine>Color Machine>Then paste the images that you have made in Step 1.



4. Maximize Visual C++>View>Solution Explorer>Double-click Main.cpp. The following should then come into view:

  1. // Dark GDK - The Game Creators - www.thegamecreators.com  
  2.   
  3. // the wizard has created a very simple project that uses Dark GDK  
  4. // it contains the basic code for a GDK application  
  5.   
  6. // whenever using Dark GDK you must ensure you include the header file  
  7. #include "DarkGDK.h"  
  8.   
  9. // the main entry point for the application is this function  
  10. void DarkGDK ( void )  
  11. {  
  12.  // turn on sync rate and set maximum rate to 60 fps  
  13.  dbSyncOn   ( );  
  14.  dbSyncRate ( 60 );  
  15.   
  16.  // our main loop  
  17.  while ( LoopGDK ( ) )  
  18.  {  
  19.   // update the screen  
  20.   dbSync ( );  
  21.  }  
  22.   
  23.  // return back to windows  
  24.  return;  
  25. }  

5. Locate the line #include "DarkGDK.h" then declare the following variables below it:
  1. //handles the spriteid of the left box  
  2. int intred;  
  3. //handles the spriteid of the middle box  
  4. int intgreen;  
  5. //handles the spriteid of the right box  
  6. int intblue;  
  7. //handles our loop  
  8. int intcounter;  
  9. //determines if the spin button has been clicked  
  10. int intclicked;  
  11. //displays our status text  
  12. char* chrmessage;  
  13. //handles the balance  
  14. int intbalance=100000;  

6. Locate the line dbSynRate(60); then replace the 60 with 10 so that dark GDK will try to play 10 frames per second and our loop will not play funny , then type the following after it:
  1. //sets the window title  
  2. dbSetWindowTitle("Color Slot Machine");  
  3. //sets the window size  
  4. dbSetWindowSize(400,400);  
  5. //loads our images  
  6. dbLoadImage("red.png",1);  
  7. dbLoadImage("green.png",2);  
  8. dbLoadImage("blue.png",3);  
  9. dbLoadImage("backdrop.png",4);  
  10. dbLoadImage("Spin.png",5);  
  11. dbLoadImage("pointer.png",6);  
  12. //displays our images  
  13. dbSprite(4,0,0,4);  
  14. //sets our backdrop alpha transparency  
  15. //so that our text will be visible  
  16. dbSetSpriteAlpha(4,150);  
  17. dbSprite(5,240,380,5);  
  18. dbSprite(1,130,100,1);  
  19. dbSprite(2,260,100,2);  
  20. dbSprite(3,390,100,3);  

7. Locate the Line while ( LoopGDK ( ) ) then type the following after the open curly brace:
  1. //sets text size to 30 points  
  2. dbSetTextSize(30);  
  3. //displays the balance  
  4. //I still don’t know how to concanate text  
  5. dbText(0,0,"Balance:");  
  6. dbText(150,0,dbStr(intbalance));  
  7.   //displays our status text  
  8.   dbText(250,250,chrmessage);  
  9.   //pessimistic programming  
  10.   //assume that the button has not yet been clicked  
  11.   intclicked=0;  
  12.   //hides our customized mouse pointer  
  13.   //so that only the mouse pointer is visible  
  14.   dbHideSprite(6);  
  15.   //Enables dbRnd to generate an unpredictable  
  16.   //random number generation  
  17.   dbRandomize(dbTimer());  
  18.   dbSetSprite(6,1,100);  
  19.   //enables our sprite to follow the mouse  
  20.   dbSprite(6,dbMouseX(),dbMouseY(),6);  
  21.   //if our hidden sprite collided with the mouse  
  22.   //and the button has been clicked  
  23.   if(dbSpriteHit(6,5)==1 && dbMouseClick()==1)  
  24.   {  
  25.   //assigns 1 to inclicked variable  
  26.   //resets the value of chrmessage  
  27.   intclicked=1;  
  28.   chrmessage="";  
  29.   //generate a random color 10 times  
  30.   for(intcounter=1;intcounter<=10;intcounter++)  
  31.   {  
  32.    intred=1 + dbRnd(2);  
  33.    intblue=1 + dbRnd(2);  
  34.    intgreen=dbRnd(2)+1;  
  35.    dbSprite(1,130,100,intred);  
  36.    dbSprite(2,260,100,intgreen);  
  37.    dbSprite(3,390,100,intblue);  
  38.    dbSync();  
  39.   }  
  40.   }  
  41.     
  42.   //check if the button has been clicked  
  43.   //if it was clicked then  
  44.   
  45.   if (intclicked==1)  
  46.   {  
  47.    //check for color matches  
  48.    if (intred==1 && intblue==1 && intgreen==1)  
  49.    {  
  50.    chrmessage="You win 1000!";  
  51.    intbalance=intbalance+1000;  
  52.     
  53.    }  
  54.    else if (intred==2 && intblue==2 && intgreen==2)  
  55.    {  
  56.    chrmessage="You win 1000!";  
  57.    intbalance=intbalance+1000;  
  58.    }  
  59.    else if (intred==3 && intblue==3 && intgreen==3)  
  60.    {  
  61.    chrmessage="You win 1000!";  
  62.    intbalance=intbalance+1000;  
  63.    }  
  64.    else if (intred==1 && intblue==2 && intgreen==3)  
  65.    {  
  66.    chrmessage="You win 2000!";  
  67.    intbalance=intbalance+1000;  
  68.    }  
  69.    //if no color matches then  
  70.    else  
  71.    {  
  72.    chrmessage="You Lose!";  
  73.    intbalance=intbalance-1000;  
  74.    }  
  75.   }  
  76.   //if the balance is less than or equal to zero  
  77.   if (intbalance<=0)  
  78.   {  
  79.   dbCLS();  
  80.   dbSetTextSize(32);  
  81.   dbText(180,250,"Insufficient balance");  
  82.   dbText(250,300,"GAME OVER");  
  83.   dbSync();  
  84.   dbWait(4000);  
  85.   return;  
  86.  }  
8. Let’s have a look of our code once again and this time, crappy comments ommitted.
  1. // Dark GDK - The Game Creators - www.thegamecreators.com  
  2.   
  3. // the wizard has created a very simple project that uses Dark GDK  
  4. // it contains the basic code for a GDK application  
  5.   
  6. // whenever using Dark GDK you must ensure you include the header file  
  7. #include "DarkGDK.h"  
  8. int intred;  
  9. int intblue;  
  10. int intgreen;  
  11. int intcounter;  
  12. int intclicked;  
  13. char* chrmessage;  
  14. int intbalance=100000;  
  15. // the main entry point for the application is this function  
  16. void DarkGDK ( void )  
  17. {  
  18.   
  19.  // turn on sync rate and set maximum rate to 60 fps  
  20.  dbSyncOn   ( );  
  21.  dbSyncRate ( 20 );  
  22.  dbSetWindowTitle("Color Slot Machine");  
  23.  dbSetWindowSize(400,400);  
  24.  dbSetGamma(500,255,500);  
  25.  dbLoadImage("red.png",1);  
  26.  dbLoadImage("green.png",2);  
  27.  dbLoadImage("blue.png",3);  
  28.  dbLoadImage("backdrop.png",4);  
  29.  dbLoadImage("Spin.png",5);  
  30.  dbLoadImage("pointer.png",6);  
  31.  dbSprite(4,0,0,4);  
  32.  dbSetSpriteAlpha(4,150);  
  33.  dbSprite(5,240,380,5);  
  34.  dbSprite(1,130,100,1);  
  35.  dbSprite(2,260,100,2);  
  36.  dbSprite(3,390,100,3);  
  37.   
  38.  // our main loop  
  39.  while ( LoopGDK ( ) )  
  40.  {  
  41.   
  42.   dbSetTextSize(30);  
  43.   dbText(0,0,"Balance:");  
  44.   dbText(150,0,dbStr(intbalance));  
  45.   dbText(250,250,chrmessage);  
  46.   intclicked=0;  
  47.   dbHideSprite(6);  
  48.   dbRandomize(dbTimer());  
  49.   dbSetSprite(6,1,100);  
  50.   dbSprite(6,dbMouseX(),dbMouseY(),6);  
  51.   if(dbSpriteHit(6,5)==1 && dbMouseClick()==1)  
  52.   {  
  53.   intclicked=1;  
  54.   chrmessage="";  
  55.    for(intcounter=1;intcounter<=10;intcounter++)  
  56.    {  
  57.    intred=1 + dbRnd(2);  
  58.    intblue=1 + dbRnd(2);  
  59.    intgreen=dbRnd(2)+1;  
  60.    dbSprite(1,130,100,intred);  
  61.    dbSprite(2,260,100,intgreen);  
  62.    dbSprite(3,390,100,intblue);  
  63.    dbSync();  
  64.    }  
  65.   }  
  66.   
  67.   if (intclicked==1)  
  68.   {  
  69.    if (intred==1 && intblue==1 && intgreen==1)  
  70.    {  
  71.    chrmessage="You win 1000!";  
  72.    intbalance=intbalance+1000;  
  73.     
  74.    }  
  75.    else if (intred==2 && intblue==2 && intgreen==2)  
  76.    {  
  77.    chrmessage="You win 1000!";  
  78.    intbalance=intbalance+1000;  
  79.    }  
  80.    else if (intred==3 && intblue==3 && intgreen==3)  
  81.    {  
  82.    chrmessage="You win 1000!";  
  83.    intbalance=intbalance+1000;  
  84.    }  
  85.    else if (intred==1 && intblue==2 && intgreen==3)  
  86.    {  
  87.    chrmessage="You win 2000!";  
  88.    intbalance=intbalance+1000;  
  89.    }  
  90.    else  
  91.    {  
  92.    chrmessage="You Lose!";  
  93.    intbalance=intbalance-1000;  
  94.    }  
  95.   }  
  96.     
  97.   if (intbalance<=0)  
  98.   {  
  99.   dbCLS();  
  100.   dbSetTextSize(32);  
  101.   dbText(180,250,"Insufficient balance");  
  102.   dbText(250,300,"GAME OVER");  
  103.   dbSync();  
  104.   dbWait(4000);  
  105.   return;  
  106.   }  
  107.   
  108.   // update the screen  
  109.     
  110.   dbSync ( );  
  111.  }  
  112.   
  113.  // return back to windows  
  114.  return;  
  115. }  
9. Press F5 to test our game. Click the Spin button and see what happens.

Creating a plain object in DarkGDK

A plain is a mesh object that can be used as ground in your game scene. To create a plain object, use the dbMakeObjectPlain function.

Syntax:

  1. dbMakeObjectPlain(object id,plain width,plain height);  

Example:
  1. dbMakeObjectPlain(1,100,100);  

By default an object is drawn in gray. You can add colors to your plain object by Using the dbColorObject function which has the following syntax:

  1. dbColorObject(object id, color);  

Colors can be defined by using the dbRGB function which has the following syntax:

  1. dbRGB(red,green,blue);  

Wherein red, green, and blue are numbers that ranges from 0-255.

The following codes demonstrates dbMakeObjectPlain and dbColorObject at work:

  1. // Dark GDK - The Game Creators - www.thegamecreators.com  
  2.   
  3. // the wizard has created a very simple project that uses Dark GDK  
  4. // it contains the basic code for a GDK application  
  5.   
  6. // whenever using Dark GDK you must ensure you include the header file  
  7. #include "DarkGDK.h"  
  8.   
  9. // the main entry point for the application is this function  
  10. void DarkGDK ( void )  
  11. {  
  12. // turn on sync rate and set maximum rate to 60 fps  
  13. dbSyncOn   ( );  
  14. dbSyncRate ( 60 );  
  15. //Creates a 400 X 200 plain object with an object id of 1 and   
  16. dbMakeObjectPlain(1,400,200);  
  17. //Applies a red color to our plain  
  18. dbColorObject(1,dbRGB(255,0,0));  
  19. //Positions the camera on the side of our object  
  20. dbPositionCamera(-1,-600,-100);  
  21. // our main loop  
  22. while ( LoopGDK ( ) )  
  23. {  
  24. // update the screen  
  25. dbSync ( );  
  26. }  
  27. //deletes all objects  
  28. for ( int i = 1; i < 50; i++ )  
  29. dbDeleteObject ( i );  
  30. // return back to windows  
  31. return;  
  32. }