Initial setup for Cube sample
This commit is contained in:
@@ -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
|
||||||
@@ -0,0 +1,67 @@
|
|||||||
|
/*
|
||||||
|
# ______ ____ ___
|
||||||
|
# | \/ ____| |___|
|
||||||
|
# | | | \ | |
|
||||||
|
#-----------------------------------------------------------------------
|
||||||
|
# Copyright 2020, tyra - https://github.com/h4570/tyra
|
||||||
|
# Licenced under Apache License 2.0
|
||||||
|
# Sandro Sobczyński <sandro.sobczynski@gmail.com>
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "camera.hpp"
|
||||||
|
|
||||||
|
#include <utils/debug.hpp>
|
||||||
|
#include <fastmath.h>
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
@@ -0,0 +1,41 @@
|
|||||||
|
/*
|
||||||
|
# ______ ____ ___
|
||||||
|
# | \/ ____| |___|
|
||||||
|
# | | | \ | |
|
||||||
|
#-----------------------------------------------------------------------
|
||||||
|
# Copyright 2020, tyra - https://github.com/h4570/tyra
|
||||||
|
# Licenced under Apache License 2.0
|
||||||
|
# Sandro Sobczyński <sandro.sobczynski@gmail.com>
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef _CAMERA_
|
||||||
|
#define _CAMERA_
|
||||||
|
|
||||||
|
#include <modules/pad.hpp>
|
||||||
|
#include <models/mesh.hpp>
|
||||||
|
#include <models/math/vector3.hpp>
|
||||||
|
#include <models/screen_settings.hpp>
|
||||||
|
#include <modules/camera_base.hpp>
|
||||||
|
#include <tamtypes.h>
|
||||||
|
|
||||||
|
/** 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
|
||||||
@@ -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);
|
||||||
|
}
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
#ifndef _CUBE_
|
||||||
|
#define _CUBE_
|
||||||
|
|
||||||
|
#include <tamtypes.h>
|
||||||
|
#include <game.hpp>
|
||||||
|
#include <engine.hpp>
|
||||||
|
#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
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
#include "cube.hpp"
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
Engine engine = Engine();
|
||||||
|
Cube game = Cube(&engine);
|
||||||
|
game.engine->init(&game, 128);
|
||||||
|
SleepThread();
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,148 @@
|
|||||||
|
/*
|
||||||
|
# ______ ____ ___
|
||||||
|
# | \/ ____| |___|
|
||||||
|
# | | | \ | |
|
||||||
|
#-----------------------------------------------------------------------
|
||||||
|
# Copyright 2020, tyra - https://github.com/h4570/tyra
|
||||||
|
# Licenced under Apache License 2.0
|
||||||
|
# Sandro Sobczyński <sandro.sobczynski@gmail.com>
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "light_manager.hpp"
|
||||||
|
#include <utils/debug.hpp>
|
||||||
|
|
||||||
|
// ----
|
||||||
|
// 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,38 @@
|
|||||||
|
/*
|
||||||
|
# ______ ____ ___
|
||||||
|
# | \/ ____| |___|
|
||||||
|
# | | | \ | |
|
||||||
|
#-----------------------------------------------------------------------
|
||||||
|
# Copyright 2020, tyra - https://github.com/h4570/tyra
|
||||||
|
# Licenced under Apache License 2.0
|
||||||
|
# Sandro Sobczyński <sandro.sobczynski@gmail.com>
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef _LIGHT_MANAGER_
|
||||||
|
#define _LIGHT_MANAGER_
|
||||||
|
|
||||||
|
#include <models/light_bulb.hpp>
|
||||||
|
#include <models/audio_listener.hpp>
|
||||||
|
|
||||||
|
/** 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
|
||||||
Reference in New Issue
Block a user