Skip to content

Tutorial part 1 : Getting started

linuxlight edited this page Jun 8, 2021 · 15 revisions

First thing you have to do is to clone our repository and download our source codes and header files. You can add them in your editor (e.g. visual studio) (make sure to check directory). Then, create a source file, and name it "Main".

In Main.cpp, create a main function. In here we will create Game object and start the game. But first, we have to make Game class. Make class and name it Game. In Game.h and Game.cpp, copy the following code.

Game.h

#pragma once

#define SCREEN_WIDTH 120
#define SCREEN_HEIGHT 70

#include "game_loop.h"

class Game : public GameLoop
{
private:
	Object* player;
	Object* boundary;

public:
	Game();

	void initialize() override;
	void updateLoop() override;
};

Game.cpp

#include "Game.h"

Game::Game()
{

}

void Game::initialize()
{
    Matrix mat_box = makeBox(getConsole().getScreenWidth(), getConsole().getScreenHeight(), 2);

    boundary = new Object(0, 0);
    boundary->makeImage(mat_box);
    boundary->makeRigidbody();
    boundary->rigidbody.makeMatrixCollider(mat_box);
    boundary->setName("boundary");

    player = new Object(30, 60);
    Matrix plane1 = makeFile2Matrix("./usrlib/plane");
    player->makeRigidbody();
    player->makeImage(plane1);
    player->rigidbody.makeMatrixCollider(plane1);
    player->setName("player");
}

void Game::updateLoop()
{
    getConsole().drawTmpObject(*player);
    getConsole().drawTmpObject(*boundary);
}

In class Game, we declared Object player and boundary. Game inherits GameLoop, so functions and variables in GameLoop can be used in class Game, which makes making a game much easier. Override function initialize() and updateLoop(), and we now have a loop. In function initialize(), we initialized player and boundary, and in updateLoop(), we drew the objects with functions in GraphicEngine. Now we can just start the game in Main.

#include <iostream>

#include "Game.h"

using namespace std;

int main(void) {
    Game game = Game();

    game.setFPS(12);
    game.buildScreen(SCREEN_WIDTH, SCREEN_HEIGHT, 11, 11);

    game.start();
}

If you compile and run, you can see the player and boundary.