Skip to content

Commit

Permalink
v1.0
Browse files Browse the repository at this point in the history
Game is done
  • Loading branch information
JayNakum committed Aug 6, 2022
1 parent 4c72598 commit ab816dc
Show file tree
Hide file tree
Showing 24 changed files with 174 additions and 40 deletions.
3 changes: 3 additions & 0 deletions OutForDelivery/OutForDelivery.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="src\House.cpp" />
<ClCompile Include="src\Cannon.cpp" />
<ClCompile Include="src\Ground.cpp" />
<ClCompile Include="src\Package.cpp" />
Expand All @@ -170,6 +171,7 @@
<ClCompile Include="src\engine\Window.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="src\House.h" />
<ClInclude Include="src\Cannon.h" />
<ClInclude Include="src\Ground.h" />
<ClInclude Include="src\Package.h" />
Expand Down Expand Up @@ -329,6 +331,7 @@
<ClInclude Include="vendors\KHR\khrplatform.h" />
<ClInclude Include="vendors\stb_image\stb_image.h" />
<ClInclude Include="src\engine\Window.h" />
<ClInclude Include="vendors\vallentin\gltext.h" />
</ItemGroup>
<ItemGroup>
<None Include="res\shaders\3.3.shader.fs" />
Expand Down
9 changes: 9 additions & 0 deletions OutForDelivery/OutForDelivery.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@
<ClCompile Include="src\Store.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="src\House.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="vendors\glad\glad.h">
Expand Down Expand Up @@ -527,6 +530,12 @@
<ClInclude Include="src\Store.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="vendors\vallentin\gltext.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="src\House.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="vendors\glm\detail\func_common.inl">
Expand Down
Binary file modified OutForDelivery/res/textures/cannon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified OutForDelivery/res/textures/grass.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added OutForDelivery/res/textures/house.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added OutForDelivery/res/textures/p_cannon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added OutForDelivery/res/textures/p_grass.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes
Binary file added OutForDelivery/res/textures/p_store.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added OutForDelivery/res/textures/package.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified OutForDelivery/res/textures/store.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
50 changes: 31 additions & 19 deletions OutForDelivery/src/Application.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
#include "Application.h"

#include <cstdlib>
#include "Ground.h"
#include "Store.h"
#include "House.h"
#include "Cannon.h"
#include "Package.h"

// settings
const unsigned int SCR_WIDTH = 900;
const unsigned int SCR_HEIGHT = 600;

Application::Application(const char* name)
: _name(name)
{
Expand Down Expand Up @@ -37,21 +34,29 @@ Application::~Application()
void Application::run()
{
bool canShoot = true;
bool reset = true;
srand(time(0));
Shader shaders("res\\shaders\\3.3.shader.vs", "res\\shaders\\3.3.shader.fs");

Ground ground(shaders);
Store store(shaders);
House house(shaders);
Cannon cannon(shaders);
Package package(shaders);

glm::mat4 projection = glm::mat4(1.0f);
// projection = glm::perspective(glm::radians(45.0f), (float)_width / (float)_height, 0.1f, 100.0f);
projection = glm::ortho(-2.0f, +2.0f, -1.5f, +1.5f, 0.1f, 100.0f);
shaders.setMat4("projection", projection);
glm::vec3 camera = glm::vec3(0.0f, 0.0f, -3.0f);

while (_isRunning)
{
float currentFrame = static_cast<float>(glfwGetTime());
deltaTime = currentFrame - lastFrame;
lastFrame = currentFrame;

if (reset)
{
camera = glm::vec3(0.0f, 0.0f, -3.0f);
reset = false;
}

_renderer->clear();

if (_window->shouldClose()) _isRunning = false;
Expand All @@ -63,29 +68,36 @@ void Application::run()
ground.render(*_renderer);
store.render(*_renderer);
cannon.render(*_renderer);

house.render(*_renderer);
package.render(*_renderer, camera);
if (_window->isPressed(GLFW_KEY_UP))
{
if(cannon.angle < 90.0f)
cannon.angle += 0.1f;
cannon.angle += 1.0f;
}
if (_window->isPressed(GLFW_KEY_DOWN))
{
if (cannon.angle > 0.0f)
cannon.angle -= 0.1f;
cannon.angle -= 1.0f;
}
if (_window->isPressed(GLFW_KEY_SPACE) && canShoot)
{
if (canShoot)
{
package.shoot(cannon.power, cannon.angle / 100);
canShoot = false;
}

canShoot = false;
}
if (_window->isPressed(GLFW_KEY_R) && !reset)
{
if (package.getPos() >= house.getPos() - 1.0f && package.getPos() <= house.getPos() + 1.0f)
{
house.reset((rand() % 10) + 1);

_window->render(&shaders);
}
package.reset();
reset = true;
canShoot = true;
}

_window->render();
glfwPollEvents();
}
}
Expand Down
1 change: 0 additions & 1 deletion OutForDelivery/src/Application.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ class Application
bool _isRunning;
Window* _window;
Renderer* _renderer;

float deltaTime = 0.0f;
float lastFrame = 0.0f;
};
2 changes: 1 addition & 1 deletion OutForDelivery/src/Cannon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ void Cannon::initTextures()
unsigned char* data = stbi_load("res\\textures\\cannon.png", &width, &height, &nrChannels, 0);
if (data)
{
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D);
}
else
Expand Down
4 changes: 2 additions & 2 deletions OutForDelivery/src/Cannon.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ class Cannon
Cannon(Shader& shader);
void render(Renderer& renderer);

float power = 0.03f;
float angle = 40.0f;
float power = 0.15f;
float angle = 30.0f;
private:

