NFG(New Future Games)

The game programming software site


Coding Ideas in c++(non mfc)

             Sample code to use to improve all your programes 


A very simple timer

If there is any code that you need
Just email me at kingpinzs@hotmail.com


#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;

//The timer fuction

void wait ( int seconds )
{
  clock_t endwait;
  endwait = clock () + seconds * CLK_TCK ;
  while (clock() < endwait) {}
}

//wait in milseconds
void waitMil (int milseconds)
{
  clock_t endwait;
  endwait = clock () + milseconds /CLOCKS_PER_SEC ;
  while (clock() < endwait) {}   
}
//wait in miniutes
void waitMin(int minute)
{
       clock_t endwait;
  endwait = clock () + (minute * 60) * CLOCKS_PER_SEC ;
  while (clock() < endwait) {}
}
//wait in hours
void waitHr(int hour)
{
clock_t endwait;
  endwait = clock () + (hour * 3600) * CLOCKS_PER_SEC ;
  while (clock() < endwait) {}
}

//updated 4/27/05 added milseconds minuites and hours
int main(int argc, char *argv[])
{
   int y = 0;
  std::cout <<"FORMATING DRIVE "<< std::endl;
  for (y=0; y<101; y++)
  {
    std::cout<< y<<"%";
    std::cout<<"\b";std::cout<<"\b";std::cout<<"\b";std::cout<<"\b";
   
    wait (1);
  }
 
  std::cout<<std::endl;
  std::cout <<"format done" << std::endl; 
  system("PAUSE"); 
  return 0;
}


These defines help make your complied projects smaller

#define VC_LEANMEAN 

#define WIN32_LEAN_AND_MEAN


A windows timer


DWORD start_clock_count = 0; //Count the time (global)

//My timing functions
///////////////////////////////////////////////////////////

//Return current tick-time
DWORD Get_Clock()
{
   return(GetTickCount());
}

///////////////////////////////////////////////////////////

//Start clock
DWORD Start_Clock()
{
   return(start_clock_count = Get_Clock());
}

////////////////////////////////////////////////////////////

//Pause the clock
DWORD Wait_Clock(DWORD count)
{
   while((Get_Clock() - start_clock_count) < count);
      return(Get_Clock());
}

////////////////////////////////////////////////////////////


Message

  MessageBox(NULL "Incorrect password! ","Error!", MB_OK);

  MessageBox(NULL,"Are you sure you want to save changes?","Confirm", MB_YESNO);


This is used to accept no input as in put
 cin.ignore(); // you have to have this or it will see there is no input as input and continue any way with having to press enter
getline (cin, input);

if (input == "gun") {
    cout << "you win and take all 107 peices of gold";
} else if (input =="run") {
    cout << "you run like a cowerd" << endl;
} else if (input.empty()) //this you can just press enter to get what you want

{
    cout << (107 % afterBattle) << " coins.";
}


std::transform(input2.begin(), input2.end(), input2.begin(), (int(*)(int))std::toupper);

This changes the string that was inputed and changes to a uppercase so nolonger case sensitive


To make sure your *.h file is included only once and will not gett error duplicate defftions you put

#pragma once

at the verey top. this works great for me

that is the old way here is the new way

#ifndef HFILENAME_H

#define HFILENAME_H

#endif


If you get error

 fatal error LNK1120: 1 unresolved externals

you forgot to include a *.lib or a *.a or an external file for the compiler to include in compilation


Simple collision detection with side of window

 

#include <iostream>
#include <stdlib.h>

using namespace std;

int main(int argc, char *argv[])
{
  
   
float speedX, speedY;
float x, y;
float mapX, mapY;   // assume these two variables are right and bottom corners of the map

x = 100; // some value
y = 100;
speedX = 1;
speedY = 1;
mapX = 640; // size of map
mapY = 480;

while(1)
{
   x += speedX;
   y += speedY;

   if(x < 0 || x > mapX) speedX *= -1;
   if(y < 0 || y > mapY) speedY *= -1;
cout << x <<"\n"
     << y <<"\n";
}
  system("PAUSE"); 
  return 0;
}


Basic way to save to a txt file

#include <iostream>
#include <stdlib.h>
#include <string>
#include <fstream>

int main()

{
std::ofstream fileOutput("file.txt");
fileOutput  << "Text goes here" << varible << endl;
 fileOutput.close();
return(0);
}


Loading text file to an array. this is the way I do it for now

#include <iostream>
#include <stdlib.h>
#include <fstream>

using namespace std;
int y, x;
int test;
int main(int argc, char *argv[])
{ ifstream fin;


  ifstream MapFile("map.txt");
  int map[16][16];
 
  for(int y = 0 ; y <16; y++)
  {for(int x = 0 ; x <16; x++)
  {
  MapFile >> map[y][x];
  test = map[y][x];
  cout <<y<<" "<<x<<endl;
  cout <<"----------"<<endl;
   cout <<" "<<test<<"id: ";
  }}
 
   MapFile.close();
 
  system("PAUSE"); 
  return 0;
}

_________________________________________________________________________________________________

to use the arrow keys ect with out using dos.h or windows.h

 

num
{
  KEY_ESC     = 27,
  ARROW_UP    = 256 + 72,
  ARROW_DOWN  = 256 + 80,
  ARROW_LEFT  = 256 + 75,
  ARROW_RIGHT = 256 + 77,
  SPACE = 32
};

static int get_code ( void )
{
  int ch = getch();

  if ( ch == 0 || ch == 224 )
    ch = 256 + getch();

  return ch;
}

ch = get_code();

can be used for any keys

 

or just use this to get anykey

_kbhit()

have to include conio.h to use it

_____________________________________________________________

 to help you figure out which way your player is facing

#include <cstdlib>
#include <iostream>

enum eDir {UNDEF, NORTH, SOUTH, EAST, WEST};
using namespace std;

int main(int argc, char *argv[])
{
   
    eDir tehDir;
char hold;
while(1){
  std::cout << "Direction: N, S, E, W>";
  std::cin >> hold;

  //quit the loop
  if(hold == 'X')
      break;

  //assign tehDir
  switch(hold) {
    case 'N': tehDir = NORTH; break;
    case 'S': tehDir = SOUTH; break;
    case 'E': tehDir = EAST; break;
    case 'W': tehDir = WEST; break;
    default: tehDir = UNDEF; break;
   }

//find out which direction we're pointing.
  switch(tehDir){
    case NORTH: std::cout << "You're going North" << endl; ;break;
    case SOUTH: std::cout << "You're going South" << endl;break;
    case EAST: std::cout << "You're going East" << endl; break;
    case WEST: std::cout << "You're going West" << endl; break;
    default: std::cout << "Undefined" << endl; break;
    }
}//end of while loop
   
    system("PAUSE");
    return EXIT_SUCCESS;
}