Update Template to use a separate compile unit for pixelGameEngine

Co-authored-by: sigonasr2 <sigonasr2@gmail.com>
master
sigonasr2 2 years ago
parent b28d48eff1
commit a002c81bce
  1. 18
      C++/scripts/build.sh
  2. 6
      C++/scripts/md5
  3. 12
      C++/scripts/release.sh
  4. 19
      C++/scripts/web.sh
  5. BIN
      C++ProjectTemplate
  6. 12
      Polygon.cpp
  7. 10
      Polygon.h
  8. 81
      main.cpp
  9. 2
      pixelGameEngine.cpp
  10. BIN
      pixelGameEngine.o

@ -1,4 +1,4 @@
#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) #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). If the "full" argument is included, it will recopmile the PixelGameEngine (should it change)
#C++ #C++
printf "Running program...\n\n\n" printf "Running program...\n\n\n"
output=$(dpkg -l | grep libx11-dev) output=$(dpkg -l | grep libx11-dev)
@ -6,16 +6,28 @@ if [[ -z $output ]]
then then
sudo apt install libx11-dev libpulse-dev mesa-common-dev libpng-dev sudo apt install libx11-dev libpulse-dev mesa-common-dev libpng-dev
fi fi
if [ ! -f "pixelGameEngine.o" ]
then
printf "Pixel Game Engine compile object missing. Compiling for the first time..."
g++ -c pixelGameEngine.cpp
fi
if [ "$1" = "test" ] if [ "$1" = "test" ]
then then
printf "Running tests...\n" printf "Running tests...\n"
echo "#define TEST_SUITE" > ./test/test.h echo "#define TEST_SUITE" > ./test/test.h
if g++ $(find . -type f -name "*.cpp") ${CUSTOM_PARAMS} -o ${PROJECT_NAME}; then if g++ $(find . -type f -name "*.cpp" -not -name "pixelGameEngine.cpp") pixelGameEngine.o ${CUSTOM_PARAMS} -o ${PROJECT_NAME}; then
./${PROJECT_NAME} "$@"
fi
elif [ "$1" = "full" ]
then
echo "" > ./test/test.h
g++ -c pixelGameEngine.cpp
if g++ $(find . -type f -name "*.cpp" -not -path "./test/*" -not -name "pixelGameEngine.cpp") pixelGameEngine.o ${CUSTOM_PARAMS} -o ${PROJECT_NAME}; then
./${PROJECT_NAME} "$@" ./${PROJECT_NAME} "$@"
fi fi
else else
echo "" > ./test/test.h echo "" > ./test/test.h
if g++ $(find . -type f -name "*.cpp" -not -path "./test/*") ${CUSTOM_PARAMS} -o ${PROJECT_NAME}; then if g++ $(find . -type f -name "*.cpp" -not -path "./test/*" -not -name "pixelGameEngine.cpp") pixelGameEngine.o ${CUSTOM_PARAMS} -o ${PROJECT_NAME}; then
./${PROJECT_NAME} "$@" ./${PROJECT_NAME} "$@"
fi fi
fi fi

@ -1,7 +1,7 @@
build.sh:d5e4e98a6f677aa0a9759ba0afb2fb77 - build.sh:f7da5b6f54c56658f5410e0df797b5a7 -
commit.sh:d03a46e721060c22ccb146e19d27e70a - commit.sh:d03a46e721060c22ccb146e19d27e70a -
debug.sh:849488515cab075948653c15eec4177b - debug.sh:849488515cab075948653c15eec4177b -
lines.sh:3b907786f7fc9204025993016c9080de - lines.sh:3b907786f7fc9204025993016c9080de -
release.sh:6a96fb84ba64ed60d31be436ec069f05 - release.sh:0ab321c3fa2f1a1b2f03b1aec3bce816 -
temp:d41d8cd98f00b204e9800998ecf8427e - temp:d41d8cd98f00b204e9800998ecf8427e -
web.sh:b982212b88b80a59ad607b47d2ff3e94 - web.sh:e317e1d492d00517a0ac0e460239daa0 -

@ -1,4 +1,4 @@
#Creates a release build that focuses on high runtime performance. #Creates a release build that focuses on high runtime performance. Use "full" argument to completely rebuild PGE.
#C++ #C++
printf "Running program...\n\n\n" printf "Running program...\n\n\n"
output=$(dpkg -l | grep libx11-dev) output=$(dpkg -l | grep libx11-dev)
@ -6,7 +6,15 @@ if [[ -z $output ]]
then then
sudo apt install libx11-dev libpulse-dev mesa-common-dev libpng-dev sudo apt install libx11-dev libpulse-dev mesa-common-dev libpng-dev
fi fi
if g++ $(find . -type f -name "*.cpp" -not -path "./test/*") ${CUSTOM_PARAMS} -O3 -s -DNDEBUG -o ${PROJECT_NAME}; then if [ "$1" == "full" ]; then
rm "pixelGameEngine.o"
fi
if [ ! -f "pixelGameEngine.o" ]
then
printf "Pixel Game Engine compile object missing. Compiling for the first time..."
g++ -c pixelGameEngine.cpp
fi
if g++ $(find . -type f -name "*.cpp" -not -path "./test/*" -not -name "pixelGameEngine.cpp") pixelGameEngine.o ${CUSTOM_PARAMS} -o ${PROJECT_NAME}; then
./${PROJECT_NAME} "$@" ./${PROJECT_NAME} "$@"
fi fi
printf "\n\n" printf "\n\n"

