-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3191524
commit 52e2659
Showing
11 changed files
with
268 additions
and
172 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.