Created
October 26, 2012 16:42
-
-
Save Gi133/3959820 to your computer and use it in GitHub Desktop.
Graphics Programming Coursework Semester 1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include "Controller.h" | |
| Controller::Controller(void) | |
| { | |
| bool keys=false; | |
| } | |
| Controller::~Controller(void) | |
| { | |
| } | |
| void Controller::CalculateDistance() | |
| { | |
| //Find the distance traveled on each axis | |
| distance_x = MousePos.end_x - MousePos.start_x; | |
| distance_y = MousePos.end_y - MousePos.start_y; | |
| //Find total Distance | |
| total_distance = sqrt((distance_x * distance_x) + (distance_y * distance_y)); | |
| } | |
| void Controller::UpdateMessages(UINT message, WPARAM wParam, LPARAM lParam) | |
| { | |
| switch(message) | |
| { | |
| case WM_KEYDOWN: | |
| keys[wParam]=true; | |
| break; | |
| case WM_KEYUP: | |
| keys[wParam]=false; | |
| break; | |
| case WM_MOUSEMOVE: | |
| MousePos.x = LOWORD (lParam); | |
| MousePos.y = HIWORD (lParam); | |
| break; | |
| case WM_LBUTTONDOWN: | |
| MousePos.start_x = MousePos.x; | |
| MousePos.start_y = MousePos.y; | |
| break; | |
| case WM_LBUTTONUP: | |
| MousePos.end_x = MousePos.x; | |
| MousePos.end_y = MousePos.y; | |
| CalculateDistance(); | |
| break; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #pragma once | |
| #include <stdio.h> | |
| #include <Windows.h> | |
| #include <math.h> | |
| class Controller | |
| { | |
| private: | |
| typedef struct Mouse | |
| { | |
| int x, y, end_x ,end_y, start_x, start_y; | |
| }Mouse; | |
| void CalculateDistance(); | |
| public: | |
| Controller(void); | |
| ~Controller(void); | |
| bool keys[256]; //Keyboard Buttons | |
| Mouse MousePos; | |
| float total_distance, distance_x, distance_y; // Total Distance Covered by the mouse cursor. | |
| void UpdateMessages(UINT message, WPARAM wParam, LPARAM lParam); | |
| char position[20]; | |
| void PrintDistance(); | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include "Enemy.h" | |
| Enemy::Enemy(void) | |
| { | |
| } | |
| Enemy::~Enemy(void) | |
| { | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #pragma once | |
| #include "Entity.h" | |
| class Enemy : | |
| public Entity | |
| { | |
| public: | |
| Enemy(void); | |
| ~Enemy(void); | |
| }; | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include "Enemy.h" | |
| Enemy::Enemy(void) | |
| { | |
| } | |
| Enemy::~Enemy(void) | |
| { | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #pragma once | |
| #include "Sprite.h" | |
| class Entity : | |
| public Sprite | |
| { | |
| private: | |
| int health; | |
| int damage; | |
| int speed; | |
| int level; | |
| bool dead; | |
| public: | |
| Entity(void); | |
| ~Entity(void); | |
| }; | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <windows.h> | |
| #include <stdio.h> | |
| #include <mmsystem.h> | |
| #include <math.h> | |
| #include "Controller.h" | |
| #include "Sprite.h" | |
| #include "Entity.h" | |
| #include "Enemy.h" | |
| #include "Player.h" | |
| int xpos, ypos; | |
| int ticker =0; | |
| Controller CONTROLS; | |
| bool started; | |
| HBITMAP theOldFrontBitMap, theOldBackBitMap; | |
| HWND ghwnd; | |
| RECT screenRect; | |
| HDC backHDC, frontHDC, bitmapHDC; //Hardware device contexts for the Buffers | |
| Sprite* SplashScreen = new Sprite ("SplashScreen"); | |
| Sprite* TestSprite = new Sprite(70, 70,"Enemy 1"); | |
| Sprite* TestSprite2 = new Sprite("Enemy 2"); | |
| BOOL waitFor(unsigned long delay); | |
| LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM); | |
| void RegisterMyWindow(HINSTANCE hInstance) | |
| { | |
| WNDCLASSEX wcex; | |
| wcex.cbSize = sizeof (wcex); | |
| wcex.style = CS_HREDRAW | CS_VREDRAW; | |
| wcex.lpfnWndProc = WndProc; | |
| wcex.cbClsExtra = 0; | |
| wcex.cbWndExtra = 0; | |
| wcex.hInstance = hInstance; | |
| wcex.hIcon = 0; | |
| wcex.hCursor = LoadCursor (NULL, IDC_ARROW); | |
| wcex.hbrBackground = (HBRUSH) (COLOR_WINDOW+1); | |
| wcex.lpszMenuName = NULL; | |
| wcex.lpszClassName = "FirstGame"; | |
| wcex.hIconSm = 0; | |
| RegisterClassEx (&wcex); | |
| } | |
| BOOL InitialiseMyWindow(HINSTANCE hInstance, int nCmdShow) | |
| { | |
| HWND hwnd; | |
| hwnd = CreateWindow ("FirstGame", | |
| "My kickass game!", | |
| WS_OVERLAPPED | WS_MINIMIZEBOX | WS_SYSMENU, | |
| CW_USEDEFAULT, | |
| CW_USEDEFAULT, | |
| 800, | |
| 600, | |
| NULL, | |
| NULL, | |
| hInstance, | |
| NULL); | |
| if (!hwnd) | |
| { | |
| return FALSE; | |
| } | |
| ShowWindow (hwnd, nCmdShow); | |
| UpdateWindow (hwnd); | |
| ghwnd = hwnd; | |
| return TRUE; | |
| } | |
| BOOL WaitFor(unsigned long delay) | |
| { | |
| static unsigned long clockStart = 0; | |
| unsigned long timePassed; | |
| unsigned long now = timeGetTime(); | |
| timePassed = now - clockStart; | |
| if (timePassed > delay) | |
| { | |
| clockStart = now; | |
| return TRUE; | |
| } | |
| else | |
| return FALSE; | |
| } | |
| LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) | |
| { | |
| CONTROLS.UpdateMessages(message, wParam, lParam); | |
| switch (message) | |
| { | |
| case WM_CREATE: | |
| break; | |
| case WM_SIZE: | |
| break; | |
| case WM_PAINT: | |
| break; | |
| case WM_DESTROY: | |
| PostQuitMessage(0); | |
| break; | |
| } | |
| return DefWindowProc (hwnd, message, wParam, lParam); | |
| } | |
| void setBuffers() | |
| { | |
| GetClientRect(ghwnd, &screenRect); //creates rect based on window client area | |
| frontHDC = GetDC(ghwnd); // Initialises front buffer device context (window) | |
| backHDC = CreateCompatibleDC(frontHDC);// sets up Back DC to be compatible with the front | |
| bitmapHDC=CreateCompatibleDC(backHDC); | |
| theOldFrontBitMap = CreateCompatibleBitmap(frontHDC, screenRect.right, | |
| screenRect.bottom); //creates bitmap compatible with the front buffer | |
| theOldBackBitMap = (HBITMAP)SelectObject(backHDC, theOldFrontBitMap); | |
| //creates bitmap compatible with the back buffer | |
| FillRect(backHDC, &screenRect, (HBRUSH)GetStockObject(0)); | |
| } | |
| void displayFrame() | |
| { | |
| BitBlt(frontHDC, screenRect.left,screenRect.top, screenRect.right, | |
| screenRect.bottom, backHDC, 0, 0, SRCCOPY); | |
| FillRect(backHDC, &screenRect, (HBRUSH)GetStockObject(0)); | |
| } | |
| void releaseResources() | |
| { | |
| SelectObject(backHDC,theOldBackBitMap); | |
| DeleteDC(backHDC); | |
| DeleteDC(bitmapHDC); | |
| ReleaseDC(ghwnd,frontHDC); | |
| } | |
| int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, | |
| PSTR szCmdLine, int nCmdShow) | |
| { | |
| MSG msg; | |
| HDC hdcWindow; | |
| started = false; | |
| SplashScreen->LoadBitmap("splash.bmp", true); | |
| TestSprite->LoadBitmap("icon.bmp", true); | |
| TestSprite2->LoadBitmap("icon2.bmp", true); | |
| RegisterMyWindow(hInstance); | |
| if (!InitialiseMyWindow(hInstance, nCmdShow)) | |
| return FALSE; | |
| setBuffers(); | |
| while (TRUE) | |
| { | |
| if (PeekMessage(&msg,NULL,0,0,PM_REMOVE)) | |
| { | |
| if (msg.message==WM_QUIT) | |
| break; | |
| TranslateMessage (&msg); | |
| DispatchMessage (&msg); | |
| } | |
| else | |
| { | |
| //Check Game State | |
| if (!started) | |
| { | |
| SplashScreen->drawSprite(bitmapHDC, backHDC); | |
| displayFrame(); | |
| if (CONTROLS.keys[VK_SPACE]) | |
| { | |
| started = true; | |
| delete SplashScreen; | |
| } | |
| } | |
| //Game shizzle | |
| else | |
| { | |
| if (WaitFor(1)) | |
| { | |
| if (CONTROLS.keys[VK_UP]) | |
| TestSprite->setDeltaY(-1); | |
| if (CONTROLS.keys[VK_DOWN]) | |
| TestSprite->setDeltaY(1); | |
| if (CONTROLS.keys[VK_RIGHT]) | |
| TestSprite->setDeltaX(1); | |
| if (CONTROLS.keys[VK_LEFT]) | |
| TestSprite->setDeltaX(-1); | |
| TestSprite2->drawSprite(bitmapHDC, backHDC); | |
| TestSprite->drawSprite(bitmapHDC, backHDC); | |
| displayFrame(); | |
| } | |
| } | |
| } | |
| } | |
| releaseResources(); | |
| return msg.wParam ; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #pragma once | |
| #include "Entity.h" | |
| class Player : | |
| public Entity | |
| { | |
| public: | |
| Player(void); | |
| ~Player(void); | |
| }; | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include "Sprite.h" | |
| Sprite::Sprite(int starting_x, int starting_y, char ObjectName[20]) | |
| { | |
| x = starting_x; | |
| y = starting_y; | |
| sprintf (Name, ObjectName); | |
| } | |
| Sprite::Sprite(char ObjectName[20]) | |
| { | |
| x = 0; | |
| y = 0; | |
| sprintf (Name, ObjectName); | |
| } | |
| Sprite::Sprite(int starting_x, int starting_y) | |
| { | |
| x = starting_x; | |
| y = starting_y; | |
| sprintf(Name, "Unassigned"); | |
| } | |
| Sprite::Sprite() | |
| { | |
| x = 0; | |
| y = 0; | |
| sprintf(Name, "Unassigned"); | |
| } | |
| Sprite::~Sprite(void) | |
| { | |
| } | |
| int Sprite::getX() {return x;} | |
| int Sprite::getY() {return y;} | |
| int Sprite::getWidth() {return width;} | |
| int Sprite::getHeight() {return height;} | |
| void Sprite::setX(int new_x) {x = new_x;} | |
| void Sprite::setDeltaX(int delta_x) {x += delta_x;} | |
| void Sprite::setY(int new_y) {y = new_y;} | |
| void Sprite::setDeltaY(int delta_y) {y += delta_y;} | |
| HBITMAP Sprite::LoadABitmap(LPSTR szFileName) | |
| { | |
| return (HBITMAP)LoadImage(NULL, szFileName, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE); | |
| } | |
| void Sprite::LoadBitmap(LPSTR szFileName, bool bAutoResize) | |
| { | |
| bitmap = LoadABitmap(szFileName); | |
| if (bAutoResize) | |
| AutoResize(); | |
| if (!TestBitmap()) | |
| ErrorMessage(1); //If bitmap not loaded, | |
| //send error message 1 to the error message function to display a message box. | |
| } | |
| void Sprite::AutoResize() | |
| { | |
| BITMAP bmap; | |
| GetObject(bitmap, sizeof(bmap), &bmap); | |
| height = bmap.bmHeight; | |
| width = bmap.bmWidth; | |
| } | |
| void Sprite::drawSprite(HDC bitmapHDC,HDC backHDC) | |
| { | |
| HBITMAP originalBitMap; | |
| originalBitMap = (HBITMAP)SelectObject(bitmapHDC,bitmap); | |
| BitBlt(backHDC, x, y, x+width, y+height, bitmapHDC, 0, 0, SRCCOPY); | |
| SelectObject(bitmapHDC,originalBitMap); | |
| } | |
| bool Sprite::TestBitmap() | |
| { | |
| if (!bitmap) | |
| return 0; | |
| else return 1; | |
| } | |
| void Sprite::ErrorMessage(int ErrorCode) | |
| { | |
| char errormsg[40]; | |
| switch (ErrorCode) | |
| { | |
| case 1: | |
| sprintf(errormsg, "Failed to load image %s", Name); | |
| } | |
| MessageBox(NULL, errormsg, "Sprite Error!",MB_OK); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #pragma once | |
| #include <Windows.h> | |
| #include <string> | |
| class Sprite | |
| { | |
| private: | |
| //Functions | |
| HBITMAP LoadABitmap(LPSTR szFileName); | |
| HBITMAP bitmap; | |
| void AutoResize(); | |
| //Variables | |
| int x, y; | |
| int height, width; | |
| char Name[20]; | |
| public: | |
| Sprite(int starting_x, int starting_y, char Name[]); | |
| Sprite(char Name[]); | |
| Sprite(int starting_x, int starting_y); | |
| Sprite(); | |
| ~Sprite(void); | |
| void LoadBitmap(LPSTR TargetIcon, bool bAutoResizeImage = false); | |
| void drawSprite(HDC bitmapHDC,HDC backHDC); | |
| bool TestBitmap(); //Check if a bitmap has been loaded or not, if not return 0. | |
| void ErrorMessage(int errorcode); //See below for description. | |
| //Getters | |
| int getX(); | |
| int getY(); | |
| int getWidth(); | |
| int getHeight(); | |
| //Mutators | |
| void setX(int new_x); //Set a new x coord | |
| void setDeltaX(int delta_x); //Move along X axis | |
| void setY(int new_y); //Set a new y coord | |
| void setDeltaY(int delta_y); //Move along y axis | |
| }; | |
| /* Error codes: | |
| 1 : Failed to load image file. | |
| */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment