Commit d8191ba9 authored by Sadman Kazi's avatar Sadman Kazi 🎸

Fix indentation and header guards

parent ff6301e2
Pipeline #89 passed with stage
in 2 minutes and 50 seconds
......@@ -16,62 +16,62 @@
class Camera
{
public:
Camera(const glm::vec3& pos, float fov, float aspect, float zNear, float zFar) : m_position(pos) {
m_perspective = glm::perspective(fov, aspect, zNear, zFar);
Camera(const glm::vec3& pos, float fov, float aspect, float zNear, float zFar) : m_position(pos) {
m_perspective = glm::perspective(fov, aspect, zNear, zFar);
//assuming that the vertical axis is y
m_forward = glm::vec3(0, 0, 1);
m_up = glm::vec3(0, 1, 0);
}
//assuming that the vertical axis is y
m_forward = glm::vec3(0, 0, 1);
m_up = glm::vec3(0, 1, 0);
}
inline glm::mat4 getViewProjection() const {
return m_perspective * glm::lookAt(m_position, m_position + m_forward, m_up);
}
inline glm::mat4 getViewProjection() const {
return m_perspective * glm::lookAt(m_position, m_position + m_forward, m_up);
}
inline void input() {
//moving the camera
if (Input::getKey(SDLK_w)) {
m_position.z += SPEED;
inline void input() {
//moving the camera
if (Input::getKey(SDLK_w)) {
m_position.z += SPEED;
if (DEBUG_MOVEMENT) {
std::cout << "Pressed W" << std::endl;
}
}
if (Input::getKey(SDLK_s)) {
m_position.z -= SPEED;
}
if (Input::getKey(SDLK_s)) {
m_position.z -= SPEED;
if (DEBUG_MOVEMENT) {
std::cout << "Pressed S" << std::endl;
}
}
if (Input::getKey(SDLK_a)) {
m_position.x += SPEED;
}
if (Input::getKey(SDLK_a)) {
m_position.x += SPEED;
if (DEBUG_MOVEMENT) {
std::cout << "Pressed A" << std::endl;
}
}
if (Input::getKey(SDLK_d)) {
m_position.x -= SPEED;
}
if (Input::getKey(SDLK_d)) {
m_position.x -= SPEED;
if (DEBUG_MOVEMENT) {
std::cout << "Pressed D" << std::endl;
}
}
}
inline void rotate(float angle, glm::vec3& axis) {
m_forward = glm::rotate(m_forward, angle, axis);
m_up = glm::rotate(m_up, angle, axis);
}
inline void translate(glm::vec3& displacement) {
m_position += displacement;
}
}
}
~Camera() {};
inline void rotate(float angle, glm::vec3& axis) {
m_forward = glm::rotate(m_forward, angle, axis);
m_up = glm::rotate(m_up, angle, axis);
}
inline void translate(glm::vec3& displacement) {
m_position += displacement;
}
~Camera() {};
private:
glm::mat4 m_perspective;
glm::vec3 m_position;
glm::vec3 m_forward;
glm::vec3 m_up;
glm::mat4 m_perspective;
glm::vec3 m_position;
glm::vec3 m_forward;
glm::vec3 m_up;
};
#endif //CAMERA_H
/*
A test game engine with C++ and OpenGL
Copyright (C) 2015 Sadman Kazi
A test game engine with C++ and OpenGL
Copyright (C) 2015 Sadman Kazi
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
*/
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
*/
#include "coreEngine.h"
#include <iostream>
......@@ -19,94 +19,94 @@ GNU General Public License for more details.
CoreEngine::CoreEngine(std::string title, int width, int height) : m_title(title), m_width(width), m_height(height)
{
m_mainWindow = new Window(title, width, height);
Window::initGraphics(0.0f, 0.0f, 0.0f, 0.0f);
m_game = new Game();
m_mainWindow = new Window(title, width, height);
Window::initGraphics(0.0f, 0.0f, 0.0f, 0.0f);
m_game = new Game();
m_isRunning = true;
m_isRunning = true;
}
CoreEngine::~CoreEngine()
{
delete m_mainWindow;
delete m_game;
delete m_mainWindow;
delete m_game;
}
void CoreEngine::start() {
std::cout << "Core engine started." << std::endl;
if (!m_isRunning) {
std::cout << "Core engine closing." << std::endl;
return;
}
Time::reset();
run();
std::cout << "Core engine started." << std::endl;
if (!m_isRunning) {
std::cout << "Core engine closing." << std::endl;
return;
}
Time::reset();
run();
}
void CoreEngine::render() {
Window::clear();
m_game->render();
m_mainWindow->update();
Window::clear();
m_game->render();
m_mainWindow->update();
}
//game loop
void CoreEngine::run() {
m_isRunning = true;
std::cout << "Engine starting...";
int frames = 0;
long frameCounter = 0;
double frameTime = 1.0 / FRAME_CAP;
//lastTime before running the game loop
long long lastTime = Time::elapsed();
double unprocessedTime = 0;
while (m_isRunning) {
bool render = false;
long long startTime = Time::elapsed();
long long passedTime = startTime - lastTime;
lastTime = startTime;
unprocessedTime += (double)passedTime/SECOND; //store the time in seconds
frameCounter += passedTime; //store the time in nanoseconds
//update the time when the loop has run for frame time seconds
while (unprocessedTime > frameTime) {
render = true;
unprocessedTime -= frameTime;
if (m_mainWindow->isClosed())
stop();
Time::setDelta(frameTime);
//update the game here
m_game->input(); //process the inputs
m_game->update(); //update game values
//display the frame count every second
if (frameCounter >= SECOND) {
std::cout << frames << std::endl;
frames = 0;
frameCounter = 0;
}
}
if (render) {
this->render();
frames++;
} else {
std::this_thread::sleep_for(std::chrono::milliseconds(1)); //delay the loop so that calculations are not made too often
m_isRunning = true;
std::cout << "Engine starting...";
int frames = 0;
long frameCounter = 0;
double frameTime = 1.0 / FRAME_CAP;
//lastTime before running the game loop
long long lastTime = Time::elapsed();
double unprocessedTime = 0;
while (m_isRunning) {
bool render = false;
long long startTime = Time::elapsed();
long long passedTime = startTime - lastTime;
lastTime = startTime;
unprocessedTime += (double)passedTime/SECOND; //store the time in seconds
frameCounter += passedTime; //store the time in nanoseconds
//update the time when the loop has run for frame time seconds
while (unprocessedTime > frameTime) {
render = true;
unprocessedTime -= frameTime;
if (m_mainWindow->isClosed())
stop();
Time::setDelta(frameTime);
//update the game here
m_game->input(); //process the inputs
m_game->update(); //update game values
//display the frame count every second
if (frameCounter >= SECOND) {
std::cout << frames << std::endl;
frames = 0;
frameCounter = 0;
}
}
if (render) {
this->render();
frames++;
} else {
std::this_thread::sleep_for(std::chrono::milliseconds(1)); //delay the loop so that calculations are not made too often
}
}
}
}
void CoreEngine::stop() {
m_isRunning = false;
m_isRunning = false;
}
/*
A test game engine with C++ and OpenGL
Copyright (C) 2015 Sadman Kazi
A test game engine with C++ and OpenGL
Copyright (C) 2015 Sadman Kazi
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
*/
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
*/
#ifndef CORE_ENGINE_H_
#define CORE_ENGINE_H_
......@@ -24,22 +24,22 @@ GNU General Public License for more details.
class CoreEngine
{
public:
CoreEngine(std::string title = TITLE, int width = WIDTH, int height = HEIGHT);
CoreEngine(std::string title = TITLE, int width = WIDTH, int height = HEIGHT);
void start();
void stop();
void start();
void stop();
~CoreEngine();
~CoreEngine();
private:
Window* m_mainWindow;
int m_width, m_height;
const std::string m_title;
bool m_isRunning;
Game* m_game;
void run();
void render();
Window* m_mainWindow;
int m_width, m_height;
const std::string m_title;
bool m_isRunning;
Game* m_game;
void run();
void render();
};
#endif // CORE_ENGINE_H_
/*
A test game engine with C++ and OpenGL
Copyright (C) 2015 Sadman Kazi
A test game engine with C++ and OpenGL
Copyright (C) 2015 Sadman Kazi
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
*/
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
*/
#include "game.h"
#include "input.h"
......@@ -20,72 +20,72 @@ GNU General Public License for more details.
Game::Game()
{
Vertex data[] = { Vertex(glm::vec3(-0.5, -0.5, 0)),
Vertex(glm::vec3(0, 0.5, 0)),
Vertex(glm::vec3(0.5, -0.5, 0)),
Vertex(glm::vec3(0, -0.5, 0.5))};
unsigned int indices[] = {0, 1, 3,
3, 1, 2,
2, 1, 0,
0, 2, 3};
Vertex data[] = { Vertex(glm::vec3(-0.5, -0.5, 0)),
Vertex(glm::vec3(0, 0.5, 0)),
Vertex(glm::vec3(0.5, -0.5, 0)),
Vertex(glm::vec3(0, -0.5, 0.5))};
m_shader = new Shader("../res/basicShader");
unsigned int indices[] = {0, 1, 3,
3, 1, 2,
2, 1, 0,
0, 2, 3};
//mesh = new Mesh(data, sizeof(data)/sizeof(data[0]), indices, sizeof(indices)/sizeof(indices[0]));
m_shader = new Shader("../res/basicShader");
m_mesh = new Mesh("../res/monkey.obj");
m_transform = new Transform();
m_camera = new Camera(glm::vec3(0.0f, 0.0f, -2.0f), 70.0f, (float)WIDTH/HEIGHT, 1.0f, 100.0f);
//mesh = new Mesh(data, sizeof(data)/sizeof(data[0]), indices, sizeof(indices)/sizeof(indices[0]));
m_transform->setScale(glm::vec3(0.5f, 0.5f, 0.5f));
m_mesh = new Mesh("../res/monkey.obj");
m_transform = new Transform();
m_camera = new Camera(glm::vec3(0.0f, 0.0f, -2.0f), 70.0f, (float)WIDTH/HEIGHT, 1.0f, 100.0f);
m_shader->addUniform("u_transform");
m_counter = 0;
m_transform->setScale(glm::vec3(0.5f, 0.5f, 0.5f));
m_shader->addUniform("u_transform");
m_counter = 0;
}
Game::~Game()
{
delete m_mesh;
delete m_shader;
delete m_transform;
delete m_camera;
delete m_mesh;
delete m_shader;
delete m_transform;
delete m_camera;
}
void Game::input() {
m_camera->input();
m_camera->input();
}
void Game::render() {
m_shader->bind();
m_mesh->draw();
/*if (Input::getKeyDown(SDLK_UP)) {
std::cout << "You have pressed up!" << std::endl;
m_camera->translate(glm::vec3(0.0f, 0.0f, 0.1f));
}
else if (Input::getKeyUp(SDLK_UP)) {
std::cout << "You have released up!" << std::endl;
}
if (Input::getMouseDown(SDL_BUTTON_LEFT)) {
std::cout << "X: " << Input::getMousePos().x << " Y: " << Input::getMousePos().y << std::endl;
}
else if (Input::getMouseUp(SDL_BUTTON_LEFT)) {
std::cout << "You released the left mouse button!" << std::endl;
}
*/
m_shader->bind();
m_mesh->draw();
/*if (Input::getKeyDown(SDLK_UP)) {
std::cout << "You have pressed up!" << std::endl;
m_camera->translate(glm::vec3(0.0f, 0.0f, 0.1f));
}
else if (Input::getKeyUp(SDLK_UP)) {
std::cout << "You have released up!" << std::endl;
}
if (Input::getMouseDown(SDL_BUTTON_LEFT)) {
std::cout << "X: " << Input::getMousePos().x << " Y: " << Input::getMousePos().y << std::endl;
}
else if (Input::getMouseUp(SDL_BUTTON_LEFT)) {
std::cout << "You released the left mouse button!" << std::endl;
}
*/
}
void Game::update() {
m_counter += Time::getDelta();
//std::cout << counter << std::endl;
float sinCounter = m_counter;
float absSinCounter = abs(sinCounter);
m_counter += Time::getDelta();
//std::cout << counter << std::endl;
float sinCounter = m_counter;
float absSinCounter = abs(sinCounter);
//transform->GetPos().x = sinCounter;
m_transform->getRot().y = sinCounter;
//transform->GetRot().z = sinCounter;
//transform->GetPos().x = sinCounter;
m_transform->getRot().y = sinCounter;
//transform->GetRot().z = sinCounter;
m_shader->setUniformMat4("u_transform", m_transform->getProjectedModel(m_camera));
m_shader->setUniformMat4("u_transform", m_transform->getProjectedModel(m_camera));
}
/*
A test game engine with C++ and OpenGL
Copyright (C) 2015 Sadman Kazi
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
*/
A test game engine with C++ and OpenGL
Copyright (C) 2015 Sadman Kazi
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
*/
#ifndef GAME_H_
#define GAME_H_
#include "mesh.h"
#include "shader.h"
#include "transform.h"
......@@ -25,19 +28,20 @@ GNU General Public License for more details.
class Game
{
public:
Game();
Game();
void input();
void update();
void render();
void input();
void update();
void render();
virtual ~Game();
virtual ~Game();
private:
Mesh* m_mesh;
Shader* m_shader;
Transform* m_transform;
Camera* m_camera;
float m_counter;
Mesh* m_mesh;
Shader* m_shader;
Transform* m_transform;
Camera* m_camera;
float m_counter;
};
#endif // GAME_H_
/*
A test game engine with C++ and OpenGL
Copyright (C) 2015 Sadman Kazi
A test game engine with C++ and OpenGL
Copyright (C) 2015 Sadman Kazi
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
*/
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
*/
#include "input.h"
#include <stdlib.h>
......@@ -26,92 +26,92 @@ std::vector<Uint8> Input::upMouse(0); //store the mouse buttons released in the
template <typename T>
bool contains(std::vector<T> list, T item) {
return std::find(list.begin(), list.end(), item) != list.end();
return std::find(list.begin(), list.end(), item) != list.end();
}
void Input::update(SDL_Event* e) {
Input::downKeys.clear();
Input::upKeys.clear();
Input::downMouse.clear();
Input::upMouse.clear();
if (e != NULL) {
std::vector<SDL_Keycode>::iterator index;
std::vector<Uint8>::iterator mouseIndex;
switch (e->type) {
//handle mouse clicks
case SDL_MOUSEBUTTONDOWN:
if (!contains(currentMouse, e->button.button)) {
Input::downMouse.push_back(e->button.button);
Input::currentMouse.push_back(e->button.button);
}
break;
case SDL_MOUSEBUTTONUP:
if (contains(currentMouse, e->button.button)) {
mouseIndex = std::find(Input::currentMouse.begin(), Input::currentMouse.end(), e->button.button);
if (mouseIndex != Input::currentMouse.end()) {
Input::currentMouse.erase(mouseIndex);
}
}
Input::upMouse.push_back(e->button.button);
break;
case SDL_KEYDOWN:
//add the key to downkeys
if (!contains(currentKeys, e->key.keysym.sym)) {
Input::downKeys.push_back(e->key.keysym.sym);
//add it to current keys as well
Input::currentKeys.push_back(e->key.keysym.sym);
}
break;
case SDL_KEYUP:
if (contains(currentKeys, e->key.keysym.sym)) {
//remove it from current keys as well
//first find and store the index of the found element
index = std::find(Input::currentKeys.begin(), Input::currentKeys.end(), e->key.keysym.sym);
if (index != Input::currentKeys.end()) {
Input::currentKeys.erase(index);
}
}
//add the key to up keys
Input::upKeys.push_back(e->key.keysym.sym);
break;
default:
break;
}
}
Input::downKeys.clear();
Input::upKeys.clear();
Input::downMouse.clear();
Input::upMouse.clear();
if (e != NULL) {
std::vector<SDL_Keycode>::iterator index;
std::vector<Uint8>::iterator mouseIndex;
switch (e->type) {
//handle mouse clicks
case SDL_MOUSEBUTTONDOWN:
if (!contains(currentMouse, e->button.button)) {
Input::downMouse.push_back(e->button.button);
Input::currentMouse.push_back(e->button.button);
}
break;
case SDL_MOUSEBUTTONUP:
if (contains(currentMouse, e->button.button)) {
mouseIndex = std::find(Input::currentMouse.begin(), Input::currentMouse.end(), e->button.button);
if (mouseIndex != Input::currentMouse.end()) {
Input::currentMouse.erase(mouseIndex);
}
}
Input::upMouse.push_back(e->button.button);
break;
case SDL_KEYDOWN:
//add the key to downkeys
if (!contains(currentKeys, e->key.keysym.sym)) {
Input::downKeys.push_back(e->key.keysym.sym);
//add it to current keys as well
Input::currentKeys.push_back(e->key.keysym.sym);
}
break;
case SDL_KEYUP:
if (contains(currentKeys, e->key.keysym.sym)) {
//remove it from current keys as well
//first find and store the index of the found element
index = std::find(Input::currentKeys.begin(), Input::currentKeys.end(), e->key.keysym.sym);
if (index != Input::currentKeys.end()) {
Input::currentKeys.erase(index);
}
}
//add the key to up keys
Input::upKeys.push_back(e->key.keysym.sym);
break;
default:
break;
}
}
}
bool Input::getKey(SDL_Keycode key) {
return contains(currentKeys, key);
return contains(currentKeys, key);
}
bool Input::getKeyDown(SDL_Keycode key) {
return contains(downKeys, key);
return contains(downKeys, key);
}
bool Input::getKeyUp(SDL_Keycode key) {
return contains(upKeys, key);
return contains(upKeys, key);
}
bool Input::getMouse(Uint8 button) {
return contains(currentMouse, button);
return contains(currentMouse, button);
}
bool Input::getMouseDown(Uint8 button) {
return contains(downMouse, button);