Rays
This commit is contained in:
72
C++/Pixel-Engine/Rays/CMakeLists.txt
Normal file
72
C++/Pixel-Engine/Rays/CMakeLists.txt
Normal file
@@ -0,0 +1,72 @@
|
||||
# This CMakeList.txt currently only supports compilation of
|
||||
#a program on windows or linux, but only with the dependancies
|
||||
#for oclPixelGameEngine.h
|
||||
|
||||
# NOTE: THIS CMAKELIST WILL NOT INSTALL DEPENDANCIES, IT WILL JUST FIND THEM AND
|
||||
#COMPILE / LINK THEM RESPECTIVELY, YOU NEED TO INSTALL THEM YOURSELF
|
||||
|
||||
# Any issues, submit an issue, or contact the author, "Ben (plane000)#8618" on Discord
|
||||
|
||||
# Currently linked / compiled by default is:
|
||||
#Threads (pthread), OpenGL, GLX, libPNG, X11
|
||||
|
||||
cmake_minimum_required(VERSION 3.7)
|
||||
project(Rage) # You can change this with your project name!
|
||||
|
||||
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} CMakeModules/) # Don't worry what this does, it just gives more compatability options with libraries!
|
||||
cmake_policy(SET CMP0037 OLD) # This just allows the / charicter in path names
|
||||
|
||||
set(ExecutableName output) # You can change this to whatever you wish the executable outputed to be, don't worry about a .exe, i'll do that!
|
||||
set(SourceDir ./) # Change src/ to wherever your code is
|
||||
|
||||
|
||||
file(GLOB_RECURSE SourceFiles
|
||||
${SourceDir}/*.cpp
|
||||
)
|
||||
|
||||
set(THREADS_PREFER_PTHREAD_FLAD ON)
|
||||
find_package(Threads REQUIRED)
|
||||
find_package(OpenGL REQUIRED)
|
||||
|
||||
|
||||
if (UNIX)
|
||||
find_package(X11 REQUIRED)
|
||||
find_package(PNG REQUIRED)
|
||||
include_directories(
|
||||
${PNG_INCLUDE_DIR}
|
||||
)
|
||||
|
||||
# Set platform spesific output names
|
||||
set(Executable ${ExecutableName}.o)
|
||||
endif (UNIX)
|
||||
if (WIN32)
|
||||
target_include_directories(
|
||||
${WinSDK}
|
||||
)
|
||||
|
||||
# Set platform spesific output names
|
||||
set(Executable ${ExecutableName}.exe)
|
||||
endif (WIN32)
|
||||
|
||||
add_executable(${Executable}
|
||||
${SourceFiles}
|
||||
)
|
||||
|
||||
target_link_libraries(${Executable}
|
||||
Threads::Threads
|
||||
OpenGL::OpenGL
|
||||
OpenGL::GL
|
||||
OpenGL::GLX
|
||||
)
|
||||
|
||||
if (UNIX)
|
||||
target_link_libraries(${Executable}
|
||||
${X11_LIBRARIES}
|
||||
PNG::PNG
|
||||
)
|
||||
endif (UNIX)
|
||||
if (WIN32)
|
||||
target_link_libraries(${Executable}
|
||||
${WinSDK}
|
||||
)
|
||||
endif (WIN32)
|
||||
47
C++/Pixel-Engine/Rays/main.cpp
Normal file
47
C++/Pixel-Engine/Rays/main.cpp
Normal file
@@ -0,0 +1,47 @@
|
||||
#define OLC_PGE_APPLICATION
|
||||
#include "olcPixelGameEngine.h"
|
||||
#include "rect.h"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
Rect rect;
|
||||
bool isHoverd = false;
|
||||
|
||||
class RaysApp : public olc::PixelGameEngine {
|
||||
public:
|
||||
RaysApp() {
|
||||
sAppName = "Rays";
|
||||
}
|
||||
|
||||
bool OnUserCreate() override {
|
||||
rect.SetRect(100, 100, 200, 100);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool OnUserUpdate(float fElapsedTime) override {
|
||||
Clear(olc::BLACK);
|
||||
if (!isHoverd) {
|
||||
DrawRect(rect.x, rect.y, rect.w, rect.h, olc::RED);
|
||||
} else {
|
||||
FillRect(rect.x, rect.y, rect.w, rect.h, olc::RED);
|
||||
}
|
||||
|
||||
std::cout << "MouseX: " << GetMouseX() << " MouseY: " << GetMouseY() << std::endl;
|
||||
|
||||
if (rect.Contains(new Vec2<int>(GetMouseX(), GetMouseY()))) {
|
||||
isHoverd = true;
|
||||
} else {
|
||||
isHoverd = false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
RaysApp app;
|
||||
if (app.Construct(500, 300, 2, 2))
|
||||
app.Start();
|
||||
return 0;
|
||||
}
|
||||
223
C++/Pixel-Engine/Rays/math.h
Normal file
223
C++/Pixel-Engine/Rays/math.h
Normal file
@@ -0,0 +1,223 @@
|
||||
#ifndef _MATH_H_
|
||||
#define _MATH_H_
|
||||
|
||||
const float DEG2RAD = 0.01745329251994329576923690768f;
|
||||
const float RAD2DEG = 57.2957795130823208767981548141f;
|
||||
|
||||
inline float ToRadian(const float Degree) {
|
||||
return (Degree * DEG2RAD);
|
||||
}
|
||||
|
||||
inline float ToDegree(const float Radian) {
|
||||
return (Radian * RAD2DEG);
|
||||
}
|
||||
|
||||
template<class T>
|
||||
struct Vec4 {
|
||||
T x, y, z, w;
|
||||
template<class P>
|
||||
Vec4(P x, P y, P z, P w) : x(x), y(y), z(z), w(w) {}
|
||||
template<class P>
|
||||
Vec4(P all) : x(all), y(all), z(all), w(all) {}
|
||||
Vec4() : x(0), y(0), z(0), w(0) {}
|
||||
inline Vec4& dot(const Vec4<T>& v) {
|
||||
return (x * v.x + y * v.y + z * v.z + w * v.w);
|
||||
}
|
||||
inline const Vec4& operator+() {
|
||||
return *this;
|
||||
}
|
||||
inline Vec4& operator-() {
|
||||
return Vec4<T>(-x, -y, -z, -w);
|
||||
}
|
||||
inline Vec4& operator+(const Vec4<T>& v) {
|
||||
return new Vec4(x + v.x, y + v.y, z + v.z, w + v.w);
|
||||
}
|
||||
inline Vec4& operator-(const Vec4<T>& v) {
|
||||
return new Vec4(x - v.x, y - v.y, z - v.z, w - v.w);
|
||||
}
|
||||
inline Vec4& operator*(const Vec4<T>& v) {
|
||||
return new Vec4(x * v.x, y * v.y, z * v.z, w * v.w);
|
||||
}
|
||||
inline Vec4& operator/(const Vec4<T>& v) {
|
||||
return new Vec4(x / v.x, y / v.y, z / v.z, w / v.w);
|
||||
}
|
||||
inline Vec4& operator+=(const Vec4<T>& v) {
|
||||
x+=v.x; y+=v.y; z+=v.z; w+=v.w;
|
||||
return *this;
|
||||
}
|
||||
inline Vec4& operator-=(const Vec4<T>& v) {
|
||||
x-=v.x; y-=v.y; z-=v.z; w-=v.w;
|
||||
return *this;
|
||||
}
|
||||
inline Vec4& operator*=(const Vec4<T>& v) {
|
||||
x*=v.x; y*=v.y; z*=v.z; w*=v.w;
|
||||
return *this;
|
||||
}
|
||||
inline Vec4& operator/=(const Vec4<T>& v) {
|
||||
x/=v.x; y/=v.y; z/=v.z; w/=v.w;
|
||||
return *this;
|
||||
}
|
||||
template<class P>
|
||||
inline Vec4& operator+=(P s) {
|
||||
x+=s; y+=s; z+=s; w+=s;
|
||||
return *this;
|
||||
}
|
||||
template<class P>
|
||||
inline Vec4& operator-=(P s) {
|
||||
x-=s; y-=s; z-=s; w-=s;
|
||||
return *this;
|
||||
}
|
||||
template<class P>
|
||||
inline Vec4& operator*=(P s) {
|
||||
x*=s; y*=s; z*=s; w*=s;
|
||||
return *this;
|
||||
}
|
||||
template<class P>
|
||||
inline Vec4& operator/=(P s) {
|
||||
x/=s; y/=s; z/=s; w/=s;
|
||||
return *this;
|
||||
}
|
||||
};
|
||||
|
||||
template<class T>
|
||||
struct Vec3 {
|
||||
T x, y, z;
|
||||
template<class P>
|
||||
Vec3(P x, P y, P z) : x(x), y(y), z(z) {}
|
||||
template<class P>
|
||||
Vec3(P all) : x(all), y(all), z(all) {}
|
||||
Vec3() : x(0), y(0), z(0) {}
|
||||
inline Vec3& cross(const Vec3<T>& v) {
|
||||
return new Vec3(
|
||||
(y * v.z - z * v.y),
|
||||
(x * v.z - z * v.x),
|
||||
(x * v.y - y * v.x)
|
||||
);
|
||||
}
|
||||
inline Vec3& dot(const Vec3<T>& v) {
|
||||
return (x * v.x + y * v.y + z * v.z);
|
||||
}
|
||||
inline const Vec3& operator+() {
|
||||
return *this;
|
||||
}
|
||||
inline Vec3& operator-() {
|
||||
return Vec3<T>(-x, -y, -z);
|
||||
}
|
||||
inline Vec3& operator+(const Vec3<T>& v) {
|
||||
return new Vec3(x + v.x, y + v.y, z + v.z);
|
||||
}
|
||||
inline Vec3& operator-(const Vec3<T>& v) {
|
||||
return new Vec3(x - v.x, y - v.y, z - v.z);
|
||||
}
|
||||
inline Vec3& operator*(const Vec3<T>& v) {
|
||||
return new Vec3(x * v.x, y * v.y, z * v.z);
|
||||
}
|
||||
inline Vec3& operator/(const Vec3<T>& v) {
|
||||
return new Vec3(x / v.x, y / v.y, z / v.z);
|
||||
}
|
||||
inline Vec3& operator+=(const Vec3<T>& v) {
|
||||
x+=v.x; y+=v.y; z+=v.z;
|
||||
return *this;
|
||||
}
|
||||
inline Vec3& operator-=(const Vec3<T>& v) {
|
||||
x-=v.x; y-=v.y; z-=v.z;
|
||||
return *this;
|
||||
}
|
||||
inline Vec3& operator*=(const Vec3<T>& v) {
|
||||
x*=v.x; y*=v.y; z*=v.z;
|
||||
return *this;
|
||||
}
|
||||
inline Vec3& operator/=(const Vec3<T>& v) {
|
||||
x/=v.x; y/=v.y; z/=v.z;
|
||||
return *this;
|
||||
}
|
||||
template<class P>
|
||||
inline Vec3& operator+=(P s) {
|
||||
x+=s; y+=s; z+=s;
|
||||
return *this;
|
||||
}
|
||||
template<class P>
|
||||
inline Vec3& operator-=(P s) {
|
||||
x-=s; y-=s; z-=s;
|
||||
return *this;
|
||||
}
|
||||
template<class P>
|
||||
inline Vec3& operator*=(P s) {
|
||||
x*=s; y*=s; z*=s;
|
||||
return *this;
|
||||
}
|
||||
template<class P>
|
||||
inline Vec3& operator/=(P s) {
|
||||
x/=s; y/=s; z/=s;
|
||||
return *this;
|
||||
}
|
||||
};
|
||||
|
||||
template<class T>
|
||||
struct Vec2 {
|
||||
T x, y;
|
||||
template<class P>
|
||||
Vec2(P x, P y) : x(x), y(y) {}
|
||||
template<class P>
|
||||
Vec2(P all) : x(all), y(all) {}
|
||||
Vec2() : x(0), y(0) {}
|
||||
inline const Vec2& operator+() {
|
||||
return *this;
|
||||
}
|
||||
inline Vec2& dot(const Vec3<T>& v) {
|
||||
return (x * v.x + y * v.y);
|
||||
}
|
||||
inline Vec2& operator-() {
|
||||
return Vec3<T>(-x, -y);
|
||||
}
|
||||
inline Vec2& operator+(const Vec2<T>& v) {
|
||||
return new Vec2(x + v.x, y + v.y);
|
||||
}
|
||||
inline Vec2& operator-(const Vec2<T>& v) {
|
||||
return new Vec2(x - v.x, y - v.y);
|
||||
}
|
||||
inline Vec2& operator*(const Vec2<T>& v) {
|
||||
return new Vec2(x * v.x, y * v.y);
|
||||
}
|
||||
inline Vec2& operator/(const Vec2<T>& v) {
|
||||
return new Vec2(x / v.x, y / v.y);
|
||||
}
|
||||
inline Vec2& operator+=(const Vec2<T>& v) {
|
||||
x+=v.x; y+=v.y;
|
||||
return *this;
|
||||
}
|
||||
inline Vec2& operator-=(const Vec2<T>& v) {
|
||||
x-=v.x; y-=v.y;
|
||||
return *this;
|
||||
}
|
||||
inline Vec2& operator*=(const Vec2<T>& v) {
|
||||
x*=v.x; y*=v.y;
|
||||
return *this;
|
||||
}
|
||||
inline Vec2& operator/=(const Vec2<T>& v) {
|
||||
x/=v.x; y/=v.y;
|
||||
return *this;
|
||||
}
|
||||
template<class P>
|
||||
inline Vec2& operator+=(P s) {
|
||||
x+=s; y+=s;
|
||||
return *this;
|
||||
}
|
||||
template<class P>
|
||||
inline Vec2& operator-=(P s) {
|
||||
x-=s; y-=s;
|
||||
return *this;
|
||||
}
|
||||
template<class P>
|
||||
inline Vec2& operator*=(P s) {
|
||||
x*=s; y*=s;
|
||||
return *this;
|
||||
}
|
||||
template<class P>
|
||||
inline Vec2& operator/=(P s) {
|
||||
x/=s; y/=s;
|
||||
return *this;
|
||||
}
|
||||
};
|
||||
|
||||
#endif
|
||||
2047
C++/Pixel-Engine/Rays/olcPixelGameEngine.h
Normal file
2047
C++/Pixel-Engine/Rays/olcPixelGameEngine.h
Normal file
File diff suppressed because it is too large
Load Diff
BIN
C++/Pixel-Engine/Rays/output.o
Executable file
BIN
C++/Pixel-Engine/Rays/output.o
Executable file
Binary file not shown.
153
C++/Pixel-Engine/Rays/rect.cpp
Normal file
153
C++/Pixel-Engine/Rays/rect.cpp
Normal file
@@ -0,0 +1,153 @@
|
||||
#include "rect.h"
|
||||
|
||||
Rect::Rect() {
|
||||
Clear();
|
||||
}
|
||||
|
||||
Rect::Rect(int x, int y, int w, int h) {
|
||||
SetRect(x, y, w, h);
|
||||
}
|
||||
|
||||
void Rect::Clear() {
|
||||
SetRect(0, 0, 0, 0);
|
||||
}
|
||||
|
||||
std::string Rect::ToString() {
|
||||
std::string res = "(";
|
||||
res += std::to_string(x);
|
||||
res += ", ";
|
||||
res += std::to_string(y);
|
||||
res += ", ";
|
||||
res += std::to_string(w);
|
||||
res += ", ";
|
||||
res += std::to_string(h);
|
||||
res += ")";
|
||||
return res;
|
||||
}
|
||||
|
||||
bool Rect::Intersects(Rect* rect) {
|
||||
int leftA = x;
|
||||
int rightA = x + w;
|
||||
int topA = y;
|
||||
int bottomA = y + h;
|
||||
|
||||
int leftB = rect->x;
|
||||
int rightB = rect->x + rect->w;
|
||||
int topB = rect->y;
|
||||
int bottomB = rect->y + rect->h;
|
||||
|
||||
if (bottomA <= topB) return false;
|
||||
if (topA >= bottomB) return false;
|
||||
if (rightA <= leftB) return false;
|
||||
if (leftA >= rightB) return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// bool Rect::Intersects(int x, int y, int w, int h) {
|
||||
// return Intersects(&CreateRect(x, y, w, h));
|
||||
// }
|
||||
|
||||
bool Rect::Contains(Rect* rect) {
|
||||
return (rect->x >= x && rect->Right() <= (x + w) && rect->y >= y && rect->Bottom() <= (y + h));
|
||||
}
|
||||
|
||||
bool Rect::Contains(Vec2<int>* point) {
|
||||
return (point->x >= x && point->x <= (x + w) && point->y >= y && point->y <= (y + h));
|
||||
}
|
||||
|
||||
bool Rect::Contains(int x, int y, int w, int h) {
|
||||
Rect tempRect(x, y, w, h);
|
||||
return Contains(&tempRect);
|
||||
}
|
||||
|
||||
Vec2<int>* Rect::Position() {
|
||||
Vec2<int>* res = new Vec2<int>(x, y);
|
||||
return res;
|
||||
}
|
||||
|
||||
Vec2<int>* Rect::Center() {
|
||||
Vec2<int>* res = new Vec2<int>(x + (w / 2), y + (h / 2));
|
||||
return res;
|
||||
}
|
||||
|
||||
int Rect::CenterX() {
|
||||
return (x + (w / 2));
|
||||
}
|
||||
|
||||
int Rect::CenterY() {
|
||||
return (y + (h / 2));
|
||||
}
|
||||
|
||||
int Rect::Left() {
|
||||
return x;
|
||||
}
|
||||
|
||||
int Rect::Right() {
|
||||
return (x + w);
|
||||
}
|
||||
|
||||
int Rect::Top() {
|
||||
return y;
|
||||
}
|
||||
|
||||
int Rect::Bottom() {
|
||||
return y + h;
|
||||
}
|
||||
|
||||
int Rect::Perimiter() {
|
||||
return (w + w + h + h);
|
||||
}
|
||||
|
||||
int Rect::Area() {
|
||||
return (w + h);
|
||||
}
|
||||
|
||||
int Rect::GetX() {
|
||||
return x;
|
||||
}
|
||||
|
||||
int Rect::GetY() {
|
||||
return y;
|
||||
}
|
||||
|
||||
int Rect::GetW() {
|
||||
return w;
|
||||
}
|
||||
|
||||
int Rect::GetH() {
|
||||
return h;
|
||||
}
|
||||
|
||||
void Rect::SetRect(int x, int y, int w, int h) {
|
||||
this->x = x;
|
||||
this->y = y;
|
||||
this->w = w;
|
||||
this->h = h;
|
||||
}
|
||||
|
||||
void Rect::SetSize(Vec2<int>* size) {
|
||||
this->x = size->x;
|
||||
this->y = size->y;
|
||||
}
|
||||
|
||||
void Rect::SetPos(Vec2<int>* pos) {
|
||||
this->w = pos->x;
|
||||
this->h = pos->y;
|
||||
}
|
||||
|
||||
void Rect::Translate(Vec2<int>* offset) {
|
||||
this->x += offset->x;
|
||||
this->y += offset->y;
|
||||
}
|
||||
|
||||
void Rect::TranslateX(int x) {
|
||||
this->x += x;
|
||||
}
|
||||
|
||||
void Rect::TranslateY(int y) {
|
||||
this->y += y;
|
||||
}
|
||||
|
||||
Rect::~Rect() {
|
||||
}
|
||||
69
C++/Pixel-Engine/Rays/rect.h
Normal file
69
C++/Pixel-Engine/Rays/rect.h
Normal file
@@ -0,0 +1,69 @@
|
||||
#ifndef _RECT_H_
|
||||
#define _RECT_H_
|
||||
|
||||
#include <string>
|
||||
#include "math.h"
|
||||
|
||||
class Rect {
|
||||
public:
|
||||
Rect();
|
||||
Rect(int x, int y, int w, int h);
|
||||
void Clear();
|
||||
|
||||
static Rect CreateRect(int x, int y, int w, int h) {
|
||||
Rect tempRect(x, y, w, h);
|
||||
return tempRect;
|
||||
}
|
||||
|
||||
Rect operator+(Rect* rect) {
|
||||
return Rect(this->x + rect->x, this->y + this->x, w, h);
|
||||
}
|
||||
Rect operator-(Rect* rect) {
|
||||
return Rect(this->x - rect->x, this->y - this->x, w, h);
|
||||
}
|
||||
bool operator==(const Rect* rect) {
|
||||
return (x == rect->x && y == rect->y && w == rect->w && h == rect->h);
|
||||
}
|
||||
bool operator!=(const Rect* rect) {
|
||||
return !(x == rect->x && y == rect->y && w == rect->w && h == rect->h);
|
||||
}
|
||||
|
||||
std::string ToString();
|
||||
|
||||
bool Intersects(Rect* rect);
|
||||
// bool Intersects(int x, int y, int w, int h);
|
||||
|
||||
bool Contains(Rect* rect);
|
||||
bool Contains(Vec2<int>* point);
|
||||
bool Contains(int x, int y, int w, int h);
|
||||
|
||||
Vec2<int>* Position();
|
||||
Vec2<int>* Center();
|
||||
int CenterX();
|
||||
int CenterY();
|
||||
|
||||
int Left();
|
||||
int Right();
|
||||
int Top();
|
||||
int Bottom();
|
||||
int Perimiter();
|
||||
int Area();
|
||||
|
||||
int GetX();
|
||||
int GetY();
|
||||
int GetW();
|
||||
int GetH();
|
||||
|
||||
void SetRect(int x, int y, int w, int h);
|
||||
void SetSize(Vec2<int>* size);
|
||||
void SetPos(Vec2<int>* pos);
|
||||
void Translate(Vec2<int>* offset);
|
||||
void TranslateX(int x);
|
||||
void TranslateY(int y);
|
||||
|
||||
int x, y, w, h;
|
||||
|
||||
virtual ~Rect();
|
||||
};
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user