Skip to content

Commit

Permalink
more abstractions, fixedUpdate
Browse files Browse the repository at this point in the history
  • Loading branch information
IgorKhramtsov committed Aug 1, 2020
1 parent 3191524 commit 52e2659
Show file tree
Hide file tree
Showing 11 changed files with 268 additions and 172 deletions.
90 changes: 90 additions & 0 deletions src/Application.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#include "Application.h"

#include <stdio.h>
#include <iostream>

Application *Application::instance;
Application::Application(const char *title, int width, int height) {
if (Application::instance == nullptr)
Application::instance = this;
else {
std::cout << "App already initialized!\n";
return;
}
// glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
// glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 5);
// glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

glfwSetErrorCallback(Application::glfw_error_callback);
if (!glfwInit()) {
std::cout << "glfw initialization failed.\n";
return;
}

m_Window = glfwCreateWindow(width, height, title, NULL, NULL);
if(!m_Window) {
std::cout << "glfw window initialization failed.\n";
return;
}
glfwMakeContextCurrent(m_Window);
if (glewInit() != 0) {
std::cout << "glew initialization failed.\n";
return;
}
glfwSetKeyCallback(m_Window, key_callback);
std::cout << "OpenGL version: " << glGetString(GL_VERSION) << '\n';

glfwGetFramebufferSize(m_Window, &m_Width, &m_Height);
glViewport(0, 0, m_Width, m_Height);
glfwSwapInterval(1);

m_Initialized = true;
m_Renderer = Renderer((float)m_Width, (float)m_Height);
}

Application::~Application() {
glfwDestroyWindow(m_Window);
glfwTerminate();
}

void Application::Run() {

auto lastTime = glfwGetTime();
double curTime;
double deltaTime = 0.;
double updateRate = 1. / 50.;

while(!glfwWindowShouldClose(m_Window)) {
m_Renderer.Clear();

curTime = glfwGetTime();
deltaTime += (curTime - lastTime);
lastTime = curTime;

if(deltaTime >= updateRate) {
onUpdate();
deltaTime -= updateRate;
}

onDraw();

glfwSwapBuffers(m_Window);
glfwPollEvents();

}
}

bool Application::isKeyPressed(int key) {
return glfwGetKey(m_Window, key) == GLFW_PRESS;
}

void Application::key_callback(GLFWwindow *window, int key, int scancode, int action, int mods) {
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
glfwSetWindowShouldClose(window, GLFW_TRUE);

Application::getApp()->onKeyCallback(key, action);
}

void Application::glfw_error_callback(int error, const char* desc) {
std::cout << "[GLFW] error #" << error << " " << desc << '\n';
}
40 changes: 40 additions & 0 deletions src/Application.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#pragma once

#include <GL/glew.h>
#include <GLFW/glfw3.h>

#include "Renderer.h"

class Application {
private:
static Application *instance;
int m_Width, m_Height;
GLFWwindow *m_Window;
bool m_Initialized = false;

static void glfw_error_callback(int error, const char* desc);
static void key_callback(GLFWwindow *window, int key, int scancode, int action, int mods);
void gldebug(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const GLchar* message, const void* userParam);

protected:
Renderer m_Renderer;

public:
Application(const char*, int, int);
~Application();

static Application *getApp() {
return Application::instance;
};

void Run();

virtual void onUpdate() = 0;
virtual void onDraw() = 0;
virtual void onKeyCallback(int key, int action) = 0;

Renderer *getRenderer() { return &m_Renderer; };
void getSize(int &width, int &height) { width = m_Width; height = m_Height; };
bool isInitialized() { return m_Initialized; };
bool isKeyPressed(int key);
};
5 changes: 5 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@ list(APPEND SOURCES
${CMAKE_CURRENT_SOURCE_DIR}/Shader.h
${CMAKE_CURRENT_SOURCE_DIR}/Shader.cpp
${CMAKE_CURRENT_SOURCE_DIR}/GameObject.h
${CMAKE_CURRENT_SOURCE_DIR}/GameObject.cpp
${CMAKE_CURRENT_SOURCE_DIR}/FileWatcher.h
${CMAKE_CURRENT_SOURCE_DIR}/Application.h
${CMAKE_CURRENT_SOURCE_DIR}/Application.cpp
${CMAKE_CURRENT_SOURCE_DIR}/MyApp.h
${CMAKE_CURRENT_SOURCE_DIR}/MyApp.cpp
)

set(SOURCES ${SOURCES} PARENT_SCOPE)
35 changes: 0 additions & 35 deletions src/FileWatcher.h

This file was deleted.

24 changes: 24 additions & 0 deletions src/GameObject.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include "GameObject.h"

GameObject::GameObject(Renderable r, Transform t, glm::vec4 c) : transform(t), color(c)
{
this->renderebale = r;
}

glm::vec2 GameObject::getCenter() const {
return glm::vec2(transform.pos.x + renderebale.width / 2., transform.pos.y + renderebale.height / 2.);
}

void GameObject::setColor(float v0, float v1, float v2, float v3) {
color = glm::vec4(v0, v1, v2, v3);
}

void GameObject::setPosition(float x, float y) {
transform.pos[0] = x;
transform.pos[1] = y;
}

