Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //CPP files first...
- //WeekTwoWork.cpp is our main thingy thing:
- // WeekTwoWork.cpp : Defines the entry point for the application.
- //
- #include "stdafx.h"
- #include "WeekTwoWork.h"
- #include <Gdiplus.h>
- #include "AppEngine.h"
- #include "Rasterizer.h"
- #include "Vertex.h"
- using namespace Gdiplus;
- #pragma comment (lib, "Gdiplus.lib")
- #define MAX_LOADSTRING 100
- #define EXIT_SUCCESS 0
- #define WINDOW_WIDTH 640
- #define WINDOW_HEIGHT 480
- // Global Variables:
- HWND hWnd;
- HDC hdc;
- HINSTANCE hInst; // current instance
- TCHAR szTitle[MAX_LOADSTRING]; // The title bar text
- TCHAR szWindowClass[MAX_LOADSTRING]; // the main window class name
- AppEngine appEngine;
- // Forward declarations of functions included in this code module:
- ATOM MyRegisterClass(HINSTANCE hInstance);
- BOOL InitInstance(HINSTANCE, int);
- LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
- INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
- int APIENTRY _tWinMain(HINSTANCE hInstance,
- HINSTANCE hPrevInstance,
- LPTSTR lpCmdLine,
- int nCmdShow)
- {
- UNREFERENCED_PARAMETER(hPrevInstance);
- UNREFERENCED_PARAMETER(lpCmdLine);
- // TODO: Place code here.
- MSG msg;
- HACCEL hAccelTable;
- GdiplusStartupInput gdiplusStartupInput;
- ULONG_PTR gdiplusToken;
- GdiplusStartup (&gdiplusToken, &gdiplusStartupInput, NULL);
- // Initialize global strings
- LoadString (hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
- LoadString (hInstance, IDC_WEEKTWOWORK, szWindowClass, MAX_LOADSTRING);
- MyRegisterClass (hInstance);
- // Perform application initialization:
- if (!InitInstance (hInstance, nCmdShow))
- {
- return FALSE;
- }
- hAccelTable= LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_WEEKTWOWORK));
- appEngine.Initialise(hWnd, hdc);
- // Main message loop:
- while (true)
- {
- if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
- {
- if (msg.message==WM_QUIT)
- {
- break;
- }
- if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
- {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- }
- appEngine.Process();
- }
- appEngine.Shutdown();
- GdiplusShutdown (gdiplusToken);
- return (int) msg.wParam;
- }
- //
- // FUNCTION: MyRegisterClass()
- //
- // PURPOSE: Registers the window class.
- //
- // COMMENTS:
- //
- // This function and its usage are only necessary if you want this code
- // to be compatible with Win32 systems prior to the 'RegisterClassEx'
- // function that was added to Windows 95. It is important to call this function
- // so that the application will get 'well formed' small icons associated
- // with it.
- //
- ATOM MyRegisterClass(HINSTANCE hInstance)
- {
- WNDCLASSEX wcex;
- wcex.cbSize= sizeof(WNDCLASSEX);
- wcex.style= CS_HREDRAW | CS_VREDRAW;
- wcex.lpfnWndProc= WndProc;
- wcex.cbClsExtra= 0;
- wcex.cbWndExtra= 0;
- wcex.hInstance= hInstance;
- wcex.hIcon= LoadIcon(hInstance, MAKEINTRESOURCE(IDI_WEEKTWOWORK));
- wcex.hCursor= LoadCursor(NULL, IDC_ARROW);
- wcex.hbrBackground= (HBRUSH)(COLOR_WINDOW+1);
- wcex.lpszMenuName= MAKEINTRESOURCE(IDC_WEEKTWOWORK);
- wcex.lpszClassName= szWindowClass;
- wcex.hIconSm= LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
- return RegisterClassEx(&wcex);
- }
- //
- // FUNCTION: InitInstance(HINSTANCE, int)
- //
- // PURPOSE: Saves instance handle and creates main window
- //
- // COMMENTS:
- //
- // In this function, we save the instance handle in a global variable and
- // create and display the main program window.
- //
- BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
- {
- //HWND hWnd;
- hInst= hInstance; // Store instance handle in our global variable
- hWnd= CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
- CW_USEDEFAULT, CW_USEDEFAULT, WINDOW_WIDTH,
- WINDOW_HEIGHT, NULL, NULL, hInstance, NULL);
- if (!hWnd)
- {
- return FALSE;
- }
- ShowWindow(hWnd, nCmdShow);
- UpdateWindow(hWnd);
- return TRUE;
- }
- //
- // FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
- //
- // PURPOSE: Processes messages for the main window.
- //
- // WM_COMMAND - process the application menu
- // WM_PAINT - Paint the main window
- // WM_DESTROY - post a quit message and return
- //
- //
- LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
- {
- int wmId, wmEvent;
- PAINTSTRUCT ps;
- HDC hdc;
- switch (message)
- {
- case WM_COMMAND:
- wmId= LOWORD(wParam);
- wmEvent= HIWORD(wParam);
- // Parse the menu selections:
- switch (wmId)
- {
- case IDM_ABOUT:
- DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
- break;
- case IDM_EXIT:
- DestroyWindow(hWnd);
- break;
- default:
- return DefWindowProc(hWnd, message, wParam, lParam);
- }
- break;
- case WM_PAINT:
- hdc=BeginPaint(hWnd, &ps);
- appEngine.Paint(hdc);
- EndPaint(hWnd, &ps);
- break;
- case WM_DESTROY:
- PostQuitMessage(0);
- break;
- default:
- return DefWindowProc(hWnd, message, wParam, lParam);
- }
- return EXIT_SUCCESS;
- }
- // Message handler for about box.
- INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
- {
- UNREFERENCED_PARAMETER(lParam);
- switch (message)
- {
- case WM_INITDIALOG:
- return (INT_PTR)TRUE;
- case WM_COMMAND:
- if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
- {
- EndDialog(hDlg, LOWORD(wParam));
- return (INT_PTR)TRUE;
- }
- break;
- }
- return (INT_PTR)FALSE;
- }
- //Vertex.cpp next:
- #include "StdAfx.h"
- #include "Vertex.h"
- // Constructors
- Vertex::Vertex(void)
- {
- Init(0.0f, 0.0f, 0.0f, 0.0f);
- }
- Vertex::Vertex(float x, float y, float z, float w)
- {
- Init(x, y, z, w);
- }
- Vertex::Vertex(const Vertex& v)
- {
- Copy(v);
- }
- Vertex::~Vertex(void)
- {
- }
- // Accessors and mutators
- float Vertex::GetX(void) const
- {
- return _x;
- }
- void Vertex::SetX(float x)
- {
- _x=x;
- }
- float Vertex::GetY(void) const
- {
- return _y;
- }
- void Vertex::SetY(float y)
- {
- _y=y;
- }
- float Vertex::GetW(void) const
- {
- return _w;
- }
- void Vertex::SetW(float w)
- {
- _w=w;
- }
- float Vertex::GetZ(void) const
- {
- return _z;
- }
- void Vertex::SetZ(float z)
- {
- _z=z;
- }
- // Operator overloads
- Vertex& Vertex::operator=(const Vertex &rhs)
- {
- if (this!=&rhs)
- {
- // Only copy if we are not assigning to ourselves.
- // (remember that a = b is the same as a.operator=(b))
- Copy(rhs);
- }
- return *this;
- }
- const Vertex Vertex::operator+(const Vertex &other) const
- {
- Vertex result;
- result.SetX(_x+other.GetX());
- result.SetY(_y+other.GetY());
- result.SetZ(_z+other.GetZ());
- result.SetW(_w+other.GetW());
- return result;
- }
- const Vertex Vertex::operator-(const Vertex &other) const
- {
- Vertex result;
- result.SetX(_x-other.GetX());
- result.SetY(_y-other.GetY());
- result.SetZ(_z-other.GetZ());
- result.SetW(_w-other.GetW());
- return result;
- }
- // Private methods
- void Vertex::Init(float x, float y, float z, float w)
- {
- _x=x;
- _y=y;
- _z=z;
- _w=w;
- }
- void Vertex::Copy(const Vertex& v)
- {
- _x=v.GetX();
- _y=v.GetY();
- _z=v.GetZ();
- _w=v.GetW();
- }
- // And now the Rasterizer.cpp source:
- #include "StdAfx.h"
- #include "Rasterizer.h"
- Rasterizer::Rasterizer(void)
- {
- // Declared private so should not be called.
- _bitmap=NULL;
- _graphics=NULL;
- }
- Rasterizer::Rasterizer(unsigned int width, unsigned int height)
- {
- // Create a bitmap of the specified size and get a reference to the graphics object
- // for that bitmap. Note that these calls could theoretically fail so we really should
- // handle that, but we will leave that for now.
- _width=width;
- _height=height;
- _bitmap=new Bitmap(_width, _height, PixelFormat32bppARGB);
- _graphics=new Graphics(_bitmap);
- }
- Rasterizer::~Rasterizer(void)
- {
- // Clean up all dynamically created objects
- if (_graphics)
- {
- delete _graphics;
- _graphics=NULL;
- }
- if (_bitmap)
- {
- delete _bitmap;
- _bitmap=NULL;
- }
- }
- // Accessors
- unsigned int Rasterizer::GetWidth() const
- {
- return _width;
- }
- unsigned int Rasterizer::GetHeight() const
- {
- return _height;
- }
- Bitmap* Rasterizer::GetBitmap() const
- {
- return _bitmap;
- }
- // Clear the bitmap using the specified colour
- void Rasterizer::Clear(const Color& color)
- {
- if (_graphics)
- {
- _graphics->Clear(color);
- }
- }
- void Rasterizer::DrawSquare(Vertex square[4], bool Update, HWND _hWnd)
- {
- HWND hWnd;
- hWnd=_hWnd;
- Pen pen(Color(255,12,48,150));
- for (int i=0; i<4; i=i+1)
- {
- if (i+1==4)
- {
- _graphics->DrawLine(&pen, square[i].GetX(), square[i].GetY(), square[0].GetX(), square[0].GetY());
- }
- else
- _graphics->DrawLine(&pen, square[i].GetX(), square[i].GetY(), square[i+1].GetX(), square[i+1].GetY());
- }
- if(!Update)
- {
- return;
- }
- else
- InvalidateRect(_hWnd, NULL, FALSE);
- /**else
- UpdateWindow(hWnd);*/
- //_graphics->DrawLine(&pen, square[0].GetX(), square[0].GetY(), square[1].GetX(), square[1].GetY());
- //_graphics->DrawLine(&pen, square[1].GetX(), square[1].GetY(), square[2].GetX(), square[2].GetY());
- //_graphics->DrawLine(&pen, square[2].GetX(), square[2].GetY(), square[3].GetX(), square[3].GetY());
- //_graphics->DrawLine(&pen, square[3].GetX(), square[3].GetY(), square[0].GetX(), square[0].GetY());
- }
- void Rasterizer::LineTo(int col[4],int x0, int y0, int x1, int y1, bool Update, HWND _hWnd)
- {
- HWND hWnd;
- hWnd= _hWnd;
- Pen pen(Color(col[0],col[1],col[2],col[3]));
- _graphics->DrawLine(&pen, x0, y0, x1, y1);
- if(!Update)
- {
- return;
- }
- else
- InvalidateRect(_hWnd, NULL, FALSE);
- //InvalidateRect(hWnd, NULL, FALSE);
- //UpdateWindow(hWnd);
- }
- // Here's our Matrix2D.cpp file:
- #include "StdAfx.h"
- #include "Matrix2D.h"
- #define WIDTH 4
- #define HEIGHT 4
- //Constructors
- Matrix2D::Matrix2D(void)
- {
- float matrix[WIDTH][HEIGHT];
- for (int i=0; i<WIDTH; i=i+1)
- {
- for (int j=0; j<HEIGHT; j=j+1)
- {
- matrix[i][j]=0.00f;
- }
- }
- Init(matrix);
- }
- Matrix2D::Matrix2D(float matrix[][WIDTH])
- {
- Init(matrix);
- }
- Matrix2D::Matrix2D(const Matrix2D& m)
- {
- Copy(m);
- }
- Matrix2D::~Matrix2D(void)
- {
- }
- //Accessors and mutators
- float Matrix2D::GetM(int x, int y) const
- {
- return _matrix[x][y];
- }
- void Matrix2D::SetM(int x, int y, float f)
- {
- for (int i=0; i<WIDTH; i=i+1)
- {
- for (int j=0; j<HEIGHT; j=j+1)
- {
- _matrix[x][y]=f;
- }
- }
- }
- //Operators
- const Matrix2D Matrix2D::operator*(const Matrix2D &other) const
- {
- Matrix2D result;
- float sum;
- for (int i=0; i<4; i=i+1)
- {
- for (int j=0; j<4; j=j+1)
- {
- sum=0;
- for (int k=0; k<4; k=k+1)
- {
- sum=sum+_matrix[i][k]*other.GetM(k,j);
- }
- result.SetM(i,j,sum);
- }
- }
- return result;
- }
- const Vertex Matrix2D::operator*(const Vertex &p) const
- {
- Vertex result;
- result.SetX(((_matrix[0][0]*p.GetX())+(_matrix[1][0]*p.GetY())+(_matrix[2][0]*p.GetZ())+(_matrix[3][0]*p.GetW())));
- result.SetY(((_matrix[0][1]*p.GetX())+(_matrix[1][1]*p.GetY())+(_matrix[2][1]*p.GetZ())+(_matrix[3][1]*p.GetW())));
- result.SetZ(((_matrix[0][2]*p.GetX())+(_matrix[1][2]*p.GetY())+(_matrix[2][2]*p.GetZ())+(_matrix[3][2]*p.GetW())));
- result.SetW(((_matrix[0][3]*p.GetX())+(_matrix[1][3]*p.GetY())+(_matrix[2][3]*p.GetZ())+(_matrix[3][3]*p.GetW())));
- return result;
- }
- //Private Methods
- void Matrix2D::Init(float matrix[][WIDTH])
- {
- for (int i=0; i<HEIGHT; i=i+1)
- {
- for (int j=0; j<WIDTH; j=j+1)
- {
- _matrix[i][j]=matrix[i][j];
- }
- }
- }
- void Matrix2D::Copy(const Matrix2D& m)
- {
- for (int i=0;i<HEIGHT;i=i+1)
- {
- for (int j=0;j<WIDTH;j=j+1)
- {
- _matrix[j][i]=m.GetM(j,i);
- }
- }
- }
- // And finally our AppEngine.cpp:
- #include "StdAfx.h"
- #include "AppEngine.h"
- #include "Rasterizer.h"
- #include <cmath>
- #include <math.h>
- #include "Matrix2D.h"
- #define PI 3.14159265
- // Globals here, innit:
- float rads=PI/180; // Work out our radians
- float angle=2.00f*rads; // Set a default value for our angle
- RECT clientWindowRect;
- float centreWidth,
- centreHeight; // For centre point of canvas
- // Main 'engine' class for our application. This is where all of the core processing work is done
- AppEngine::AppEngine(void)
- {
- _rasterizer=NULL;
- }
- AppEngine::~AppEngine(void)
- {
- // Make sure everything is cleaned up (in case Shutdown() was now called directly)
- Shutdown();
- }
- void AppEngine::Initialise(HWND hWnd, HDC hdc)
- {
- _hdc=hdc;
- _hWnd=hWnd;
- // Get the width and height of the window so that we can tell the rasterizer to create a bitmap
- // of the same size. Note that this only works for the initial size of the window. If you want
- // your code to handle resizing of the window, you will need to add code for this yourself.
- long int width;
- long int height;
- RECT clientWindowRect;
- if (GetClientRect(_hWnd, &clientWindowRect))
- {
- width=clientWindowRect.right;
- height=clientWindowRect.bottom;
- }
- else
- {
- width=640;
- height=480;
- }
- // This will therefore define our relative centre point:
- centreWidth=width/2.00f;
- centreHeight=height/2.00f;
- _rasterizer=new Rasterizer((unsigned int)width,(unsigned int)height);
- }
- void AppEngine::Process(void)
- {
- // At the moment, all we do is render a new image. In the future, we might do more here
- Render();
- }
- void AppEngine::Render(void)
- {
- // Default colour, with full alpha (non-transparent):
- _rasterizer->Clear(Color(255,255,255,255));
- // This will define the length and height of our object:
- float sqWid=30.00f,
- sqHei=15.00f;
- // Okay, so if we want the object in the centre of the
- // window, we could do:
- float topLeftX= centreWidth-sqWid;
- float topLeftY= centreHeight-sqHei;
- // Now we can work out the relative centre of your object
- // assuming that it's a square, by:
- sqWid=sqWid*2; sqHei=sqHei*2; // Can you see what I did there?
- float centreP= (topLeftX*0.25f)+
- ((topLeftX+sqWid)*0.25f)+
- (topLeftY*0.25f)+
- ((topLeftY+sqHei)*0.25f);
- float Y= centreP;
- Vertex square [4];
- square[0]= Vertex(topLeftX,topLeftY,0,1);
- square[1]= Vertex(topLeftX+sqWid,topLeftY,0,1);
- square[2]= Vertex(topLeftX+sqWid,topLeftY+sqHei,0,1);
- square[3]= Vertex(topLeftX,topLeftY+sqHei,0,1);
- Vertex point;
- point= square[0];
- // Let's do this:
- _rasterizer->DrawSquare(square, true, _hWnd);
- // Identity matrix:
- float matrixID[4][4];
- matrixID[0][0]=1.00f; matrixID[0][1]=0.00f; matrixID[0][2]=0.00f; matrixID[0][3]=0.00f;
- matrixID[1][0]=0.00f; matrixID[1][1]=1.00f; matrixID[1][2]=0.00f; matrixID[1][3]=0.00f;
- matrixID[2][0]=0.00f; matrixID[1][1]=0.00f; matrixID[1][2]=1.00f; matrixID[1][3]=0.00f;
- matrixID[3][0]=0.00f; matrixID[3][1]=0.00f; matrixID[3][2]=0.00f; matrixID[3][3]=1.00f;
- Matrix2D _matrixID(matrixID);
- // Y rotation, also called heading or pan, "Y" is the rotation value:
- float matrixYPan[4][4];
- matrixYPan[0][0]=cos(Y); matrixYPan[0][1]=0.00f; matrixYPan[0][2]=sin(Y); matrixYPan[0][3]=0.00f;
- matrixYPan[1][0]=0.00f; matrixYPan[1][1]=1.00f; matrixYPan[1][2]=0.00f; matrixYPan[1][3]=0.00f;
- matrixYPan[2][0]=-sin(Y); matrixYPan[1][1]=0.00f; matrixYPan[1][2]=cos(Y); matrixYPan[1][3]=0.00f;
- matrixYPan[3][0]=0.00f; matrixYPan[3][1]=0.00f; matrixYPan[3][2]=0.00f; matrixYPan[3][3]=1.00f;
- Matrix2D _matrixYPan(matrixYPan);
- // Basic rotate matrix:
- float matrixRotate[4][4];
- matrixRotate[0][0]=cos(0.1f); matrixRotate[1][0]=-sin(0.1f); matrixRotate[2][0]=0.00f; matrixRotate[3][0]=0.00f;
- matrixRotate[0][1]=sin(0.1f); matrixRotate[1][1]=cos(0.1f); matrixRotate[2][1]=0.00f; matrixRotate[2][1]=0.00f;
- matrixRotate[0][2]=0.00f; matrixRotate[1][2]=0.00f; matrixRotate[2][2]=1.00f; matrixRotate[2][2]=0.00f;
- matrixRotate[0][3]=0.00f; matrixRotate[1][3]=0.00f; matrixRotate[2][3]=0.00f; matrixRotate[3][3]=1.00f;
- Matrix2D _matrixRotate(matrixRotate);
- float matrixScale[4][4];
- matrixScale[0][0]=1.10f; matrixScale[1][0]=0.00f; matrixScale[2][0]=0.00f; matrixScale[3][0]=0.00f;
- matrixScale[0][1]=0.00f; matrixScale[1][1]=1.10f; matrixScale[2][1]=0.00f; matrixScale[3][1]=0.00f;
- matrixScale[0][2]=0.00f; matrixScale[1][2]=0.00f; matrixScale[2][2]=0.00f; matrixScale[3][2]=0.00f;
- matrixScale[0][3]=0.00f; matrixScale[1][3]=0.00f; matrixScale[2][3]=0.00f; matrixScale[3][3]=1.00f;
- Matrix2D _matrixScale(matrixScale);
- Matrix2D scaleAndRotateMatirix=_matrixScale*_matrixRotate;
- // Yay for Mathematics!
- for (int j=0; j<360;j=j+1) // j=j+1 is more like the Mathematics I'm all about!
- {
- for (int i=0; i<4; i=i+1)
- {
- square[i]=square[i]-point;
- square[i]=_matrixRotate*square[i];
- //square[i]=scaleAndRotateMatirix*square[i];
- square[i]=square[i]+point;
- }
- _rasterizer->DrawSquare(square, true, _hWnd); //draw scaled square
- //Paint (_hdc);
- }
- int blue[4]={255, 0, 0, 255};
- int red [4]={255, 255, 0, 0};
- int y = 256;
- for (int x=0; x<256; x=x+5)
- {
- _rasterizer->LineTo(blue, 0, y, x, 0, true, _hWnd);
- _rasterizer->LineTo(red, 256, x, y, 256, true, _hWnd);
- y=y-5;
- Paint (_hdc);
- ReleaseDC(_hWnd, _hdc);
- //Paint (_hdc);
- }
- // Ensure that Windows is told that the contents of the window now needs painting
- }
- void AppEngine::Paint(HDC hdc)
- {
- // Copy the contents of the rasterizer's bitmap to our window
- if (_rasterizer)
- {
- Graphics graphics(hdc);
- graphics.DrawImage(_rasterizer->GetBitmap(), 0, 0, _rasterizer->GetWidth(), _rasterizer->GetHeight());
- }
- }
- void AppEngine::Shutdown(void)
- {
- // Clean up all memory that has been dynamically allocated
- if (_rasterizer)
- {
- delete _rasterizer;
- _rasterizer = NULL;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement