Browse Source

Refactoring save point, made program 3x faster.

angel 7 years ago
parent
commit
9c615433cb

+ 36 - 0
include/buffer.h

@@ -0,0 +1,36 @@
+#ifndef BUFFER_H
+#define BUFFER_H
+
+#include "SDL.h"
+#include <type_traits>
+
+template<class T>
+struct Buffer{
+        int mWidth;
+        int mHeight;
+        int mPixelCount;
+        int mPitch;
+        T *buffer;
+
+        Buffer(int w, int h, T * array) 
+        : mWidth(w), mHeight(h), mPixelCount(w*h),
+                mPitch(w*sizeof(T)), buffer(array) 
+        {}
+        ~Buffer(){
+                delete [] buffer;
+        };
+        
+        void clear(){
+                if(std::is_same<T,float>::value){
+                        for(int i = 0; i < mPixelCount;++i){
+                                buffer[i]  = 0.0f;
+                        }
+                }
+                else{
+                        memset(buffer,0, mPitch*mHeight);       
+                }
+        }
+        
+};
+
+#endif

+ 30 - 0
include/camera.h

@@ -0,0 +1,30 @@
+#ifndef CAMERA_H
+#define CAMERA_H
+
+#include "matrix.h"
+#include "vector3.h"
+
+
+class Camera{
+    public:
+        Camera();
+
+        void update();
+
+    private:
+
+        //Position and direction of camera
+        Vector3 position{0,0,8};
+        Vector3 target;
+        Vector3 up{0,1,0};
+
+        //Matrices and frustrum stuff
+        Matrix4 viewMatrix;
+        Matrix4 projectionMatrix;
+        float fov{75};
+        float near{1};
+        float far{100};
+        float aspectRatio{1};
+};
+
+#endif

+ 40 - 0
include/displayManager.h

@@ -0,0 +1,40 @@
+#ifndef DISPLAYMANAGER_H
+#define DISPLAYMANAGER_H
+
+#include "SDL.h"
+#include "buffer.h"
+#include "texture.h"
+
+class DisplayManager{
+
+    public:
+        const static int SCREEN_WIDTH  = 640; //640
+        const static int SCREEN_HEIGHT = 480; //480
+
+        //Dummy Constructor / Destructor
+        DisplayManager();
+        ~DisplayManager();
+
+        //Initializes SDL context and creates window according to values above 
+        bool startUp();
+        void shutDown();
+
+        //Clear screens to black
+        void clear();
+
+        //Swaps the pixel buffer with the texture buffer and draws to screen
+        void draw(Buffer<Uint32> *pixelBuffer);
+
+    private:
+        bool startSDL();
+        bool createWindow();
+        bool createSDLRenderer();
+        bool createScreenTexture();
+
+        Texture      screenTexture;
+        SDL_Surface  *surface;
+        SDL_Renderer *SDLRenderer;
+        SDL_Window   *mainWindow;
+};
+
+#endif

+ 14 - 17
include/engine.h

@@ -2,36 +2,33 @@
 #define ENGINE_H
 #define ENGINE_H
 
 
 //Headers
 //Headers
-#include "windowManager.h"
-#include "renderManager.h"
+#include "displayManager.h"
+#include "softwareRenderer.h"
 #include "inputManager.h"
 #include "inputManager.h"
-#include "model.h"
-#include "matrix.h"
+#include "sceneManager.h"
 
 
+//Minimal graphics engine application
 class Engine{
 class Engine{
 
 
     public:
     public:
+        //Dummy constructors / Destructors
         Engine();
         Engine();
-
         ~Engine();
         ~Engine();
 
 
+        //I use these methods instead of constructors and destructors
+        //because I want to be able to control initialization order. 
+        //You'll see the same idea applied to all subsystem level classes.
         bool startUp();
         bool startUp();
-
         void shutDown();
         void shutDown();
 
 
-        void mainLoop();
-
-        void loadModels();
-
-        //Will not be here forever
-        void updateCamera();
+        //Contains the scene switching logic and the main application loop
+        void run();
 
 
     private:
     private:
-        WindowManager FEWindowManager;
-        RenderManager FERenderManager;
-        InputManager  FEInputManager;
-        Model        *sceneModels;
-        Matrix4      viewMatrix;
+        DisplayManager   gDisplayManager;
+        SoftwareRenderer gRenderer;
+        InputManager     gInputManager;
+        SceneManager     gSceneManager;
 };
 };
 
 
 #endif
 #endif

+ 0 - 1
include/inputManager.h

@@ -19,7 +19,6 @@ class InputManager{
 
 
     private:
     private:
         bool handleEvent(SDL_Event * event);
         bool handleEvent(SDL_Event * event);
-
 };
 };
 
 
 
 

+ 0 - 1
include/matrix.h

@@ -40,7 +40,6 @@ class Matrix4{
         Matrix4(){};
         Matrix4(){};
 
 
     private:
     private:
-        
         std::array<float, 16> mMatrix{};
         std::array<float, 16> mMatrix{};
 };
 };
 
 

+ 2 - 0
include/model.h

