Sent you e-mail. For brevity, I'll put a example class here. I'll use CBall, because it's relatively simple.
First, I split the definition and the header up:
CBall.h:
Code
#pragma once
#include <windows.h>
class cBall {
private:
int iForeground; /* Ball's foreground color */
int iBackground; /* Ball's Background color */
int iBall_X; /* Ball's X coordinate */
int iBall_Y; /* Ball's Y coordinate */
int iVelocity_X; /* Ball's X velocity (speed) */
int iVelocity_Y; /* Ball's Y velocity (speed) */
SMALL_RECT smWalls;
public:
cBall() { };
void ResetBall(const int X, const int Y);
void UpdateBall();
void DrawBall();
void ReverseBall();
inline int GetXVelocity() { return iVelocity_X; }
inline int GetYVelocity() { return iVelocity_Y; }
inline int GetXBall() { return iBall_X; }
inline int GetYBall() { return iBall_Y; }
inline void SetForeground(const int Color) { iForeground = Color; return; }
inline void SetBackground(const int Color) { iBackground = Color; return; }
inline void SetXBall(const int X) { iBall_X = X; return; }
inline void SetYBall(const int Y) { iBall_Y = Y; return; }
inline void SetXVelocity(const int Velocity) { iVelocity_X = Velocity; return; }
inline void SetYVelocity(const int Velocity) { iVelocity_Y = Velocity; return; }
inline void SetLeftWall(const int Wall) { smWalls.Left = Wall; return; }
inline void SetRightWall(const int Wall) { smWalls.Right = Wall; return; }
inline void SetTopWall(const int Wall) { smWalls.Top = Wall; return; }
inline void SetBottomWall(const int Wall) { smWalls.Bottom = Wall; return; }
};
Splitting up enables fast incremental builds, due to smart compiler optimization. I also moved the required includes to
CBall.cpp, which helps make building faster. I'd like to eliminate the need for
windows.h, but
SMALL_RECT is an integral part of this class.
The next major thing was explicitly inlining your simple functions. Using this tells the compiler to copy the function to the local scope, rather than doing a function call and going through the stack. This is a tiny little speedup that most compilers do with /O2 or better, but it certainly doesn't hurt. You can also remove all those empty
return calls. They don't do anything.
CBall.cpp:
Code
#include "CBall.h"
#include "TextControl.h"
#include <iostream>
void cBall::ReverseBall()
{
if (rand() % 9)
iVelocity_X = -iVelocity_X;
else
iVelocity_X = (-iVelocity_X + 1);
}
void cBall::DrawBall()
{
/* Draw the ball at the new position */
PlaceCursor(iBall_X, iBall_Y);
SetColor(iForeground, iBackground);
printf("รน");
}
void cBall::UpdateBall()
{
/* Erase the old ball */
PlaceCursor(iBall_X, iBall_Y);
printf(" ");
/* Bounce if the ball hits a wall */
if (iBall_X > smWalls.Right || iBall_X < smWalls.Top) { iVelocity_X = -iVelocity_X; }
if (iBall_Y > smWalls.Bottom || iBall_Y < smWalls.Top) { iVelocity_Y = -iVelocity_Y; }
/* Update the position */
iBall_X += iVelocity_X;
iBall_Y += iVelocity_Y;
}
void cBall::ResetBall(const int X, const int Y) {
PlaceCursor(iBall_X, iBall_Y);
printf(" ");
iBall_X = X;
iBall_Y = Y;
iVelocity_X = 1;
iVelocity_Y = 1;
}
There really aren't any changes to the underlying functionality, aside from removing the superfluous
return statements, but I've included it to show the syntax for defining class member functions.