Skip to content
Permalink
Browse files
Basic project with helpful drawing code text file.
  • Loading branch information
ab8809 committed May 19, 2020
1 parent dd0eecc commit 7950361e7dcff6e934e98237a177943bd0a0ce42
Show file tree
Hide file tree
Showing 12 changed files with 488 additions and 3 deletions.
@@ -99,10 +99,12 @@
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<AdditionalIncludeDirectories>Library Files\include</AdditionalIncludeDirectories>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalLibraryDirectories>Library Files</AdditionalLibraryDirectories>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
@@ -138,7 +140,18 @@
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="Cube.cpp" />
<ClCompile Include="GameObject.cpp" />
<ClCompile Include="main.cpp" />
<ClCompile Include="Sphere.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="Cube.h" />
<ClInclude Include="GameObject.h" />
<ClInclude Include="Sphere.h" />
</ItemGroup>
<ItemGroup>
<Text Include="Helpful Drawing Code.txt" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
@@ -18,5 +18,30 @@
<ClCompile Include="main.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="Cube.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="GameObject.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="Sphere.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="Cube.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="GameObject.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="Sphere.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<Text Include="Helpful Drawing Code.txt">
<Filter>Resource Files</Filter>
</Text>
</ItemGroup>
</Project>
@@ -0,0 +1,47 @@
#include "Cube.h"

//We can either hard code the values in GameObject (like in Cube())
//or
//We can use the GameEngine constructors to fill the values ( like in Cube(x, y, z, size))
Cube::Cube()
{
x = 0.0f;
y = 0.0f;
z = 0.0f;

r = 1.0f;
g = 0.0f;
b = 1.0f;

size = 1.0f;
}

Cube::Cube(float _x, float _y, float _z, float _size)
:GameObject(_x, _y, _z) //note the use of GameObject constructor
{
size = _size;
}

Cube::Cube(float _x, float _y, float _z,
float _r, float _g, float _b,
float _size):
GameObject(_x, _y, _z, _r, _g, _b) //note the use of GameObject constructor
{
size = _size;
}

//Position and colour a cube in the scene
void Cube::Draw()
{
glPushMatrix();
glTranslatef(x, y, z);
glColor3f(r, g, b);
glutSolidCube(size);
//glutWireCube(size) - if you'd rather see through it
glPopMatrix();
}

void Cube::Update(float deltaTime)
{
//Cubes are static at the moment so nothing changes for them
}
24 Cube.h
@@ -0,0 +1,24 @@
#pragma once

#include "GameObject.h"

//A basic cube class derived from a game object
class Cube: public GameObject
{
public:
// -- variables --
float size;

// -- constructors/destructors --
Cube(); //default constructor - aka no values passed in (position at 0,0,0 - colour magenta - size 1)
Cube(float _x, float _y, float _z, float _size); //pass in a position and size
Cube(float _x, float _y, float _z,
float _r, float _g, float _b,
float _size); //pass in a position, colour and size
~Cube() {}; //default destructor

// -- functions --
//Need to give definions to GameObject's pure virtual functions
virtual void Draw();
virtual void Update(float deltaTime);
};
@@ -0,0 +1,41 @@
#include "GameObject.h"

std::map<char, bool> GameObject::keys;
std::map<int, bool> GameObject::specialKeys;

GameObject::GameObject()
{
x = 0.0f;
y = 0.0f;
z = 0.0f;

r = 1.0f;
g = 0.0f;
b = 1.0f;
}

GameObject::GameObject(float _x, float _y, float _z)
{
x = _x;
y = _y;
z = _z;

r = 1.0f;
g = 1.0f;
b = 1.0f;
}

GameObject::GameObject(float _x, float _y, float _z, float _r, float _g, float _b)
{
x = _x;
y = _y;
z = _z;

r = _r;
g = _g;
b = _b;
}

GameObject::~GameObject()
{
}
@@ -0,0 +1,31 @@
#pragma once

#include <GL/glew.h>
#include <GL/freeglut.h>

#include <map>

//A basic abstract class that all game objects in the scene are based off
// abstract = We never will make a direct instance of GameObject but will make classes inherit from it
class GameObject
{
public:
// -- variables --
float x, y, z; //position
float r, g, b; //colour

static std::map<char, bool> keys; //save key presses and let game objects access them
static std::map<int, bool> specialKeys; //static = one instance of these variables for the whole class (top of .cpp)

// -- constructors/destructors --
GameObject(); //default constructor - aka no values passed in (position at 0,0,0 - colour magenta)
GameObject(float _x, float _y, float _z); //pass in a position (no colour passed in so defaults to white)
GameObject(float _x, float _y, float _z,
float _r, float _g, float _b); //pass in a position, colour
~GameObject(); //default destructor

// -- functions --
//pure virtual so all classes that derive from this one need to include a definion for these
virtual void Draw() = 0;
virtual void Update(float deltaTime) = 0;
};
@@ -0,0 +1,64 @@
This legacy OpenGL code might be useful for drawing/displaying the
physics parts of the assignment.

