Here is a header that I have created. Any more ideas would be great or optimizing the current code would be better.
Code:
#ifdef WIN32
#include <windows.h>
#endif
#include <iostream>
#include <ctime>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
void dans_sleep(double seconds)
{
#ifdef WIN32
sleep(seconds * 1000);
#else
usleep(seconds * 1000000);
#endif
}
void dans_clear(double seconds)
{
#ifdef WIN32
dans_sleep(seconds);
system("cls");
#else
dans_sleep(seconds);
system("clear");
#endif
}
void dans_clear()
{
#ifdef WIN32
system("cls");
#else
system("clear");
#endif
}
void dans_pause()
{
cout << "Please hit enter to continue...";
cin.get();
}
void dans_swap(int &a, int &b)
{
int temp = a;
a = b;
b = temp;
}
void dans_swap(char &a, char &b)
{
char temp = a;
a = b;
b = temp;
}
void dans_swap(double &a, double &b)
{
double temp = a;
a = b;
b = temp;
}
void dans_swap(float &a, float &b)
{
float temp = a;
a = b;
b = temp;
}
void dans_ascii(char *world, int width, int height)
{
for(int a = 0; a < height; a++)
{
for(int b = 0; b < width; b++)
{
cout << *(world + (a * width) + b);
}
cout << endl;
}
}
void dans_gotoxy(int x, int y)
{
#ifdef WIN32
COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
#else
char essq[100];
char xstr[100];
char ystr[100];
sprintf(xstr, "%d", x);
sprintf(ystr, "%d", y);
essq[0] = '\0';
strcat(essq, "\033[");
strcat(essq, ystr);
strcat(essq, "d");
strcat(essq, "\033[");
strcat(essq, xstr);
strcat(essq, "G");
printf("%s", essq);
#endif
}