@@ -14,6 +14,8 @@ class Model{
         Mesh *getMesh();
         Mesh *getMesh();
         void initPosition(TransformParameters initPos);
         void initPosition(TransformParameters initPos);
 
 
+        void update();
+
     private:
     private:
         void buildBoundaryBox();
         void buildBoundaryBox();
         void buildMesh(std::string path);
         void buildMesh(std::string path);

+ 3 - 3
include/rasterizer.h

@@ -2,13 +2,13 @@
 #define RASTERIZER_H
 #define RASTERIZER_H
 
 
 #include "SDL.h"
 #include "SDL.h"
-#include "canvas.h"
+#include "buffer.h"
 #include "model.h"
 #include "model.h"
 
 
 class Rasterizer{
 class Rasterizer{
 
 
     public:
     public:
-        Rasterizer(Canvas *canvas) :mCanvas(canvas){}
+        Rasterizer(Buffer<Uint32> *buffer) :mPixelBuffer(buffer){}
 
 
         void drawTriangles(Vector3 &v1, Vector3 &v2, Vector3 &v3, float intensity);
         void drawTriangles(Vector3 &v1, Vector3 &v2, Vector3 &v3, float intensity);
 
 
@@ -35,7 +35,7 @@ class Rasterizer{
 
 
         void setDepthBufferAtLocation(int x, int y, float depth);
         void setDepthBufferAtLocation(int x, int y, float depth);
 
 
-        Canvas * mCanvas;
+        Buffer<Uint32> * mPixelBuffer;
 
 
         Uint32 white = SDL_MapRGBA(mappingFormat, 0xFF,0xFF,0xFF,0xFF);
         Uint32 white = SDL_MapRGBA(mappingFormat, 0xFF,0xFF,0xFF,0xFF);
         Uint32 red = SDL_MapRGBA(mappingFormat, 0xFF,0x00,0x00,0xFF);
         Uint32 red = SDL_MapRGBA(mappingFormat, 0xFF,0x00,0x00,0xFF);

+ 0 - 48
include/renderManager.h

@@ -1,48 +0,0 @@
-#ifndef RENDERMANAGER_H
-#define RENDERMANAGER_H
-
-#include "SDL.h"
-#include "windowManager.h"
-#include "texture.h"
-#include "rasterizer.h"
-#include "canvas.h"
-#include "model.h"
-
-class RenderManager {
-
-    public:
-        RenderManager();
-
-        ~RenderManager();
-
-        bool startUp(WindowManager windowManager);        
-
-        void render(Model *models, Matrix4 &mat);
-
-        void shutDown();
-
-    private:
-        //Init methods
-        bool createRenderer(SDL_Window * mainWindow);
-        bool createCanvas();
-        bool createScreenTexture();
-        void createProjectionMatrix();
-
-        //Rendering pipeline stuff
-        void clearScreen();
-        void updateScreen();
-
-        //Per vertex stuff
-        
-        //Culling
-        bool frustrumCulling(Model *model, Matrix4 &viewMatrix);
-
-        SDL_Renderer *mainRenderer;
-        Texture screenTexture;
-        Canvas *mainCanvas;
-        Rasterizer *raster;
-        Matrix4 projectionMatrix;
-
-};
-
-#endif

+ 22 - 0
include/scene.h

@@ -0,0 +1,22 @@
+#ifndef SCENE_H
+#define SCENE_H
+
+#include <string>
+#include "model.h"
+#include "camera.h"
+
+class Scene{
+    public:
+        //Builds a scene based on a path containing the scene's content
+        Scene(std::string path);
+        ~Scene();
+
+        //Updates all objects and cameras in scene
+        void update();
+    private:
+        Camera mainCamera;
+        Model *sceneModel;
+        void loadSceneModels(std::string path);
+};
+
+#endif

+ 32 - 0
include/sceneManager.h

@@ -0,0 +1,32 @@
+#ifndef SCENEMANAGER_H
+#define SCENEMANAGER_H
+
+#include "scene.h"
+//Add an enum in the future with the different scenes that it should be able to
+//render
+class SceneManager{
+
+    public:
+        //Dummy Constructor / Destructor
+        SceneManager();
+        ~SceneManager();
+
+        //Initializes and closes all scene related stuff
+        bool startUp();
+        void shutDown();
+
+        // Scene switching
+        bool switchScene();
+
+        // Update current scene
+        void update();
+
+    private:
+        bool loadScene();
+        bool closeScene();
+
+        Scene *currentScene;
+};
+
+
+#endif

+ 38 - 0
include/softwareRenderer.h

@@ -0,0 +1,38 @@
+#ifndef SRENDERER_H
+#define SRENDERER_H
+
+#include "displayManager.h"
+#include "rasterizer.h" 
+#include "buffer.h"
+
+class SoftwareRenderer {
+
+    public:
+        //Dummy Constructor / Destructor
+        SoftwareRenderer();
+        ~SoftwareRenderer();
+
+        //Creates all buffers and initializes all sub-programs needed
+        //For rendering
+        bool startUp(DisplayManager &displayManager);  
+        void shutDown();      
+
+        //void render(Model *models, Matrix4 &mat);
+        void render();
+        
+    private:
+        //Buffer methods
+        bool createBuffers();
+        void clearBuffers();
+
+        DisplayManager * screen;
+        Buffer<float> * zBuffer;
+        Buffer<Uint32> * pixelBuffer;
+        
+        Rasterizer *raster;
+
+        //Culling
+        //bool frustrumCulling(Model *model, Matrix4 &viewMatrix);
+};
+
+#endif

+ 6 - 4
include/texture.h

@@ -3,6 +3,8 @@
 
 
 #include "SDL.h"
 #include "SDL.h"
 
 
+//Needs another pass in the future, since it's so highly coupled with SDL
+//Okay for now but FIX THIS
 class Texture{
 class Texture{
 
 
     public:
     public:
@@ -14,21 +16,21 @@ class Texture{
 
 
         bool createBlank(SDL_Renderer * mainRenderer, int width, int height);
         bool createBlank(SDL_Renderer * mainRenderer, int width, int height);
 
 
-        void updateTexture(Uint32 * pixels);
+        void update(Uint32 * pixels);
 
 
         void renderToScreen(SDL_Renderer * mainRenderer);
         void renderToScreen(SDL_Renderer * mainRenderer);
 
 
     private:
     private:
-        int mPitch;
         SDL_Texture* mTexture;
         SDL_Texture* mTexture;
 
 
         //Dimensions
         //Dimensions
         int mWidth;
         int mWidth;
         int mHeight;
         int mHeight;
+        int mPitch;
 
 
         //Only used to lock and unlock pixels, not  the actual pixel
         //Only used to lock and unlock pixels, not  the actual pixel
-         //values
-        void* mPixels;
+        //values FIX THIS
+        void* mTrash;
 
 
 };
 };
 
 

+ 0 - 33
include/windowManager.h

@@ -1,33 +0,0 @@
-#ifndef WINDOWMANAGER_H
-#define WINDOWMANAGER_H
-
-#include "SDL.h"
-
-class WindowManager{
-
-    public:
-    const static int SCREEN_WIDTH  = 640; //640
-    const static int SCREEN_HEIGHT = 480; //480
-    WindowManager();
-
-    ~WindowManager();
-
-    bool startUp();
-
-    bool startSDL();
-
-    bool createWindow();
-
-    SDL_Window* getWindow();
-
-    void shutDown();
-
-    void wait();
-
-    private:
-    SDL_Window  *mainWindow;
-
-
-};
-
-#endif

+ 21 - 0
src/camera.cpp

@@ -0,0 +1,21 @@
+#include "camera.h"
+#include "SDL.h"
+#include "displayManager.h"
+
+Camera::Camera(){
+    viewMatrix = Matrix4::lookAt(position,target,up);
+    aspectRatio =  DisplayManager::SCREEN_WIDTH/DisplayManager::SCREEN_WIDTH;
+    projectionMatrix = Matrix4::makeProjectionMatrix(fov, aspectRatio, near,far);
+}
+
+void Camera::update(){
+    float t = static_cast<float>(SDL_GetTicks());
+    float radius = 8;
+    float camX   = std::sin(t/4000) * radius;
+    float camZ   = std::cos(t/4000) * radius;
+    position.x = camX;
+    position.y = 0;
+    position.z = camZ;
+    viewMatrix = Matrix4::lookAt(position,target,up);
+    //viewMatrix = (Matrix4::makeTranslateMat(0,camX*0.25,0)*viewMatrix)
+}

+ 109 - 0
src/displayManager.cpp

@@ -0,0 +1,109 @@
+#include "displayManager.h"
+
+DisplayManager::DisplayManager(){
+    
+}
+
+DisplayManager::~DisplayManager(){
+    
+}
+
+bool DisplayManager::startUp(){
+    bool success = true;
+    if( !startSDL() ){
+        success = false;
+    }
+    else{
+        if( !createWindow() ){
+            success = false;
+        }
+        else{
+            // if( !createSDLRenderer() ){
+            //     success = false;
+            // }
+            // else{
+            //     if( !createScreenTexture() ){
+            //         success = false;
+            //     }
+            // }
+
+        }
+    }
+    return success;
+}
+
+void DisplayManager::shutDown(){
+    // screenTexture.free();
+  
+    // SDL_FreeSurface(surface);
+
+    // SDL_DestroyRenderer(SDLRenderer);
+    // SDLRenderer = nullptr;
+
+    SDL_DestroyWindow(mainWindow);
+    mainWindow = nullptr;
+
+    SDL_Quit();
+}
+
+bool DisplayManager::startSDL(){
+    if( SDL_Init(SDL_INIT_VIDEO) != 0){
+        printf("Failed to initialize SDL. Error: %s\n", SDL_GetError() );
+        return  false;
+    }
+    return true;
+}
+
+bool DisplayManager::createWindow(){
+    mainWindow = SDL_CreateWindow( "SoftwareRenderer", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, 0);
+    surface = SDL_GetWindowSurface(mainWindow);
+    if( mainWindow == nullptr){
+        printf("Could not create window. Error: %s\n", SDL_GetError() );
+        return false;
+    }
+    return true;
+}
+
+bool DisplayManager::createSDLRenderer(){
+    //SDLRenderer = SDL_CreateRenderer(mainWindow, -1, SDL_RENDERER_PRESENTVSYNC);
+    SDLRenderer = SDL_CreateSoftwareRenderer(surface);
+    if(SDLRenderer == nullptr){
+        printf("Could not create renderer context. Error: %s\n", SDL_GetError());
+        return false;
+    }
+    return true;
+}
+
+bool DisplayManager::createScreenTexture(){
+    if(! screenTexture.createBlank(SDLRenderer, SCREEN_WIDTH, SCREEN_HEIGHT)){
+        printf("Could not create texture. Error: %s\n", SDL_GetError());
+        return false;
+    }
+    return true;
+}
+
+void DisplayManager::clear(){
+    SDL_LockSurface(surface);
+    SDL_memset(surface->pixels,0 ,surface->h * surface->pitch);
+    SDL_UnlockSurface(surface);
+    //SDL_SetRenderDrawColor(SDLRenderer, 0x00, 0x00, 0x00, 0xFF);
+    //SDL_RenderClear(SDLRenderer);
+
+}
+
+void DisplayManager::draw(Buffer<Uint32> *pixels){
+    
+    // screenTexture.update(pixels->buffer);
+
+    // //Render texture to screen
+    // screenTexture.renderToScreen(SDLRenderer);
+
+    // //Update screen to window
+    // SDL_RenderPresent( SDLRenderer );
+
+    //Testing pure software rendering
+    SDL_LockSurface(surface);
+    memcpy(surface->pixels, pixels->buffer, pixels->mHeight*pixels->mPitch);
+    SDL_UnlockSurface(surface);
+    SDL_UpdateWindowSurface(mainWindow);
+}

+ 47 - 56
src/engine.cpp

@@ -1,8 +1,4 @@
 #include "engine.h"
 #include "engine.h"
-#include <string>
-#include <vector3.h>
-#include <matrix.h>
-#include <math.h>
 
 
 Engine::Engine(){
 Engine::Engine(){
 }
 }
@@ -10,91 +6,86 @@ Engine::Engine(){
 Engine::~Engine(){
 Engine::~Engine(){
 }
 }
 
 
+//Starts up subsystems in an order that satifies their dependencies
 bool Engine::startUp(){
 bool Engine::startUp(){
     bool success = true;
     bool success = true;
-    //Startup window manager and create window
-    if( !FEWindowManager.startUp() ){
+    //Start up of all SDL related content
+    if( !gDisplayManager.startUp() ){
         success = false;
         success = false;
         printf("Failed to initialize window manager.\n");
         printf("Failed to initialize window manager.\n");
     }
     }
     else{
     else{
-        if( !FERenderManager.startUp(FEWindowManager) ){
+        //Gets window and creates all buffers according to window size
+        if( !gRenderer.startUp(gDisplayManager) ){
             success = false;
             success = false;
             printf("Failed to initialize render manager.\n");
             printf("Failed to initialize render manager.\n");
         }
         }
         else{
         else{
-            if( !FEInputManager.startUp() ){
+            //Loads default scene
+            if( !gSceneManager.startUp() ){
             success = false;
             success = false;
-            printf("Failed to initialize input manager.\n");
+            printf("Failed to initialize scene manager.\n");
+            }
+            else{
+                if ( !gInputManager.startUp() ){
+                    success = false;
+                    printf("Failed to initialize input manager.\n");
+                }
             }
             }
         }
         }
     }
     }
     return success;
     return success;
 }
 }
 
 
+//Closing in opposite order to avoid dangling pointers
 void Engine::shutDown(){
 void Engine::shutDown(){
-    delete sceneModels;
-    sceneModels = nullptr;
-    FEInputManager.shutDown();
-    FERenderManager.shutDown();
-    FEWindowManager.shutDown();
-
+    printf("Closing input manager.\n");
+    gInputManager.shutDown();
+    printf("Closing Scene manager.\n");
+    gSceneManager.shutDown();
+    printf("Closing renderer manager.\n");
+    gRenderer.shutDown();
+    printf("Closing display manager.\n");
+    gDisplayManager.shutDown();
 }
 }
 
 
-void Engine::mainLoop(){
+//Runs main application loop and allows for scene changes
+void Engine::run(){
+
+    //Main flags
     bool done = false;
     bool done = false;
+    bool switchScene = false;
+
+    //Counters
     int count = 0;
     int count = 0;
     unsigned int end = 0;
     unsigned int end = 0;
     unsigned int start = 0;
     unsigned int start = 0;
+
     printf("Entered Main Loop!\n");
     printf("Entered Main Loop!\n");
-    
     while(!done){
     while(!done){
         start = SDL_GetTicks();
         start = SDL_GetTicks();
         ++count;
         ++count;
+        
+        //If scene switching has been called you break out of the current loop 
+        if( switchScene ){
+            if( !gSceneManager.switchScene() ){
+                printf("Failed to switch scene! Quitting.\n");
+                continue;
+            }
+            else switchScene = false;
+        }
 
 
         //Handle all user input
         //Handle all user input
-        done = FEInputManager.processInput();
+        done = gInputManager.processInput();
 
 
-        //Update entities here in the future
-        //Right now only does simple demo stuff
-        //Maybe physics based in the future??
-        updateCamera();
+        //Update all models and camera in the current scene
+        gSceneManager.update();
+
+        //Enter rendering loop and render current scene
+        gRenderer.render();
 
 
-        //Perform all render calculations and update screen
-        FERenderManager.render(sceneModels, viewMatrix);
         end = SDL_GetTicks();
         end = SDL_GetTicks();
-        //SDL_Delay(100);
         printf("%2.1d: Loop elapsed time (ms):%d\n",count,end - start);
         printf("%2.1d: Loop elapsed time (ms):%d\n",count,end - start);
     }
     }
+    printf("Closing engine\n");
 }
 }
-
-void Engine::loadModels(){
-    //In the future I want to read all o the models in the model folder
-    //And build them here.  For now I force it to be only one.
-    //Probably should be its own class in the future
-    std::string path = "../models/cow.obj";
-    sceneModels = new Model(path);
-
-    //We also initialize the model position here position here
-    TransformParameters initParameters;
-    //initParameters.scaling = Vector3(1, 60, 60);
-    initParameters.rotation = Vector3(0,0,0);
-    initParameters.translation = Vector3(0, -1, 0);
-
-    sceneModels->initPosition(initParameters);
-
-    //sceneModels->describeMesh();
-}
-
-//This should be its own class in the future
-void Engine::updateCamera(){
-    float t = static_cast<float>(SDL_GetTicks());
-    float radius = 8;
-    float camX   = std::sin(t/4000) * radius;
-    float camZ   = std::cos(t/4000) * radius;
-    Vector3 pos(camX, 0, camZ);
-    Vector3 tar;
-    Vector3 v(0,1,0);
-    viewMatrix = Matrix4::lookAt(pos,tar,v);
-    //viewMatrix = (Matrix4::makeTranslateMat(0,camX*0.25,0)*viewMatrix);
-}

+ 1 - 2
src/main.cpp

@@ -4,8 +4,7 @@ int main(int argc, char *argv[]){
 
 
     Engine SSGE; //Simple Software Graphics Engine
     Engine SSGE; //Simple Software Graphics Engine
     if(SSGE.startUp()){
     if(SSGE.startUp()){
-        SSGE.loadModels();
-        SSGE.mainLoop();
+        SSGE.run();
     }
     }
     else{
     else{
         printf("SSGE could not initialize successfully. Shutting down.\n");
         printf("SSGE could not initialize successfully. Shutting down.\n");

+ 4 - 2
src/model.cpp

@@ -61,8 +61,6 @@ void Model::loadFaces(std::ifstream &file){
 }
 }
 
 
 void Model::loadVertices(std::ifstream &file){
 void Model::loadVertices(std::ifstream &file){
-    
-
     std::string line, v, x ,y ,z;
     std::string line, v, x ,y ,z;
     while(!file.eof()){
     while(!file.eof()){
         std::getline(file,line);
         std::getline(file,line);
@@ -144,4 +142,8 @@ void Model::buildBoundaryBox(){
     mBounds.mMinY = minY;
     mBounds.mMinY = minY;
     mBounds.mMinZ = minZ;
     mBounds.mMinZ = minZ;
 
 
+}
+
+void Model::update(){
+    
 }
 }

+ 117 - 117
src/rasterizer.cpp

@@ -10,8 +10,8 @@ void Rasterizer::makeCoolPattern(){
     Uint32 green = SDL_MapRGBA(mappingFormat, 0x00,0xFF,0x00,0xFF);
     Uint32 green = SDL_MapRGBA(mappingFormat, 0x00,0xFF,0x00,0xFF);
     Uint32 blue = SDL_MapRGBA(mappingFormat, 0x00,0x00,0xFF,0xFF);
     Uint32 blue = SDL_MapRGBA(mappingFormat, 0x00,0x00,0xFF,0xFF);
 
 
-    for(int y = 0; y < mCanvas->mHeight; ++y){
-        for(int x = 0; x < mCanvas->mWidth; ++x){
+    for(int y = 0; y < mPixelBuffer->mHeight; ++y){
+        for(int x = 0; x < mPixelBuffer->mWidth; ++x){
             Uint8 r = x*y % 256;
             Uint8 r = x*y % 256;
             Uint8 g = y % 256;
             Uint8 g = y % 256;
             Uint8 b = x % 256;
             Uint8 b = x % 256;
@@ -33,153 +33,153 @@ void Rasterizer::testPattern(){
 
 
 //Draws triangles using edge detection and baricentric coordinates,
 //Draws triangles using edge detection and baricentric coordinates,
 //Also shades based on a light coming directly from the camera
 //Also shades based on a light coming directly from the camera
-void Rasterizer::drawTriangles(Vector3 &a, Vector3 &b, Vector3 &c, float intensity ){
-    Uint32 color = SDL_MapRGBA(mappingFormat,
-                255*intensity,255*intensity,255*intensity,0xFF);
+// void Rasterizer::drawTriangles(Vector3 &a, Vector3 &b, Vector3 &c, float intensity ){
+//     Uint32 color = SDL_MapRGBA(mappingFormat,
+//                 255*intensity,255*intensity,255*intensity,0xFF);
 
 
-    //Converting to screen space
-    std::array<int, 3> xVerts;
-    std::array<int, 3> yVerts;
-    std::array<float, 3> zVerts;
-    xVerts[0] = (a.x + 1 ) * mCanvas->mWidth * 0.5;
-    yVerts[0] = (-a.y + 1 ) * mCanvas->mHeight * 0.5;
-    zVerts[0] = (a.z);
+//     //Converting to screen space
+//     std::array<int, 3> xVerts;
+//     std::array<int, 3> yVerts;
+//     std::array<float, 3> zVerts;
+//     xVerts[0] = (a.x + 1 ) * mCanvas->mWidth * 0.5;
+//     yVerts[0] = (-a.y + 1 ) * mCanvas->mHeight * 0.5;
+//     zVerts[0] = (a.z);
 
 
-    xVerts[1] = (b.x +1 ) * mCanvas->mWidth  * 0.5;
-    yVerts[1] = (-b.y +1 ) * mCanvas->mHeight * 0.5;
-    zVerts[1] = (b.z);
+//     xVerts[1] = (b.x +1 ) * mCanvas->mWidth  * 0.5;
+//     yVerts[1] = (-b.y +1 ) * mCanvas->mHeight * 0.5;
+//     zVerts[1] = (b.z);
 
 
-    xVerts[2] = (c.x +1 ) * mCanvas->mWidth  * 0.5;
-    yVerts[2] = (-c.y +1 ) * mCanvas->mHeight * 0.5;
-    zVerts[2] = (c.z);
+//     xVerts[2] = (c.x +1 ) * mCanvas->mWidth  * 0.5;
+//     yVerts[2] = (-c.y +1 ) * mCanvas->mHeight * 0.5;
+//     zVerts[2] = (c.z);
 
 
 
 
-    //Finding triangle bounding box
-    int xMax = *std::max_element(xVerts.begin(),xVerts.end());
-    int yMax = *std::max_element(yVerts.begin(),yVerts.end());
-    int xMin = *std::min_element(xVerts.begin(),xVerts.end());
-    int yMin = *std::min_element(yVerts.begin(),yVerts.end());
+//     //Finding triangle bounding box
+//     int xMax = *std::max_element(xVerts.begin(),xVerts.end());
+//     int yMax = *std::max_element(yVerts.begin(),yVerts.end());
+//     int xMin = *std::min_element(xVerts.begin(),xVerts.end());
+//     int yMin = *std::min_element(yVerts.begin(),yVerts.end());
 
 
-    //Find triangle area
-    float area = std::abs((xVerts[2]-xVerts[0])*(yVerts[1]-yVerts[0]) 
-                            - (xVerts[1]-xVerts[0])*(yVerts[2]-yVerts[0]));
+//     //Find triangle area
+//     float area = std::abs((xVerts[2]-xVerts[0])*(yVerts[1]-yVerts[0]) 
+//                             - (xVerts[1]-xVerts[0])*(yVerts[2]-yVerts[0]));
 
 
-    //Iterating the triangle bounding box
-    //printf("Iterating through triangle bounding box\n");
-    //printf("ymin: %d, ymax: %d, xmin: %d, xmax: %d\n",yMin, yMax, xMin, xMax);
-    for(int y = yMin; y <= yMax; ++y){
-        for(int x = xMin; x <= xMax; ++x){
-            //Throw pixel away if not in screen
-            if( x < 0 || x > mCanvas->mWidth || y < 0 || y > mCanvas->mHeight ) continue;
+//     //Iterating the triangle bounding box
+//     //printf("Iterating through triangle bounding box\n");
+//     //printf("ymin: %d, ymax: %d, xmin: %d, xmax: %d\n",yMin, yMax, xMin, xMax);
+//     for(int y = yMin; y <= yMax; ++y){
+//         for(int x = xMin; x <= xMax; ++x){
+//             //Throw pixel away if not in screen
+//             if( x < 0 || x > mCanvas->mWidth || y < 0 || y > mCanvas->mHeight ) continue;
 
 
-            float lambda0 = ((x-xVerts[0])*(yVerts[1]-yVerts[0]) 
-                            - (xVerts[1]-xVerts[0])*(y-yVerts[0])) / area;
+//             float lambda0 = ((x-xVerts[0])*(yVerts[1]-yVerts[0]) 
+//                             - (xVerts[1]-xVerts[0])*(y-yVerts[0])) / area;
 
 
-            float lambda1 = ((x-xVerts[1])*(yVerts[2]-yVerts[1]) 
-                            - (xVerts[2]-xVerts[1])*(y-yVerts[1])) / area;
+//             float lambda1 = ((x-xVerts[1])*(yVerts[2]-yVerts[1]) 
+//                             - (xVerts[2]-xVerts[1])*(y-yVerts[1])) / area;
 
 
-            float lambda2 = ((x-xVerts[2])*(yVerts[0]-yVerts[2]) 
-                            - (xVerts[0]-xVerts[2])*(y-yVerts[2])) / area;
+//             float lambda2 = ((x-xVerts[2])*(yVerts[0]-yVerts[2]) 
+//                             - (xVerts[0]-xVerts[2])*(y-yVerts[2])) / area;
 
 
-            //Throw pixel away if not in triangle
-            if(lambda0 < 0 || lambda1 < 0 || lambda2 < 0) continue;
+//             //Throw pixel away if not in triangle
+//             if(lambda0 < 0 || lambda1 < 0 || lambda2 < 0) continue;
 
 
-            float depth = lambda1*zVerts[0] + lambda2*zVerts[1] + lambda0*zVerts[2];
+//             float depth = lambda1*zVerts[0] + lambda2*zVerts[1] + lambda0*zVerts[2];
 
 
-            //If any of the edge functions are smaller than zero, discard the point
+//             //If any of the edge functions are smaller than zero, discard the point
             
             
-            if(getDepthBufferAtLocation(x,y) < depth){
-                float depthDiff = std::abs(depth - getDepthBufferAtLocation(x,y));
-                if(depth <= 1.0){
-                    if(depthDiff < 0.1){
-                    //printf("Bari: %f, %f, %f\n",lambda0,lambda1,lambda2);
-                    //printf("Depth Buffer: %f\n",getDepthBufferAtLocation(x,y));
-                    //printf("Pixel Depth: %f \n",depth);
-                    setDepthBufferAtLocation(x,y,depth);
-                    setPixelColor(x, y, color);
-
-                    }
-                    else{
-
-                        setDepthBufferAtLocation(x,y,depth);
-                        setPixelColor(x, y, color);
-                    }
-                }
+//             if(getDepthBufferAtLocation(x,y) < depth){
+//                 float depthDiff = std::abs(depth - getDepthBufferAtLocation(x,y));
+//                 if(depth <= 1.0){
+//                     if(depthDiff < 0.1){
+//                     //printf("Bari: %f, %f, %f\n",lambda0,lambda1,lambda2);
+//                     //printf("Depth Buffer: %f\n",getDepthBufferAtLocation(x,y));
+//                     //printf("Pixel Depth: %f \n",depth);
+//                     setDepthBufferAtLocation(x,y,depth);
+//                     setPixelColor(x, y, color);
+
+//                     }
+//                     else{
+
+//                         setDepthBufferAtLocation(x,y,depth);
+//                         setPixelColor(x, y, color);
+//                     }
+//                 }
                 
                 
                 
                 
                     
                     
-            } 
-
-        }
-    }
-
-}  
-
-void Rasterizer::drawWireFrame(Vector3 &v1, Vector3 &v2, Vector3 &v3 ){
-    drawLine(v1, v2, red);
-    drawLine(v2, v3, green);
-    drawLine(v1, v3, blue);
-}   
-
-void Rasterizer::drawLine(Vector3 &vertex1, Vector3 &vertex2, Uint32 &color){
-    int x1 = (vertex1.x + 1 ) * mCanvas->mWidth * 0.5;
-    int y1 = (-vertex1.y + 1 ) * mCanvas->mHeight * 0.5;
-    int x2 = (vertex2.x +1 ) * mCanvas->mWidth  * 0.5;
-    int y2 = (-vertex2.y +1 ) * mCanvas->mHeight * 0.5;
-
-    //transpose line if it is too steep
-    bool steep = false;
-    if (std::abs(x1-x2) < std::abs(y1-y2) ){
-        std::swap(x1,y1);
-        std::swap(x2,y2);
-        steep = true;
-    }
-
-    //Redefine line so that it is left to right
-    if ( x1  > x2 ){
-        std::swap(x1,x2);
-        std::swap(y1,y2);
-    }
-    int dx = x2 - x1;
-    int dy = y2 - y1;
-    int derror2 = std::abs(dy)*2;
-    int error2 = 0;
-    int y = y1;
-
-    for(int x=x1; x <= x2 ; x++){
-        if(steep){
-                setPixelColor(y, x, color);
-            }
-            else{
-                setPixelColor( x, y, color);
-            }
-        error2 += derror2;
-        if (error2 > dx){
-            y += (y2 > y1  ? 1 : -1);
-            error2 -= dx*2;
-        }
-
-    }
+//             } 
+
+//         }
+//     }
+
+// }  
+
+// void Rasterizer::drawWireFrame(Vector3 &v1, Vector3 &v2, Vector3 &v3 ){
+//     drawLine(v1, v2, red);
+//     drawLine(v2, v3, green);
+//     drawLine(v1, v3, blue);
+// }   
+
+// void Rasterizer::drawLine(Vector3 &vertex1, Vector3 &vertex2, Uint32 &color){
+//     int x1 = (vertex1.x + 1 ) * mCanvas->mWidth * 0.5;
+//     int y1 = (-vertex1.y + 1 ) * mCanvas->mHeight * 0.5;
+//     int x2 = (vertex2.x +1 ) * mCanvas->mWidth  * 0.5;
+//     int y2 = (-vertex2.y +1 ) * mCanvas->mHeight * 0.5;
+
+//     //transpose line if it is too steep
+//     bool steep = false;
+//     if (std::abs(x1-x2) < std::abs(y1-y2) ){
+//         std::swap(x1,y1);
+//         std::swap(x2,y2);
+//         steep = true;
+//     }
+
+//     //Redefine line so that it is left to right
+//     if ( x1  > x2 ){
+//         std::swap(x1,x2);
+//         std::swap(y1,y2);
+//     }
+//     int dx = x2 - x1;
+//     int dy = y2 - y1;
+//     int derror2 = std::abs(dy)*2;
+//     int error2 = 0;
+//     int y = y1;
+
+//     for(int x=x1; x <= x2 ; x++){
+//         if(steep){
+//                 setPixelColor(y, x, color);
+//             }
+//             else{
+//                 setPixelColor( x, y, color);
+//             }
+//         error2 += derror2;
+//         if (error2 > dx){
+//             y += (y2 > y1  ? 1 : -1);
+//             error2 -= dx*2;
+//         }
+
+//     }
         
         
-}
+// }
 
 
 void Rasterizer::setPixelColor(int x, int y, Uint32 color){
 void Rasterizer::setPixelColor(int x, int y, Uint32 color){
         int arrayCoordinates = convertCoordinates(x,y); 
         int arrayCoordinates = convertCoordinates(x,y); 
-        mCanvas->mBuffer[arrayCoordinates] = color;
+        mPixelBuffer->buffer[arrayCoordinates] = color;
     
     
 }
 }
 
 
 int Rasterizer::convertCoordinates(int x, int y){
 int Rasterizer::convertCoordinates(int x, int y){
-    return ((y * mCanvas->mWidth) + x);
+    return ((y * mPixelBuffer->mWidth) + x);
 }
 }
 
 
 float Rasterizer::getDepthBufferAtLocation(int x, int y){
 float Rasterizer::getDepthBufferAtLocation(int x, int y){
     int arrayCoordinates = convertCoordinates(x,y);
     int arrayCoordinates = convertCoordinates(x,y);
-    return mCanvas->mDBuffer[arrayCoordinates];
+    return mPixelBuffer->buffer[arrayCoordinates];
 }
 }
 
 
 void Rasterizer::setDepthBufferAtLocation(int x, int y, float depth){
 void Rasterizer::setDepthBufferAtLocation(int x, int y, float depth){
     int arrayCoordinates = convertCoordinates(x,y);
     int arrayCoordinates = convertCoordinates(x,y);
-    mCanvas->mDBuffer[arrayCoordinates] = depth;
+    mPixelBuffer->buffer[arrayCoordinates] = depth;
 }
 }
 
 

+ 0 - 163
src/renderManager.cpp

@@ -1,163 +0,0 @@
-#include "renderManager.h"
-
-RenderManager::RenderManager(){
-
-}
-
-RenderManager::~RenderManager(){
-
-}
-
-bool RenderManager::startUp(WindowManager WindowManager){
-    //Get our current window and create a renderer for it.
-    SDL_Window * mainWindow = WindowManager.getWindow();
-    if( !createRenderer(mainWindow) ){
-        return false;
-    }
-    else{
-        if( !createScreenTexture() ){
-            return false;
-        }
-        else{
-            if( !createCanvas() ){
-                printf("Could not build canvas.\n");
-                return false;
-            }
-            //Create rasterizer to begin drawing
-            raster = new Rasterizer(mainCanvas);
-            createProjectionMatrix();
-            return true;
-        }
-    }
-}
-
-void RenderManager::createProjectionMatrix(){
-    float fov = 75;
-    float aspectRatio = mainCanvas->mWidth / (float)mainCanvas->mHeight;
-    float near = 1;
-    float far  = 100;
-    projectionMatrix = Matrix4::makeProjectionMatrix(fov, aspectRatio, near,far);
-}
-
-bool RenderManager::createScreenTexture(){
-    if(! screenTexture.createBlank(mainRenderer, WindowManager::SCREEN_WIDTH, WindowManager::SCREEN_HEIGHT)){
-        printf("Could not create texture. Error: %s\n", SDL_GetError());
-        return false;
-    }
-    return true;
-}
-
-bool RenderManager::createCanvas(){
-    int pixelCount = WindowManager::SCREEN_WIDTH * WindowManager::SCREEN_HEIGHT;
-    int pitch      = WindowManager::SCREEN_WIDTH * sizeof(Uint32);
-    mainCanvas = new Canvas(WindowManager::SCREEN_WIDTH,
-                            WindowManager::SCREEN_HEIGHT,
-                            pixelCount, pitch,
-                            new Uint32[pixelCount], new float[pixelCount]);
-    SDL_memset(mainCanvas->mBuffer, 0, mainCanvas->mPixelCount * sizeof(Uint32) );
-    for(int i = 0; i < mainCanvas->mPixelCount;++i){
-        mainCanvas->mDBuffer[i]  = 0.0f;
-    }
-    return mainCanvas != NULL;
-}
-
-bool RenderManager::createRenderer(SDL_Window *window){
-    mainRenderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_PRESENTVSYNC);
-    if(mainRenderer == nullptr){
-        printf("Could not create renderer context. Error: %s\n", SDL_GetError());
-        return false;
-    }
-    return true;
-}
-
-void RenderManager::shutDown(){
-    delete raster;
-    delete mainCanvas->mBuffer;
-    delete mainCanvas->mDBuffer;
-    delete mainCanvas;
-    screenTexture.free();
-    SDL_DestroyRenderer(mainRenderer);
-    mainRenderer = nullptr;
-}
-
-void RenderManager::render(Model *models, Matrix4 &viewMatrix){
-
-    //Clear Screen back to black
-    clearScreen();
-
-    //Getting the meshes and faces 
-    Mesh * modelMesh = models->getMesh();
-    std::vector<Vector3> * faces = &modelMesh->faces;
-    std::vector<Vector3> * vertices = &modelMesh->vertices;
-
-    //If the frustrum culling returns true it means the model has been culled
-    //Because no other models are in the scene we return
-    if (frustrumCulling(models, viewMatrix)){
-        printf("Model culled!\n");
-        return;
-    } 
-
-    //Kind of a vertex shader I guess?
-    for (Vector3 f : *faces ){
-
-        //View matrix transform
-        Vector3 v0 = viewMatrix.matMultVec((*vertices)[f.x-1]); //-1 because .obj file starts face count
-        Vector3 v1 = viewMatrix.matMultVec((*vertices)[f.y-1]); // from 1. Should probably fix this 
-        Vector3 v2 = viewMatrix.matMultVec((*vertices)[f.z-1]); // At some point
-
-        //Back face culling in view space
-        Vector3 N1 = v1 - v0;
-        Vector3 N2 = v2 - v0;
-        Vector3 N  = (N1.crossProduct(N2)).normalized();
-        Vector3 neg = (v0.neg()).normalized();
-        float intensity = N.dotProduct(neg);
-        if(intensity < 0.0) continue; //Exit if you can't even see it
-
-
-        //stupid frustrum culling
-        //if(v1.x < -1 || v1.x > 1 || v1.y < -1 || v1.y > 1 || v1.z > 1 || v1.z < -1) continue;
-        //if(v2.x < -1 || v2.x > 1 || v2.y < -1 || v2.y > 1 || v2.z > 1 || v2.z < -1) continue;
-        //if(v3.x < -1 || v3.x > 1 || v3.y < -1 || v3.y > 1 || v3.z > 1 || v3.z < -1) continue;
-
-        // Projection matrix transformation
-        v0 = projectionMatrix.matMultVec(v0);
-        v1 = projectionMatrix.matMultVec(v1);
-        v2 = projectionMatrix.matMultVec(v2);
-
-        //Rasterizing and shading in one
-        raster->drawTriangles(v0,v1,v2,intensity);
-               
-    }
-    //Apply the pixel changes and redraw
-    updateScreen();
-}
-
-void RenderManager::updateScreen(){
-    
-    screenTexture.updateTexture(mainCanvas->mBuffer);
-
-    //Render texture to screen
-    screenTexture.renderToScreen(mainRenderer);
-
-    //Update screen to window
-    SDL_RenderPresent( mainRenderer );
-}
-
-void RenderManager::clearScreen(){
-    SDL_SetRenderDrawColor(mainRenderer, 0x00, 0x00, 0x00, 0xFF);
-    SDL_RenderClear(mainRenderer);
-    memset(mainCanvas->mBuffer,0, mainCanvas->mPitch*mainCanvas->mHeight);
-    for(int i = 0; i < mainCanvas->mPixelCount;++i){
-        mainCanvas->mDBuffer[i]  = 0.0f;
-    }
-}
-
-bool RenderManager::frustrumCulling(Model *model, Matrix4 &viewMatrix){
-    bool cull = false;
-    Matrix4 viewProjectionMat = projectionMatrix*viewMatrix;
-
-    
-
-    return cull;
-}
-

+ 32 - 0
src/scene.cpp

@@ -0,0 +1,32 @@
+#include <scene.h>
+
+Scene::Scene(std::string path){
+    loadSceneModels(path);
+}
+
+Scene::~Scene(){
+    delete sceneModel;
+}
+
+void Scene::update(){
+    mainCamera.update();
+    sceneModel->update();
+}
+
+void Scene::loadSceneModels(std::string path){
+    //In the future I want to read all o the models in the model folder
+    //And build them here.  For now I force it to be only one.
+    std::string fullPath = "../models/";
+    fullPath = fullPath + path;
+    sceneModel = new Model(fullPath);
+
+    //We also initialize the model position here position here
+    TransformParameters initParameters;
+    //initParameters.scaling = Vector3(1, 60, 60);
+    initParameters.rotation = Vector3(0,0,0);
+    initParameters.translation = Vector3(0, -1, 0);
+
+    sceneModel->initPosition(initParameters);
+
+    //sceneModel->describeMesh();
+}

+ 37 - 0
src/sceneManager.cpp

@@ -0,0 +1,37 @@
+#include "sceneManager.h"
+
+SceneManager::SceneManager(){
+    
+}
+
+SceneManager::~SceneManager(){
+    
+}
+
+bool SceneManager::startUp(){
+    if (!loadScene()){
+        printf("Could not load scene.\n");
+        return false;
+    }
+    return true;
+}
+
+void SceneManager::shutDown(){
+    delete currentScene;
+}
+
+//Not implemented yet, but I will want to do this in the future
+bool SceneManager::switchScene(){
+    return true;
+}
+
+//for now just loads the teapot.obj
+bool SceneManager::loadScene(){
+    bool success = true;
+    currentScene = new Scene("teapot.obj");
+    return success;
+}
+
+void SceneManager::update(){
+    currentScene->update();
+}

+ 124 - 0
src/softwareRenderer.cpp

@@ -0,0 +1,124 @@
+#include "softwareRenderer.h"
+
+SoftwareRenderer::SoftwareRenderer(){
+
+}
+
+SoftwareRenderer::~SoftwareRenderer(){
+
+}
+
+bool SoftwareRenderer::startUp(DisplayManager &displayManager){
+    screen = &displayManager;
+    if( !createBuffers() ){
+        return false;
+    }
+    //Create rasterizer to begin drawing
+    raster = new Rasterizer(pixelBuffer);
+    //createProjectionMatrix();
+    return true;
+}
+
+void SoftwareRenderer::shutDown(){
+    delete zBuffer;
+    delete pixelBuffer;
+    delete raster;
+}
+
+void SoftwareRenderer::clearBuffers(){
+    zBuffer->clear();
+    pixelBuffer->clear();
+}
+
+bool SoftwareRenderer::createBuffers(){
+    int w = DisplayManager::SCREEN_WIDTH;
+    int h = DisplayManager::SCREEN_HEIGHT;
+    int pixelCount = w*h;
+    bool success = true;
+
+    zBuffer = new Buffer<float>(w, h, new float[pixelCount]);
+    if( zBuffer == nullptr){
+        printf("Could not build z-Buffer.\n");
+        success = false;
+    }
+    else{
+        pixelBuffer = new Buffer<Uint32>(w, h, new Uint32[pixelCount]);
+        if( pixelBuffer == nullptr){
+            printf("Could not build pixel Buffer.\n");
+            success = false;
+        }
+    }
+    return success;
+}
+
+void SoftwareRenderer::render(){
+
+    //Clear screen and reset current buffers
+    screen->clear();
+    clearBuffers();
+
+    //Getting the meshes and faces 
+    // Mesh * modelMesh = models->getMesh();
+    // std::vector<Vector3> * faces = &modelMesh->faces;
+    // std::vector<Vector3> * vertices = &modelMesh->vertices;
+
+    // //If the frustrum culling returns true it means the model has been culled
+    // //Because no other models are in the scene we return
+    // // if (frustrumCulling(models, viewMatrix)){
+    // //     printf("Model culled!\n");
+    // //     return;
+    // // } 
+    // //Kind of a vertex shader I guess?
+    // for (Vector3 f : *faces ){
+
+    //     //View matrix transform
+    //     Vector3 v0 = viewMatrix.matMultVec((*vertices)[f.x-1]); //-1 because .obj file starts face count
+    //     Vector3 v1 = viewMatrix.matMultVec((*vertices)[f.y-1]); // from 1. Should probably fix this 
+    //     Vector3 v2 = viewMatrix.matMultVec((*vertices)[f.z-1]); // At some point
+
+    //     //Back face culling in view space
+    //     Vector3 N1 = v1 - v0;
+    //     Vector3 N2 = v2 - v0;
+    //     Vector3 N  = (N1.crossProduct(N2)).normalized();
+    //     Vector3 neg = (v0.neg()).normalized();
+    //     float intensity = N.dotProduct(neg);
+    //     if(intensity < 0.0) continue; //Exit if you can't even see it
+
+
+    //     //stupid frustrum culling
+    //     //if(v1.x < -1 || v1.x > 1 || v1.y < -1 || v1.y > 1 || v1.z > 1 || v1.z < -1) continue;
+    //     //if(v2.x < -1 || v2.x > 1 || v2.y < -1 || v2.y > 1 || v2.z > 1 || v2.z < -1) continue;
+    //     //if(v3.x < -1 || v3.x > 1 || v3.y < -1 || v3.y > 1 || v3.z > 1 || v3.z < -1) continue;
+
+    //     // Projection matrix transformation
+    //     v0 = projectionMatrix.matMultVec(v0);
+    //     v1 = projectionMatrix.matMultVec(v1);
+    //     v2 = projectionMatrix.matMultVec(v2);
+
+    //     //Rasterizing and shading in one
+    raster->makeCoolPattern();
+               
+    // }
+    //Apply the pixel changes and redraw
+    screen->draw(pixelBuffer);
+}
+
+// bool SoftwareRenderer::frustrumCulling(Model *model, Matrix4 &viewMatrix){
+//     bool cull = false;
+//     Matrix4 viewProjectionMat = projectionMatrix*viewMatrix;
+//     return cull;
+// }
+
+// bool SoftwareRenderer::createCanvas(){
+//     int pixelCount = WindowManager::SCREEN_WIDTH * WindowManager::SCREEN_HEIGHT;
+//     int pitch      = WindowManager::SCREEN_WIDTH * sizeof(Uint32);
+//     mainCanvas = new Canvas(WindowManager::SCREEN_WIDTH,
+//                             WindowManager::SCREEN_HEIGHT,
+//                             pixelCount, pitch,
+//                             new Uint32[pixelCount], new float[pixelCount]);
+//     SDL_memset(mainCanvas->mBuffer, 0, mainCanvas->mPixelCount * sizeof(Uint32) );
+//     for(int i = 0; i < mainCanvas->mPixelCount;++i){
+//         mainCanvas->mDBuffer[i]  = 0.0f;
+//     }
+//     return mainCanvas != NULL;
+// }

+ 6 - 6
src/texture.cpp

@@ -1,5 +1,5 @@
 #include "texture.h"
 #include "texture.h"
-#include "windowManager.h"
+
 
 
 Texture::Texture(){
 Texture::Texture(){
     mTexture = NULL;
     mTexture = NULL;
@@ -18,6 +18,7 @@ void Texture::free(){
         mWidth  = 0;
         mWidth  = 0;
         mHeight = 0;
         mHeight = 0;
         mPitch  = 0; 
         mPitch  = 0; 
+        mTrash = nullptr;
     }
     }
 }
 }
 
 
@@ -31,17 +32,16 @@ bool Texture::createBlank(SDL_Renderer  *renderer, int width, int height ){
     return mTexture != NULL;
     return mTexture != NULL;
 }
 }
 
 
-void Texture::updateTexture(Uint32 * pixels){
+void Texture::update(Uint32 * pixels){
 
 
     //Lock texture for manipulation
     //Lock texture for manipulation
-    SDL_LockTexture(mTexture, NULL, &mPixels, &mPitch );
+    SDL_LockTexture(mTexture, NULL, &mTrash, &mPitch );
     //Copy pixels to texture
     //Copy pixels to texture
-    memcpy(mPixels, pixels, mHeight*mPitch);
+    memcpy(mTrash, pixels, mHeight*mPitch);
 
 
     //Update texture
     //Update texture
     SDL_UnlockTexture(mTexture);
     SDL_UnlockTexture(mTexture);
-    mPixels = nullptr;
-    //SDL_UpdateTexture(mTexture, NULL, pixels, mPitch);
+    mTrash = nullptr;
 }
 }
 
 
 void Texture::renderToScreen(SDL_Renderer * renderer){
 void Texture::renderToScreen(SDL_Renderer * renderer){

+ 0 - 46
src/windowManager.cpp

@@ -1,46 +0,0 @@
-#include "windowManager.h"
-
-WindowManager::WindowManager(){
-    
-}
-
-WindowManager::~WindowManager(){
-    
-}
-
-bool WindowManager::startSDL(){
-    bool success = true;
-    if( SDL_Init(SDL_INIT_VIDEO) != 0){
-        printf("Failed to initialize SDL. Error: %s\n", SDL_GetError() );
-        success = false;
-    }
-    return success;
-}
-
-bool WindowManager::createWindow(){
-    bool success = true;
-    mainWindow = SDL_CreateWindow( "SoftwareRenderer", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, 0);
-        if( mainWindow == nullptr){
-            printf("Coult not create window. Error: %s\n", SDL_GetError() );
-            success = false;
-        }
-    return success;
-}
-
-bool WindowManager::startUp(){
-    if(!startSDL()){
-        return false;
-    }
-    else{
-       return createWindow();
-    }
-}
-SDL_Window * WindowManager::getWindow(){
-    return mainWindow;
-}
-
-void WindowManager::shutDown(){
-    SDL_DestroyWindow(mainWindow);
-    mainWindow = nullptr;
-    SDL_Quit();
-}