Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define WIN32_LEAN_AND_MEAN
- #define INITGUID
- #include <windows.h>
- #include <windowsx.h>
- #include <mmsystem.h>
- #include <iostream>
- #include <stdio.h>
- #include "MyMacros.h"
- #include <conio.h>
- #include <stdlib.h>
- #include <malloc.h>
- #include <memory.h>
- #include <string.h>
- #include <stdarg.h>
- #include <math.h>
- #include <io.h>
- #include <fcntl.h>
- #include <ddraw.h>
- /* Declare Windows procedure */
- LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
- // Prototypes /////////////////////////////////////////////
- int Game_Init(void *parms = NULL, int num_parms = 0);
- int Game_Main(void *parms = NULL, int num_parms = 0);
- int Game_Shutdown(void *parms = NULL, int num_parms = 0);
- /* Make the class name into a global variable */
- char szClassName[ ] = "Window_Class_One";
- // Globals ////////////////////////////////////////////////
- HWND main_window_handle = NULL;
- HINSTANCE app_instance = NULL;
- LPDIRECTDRAW7 lpdd7 = NULL;
- LPDIRECTDRAWPALETTE lpdd7pal = NULL;
- // palette
- PALETTEENTRY palette[256];
- // DirectDraw Surface descriptions
- DDSURFACEDESC2 ddsd;
- // DirectDraw surface handle
- LPDIRECTDRAWSURFACE7 lpddsprimary;
- int WINAPI WinMain (HINSTANCE hinstance,
- HINSTANCE hPrevInstance,
- LPSTR lpszArgument,
- int nCmdShow)
- {
- HWND hwnd; /* This is the handle for our window */
- MSG msg; /* Here messages to the application are saved */
- WNDCLASSEX wincl; /* Data structure for the windowclass */
- /* The Window structure */
- wincl.hInstance = hinstance;
- wincl.lpszClassName = szClassName;
- wincl.lpfnWndProc = WindowProcedure; /* This function is called by windows */
- wincl.style = CS_OWNDC| CS_HREDRAW| CS_VREDRAW | CS_DBLCLKS; /* Catch double-clicks */
- wincl.cbSize = sizeof (WNDCLASSEX);
- /* Use default icon and mouse-pointer */
- wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
- wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
- wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
- wincl.lpszMenuName = NULL; /* No menu */
- wincl.cbClsExtra = 0; /* No extra bytes after the window class */
- wincl.cbWndExtra = 0; /* structure or the window instance */
- /* Use Windows's default colour as the background of the window */
- wincl.hbrBackground = (HBRUSH) GetStockObject(BLACK_BRUSH);
- /* Register the window class, and if it fails quit the program */
- if (!RegisterClassEx (&wincl))
- return 0;
- /* The class is registered, let's create the program*/
- hwnd = CreateWindowEx (
- 0, /* Extended possibilites for variation */
- szClassName, /* Classname */
- "DirectX Console", /* Title Text */
- WS_POPUP | WS_VISIBLE, /* default window */
- 0, /* Windows decides the position */
- 0, /* where the window ends up on the screen */
- SCREEN_WIDTH, /* The programs width */
- SCREEN_HEIGHT, /* and height in pixels */
- NULL, /* The window is a child-window to desktop */
- NULL, /* No menu */
- hinstance, /* Program Instance handler */
- NULL /* No Window Creation data */
- );
- // needed for when passing handle around functions
- main_window_handle = hwnd;
- // needed for when passing instance around functions
- app_instance = hinstance;
- /* Make the window visible on the screen */
- //ShowWindow (hwnd, nCmdShow);
- Game_Init();
- /* Run the message loop. It will run until GetMessage() returns 0 */
- while(true)
- {
- if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
- {
- if(msg.message == WM_QUIT)
- {
- std::cout << "Quit message recieved" << std::endl;
- break;
- }
- /* Translate virtual-key messages into character messages */
- TranslateMessage(&msg);
- /* Send message to WindowProcedure */
- DispatchMessage(&msg);
- }
- Game_Main();
- }
- std::cout << "Left main loop and quitting..." << std::endl;
- Game_Shutdown();
- /* The program return-value is 0 - The value that PostQuitMessage() gave */
- return msg.wParam;
- }
- /* This function is called by the Windows function DispatchMessage() */
- LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
- {
- HDC hdc;
- PAINTSTRUCT ps;
- //char bufferWP[80]; //for local printing
- switch (message) /* handle the messages */
- {
- case WM_PAINT:
- // simply validate the window
- hdc = BeginPaint(hwnd, &ps);
- // stuff here
- EndPaint(hwnd, &ps);
- break;
- case WM_CLOSE:
- std::cout << "Close signal recieved" << std::endl;
- break;
- case WM_DESTROY:
- std::cout << "Destroy signal recieved" << std::endl;
- PostQuitMessage (0); /* send a WM_QUIT to the message queue */
- break;
- default: /* for messages that we don't deal with */
- return DefWindowProc (hwnd, message, wParam, lParam);
- }
- return 0;
- }
- int Game_Init(void *parms, int num_parms)
- {
- // Instructions!
- // 1. Setup the Directx Object through createex
- // 2. Set the cooperation level between DX object and windows through SetCooperativeLevel
- // 3. Set the Display mode with SetDisplayMode
- // 4. Take your array of palette entries and fill them (can be omitted)
- // 5. Create palette with CreatePalette using palette entries and DDCAPS_INITIALIZE
- // (Don't used DDCAPS_INITIALIZE if you're not setting your own palette)
- // 6. Clear and then fill out DDSURFACEDESC2 with info about the surface
- // 7. Create surface using the DDSURFACEDESC2 settings and the new pointer of LPDIRECTDRAWSURFACE7
- // using CreateSurface
- // 8. Now combine new surface and palette with SelectPalette
- // The following is done in GameMain().
- // 9. clear the ddsd class and reuse it for locking the surface
- // 10. Set up aliases for lpitch and the surface to reduce casting
- // 11. Randomly plot some pixels
- // 12. Unlock the surface area
- //DirectX setup:
- HRESULT DDresult = (DirectDrawCreateEx(NULL, (void **)&lpdd7, IID_IDirectDraw7, NULL));
- switch(DDresult)
- {
- case DD_OK:
- std::cout << "DD7 initialised." << std::endl;
- break;
- case DDERR_DIRECTDRAWALREADYCREATED:
- std::cout << "DD already created." << std::endl;
- break;
- case DDERR_GENERIC:
- std::cout << "Unknown problem..." << std::endl;
- break;
- case DDERR_INVALIDDIRECTDRAWGUID:
- std::cout << "Unknown GUID." << std::endl;
- break;
- case DDERR_INVALIDPARAMS:
- std::cout << "Invalid paramters - please double check." << std::endl;
- break;
- case DDERR_NODIRECTDRAWHW:
- std::cout << "No DirectDraw capable hardware" << std::endl;
- break;
- case DDERR_OUTOFMEMORY:
- std::cout << "Out of memroy." << std::endl;
- break;
- default:
- std::cout << "Default error..." << std::endl;
- break;
- }
- // Set up the correct coop modes for windows
- if(SUCCEEDED(lpdd7->SetCooperativeLevel(main_window_handle, DDSCL_EXCLUSIVE |
- DDSCL_ALLOWREBOOT |
- DDSCL_FULLSCREEN)))
- {
- std::cout << "Cooperative levels set succesfully." << std::endl;
- }
- // set the video mode
- if(FAILED(lpdd7->SetDisplayMode(SCREEN_WIDTH, SCREEN_HEIGHT, BPP,0,0)))
- {
- std::cout << "Displaymode set correctly" << std::endl;
- }
- // manually fill our first element in palette
- palette[0].peRed = 0;
- palette[0].peGreen = 0;
- palette[0].peBlue = 0;
- palette[0].peFlags = PC_NOCOLLAPSE;
- //fill the palette with random numbers
- for(unsigned char colour = 1; colour < 255; colour++)
- {
- palette[colour].peRed = rand()%256;
- palette[colour].peGreen = rand()%256;
- palette[colour].peBlue = rand()%256;
- //Next part very important, stops windows and DX
- // optimizing palette for us
- palette[colour].peFlags = PC_NOCOLLAPSE;
- }
- // manually fill out last element in palette
- palette[255].peRed = 255;
- palette[255].peGreen = 255;
- palette[255].peBlue = 255;
- palette[255].peFlags = PC_NOCOLLAPSE;
- // initialise the palatte and pass the handle back
- if(SUCCEEDED(lpdd7->CreatePalette(DDPCAPS_8BIT | DDPCAPS_ALLOW256 | DDPCAPS_INITIALIZE, palette, &lpdd7pal, NULL)))
- std::cout << "Palette created and initialized: " << lpdd7pal << "." << std::endl;
- //clear out the memory first!
- memset(&ddsd, 0, sizeof(ddsd));
- // fill out the description for the surface
- ddsd.dwSize = sizeof(DDSURFACEDESC2);
- ddsd.dwFlags = DDSD_CAPS;
- ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
- // now create the surface with the above descriptor...
- if(SUCCEEDED(lpdd7->CreateSurface(&ddsd, &lpddsprimary, NULL)))
- std::cout << "Surface created." << std::endl;
- // ... and add the palette
- if(SUCCEEDED(lpddsprimary->SetPalette(lpdd7pal)))
- std::cout << "Palette succesfully added." << std::endl;
- return 0;
- }
- int Game_Main(void *parms, int num_parms)
- {
- if(MACKEYDOWN(VK_ESCAPE))
- SendMessage(main_window_handle, WM_CLOSE, 0, 0);
- DWORD timer = GetTickCount();
- static int counter = 0;
- counter++;
- //now that the surface is created, reuse the surface descriptor class
- memset(&ddsd, 0, sizeof(ddsd));
- ddsd.dwSize = sizeof(ddsd);
- if(FAILED(lpddsprimary->Lock(NULL, &ddsd, DDLOCK_SURFACEMEMORYPTR | DDLOCK_WAIT, NULL)))
- {
- std::cout << "Area lock failed at iteration " << counter << std::endl;
- Sleep(5);
- PostQuitMessage(0);
- return(0);
- }
- // aliases to stop having to cast.
- int mempitch = ddsd.lPitch;
- UCHAR *video_buffer = (UCHAR *)ddsd.lpSurface;
- // Unlock Screen
- if(FAILED(lpddsprimary->Unlock(NULL)))
- {
- std::cout << "Surface area unlock failed at iteration " << counter << std::endl;
- Sleep(5);
- PostQuitMessage(0);
- return(0);
- }
- Sleep(30);
- return 0;
- }
- int Game_Shutdown(void *parms, int num_parms)
- {
- if(lpdd7)
- {
- if(lpdd7pal)
- {
- lpdd7pal->Release();
- lpdd7pal = NULL;
- std::cout << "Palette destroyed" << std::endl;
- }
- if(lpddsprimary)
- {
- lpddsprimary->Release();
- lpddsprimary = NULL;
- std::cout << "Surface destroyed" << std::endl;
- }
- if(lpdd7)
- {
- lpdd7->Release();
- lpdd7 = NULL;
- std::cout << "Interface destroyed" << std::endl;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment