c++ - Cannot place Collision Detection debug rectangle over game sprites -
i following along let's make rpg (c++/sdl2) - tutorials on youtube , stuck on let's make rpg (c++/sdl2) - part 35 continuation on collision (https://www.youtube.com/watch?v=dlu6g2s3ta0&list=plhm_a02ntaavey-4ezh7p6bbosv-dka-0).
i trying place transparent rectangle on sprites debug collision detection. not getting errors there no 1 piece of code can show you.
can please shed light on went wrong. i'm new c++ i'm not giving up. thank time. i'm genuinely grateful.
this code both header , cpp file of collision rectangle
#include "collisionrectangle.h" collisionrectangle::collisionrectangle() { offsetx = 0; offsety = 0; setrectangle(0,0,0,0); } collisionrectangle::collisionrectangle(int x, int y, int w, int h) { offsetx = x; offsety = y; setrectangle(0,0,w,h); } collisionrectangle::~collisionrectangle() { //dtor } void collisionrectangle::setrectangle(int x, int y, int w, int h) { collisionrect.x = x + offsety; collisionrect.y = y + offsety; collisionrect.w = w; collisionrect.h = h; } //header #pragma once #include "stdafx.h" class collisionrectangle { public: collisionrectangle(); collisionrectangle(int x, int y, int w, int h); ~collisionrectangle(void); void setrectangle(int x, int y, int w, int h); sdl_rect getrectangle(){ return collisionrect; } void setx(int x){ collisionrect.x = x + offsetx; } void sety(int y){ collisionrect.y = y + offsety; } private: int offsetx; int offsety; sdl_rect collisionrect; };
this code both header , cpp file of sprites
#include "sprite.h" sprite::sprite(sdl_renderer* passed_renderer, std::string filepath, int x, int y, int w, int h, float *passed_camerax, float *passed_cameray, collisionrectangle passed_collisionrect) { collisionrect = passed_collisionrect; renderer = passed_renderer; collisionsdl_rect = collisionrect.getrectangle(); image = null; image = img_loadtexture(renderer, filepath.c_str()); if (image == null) { std::cout << "couldn't load " << filepath.c_str() << std::endl; } collisionimage = null; collisionimage = img_loadtexture(renderer, "data/debugimages/debugbox.png"); if (collisionimage == null) { std::cout << "couldn't load " << "collisionimage" << std::endl; } rect.x = x; rect.y = y; rect.w = w; rect.h = h; sdl_querytexture(image,null,null, &img_width, &img_height); crop.x = 0; crop.y = 0; crop.w = img_width; crop.h = img_height; x_pos = x; y_pos = y; origin_x = 0; origin_y = 0; currentframe = 0; amount_frame_x = 0; amount_frame_y = 0; camerax = passed_camerax; cameray = passed_cameray; camera.x = rect.x + *camerax; camera.y = rect.y + *cameray; camera.w = rect.w; camera.h = rect.h; } void sprite::drawsteady() { sdl_rendercopy(renderer,image, &crop, &rect); } void sprite::draw() { camera.x = rect.x + *camerax; camera.y = rect.y + *cameray; collisionrect.setx(rect.x + *camerax); collisionrect.sety(rect.y + *cameray); sdl_rendercopy(renderer,image, &crop, &camera); sdl_rendercopy(renderer,collisionimage, null, &collisionsdl_rect); } void sprite::setupanimation(int passed_amount_x, int passed_amount_y) { amount_frame_x = passed_amount_x; amount_frame_y = passed_amount_y; } void sprite::playanimation(int beginframe, int endframe, int row, float speed) { if (animationdelay+speed < sdl_getticks()) { if (endframe <= currentframe) currentframe = beginframe; else currentframe++; crop.x = currentframe * (img_width/amount_frame_x); crop.y = row * (img_height/amount_frame_y); crop.w = img_width/amount_frame_x; crop.h = img_height/amount_frame_y; animationdelay = sdl_getticks(); } } sprite::~sprite() { sdl_destroytexture(image); } void sprite::setx(float x) { x_pos = x; rect.x = int(x_pos - origin_x); } void sprite::sety(float y) { y_pos = y; rect.y = int(y_pos - origin_y); } void sprite::setposition(float x, float y) { x_pos = x; y_pos = y; rect.x = int(x_pos - origin_x); rect.y = int(y_pos - origin_y); } float sprite::getx() { return x_pos; } float sprite::gety() { return y_pos; } void sprite::setorigin(float x, float y) { origin_x = x; origin_y = y; setposition(getx(),gety()); } void sprite::setwidth(int w) { rect.w = w; } void sprite::setheight(int h) { rect.h = h; } int sprite::getwidth() { return rect.w; } int sprite::getheight() { return rect.h; } //header #pragma once #include "stdafx.h" #include "sdl_setup.h" #include "collisionrectangle.h" class sprite { public: sprite(sdl_renderer* passed_renderer, std::string filepath, int x, int y, int w, int h, float *camerax, float *cameray, collisionrectangle passed_collisionrect); ~sprite(); void draw(); void drawsteady(); void setx(float x); void sety(float y); void setposition(float x, float y); float getx(); float gety(); void setorigin(float x, float y); int getwidth(); int getheight(); void setheight(int h); void setwidth(int w); void playanimation(int beginframe, int endframe, int row, float speed); void setupanimation(int passed_amount_x, int passed_amount_y); private: collisionrectangle collisionrect; sdl_rect camera; sdl_rect collisionsdl_rect; float *camerax; float *cameray; float origin_x; float origin_y; float x_pos; float y_pos; sdl_texture* image; sdl_texture* collisionimage; sdl_rect rect; sdl_rect crop; sdl_renderer* renderer; int img_width; int img_height; int currentframe; int animationdelay; int amount_frame_x; int amount_frame_y; };
Comments
Post a Comment