Noticed posible future memory leak, Camera class complete, zoom not working, TODO updated and entity class constructor bug where m_renderer was nullptr fixed

This commit is contained in:
plane000
2018-10-10 20:24:33 +01:00
parent 49b3aa9904
commit faf47367eb
7 changed files with 51 additions and 19 deletions

View File

@@ -4,6 +4,10 @@ x -> complete
- -> depreciated / not doing
? -> in development
***URGENT FIX NEEDED***
[ ] Memory leak in rendering pipeline
**TODO**
[ ] Fix entity / sprite resizing
[x] Add sprite methods to entity
[x] Remove member initialization lists from Sprite and Entity, use Vec2* instead
@@ -11,7 +15,7 @@ x -> complete
[ ] Maybe use preprocessor statements
[ ] Time and other useful logging methods
[ ] Empty constructor, instance method
[ ] Rect class to extend SDL_Rect and add conversion capabilitys, colision and intersection to it
[x] Rect class to extend SDL_Rect and add conversion capabilitys, colision and intersection to it
[x] Camera class to use Rect* instead of SLD_Rect*
[x] Add and subtract (+ -) operators to add rectangles and subtract them together
[x] Equal and not equal (!= ==) operators to return true / flase if the rectangles match
@@ -22,15 +26,16 @@ x -> complete
[x] Switch other classes to use this instead of SDL_Rect* and make sure to update the render pipeline
[x] Center point
[-] Maybe a point class - used Vec2*
[ ] Game camera class and redo rendering pipeline
[x] Game camera class and redo rendering pipeline
[?] Add rotation and flipping for entities and sprites
[x] Camera class
[ ] Make the camera class control the rendering of the active scene
[ ] Game coordinate system
[ ] Render to GameWorld coordinates instead of screen coordinates
[ ] Each entity and sprite should store a reference to Camera
[x] Make the camera class control the rendering of the active scene
[x] Game coordinate system
[x] Render to GameWorld coordinates instead of screen coordinates
[-] Each entity and sprite should store a reference to Camera
[ ] Fix zoom in the camera
[ ] GameWorld class with coordinate system
[ ] Switch between camera modes on GameWorld
[?] Switch between camera modes on GameWorld
[ ] Multiple scenes stored as levels
[ ] UI and HUD (maybe later tho)
[ ] Framerate

View File

@@ -20,6 +20,10 @@ public:
int GetW();
int GetH();
//TODO: zoom zooms everything, regardless of position or size
//it should zoom relative to objects in the active game scene
// float Zoom = 1;
virtual ~Camera();
private:
Rect* m_view;

View File