float _vertices[20] = {
Expand Down
2 changes: 1 addition & 1 deletion OutForDelivery/src/Ground.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ void Ground::initTextures()
unsigned char* data = stbi_load("res\\textures\\grass.png", &width, &height, &nrChannels, 0);
if (data)
{
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D);
}
else
Expand Down
77 changes: 77 additions & 0 deletions OutForDelivery/src/House.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#include "House.h"
#include "stb_image/stb_image.h"

void House::reset(float newPos)
{
translation.x = newPos;
}

void House::render(Renderer& renderer)
{
m_shader.use();
glBindTexture(GL_TEXTURE_2D, texture);

glm::mat4 model = glm::mat4(1.0f);
model = glm::translate(model, translation);
m_shader.setMat4("model", model);

renderer.render(VAO, EBO);
}

void House::initTextures()
{

m_shader.use();
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

int width, height, nrChannels;
stbi_set_flip_vertically_on_load(true);

unsigned char* data = stbi_load("res\\textures\\house.png", &width, &height, &nrChannels, 0);
if (data)
{
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D);
}
else
{
ERROR("Failed to load texture.");
}
stbi_image_free(data);
m_shader.setInt("texture", 0);
}

House::House(Shader& shader)
: m_shader(shader)
{
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glGenBuffers(1, &EBO);

glBindVertexArray(VAO);

glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(_vertices), _vertices, GL_STATIC_DRAW);

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(_indices), _indices, GL_STATIC_DRAW);

// position attribute
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
// texture coord attribute
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(3 * sizeof(float)));
glEnableVertexAttribArray(1);

initTextures();

glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
}
33 changes: 33 additions & 0 deletions OutForDelivery/src/House.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#pragma once
#include "engine/Renderer.h"

class House
{
public:
House(Shader& shader);
void render(Renderer& renderer);
void reset(float newPos);
inline float getPos() { return translation.x; }
private:

float _vertices[20] = {
// positions // texture coords
0.5f, 0.5f, 0.0f, 1.0f, 1.0f, // top right
0.5f, -0.5f, 0.0f, 1.0f, 0.0f, // bottom right
-0.5f, -0.5f, 0.0f, 0.0f, 0.0f, // bottom left
-0.5f, 0.5f, 0.0f, 0.0f, 1.0f // top left
};
unsigned int _indices[6] = {
0, 1, 3, // first Triangle
1, 2, 3 // second Triangle
};
unsigned int VAO, VBO, EBO;

unsigned int texture = 0;
void initTextures();

glm::vec3 translation = glm::vec3(10.0f, -0.5f, 0.0f);

Shader m_shader;

};
13 changes: 9 additions & 4 deletions OutForDelivery/src/Package.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ void Package::shoot(float power, float angle)
fire = true;
}

void Package::reset()
{
translation = glm::vec3(-0.1f, -0.9f, 0.0f);
}

void Package::render(Renderer& renderer, glm::vec3& camera)
{
m_shader.use();
Expand All @@ -27,8 +32,8 @@ void Package::render(Renderer& renderer, glm::vec3& camera)
translation.x += xVelocity;
translation.y += yVelocity;

rotation -= 1.0f ;
yVelocity -= 0.0001f; // resistance
rotation -= xVelocity * 10 ;
yVelocity -= 0.001f; // resistance

if (translation.y <= -0.9f)
{
Expand Down Expand Up @@ -57,10 +62,10 @@ void Package::initTextures()
int width, height, nrChannels;
stbi_set_flip_vertically_on_load(true);

unsigned char* data = stbi_load("res\\textures\\package.jpg", &width, &height, &nrChannels, 0);
unsigned char* data = stbi_load("res\\textures\\package.png", &width, &height, &nrChannels, 0);
if (data)
{
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D);
}
else
Expand Down
5 changes: 3 additions & 2 deletions OutForDelivery/src/Package.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ class Package
Package(Shader& shader);
void render(Renderer& renderer, glm::vec3&);
void shoot(float power, float angle);

inline float getPos() { return translation.x; }
void reset();
private:
float yVelocity = 0.0f;
float xVelocity = 0.001f;
Expand All @@ -31,7 +32,7 @@ class Package
unsigned int texture = 0;
void initTextures();

glm::vec3 translation = glm::vec3(0.0f, -0.9f, 0.0f);
glm::vec3 translation = glm::vec3(-0.1f, -0.9f, 0.0f);

Shader m_shader;

Expand Down
2 changes: 1 addition & 1 deletion OutForDelivery/src/Store.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ void Store::initTextures()
unsigned char* data = stbi_load("res\\textures\\store.png", &width, &height, &nrChannels, 0);
if (data)
{
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D);
}
else
Expand Down
4 changes: 2 additions & 2 deletions OutForDelivery/src/engine/Renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ void Renderer::render(const unsigned int& VAO, const unsigned int& EBO) const

void Renderer::clear() const
{
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClearColor(0.607f, 0.721f, 0.929f, 1.0f);
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

glfwSwapInterval(1);
}
7 changes: 1 addition & 6 deletions OutForDelivery/src/engine/Window.cpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
#include "Window.h"

void Window::render(Shader* shader)
void Window::render()
{
if (_updateViewport)
{
glfwGetFramebufferSize(_glfwWindow, &_viewportWidth, &_viewportHeight);
glViewport(0, 0, _viewportWidth, _viewportHeight);

glm::mat4 projection = glm::mat4(1.0f);
projection = glm::perspective(glm::radians(45.0f), (float)_width / (float)_height, 0.1f, 100.0f);
shader->setMat4("projection", projection);

_updateViewport = false;
}

Expand Down
Loading

0 comments on commit ab816dc

Please sign in to comment.