Add circle render

This commit is contained in:
zvon 2020-08-21 20:53:21 +02:00
parent 12c6c9049f
commit eaa463fb85
2 changed files with 37 additions and 9 deletions

View File

@ -102,7 +102,7 @@ void addStuff(SDLPP::Scene &scene, std::shared_ptr<SDLPP::Renderer> &r) {
x->addCollision(SDLPP::Rect(0.3,0.7,0.05,0.31)); x->addCollision(SDLPP::Rect(0.3,0.7,0.05,0.31));
x->addCollision(SDLPP::Rect(0.65,0.7,0.05,0.31)); x->addCollision(SDLPP::Rect(0.65,0.7,0.05,0.31));
x->addCollision(SDLPP::Rect(0.2,0.3,0.6,0.45)); x->addCollision(SDLPP::Rect(0.2,0.3,0.6,0.45));
x->addCollision(SDLPP::Rect(0.35,0,0.3,0.3)); x->addCollision(SDLPP::Circle(0.5,0.15,0.3));
x->setTexture("5.png"); x->setTexture("5.png");
x->setId(PLAYER_ID); x->setId(PLAYER_ID);
x->setColiderColor("00FF00"); x->setColiderColor("00FF00");
@ -273,7 +273,7 @@ int main() {
SDL_setFramerate(&gFPS, 60); SDL_setFramerate(&gFPS, 60);
std::thread inputThread(doInput, main_scene); std::thread inputThread(doInput, main_scene);
while( !quit ) { while( !quit ) {
// SDL_framerateDelay(&gFPS); SDL_framerateDelay(&gFPS);
main_scene->renderScene(); main_scene->renderScene();
main_scene->presentScene(); main_scene->presentScene();
frames++; frames++;

View File

@ -3,6 +3,7 @@
#include <SDL2/SDL.h> #include <SDL2/SDL.h>
#include <SDL2/SDL_image.h> #include <SDL2/SDL_image.h>
#include <cmath>
#include <iostream> #include <iostream>
#include <limits> #include <limits>
#include <memory> #include <memory>
@ -549,19 +550,46 @@ private:
class Circle : public CollisionPolygon { class Circle : public CollisionPolygon {
public: public:
Circle(double x, double y, double rad) : CollisionPolygon(x, y) { Circle(double x, double y, double rad) : CollisionPolygon(x, y) {
rad_ = rad; original_rad = rad;
} }
virtual ~Circle() {} virtual ~Circle() {}
virtual bool colidesWith(const CollisionPolygon &other) const; virtual bool colidesWith(const CollisionPolygon &other) const override;
virtual bool isCircle() const { return true; } virtual bool isCircle() const override { return true; }
virtual int topmost() const { return getY() - rad_; } virtual int topmost() const override { return getY() - rad_; }
virtual int bottommost() const { return getY() + rad_; }; virtual int bottommost() const override { return getY() + rad_; };
virtual int leftmost() const { return getX() - rad_; } virtual int leftmost() const override { return getX() - rad_; }
virtual int rightmost() const { return getX() + rad_; } virtual int rightmost() const override { return getX() + rad_; }
virtual void updateCollision(int x, int y, int w, int h) override {
position_x = original_x * w + x;
position_y = original_y * h + y;
rad_ = original_rad * w;
}
virtual void render(Renderer &renderer, const std::tuple<int,int,int> &color) override {
std::vector<int> rect = {leftmost(), topmost(), rightmost(), bottommost()};
auto center_x = getX();
auto center_y = getY();
auto radsq = rad_ * rad_;
for(int i = rect[0]; i <= rect[2]; i++) {
auto xdiff = center_x - i;
auto xdist = xdiff * xdiff;
auto allowed_rad = sqrt(radsq - xdist);
SDL_SetRenderDrawColor(renderer.getRendererPtr(), std::get<0>(color), std::get<1>(color), std::get<2>(color), 0x40);
SDL_RenderDrawLine(renderer.getRendererPtr(), i, center_y - allowed_rad, i, center_y + allowed_rad);
SDL_SetRenderDrawColor(renderer.getRendererPtr(), std::get<0>(color), std::get<1>(color), std::get<2>(color), 0x80);
SDL_RenderDrawLine(renderer.getRendererPtr(), i, center_y - allowed_rad, i, center_y - allowed_rad + 2);
SDL_RenderDrawLine(renderer.getRendererPtr(), i, center_y + allowed_rad, i, center_y + allowed_rad - 2);
}
SDL_SetRenderDrawColor(renderer.getRendererPtr(), 0xFF, 0, 0, 0xFF);
SDL_RenderDrawLine(renderer.getRendererPtr(), center_x, center_y, center_x + rad_, center_y);
SDL_RenderDrawLine(renderer.getRendererPtr(), center_x, center_y, center_x, center_y + rad_);
SDL_RenderDrawLine(renderer.getRendererPtr(), center_x, center_y, center_x - rad_, center_y);
SDL_RenderDrawLine(renderer.getRendererPtr(), center_x, center_y, center_x, center_y - rad_);
}
private: private:
int getRadius() const { int getRadius() const {
return rad_; return rad_;
} }
double original_rad;
int rad_; int rad_;
}; };