@@ -6,15 +6,20 @@ Entity::Entity(std::string name, Renderer* renderer)
, m_col(new Vec4(0, 0, 0, 255))
, m_rect(new Rect(0, 0, 0, 0)) {
this->m_renderer = renderer;
this->m_name = name;
this->m_renderer = renderer;
this->Rendertype = RenderType::MODE_TEXTURE;
}
Entity::Entity(std::string name, Renderer* renderer, PolyDrawType drawType) {
Entity::Entity(std::string name, Renderer* renderer, PolyDrawType drawType)
: Pos(new Vec2(0, 0))
, Size(new Vec2(0, 0))
, m_col(new Vec4(0, 0, 0, 255))
, m_rect(new Rect(0, 0, 0, 0)) {
this->m_name = name;
this->Drawtype = drawType;
this->m_renderer = renderer;
this->Rendertype = RenderType::MODE_POLYGON;
}
@@ -53,7 +58,7 @@ bool Entity::LoadTexture(SDL_Surface* image) {
}
void Entity::SetDrawColour(Vec4* col) {
m_renderer->SetRendererColour(col);
this->m_col = col;
}
void Entity::SetRect(Rect* rect) {

View File

@@ -47,8 +47,8 @@ public:
virtual ~Entity();
protected:
Renderer* m_renderer;
std::string PATH = "C:/Users/Ben/Desktop/crumpet-engine";
// std::string PATH = "E:/Games/crumpet-engine";
// std::string PATH = "C:/Users/Ben/Desktop/crumpet-engine";
std::string PATH = "E:/Games/crumpet-engine";
private:
std::string m_name;

View File

@@ -12,13 +12,23 @@ int main(int argc, char** argv) {
game.UseCamera("free");
Timer timer;
Entity rect("rect", game.renderer, PolyDrawType::DRAW_FILLED_RECT);
rect.SetDrawColour(new Vec4(35, 89, 89, 255));
rect.SetRect(new Rect(130, 20, 100, 100));
Entity lines("lines", game.renderer, PolyDrawType::DRAW_LINES);
lines.SetDrawColour(new Vec4(164, 66, 244, 255));
lines.AddVecPoint(new Vec4(1, 1, 3323, 5335));
lines.AddVecPoint(new Vec4(626, 1, 333, 344));
lines.AddVecPoint(new Vec4(1, 23, 645, 5335));
Sprite sans("sans", game.renderer, SpriteType::SPRITE_ANIMATED);
sans.LoadSpriteTextures("/resources/sans-undertale-spritesheet.png");
sans.UseSpriteSheet(SpriteState::STATE_FRONT, 30, 9, 230, 300, 10, 4);
sans.UseSpriteSheet(SpriteState::STATE_RIGHT, 30, 320, 170, 300, 10, 4);
sans.UseSpriteSheet(SpriteState::STATE_LEFT, 40, 640, 170, 300, 10, 4);
sans.Pos = &Vec2(100, 100);
Sprite explosion("explosion", game.renderer ,SpriteType::SPRITE_ANIMATED);
explosion.LoadSpriteTextures("/resources/explosion.png");
explosion.UseSpriteSheet(SpriteState::STATE_DEFAULT, 1, 260, 64, 63, 0, 16);
@@ -66,6 +76,8 @@ int main(int argc, char** argv) {
game.renderer->RenderClear();
sans.Render();
explosion.Render();
rect.Render();
lines.Render();
game.renderer->RenderUpdate();
}

View File

@@ -21,25 +21,30 @@ Renderer::Renderer(std::string title, int width, int height, int targetFramerate
}
void Renderer::SetRendererColour(Vec4* col) {
SDL_SetRenderDrawColor(SDLRenderer, col->x, col->y, col->z, col->w);
SDL_SetRenderDrawColor(this->SDLRenderer, col->x, col->y, col->z, col->w);
}
void Renderer::RenderEmptyRect(Rect* rect) {
SDL_RenderDrawRect(SDLRenderer, rect->ToSDLRect());
SDL_Rect temp{ rect->GetX() - ActiveCamera->GetX(), rect->GetY() - ActiveCamera->GetY(), rect->GetW(), rect->GetH() };
SDL_RenderDrawRect(this->SDLRenderer, &temp);
}
void Renderer::RenderFilledRect(Rect* rect) {
SDL_RenderFillRect(SDLRenderer, rect->ToSDLRect());
SDL_Rect temp{ rect->GetX() - ActiveCamera->GetX(), rect->GetY() - ActiveCamera->GetY(), rect->GetW(), rect->GetH() };
SDL_RenderFillRect(this->SDLRenderer, &temp);
}
void Renderer::RenderLines(std::vector<Vec4*> points) {
for (unsigned int i = 0; i < points.size(); i++) {
SDL_RenderDrawLine(SDLRenderer, points[i]->x, points[i]->y, points[i]->z, points[i]->w);
SDL_RenderDrawLine(this->SDLRenderer, points[i]->x - ActiveCamera->GetX(), points[i]->y - ActiveCamera->GetY(), points[i]->z - ActiveCamera->GetX(), points[i]->w - ActiveCamera->GetY());
}
}
void Renderer::RenderTexture(Rect* fromRect, Rect* toRect, SDL_Texture* texture) {
SDL_RenderCopy(SDLRenderer, texture, fromRect->ToSDLRect(), toRect->ToSDLRect());
//Zoom implimentation (BROKEN)
// SDL_Rect toSDLRect{ toRect->GetX() - ActiveCamera->GetX(), toRect->GetY() - ActiveCamera->GetY(), toRect->GetW() * ActiveCamera->Zoom, toRect->GetH() * ActiveCamera->Zoom };
SDL_Rect toSDLRect{ toRect->GetX() - ActiveCamera->GetX(), toRect->GetY() - ActiveCamera->GetY(), toRect->GetW(), toRect->GetH() };
SDL_RenderCopy(SDLRenderer, texture, fromRect->ToSDLRect(), &toSDLRect);
}
void Renderer::RenderTexture(Rect* fromRect, Rect* toRect, SDL_Surface* surface) {

View File

@@ -56,9 +56,10 @@ public:
void MoveX(int offest);
void MoveY(int offest);
Vec2* Pos;
void Render();
Vec2* Pos;
virtual ~Sprite();
private:
std::map<SpriteState, std::map<int, Rect*>> m_spriteMaps;