Jump to content

C++ Keeping it tidy


Mince
 Share

Recommended Posts

I see lots of you members are with it when it comes to programming in c++.

 

The examples given seem messy and basic. I wanted to tidy it up I realise you must declare world,cam,buffers ect so I added the vars at the top and made some functions to crete cam and load level.

 

When it compiles I get warnings about the possibility of accuracy loss from the following vars

 

 

IS THIS EXCEPTABLE and or THE RIGHT WAY ??

//Global Vars

TCamera fgcam;

TLight light;

TVec3 camrotation;

TEntity campiv;

TEmitter fire;

TWorld foreground;

TCamera cam;

TBuffer gbuffer;

TBuffer lightbuffer;

 

 

But it compiles and when I run it it crashes out.

Here is the full code please some1 take a second to help and maybe give a example,

  • Your probably familiar with this one,its from the Heat/Fire example.

 

#include "engine.h"

#include <iostream>

#include <string>

 

const int ScreenWidth = 800;

const int ScreenHeight = 600;

const char* MediaDir = "C:/Program Files/Leadwerks Engine SDK";

const char* AppTitle = "Mince Machine";

 

void ErrOut( const std::string& message ) { std::cerr << message << std::endl; }

 

//Global Vars

TCamera fgcam;

TLight light;

TVec3 camrotation;

TEntity campiv;

TEmitter fire;

TWorld foreground;

TCamera cam;

TBuffer gbuffer;

TBuffer lightbuffer;

 

float mx=0.0,my=0.0;

 

class MinceMachine{

public:

void MMCreateBuffer()

{

//Create a render buffer

TBuffer gbuffer=CreateBuffer(1024,768,BUFFER_COLOR|BUFFER_DEPTH|BUFFER_NORMAL);

TBuffer lightbuffer=CreateBuffer(1024,768,BUFFER_COLOR|BUFFER_DEPTH);

}

 

void CreateCam()

{

TCamera fgcam=CreateCamera();

TEntity campiv=CreatePivot();

 

PositionEntity(campiv,Vec3(0,1,0));

 

TCamera cam=CreateCamera(campiv);

PositionEntity(cam,Vec3(0,0,-4));

}

 

void LoadLevel(const_str level)

{

LoadMesh("scene.gmf");

TWorld foreground=CreateWorld();

CameraClearMode(fgcam,0);

}

 

 

void Setup(int mode)

{

// leTFilter(1);

// leAFilter(1);

// leSetHDR(mode);

// leSetBloom(mode);

// leSetAntialias(4);

// leSetSSAO(0);

// leSetGodRays(0);

// SetAppTitle( AppTitle );

// RegisterAbstractPath( MediaDir );

// // Set Lua framework object

// SetGlobalObject( "fw", fw );

}

 

 

 

void CreateLight(float x,float y,float z)

{

TLight light = CreatePointLight();

PositionEntity(light,Vec3(x,y,z));

EntityColor(light,Vec4(1,0.5,0,0.1));

}

 

 

 

 

void mouse()

{

TVec3 camrotation;

HideMouse();

MoveMouse(GraphicsWidth()/2,GraphicsHeight()/2);

}

 

void MouseCamupdate()

{

mx=Curve(MouseX()-GraphicsWidth()/2,mx,3);

my=Curve(MouseY()-GraphicsHeight()/2,my,3);

MoveMouse(GraphicsWidth()/2,GraphicsHeight()/2);

camrotation=EntityRotation(campiv);

camrotation.X+=my;

camrotation.Y-=mx;

 

RotateEntity(campiv,camrotation);

}

 

 

void CreateHeat(int Intensity,int LifeTime,float x,float y, float z,int RAD)

{

//Bit Of Red

// TEmitter fire = CreateEmitter(Intensity,LifeTime,Vec3(x,y,z));

// EntityColor(fire,Vec4(1,0.5,0,0.1));

// PaintEntity(fire,LoadMaterial("fire.mat"));

// SetEmitterRadius(fire,RAD,RAD/2);

// SetEmitterWaver(fire,0.5);

// SetEmitterRotationSpeed(fire,0.5);

// SetEmitterArea(fire,Vec3(1,0.5,1));

//Create the heat haze emitter

// TEmitter heat = CreateEmitter(Intensity/5,LifeTime*2,Vec3(x,y-0.5,z));

// TMaterial material=LoadMaterial("heathaze.mat");

// SetMaterialTexture(material,GetColorBuffer(lightbuffer),1);

// SetMaterialTexture(material,GetDepthBuffer(gbuffer),2);

// PaintEntity(heat,material);

// SetEmitterRadius(heat,RAD+0.5,RAD+0.5);

// SetEmitterWaver(heat,0.5);

// SetEmitterRotationSpeed(heat,0.1);

// SetEmitterArea(heat,Vec3(1,0.5,1));

}

 

};//End of Mince Engine Machine Class

 

 

