Browse Source

Restructured project into classes and refactored all functions.

angel 7 years ago
parent
commit
fa70d63c8e

+ 4 - 3
CMakeLists.txt

@@ -3,11 +3,12 @@ project(softwareRenderer)
 
 
 set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/modules")
-set(SDL2_PATH "C:\\vs_dev_lib\\SDL2-2.0.8")
+#set(SDL2_PATH "C:\\vs_dev_lib\\SDL2-2.0.8")
 find_package(SDL2 REQUIRED)
-include_directories(${SDL2_INCLUDE_DIR})
+include_directories(${SDL2_INCLUDE_DIR} include)
 
-add_executable(softwareRenderer src/main.cpp)
+file(GLOB source_files "*.h" "*.cpp" "src/*.cpp" "include/*.h")
+add_executable(softwareRenderer ${source_files})
 target_link_libraries(softwareRenderer ${SDL2_LIBRARY})
 
 

+ 29 - 0
include/engine.h

@@ -0,0 +1,29 @@
+#ifndef ENGINE_H
+#define ENGINE_H
+
+//Headers
+#include "windowManager.h"
+#include "renderManager.h"
+#include "inputManager.h"
+
+class Engine{
+
+    public:
+        Engine();
+
+        ~Engine();
+
+        bool startUp();
+
+        void shutDown();
+
+        void mainLoop();
+
+    private:
+        WindowManager FEWindowManager;
+        RenderManager FERenderManager;
+        InputManager  FEInputManager;
+};
+
+
+#endif

+ 26 - 0
include/inputManager.h

@@ -0,0 +1,26 @@
+#ifndef INPUTMANAGER_H
+#define INPUTMANAGER_H
+
+//Headers
+#include"SDL.h"
+
+class InputManager{
+
+    public:
+        InputManager();
+
+        ~InputManager();
+
+        bool startUp();
+
+        bool processInput();
+
+        void shutDown();
+
+    private:
+        bool handleEvent(SDL_Event * event);
+
+};
+
+
+#endif

+ 0 - 0
include/pixelPainter.h


+ 42 - 0
include/renderManager.h

@@ -0,0 +1,42 @@
+#ifndef RENDERMANAGER_H
+#define RENDERMANAGER_H
+
+#include "SDL.h"
+#include "windowManager.h"
+#include "texture.h"
+
+class RenderManager{
+
+    public:
+        static const Uint32 PIXEL_FORMAT = SDL_PIXELFORMAT_RGBA8888;
+        RenderManager();
+
+        ~RenderManager();
+
+        bool startUp(WindowManager windowManager);
+
+        bool createRenderer(SDL_Window * mainWindow);
+
+        bool createScreenTexture();
+
+        void clearScreen();
+
+        void updateScreen();
+
+        bool createBuffer();
+
+        void render();
+
+        void createPixelPattern();
+
+        void shutDown();
+
+    private:
+        int pixelCount;
+        Uint32 * buffer1;
+        SDL_Renderer  *mainRenderer;
+        Texture screenTexture;
+    
+};
+
+#endif

+ 31 - 0
include/texture.h

@@ -0,0 +1,31 @@
+#ifndef TEXTURE_H
+#define TEXTURE_H
+
+#include "SDL.h"
+
+class Texture{
+
+    public:
+        Texture();
+
+        ~Texture();
+        
+        void free();
+
+        bool createBlank(SDL_Renderer * mainRenderer, int width, int height);
+
+        void updateTexture(Uint32 * pixels);
+
+        void renderToScreen(SDL_Renderer * mainRenderer);
+
+    private:
+        int mPitch;
+        SDL_Texture* mTexture;
+
+        //Dimensions
+        int mWidth;
+        int mHeight;
+
+};
+
+#endif

+ 33 - 0
include/windowManager.h

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

+ 63 - 0
src/engine.cpp

