Stilllegung des Forums
Das Forum wurde am 05.06.2023 nach über 20 Jahren stillgelegt (weitere Informationen und ein kleiner Rückblick).
Registrierungen, Anmeldungen und Postings sind nicht mehr möglich. Öffentliche Inhalte sind weiterhin zugänglich.
Das Team von spieleprogrammierer.de bedankt sich bei der Community für die vielen schönen Jahre.
Wenn du eine deutschsprachige Spieleentwickler-Community suchst, schau doch mal im Discord und auf ZFX vorbei!
Werbeanzeige
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
// INCLUDES /////////////////////////////////////////////// #define WIN32_LEAN_AND_MEAN #define INITGUID #include <windows.h> // include important windows stuff #include <windowsx.h> #include <mmsystem.h> //#include <iostream.h> // include important C/C++ stuff #include <conio.h> #include <stdlib.h> #include <malloc.h> #include <memory.h> #include <string.h> #include <stdarg.h> #include <stdio.h> #include <math.h> #include <io.h> #include <fcntl.h> //Eigene Includes: #include "Engine.h" extern CEngine Engine; HWND main_window_handle = NULL; // save the window handle HINSTANCE main_instance = NULL; // save the instance char buffer[80]; /* Declare Windows procedure */ LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM); int WINAPI WinMain (HINSTANCE hThisInstance, HINSTANCE hPrevInstance, LPSTR lpszArgument, int nFunsterStil) { HWND hwnd; /* This is the handle for our window */ MSG messages; /* Here messages to the application are saved */ WNDCLASSEX wincl; /* Data structure for the windowclass */ /* The Window structure */ wincl.hInstance = hThisInstance; wincl.lpszClassName = WINDOW_CLASS_NAME; wincl.lpfnWndProc = WindowProcedure; /* This function is called by windows */ wincl.style = 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 color 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 */ WINDOW_CLASS_NAME, /* Classname */ WINDOW_CLASS_NAME, /* Title Text */ WS_POPUP, /* default window */ CW_USEDEFAULT, /* Windows decides the position */ CW_USEDEFAULT, /* where the window ends up on the screen */ WINDOW_WIDTH, /* The programs width */ WINDOW_HEIGHT, /* and height in pixels */ HWND_DESKTOP, /* The window is a child-window to desktop */ NULL, /* No menu */ hThisInstance, /* Program Instance handler */ NULL /* No Window Creation data */ ); /* Make the window visible on the screen */ ShowWindow (hwnd, nFunsterStil); UpdateWindow(hwnd); // save the window handle and instance in a global main_window_handle = hwnd; main_instance = hThisInstance; //Spiel starten: Engine.InitGame(); // enter main event loop while(1) { if (PeekMessage(&messages,NULL,0,0,PM_REMOVE)) { // test if this is a quit if (messages.message == WM_QUIT) break; // translate any accelerator keys TranslateMessage(&messages); // send the message to the window proc DispatchMessage(&messages); } // end if // main game processing goes here Engine.MainGame(); } // end while //Hier wird das Spiel herrunter gefahren: Engine.ShutdownGame(); /* The program return-value is 0 - The value that PostQuitMessage() gave */ return messages.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; switch (message) /* handle the messages */ { case WM_CREATE: break; case WM_PAINT: hdc=BeginPaint(hwnd,&ps); EndPaint(hwnd, &ps); break; case WM_DESTROY: 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; } |
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
#ifndef Engine_H #define Engine_H //Compilierer Includes: #include <windows.h> //Eigene Includes: #include "DirectX/ddraw.h" // DEFINES //////////////////////////////////////////////// // defines for windows #define WINDOW_CLASS_NAME "Game.exe" // class name #define WINDOW_WIDTH 800 // size of window #define WINDOW_HEIGHT 600 #define SCREEN_WIDTH 800 // size of screen #define SCREEN_HEIGHT 600 #define SCREEN_BPP 16 // bits per pixel // MACROS ///////////////////////////////////////////////// // these read the keyboard asynchronously #define KEY_DOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0) #define KEY_UP(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 0 : 1) // this builds a 16 bit color value in 5.5.5 format (1-bit alpha mode) #define _RGB16BIT555(r,g,b) ((b & 31) + ((g & 31) << 5) + ((r & 31) << 10)) // this builds a 16 bit color value in 5.6.5 format (green dominate mode) #define _RGB16BIT565(r,g,b) ((b & 31) + ((g & 63) << 5) + ((r & 31) << 11)) // TYPES ////////////////////////////////////////////////// typedef unsigned short USHORT; typedef unsigned short WORD; typedef unsigned char UCHAR; typedef unsigned char BYTE; class CEngine { public: //Konstruktor/Destruktor: CEngine(); ~CEngine(); //Spiel starten: void InitGame(); //Grafik Stuff: void InitDirectDraw(); void InitPrimarySurface(); void InitSecondarySurface(); //Tastatur(Eingaben)Stuff: void InitDirectInput(); //SoundStuff: void InitDirectSound(); //Hauptteil des Spiels: void MainGame(); //Spiel beenden: void ShutdownGame(); //Grafik Stuff: void ReleaseDirectDraw(); void ReleasePrimarySurface(); void ReleaseSecondarySurface(); //Tastatur(Eingaben)Stuff: void ReleaseDirectInput(); //SoundStuff: void ReleaseDirectSound(); private: protected: }; #endif //Engine_H |
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 |
#include "Engine.h" LPDIRECTDRAW7 lpdd = NULL; // dd object LPDIRECTDRAWSURFACE7 lpddsprimary = NULL; // dd primary surface LPDIRECTDRAWSURFACE7 lpddsback = NULL; // dd back surface LPDIRECTDRAWPALETTE lpddpal = NULL; // a pointer to the created dd palette DDSURFACEDESC2 ddsd; // a direct draw surface description struct DDSCAPS2 ddscaps; // a direct draw surface capabilities struct HRESULT ddrval; // result back from dd calls //Globales Engine Objekt: CEngine Engine; //Fenster Hanlder: extern HWND main_window_handle; extern HINSTANCE main_instance; extern char buffer[80]; //Konstruktor/Destruktor: CEngine::CEngine() { } CEngine::~CEngine() { } //Grafik Stuff: void CEngine::InitGame() { InitDirectDraw(); InitPrimarySurface(); InitSecondarySurface(); InitDirectSound(); InitDirectInput(); } void CEngine::InitDirectDraw() { // this function is where you do all the initialization // for your game // create object and test for error if (DirectDrawCreateEx(NULL, (void **)&lpdd, IID_IDirectDraw7, NULL)!=DD_OK) { MessageBox(main_window_handle, "Das DirectX Objekt konnte nicht erstellt werden!", "Fehler beim erstellen des DX Objekts", MB_OK); } // set cooperation level to windowed mode normal if ((lpdd->SetCooperativeLevel(main_window_handle, DDSCL_ALLOWMODEX | DDSCL_FULLSCREEN | DDSCL_EXCLUSIVE | DDSCL_ALLOWREBOOT))!=DD_OK) { MessageBox(main_window_handle, "Die Kooperationsebene konnte nicht gesetzt werden!", "Kooperationsfehler", MB_OK); } // set the display mode if ((lpdd->SetDisplayMode(SCREEN_WIDTH,SCREEN_HEIGHT,16,0,0))!=DD_OK) { MessageBox(main_window_handle, "Die Bildschrimgröße konnte nicht angepasst werden!", "Display Fehler", MB_OK); } } void CEngine::InitPrimarySurface() { // Create the primary surface memset(&ddsd,0,sizeof(ddsd)); ddsd.dwSize = sizeof(ddsd); // set the flags to validate both the capabilities // field and the backbuffer count field ddsd.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT; // we need to let dd know that we want a complex // flippable surface structure, set flags for that ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_FLIP | DDSCAPS_COMPLEX; // set the backbuffer count to 1 ddsd.dwBackBufferCount = 1; if (lpdd->CreateSurface(&ddsd,&lpddsprimary,NULL)!=DD_OK) { MessageBox(main_window_handle,"Die Primäroberfäche konnte nicht erstellt werden!", "Fehler beim erstellen der Primär Oberfläche",MB_OK); } // query for the backbuffer or secondary surface // notice the use of ddscaps to indicate what // we are requesting ddscaps.dwCaps = DDSCAPS_BACKBUFFER; // get the surface lpddsprimary->GetAttachedSurface(&ddscaps,&lpddsback); } void CEngine::InitSecondarySurface() { } //Tastatur(Eingaben)Stuff: void CEngine::InitDirectInput() { } //SoundStuff: void CEngine::InitDirectSound() { } //Spielteil: void CEngine::MainGame() { // this is the workhorse of your game it will be called // continuously in real-time this is like main() in C // all the calls for you game go here! USHORT *back_buffer = NULL, // used to draw *dest_ptr = NULL, // used in line by line copy *src_ptr = NULL; // " " // check of user is trying to exit if (KEY_DOWN(VK_ESCAPE) || KEY_DOWN(VK_SPACE)) PostMessage(main_window_handle, WM_DESTROY,0,0); // erase secondary back buffer memset(&ddsd,0,sizeof(ddsd)); ddsd.dwSize = sizeof(ddsd); // lock the secondary surface lpddsback->Lock(NULL,&ddsd, DDLOCK_SURFACEMEMORYPTR | DDLOCK_WAIT,NULL); // get video pointer to secondary surface back_buffer = (USHORT *)ddsd.lpSurface; // clear back buffer out, remember in 16bit mode there are // 2 bytes per pixel, so watch out for addressing and multiplying // issues in this code // linear memory if (ddsd.lPitch == SCREEN_WIDTH*2) memset(back_buffer,0,SCREEN_WIDTH*SCREEN_HEIGHT*(SCREEN_BPP/8)); else { // non-linear memory // make copy of video pointer dest_ptr = back_buffer; // clear out memory one line at a time for (int y=0; y < SCREEN_HEIGHT; y++) { // clear next line memset(dest_ptr,0,SCREEN_WIDTH*(SCREEN_BPP/8)); // advance pointer to next line dest_ptr+=(ddsd.lPitch/2); } // end for y } // end else // perform game logic... // draw the next frame into the secondary back buffer // plot 5000 random pixels for (int index = 0; index < 5000; index++) { int x = rand()%SCREEN_WIDTH; int y = rand()%SCREEN_HEIGHT; UCHAR red = rand()%256; UCHAR green = rand()%256; UCHAR blue = rand()%256; // back_buffer is a short pointer, BUT lpitch is in bytes // so divide by 2, to get number of "pixels" or words back_buffer[x+y*(ddsd.lPitch/2)] = _RGB16BIT565(red, green, blue); } // end for index // unlock secondary buffer lpddsback->Unlock(NULL); // flip pages while(lpddsprimary->Flip(NULL, DDFLIP_WAIT)!=DD_OK); // wait a sec Sleep(10); // return success } //Grafik Stuff: void CEngine::ShutdownGame() { ReleaseSecondarySurface(); ReleasePrimarySurface(); ReleaseDirectDraw(); } void CEngine::ReleaseDirectDraw() { // this function is where you shutdown your game and // release all resources that you allocated // release the directdraw object if (lpdd!=NULL) lpdd->Release(); } void CEngine::ReleasePrimarySurface() { // first release the primary surface if (lpddsprimary!=NULL) lpddsprimary->Release(); } void CEngine::ReleaseSecondarySurface() { // first release the secondary surface if(lpddsback!=NULL) lpddsback->Release(); } //Tastatur(Eingaben)Stuff: void CEngine::ReleaseDirectInput() { } //SoundStuff: void CEngine::ReleaseDirectSound() { } |
Anonymous
unregistriert
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
// Engine.h #ifndef _ENGINE_H_ #define _ENGINE_H_ #pragma once #include <windows.h> #include <ddraw.h> #define SCREEN_WIDTH 800 #define SCREEN_HEIGHT 600 #define SCREEN_BPP 16 #define KEY_DOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0) #define KEY_UP(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 0 : 1) #define _RGB16BIT555(r,g,b) ((b & 31) + ((g & 31) << 5) + ((r & 31) << 10)) #define _RGB16BIT565(r,g,b) ((b & 31) + ((g & 63) << 5) + ((r & 31) << 11)) class CEngine { public: CEngine(void); ~CEngine(void); public: bool InitGame(::HWND__* hWnd, ::HINSTANCE__* hInstance); void MainGame(void); void ShutdownGame(void); protected: bool InitDirectDraw(void); bool InitPrimarySurface(void); bool InitSecondarySurface(void); bool InitDirectInput(void); bool InitDirectSound(void); public: void ReleaseDirectDraw(void); void ReleasePrimarySurface(void); void ReleaseSecondarySurface(void); void ReleaseDirectInput(void); void ReleaseDirectSound(void); protected: LPDIRECTDRAW7 m_lpDD; LPDIRECTDRAWSURFACE7 m_lpDDSPrimary; LPDIRECTDRAWSURFACE7 m_lpDDSBack; LPDIRECTDRAWPALETTE m_lpDDPal; DDSURFACEDESC2 m_DDSD; DDSCAPS2 m_DDScaps; ::HWND__* m_hWnd; ::HINSTANCE__* m_hInstance; }; #endif |
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
// Engine.cpp #include "Engine.h" CEngine::CEngine(void) { m_lpDD = NULL; m_lpDDSPrimary = NULL; m_lpDDSBack = NULL; m_lpDDPal = NULL; } CEngine::~CEngine(void) { } bool CEngine::InitGame(::HWND__* hWnd, ::HINSTANCE__* hInstance) { if(!hWnd || !hInstance) return false; m_hWnd = hWnd; m_hInstance = hInstance; if(!InitDirectDraw()) return false; if(!InitPrimarySurface()) return false; if(!InitSecondarySurface()) return false; if(!InitDirectSound()) return false; if(!InitDirectInput()) return false; return true; } bool CEngine::InitDirectDraw(void) { if(DirectDrawCreateEx(NULL, (void **)&m_lpDD, IID_IDirectDraw7, NULL) != DD_OK) { ::MessageBox(m_hWnd, "Das DirectX Objekt konnte nicht erstellt werden!", "Fehler beim erstellen des DX Objekts", MB_OK | MB_ICONERROR); return false; } if((m_lpDD->SetCooperativeLevel(m_hWnd, DDSCL_ALLOWMODEX | DDSCL_FULLSCREEN | DDSCL_EXCLUSIVE | DDSCL_ALLOWREBOOT))!=DD_OK) { ::MessageBox(m_hWnd, "Die Kooperationsebene konnte nicht gesetzt werden!", "Kooperationsfehler", MB_OK | MB_ICONERROR); return false; } if((m_lpDD->SetDisplayMode(SCREEN_WIDTH, SCREEN_HEIGHT, 16, 0, 0)) != DD_OK) { ::MessageBox(m_hWnd, "Die Bildschrimgröße konnte nicht angepasst werden!", "Display Fehler", MB_OK | MB_ICONERROR); return false; } return true; } bool CEngine::InitPrimarySurface(void) { memset(&m_DDSD, 0, sizeof(DDSURFACEDESC2)); m_DDSD.dwSize = sizeof(DDSURFACEDESC2); m_DDSD.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT; m_DDSD.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_FLIP | DDSCAPS_COMPLEX; m_DDSD.dwBackBufferCount = 1; if(m_lpDD->CreateSurface(&m_DDSD, &m_lpDDSPrimary, NULL) != DD_OK) { ::MessageBox(m_hWnd, "Die Primäroberfäche konnte nicht erstellt werden!", "Fehler beim erstellen der Primär Oberfläche", MB_OK | MB_ICONERROR); return false; } m_DDScaps.dwCaps = DDSCAPS_BACKBUFFER; m_lpDDSPrimary->GetAttachedSurface(&m_DDScaps, &m_lpDDSBack); return true; } bool CEngine::InitSecondarySurface(void) { return true; } bool CEngine::InitDirectInput(void) { return true; } bool CEngine::InitDirectSound(void) { return true; } void CEngine::MainGame(void) { unsigned short *pusBackBuffer = NULL; unsigned short *pusDest = NULL; unsigned short *pusSrc = NULL; if(KEY_DOWN(VK_ESCAPE) || KEY_DOWN(VK_SPACE)) ::PostMessage(m_hWnd, WM_DESTROY, 0, 0); memset(&m_DDSD, 0, sizeof(DDSURFACEDESC2)); m_DDSD.dwSize = sizeof(DDSURFACEDESC2); m_lpDDSBack->Lock(NULL, &m_DDSD, DDLOCK_SURFACEMEMORYPTR | DDLOCK_WAIT, NULL); pusBackBuffer = (unsigned short*)m_DDSD.lpSurface; if(m_DDSD.lPitch == SCREEN_WIDTH*2) memset(pusBackBuffer, 0, SCREEN_WIDTH*SCREEN_HEIGHT*(SCREEN_BPP/8)); else { pusDest = pusBackBuffer; for(unsigned int y = 0; y < (unsigned int)SCREEN_HEIGHT; y++) { memset(pusDest, 0, SCREEN_WIDTH*(SCREEN_BPP/8)); pusDest += (m_DDSD.lPitch/2); } } for (unsigned int uiIndex = 0; uiIndex < 5000; uiIndex++) { int x = rand()%SCREEN_WIDTH; int y = rand()%SCREEN_HEIGHT; WORD wRed = rand()%256; WORD wGreen = rand()%256; WORD wBlue = rand()%256; pusBackBuffer[x+y*(m_DDSD.lPitch/2)] = _RGB16BIT565(wRed, wGreen, wBlue); } m_lpDDSBack->Unlock(NULL); while(m_lpDDSPrimary->Flip(NULL, DDFLIP_WAIT) != DD_OK); } void CEngine::ShutdownGame(void) { ReleaseSecondarySurface(); ReleasePrimarySurface(); ReleaseDirectDraw(); } void CEngine::ReleaseDirectDraw(void) { if(m_lpDD) { m_lpDD->Release(); m_lpDD = NULL; } } void CEngine::ReleasePrimarySurface(void) { if(m_lpDDSPrimary) { m_lpDDSPrimary->Release(); m_lpDDSPrimary = NULL; } } void CEngine::ReleaseSecondarySurface(void) { if(m_lpDDSBack) { m_lpDDSBack->Release(); m_lpDDSBack = NULL; } } void CEngine::ReleaseDirectInput(void) { } void CEngine::ReleaseDirectSound(void) { } |
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
// Main.cpp #define WIN32_LEAN_AND_MEAN #include <windows.h> #include <mmsystem.h> #include <stdlib.h> #include <malloc.h> #include <memory.h> #include <tchar.h> #include "Engine.h" #define WINDOW_WIDTH 800 #define WINDOW_HEIGHT 600 long __stdcall WndProc(::HWND__*, unsigned int, unsigned int, long); int __stdcall WinMain (::HINSTANCE__* hInstance, ::HINSTANCE__* hPrevInstance, LPTSTR lpszArgument, int iCmdShow) { ::HWND__* hWnd; ::MSG msg; ::WNDCLASSEX wincl; wincl.hInstance = hInstance; wincl.lpszClassName = "Engine"; wincl.lpfnWndProc = WndProc; wincl.style = CS_DBLCLKS; wincl.cbSize = sizeof (WNDCLASSEX); wincl.hIcon = LoadIcon(NULL, IDI_APPLICATION); wincl.hIconSm = LoadIcon(NULL, IDI_APPLICATION); wincl.hCursor = LoadCursor(NULL, IDC_ARROW); wincl.lpszMenuName = NULL; wincl.cbClsExtra = 0; wincl.cbWndExtra = 0; wincl.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH); if(!RegisterClassEx(&wincl)) return 0; hWnd = CreateWindowEx(0, "Engine", "Engine Testanwendung 1.0", WS_POPUP, CW_USEDEFAULT, CW_USEDEFAULT, WINDOW_WIDTH, WINDOW_HEIGHT, NULL, NULL, hInstance, NULL); ShowWindow(hWnd, iCmdShow); UpdateWindow(hWnd); CEngine Engine; Engine.InitGame(hWnd, hInstance); while(true) { if(PeekMessage(&msg,NULL,0,0,PM_REMOVE) == TRUE) { if(msg.message == WM_QUIT) break; TranslateMessage(&msg); DispatchMessage(&msg); } Engine.MainGame(); } Engine.ShutdownGame(); return (int)msg.wParam; } long CALLBACK WndProc(::HWND__* hWnd, unsigned int uiMsg, unsigned int wParam, long lParam) { ::HDC__* hDC; ::PAINTSTRUCT ps; switch(uiMsg) { case WM_CREATE: break; case WM_PAINT: hDC = BeginPaint(hWnd,&ps); EndPaint(hWnd, &ps); break; case WM_DESTROY: PostQuitMessage(0); break; default: return (long)DefWindowProc(hWnd, uiMsg, wParam, lParam); } return 0; } |
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
// INCLUDES /////////////////////////////////////////////// #define WIN32_LEAN_AND_MEAN #define INITGUID #include <windows.h> /* #include <windowsx.h> #include <mmsystem.h> #include <iostream.h> #include <conio.h> #include <stdlib.h> #include <malloc.h> #include <memory.h> #include <string.h> #include <stdarg.h> #include <stdio.h> #include <math.h> #include <io.h> #include <fcntl.h> */ //Eigene Includes: #include "Engine.h" extern CEngine Engine; HWND main_window_handle = NULL; HINSTANCE main_instance = NULL; char buffer[80]; LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM); int WINAPI WinMain (HINSTANCE hThisInstance, HINSTANCE hPrevInstance, LPSTR lpszArgument, int nFunsterStil) { HWND hwnd; MSG messages; WNDCLASSEX wincl; wincl.hInstance = hThisInstance; wincl.lpszClassName = WINDOW_CLASS_NAME; wincl.lpfnWndProc = WindowProcedure; wincl.style = CS_DBLCLKS; wincl.cbSize = sizeof (WNDCLASSEX); wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION); wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION); wincl.hCursor = LoadCursor (NULL, IDC_ARROW); wincl.lpszMenuName = NULL; wincl.cbClsExtra = 0; wincl.cbWndExtra = 0; wincl.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH); if (!RegisterClassEx (&wincl)) return 0; hwnd = CreateWindowEx ( 0, WINDOW_CLASS_NAME, WINDOW_CLASS_NAME, WS_POPUP, CW_USEDEFAULT, CW_USEDEFAULT, WINDOW_WIDTH, WINDOW_HEIGHT, NULL, NULL, hThisInstance, NULL ); ShowWindow (hwnd, nFunsterStil); UpdateWindow(hwnd); main_window_handle = hwnd; main_instance = hThisInstance; //Spiel starten: Engine.InitGame(); // Spielschleife: while(1) { if (PeekMessage(&messages,NULL,0,0,PM_REMOVE)) { if (messages.message == WM_QUIT) break; TranslateMessage(&messages); DispatchMessage(&messages); } //Hier läuft das Spiel ab: Engine.MainGame(); } //Hier wird das Spiel herrunter gefahren: Engine.ShutdownGame(); return messages.wParam; } LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { HDC hdc; PAINTSTRUCT ps; switch (message) { case WM_CREATE: break; case WM_PAINT: hdc=BeginPaint(hwnd,&ps); EndPaint(hwnd, &ps); break; case WM_DESTROY: PostQuitMessage (0); break; default: return DefWindowProc (hwnd, message, wParam, lParam); } return 0; } |
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
#ifndef Engine_H #define Engine_H //Compilierer Includes: #include <windows.h> //Eigene Includes: #include "DirectX/ddraw.h" // DEFINES //////////////////////////////////////////////// // defines for windows const char WINDOW_CLASS_NAME[20]="Game"; // class name const unsigned long WINDOW_WIDTH=800; // size of window const unsigned long WINDOW_HEIGHT=600; const unsigned long SCREEN_WIDTH=800; // size of screen const unsigned long SCREEN_HEIGHT=600; const unsigned long SCREEN_BPP=16; // bits per pixel // MACROS ///////////////////////////////////////////////// // these read the keyboard asynchronously #define KEY_DOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0) #define KEY_UP(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 0 : 1) // this builds a 16 bit color value in 5.5.5 format (1-bit alpha mode) #define _RGB16BIT555(r,g,b) ((b & 31) + ((g & 31) << 5) + ((r & 31) << 10)) // this builds a 16 bit color value in 5.6.5 format (green dominate mode) #define _RGB16BIT565(r,g,b) ((b & 31) + ((g & 63) << 5) + ((r & 31) << 11)) // TYPES ////////////////////////////////////////////////// /* typedef unsigned short USHORT; typedef unsigned short WORD; typedef unsigned char UCHAR; typedef unsigned char BYTE; */ class CEngine { public: //Konstruktor/Destruktor: CEngine(); ~CEngine(); //Spiel starten: void InitGame(); //Grafik Stuff: void InitDirectDraw(); void InitPrimarySurface(); void InitSecondarySurface(); //Tastatur(Eingaben)Stuff: void InitDirectInput(); //SoundStuff: void InitDirectSound(); //Hauptteil des Spiels: void MainGame(); //Spiel beenden: void ShutdownGame(); //Grafik Stuff: void ReleaseDirectDraw(); void ReleasePrimarySurface(); void ReleaseSecondarySurface(); //Tastatur(Eingaben)Stuff: void ReleaseDirectInput(); //SoundStuff: void ReleaseDirectSound(); private: //DirectX Stuff: LPDIRECTDRAW7 lpdd; // dd object LPDIRECTDRAWSURFACE7 lpddsprimary; // dd primary surface LPDIRECTDRAWSURFACE7 lpddsback; // dd back surface LPDIRECTDRAWPALETTE lpddpal; // a pointer to the created dd palette DDSURFACEDESC2 ddsd; // a direct draw surface description struct DDSCAPS2 ddscaps; // a direct draw surface capabilities struct HRESULT ddrval; // result back from dd calls }; #endif //Engine_H |
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 |
#include "Engine.h" //Globales Engine Objekt: CEngine Engine; //Fenster Hanlder: extern HWND main_window_handle; extern HINSTANCE main_instance; extern char buffer[80]; //Konstruktor/Destruktor: CEngine::CEngine() { lpdd = NULL; // dd object lpddsprimary = NULL; // dd primary surface lpddsback = NULL; // dd back surface lpddpal = NULL; // a pointer to the created dd palette } CEngine::~CEngine() { } //Grafik Stuff: void CEngine::InitGame() { InitDirectDraw(); InitPrimarySurface(); InitSecondarySurface(); InitDirectSound(); InitDirectInput(); } void CEngine::InitDirectDraw() { // this function is where you do all the initialization // for your game // create object and test for error if (DirectDrawCreateEx(NULL, (void **)&lpdd, IID_IDirectDraw7, NULL)!=DD_OK) { MessageBox(main_window_handle, "Das DirectX Objekt konnte nicht erstellt werden!", "Fehler beim erstellen des DX Objekts", MB_OK); } // set cooperation level to windowed mode normal if ((lpdd->SetCooperativeLevel(main_window_handle, DDSCL_ALLOWMODEX | DDSCL_FULLSCREEN | DDSCL_EXCLUSIVE | DDSCL_ALLOWREBOOT))!=DD_OK) { MessageBox(main_window_handle, "Die Kooperationsebene konnte nicht gesetzt werden!", "Kooperationsfehler", MB_OK); PostMessage(main_window_handle, WM_DESTROY,0,0); } // set the display mode if ((lpdd->SetDisplayMode(SCREEN_WIDTH,SCREEN_HEIGHT,16,0,0))!=DD_OK) { MessageBox(main_window_handle, "Die Bildschrimgröße konnte nicht angepasst werden!", "Display Fehler", MB_OK); PostMessage(main_window_handle, WM_DESTROY,0,0); } } void CEngine::InitPrimarySurface() { // Create the primary surface memset(&ddsd,0,sizeof(ddsd)); ddsd.dwSize = sizeof(ddsd); // set the flags to validate both the capabilities // field and the backbuffer count field ddsd.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT; // we need to let dd know that we want a complex // flippable surface structure, set flags for that ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_FLIP | DDSCAPS_COMPLEX; // set the backbuffer count to 1 ddsd.dwBackBufferCount = 1; if (lpdd->CreateSurface(&ddsd,&lpddsprimary,NULL)!=DD_OK) { MessageBox(main_window_handle,"Die Primäroberfäche konnte nicht erstellt werden!", "Fehler beim erstellen der Primär Oberfläche",MB_OK); PostMessage(main_window_handle, WM_DESTROY,0,0); } // query for the backbuffer or secondary surface // notice the use of ddscaps to indicate what // we are requesting ddscaps.dwCaps = DDSCAPS_BACKBUFFER; // get the surface lpddsprimary->GetAttachedSurface(&ddscaps,&lpddsback); } void CEngine::InitSecondarySurface() { } //Tastatur(Eingaben)Stuff: void CEngine::InitDirectInput() { } //SoundStuff: void CEngine::InitDirectSound() { } //Spielteil: void CEngine::MainGame() { // this is the workhorse of your game it will be called // continuously in real-time this is like main() in C // all the calls for you game go here! USHORT *back_buffer = NULL, // used to draw *dest_ptr = NULL, // used in line by line copy *src_ptr = NULL; // " " // check of user is trying to exit if (KEY_DOWN(VK_ESCAPE) || KEY_DOWN(VK_SPACE)) PostMessage(main_window_handle, WM_DESTROY,0,0); // erase secondary back buffer memset(&ddsd,0,sizeof(ddsd)); ddsd.dwSize = sizeof(ddsd); // lock the secondary surface lpddsback->Lock(NULL,&ddsd, DDLOCK_SURFACEMEMORYPTR | DDLOCK_WAIT,NULL); // get video pointer to secondary surface back_buffer = (USHORT *)ddsd.lpSurface; // clear back buffer out, remember in 16bit mode there are // 2 bytes per pixel, so watch out for addressing and multiplying // issues in this code // linear memory if (ddsd.lPitch == SCREEN_WIDTH*2) memset(back_buffer,0,SCREEN_WIDTH*SCREEN_HEIGHT*(SCREEN_BPP/8)); else { // non-linear memory // make copy of video pointer dest_ptr = back_buffer; // clear out memory one line at a time for (int y=0; y < SCREEN_HEIGHT; y++) { // clear next line memset(dest_ptr,0,SCREEN_WIDTH*(SCREEN_BPP/8)); // advance pointer to next line dest_ptr+=(ddsd.lPitch/2); } // end for y } // end else // perform game logic... // draw the next frame into the secondary back buffer // plot 5000 random pixels for (unsigned int index = 0; index < 5000; index++) { int x = rand()%SCREEN_WIDTH; int y = rand()%SCREEN_HEIGHT; UCHAR red = rand()%256; UCHAR green = rand()%256; UCHAR blue = rand()%256; // back_buffer is a short pointer, BUT lpitch is in bytes // so divide by 2, to get number of "pixels" or words back_buffer[x+y*(ddsd.lPitch/2)] = _RGB16BIT565(red, green, blue); } // end for index // unlock secondary buffer lpddsback->Unlock(NULL); // flip pages if(lpddsprimary->Flip(NULL, DDFLIP_WAIT)!=DD_OK) { MessageBox(main_window_handle,"Es gab einen Fehler beim flippen!", "Flipping Fehler!",MB_OK); PostMessage(main_window_handle, WM_DESTROY,0,0); } } //Grafik Stuff: void CEngine::ShutdownGame() { ReleaseSecondarySurface(); ReleasePrimarySurface(); ReleaseDirectDraw(); ReleaseDirectInput(); ReleaseDirectSound(); } void CEngine::ReleaseDirectDraw() { // this function is where you shutdown your game and // release all resources that you allocated // release the directdraw object if (lpdd!=NULL) lpdd->Release(); } void CEngine::ReleasePrimarySurface() { // first release the primary surface if (lpddsprimary!=NULL) lpddsprimary->Release(); } void CEngine::ReleaseSecondarySurface() { // first release the secondary surface if(lpddsback!=NULL) lpddsback->Release(); } //Tastatur(Eingaben)Stuff: void CEngine::ReleaseDirectInput() { } //SoundStuff: void CEngine::ReleaseDirectSound() { } |
C-/C++-Quelltext |
|
1 2 3 4 5 6 |
::HWND__* ::HINSTANCE__* //Und nicht einfach: HWND HINSTANCE |
Werbeanzeige