Skip to content

Commit

Permalink
v0.0.3
Browse files Browse the repository at this point in the history
Cannon and Store
  • Loading branch information
JayNakum committed Aug 5, 2022
1 parent 14e6167 commit 4c72598
Show file tree
Hide file tree
Showing 17 changed files with 273 additions and 20 deletions.
4 changes: 4 additions & 0 deletions OutForDelivery/OutForDelivery.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -157,24 +157,28 @@
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="src\Cannon.cpp" />
<ClCompile Include="src\Ground.cpp" />
<ClCompile Include="src\Package.cpp" />
<ClCompile Include="src\Application.cpp" />
<ClCompile Include="src\engine\Log.cpp" />
<ClCompile Include="src\engine\Renderer.cpp" />
<ClCompile Include="src\Store.cpp" />
<ClCompile Include="vendors\glad\glad.c" />
<ClCompile Include="vendors\glm\detail\glm.cpp" />
<ClCompile Include="vendors\stb_image\stb_image.cpp" />
<ClCompile Include="src\engine\Window.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="src\Cannon.h" />
<ClInclude Include="src\Ground.h" />
<ClInclude Include="src\Package.h" />
<ClInclude Include="src\Application.h" />
<ClInclude Include="src\engine\Camera.h" />
<ClInclude Include="src\engine\Log.h" />
<ClInclude Include="src\engine\Renderer.h" />
<ClInclude Include="src\engine\Shader.h" />
<ClInclude Include="src\Store.h" />
<ClInclude Include="vendors\glad\glad.h" />
<ClInclude Include="vendors\GLFW\glfw3.h" />
<ClInclude Include="vendors\GLFW\glfw3native.h" />
Expand Down
12 changes: 12 additions & 0 deletions OutForDelivery/OutForDelivery.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@
<ClCompile Include="src\Ground.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="src\Cannon.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="src\Store.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="vendors\glad\glad.h">
Expand Down Expand Up @@ -515,6 +521,12 @@
<ClInclude Include="src\Ground.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="src\Cannon.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="src\Store.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="vendors\glm\detail\func_common.inl">
Expand Down
Binary file added 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.
File renamed without changes
Binary file removed OutForDelivery/res/textures/logo.png
Binary file not shown.
File renamed without changes
Binary file added 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.
29 changes: 25 additions & 4 deletions OutForDelivery/src/Application.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include "Application.h"

#include "Ground.h"
#include "Store.h"
#include "Cannon.h"
#include "Package.h"

// settings
Expand Down Expand Up @@ -34,12 +36,15 @@ Application::~Application()

void Application::run()
{
bool canShoot = true;
Shader shaders("res\\shaders\\3.3.shader.vs", "res\\shaders\\3.3.shader.fs");

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

glm::vec3 camera = glm::vec3(-2.0f, 0.0f, -3.0f);
glm::vec3 camera = glm::vec3(0.0f, 0.0f, -3.0f);

while (_isRunning)
{
Expand All @@ -56,13 +61,29 @@ void Application::run()
shaders.setMat4("view", view);

ground.render(*_renderer);
store.render(*_renderer);
cannon.render(*_renderer);

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

}

_window->render(&shaders);
glfwPollEvents();
Expand Down
73 changes: 73 additions & 0 deletions OutForDelivery/src/Cannon.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#include "Cannon.h"

#include "stb_image/stb_image.h"

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

glm::mat4 model = glm::mat4(1.0f);
model = glm::translate(model, translation);
model = glm::rotate(model, glm::radians(angle), glm::vec3(0.0f, 0.0f, 1.0f));
m_shader.setMat4("model", model);

renderer.render(VAO, EBO);
}

void Cannon::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\\cannon.png", &width, &height, &nrChannels, 0);
if (data)
{
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D);
}
else
{
ERROR("Failed to load texture.");
}
stbi_image_free(data);
m_shader.setInt("texture", 0);
}

Cannon::Cannon(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);
}
34 changes: 34 additions & 0 deletions OutForDelivery/src/Cannon.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#pragma once
#include "engine/Renderer.h"