@@ -0,0 +1,63 @@
+#include "engine.h"
+#include "windowManager.h"
+#include "renderManager.h"
+#include "inputManager.h"
+#include <stdio.h>
+
+Engine::Engine(){
+
+}
+
+Engine::~Engine(){
+    
+}
+
+bool Engine::startUp(){
+    bool success = true;
+    //Startup window manager and create window
+    if( !FEWindowManager.startUp() ){
+        success = false;
+        printf("Failed to initialize window manager.\n");
+    }
+    else{
+        if( !FERenderManager.startUp(FEWindowManager) ){
+            success = false;
+            printf("Failed to initialize render manager.\n");
+        }
+        else{
+            if( !FEInputManager.startUp() ){
+            success = false;
+            printf("Failed to initialize input manager.\n");
+            }
+        }
+    }
+    return success;
+}
+
+void Engine::shutDown(){
+    FEInputManager.shutDown();
+    FERenderManager.shutDown();
+    FEWindowManager.shutDown();
+}
+
+void Engine::mainLoop(){
+    bool done = false;
+    int count = 0;
+    printf("Entered Main Loop!\n");
+    
+    while(!done){
+        count++;
+
+        //Handle all user input
+        done = FEInputManager.processInput();
+
+        //Update entities here in the future
+        //TO DO
+
+        //Perform all render calculations and update screen
+        FERenderManager.render();
+
+        SDL_Delay(500);
+        printf("Loop Iteration N:%d\n",count);
+    }
+}

+ 34 - 0
src/inputManager.cpp

@@ -0,0 +1,34 @@
+#include "inputManager.h"
+
+InputManager::InputManager(){
+
+}
+
+InputManager::~InputManager(){
+
+}
+
+bool InputManager::startUp(){
+    //Nothing to do yet
+    return true;
+}
+
+void InputManager::shutDown(){
+    //Nothing to do yet
+}
+
+bool InputManager::processInput(){
+    bool done = false;
+    SDL_Event Event;
+    while(SDL_PollEvent(&Event)){
+       done = handleEvent(&Event);
+    }
+    return done;
+}
+
+bool InputManager::handleEvent(SDL_Event * event){
+    if(event->type == SDL_QUIT){
+        return true;
+    }
+    return false;
+}

+ 11 - 175
src/main.cpp

@@ -1,184 +1,20 @@
-#include <stdio.h>
-#include "SDL.h"
-
-//Screen size variables
-const int SCREEN_WIDTH = 640;
-const int SCREEN_HEIGHT = 480;
-
-//SDL startup and window initialization
-bool init();
-
-//Cleanup of memory and shutdown SDL
-void close();
-
-//Keyboard and mouse input managing
-void eventManager();
-
-//Clear screen to 0 black pixels
-void clearScreen();
-
-//Simple pixel modification function
-void modifyPixels();
-
-//Render pipeline function
-void render();
-
-//Current main project loop
-void loop();
+#include "engine.h"
 
 //Global variables
-SDL_Window  *gWindow = nullptr;
-SDL_Renderer *gRenderer = nullptr;
-SDL_Texture *gTexture = nullptr;
-Uint32 *gBuffer = nullptr;
-bool quitFlag = false;
+// SDL_Texture *gTexture   = nullptr;
+// Uint32 *gBuffer         = nullptr;
+// bool quitFlag           = false;
 
