generated from sigonasr2/CPlusPlusProjectTemplate
Finally, test input matches up
Co-authored-by: sigonasr2 <sigonasr2@gmail.com>
This commit is contained in:
parent
771136df8e
commit
fe8044562a
Binary file not shown.
214
main.cpp
214
main.cpp
@ -3,87 +3,151 @@
|
|||||||
#include "olcutils.h"
|
#include "olcutils.h"
|
||||||
|
|
||||||
using namespace olc;
|
using namespace olc;
|
||||||
|
std::ifstream file("testinput");
|
||||||
|
std::map<std::string,int> filedata;
|
||||||
|
std::string pwd;
|
||||||
|
std::string tempstr;
|
||||||
|
|
||||||
class Example : public olc::PixelGameEngine
|
class File{
|
||||||
{
|
public:
|
||||||
public:
|
std::map<std::string,File> files;
|
||||||
Example()
|
int size;
|
||||||
{
|
|
||||||
sAppName = "Example";
|
|
||||||
}
|
|
||||||
|
|
||||||
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
|
|
||||||
{
|
|
||||||
// Called once at the start, so create things here
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool OnUserUpdate(float fElapsedTime) override
|
|
||||||
{
|
|
||||||
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;
|
|
||||||
|
|
||||||
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 {
|
|
||||||
DrawCircle(originPoint,5,RED);
|
|
||||||
}
|
|
||||||
DrawLine(originPoint,GetMousePos());
|
|
||||||
|
|
||||||
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 DisplayContents(File&f,std::string path,int filter,int*filtersum){
|
||||||
|
std::cout<<" Contents of "<<path<<"(Size "<<f.size<<"): "<<std::endl;
|
||||||
|
int sum=0;
|
||||||
|
for (std::map<std::string,File>::iterator it=f.files.begin();it!=f.files.end();it++){
|
||||||
|
int amt=DisplayContents(it->second,(path=="/")?path+it->first:path+"/"+it->first,filter,filtersum);
|
||||||
|
sum+=amt;
|
||||||
|
}
|
||||||
|
if (sum<=filter){
|
||||||
|
*filtersum+=sum;
|
||||||
|
}
|
||||||
|
std::cout<<" Size of "<<path<<": "<<sum<<std::endl;
|
||||||
|
return sum+f.size;
|
||||||
|
}
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
Example demo;
|
File rootFile{{},0};
|
||||||
if (demo.Construct(128, 120, 8, 8))
|
while (file.good()||tempstr.length()>0){
|
||||||
demo.Start();
|
std::string line;
|
||||||
|
if (tempstr.length()!=0){
|
||||||
|
line=tempstr;
|
||||||
|
tempstr="";
|
||||||
|
} else {
|
||||||
|
std::getline(file,line);
|
||||||
|
}
|
||||||
|
//std::cout<<line<<std::endl;
|
||||||
|
if (line[0]=='$'){
|
||||||
|
//Command.
|
||||||
|
std::string command=line.substr(line.find_first_of(' ')+1,2);
|
||||||
|
std::string arg=line.substr(line.find_last_of(' ')+1,std::string::npos);
|
||||||
|
std::cout<<"Command "<<command<<" w/arg "<<arg<<std::endl;
|
||||||
|
if (command=="cd"){
|
||||||
|
if (arg==".."){
|
||||||
|
pwd=pwd.substr(0,pwd.find_last_of('/'));
|
||||||
|
} else
|
||||||
|
if (arg=="/"){
|
||||||
|
pwd="/";
|
||||||
|
} else {
|
||||||
|
if (pwd=="/"){
|
||||||
|
pwd+=arg;
|
||||||
|
} else {
|
||||||
|
pwd+="/"+arg;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
std::cout<<"New directory is "<<pwd<<std::endl;
|
||||||
|
} else {
|
||||||
|
//It's ls.
|
||||||
|
while (file.good()) {
|
||||||
|
std::getline(file,line);
|
||||||
|
if (line[0]=='$'){
|
||||||
|
tempstr=line;
|
||||||
|
break; //It's now a command again.
|
||||||
|
}
|
||||||
|
std::string data1=line.substr(0,line.find_first_of(' '));
|
||||||
|
std::string filename=line.substr(line.find_first_of(' ')+1,std::string::npos);
|
||||||
|
std::cout<<"ls part 1: "<<data1<<"// Filename:"<<filename<<std::endl;
|
||||||
|
if (data1=="dir"){
|
||||||
|
std::string navigator;
|
||||||
|
if (pwd=="/") {
|
||||||
|
navigator=pwd+filename;
|
||||||
|
} else {
|
||||||
|
navigator=pwd+"/"+filename;
|
||||||
|
}
|
||||||
|
std::cout<<" Navigator starts at "<<navigator<<std::endl;
|
||||||
|
File*filer=&rootFile;
|
||||||
|
while(navigator!=filename){
|
||||||
|
std::string dir=navigator.substr(0,navigator.find_first_of('/'));
|
||||||
|
if (dir.length()==0){
|
||||||
|
navigator=navigator.substr(navigator.find_first_of('/')+1,std::string::npos);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (filer->files.find(dir)==filer->files.end()){
|
||||||
|
//Does not exist. Create.
|
||||||
|
filer->files[dir]=File();
|
||||||
|
std::cout<<"Created "<<dir<<". Size: "<<filer->files.size()<<std::endl;
|
||||||
|
filer=&filer->files[dir];
|
||||||
|
}else{
|
||||||
|
filer=&filer->files[dir];
|
||||||
|
}
|
||||||
|
navigator=navigator.substr(navigator.find_first_of('/')+1,std::string::npos);
|
||||||
|
std::cout<<" Navigator is now at "<<navigator<<std::endl;
|
||||||
|
}
|
||||||
|
//Create this directory too.
|
||||||
|
if (filer->files.find(navigator)==filer->files.end()){
|
||||||
|
//Does not exist. Create.
|
||||||
|
filer->files[navigator]=File();
|
||||||
|
std::cout<<"Created "<<navigator<<". Size: "<<filer->files.size()<<std::endl;
|
||||||
|
filer=&filer->files[navigator];
|
||||||
|
}else{
|
||||||
|
filer=&filer->files[navigator];
|
||||||
|
}
|
||||||
|
} else
|
||||||
|
if (data1.length()>0&&data1!="dir"){
|
||||||
|
int size=std::atoi(line.substr(0,line.find_first_of(' ')).c_str());
|
||||||
|
std::string filename=line.substr(line.find_first_of(' ')+1,std::string::npos);
|
||||||
|
File*filer=&rootFile;
|
||||||
|
std::string navigator;
|
||||||
|
if (pwd=="/") {
|
||||||
|
navigator=pwd+filename;
|
||||||
|
} else {
|
||||||
|
navigator=pwd+"/"+filename;
|
||||||
|
}
|
||||||
|
std::cout<<" Navigator starts at "<<navigator<<std::endl;
|
||||||
|
while(navigator!=filename){
|
||||||
|
std::string dir=navigator.substr(0,navigator.find_first_of('/'));
|
||||||
|
if (dir.length()==0){
|
||||||
|
navigator=navigator.substr(navigator.find_first_of('/')+1,std::string::npos);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (filer->files.find(dir)==filer->files.end()){
|
||||||
|
//Does not exist. Create.
|
||||||
|
filer->files[dir]=File();
|
||||||
|
std::cout<<"Created "<<dir<<". Size: "<<filer->files.size()<<std::endl;
|
||||||
|
filer=&filer->files[dir];
|
||||||
|
}else{
|
||||||
|
filer=&filer->files[dir];
|
||||||
|
}
|
||||||
|
navigator=navigator.substr(navigator.find_first_of('/')+1,std::string::npos);
|
||||||
|
std::cout<<" Navigator is now at "<<navigator<<std::endl;
|
||||||
|
}
|
||||||
|
//This file gets its size set.
|
||||||
|
filer->files[navigator]=File();
|
||||||
|
std::cout<<"Created "<<navigator<<". Size: "<<filer->files.size()<<std::endl;
|
||||||
|
filer->files[navigator].size=size;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
std::cout<<"end ls"<<std::endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
std::cout<<line<<std::endl;
|
||||||
|
}
|
||||||
|
int sum=0;
|
||||||
|
DisplayContents(rootFile,"/",100000,&sum);
|
||||||
|
std::cout<<"Sum: "<<sum<<std::endl;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user