Remember, this code will have to go into an object's Draw() function
or
In the drawScene() function (between gluLookAt() and glutSwapBuffers()) in main.cpp to show.

-- Line Drawing --

You can draw a line between two points via the below code.
This could be useful for visually showing forces, acceleration, velocity, boundaries etc.

The indention isn't needed - it just makes it easier to read.

This example draws a red line between (0, 0, 0) and (1, 1, 1).

glPushMatrix();
glBegin(GL_LINES); //note its GL_LINES and not GL_LINE!
glColor3f(1, 0, 0); //give the line a colour
glVertex3f(0, 0, 0); //line start
glVertex3f(1, 1, 1); //line end
glEnd();
glPopMatrix();

This example uses an object's position to draw the positive axis from its center.
Each 2 points passed in via glVertex3f creates 1 line (so this example has 3 lines in one glBegin()).

glPushMatrix();
glBegin(GL_LINES);

glColor3f(1, 0, 0);
glVertex3f(x, y, z); //uses the game objects position values
glVertex3f(x + 3, y, z); //positive x axis

glColor3f(0, 1, 0); //note the colour change per line
glVertex3f(x, y, z);
glVertex3f(x, y + 3, z); //positive y axis

glColor3f(1, 0, 1);
glVertex3f(x, y, z);
glVertex3f(x, y, z + 3); //positive z axis
glEnd();
glPopMatrix();

-- Debug Object Drawing --

If you want to place a wireframe object around an object (perhaps to show a collider).
Choose whatever radius/size value you need to make the debug object bigger or smaller.
This would be done seperate to the push and pop you have for the normal objects drawing.

//normal objects push/pop drawing code here first

glPushMatrix();
glTranslatef(x, y, z); //go the the game objects position
glColor3f(1, 0, 1); //make it magneta for easy debug testing
glutWireSphere(radius, 10, 10); //where radius is a float
//or
//glutWireCube(size); //where size is a float
glPopMatrix();

-- Colour changes --

You may want to change the colour of an object when something happens (like a collision).
This can be done by changing the public colour variables of the object directly.
@@ -0,0 +1,55 @@
#include "Sphere.h"

//We can either hard code the values in GameObject (like in Sphere())
//or
//We can use the GameEngine constructors to fill the values ( like in Sphere(x, y, z, radius, speed))
Sphere::Sphere()
{
x = 0.0f;
y = 0.0f;
z = 0.0f;

r = 1.0f;
g = 0.0f;
b = 1.0f;

radius = 1.0f;
moveSpeed = 5.0f;
}

Sphere::Sphere(float _x, float _y, float _z, float _radius, float _moveSpeed)
:GameObject(_x, _y, _z) //note the use of GameObject constructor
{
radius = _radius;
moveSpeed = _moveSpeed;
}

Sphere::Sphere(float _x, float _y, float _z,
float _r, float _g, float _b,
float _radius, float _moveSpeed):
GameObject(_x, _y, _z, _r, _g, _b) //note the use of GameObject constructor
{
radius = _radius;
moveSpeed = _moveSpeed;
}

void Sphere::Draw()
{
glPushMatrix();
glTranslatef(x, y, z);
glColor3f(r, g, b);
//glutSolidSphere(radius, 10, 10); //if you'd rather it shows solid
glutWireSphere(radius, 10, 10);
glPopMatrix();
}

void Sphere::Update(float deltaTime)
{
//This uses only special keys at the moment - aka the arrow keys
//You can use normal keys via
//GameObject::keys['a'] for the a key for example
if (GameObject::specialKeys[GLUT_KEY_UP] == true)
y += moveSpeed * deltaTime;
if (GameObject::specialKeys[GLUT_KEY_DOWN] == true)
y -= moveSpeed * deltaTime;
}
@@ -0,0 +1,26 @@
#pragma once

#include "GameObject.h"

//A basic sphere class derived from a game object
//It can move (basic position changing only at the moment!) via the arrow keys
class Sphere : public GameObject
{
public:
// -- variables --
float radius;
float moveSpeed;

// -- constructors/destructors --
Sphere(); //default constructor - aka no values passed in (position at 0,0,0 - colour magenta - radius 1 - move speed - 1)
Sphere(float _x, float _y, float _z, float _radius, float _moveSpeed); //pass in a position, radius and movespeed
Sphere(float _x, float _y, float _z,
float _r, float _g, float _b,
float _radius, float _moveSpeed); //pass in a position, colour, radius and move speed
~Sphere() {}; //default destructor

// -- functions --
//Need to give definions to GameObject's pure virtual functions
virtual void Draw();
virtual void Update(float deltaTime);
};
BIN +230 KB freeglut.dll
Binary file not shown.
BIN +413 KB glew32.dll
Binary file not shown.

0 comments on commit 7950361

Please sign in to comment.