-bool init(){
-    //Initialization flag
-    bool success = true;
+int main(int argc, char *argv[]){
 
-    if( SDL_Init(SDL_INIT_VIDEO) != 0){
-       printf("Failed to initialize SDL. Error: %s\n", SDL_GetError() );
-       success = false;
+    Engine mainEngine;
+    if(mainEngine.startUp()){
+        mainEngine.mainLoop();
     }
     else{
-        //Window creation
-        gWindow = SDL_CreateWindow( "SDL2Test", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, 0);
-        if( gWindow == nullptr){
-            printf("Coult not create window. Error: %s\n", SDL_GetError() );
-            success = false;
-        }
-        else{
-            //Create render instance
-            gRenderer = SDL_CreateRenderer(gWindow, -1, SDL_RENDERER_PRESENTVSYNC);
-            if(gRenderer == nullptr){
-                printf("Could not create renderer context. Error: %s\n", SDL_GetError());
-                success = false;
-            }
-            else{
-
-                //Create Texture instance
-                gTexture = SDL_CreateTexture(gRenderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_STATIC, SCREEN_WIDTH, SCREEN_HEIGHT);
-                if(gTexture == nullptr){
-                    printf("Could not create texture. Error: %s\n", SDL_GetError());
-                    success = false;
-                }
-                else{
-                    //Create pixel buffer
-                    gBuffer = new Uint32[SCREEN_HEIGHT * SCREEN_WIDTH];
-                    SDL_memset(gBuffer, 0, SCREEN_WIDTH * SCREEN_HEIGHT * sizeof(Uint32) );
-                }
-            }
-        }
-
-    }
-
-    return success;
-}
-
-void close(){
-    delete[] gBuffer;
-    SDL_DestroyTexture(gTexture);
-    SDL_DestroyRenderer(gRenderer);
-    SDL_DestroyWindow(gWindow);
-    SDL_Quit();
-    gBuffer = nullptr;
-    gTexture = nullptr;
-    gRenderer = nullptr;
-    gWindow = nullptr;
-}
-
-void eventManager(){
-    SDL_Event test_event;
-    while(SDL_PollEvent(&test_event)){
-        if(test_event.type == SDL_QUIT){
-            quitFlag = true;
-        }
-    }
-}
-
-void modifyPixels(){
-    //Get window pixel format
-    Uint32 format = SDL_PIXELFORMAT_RGBA8888;
-    SDL_PixelFormat *mappingFormat = SDL_AllocFormat (format);
-
-    //Get pixel count
-    int pixelCount = SCREEN_HEIGHT * SCREEN_WIDTH;
-
-    //Set color data
-    Uint32 red1 = SDL_MapRGBA(mappingFormat, 0xFF,0x00,0x00,0x60);
-    Uint32 green = SDL_MapRGBA(mappingFormat, 0x00,0xFF,0x00,0x80);
-    Uint32 blue = SDL_MapRGBA(mappingFormat, 0x00,0x00,0xFF,0xFF);
-    //Color in certain pixels
-    for(int i = 0; i < pixelCount; ++i){
-        if( (i % 50) == 0){
-            gBuffer[i] = red1;
-        }
-        if((i % 1000) == 0){
-            gBuffer[i] = green;
-        }
-        if((i % 2000) == 0){
-            gBuffer[i] = blue;
-        }
-    }
-}
-
-// Uint32 getPixel(SDL_Surface &surface, int x, int y){
-//     Uint32 *pixels = (Uint32 *)gScreen->pixels;
-//     int arrayIndex = x + (gScreen->pitch * y);
-//     return  pixels[arrayIndex];
-// }
-
-// void setPixel(SDL_Surface &surface, int x, int y, Uint8 r, Uint8 g, Uint8 b, Uint8 a){
-//     Uint32 *pixels = (Uint32 *)gScreen->pixels;
-//     int arrayIndex = x + (gScreen->pitch * y);
-//     pixels[arrayIndex] == r;
-// }
-
-void render(){
-
-    //Clear Screen back to black
-    SDL_SetRenderDrawColor(gRenderer, 0x00, 0x00, 0x00, 0xFF);
-    SDL_RenderClear(gRenderer);
-    //Perform any modifications we want on the pixels
-    modifyPixels();
-
-    //Apply the pixel change to the texture
-    SDL_UpdateTexture(gTexture, NULL, gBuffer, SCREEN_WIDTH*sizeof(Uint32) );
-    
-    //Render texture to screen
-    SDL_SetTextureBlendMode(gTexture, SDL_BLENDMODE_BLEND);
-    SDL_RenderCopy(gRenderer, gTexture, NULL , NULL);
-
-    //Update screen to window
-    //SDL_SetRenderDrawBlendMode(gRenderer, SDL_BLENDMODE_BLEND);
-    SDL_RenderPresent( gRenderer );
-}
-
-void loop(){
-    while(!quitFlag){
-
-        //Manage Events
-        eventManager();
-
-        //Render pipeline
-        render();
-
-        SDL_Delay(500);
+        printf("Engine could not initialize successfully. Shutting down.\n");
     }
-}
-
-int main(int argc, char *argv[]){
-    //Init SDL stuff
-   if (!init()){
-       printf("Failed to initialize.\n");
-   }
-   else{
-       //Main loop
-        loop();
-   }
-   //Free all memory and set all pointers to null
-   close();
+    mainEngine.shutDown();
 
-   return 0;
+    return 0;
 }

+ 0 - 0
src/pixelPainter.cpp


+ 112 - 0
src/renderManager.cpp

@@ -0,0 +1,112 @@
+#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( !createBuffer() ){
+                printf("Could not build buffer.\n");
+                return false;
+            }
+            return true;
+        }
+    }
+}
+
+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::createBuffer(){
+    pixelCount = WindowManager::SCREEN_WIDTH * WindowManager::SCREEN_HEIGHT;
+    buffer1 = new Uint32[pixelCount];
+    SDL_memset(buffer1, 0, pixelCount * sizeof(Uint32) );
+    return buffer1 != 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[] buffer1;
+    buffer1 = nullptr;
+    screenTexture.free();
+    SDL_DestroyRenderer(mainRenderer);
+    mainRenderer = nullptr;
+}
+
+void RenderManager::render(){
+
+    //Clear Screen back to black
+    clearScreen();
+
+    //Perform any modifications we want on the pixels
+    createPixelPattern();
+
+    //Apply the pixel change to the texture
+    screenTexture.updateTexture(buffer1);
+
+    //Switch screen texture with back texture and re-draw
+    updateScreen();
+}
+
+void RenderManager::updateScreen(){
+    //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);
+}
+
+void RenderManager::createPixelPattern(){
+    //Get window pixel format
+    SDL_PixelFormat *mappingFormat = SDL_AllocFormat (PIXEL_FORMAT);
+
+    //Set color data
+    Uint32 red = SDL_MapRGBA(mappingFormat, 0xFF,0x00,0x00,0x60);
+    Uint32 green = SDL_MapRGBA(mappingFormat, 0x00,0xFF,0x00,0x80);
+    Uint32 blue = SDL_MapRGBA(mappingFormat, 0x00,0x00,0xFF,0xFF);
+    //Color in certain pixels
+    for(int i = 0; i < pixelCount; ++i){
+        if( (i % 50) == 0){
+            buffer1[i] = red;
+        }
+        if((i % 1000) == 0){
+            buffer1[i] = green;
+        }
+        if((i % 2000) == 0){
+            buffer1[i] = blue;
+        }
+    }
+}
+