@ -1,23 +1,30 @@
#Compiles emscripten instance of this project for the web. #Compiles emscripten instance of this project for the web. Use "full" argument if your PGE has to be completely rebuilt. Use "headless" argument for a headless version.
#C++ #C++
output=$(dpkg -l | grep libx11-dev) output=$(dpkg -l | grep libx11-dev)
if [[ -z $output ]] if [[ -z $output ]]
then then
sudo apt install libx11-dev libpulse-dev mesa-common-dev libpng-dev sudo apt install libx11-dev libpulse-dev mesa-common-dev libpng-dev
fi fi
if [[ "$1" == "full" || "$2" == "full" ]]; then
rm "pixelGameEngine_wasm.o"
fi
if [ ! -f "pixelGameEngine_wasm.o" ]
then
printf "Pixel Game Engine compile object missing. Compiling for the first time..."
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 -c pixelGameEngine.cpp -o pixelGameEngine_wasm.o
fi
if [ -d "assets" ]; then 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 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/*" -not -name "pixelGameEngine.cpp") pixelGameEngine_wasm.o -o ${PROJECT_NAME}.html --preload-file ./assets
else else
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 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/*" -not -name "pixelGameEngine.cpp") pixelGameEngine_wasm.o -o ${PROJECT_NAME}.html
fi fi
cp buildtemplate.html ${PROJECT_NAME}.html cp buildtemplate.html ${PROJECT_NAME}.html
sed -i "s/_REPLACEME_/$PROJECT_NAME.js/" ${PROJECT_NAME}.html sed -i "s/_REPLACEME_/$PROJECT_NAME.js/" ${PROJECT_NAME}.html
if [ "$1" == "headless" ]; then if [[ "$1" == "headless" || "$2" == "headless" ]]; then
echo "Running as headless web server" echo "Running as headless web server"
emrun --no_browser ${PROJECT_NAME}.html emrun --no_browser ${PROJECT_NAME}.html
else else
emrun --serve_after_close ${PROJECT_NAME}.html emrun --serve_after_close ${PROJECT_NAME}.html
fi fi

Binary file not shown.

@ -0,0 +1,12 @@
#include "Polygon.h"
Polygon::Polygon(std::initializer_list<vf2d>ls){
points.resize(ls.size());
std::copy(ls.begin(),ls.end(),points.begin());
}
void Polygon::DrawPolygon(){
for (int i=0;i<points.size();i++){
}
}

@ -0,0 +1,10 @@
#include "pixelGameEngine.h"
using namespace olc;
class Polygon{
public:
std::vector<vf2d>points;
Polygon(std::initializer_list<vf2d>ls);
void DrawPolygon();
};

@ -1,11 +1,15 @@
#define OLC_PGE_APPLICATION
#include "pixelGameEngine.h" #include "pixelGameEngine.h"
#include "olcutils.h" #include "Polygon.h"
using namespace olc; using namespace olc;
#define WIDTH 640
#define HEIGHT 480
class Example : public olc::PixelGameEngine class Example : public olc::PixelGameEngine
{ {
public: public:
Example() Example()
{ {
@ -13,76 +17,39 @@ public:
} }
public: 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 bool OnUserCreate() override
{ {
// Called once at the start, so create things here Polygon poly{{30,30},{20,20},{-7,6}};
return true; return true;
} }
bool OnUserUpdate(float fElapsedTime) override bool OnUserUpdate(float fElapsedTime) override
{ {
vf2d velocity={(GetKey(D).bHeld-GetKey(A).bHeld)*20*fElapsedTime,(GetKey(S).bHeld-GetKey(W).bHeld)*20*fElapsedTime}; Clear(BLACK);
vf2d contact_point; return true;
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); bool OnUserDestroy()override{
return true; return true;
} }
}; };
enum Direction{
RIGHT,
DOWN,
LEFT,
UP
};
struct Data{
int x,y;
};
int main() int main()
{ {
Example demo; Example demo;
if (demo.Construct(128, 120, 8, 8)) if (demo.Construct(640, 480, 4, 4))
demo.Start(); demo.Start();
return 0; return 0;

@ -0,0 +1,2 @@
#define OLC_PGE_APPLICATION
#include "pixelGameEngine.h"

Binary file not shown.
Loading…
Cancel
Save