c++ game development with oxygine
TRANSCRIPT
Advantages/disadvantages
Actions before starting
Game Project Structure and Mechanics
Oxygine framework
C++ AdvantagesNo extra plugins required for running programs.
Many powerful libs and tools can be applied with c++ programs and this framework
One or two week entrance barrier for C++ junior developers
Engine exist for more than 5 years. Based on SDL2
C++ DisadvantagesNot much web resources about C++ game dev (gamedev.net gamedev.stackexchange.net)
C++98, C++11, C++14 conflicts in many OS build version
Hard to debug C++ game events
A lot of code.
Quotes from web: It takes 10x as many lines of code to do simpleThings.
C++ is dangerous
Before you startingGame design patterns and Activities.
IDE(VS,Qtcreator,Clion,Eclipse,vim/emacs)
Team and resources
In GeneralMemory : Supports smart pointers.
Support culling and fast rendering and compress atlasses.
spGrass grass = new Grass();grass->setCull(true);
spMainMenu _menu = new MainMenu();menu ->addEventListener(TouchEvent::TOUCH, CLOSURE(this, &Game::onTouch));void Game::onTouch(Event* e){_menu->close();};~Game(){removeAllListeners();}// just need to worry about this
DECLARE_SMART(Player, spPlayer);
A lot of things take from ActionScript3.
Grass Tile
In GeneralVectors ,matrices,math and coordinates systems
Vector2 v = target - getPosition();
Vector2 resnorm = v.Normalized() * max_velocityVector;
Vector2 velocity = resnorm - velocityVector;
void Game::onScrollDown(Event*){ Transform is size,position and angle of a object.
Transform t = getTransform(); // game form on scene
t.scale(Vector2(0.9, 0.9));;
setTransform(t);
Rect r = compoundBounds(t);
}
Main classesActor
Actor is a base class in scene graph. It can be moved, rotated, scaled, animated. Actors can have other actors as children. When a parent is transformed, all its children are transformed as well.
spActor player = new Player(this,x,y);
Sprite
You would widely use Sprites in your app. Sprite is subclass of Actor. It is using for displaying images and animations.
Polygon
Polygon is subclass of Sprite. It is used for rendering custom vertices array.
ColorRectSprite
ColorRectSprite is subclass of Sprite. It is rectangle filled with one color.
core::init_desc desc;
desc.title = "Oxygine Application";
#if OXYGINE_SDL || OXYGINE_EMSCRIPTEN
// The initial window size can be set up
here on SDL builds
desc.w = 960;
desc.h = 640;
#endif
core::init(&desc);
Stage::instance = new Stage(true); // your
stage attach to video driver output
Point size = core::getDisplaySize();
getStage()->setSize(size);
game_init();
while (1)
{
int done = mainloop();
if (done)
break;
}
// shut down resources
game_destroy();
// Releases all internal
components and the stage
core::release();
Game begins with entry point
Simplify main function game_init();
while (1)
{
int done = mainloop();
if (done)
break;
}
// shut down resources
game_destroy();
}
MainLoop renderint mainloop()
{
bool done = core::update(); game_update();
getStage()->update();
if (core::beginRendering()){
Color clearColor(32, 32, 32, 255); Rect viewport(Point(0, 0),
core::getDisplaySize());
getStage()->render(clearColor, viewport);
core::swapDisplayBuffers();
}
return done ? 1 : 0;
}
Init Game function//called from main.cpp
void game_init()
{
//load xml file with resources definition
gameResources.loadXML("res.xml");
sound.loadXml(“sound.xml”);
magic_particles.loadXml(“magic.xml”);
//lets create our client code simple actor
spMainActor actor = new MainActor;
//and add it to Stage as child
getStage()->addChild(actor);
}
YourGame.h/cpp fileclass MainActor: public Actor
{
public:
spTextField _text;
spSprite _button;
MainActor()
{
//create button Sprite
spSprite button = new Sprite();
//setup it:
//set button.png image. Resource 'button' defined in 'res.xml'
button->setResAnim(gameResources.getResAnim("button"));
//centered button at screen
Vector2 pos = getStage()->getSize() / 2 - button->getSize() / 2;
button->setPosition(pos);
addChild(button);
}
Animations
Sprite Animations
ResAnim* resAnim = resources.getResAnim("run");
spTween tween = sprite->addTween(TweenAnim(resAnim), duration);
tween->addEventListener(TweenEvent::DONE, [](Event*){
log::messageln("tween done");
});
spTweenQueue tween = new TweenQueue;
tween->add(Actor::TweenX(100), 500);
tween->add(Actor::TweenY(100), 250);
tween->add(Actor::TweenAlpha(0), 300);
sprite->addTween(tween);
MAKEproject (FirstGame)
add_subdirectory(../../../ oxygine-framework)
add_definitions(${OXYGINE_DEFINITIONS})
include_directories(${OXYGINE_INCLUDE_DIRS})
link_directories(${OXYGINE_LIBRARY_DIRS})
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OXYGINE_CXX_FLAGS}")
add_executable(HelloWorld ../src/firstgame.cpp ../src/main.cpp ../src/firstgame.h )
target_link_libraries(FirstGame ${OXYGINE_CORE_LIBS})
.gen_template.py -all // create all make files for all platforms // for users
g++ -pthread -std=c++11 -Ioxygen/oxygine-framework/oxygine/src/ -Ioxygen/SDL/include/ -Loxygen/oxygine-framework/libs -Loxygen/oxygine-framework/examples/Hello/oxygine-framework -o 123 123.cpp -loxygine-framework -lSDL2 -lSDL2main -lGL -ljpeg -lpng -lz
Project structure<?xml version="1.0"?>
<resources>
<set path = "images" />
<atlas>
<image file="anim.png" cols = "8" />
<image file="button.png"/>
</atlas>
<set path = "fonts" />
<font file="main.fnt" />
</resources>
Game ObjectGame object is anything that gets rendered on a game scene. What we need is to update, trigger, collide, calculate or draw objects.
Lifecycle:Init();update(const UpdateStatus&)
GameObject Include:SoundDynamic DataPhysicsRenderEvent Listening
GameObject.hclass Unit : public Object
{
public:
Unit();
virtual ~Unit();
void destroy();
void init(MainActor* game);
void init(MainActor*,float x,float y);
void init(MainActor*, pugi::xml_node*);
void update(const UpdateState &us);
void setPosition(const Vector2 &pos);
b2Body* _body;
spActor _view;
Game* _game;
enum Type
{ Player, Enemy}
};
}
class Zombie : public Unit { }class Npc : public Unit{}class Player: public Unit{}
Box2D A 2D Physics Engine for Games b2BodyDef myBodyDef;
myBodyDef.type = b2_dynamicBody; //this will be a dynamic body
myBodyDef.position.Set(0, 20); //set the starting position
myBodyDef.angle = 0; //set the starting angle
b2Body* dynamicBody = m_world->CreateBody(&myBodyDef);
b2PolygonShape boxShape;
boxShape.SetAsBox(1,1);
b2FixtureDef boxFixtureDef;
boxFixtureDef.shape = &boxShape;
boxFixtureDef.density = 1;
dynamicBody->CreateFixture(&boxFixtureDef);
Mouse, Keyboard, Phone, Game Events etc.void clickHandler(Event*) {}
submitButton->addEventListener(TouchEvent::CLICK, CLOSURE(this, &SomeClass::clickHandler));
Event targeting
.
AchieveEarnedEvent event("awesome");
target->dispatchEvent(&event);
getStage()->addEventListener
(Event::MouseEvent,Event::Scroll_DOWN);
Oxygine key:api:
bool wasPressed(keycode);
bool wasReleased(keycode);
Oxygine.orghttps://github.com/oxygine // sound,flow,magicparticles,social,billing modules
https://github.com/oxygine/oxygine-framework/wiki/
Has forum and skype group