+ 41 - 0
src/texture.cpp

@@ -0,0 +1,41 @@
+#include "texture.h"
+#include "windowManager.h"
+
+Texture::Texture(){
+    mTexture = NULL;
+    mWidth   = 0;
+    mHeight  = 0;
+    mPitch   = 0;
+}
+
+Texture::~Texture(){
+    free();
+}
+
+void Texture::free(){
+    if(mTexture != NULL){
+        SDL_DestroyTexture (mTexture);
+        mWidth  = 0;
+        mHeight = 0;
+        mPitch  = 0; 
+    }
+}
+
+bool Texture::createBlank(SDL_Renderer  *renderer, int width, int height ){
+    mWidth  = width;
+    mHeight = height;
+    mPitch  = width * sizeof(Uint32);
+    mTexture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888,
+        SDL_TEXTUREACCESS_STATIC, mWidth, mHeight);
+
+    return mTexture != NULL;
+}
+
+void Texture::updateTexture(Uint32 * pixels){
+    SDL_UpdateTexture(mTexture, NULL, pixels, mPitch);
+}
+
+void Texture::renderToScreen(SDL_Renderer * renderer){
+    SDL_SetTextureBlendMode(mTexture, SDL_BLENDMODE_BLEND);
+    SDL_RenderCopy(renderer, mTexture, NULL , NULL);
+}

+ 46 - 0
src/windowManager.cpp

@@ -0,0 +1,46 @@
+#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();
+}