From 11717f42ebe74ab88b118b52f0df50f0a86e9d0e Mon Sep 17 00:00:00 2001 From: Wellinator Date: Fri, 2 Apr 2021 09:01:14 -0300 Subject: [PATCH] Initial setup for Cube sample --- src/samples/cube/Makefile | 33 +++++ src/samples/cube/camera.cpp | 67 +++++++++ src/samples/cube/camera.hpp | 41 ++++++ src/samples/cube/cube.cpp | 32 +++++ src/samples/cube/cube.hpp | 28 ++++ src/samples/cube/main.cpp | 10 ++ src/samples/cube/managers/light_manager.cpp | 148 ++++++++++++++++++++ src/samples/cube/managers/light_manager.hpp | 38 +++++ 8 files changed, 397 insertions(+) create mode 100644 src/samples/cube/Makefile create mode 100644 src/samples/cube/camera.cpp create mode 100644 src/samples/cube/camera.hpp create mode 100644 src/samples/cube/cube.cpp create mode 100644 src/samples/cube/cube.hpp create mode 100644 src/samples/cube/main.cpp create mode 100644 src/samples/cube/managers/light_manager.cpp create mode 100644 src/samples/cube/managers/light_manager.hpp diff --git a/src/samples/cube/Makefile b/src/samples/cube/Makefile new file mode 100644 index 0000000..7d1b17f --- /dev/null +++ b/src/samples/cube/Makefile @@ -0,0 +1,33 @@ +EE_BIN = cube.elf + +TYRA_DIR = ./../../engine + +EE_OBJS = \ + managers/light_manager.o \ + camera.o \ + cube.o \ + main.o + +EE_LIBS = -ltyra + +all: $(EE_BIN) + $(EE_STRIP) --strip-all $(EE_BIN) + mv $(EE_BIN) bin/$(EE_BIN) + rm $(EE_OBJS) + +rebuild-engine: + cd $(TYRA_DIR) && make clean && make + +clean: + rm -f $(EE_OBJS) + +run: $(EE_BIN) + killall -v ps2client || true + ps2client reset + ps2client reset + $(EE_STRIP) --strip-all $(EE_BIN) + mv $(EE_BIN) bin/$(EE_BIN) + rm $(EE_OBJS) + cd bin/ && ps2client execee host:$(EE_BIN) + +include $(TYRA_DIR)/Makefile.pref diff --git a/src/samples/cube/camera.cpp b/src/samples/cube/camera.cpp new file mode 100644 index 0000000..e797c0f --- /dev/null +++ b/src/samples/cube/camera.cpp @@ -0,0 +1,67 @@ +/* +# ______ ____ ___ +# | \/ ____| |___| +# | | | \ | | +#----------------------------------------------------------------------- +# Copyright 2020, tyra - https://github.com/h4570/tyra +# Licenced under Apache License 2.0 +# Sandro Sobczyński +*/ + +#include "camera.hpp" + +#include +#include + +const float CAMERA_Y = 30.0F; + +// ---- +// Constructors/Destructors +// ---- + +Camera::Camera(ScreenSettings *t_screen) : CameraBase(t_screen, &position) +{ + PRINT_LOG("Initializing camera"); + verticalLevel = 80.0F; + PRINT_LOG("Camera initialized!"); +} + +Camera::~Camera() {} + +// ---- +// Methods +// ---- + +void Camera::update(Pad &t_pad, Mesh &t_mesh) +{ + rotate(t_pad); + followBy(t_mesh); + lookAt(t_mesh.position); +} + +/** Set camera rotation by pad right joy and update unit circle + * https://en.wikipedia.org/wiki/Unit_circle + * https://www.desmos.com/calculator/3e7iypw4ow + */ +void Camera::rotate(Pad &t_pad) +{ + if (t_pad.rJoyH <= 100) + horizontalLevel -= 0.08; + else if (t_pad.rJoyH >= 200) + horizontalLevel += 0.08; + if (t_pad.rJoyV <= 50 && verticalLevel > 25.0F) + verticalLevel -= 0.9F; + else if (t_pad.rJoyV >= 200 && verticalLevel < 80.0F) + verticalLevel += 0.9F; + unitCirclePosition.x = (sin(horizontalLevel) * verticalLevel); + unitCirclePosition.y = CAMERA_Y; + unitCirclePosition.z = (cos(horizontalLevel) * verticalLevel); +} + +/** Rotate camera around 3D object */ +void Camera::followBy(Mesh &t_mesh) +{ + position.x = unitCirclePosition.x + t_mesh.position.x; + position.y = unitCirclePosition.y + t_mesh.position.y; + position.z = unitCirclePosition.z + t_mesh.position.z; +} diff --git a/src/samples/cube/camera.hpp b/src/samples/cube/camera.hpp new file mode 100644 index 0000000..49735d2 --- /dev/null +++ b/src/samples/cube/camera.hpp @@ -0,0 +1,41 @@ +/* +# ______ ____ ___ +# | \/ ____| |___| +# | | | \ | | +#----------------------------------------------------------------------- +# Copyright 2020, tyra - https://github.com/h4570/tyra +# Licenced under Apache License 2.0 +# Sandro Sobczyński +*/ + +#ifndef _CAMERA_ +#define _CAMERA_ + +#include +#include +#include +#include +#include +#include + +/** 3D camera which follow by 3D object. Can be rotated via pad */ +class Camera : public CameraBase +{ + +public: + Vector3 up, position, unitCirclePosition; + float horizontalLevel, verticalLevel; + + Camera(ScreenSettings *t_screen); + ~Camera(); + + void update(Pad &t_pad, Mesh &t_mesh); + void rotate(Pad &t_pad); + void followBy(Mesh &t_mesh); + +protected: + Vector3 *getPosition() { return &position; }; + ScreenSettings screen; +}; + +#endif diff --git a/src/samples/cube/cube.cpp b/src/samples/cube/cube.cpp new file mode 100644 index 0000000..c0b4399 --- /dev/null +++ b/src/samples/cube/cube.cpp @@ -0,0 +1,32 @@ +#include "cube.hpp" + +Cube::Cube(Engine *t_engine) + : engine(t_engine), camera(&t_engine->screen) +{ + return; +} + +Cube::~Cube() +{ + return; +} + +void Cube::onInit(){ + setBgColorAndAmbientColor(); + engine->renderer->setCameraDefinitions(&camera.view, &camera.unitCirclePosition, camera.planes); +} + +void Cube::onUpdate(){ + lightManager.update(); +} + +void Cube::setBgColorAndAmbientColor() +{ + color_t bgColor; + bgColor.r = 0x20; + bgColor.g = 0x20; + bgColor.b = 0x20; + engine->renderer->setWorldColor(bgColor); + Vector3 ambient = Vector3(0.003F, 0.003F, 0.003F); + engine->renderer->setAmbientLight(ambient); +} \ No newline at end of file diff --git a/src/samples/cube/cube.hpp b/src/samples/cube/cube.hpp new file mode 100644 index 0000000..b46c0bc --- /dev/null +++ b/src/samples/cube/cube.hpp @@ -0,0 +1,28 @@ +#ifndef _CUBE_ +#define _CUBE_ + +#include +#include +#include +#include "managers/light_manager.hpp" +#include "./camera.hpp" + +class Cube : public Game +{ + +public: + Cube(Engine *t_engine); + ~Cube(); + + void onInit(); + void onUpdate(); + + Engine *engine; + +private: + void setBgColorAndAmbientColor(); + LightManager lightManager; + Camera camera; +}; + +#endif \ No newline at end of file diff --git a/src/samples/cube/main.cpp b/src/samples/cube/main.cpp new file mode 100644 index 0000000..1b62314 --- /dev/null +++ b/src/samples/cube/main.cpp @@ -0,0 +1,10 @@ +#include "cube.hpp" + +int main() +{ + Engine engine = Engine(); + Cube game = Cube(&engine); + game.engine->init(&game, 128); + SleepThread(); + return 0; +} \ No newline at end of file diff --git a/src/samples/cube/managers/light_manager.cpp b/src/samples/cube/managers/light_manager.cpp new file mode 100644 index 0000000..a87f322 --- /dev/null +++ b/src/samples/cube/managers/light_manager.cpp @@ -0,0 +1,148 @@ +/* +# ______ ____ ___ +# | \/ ____| |___| +# | | | \ | | +#----------------------------------------------------------------------- +# Copyright 2020, tyra - https://github.com/h4570/tyra +# Licenced under Apache License 2.0 +# Sandro Sobczyński +*/ + +#include "light_manager.hpp" +#include + +// ---- +// Constructors/Destructors +// ---- + +LightManager::LightManager() +{ + PRINT_LOG("Creating light manager!"); + bulbsCount = 2; + + bulbs = new LightBulb[2]; + bulb1Dir = 0; + bulbs[0].intensity = 1; + bulbs[0].position.set(30.00F, 30.00F, -30.00F); + + bulb2Dir = 1; + bulbs[1].intensity = 1; + bulbs[1].position.set(-30.00F, 30.00F, 30.00F); + + audioOffset = audioTick = 0; + + PRINT_LOG("Light manager created!"); +} + +LightManager::~LightManager() +{ + delete[] bulbs; +} + +// ---- +// Methods +// ---- + +/** Light bulb moving test */ +void LightManager::update() +{ + updateBulb1(); + updateBulb2(); +} + +void LightManager::updateBulb1() +{ + if (bulb1Dir == 1) + { + if (areLightsInverted) + { + bulbs[0].position.x += 0.6F; + bulbs[0].position.z -= 0.6F; + } + else + { + bulbs[0].position.x += 0.6F; + bulbs[0].position.z += 0.6F; + } + } + else + { + if (areLightsInverted) + { + bulbs[0].position.x -= 0.6F; + bulbs[0].position.z += 0.6F; + } + else + { + bulbs[0].position.x -= 0.6F; + bulbs[0].position.z -= 0.6F; + } + } +} + +void LightManager::updateBulb2() +{ + if (bulb2Dir == 1) + { + if (areLightsInverted) + { + bulbs[1].position.x -= 0.6F; + bulbs[1].position.z += 0.6F; + } + else + { + bulbs[1].position.x += 0.6F; + bulbs[1].position.z += 0.6F; + } + } + else + { + if (areLightsInverted) + { + bulbs[1].position.x += 0.6F; + bulbs[1].position.z -= 0.6F; + } + else + { + bulbs[1].position.x -= 0.6F; + bulbs[1].position.z -= 0.6F; + } + } +} + +/** Called by audio thread */ +void LightManager::onAudioTick() +{ + bulb1Dir = bulb1Dir == 1 ? 0 : 1; + bulb2Dir = bulb2Dir == 1 ? 0 : 1; + if (audioTick++ > 17) + { + if (++audioOffset > 3) + { + audioOffset = 0; + if (areLightsInverted) + { + areLightsInverted = false; + bulbs[0].position.x = 20.0F; + bulbs[0].position.z = -20.0F; + + bulbs[1].position.x = -20.0F; + bulbs[1].position.z = 20.0F; + } + else + { + areLightsInverted = true; + bulbs[0].position.x = 20.0F; + bulbs[0].position.z = 20.0F; + + bulbs[1].position.x = -20.0F; + bulbs[1].position.z = -20.0F; + } + } + } + else + { + bulbs[0].intensity += 9; + bulbs[1].intensity += 9; + } +} diff --git a/src/samples/cube/managers/light_manager.hpp b/src/samples/cube/managers/light_manager.hpp new file mode 100644 index 0000000..dbc3bf8 --- /dev/null +++ b/src/samples/cube/managers/light_manager.hpp @@ -0,0 +1,38 @@ +/* +# ______ ____ ___ +# | \/ ____| |___| +# | | | \ | | +#----------------------------------------------------------------------- +# Copyright 2020, tyra - https://github.com/h4570/tyra +# Licenced under Apache License 2.0 +# Sandro Sobczyński +*/ + +#ifndef _LIGHT_MANAGER_ +#define _LIGHT_MANAGER_ + +#include +#include + +/** Class which is maintaining floors objects */ +class LightManager +{ + +public: + LightManager(); + ~LightManager(); + + LightBulb *bulbs; + u8 bulbsCount; + void update(); + void onAudioTick(); + +private: + u8 bulb1Dir, bulb2Dir; + void updateBulb1(); + void updateBulb2(); + u8 audioOffset, areLightsInverted; + u32 audioTick; +}; + +#endif