Post by aphoticgenesis on Nov 15, 2009 21:26:06 GMT -5
[glow=red,2,300]Stumpy798's ultimate onslaught of C++ tutorials
This is alot of tutorials and code for helping others learn C++, it will be added to till the end of time.[/glow]
Structures:
Threads:
Classes:
Keyboard Input:
Clipboard Data:
Random Number Generation:
Switch Statements:
Number Guessing Game:
Timer:
This is alot of tutorials and code for helping others learn C++, it will be added to till the end of time.[/glow]
Structures:
// Structures - a good way to pack information
// headers
#include <windows.h>
#include <stdio.h>
struct SPerson {
char szFirstName[20];
char szLastName[20];
};
SPerson g_person;
int main() {
// get the person's first name
printf("First name: "); gets(g_person.szFirstName);
// get the person's last name
printf("Last name: "); gets(g_person.szLastName);
// show the person's name
printf("Your name is: %s %s \n", g_person.szFirstName, g_person.szLastName);
}
Threads:
// Threading - threading can be useful in many different situations, for example
// when you want to run two loop like in a game you have the game loop and if your trying to join someone
// elses game thats another loop looking for the host
// headers
#include <windows.h>
#include <stdio.h>
// our thread handles which will be used the create the actual threads
HANDLE g_hThread1;
HANDLE g_hThread2;
// holds our thread ids
int g_ThreadId[1];
// declare our first loop
void FirstLoop();
// declare our second loop
void SecondLoop();
int main() {
// create a loop
while(true) {
g_hThread1 = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)FirstLoop, (void*)0, NULL, (LPDWORD)g_ThreadId[0]);
g_hThread2 = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)SecondLoop, (void*)0, NULL, (LPDWORD)g_ThreadId[1]);
}
// close our handles, free
CloseHandle(g_hThread1);
CloseHandle(g_hThread2);
}
// define our first loop
void FirstLoop() {
while(true) {
}
}
// define our second loop
void SecondLoop() {
while(true) {
}
}
Classes:
// Classes - useful for big things
// example: part of a game engine
// headers
#include <windows.h>
#include <stdio.h>
// starts from 0 and goes to 15 so its actaully 16 adding the base
#define MAX_PLAYERS 15
class CPlayer {
// can be accessed outside of the class
public:
// class contructor
// initializez defaults for class
CPlayer();
// class deconstructor
// deinitializes defaults for class
virtual ~CPlayer();
void SetPlayerName(char*);
void SetPlayerPosition(float, float, float);
void PrintPlayerInfo();
// only can be accessed by the class
private:
float m_PosX, m_PosY, m_PosZ;
char m_szPlayerName[255];
};
// class contructor
// set class defaults
CPlayer::CPlayer() {
m_PosX = 1.0f;
m_PosY = 1.0f;
m_PosZ = 1.0f;
strcpy(m_szPlayerName, "Default");
}
// class deconstructor
// set class default after
CPlayer::~CPlayer() {
m_PosX = NULL;
m_PosY = NULL;
m_PosZ = NULL;
}
// set the player's name
void CPlayer::SetPlayerName(char *szPlayerName) {
sprintf(m_szPlayerName, "%s", szPlayerName);
}
// set the player's position
void CPlayer::SetPlayerPosition(float PosX, float PosY, float PosZ) {
m_PosX = PosX;
m_PosY = PosY;
m_PosZ = PosZ;
}
// show the player info
void CPlayer::PrintPlayerInfo() {
printf("Player name: %s | PosX: %f | PosY: %f | PosZ: %f \n", m_szPlayerName, m_PosX, m_PosY, m_PosZ);
}
// declare new globals from class
CPlayer g_player[MAX_PLAYERS];
int main() {
// setup player 0
g_player[0].SetPlayerName("SEGnosis");
g_player[0].SetPlayerPosition(10.5f, 2.5f, 5.5f);
g_player[0].PrintPlayerInfo();
// setup player 1
g_player[1].SetPlayerName("Stumpy798");
g_player[1].SetPlayerPosition(20.5f, 5.5f, 3.5f);
g_player[1].PrintPlayerInfo();
}
Keyboard Input:
// Keyboard Input - useful for events when pressing a key
// example: you press the w key and your player moves forward
#include <windows.h>
#include <stdio.h>
// macro
#define PLAYER_SPEED 0.07f
// globals
float g_PosZ;
char g_szPosition[100];
char g_szWindowTitle[1000];
int main() {
while(true) {
// keypress
if(GetAsyncKeyState('W')) {
// add to the z position
g_PosZ += PLAYER_SPEED;
// copy the positions
sprintf(g_szPosition, "PosZ: %f", g_PosZ);
}
if(GetAsyncKeyState('S')) {
g_PosZ -= PLAYER_SPEED;
sprintf(g_szPosition, "PosZ: %f", g_PosZ);
}
// set the text of any window in front of the posx position
SetWindowText(GetForegroundWindow(), g_szPosition);
Sleep(1);
}
}
Clipboard Data:
// Clipboard Data - useful for getting information stored in your clipboard
// headers
#include <windows.h>
#include <stdio.h>
// macro - the max length of the clipbaord that can be stored
#define MAX_CLIPBOARD_LENGTH 10000000
// globals
HANDLE g_hClipboard;
char g_szClipboardText[MAX_CLIPBOARD_LENGTH];
int main() {
// open the clipboard and get the data then close it
if(OpenClipboard(NULL)) { g_hClipboard = GetClipboardData(CF_TEXT); CloseClipboard(); }
// copy the clipboard data to g_szClipboardText
sprintf(g_szClipboardText, "%s", (char*)g_hClipboard);
// print the text from g_szClipboardText
printf(g_szClipboardText);
}
Random Number Generation:
// Random Number Generation - useful for things like encryption or terrain mapping
// example: generating a random numbers for terrain in a game
// headers
#include <windows.h>
#include <stdio.h>
// macro - the random number cannot go past this
#define MAX_RANDOM_NUMBER 100
int g_RandomNumber;
char g_szRandomNumber[1000];
int main() {
// srand() changes the number generation pool
srand(GetTickCount());
// create the random number
g_RandomNumber = rand()%MAX_RANDOM_NUMBER;
// copy the random number into a char
sprintf(g_szRandomNumber, "%d \n", g_RandomNumber);
// output the random number
printf(g_szRandomNumber);
}
Switch Statements:
// Switch Statments - useful for repeating things by using ids and stuff
// example: different modes in a game
#include <windows.h>
#include <stdio.h>
// macros - different game modes
#define GM_ONMENU 1
#define GM_INGAME 2
// holds the current game mode
int g_GameMode;
bool g_bExiting;
int main() {
printf("Press '1' to go on the menu and press '2' to go in the game \n\n");
// our main game loop
while(!g_bExiting) {
// change the game mode
if(GetAsyncKeyState('1')) { g_GameMode = GM_ONMENU; }
if(GetAsyncKeyState('2')) { g_GameMode = GM_INGAME; }
// which game mode are we in?
while(true) {
switch(g_GameMode) {
case GM_ONMENU:
// this is what happens when your on the menu
printf("You are on the menu \n\n");
g_bExiting = true;
break;
case GM_INGAME:
// this is what happends when your in the game
printf("You are in the game \n\n");
g_bExiting = true;
break;
} break;
}
}
system("PAUSE");
return(0);
}
Number Guessing Game:
// Number Guessing Game - guess the right number to win
// headers
#include <windows.h>
#include <stdio.h>
#include <iostream.h>
// standard namespace
using namespace std;
// macro - different game modes
#define GM_EASY 1
#define GM_MEDIUM 2
#define GM_HARD 3
// globals
int g_GameMode;
bool g_bExiting;
int g_DifficultyNumber;
int g_Number;
// declare our mode function
// set our mode
// choose the difficulty level
void Mode(int);
int main() {
srand(GetTickCount());
// default difficulty number
g_DifficultyNumber = 25;
// a random number
g_Number = rand()%g_DifficultyNumber;
// get the game mode
cout << "1. Easy \n2. Medium \n3. Hard \n\n";
std::cin >> g_GameMode;
// our main game loop
while(!g_bExiting) {
if(g_GameMode == GM_EASY) { cout << endl; Mode(25); }
if(g_GameMode == GM_MEDIUM) { cout << endl; Mode(50); }
if(g_GameMode == GM_HARD) { cout << endl; Mode(100); }
}
system("PAUSE");
return(0);
}
// define our mode function
// this functions choose the difficulty number
void Mode(int Number) {
// set the difficulty number
g_DifficultyNumber = Number;
int GetNumber;
// get the users guess
cout << "Guess a number: ";
std::cin >> GetNumber;
if(GetNumber != g_Number) { cout << "Wrong number! \n"; } else { g_bExiting = true; cout << "You won! \n\n"; }
}
Timer:
// Timer - useful for many different things
// example: your player is falling see how long they are falling and multiply the damage by the fall timer
// headers
#include <windows.h>
#include <stdio.h>
#define COUNT 60
// our timer class
// will control the timer
class CTimer {
public:
CTimer();
virtual ~CTimer();
// set the timer
void SetTimer(int);
// starts the timer from time
void StartTimer();
// pauses the timer from time
void ResetTimer();
// outputs timer info
void OutputTimer();
private:
// holds the time for the class
int m_Time;
};
// class contructor
CTimer::CTimer() {
m_Time = 0;
}
// class deconstructor
CTimer::~CTimer() {
m_Time = NULL;
}
// set the initial time for the timer
void CTimer::SetTimer(int Time) {
m_Time = Time;
}
// start the timer
void CTimer::StartTimer() {
++m_Time;
}
// put the timer back to default
void CTimer::ResetTimer() {
m_Time = 0;
}
// display timer information to title text of any foreground window
void CTimer::OutputTimer() {
static char szTimerInfo[500] = {0};
sprintf(szTimerInfo, "Time: %d", m_Time/COUNT);
SetWindowText(GetForegroundWindow(), szTimerInfo);
}
// decalre a g_timer from our timer class
CTimer g_timer;
int main() {
// init the timer
g_timer.SetTimer(0);
// our main loop
while(true) {
// start the counting
g_timer.StartTimer();
// display counting to title text of foreground window
g_timer.OutputTimer();
// if you press the p key then the timer will reset to its default number
if(GetAsyncKeyState('P')) { g_timer.ResetTimer(); }
// let the CPU(central processing unit) catch up
Sleep(1);
}
}