Răsfoiți Sursa

Cleaned up code a bit

angel 7 ani în urmă
părinte
comite
d651e1f957

+ 18 - 0
include/canvas.h

@@ -0,0 +1,18 @@
+#ifndef CANVAS_H
+#define CANVAS_H
+
+    #include "SDL.h"
+
+    struct Canvas{
+            Canvas(int w, int h, int px, int pi, Uint32 * buf) 
+                : mWidth(w), mHeight(h), mPixelCount(px), mPitch(pi),mBuffer(buf) 
+            {
+            }
+            int mWidth;
+            int mHeight;
+            int mPixelCount;
+            int mPitch;
+            Uint32 *mBuffer; 
+    };
+
+#endif

+ 0 - 0
include/pixelPainter.h


+ 25 - 0
include/rasterizer.h

@@ -0,0 +1,25 @@
+#ifndef RASTERIZER_H
+#define RASTERIZER_H
+
+#include "SDL.h"
+#include "canvas.h"
+
+class Rasterizer{
+
+    public:
+        static const Uint32 PIXEL_FORMAT = SDL_PIXELFORMAT_RGBA8888;
+        Rasterizer(Canvas *canvas) :mCanvas(canvas){}
+
+        Uint32 getPixelColor(int x, int y);
+
+        void setPixelColor(Uint32 color, int x, int y);
+
+        void makeCoolPattern();
+
+    private:
+        int convertCoordinates(int x, int y);
+        Canvas * mCanvas;
+
+};
+
+#endif

+ 6 - 7
include/renderManager.h

@@ -4,11 +4,12 @@
 #include "SDL.h"
 #include "windowManager.h"
 #include "texture.h"
+#include "rasterizer.h"
+#include "canvas.h"
 
 class RenderManager{
 
     public:
-        static const Uint32 PIXEL_FORMAT = SDL_PIXELFORMAT_RGBA8888;
         RenderManager();
 
         ~RenderManager();
@@ -23,19 +24,17 @@ class RenderManager{
 
         void updateScreen();
 
-        bool createBuffer();
+        bool createCanvas();
 
         void render();
 
-        void createPixelPattern();
-
         void shutDown();
 
     private:
-        int pixelCount;
-        Uint32 * buffer1;
-        SDL_Renderer  *mainRenderer;
+        SDL_Renderer *mainRenderer;
         Texture screenTexture;
+        Canvas *mainCanvas;
+        Rasterizer *raster;
     
 };
 

+ 1 - 1
src/engine.cpp

@@ -46,7 +46,7 @@ void Engine::mainLoop(){
     printf("Entered Main Loop!\n");
     
     while(!done){
-        count++;
+        ++count;
 
         //Handle all user input
         done = FEInputManager.processInput();

+ 0 - 0
src/pixelPainter.cpp


+ 27 - 0
src/rasterizer.cpp

@@ -0,0 +1,27 @@
+#include "rasterizer.h"
+
+void Rasterizer::makeCoolPattern(){
+    SDL_PixelFormat *mappingFormat = SDL_AllocFormat (PIXEL_FORMAT);
+    Uint32 red = SDL_MapRGBA(mappingFormat, 0xFF,0x00,0x00,0xFF);
+    Uint32 green = SDL_MapRGBA(mappingFormat, 0x00,0xFF,0x00,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){
+            Uint8 r = x*y % 256;
+            Uint8 g = y % 256;
+            Uint8 b = x % 256;
+            Uint32 color = SDL_MapRGBA(mappingFormat, r,g,b,0xFF);
+            setPixelColor(color, x, y);
+        }
+    }
+}
+
+void Rasterizer::setPixelColor(Uint32 color, int x, int y){
+    int arrayCoordinates = convertCoordinates(x,y);
+    mCanvas->mBuffer[arrayCoordinates] = color;
+}
+
+int Rasterizer::convertCoordinates(int x, int y){
+    return ((y * mCanvas->mWidth) + x);
+}

+ 39 - 32
src/renderManager.cpp

@@ -19,10 +19,12 @@ bool RenderManager::startUp(WindowManager WindowManager){
             return false;
         }
         else{
-            if( !createBuffer() ){
-                printf("Could not build buffer.\n");
+            if( !createCanvas() ){
+                printf("Could not build canvas.\n");
                 return false;
             }
+            //Create rasterizer to begin drawing
+            raster = new Rasterizer(mainCanvas);
             return true;
         }
     }
@@ -36,11 +38,15 @@ bool RenderManager::createScreenTexture(){
     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::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]);
+    SDL_memset(mainCanvas->mBuffer, 0, mainCanvas->mPixelCount * sizeof(Uint32) );
+    return mainCanvas != NULL;
 }
 
 bool RenderManager::createRenderer(SDL_Window *window){
@@ -53,8 +59,9 @@ bool RenderManager::createRenderer(SDL_Window *window){
 }
 
 void RenderManager::shutDown(){
-    delete[] buffer1;
-    buffer1 = nullptr;
+    delete raster;
+    delete mainCanvas->mBuffer;
+    delete mainCanvas;
     screenTexture.free();
     SDL_DestroyRenderer(mainRenderer);
     mainRenderer = nullptr;
@@ -66,10 +73,10 @@ void RenderManager::render(){
     clearScreen();
 
     //Perform any modifications we want on the pixels
-    createPixelPattern();
+    raster->makeCoolPattern();
 
     //Apply the pixel change to the texture
-    screenTexture.updateTexture(buffer1);
+    screenTexture.updateTexture(mainCanvas->mBuffer);
 
     //Switch screen texture with back texture and re-draw
     updateScreen();
@@ -88,25 +95,25 @@ void RenderManager::clearScreen(){
     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;
-        }
-    }
-}
+// 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;
+//         }
+//     }
+// }