int main(int argc, char** argv)

{

Graphics(1024,768,0,60,GRAPHICS_BACKBUFFER|GRAPHICS_DEPTHBUFFER);

//Create a world

TWorld world=CreateWorld();

if (!world)

{

MessageBoxA(0,"Error","Failed to create world.",0);

//goto exitapp;

return 0;

}

 

//Creeate Refrence Object

MinceMachine Engine;

 

 

Engine.Setup(1);

Engine.CreateCam();

Engine.MMCreateBuffer();

Engine.LoadLevel("scene.gmf");

Engine.CreateHeat(100,1000,0,2,0,0.5);

SetWorld(world);

Engine.CreateLight(0,0.25,0);

Engine.mouse();

Engine.MouseCamupdate();

 

 

//Main loop

while(!KeyHit(KEY_ESCAPE))

{

Engine.MouseCamupdate();

UpdateWorld();

 

//Render Main World

SetBuffer(gbuffer);

SetWorld(world);

RenderWorld();

 

//Render Lights

//SetBuffer(lightbuffer);

//RenderLights(gbuffer);

//SetBuffer(BackBuffer());

//DrawImage(GetColorBuffer(lightbuffer),0,TextureHeight(GetColorBuffer(lightbuffer)),TextureWidth(GetColorBuffer(lightbuffer)),-TextureHeight(GetColorBuffer(lightbuffer)));

 

//Forground Render

SetWorld(foreground);

SetBuffer(BackBuffer());

ClearBuffer(BUFFER_DEPTH);

SetEntityMatrix(fgcam,GetEntityMatrix(cam));

RenderWorld();

 

SetShader(0);

SetWorld(world);

 

 

Flip();

} //End of While Loop

 

}//End of Program

Link to comment
Share on other sites

I generally don't make any global variables. Put those global variables inside your class. Give your class an Init() function that does all these other things like creating the camera and such. Give your class a Run() function that does the main loop, and give your class a Destroy() function that cleans everything up. Your main function would then look more like:

 

int main()
{
MinceMachine Engine;

if(!Engine.Init())
return 1;

Engine.Run();

Engine.Destory();

return 0;

}

 

Nice and clean! I think the purpose of int main() should be to pass control as fast as possible to a more organized system.

 

This is what I warned Josh about with examples that he makes. He makes things global and everything inside int main() which I get for the point of showing the example, but for newer people to C++ it gets them into bad habits and we end up with a game written entirely inside int main(). :)

  • Upvote 1
Link to comment
Share on other sites

Also, define your classes in seperate files with associated headers and simply include the headers in files in which you wish to reference the objects, usually their header file. Makes the main code much more readable and modularises the code nicely.

 

If you need to share variables between classes then pass them as parameters or create a singleton which contains these and allows global access.

Intel Core i5 2.66 GHz, Asus P7P55D, 8Gb DDR3 RAM, GTX460 1Gb DDR5, Windows 7 (x64), LE Editor, GMax, 3DWS, UU3D Pro, Texture Maker Pro, Shader Map Pro. Development language: C/C++

Link to comment
Share on other sites

Also, just note that your engine class is just the start of things. You "should" have many more classes and your engine class "should" pass control around to these more specific classes. A common way would be a Finite State Machine. see the asset store for some code for that, but it helps break your game into more manageable components.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...