void GameObject::setScale(float x, float y) {
transform.scale[0] = x;
transform.scale[1] = y;
}
26 changes: 5 additions & 21 deletions src/GameObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,10 @@ class GameObject {
glm::vec4 color = glm::vec4(1.f, 0.f, 0.f, 1.f);

GameObject() = default;
GameObject(Renderable r, Transform t, glm::vec4 c) : transform(t), color(c)
{
this->renderebale = r;
}
GameObject(Renderable r, Transform t, glm::vec4 c);

glm::vec2 getCenter() const {
return glm::vec2(transform.pos.x + renderebale.width / 2., transform.pos.y + renderebale.height / 2.);
}

void setColor(float v0, float v1, float v2, float v3) {
color = glm::vec4(v0, v1, v2, v3);
}

void setPosition(float x, float y) {
transform.pos[0] = x;
transform.pos[1] = y;
}

void setScale(float x, float y) {
transform.scale[0] = x;
transform.scale[1] = y;
}
glm::vec2 getCenter() const;
void setColor(float v0, float v1, float v2, float v3);
void setPosition(float x, float y);
void setScale(float x, float y);
};
57 changes: 57 additions & 0 deletions src/MyApp.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#include "MyApp.h"

MyApp::MyApp(const char *title, int width, int height) : Application(title, width, height) {
m_Renderer.setClearColor(0.1f, 0.1f, 0.1f, 1.f);

Start();
}

void MyApp::Start() {
int width, height;
getSize(width, height);

int brick_w = ((width - padding_x * 2) - (spacing * 15)) / 15;
int brick_h = 20;
auto renderable = m_Renderer.CreateRect(brick_w, brick_h);

for (int i = 0; i < 30; i++) {
m_Bricks[i] = GameObject();
m_Bricks[i].renderebale = renderable;
m_Bricks[i].setPosition(
padding_x + (brick_w + spacing) * (i % 15),
((float)height - padding_y) - ((brick_h + spacing) * (i % 2))
);
m_Bricks[i].setColor(0.3f, 0.3f, 0.7f, 1.f);
}
m_Platform = GameObject();
m_Platform.renderebale = m_Renderer.CreateRect(120, 20);
m_Platform.setColor(0.3f, 0.43f, 0.3f, 1.f);
m_Platform.setPosition(width / 2.f - 120.f / 2.f, 20.f + 30.f);

m_Ball = GameObject();
m_Ball.renderebale = m_Renderer.CreateRect(25, 25);
m_Ball.setColor(0.7f, 0.3f, 0.3f, 1.f);
m_Ball.setPosition(m_Platform.transform.pos.x + 120.f / 2.f - 25.f / 2.f, m_Platform.transform.pos.y + 30.f);
}

void MyApp::onUpdate() {
if (isKeyPressed(GLFW_KEY_LEFT))
m_Platform.setPosition(m_Platform.transform.pos.x - 1.f, m_Platform.transform.pos.y);
}

void MyApp::onDraw() {
for (int i = 0; i < 30; i++) {
m_Renderer.Draw(m_Bricks[i], m_SimpleShader);
}

m_Renderer.Draw(m_Platform, m_SimpleShader);

m_Renderer.UseShader(m_BallShader);
m_BallShader.setUniformVec2f("u_Center", m_Ball.getCenter());
m_BallShader.setUniform1f("u_Radius", m_Ball.renderebale.height / 2.);
m_Renderer.Draw(m_Ball, m_BallShader);
}

void MyApp::onKeyCallback(int key, int action) {

}
24 changes: 24 additions & 0 deletions src/MyApp.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#pragma once

#include "Application.h"
#include "GameObject.h"

class MyApp : public Application {
private:
GameObject *m_Bricks = new GameObject[30];
GameObject m_Platform;
GameObject m_Ball;
Shader m_SimpleShader = Shader("assets/brick.vert", "assets/brick.frag");
Shader m_BallShader = Shader("assets/brick.vert", "assets/ball.frag");

int padding_x = 50;
int padding_y = 200;
int spacing = 10;
public:
MyApp(const char *title, int width, int height);

void Start();
void onUpdate();
void onDraw();
void onKeyCallback(int key, int action);
};
12 changes: 12 additions & 0 deletions src/Renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ Renderer::Renderer(float width, float height) {
this->m_View = glm::mat4(1.0f);
}

void Renderer::setClearColor(float v0, float v1, float v2, float v3) {
m_ClearColor.r = v0;
m_ClearColor.g = v1;
m_ClearColor.b = v2;
m_ClearColor.a = v3;
}

Renderable Renderer::CreateRect(float w, float h) {
const float vertices[] = {
0.f, 0.f,
Expand Down Expand Up @@ -64,3 +71,8 @@ void Renderer::Draw(const GameObject &obj,const Shader &shader) {
GLCALL(glBindVertexArray(obj.renderebale.vao));
GLCALL(glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0));
}

void Renderer::Clear() {
glClear(GL_COLOR_BUFFER_BIT);
glClearColor(m_ClearColor.r, m_ClearColor.g, m_ClearColor.b, m_ClearColor.a);
}
6 changes: 6 additions & 0 deletions src/Renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ struct Transform {
glm::vec3 pos = glm::vec3(0.f, 0.f, 0.f);
glm::vec3 scale = glm::vec3(1.f, 1.f, 1.f);
glm::mat4 model = glm::mat4(1.0f);
glm::vec2 velocity = glm::vec2(0.f, 0.f);

Transform() = default;
};
Expand All @@ -23,15 +24,20 @@ class Renderer {
private:
glm::mat4 m_Proj;
glm::mat4 m_View;
glm::vec4 m_ClearColor;

unsigned int currentShader = 0;


public:
Renderer() = default;
Renderer(float width, float height);
~Renderer() = default;
Renderable CreateRect(float w, float h);

void setClearColor(float v0, float v1, float v2, float v3);
void Clear();

void UseShader(const Shader&);
void Draw(const GameObject&, const Shader&);
};
Loading

0 comments on commit 52e2659

Please sign in to comment.