RayVsRect test works fine
Co-authored-by: sigonasr2 <sigonasr2@gmail.com>
This commit is contained in:
parent
be1efe8cee
commit
b28d48eff1
@ -1,6 +1,11 @@
|
||||
#Compiles the entire program then runs it, producing an executable. If the "test" argument is included, will try and run tests too (in the test folder)
|
||||
#C++
|
||||
printf "Running program...\n\n\n"
|
||||
output=$(dpkg -l | grep libx11-dev)
|
||||
if [[ -z $output ]]
|
||||
then
|
||||
sudo apt install libx11-dev libpulse-dev mesa-common-dev libpng-dev
|
||||
fi
|
||||
if [ "$1" = "test" ]
|
||||
then
|
||||
printf "Running tests...\n"
|
||||
|
@ -1,6 +1,11 @@
|
||||
#Compiles the entire program with debug flags then runs it in gdb. If the "test" argument is included, will try and run tests too (in the test folder)
|
||||
#C++
|
||||
printf "Running program...\n\n\n"
|
||||
output=$(dpkg -l | grep libx11-dev)
|
||||
if [[ -z $output ]]
|
||||
then
|
||||
sudo apt install libx11-dev libpulse-dev mesa-common-dev libpng-dev
|
||||
fi
|
||||
if [ "$1" = "test" ]
|
||||
then
|
||||
printf "Running tests...\n"
|
||||
|
@ -1,7 +1,7 @@
|
||||
build.sh:6fbec04ed16be42bab846f1eed603133 -
|
||||
build.sh:d5e4e98a6f677aa0a9759ba0afb2fb77 -
|
||||
commit.sh:d03a46e721060c22ccb146e19d27e70a -
|
||||
debug.sh:7f57c6640be5f262988961c3b45dce97 -
|
||||
debug.sh:849488515cab075948653c15eec4177b -
|
||||
lines.sh:3b907786f7fc9204025993016c9080de -
|
||||
release.sh:0a525311cc14b9c8aefc6f2b816129a1 -
|
||||
release.sh:6a96fb84ba64ed60d31be436ec069f05 -
|
||||
temp:d41d8cd98f00b204e9800998ecf8427e -
|
||||
web.sh:96f2c316536011a3defac50aecae487d -
|
||||
web.sh:b982212b88b80a59ad607b47d2ff3e94 -
|
||||
|
@ -1,6 +1,11 @@
|
||||
#Creates a release build that focuses on high runtime performance.
|
||||
#C++
|
||||
printf "Running program...\n\n\n"
|
||||
output=$(dpkg -l | grep libx11-dev)
|
||||
if [[ -z $output ]]
|
||||
then
|
||||
sudo apt install libx11-dev libpulse-dev mesa-common-dev libpng-dev
|
||||
fi
|
||||
if g++ $(find . -type f -name "*.cpp" -not -path "./test/*") ${CUSTOM_PARAMS} -O3 -s -DNDEBUG -o ${PROJECT_NAME}; then
|
||||
./${PROJECT_NAME} "$@"
|
||||
fi
|
||||
|
@ -1,5 +1,10 @@
|
||||
#Compiles emscripten instance of this project for the web.
|
||||
#C++
|
||||
output=$(dpkg -l | grep libx11-dev)
|
||||
if [[ -z $output ]]
|
||||
then
|
||||
sudo apt install libx11-dev libpulse-dev mesa-common-dev libpng-dev
|
||||
fi
|
||||
if [ -d "assets" ]; then
|
||||
em++ -std=c++17 -O2 -s ALLOW_MEMORY_GROWTH=1 -s MAX_WEBGL_VERSION=2 -s MIN_WEBGL_VERSION=2 -s USE_SDL_MIXER=2 -s USE_LIBPNG=1 $(find . -type f -name "*.cpp" -not -path "./test/*") -o ${PROJECT_NAME}.html -I pixelGameEngine.h --preload-file ./assets
|
||||
else
|
||||
|
Binary file not shown.
217
main.cpp
217
main.cpp
@ -1,188 +1,89 @@
|
||||
#define OLC_PGE_APPLICATION
|
||||
#include "pixelGameEngine.h"
|
||||
#include "olcutils.h"
|
||||
|
||||
// PGEX Require the presence of olc::PixelGameEngine
|
||||
#define OLC_PGEX_QUICKGUI
|
||||
#include "quickgui.h"
|
||||
using namespace olc;
|
||||
|
||||
namespace olc::QuickGUI{
|
||||
class PictureBox : public olc::QuickGUI::BaseControl{
|
||||
private:
|
||||
olc::vi2d clickPos;
|
||||
public:
|
||||
PictureBox(olc::QuickGUI::Manager& manager, // Associate with a Manager
|
||||
const olc::vf2d& pos,
|
||||
const olc::vf2d& size,
|
||||
const olc::vf2d& offset,
|
||||
olc::Renderable& image);
|
||||
|
||||
public:
|
||||
// Minium value
|
||||
olc::vf2d offset;
|
||||
olc::vf2d pos;
|
||||
olc::vf2d size;
|
||||
olc::Renderableℑ
|
||||
|
||||
public: // BaseControl overrides
|
||||
void Update(olc::PixelGameEngine* pge) override;
|
||||
void Draw(olc::PixelGameEngine* pge) override;
|
||||
void DrawDecal(olc::PixelGameEngine* pge) override;
|
||||
};
|
||||
|
||||
PictureBox::PictureBox(olc::QuickGUI::Manager& manager, const olc::vf2d& pos, const olc::vf2d& size, const olc::vf2d& offset,olc::Renderable& image)
|
||||
: BaseControl(manager),pos(pos),size(size),offset(offset),image(image){}
|
||||
|
||||
void PictureBox::Update(olc::PixelGameEngine* pge)
|
||||
{
|
||||
bPressed = false;
|
||||
bReleased = false;
|
||||
float fElapsedTime = pge->GetElapsedTime();
|
||||
|
||||
olc::vf2d vMouse = pge->GetMousePos();
|
||||
if (m_state != State::Click)
|
||||
{
|
||||
if (vMouse.x >= pos.x && vMouse.x < pos.x + size.x &&
|
||||
vMouse.y >= pos.y && vMouse.y < pos.y + size.y)
|
||||
{
|
||||
m_fTransition += fElapsedTime * m_manager.fHoverSpeedOn;
|
||||
m_state = State::Hover;
|
||||
|
||||
bPressed = pge->GetMouse(olc::Mouse::LEFT).bPressed;
|
||||
if (bPressed)
|
||||
{
|
||||
m_state = State::Click;
|
||||
clickPos = vMouse;
|
||||
}
|
||||
|
||||
bHeld = pge->GetMouse(olc::Mouse::LEFT).bHeld;
|
||||
}
|
||||
else
|
||||
{
|
||||
m_fTransition -= fElapsedTime * m_manager.fHoverSpeedOff;
|
||||
m_state = State::Normal;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
bHeld = pge->GetMouse(olc::Mouse::LEFT).bHeld;
|
||||
bReleased = pge->GetMouse(olc::Mouse::LEFT).bReleased;
|
||||
if (bReleased) m_state = State::Normal;
|
||||
offset+=clickPos-vMouse;
|
||||
offset.x=std::clamp((int)offset.x,0,image.Sprite()->width-(int)size.x);
|
||||
offset.y=std::clamp((int)offset.y,0,image.Sprite()->height-(int)size.y);
|
||||
clickPos=vMouse;
|
||||
}
|
||||
}
|
||||
|
||||
void PictureBox::Draw(olc::PixelGameEngine* pge)
|
||||
{
|
||||
if (!bVisible)
|
||||
return;
|
||||
switch (m_state)
|
||||
{
|
||||
case State::Disabled: {
|
||||
pge->FillRect(pos,size,m_manager.colDisable);
|
||||
}break;
|
||||
case State::Normal:
|
||||
case State::Hover: {
|
||||
pge->FillRect(pos,size,olc::PixelLerp(m_manager.colNormal, m_manager.colHover, m_fTransition));
|
||||
}break;
|
||||
case State::Click: {
|
||||
pge->FillRect(pos,size,m_manager.colClick);
|
||||
}break;
|
||||
}
|
||||
pge->DrawPartialSprite({(int)pos.x+1,(int)pos.y+1},image.Sprite(),offset,{(int)size.x-2,(int)size.y-2});
|
||||
pge->DrawRect(pos,size,m_manager.colBorder);
|
||||
}
|
||||
|
||||
void PictureBox::DrawDecal(olc::PixelGameEngine* pge)
|
||||
{
|
||||
if (!bVisible)
|
||||
return;
|
||||
|
||||
switch (m_state)
|
||||
{
|
||||
case State::Disabled: {
|
||||
pge->FillRectDecal(pos,size,olc::PixelLerp(m_manager.colNormal, m_manager.colHover, m_fTransition));
|
||||
}break;
|
||||
case State::Normal:
|
||||
case State::Hover: {
|
||||
pge->FillRectDecal(pos,size,olc::PixelLerp(m_manager.colNormal, m_manager.colHover, m_fTransition));
|
||||
}break;
|
||||
case State::Click: {
|
||||
pge->FillRectDecal(pos,size,m_manager.colClick);
|
||||
}break;
|
||||
}
|
||||
pge->DrawPartialDecal({pos.x+1,pos.y+1},image.Decal(),offset,{size.x-2,size.y-2});
|
||||
pge->SetDecalMode(olc::DecalMode::WIREFRAME);
|
||||
pge->DrawRectDecal(pos,size,m_manager.colBorder);
|
||||
pge->SetDecalMode(olc::DecalMode::NORMAL);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
class olcDemo_QuickGUI : public olc::PixelGameEngine
|
||||
class Example : public olc::PixelGameEngine
|
||||
{
|
||||
public:
|
||||
olcDemo_QuickGUI()
|
||||
Example()
|
||||
{
|
||||
sAppName = "olcDemo_QuickGUI";
|
||||
sAppName = "Example";
|
||||
}
|
||||
|
||||
protected:
|
||||
olc::QuickGUI::Manager guiManager;
|
||||
|
||||
olc::QuickGUI::PictureBox* pictureBox = nullptr;
|
||||
olc::QuickGUI::Slider* pictureBoxSliderX = nullptr;
|
||||
olc::QuickGUI::Slider* pictureBoxSliderY = nullptr;
|
||||
olc::Renderable dirtimg;
|
||||
|
||||
public:
|
||||
bool RayVsRect(const vf2d ray_origin, const vf2d ray_dir, const olc::utils::geom2d::rect<float> target, vf2d&contact_point, vf2d&contact_normal, float&t_hit_near){
|
||||
|
||||
contact_normal = { 0, 0 };
|
||||
contact_point = { 0, 0 };
|
||||
|
||||
vf2d t_near = {(target.pos.x - ray_origin.x) / ray_dir.x, (target.pos.y - ray_origin.y) / ray_dir.y};
|
||||
vf2d t_far = {(target.pos.x + target.size.x - ray_origin.x) / ray_dir.x, (target.pos.y + target.size.y - ray_origin.y) / ray_dir.y};
|
||||
|
||||
if (t_near.x > t_far.x) {float b; b = t_near.x; t_near.x = t_far.x; t_far.x = b;};
|
||||
if (t_near.y > t_far.y) {float b; b = t_near.y; t_near.y = t_far.y; t_far.y = b;};
|
||||
|
||||
if (t_near.x > t_far.y || t_near.y > t_far.x) return false;
|
||||
|
||||
t_hit_near = fmax(t_near.x, t_near.y);
|
||||
float t_hit_far = fmin(t_far.x, t_far.y);
|
||||
|
||||
if (t_hit_far < 0) return false;
|
||||
|
||||
contact_point.x = ray_origin.x + t_hit_near * ray_dir.x;
|
||||
contact_point.y = ray_origin.y + t_hit_near * ray_dir.y;
|
||||
|
||||
if (t_near.x > t_near.y)
|
||||
if ( 1.0f / ray_dir.x < 0)
|
||||
contact_normal = { 1, 0 };
|
||||
else
|
||||
contact_normal = { -1, 0};
|
||||
else
|
||||
if ( t_near.x < t_near.y)
|
||||
if ( 1.0f / ray_dir.y < 0)
|
||||
contact_normal = { 0, 1 };
|
||||
else
|
||||
contact_normal = { 0, -1 };
|
||||
|
||||
return true;
|
||||
|
||||
|
||||
}
|
||||
vf2d originPoint={16,16};
|
||||
bool OnUserCreate() override
|
||||
{
|
||||
dirtimg.Load("dirtblock.png");
|
||||
|
||||
pictureBox = new olc::QuickGUI::PictureBox(guiManager,
|
||||
{ 10.0f, 10.0f }, { 128.0f, 128.0f }, {0,0}, dirtimg);
|
||||
pictureBoxSliderX = new olc::QuickGUI::Slider(guiManager,{pictureBox->pos.x,pictureBox->pos.y+pictureBox->size.y+16},{pictureBox->pos.x+pictureBox->size.x,pictureBox->pos.y+pictureBox->size.y+16},
|
||||
0,pictureBox->image.Sprite()->width-(int)pictureBox->size.x,pictureBox->offset.x);
|
||||
pictureBoxSliderY = new olc::QuickGUI::Slider(guiManager,{pictureBox->pos.x+pictureBox->size.x+16,pictureBox->pos.y},{pictureBox->pos.x+pictureBox->size.x+16,pictureBox->pos.y+pictureBox->size.y},
|
||||
0,pictureBox->image.Sprite()->height-(int)pictureBox->size.y,pictureBox->offset.y);
|
||||
|
||||
guiManager.AddControl(pictureBox);
|
||||
guiManager.AddControl(pictureBoxSliderX);
|
||||
// Called once at the start, so create things here
|
||||
return true;
|
||||
}
|
||||
|
||||
bool OnUserUpdate(float fElapsedTime) override
|
||||
{
|
||||
guiManager.Update(this);
|
||||
vf2d velocity={(GetKey(D).bHeld-GetKey(A).bHeld)*20*fElapsedTime,(GetKey(S).bHeld-GetKey(W).bHeld)*20*fElapsedTime};
|
||||
vf2d contact_point;
|
||||
vf2d contact_normal;
|
||||
float t_hit_near;
|
||||
|
||||
if (pictureBoxSliderX->bHeld) {
|
||||
pictureBox->offset.x=pictureBoxSliderX->fValue;
|
||||
Clear(Pixel(64,64,255));
|
||||
if (!olc::utils::geom2d::overlaps(olc::utils::geom2d::circle<float>{originPoint+velocity,5},olc::utils::geom2d::rect<float>{{32,32},{64,32}})) {
|
||||
originPoint+=velocity;
|
||||
DrawCircle(originPoint,5);
|
||||
} else {
|
||||
pictureBoxSliderX->fValue=pictureBox->offset.x;
|
||||
DrawCircle(originPoint,5,RED);
|
||||
}
|
||||
DrawLine(originPoint,GetMousePos());
|
||||
|
||||
if (pictureBoxSliderY->bHeld) {
|
||||
pictureBox->offset.y=pictureBoxSliderY->fValue;
|
||||
} else {
|
||||
pictureBoxSliderY->fValue=pictureBox->offset.y;
|
||||
}
|
||||
|
||||
// Draw Stuff!
|
||||
Clear(olc::BLACK);
|
||||
|
||||
guiManager.DrawDecal(this);
|
||||
DrawRect({32,32},{64,32},RayVsRect(originPoint, GetMousePos()-originPoint, olc::utils::geom2d::rect<float>{{32,32},{64,32}},contact_point,contact_normal,t_hit_near)&&t_hit_near<1?YELLOW:WHITE);
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
int main()
|
||||
{
|
||||
olcDemo_QuickGUI demo;
|
||||
if (demo.Construct(256, 240, 4, 4))
|
||||
Example demo;
|
||||
if (demo.Construct(128, 120, 8, 8))
|
||||
demo.Start();
|
||||
|
||||
return 0;
|
||||
}
|
1024
olcutils.h
Normal file
1024
olcutils.h
Normal file
File diff suppressed because it is too large
Load Diff
Loading…
x
Reference in New Issue
Block a user