class Cannon
{
public:
Cannon(Shader& shader);
void render(Renderer& renderer);

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

float _vertices[20] = {
// positions // texture coords
0.3f, 0.15f, 0.0f, 1.0f, 1.0f, // top right
0.3f, -0.10f, 0.0f, 1.0f, 0.0f, // bottom right
-0.3f, -0.10f, 0.0f, 0.0f, 0.0f, // bottom left
-0.3f, 0.15f, 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(0.0f, -0.9f, 0.0f);

Shader m_shader;

};
4 changes: 2 additions & 2 deletions OutForDelivery/src/Ground.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ void Ground::initTextures()
int width, height, nrChannels;
stbi_set_flip_vertically_on_load(true);

unsigned char* data = stbi_load("res\\textures\\tex.png", &width, &height, &nrChannels, 0);
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);
Expand All @@ -44,7 +44,7 @@ void Ground::initTextures()
m_shader.setInt("texture", 0);
}

Ground::Ground(Shader shader)
Ground::Ground(Shader& shader)
: m_shader(shader)
{
glGenVertexArrays(1, &VAO);
Expand Down
4 changes: 2 additions & 2 deletions OutForDelivery/src/Ground.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
class Ground
{
public:
Ground(Shader shader);
Ground(Shader& shader);
void render(Renderer& renderer);

inline float getTop() { return (translation.y + height); }
Expand All @@ -15,7 +15,7 @@ class Ground
private:

const float height = 0.5f;
const float width = 10.0f;
const float width = 100.0f;


float _vertices[20] = {
Expand Down
18 changes: 11 additions & 7 deletions OutForDelivery/src/Package.cpp
Original file line number Diff line number Diff line change
@@ -1,40 +1,44 @@
#include "Package.h"
#include <cmath>

#include "stb_image/stb_image.h"

void Package::shoot(float power, float angle)
{
rotation = angle * 100;
xVelocity = power * cos(angle);
yVelocity = power * sin(angle);
fire = true;
}

void Package::render(Renderer& renderer, glm::vec3* camera)
void Package::render(Renderer& renderer, glm::vec3& camera)
{
m_shader.use();
glBindTexture(GL_TEXTURE_2D, texture);

glm::mat4 model = glm::mat4(1.0f);
model = glm::translate(model, translation);
if(fire)
model = glm::rotate(model, glm::radians(rotation), glm::vec3(0.0f, 0.0f, 1.0f));
m_shader.setMat4("model", model);

if (fire)
{
yVelocity -= 0.0001f; // resistance

translation.x += xVelocity;
translation.y += yVelocity;

rotation -= 1.0f ;
yVelocity -= 0.0001f; // resistance

if (translation.y <= -0.9f)
{
xVelocity = 0.0f;
yVelocity = 0.0f;
fire = false;
}
camera.x = -translation.x;
}
camera->x = -translation.x;


renderer.render(VAO, EBO);
}

Expand All @@ -53,7 +57,7 @@ void Package::initTextures()
int width, height, nrChannels;
stbi_set_flip_vertically_on_load(true);

unsigned char* data = stbi_load("res\\textures\\tex.jpg", &width, &height, &nrChannels, 0);
unsigned char* data = stbi_load("res\\textures\\package.jpg", &width, &height, &nrChannels, 0);
if (data)
{
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
Expand All @@ -67,7 +71,7 @@ void Package::initTextures()
m_shader.setInt("texture", 0);
}

Package::Package(Shader shader)
Package::Package(Shader& shader)
: m_shader(shader)
{
glGenVertexArrays(1, &VAO);
Expand Down
8 changes: 4 additions & 4 deletions OutForDelivery/src/Package.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
class Package
{
public:
Package(Shader shader);
void render(Renderer& renderer, glm::vec3*);
Package(Shader& shader);
void render(Renderer& renderer, glm::vec3&);
void shoot(float power, float angle);

private:
float yVelocity = 0.0f;
float xVelocity = 0.001f;
bool fire = false;

float rotation = 0.0f;

float _vertices[20] = {
// positions // texture coords
Expand All @@ -31,7 +31,7 @@ class Package
unsigned int texture = 0;
void initTextures();

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

Shader m_shader;

Expand Down
Loading

0 comments on commit 4c72598

Please sign in to comment.