From c84dedd768224e36d6b58a42beea0f59ceb84aea Mon Sep 17 00:00:00 2001 From: BuildTools Date: Tue, 26 Jan 2021 16:41:34 -0500 Subject: [PATCH] enable separation of game and engine components --- demo/demo.cpp | 19 +++++++++++++++++++ demo/demo.h | 19 +++++++++++++++++++ main.cpp | 34 +++++++--------------------------- stuff/objects/argb.h | 22 ++++++++++++++++++++++ stuff/objects/ray2D.h | 12 ++++++------ stuff/objects/square.h | 2 ++ threedee.vcxproj | 3 +++ threedee.vcxproj.filters | 9 +++++++++ 8 files changed, 87 insertions(+), 33 deletions(-) create mode 100644 demo/demo.cpp create mode 100644 demo/demo.h create mode 100644 stuff/objects/argb.h diff --git a/demo/demo.cpp b/demo/demo.cpp new file mode 100644 index 0000000..93f9328 --- /dev/null +++ b/demo/demo.cpp @@ -0,0 +1,19 @@ +#include "demo.h" + +void game::run(SDL_Renderer* renderer) +{ + ray2D ray = ray2D::trace(vector2(0, 201), vector2(1, 0)); + ray2D ray2 = ray2D::trace(vector2(201, 0), vector2(0, 1)); + SDL_RenderDrawLine(renderer, ray.start.x, ray.start.y, ray.end.x, ray.end.y); + SDL_RenderDrawLine(renderer, ray2.start.x, ray2.start.y, ray2.end.x, ray2.end.y); + std::cout << global::entList.at(0) << std::endl; + if (ray.hitEnt) + std::cout << std::hex << ray.hitEnt << std::endl; + + //demo rays with moving object + global::entList.at(0)->x = global::mousePos.x; + global::entList.at(0)->y = global::mousePos.y; + + global::entList.run(renderer); + global::entList3D.run(renderer); +} \ No newline at end of file diff --git a/demo/demo.h b/demo/demo.h new file mode 100644 index 0000000..ae6ebee --- /dev/null +++ b/demo/demo.h @@ -0,0 +1,19 @@ +#pragma once +#include +#include +#include +#include "SDL.h" + +#include "../stuff/objects/vector2.h" +#include "../stuff/objects/vector3.h" +#include "../stuff/objects/ray2D.h" + +#include "../stuff/globals/globals.h" +#include "../stuff/callbacks/callbacks.h" +#include "../stuff/objects/square.h" +#include "../stuff/objects/argb.h" + +namespace game +{ + void run(SDL_Renderer* renderer); +} \ No newline at end of file diff --git a/main.cpp b/main.cpp index 70307ac..432d324 100644 --- a/main.cpp +++ b/main.cpp @@ -3,13 +3,7 @@ #include #include "SDL.h" -#include "stuff/objects/vector2.h" -#include "stuff/objects/vector3.h" -#include "stuff/objects/ray2D.h" - -#include "stuff/globals/globals.h" -#include "stuff/callbacks/callbacks.h" -#include "stuff/objects/square.h" +#include "demo/demo.h" int main(int argc, char** argv) { @@ -21,34 +15,20 @@ int main(int argc, char** argv) window = SDL_CreateWindow("threedee", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 1280, 720, SDL_WINDOW_SHOWN); surface = SDL_GetWindowSurface(window); renderer = SDL_CreateRenderer(window, -1, NULL); - SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); + SETRENDER_WHITE; SDL_Event eventHandler; global::entList.push_back(&square(200, 200, 50)); - //global::entList3D.push_back(cube(200, 200, 100, 50)); - while (global::running) { callbacks::handleCallbacks(&eventHandler); - SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0); - SDL_RenderFillRect(renderer, NULL); - SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); - - ray2D ray = ray2D::trace(vector2(0, 201), vector2(1, 0)); - ray2D ray2 = ray2D::trace(vector2(201, 0), vector2(0, 1)); - SDL_RenderDrawLine(renderer, ray.start.x, ray.start.y, ray.end.x, ray.end.y); - SDL_RenderDrawLine(renderer, ray2.start.x, ray2.start.y, ray2.end.x, ray2.end.y); - std::cout << global::entList.at(0) << std::endl; - if (ray.hitEnt) - std::cout << std::hex << ray.hitEnt << std::endl; - //demo rays with moving object - global::entList.at(0)->x = global::mousePos.x; - global::entList.at(0)->y = global::mousePos.y; - - global::entList.run(renderer); - global::entList3D.run(renderer); + SETRENDER_BLACK; + SDL_RenderClear(renderer); + SETRENDER_WHITE; + + game::run(renderer); SDL_RenderPresent(renderer); SDL_Delay(1); diff --git a/stuff/objects/argb.h b/stuff/objects/argb.h new file mode 100644 index 0000000..965954e --- /dev/null +++ b/stuff/objects/argb.h @@ -0,0 +1,22 @@ +#pragma once +#include +#include "SDL.h" +struct argb +{ + BYTE a, r, g, b; + argb(BYTE a_, BYTE r_, BYTE g_, BYTE b_) + { + a = a_; + r = r_; + g = g_; + b = b_; + } +}; + +#define WHITE argb(255, 255, 255, 255) +#define RED argb(255, 255, 0, 0) +#define BLUE argb(255, 0, 0, 255) +#define GREEN argb(255, 0, 255, 0) + +#define SETRENDER_WHITE SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255) +#define SETRENDER_BLACK SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0) \ No newline at end of file diff --git a/stuff/objects/ray2D.h b/stuff/objects/ray2D.h index f99592a..bd70ac7 100644 --- a/stuff/objects/ray2D.h +++ b/stuff/objects/ray2D.h @@ -1,14 +1,9 @@ #pragma once +#include #include "../globals/globals.h" #include "vector2.h" -//temp -bool inRect(vector2 point, vector2 a, vector2 b) -{ - return (point.x > a.x && point.y > a.y && point.x < b.x && point.y < b.y); -} - struct ray2D { vector2 start; @@ -45,4 +40,9 @@ struct ray2D } return ray; } + //temp + static bool inRect(vector2 point, vector2 a, vector2 b) + { + return (point.x > a.x && point.y > a.y && point.x < b.x&& point.y < b.y); + } }; \ No newline at end of file diff --git a/stuff/objects/square.h b/stuff/objects/square.h index 91d3d2c..af927d9 100644 --- a/stuff/objects/square.h +++ b/stuff/objects/square.h @@ -2,6 +2,7 @@ #include "SDL.h" #include "vector2.h" +#include "argb.h" /* squares are the basic 2D entity */ @@ -18,6 +19,7 @@ struct square : public vector2 } void run(SDL_Renderer* renderer) { + SETRENDER_WHITE; SDL_Rect rect{ this->x, this->y, this->radius, this->radius }; SDL_RenderFillRect(renderer, &rect); } diff --git a/threedee.vcxproj b/threedee.vcxproj index 8fed1ec..4738bef 100644 --- a/threedee.vcxproj +++ b/threedee.vcxproj @@ -154,6 +154,7 @@ + @@ -241,6 +242,7 @@ + @@ -252,6 +254,7 @@ + diff --git a/threedee.vcxproj.filters b/threedee.vcxproj.filters index 8c50236..577961c 100644 --- a/threedee.vcxproj.filters +++ b/threedee.vcxproj.filters @@ -303,6 +303,12 @@ Header Files + + Header Files + + + Header Files + @@ -314,5 +320,8 @@ Source Files + + Source Files + \ No newline at end of file